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
|
"""Determinism test = uninitialized-memory detector.
Identical input, repeated. Any output byte that VARIES between runs cannot come
from our request -- it comes from uninitialized stack/heap or stale PSP state.
That is the disclosure primitive this project has been missing.
Also dumps the payload so constant-but-unexplained bytes can be inspected.
"""
import ctypes,struct,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
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)
except RuntimeError: pass
except TimeoutError: return None
return bytes(adev.vram.view(shm,S)[:])
asd,_=load(am.GFX_CMD_ID_LOAD_ASD,open(P+'asd_full.bin','rb').read())
dtm,dsh=load(am.GFX_CMD_ID_LOAD_TA,open(P+'dtm_full.bin','rb').read())
hdcp,hsh=load(am.GFX_CMD_ID_LOAD_TA,open(P+'hdcp_full.bin','rb').read())
def topo(h):
b=bytearray(S); struct.pack_into('<I',b,0,2); struct.pack_into('<10I',b,0x10,h,1,1,0,0,0,0,0,0,0)
o=inv(dtm,dsh,b,2); return struct.unpack_from('<I',o,8)[0] if o else None
print("bring-up:",[topo(h) for h in range(8)])
def open_slot():
b=bytearray(S); struct.pack_into('<I',b,0,0x0E); struct.pack_into('<I',b,0xC,0)
o=inv(hdcp,hsh,b,0x0E); return struct.unpack_from('<I',o,4)[0], struct.unpack_from('<I',o,0x1410)[0]
g,SLOT=open_slot(); print("gate ret=%s slot=%s"%(g,SLOT))
if g!=0: print("GATE CLOSED - cold cycle needed"); sys.exit(1)
def mk(sub,N,fill=0x41):
ln=5*N+25+1
b=bytearray(S)
for i in range(0x1400,S): b[i]=MARK
struct.pack_into('<I',b,0,0x0F); struct.pack_into('<I',b,0xC,SLOT)
struct.pack_into('<I',b,0x10,sub); struct.pack_into('<I',b,0x14,ln)
b[0x28]=sub; b[0x29]=0; b[0x2A]=0; b[0x2B]=N&0xFF; b[0x2C]=0
for k in range(0x32,min(0x28+ln+1,0x1400)): b[k]=fill
return b
print("\nsubcmd N varying_bytes payload(first 40B of 0x1410)")
found=[]
for sub in (0,4,5,7,8,12,15,16,20):
for N in (0,8,40):
outs=[]
for rep in range(6):
o=inv(hdcp,hsh,mk(sub,N),0x0F)
if o is None: print(" TIMEOUT sub=%d N=%d"%(sub,N)); break
outs.append(o[0x1400:0x1500])
if len(outs)<6: continue
vary=[i for i in range(len(outs[0])) if len(set(x[i] for x in outs))>1]
tag=""
if vary:
tag=" <== NON-DETERMINISTIC at %s"%[hex(0x1400+i) for i in vary[:8]]
found.append((sub,N,vary))
print(" %-5d %-3d %5d %s%s"%(sub,N,len(vary),outs[0][0x10:0x38].hex(),tag))
print("\nnon-deterministic sites: %d"%len(found))
# also: does the payload differ when we change ONLY the fill byte? (echo vs leak)
o1=inv(hdcp,hsh,mk(12,40,0x41),0x0F); o2=inv(hdcp,hsh,mk(12,40,0x42),0x0F)
d12=[i for i in range(0x1400,0x1500) if o1[i]!=o2[i]]
print("bytes in output that track our fill byte (echo, not leak): %d"%len(d12))
print("sos_alive:",psp.is_sos_alive())
|