mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
213 lines
8.7 KiB
Python
213 lines
8.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Run or verify original WPR CPU shadow copy (_nv016753rm) vectors in isolated private RAM."""
|
|
import argparse
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tools"))
|
|
from _falcon_acr_oracle_elf import ( # noqa: E402
|
|
GPU,
|
|
TEXT,
|
|
OracleUnicornHarness,
|
|
discover_rm_object,
|
|
load_relocated_rm_object,
|
|
write_or_check_json,
|
|
)
|
|
|
|
|
|
def main(argv=None) -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description="Run or verify WPR CPU shadow copy (_nv016753rm) oracle."
|
|
)
|
|
parser.add_argument("--rm-object", type=Path, default=None)
|
|
parser.add_argument("--check", action="store_true")
|
|
opt = parser.parse_args(argv)
|
|
|
|
loaded = load_relocated_rm_object(discover_rm_object(opt.rm_object))
|
|
acr = GPU + 0x810000
|
|
selector = GPU + 0x820000
|
|
regions = GPU + 0x830000
|
|
descriptors = GPU + 0x840000
|
|
request = GPU + 0x850000
|
|
output = GPU + 0x860000
|
|
cpu_base = GPU + 0x900000
|
|
|
|
identities = {descriptors + i * 0x100: i + 1 for i in range(5)}
|
|
bases = {d: identifier * 0x100000 for d, identifier in identities.items()}
|
|
cpus = {d: cpu_base + (identifier - 1) * 0x1000 for d, identifier in identities.items()}
|
|
cpu_ids = {value: identities[d] for d, value in cpus.items()}
|
|
state = {"events": [], "fail_map": 0}
|
|
req_info_addr = TEXT + loaded["defined"]["_nv016932rm"]
|
|
|
|
def on_pre_step(harness, emu, address):
|
|
if address == req_info_addr:
|
|
state["events"].append(["request_info"])
|
|
|
|
def on_custom(harness, emu, name):
|
|
x86 = harness.x86
|
|
if name == "physical_at":
|
|
descriptor = emu.reg_read(x86.UC_X86_REG_RDI)
|
|
offset = emu.reg_read(x86.UC_X86_REG_RDX)
|
|
identifier = identities[descriptor]
|
|
state["events"].append(["physical", identifier, offset])
|
|
harness.ret(bases[descriptor] + offset)
|
|
return True
|
|
if name == "cpu_map":
|
|
descriptor = emu.reg_read(x86.UC_X86_REG_RSI)
|
|
identifier = identities[descriptor]
|
|
state["events"].append(["map", identifier])
|
|
harness.ret(0 if state["fail_map"] == identifier else cpus[descriptor])
|
|
return True
|
|
if name == "cpu_unmap":
|
|
state["events"].append(
|
|
[
|
|
"unmap",
|
|
identities[emu.reg_read(x86.UC_X86_REG_RSI)],
|
|
emu.reg_read(x86.UC_X86_REG_RDX),
|
|
]
|
|
)
|
|
harness.ret()
|
|
return True
|
|
if name == "group":
|
|
state["events"].append(["group", emu.reg_read(x86.UC_X86_REG_RCX)])
|
|
harness.ret()
|
|
return True
|
|
if name == "free_leaf":
|
|
state["events"].append(["free"])
|
|
harness.ret()
|
|
return True
|
|
if name == "allocate_leaf":
|
|
req_bytes = emu.reg_read(x86.UC_X86_REG_RDI)
|
|
state["events"].append(["buffer", req_bytes])
|
|
idx = harness.allocations
|
|
harness.allocations += 1
|
|
if idx == harness.alloc_fail:
|
|
harness.ret()
|
|
return True
|
|
result = harness.alloc_next
|
|
harness.alloc_next += (req_bytes + 15) & ~15
|
|
harness.ret(result)
|
|
return True
|
|
if name == "os_mem_copy":
|
|
destination = emu.reg_read(x86.UC_X86_REG_RDI)
|
|
source = emu.reg_read(x86.UC_X86_REG_RSI)
|
|
nbytes = emu.reg_read(x86.UC_X86_REG_RDX)
|
|
state["events"].append(["copy", cpu_ids.get(destination, 0), cpu_ids.get(source, 0), nbytes])
|
|
emu.mem_write(destination, bytes(emu.mem_read(source, nbytes)))
|
|
harness.ret(destination)
|
|
return True
|
|
return False
|
|
|
|
harness = OracleUnicornHarness(loaded, custom_handler=on_custom, pre_step=on_pre_step)
|
|
for address in (acr, selector, regions, descriptors, request, output):
|
|
harness.uc.mem_map(address, 0x2000)
|
|
harness.uc.mem_map(cpu_base, 0x10000)
|
|
harness.uc.mem_write(selector + 0x10, struct.pack("<H", 35))
|
|
harness.uc.mem_write(selector + 0x30, struct.pack("<H", 2))
|
|
harness.call("_nv005947rm", [acr, selector])
|
|
harness.uc.mem_write(GPU + 0xCAC, struct.pack("<I", 0x20))
|
|
harness.uc.mem_write(acr + 0x394, struct.pack("<I", 2))
|
|
harness.uc.mem_write(acr + 0x3A0, struct.pack("<Q", regions))
|
|
harness.uc.mem_write(acr + 0x385, b"\0")
|
|
harness.uc.mem_write(acr + 0x386, b"\1")
|
|
|
|
for fn, hook_name in (
|
|
("_nv043721rm", "physical_at"),
|
|
("_nv043739rm", "cpu_map"),
|
|
("_nv043773rm", "cpu_unmap"),
|
|
("_nv031478rm", "group"),
|
|
("_nv046747rm", "diagnostic_leaf"),
|
|
):
|
|
harness.hooks[TEXT + harness.defined[fn]] = hook_name
|
|
|
|
cases = []
|
|
for count in (1, 2):
|
|
for request_kind in ("found", "missing"):
|
|
for fail_map in (0, 1, 2):
|
|
state["events"] = []
|
|
state["fail_map"] = fail_map
|
|
harness.allocations = 0
|
|
harness.alloc_fail = 99999
|
|
harness.alloc_next = GPU + 0x10000
|
|
harness.uc.mem_write(output, b"\xa5" * 0x78)
|
|
harness.uc.mem_write(regions, bytes(0x2000))
|
|
harness.uc.mem_write(acr + 0x398, struct.pack("<I", count))
|
|
harness.uc.mem_write(request, bytes(0x2000))
|
|
harness.uc.mem_write(
|
|
acr + 0x3A8, struct.pack("<Q", request if request_kind == "found" else 0)
|
|
)
|
|
harness.uc.mem_write(request + 8, struct.pack("<I", 1))
|
|
harness.uc.mem_write(request + 0x20, struct.pack("<I", 1))
|
|
harness.uc.mem_write(request + 0x28, struct.pack("<Q", descriptors + 0x400))
|
|
harness.uc.mem_write(request + 0x30, struct.pack("<I", 1))
|
|
bases[descriptors + 0x400] = bases[descriptors] + 256
|
|
for index in range(2):
|
|
d = descriptors + index * 0x200
|
|
shadow = d + 0x100
|
|
size = 512 + index * 256
|
|
harness.uc.mem_write(d + 0x30, struct.pack("<Q", size))
|
|
harness.uc.mem_write(shadow + 0x30, struct.pack("<Q", size))
|
|
off = (index + 1) * 0x38
|
|
harness.uc.mem_write(regions + off, struct.pack("<II", 0xF + index, 0xC + index))
|
|
harness.uc.mem_write(regions + off + 0x10, struct.pack("<I", 4 << index))
|
|
harness.uc.mem_write(regions + off + 0x18, struct.pack("<QQ", d, shadow))
|
|
harness.uc.mem_write(cpus[d], bytes((i * 13 + index) & 255 for i in range(size)))
|
|
harness.uc.mem_write(cpus[shadow], bytes(size))
|
|
status = harness.call("_nv016753rm", [GPU, acr, output])
|
|
expected_status = 0x1A if fail_map else 0
|
|
if status != expected_status:
|
|
raise SystemExit(
|
|
f"Expected status {expected_status:#x} for fail_map={fail_map}, got {status:#x}"
|
|
)
|
|
cases.append(
|
|
{
|
|
"region_count": count,
|
|
"request_kind": request_kind,
|
|
"fail_map": fail_map,
|
|
"status": status,
|
|
"events": list(state["events"]),
|
|
"output": bytes(harness.uc.mem_read(output, 0x78)).hex(),
|
|
"shadows": [
|
|
bytes(
|
|
harness.uc.mem_read(
|
|
cpus[descriptors + index * 0x200 + 0x100], 512 + index * 256
|
|
)
|
|
).hex()
|
|
for index in range(count)
|
|
],
|
|
}
|
|
)
|
|
|
|
report = {
|
|
"artifact_sha256": loaded["digest"],
|
|
"original_parent": "016753",
|
|
"original_request_helper": "016932",
|
|
"original_actual_noop": "016963",
|
|
"registry_precondition": "actual3850 configured viaRMAcrUseCeForShadowCopy=0",
|
|
"single_gpu_precondition": "actualCAC20 registered group path",
|
|
"scripted_leaves": [
|
|
"VRAM physical query",
|
|
"CPU map/unmap",
|
|
"group boundary",
|
|
"CPU allocation/free",
|
|
"CPU memcpy",
|
|
],
|
|
"cases": cases,
|
|
"physical_gpu_access": False,
|
|
"original_ready_or_authentication_proven": False,
|
|
}
|
|
write_or_check_json(
|
|
ROOT / "reports/gpu-wpr-cpu-shadow-oracle.json",
|
|
report,
|
|
opt.check,
|
|
"WPR CPU shadow oracle report",
|
|
)
|
|
print(len(cases), "actual016753 CPU-shadow parent cases")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|