Overview
Fastbin Dup Into Stack extends the basic fastbin double-free attack to achieve arbitrary memory allocation. By corrupting the forward pointer in a freed fastbin chunk, we can trickmalloc() into returning a pointer to nearly any memory location - including the stack, data segments, or other controlled regions.
Glibc Version Compatibility: Latest (tested on glibc 2.23 - 2.41)Requires a heap address leak on glibc >= 2.32 due to Safe Linking.
What This Achieves
- Arbitrary allocation: Get
malloc()to return a pointer to attacker-controlled address - Stack control: Allocate memory on the stack for code execution primitives
- Memory corruption: Write to arbitrary locations through malloc’d pointers
- CTF primitive: Core technique for many heap exploitation challenges
Prerequisites
- Ability to perform a double-free (see Fastbin Dup)
- Write-after-free: Ability to corrupt freed chunk contents
- Heap leak (glibc >= 2.32): Required for Safe Linking bypass
- Target address: Knowledge of where you want to allocate (e.g., stack address)
- Fake chunk size: Target address must have valid size field
The Technique
Key Insight
Once we have a double-free, we control a chunk that’s still in the freelist. By writing to this chunk, we can corrupt the forward pointer that determines the next allocation target. This allows us to inject an arbitrary address into the freelist.
Step-by-Step Walkthrough
Prepare target address
Set up the target location with a fake chunk size. The size must pass basic checks.The target address should point to where the size field would be in a real chunk.
Allocate to get write access
Allocate twice to get back to the duplicated chunk.We have a pointer
d to chunk ‘a’, which is still in the freelist!Corrupt forward pointer
Overwrite the forward pointer with the target address. On glibc >= 2.32, must use Safe Linking encoding.
Allocate to inject fake chunk
Next allocation removes our corrupted chunk from the freelist and injects the fake address.
Full Source Code
Key Concepts
Safe Linking Bypass
Safe Linking Bypass
Starting with glibc 2.32, forward pointers in fastbins and tcache are obfuscated using Safe Linking:This means:
- You need to know the heap address of the corrupted chunk
- You need to know your target address
- The XOR operation reverses when malloc processes the pointer
Fake Chunk Requirements
Fake Chunk Requirements
For malloc to accept your fake chunk, it must pass several checks:
- Size field alignment: Must be at proper offset (usually +8 bytes from returned pointer)
- Size value: Must match the fastbin size we’re working with (0x20 for 8-byte allocations)
- Alignment: Address should be 16-byte aligned on 64-bit systems
- In-use bit: Usually set (lowest bit of size = 1)
Why use calloc() instead of malloc()?
Why use calloc() instead of malloc()?
The source code uses
calloc() instead of malloc(). This is important because:calloc()zeroes out the allocated memory- This can help bypass certain checks or initialize fake chunks
- In some cases, it’s just for clearer demonstration
malloc() as well.Common Use Cases
- Stack pivoting: Allocate on stack to control return addresses
- GOT overwrites: Allocate at GOT entries to hijack function pointers
- Arbitrary write: Get malloc to return pointer to target, then write via that pointer
- Bypass ASLR: When you have partial address leaks
Defense Mechanisms
CTF Challenges
This technique has been used in numerous CTF challenges:9447 CTF 2015
search-engine - Classic fastbin corruption challengeWriteup
0CTF 2017
babyheap - Modern heap exploitation with Safe Linking considerationsWriteup
Related Techniques
- Fastbin Dup - The foundation technique
- [House of Spirit/techniques/house/house-of-spirit) - Alternative approach to arbitrary allocation
- Tcache Poisoning - Similar technique for tcache
Practice & Resources
Ret2 Wargames
Debug this technique interactively in your browser using GDB
