91 lines
3.2 KiB
Python
91 lines
3.2 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', *map(str, args)], check=True)
|
|
|
|
|
|
def main():
|
|
bench, rd = map(Path, sys.argv[1:3])
|
|
mode = sys.argv[3]
|
|
command = sys.argv[5:]
|
|
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):
|
|
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, Path(dest or src), 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)
|
|
|
|
hidden = [bench, (bench / 'state').resolve()]
|
|
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)
|
|
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():
|
|
p = line.split()[4].replace('\\040', ' ')
|
|
if p not in ('/proc', '/dev') and not p.startswith(('/proc/', '/dev/')):
|
|
mounts.append(p)
|
|
for p in sorted(set(mounts), key=len, reverse=True):
|
|
mount('-o', 'remount,bind,ro', p)
|
|
for src, dst, writable, directory in saved:
|
|
if writable:
|
|
mount('-o', 'remount,bind,rw', 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()
|