data-gen-bpu/scripts/jail_setup.py

102 lines
4.0 KiB
Python

"""Mount construction runs before untrusted code, then drops every capability."""
import os
import sys
import subprocess
import tempfile
from pathlib import Path
import yaml
def mount(*args):
subprocess.run(['mount', '-n', *map(str, args)], check=True)
def main():
bench, rd = map(Path, sys.argv[1:3])
mode = sys.argv[3]
command = sys.argv[5:]
if Path(command[0]).name == 'pi' and '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((bench / 'config.yaml').read_text())
jd = rd.parent.parent
run = jd.parent
work = Path(os.path.expanduser(cfg['paths']['work_root'])) if cfg['paths'].get('work_root') else None
stash = Path(tempfile.mkdtemp(prefix='bench-jail-'))
saved = []
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', 'data', 'cache'):
save(bench / name, bench / name)
real = (bench / name).resolve()
if real != bench / name:
save(real)
save(bench.parent / '.horizon')
save(jd / 'job.json')
save(jd / 'data')
if mode == 'repair':
save(jd / 'oracle')
save(rd)
save(rd / 'workspace', writable=True)
save(rd / 'handles', writable=True)
if work:
own = work / run.name / jd.name / ('r' + rd.name)
# Caller prepares production work directories; missing probe work stays empty.
save(own)
state_real = (bench / "state").resolve()
hidden = [bench, state_real]
if work:
hidden.append(work.resolve())
# Save paths can live under state in the isolated smoke fixture.
for p in dict.fromkeys(hidden):
if p.is_dir():
mount('-t', 'tmpfs', '-o', 'mode=755', 'tmpfs', p)
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)
# 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 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_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()