← all posts

· 4 min read · by Eav Puthcambo

One off-by-one against the Heap Devil

A notes program with every modern protection on, where a single "greater-than" that should be "greater-than-or-equal" opens the door to a use-after-free, and from there to a shell.

H34p D3v1l, LYKN CTF 2026 (pwn / heap)

Even demons make mistakes. The Heap Devil has invited you to play a dangerous game with his "unbreakable" contracts.

The binary is a notes manager on modern glibc (2.39), with everything turned on:

RELRO:    Full RELRO     (no GOT overwrite)
Canary:   found
NX:       enabled
PIE:      enabled        (need a leak)

Full RELRO rules out overwriting the GOT, and recent glibc removed the old malloc/free hooks, so the two classic "one write and you win" heap tricks are both gone. This has to end in leaking everything and building a ROP chain. The whole thing hinges on one mistake the Devil made.

The mistake

Each note is a small struct with an in_use flag, a size, an id, and a data pointer. The menu lets you create, view, edit, delete and resize. Two of the handlers validate the index the same wrong way:

if (index < 0 || index > num_notes) {   // should be >=
    puts("Invalid index!");
    return;
}

index == num_notes slips through: one slot past the last live note.

On its own that's just an out-of-bounds slot. What makes it dangerous is that delete shifts the later entries down and decrements the count, but never clears the slot that falls off the end. So after you delete the current last note, that stale slot keeps the note's old in_use flag and data pointer, now pointing at freed memory. Combine the two bugs and view(num_notes) is a use-after-free read and edit(num_notes) is a use-after-free write, straight onto a freed chunk's contents.

Turning a UAF into "allocate anywhere"

glibc's tcache protects its free-list pointers by XOR-ing them with a key derived from the chunk's own address (safe-linking). But a UAF read hands you that mangled pointer, and the key is just the chunk address shifted right by 12, which the leaked value itself reveals. So you can read the key, then use a UAF write to replace the pointer with key XOR target. The next couple of allocations then hand you back memory at an address of your choosing.

That one primitive is the whole game. A couple of glibc details have to be respected to keep it reliable (chunk pointers must be 16-byte aligned, and tcache zeroes a small bookkeeping window on handout), but once it works, everything else is leaks.

Three leaks, all free

  • Heap base. Free a lone chunk and read its stale pointer. With an empty free list the mangled value is the heap base shifted right by 12.
  • libc. main opens /dev/random at startup and never uses the handle again. That leaves a FILE struct sitting in the heap whose vtable field is a fixed libc-relative pointer. Point an allocation at it, read the vtable, subtract the known offset, and you have libc, without disturbing anything.
  • Stack. Aim the same primitive at libc's environ global, which holds a live stack address.

Landing the chain

Every menu handler is called from the same spot in the dispatch loop, so the distance from environ to a handler's saved return address on the stack is a fixed offset, the same under every run of ASLR. Measure it once in a debugger and it's pinned forever.

One of the handlers ends in leave; ret, so writing a short ROP chain (pop the /bin/sh argument, call system) directly over its saved return address hijacks control the moment it returns, with no stack overflow needed. The tcache primitive delivers the "buffer" straight onto the stack. The chain needs one bare ret gadget as alignment filler, because system uses instructions that require the stack 16-byte aligned, and this landing is off by eight.

The one reliability tax

Every write goes through the program's fgets, which stops at a newline. If ASLR ever hands back an address whose bytes contain 0x0a, the write gets silently truncated mid-payload. So the solver checks every value it's about to send, and if it finds a stray newline, it drops the connection and retries against a fresh process. New ASLR almost never repeats the unlucky byte.

[*] heap base : 0x59873a1aa000
[*] libc base : 0x7844c1df4000
[*] environ   : 0x7fff1755ce58
[*] ret slot  : 0x7fff1755ccf8
LYKNCTF{0utsm4rt3d_th3_h34p_d3v1l}

Every mitigation in place, and the whole thing still unravels from one > that should have been >=.

📂 Full writeup with the allocation primitive and the complete chain: ctf-writeups/pwn/…/H34p D3v1l

Thanks for reading! Questions or corrections? Ping me on GitHub.

Read more

all posts →