Skip to main content

Overview

The large bin attack is a powerful heap exploitation technique that leverages the insertion logic of chunks into large bins. Unlike the unsorted bin attack (patched in 2.29), large bin attack still works in modern glibc and allows writing heap addresses to two arbitrary memory locations simultaneously.
This technique is based on research by dangokyo’s 2018 blog post and continues to work in the latest glibc versions.

Glibc Version Compatibility

Working Versions

All versions (2.23 - 2.41+)

Status

Still working in modern glibc!

What Does This Technique Achieve?

Large bin attack allows you to:
  • Write two heap addresses to arbitrary memory locations in a single operation
  • Overwrite global_max_fast - enable fastbin attacks on larger chunks
  • Corrupt function pointers - prepare for code execution
  • Bypass modern protections - still works after unsorted bin attack was patched
Chunks must be in the large bin size range (> 0x400 bytes on x64) for this technique to work.

The Vulnerability

This attack requires:
  1. Heap overflow or UAF - ability to overwrite large bin chunk metadata
  2. Three large chunks - to trigger the specific insertion path
  3. Known target addresses - where you want to write heap pointers

Technical Details

Large Bin Insertion Logic

Large bins are sorted by size, with additional fd_nextsize/bk_nextsize pointers:
By controlling victim->bk_nextsize and fwd->bk, we can write to two arbitrary locations!

The Two Write Primitives

  1. First write via bk_nextsize:
  2. Second write via bk:
The offsets (0x20 and 0x10) are for fd_nextsize and fd positions respectively. On x64: 0x20 = 32 bytes (4 pointers), 0x10 = 16 bytes (2 pointers).

Memory Layout

Step-by-Step Exploitation

1

Allocate Three Large Chunks

Create three chunks in large bin size range:
2

Free First Two Chunks

Free p1 and p2 to place them in unsorted bin:
3

Move p2 to Large Bin

Allocate a small chunk to trigger sorting:
Now p2 is alone in the large bin, establishing the baseline for our attack.
4

Free Third Chunk

Free p3 to place it in unsorted bin:
5

Corrupt p2 Metadata

Exploit vulnerability to modify p2’s metadata:
The size corruption is critical: p2’s size (0x3f1) must be smaller than p3’s size (0x501) to trigger the insertion path we want.
6

Trigger Large Bin Insertion

Allocate to move p3 into large bin:
When p3 is sorted into the large bin:
  1. p3 is larger than p2 (0x501 > 0x3f1)
  2. p3 will be inserted at the head
  3. This triggers: p2->bk_nextsize->fd_nextsize = p3
  4. And: p2->bk = p3
  5. Result: Two arbitrary writes!
7

Verify the Writes

Check that both targets were overwritten:

Full Source Code

Why This Still Works

Unlike the unsorted bin attack, large bin attack remains unpatched because:
  1. Complex insertion logic - Large bins require sorted insertion by size
  2. Necessary functionality - The fd_nextsize/bk_nextsize mechanism is required for performance
  3. Harder to validate - Checking all pointers would significantly impact performance

Common Attack Targets

1. global_max_fast (Most Common)

2. Double Corruption

The unique advantage of large bin attack is two writes:

Advanced Techniques

Controlling Written Value

The written value is the address of the newly inserted chunk (p3). You can:
  1. Groom heap layout - allocate p3 at a predictable address
  2. Use the heap address - if it points to controlled data
  3. Partial overwrites - on systems with ASLR where lower bytes are known

Chaining with Other Techniques

1

Leak libc

Use unsorted bin chunks to leak addresses
2

Large bin attack

Overwrite global_max_fast
3

Fastbin attack

Now possible on larger chunks
4

__malloc_hook

Overwrite for code execution

0CTF 2018

heapstorm2 - Showcases large bin attack in a realistic CTF scenario

Practice

Try it in your browser

Debug this technique interactively on Ret2 Wargames

References