ce426f
commit 5bd80bfe9ca0d955bfbbc002781bc7b01b6bcb06
ce426f
Author: Paul Pluzhnikov <ppluzhnikov@google.com>
ce426f
Date:   Fri Feb 6 00:30:42 2015 -0500
ce426f
ce426f
    CVE-2015-1472: wscanf allocates too little memory
ce426f
    
ce426f
    BZ #16618
ce426f
    
ce426f
    Under certain conditions wscanf can allocate too little memory for the
ce426f
    to-be-scanned arguments and overflow the allocated buffer.  The
ce426f
    implementation now correctly computes the required buffer size when
ce426f
    using malloc.
ce426f
    
ce426f
    A regression test was added to tst-sscanf.
ce426f
ce426f
--- glibc-2.17-c758a686/stdio-common/vfscanf.c	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/stdio-common/vfscanf.c	2015-05-28 14:01:58.512816390 -0400
ce426f
@@ -272,9 +272,10 @@
ce426f
       if (__builtin_expect (wpsize == wpmax, 0))			    \
ce426f
 	{								    \
ce426f
 	  CHAR_T *old = wp;						    \
ce426f
-	  size_t newsize = (UCHAR_MAX + 1 > 2 * wpmax			    \
ce426f
-			    ? UCHAR_MAX + 1 : 2 * wpmax);		    \
ce426f
-	  if (use_malloc || !__libc_use_alloca (newsize))		    \
ce426f
+	  bool fits = __glibc_likely (wpmax <= SIZE_MAX / sizeof (CHAR_T) / 2); \
ce426f
+	  size_t wpneed = MAX (UCHAR_MAX + 1, 2 * wpmax);		    \
ce426f
+	  size_t newsize = fits ? wpneed * sizeof (CHAR_T) : SIZE_MAX;	    \
ce426f
+	  if (!__libc_use_alloca (newsize))				    \
ce426f
 	    {								    \
ce426f
 	      wp = realloc (use_malloc ? wp : NULL, newsize);		    \
ce426f
 	      if (wp == NULL)						    \
ce426f
@@ -286,14 +287,13 @@
ce426f
 		}							    \
ce426f
 	      if (! use_malloc)						    \
ce426f
 		MEMCPY (wp, old, wpsize);				    \
ce426f
-	      wpmax = newsize;						    \
ce426f
+	      wpmax = wpneed;						    \
ce426f
 	      use_malloc = true;					    \
ce426f
 	    }								    \
ce426f
 	  else								    \
ce426f
 	    {								    \
ce426f
 	      size_t s = wpmax * sizeof (CHAR_T);			    \
ce426f
-	      wp = (CHAR_T *) extend_alloca (wp, s,			    \
ce426f
-					     newsize * sizeof (CHAR_T));    \
ce426f
+	      wp = (CHAR_T *) extend_alloca (wp, s, newsize);		    \
ce426f
 	      wpmax = s / sizeof (CHAR_T);				    \
ce426f
 	      if (old != NULL)						    \
ce426f
 		MEMCPY (wp, old, wpsize);				    \
ce426f
--- glibc-2.17-c758a686/stdio-common/tst-sscanf.c	2012-12-24 22:02:13.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/stdio-common/tst-sscanf.c	2015-06-03 13:56:10.883924259 -0400
ce426f
@@ -196,5 +196,38 @@
ce426f
 	}
ce426f
     }
ce426f
 
ce426f
+  /* BZ #16618
ce426f
+     The test will segfault during SSCANF if the buffer overflow
ce426f
+     is not fixed.  The size of `s` is such that it forces the use
ce426f
+     of malloc internally and this triggers the incorrect computation.
ce426f
+     Thus the value for SIZE is arbitrariy high enough that malloc
ce426f
+     is used.  */
ce426f
+  {
ce426f
+#define SIZE 131072
ce426f
+    CHAR *s = malloc ((SIZE + 1) * sizeof (*s));
ce426f
+    if (s == NULL)
ce426f
+      abort ();
ce426f
+    for (size_t i = 0; i < SIZE; i++)
ce426f
+      s[i] = L('0');
ce426f
+    s[SIZE] = L('\0');
ce426f
+    int i = 42;
ce426f
+    /* Scan multi-digit zero into `i`.  */
ce426f
+    if (SSCANF (s, L("%d"), &i) != 1)
ce426f
+      {
ce426f
+	printf ("FAIL: bug16618: SSCANF did not read one input item.\n");
ce426f
+	result = 1;
ce426f
+      }
ce426f
+    if (i != 0)
ce426f
+      {
ce426f
+	printf ("FAIL: bug16618: Value of `i` was not zero as expected.\n");
ce426f
+	result = 1;
ce426f
+      }
ce426f
+    free (s);
ce426f
+    if (result != 1)
ce426f
+      printf ("PASS: bug16618: Did not crash.\n");
ce426f
+#undef SIZE
ce426f
+  }
ce426f
+
ce426f
+
ce426f
   return result;
ce426f
 }