Safe-Linking – Eliminating a 20 year-old malloc() exploit primitive
The single-linked lists used by malloc() implementations (Fast-Bins and TCache) stored next-chunk pointers without protection, so a heap buffer overflow could overwrite a pointer and turn a limited overflow into an arbitrary-malloc primitive. The authors designed Safe-Linking, which XORs each stored pointer with a value derived from its own address and checks alignment, and got it integrated into glibc and uClibc-NG.
research.checkpoint.com
Published on research.checkpoint.com
One of our goals for every research project we work on in Check Point Research is to get an intimate understanding of how software work: What components do they contain? Are they vulnerable?
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the allocator stored the next-free-chunk pointer in single-linked lists (Fast-Bins, TCache) as a plain, unprotected value, so a heap overflow could overwrite it and redirect later allocations.
Impact
Without a pointer leak, an attacker could turn a heap overflow into an arbitrary allocation at a chosen address; Safe-Linking statistically blocks pointer hijacking and unaligned chunks, though it is not a magic bullet and does not stop all heap exploits.
What the attacker needed
A heap buffer overflow or underflow; no heap or pointer leak available; attacker cannot predict ASLR heap bits
The fix
The fix XORs each stored single-linked-list pointer with its own address shifted by the page size and rejects unaligned revealed pointers; it was integrated into glibc (release 2.32) and released in uClibc-NG v1.0.33, while gperftools integration was pending and Google's TCMalloc rejected it.
How it was found
- Find a heap-based buffer overflow that gives a small, controllable overwrite of an adjacent free chunk.
- Overflow into the freed chunk's size and next-pointer fields to point the list at an arbitrary address.
- Trigger allocations of the matching size to receive a chunk at the chosen address (malloc-where).
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
When a program frees memory, the memory allocator keeps a list of free blocks so it can hand them out again later. In some allocators those lists stored the address of the next free block as a plain number, so a bug that lets an attacker write past one buffer could rewrite that number and make the allocator later return a block at an address the attacker chose. Safe-Linking scrambles each stored address with a value derived from where it is stored, so an attacker who does not know the random memory layout cannot forge a useful value.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.