bdc76f
commit 30a17d8c95fbfb15c52d1115803b63aaa73a285c
bdc76f
Author: Pochang Chen <johnchen902@gmail.com>
bdc76f
Date:   Thu Aug 16 15:24:24 2018 -0400
bdc76f
bdc76f
    malloc: Verify size of top chunk.
bdc76f
    
bdc76f
    The House of Force is a well-known technique to exploit heap
bdc76f
    overflow. In essence, this exploit takes three steps:
bdc76f
    1. Overwrite the size of top chunk with very large value (e.g. -1).
bdc76f
    2. Request x bytes from top chunk. As the size of top chunk
bdc76f
       is corrupted, x can be arbitrarily large and top chunk will
bdc76f
       still be offset by x.
bdc76f
    3. The next allocation from top chunk will thus be controllable.
bdc76f
    
bdc76f
    If we verify the size of top chunk at step 2, we can stop such attack.
bdc76f
bdc76f
diff --git a/malloc/malloc.c b/malloc/malloc.c
bdc76f
index e450597e2e527fb7..d8d4581a9dcea80a 100644
bdc76f
--- a/malloc/malloc.c
bdc76f
+++ b/malloc/malloc.c
bdc76f
@@ -4084,6 +4084,9 @@ _int_malloc (mstate av, size_t bytes)
bdc76f
       victim = av->top;
bdc76f
       size = chunksize (victim);
bdc76f
 
bdc76f
+      if (__glibc_unlikely (size > av->system_mem))
bdc76f
+        malloc_printerr ("malloc(): corrupted top size");
bdc76f
+
bdc76f
       if ((unsigned long) (size) >= (unsigned long) (nb + MINSIZE))
bdc76f
         {
bdc76f
           remainder_size = size - nb;