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