Overview
Tcache Relative Write is a powerful technique that exploits insufficient bounds checking on tcache bin indices to achieve out-of-bounds writes in thetcache_perthread_struct metadata. By writing a large value to mp_.tcache_bins, attackers can control where tcache metadata (counts and pointers) are written, enabling arbitrary decimal writes and chunk pointer writes on the heap.
Glibc Compatibility: Works on glibc 2.30+ where the tcache index vulnerability exists.
What It Achieves
This technique allows you to:- Write arbitrary decimal values (0-7+) to arbitrary heap locations
- Write chunk pointers to arbitrary heap locations
- Corrupt tcache metadata for further exploitation
- Enable tcache poisoning without direct freelist corruption
- Trigger heap leaks by manipulating metadata
The Core Vulnerability
The allocator doesn’t properly validate tcache bin indices. When you corruptmp_.tcache_bins with a large value, you bypass the only check:
mp_.tcache_bins huge, any tc_idx passes the check, even out-of-bounds indices.
Full Source Code
Step-by-Step Walkthrough
1
Obtain Libc Leak
Free a large chunk (>0x408) to get libc pointers:
2
Corrupt mp_.tcache_bins
Calculate the location of This can be achieved via:
mp_.tcache_bins relative to the leak and write a huge value:- Large bin attack
- Fastbin reverse into tcache
- House of Mind (fastbins)
- Any technique that writes large values to arbitrary locations
3
Calculate Target Delta
Compute the offset from tcache metadata to your target location:
4
Calculate Required Chunk Size
Use the formulas to determine what size chunk to allocate:
5
Trigger the Write
Allocate and free a chunk of the computed size:When
free calls tcache_put, the out-of-bounds tc_idx causes writes to your chosen location.6
Exploit the Primitive
Use the written value for further exploitation:
- Corrupt chunk sizes for overlapping chunks
- Write chunk pointers for tcache poisoning
- Manipulate metadata for other attacks
The Mathematics
Understanding tc_idx Calculation
The tcache index is computed as:Write Location Calculation
For pointer writes:Example Calculation
Example Calculation
Suppose:
tcache_base = 0x555555559000- Target location:
0x5555555596b8(want to write counter here)
Attack Patterns
Pattern 1: Overlapping Chunks
Corrupt a chunk’s size field to create overlaps:Pattern 2: Tcache Metadata Poisoning
Write chunk pointers intotcache->entries for poisoning:
Pattern 3: Heap Leak
Write chunk pointers to leak heap addresses:Why This Is Advanced
Tcache Relative Write is considered advanced because:- Complex Prerequisites: Requires libc leak + ability to write large values
- Mathematical Precision: Must calculate exact chunk sizes from deltas
- Pointer Arithmetic Mastery: Understanding C pointer math and memory layout
- Multi-Stage Exploitation: Often used as primitive for more complex chains
- Version-Specific Offsets: Tcache structure layout varies by glibc version
Limitations and Considerations
Achieving Fully Arbitrary Writes
If you can also corruptmp_.tcache_count to a large value, you can write arbitrary decimals by freeing the same chunk multiple times:
Combining with Other Techniques
With Large Bin Attack
Use large bin attack to corruptmp_.tcache_bins:
Large Bin Attack
Learn how to write large values to arbitrary locations
With House of Mind
Debugging Tips
Use GDB with pwndbg/gef to visualize:CTF Applications
This technique has been useful in:- Modern CTF challenges with limited primitives
- Scenarios where direct tcache poisoning is prevented
- Challenges requiring heap leak primitives
- Situations where you have large bin attack but need more control
Common Pitfalls
Implementation Helpers
Related Techniques
- Large Bin Attack - To corrupt
mp_.tcache_bins - [House of Mind Fastbins/techniques/house/house-of-mind-fastbins) - Alternative corruption method
- [Tcache Poisoning/techniques/tcache/tcache-poisoning) - What you can do with pointer writes
- Overlapping Chunks - Using counter writes
References
- Original Research by D4R30
- PoC by Mahdyar Bahrami (D4R30)
- Applicable on glibc 2.30+ (tested through 2.41)
See Also
- Fastbin Reverse Into Tcache - Alternative for corruption
- Tcache Stashing Unlink Attack - Related metadata manipulation
