From 1b8d3b2a827c47ddae8dba7f40f23e011dde09d0 Mon Sep 17 00:00:00 2001 From: Christian Grothoff 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