Lion Cipher Lab

Write your own memory allocator (malloc/free) from scratch

Why it matters

Every C and C++ program you will ever analyze, and the runtime under most other languages, gets its dynamic memory from one function: malloc, paired with free. To a beginner malloc is a black box that 'gives you memory.' But the exploitation world lives INSIDE that black box: the entire family of heap attacks -- the House of Force, House of Botcake, House of Einherjar, House of Orange, House of Spirit, tcache poisoning, use-after-free -- works by corrupting the allocator's own bookkeeping (chunk headers, free-list pointers, the top chunk) so that a later malloc hands the attacker a pointer to…

What you'll learn

Steps in this lab

  1. The one idea: the kernel gives you a blank region; malloc is the librarian that hands out pieces
  2. Get a Linux x86-64 shell and a C compiler
  3. The design: a chunk header, an address-ordered free list, and five small jobs
  4. Write the allocator: myalloc.c
  5. Run it -- watch chunks coalesce, split, and route to mmap with your own eyes
  6. The named failure modes (why this allocator is a teaching tool, not a shippable one)
  7. How real allocators differ -- and why THIS metadata is what heap exploits corrupt
  8. The judgment: when to write your own allocator, and when to never

Open the full interactive lab →