Files
OBLITERATUS/tests/test_mcp_runtime_layout.py
Joseph MaglyandClaude Opus 5.5 5e451b7a92 fix(mcp): fail closed on interpreters whose loader the sandbox can't mount
The MCP worker sandbox mounts interpreter libraries from standard (FHS)
locations only. A Nix-built interpreter's dynamic loader lives under
/nix/store, so bubblewrap failed with an opaque execvp error.

- New obliteratus/mcp_runtime_layout.py reads the interpreter's ELF
  PT_INTERP loader with a bounded program-header parse; malformed or
  non-ELF binaries fail closed.
- _runtime_paths refuses with "unsupported interpreter layout" when the
  loader isn't among the mounted paths; FHS and static interpreters are
  unchanged. runtime_layout_supported() reports the reason.
- The real bubblewrap boundary test skips with that reason on
  unsupported layouts, as it already does without bubblewrap; on FHS
  interpreters it still runs. Mount-construction and supervisor tests
  no longer depend on the host loader layout.
- Unit tests cover real and synthetic ELF loaders, static binaries,
  non-ELF and truncated input, refusal and acceptance.

Full non-FHS support (mounting the runtime closure) is tracked in #521.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:16:36 -05:00

65 lines
2.4 KiB
Python

"""The MCP sandbox refuses interpreters whose dynamic loader it cannot mount."""
from __future__ import annotations
import struct
import sys
from pathlib import Path
import pytest
from obliteratus import mcp_worker
from obliteratus.mcp_runtime_layout import elf_interpreter
pytestmark = pytest.mark.cpu
def elf64(path: Path, interp: bytes | None) -> Path:
"""Minimal little-endian ELF64 with an optional PT_INTERP program header."""
phoff, phentsize = 64, 56
phnum = 1 if interp is not None else 0
data_off = phoff + phentsize * phnum
ident = b"\x7fELF" + bytes([2, 1, 1]) + bytes(9)
header = struct.pack("<HHIQQQIHHHHHH", 2, 62, 1, 0, phoff, 0, 0, 64, phentsize, phnum,
64, 0, 0)
blob = ident + header
if interp is not None:
payload = interp + b"\0"
blob += struct.pack("<IIQQQQQQ", 3, 4, data_off, 0, 0, len(payload), len(payload), 1)
blob += payload
path.write_bytes(blob)
return path
def test_reads_the_loader_of_a_real_interpreter():
loader = elf_interpreter(Path(sys.executable).resolve())
assert loader is None or loader.startswith("/")
def test_synthetic_elf_loader_and_static_binaries(tmp_path):
nix = "/nix/store/abc-glibc/lib/ld-linux-x86-64.so.2"
assert elf_interpreter(elf64(tmp_path / "dyn", nix.encode())) == nix
assert elf_interpreter(elf64(tmp_path / "static", None)) is None
@pytest.mark.parametrize("payload", [b"#!/bin/sh\n", b"\x7fELF", b""],
ids=["script", "truncated", "empty"])
def test_non_elf_or_truncated_binaries_fail_closed(tmp_path, payload):
target = tmp_path / "bad"
target.write_bytes(payload)
with pytest.raises((ValueError, struct.error)):
elf_interpreter(target)
def test_unmounted_loader_is_refused_with_a_clear_reason(monkeypatch):
monkeypatch.setattr(mcp_worker, "elf_interpreter",
lambda _path: "/nix/store/abc-glibc/lib/ld-linux-x86-64.so.2")
with pytest.raises(ValueError, match="unsupported interpreter layout"):
mcp_worker._runtime_paths(network=False)
supported, reason = mcp_worker.runtime_layout_supported()
assert supported is False and "/nix/store/abc-glibc" in reason
def test_mounted_or_static_loaders_are_accepted(monkeypatch):
monkeypatch.setattr(mcp_worker, "elf_interpreter", lambda _path: None)
assert mcp_worker.runtime_layout_supported() == (True, "")