mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
182 lines
7.2 KiB
Python
182 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Read original ELF symbols/source only; never call or change the driver."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import platform
|
|
import struct
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DEFAULT_ANALYSIS_ROOT = Path('/home/geodd/nvidia-driver/analysis-580.178.04')
|
|
|
|
|
|
def find_rm_object(explicit: Path | None = None) -> Path:
|
|
if explicit is not None:
|
|
if explicit.is_file():
|
|
return explicit
|
|
raise SystemExit(f'Explicit --rm-object not found: {explicit}')
|
|
env_rm = os.environ.get('NV_RS_RM_OBJECT')
|
|
if env_rm:
|
|
p = Path(env_rm)
|
|
if p.is_file():
|
|
return p
|
|
raise SystemExit(f'NV_RS_RM_OBJECT not found: {p}')
|
|
candidates = []
|
|
if os.environ.get('NV_RS_KERNEL_SRC'):
|
|
base = Path(os.environ['NV_RS_KERNEL_SRC'])
|
|
candidates.extend([base / 'nvidia/nv-kernel.o_binary', base])
|
|
analysis = Path(os.environ.get('NV_RS_ANALYSIS_ROOT', str(DEFAULT_ANALYSIS_ROOT)))
|
|
candidates.append(analysis / 'extracted/kernel/nvidia/nv-kernel.o_binary')
|
|
candidates.extend([
|
|
Path('/usr/src/nvidia-580.178.04/nvidia/nv-kernel.o_binary'),
|
|
Path('/usr/src/nvidia-srv-580.178.04/nvidia/nv-kernel.o_binary'),
|
|
])
|
|
for candidate in candidates:
|
|
if candidate.is_file():
|
|
return candidate
|
|
raise SystemExit('Cannot find 580.178.04 nv-kernel.o_binary; set NV_RS_RM_OBJECT or pass --rm-object')
|
|
|
|
|
|
def find_nvidia_ko(explicit: Path | None = None) -> Path:
|
|
if explicit is not None:
|
|
if explicit.is_file():
|
|
return explicit
|
|
raise SystemExit(f'Explicit --nvidia-ko not found: {explicit}')
|
|
env_ko = os.environ.get('NV_RS_NVIDIA_KO')
|
|
if env_ko:
|
|
p = Path(env_ko)
|
|
if p.is_file():
|
|
return p
|
|
raise SystemExit(f'NV_RS_NVIDIA_KO not found: {p}')
|
|
release = platform.release()
|
|
candidates = [
|
|
Path(f'/lib/modules/{release}/updates/dkms/nvidia.ko'),
|
|
Path('/lib/modules/5.15.0-202-generic/updates/dkms/nvidia.ko'),
|
|
]
|
|
for candidate in candidates:
|
|
if candidate.is_file():
|
|
return candidate
|
|
raise SystemExit('Cannot find installed nvidia.ko; set NV_RS_NVIDIA_KO or pass --nvidia-ko')
|
|
|
|
|
|
def _parse_elf64(raw: bytes):
|
|
if len(raw) < 64 or raw[:4] != b'\x7fELF' or raw[4] != 2 or raw[5] != 1:
|
|
raise SystemExit('input is not a valid 64-bit little-endian ELF file')
|
|
e_shoff, = struct.unpack_from('<Q', raw, 40)
|
|
e_shentsize, e_shnum, e_shstrndx = struct.unpack_from('<HHH', raw, 58)
|
|
sh0 = struct.unpack_from('<IIQQQQIIQQ', raw, e_shoff)
|
|
if e_shnum == 0:
|
|
e_shnum = sh0[5]
|
|
if e_shstrndx == 0xFFFF:
|
|
e_shstrndx = sh0[6]
|
|
hdrs = [struct.unpack_from('<IIQQQQIIQQ', raw, e_shoff + i * e_shentsize) for i in range(e_shnum)]
|
|
str_off, str_size = hdrs[e_shstrndx][4], hdrs[e_shstrndx][5]
|
|
shstr = raw[str_off:str_off + str_size]
|
|
sections = []
|
|
by_name = {}
|
|
for idx, h in enumerate(hdrs):
|
|
nul = shstr.find(b'\x00', h[0])
|
|
name = shstr[h[0]:nul if nul != -1 else len(shstr)].decode('utf-8', errors='replace')
|
|
sections.append((name, h))
|
|
by_name[name] = idx
|
|
return sections, by_name
|
|
|
|
|
|
def _load_symbols(raw: bytes, sections, sym_idx: int):
|
|
_, h = sections[sym_idx]
|
|
entsize = h[9] or 24
|
|
str_h = sections[h[6]][1]
|
|
strtab = raw[str_h[4]:str_h[4] + str_h[5]]
|
|
ext = None
|
|
for _, sh in sections:
|
|
if sh[1] == 18 and sh[6] == sym_idx:
|
|
ext = struct.unpack_from(f'<{sh[5] // 4}I', raw, sh[4])
|
|
break
|
|
syms = []
|
|
for i in range(h[5] // entsize):
|
|
st_name, st_info, _st_other, st_shndx, st_value, st_size = struct.unpack_from('<IBBHQQ', raw, h[4] + i * entsize)
|
|
if st_shndx == 0xFFFF and ext is not None and i < len(ext):
|
|
st_shndx = ext[i]
|
|
nul = strtab.find(b'\x00', st_name)
|
|
name = strtab[st_name:nul if nul != -1 else len(strtab)].decode('utf-8', errors='replace')
|
|
syms.append((name, st_shndx, st_value, st_size, st_info & 0xF))
|
|
return syms
|
|
|
|
|
|
def symbol(path: Path, name: str) -> dict:
|
|
raw = path.read_bytes()
|
|
sections, by_name = _parse_elf64(raw)
|
|
syms = _load_symbols(raw, sections, by_name['.symtab'])
|
|
item = next((s for s in syms if s[0] == name), None)
|
|
if item is None:
|
|
raise SystemExit(f'symbol {name!r} not found in {path}')
|
|
sec_name, sec_h = sections[item[1]]
|
|
start, size = item[2], item[3]
|
|
code = raw[sec_h[4] + start:sec_h[4] + start + size]
|
|
relocations = []
|
|
for _rname, rh in sections:
|
|
sh_type = rh[1]
|
|
if sh_type not in (4, 9): # SHT_RELA (4), SHT_REL (9)
|
|
continue
|
|
if sections[rh[7]][0] != sec_name:
|
|
continue
|
|
rsyms = _load_symbols(raw, sections, rh[6])
|
|
entsize = rh[9] or (24 if sh_type == 4 else 16)
|
|
for i in range(rh[5] // entsize):
|
|
if sh_type == 4:
|
|
r_offset, r_info, r_addend = struct.unpack_from('<QQq', raw, rh[4] + i * entsize)
|
|
else:
|
|
r_offset, r_info = struct.unpack_from('<QQ', raw, rh[4] + i * entsize)
|
|
r_addend = None
|
|
if start <= r_offset < start + size:
|
|
relocations.append({
|
|
'offset': r_offset - start,
|
|
'target': rsyms[r_info >> 32][0],
|
|
'addend': r_addend,
|
|
})
|
|
return {
|
|
'path': str(path),
|
|
'file_sha256': hashlib.sha256(raw).hexdigest(),
|
|
'symbol': name,
|
|
'section': sec_name,
|
|
'offset': hex(start),
|
|
'size': size,
|
|
'bytes': code.hex(),
|
|
'relocations': relocations,
|
|
}
|
|
|
|
|
|
def main(argv=None) -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--rm-object', type=Path, default=None)
|
|
parser.add_argument('--nvidia-ko', type=Path, default=None)
|
|
args = parser.parse_args(argv)
|
|
|
|
wrapper = symbol(find_rm_object(args.rm_object), '_nv036619rm')
|
|
installed = symbol(find_nvidia_ko(args.nvidia_ko), 'os_is_vgx_hyper')
|
|
if wrapper['size'] != 9 or not wrapper['bytes'].startswith('f30f1efae9'):
|
|
raise SystemExit(f'unexpected _nv036619rm bytes: {wrapper["bytes"]}')
|
|
if not any(row['target'] == 'os_is_vgx_hyper' for row in wrapper['relocations']):
|
|
raise SystemExit('_nv036619rm does not relocate to os_is_vgx_hyper')
|
|
if installed['bytes'] != 'e8000000005531c04889e55de900000000':
|
|
raise SystemExit(f'unexpected os_is_vgx_hyper bytes: {installed["bytes"]}')
|
|
if [r['target'] for r in installed['relocations']] != ['__fentry__', '__x86_return_thunk']:
|
|
raise SystemExit(f'unexpected os_is_vgx_hyper relocations: {installed["relocations"]}')
|
|
|
|
output = {
|
|
'scope': 'Static ELF bytes and exact relocations; no loaded-driver execution',
|
|
'fresh_REA': 'reports/timer-timeout-vgx-rea/_nv036619rm.rea.json',
|
|
'original_RM_tail_forwarder': wrapper,
|
|
'installed_Linux_false_implementation': installed,
|
|
}
|
|
out = ROOT / 'reports/timer-timeout-vgx-binary-evidence.json'
|
|
out.parent.mkdir(parents=True, exist_ok=True)
|
|
out.write_text(json.dumps(output, indent=2) + '\n', encoding='utf-8')
|
|
print('Original nine-byte RM tail-forwarder and installed constant-false Linux implementation verified.')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|