73 lines
3.2 KiB
Python
73 lines
3.2 KiB
Python
"""Trusted terminal mount constructor. No shell parsing and no unsafe fallback.
|
|
|
|
Invoked in a fresh user/mount/PID namespace; formal/board execution additionally
|
|
uses a network namespace. Only explicit runtime inputs and the task are visible.
|
|
"""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import resource
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def main():
|
|
config = json.loads(Path(sys.argv[1]).read_text())
|
|
root = Path(config['jailroot'])
|
|
def mount(*args):
|
|
subprocess.run(['/bin/mount', '-n', *map(str, args)], check=True, close_fds=True)
|
|
mount('--make-rprivate', '/')
|
|
mount('-t', 'tmpfs', '-o', 'mode=755,size=32m', 'tmpfs', root)
|
|
def bind(source, destination=None, writable=False):
|
|
source = Path(source)
|
|
if not source.exists():
|
|
return
|
|
target = root / str(destination or source).lstrip('/')
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
if source.is_dir():
|
|
target.mkdir(exist_ok=True)
|
|
else:
|
|
target.touch(exist_ok=True)
|
|
mount('--bind', source.resolve(), target)
|
|
if not writable:
|
|
mount('-o', 'remount,bind,ro,nosuid', target)
|
|
# No host home, /root, /etc credentials, benchmark source or label packages.
|
|
for name in ('/usr', '/bin', '/sbin', '/lib', '/lib64'):
|
|
bind(name)
|
|
for name in ('/etc/ld.so.cache', '/etc/resolv.conf', '/etc/hosts', '/etc/ssl/certs'):
|
|
bind(name)
|
|
for name in config.get('runtimes', []):
|
|
bind(name)
|
|
bind(config['workspace'], '/workspace', not config['frozen'])
|
|
bind(config['output'], '/out', True)
|
|
bind(config['input'], '/input')
|
|
for name in ('tmp', 'dev', 'proc', 'home'):
|
|
(root / name).mkdir(exist_ok=True)
|
|
mount('-t', 'tmpfs', '-o', 'mode=1777,size=2g,nosuid,nodev', 'tmpfs', root / 'tmp')
|
|
mount('-t', 'tmpfs', '-o', 'mode=755,size=1m,nosuid', 'tmpfs', root / 'dev')
|
|
for name in ('null', 'zero', 'random', 'urandom'):
|
|
bind('/dev/' + name, writable=True)
|
|
# Device admission is platform-selected, never a candidate parameter.
|
|
for name in config.get('devices', []):
|
|
bind(name, writable=True)
|
|
mount('-t', 'proc', '-o', 'nosuid,nodev,noexec', 'proc', root / 'proc')
|
|
resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
|
|
resource.setrlimit(resource.RLIMIT_NOFILE, (512, 512))
|
|
resource.setrlimit(resource.RLIMIT_FSIZE, (8 * 1024**3, 8 * 1024**3))
|
|
resource.setrlimit(resource.RLIMIT_CPU, (config['timeout'], config['timeout'] + 1))
|
|
os.chroot(root)
|
|
os.chdir('/workspace/' + config['cwd'])
|
|
# Drop the board lock/transport descriptors before candidate code can see them.
|
|
os.closerange(3, 65536)
|
|
env = {'PATH': config.get('path', '/usr/bin:/bin'), 'HOME': '/tmp/home',
|
|
'TMPDIR': '/tmp', 'LANG': 'C.UTF-8', 'CUDA_VISIBLE_DEVICES': '',
|
|
'PYTHONDONTWRITEBYTECODE': '1', 'PIP_DISABLE_PIP_VERSION_CHECK': '1',
|
|
'OMP_NUM_THREADS': '1', 'OPENBLAS_NUM_THREADS': '1'}
|
|
Path('/tmp/home').mkdir()
|
|
os.execve('/usr/bin/setpriv', ['setpriv', '--bounding-set=-all', '--inh-caps=-all',
|
|
'--ambient-caps=-all', '--no-new-privs', '--', '/bin/bash', '-c', config['command']], env)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|