Heap Security Checks Reference
This skill provides a comprehensive reference for security checks performed by libc heap management functions. Use it to understand error messages, identify exploitation opportunities, and debug heap vulnerabilities.
Quick Lookup by Error Message
When you see a heap error message, find it below to understand what check failed:
| Error Message | Function | What It Means |
|---|---|---|
corrupted size vs. prev_size |
unlink |
Chunk size doesn't match prev_size in next chunk |
corrupted double-linked list |
unlink |
Forward/backward pointers don't match |
malloc(): unaligned fastbin chunk detected |
_int_malloc |
Fastbin chunk address is misaligned |
malloc(): memory corruption (fast) |
_int_malloc |
Fastbin chunk size doesn't match bin index |
malloc(): smallbin double linked list corrupted |
_int_malloc |
Small bin linked list integrity check failed |
malloc(): invalid size (unsorted) |
_int_malloc |
Unsorted bin chunk size is out of range |
malloc(): unsorted double linked list corrupted |
_int_malloc |
Unsorted bin linked list integrity check failed |
malloc(): corrupted top size |
_int_malloc |
Top chunk size exceeds system memory |
malloc(): unaligned tcache chunk detected |
tcache_get_n |
Tcache chunk address is misaligned |
realloc(): invalid pointer |
__libc_realloc |
Realloc pointer is misaligned or size incorrect |
free(): invalid pointer |
_int_free |
Free pointer is not aligned |
free(): invalid size |
_int_free |
Chunk size is too small or misaligned |
free(): too many chunks detected in tcache |
_int_free |
Tcache count exceeds limit |
free(): double free detected in tcache 2 |
_int_free |
Chunk already in tcache |
double free or corruption (fasttop) |
_int_free |
Chunk already at top of fastbin |
double free or corruption (top) |
_int_free_merge_chunk |
Attempting to free top chunk |
double free or corruption (out) |
_int_free_merge_chunk |
Next chunk outside arena boundaries |
double free or corruption (!prev) |
_int_free_merge_chunk |
Previous chunk not marked as in-use |
free(): invalid next size (normal) |
_int_free_merge_chunk |
Next chunk size out of range |
corrupted size vs. prev_size while consolidating |
_int_free_merge_chunk |
Prev_size mismatch during consolidation |
free(): corrupted unsorted chunks |
_int_free_create_chunk |
Unsorted bin linked list corrupted |
Function-by-Function Security Checks
unlink
Performs checks when unlinking chunks from bins:
Size vs prev_size check
- Verifies chunk size matches
prev_sizein next chunk - Error:
corrupted size vs. prev_size
- Verifies chunk size matches
Double-linked list integrity
- Checks
P->fd->bk == PandP->bk->fw == P - Error:
corrupted double-linked list
- Checks
Nextsize list integrity (non-small chunks)
- Checks
P->fd_nextsize->bk_nextsize == PandP->bk_nextsize->fd_nextsize == P - Error:
corrupted double-linked list (not small)
- Checks
_int_malloc
Fast Bin Search Checks
Chunk alignment
- Error:
malloc(): unaligned fastbin chunk detected 2
- Error:
Forward chunk alignment
- Error:
malloc(): unaligned fastbin chunk detected
- Error:
Size vs bin index
- Error:
malloc(): memory corruption (fast)
- Error:
Tcache fill alignment
- Error:
malloc(): unaligned fastbin chunk detected 3
- Error:
Small Bin Search Checks
- Linked list integrity
- Checks
victim->bk->fd != victim - Error:
malloc(): smallbin double linked list corrupted
- Checks
Consolidate Checks (per fast bin chunk)
Chunk alignment
- Error:
malloc_consolidate(): unaligned fastbin chunk detected
- Error:
Size vs bin index
- Error:
malloc_consolidate(): invalid chunk size
- Error:
Prev_size consistency
- Error:
corrupted size vs. prev_size in fastbins
- Error:
Unsorted Bin Search Checks
Chunk size range
- Error:
malloc(): invalid size (unsorted)
- Error:
Next chunk size range
- Error:
malloc(): invalid next size (unsorted)
- Error:
Prev_size consistency
- Error:
malloc(): mismatching next->prev_size (unsorted)
- Error:
Linked list integrity
- Checks
victim->bck->fd == victimandvictim->fd == av (arena) - Error:
malloc(): unsorted double linked list corrupted
- Checks
Prev_inuse flag
- Error:
malloc(): invalid next->prev_inuse (unsorted)
- Error:
Nextsize list integrity
- Error:
malloc(): largebin double linked list corrupted (nextsize)
- Error:
Backward list integrity
- Error:
malloc(): largebin double linked list corrupted (bk)
- Error:
Large Bin Search Checks
By index search
- Checks
bck->fd->bk != bck - Error:
malloc(): corrupted unsorted chunks
- Checks
Next bigger search
- Checks
bck->fd->bk != bck - Error:
malloc(): corrupted unsorted chunks2
- Checks
Top Chunk Use Checks
- Top chunk size
- Checks
chunksize(av->top) > av->system_mem - Error:
malloc(): corrupted top size
- Checks
tcache_get_n
- Chunk alignment
- Error:
malloc(): unaligned tcache chunk detected
- Error:
tcache_thread_shutdown
- Chunk alignment
- Error:
tcache_thread_shutdown(): unaligned tcache chunk detected
- Error:
__libc_realloc
- Pointer validity
- Checks alignment and size correctness
- Error:
realloc(): invalid pointer
_int_free
Initial Checks
Pointer alignment
- Error:
free(): invalid pointer
- Error:
Size validity
- Checks size > MINSIZE and alignment
- Error:
free(): invalid size
Tcache Checks
Tcache count limit
- Checks against
mp_.tcache_count - Error:
free(): too many chunks detected in tcache
- Checks against
Entry alignment
- Error:
free(): unaligned chunk detected in tcache 2
- Error:
Double free detection
- Error:
free(): double free detected in tcache 2
- Error:
Fast Bin Checks
Next size validity
- Error:
free(): invalid next size (fast)
- Error:
Fastbin top check
- Error:
double free or corruption (fasttop)
- Error:
Top chunk size consistency
- Error:
invalid fastbin entry (free)
- Error:
_int_free_merge_chunk
Top chunk check
- Error:
double free or corruption (top)
- Error:
Arena boundary check
- Error:
double free or corruption (out)
- Error:
Prev_inuse check
- Error:
double free or corruption (!prev)
- Error:
Next chunk size
- Error:
free(): invalid next size (normal)
- Error:
Prev_size during consolidation
- Error:
corrupted size vs. prev_size while consolidating
- Error:
_int_free_create_chunk
- Unsorted bin integrity
- Checks
unsorted_chunks(av)->fd->bk == unsorted_chunks(av) - Error:
free(): corrupted unsorted chunks
- Checks
do_check_malloc_state
- Fast bin alignment
- Error:
do_check_malloc_state(): unaligned fastbin chunk detected
- Error:
malloc_consolidate
Fast bin alignment
- Error:
malloc_consolidate(): unaligned fastbin chunk detected
- Error:
Fast bin size
- Error:
malloc_consolidate(): invalid chunk size
- Error:
_int_realloc
Old size validity
- Error:
realloc(): invalid old size
- Error:
Next size validity
- Error:
realloc(): invalid next size
- Error:
Exploitation Implications
Bypassing Checks
- unlink checks: Can be bypassed with controlled heap corruption if you can manipulate fd/bk pointers
- Alignment checks: Require proper chunk alignment (typically 8 or 16 bytes)
- Size checks: Need to maintain valid size ranges for each bin type
- Double-linked list checks: Require consistent forward/backward pointer manipulation
Common Exploitation Patterns
- Use-after-free: Trigger when freed chunk is reallocated before checks
- Double-free: Exploit before double-free detection triggers
- Heap overflow: Corrupt adjacent chunk metadata to bypass size checks
- Tcache poisoning: Manipulate tcache entries before alignment checks
- Fastbin attack: Exploit fastbin's LIFO nature and limited checks
Debugging Tips
- Read the error message carefully - It tells you exactly which check failed
- Check the function context - Know which malloc/free path triggered the error
- Examine chunk metadata - Look at size, prev_size, fd, bk fields
- Verify alignment - Most errors relate to misaligned pointers
- Check bin membership - Ensure chunk is in the expected bin type
Related Resources
unlink.md- Detailed unlink operation analysismalloc-and-sysmalloc.md- Malloc internalsfree.md- Free operation internalsreferences/schemas.md- JSON structures for evals