View Issue Details

IDProjectCategoryView StatusLast Update
0012171GNUnetutil librarypublic2026-11-18 11:31
Reporterbot-cg [agent] Assigned Toschanzen  
PrioritynormalSeveritymajorReproducibilityalways
Status resolvedResolutionfixed 
Product VersionGit master 
Target Version0.30.0Fixed in Version0.30.0 
Summary0012171: GNUNET_CONFIGURATION_deserialize uses a stale line length: out-of-bounds access, and section headers with whitespace or CRLF are rejected
DescriptionGNUNET_CONFIGURATION_deserialize computes line_size from the raw buffer and keeps using it after GNUNET_strndup has stopped at a NUL byte in the line, and after leading and trailing whitespace has been stripped.

With a NUL byte in a line, the loop that turns tabs into spaces and the trailing-whitespace loop index up to line_size in a shorter allocation, so they read, and may write, out of bounds.

The section header test ']' == line[line_size - 1] uses the length from before stripping, relative to the pointer after the leading whitespace. With two or more blanks before '[' it reads past the allocation, and whenever whitespace was stripped it looks at the wrong byte. So " [a]", "[a] " and the "[a]\r" of a file with CRLF line endings are rejected with "Syntax error while deserializing in line 1", although the whitespace is stripped precisely so that such lines work.

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 '[a]\nX = 1\n [b]\n' > lead.conf
valgrind gnunet-config -n -c lead.conf -s a
  Invalid read of size 1 at GNUNET_CONFIGURATION_deserialize (configuration.c:1018)

printf '[a]\nX = 1\000\t\t\t\t \n' > nul.conf
valgrind gnunet-config -n -c nul.conf -s a
  Invalid read of size 1 at GNUNET_CONFIGURATION_deserialize (configuration.c:880 and 882)

printf '[a]\r\nX = 1\r\n' > crlf.conf
gnunet-config -n -c crlf.conf -s a
  WARNING Syntax error while deserializing in line 1
Additional InformationThe attached patch rejects a line that contains a NUL byte ("NUL byte in line %u") and recomputes line_size after stripping. That changes behaviour: section headers with surrounding whitespace, and CRLF files, are now accepted, and a line such as " [a=b]", which used to set option "[a" to "b]" in the current section, now starts section "a=b". No .conf file in the gnunet, exchange, merchant, anastasis, sync, donau or challenger trees has a section header with surrounding whitespace or CRLF line endings.
TagsNo tags attached.
Attached Files
0005-util-use-the-stripped-line-length-when-parsing-confi.patch (4,393 bytes)   
From 1b8d3b2a827c47ddae8dba7f40f23e011dde09d0 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 11:17:52 +0100
Subject: [PATCH 5/7] util: use the stripped line length when parsing
 configurations

GNUNET_CONFIGURATION_deserialize kept using the line length from before
whitespace was stripped and before GNUNET_strndup stopped at a NUL byte, so
it read and wrote out of bounds and rejected section headers with
surrounding whitespace, including CRLF line endings, which are now
accepted.  A NUL byte in a line is now an error.

Fixes #12171
Assisted by LLM
---
 src/lib/util/configuration.c      |  9 ++++
 src/lib/util/test_configuration.c | 70 +++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)

diff --git a/src/lib/util/configuration.c b/src/lib/util/configuration.c
index 8e318d192..3a1d51314 100644
--- a/src/lib/util/configuration.c
+++ b/src/lib/util/configuration.c
@@ -873,6 +873,14 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
     line = line_orig;
     /* increment line number */
     nr++;
+    if (strlen (line_orig) != line_size)
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           "NUL byte in line %u\n",
+           nr);
+      ret = GNUNET_SYSERR;
+      break;
+    }
     /* tabs and '\r' are whitespace */
     emptyline = GNUNET_YES;
     for (size_t i = 0; i < line_size; i++)
@@ -897,6 +905,7 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
     /* remove leading whitespace */
     for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++)
       ;
+    line_size = strlen (line);
 
     /* ignore comments */
     if ( ('#' == line[0]) ||
diff --git a/src/lib/util/test_configuration.c b/src/lib/util/test_configuration.c
index 6303026fa..b340373de 100644
--- a/src/lib/util/test_configuration.c
+++ b/src/lib/util/test_configuration.c
@@ -1108,6 +1108,75 @@ testSerializeInaccessible (void)
 }
 
 
+/**
+ * Check that a section header line in @a data is accepted.
+ */
+static int
+check_section_header (const char *data,
+                      size_t size)
+{
+  struct GNUNET_CONFIGURATION_Handle *pcfg;
+  char *v = NULL;
+  int ret = 0;
+
+  pcfg = GNUNET_CONFIGURATION_create (GNUNET_OS_project_data_gnunet ());
+  if ( (GNUNET_OK !=
+        GNUNET_CONFIGURATION_deserialize (pcfg,
+                                          data,
+                                          size,
+                                          NULL)) ||
+       (GNUNET_OK !=
+        GNUNET_CONFIGURATION_get_value_string (pcfg,
+                                               "b",
+                                               "Y",
+                                               &v)) ||
+       (0 != strcmp (v,
+                     "2")) )
+  {
+    fprintf (stderr,
+             "Failed to parse section header in `%s'\n",
+             data);
+    ret = 1;
+  }
+  GNUNET_free (v);
+  GNUNET_CONFIGURATION_destroy (pcfg);
+  return ret;
+}
+
+
+/**
+ * Test parsing lines with whitespace around section headers and with
+ * NUL bytes.
+ */
+static int
+testParseLines (void)
+{
+  static const char nul[] = "[a]\nX = 1\0\t\t\t\t\n";
+  struct GNUNET_CONFIGURATION_Handle *pcfg;
+  int ret = 0;
+
+  ret += check_section_header ("[a]\nX = 1\n  [b]\nY = 2\n",
+                               strlen ("[a]\nX = 1\n  [b]\nY = 2\n"));
+  ret += check_section_header ("[b] \nY = 2\n",
+                               strlen ("[b] \nY = 2\n"));
+  ret += check_section_header ("[b]\r\nY = 2\r\n",
+                               strlen ("[b]\r\nY = 2\r\n"));
+  pcfg = GNUNET_CONFIGURATION_create (GNUNET_OS_project_data_gnunet ());
+  if (GNUNET_SYSERR !=
+      GNUNET_CONFIGURATION_deserialize (pcfg,
+                                        nul,
+                                        sizeof (nul) - 1,
+                                        NULL))
+  {
+    fprintf (stderr,
+             "Accepted a NUL byte in a line\n");
+    ret++;
+  }
+  GNUNET_CONFIGURATION_destroy (pcfg);
+  return ret;
+}
+
+
 int
 main (int argc, char *argv[])
 {
@@ -1121,6 +1190,7 @@ main (int argc, char *argv[])
   failureCount += testExpandEmpty ();
   failureCount += testExpandRecursive ();
   failureCount += testSerializeInaccessible ();
+  failureCount += testParseLines ();
   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:31

manager   ~0030570

Applied as 21956e2431991240abc118cdb514ed2eb3a0347a

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: 0005-util-use-the-stripped-line-length-when-parsing-confi.patch
2026-11-18 11:06schanzenAssigned To => schanzen
2026-11-18 11:06schanzenStatusnew => assigned
2026-11-18 11:06schanzenTarget Version => 0.30.0
2026-11-18 11:31schanzenStatusassigned => resolved
2026-11-18 11:31schanzenResolutionopen => fixed
2026-11-18 11:31schanzenFixed in Version => 0.30.0
2026-11-18 11:31schanzenNote Added: 0030570