Files
nvdriver/tools/gpu_sec2_original_unpack.py

92 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Verify original SEC2 packed firmware decompression against decoded artifacts in private RAM."""
import argparse
import hashlib
import json
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tools"))
from _acr_elf_harness import ( # noqa: E402
GPU,
RO,
AcrUnicornHarness,
discover_rm_object,
load_relocated_rm_object,
resolve_repo_path,
write_or_check_json,
)
def main(argv=None) -> int:
parser = argparse.ArgumentParser(
description="Run or verify SEC2 original packed firmware unpack 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))
harness = AcrUnicornHarness(
loaded, heap_size=0x800000, hook_free_and_ctx=True, step_limit=100000000
)
out_base = 0x30000000
harness.uc.mem_map(out_base, 0x400000)
records = json.loads((ROOT / "reports/gpu-sec2-packed-inventory.json").read_text())["records"]
results = []
for row in records:
harness.allocations = 0
harness.alloc_next = GPU + 0x10000
harness.alloc_fail = 999999
harness.visited.clear()
descriptor = RO + int(row["descriptor"], 16) - 0x0111D660
expected = resolve_repo_path(row["decoded_file"]).read_bytes()
harness.uc.mem_write(out_base, bytes([0xA5]) * (len(expected) + 256))
status = harness.call("_nv017881rm", [descriptor, out_base, len(expected)])
actual = bytes(harness.uc.mem_read(out_base, len(expected)))
if status != 0:
raise SystemExit(f"SEC2 unpack failed for {row['bundle']}/{row['tag']}: status={status:#x}")
if actual != expected:
raise SystemExit(f"SEC2 unpacked bytes differ for {row['bundle']}/{row['tag']}")
if bytes(harness.uc.mem_read(out_base + len(expected), 256)) != bytes([0xA5]) * 256:
raise SystemExit(f"SEC2 unpack overwrote guard canary for {row['bundle']}/{row['tag']}")
results.append(
{
"bundle": row["bundle"],
"tag": row["tag"],
"status": status,
"decoded_sha256": hashlib.sha256(actual).hexdigest(),
"allocations": harness.allocations,
"original_functions_entered": sorted(harness.visited),
"external_process_precondition": "049765 executed in valid process context; isolated stub",
"external_cpu_services": [
"052687 allocation",
"052701 free",
"os_mem_set",
"os_mem_copy",
"diagnostics",
],
}
)
print(row["bundle"], row["tag"], len(expected), harness.allocations, flush=True)
report = {
"artifact_sha256": loaded["digest"],
"cases": results,
"physical_gpu_access": False,
"signature_authentication": False,
}
write_or_check_json(
ROOT / "reports/gpu-sec2-original-unpack-oracle.json",
report,
opt.check,
"SEC2 original unpack oracle report",
)
return 0
if __name__ == "__main__":
raise SystemExit(main())