]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: Add integrity check to largebin nextsizes
authorBen Kallus <benjamin.p.kallus.gr@dartmouth.edu>
Fri, 14 Feb 2025 05:34:54 +0000 (00:34 -0500)
committerDJ Delorie <dj@redhat.com>
Mon, 3 Mar 2025 23:31:27 +0000 (18:31 -0500)
If attacker overwrites the bk_nextsize link in the first chunk of a
largebin that later has a smaller chunk inserted into it, malloc will
write a heap pointer into an attacker-controlled address [0].

This patch adds an integrity check to mitigate this attack.

[0]: https://github.com/shellphish/how2heap/blob/master/glibc_2.39/large_bin_attack.c

Signed-off-by: Ben Kallus <benjamin.p.kallus.gr@dartmouth.edu>
Reviewed-by: DJ Delorie <dj@redhat.com>
malloc/malloc.c

index dcac903e2a2274a9eaa64af278ba77abe714179e..931ca481127c6f5199787058d86a0621328de04b 100644 (file)
@@ -4244,6 +4244,9 @@ _int_malloc (mstate av, size_t bytes)
                       fwd = bck;
                       bck = bck->bk;
 
+                      if (__glibc_unlikely (fwd->fd->bk_nextsize->fd_nextsize != fwd->fd))
+                        malloc_printerr ("malloc(): largebin double linked list corrupted (nextsize)");
+
                       victim->fd_nextsize = fwd->fd;
                       victim->bk_nextsize = fwd->fd->bk_nextsize;
                       fwd->fd->bk_nextsize = victim->bk_nextsize->fd_nextsize = victim;