Lesson 3 of 14 · 13m
The Heap: malloc, Chunks, and Freelist Metadata
What malloc and free really do in glibc — chunk headers, bins, the tcache — and why allocator metadata living inside attacker memory is the whole heap game.
malloc is a memory allocator, not a syscall
Here is what happens when you call malloc(24). You do not enter the kernel. glibc's allocator — ptmalloc2, a fork of dlmalloc — picks a bin, finds or carves a chunk, stamps an 8-byte header on it, and hands you back chunk + 16. The kernel only gets involved when the arena needs more memory to carve: brk/sbrk grows the heap region upward, and mmap handles allocations ≥ 128 KB (M_MMAP_THRESHOLD).
Every chunk glibc returns is wrapped in bookkeeping that sits in the heap itself, directly adjacent to your data:
chunk: [ prev_size (8) ][ size | flags (8) ]
user: [ your bytes ... ]size's low three bits are flags: P (previous chunk in use), M (mmap'd), N (non-main arena). Free a chunk and it goes into a bin — recycled memory, not freed memory. Since glibc 2.26 (2017), the first stop is the tcache: a per-thread singly-linked list of up to 7 same-size chunks, checked before every other bin. The pointer to the next free tcache chunk (e->next) is stored inside the freed user area — in the bytes you used to own.
That sentence is the entire basis of modern heap exploitation. Allocator metadata lives inline with attacker data, in attacker-reachable memory, protected only by consistency checks the allocator added incrementally — and only after someone demonstrated each abuse in the wild.
The wrong mental model
The model people carry is free(p) meaning "this memory is gone." Nothing is gone. free links the chunk into a bin and leaves the bytes where they were; the next malloc of a matching size hands back the same address. Two consequences follow. A use-after-free is not exotic — it is what happens when you keep a pointer to memory that malloc now manages as a free chunk, and writing through that stale pointer writes into freelist metadata (the next pointer). And a double free is not "freeing twice so it crashes" — on glibc before 2.32, or when you bypass the tcache e->key check, it puts the same chunk on the freelist twice, so two later mallocs return the same address to different callers. Two objects aliasing one allocation is arbitrary-write waiting to happen.
Prove it in the real world
The allocator's inline-metadata design has been production-grade for thirty years and exploitable for all of them. CVE-2021-3156, "Baron Samedit" — disclosed January 26, 2021 by Qualys — was a heap overflow in sudo's argv unescaping (sudoedit -s '\\), a single byte miscount that smashed adjacent chunk data into local root on effectively every Linux distribution since 2011. CVE-2023-4911, "Looney Tunables" (Qualys, October 2023), overflowed the heap inside ld.so parsing GLIBC_TUNABLES — the allocator's own runtime — into root on Fedora, Ubuntu, Debian defaults. Neither needed kernel bugs. Both needed exactly this lesson: heap layout, chunk adjacency, and what the allocator trusts.
There is a second arena worth knowing exists: the main arena is one big freelist set per thread, but glibc also lets threads allocate arenas on demand — which is why gef heap arenas on a multithreaded target shows more than one. And mmap-threshold chunks bypass bins entirely: free on a M-flagged chunk calls munmap, and the memory leaves the address space. That distinction — "bin-recycled" vs "unmapped" — is what decides whether a dangling pointer reads stale data or faults.
Land on practice
Learn the bins in order: tcache (7 per size, per-thread, next in user data) → fastbins (size-classed LIFO) → unsorted → small/large. Read them in GDB: gef heap bins tcache, gef heap chunks. Then read one exploit primitive at a time — tcache poisoning first (corrupt e->next, get malloc to return an arbitrary address), since glibc ≤2.31 shipped no check on it at all and it teaches you why safe-linking (glibc 2.32, 2020) was added: e->next is now XOR'd with chunk_addr >> 12, so a poisoned pointer without a heap leak produces garbage, not control.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
A Password-Free Path to Root: Inside the macOS Screen Sharing Vulnerability Now Under Attack
A logic bug in screensharingd let attackers skip authentication entirely and land as root — no password required. Here's how it was found, patched, reverse-engineered, and finally weaponized.
BlueHammer: The Unpatched Windows Zero-Day That Weaponizes Microsoft Defender Against Your Own System
BlueHammer is an unpatched, publicly released Windows local privilege escalation exploit that chains Microsoft Defender's update workflow, Volume Shadow Copy, Cloud Files callbacks, and opportunistic locks to reach NT AUTHORITY\SYSTEM from a standard user account.
What is Privilege Escalation? How Attackers Go From Foothold to Full Control
An attacker starts with a low-privileged shell or user account and escalates to root or Administrator using misconfigurations, SUID binaries, kernel exploits, and Active Directory attacks. Here's every technique, the tools professionals use, and how to harden against it.
Jellyfin's Threat Model Is Your Brother-in-Law
Four Jellyfin CVEs landed in 2026 and not one of them needs an admin account. One needs only the ability to name a file. Meanwhile Jellyfin 12 shipped in September, the version numbers stopped starting with 10, and every "am I patched" check you wrote is now wrong.