Overview
The unsorted bin attack is a heap exploitation technique that allows writing a large value (a heap address) to an arbitrary memory location. It exploits the linking mechanism of the unsorted bin to perform an arbitrary write, typically used as a setup step for more complex attacks.Glibc Version Compatibility
Working Versions
glibc 2.23 - 2.28
Patched
glibc 2.29+ (April 2019)
What Does This Technique Achieve?
This attack allows you to:- Write a large value (heap address) to an arbitrary memory location
- Overwrite
global_max_fast- enable fastbin attacks on larger chunks - Corrupt function pointers - if you can tolerate a heap address value
- Setup for further exploitation - prepare the heap for follow-up attacks
The unsorted bin attack is generally prepared for further attacks. A common target is the
global_max_fast variable in libc, which when overwritten enables fastbin attacks on arbitrarily large chunks.The Vulnerability
The attack requires:- Heap overflow or UAF - ability to overwrite a freed chunk’s
bkpointer - Known target address - where you want to write the large value
- Subsequent malloc - to trigger the unsorted bin processing
Technical Details
Unsorted Bin Linking
When a chunk is allocated from the unsorted bin, malloc performs:victim->bk, we control where bck->fd points, causing malloc to write unsorted_chunks(av) (a heap address) to target_address - 16 bytes.
The Write Primitive
The offset of 16 bytes accounts for the
fd pointer position in the chunk structure. On 32-bit systems, use offset of 8 bytes.Memory Layout
Step-by-Step Exploitation
1
Allocate and Free Chunk
Create a chunk and free it to place in unsorted bin:After this,
p->bk points back to unsorted_chunks(av).2
Identify Target
Choose your target address (must be writable):
3
Corrupt bk Pointer
Exploit vulnerability to overwrite the
bk pointer:4
Trigger Allocation
Call malloc to trigger the unsorted bin processing:During this malloc:
- Victim is removed from unsorted bin
bck = victim->bk=&stack_var - 2bck->fd = unsorted_chunks(av)writes heap address tostack_var
5
Verify the Write
Check that the target was overwritten:
Full Source Code
View Complete Exploit Code
View Complete Exploit Code
Common Attack Targets
1. global_max_fast
The most common target for unsorted bin attack is
global_max_fast in libc.global_max_fast with a large value allows you to:
- Treat larger chunks as fastbin chunks
- Bypass fastbin size checks
- Enable fastbin attacks on arbitrary-sized chunks
2. Function Pointers
If you can tolerate the heap address value:Why This Works
The key is in the unsorted bin’s doubly-linked list management:The Patch (glibc 2.29)
The patch added this check:victim->bk->fd points back to victim, preventing the attack.
Exploitation in Practice
1
Leak libc address
Use unsorted bin to leak libc addresses first
2
Unsorted bin attack
Overwrite
global_max_fast to large value3
Fastbin attack
Exploit fastbin to get arbitrary allocation
4
Code execution
Overwrite
__malloc_hook or similarRelated CTF Challenges
0CTF 2016
zerostorage - Classic unsorted bin attack usage
Practice
Try it in your browser
Debug this technique interactively on Ret2 Wargames (glibc 2.27)
Related Techniques
- Unsorted Bin Into Stack - Get arbitrary allocation instead of write
- Large Bin Attack - Similar concept with large bins (still works in modern glibc)
- [House of Roman/techniques/house/house-of-roman) - Combines unsorted bin attack with other primitives
