#!/usr/bin/env python3 """The MALLOC-path hijack: a second, never-analysed entry state. gcp_call_once_init (0x40291c) has exactly TWO call sites (verified by BL xref): 0x00405612 inside gcp_tlApiFree (0x4055f4) <- called from TA_CloseSessionEntryPoint: UNLOAD 0x00405738 inside gcp_tlApiMalloc (0x4056f0) <- called from 0x405442, the session allocator: LOAD Every result so far (Add.95 onward) analysed only the UNLOAD/free path, because hijack_state.py armed its hook AFTER LOAD_TA and therefore missed the malloc occurrence. The malloc path has a different frame, a different request block and different register contents, so its gadget set is a DIFFERENT problem -- worth measuring before concluding anything about it. Captured at the start of basic block 0x402930 (`mov r0,r1 ; movs r1,#0 ; movs r2,#0 ; movs r3,#0 ; bx ip`), so the state at the `bx ip` itself is: r0 = r1 here, r1 = r2 = r3 = 0, everything else as captured. """ import sys, os, struct, importlib.util, glob sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) sys.path.insert(0, os.path.dirname(__file__)) from unicorn.arm_const import * H=os.path.dirname(os.path.abspath(__file__)) _h=importlib.util.spec_from_file_location("hmw", os.path.join(H,"heap_meta_who.py")) HM=importlib.util.module_from_spec(_h); _h.loader.exec_module(HM) from pspemu import armmmu REGS=[("r0",UC_ARM_REG_R0),("r1",UC_ARM_REG_R1),("r2",UC_ARM_REG_R2),("r3",UC_ARM_REG_R3), ("r4",UC_ARM_REG_R4),("r5",UC_ARM_REG_R5),("r6",UC_ARM_REG_R6),("r7",UC_ARM_REG_R7), ("r8",UC_ARM_REG_R8),("r9",UC_ARM_REG_R9),("r10",UC_ARM_REG_R10),("r11",UC_ARM_REG_R11), ("ip",UC_ARM_REG_R12),("sp",UC_ARM_REG_SP),("lr",UC_ARM_REG_LR),("cpsr",UC_ARM_REG_CPSR)] caps=[]; armed=[False] def main(): cont=glob.glob(os.path.join(HM.CONTAINERS,"ta_22-0-0-77_*"))[0] # On the FIRST (malloc) call the cache is zero, so the bne at 0x402928 is NOT taken and control # falls through 0x40292e (`str r0,[r2]`, which caches the resolved entry) straight into 0x402930 # -- all one basic block. So 0x402930 is not a block boundary on that path and a BLOCK hook never # fires. Install a CODE hook on 0x402938 instead, as soon as any TA code runs. from unicorn import UC_HOOK_CODE def on_code(uc,addr,size,user): if addr!=0x402938: return st={n:uc.reg_read(r) for n,r in REGS} try: frame=bytes(armmmu.read_va(uc,st["sp"],0x90)) except Exception: frame=b"" caps.append((st,frame)) def on_block(uc,addr,size,user): if armed[0] or not (0x400100 <= addr < 0x417000): return armed[0]=True uc.hook_add(UC_HOOK_CODE,on_code,begin=0x402938,end=0x402938); uc.ctl_flush_tb() env=HM.setup_hooked(cont,on_block,verbose=True) # block hook installed BEFORE LOAD_TA if env is None: print("load failed"); return 1 print("\ncall_once_init reached %d time(s) during LOAD_TA"%len(caps)) for n,(st,frame) in enumerate(caps): print("\n=== occurrence #%d (the MALLOC path) ==="%(n+1)) eff=dict(st); eff["r0"]=st["r1"]; eff["r1"]=eff["r2"]=eff["r3"]=0 for k,(name,_) in enumerate(REGS): print(" %-5s = %#010x"%(name,eff[name]), end="\n" if k%2 else "") cp=st["cpsr"] print(" flags N=%d Z=%d C=%d V=%d mode=%#x"%((cp>>31)&1,(cp>>30)&1,(cp>>29)&1,(cp>>28)&1,cp&0x1f)) print(" pointers into writable memory:") for name in ("r0","r4","r5","r6","r7","sp","lr"): v=eff[name] for lo,hi,rn,floor in ((0x407000,0x417000,".bss",0x40707f),(0x418000,0x41c000,"stack",0x41807f), (0x41d000,0x421000,"heap",0x41d07f),(0x422000,0x423000,"procpage",0x42207f)): if lo<=v=floor else "below floor")) if frame: print(" frame at sp:") for off in range(0,0x90,16): print(" +%02x: %s"%(off," ".join("%08x"%struct.unpack_from("