aboutsummaryrefslogtreecommitdiffstats
path: root/psp_out/probe_invoke_exhaust.py
blob: 534270b08f03e74425a4ca9a019ea6dde6635a55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Does the PSP wedge after a fixed number of TA invocations, with BENIGN input?

Prior fuzzing wedged at exactly 1006 invocations in two independent runs with
different command mixes. If a well-formed, repeatedly-valid command also wedges
at the same count, this is a plain resource leak in the TA invoke path -- a
host-reachable DoS that any heavy HDCP user would hit, not a fuzz artifact.
"""
import ctypes,struct,time,sys
from tinygrad import Device
from tinygrad.runtime.autogen.am import am
from tinygrad.helpers import lo32,hi32
d=Device["AMD"]; adev=d.iface.dev_impl; psp=adev.psp
SZ=ctypes.sizeof(am.struct_psp_gfx_cmd_resp); P='/Users/user/dev/amd-psp-re/psp_out/'
def stat(): return int.from_bytes(bytes(adev.vram.view(psp.cmd_paddr,SZ)[:])[864:868],"little")
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True); return pa,adev.paddr2mc(pa)
def load(cmd,blob):
    ap,amc=alloc(0x40000); cp,cmc=alloc(0x8000)
    adev.vram.view(ap,len(blob))[:]=blob; adev.gmc.flush_hdp()
    adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
    c=am.struct_psp_gfx_cmd_resp(cmd_id=cmd); t=c.cmd.cmd_load_ta
    t.app_phy_addr_hi,t.app_phy_addr_lo=hi32(amc),lo32(amc); t.app_len=len(blob)
    t.cmd_buf_phy_addr_hi,t.cmd_buf_phy_addr_lo=hi32(cmc),lo32(cmc); t.cmd_buf_len=0x8000
    return psp._ring_submit(c).resp.session_id,cp
NINV=0
def inv(sid,shm,buf,tc):
    global NINV
    adev.vram.view(shm,0x8000)[:]=bytes(buf); adev.gmc.flush_hdp()
    adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
    ic=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD); iv=ic.cmd.cmd_invoke_cmd
    iv.session_id,iv.ta_cmd_id=sid,tc
    NINV+=1; t0=time.time()
    try: psp._ring_submit(ic); return 0,bytes(adev.vram.view(shm,0x40)[:]),time.time()-t0
    except RuntimeError: return stat(),None,time.time()-t0
    except TimeoutError: return "TIMEOUT",None,time.time()-t0
a,_=load(am.GFX_CMD_ID_LOAD_ASD,open(P+'asd_full.bin','rb').read())
ds,dshm=load(am.GFX_CMD_ID_LOAD_TA,open(P+'dtm_full.bin','rb').read())
hs,hshm=load(am.GFX_CMD_ID_LOAD_TA,open(P+'hdcp_full.bin','rb').read())
print("sids asd=%#x dtm=%#x hdcp=%#x"%(a,ds,hs))
MODE=sys.argv[1] if len(sys.argv)>1 else 'hdcp'
MAX=int(sys.argv[2]) if len(sys.argv)>2 else 1400
if MODE=='hdcp':
    b=bytearray(0x8000); struct.pack_into('<I',b,0,0x02)   # benign destroy_session, always valid shape
    tgt=(hs,hshm,bytes(b),0x02)
else:
    b=bytearray(0x8000); struct.pack_into('<I',b,0,2); struct.pack_into('<10I',b,0x10,0,1,1,0,0,0,0,0,0,0)
    tgt=(ds,dshm,bytes(b),2)
print("issuing identical BENIGN %s invocations until wedge (max %d)"%(MODE,MAX))
for i in range(MAX):
    ps,out,dt=inv(*tgt)
    if ps=="TIMEOUT" or dt>0.5:
        print("  >> WEDGE at invocation #%d (total ring invokes incl. setup = %d)"%(i,NINV))
        print("     dt=%.2f sos_alive=%s"%(dt,psp.is_sos_alive())); break
    if i%200==0: print("  ..%d ok (ta_ret=%s)"%(i,struct.unpack_from('<I',out,4)[0] if out else None))
else:
    print("  no wedge in %d invocations (total %d)"%(MAX,NINV))
print("sos_alive:",psp.is_sos_alive())