]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
CVE-2015-1472: wscanf allocates too little memory
authorPaul Pluzhnikov <ppluzhnikov@google.com>
Fri, 6 Feb 2015 05:30:42 +0000 (00:30 -0500)
committerAdhemerval Zanella <azanella@linux.vnet.ibm.com>
Mon, 23 Feb 2015 14:26:06 +0000 (09:26 -0500)
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

ChangeLog
NEWS
stdio-common/tst-sscanf.c
stdio-common/vfscanf.c

index ad9df0f243b687db6cc8d4531a6001565aa22801..b0b9debca09c1e043cc2bbc6880434b48956275b 100644 (file)
--- 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.
+
 2014-05-26  Adhemerval Zanella  <azanella@linux.vnet.ibm.com>
 
        * sysdeps/powerpc/powerpc32/power4/memset.S (memset): Replace insrdi
diff --git a/NEWS b/NEWS
index 1c9acf3708dc8db9975d84f3ad55331f164d097f..754ac0a7e481b97ba2213710fd2f0191855f8465 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,7 +11,8 @@ Version 2.18.1
 
   14143, 14155, 14547, 14699, 15532, 15427, 15522, 15680, 15723, 15734,
   15735, 15797, 15892, 15895, 15909, 15915, 15917, 15996, 16072, 16150,
-  16414, 16430, 16431, 16617, 17048, 17137, 17187, 17325, 17625, 17630.
+  16414, 16430, 16431, 16617, 16618, 17048, 17137, 17187, 17325, 17625,
+  17630.
 
 * Support for powerpc64le has been added.
 
index 3c34f58a635d5418830b2c49ecc1230360a9bed4..8f73c9510b80502d0bb91629ffbc412347aa86ef 100644 (file)
@@ -230,5 +230,38 @@ 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;
 }
index 093c8b0c5805b82262a4e9680d02d9c277bcee44..8980a6ec8d668139c1aa0a96481d5967286fd68d 100644 (file)
@@ -272,9 +272,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 = __glibc_likely (wpmax <= SIZE_MAX / sizeof (CHAR_T) / 2); \
+         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)                                               \
@@ -286,14 +287,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);                                   \