"""Mount construction runs before untrusted code, then drops every capability.""" import os import json import sys import subprocess import tempfile import shutil from pathlib import Path import yaml # This script is also invoked directly by agent_jail.sh, before any mounts. sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from harness.runners import isolation_policy def mount(*args): subprocess.run(['mount', '-n', *map(str, args)], check=True) def visible_mount(mount_id, point): """Match the path's visible mount, excluding mountinfo entries hidden by overlays.""" try: fd = os.open(point, os.O_PATH | os.O_CLOEXEC) except (FileNotFoundError, NotADirectoryError, PermissionError): return False try: ids = [line.split(':', 1)[1].strip() for line in Path(f'/proc/self/fdinfo/{fd}').read_text().splitlines() if line.startswith('mnt_id:')] if len(ids) != 1: raise RuntimeError('visible mount identity unavailable') return ids[0] == str(mount_id) finally: os.close(fd) def runner_policy(rd, env): """Read the trusted outer-worker snapshot, never Agent-writable workspace settings.""" snapshot = rd / 'rollout.json' settings = json.loads(snapshot.read_text()) if snapshot.exists() else {} recorded = settings.get('runner', 'pi') if snapshot.exists() else None if snapshot.exists() and (not isinstance(recorded, str) or not recorded): raise ValueError('invalid runner identity in rollout snapshot') requested = env.get('BENCH_RUNNER') if recorded and requested and recorded != requested: raise ValueError('runner identity mismatch between rollout and environment') name = recorded or requested or 'pi' # Legacy Pi smoke fixtures have no snapshot. policy = isolation_policy(name) if 'jail_policy' in settings: if settings['jail_policy'] != 'private-home': raise ValueError('invalid recorded jail policy') policy = 'private-home' return name, policy def private_home_inputs(cfg, command, configured_path, protected): """Named read-only runtime inputs; do not restore HOME or a hidden data root.""" protected = [root.resolve() for root in protected] inputs = [configured_path(value) for value in (cfg['paths'].get('venv') or {}).values()] node = cfg['paths'].get('node_bin') if node and '/' in node: executable = configured_path(node).resolve() # A configured Node distribution needs its lib/node_modules alongside bin. inputs.append(executable.parent.parent if executable.parent.name == 'bin' else executable) # Use the executable actually selected by the adapter, including PATH lookup. binary = str(configured_path(command)) if '/' in command else shutil.which(command) if binary: inputs.append(Path(binary)) for source in inputs: source = source.resolve() if any(source == root or source in root.parents for root in protected): raise ValueError(f'runner runtime input would expose a hidden root: {source}') return list(dict.fromkeys(inputs)) def main(): bench, rd = map(Path, sys.argv[1:3]) mode = sys.argv[3] command = sys.argv[5:] _, policy = runner_policy(rd, os.environ) if 'guard-smoke' not in rd.parts and os.environ.get('BENCH_OUTER_WORKER') != '1': raise RuntimeError('v5 jail requires the new outer-worker rollout driver; old collect must drain naturally') cfg = yaml.safe_load(Path(os.environ.get('BENCH_CONFIG') or bench / 'config.yaml').read_text()) host_home = Path(os.environ.get('BENCH_HOST_HOME') or os.path.expanduser('~')).resolve() def configured_path(value): value = str(host_home / value[2:]) if value.startswith('~/') else value return Path(os.path.expandvars(value)) jd = rd.parent.parent run = jd.parent work = configured_path(cfg['paths']['work_root']) if cfg['paths'].get('work_root') else None legacy_state_real = (bench / 'state').resolve() state_real = Path(os.environ.get('BENCH_STATE_DIR') or bench / 'state').expanduser().resolve() runtime_inputs = [] if policy == 'private-home': if host_home in (Path('/'), bench, rd, rd / 'workspace'): raise RuntimeError('invalid host HOME for runner isolation') protected = [host_home, bench.resolve(), state_real, legacy_state_real] if work: protected.append(work.resolve()) runtime_inputs = private_home_inputs(cfg, command[0], configured_path, protected) # TMPDIR may be inside HOME, the checkout, or state, all masked below. # Keep construction aliases in /tmp until the final private /tmp mount. stash = Path(tempfile.mkdtemp(prefix='bench-jail-', dir='/tmp')) saved = [] metadata = json.loads((rd / 'rollout.json').read_text()) if (rd / 'rollout.json').exists() else {} natural = metadata.get('natural_agent_version') == 'natural-agent-v1' def save(src, dest=None, writable=False): destination = Path(dest) if dest is not None else Path(src) src = Path(src).resolve() if not src.exists(): return target = stash / str(len(saved)) if src.is_dir(): target.mkdir() else: target.touch() mount('--bind', src, target) saved.append((target, destination, writable, src.is_dir())) # Preserve only named inputs; never enumerate runs or work_root. for name in (('bin', 'agent') if natural else ('bin', 'agent', 'data', 'cache')): save(bench / name, bench / name) real = (bench / name).resolve() if real != bench / name: save(real) save(bench / 'harness/adapters') save(bench.parent / '.horizon') save(jd / 'job.json') if not natural: save(jd / 'data') # Natural task materials are already covered by the read-only rd bind. if mode == 'repair': ref = json.loads((jd / 'job.json').read_text()).get('oracle_ref') source = bench / 'runs' / ref['run_id'] / ref['job_id'] / 'oracle' if ref else jd / 'oracle' save(source, jd / 'oracle') # historical evidence, same read-only sandbox path save(rd) save(rd / 'workspace', writable=True) save(rd / 'handles', writable=True) for source in runtime_inputs: save(source) if work: own = work / run.name / jd.name / ('r' + rd.name) # Caller prepares production work directories; missing probe work stays empty. save(own) hidden = [bench, state_real, legacy_state_real] if work: hidden.append(work.resolve()) if policy == 'private-home': hidden.append(host_home) if natural: hidden.extend(Path(p) for p in ('/mnt', '/media', '/data', '/root')) # Save paths can live under state in the isolated smoke fixture. # Mask parents first: masking HOME last leaves covered child mounts in # mountinfo that cannot subsequently be remounted through their old paths. for p in sorted(dict.fromkeys(hidden), key=lambda item: len(item.parts)): if p.is_dir(): mount('-t', 'tmpfs', '-o', 'mode=755', 'tmpfs', p) bench.mkdir(parents=True, exist_ok=True) if state_real != bench / "state": (bench / "state").symlink_to(state_real, target_is_directory=True) for src, dst, writable, directory in saved: dst.parent.mkdir(parents=True, exist_ok=True) if directory: dst.mkdir(exist_ok=True) elif not dst.exists(): dst.touch() mount('--bind', src, dst) if policy == 'private-home': # Exploratory shell code gets no direct accelerator device access. for device in sorted(set(Path('/dev').glob('nvidia*')) | {Path('/dev/dri'), Path('/dev/kfd')}): if device.is_dir(): mount('-t', 'tmpfs', '-o', 'mode=755', 'tmpfs', device) elif device.exists(): mount('--bind', '/dev/null', device) # Read-only ALL visible mounts (including aliases and mount construction stash). mounts = {} for line in Path('/proc/self/mountinfo').read_text().splitlines(): fields = line.split() p = fields[4].replace('\\040', ' ') if 'ro' in fields[5].split(',') or not os.access(p, os.R_OK): continue if not visible_mount(fields[0], p): continue if p not in ('/proc', '/dev') and not p.startswith('/proc/') and (not p.startswith('/dev/') or p in ('/dev/shm', '/dev/mqueue')): mounts[p] = fields[5] for p in sorted(set(mounts), key=len, reverse=True): flags = ','.join(x for x in mounts[p].split(',') if x not in ('rw', 'ro')) mount('-o', 'remount,bind,ro,' + flags, p) for src, dst, writable, directory in saved: if writable: flags = ','.join(x for x in mounts.get(str(dst), '').split(',') if x not in ('rw', 'ro', '')) mount('-o', 'remount,bind,rw,' + flags, dst) # Hide temporary bind aliases and host /tmp; the agent gets its own scratch. mount('-t', 'tmpfs', '-o', 'mode=1777', 'tmpfs', '/tmp') os.environ.update(BENCH_JAILED='1', BENCH_JAIL_POLICY=policy, BENCH_ROLLOUT_DIR=str(rd), BENCH_ROOT=str(bench)) os.chdir(rd / 'workspace') os.execvp('setpriv', ['setpriv', '--bounding-set=-all', '--inh-caps=-all', '--ambient-caps=-all', '--no-new-privs', '--', *command]) if __name__ == '__main__': main()