about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Weimer <fw@deneb.enyo.de>2018-12-31 22:04:36 +0100
committerFlorian Weimer <fw@deneb.enyo.de>2019-01-01 10:32:37 +0100
commit22b7478755c07f4741db85d14fc2b57837078ca6 (patch)
tree2622e80c2f8de61e5378476a3c598cba9fb584eb
parentfcd316654a4510281fff32194b3b9f90e3dfab83 (diff)
downloadglibc-22b7478755c07f4741db85d14fc2b57837078ca6.tar.gz
glibc-22b7478755c07f4741db85d14fc2b57837078ca6.tar.xz
glibc-22b7478755c07f4741db85d14fc2b57837078ca6.zip
malloc: Always call memcpy in _int_realloc [BZ #24027]
This commit removes the custom memcpy implementation from _int_realloc
for small chunk sizes.  The ncopies variable has the wrong type, and
an integer wraparound could cause the existing code to copy too few
elements (leaving the new memory region mostly uninitialized).
Therefore, removing this code fixes bug 24027.

(cherry picked from commit b50dd3bc8cbb1efe85399b03d7e6c0310c2ead84)
-rw-r--r--ChangeLog7
-rw-r--r--NEWS1
-rw-r--r--malloc/malloc.c43
3 files changed, 9 insertions, 42 deletions
diff --git a/ChangeLog b/ChangeLog
index cbf1876cae..399d162f03 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2018-12-31  Florian Weimer  <fw@deneb.enyo.de>
+
+	[BZ #24027]
+	* malloc/malloc.c (_int_realloc): Always call memcpy for the
+	copying operation.  (ncopies had the wrong type, resulting in an
+	integer wraparound and too few elements being copied.)
+
 2016-01-28  Carlos O'Donell  <carlos@redhat.com>
 	    Alexey Makhalov <amakhalov@vmware.com>
 	    Florian Weimer <fweimer@redhat.com>
diff --git a/NEWS b/NEWS
index d5868b2e09..f7536ac0d9 100644
--- a/NEWS
+++ b/NEWS
@@ -81,6 +81,7 @@ The following bugs are resolved with this release:
     to SSIZE_MAX (CVE-2018-11236)
   [23196] string: __mempcpy_avx512_no_vzeroupper mishandles large copies
     (CVE-2018-11237)
+  [24027] malloc: Integer overflow in realloc
 
 Version 2.24
 
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 1f5f166ea2..8a1321d292 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -4271,11 +4271,6 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
   mchunkptr        bck;             /* misc temp for linking */
   mchunkptr        fwd;             /* misc temp for linking */
 
-  unsigned long    copysize;        /* bytes to copy */
-  unsigned int     ncopies;         /* INTERNAL_SIZE_T words to copy */
-  INTERNAL_SIZE_T* s;               /* copy source */
-  INTERNAL_SIZE_T* d;               /* copy destination */
-
   const char *errstr = NULL;
 
   /* oldmem size */
@@ -4353,43 +4348,7 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
             }
           else
             {
-              /*
-                 Unroll copy of <= 36 bytes (72 if 8byte sizes)
-                 We know that contents have an odd number of
-                 INTERNAL_SIZE_T-sized words; minimally 3.
-               */
-
-              copysize = oldsize - SIZE_SZ;
-              s = (INTERNAL_SIZE_T *) (chunk2mem (oldp));
-              d = (INTERNAL_SIZE_T *) (newmem);
-              ncopies = copysize / sizeof (INTERNAL_SIZE_T);
-              assert (ncopies >= 3);
-
-              if (ncopies > 9)
-                memcpy (d, s, copysize);
-
-              else
-                {
-                  *(d + 0) = *(s + 0);
-                  *(d + 1) = *(s + 1);
-                  *(d + 2) = *(s + 2);
-                  if (ncopies > 4)
-                    {
-                      *(d + 3) = *(s + 3);
-                      *(d + 4) = *(s + 4);
-                      if (ncopies > 6)
-                        {
-                          *(d + 5) = *(s + 5);
-                          *(d + 6) = *(s + 6);
-                          if (ncopies > 8)
-                            {
-                              *(d + 7) = *(s + 7);
-                              *(d + 8) = *(s + 8);
-                            }
-                        }
-                    }
-                }
-
+	      memcpy (newmem, chunk2mem (oldp), oldsize - SIZE_SZ);
               _int_free (av, oldp, 1);
               check_inuse_chunk (av, newp);
               return chunk2mem (newp);