#!/usr/bin/env python3 """The INVOKE_CMD ring structure is 784 bytes; we have only ever set two fields. `struct_psp_gfx_cmd_invoke_cmd` is 784 bytes but every invoke in this audit set only session_id and ta_cmd_id, leaving ~776 bytes of host-controlled data zero. That data is parsed by the PSP straight from the ring command, a different input path from the shared buffer, and it has never been fuzzed. Fill the remainder with patterns and with per-dword mutations; flag status changes, stalls or writes outside the declared window. """ import ctypes, glob, struct, sys, time sys.path.insert(0,'/root/psp/hw') from tinygrad import Device import tinygrad.runtime.autogen.am.am as am from ring_wrap import install adev=Device["AMD"].iface.dev_impl; psp=adev.psp submit=install(psp,adev) PAT=0x77777777 base=open(glob.glob('/root/psp/contall/ta_42-0-0-6_1c874c3c*')[0],'rb').read() def alloc(n): pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa) ap_,amc=alloc(0x80000); cp,cmc=alloc(0x10000) n=(len(base)+3)&~3 adev.vram.view(ap_,n)[:]=base+b'\x00'*(n-len(base)); 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=amc&0xFFFFFFFF,amc>>32; t.app_len=len(base) t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32; t.cmd_buf_len=0x4000 r=submit(c); sid=r.resp.session_id print('load %#x sid=%s ; invoke struct is %d bytes' %(r.resp.status&0xFFFFFFFF,sid,ctypes.sizeof(am.struct_psp_gfx_cmd_invoke_cmd)), flush=True) def inv(tag, fill=None, mut=None): adev.vram.view(cp,0x10000)[:]=bytes(struct.pack(' %#-12x %.1fs beyond-window=%d%s'%(tag,st,dt,bey,' <<< STALL' if dt>5 else ''), flush=True) return dt>5 base_st = inv('CONTROL zeros') for f,nm in ((0xFFFFFFFF,'ones'),(0x80000000,'msb'),(0x00001000,'0x1000'),(0x8001680000&0xFFFFFFFF,'addr-like')): if inv('fill remainder = %s'%nm, fill=f): sys.exit(3) hits=[] for off in range(16, 16+784, 4): c2=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD) raw=memoryview(c2).cast('B'); struct.pack_into('5: print('STALL at dword %#x'%off, flush=True); break if st!=0: hits.append((hex(off),hex(st))) print('dwords whose value changes the status: %s'%(hits if hits else 'NONE across all 196'), flush=True)