mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
Let the MCP worker sandbox run Nix/Guix interpreters by mounting exactly the interpreter's resolved runtime closure, read-only (#521). - obliteratus/mcp_runtime_layout.py resolves the closure only when the interpreter's loader isn't already mounted: the interpreter, its loader, stdlib lib-dynload modules and every shared object listed in installed distributions' RECORD files, following DT_NEEDED with glibc's search order (DT_RPATH chain, LD_LIBRARY_PATH, DT_RUNPATH, loader directory; $ORIGIN only). - Each required /nix/store or /gnu/store path is mounted read-only at its original location; other libraries are mounted as single files. Never /, a store root, home, tmp, runtime dirs or the checkout; store names are validated and every mount stays fd-pinned. - The sandbox and the supervisor receive only the LD_LIBRARY_PATH entries the closure actually used, never the ambient value; FHS hosts get none and their mount set is unchanged. - Bounded parsing and resolution with precise fail-closed refusals; the #522 loader refusal still applies to anything unresolved. - The bubblewrap boundary test now runs and passes under the Nix flake, and the worker-boundary and crash-window tests pass there again.
447 lines
21 KiB
Python
447 lines
21 KiB
Python
"""Non-FHS (Nix/Guix) interpreters get exactly their resolved runtime closure, read-only."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import struct
|
|
import sys
|
|
import sysconfig
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from obliteratus import mcp_runtime_layout as layout
|
|
from obliteratus import mcp_worker
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
H = [c * 32 for c in "0123456789abcdfg"] # valid store hashes
|
|
|
|
|
|
def elf(path: Path, *, needed=(), rpath=None, runpath=None, soname=None, interp=None,
|
|
machine=62, tags=None, strtab_vaddr=None, link_to_load=True) -> Path:
|
|
"""Minimal little-endian ELF64 shared object with a dynamic section."""
|
|
strings, offsets = bytearray(b"\0"), {}
|
|
for value in (*needed, rpath, runpath, soname):
|
|
if value is not None and value not in offsets:
|
|
offsets[value] = len(strings)
|
|
strings += value.encode() + b"\0"
|
|
entries = [(1, offsets[name]) for name in needed]
|
|
entries += [(14, offsets[soname])] if soname is not None else []
|
|
entries += [(15, offsets[rpath])] if rpath is not None else []
|
|
entries += [(29, offsets[runpath])] if runpath is not None else []
|
|
phnum = 3 if interp is not None else 2
|
|
strtab = 64 + 56 * phnum
|
|
payload = (interp.encode() + b"\0") if interp is not None else b""
|
|
interp_off = strtab + len(strings)
|
|
dynamic = interp_off + len(payload)
|
|
entries += tags if tags is not None else [(5, strtab if strtab_vaddr is None else strtab_vaddr),
|
|
(10, len(strings))]
|
|
entries.append((0, 0))
|
|
dyn = b"".join(struct.pack("<qQ", tag, value) for tag, value in entries)
|
|
total = dynamic + len(dyn)
|
|
header = b"\x7fELF" + bytes([2, 1, 1]) + bytes(9) + struct.pack(
|
|
"<HHIQQQIHHHHHH", 3, machine, 1, 0, 64, 0, 0, 64, 56, phnum, 64, 0, 0)
|
|
load = total if link_to_load else 8
|
|
phdrs = struct.pack("<IIQQQQQQ", 1, 5, 0, 0, 0, load, load, 4096)
|
|
phdrs += struct.pack("<IIQQQQQQ", 2, 6, dynamic, dynamic, dynamic, len(dyn), len(dyn), 8)
|
|
if interp is not None:
|
|
phdrs += struct.pack("<IIQQQQQQ", 3, 4, interp_off, 0, 0, len(payload), len(payload), 1)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(header + phdrs + bytes(strings) + payload + dyn)
|
|
return path
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp(tmp_path):
|
|
return tmp_path.resolve()
|
|
|
|
|
|
@pytest.fixture
|
|
def store(tmp, monkeypatch):
|
|
root = tmp / "nix" / "store"
|
|
root.mkdir(parents=True)
|
|
monkeypatch.setattr(layout, "STORE_ROOTS", (str(root),))
|
|
return root
|
|
|
|
|
|
def link(path: Path, target) -> None:
|
|
if not path.is_symlink():
|
|
path.symlink_to(target)
|
|
|
|
|
|
def runtime(store: Path, tmp: Path):
|
|
"""A Nix-shaped interpreter: loader/libc, libpython, libstdc++ via a store symlink."""
|
|
glibc = store / f"{H[0]}-glibc" / "lib"
|
|
loader = elf(glibc / "ld-linux.so.2", soname="ld-linux.so.2")
|
|
elf(glibc / "libc.so.6", soname="libc.so.6")
|
|
python = store / f"{H[1]}-python"
|
|
exe = elf(python / "bin" / "python", interp=str(loader), needed=("libpython.so.1", "libc.so.6"),
|
|
runpath=f"{python}/lib:{glibc}")
|
|
elf(python / "lib" / "libpython.so.1", needed=("libc.so.6",), runpath="")
|
|
real = elf(store / f"{H[2]}-gcc" / "lib" / "libstdc++.so.6.0.34", needed=("libc.so.6",))
|
|
lib = store / f"{H[3]}-gcc-lib" / "lib"
|
|
lib.mkdir(parents=True, exist_ok=True)
|
|
link(lib / "libstdc++.so.6", real)
|
|
(store / f"{H[4]}-unused" / "lib").mkdir(parents=True, exist_ok=True)
|
|
site = tmp / "site"
|
|
ext = elf(site / "pkg" / "_ext.so", needed=("libbundled.so", "libstdc++.so.6", "ld-linux.so.2"),
|
|
rpath="$ORIGIN/../pkg.libs")
|
|
elf(site / "pkg.libs" / "libbundled.so", needed=("libc.so.6",))
|
|
info = site / "pkg-1.0.dist-info"
|
|
info.mkdir(exist_ok=True)
|
|
(info / "METADATA").write_text("Metadata-Version: 2.1\nName: pkg\nVersion: 1.0\n")
|
|
(info / "RECORD").write_text("pkg/_ext.so,,\npkg.libs/libbundled.so,,\npkg/gone.so,,\n"
|
|
"../outside.so,,\npkg-1.0.dist-info/RECORD,,\n")
|
|
elf(tmp / "outside.so")
|
|
stdlib = python / "lib" / "python3"
|
|
dynload = stdlib / "lib-dynload"
|
|
elf(dynload / "_ssl.so", needed=("libc.so.6",))
|
|
link(dynload / "linked.so", dynload / "_ssl.so")
|
|
(dynload / "README").write_text("not a module")
|
|
mounts = [(exe, str(exe)), (stdlib, str(stdlib)), (site, "/mcp-runtime/purelib")]
|
|
return exe, loader, site, stdlib, mounts, lib, ext
|
|
|
|
|
|
def closure(store, tmp, *, ld=None, **kwargs):
|
|
exe, loader, site, stdlib, mounts, lib, _ = runtime(store, tmp)
|
|
roots = layout.extension_modules(stdlib, [site, site])
|
|
ld = f"{store}/{H[4]}-unused/lib:{lib}::" if ld is None else ld
|
|
return layout.runtime_closure(exe, str(loader), roots, mounts, ld, [str(loader.parent)],
|
|
forbidden=(), **kwargs)
|
|
|
|
|
|
def test_dynamic_section_parsing_of_needed_paths_soname_and_loader(tmp):
|
|
path = elf(tmp / "lib.so", needed=("liba.so", "libb.so"), rpath="$ORIGIN:/x",
|
|
runpath="/r1:/r2", soname="lib.so.1", interp="/nix/store/l/ld.so")
|
|
info = layout.elf_dynamic(path)
|
|
assert info.needed == ("liba.so", "libb.so") and info.soname == "lib.so.1"
|
|
assert info.rpath == ("$ORIGIN", "/x") and info.runpath == ("/r1", "/r2")
|
|
assert info.interp == "/nix/store/l/ld.so" == layout.elf_interpreter(path)
|
|
assert info.kind == (2, 1, 62)
|
|
empty = layout.elf_dynamic(elf(tmp / "empty.so", runpath=""))
|
|
assert empty.runpath == () and empty.needed == () # present (suppresses RPATH) but empty
|
|
real = layout.elf_dynamic(Path(sys.executable).resolve())
|
|
assert real.needed or real.interp is None
|
|
|
|
|
|
@pytest.mark.parametrize(("kwargs", "message"), [
|
|
({"needed": ("liba.so",), "strtab_vaddr": 1 << 40}, "string table"),
|
|
({"needed": ("liba.so",), "tags": [(5, 0)]}, "string table"),
|
|
({"needed": ("liba.so",), "tags": [(5, 64 + 112), (10, 5)]}, "unterminated"),
|
|
({"tags": [(1, 99), (5, 64 + 112), (10, 1)]}, "string table"),
|
|
({"needed": ("liba.so",), "link_to_load": False}, "string table"),
|
|
], ids=["vaddr", "no-strsz", "unterminated", "offset", "unmapped"])
|
|
def test_malformed_dynamic_sections_fail_closed(tmp, kwargs, message):
|
|
with pytest.raises(ValueError, match=message):
|
|
layout.elf_dynamic(elf(tmp / "bad.so", **kwargs))
|
|
|
|
|
|
def test_elf_bounds_and_non_regular_inputs(tmp, monkeypatch):
|
|
path = elf(tmp / "lib.so", needed=tuple(f"lib{i}.so" for i in range(4)))
|
|
data = bytearray(path.read_bytes())
|
|
many = tmp / "phnum.so"
|
|
many.write_bytes(bytes(data[:56]) + struct.pack("<H", 600) + bytes(data[58:]))
|
|
with pytest.raises(ValueError, match="program headers"):
|
|
layout.elf_dynamic(many)
|
|
monkeypatch.setattr(layout, "MAX_NEEDED", 2)
|
|
with pytest.raises(ValueError, match="excessive ELF dependencies"):
|
|
layout.elf_dynamic(path)
|
|
monkeypatch.setattr(layout, "MAX_ELF_READ", 100)
|
|
with pytest.raises(ValueError, match="read ceiling"):
|
|
layout.elf_dynamic(path)
|
|
huge = elf(tmp / "huge.so", tags=[(99, 0)] * 9000)
|
|
monkeypatch.setattr(layout, "MAX_ELF_READ", 1 << 20)
|
|
with pytest.raises(ValueError, match="excessive ELF dynamic section"):
|
|
layout.elf_dynamic(huge)
|
|
os.mkfifo(tmp / "fifo")
|
|
with pytest.raises(ValueError, match="regular file"):
|
|
fd = os.open(tmp / "fifo", os.O_RDWR) # keep open() from blocking
|
|
try:
|
|
layout.elf_dynamic(tmp / "fifo")
|
|
finally:
|
|
os.close(fd)
|
|
(tmp / "script").write_bytes(b"#!/bin/sh\n" + bytes(64))
|
|
with pytest.raises(ValueError, match="not a supported ELF"):
|
|
layout.elf_dynamic(tmp / "script")
|
|
|
|
|
|
def test_trace_path_records_every_symlink_and_refuses_loops(tmp):
|
|
(tmp / "real").mkdir()
|
|
target = tmp / "real" / "lib.so"
|
|
target.write_bytes(b"x")
|
|
(tmp / "dir").symlink_to("real")
|
|
(tmp / "link").symlink_to(tmp / "dir" / "lib.so")
|
|
real, links = layout.trace_path(str(tmp / "link"))
|
|
assert real == target and links == (str(tmp / "link"), str(tmp / "dir"))
|
|
assert layout.trace_path(str(tmp / "dir" / ".." / "real" / "./lib.so"))[0] == target
|
|
(tmp / "loop").symlink_to("loop")
|
|
with pytest.raises(ValueError, match="too many symlinks"):
|
|
layout.trace_path(str(tmp / "loop"))
|
|
for bad in ("relative/lib.so", "/" + "x" * 5000):
|
|
with pytest.raises(ValueError, match="invalid runtime path"):
|
|
layout.trace_path(bad)
|
|
with pytest.raises(FileNotFoundError):
|
|
layout.trace_path(str(tmp / "missing"))
|
|
|
|
|
|
def test_trace_path_detects_replacement_during_resolution(tmp, monkeypatch):
|
|
(tmp / "lib.so").write_bytes(b"x")
|
|
monkeypatch.setattr(layout.Path, "resolve", lambda self, strict=False: Path("/elsewhere"))
|
|
with pytest.raises(ValueError, match="changed while resolving"):
|
|
layout.trace_path(str(tmp / "lib.so"))
|
|
|
|
|
|
def test_store_directory_and_coverage():
|
|
assert layout.store_directory(f"/nix/store/{H[0]}-glibc/lib/libc.so.6") == f"/nix/store/{H[0]}-glibc"
|
|
assert layout.store_directory(f"/gnu/store/{H[1]}-glibc") == f"/gnu/store/{H[1]}-glibc"
|
|
assert layout.store_directory("/usr/lib/libc.so.6") is None
|
|
for bad in ("/nix/store/short-name/lib", f"/nix/store/{'e' * 32}-x", "/nix/store/" + H[0]):
|
|
with pytest.raises(ValueError, match="malformed store path"):
|
|
layout.store_directory(bad)
|
|
stdlib = Path(sysconfig.get_path("stdlib")).resolve()
|
|
mounts = [(Path(sys.executable).resolve(), "/bin/py"), (stdlib, "/mcp-runtime/std")]
|
|
assert layout.covered("/bin/py", mounts) and not layout.covered("/bin/py/x", mounts)
|
|
assert layout.covered("/mcp-runtime/std/os.py", mounts, aliases=False)
|
|
assert layout.covered(f"{stdlib}/os.py", mounts)
|
|
assert not layout.covered(f"{stdlib}/os.py", mounts, aliases=False)
|
|
assert not layout.covered("/mcp-runtime/stdx", mounts)
|
|
|
|
|
|
def test_extension_modules_are_bounded_and_never_leave_the_mounted_trees(store, tmp, monkeypatch):
|
|
exe, loader, site, stdlib, *_ = runtime(store, tmp)
|
|
roots = layout.extension_modules(stdlib, [site, site])
|
|
assert roots == sorted([stdlib / "lib-dynload" / "_ssl.so", site / "pkg" / "_ext.so",
|
|
site / "pkg.libs" / "libbundled.so"])
|
|
assert layout.extension_modules(tmp / "no-stdlib", []) == []
|
|
monkeypatch.setattr(layout, "MAX_RECORD_FILES", 3)
|
|
with pytest.raises(ValueError, match="distribution records"):
|
|
layout.extension_modules(stdlib, [site])
|
|
monkeypatch.setattr(layout, "MAX_OBJECTS", 1)
|
|
with pytest.raises(ValueError, match="lib-dynload"):
|
|
layout.extension_modules(stdlib, [])
|
|
|
|
|
|
def test_closure_mounts_exactly_the_store_paths_resolved_at_their_original_paths(store, tmp):
|
|
result = closure(store, tmp)
|
|
expected = {f"{store}/{H[0]}-glibc", f"{store}/{H[1]}-python", f"{store}/{H[2]}-gcc",
|
|
f"{store}/{H[3]}-gcc-lib"}
|
|
assert {destination for _, destination in result.mounts} == expected
|
|
assert all(str(source) == destination for source, destination in result.mounts)
|
|
# Only the LD_LIBRARY_PATH entry that resolved something reaches the sandbox.
|
|
assert result.library_path == (f"{store}/{H[3]}-gcc-lib/lib",)
|
|
assert str(store) not in {destination for _, destination in result.mounts}
|
|
|
|
|
|
def test_closure_without_library_path_refuses_with_the_unresolvable_dependency(store, tmp):
|
|
with pytest.raises(ValueError, match=r"cannot resolve libstdc\+\+\.so\.6 needed by .*_ext\.so"):
|
|
closure(store, tmp, ld="")
|
|
|
|
|
|
@pytest.mark.parametrize(("ld", "message"), [
|
|
("relative/lib", "unsupported LD_LIBRARY_PATH"),
|
|
("/x/$ORIGIN", "unsupported LD_LIBRARY_PATH"),
|
|
(":".join(f"/d{i}" for i in range(65)), "excessive LD_LIBRARY_PATH"),
|
|
])
|
|
def test_ambient_library_path_is_validated(store, tmp, ld, message):
|
|
with pytest.raises(ValueError, match=message):
|
|
closure(store, tmp, ld=ld)
|
|
|
|
|
|
def test_closure_depth_and_object_ceilings(store, tmp, monkeypatch):
|
|
monkeypatch.setattr(layout, "MAX_DEPTH", 0)
|
|
with pytest.raises(ValueError, match="depth ceiling"):
|
|
closure(store, tmp)
|
|
monkeypatch.setattr(layout, "MAX_DEPTH", 32)
|
|
monkeypatch.setattr(layout, "MAX_OBJECTS", 3)
|
|
with pytest.raises(ValueError, match="object ceiling"):
|
|
closure(store, tmp)
|
|
|
|
|
|
def _single(store, tmp, ext_kwargs, *, extra=None, ld=""):
|
|
exe, loader, site, stdlib, mounts, lib, _ = runtime(store, tmp)
|
|
if extra:
|
|
extra(store, tmp)
|
|
root = elf(site / "pkg" / "_custom.so", **ext_kwargs)
|
|
return layout.runtime_closure(exe, str(loader), [root], mounts, ld, [str(loader.parent)],
|
|
forbidden=())
|
|
|
|
|
|
@pytest.mark.parametrize(("ext", "message"), [
|
|
({"needed": ("libx.so",), "runpath": "$LIB/x"}, "unsupported ELF search path"),
|
|
({"needed": ("libx.so",), "runpath": "/a::/b"}, "unsupported ELF search path"),
|
|
({"needed": ("libx.so",), "rpath": "relative"}, "unsupported ELF search path"),
|
|
({"needed": ("libx.so",), "runpath": ":".join(f"/r{i}" for i in range(65))}, "excessive ELF search"),
|
|
({"needed": ("sub/libx.so",)}, "relative DT_NEEDED"),
|
|
({"needed": ("/nonexistent/libx.so",)}, "cannot resolve /nonexistent/libx.so"),
|
|
({"needed": ("..",)}, "invalid DT_NEEDED"),
|
|
({"machine": 183}, "does not match the interpreter ELF type"),
|
|
], ids=["token", "empty-entry", "relative-rpath", "many", "relative-needed", "absolute-missing",
|
|
"dotdot", "machine"])
|
|
def test_closure_refusal_paths(store, tmp, ext, message):
|
|
with pytest.raises(ValueError, match=message):
|
|
_single(store, tmp, ext)
|
|
|
|
|
|
def test_absolute_needed_and_skipped_incompatible_candidates(store, tmp):
|
|
other = store / f"{H[5]}-other" / "lib"
|
|
good = store / f"{H[6]}-good" / "lib"
|
|
|
|
def extra(store, tmp):
|
|
elf(other / "libx.so", machine=183) # wrong machine: glibc skips it
|
|
(other / "liby.so").write_bytes(b"garbage") # not ELF: skipped
|
|
elf(good / "libx.so")
|
|
elf(good / "liby.so")
|
|
elf(good / "libabs.so")
|
|
|
|
result = _single(store, tmp, {"needed": ("libx.so", "liby.so", str(good / "libabs.so")),
|
|
"runpath": f"{other}:{good}"}, extra=extra)
|
|
destinations = {destination for _, destination in result.mounts}
|
|
assert f"{store}/{H[6]}-good" in destinations and f"{store}/{H[5]}-other" not in destinations
|
|
|
|
|
|
def test_rpath_chain_is_inherited_only_without_runpath(store, tmp):
|
|
chained = store / f"{H[7]}-chained" / "lib"
|
|
|
|
def extra(store, tmp):
|
|
elf(chained / "libinner.so")
|
|
elf(tmp / "site" / "pkg" / "libmid.so", needed=("libinner.so",))
|
|
elf(tmp / "site" / "pkg" / "libblocked.so", needed=("libinner.so",), runpath="/none")
|
|
|
|
result = _single(store, tmp, {"needed": ("libmid.so",), "rpath": f"$ORIGIN:{chained}"},
|
|
extra=extra)
|
|
assert f"{store}/{H[7]}-chained" in {destination for _, destination in result.mounts}
|
|
with pytest.raises(ValueError, match="cannot resolve libinner.so needed by .*libblocked"):
|
|
_single(store, tmp, {"needed": ("libblocked.so",), "rpath": f"$ORIGIN:{chained}"},
|
|
extra=extra)
|
|
|
|
|
|
def test_missing_loader_is_refused_with_its_path(store, tmp):
|
|
exe, loader, site, stdlib, mounts, *_ = runtime(store, tmp)
|
|
with pytest.raises(ValueError, match="dynamic loader /nix/missing/ld.so cannot be resolved"):
|
|
layout.runtime_closure(exe, "/nix/missing/ld.so", [], mounts, "", [], forbidden=())
|
|
|
|
|
|
def test_store_symlink_escaping_the_store_is_refused(store, tmp, monkeypatch):
|
|
monkeypatch.setattr(layout, "_UNTRUSTED", ())
|
|
outside = elf(tmp / "host" / "libesc.so")
|
|
|
|
def extra(store, tmp):
|
|
lib = store / f"{H[8]}-esc" / "lib"
|
|
lib.mkdir(parents=True)
|
|
(lib / "libesc.so").symlink_to(outside)
|
|
|
|
with pytest.raises(ValueError, match="symlink escapes the store"):
|
|
_single(store, tmp, {"needed": ("libesc.so",), "runpath": f"{store}/{H[8]}-esc/lib"},
|
|
extra=extra)
|
|
|
|
|
|
def test_site_symlink_escaping_the_mounted_tree_is_refused(store, tmp, monkeypatch):
|
|
monkeypatch.setattr(layout, "_UNTRUSTED", ())
|
|
outside = elf(tmp / "host" / "libesc.so")
|
|
|
|
def extra(store, tmp):
|
|
(tmp / "site" / "pkg").mkdir(parents=True, exist_ok=True)
|
|
(tmp / "site" / "pkg" / "libesc.so").symlink_to(outside)
|
|
|
|
with pytest.raises(ValueError, match="escapes the mounted tree"):
|
|
_single(store, tmp, {"needed": ("libesc.so",), "runpath": "$ORIGIN"}, extra=extra)
|
|
|
|
|
|
def test_non_store_libraries_mount_single_files_outside_untrusted_locations(store, tmp, monkeypatch):
|
|
monkeypatch.setattr(layout, "_UNTRUSTED", ())
|
|
opt = tmp / "opt" / "lib"
|
|
lib = elf(opt / "libopt.so.1.2")
|
|
(opt / "libopt.so.1").symlink_to("libopt.so.1.2")
|
|
result = _single(store, tmp, {"needed": ("libopt.so.1",)}, ld=str(opt))
|
|
files = [mount for mount in result.mounts if layout.store_directory(mount[1]) is None]
|
|
assert files == [(lib, str(opt / "libopt.so.1"))]
|
|
assert result.library_path == (str(opt),)
|
|
dotted = _single(store, tmp, {"needed": ("libopt.so.1",)}, ld=f"{opt}/../lib")
|
|
assert (lib, str(opt / "libopt.so.1")) in dotted.mounts
|
|
exe, loader, site, stdlib, mounts, *_ = runtime(store, tmp)
|
|
root = elf(site / "pkg" / "_custom.so", needed=("libopt.so.1",))
|
|
with pytest.raises(ValueError, match="outside trusted locations"):
|
|
layout.runtime_closure(exe, str(loader), [root], mounts, str(opt), [str(loader.parent)],
|
|
forbidden=(tmp / "opt",))
|
|
|
|
|
|
def test_untrusted_defaults_and_plan_structural_refusals(tmp, monkeypatch):
|
|
assert {"/tmp", "/var/tmp", "/proc", "/mcp-runtime"} <= set(layout._UNTRUSTED)
|
|
forbidden = layout._forbidden()
|
|
assert str(Path(layout.__file__).resolve().parents[1]) in forbidden
|
|
assert os.path.expanduser("~") in forbidden
|
|
monkeypatch.setattr(layout, "_UNTRUSTED", ())
|
|
library = tmp / "libtop.so"
|
|
library.write_bytes(b"x")
|
|
with pytest.raises(ValueError, match="broad runtime mount refused"):
|
|
layout._plan([("/libtop.so", library, ())], [], ())
|
|
with pytest.raises(ValueError, match="must be a store path or a file"):
|
|
layout._plan([(str(tmp / "alias" / "lib"), tmp, ())], [], ())
|
|
|
|
|
|
# --- worker integration -------------------------------------------------------------------
|
|
|
|
def _forbidden_destinations() -> set[str]:
|
|
home = os.path.expanduser("~")
|
|
checkout = str(Path(mcp_worker.__file__).resolve().parents[1])
|
|
return {"/", *layout.STORE_ROOTS, home, checkout, "/tmp", "/var/tmp", os.getcwd()}
|
|
|
|
|
|
def test_actual_mount_set_is_never_broad_and_fhs_adds_no_store_paths():
|
|
try:
|
|
paths, library_path = mcp_worker._runtime_layout(False)
|
|
except ValueError as exc: # an unresolvable closure fails closed, never broadens
|
|
assert "unsupported interpreter layout" in str(exc)
|
|
return
|
|
destinations = [destination for _, destination in paths]
|
|
assert not _forbidden_destinations().intersection(destinations)
|
|
for source, destination in paths:
|
|
store = layout.store_directory(destination)
|
|
if source.is_dir() and store is not None and destination != sysconfig.get_path("stdlib"):
|
|
assert store == destination and str(source) == destination
|
|
loader = mcp_worker.elf_interpreter(Path(sys.executable).resolve())
|
|
environment = mcp_worker._environment(False)
|
|
if loader is None or layout.store_directory(loader) is None:
|
|
# FHS: unchanged set (no store paths) and no LD_LIBRARY_PATH in the sandbox.
|
|
assert not any(layout.store_directory(d) for d in destinations if d.startswith(layout.STORE_ROOTS))
|
|
assert library_path == () and "LD_LIBRARY_PATH" not in environment
|
|
else:
|
|
assert layout.covered(loader, paths, aliases=False)
|
|
ambient = set(filter(None, os.environ.get("LD_LIBRARY_PATH", "").split(":")))
|
|
assert set(library_path) <= ambient
|
|
assert environment.get("LD_LIBRARY_PATH", "") == ":".join(library_path)
|
|
|
|
|
|
def test_fhs_layout_never_runs_closure_resolution(monkeypatch):
|
|
calls = []
|
|
monkeypatch.setattr(mcp_worker, "elf_interpreter", lambda _path: "/lib64/ld-linux-x86-64.so.2")
|
|
monkeypatch.setattr(mcp_worker, "covered", lambda path, paths, aliases=True: True)
|
|
monkeypatch.setattr(mcp_worker, "runtime_closure", lambda *a, **k: calls.append(a))
|
|
paths, library_path = mcp_worker._runtime_layout(False)
|
|
assert calls == [] and library_path == ()
|
|
assert not any(d.startswith(layout.STORE_ROOTS) and layout.store_directory(d) == d
|
|
for _, d in paths)
|
|
|
|
|
|
def test_worker_adds_closure_mounts_and_only_their_library_path(monkeypatch, tmp):
|
|
fake = "/nix/store/" + H[0] + "-glibc/lib/ld-linux-x86-64.so.2"
|
|
monkeypatch.setattr(mcp_worker, "elf_interpreter", lambda _path: fake)
|
|
monkeypatch.setenv("LD_LIBRARY_PATH", "/ambient/secret:/nix/store/" + H[1] + "-gcc/lib")
|
|
seen = {}
|
|
|
|
def resolved(executable, loader, roots, mounts, library_path, system_dirs):
|
|
seen.update(loader=loader, library_path=library_path, system_dirs=system_dirs)
|
|
return layout.RuntimeClosure(((Path(tmp), fake),), ("/nix/store/" + H[1] + "-gcc/lib",))
|
|
|
|
monkeypatch.setattr(mcp_worker, "runtime_closure", resolved)
|
|
paths, library_path = mcp_worker._runtime_layout(False)
|
|
assert (Path(tmp), fake) in paths and seen["system_dirs"] == [os.path.dirname(fake)]
|
|
assert seen["library_path"] == os.environ["LD_LIBRARY_PATH"]
|
|
assert mcp_worker.runtime_layout_supported() == (True, "")
|
|
assert mcp_worker._environment(False)["LD_LIBRARY_PATH"] == "/nix/store/" + H[1] + "-gcc/lib"
|
|
monkeypatch.setattr(mcp_worker, "runtime_closure",
|
|
lambda *a: layout.RuntimeClosure((), ()))
|
|
supported, reason = mcp_worker.runtime_layout_supported()
|
|
assert not supported and "outside the mounted runtime" in reason
|
|
assert "LD_LIBRARY_PATH" not in mcp_worker._environment(False)
|