TL;DR
A Tetris implementation with an out-of-bounds cell write and a width field that can be corrupted into an arbitrary-length stack read. Both primitives were verified against the real binary and a working stack leak runs on the live target; the exploit stalls on an arithmetic budget, not on a missing idea.
01Recon
teto.c (503 lines) inside pwn.red/jail, Ubuntu 24.04. Full RELRO, canary, NX, PIE. CET/SHSTK flags are present in the ELF but not enforced, overwriting a return gives a plain SIGSEGV, not a control- protection fault, so return hijack is on the table.
No win function. The import list is printf puts malloc free memmove memset perror _exit clock_gettime select read fflush rand __stack_chk_fail, no system, no execve, no syscall gadget, so a libc one_gadget is required, which means a libc leak is required.
The game state lives entirely on play_session_live's stack frame:
struct Game { u32 score; u16 height; u16 width; u8 stage[40]; }; // 48 bytes
// ROW_BYTES = 2, W = 10, H = 20Indexing everything by byte offset from Game.stage (stage sits at play_rbp-0x38):
idx -2,-1 = width (low, high)
idx 0..39 = board. row r -> idx 2r bits 0-7 = cols 0-7 ; idx 2r+1 bits 0-1 = cols 8,9
bits 2-7 of every ODD idx are unreachable by normal piece placement
idx 40..47 = padding (unused locals)
idx 48 = play_session_live canary idx 56 = saved rbp
idx 64 = play's return into main = PIE + 0x2e69
idx 80 = main's canary idx 88 = main's saved rbp
idx 96 = main's return into __libc_start_call_main = libc + 0x2a1caDeterminism. There is no srand, so the rand()%7 piece order is fixed for seed 1: O Z T J O S S T O S T J L Z L I.... Gravity never ticks while input is buffered, so a keystroke burst is fully deterministic, which is what makes the whole thing scriptable and simulable.
02The bug
Two distinct bugs, both stemming from the same inconsistency: board logic uses a fixed ROW_BY TES = 2 while rendering and cell access use a stride derived from the mutable width field.
Bug 1, out-of-bounds OR-write via y == -1. set_cell accepts y == -1, which computes idx = -2 + (x>>3). Taking an I-piece at spawn (y = -2), moving it to column X and pressing w makes entry_kick's first kick (0,-2) land it at y = -4, where every cell has y < 0, so collides only checks x >= width, and only the y == -1 cell survives the if (y < -1) return; guard. The result is exactly one bit set at a caller-chosen index:
idx = ((X+1) >> 3) - 2 , bit = (X+1) & 7 , requires width > 8*(idx+2)+3It adds no board cells at all. Confirmed on the real binary by writing into visible board cells.
Bug 2, unbounded stack read via a corrupted width. width sits at idx -2, inside the OR-write's reach. draw_live / get_cell use the width-derived stride while board logic keeps ROW_BYTES = 2, so inflating width makes the renderer read far past the frame and print stack bytes as # /..
Bug 3, clear_row as a bulk shift primitive. memmove(base+stride, base, y*stride); memset(base, 0, stride) with stride = (width+7)/8 shifts idx[0, y*S) up by S and zeroes idx[0,S). This is what moves attacker-written bytes onto the return address.
03Exploitation
What works, verified on the live target
The leak is complete and reliable (exploit/leak3.py):
- Play normally to 60 occupied cells, this sets ceiling_pressure, which is sticky and never reset, and is the precondition for entry_kick.
- The I-piece at sequence index 15 sets width bit 4; five harmless drops; the I at index 21 sets bit 6 → width 90.
- Read one rendered frame and decode it.
Width 90 is the load-bearing choice: stride 12 with ncols = min(80, width) = 80 gives 10 full bytes per row at idx 12y.. 12y+9, so row 4 covers the canary at idx 48-55 and row 8 covers the libc return at idx 96-103.
This yields, on every run against the live target: the canary (LSB 0x00), the libc base (page-aligned), and the PIE base (page-aligned). PIE = idx64 - 0x2e69; libc = idx96 - 0x2a1ca.
The exploit design (validated byte-exactly in simulation)
Because a clear's memset always zeroes a prefix from index 0, no single clear can write idx 64 while preserving the canary at idx 48, so the canary must be rewritten from the leak. The design is a conveyor: bytes are written low, then shifted up by successive clears, landing on the canary and return slots.
Widths: 10 --bit5--> 42 --bit6--> 106 --bit7--> 234 (stride 30, reach 27)
W0 seed the gadget's invisible bits at idx 5,7,9 (zero real board cells)
burn (clear_row(y>=6)) -> carries idx 4..9 to idx 34..39, zeroes idx 40,41
W1 paint canary rows 9-12 and gadget rows 17-19 with real pieces,
OR-write the unpaintable bits, trigger row 16 through a col-9 well
final clear -> canary lands at idx 48, gadget at idx 64
W2 recharge with clear_row(0) (memset covers only idx 0..29, payload survives),
bump width to 746 (stride 94, reach 91), OR-write the remainder directly
top out -> play returns -> canary check passes -> RIP = one_gadgetThe chain was verified byte-exactly in the simulator: seed 9 bits → burn → recharge (seed intact) → paint → final clear → canary exact at idx 48, gadget exact at idx 64, no extra full rows. Engine-verified sub- results: 400 consecutive burns (4.2 pieces/burn, idx 40,41 zero every time), 9 consecutive recharges with the payload intact each cycle, w0_plan solving 54/60 I-dense offsets.
Gadget selection
Register state at play's ret (verified): rax == 0 free from the canary-check sub; rsp & 0xf == 0 free; rbx == argv; rbp = whatever the conveyor left at idx 56.
ONE_GADGET VERDICT (2.39-0UBUNTU8.7)
0x583ec / 0x583f3 need rbx == NULL or (u16)[rbx] == 0 ; rbx = argv ⇒ dead
need rbp-0x50 writable; rbp comes from board bytes ⇒ dead0xef4ce / 0xef527 / 0xef52b
0x11195a needs [rsp+0x70] == NULL and [rsp+0x48] == NULL = idx 184 and idx 144, both determined by burn history and fully simulable ⇒ the target
Why it did not land
The payload costs 55 actions (canary 28 bits + gadget 26 bits + 1 width bump), reduced by whatever can be painted with real pieces instead of OR-written. Twenty of those bits are an irreducible floor: stride is always even, so a conveyor shift never changes byte parity, and an odd target byte always sources from an odd byte, whose bits 2-7 are physically unpaintable (they are "columns 10-15", invisible to the game).
Measured ceilings over 600,000 sequence offsets:
WINDOW CAPACITY MAX I-PIECES OFFSETS ≥18 ≥20 ≥22
pre-clear (W1) 80 cells 18 @ off 548,668 3 — —
post-clear (W2) 160 cells 23 @ off 165,492 558 97 10Best achieved seeds + painted + A = 35, leaving W2 needing ~20 OR-writes. P(W2 I-count >= 20) ~ 97/600,000, about 1 offset in 6,200. W2 is positioned by repeating recharges of ~28 pieces each, so reaching a good offset needs hundreds of consecutive recharges, and recharge reliability is ~90% per cycle, the chain breaks long before it arrives.
Two ways to close it, neither of which was completed: push painted + A from 26 to ~30 (the painter's ceiling looks like ~24 painted, so the gain must come from A), or make recharge_plan2 near-deterministic with a bigger beam and more aggressive row-0 OR patching. Either drops W2's requirement to ≤16 and/or lets the recharge chain survive long enough.
04Dead ends
- Rigorously ruled out (documented so they are not re-derived): no single clear writes idx 64 while preserving idx 48; clear_lines does not compound, because the first clear_row shifts the board and the other rows stop being full; memmove only ever moves data up, so no libc pointer above the return can be moved down onto it; OR-only cannot convert idx 64's PIE value into libc, and turning libc+
0x2a1cainto a one_gadget by pure OR requires a bit-subset relation that measured 0% over random bases; height / score are unreachable (set_cell's minimum index is -2); width bit 0 is unsettable; there is no heap or GOT reach. - The leak's silent truncation. Byte b of a row is only fully readable when 8b+7 <= ncols-1. At width < 80 the last byte of each row is partial, which silently truncated the canary MSB and produced implausible values (
0x00/0x01/0x02). Width 90 fixes it. 0x2a1ca, not0x2a1cc, the call rax at0x2a1c8is a 2-byte instruction. With the wrong offset the leaked libc base is not page-aligned.- An entry-kick lock calls lock_piece but not clear_lines. Completing a row purely with OR-writes therefore does not fire a clear; a normally-locked piece has to follow.
- Running the leak locally reports "libc base MISALIGNED", correct and harmless, because the local libc is 2.43 and its call_main return offset differs from the target's 2.39.
- An independent agent's window-alignment search (exploit/align.py) found 0 feasible offsets for the earlier 2-window design, which is what motivated the 3-window seeding + recharge decoupling redesign.
- The baroqueness of the whole construction is itself evidence this is not the intended path. The most promising next move is to find the author's intended technique, most likely a second write primitive or a non-return control-flow target.
FOUR ROUTES. THREE DEAD ENDS.
AND THE FOURTH RAN OUT OF WRITE BUDGET