[PATCH] stdio-common: Fix double null terminator in wide scanf %s and %[ (BZ 34989, CVE-2026-115819)

Florian Weimer fweimer@redhat.com
Wed Nov 11 16:29:45 GMT 2026


* 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).

> 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.

Thanks,
Florian



More information about the Libc-alpha mailing list