#!/usr/bin/env python3 """Do any TA commands leak PSP-internal memory back to the host shared buffer? Every hardware test so far looked at statuses. This looks at CONTENT: prefill the shared buffer with a known pattern, invoke, then examine every byte the TA changed. Flag dwords that look like PSP-internal addresses -- SYS_DRV code/data (0xE0xxxxxx-0xE8xxxxxx), TA space (0x004xxxxx), or the PSP SRAM window -- since those are pointers that should never reach host memory, and they are exactly what a disclosure primitive returns. Controls: * the prefill pattern makes "changed" unambiguous (no relying on zeros); * a dword equal to the command id or to a value the host itself wrote is ignored. """ import glob, os, struct, sys, time from tinygrad import Device import tinygrad.runtime.autogen.am.am as am adev = Device["AMD"].iface.dev_impl; psp = adev.psp PAT = 0xCCCCCCCC 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(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=0x8000 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 looks_internal(v): return (0xE0000000 <= v < 0xE8000000) or (0x00400000 <= v < 0x00420000) or \ (0x000E0000 <= v < 0x00100000) or (0x3F000000 <= v < 0x40000000) def probe(sid, shm, cid, size=0x800): buf = bytearray(struct.pack('1 else None SKIP={'AUC':{6},'FP':{7}} # known wedges 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 found=[] for cid in range(0,12): if cid in SKIP.get(name,set()): continue s,ch,hits = probe(sid, shm, cid) if hits: found.append((cid,s,ch,hits[:6])) print('%-6s %s'%(name, ('LEAK-SHAPED: %s'%found[:3]) if found else 'no internal-looking dwords returned'))