"""Linux Landlock check: run an installed wheel with source checkouts inaccessible. This is a validation helper, not a sandbox for a tested model. Install the wheel with pip --target DIR, then invoke with the simulation Python and --wheel-root DIR. """ import argparse import ctypes import json import os from pathlib import Path import sys def restrict_filesystem(allowed): libc = ctypes.CDLL(None, use_errno=True) abi = libc.syscall(444, 0, 0, 1) if abi < 1: raise OSError(ctypes.get_errno(), 'Landlock unavailable') access = (1 << 13) - 1 if abi >= 2: access |= 1 << 13 if abi >= 3: access |= 1 << 14 class Ruleset(ctypes.Structure): _fields_ = [('handled_access_fs', ctypes.c_uint64)] class PathRule(ctypes.Structure): _pack_ = 1 _fields_ = [('allowed_access', ctypes.c_uint64), ('parent_fd', ctypes.c_int)] rules = Ruleset(access) fd = libc.syscall(444, ctypes.byref(rules), ctypes.sizeof(rules), 0) if fd < 0: raise OSError(ctypes.get_errno(), 'Landlock create failed') try: for directory in allowed: if not Path(directory).exists(): continue parent = os.open(str(directory), os.O_PATH | os.O_CLOEXEC) try: rule = PathRule(access, parent) if libc.syscall(445, fd, 1, ctypes.byref(rule), 0) != 0: raise OSError(ctypes.get_errno(), 'Landlock path rule failed') finally: os.close(parent) if libc.prctl(38, 1, 0, 0, 0) != 0 or libc.syscall(446, fd, 0) != 0: raise OSError(ctypes.get_errno(), 'Landlock restrict failed') finally: os.close(fd) return abi def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--wheel-root', type=Path, required=True) parser.add_argument('--work-dir', type=Path, required=True) parser.add_argument('--forbid', type=Path, action='append', default=[]) parser.add_argument('--command', choices=['smoke', 'replay-experts', 'export-references'], default='smoke') parser.add_argument('--ids', default='all') args = parser.parse_args() wheel = args.wheel_root.resolve() work = args.work_dir.resolve() work.mkdir(parents=True, exist_ok=True) forbidden = [p.resolve() for p in args.forbid] # Resolve test files before the OS denies traversal of their source roots. probes = [next(p.rglob('*.py')) for p in forbidden] allowed = ['/usr', '/lib', '/lib64', '/etc', '/proc', '/dev', '/sys', sys.prefix, sys.base_prefix, wheel, work] os.chdir(work) os.environ.update(XDG_CACHE_HOME=str(work / 'cache'), TMPDIR=str(work), NUMBA_CACHE_DIR=str(work / 'numba'), PYTHONDONTWRITEBYTECODE='1', OPENBLAS_NUM_THREADS='1', OMP_NUM_THREADS='1') os.environ.pop('LIBERO_CONFIG_PATH', None) sys.path.insert(0, str(wheel)) abi = restrict_filesystem(allowed) for probe in probes: try: probe.read_bytes() except PermissionError: continue raise RuntimeError(f'source is still readable: {probe}') import geomotion from geomotion import cli if not Path(geomotion.__file__).resolve().is_relative_to(wheel): raise RuntimeError('did not import the installed wheel') report = dict(landlock_abi=abi, forbidden_sources=[str(p) for p in forbidden], installed_module=str(geomotion.__file__)) (work / 'isolation.json').write_text(json.dumps(report, indent=2) + '\n') result = cli.main(['doctor']) if result: return result destination = work / ('references' if args.command == 'export-references' else args.command + '.json') return cli.main([args.command, '--ids', args.ids, '--output', str(destination)]) if __name__ == '__main__': raise SystemExit(main())