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