#!/usr/bin/env python3 """Misaligned descriptor addresses: does the PSP assume 4K (or any) alignment? Every buffer passed in this audit was page-aligned. Firmware that assumes alignment and then masks or rounds an address can read/write outside the caller's region. Untested until now. Cases, each a fresh LOAD_TA (control first): aligned -> expect 0x0 cmd_buf +1 / +2 / +4 / +0x40 / +0x800 (byte, half, word, cacheline, sub-page) app_phy_addr +1 / +4 / +0x40 A clean rejection or clean success is benign; a stall or a write outside the region is not. """ 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) PAT=0x33333333 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(tag, cmd_off=0, app_off=0): ap_,amc=alloc(0x80000); cp,cmc=alloc(0x20000) n=(len(base)+3)&~3 adev.vram.view(ap_,n)[:]=base+b'\x00'*(n-len(base)) adev.vram.view(cp,0x20000)[:]=bytes(struct.pack('>32; t.app_len=len(base) t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=b&0xFFFFFFFF,b>>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 extra='' if st==0 and sid is not None and dt<5: buf=bytearray(0x4000); struct.pack_into('5 run('CONTROL aligned') for off in (1,2,4,0x40,0x800): if run('cmd_buf +%#x'%off, cmd_off=off): sys.exit(3) for off in (1,4,0x40): if run('app_addr +%#x'%off, app_off=off): sys.exit(3)