Tcache Exploitation Skill
A comprehensive guide for identifying and exploiting Tcache vulnerabilities in binary exploitation challenges.
When to Use This Skill
Use this skill when working with:
- Heap exploitation challenges (CTF, security research, vulnerability analysis)
- Binaries compiled with Glibc 2.26 or newer (Tcache introduced in 2.26)
- Vulnerabilities involving
malloc, free, double free, use-after-free, or heap overflow
- Challenges requiring libc address leaks or arbitrary writes
- Any mention of
tcache, malloc_hook, free_hook, or heap bin manipulation
Core Concepts
What is Tcache?
Tcache (thread cache) was introduced in Glibc 2.26 to improve memory allocation performance. It maintains per-thread caches of freed chunks before returning them to the main heap.
Key properties:
- Per-thread cache (not shared across threads)
- Stores up to 7 chunks per size class by default
- Uses a singly-linked list (FD pointer only)
- No size validation on allocation (unlike fast bins)
- No alignment checks in older versions (error:
malloc(): unaligned tcache chunk detected in newer versions)
Tcache Poisoning Attack
The Tcache attack (also called Tcache poisoning) is similar to fast bin attacks:
Goal: Overwrite the next pointer (FD) in a freed chunk to point to an arbitrary address, then allocate that address.
Basic flow:
- Allocate a chunk and free it (adds to tcache)
- Overflow/modify the freed chunk's FD pointer to point to target address
- Allocate a chunk of the same size → returns the target address
- Use the returned chunk to overwrite critical data (e.g.,
__malloc_hook)
Important: The target address must be aligned (8-byte aligned on 64-bit systems). If not aligned, you may need to run the binary multiple times or find an aligned target.
Tcache Index Attack
The tcache structure itself contains metadata that can be exploited:
Tcache structure layout:
- Array of chunk counts per size index
- Array of head pointers for each tcache bin
Attack pattern:
- Modify the tcache metadata (chunk count or head pointer)
- Make a tcache bin's head pointer point to a desired address (e.g.,
__malloc_hook)
- Allocate a chunk of that size → returns the target address
- Overwrite the target (e.g., write one-gadget to
__malloc_hook)
Common Attack Patterns
Pattern 1: Double Free Tcache Poisoning
Vulnerability: Double free of the same chunk
Exploitation:
- Allocate chunk A
- Free chunk A (A → NULL in tcache)
- Free chunk A again (A → A in tcache, self-referential)
- Allocate chunk A (returns A, tcache head now points to A's FD)
- Modify A's FD to point to
__free_hook
- Allocate again → returns chunk at
__free_hook
- Write
system address to __free_hook
- Free a chunk containing "/bin/sh" → shell
Pattern 2: Heap Overflow + Size Manipulation
Vulnerability: 1-byte heap overflow allowing size header modification
Exploitation:
- Allocate chunk A (small size)
- Overflow to increase A's size header
- Free chunk A (goes to tcache of fake larger size)
- Allocate chunk of fake size → returns A but with larger apparent size
- Use the extra space to overwrite next chunk's FD pointer
- Point FD to
__malloc_hook
- Allocate twice: first gets legit chunk, second gets
__malloc_hook
- Write one-gadget to
__malloc_hook
- Trigger malloc → shell
Pattern 3: Tcache Index Corruption
Vulnerability: Ability to free arbitrary heap addresses
Exploitation:
- Allocate and free a chunk of specific size that creates value 0x100 in tcache metadata
- The tcache stores chunk counts in specific bytes, creating exploitable values
- Free the address 0x100 (looks like a valid chunk)
- This adds 0x100 to the tcache index for size 0x100
- Allocate size 0x100 → returns address 0x100
- Use this to overwrite other tcache indexes
- Point an index to
__malloc_hook, allocate that size, write one-gadget
Pattern 4: Libc Leak via Unsorted Bin
Vulnerability: Use-after-free or double free
Exploitation:
- Fill all tcaches (7 chunks per size class)
- Add one more chunk → goes to unsorted bin
- Empty the tcache (allocate all cached chunks)
- Re-allocate the unsorted bin chunk
- Overwrite only first 8 bytes, leaving second address intact
- The second address is a libc pointer → leak libc base
Step-by-Step Exploitation Workflow
Step 1: Identify the Vulnerability
Look for:
- Double free vulnerabilities
- Use-after-free conditions
- Heap overflow (especially 1-byte overflows)
- Arbitrary free capabilities
- Uninitialized pointers in heap chunks
Step 2: Determine Glibc Version
Check if Tcache is available:
- Glibc 2.26+ has Tcache
- Use
ldd --version or check binary's linked libc
- Tcache errors:
malloc(): unaligned tcache chunk detected
Step 3: Map the Heap Layout
Understand:
- Where chunks are allocated
- Where tcache metadata is stored
- Distance between chunks and targets (
__malloc_hook, __free_hook)
- Alignment requirements (8-byte on 64-bit)
Step 4: Choose Attack Pattern
Based on vulnerability type:
- Double free → Pattern 1 (self-referential tcache)
- Heap overflow → Pattern 2 (size manipulation)
- Arbitrary free → Pattern 3 (tcache index corruption)
- Need libc leak → Pattern 4 (unsorted bin leak)
Step 5: Craft the Exploit
- Fill tcaches if needed (7 chunks per size class)
- Trigger the vulnerability (double free, overflow, etc.)
- Manipulate FD pointers to point to targets
- Allocate chunks to get control over target addresses
- Write payloads (one-gadget, system address, etc.)
- Trigger the payload (malloc, free, etc.)
Step 6: Handle Alignment
If getting unaligned tcache chunk detected:
- Find aligned target addresses
- Run binary multiple times until alignment works
- Use heap feng-shui to control alignment
Key Addresses to Target
| Address |
Purpose |
How to Exploit |
__malloc_hook |
Called on malloc |
Write one-gadget, trigger malloc |
__free_hook |
Called on free |
Write system, free "/bin/sh" |
stdout |
File pointer |
FSOP (File Stream Oriented Programming) |
tcache metadata |
Tcache structure |
Corrupt to control allocations |
Common One-Gadgets
Find one-gadgets in libc:
one_gadget /path/to/libc.so.6
Common patterns:
ret; pop rdi; ret + /bin/sh address
execve gadgets
system gadgets
Debugging Tips
Check Tcache State
Use GDB with heap commands:
heap analyze
heap chunks
heap tcache
Common Errors
malloc(): unaligned tcache chunk detected → Address not 8-byte aligned
double free or corruption → Tcache not being used, or older Glibc
corrupted size vs. prev_size → Size header corruption
Verify Exploit
- Check if libc leak works (compare with
ldd output)
- Verify FD pointer overwrites (GDB
x/gx command)
- Confirm hook addresses are writable
- Test one-gadget execution
Learning Resources
CTF Writeups (Study These Patterns)
DCQuals19 Babyheap - Libc leak + Tcache attack with 1-byte overflow
Plaid19 Cpp - Double free Tcache poisoning
CSAW19 Popping Caps0 - Tcache index attack
CSAW19 Popping Caps1 - Optimized tcache index attack
HTB Math Door - Write-after-free + heap feng-shui
Reference Material
Quick Reference
Tcache Size Classes
Tcache has 7 chunks per size class. Common sizes:
- 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0x100
Chunk Structure (64-bit)
[prev_size: 8 bytes]
[size: 8 bytes] (includes prev_inuse flag)
[FD pointer: 8 bytes] (tcache only, no BK)
[payload: size - 16 bytes]
Exploit Checklist
When This Skill Applies
Use this skill for:
- CTF heap exploitation challenges
- Security research on heap vulnerabilities
- Binary analysis involving malloc/free
- Learning heap exploitation techniques
- Writing exploit code for Tcache vulnerabilities
Do NOT use this skill for:
- Non-heap vulnerabilities (stack, format string, etc.)
- Glibc versions before 2.26 (no Tcache)
- Non-Glibc allocators (jemalloc, tcmalloc, etc.)
- High-level application bugs without heap primitives
1---2name: tcache-exploitation3description: How to identify and exploit Tcache bin attacks in heap vulnerabilities. Use this skill whenever the user mentions heap exploitation, tcache, malloc, free, heap overflow, double free, use-after-free, libc leaks, malloc_hook, free_hook, or any heap-related binary exploitation challenge. This skill covers Tcache poisoning, Tcache index attacks, and common CTF patterns for Glibc 2.26+.4metadata:5 source: https://skillmd.com/skills/abelrguezr/tcache-exploitation6---78# Tcache Exploitation Skill910A comprehensive guide for identifying and exploiting Tcache vulnerabilities in binary exploitation challenges.1112## When to Use This Skill1314Use this skill when working with:15- Heap exploitation challenges (CTF, security research, vulnerability analysis)16- Binaries compiled with Glibc 2.26 or newer (Tcache introduced in 2.26)17- Vulnerabilities involving `malloc`, `free`, double free, use-after-free, or heap overflow18- Challenges requiring libc address leaks or arbitrary writes19- Any mention of `tcache`, `malloc_hook`, `free_hook`, or heap bin manipulation2021## Core Concepts2223### What is Tcache?2425Tcache (thread cache) was introduced in Glibc 2.26 to improve memory allocation performance. It maintains per-thread caches of freed chunks before returning them to the main heap.2627**Key properties:**28- Per-thread cache (not shared across threads)29- Stores up to 7 chunks per size class by default30- Uses a singly-linked list (FD pointer only)31- No size validation on allocation (unlike fast bins)32- No alignment checks in older versions (error: `malloc(): unaligned tcache chunk detected` in newer versions)3334### Tcache Poisoning Attack3536The Tcache attack (also called Tcache poisoning) is similar to fast bin attacks:3738**Goal:** Overwrite the `next` pointer (FD) in a freed chunk to point to an arbitrary address, then allocate that address.3940**Basic flow:**411. Allocate a chunk and free it (adds to tcache)422. Overflow/modify the freed chunk's FD pointer to point to target address433. Allocate a chunk of the same size → returns the target address444. Use the returned chunk to overwrite critical data (e.g., `__malloc_hook`)4546**Important:** The target address must be aligned (8-byte aligned on 64-bit systems). If not aligned, you may need to run the binary multiple times or find an aligned target.4748### Tcache Index Attack4950The tcache structure itself contains metadata that can be exploited:5152**Tcache structure layout:**53- Array of chunk counts per size index54- Array of head pointers for each tcache bin5556**Attack pattern:**571. Modify the tcache metadata (chunk count or head pointer)582. Make a tcache bin's head pointer point to a desired address (e.g., `__malloc_hook`)593. Allocate a chunk of that size → returns the target address604. Overwrite the target (e.g., write one-gadget to `__malloc_hook`)6162## Common Attack Patterns6364### Pattern 1: Double Free Tcache Poisoning6566**Vulnerability:** Double free of the same chunk6768**Exploitation:**691. Allocate chunk A702. Free chunk A (A → NULL in tcache)713. Free chunk A again (A → A in tcache, self-referential)724. Allocate chunk A (returns A, tcache head now points to A's FD)735. Modify A's FD to point to `__free_hook`746. Allocate again → returns chunk at `__free_hook`757. Write `system` address to `__free_hook`768. Free a chunk containing "/bin/sh" → shell7778### Pattern 2: Heap Overflow + Size Manipulation7980**Vulnerability:** 1-byte heap overflow allowing size header modification8182**Exploitation:**831. Allocate chunk A (small size)842. Overflow to increase A's size header853. Free chunk A (goes to tcache of fake larger size)864. Allocate chunk of fake size → returns A but with larger apparent size875. Use the extra space to overwrite next chunk's FD pointer886. Point FD to `__malloc_hook`897. Allocate twice: first gets legit chunk, second gets `__malloc_hook`908. Write one-gadget to `__malloc_hook`919. Trigger malloc → shell9293### Pattern 3: Tcache Index Corruption9495**Vulnerability:** Ability to free arbitrary heap addresses9697**Exploitation:**981. Allocate and free a chunk of specific size that creates value 0x100 in tcache metadata992. The tcache stores chunk counts in specific bytes, creating exploitable values1003. Free the address 0x100 (looks like a valid chunk)1014. This adds 0x100 to the tcache index for size 0x1001025. Allocate size 0x100 → returns address 0x1001036. Use this to overwrite other tcache indexes1047. Point an index to `__malloc_hook`, allocate that size, write one-gadget105106### Pattern 4: Libc Leak via Unsorted Bin107108**Vulnerability:** Use-after-free or double free109110**Exploitation:**1111. Fill all tcaches (7 chunks per size class)1122. Add one more chunk → goes to unsorted bin1133. Empty the tcache (allocate all cached chunks)1144. Re-allocate the unsorted bin chunk1155. Overwrite only first 8 bytes, leaving second address intact1166. The second address is a libc pointer → leak libc base117118## Step-by-Step Exploitation Workflow119120### Step 1: Identify the Vulnerability121122Look for:123- Double free vulnerabilities124- Use-after-free conditions125- Heap overflow (especially 1-byte overflows)126- Arbitrary free capabilities127- Uninitialized pointers in heap chunks128129### Step 2: Determine Glibc Version130131Check if Tcache is available:132- Glibc 2.26+ has Tcache133- Use `ldd --version` or check binary's linked libc134- Tcache errors: `malloc(): unaligned tcache chunk detected`135136### Step 3: Map the Heap Layout137138Understand:139- Where chunks are allocated140- Where tcache metadata is stored141- Distance between chunks and targets (`__malloc_hook`, `__free_hook`)142- Alignment requirements (8-byte on 64-bit)143144### Step 4: Choose Attack Pattern145146Based on vulnerability type:147- **Double free** → Pattern 1 (self-referential tcache)148- **Heap overflow** → Pattern 2 (size manipulation)149- **Arbitrary free** → Pattern 3 (tcache index corruption)150- **Need libc leak** → Pattern 4 (unsorted bin leak)151152### Step 5: Craft the Exploit1531541. **Fill tcaches** if needed (7 chunks per size class)1552. **Trigger the vulnerability** (double free, overflow, etc.)1563. **Manipulate FD pointers** to point to targets1574. **Allocate chunks** to get control over target addresses1585. **Write payloads** (one-gadget, system address, etc.)1596. **Trigger the payload** (malloc, free, etc.)160161### Step 6: Handle Alignment162163If getting `unaligned tcache chunk detected`:164- Find aligned target addresses165- Run binary multiple times until alignment works166- Use heap feng-shui to control alignment167168## Key Addresses to Target169170| Address | Purpose | How to Exploit |171|---------|---------|----------------|172| `__malloc_hook` | Called on malloc | Write one-gadget, trigger malloc |173| `__free_hook` | Called on free | Write system, free "/bin/sh" |174| `stdout` | File pointer | FSOP (File Stream Oriented Programming) |175| `tcache` metadata | Tcache structure | Corrupt to control allocations |176177## Common One-Gadgets178179Find one-gadgets in libc:180```bash181one_gadget /path/to/libc.so.6182```183184Common patterns:185- `ret; pop rdi; ret` + `/bin/sh` address186- `execve` gadgets187- `system` gadgets188189## Debugging Tips190191### Check Tcache State192193Use GDB with `heap` commands:194```gdb195heap analyze196heap chunks197heap tcache198```199200### Common Errors201202- `malloc(): unaligned tcache chunk detected` → Address not 8-byte aligned203- `double free or corruption` → Tcache not being used, or older Glibc204- `corrupted size vs. prev_size` → Size header corruption205206### Verify Exploit2072081. Check if libc leak works (compare with `ldd` output)2092. Verify FD pointer overwrites (GDB `x/gx` command)2103. Confirm hook addresses are writable2114. Test one-gadget execution212213## Learning Resources214215### CTF Writeups (Study These Patterns)2162171. **DCQuals19 Babyheap** - Libc leak + Tcache attack with 1-byte overflow218 - https://guyinatuxedo.github.io/29-tcache/dcquals19_babyheap/index.html2192202. **Plaid19 Cpp** - Double free Tcache poisoning221 - https://guyinatuxedo.github.io/29-tcache/plaid19_cpp/index.html2222233. **CSAW19 Popping Caps0** - Tcache index attack224 - https://guyinatuxedo.github.io/44-more_tcache/csaw19_popping_caps0/index.html2252264. **CSAW19 Popping Caps1** - Optimized tcache index attack227 - https://guyinatuxedo.github.io/44-more_tcache/csaw19_popping_caps1/index.html2282295. **HTB Math Door** - Write-after-free + heap feng-shui230 - https://7rocky.github.io/en/ctf/other/htb-cyber-apocalypse/math-door/231232### Reference Material233234- **Guy In A Tuxedo Tcache Explanation** - Comprehensive Tcache guide235 - https://guyinatuxedo.github.io/29-tcache/tcache_explanation/index.html236237- **Bins and Memory Allocations** - Heap bin fundamentals238 - See: `bins-and-memory-allocations.md`239240## Quick Reference241242### Tcache Size Classes243244Tcache has 7 chunks per size class. Common sizes:245- 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0x100246247### Chunk Structure (64-bit)248249```250[prev_size: 8 bytes]251[size: 8 bytes] (includes prev_inuse flag)252[FD pointer: 8 bytes] (tcache only, no BK)253[payload: size - 16 bytes]254```255256### Exploit Checklist257258- [ ] Identify vulnerability type259- [ ] Confirm Glibc 2.26+260- [ ] Map heap layout261- [ ] Choose attack pattern262- [ ] Fill tcaches if needed263- [ ] Trigger vulnerability264- [ ] Overwrite FD pointers265- [ ] Allocate target chunks266- [ ] Write payload267- [ ] Trigger payload268- [ ] Handle alignment issues269270## When This Skill Applies271272Use this skill for:273- CTF heap exploitation challenges274- Security research on heap vulnerabilities275- Binary analysis involving malloc/free276- Learning heap exploitation techniques277- Writing exploit code for Tcache vulnerabilities278279Do NOT use this skill for:280- Non-heap vulnerabilities (stack, format string, etc.)281- Glibc versions before 2.26 (no Tcache)282- Non-Glibc allocators (jemalloc, tcmalloc, etc.)283- High-level application bugs without heap primitives