Overview
The heap is a region of memory used for dynamic memory allocation. Unlike stack memory, which grows and shrinks automatically with function calls, heap memory must be explicitly allocated and freed by the programmer using functions likemalloc() and free().
glibc malloc Implementation
The GNU C Library (glibc) malloc is the default memory allocator on most Linux systems. It manages heap memory efficiently by organizing freed memory into different bins based on size and reusing previously allocated chunks.Understanding glibc malloc internals is crucial for heap exploitation, as many vulnerabilities arise from the allocator’s behavior.
Chunk Structure
Every allocation in glibc malloc is represented as a chunk. A chunk contains metadata along with the user data:Key Points
- prev_size: Stores the size of the previous chunk if it’s free
- size: Contains the chunk size and three flag bits (P, M, N)
- fd/bk: Forward and backward pointers form doubly-linked lists of free chunks
- User data: Overlaps with fd/bk when the chunk is allocated
Bin Types
glibc malloc organizes free chunks into different “bins” - data structures that hold freed chunks for efficient reuse.tcache (Thread Cache)
- Size range: Small allocations (up to 1032 bytes on 64-bit)
- Structure: Singly-linked list per thread
- Count: 64 bins, one per size class
- Max chunks: 7 chunks per bin (default)
- Performance: Fastest, no thread locking needed
fastbins
- Size range: 16-80 bytes (64-bit), 8-64 bytes (32-bit)
- Structure: Singly-linked list (LIFO)
- Count: 10 bins
- Coalescing: Chunks are NOT merged with adjacent free chunks
- Performance: Fast, but slower than tcache
smallbins
- Size range: Less than 1024 bytes (64-bit)
- Structure: Doubly-linked list (FIFO)
- Count: 62 bins
- Coalescing: Chunks ARE merged with adjacent free chunks
- Exact fit: Each bin contains one specific size
largebins
- Size range: 1024 bytes and larger (64-bit)
- Structure: Doubly-linked list sorted by size
- Count: 63 bins
- Coalescing: Chunks ARE merged with adjacent free chunks
- Size range: Each bin contains a range of sizes
Allocation Mechanics
When you callmalloc(size), glibc follows this allocation strategy:
Free Mechanics
When you callfree(ptr), glibc performs several operations:
Size Calculations
The actual chunk size differs from the requested size:Key Concepts for Exploitation
Many heap exploits rely on understanding these fundamental behaviors:
- First-fit allocation: malloc reuses the first suitable free chunk
- Use-after-free: Freed memory retains pointers (fd/bk) that overlap with user data
- Chunk coalescing: Adjacent free chunks are merged (except fastbins/tcache)
- Metadata corruption: Overwriting chunk headers can hijack control flow
- Bin manipulation: Corrupting bin lists can cause malloc to return arbitrary addresses
Next Steps
Malloc Playground
Interactive tool to experiment with allocations
First Fit
Understanding first-fit allocation strategy
tcache Index
How tcache indices are calculated
Techniques
Explore heap exploitation techniques
