Overview
Overlapping chunks is a heap exploitation technique where an attacker corrupts the size field of a freed chunk to cause malloc to allocate a region that overlaps with existing allocated chunks. This allows reading and writing data through two different pointers to the same memory region.This technique is taken from the Glibc Adventures: The Forgotten Chunks paper by Context Information Security.
Glibc Version Compatibility
Working Versions
glibc 2.23 - 2.28
Patched
glibc 2.29+ (April 2019)
What Does This Technique Achieve?
Overlapping chunks allows you to:- Create memory aliasing - two pointers to the same memory
- Read sensitive data - access chunk metadata through overlapping region
- Corrupt other chunks - modify fd/bk pointers of adjacent chunks
- Bypass protections - confuse heap integrity checks
The Vulnerability
This attack requires:- Heap overflow - ability to overwrite the size field of a freed chunk
- Freed chunk in unsorted bin - target chunk must be in unsorted bin
- Subsequent allocation - with size matching the corrupted size
Technical Details
Size Field Manipulation
When a chunk is freed and placed in the unsorted bin, its size field determines how much memory will be allocated when that chunk is reused. By increasing this size, we can make malloc return more memory than was originally freed.Heap Layout Before Attack
Heap Layout After Attack
p4 and p3 point to overlapping memory regions!
Step-by-Step Exploitation
Allocate Three Chunks
Create three chunks on the heap:
We subtract 8 (or 16 on some systems) to account for chunk metadata overhead.
Corrupt Size Field
Exploit an overflow to modify p2’s size field:
Why 0x181 instead of 0x180?The size field’s lower 3 bits are flags:
- Bit 0: PREV_INUSE (1 = previous chunk is in use)
- Bit 1: IS_MMAPPED (0 = not mmapped)
- Bit 2: NON_MAIN_ARENA (0 = main arena)
Allocate Overlapping Chunk
Request allocation matching the corrupted size:Malloc will:
- Find p2 in unsorted bin
- See size is 0x180 (close enough to request)
- Return the chunk
- p4 now extends over where p3 is located
Full Source Code
View Complete Exploit Code
View Complete Exploit Code
Exploitation Scenarios
1. Corrupting Chunk Metadata
The most powerful use case:2. Information Disclosure
3. Type Confusion
Bypassing Tcache (glibc 2.27+)
In glibc 2.27 and later, tcache intercepts small allocations. To use overlapping chunks:
Method 1: Use Large Chunks
Method 2: Fill Tcache First
The Patch (glibc 2.29)
The patch added size validation:- Size is reasonable
- Next chunk’s prev_size matches current chunk’s size
Related CTF Challenges
hack.lu CTF 2015
bookstore - Classic overlapping chunks
Nuit du Hack 2016
night-deamonic-heap - Advanced overlap usage
Practice
Try it in your browser
Debug this technique interactively on Ret2 Wargames
Variants
- Overlapping Chunks 2 - Variant using in-use chunks (also patched in 2.29)
- Mmap Overlapping Chunks - Same concept for mmap’d regions (still works!)
Related Techniques
- [Poison Null Byte/techniques/advanced/poison-null-byte) - Another way to create overlapping chunks
- [House of Einherjar/techniques/house/house-of-einherjar) - Uses similar size corruption concepts
