Skip to main content

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)
Patch Information: glibc commit b90ddd08 The patch added size consistency checks when allocating from the unsorted bin.

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
In glibc 2.27+, you need to avoid tcache by allocating chunks larger than 0x410 bytes, or by filling the tcache bins first.

The Vulnerability

This attack requires:
  1. Heap overflow - ability to overwrite the size field of a freed chunk
  2. Freed chunk in unsorted bin - target chunk must be in unsorted bin
  3. 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

Now p4 and p3 point to overlapping memory regions!

Step-by-Step Exploitation

1

Allocate Three Chunks

Create three chunks on the heap:
We subtract 8 (or 16 on some systems) to account for chunk metadata overhead.
2

Free Middle Chunk

Free p2 to place it in the unsorted bin:
3

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)
Setting PREV_INUSE (0x181) maintains heap stability by indicating p1 is still allocated.
4

Allocate Overlapping Chunk

Request allocation matching the corrupted size:
Malloc will:
  1. Find p2 in unsorted bin
  2. See size is 0x180 (close enough to request)
  3. Return the chunk
  4. p4 now extends over where p3 is located
5

Exploit the Overlap

Now you can manipulate memory through both pointers:

Full Source 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:
This checks that:
  1. Size is reasonable
  2. Next chunk’s prev_size matches current chunk’s size

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

  • [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

References