Files
nvdriver/tools/vm/run_guest.py
Eric Hartford d5e0b9bb87 Wave 7A: Proactive P0 lock/UAF fixes and kernel build/compat hardening
Cluster 2 (RMAPI/UVM/Locking/Memory P0s):
- Convert runtime_start_users to atomic_t so wait_event() in
  nv_rs_rmapi_node_gpu_remove never calls mutex_lock while !TASK_RUNNING.
- Guard all 6 unlocked nv_rs_alloc_reg_find() call sites in rmapi_node.c with
  nv_rs_rmapi_alloc_lock.
- Run nv_rs_rmapi_rc_recover_fault while nv_rs_rmapi_lock is held from
  nv_rs_rmapi_node_trylock() in nv_rs_uvm_fault_work_fn (no blocking re-lock).
- Filter MANAGED_RANGES and PAGE_TREES by UvmFile owner in uvm_node.rs to
  prevent cross-process managed_unmap UAF; use flush_gpu_tlb_trylock() +
  TLB_FLUSH_PENDING deferral so .mmap/vm_ops->close never touch LIVE_ENGINES
  without nv_rs_rmapi_lock.
- Pin FD-exported memory backings in memory::Table::export_pins and check via
  shared backing_shares_resource() so export -> free original -> import never
  UAFs; release orphaned backings on FD owner close.
- Fix Backing::System map_protection(flags), deduplicate video_allocated_bytes,
  and unmap Backing::Register in drop_mappings.

Cluster 4 (Kernel Build Hygiene & Linux 5.15-7.3 Compat P0s):
- Factor shared kernel-open/compat_probes.mk for Kbuild + build_guest_test.sh
  (fixing Linux 5.15 char *date probe and Linux 7.0+ guest_build).
- Extend tools/prepare_kernel_archive.py to strip all kernel-exported
  compiler_builtins mem/string members (strlen/memcpy/memmove/memset/memcmp/bcmp)
  and wire it into tools/vm/build_guest_test.sh.
- Fix tools/check_kernel_module.py no_unhardened_sls_return to check per-ret
  int3 succession instead of file-wide has_int3.
- Set AR=llvm-ar / NM=llvm-nm under LLVM=1 in utils.mk, LC_ALL=C and non-rustup
  fallback in verify_rust_guest.py/check_native_codegen.py, and 1024 MiB guest RAM.

Validated: cargo fmt/clippy/test (958+2), test_gate_cases.py (24/24),
test_kernel_archive.py (5/5), NV_RS_KERNEL_IBT=0 & 1 make modules +
check_kernel_module.py (largest_stack=1000), QEMU EDU verify_rust_guest.py.
2026-12-02 01:34:46 +00:00

47 lines
2.9 KiB
Python

#!/usr/bin/env python3
from pathlib import Path
import subprocess,json,hashlib,re,argparse,os,pwd
from guest_results import validate_guest_results
parser=argparse.ArgumentParser();parser.add_argument("--iommu",action="store_true");options=parser.parse_args()
R=Path(__file__).resolve().parents[2];O=R/'target/vm';user=pwd.getpwuid(os.getuid()).pw_name;ram_mb=os.environ.get('NV_RS_VM_RAM_MB','1024');qemu=['qemu-system-x86_64'] if os.access('/dev/kvm',os.R_OK|os.W_OK) else ['sudo','-n','-u',user,'-g','kvm','qemu-system-x86_64'];args=qemu+['-machine','q35,accel=kvm','-cpu','host','-m',ram_mb,'-smp','2','-nodefaults','-display','none','-serial','stdio','-monitor','none','-no-reboot','-nic','none','-device','edu,addr=01.0,dma_mask=4294967295','-kernel',str(O/'vmlinuz'),'-initrd',str(O/'initramfs.cpio.gz'),'-append','console=ttyS0 rdinit=/init panic=-1 nokaslr ignore_loglevel']
if options.iommu:
args[args.index('-append')+1]+=' intel_iommu=on iommu.strict=1'
args+=['-device','intel-iommu,intremap=off,caching-mode=on']
suffix='-iommu' if options.iommu else ''
timeout=False
validation_errors=[]
try:
p=subprocess.run(args,capture_output=True,text=True,timeout=90);out=p.stdout+p.stderr;code=p.returncode
except subprocess.TimeoutExpired as e:
timeout=True;out=(e.stdout or b'').decode(errors='replace')+(e.stderr or b'').decode(errors='replace');code=None
except OSError as e:
out="";code=None
validation_errors.append(f"cannot launch EDU guest: {e}")
reports=R/'reports'
reports.mkdir(exist_ok=True)
(reports/f'vm-adapter{suffix}-serial.log').write_text(out)
results=[]
for match in re.findall(r'NV_RS_VM_PASS ([^\r\n]*)',out):
try:
result=json.loads(match)
except json.JSONDecodeError:
validation_errors.append("unreadable EDU guest result")
continue
if not isinstance(result,dict):
validation_errors.append("EDU guest result is not a JSON object")
continue
results.append(result)
validation_errors.extend(validate_guest_results(results,out,options.iommu))
input_hashes={}
for artifact in (O/'vmlinuz',O/'initramfs.cpio.gz',R/'target/vm-module/nv_rs_edu_test.ko'):
relative=str(artifact.relative_to(R))
try:
input_hashes[relative]=hashlib.sha256(artifact.read_bytes()).hexdigest()
except OSError as e:
validation_errors.append(f"cannot hash EDU guest input {relative}: {e}")
d={'passed':not timeout and code==0 and not validation_errors,'validation_errors':validation_errors,'exit_code':code,'timeout':timeout,'guest_results':results,'command':args,'scope':'Real Linux PCI/MMIO/coherent-DMA/IRQ adapters and Rust-backed unpublished GV100-format page tables and coherent instance blocks in QEMU EDU, not NVIDIA hardware or CUDA parity. Production adapter sources use guest-only EDU admission macro.','inputs':input_hashes,'specification':'https://www.qemu.org/docs/master/specs/edu.html'}
(reports/f'vm-adapter{suffix}-validation.json').write_text(json.dumps(d,indent=2)+'\n');print(json.dumps(d,indent=2));raise SystemExit(0 if d['passed'] else 1)