aboutsummaryrefslogtreecommitdiffstats
path: root/hw/ta_oob_write.py
blob: fca3fdc11972531d5b79011c8f6d3a00a86d246c (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
#!/usr/bin/env python3
"""Does any TA write OUTSIDE the shared buffer length declared at LOAD_TA?

LOAD_TA declares cmd_buf_len. If a TA writes past it, the PSP is corrupting host memory
beyond a bound the host set -- a memory-corruption primitive, in the direction that matters
(privileged writer, host victim).

Method: allocate 0x8000, declare cmd_buf_len = 0x1000, fill the WHOLE 0x8000 with a guard
pattern, invoke each command, then check whether any byte at or beyond 0x1000 changed.

Control: bytes below 0x1000 are expected to change (that is the legitimate window); if nothing
changes anywhere, the harness did not exercise the TA and the result is void.
"""
import glob, struct, sys
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
adev = Device["AMD"].iface.dev_impl; psp = adev.psp
PAT = 0xA5A5A5A5
DECL = int(__import__('os').environ.get('DECL','0x1000'),0)
TOTAL = 0x8000
def alloc(n):
    pa = adev.mm.palloc(n, align=0x1000, zero=True, boot=False); return pa, adev.paddr2mc(pa)
def load(blob):
    ap_, amc = alloc(0x80000); cp, cmc = alloc(TOTAL)
    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=DECL                      # <-- declare only 0x1000
    try:
        r=psp._ring_submit(c); return r.resp.status&0xFFFFFFFF, r.resp.session_id, cp
    except Exception as e:
        try: return int(str(e).strip().split()[-1])&0xFFFFFFFF,None,cp
        except Exception: return -1,None,cp
def probe(sid, shm, cid):
    buf = bytearray(struct.pack('<I', PAT) * (TOTAL//4))
    struct.pack_into('<I', buf, 0, cid)
    adev.vram.view(shm, TOTAL)[:] = 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
    try:
        r=psp._ring_submit(c); st=r.resp.status&0xFFFFFFFF
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    adev.gmc.flush_hdp()
    out=bytes(adev.vram.view(shm, TOTAL)[:])
    inside = sum(1 for o in range(4,DECL,4) if struct.unpack_from('<I',out,o)[0]!=PAT)
    beyond = [(o,struct.unpack_from('<I',out,o)[0]) for o in range(DECL,TOTAL,4)
              if struct.unpack_from('<I',out,o)[0]!=PAT]
    return st, inside, beyond
TAS=[('AUC','ta_16-0-0-9_9e9044cf'),('CSU','ta_42-0-0-6_1c874c3c'),('FP','ta_19-0-0-8_7e8be282'),
     ('GCP','ta_22-0-0-77_7c426e7f'),('MERIT','ta_24-0-0-70_5cd4afda'),('OC','ta_50-0-0-1_cbd47c59'),
     ('OPM','ta_25-0-0-81_dc596ba6'),('TMZ','ta_29-0-0-4_ce34f538'),('WSPE','ta_30-0-0-38_ec5dee6d'),
     ('XGMI','ta_32-0-0-20_20a3d1a4')]
only=sys.argv[1].split(',') if len(sys.argv)>1 else None
SKIP={'AUC':{6},'FP':{7}}
for name,stem in TAS:
    if only and name not in only: continue
    cands=glob.glob('/root/psp/contall/'+stem+'*')
    if not cands: continue
    st,sid,shm=load(open(cands[0],'rb').read())
    if st!=0 or sid is None: print('%-6s LOAD %#x'%(name,st)); continue
    tot_in=0; oob=[]
    for cid in range(0,12):
        if cid in SKIP.get(name,set()): continue
        s,ins,bey = probe(sid, shm, cid)
        tot_in += ins
        if bey: oob.append((cid,s,len(bey),bey[:4]))
    verdict = ('OOB WRITES: %s'%oob[:3]) if oob else ('no writes past the declared length'
               if tot_in else 'VOID: nothing changed anywhere (harness did not exercise the TA)')
    print('%-6s in-window dwords changed=%-5d %s'%(name,tot_in,verdict))