about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaul Pluzhnikov <ppluzhnikov@google.com>2015-02-06 00:30:42 -0500
committerAdhemerval Zanella <azanella@linux.vnet.ibm.com>2015-02-23 10:00:32 -0500
commit627eabb20f2b70faa3698e2c0124094c6d51af8e (patch)
tree52a452489ef9c9b27f7d40bb39853e0781b5c164
parentec36394743c15fedca294219f2254b180c4e327c (diff)
downloadglibc-627eabb20f2b70faa3698e2c0124094c6d51af8e.tar.gz
glibc-627eabb20f2b70faa3698e2c0124094c6d51af8e.tar.xz
glibc-627eabb20f2b70faa3698e2c0124094c6d51af8e.zip
CVE-2015-1472: wscanf allocates too little memory
BZ #16618

Under certain conditions wscanf can allocate too little memory for the
to-be-scanned arguments and overflow the allocated buffer.  The
implementation now correctly computes the required buffer size when
using malloc.

A regression test was added to tst-sscanf.

Conflicts:
	ChangeLog
	NEWS
	stdio-common/tst-sscanf.c
-rw-r--r--ChangeLog8
-rw-r--r--NEWS6
-rw-r--r--stdio-common/tst-sscanf.c32
-rw-r--r--stdio-common/vfscanf.c12
4 files changed, 49 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index b15b0ab3fd..5b73423044 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2015-02-05  Paul Pluzhnikov  <ppluzhnikov@google.com>
+
+	[BZ #16618]
+	* stdio-common/tst-sscanf.c (main): Test for buffer overflow.
+	* stdio-common/vfscanf.c (_IO_vfscanf_internal): Compute needed
+	size in bytes. Store needed elements in wpmax. Use needed size
+	in bytes for extend_alloca.
+
 2013-05-21  Andreas Schwab  <schwab@suse.de>
 
 	[BZ #15014]
diff --git a/NEWS b/NEWS
index 7b466bfd90..b0c3072c45 100644
--- a/NEWS
+++ b/NEWS
@@ -9,9 +9,9 @@ Version 2.16.1
 
 * The following bugs are resolved with this release:
 
-  6530, 14195, 14547, 14459, 14476, 14562, 14621, 14648, 14699, 14719, 14756,
-  14831, 15014, 15078, 15754, 15755, 16072, 16431, 16617, 17048, 17137,
-  17187, 17325, 17625, 17630.
+  6530, 14195, 14547, 14459, 14476, 14562, 14621, 14648, 14699, 14719,
+  14756, 14831, 15014, 15078, 15754, 15755, 16072, 16431, 16617, 16618,
+  17048, 17137, 17187, 17325, 17625, 17630.
 
 * CVE-2104-7817 The wordexp function could ignore the WRDE_NOCMD flag
   under certain input conditions resulting in the execution of a shell for
diff --git a/stdio-common/tst-sscanf.c b/stdio-common/tst-sscanf.c
index a2db04cd99..9374c4865e 100644
--- a/stdio-common/tst-sscanf.c
+++ b/stdio-common/tst-sscanf.c
@@ -196,5 +196,37 @@ main (void)
 	}
     }
 
+  /* BZ #16618
+     The test will segfault during SSCANF if the buffer overflow
+     is not fixed.  The size of `s` is such that it forces the use
+     of malloc internally and this triggers the incorrect computation.
+     Thus the value for SIZE is arbitrariy high enough that malloc
+     is used.  */
+  {
+#define SIZE 131072
+    CHAR *s = malloc ((SIZE + 1) * sizeof (*s));
+    if (s == NULL)
+      abort ();
+    for (size_t i = 0; i < SIZE; i++)
+      s[i] = L('0');
+    s[SIZE] = L('\0');
+    int i = 42;
+    /* Scan multi-digit zero into `i`.  */
+    if (SSCANF (s, L("%d"), &i) != 1)
+      {
+	printf ("FAIL: bug16618: SSCANF did not read one input item.\n");
+	result = 1;
+      }
+    if (i != 0)
+      {
+	printf ("FAIL: bug16618: Value of `i` was not zero as expected.\n");
+	result = 1;
+      }
+    free (s);
+    if (result != 1)
+      printf ("PASS: bug16618: Did not crash.\n");
+#undef SIZE
+  }
+
   return result;
 }
diff --git a/stdio-common/vfscanf.c b/stdio-common/vfscanf.c
index 23a8ac7694..f4fe6801ac 100644
--- a/stdio-common/vfscanf.c
+++ b/stdio-common/vfscanf.c
@@ -271,9 +271,10 @@ _IO_vfscanf_internal (_IO_FILE *s, const char *format, _IO_va_list argptr,
       if (__builtin_expect (wpsize == wpmax, 0))			    \
 	{								    \
 	  CHAR_T *old = wp;						    \
-	  size_t newsize = (UCHAR_MAX + 1 > 2 * wpmax			    \
-			    ? UCHAR_MAX + 1 : 2 * wpmax);		    \
-	  if (use_malloc || !__libc_use_alloca (newsize))		    \
+	  bool fits = __builtin_expect ((wpmax <= SIZE_MAX / sizeof (CHAR_T) / 2), 1); \
+	  size_t wpneed = MAX (UCHAR_MAX + 1, 2 * wpmax);		    \
+	  size_t newsize = fits ? wpneed * sizeof (CHAR_T) : SIZE_MAX;	    \
+	  if (!__libc_use_alloca (newsize))				    \
 	    {								    \
 	      wp = realloc (use_malloc ? wp : NULL, newsize);		    \
 	      if (wp == NULL)						    \
@@ -285,14 +286,13 @@ _IO_vfscanf_internal (_IO_FILE *s, const char *format, _IO_va_list argptr,
 		}							    \
 	      if (! use_malloc)						    \
 		MEMCPY (wp, old, wpsize);				    \
-	      wpmax = newsize;						    \
+	      wpmax = wpneed;						    \
 	      use_malloc = true;					    \
 	    }								    \
 	  else								    \
 	    {								    \
 	      size_t s = wpmax * sizeof (CHAR_T);			    \
-	      wp = (CHAR_T *) extend_alloca (wp, s,			    \
-					     newsize * sizeof (CHAR_T));    \
+	      wp = (CHAR_T *) extend_alloca (wp, s, newsize);		    \
 	      wpmax = s / sizeof (CHAR_T);				    \
 	      if (old != NULL)						    \
 		MEMCPY (wp, old, wpsize);				    \