From 9fc67be87d3a6cdebded775d4242b5b2a174b2f2 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 18 Nov 2026 11:16:20 +0100 Subject: [PATCH 2/7] util: stop $-expansion at definitions that refer to themselves A [PATHS] option or environment variable whose value refers to itself, directly or through others, recursed until the stack overflowed; the depth limit only covered ${VAR:-default} chains. Fixes #12169 Assisted by LLM --- src/lib/util/configuration.c | 94 ++++++++++++++++++++++++++----- src/lib/util/test_configuration.c | 47 ++++++++++++++++ 2 files changed, 128 insertions(+), 13 deletions(-) diff --git a/src/lib/util/configuration.c b/src/lib/util/configuration.c index 7ecf76044..b16f4ccaa 100644 --- a/src/lib/util/configuration.c +++ b/src/lib/util/configuration.c @@ -2680,6 +2680,30 @@ GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, } +/** + * A [PATHS] option or environment variable whose value is being + * $-expanded, used to detect definitions that refer to themselves. + */ +struct ExpansionFrame +{ + /** + * The expansion this one is part of, or NULL. + */ + const struct ExpansionFrame *parent; + + /** + * Name of the variable whose value is being expanded. + */ + const char *name; +}; + + +static char * +expand_all (const struct GNUNET_CONFIGURATION_Handle *cfg, + char *orig, + const struct ExpansionFrame *frame); + + /** * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR" * where either in the "PATHS" section or the environment "FOO" is @@ -2693,6 +2717,8 @@ GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, * @param cfg configuration to use for path expansion * @param orig string to $-expand (will be freed!) * @param depth recursion depth, used to detect recursive expansions + * @param frame variables whose values are being expanded, a variable + * among them is treated as undefined * @param[out] skip set to the length of the expanded term at the start * of the result, or to 1 if the term was not expanded, so that the * caller can continue after it @@ -2702,6 +2728,7 @@ static char * expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, char *orig, unsigned int depth, + const struct ExpansionFrame *frame, size_t *skip) { char *prefix; @@ -2716,6 +2743,8 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, char *erased_pos; size_t len; size_t def_skip; + bool recursive = false; + struct ExpansionFrame inner; *skip = 1; if (NULL == orig) @@ -2809,18 +2838,35 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, start, post, def); - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, - "PATHS", - start, - &prefix)) + for (const struct ExpansionFrame *pos = frame; + NULL != pos; + pos = pos->parent) + { + if (0 == strcmp (pos->name, + start)) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + "The definition of `%s' refers to itself, not expanding it\n", + start); + recursive = true; + break; + } + } + if (recursive || + (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_string (cfg, + "PATHS", + start, + &prefix))) { - if (NULL == (env = getenv (start))) + if (recursive || + (NULL == (env = getenv (start)))) { /* try default */ def = expand_dollar (cfg, def, depth + 1, + frame, &def_skip); env = def; } @@ -2838,8 +2884,11 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, } prefix = GNUNET_strdup (env); } - prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, - prefix); + inner.parent = frame; + inner.name = start; + prefix = expand_all (cfg, + prefix, + &inner); if ((erased_pos) && ('}' != erased_char)) { len = strlen (prefix) + 1; @@ -2858,10 +2907,18 @@ expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, } -char * -GNUNET_CONFIGURATION_expand_dollar ( - const struct GNUNET_CONFIGURATION_Handle *cfg, - char *orig) +/** + * $-expand all terms in a string. + * + * @param cfg configuration to use for path expansion + * @param orig string to $-expand (will be freed!) + * @param frame variables whose values are being expanded + * @return $-expanded string + */ +static char * +expand_all (const struct GNUNET_CONFIGURATION_Handle *cfg, + char *orig, + const struct ExpansionFrame *frame) { char *dup; size_t i; @@ -2877,7 +2934,7 @@ GNUNET_CONFIGURATION_expand_dollar ( continue; } dup = GNUNET_strdup (orig + i); - dup = expand_dollar (cfg, dup, 0, &skip); + dup = expand_dollar (cfg, dup, 0, frame, &skip); GNUNET_assert (NULL != dup); /* make compiler happy */ len = strlen (dup) + 1; orig = GNUNET_realloc (orig, i + len); @@ -2891,6 +2948,17 @@ GNUNET_CONFIGURATION_expand_dollar ( } +char * +GNUNET_CONFIGURATION_expand_dollar ( + const struct GNUNET_CONFIGURATION_Handle *cfg, + char *orig) +{ + return expand_all (cfg, + orig, + NULL); +} + + enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename ( const struct GNUNET_CONFIGURATION_Handle *cfg, diff --git a/src/lib/util/test_configuration.c b/src/lib/util/test_configuration.c index 12067a91f..28087ab05 100644 --- a/src/lib/util/test_configuration.c +++ b/src/lib/util/test_configuration.c @@ -1015,6 +1015,52 @@ testExpandEmpty (void) } +/** + * Test $-expansion of definitions that refer to themselves. + */ +static int +testExpandRecursive (void) +{ + struct GNUNET_CONFIGURATION_Handle *ecfg; + int ret = 0; + + ecfg = GNUNET_CONFIGURATION_create (GNUNET_OS_project_data_gnunet ()); + GNUNET_CONFIGURATION_set_value_string (ecfg, + "PATHS", + "SELF", + "$SELF/x"); + GNUNET_CONFIGURATION_set_value_string (ecfg, + "PATHS", + "A", + "$B/a"); + GNUNET_CONFIGURATION_set_value_string (ecfg, + "PATHS", + "B", + "$A/b"); + GNUNET_CONFIGURATION_set_value_string (ecfg, + "PATHS", + "D", + "${D:-dflt}/d"); + GNUNET_CONFIGURATION_set_value_string (ecfg, + "PATHS", + "TWICE", + "${TWICE}:${TWICE}"); + GNUNET_assert (0 == setenv ("GNUNET_TEST_SELF_ENV", + "$GNUNET_TEST_SELF_ENV/e", + 1)); + ret += check_expansion (ecfg, "$SELF", "$SELF/x"); + ret += check_expansion (ecfg, "$A", "$A/b/a"); + ret += check_expansion (ecfg, "$D", "dflt/d"); + ret += check_expansion (ecfg, "$TWICE", "${TWICE}:${TWICE}"); + ret += check_expansion (ecfg, + "$GNUNET_TEST_SELF_ENV", + "$GNUNET_TEST_SELF_ENV/e"); + GNUNET_assert (0 == unsetenv ("GNUNET_TEST_SELF_ENV")); + GNUNET_CONFIGURATION_destroy (ecfg); + return ret; +} + + int main (int argc, char *argv[]) { @@ -1026,6 +1072,7 @@ main (int argc, char *argv[]) failureCount += testDiagnosticsInlineSecret (); failureCount += testClean (); failureCount += testExpandEmpty (); + failureCount += testExpandRecursive (); if (failureCount > 0) goto error; -- 2.47.3