Overview
Fastbin Dup is a classic double-free attack that exploits the fastbin freelist to trickmalloc() into returning an already-allocated heap pointer. By carefully ordering free operations, an attacker can bypass the double-free check and create a duplicate pointer in the freelist.
Glibc Version Compatibility: Latest (tested on glibc 2.23 - 2.41)This technique works on modern glibc versions but requires filling the tcache first (on glibc >= 2.26).
What This Achieves
- Duplicate allocation: Get
malloc()to return the same pointer twice - Memory corruption: Modify the same memory region through multiple pointers
- Heap control: Manipulate allocator metadata for further exploitation
Prerequisites
- Ability to trigger multiple
free()calls - Control over allocation order
- On glibc >= 2.26: Need to fill tcache first (7 frees of the same size)
The Technique
Step-by-Step Walkthrough
Fill the tcache
On modern glibc (>= 2.26), allocate and free 7 chunks of the same size to fill the tcache. This forces subsequent frees into the fastbin.
Allocate target chunks
Allocate three chunks. We’ll use the first one (A) for the double-free attack.
Free an intermediate chunk
Cannot directly free A again (double-free check). Free B first to move A away from the head.
Free the first chunk again
Now A is no longer at the head, so we can free it again without triggering the check.
Full Source Code
Key Concepts
Why does this work?
Why does this work?
The fastbin uses a singly-linked list (LIFO) structure. The double-free check only verifies that the chunk being freed is not at the head of the freelist. By freeing an intermediate chunk (B), we move A away from the head position, allowing it to be freed again.This creates a cycle in the freelist:
[a -> b -> a]. When we allocate:- First malloc returns A (head)
- Second malloc returns B (new head)
- Third malloc returns A again (following the cycle)
What about the tcache?
What about the tcache?
On glibc >= 2.26, the tcache was introduced as a per-thread cache that sits before fastbins. The tcache holds up to 7 chunks per size. You must fill it first (with 7 frees) to force subsequent frees into the fastbin, where this technique applies.
Safe Linking (glibc >= 2.32)
Safe Linking (glibc >= 2.32)
Starting with glibc 2.32, Safe Linking was introduced to make exploitation harder. However, this basic fastbin_dup still works because we’re not corrupting the forward pointer - we’re exploiting the logic of the double-free check itself.
Common Use Cases
- Overlapping chunks: Create multiple pointers to the same memory for UAF-style attacks
- Heap feng shui: Control heap layout by manipulating allocation patterns
- Primitive building block: Often the first step in more complex exploits
Defense Mechanisms
Related Techniques
- Fastbin Dup Into Stack - Extends this to arbitrary write
- Fastbin Dup Consolidate - Alternative approach using malloc_consolidate
- [House of Spirit/techniques/house/house-of-spirit) - Complementary technique using fake chunks
Practice & Resources
Ret2 Wargames
Debug this technique interactively in your browser using GDB
