skills/binary-exploitation/libc-heap/heap-memory-functions/malloc-and-sysmalloc/SKILL.md
--- name: malloc-internals description: Understand glibc malloc implementation, heap allocation flow, and memory management internals. Use this skill whenever the user asks about malloc, sysmalloc, heap exploitation, glibc memory allocation, binary exploitation heap challenges, or any questions about how malloc works internally. Trigger for: malloc questions, heap vulnerability research, CTF heap challenges, understanding allocation order, fastbin/smallbin/largebin/tcache behavior, security chec
npx skillsauth add abelrguezr/hacktricks-skills skills/binary-exploitation/libc-heap/heap-memory-functions/malloc-and-sysmallocInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
This skill provides comprehensive knowledge about glibc's malloc implementation, including allocation flow, bin management, security checks, and system allocation.
When malloc() is called, glibc follows this decision tree:
__libc_malloc(bytes)Entry point for all malloc calls.
What it does:
_int_malloc() in appropriate arenatag_new_usable() for securityKey behavior:
_int_malloc(arena, bytes)Main allocation logic that searches all bins.
Flow:
checked_request2size()sysmalloc() directlymalloc_consolidate() to move fast chunks to unsortedsysmalloc()malloc_consolidate(arena)Called for large allocations to prevent fragmentation.
What it does:
have_fastchunks = falseSecurity checks performed:
unaligned fastbin chunk detected - chunk misalignedinvalid chunk size - size doesn't match fastbin indexcorrupted size vs. prev_size in fastbins - prev_size mismatchsysmalloc(bytes, arena)Requests memory from the system when bins are exhausted.
When called:
Strategies:
sysmalloc_mmap() for large allocations or when arena is nullMORECORE (sbrk) to extend heapPurpose: Thread-local cache for fast allocations
Behavior:
__libc_malloc()Key points:
tcache_count chunks per bin maximumtcache_unsorted_limit limits unsorted bin processingPurpose: Very fast allocation for small chunks
Characteristics:
get_max_fast() (typically 80 bytes)Security checks:
unaligned fastbin chunk detected - misaligned chunkunaligned fastbin chunk detected 2 - misaligned victimunaligned fastbin chunk detected 3 - misaligned tcache fill chunkmemory corruption (fast) - size doesn't match indexTcache interaction: When a fast bin chunk is used, remaining chunks of same size are moved to tcache
Purpose: Fast allocation for small chunks with integrity checks
Characteristics:
MINSIZE to MAX_SMALLBIN (typically 80-512 bytes)bck->fd == victimSecurity checks:
smallbin double linked list corrupted - integrity check failedTcache interaction: Remaining chunks moved to tcache after allocation
Purpose: Temporary holding for freed chunks before binning
Characteristics:
last_remainder for small allocation localitySecurity checks (per chunk):
invalid size (unsorted) - size too small or too biginvalid next size (unsorted) - next chunk size invalidmismatching next->prev_size (unsorted) - prev_size mismatchunsorted double linked list corrupted - fd/bk integrityinvalid next->prev_inuse (unsorted) - prev_inuse bit wrongSpecial behavior:
last_remainder used for small allocations to promote localityMAX_ITERS (10000) limit on traversalPurpose: Store larger chunks with best-fit search
Characteristics:
binmap) tracks non-empty binsSecurity checks:
largebin double linked list corrupted (nextsize) - skip list integritylargebin double linked list corrupted (bk) - main list integritySearch strategy:
Purpose: Last chunk in arena, can be extended
Characteristics:
sysmalloc() when too smallSecurity check:
corrupted top size - size exceeds system_memBehavior:
sysmalloc()| Check | Location | Error Message |
|-------|----------|---------------|
| Fastbin alignment | _int_malloc | unaligned fastbin chunk detected |
| Fastbin size | _int_malloc | memory corruption (fast) |
| Smallbin integrity | _int_malloc | smallbin double linked list corrupted |
| Unsorted size | _int_malloc | invalid size (unsorted) |
| Unsorted next size | _int_malloc | invalid next size (unsorted) |
| Unsorted prev_size | _int_malloc | mismatching next->prev_size (unsorted) |
| Unsorted integrity | _int_malloc | unsorted double linked list corrupted |
| Unsorted prev_inuse | _int_malloc | invalid next->prev_inuse (unsorted) |
| Largebin nextsize | _int_malloc | largebin double linked list corrupted (nextsize) |
| Largebin bk | _int_malloc | largebin double linked list corrupted (bk) |
| Top size | _int_malloc | corrupted top size |
| Unsorted chunks | _int_malloc | corrupted unsorted chunks |
| Consolidate alignment | malloc_consolidate | unaligned fastbin chunk detected |
| Consolidate size | malloc_consolidate | invalid chunk size |
| Consolidate prev_size | malloc_consolidate | corrupted size vs. prev_size in fastbins |
Vulnerability: Fast bins don't verify chunk size on removal
Exploitation:
Mitigation: Tcache, fastbin size checks (glibc 2.31+)
Vulnerability: Unsorted bin uses first-fit, chunks binned after use
Exploitation:
Mitigation: Understanding binning behavior
Vulnerability: Tcache is thread-local, no size verification
Exploitation:
Mitigation: Tcache per-thread, fd pointer checks
Main Arena:
Thread Arenas:
Arena Selection:
arena_get() finds suitable arenaKey constants:
MALLOC_ALIGNMENT - typically 16 bytesCHUNK_HDR_SZ - chunk header size (typically 16 bytes)MINSIZE - minimum allocatable sizepagesize - system page size (typically 4096 bytes)Alignment rules:
front_misalign handles alignment in mmap'd regionsNote: This skill covers glibc's ptmalloc2 implementation. Behavior may vary slightly between glibc versions. Always verify against the specific version you're working with.
testing
How to perform a House of Lore (small bin attack) heap exploitation. Use this skill whenever the user mentions heap exploitation, small bin attacks, fake chunks, glibc heap vulnerabilities, or needs to insert fake chunks into small bins for arbitrary read/write. Trigger for CTF challenges involving heap corruption, glibc 2.31+ exploitation, or when the user needs to bypass malloc sanity checks using fake chunk linking.
testing
How to perform House of Force heap exploitation attacks. Use this skill whenever the user mentions heap exploitation, House of Force, top chunk manipulation, arbitrary memory allocation, malloc manipulation, or wants to allocate chunks at specific addresses. Also trigger for CTF challenges involving heap overflows, top chunk size overwrites, or when the user needs to calculate evil_size for heap attacks. Make sure to use this skill for any binary exploitation task involving glibc heap manipulation, even if they don't explicitly say "House of Force".
tools
How to perform House of Einherjar heap exploitation to allocate memory at arbitrary addresses. Use this skill whenever the user mentions heap exploitation, glibc heap attacks, arbitrary memory allocation, off-by-one overflow exploitation, tcache poisoning, fast bin attacks, or any CTF challenge involving heap manipulation. This is essential for binary exploitation tasks where you need to control malloc() return addresses.
testing
How to identify, analyze, and exploit heap overflow vulnerabilities in binary exploitation challenges and real-world scenarios. Use this skill whenever the user mentions heap overflows, memory corruption, heap grooming, tcache poisoning, fast-bin attacks, or any heap-related vulnerability in CTF challenges, binary analysis, or security research. This skill covers heap overflow fundamentals, exploitation techniques, heap grooming strategies, and real-world CVE analysis.