aboutsummaryrefslogtreecommitdiffstats
path: root/hw/hdr_field_test.py
blob: f25323e2784edde90056d03879c0d00bdc8a6161 (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
#!/usr/bin/env python3
"""TA header fields +0x1c and +0x20 survive modification and still authenticate. Used or ignored?

Zeroing either still yields LOAD_TA status 0x0 on a signed image, so they are outside what the
signature covers (or are not checked). Both hold addresses in the image's own range
(+0x1c = 0x402000, +0x20 = 0x4011a0), so if they steer anything -- an entry point, a copy
destination -- an attacker controls it without forging a signature.

Test: set each to a range of values and see whether (a) the load still succeeds, and (b) the
loaded TA still behaves the same when invoked. A behaviour change means the field is CONSUMED.
"""
import 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)
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)
def run(off,val,tag):
    blob=bytearray(base)
    if off is not None: struct.pack_into('<I',blob,off,val)
    blob=bytes(blob)
    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
    t0=time.time()
    try: r=submit(c); st=r.resp.status&0xFFFFFFFF; sid=r.resp.session_id
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
        sid=None
    dt=time.time()-t0
    out=''
    if st==0 and sid is not None and dt<5:
        buf=bytearray(0x4000); struct.pack_into('<I',buf,0,1)
        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,1
        t1=time.time()
        try: s=submit(c).resp.status&0xFFFFFFFF
        except Exception as e:
            try: s=int(str(e).strip().split()[-1])&0xFFFFFFFF
            except Exception: s=-1
        adev.gmc.flush_hdp()
        out='invoke1=%#-10x %.1fs'%(s,time.time()-t1)
    print('  %-30s load %#-11x %.1fs  %s'%(tag,st,dt,out), flush=True)
    return dt>5
run(None,0,'CONTROL unmodified')
for off in (0x1c,0x20):
    for val,vn in ((0,'0'),(0x400100,'image base'),(0xDEADBEEF,'0xdeadbeef'),(0xFFFFFFFF,'ones')):
        if run(off,val,'hdr[%#x]=%s'%(off,vn)): sys.exit(3)