BabyAllocator — pwnable.tw (500 pts)
← Back to pwnable.tw Challenge Index
nc chall.pwnable.tw 10404Flag:
FLAG{Spl1t_st4ck_spl1t_h34p_th3n_spl1t_7h3_sh3ll}
Challenge Overview
A stripped x86-64 binary compiled with GCC's -fsplit-stack feature, linked against glibc 2.23. The split-stack mechanism uses __morestack / __morestack_allocate_stack_space from libgcc/generic-morestack.c to dynamically allocate stack segments via mmap when the current segment is exhausted. The binary presents a menu-driven "allocator" interface:
- Allocate on stack —
alloca()with size[0x80, 0xFFF], reads a 15-byte name viascanf("%15s") - Allocate on heap —
malloc()with size> 0xFF, reads a 15-byte name - Write data — writes to the current allocation buffer
- New allocator — recursive call into the menu function (pushes a new stack frame, possibly triggering
__morestackto allocate a new stack segment) - Release — returns from the current recursive call (pops back to the previous frame/segment)
- Exit
checksec:
Arch: amd64-64-little
RELRO: Full RELRO # GOT not writable
Stack: No canary
NX: NX enabled
PIE: No PIE (base 0x3fe000/0x400000)Vulnerabilities
V1 — scanf Corrupts Split-Stack Segment Metadata (Primary, Exploitable)
Root Cause: The split-stack mechanism stores per-segment metadata (a struct stack_segment) at the base (lowest address) of each mmap'd stack page. When libc functions like __isoc99_scanf are called, they are not compiled with -fsplit-stack and are unaware of the split-stack segment boundaries. scanf's large local variables (internal buffers, _IO_FILE pointers to stdin/stdout) can underflow past the current stack frame and overwrite the struct stack_segment header at the page base.
// From gcc/libgcc/generic-morestack.c
struct stack_segment {
struct stack_segment *next; // +0x00
struct stack_segment *prev; // +0x08
size_t size; // +0x10
void *old_stack; // +0x18
void *old_stack_limit; // +0x20
struct dynamic_allocation_blocks *free_dynamic_allocation; // +0x28
...
};
struct dynamic_allocation_blocks {
struct dynamic_allocation_blocks *next;
size_t size;
void *block; // <-- returned by __morestack_allocate_stack_space
};Trigger: By carefully controlling recursion depth (option 4) and alloca sizes (option 1), the attacker positions $rsp so that when scanf is called during the next alloca, its internal local variables land exactly on top of the stack_segment header. Key corruptions:
free_dynamic_allocation(+0x28) gets overwritten with a pointer to_IO_2_1_stdin_(a libc address left on the stack byscanf's internals).size(+0x10) gets overwritten with a large value, orprev/nextpointers are corrupted.
Impact: On the next call to __morestack_allocate_stack_space, it checks free_dynamic_allocation. Since this now points to stdin, it interprets stdin's fields as a dynamic_allocation_blocks struct:
stdin->_flags(0xfbad208b) →.next- bytes at
stdin+0x08→.size(some large value) - bytes at
stdin+0x10→.block(an address near_IO_2_1_stdin_+0x84..0x90)
If .size >= requested_size, it returns .block — giving the attacker a write primitive into libc's _IO_2_1_stdin_ structure and surrounding memory (including __malloc_hook, __free_hook, and _IO_2_1_stdout_).
V2 — scanf Corrupts Segment prev/next Pointers (Alternative Corruption Target)
Same root cause as V1, but different fields are targeted. By adjusting the stack alignment differently, scanf's local variables can overwrite:
prev— causesrelease()to pop to a wrong segment, potentially one whose content the attacker controlsnext— causes subsequent segment pushes to use a fake segment headersize— inflates the segment size, allowing subsequentallocato return a pointer past the segment boundary into older stack frames
V3 — Segment Size Corruption → Stack Frame Overlap (Alternative)
If scanf overwrites the size field of a segment to a very large value, then on returning to that segment, alloca can allocate beyond the real segment boundary. This overlaps with older stack frames from previous recursion levels, allowing the attacker to overwrite saved return addresses → ROP.
Key Data Structures
struct stack_segment (at base of each mmap'd stack page)
| Offset | Field | Description |
|---|---|---|
+0x00 |
next |
Next segment in linked list |
+0x08 |
prev |
Previous segment |
+0x10 |
size |
Usable size of this segment |
+0x18 |
old_stack |
Saved RSP from before segment switch |
+0x20 |
old_stack_limit |
Saved stack limit |
+0x28 |
free_dynamic_allocation |
Linked list of dynamically allocated blocks |
struct dynamic_allocation_blocks (pointed to by free_dynamic_allocation)
| Offset | Field | Description |
|---|---|---|
+0x00 |
next |
Next block in free list |
+0x08 |
size |
Size of this block |
+0x10 |
block |
Pointer returned by __morestack_allocate_stack_space |
Exploit Paths
Path A — free_dynamic_allocation → stdin → Write __malloc_hook / FSOP
Used by: exp.py, solutions 1351, 1980, 8153, 31599, 32010, 35917, 37983
Steps:
- Deep recursion to push RSP onto mmap'd stack segments (3×
alloca(0xFFF)+ manyrecurse()). - Precise
allocasize positions RSP soscanf's locals overwritefree_dynamic_allocationwith&_IO_2_1_stdin_. release()+alloca(large)— the corruptedfree_dynamic_allocationmakes__morestack_allocate_stack_spacereturnstdin+0x90(or similar offset), giving a write primitive into libc data.- Overwrite
_IO_2_1_stdout_to craft a stdout FILE struct with_IO_write_basepointing to a GOT entry → stdout flush leaks a resolved libc address → libc base. - Overwrite
stdinvtable pointer to a fake vtable where__underflow=one_gadget. - Trigger
scanf(recurse + option 1) →__uflowdispatches through fake vtable → shell.
Variant (31599, 32010, 35917): Instead of vtable hijack, overwrite __malloc_hook with one_gadget or a puts gadget. Trigger via malloc() (option 2, heap allocation). Some solutions use __malloc_hook → puts_gadget for a two-stage leak, then rewrite __malloc_hook → one_gadget.
Reliability: Deterministic once the correct recursion depth and alloca sizes are found. The offset calibration differs between local and remote (usually a small constant adjustment).
Path B — Segment size / prev Corruption → Stack Frame Overlap → ROP
Used by: Solutions 59, 363, 550, 821, 1194, 1458, 1980, 2673, 5586, 6748, 9251, 10102, 14523, 26957
The most common approach. Instead of targeting free_dynamic_allocation, this path corrupts segment metadata to achieve a stack buffer overflow in a previous frame.
Steps:
- Create adjacent stack segments through careful recursion and
allocapatterns. Optionally interleave largemalloc(heap) allocations to control mmap layout and create gaps. - Trigger
scanfcorruption of the segment'ssizefield (inflated) orprevpointer (redirected). - Return to the corrupted segment via repeated
release(). The inflatedsizemeans subsequentallocain that frame can write past the real segment boundary into an older frame's stack. - Overwrite saved RBP/RIP of a return frame with a ROP chain.
- ROP to leak libc (e.g.,
puts(GOT_entry)orwrite(1, GOT_entry, 8)) → compute libc base. - Second ROP stage:
readinto BSS →system("/bin/sh"), ormprotect+readshellcode → jump to shellcode, or directone_gadget.
Common ROP gadgets (No PIE binary):
pop_rdi = 0x40194e
pop_rsi = 0x4016df
pop_rdx_rax = 0x4026c5 # pop rdx; pop rax; add rsp, 8; pop rbp; ret
mprotect = 0x400e98 # PLT
read_plt = 0x400e48
puts_plt = 0x400e10
write_plt = 0x400e18Variant (821, "memory gap"): Create stacks A–D, use mmap'd heap to create a gap between segments, free the heap to leave the gap, allocate new segments into the gap at higher addresses, use scanf to corrupt the lower segment's size, then overflow downward into older frames.
Variant (14523, "fake prev"): Overwrite segment A's prev pointer to point to a fake segment header between A and B. The fake header's prev is partially overwritten (1/16 brute force) to point to segment D. Popping through the fake chain skips segments, landing in C where the attacker can overwrite return addresses.
Reliability: Most variants are deterministic. Solution 14523 requires 1/16 brute force for partial prev overwrite.
Path C — TLS Corruption → _call_tls_dtors Hijack
Used by: Solution 9251
An exotic path that targets the thread-local storage (TLS) area adjacent to stack segments:
- Position stack segments adjacent to
fs_base(TLS segment) via deep recursion. - Corrupt
fs:[-0x108](a__call_tls_dtorslinked list pointer) with a stack address pointing to attacker-controlled data. - Corrupt
fs:[0x30](the pointer guard used forPTR_DEMANGLE) withvalue ^ read_gadget_addr. - Call
exit()(via invalid input to size prompt) →_call_tls_dtors→ demangled function pointer →readgadget → reads ROP chain from socket →puts(GOT)→ libc leak →system("/bin/sh").
Reliability: ~15/16 (depends on libc address not having 0xf nibble at a specific position).
Path D — Heap Spray + __malloc_hook Overwrite
Used by: Solution 8153
- Spray heap with 0xFF large
malloc(0x20000)allocations, each filled withp64(target_gadget)to create a "gadget sled" in memory. - Trigger
scanfcorruption to get a write pointer nearstdin. - Overwrite
__malloc_hookwith address of aputsgadget via the write primitive. - Trigger malloc with
size = GOT_entry→puts(GOT_entry)leaks libc. - Overwrite
__malloc_hookwithone_gadget→ triggermalloc→ shell.
Reliability: ~1/32 (heap spray hit probability for landing in correct page).
Exploit Primitive Summary
| Primitive | Source | Reliability |
|---|---|---|
scanf corrupts segment metadata |
V1: split-stack + libc stack usage | Deterministic with correct depth/size |
Write into stdin/stdout/hooks |
V1 → free_dynamic_allocation = &stdin |
Deterministic |
| Stack frame overlap (ROP) | V2/V3: corrupted size/prev |
Deterministic (most variants) |
| libc leak via stdout FSOP | Crafted stdout _IO_write_base = GOT |
Deterministic |
libc leak via puts(GOT) ROP |
Stack overlap → ROP chain | Deterministic |
| Code execution | one_gadget / __malloc_hook / ROP → system |
Deterministic |
| TLS hijack | Stack-adjacent-to-TLS corruption | ~15/16 |
Offsets (pwnable.tw libc_64.so.6, glibc 2.23)
# Binary (No PIE)
pop_rdi = 0x40194e
pop_rsi = 0x4016df
pop_rdx_rax = 0x4026c5 # pop rdx; pop rax; add rsp, 8; pop rbp; ret
pop_rsp_r13 = 0x401ee1
mprotect_plt = 0x400e98
read_plt = 0x400e48
puts_plt = 0x400e10
write_plt = 0x400e18
GOT_puts = 0x603f40
# libc
one_gadgets = [0x45216, 0x4526a, 0xef6c4, 0xf0567]
system = 0x45390
__malloc_hook = 0x3c3b10
__free_hook = 0x3c57a8
stdin = 0x3c38e0 # _IO_2_1_stdin_
stdout = 0x3c4620 # _IO_2_1_stdout_Solution Write-ups
| File | Primary Path | Key Technique | Notes |
|---|---|---|---|
59.md |
B | Brute-force stack alignment → ROP | "Solved with pure luck"; later understood scanf corruption |
363.md |
B | mprotect + read shellcode → ROP | "I don't understand my solution" |
550.md |
B | free_dynamic_allocation → stack addr → ROP |
Clear explanation of the core vulnerability |
821.md |
B | Memory gap trick → stack overlap → ROP + one_gadget | Detailed mmap layout manipulation |
1194.md |
B | scanf fake heap_alloca buffer → ROP |
Uses ret2csu for clean ROP |
1351.md |
B | Deep recursion → mprotect + shellcode | Concise; clean gadget chain |
1458.md |
A | free_dynamic_allocation → stdin → __malloc_hook |
Two-stage: malloc_hook → leak, then → one_gadget |
1980.md |
B | scanf header corruption → alloca to stack → ROP | Documented the vulnerability mechanism |
2673.md |
B | Leave fake stack addr at [rbx+0x28] → ROP |
Brief but effective |
2972.md |
B | 251 recurse + release pattern → ROP | Minimal explanation; heavy trial-and-error |
5586.md |
B | 57× stack fills + multiple releases → ROP | Uses ret2csu pattern |
6748.md |
B | Massive recurse (0x10×alloca + 0x60 bare) + releases → ROP | Trial-and-error alignment |
8153.md |
D | Heap spray 0xFF × malloc(0x20000) + __malloc_hook |
~1/32 reliability; spray-based |
9251.md |
C | TLS corruption → _call_tls_dtors → read gadget → ROP |
Most exotic path; ~15/16 reliability |
10102.md |
B | Overwrite segment prev → fake segment chain → stack overlap |
1/16 brute force for prev partial overwrite |
14523.md |
B | Adjacent mmap stacks via heap gap → scanf size corruption |
Detailed 8-attempt journal of failed approaches |
26957.md |
B | Overwrite segment header +0x00 with low addr → alloca overflow |
Concise |
31599.md |
A | free_dynamic_allocation → stdin → __malloc_hook → leak + one_gadget |
Clean two-stage exploit |
32010.md |
A | stdin write → stdout FSOP leak → stdout vtable → one_gadget | FSOP approach with vtable call [rax+0x38] |
35917.md |
A | stdin+0x90 write → __malloc_hook → puts leak → one_gadget |
Detailed; includes realloc alignment trick |
37983.md |
B | mmap ordering abuse → adjacent stacks → scanf → ROP | Local-only; includes IDA script for scanf offset analysis |
Files
| File | Description |
|---|---|
exp.py |
Working exploit (Path A: FSOP stdin/stdout vtable hijack) |
desc.txt |
Challenge description |
artifacts/babyallocator |
Original challenge binary (x86-64, stripped, -fsplit-stack) |
artifacts/libc_64.so.6 |
Remote libc (glibc 2.23) |
solution/*.md |
Community write-ups (21 solutions) |