Shrinking Free Chunks
Last updated
Last updated
This attack was described in 'Glibc Adventures: The Forgotten Chunk'. It makes use of a single byte heap overflow (commonly found due to the 'off by one'. The goal of this attack is to make 'malloc' return a chunk that overlaps with an already allocated chunk, currently in use. First 3 consecutive chunks in memory (a
, b
, c
) are allocated and the middle one is freed. The first chunk is overflowed, resulting in an overwrite of the 'size' of the middle chunk. The least significant byte to 0 by the attacker. This 'shrinks' the chunk in size. Next, two small chunks (b1
and b2
) are allocated out of the middle free chunk. The third chunk's prev_size
does not get updated as b
+ b->size
no longer points to c
. It, in fact, points to a memory region 'before' c
. Then, b1
along with the c
is freed. c
still assumes b
to be free (since prev_size
didn't get updated and hence c
- c->prev_size
still points to b
) and consolidates itself with b
. This results in a big free chunk starting from b
and overlapping with b2
. A new malloc returns this big chunk, thereby completing the attack. The following figure sums up the steps:
Image Source: https://www.contextis.com/documents/120/Glibc_Adventures-The_Forgotten_Chunks.pdf
Consider this sample code (download the complete version here):
big
now points to the initial b
chunk and overlaps with b2
. Updating contents of big
updates contents of b2
, even when both these chunks are never passed to free
.
Note that instead of shrinking b
, the attacker could also have increased the size of b
. This will result in a similar case of overlap. When 'malloc' requests another chunk of the increased size, b
will be used to service this request. Now c
's memory will also be part of this new chunk returned.