aboutsummaryrefslogtreecommitdiffstats
path: root/psp_out/fuzz_hdcp_10_13.py
blob: 68601a3f62b2f0c0489b2cfd889b2ad9e742d92c (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Fuzz HDCP commands 0x10-0x13 -- never touched (earlier runs stopped at 0x0F).
  0x10 hdcp2_enable_dp_stream_encryption
  0x11 hdcp_close_all_sessions      (also resets slots, avoids exhaustion)
  0x12 hdcp_set_protection
  0x13 hdcp_get_srm_status          (returns data -> disclosure candidate)
"""
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,'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 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 mk(cmd,mut=None,mark=True):
    b=bytearray(S)
    if mark:
        for i in range(0x1400,S): b[i]=MARK
    struct.pack_into('<I',b,0,cmd)
    if mut: mut(b)
    return b
# baseline: what does each new command do?
print("\nbaseline (zeroed request):")
for cmd in (0x10,0x11,0x12,0x13):
    b=mk(cmd); sent=bytes(b); o=inv(hdcp,hsh,b,cmd)
    if o is None: print("  %#04x TIMEOUT"%cmd); continue
    r=struct.unpack_from('<I',o,4)[0]
    diff=[i for i in range(S) if sent[i]!=o[i]]
    print("  %#04x ret=%-5s wrote %d bytes %s %s"%(cmd,r,len(diff),
        ("%#06x..%#06x"%(diff[0],diff[-1])) if diff else "", o[diff[0]:diff[0]+32].hex() if diff else ""))
rng=random.Random(int(sys.argv[1]) if len(sys.argv)>1 else 9)
ROUNDS=int(sys.argv[2]) if len(sys.argv)>2 else 4000
EX=[0,1,2,3,4,5,7,8,0xff,0x100,0xffff,0x10000,0xffffffff,0x7fffffff,0x80000000,24,25,32,63,64,255,0x27b,0x1400,0x2817]
rets=collections.defaultdict(collections.Counter); leaks={}; big=[]
for r in range(ROUNDS):
    cmd=rng.choice([0x10,0x11,0x12,0x13])
    def mut(b):
        for off in (0x0C,0x10,0x14,0x18,0x1C,0x20,0x24):
            struct.pack_into('<I',b,off,rng.choice(EX))
        n=rng.choice([16,64,512,0x244,0x400])
        for k in range(0x28,min(0x28+n,0x1400)): b[k]=rng.randrange(256)
        for _ in range(8):
            o2=rng.randrange(0x28,0x1300)&~3; struct.pack_into('<I',b,o2,rng.choice(EX))
    b=mk(cmd,mut); sent=bytes(b); o=inv(hdcp,hsh,b,cmd)
    if o is None:
        print("  [r=%d] TIMEOUT cmd=%#x alive=%s"%(r,cmd,psp.is_sos_alive()))
        open('anom_hdcp_%02x_r%d.bin'%(cmd,r),'wb').write(sent); break
    rets[cmd][struct.unpack_from('<I',o,4)[0]]+=1
    diff=[i for i in range(0x1400,S) if sent[i]!=o[i]]
    if len(diff)>24 and len(big)<8:
        big.append(cmd); print("  [r=%d] cmd %#x wrote %d bytes %#06x..%#06x : %s"%(r,cmd,len(diff),diff[0],diff[-1],o[diff[0]:diff[0]+40].hex()))
    for off in range(0x1400,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=%#x ***"%(r,cl,v,off,cmd))
            open('leak1013_r%d.bin'%r,'wb').write(o)
    if r%1000==0 and r: print("  ..r=%d alive=%s leaks=%d"%(r,psp.is_sos_alive(),len(leaks)))
print("\nret histograms:")
for c in sorted(rets): print("  %#04x : %s"%(c,dict(rets[c])))
print("PSP-pointer leaks: %d"%len(leaks))
print("sos_alive:",psp.is_sos_alive())