Skip to main content

Overview

Tcache Relative Write is a powerful technique that exploits insufficient bounds checking on tcache bin indices to achieve out-of-bounds writes in the tcache_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
Prerequisites:
  • Ability to write a large value (>64) to mp_.tcache_bins
  • Libc leak to locate mp_ structure
  • Ability to malloc/free with sizes > 0x408 (larger than tcache max)

The Core Vulnerability

The allocator doesn’t properly validate tcache bin indices. When you corrupt mp_.tcache_bins with a large value, you bypass the only check:
By making 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 mp_.tcache_bins relative to the leak and write a huge value:
This can be achieved via:
  • 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:
For counter writes:
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 into tcache->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:
  1. Complex Prerequisites: Requires libc leak + ability to write large values
  2. Mathematical Precision: Must calculate exact chunk sizes from deltas
  3. Pointer Arithmetic Mastery: Understanding C pointer math and memory layout
  4. Multi-Stage Exploitation: Often used as primitive for more complex chains
  5. Version-Specific Offsets: Tcache structure layout varies by glibc version

Limitations and Considerations

Important Limitations:
  1. Requires Libc Leak: Must know mp_ address to corrupt tcache_bins
  2. Limited Write Values:
    • Counters: Write small integers (incremented on each free)
    • Pointers: Write chunk addresses (the freed chunk)
  3. Size Constraints: Chunk sizes must be valid and allocatable
  4. ASLR Considerations: Heap addresses change, but deltas remain constant

Achieving Fully Arbitrary Writes

If you can also corrupt mp_.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 corrupt mp_.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

Avoid These Mistakes:
  1. Wrong offset calculation: tcache->counts is at +0x10, tcache->entries at +0x80
  2. Forgetting alignment: Chunk sizes must be 0x10-aligned
  3. Off-by-one in formula: The formulas are precise - don’t adjust them
  4. Not accounting for chunk header: User size is nb - 0x10
  5. ASLR confusion: Use deltas, not absolute addresses in formulas

Implementation Helpers

  • 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

See Also