aboutsummaryrefslogtreecommitdiffstats
path: root/hw/app_range.py
blob: f4610c36c1b91d63a5054732e2883006d778b3cf (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
#!/usr/bin/env python3
"""Is the TA IMAGE source range validated the same way as the command buffer?

Pass 87 showed cmd_buf is range-checked against the TMR. The image source (app_phy_addr,
app_len) is the other half of the LOAD_TA descriptor and has never been fuzzed. If it is not
checked, pointing the image at trusted memory would have the PSP read TMR content into its
staging/authentication path -- and an oversized app_len would have it read past our
allocation.

Cases (fresh submit each):
  A control: real image, real length          -> expect 0x0
  B real image, app_len far larger            -> reads past our allocation
  C app_phy_addr inside the TMR               -> is the image source range-checked?
  D app starts below TMR, length extends in   -> the straddle case
  E app_len = 0                               -> zero-length image
"""
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)
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
blob=open(glob.glob('/root/psp/contall/ta_42-0-0-6_1c874c3c*')[0],'rb').read()
tmr_mc=adev.paddr2mc(psp.tmr_paddr)
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()
print('TMR starts %#x ; our image at %#x (%d bytes)'%(tmr_mc,amc,len(blob)), flush=True)
def go(tag, app_mc, app_len):
    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=app_mc&0xFFFFFFFF,app_mc>>32; t.app_len=app_len
    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
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    dt=time.time()-t0
    print('  %-44s app=%#-12x len=%#-9x -> %#-10x %.1fs%s'%(tag,app_mc,app_len,st,dt,
          '  <<< HANG' if dt>5 else ''), flush=True)
go('A control: real image and length', amc, len(blob))
go('B real image, app_len 16x larger', amc, len(blob)*16)
go('C app_phy_addr INSIDE the TMR', tmr_mc+0x10000, len(blob))
go('D app below TMR, length extends into it', tmr_mc-0x2000, 0x8000)
go('E app_len = 0', amc, 0)