Heap Exploitation
The heap is the dynamic memory managed by malloc/free. Unlike the stack, there are no return addresses to overwrite, but the memory manager (the allocator) stores metadata between and around chunks, and that metadata can be corrupted. Heap exploitation consists of manipulating that metadata —via overflows, use-after-free, double-free— to trick the allocator into arbitrary writes that end in control of the flow. It’s more complex than the stack and very dependent on the libc version.
Threat model
Section titled “Threat model”The attacker controls allocations and frees through a typical CTF menu (add / edit / free / show) and can trigger at least one misuse: writing past a chunk, freeing twice, or using a pointer after free. The goal is to turn that metadata corruption into an arbitrary read/write primitive and, from there, control of RIP. Lab/CTF scenario, with a known libc version.
Theory: the allocator (glibc / ptmalloc)
Section titled “Theory: the allocator (glibc / ptmalloc)”A chunk has a metadata header right before the user data:
prev_size— size of the previous chunk if it’s free.size— size of this chunk, with the low 3 bits as flags (PREV_INUSE,IS_MMAPPED,NON_MAIN_ARENA).- Then the data area returned by
malloc.
On free, free chunks are stored in linked lists (bins):
- tcache (glibc ≥ 2.26) — fast per-size LIFO lists, the most common modern target.
- fastbins — LIFO lists for small chunks.
- unsorted / small / large bins — the rest.
The key: when a chunk is freed, the allocator writes list pointers (fd, bk) into its data area. If you control those pointers, you control where the allocator “thinks” free chunks are → the next malloc returns a pointer wherever you want.
flowchart TD
A["free(A)"] --> B["A enters tcache; A->fd = next free"]
B --> C["UAF/overflow: edit A->fd = &target"]
C --> D["malloc() -> returns A"]
D --> E["malloc() -> returns &target"]
E --> F["arbitrary write/read → __free_hook / GOT / FSOP"]
F --> G["system / one_gadget → shell"]
Heap vulnerability classes
Section titled “Heap vulnerability classes”- Heap overflow — write past a chunk and corrupt the next chunk’s header.
- Use-After-Free (UAF) — use a chunk after
free: read/write another’s data, or the bin pointers. - Double-Free — free twice and corrupt the lists (tcache/fastbin).
- Type confusion — treat an object as if it were another type.
Classic techniques (depending on libc version)
Section titled “Classic techniques (depending on libc version)”- tcache poisoning (the modern bread and butter) — corrupt a tcache chunk’s
fdso the nextmallocreturns a controlled address. - fastbin dup — double-free in fastbin so the allocator returns the same chunk twice.
- unlink (old) — abuse the
unlinkmacro for an arbitrary write. - House of X — a family of advanced techniques (Spirit, Force, Einherjar, Orange…) for limited primitives.
Exploitation (lab): tcache poisoning
Section titled “Exploitation (lab): tcache poisoning”With a UAF or double-free over tcache, the flow is:
from pwn import *context.binary = elf = ELF('./heapnote')
io = process('./heapnote')# (add/edit/free/show functions wrap the challenge menu)
add(0, 0x30) # chunk Afree(0) # A -> tcache; A->fd points to the next free# UAF: overwrite A->fd with the target address (e.g. __free_hook)target = libc.sym['__free_hook']edit(0, p64(target ^ (heap_base >> 12))) # safe-linking if glibc >= 2.32add(1, 0x30) # malloc returns Aadd(2, 0x30) # malloc returns &target -> controlled writeedit(2, p64(libc.sym['system'])) # __free_hook = system# now free() of a chunk containing "/bin/sh" runs system("/bin/sh")From arbitrary write to shell
Section titled “From arbitrary write to shell”- Old libc (< 2.34): overwrite
__free_hook = systemthenfree(chunk)with"/bin/sh"inside; or__malloc_hook+one_gadget. - Modern libc (≥ 2.34, no hooks): FSOP (File Stream Oriented Programming), overwrite
_IO_FILE/_IO_list_allstructures, or reachone_gadgetanother way.
Version dependence
Section titled “Version dependence”Heap exploitation changes a lot between glibc versions due to added mitigations. Always identify the exact libc version and use the technique that applies:
- tcache key (≥ 2.29) — detects naive double-free in tcache.
- safe-linking (≥ 2.32) — tcache/fastbin
fdpointers are “encrypted” with(address >> 12); you need a heap leak to compute the right value. - hook removal (≥ 2.34) —
__malloc_hook/__free_hookare gone; forces FSOP or other routes.
- pwntools — interaction and packing.
- GDB + pwndbg/GEF —
heap,bins,tcachecommands to view the allocator state. - how2heap (shellphish) — reproducible examples of each technique, per version.
Mitigations / Blue Team
Section titled “Mitigations / Blue Team”- Up-to-date glibc (tcache key, safe-linking, no hooks).
- Hardened allocators (e.g. hardened_malloc, Scudo) in critical environments.
- Sanitizers (ASan) to detect UAF/heap-overflow in development and fuzzing.
- Memory-safe languages (Rust) eliminate UAF/double-free by design.
- Set pointers to
NULLafterfree; review pointer and size handling.
Checklist
Section titled “Checklist”- Identify the exact glibc version
- Identify the bug: overflow / UAF / double-free
- View the heap state in GDB (
heap/bins/tcache) - Heap leak if safe-linking is present (≥ 2.32)
- tcache/fastbin poisoning →
mallocreturns a controlled address - Write-what-where →
__free_hook/GOT/FSOP →system/one_gadget - Shell/flag locally → remote
- Practice the specific technique with how2heap