Documentation Index
Fetch the complete documentation index at: https://mintlify.com/shellphish/how2heap/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The first-fit algorithm is the core allocation strategy used by glibc malloc when selecting a free chunk to satisfy a memory request. When malloc needs to allocate memory, it searches through the appropriate bin and returns the first chunk that is large enough, rather than searching for the best-fitting chunk.This seemingly simple design decision has significant implications for heap exploitation, particularly in use-after-free scenarios.
How First-Fit Works
When you callmalloc(size), the allocator:
Calculates required size
Converts the requested size to the actual chunk size needed (including metadata and alignment).
Searches for a free chunk
Looks through the appropriate bin (tcache, fastbin, smallbin, etc.) for a free chunk.
Returns first match
Returns the first chunk that is large enough, even if a better-fitting chunk exists later in the list.
First-fit prioritizes speed over optimal memory usage. It avoids traversing the entire free list to find the best match.
Demonstration Program
The following program demonstrates first-fit allocation behavior:first_fit.c
Running the Program
Key Observations
Let’s break down what happens in this program:Free first chunk
Allocate smaller chunk
a’s address is large enough. First-fit returns this chunk immediately.Exploitation Implications
First-fit allocation makes certain heap exploits more reliable:Use-After-Free (UAF)
Example UAF scenario
Heap Spray
First-fit makes heap spraying more predictable:Heap Feng Shui
Exploiters can manipulate heap layout by:- Allocating chunks in specific order
- Freeing strategic chunks to create controlled free list
- Triggering vulnerability that allocates from manipulated free list
- Achieving predictable memory reuse due to first-fit
Why Not Best-Fit?
You might wonder why glibc doesn’t use a best-fit algorithm (find the smallest chunk that fits). The answer is performance:First-Fit
- Fast: O(1) to O(n) with early exit
- Returns first match found
- Less memory traversal
- Good locality
Best-Fit
- Slow: O(n) always
- Must check all chunks
- More memory traversal
- Better memory efficiency
For most applications, the performance gain from first-fit outweighs the slight memory fragmentation cost.
Defense Considerations
To protect against first-fit-based exploits:Avoid use-after-free
Null out pointers after freeing them, and implement proper lifetime management.
Consider safe languages
Languages with automatic memory management eliminate entire classes of heap exploits.
Practical Exercise
Try this experiment with the program:Related Techniques
Tcache Poisoning
Exploit tcache’s LIFO structure with first-fit
Fastbin Dup
Double-free with first-fit allocation
House of Spirit
Create fake chunks that first-fit will allocate
Heap Basics
Review malloc fundamentals
Summary
First-fit allocation is:
- Fast: Returns the first suitable chunk without exhaustive search
- Predictable: Same allocation patterns produce consistent results
- Exploitable: Enables reliable use-after-free and heap grooming attacks
- Fundamental: Understanding this is essential for heap exploitation
