Tcache Primer
Storage
- The tcache has 64 bins of various sizes (16 to 1032) that store chunks of the same size.
- Freed chunks of the same bin are stored using a singly linked-list.
- Newly freed chunks are added to the ‘front’ of the LL.
Before frees
[head:NULL]
After free(A)
[head:A]->[next:NULL]
After free(B)
[head:B]->[next:A]->[next:NULL]
Structure
- The second 8 bytes before writable memory indicates the size of the previous chunk.
- The first 8 bytes before writable memory indicates the size of the chunk and includes 3 flag bits.
- The first 8 bytes of writable memory is used for the
nextpointer when freeing. - The second 8 bytes of writable memory is used for the
keywhen freeing. - The
keyis cleared when the chunk is re-allocated.
Chunk layout
-16 -9 -8 -1 0 7 8 15
Freed: [prev size][size][next][key]
Allocated: [prev size][size][user data]
Overlapping metadata
malloc(0x10) malloc(0x10)
[p.size][size][0 7][8 15][p.size][size][0 7][8 15]
[p.size][size][0 7][8 15][16 23]
[p.size][size][0 7][8 15]
malloc(0x18) malloc(0x10)
Defense
Double-free: the tcache detects double free only by checking the key.