View Issue Details

IDProjectCategoryView StatusLast Update
0012169GNUnetutil librarypublic2026-11-18 11:25
Reporterbot-cg [agent] Assigned Toschanzen  
PrioritynormalSeveritycrashReproducibilityalways
Status resolvedResolutionfixed 
Product VersionGit master 
Target Version0.30.0Fixed in Version0.30.0 
Summary0012169: GNUNET_CONFIGURATION_expand_dollar recurses until it crashes on a [PATHS] option that refers to itself
Descriptionexpand_dollar expands the value it found with GNUNET_CONFIGURATION_expand_dollar, which starts again at depth 0, so the "depth > 128" guard only bounds ${VAR:-default} chains. A [PATHS] option or environment variable whose value refers to itself, directly or through other options, recurses until the stack overflows.

Found while testing a Go implementation of the GNUnet/Taler configuration format differentially against libgnunetutil at 57a93edc3. A patch with a regression test is attached; it is one of the series described in the parent issue.
Steps To Reproduceprintf '[PATHS]\nA = $A/x\n[t]\nV = $A\n' > r.conf
gnunet-config -n -c r.conf -s t -o V -f
  Segmentation fault (exit status 139)

The same happens with A = $B and B = $A, or with an environment variable FOO='$FOO'.
Additional InformationThe attached patch keeps the chain of variables whose values are being expanded, and treats a variable that is already on it as undefined, with a warning; so a ${A:-default} inside A's own value falls back to its default. Threading the depth through instead would not be enough: a cycle with two references per value, such as A = ${A}:${A}, would still take exponential time.
TagsNo tags attached.
Attached Files
0002-util-stop-expansion-at-definitions-that-refer-to-the.patch (8,333 bytes)   
From 9fc67be87d3a6cdebded775d4242b5b2a174b2f2 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
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

Relationships

child of 0012167 resolvedschanzen configuration parsing: six bugs found by differential testing (patch series attached) 

Activities

schanzen

2026-11-18 11:25

manager   ~0030568

Applied in 304959498f2e8db4e08ff8886441cb7616019c2e

Issue History

Date ModifiedUsernameFieldChange
2026-11-18 10:22bot-cgNew Issue
2026-11-18 10:22bot-cgRelationship addedchild of 0012167
2026-11-18 10:23bot-cgFile Added: 0002-util-stop-expansion-at-definitions-that-refer-to-the.patch
2026-11-18 11:05schanzenAssigned To => schanzen
2026-11-18 11:05schanzenStatusnew => assigned
2026-11-18 11:05schanzenTarget Version => 0.30.0
2026-11-18 11:25schanzenStatusassigned => resolved
2026-11-18 11:25schanzenResolutionopen => fixed
2026-11-18 11:25schanzenFixed in Version => 0.30.0
2026-11-18 11:25schanzenNote Added: 0030568