#!/usr/bin/env python3 """Handle / state-machine fuzz: feed a command's OUTPUT back in as a handle. The payload fuzzes mutate inputs only. The GCP lifetime defects found statically earlier in this project (create/destroy/lookup on a slot table) are a different shape: they need a handle produced by one command to be replayed into another, including after it is destroyed. This drives that shape generically: invoke a command, harvest the dwords it wrote as candidate handles, then replay each into every other command -- as-is, and mutated (+1, |0x80000000, 0xFFFFFFFF). Flags a stale/forged handle that is ACCEPTED where the same command rejects a random value, which is the signature of a missing liveness or ownership check. Control: a random 32-bit value in the same field must be rejected; if everything is accepted the command ignores the field and the result is not evidence. """ import glob, random, 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) def alloc(n): pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa) TAS={'GCP':'ta_22-0-0-77_7c426e7f','MERIT':'ta_24-0-0-70_5cd4afda','OPM':'ta_25-0-0-81_dc596ba6', 'CSU':'ta_42-0-0-6_1c874c3c','OC':'ta_50-0-0-1_cbd47c59','XGMI':'ta_32-0-0-20_20a3d1a4'} name=sys.argv[1] blob=open(glob.glob('/root/psp/contall/'+TAS[name]+'*')[0],'rb').read() ap_,amc=alloc(0x80000); cp,cmc=alloc(0x8000) n=(len(blob)+3)&~3 adev.vram.view(ap_,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=amc&0xFFFFFFFF,amc>>32; t.app_len=len(blob) 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('%s load %#x sid=%s'%(name,r.resp.status&0xFFFFFFFF,sid), flush=True) def inv(cid, fields): buf=bytearray(0x4000); struct.pack_into('5: print(' stall on cmd %d'%cid, flush=True); break for v in outs: if v not in (0,cid) and v!=0xFFFFFFFF: handles.add(v) print(' harvested %d candidate handles'%len(handles), flush=True) hits=[] for cid in range(0,12): rnd=rng.getrandbits(32) base,_,_ = inv(cid, [(4,rnd)]) # control: random value in the handle slot for h in list(handles)[:8]: for mut,tag in ((h,'as-is'),(h+1,'+1'),(h|0x80000000,'|msb'),(0xFFFFFFFF,'ones')): s,dt,_ = inv(cid, [(4,mut)]) if dt>5: hits.append(('STALL',cid,tag,hex(mut))); break if s==0 and base!=0: hits.append(('ACCEPTED',cid,tag,hex(mut),'ctrl=%#x'%base)) if hits and hits[-1][0]=='STALL': break if hits and hits[-1][0]=='STALL': break print('%s: %s'%(name, hits[:6] if hits else 'no stale/forged handle accepted where a random value is rejected'), flush=True)