aboutsummaryrefslogtreecommitdiffstats
path: root/hw/invoke_struct_fuzz.py
blob: 61cbf791bef2f7dff0916938abd2ab44af5f092f (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
#!/usr/bin/env python3
"""The INVOKE_CMD ring structure is 784 bytes; we have only ever set two fields.

`struct_psp_gfx_cmd_invoke_cmd` is 784 bytes but every invoke in this audit set only
session_id and ta_cmd_id, leaving ~776 bytes of host-controlled data zero. That data is
parsed by the PSP straight from the ring command, a different input path from the shared
buffer, and it has never been fuzzed.

Fill the remainder with patterns and with per-dword mutations; flag status changes, stalls or
writes outside the declared window.
"""
import ctypes, glob, 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)
PAT=0x77777777
base=open(glob.glob('/root/psp/contall/ta_42-0-0-6_1c874c3c*')[0],'rb').read()
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
ap_,amc=alloc(0x80000); cp,cmc=alloc(0x10000)
n=(len(base)+3)&~3
adev.vram.view(ap_,n)[:]=base+b'\x00'*(n-len(base)); 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(base)
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('load %#x sid=%s ; invoke struct is %d bytes'
      %(r.resp.status&0xFFFFFFFF,sid,ctypes.sizeof(am.struct_psp_gfx_cmd_invoke_cmd)), flush=True)
def inv(tag, fill=None, mut=None):
    adev.vram.view(cp,0x10000)[:]=bytes(struct.pack('<I',PAT)*(0x10000//4)); adev.gmc.flush_hdp()
    c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
    raw=memoryview(c).cast('B')
    lo=16
    if fill is not None:
        for off in range(lo, min(len(raw), 16+784), 4): struct.pack_into('<I',raw,off,fill)
    if mut is not None:
        struct.pack_into('<I',raw,mut[0],mut[1])
    # set the two real fields AFTER filling, via the accessors, so they survive
    iv=c.cmd.cmd_invoke_cmd; iv.session_id,iv.ta_cmd_id=sid,1
    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,0x10000)[:])
    bey=sum(1 for o in range(0x4000,0x10000,4) if struct.unpack_from('<I',out,o)[0]!=PAT)
    print('  %-32s -> %#-12x %.1fs beyond-window=%d%s'%(tag,st,dt,bey,'  <<< STALL' if dt>5 else ''), flush=True)
    return dt>5
base_st = inv('CONTROL zeros')
for f,nm in ((0xFFFFFFFF,'ones'),(0x80000000,'msb'),(0x00001000,'0x1000'),(0x8001680000&0xFFFFFFFF,'addr-like')):
    if inv('fill remainder = %s'%nm, fill=f): sys.exit(3)
hits=[]
for off in range(16, 16+784, 4):
    c2=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
    raw=memoryview(c2).cast('B'); struct.pack_into('<I',raw,off,0xFFFFFFFF)
    iv=c2.cmd.cmd_invoke_cmd; iv.session_id,iv.ta_cmd_id=sid,1
    t0=time.time()
    try: st=submit(c2).resp.status&0xFFFFFFFF
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    dt=time.time()-t0
    if dt>5: print('STALL at dword %#x'%off, flush=True); break
    if st!=0: hits.append((hex(off),hex(st)))
print('dwords whose value changes the status: %s'%(hits if hits else 'NONE across all 196'), flush=True)