stdio-common: Fix heap overflow in scanf %mc pattern

Message ID 20260319174311.972443-1-marocketbd@gmail.com (mailing list archive)
State Superseded
Headers
Series stdio-common: Fix heap overflow in scanf %mc pattern |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Rocket Ma March 19, 2026, 5:43 p.m. UTC
  * stdio-common/vfscanf-internal.c: when `WIDTH` in `%WIDTHmc` or
`%WIDTHmC` greater than 1024, user could read one more byte into heap,
leading into off-by-one overflow.

This patch fixes Bug 34008[1].

[1]: https://sourceware.org/bugzilla/show_bug.cgi?id=34008

Signed-off-by: Rocket Ma <marocketbd@gmail.com>
---
 stdio-common/vfscanf-internal.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Adhemerval Zanella Netto March 20, 2026, 5:42 p.m. UTC | #1
On 19/03/26 14:43, Rocket Ma wrote:
> * stdio-common/vfscanf-internal.c: when `WIDTH` in `%WIDTHmc` or
> `%WIDTHmC` greater than 1024, user could read one more byte into heap,
> leading into off-by-one overflow.
> 
> This patch fixes Bug 34008[1].
> 
> [1]: https://sourceware.org/bugzilla/show_bug.cgi?id=34008
> 
> Signed-off-by: Rocket Ma <marocketbd@gmail.com>

Hi, thanks for working on this. Could you add a regression tests similar
to the one on the bug report?

> ---
>  stdio-common/vfscanf-internal.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/stdio-common/vfscanf-internal.c b/stdio-common/vfscanf-internal.c
> index 59fc8208aa..b33ee0652c 100644
> --- a/stdio-common/vfscanf-internal.c
> +++ b/stdio-common/vfscanf-internal.c
> @@ -856,7 +856,7 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr,
>  			  /* Enlarge the buffer.  */
>  			  size_t newsize
>  			    = strsize
> -			      + (strsize >= width ? width - 1 : strsize);
> +			      + (strsize >= width ? width : strsize);
>  
>  			  str = (char *) realloc (*strptr, newsize);
>  			  if (str == NULL)
> @@ -929,7 +929,7 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr,
>  		      && wstr == (wchar_t *) *strptr + strsize)
>  		    {
>  		      size_t newsize
> -			= strsize + (strsize > width ? width - 1 : strsize);
> +			= strsize + (strsize > width ? width : strsize);
>  		      /* Enlarge the buffer.  */
>  		      wstr = (wchar_t *) realloc (*strptr,
>  						  newsize * sizeof (wchar_t));
> @@ -984,7 +984,7 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr,
>  		    && wstr == (wchar_t *) *strptr + strsize)
>  		  {
>  		    size_t newsize
> -		      = strsize + (strsize > width ? width - 1 : strsize);
> +		      = strsize + (strsize > width ? width : strsize);
>  		    /* Enlarge the buffer.  */
>  		    wstr = (wchar_t *) realloc (*strptr,
>  						newsize * sizeof (wchar_t));
  
Rocket Ma March 20, 2026, 6:03 p.m. UTC | #2
> Hi, thanks for working on this. Could you add a regression tests similar
> to the one on the bug report?

I should be able to spare some time to write some tests. I'll try my best,
though perhaps I don't have that much time. Anyway, next Tuesday is the
deadline for me to complete all regression tests.
  
Rocket Ma April 2, 2026, 5:30 p.m. UTC | #3
By the way, do you know why the test on aarch64 failed with exit code
127? That's a bit confusing.

Cheers,
Rocket
  

Patch

diff --git a/stdio-common/vfscanf-internal.c b/stdio-common/vfscanf-internal.c
index 59fc8208aa..b33ee0652c 100644
--- a/stdio-common/vfscanf-internal.c
+++ b/stdio-common/vfscanf-internal.c
@@ -856,7 +856,7 @@  __vfscanf_internal (FILE *s, const char *format, va_list argptr,
 			  /* Enlarge the buffer.  */
 			  size_t newsize
 			    = strsize
-			      + (strsize >= width ? width - 1 : strsize);
+			      + (strsize >= width ? width : strsize);
 
 			  str = (char *) realloc (*strptr, newsize);
 			  if (str == NULL)
@@ -929,7 +929,7 @@  __vfscanf_internal (FILE *s, const char *format, va_list argptr,
 		      && wstr == (wchar_t *) *strptr + strsize)
 		    {
 		      size_t newsize
-			= strsize + (strsize > width ? width - 1 : strsize);
+			= strsize + (strsize > width ? width : strsize);
 		      /* Enlarge the buffer.  */
 		      wstr = (wchar_t *) realloc (*strptr,
 						  newsize * sizeof (wchar_t));
@@ -984,7 +984,7 @@  __vfscanf_internal (FILE *s, const char *format, va_list argptr,
 		    && wstr == (wchar_t *) *strptr + strsize)
 		  {
 		    size_t newsize
-		      = strsize + (strsize > width ? width - 1 : strsize);
+		      = strsize + (strsize > width ? width : strsize);
 		    /* Enlarge the buffer.  */
 		    wstr = (wchar_t *) realloc (*strptr,
 						newsize * sizeof (wchar_t));