diff options
| author | user <user@clank> | 2026-09-23 00:30:17 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-09-23 00:30:17 +0200 |
| commit | df3aa862b504ea90c535ea8487a9508939d95093 (patch) | |
| tree | 1796d5c6948bbc4945f20d66ed53376c16146c49 | |
| parent | Trace RAS's init gate to a SYS_DRV RPC dependency (diff) | |
| download | amd-psp-re-df3aa862b504ea90c535ea8487a9508939d95093.tar.gz amd-psp-re-df3aa862b504ea90c535ea8487a9508939d95093.tar.xz amd-psp-re-df3aa862b504ea90c535ea8487a9508939d95093.zip | |
EXPERIMENTAL: bridge TA service-1 RPCs into a real SYS_DRV (does not yet open RAS)
Acts on the RAS diagnosis from the previous commit. Component-level, SVC 0xF2
service id 1 is bound to CryptoService -- the wrong service -- so RAS's four
SYS_DRV RPCs could never do anything. This routes them into a real SYS_DRV, the
same shape as Bus.route_asd routes service 2 into ASD.
The TA-side request needs no translation: dumped at the gate, it is already in
SYS_DRV's own format ([0]=opcode, [1]=2, {ptr,len} pairs from dword 2). The
bridge shadow-copies the caller's buffers into SYS_DRV, rewrites the pointers,
runs sub_E087E4, and copies results back without ever handing the TA a shadow
pointer.
What works: SYS_DRV comes up with its init gate genuinely open (run_init,
opcode 0x1000, init_flag 0 -> 1, asserted -- not a stub), requests forward and
execute, and opcode 0x1077 returns 0.
What does not: opcode 0x0001 faults inside SYS_DRV at 0xE01940, the store half
of a bulk ARM memcpy, writing to ~0x680 -- a near-null pointer plus an offset.
So SYS_DRV is copying into a buffer whose address it expected to find in a
request field we have not modelled. Supplying the config block at request
dword 18 (sysdrv_env.REQ_CONFIG_DWORD, which the TA leaves NULL) was tried and
changes nothing.
Committed as EXPERIMENTAL and clearly labelled because the mechanism is real and
the blocker is precisely located, not because it is finished. Stated in the file
and in the output: this is a harness gap, NOT a SYS_DRV defect -- we are handing
SYS_DRV a request assembled by hand from a TA-side capture with at least one
field still unknown, so no fault seen here is evidence about the firmware. RAS
command coverage remains explicitly not reportable.
Second known gap recorded rather than hidden: the region validators sub_E0AB44 /
sub_E0DEF4 are RET1'd here exactly as fuzz_sysdrv does, but our shadow buffers
live in SYS_DRV scratch, which the real check would refuse. That is the same
problem fuzz_asd had with asd_validate_ptr and deserves the same fix -- move the
shadows where the validator accepts them -- rather than a patch.
| -rw-r--r-- | psp_emu/README.md | 1 | ||||
| -rw-r--r-- | psp_emu/harness/sysdrv_bridge.py | 159 |
2 files changed, 160 insertions, 0 deletions
diff --git a/psp_emu/README.md b/psp_emu/README.md index 9332ae0..6773248 100644 --- a/psp_emu/README.md +++ b/psp_emu/README.md @@ -28,6 +28,7 @@ Images are read from `../psp_out/` (`elf/*.elf`, falling back to `*_full.bin`). | `harness/load_asd.py` | ~51s | ASD driver loaded, verified and executing (+2 negative controls) | | `harness/asd_rpc.py` | ~10s | HDCP's ASD RPCs executing in the real ASD dispatcher | | `harness/dtm_ras.py` | ~20s | DTM commands 2/3/4 driven; RAS gate documented | +| `harness/sysdrv_bridge.py` | ~60s | **experimental** — TA SVC 0xF2 service-1 routed into a real SYS_DRV; does not yet open RAS's gate | | `harness/fuzz_pss_differential.py` | ~90s | firmware PSS parser vs RFC 8017, own keypair | | `harness/fuzz_ta_invoke.py` | ~0.25s/invoke | host-driven TA invoke fuzzing — **exits 2**, see below | diff --git a/psp_emu/harness/sysdrv_bridge.py b/psp_emu/harness/sysdrv_bridge.py new file mode 100644 index 0000000..69d7fd5 --- /dev/null +++ b/psp_emu/harness/sysdrv_bridge.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python3 +"""EXPERIMENTAL: route a TA's SVC 0xF2 service-1 calls into a real SYS_DRV. + +Status: partially working. It does NOT yet open RAS's init gate, and it does not +report RAS results. Committed because the mechanism works and the remaining +blocker is precisely located, not because the job is done. + +Why this exists +--------------- +`harness/dtm_ras.py` traced RAS's init gate to a SYS_DRV RPC dependency: RAS's +`TA_OpenSessionEntryPoint` issues four SVC 0xF2 service-1 calls (opcodes 0x0001, +0x000d, 0x0002, 0x1077) and only sets the gate byte at 0x4060e0 when they +succeed. Component-level, service id 1 is bound to `CryptoService` -- the wrong +service entirely -- so those calls could never do anything. + +The request RAS builds is already in SYS_DRV's own format, verified by dumping +it at the gate: `[0]=opcode`, `[1]=2`, `{ptr,len}` pairs from dword 2. So the +bridge is the same shape as `Bus.route_asd`: shadow-copy the caller's buffers +into SYS_DRV, rewrite the pointers, run `sub_E087E4`, copy results back. + +What works +---------- +* SYS_DRV comes up with its init gate genuinely open (`sysdrv_env.run_init`, + opcode 0x1000 -> `init_flag` 0 -> 1), so this is a real SYS_DRV, not a stub. +* Requests forward and execute. Opcode 0x1077 returns 0. + +What does not +------------- +Opcode 0x0001 faults inside SYS_DRV at `0xE01940`: + + 00e0193c subs r2, r2, #0x20 + 00e01940 stm r0!, {r3, r4, ip, lr} <-- WRITE_UNMAPPED at 0x680 + +That is the store half of a bulk ARM memcpy, with a destination of ~0x680 -- +a near-null pointer plus an offset. So SYS_DRV is copying into a buffer whose +address it expected to find somewhere we have not modelled. Supplying the +config block at request dword 18 (`sysdrv_env.REQ_CONFIG_DWORD`, which +`sysdrv_env.dispatch` provides and the TA leaves NULL) was tried and does not +change it. + +**This is a harness gap, not a firmware defect.** Nothing here should be read as +a SYS_DRV bug: we are handing it a request assembled by hand from a TA-side +capture, with at least one field still unmodelled. Finding which field carries +that destination is the next step. + +Also unresolved: the region validators `sub_E0AB44` / `sub_E0DEF4` are RET1'd +here, exactly as `fuzz_sysdrv` does. On the real path sOS has already validated +the buffers, but our shadow buffers live in SYS_DRV scratch, which the real +check would refuse -- the same problem `fuzz_asd` had with `asd_validate_ptr`, +and it deserves the same fix (put the shadows where the validator accepts them) +rather than a patch. +""" +import os +import struct +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) + +from pspemu import Component, sysdrv_env # noqa: E402 + +RAS_OPEN, RAS_CREATE, RAS_FLAG = 0x402C64, 0x402B08, 0x4060E0 +SD_ENTRY = 0xE087E4 +REQ_BYTES = 0x7C +PAIRS = [(2, 3), (4, 5), (6, 7), (8, 9)] # dword indices of {ptr,len} +RET1 = bytes([0x01, 0x20, 0x70, 0x47]) # MOVS R0,#1 ; BX LR + + +def make_sysdrv(verbose=True): + sd = sysdrv_env.make() + rv, fault, _blocks = sysdrv_env.run_init(sd) + flag = sysdrv_env.init_flag(sd) + if verbose: + print("SYS_DRV: run_init rv=%s fault=%s init_flag=%d" % (rv, fault, flag)) + if flag != 1: + raise RuntimeError("SYS_DRV init gate did not open; every RPC would " + "bounce off sub_E087E4 and mean nothing") + sd.write(0xE0AB44, RET1) # see the docstring: validator patch, a known gap + sd.write(0xE0DEF4, RET1) + return sd + + +def make_bridge(sd, log): + scratch, req_buf = sd.alloc(0x8000), sd.alloc(0x100) + cfg = sd.alloc(0x400) + sd.write(cfg, b"\x00" * 0x400) + + def bridge(caller, req_ptr): + raw = caller.read(req_ptr, REQ_BYTES) + d = list(struct.unpack("<%dI" % (REQ_BYTES // 4), raw)) + orig = list(d) + shadows, off = [], 0 + for pi, li in PAIRS: + ptr, ln = d[pi], d[li] + if ptr and 0 < ln <= 0x1000: + sd.write(scratch + off, caller.read(ptr, ln)) + shadows.append((ptr, scratch + off, ln)) + d[pi] = scratch + off + off += (ln + 0xF) & ~0xF + if d[sysdrv_env.REQ_CONFIG_DWORD] == 0: + d[sysdrv_env.REQ_CONFIG_DWORD] = cfg + sd.write(req_buf, struct.pack("<%dI" % (REQ_BYTES // 4), *d)) + try: + rv = sd.run_function(SD_ENTRY, (req_buf, 2, 0, 0), count=20_000_000) + except Exception as e: + f = sd.faults[-1] if sd.faults else {} + log.append((orig[0], "FAULT %s pc=%s addr=%s" % ( + f.get("kind"), hex(f.get("pc", 0)), hex(f.get("addr", 0))))) + return 0xFFFF0000 + out = bytearray(sd.read(req_buf, REQ_BYTES)) + for pi, _li in PAIRS: # never hand back our shadows + struct.pack_into("<I", out, pi * 4, orig[pi]) + caller.write(req_ptr, bytes(out)) + for o, dst, ln in shadows: + caller.write(o, sd.read(dst, ln)) + log.append((orig[0], hex(rv))) + return rv + + return bridge + + +def main(): + sd = make_sysdrv() + log = [] + ras = Component("ras") + for s in (0xF0, 0xF1, 0x5E, 0x5F, 0x76, 0x7B, 0x7C, 0x93, 0x66): + ras.svc.register(s, lambda comp, n: 0) + ras.svc.register(0x8D, lambda comp, n: 0x31) # ASIC id, as fuzz_asd models + ras.set_rpc_services({1: make_bridge(sd, log), 2: lambda comp, req: 0}) + try: + ras.run_function(RAS_CREATE, (0, 0, 0, 0)) + except Exception: + pass + buf = ras.alloc(0x8000); ras.write(buf, b"\x00" * 0x8000) + p = ras.alloc(0x40); ras.write(p, b"\x00" * 0x40) + ras.wr32(p + 0, buf); ras.wr32(p + 4, 0x4000) + k = ras.alloc(8); ras.write(k, b"\x00" * 8) + + before = ras.read(RAS_FLAG, 1)[0] + rv = ras.run_function(RAS_OPEN, (7, p, k, 0)) + after = ras.read(RAS_FLAG, 1)[0] + print("\nRAS open_session rv=%#x init flag %d -> %d" % (rv, before, after)) + print("SYS_DRV RPCs forwarded:") + for op, res in log: + print(" opcode %#06x -> %s" % (op, res)) + + print("\n== status ==") + print(" SYS_DRV real, init gate open : yes") + print(" RPCs forwarded : %d" % len(log)) + print(" RAS init gate opened : %s" % ("yes" if after else "NO")) + if not after: + print("\n RAS COMMAND COVERAGE STILL NOT REPORTABLE. The bridge mechanism") + print(" works but at least one request field is unmodelled (see the") + print(" 0xE01940 memcpy note in this file's docstring). Nothing here is") + print(" evidence of a SYS_DRV defect.") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) |
