Skip to main content

Overview

Tcache poisoning is a heap exploitation technique that manipulates the tcache freelist to make malloc() return a pointer to an arbitrary memory location. This attack is very similar to the fastbin corruption attack but targets the tcache mechanism introduced in glibc 2.26.
The tcache (thread cache) is a per-thread cache for heap chunks that speeds up allocation and deallocation by avoiding locks. Each thread has up to 64 tcache bins, with each bin holding up to 7 chunks of the same size.

Glibc Version Compatibility

Works on: glibc 2.26 and later Important patches:
  • Commit 77dc0d86: Requires creating and freeing one more chunk for padding before fd pointer hijacking
  • Commit a1a486d7: Heap address leak required (glibc 2.32+) due to safe-linking protection. The same patch ensures returned chunks are properly aligned.

What This Technique Achieves

1

Allocate chunks and free them

Chunks are placed into the tcache freelist
2

Corrupt the fd pointer

Overwrite the forward pointer of a freed tcache chunk to point to the target address
3

Allocate to drain tcache

First malloc returns the legitimate chunk
4

Get arbitrary pointer

Second malloc returns a pointer to the target address (stack, data segment, etc.)

Key Differences from Fastbin Attack

  • No size checks: Tcache doesn’t verify the size field of the next chunk
  • No double-free protection: (in glibc 2.26-2.28, patched in 2.29+)
  • Simpler exploitation: Fewer checks make tcache poisoning easier than fastbin corruption
  • Safe-linking: From glibc 2.32+, fd pointers are mangled with (ptr ^ (heap_addr >> 12))

Source Code

Step-by-Step Walkthrough

1. Setup Target Address

The target must be 16-byte aligned (glibc 2.32+ requirement). We scan the stack array to find a properly aligned address.

2. Allocate and Free Chunks

After freeing, the tcache bin for size 128 looks like:

3. Poison the Tcache (Vulnerability Point)

This is where the vulnerability is exploited. An attacker with a write primitive can overwrite the fd pointer.
Safe-linking explanation (glibc 2.32+):
  • The fd pointer is XORed with (heap_addr >> 12)
  • This prevents simple pointer overwrites
  • Requires leaking a heap address to calculate the correct mangled pointer
After poisoning:

4. Trigger Arbitrary Allocation

The second malloc returns our target address, giving us control over an arbitrary memory location.

Prerequisites

1

Use-after-free or overflow

Ability to overwrite the fd pointer of a freed tcache chunk
2

Heap address leak (glibc 2.32+)

Required to calculate the mangled pointer for safe-linking
3

Target address alignment

Target must be 16-byte aligned (glibc 2.32+)

Detection and Mitigation

Mitigations added over time:
  • Safe-linking (glibc 2.32): Mangles fd pointers with heap address bits
  • Double-free detection (glibc 2.29): Added key field to detect double frees
  • Alignment checks (glibc 2.32): Ensures returned chunks are properly aligned
Detection methods:
  • Monitor for misaligned chunk pointers in tcache bins
  • Check for fd pointers pointing outside heap regions
  • Use heap profiling tools to detect anomalous allocation patterns

Try It Yourself

Practice on Ret2 Wargames

Debug this technique in an interactive browser environment