Files
nvdriver/tools/vm/verify_rust_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

25 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""Reject unsupported Rust module code generation before starting a guest."""
from pathlib import Path
import subprocess,re,json,hashlib,shutil,os
R=Path(__file__).resolve().parents[2];O=R/'target/vm-module';obj=O/'rust_backing.o_binary';module=O/'nv_rs_edu_test.ko'
def run(*args):return subprocess.check_output(args,text=True,env=dict(os.environ,LC_ALL='C'))
assembly=run('objdump','-dr',str(obj));relocations=run('readelf','-Wr',str(module));sections=run('readelf','-SW',str(obj));fn='';frames={};pushes={}
for line in assembly.splitlines():
m=re.search(r'<([^>]+)>:',line)
if m:fn=m.group(1)
m=re.search(r'sub\s+\$0x([0-9a-f]+),%rsp',line)
if m:frames[fn]=frames.get(fn,0)+int(m.group(1),16)
if re.search(r'\bpush\s',line):pushes[fn]=pushes.get(fn,0)+8
# Conservative static accounting: reserve all fixed subtractions and pushes in
# a function. This is not a whole-call-chain stack proof; fixed prologues and
# no dynamic stack adjustment are separately checked for this guest image.
all_fns=set(frames)|set(pushes);usage={f:frames.get(f,0)+pushes.get(f,0) for f in all_fns};maximum=max(usage.values(),default=0)
dynamic=bool(re.search(r'\bsub\s+%[^,]+,%rsp',assembly))
checks={'no_unsupported_GOT_relocations':'GOT' not in relocations,'no_SIMD_x87_registers':not re.search(r'%(?:[xyz]mm\d+|mm\d+|st(?:\(|\b))',assembly),'no_Rust_unwind_sections':not re.search(r'\.eh_frame|\.gcc_except_table',sections),'bounded_fixed_function_stack':maximum<=1024,'no_dynamic_RSP_subtraction':not dynamic,'entrypoint_present':'vm_rust_backing' in run('nm','--defined-only',str(obj))}
rustc_env=os.environ.get('RUSTC');rustup_bin=shutil.which('rustup') or (str(Path.home()/'.cargo/bin/rustup') if (Path.home()/'.cargo/bin/rustup').exists() else None)
compiler=Path(rustc_env) if rustc_env else (Path(run(rustup_bin,'which','rustc').strip()) if rustup_bin else Path(shutil.which('rustc') or str(Path.home()/'.cargo/bin/rustc')))
flags=next(l.split('=',1)[1].strip('"') for l in (R/'tools/vm/build_guest_test.sh').read_text().splitlines() if l.startswith('RUSTFLAGS='))
d={'passed':all(checks.values()),'checks':checks,'largest_conservative_function_stack_bytes':maximum,'fixed_frame_bytes':frames,'conservative_frame_bytes':usage,'compiler':{'path':str(compiler),'sha256':hashlib.sha256(compiler.read_bytes()).hexdigest(),'version':run(str(compiler),'-vV')},'flags':flags,'module_sha256':hashlib.sha256(module.read_bytes()).hexdigest(),'rust_object_sha256':hashlib.sha256(obj.read_bytes()).hexdigest(),'scope':'Guest-only image structure/codegen checks. Runtime success is recorded separately. Per-function bound is not a full call-chain proof.'}
(R/'reports/vm-rust-object-validation.json').write_text(json.dumps(d,indent=2)+'\n');print('Guest Rust object validation:',checks,'maxframe=',maximum);assert d['passed']