Hacktricks-skills heap-security-checks
Reference for libc heap memory function security checks and error messages. Use this skill whenever the user is debugging heap vulnerabilities, analyzing heap exploitation, studying glibc malloc/free internals, or needs to understand what specific heap error messages mean. Trigger on mentions of heap corruption, malloc/free errors, tcache, fastbins, unsorted bins, or any libc heap function security checks.
git clone https://github.com/abelrguezr/hacktricks-skills
skills/binary-exploitation/libc-heap/heap-memory-functions/heap-functions-security-checks/SKILL.MDHeap 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 |
|---|---|---|
| | Chunk size doesn't match prev_size in next chunk |
| | Forward/backward pointers don't match |
| | Fastbin chunk address is misaligned |
| | Fastbin chunk size doesn't match bin index |
| | Small bin linked list integrity check failed |
| | Unsorted bin chunk size is out of range |
| | Unsorted bin linked list integrity check failed |
| | Top chunk size exceeds system memory |
| | Tcache chunk address is misaligned |
| | Realloc pointer is misaligned or size incorrect |
| | Free pointer is not aligned |
| | Chunk size is too small or misaligned |
| | Tcache count exceeds limit |
| | Chunk already in tcache |
| | Chunk already at top of fastbin |
| | Attempting to free top chunk |
| | Next chunk outside arena boundaries |
| | Previous chunk not marked as in-use |
| | Next chunk size out of range |
| | Prev_size mismatch during consolidation |
| | Unsorted bin linked list corrupted |
Function-by-Function Security Checks
unlink
unlinkPerforms checks when unlinking chunks from bins:
-
Size vs prev_size check
- Verifies chunk size matches
in next chunkprev_size - Error:
corrupted size vs. prev_size
- Verifies chunk size matches
-
Double-linked list integrity
- Checks
andP->fd->bk == PP->bk->fw == P - Error:
corrupted double-linked list
- Checks
-
Nextsize list integrity (non-small chunks)
- Checks
andP->fd_nextsize->bk_nextsize == PP->bk_nextsize->fd_nextsize == P - Error:
corrupted double-linked list (not small)
- Checks
_int_malloc
_int_mallocFast 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
andvictim->bck->fd == victimvictim->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
tcache_get_n- Chunk alignment
- Error:
malloc(): unaligned tcache chunk detected
- Error:
tcache_thread_shutdown
tcache_thread_shutdown- Chunk alignment
- Error:
tcache_thread_shutdown(): unaligned tcache chunk detected
- Error:
__libc_realloc
__libc_realloc- Pointer validity
- Checks alignment and size correctness
- Error:
realloc(): invalid pointer
_int_free
_int_freeInitial 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
_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
_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
do_check_malloc_state- Fast bin alignment
- Error:
do_check_malloc_state(): unaligned fastbin chunk detected
- Error:
malloc_consolidate
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
_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
- Detailed unlink operation analysisunlink.md
- Malloc internalsmalloc-and-sysmalloc.md
- Free operation internalsfree.md
- JSON structures for evalsreferences/schemas.md