[PATCH] stdio-common: Fix double null terminator in wide scanf %s and %[ (BZ 34989, CVE-2026-115819)
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Wed Nov 11 17:09:28 GMT 2026
On 11/11/26 13:29, Florian Weimer wrote:
> * Adhemerval Zanella:
>
>> diff --git a/stdio-common/tst-wscanf-nul.c b/stdio-common/tst-wscanf-nul.c
>> new file mode 100644
>> index 00000000000..18d317b65b7
>> --- /dev/null
>> +++ b/stdio-common/tst-wscanf-nul.c
>
>> +/* Store the conversion into a buffer of exactly the size required for
>> + the expected result and its terminator, followed by an inaccessible
>> + page, so a write past the terminator faults. */
>> +static void
>> +check_buffer (const struct test *t, FILE *fp)
>> +{
>> + size_t size = strlen (t->expected) + 1;
>> + struct support_next_to_fault ntf = support_next_to_fault_allocate (size);
>> + memset (ntf.buffer, 0xff, size);
>> +
>> + int r = fp == NULL
>> + ? swscanf (t->input, t->format, ntf.buffer)
>> + : fwscanf (fp, t->format, ntf.buffer);
>> + TEST_COMPARE (r, 1);
>> + TEST_COMPARE_BLOB (ntf.buffer, size, t->expected, size);
>> +
>> + support_next_to_fault_free (&ntf);
>> +}
>> +
>> +static void
>> +check_test (const struct test *t)
>> +{
>> + if (test_verbose > 0)
>> + printf ("info: input \"%ls\" format \"%ls\"\n", t->input, t->format);
>> +
>> + check_buffer (t, NULL);
>> +
>> + /* fmemopen streams can not be wide oriented, so use a temporary
>> + file instead. */
>> + FILE *fp = tmpfile ();
>> + TEST_VERIFY_EXIT (fp != NULL);
>> + TEST_VERIFY_EXIT (fputws (t->input, fp) >= 0);
>> + rewind (fp);
>> + check_buffer (t, fp);
>> + xfclose (fp);
>> +
>> + char *p = NULL;
>> + TEST_COMPARE (swscanf (t->input, t->mformat, &p), 1);
>> + TEST_COMPARE_STRING (p, t->expected);
>> + free (p);
>> +}
>
> The swscanf test should go up, after the check_buffer call, and there
> could be a fwscanf check for t->mformat (after a rewind on fp).
Ack.
>
>> diff --git a/stdio-common/vfscanf-internal.c b/stdio-common/vfscanf-internal.c
>> index a15669f0566..43042178208 100644
>> --- a/stdio-common/vfscanf-internal.c
>> +++ b/stdio-common/vfscanf-internal.c
>> @@ -1141,17 +1141,18 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr,
>> {
>> #ifdef COMPILE_WSCANF
>> /* We have to emit the code to get into the initial
>> - state. */
>> + state. __wcrtomb also stores the terminating null
>> + byte, which is included in N. */
>> char buf[MB_LEN_MAX];
>> size_t n = __wcrtomb (buf, L'\0', &state);
>> if (n > 0 && (flags & MALLOC)
>> - && str + n >= *strptr + strsize)
>> + && str + n > *strptr + strsize)
>> {
>
> This code performs out-of-bounds pointer arithmetic. There's also a
> missing check for n == (size_t) -1 (the error indicator for wcrtomb),
> but that shouldn't be reachable because the null wide character is
> valid.
Ack.
Update patch below:
>From f1ec60f026d5ffcd18b1361a93c433e653e3b73b Mon Sep 17 00:00:00 2001
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Fri, 9 Oct 2026 13:18:07 -0300
Subject: [PATCH v2] stdio-common: Fix double null terminator in wide scanf %s and
%[ (BZ 34989, CVE-2026-115819)
For %s and %[ without the 'l' modifier, the wide scanf functions
convert the input into the char array with __wcrtomb. To emit the
sequence that returns to the initial shift state, the conversion is
finished with __wcrtomb (buf, L'\0', &state), which also stores the
terminating null byte, and the N bytes are copied to the output. The
code then appends another null byte, so two are written.
If the input fills the field width, an array sized for the converted
string plus its terminator (as C requires) is written one byte past
its end, for instance:
char buf[5];
swscanf (L"abcd", L"%4s", buf);
stores "abcd\0\0". The 'm' modifier allocated one extra byte for it,
which was later trimmed.
Only append the null byte explicitly for the narrow scanf, and size
the 'm' buffer for the N bytes from __wcrtomb.
The new test checks swscanf and fwscanf with the destination placed
right before a PROT_NONE page, in the C and a UTF-8 locale, and the
'm' modifier allocation for a range of string lengths.
Checked on x86_64-linux-gnu.
Reported-by: Russell Miller <rmiller@duskglow.com>
---
stdio-common/Makefile | 2 +
stdio-common/tst-wscanf-nul.c | 169 ++++++++++++++++++++++++++++++++
stdio-common/vfscanf-internal.c | 30 +++---
3 files changed, 189 insertions(+), 12 deletions(-)
create mode 100644 stdio-common/tst-wscanf-nul.c
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index b06e42b7e5f..b9c39de9787 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -353,6 +353,7 @@ tests := \
tst-vfscanf-bz34008 \
tst-wc-printf \
tst-wscanf-bz34208 \
+ tst-wscanf-nul \
tstdiomisc \
tstgetln \
tstscanf \
@@ -560,6 +561,7 @@ $(objpfx)tst-vfprintf-mbs-prec.out: $(gen-locales)
$(objpfx)tst-vfprintf-width-i18n.out: $(gen-locales)
$(objpfx)tst-grouping3.out: $(gen-locales)
$(objpfx)tst-scanf-to_inpunct.out: $(gen-locales)
+$(objpfx)tst-wscanf-nul.out: $(gen-locales)
endif
tst-printf-bz18872-ENV = MALLOC_TRACE=$(objpfx)tst-printf-bz18872.mtrace \
diff --git a/stdio-common/tst-wscanf-nul.c b/stdio-common/tst-wscanf-nul.c
new file mode 100644
index 00000000000..bda1bcd7ed7
--- /dev/null
+++ b/stdio-common/tst-wscanf-nul.c
@@ -0,0 +1,169 @@
+/* Test that wide scanf %s and %[ store a single null terminator.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <array_length.h>
+#include <limits.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+#include <support/check.h>
+#include <support/next_to_fault.h>
+#include <support/support.h>
+#include <support/test-driver.h>
+#include <support/xstdio.h>
+
+struct test
+{
+ const wchar_t *input;
+ const wchar_t *format;
+ /* The same conversion with the 'm' modifier. */
+ const wchar_t *mformat;
+ /* Multibyte result, without the terminating null byte. */
+ const char *expected;
+};
+
+static const struct test tests_c[] =
+ {
+ { L"abcd", L"%4s", L"%4ms", "abcd" },
+ { L"abcdef", L"%4s", L"%4ms", "abcd" },
+ { L"ab", L"%4s", L"%4ms", "ab" },
+ { L"abcd", L"%s", L"%ms", "abcd" },
+ { L"a", L"%1s", L"%1ms", "a" },
+ { L"abcd", L"%4[a-z]", L"%4m[a-z]", "abcd" },
+ { L"abcdef", L"%4[a-z]", L"%4m[a-z]", "abcd" },
+ { L"abcd", L"%[^x]", L"%m[^x]", "abcd" },
+ };
+
+static const struct test tests_utf8[] =
+ {
+ { L"\u00e4\u00f6\u00fc", L"%3s", L"%3ms", "\xc3\xa4\xc3\xb6\xc3\xbc" },
+ { L"\u00e4\u00f6\u00fcx", L"%3s", L"%3ms", "\xc3\xa4\xc3\xb6\xc3\xbc" },
+ { L"\u00e4\u00f6\u00fc", L"%s", L"%ms", "\xc3\xa4\xc3\xb6\xc3\xbc" },
+ { L"\u00e4\u00f6\u00fc", L"%3[^x]", L"%3m[^x]",
+ "\xc3\xa4\xc3\xb6\xc3\xbc" },
+ { L"\u00e4\u00f6\u00fcx", L"%[^x]", L"%m[^x]",
+ "\xc3\xa4\xc3\xb6\xc3\xbc" },
+ };
+
+/* Store the conversion into a buffer of exactly the size required for
+ the expected result and its terminator, followed by an inaccessible
+ page, so a write past the terminator faults. */
+static void
+check_buffer (const struct test *t, FILE *fp)
+{
+ size_t size = strlen (t->expected) + 1;
+ struct support_next_to_fault ntf = support_next_to_fault_allocate (size);
+ memset (ntf.buffer, 0xff, size);
+
+ int r = fp == NULL
+ ? swscanf (t->input, t->format, ntf.buffer)
+ : fwscanf (fp, t->format, ntf.buffer);
+ TEST_COMPARE (r, 1);
+ TEST_COMPARE_BLOB (ntf.buffer, size, t->expected, size);
+
+ support_next_to_fault_free (&ntf);
+}
+
+/* Same as check_buffer, but with the 'm' modifier allocating the
+ result. */
+static void
+check_malloc (const struct test *t, FILE *fp)
+{
+ char *p = NULL;
+ int r = fp == NULL
+ ? swscanf (t->input, t->mformat, &p)
+ : fwscanf (fp, t->mformat, &p);
+ TEST_COMPARE (r, 1);
+ TEST_COMPARE_STRING (p, t->expected);
+ free (p);
+}
+
+static void
+check_test (const struct test *t)
+{
+ if (test_verbose > 0)
+ printf ("info: input \"%ls\" format \"%ls\"\n", t->input, t->format);
+
+ check_buffer (t, NULL);
+ check_malloc (t, NULL);
+
+ /* fmemopen streams can not be wide oriented, so use a temporary
+ file instead. */
+ FILE *fp = tmpfile ();
+ TEST_VERIFY_EXIT (fp != NULL);
+ TEST_VERIFY_EXIT (fputws (t->input, fp) >= 0);
+ rewind (fp);
+ check_buffer (t, fp);
+ rewind (fp);
+ check_malloc (t, fp);
+ xfclose (fp);
+}
+
+/* Exercise the 'm' modifier buffer growth for a range of input
+ lengths, where each input character converts to CHARLEN bytes. */
+static void
+check_alloc (wchar_t wc, const char *mb, size_t charlen)
+{
+ enum { maxlen = 300 };
+ wchar_t input[maxlen + 1];
+ char expected[maxlen * MB_LEN_MAX + 1];
+
+ for (size_t len = 1; len <= maxlen; len++)
+ {
+ for (size_t i = 0; i < len; i++)
+ {
+ input[i] = wc;
+ memcpy (expected + i * charlen, mb, charlen);
+ }
+ input[len] = L'\0';
+ expected[len * charlen] = '\0';
+
+ char *p = NULL;
+ TEST_COMPARE (swscanf (input, L"%ms", &p), 1);
+ TEST_COMPARE_STRING (p, expected);
+ free (p);
+
+ p = NULL;
+ TEST_COMPARE (swscanf (input, L"%m[^x]", &p), 1);
+ TEST_COMPARE_STRING (p, expected);
+ free (p);
+ }
+}
+
+static int
+do_test (void)
+{
+ for (size_t i = 0; i < array_length (tests_c); i++)
+ check_test (&tests_c[i]);
+ check_alloc (L'a', "a", 1);
+
+ xsetlocale (LC_ALL, "de_DE.UTF-8");
+ for (size_t i = 0; i < array_length (tests_c); i++)
+ check_test (&tests_c[i]);
+ for (size_t i = 0; i < array_length (tests_utf8); i++)
+ check_test (&tests_utf8[i]);
+ check_alloc (L'a', "a", 1);
+ check_alloc (L'\u00e4', "\xc3\xa4", 2);
+ check_alloc (L'\u20ac', "\xe2\x82\xac", 3);
+
+ return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/stdio-common/vfscanf-internal.c b/stdio-common/vfscanf-internal.c
index a15669f0566..c7f08b6e3c1 100644
--- a/stdio-common/vfscanf-internal.c
+++ b/stdio-common/vfscanf-internal.c
@@ -1141,17 +1141,19 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr,
{
#ifdef COMPILE_WSCANF
/* We have to emit the code to get into the initial
- state. */
+ state. __wcrtomb also stores the terminating null
+ byte, which is included in N. */
char buf[MB_LEN_MAX];
size_t n = __wcrtomb (buf, L'\0', &state);
- if (n > 0 && (flags & MALLOC)
- && str + n >= *strptr + strsize)
+ if (__glibc_unlikely (n == (size_t) -1))
+ encode_error ();
+ if ((flags & MALLOC) && str - *strptr + n > strsize)
{
/* Enlarge the buffer. */
size_t strleng = str - *strptr;
char *newstr;
- newstr = (char *) realloc (*strptr, strleng + n + 1);
+ newstr = (char *) realloc (*strptr, strleng + n);
if (newstr == NULL)
{
if (flags & POSIX_MALLOC)
@@ -1171,13 +1173,14 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr,
{
*strptr = newstr;
str = newstr + strleng;
- strsize = strleng + n + 1;
+ strsize = strleng + n;
}
}
str = __mempcpy (str, buf, n);
-#endif
+#else
*str++ = '\0';
+#endif
if ((flags & MALLOC) && str - *strptr != strsize)
{
@@ -2994,17 +2997,19 @@ digits_extended_fail:
{
#ifdef COMPILE_WSCANF
/* We have to emit the code to get into the initial
- state. */
+ state. __wcrtomb also stores the terminating null
+ byte, which is included in N. */
char buf[MB_LEN_MAX];
size_t n = __wcrtomb (buf, L'\0', &state);
- if (n > 0 && (flags & MALLOC)
- && str + n >= *strptr + strsize)
+ if (__glibc_unlikely (n == (size_t) -1))
+ encode_error ();
+ if ((flags & MALLOC) && str - *strptr + n > strsize)
{
/* Enlarge the buffer. */
size_t strleng = str - *strptr;
char *newstr;
- newstr = (char *) realloc (*strptr, strleng + n + 1);
+ newstr = (char *) realloc (*strptr, strleng + n);
if (newstr == NULL)
{
if (flags & POSIX_MALLOC)
@@ -3024,13 +3029,14 @@ digits_extended_fail:
{
*strptr = newstr;
str = newstr + strleng;
- strsize = strleng + n + 1;
+ strsize = strleng + n;
}
}
str = __mempcpy (str, buf, n);
-#endif
+#else
*str++ = '\0';
+#endif
if ((flags & MALLOC) && str - *strptr != strsize)
{
--
2.53.0
More information about the Libc-alpha
mailing list