d8307d
commit ebe544bf6e8eec35e754fd49efb027c6f161b6cb
d8307d
Author: Istvan Kurucsai <pistukem@gmail.com>
d8307d
Date:   Thu Dec 20 23:30:07 2018 -0500
d8307d
d8307d
    malloc: Add more integrity checks to mremap_chunk.
d8307d
    
d8307d
    * malloc/malloc.c (mremap_chunk): Additional checks.
d8307d
d8307d
diff --git a/malloc/malloc.c b/malloc/malloc.c
d8307d
index eb6a8ff33c0c313b..4df5cb4862a7b854 100644
d8307d
--- a/malloc/malloc.c
d8307d
+++ b/malloc/malloc.c
d8307d
@@ -2856,16 +2856,22 @@ mremap_chunk (mchunkptr p, size_t new_size)
d8307d
   char *cp;
d8307d
 
d8307d
   assert (chunk_is_mmapped (p));
d8307d
-  assert (((size + offset) & (GLRO (dl_pagesize) - 1)) == 0);
d8307d
+
d8307d
+  uintptr_t block = (uintptr_t) p - offset;
d8307d
+  uintptr_t mem = (uintptr_t) chunk2mem(p);
d8307d
+  size_t total_size = offset + size;
d8307d
+  if (__glibc_unlikely ((block | total_size) & (pagesize - 1)) != 0
d8307d
+      || __glibc_unlikely (!powerof2 (mem & (pagesize - 1))))
d8307d
+    malloc_printerr("mremap_chunk(): invalid pointer");
d8307d
 
d8307d
   /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */
d8307d
   new_size = ALIGN_UP (new_size + offset + SIZE_SZ, pagesize);
d8307d
 
d8307d
   /* No need to remap if the number of pages does not change.  */
d8307d
-  if (size + offset == new_size)
d8307d
+  if (total_size == new_size)
d8307d
     return p;
d8307d
 
d8307d
-  cp = (char *) __mremap ((char *) p - offset, size + offset, new_size,
d8307d
+  cp = (char *) __mremap ((char *) block, total_size, new_size,
d8307d
                           MREMAP_MAYMOVE);
d8307d
 
d8307d
   if (cp == MAP_FAILED)