116 lines
4.8 KiB
Python
116 lines
4.8 KiB
Python
"""Trusted mount constructor; invoked only by recipe_runtime under unshare."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import resource
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def mount(*args):
|
|
subprocess.run(["/bin/mount", "-n", *map(str, args)], check=True,
|
|
stdin=subprocess.DEVNULL, close_fds=True)
|
|
|
|
|
|
def interpreter_links(root: Path, executable: str) -> Path:
|
|
"""Keep intermediate venv Python aliases without mounting a host bin folder.
|
|
|
|
Mounting only the final binary misses chains such as
|
|
venv/bin/python -> /usr/bin/python3 -> python3.10.
|
|
"""
|
|
source = Path(os.path.abspath(executable))
|
|
seen = set()
|
|
while source.is_symlink():
|
|
if source in seen or len(seen) >= 40:
|
|
raise ValueError("cyclic interpreter symlink chain")
|
|
seen.add(source)
|
|
link = os.readlink(source)
|
|
target = root / str(source).lstrip("/")
|
|
if not os.path.lexists(target):
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.symlink_to(link)
|
|
source = Path(os.path.abspath(link if os.path.isabs(link) else source.parent / link))
|
|
return source
|
|
|
|
|
|
def main():
|
|
config = json.loads(Path(sys.argv[1]).read_text())
|
|
root = Path(config["jailroot"])
|
|
runtime = config["runtime"]
|
|
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
|
|
destination = Path(destination or source)
|
|
target = root / str(destination).lstrip("/")
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
if source.is_dir():
|
|
target.mkdir(exist_ok=True)
|
|
elif not target.exists():
|
|
target.touch()
|
|
mount("--bind", source.resolve(), target)
|
|
if not writable:
|
|
mount("-o", "remount,bind,ro,nosuid,nodev", target)
|
|
|
|
# Dynamic linker libraries, without the host /etc, /home, /data or /proc.
|
|
for name in ("/lib", "/lib64", "/usr/lib", "/usr/lib64"):
|
|
if Path(name).exists():
|
|
bind(name)
|
|
for shared in Path("/usr/local/lib").glob("*.so*"):
|
|
bind(shared)
|
|
prefix = Path(runtime["prefix"])
|
|
base = Path(runtime["base_prefix"])
|
|
broad = {Path("/"), Path("/usr"), Path("/usr/local"), Path("/opt")}
|
|
if prefix not in broad:
|
|
bind(prefix)
|
|
elif prefix == Path("/"):
|
|
raise ValueError("invalid runtime prefix")
|
|
for item in runtime["sys_path"]:
|
|
source = Path(item)
|
|
if not source.is_absolute() or not (source.is_relative_to(prefix) or source.is_relative_to(base)):
|
|
raise ValueError("runtime uses a path outside its configured environment")
|
|
if source.exists():
|
|
bind(source)
|
|
final_alias = interpreter_links(root, runtime["launch_python"])
|
|
bind(runtime["binary"])
|
|
if str(final_alias) != runtime["binary"]:
|
|
bind(runtime["binary"], final_alias)
|
|
bind("/usr/bin/setpriv")
|
|
bind(config["harness"], "/tool/harness")
|
|
bind(config["snapshot"], "/snapshot")
|
|
bind(config["input"], "/input")
|
|
bind(config["output"], "/out", writable=True)
|
|
for directory in ("tmp", "dev", "dev/shm", "proc", "home"):
|
|
(root / directory).mkdir(parents=True, exist_ok=True)
|
|
mount("-t", "tmpfs", "-o", "mode=1777,size=1g,nosuid,nodev", "tmpfs", root / "tmp")
|
|
(root / "tmp/home").mkdir(mode=0o700)
|
|
mount("-t", "tmpfs", "-o", "mode=755,size=1m,nosuid", "tmpfs", root / "dev")
|
|
(root / "dev/shm").mkdir()
|
|
mount("-t", "tmpfs", "-o", "mode=1777,size=1g,nosuid,nodev", "tmpfs", root / "dev/shm")
|
|
for name in ("null", "zero", "random", "urandom"):
|
|
bind("/dev/" + 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, (256, 256))
|
|
resource.setrlimit(resource.RLIMIT_AS, (16 * 1024**3, 16 * 1024**3))
|
|
resource.setrlimit(resource.RLIMIT_NPROC, (256, 256))
|
|
resource.setrlimit(resource.RLIMIT_FSIZE, (config["max_bytes"], config["max_bytes"]))
|
|
resource.setrlimit(resource.RLIMIT_CPU, (config["timeout_s"], config["timeout_s"] + 1))
|
|
os.chroot(root)
|
|
os.chdir("/out")
|
|
env = {"PATH": "/usr/bin:/bin", "HOME": "/tmp/home", "TMPDIR": "/tmp", "LANG": "C.UTF-8",
|
|
"OMP_NUM_THREADS": "1", "OPENBLAS_NUM_THREADS": "1", "MKL_NUM_THREADS": "1",
|
|
"PYTHONDONTWRITEBYTECODE": "1", "PYTHONNOUSERSITE": "1", "CUDA_VISIBLE_DEVICES": ""}
|
|
os.execve("/usr/bin/setpriv", ["setpriv", "--bounding-set=-all", "--inh-caps=-all",
|
|
"--ambient-caps=-all", "--no-new-privs", "--", runtime["launch_python"],
|
|
"-I", "-B", "/tool/harness/tools/recipe_model.py", "/input/request.json"], env)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|