aboutsummaryrefslogtreecommitdiffstats
path: root/hw/ta_handlefuzz.py
blob: 1e30ecfa39c5eda192fa69e5d2541e7da42b201b (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
#!/usr/bin/env python3
"""Handle / state-machine fuzz: feed a command's OUTPUT back in as a handle.

The payload fuzzes mutate inputs only. The GCP lifetime defects found statically earlier in
this project (create/destroy/lookup on a slot table) are a different shape: they need a
handle produced by one command to be replayed into another, including after it is destroyed.

This drives that shape generically: invoke a command, harvest the dwords it wrote as candidate
handles, then replay each into every other command -- as-is, and mutated (+1, |0x80000000,
0xFFFFFFFF). Flags a stale/forged handle that is ACCEPTED where the same command rejects a
random value, which is the signature of a missing liveness or ownership check.

Control: a random 32-bit value in the same field must be rejected; if everything is accepted
the command ignores the field and the result is not evidence.
"""
import glob, random, struct, sys, time
sys.path.insert(0,'/root/psp/hw')
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
from ring_wrap import install
adev=Device["AMD"].iface.dev_impl; psp=adev.psp
submit=install(psp,adev)
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
TAS={'GCP':'ta_22-0-0-77_7c426e7f','MERIT':'ta_24-0-0-70_5cd4afda','OPM':'ta_25-0-0-81_dc596ba6',
     'CSU':'ta_42-0-0-6_1c874c3c','OC':'ta_50-0-0-1_cbd47c59','XGMI':'ta_32-0-0-20_20a3d1a4'}
name=sys.argv[1]
blob=open(glob.glob('/root/psp/contall/'+TAS[name]+'*')[0],'rb').read()
ap_,amc=alloc(0x80000); cp,cmc=alloc(0x8000)
n=(len(blob)+3)&~3
adev.vram.view(ap_,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp()
c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_LOAD_TA); t=c.cmd.cmd_load_ta
t.app_phy_addr_lo,t.app_phy_addr_hi=amc&0xFFFFFFFF,amc>>32; t.app_len=len(blob)
t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32; t.cmd_buf_len=0x4000
r=submit(c); sid=r.resp.session_id
print('%s load %#x sid=%s'%(name,r.resp.status&0xFFFFFFFF,sid), flush=True)
def inv(cid, fields):
    buf=bytearray(0x4000); struct.pack_into('<I',buf,0,cid)
    for off,val in fields: struct.pack_into('<I',buf,off,val&0xFFFFFFFF)
    adev.vram.view(cp,0x4000)[:]=bytes(buf); adev.gmc.flush_hdp()
    c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
    iv=c.cmd.cmd_invoke_cmd; iv.session_id,iv.ta_cmd_id=sid,cid
    t0=time.time()
    try: st=submit(c).resp.status&0xFFFFFFFF
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    dt=time.time()-t0
    adev.gmc.flush_hdp()
    out=bytes(adev.vram.view(cp,0x200)[:])
    return st, dt, [struct.unpack_from('<I',out,o)[0] for o in range(4,0x40,4)]
rng=random.Random(0xBEEF)
handles=set()
for cid in range(0,12):
    s,dt,outs = inv(cid, [])
    if dt>5: print('  stall on cmd %d'%cid, flush=True); break
    for v in outs:
        if v not in (0,cid) and v!=0xFFFFFFFF: handles.add(v)
print('  harvested %d candidate handles'%len(handles), flush=True)
hits=[]
for cid in range(0,12):
    rnd=rng.getrandbits(32)
    base,_,_ = inv(cid, [(4,rnd)])         # control: random value in the handle slot
    for h in list(handles)[:8]:
        for mut,tag in ((h,'as-is'),(h+1,'+1'),(h|0x80000000,'|msb'),(0xFFFFFFFF,'ones')):
            s,dt,_ = inv(cid, [(4,mut)])
            if dt>5: hits.append(('STALL',cid,tag,hex(mut))); break
            if s==0 and base!=0: hits.append(('ACCEPTED',cid,tag,hex(mut),'ctrl=%#x'%base))
        if hits and hits[-1][0]=='STALL': break
    if hits and hits[-1][0]=='STALL': break
print('%s: %s'%(name, hits[:6] if hits else 'no stale/forged handle accepted where a random value is rejected'), flush=True)