Forging chunks
After a chunk is freed, it is inserted in a binlist. However, the pointer is still available in the program. If the attacker has control of this pointer, he/she can modify the linked list structure in bins and insert his/her own 'forged' chunk. The sample program shown below shows how this is possible in the case of fastbin freelist.
The forged chunk's size parameter was set equal to 0x20 so that it passes the security check "malloc(): memory corruption (fast)". This check checks whether the size of the chunk falls in the range for that particular fastbin. Also, note that the data for an allocated chunk starts from the 'fd' pointer. This is also evident in the above program as victim
points 0x10
(0x8+0x8) bytes ahead of the 'forged chunk'.
The state of the particular fastbin progresses as:
'a' freed.
head -> a -> tail
a's fd pointer changed to point to 'forged chunk'.
head -> a -> forged chunk -> undefined (fd of forged chunk will in fact be holding attacker's data)
'malloc' request
head -> forged chunk -> undefined
'malloc' request by victim
head -> undefined [ forged chunk is returned to the victim ]
Note the following:
Another 'malloc' request for the fast chunk in the same bin list will result in segmentation fault.
Even though we request for 10 bytes and set the size of the forged chunk as 32 (0x20) bytes, both fall in the same fastbin range of 32-byte chunks.
This attack for small and large chunks will be seen later as 'House of Lore'.
The above code is designed for 64-bit machines. To run it on 32-bit machines, replace
unsigned long long
withunsigned int
as pointers are now 4 bytes instead of 8 bytes. Also, instead of using 32 bytes as size for forged chunk, a small of the size of around 17 bytes should work.
Last updated