- terminal_jail:系统目录改递归绑定(S100P 沙箱容器在 /usr 下有只读的主机挂载,用户命名空间里非递归绑定被拒)、 加 /host_lib 和只读 /sys(BPU 运行库读核心类型)、透传板上 LD_LIBRARY_PATH(libdnn/libhbucp 在 /usr/hobot/lib) - collector 的请求检查只认 development 目标且不能热换,所以 --board 在命令前加标记,由新进程里的 terminal.run 转成板上请求; 只对 v18+、nash-m 开放;上传当前目录(本地文件 + 只在开发环境里的大文件,如 hbm),一次最多 30 分钟、4 GiB;跑完删副本 - S600 板卡容器的 seccomp 禁止所有命名空间,暂不开放 - 实测:rdk-sandbox 上隔离环境里 /etc/shadow、/userdata、/root 不可见、无网络,mobilenet.v2 0.79 ms,回执确认清理 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
3.7 KiB
Python
78 lines
3.7 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)
|
|
# Recursive: inside a user namespace a plain bind of a tree with locked submounts is EINVAL
|
|
# (10-16: the S100P sandbox container has read-only host mounts at /usr/hobot, /usr/lib/hobot_spdev).
|
|
mount('--rbind', source.resolve(), target)
|
|
if not writable:
|
|
mount('-o', 'remount,bind,ro,nosuid', target)
|
|
# No host home, /root, /etc credentials, benchmark source or label packages.
|
|
# /host_lib: the board container's host libraries; /sys (read-only): the BPU runtime reads the core type there
|
|
for name in ('/usr', '/bin', '/sbin', '/lib', '/lib64', '/host_lib', '/sys'):
|
|
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',
|
|
# The host's own library path (board: /usr/hobot/lib holds libdnn/libhbucp that hbm_runtime loads).
|
|
**({'LD_LIBRARY_PATH': os.environ['LD_LIBRARY_PATH']} if os.environ.get('LD_LIBRARY_PATH') else {})}
|
|
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()
|