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