Overview
Fastbin Reverse Into Tcache is a powerful technique that exploits the automatic transfer of fastbin chunks into the tcache. When the tcache is empty and an allocation occurs, glibc moves up to 7 chunks from the fastbin into the tcache in reverse order. By corrupting a fastbin chunk’s forward pointer, we can inject an arbitrary address into this transfer process, causing malloc to write a large heap pointer value to an attacker-controlled address.Glibc Version Compatibility: glibc >= 2.26 (when tcache was introduced)Requires heap address leak on glibc >= 2.32 due to Safe Linking.
What This Achieves
- Arbitrary write: Write a large heap pointer value to any address
- Stack corruption: Overwrite stack variables with heap addresses
- GOT overwrites: Modify function pointers (with careful size alignment)
- Metadata corruption: Corrupt heap management structures
- Similar to unsorted bin attack: But works with small allocations (≤ 0x78 bytes)
This technique is intended to have a similar effect to the classic unsorted_bin_attack, except it works with small allocation sizes and is still viable in modern glibc versions.
Prerequisites
- Tcache support: glibc >= 2.26
- Multiple free calls: At least 8 frees (7 for tcache, 1+ for fastbin)
- Write-after-free: Ability to corrupt freed chunk metadata
- Heap leak (glibc >= 2.32): Required for Safe Linking bypass
- Target address knowledge: Know where you want to write
- Controlled data: Optional but helpful for terminating list traversal
The Technique
Key Insight
When malloc needs to refill an empty tcache, it takes up to 7 chunks from the fastbin and inserts them in reverse order. This reversal process follows the forward pointers of each chunk and writes them as next pointers in the tcache.If we corrupt a forward pointer to point to a fake chunk at our target address, malloc will:
- Follow our corrupted pointer
- Write the “next” pointer (heap address) to our target
- Continue traversing the list
Step-by-Step Walkthrough
Fill tcache and fastbin
Allocate many chunks, then free 7 to fill tcache and more to populate fastbin.Now: tcache is full (7 chunks), fastbin has 7 chunks with victim in the list.
Prepare target address
Set up the memory location you want to write to.Initially, stack_var contains garbage values that will be overwritten.
Corrupt fastbin forward pointer
Use a vulnerability (buffer overflow, UAF) to overwrite victim’s forward pointer.
Empty the tcache
Allocate 7 times to completely drain the tcache.The tcache is now empty, setting up for the critical transfer.
Trigger fastbin-to-tcache transfer
The next allocation triggers the reverse transfer from fastbin to tcache.What happens:
- Tcache is empty, so malloc looks at fastbin
- Finds chunks in fastbin
- Takes up to 7 chunks in reverse order to refill tcache
- While traversing, follows our corrupted pointer
- Writes heap pointer value to stack_var[0]
Full Source Code
Key Concepts
Why reverse order?
Why reverse order?
The tcache refill process from fastbin works like this:Because tcache insertion is LIFO (at the head), chunks are reversed. When following forward pointers during this process, the allocator writes these pointers into the tcache structure - which includes writing to our target address!
Comparison with unsorted bin attack
Comparison with unsorted bin attack
Unsorted Bin Attack (patched in glibc 2.29):
- Worked with large chunks (>= 0x80 bytes)
- Wrote a heap/libc address to target
- Required unsorted bin manipulation
- Patched with stricter checks
- Works with small chunks (≤ 0x78 bytes)
- Writes a heap address to target
- Leverages tcache refill mechanism
- Still works in modern glibc (with heap leak)
Safe Linking impact
Safe Linking impact
On glibc >= 2.32, forward pointers are encoded:This requires:
- Heap leak: To know victim’s address
- Target address: Where we want to write
How many chunks to free?
How many chunks to free?
You need to free between 1 and 6 additional chunks after the victim:
- Exactly 6: If you don’t control target memory (safest)
- Fewer than 6: If you can forge a NULL pointer at the target to terminate traversal
- Why?: The traversal must eventually terminate with a valid or NULL pointer
Common Use Cases
- Overwrite stack variables: Write heap pointers to stack for later exploitation
- Corrupt heap metadata: Target tcache_perthread_struct or other allocator data
- Bypass ASLR: Leak addresses by writing heap pointers to readable locations
- Chain with other exploits: Use the arbitrary write as a first stage
Defense Mechanisms
Exploitation Notes
Tips for reliable exploitation:
- Heap leak is mandatory on glibc >= 2.32
- Target alignment: Ensure target address is 16-byte aligned
- List termination: Free enough chunks or forge NULL to terminate
- Allocation size: Works best with sizes 0x20-0x78 (fastbin range)
- Tcache management: Track how many chunks are in tcache vs fastbin
Variations
Fewer fastbin chunks
Fewer fastbin chunks
If you control data at the target:This reduces the number of required frees.
Related Techniques
- [Unsorted Bin Attack/techniques/bins/unsorted-bin-attack) - Similar goal, different bin
- Fastbin Dup Into Stack - Related fastbin corruption
- Tcache Poisoning - Direct tcache corruption
- Tcache Stashing Unlink - Similar transfer exploitation
Practice & Resources
Ret2 Wargames
Debug this technique interactively in your browser using GDB
