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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
"""Fuzz the RAS TA -- ~100 of its 119 functions were never audited and prior
campaigns never actually invoked it (they misread their own input as the status).
Command in frame[0]: 0 ENABLE, 1 DISABLE, 2 TRIGGER_ERROR, 3 QUERY_BLOCK_INFO,
4 QUERY_SUB_BLOCK_INFO. Response lands at +0x810..0x823.
cmd 2 TRIGGER_ERROR is included only in the second phase and gated behind a
health check each round -- it drives real error injection.
"""
import ctypes,struct,random,sys,collections
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/'; S=0x8000; MARK=0x5A
RANGES=[(0x00020000,0x00070000,'sOS'),(0x00400000,0x00412000,'RAS/HDCP'),
(0x00D00000,0x00D50000,'ASD'),(0x00E10000,0x00E80000,'SYSDRV'),(0x0020A000,0x0020C000,'TAstage')]
def classify(v):
for lo,hi,nm in RANGES:
if lo<=v<hi: return nm
return None
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(S)
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=S
return psp._ring_submit(c).resp.session_id, cp
def inv(sid,shm,buf,tc):
adev.vram.view(shm,S)[:]=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
try: psp._ring_submit(ic); ps=0
except RuntimeError: ps=stat()
except TimeoutError: return "TIMEOUT",None
return ps, bytes(adev.vram.view(shm,S)[:])
ras,rsh=load(am.GFX_CMD_ID_LOAD_TA, open(P+'ras_full.bin','rb').read())
print("RAS sid=%s"%ras)
rng=random.Random(int(sys.argv[1]) if len(sys.argv)>1 else 17)
ROUNDS=int(sys.argv[2]) if len(sys.argv)>2 else 6000
PHASE2=len(sys.argv)>3 and sys.argv[3]=='trigger'
CMDS=[0,1,3,4]+([2] if PHASE2 else [])
EX=[0,1,2,3,4,5,6,7,8,9,0xa,0xb,0xc,0xd,0xe,0xf,0x10,0x1f,0x20,0x21,0x31,0x32,0xff,0x100,
0xffff,0x10000,0xffffffff,0x7fffffff,0x80000000,0xfffffffe,49,64,255,256,0x828,0x829]
rets=collections.defaultdict(collections.Counter); leaks={}; hangs=0; sizes=collections.Counter()
for r in range(ROUNDS):
c=rng.choice(CMDS)
b=bytearray(S)
for i in range(0x800,S): b[i]=MARK
struct.pack_into('<I',b,0,c)
# fuzz the request area (RAS reads its params from the shared buffer)
for off in range(4,0x200,4):
if rng.random()<0.6: struct.pack_into('<I',b,off,rng.choice(EX))
else: struct.pack_into('<I',b,off,rng.getrandbits(32))
sent=bytes(b)
ps,o=inv(ras,rsh,b,rng.choice([c,5]))
if o is None:
hangs+=1; print(" [r=%d] HANG cmd=%d alive=%s"%(r,c,psp.is_sos_alive()))
open('rashang_r%d.bin'%r,'wb').write(sent)
if not psp.is_sos_alive() or hangs>3: break
continue
rets[c][struct.unpack_from('<I',o,8)[0]]+=1 # +0x08 is the RAS status; +0x04 is just the cmd echo
diff=[i for i in range(S) if sent[i]!=o[i]]
sizes[len(diff)]+=1
for off in range(0,S-3,4):
if sent[off:off+4]==o[off:off+4]: continue
v=struct.unpack_from('<I',o,off)[0]; cl=classify(v)
if cl and (cl,off) not in leaks:
leaks[(cl,off)]=v
print(" [r=%d] *** PSP POINTER LEAK %s %#010x at shm+%#x cmd=%d ***"%(r,cl,v,off,c))
open('rasleak_r%d.bin'%r,'wb').write(o)
if r%1500==0 and r: print(" ..r=%d alive=%s leaks=%d hangs=%d"%(r,psp.is_sos_alive(),len(leaks),hangs))
print("\nwrite-size histogram (top): %s"%sorted(sizes.items(),key=lambda t:-t[0])[:8])
for c in sorted(rets): print(" cmd %d rets: %s"%(c,dict(list(rets[c].items())[:8])))
print("PSP-pointer leaks: %d hangs: %d"%(len(leaks),hangs))
print("sos_alive:",psp.is_sos_alive())
|