]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: Add more integrity checks to mremap_chunk.
authorIstvan Kurucsai <pistukem@gmail.com>
Fri, 21 Dec 2018 04:30:07 +0000 (23:30 -0500)
committerArjun Shankar <arjun@redhat.com>
Thu, 2 May 2019 12:28:48 +0000 (14:28 +0200)
* malloc/malloc.c (mremap_chunk): Additional checks.

(cherry picked from commit ebe544bf6e8eec35e754fd49efb027c6f161b6cb)

ChangeLog
malloc/malloc.c

index c42b55f72fc10dcb90a31b3271b50e8ee8b4bc9f..e2d7e3caca0cc8ba2f8b04c869d2cfaae3874ccb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2018-12-20  Istvan Kurucsai <pistukem@gmail.com>
+
+       * malloc/malloc.c (mremap_chunk): Additional checks.
+
 2018-08-17  Istvan Kurucsai  <pistukem@gmail.com>
 
        * malloc/malloc.c (_int_malloc): Additional binning code checks.
index 520a4faa7c1dd61244b848aa4405a17bc85d1f65..7880fcd1c3b00b76491339ec851df077e324bcba 100644 (file)
@@ -2849,16 +2849,22 @@ mremap_chunk (mchunkptr p, size_t new_size)
   char *cp;
 
   assert (chunk_is_mmapped (p));
-  assert (((size + offset) & (GLRO (dl_pagesize) - 1)) == 0);
+
+  uintptr_t block = (uintptr_t) p - offset;
+  uintptr_t mem = (uintptr_t) chunk2mem(p);
+  size_t total_size = offset + size;
+  if (__glibc_unlikely ((block | total_size) & (pagesize - 1)) != 0
+      || __glibc_unlikely (!powerof2 (mem & (pagesize - 1))))
+    malloc_printerr("mremap_chunk(): invalid pointer");
 
   /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */
   new_size = ALIGN_UP (new_size + offset + SIZE_SZ, pagesize);
 
   /* No need to remap if the number of pages does not change.  */
-  if (size + offset == new_size)
+  if (total_size == new_size)
     return p;
 
-  cp = (char *) __mremap ((char *) p - offset, size + offset, new_size,
+  cp = (char *) __mremap ((char *) block, total_size, new_size,
                           MREMAP_MAYMOVE);
 
   if (cp == MAP_FAILED)