Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: minimal OS sandbox for Python and Terminal tools on Linux and macOS (#10526)
Daniel Han5a55b9b43f32
diff --git a/.github/workflows/studio-backend-ci.yml b/.github/workflows/studio-backend-ci.ymlindex 0dfed4699bc..38aa526e3d5 100644--- a/.github/workflows/studio-backend-ci.yml+++ b/.github/workflows/studio-backend-ci.yml@@ -55,6 +55,7 @@ on:# absent for exactly the change they guard. Same reason as install.sh above.- '.github/actions/**'- '.github/workflows/studio-backend-ci.yml'+ - '.github/scripts/retry-with-apt-lock.sh'# tests/python/test_amd_extras_contract.py asserts against this file.- '.github/workflows/security-audit.yml'push:@@ -248,6 +249,29 @@ jobs:pyproject.tomlstudio/backend/requirements/*.txt+ - name: Install bubblewrap, so the sandbox tests run instead of skipping+ # Bound apt retries; 25 minutes covers both calls' 1450s worst case.+ timeout-minutes: 25+ env:+ RETRY_ATTEMPTS: '2'+ RETRY_ATTEMPT_TIMEOUT: '300'+ run: |+ bash .github/scripts/retry-with-apt-lock.sh sudo sh -c \+ 'apt-get install -y --no-install-recommends bubblewrap || { apt-get update && apt-get install -y --no-install-recommends bubblewrap; }'+ # Ubuntu 24.04 restricts user namespaces. Apply the recommended bwrap+ # AppArmor profile without disabling the host restriction.+ if [ "$(cat /proc/sys/kernel/apparmor_restrict_unprivileged_userns 2>/dev/null || echo 0)" = "1" ]; then+ bash .github/scripts/retry-with-apt-lock.sh sudo sh -c \+ 'apt-get install -y --no-install-recommends apparmor-profiles || { apt-get update && apt-get install -y --no-install-recommends apparmor-profiles; }'+ sudo apparmor_parser -r /etc/apparmor.d/bwrap-userns-restrict || true+ fi+ # Report unavailable isolation so host-dependent skips are explicit.+ if bwrap --ro-bind / / --unshare-user true; then+ echo "bubblewrap can build a sandbox on this runner"+ else+ echo "::warning::bubblewrap cannot build a sandbox here; confinement tests will skip"+ fi+- name: Install backend test dependencies (CPU only)run: |python -m pip install --upgrade pipdiff --git a/studio/backend/core/inference/os_sandbox.py b/studio/backend/core/inference/os_sandbox.pynew file mode 100644index 00000000000..606cfbccbe2--- /dev/null+++ b/studio/backend/core/inference/os_sandbox.py@@ -0,0 +1,605 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Studio tool launch contract and the OS-isolation backends (bwrap, Seatbelt)."""++from __future__ import annotations+import functools+import hashlib+import os+import platform+import re+import shutil+import stat+import subprocess+import sys+import time+from dataclasses import dataclass, field+from typing import Any, BinaryIO, Callable, Literal+from loggers import get_logger++logger = get_logger(__name__)++ToolExecutionMode = Literal["auto", "required", "full"]+TOOL_EXECUTION_MODES = ("auto", "required", "full")++PROFILE_VERSION = "unsloth-sandbox-v1"++# tools.py re-adds this to an UNISOLATED launch only if it already exists, which+# keeps a host that never isolates byte-identical to main.+SESSION_PACKAGES_RELPATH = ".unsloth-packages"++_SOFTWARE_SAFEGUARDS = (+ "process_guard",+ "command_and_code_analysis",+ "sanitized_environment",+ "resource_limits",+ "descriptor_closure",+ "workdir_policy",+ "streaming",+ "timeout",+ "cancellation",+ "reaping",+ "cleanup",+)+_OS_ISOLATION_SAFEGUARDS = _SOFTWARE_SAFEGUARDS + ("filesystem_isolation", "process_isolation")+_FULL_SAFEGUARDS = ("timeout", "cancellation", "reaping", "cleanup")+++class SandboxUnavailableError(RuntimeError):+ """OS isolation is unavailable or the requested launch was refused."""++ def __init__(+ self,+ message: str = "",+ *,+ remediation: str = "",+ ) -> None:+ super().__init__(message)+ self.remediation = remediation+++class WorkdirUnsafeError(SandboxUnavailableError):+ """Distinguished by TYPE, not by re-probing: a transient probe failure must+ not re-open the very channel the workdir scan just found."""+++class SandboxBuildError(SandboxUnavailableError):+ """The probe passed but this launch could not be built. Refused, not fallen+ back: the errno is reachable from inside the jail, so a tool call that fills+ the disk could otherwise buy itself an unisolated launch."""+++@dataclass(frozen = True)+class SandboxCapability:+ """``available`` is never inferred from a binary being on disk: an installed+ bwrap on a host that denies user namespaces looks identical until you try."""++ backend: str+ available: bool+ reason: str+ environment: str = "unknown"+ protection_state: str = "unavailable"+ profile_id: str = "none"+ limitations: tuple[str, ...] = ()+ probe_generation: str = ""+ environment_fingerprint: str = ""+ remediation: str = ""+++@dataclass(frozen = True)+class ToolExecutionRecord:+ """Record the requested policy and the isolation and safeguards actually applied."""++ requested_mode: ToolExecutionMode+ effective_mode: str+ environment: str+ backend: str+ profile_id: str+ probe_generation: str+ os_isolation: bool+ retained_safeguards: tuple[str, ...]+ limitations: tuple[str, ...] = ()+ # Always "unrestricted": this confines the filesystem, not the network.+ network_policy: str = "unrestricted"++ def as_dict(self) -> dict[str, object]:+ return {+ "requested_mode": self.requested_mode,+ "effective_mode": self.effective_mode,+ "environment": self.environment,+ "backend": self.backend,+ "profile_id": self.profile_id,+ "probe_generation": self.probe_generation,+ "os_isolation": self.os_isolation,+ "retained_safeguards": list(self.retained_safeguards),+ "limitations": list(self.limitations),+ "network_policy": self.network_policy,+ }+++@dataclass(frozen = True)+class ToolLaunchPlan:+ argv: tuple[str, ...]+ workdir: str+ env: dict[str, str]+ preexec_fn: Callable[[], None] | None = None+ requested_mode: ToolExecutionMode = "auto"+ timeout_seconds: int | None = None+ close_fds: bool = True+ terminate_descendants: bool = True+ # Set by the trusted tool owner, never inferred from model args.+ execution_kind: Literal["python", "terminal"] | None = None+++@dataclass+class PreparedSandboxLaunch:+ argv: tuple[str, ...]+ workdir: str+ env: dict[str, str]+ preexec_fn: Callable[[], None] | None+ backend: str+ execution_record: ToolExecutionRecord | None = None+ pass_fds: tuple[int, ...] = ()+ owned_files: list[BinaryIO] = field(default_factory = list)+ cleanup_paths: list[str] = field(default_factory = list)+ timeout_seconds: int | None = None+ close_fds: bool = True+ terminate_descendants: bool = True+ cleanup_callbacks: list[Callable[[], None]] = field(default_factory = list)+ cleanup_diagnostics: list[str] = field(default_factory = list)++ def cleanup(self) -> None:+ while self.cleanup_callbacks:+ callback = self.cleanup_callbacks.pop()+ try:+ callback()+ except Exception as exc: # noqa: BLE001 - cleanup continues in LIFO order+ diagnostic = f"{type(exc).__name__}: {exc}"+ self.cleanup_diagnostics.append(diagnostic)+ logger.warning("Sandbox cleanup failed: %s", diagnostic, exc_info = True)+ while self.owned_files:+ try:+ self.owned_files.pop().close()+ except Exception as exc: # noqa: BLE001 - cleanup must continue+ diagnostic = f"{type(exc).__name__}: {exc}"+ self.cleanup_diagnostics.append(diagnostic)+ logger.warning("Could not close sandbox-owned file: %s", diagnostic, exc_info = True)+ while self.cleanup_paths:+ path = self.cleanup_paths.pop()+ try:+ shutil.rmtree(path)+ except OSError:+ self.cleanup_diagnostics.append(f"could not remove private sandbox path: {path}")+ logger.warning("Could not remove private sandbox path %s", path, exc_info = True)+++def spawn_prepared_launch(prepared: PreparedSandboxLaunch, **popen_kwargs: Any) -> object:+ return subprocess.Popen(prepared.argv, **popen_kwargs)+++WORKDIR_SCAN_ENTRIES = 50_000+WORKDIR_SCAN_SECONDS = 5.0+# The shared cache is walked per launch, so its budget is tighter than the+# workdir's; a cache too big to check in it is simply not shared.+CACHE_SCAN_ENTRIES = 50_000+CACHE_SCAN_SECONDS = 3.0+++def _host_channel_hazard(root: str, max_entries: int, seconds: float) -> str | None:+ """Return a host-access hazard under *root*, or None; never raise.++ Reject sockets, devices, FIFOs, external hard links, nested mounts and scan overruns.+ Tool-created entries cannot be distinguished from host entries. The root+ itself may be a mount point.+ """+ deadline = time.monotonic() + seconds+ entries = 0+ # Refusing every st_nlink > 1 would refuse any tree built by `cp -al`,+ # `git clone --local` or pip; only an unaccounted link leads outside.+ links: dict[tuple[int, int], list] = {}+ unreadable: list[str] = []++ for base, dirs, names in os.walk(+ root, followlinks = False, onerror = lambda exc: unreadable.append(exc.filename or root)+ ):+ if unreadable:+ return f"{unreadable[0]} cannot be fully inspected"+ for name in (*dirs, *names):+ entries += 1+ if entries > max_entries or time.monotonic() > deadline:+ return f"too large to check for host channels (over {max_entries} entries or {seconds:.0f}s)"+ path = os.path.join(base, name)+ try:+ info = os.lstat(path)+ except OSError:+ return f"changed during its safety scan: {path}"+ if stat.S_ISLNK(info.st_mode):+ continue+ if stat.S_ISDIR(info.st_mode):+ # Misses a same-filesystem bind mount; Linux also asks the mount table.+ if os.path.ismount(path):+ return f"contains a nested host mount: {path}"+ continue+ if not stat.S_ISREG(info.st_mode):+ return f"contains a device or IPC node: {path}"+ if info.st_nlink > 1:+ found = links.setdefault((info.st_dev, info.st_ino), [0, info.st_nlink, path])+ found[0] += 1+ if unreadable:+ return f"{unreadable[0]} cannot be fully inspected"+ for found, total, path in links.values():+ if found < total:+ return f"contains a file hard-linked from outside it: {path}"+ return None+++def scan_workdir_for_host_channels(workdir: str) -> None:+ """The writable session workdir. A hazard here fails the call."""+ hazard = _host_channel_hazard(workdir, WORKDIR_SCAN_ENTRIES, WORKDIR_SCAN_SECONDS)+ if hazard is not None:+ raise WorkdirUnsafeError(f"the session workdir {hazard}")+++def cache_share_hazard(path: str) -> str | None:+ """Return a reason not to share this writable cache component, or None.++ Apply the workdir's host-access checks: sockets and hard links can expose+ host resources. Unsafe components are omitted, not launch failures, so a+ planted socket cannot disable later calls. Missing caches are re-downloaded.+ """+ return _host_channel_hazard(path, CACHE_SCAN_ENTRIES, CACHE_SCAN_SECONDS)+++_LINUX_REQUIRED_BINARIES = ("bwrap",)+_FALLBACK_NOTE = "Python and Terminal still run, with software safeguards only and no OS isolation."++[email protected]_cache(maxsize = 1)+def _linux_userns_blocked_by_apparmor() -> bool:+ """Whether Ubuntu 23.10+'s ``apparmor_restrict_unprivileged_userns`` denies the+ user namespace bwrap needs. Cached: it forks, and every snapshot reaches it."""+ try:+ with open("/proc/sys/kernel/apparmor_restrict_unprivileged_userns", encoding = "utf-8") as f:+ if f.read().strip() != "1":+ return False+ except OSError:+ return False+ # The sysctl alone is not proof: a profile may permit bwrap.+ try:+ probe = subprocess.run(+ ["unshare", "--user", "--map-root-user", "true"],+ stdin = subprocess.DEVNULL,+ stdout = subprocess.DEVNULL,+ stderr = subprocess.DEVNULL,+ timeout = 10,+ )+ except (OSError, subprocess.SubprocessError):+ return False+ return probe.returncode != 0++[email protected]_cache(maxsize = 1)+def editable_source_roots() -> tuple[str, ...]:+ """Editable code outside site-packages, from PEP 610 records shared by .pth and PEP 660 installs."""+ roots: list[str] = []+ try:+ from importlib import metadata+ import json+ from urllib.parse import unquote, urlparse+ except Exception: # noqa: BLE001 - never fail a launch over this+ return ()+ try:+ distributions = list(metadata.distributions())+ except Exception: # noqa: BLE001+ return ()+ for dist in distributions:+ try:+ raw = dist.read_text("direct_url.json")+ if not raw:+ continue+ record = json.loads(raw)+ if not record.get("dir_info", {}).get("editable"):+ continue+ parsed = urlparse(record.get("url", ""))+ if parsed.scheme != "file":+ continue+ path = os.path.abspath(unquote(parsed.path))+ except Exception: # noqa: BLE001 - a malformed record is not a launch failure+ continue+ if path in ("/", "/usr") or not os.path.isdir(path):+ continue+ for importable in _importable_entries(path, _declared_names(dist)):+ if importable not in roots:+ roots.append(importable)+ return tuple(roots)++[email protected]_cache(maxsize = 1)+def editable_import_roots() -> tuple[str, ...]:+ """Import roots need listing: bwrap creates empty parents, Seatbelt needs literal grants."""+ return tuple(dict.fromkeys(os.path.dirname(path) for path in editable_source_roots()))+++def _declared_names(dist) -> frozenset[str]:+ """Declared names also identify PEP 420 namespaces, which have no __init__.py."""+ names: set[str] = set()+ try:+ raw = dist.read_text("top_level.txt") or ""+ names.update(line.strip() for line in raw.splitlines() if line.strip())+ except Exception: # noqa: BLE001 - a missing or unreadable record is not fatal+ pass+ try:+ project = (dist.metadata["Name"] or "").strip()+ except Exception: # noqa: BLE001+ project = ""+ if project:+ names.add(re.sub(r"[-_.]+", "_", project).lower())+ return frozenset(name for name in names if name and "/" not in name and name != "..")+++def _within_root(path: str, root: str) -> bool:+ """Whether *path* stays under *root* once resolved, by whole components."""+ resolved, base = os.path.realpath(path), os.path.realpath(root)+ return resolved == base or resolved.startswith(base.rstrip(os.sep) + os.sep)+++def _importable_entries(+ project_root: str, declared: frozenset[str] = frozenset()+) -> tuple[str, ...]:+ """Grant package entries, never the checkout's .env, .git or unrelated fixtures.++ Use the installer's sys.path root when present; an unconfirmed import gets no grant.+ """+ import_roots = [+ entry+ for entry in sys.path+ if entry+ and (+ os.path.abspath(entry) == project_root+ or os.path.abspath(entry).startswith(project_root + os.sep)+ )+ ]+ # A PEP 660 finder puts nothing on sys.path, so fall back to the two layouts+ # that cover almost everything published.+ guessed = [+ fallback+ for fallback in (project_root, os.path.join(project_root, "src"))+ if fallback not in import_roots and os.path.isdir(fallback)+ ]+ found: list[str] = []+ for import_root in (*import_roots, *guessed):+ try:+ names = sorted(os.listdir(import_root))+ except OSError:+ continue+ for name in names:+ if name.startswith(".") or name.endswith((".egg-info", ".dist-info")):+ continue+ entry = os.path.join(import_root, name)+ declared_here = name in declared or name.removesuffix(".py") in declared+ if import_root in guessed and not declared_here:+ # A guessed layout may include unrelated deploy.py or tests/ packages.+ continue+ # Both backends grant resolved targets, so a declared symlink must stay in the checkout.+ if not _within_root(entry, project_root):+ continue+ package = os.path.isdir(entry) and (+ declared_here or os.path.exists(os.path.join(entry, "__init__.py"))+ )+ module = name.endswith(".py") and (declared_here or os.path.isfile(entry))+ if (package or module) and entry not in found:+ found.append(entry)+ return tuple(found)+++def linux_unavailable_remediation() -> str:+ missing = [name for name in _LINUX_REQUIRED_BINARIES if shutil.which(name) is None]+ if missing:+ return (+ f"Install the missing Linux prerequisites ({', '.join(missing)}) with your "+ f"distribution's package manager, or re-run Studio setup. {_FALLBACK_NOTE}"+ )+ if _linux_userns_blocked_by_apparmor():+ return (+ "This host denies unprivileged user namespaces "+ "(kernel.apparmor_restrict_unprivileged_userns=1, the default on Ubuntu 23.10 and "+ "newer), so bubblewrap cannot build a sandbox even though it is installed. Grant "+ "bwrap the userns permission with an AppArmor profile "+ "(/etc/apparmor.d/bwrap-userns-restrict from the apparmor-profiles package), or "+ f"re-run Studio setup, which offers to install it. {_FALLBACK_NOTE}"+ )+ return f"This host cannot start an OS sandbox. {_FALLBACK_NOTE}"+++def _runtime_identity() -> str:+ """Changes when the interpreter or this module changes, so swapping the venv+ under a running Studio re-probes instead of reusing a stale verdict."""+ digest = hashlib.sha256()+ for path in (sys.executable, __file__):+ resolved = os.path.realpath(path)+ digest.update(resolved.encode())+ try:+ info = os.stat(resolved)+ digest.update(str((info.st_size, info.st_mtime_ns)).encode())+ except OSError:+ digest.update(b"missing")+ digest.update((PROFILE_VERSION + sys.platform + platform.release()).encode())+ digest.update((os.path.abspath(sys.executable) + sys.prefix).encode())+ return digest.hexdigest()+++def _unavailable(reason: str, remediation: str, identity: str) -> SandboxCapability:+ return SandboxCapability(+ backend = "none",+ available = False,+ reason = reason,+ environment = sys.platform,+ protection_state = "unavailable",+ limitations = ("no_os_isolation",),+ probe_generation = hashlib.sha256((identity + "unavailable").encode()).hexdigest(),+ environment_fingerprint = identity,+ remediation = remediation,+ )+++def capability_snapshot(*, force: bool = False) -> SandboxCapability:+ identity = _runtime_identity()+ if sys.platform == "linux":+ from . import sandbox_linux+ backend = sandbox_linux+ elif sys.platform == "darwin":+ from . import sandbox_macos+ backend = sandbox_macos+ else:+ return _unavailable(+ "OS isolation for Studio tools is available on Linux and macOS only.",+ f"No sandbox backend exists for this platform. {_FALLBACK_NOTE}",+ identity,+ )++ from .sandbox_probe import probe++ available, reason = probe(backend, force = force)+ if not available:+ remediation = (+ linux_unavailable_remediation()+ if sys.platform == "linux"+ else f"This host cannot start an OS sandbox. {_FALLBACK_NOTE}"+ )+ return _unavailable(reason, remediation, identity)+ return SandboxCapability(+ backend = backend.BACKEND_NAME,+ available = True,+ reason = reason,+ environment = sys.platform,+ protection_state = "preview",+ profile_id = backend.PROFILE_ID,+ limitations = backend.LIMITATIONS,+ probe_generation = hashlib.sha256((identity + "available").encode()).hexdigest(),+ environment_fingerprint = identity,+ remediation = (+ "Python and Terminal run with OS isolation. The network is not confined; a tool "+ "call can still reach the internet."+ ),+ )+++def _record(+ plan: ToolLaunchPlan,+ capability: SandboxCapability,+ *,+ effective_mode: str,+ os_isolation: bool,+ backend: str,+ profile_id: str,+ safeguards: tuple[str, ...],+ limitations: tuple[str, ...],+) -> ToolExecutionRecord:+ return ToolExecutionRecord(+ requested_mode = plan.requested_mode,+ effective_mode = effective_mode,+ environment = capability.environment,+ backend = backend,+ profile_id = profile_id,+ probe_generation = capability.probe_generation,+ os_isolation = os_isolation,+ retained_safeguards = tuple(+ item for item in safeguards if item != "timeout" or plan.timeout_seconds is not None+ ),+ limitations = limitations,+ )+++def _software_only_limitations() -> tuple[str, ...]:+ # Teardown is killpg on the captured group, which a tool that calls setsid+ # and closes stdout survives.+ limitations = ["no_os_isolation", "host_files_readable", "unrestricted_network"]+ if sys.platform != "win32":+ limitations.append("detached_descendant_cleanup_unverified")+ return tuple(limitations)+++def prepare_tool_launch(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:+ """Unavailable hosts fall back in auto; unsafe workdirs and build failures refuse."""+ if plan.requested_mode not in TOOL_EXECUTION_MODES:+ raise SandboxUnavailableError(f"unknown tool execution mode: {plan.requested_mode!r}")++ if plan.requested_mode == "full":+ return PreparedSandboxLaunch(+ argv = plan.argv,+ workdir = plan.workdir,+ env = plan.env,+ preexec_fn = plan.preexec_fn,+ backend = "none",+ timeout_seconds = plan.timeout_seconds,+ close_fds = plan.close_fds,+ terminate_descendants = plan.terminate_descendants,+ execution_record = ToolExecutionRecord(+ requested_mode = plan.requested_mode,+ effective_mode = "full",+ environment = sys.platform,+ backend = "none",+ profile_id = "full-access",+ probe_generation = "",+ os_isolation = False,+ retained_safeguards = tuple(+ item+ for item in _FULL_SAFEGUARDS+ if item != "timeout" or plan.timeout_seconds is not None+ ),+ limitations = ("security_restrictions_disabled",),+ ),+ )++ capability = capability_snapshot()++ if not capability.available:+ if plan.requested_mode == "required":+ raise SandboxUnavailableError(+ f"OS_ISOLATION_UNAVAILABLE: {capability.reason}",+ remediation = capability.remediation,+ )+ return PreparedSandboxLaunch(+ argv = plan.argv,+ workdir = plan.workdir,+ env = plan.env,+ preexec_fn = plan.preexec_fn,+ backend = "software-safeguards",+ timeout_seconds = plan.timeout_seconds,+ close_fds = plan.close_fds,+ terminate_descendants = plan.terminate_descendants,+ execution_record = _record(+ plan,+ capability,+ effective_mode = "software_safeguards",+ os_isolation = False,+ backend = "software-safeguards",+ profile_id = "software-safeguards-v1",+ safeguards = _SOFTWARE_SAFEGUARDS,+ limitations = _software_only_limitations(),+ ),+ )++ if sys.platform == "linux":+ from . import sandbox_linux as backend+ else:+ from . import sandbox_macos as backend++ try:+ prepared = backend.prepare(plan)+ except OSError as exc:+ # Must be typed: raw, this reaches tools.py's general `except Exception`,+ # which answers `auto` by running with software safeguards.+ raise SandboxBuildError(f"the sandbox could not be built on this host: {exc}") from exc+ prepared.execution_record = _record(+ plan,+ capability,+ effective_mode = "os_isolated",+ os_isolation = True,+ backend = capability.backend,+ profile_id = capability.profile_id,+ safeguards = _OS_ISOLATION_SAFEGUARDS,+ limitations = capability.limitations,+ )+ return prepareddiff --git a/studio/backend/core/inference/sandbox_landlock.py b/studio/backend/core/inference/sandbox_landlock.pynew file mode 100644index 00000000000..d1fb010c2e1--- /dev/null+++ b/studio/backend/core/inference/sandbox_landlock.py@@ -0,0 +1,178 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Landlock ABI 6 scoping, which blocks host abstract AF_UNIX sockets.++Those live in the shared network namespace, so no mount or bind rule hides them+and seccomp cannot close it either (the address is behind a pointer). Best+effort: on a pre-6.12 kernel nothing is applied and ``LIMITATIONS`` says so.+"""++from __future__ import annotations++import ctypes+import functools+import os+import select+import signal+import struct+from typing import Callable++_NR_LANDLOCK_CREATE_RULESET = 444+_NR_LANDLOCK_RESTRICT_SELF = 446+_LANDLOCK_CREATE_RULESET_VERSION = 1 << 0+_LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET = 1 << 0+# struct landlock_ruleset_attr; the third member is ABI 6's, so this size is+# itself the version check -- an older kernel answers E2BIG.+_RULESET_ATTR = struct.pack("=QQQ", 0, 0, _LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET)+_PR_SET_NO_NEW_PRIVS = 38+# Built at import, never in the forked child: see apply_abstract_scope.+_RULESET_ATTR_BUFFER = ctypes.create_string_buffer(_RULESET_ATTR, len(_RULESET_ATTR))+_RULESET_ATTR_REF = ctypes.byref(_RULESET_ATTR_BUFFER)+_RULESET_ATTR_SIZE = ctypes.c_size_t(len(_RULESET_ATTR))+_ZERO_FLAGS = ctypes.c_uint32(0)+# The child binds one socket and calls two syscalls, so this is a hang budget,+# not a work budget.+_PROBE_TIMEOUT_SECONDS = 5.0++try:+ # Resolved at import, never in the forked child, where an import can+ # deadlock on a lock a thread held at fork time.+ _libc = ctypes.CDLL(None, use_errno = True)+ _libc.syscall.restype = ctypes.c_long+except (OSError, TypeError, AttributeError): # pragma: no cover+ # TypeError is Windows: CDLL(None) means "the running process" only where+ # dlopen has that convention, and ctypes there tests the name for a+ # separator before anything else. Importing this module raised, which is not+ # something a Linux-only helper should do on a platform that never calls it.+ _libc = None++[email protected]_cache(maxsize = 1)+def abstract_scope_supported() -> bool:+ """Cache whether a child can apply the scope, not just query its ABI.++ Outer sandboxes and nesting limits can deny application. Probe in a child+ because restrict_self is irreversible. Failure retains the abstract-socket+ limitation without disabling filesystem and PID isolation.+ """+ if _libc is None:+ return False+ if not _abi_reports_scope():+ return False+ # Bind in the unscoped parent: the child may reach its own sockets, but must+ # not reach sockets outside its Landlock domain.+ import socket as _socket++ name = b"\0unsloth-scope-" + os.urandom(6).hex().encode()+ listener = _socket.socket(_socket.AF_UNIX, _socket.SOCK_STREAM)+ try:+ listener.bind(name)+ listener.listen(1)+ except OSError:+ listener.close()+ return False+ read_fd, write_fd = os.pipe()+ pid = os.fork()+ if pid == 0: # pragma: no cover - the child never returns+ try:+ os.close(read_fd)+ listener.close()+ apply_abstract_scope()+ client = _socket.socket(_socket.AF_UNIX, _socket.SOCK_STREAM)+ client.settimeout(2)+ try:+ client.connect(name)+ os.write(write_fd, b"n") # reached it, so nothing was applied+ except OSError:+ os.write(write_fd, b"y") # refused, so the scope is in force+ except BaseException:+ try:+ os.write(write_fd, b"n")+ except OSError:+ pass+ finally:+ os._exit(0)+ os.close(write_fd)+ answer = b""+ try:+ # A forked child can deadlock on inherited locks. Bound the read and kill+ # a stalled child; timeout leaves the scope unproven, not tools disabled.+ if select.select([read_fd], [], [], _PROBE_TIMEOUT_SECONDS)[0]:+ answer = os.read(read_fd, 1)+ except OSError:+ answer = b""+ finally:+ os.close(read_fd)+ listener.close()+ _reap(pid)+ return answer == b"y"+++def _reap(pid: int) -> None:+ """SIGKILL then wait. Harmless for a child that already exited: it is a+ zombie until reaped, so the signal lands on nothing and the wait returns."""+ try:+ os.kill(pid, signal.SIGKILL)+ except OSError:+ pass+ try:+ os.waitpid(pid, 0)+ except OSError:+ pass+++def _abi_reports_scope() -> bool:+ """NULL attr only reports the ABI version and changes nothing."""+ if _libc is None:+ return False+ try:+ version = _libc.syscall(+ _NR_LANDLOCK_CREATE_RULESET,+ None,+ ctypes.c_size_t(0),+ ctypes.c_uint32(_LANDLOCK_CREATE_RULESET_VERSION),+ )+ except (OSError, AttributeError, TypeError):+ return False+ return version >= 6+++def apply_abstract_scope() -> None:+ """Apply in the forked child without raising or logging; the probe checks success.++ Reuse import-time allocations to reduce inherited-lock deadlocks before+ exec, when Popen's timeout has not started. This is not async-signal-safe.+ """+ if _libc is None:+ return+ ctypes.set_errno(0)+ ruleset = _libc.syscall(+ _NR_LANDLOCK_CREATE_RULESET,+ _RULESET_ATTR_REF,+ _RULESET_ATTR_SIZE,+ _ZERO_FLAGS,+ )+ if ruleset < 0:+ return+ try:+ # Required before restrict_self for an unprivileged caller. Set again+ # here so this does not depend on which pre-exec it was composed with.+ _libc.prctl(_PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)+ _libc.syscall(_NR_LANDLOCK_RESTRICT_SELF, ctypes.c_int(int(ruleset)), _ZERO_FLAGS)+ finally:+ try:+ os.close(int(ruleset))+ except OSError:+ pass+++def with_abstract_scope(preexec_fn: "Callable[[], None] | None") -> "Callable[[], None]":+ """*preexec_fn* first (it is the setsid the kill paths signal), then the scope."""++ def preexec() -> None:+ if preexec_fn is not None:+ preexec_fn()+ apply_abstract_scope()++ return preexecdiff --git a/studio/backend/core/inference/sandbox_linux.py b/studio/backend/core/inference/sandbox_linux.pynew file mode 100644index 00000000000..f25a4156b06--- /dev/null+++ b/studio/backend/core/inference/sandbox_linux.py@@ -0,0 +1,646 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The bubblewrap backend: a read-only system, one writable directory, no home.++Binds are parent directories, never individual shared objects: enumerating an+interpreter's ``.so`` files still misses the one dlopen() reaches at runtime.+"""++from __future__ import annotations++import os+import shutil+import stat+import site+import subprocess+import sys+import sysconfig+import tempfile+import threading+import time+from functools import lru_cache++from loggers import get_logger++from . import sandbox_landlock, sandbox_seccomp+from .os_sandbox import (+ CACHE_SCAN_SECONDS,+ PROFILE_VERSION,+ SESSION_PACKAGES_RELPATH,+ PreparedSandboxLaunch,+ SandboxUnavailableError,+ ToolLaunchPlan,+ WorkdirUnsafeError,+ cache_share_hazard,+ editable_source_roots,+ scan_workdir_for_host_channels,+)++logger = get_logger(__name__)++BACKEND_NAME = "bubblewrap"+PROFILE_ID = f"linux-bwrap-{PROFILE_VERSION}"+LIMITATIONS = (+ "unrestricted_network",+ "system_paths_readable",+ "model_cache_writable",+ # --dev builds a fresh /dev, so a sandboxed tool call is CPU-only.+ "gpu_devices_hidden",+ # --unshare-pid: a backgrounded process dies with the foreground command.+ "detached_processes_die_with_the_call",+ # Abstract AF_UNIX sockets live in the shared network namespace, so without+ # the Landlock scope (Linux 6.12+) a launch reaches the session bus and X.+ *(() if sandbox_landlock.abstract_scope_supported() else ("host_abstract_sockets_reachable",)),+ # Read-only mounts do not block socket connect(); system roots are not scanned per call.+ "host_pathname_sockets_reachable",+ "shared_kernel",+)++_SYSTEM_ROOTS = (+ "/usr/bin",+ "/usr/sbin",+ "/usr/lib",+ "/usr/lib64",+ # git keeps its helpers in /usr/libexec/git-core on Fedora and RHEL.+ "/usr/libexec",+ "/usr/local/bin",+ "/usr/local/lib",+ "/usr/local/lib64",+ "/usr/local/libexec",+ "/usr/local/sbin",+ "/usr/local/share",+ "/usr/share",+ "/opt",+ # Headers, for a pip install with no wheel that builds from source.+ "/usr/include",+ "/usr/local/include",+ "/bin",+ "/sbin",+ "/lib",+ "/lib64",+)+# /etc is fresh in the jail; without these nothing dynamically linked starts.+_ETC_FILES = (+ "/etc/alternatives",+ "/etc/ld.so.cache",+ "/etc/ld.so.conf",+ "/etc/ld.so.conf.d",+ "/etc/localtime",+ "/etc/nsswitch.conf",+)+# Bound only when it passes _trusted_system_file. git reads it for a corporate+# proxy, a custom CA path or a URL rewrite, and /etc is fresh in the jail, so+# without it an otherwise valid clone fails against defaults. macOS already binds+# it; this is the Linux half.+_ETC_FILES_IF_TRUSTED = ("/etc/gitconfig",)+# Resolution and TLS trust. The PUBLIC halves one by one, never /etc/ssl or+# /etc/pki whole: both carry private keys beside the certificates, and a miss+# here breaks verification loudly where a miss in a mask list leaks a key quietly.+_NETWORK_FILES = (+ "/etc/resolv.conf",+ "/etc/hosts",+ "/etc/host.conf",+ "/etc/gai.conf",+ "/etc/services",+ "/etc/protocols",+ "/etc/ssl/certs",+ "/etc/ssl/cert.pem",+ "/etc/ssl/openssl.cnf",+ "/etc/pki/tls/certs",+ "/etc/pki/tls/cert.pem",+ "/etc/pki/tls/openssl.cnf",+ "/etc/pki/ca-trust",+ "/etc/ca-certificates",+ "/etc/ca-certificates.conf",+ "/etc/crypto-policies",+ "/var/lib/ca-certificates",+)+# Bound at the jail's own HOME with HF_HOME pinned to match: a host HF_HOME+# pointing somewhere unbound would fail on a read-only root.+_MODEL_CACHE_RELPATH = os.path.join(".cache", "huggingface")+# Exclude $HF_HOME/token and executable modules. Writable hub is a deliberate tradeoff:+# it preserves downloads across sessions, but tools can poison snapshots/*/modeling_*.py+# for a later host load using opt-in trust_remote_code. See model_cache_writable and #5603.+_MODEL_CACHE_SUBDIRS = ("hub", "datasets", "xet", "assets")+# NixOS keeps glibc here, so a store interpreter cannot link without it.+_NIX_STORE = "/nix/store"+++def _trusted_system_file(path: str) -> bool:+ """Require a root-owned, non-user-writable regular file with no symlink hops.++ Otherwise a config bind could expose a user-controlled path outside the jail.+ """+ try:+ info = os.lstat(path)+ except OSError:+ return False+ if not stat.S_ISREG(info.st_mode):+ return False # a symlink or anything else is not followed+ return info.st_uid == 0 and not info.st_mode & (stat.S_IWGRP | stat.S_IWOTH)+++def _within(path: str, root: str) -> bool:+ path, root = os.path.abspath(path), os.path.abspath(root)+ try:+ return os.path.commonpath((path, root)) == root+ except ValueError:+ return False+++@lru_cache(maxsize = 8)+def _bwrap_long_options(identity: tuple[str, int, int]) -> frozenset[str]:+ """Keyed by file identity so a package upgrade under a running Studio is+ re-read, not answered from a verdict about the old binary."""+ try:+ completed = subprocess.run(+ [identity[0], "--help"],+ stdin = subprocess.DEVNULL,+ stdout = subprocess.PIPE,+ stderr = subprocess.PIPE,+ text = True,+ encoding = "utf-8",+ errors = "replace",+ timeout = 5,+ close_fds = True,+ )+ except (OSError, subprocess.SubprocessError):+ return frozenset()+ return frozenset(+ token.strip().rstrip(",")+ for line in f"{completed.stdout}\n{completed.stderr}".splitlines()+ for token in line.split()+ if token.startswith("--")+ )+++def _bwrap_supports(bwrap: str, option: str) -> bool:+ try:+ info = os.stat(bwrap)+ identity = (bwrap, info.st_ino, info.st_mtime_ns)+ except OSError:+ identity = (bwrap, 0, 0)+ return option in _bwrap_long_options(identity)+++def _host_mount_points() -> tuple[str, ...]:+ """Every host mount point; unreadable is a refusal, not an empty list. Not+ resolved: realpath on each entry blocks on a stale NFS mount."""+ points: list[str] = []+ try:+ with open("/proc/self/mountinfo", encoding = "utf-8") as stream:+ for line in stream:+ fields = line.split()+ if len(fields) < 5:+ raise SandboxUnavailableError("cannot parse the host mount table")+ raw = (+ fields[4]+ .replace("\\040", " ")+ .replace("\\011", "\t")+ .replace("\\012", "\n")+ .replace("\\134", "\\")+ )+ points.append(raw)+ except OSError as exc:+ raise SandboxUnavailableError("cannot read the host mount table") from exc+ return tuple(points)+++def _runtime_paths_under(workdir: str) -> tuple[str, ...]:+ """Protect Studio's runtime after the writable workdir bind.++ CPython can keep an alias in sys.prefix, so containment uses resolved paths.+ External targets get no bind: a venv/lib link to ~/.ssh must stay hidden.+ """+ canonical_root = os.path.realpath(workdir)+ inside: list[str] = []+ # Standalone Python may have no bin/; the probe executes this file on the host.+ candidates = [sys.executable, *editable_source_roots()]+ for prefix in (sys.prefix, sys.base_prefix, sys.exec_prefix, sys.base_exec_prefix):+ candidates.extend(+ os.path.join(prefix, name)+ for name in ("bin", "include", "lib", "lib64", "libexec", "pyvenv.cfg", "ssl")+ )+ for candidate in candidates:+ if not os.path.exists(candidate):+ continue+ written, resolved = os.path.abspath(candidate), os.path.realpath(candidate)+ # Protect both target and name: otherwise the writable alias can be replaced.+ if _within(written, canonical_root) and not _within(resolved, canonical_root):+ if candidate is sys.executable:+ # Refuse: protecting this alias would expose its external target.+ raise WorkdirUnsafeError(+ f"the session workdir holds a link to the Python that runs Studio: {written}"+ )+ continue+ for path in (written, resolved):+ if _within(path, canonical_root) and path not in inside:+ inside.append(path)+ return tuple(inside)+++def _validate_workdir(workdir: str) -> str:+ """The mount table is re-read here because the shared scan's ``os.path.ismount``+ compares device numbers and misses a same-filesystem bind mount, which is what+ the recursive workdir bind would carry in writable."""+ resolved = os.path.realpath(workdir)+ if not os.path.isdir(resolved) or os.path.dirname(resolved) == resolved:+ raise WorkdirUnsafeError("the session workdir is not a safe canonical directory")+ for mount in _host_mount_points():+ if mount != resolved and _within(mount, resolved):+ raise WorkdirUnsafeError(f"the session workdir contains a nested host mount: {mount}")+ scan_workdir_for_host_channels(resolved)+ return resolved+++def _runtime_read_paths(+ workdir: str,+ system_roots: tuple[str, ...],+ alias: str | None = None,+) -> tuple[str, ...]:+ """Find runtime roots from the interpreter, not inherited ``sys.path``.++ Exclude both workdir spellings before resolving candidates. Otherwise a+ venv under *alias* could expose an external symlink target such as ~/.ssh.+ """+ # All four: for a uv-managed interpreter base_prefix and base_exec_prefix are+ # different spellings, and lib-dynload hangs off the alias one alone.+ prefixes = (sys.prefix, sys.base_prefix, sys.exec_prefix, sys.base_exec_prefix)+ candidates: list[str] = [+ os.path.join(os.path.dirname(os.path.abspath(__file__)), "sandbox_site"),+ ]+ # Subdirectories, never the prefix itself: ``python -m venv .`` at a project+ # root makes sys.prefix the project root. "ssl" and "libexec" are for a Conda+ # or Homebrew git-remote-https; "include" is Python.h for a source build.+ for prefix in prefixes:+ candidates.extend(+ os.path.join(prefix, name)+ for name in ("bin", "include", "lib", "lib64", "libexec", "pyvenv.cfg", "ssl")+ )+ try:+ paths = sysconfig.get_paths()+ candidates.extend(+ paths[key]+ for key in ("stdlib", "platstdlib", "purelib", "platlib", "include", "platinclude")+ if paths.get(key)+ )+ except (KeyError, OSError):+ pass+ # An editable install keeps its code outside site-packages, so without this a+ # sandboxed `import unsloth` fails where the same environment imported it a+ # moment earlier. Added as CANDIDATES, so the workdir exclusion, the+ # filesystem-root refusal and the dual-spelling handling below all apply.+ candidates.extend(editable_source_roots())+ # Keep argv[0]'s spelling; bind the file, since its parent may be the user's home.+ # Last so an existing prefix/bin bind covers ordinary venv, conda and uv layouts.+ candidates.append(sys.executable)+ try:+ candidates.extend(site.getsitepackages())+ except AttributeError:+ pass # a stripped virtualenv without the real site module+ multiarch = sysconfig.get_config_var("MULTIARCH")+ if multiarch:+ candidates.extend(os.path.join(prefix, "lib", multiarch) for prefix in prefixes)++ selected: list[str] = []+ for candidate in candidates:+ if not candidate or not os.path.isabs(candidate):+ continue+ # On the candidate as WRITTEN: checking only the resolved form would skip+ # <workdir>/venv/lib and then bind the ~/.ssh it was symlinked to. Both+ # spellings of the workdir, since sys.prefix may carry either.+ written = os.path.abspath(candidate)+ if any(_within(written, root) for root in (workdir, alias) if root):+ continue+ # Both spellings: a venv reached through a symlink needs the link's own+ # path bound as well as the directory it lands on.+ for path in (os.path.abspath(candidate), os.path.realpath(candidate)):+ if path == os.path.sep:+ raise SandboxUnavailableError(+ f"this interpreter would require binding the filesystem root: {candidate}"+ )+ if not os.path.exists(path):+ continue+ if any(_within(path, root) for root in (*system_roots, *selected)):+ continue+ selected.append(path)+ return tuple(selected)+++def _identity_files() -> tuple[str, str, str]:+ """Synthesise passwd and group so getpwuid() works without the host account+ database. One entry, this uid, no home."""+ directory = tempfile.mkdtemp(prefix = "unsloth-sandbox-identity-")+ uid, gid = os.getuid(), os.getgid()+ passwd, group = os.path.join(directory, "passwd"), os.path.join(directory, "group")+ try:+ with open(passwd, "w", encoding = "utf-8") as stream:+ stream.write(f"studio:x:{uid}:{gid}:Studio sandbox:/nonexistent:/bin/sh\n")+ with open(group, "w", encoding = "utf-8") as stream:+ stream.write(f"studio:x:{gid}:\n")+ os.chmod(passwd, 0o600)+ os.chmod(group, 0o600)+ except Exception:+ shutil.rmtree(directory, ignore_errors = True)+ raise+ return directory, passwd, group+++def _tmpdir(plan: ToolLaunchPlan, workdir: str) -> str:+ """The caller's TMPDIR when it is inside the workdir: the private /tmp dies+ with the mount namespace, dropping what ``tempfile`` wrote. Containment is+ decided on the canonical form, but the ANSWER is the caller's spelling."""+ requested = plan.env.get("TMPDIR") or ""+ if not requested:+ return "/tmp"+ return requested if _within(os.path.realpath(requested), workdir) else "/tmp"+++def _pythonpath(plan: ToolLaunchPlan, packages: str) -> str:+ """Appended, never prepended: an installed package must not shadow tools.py's+ sandbox_site shim."""+ inherited = plan.env.get("PYTHONPATH") or ""+ return os.pathsep.join(part for part in (inherited, packages) if part)+++def _path(plan: ToolLaunchPlan, packages: str) -> str:+ """Caller's PATH plus the package target's ``bin``, LAST: that directory is+ writable by the tool call, so a planted binary must not shadow a bare command+ the approval logic treats as safe."""+ inherited = plan.env.get("PATH") or ""+ return os.pathsep.join(part for part in (inherited, os.path.join(packages, "bin")) if part)+++# Bound the wait: NFS/FUSE can block inside isdir/scandir beyond the scan's deadline.+# Use a thread because this runs in a threaded server; an uninspected cache is not shared.+_CACHE_INSPECT_SECONDS = CACHE_SCAN_SECONDS + 2.0+++def _inspect_cache_component(name: str, path: str) -> "str | None":+ """The hazard for one component, or a reason it could not be inspected."""+ if not os.path.isdir(path):+ return "is not a directory"+ nested = next((m for m in _host_mount_points() if m != path and _within(m, path)), None)+ if nested is not None:+ return f"contains a nested host mount: {nested}"+ return cache_share_hazard(path)+++# Retain timed-out workers until they finish, preventing a thread leak on a wedged path.+_cache_scan_pending: "dict[str, threading.Thread]" = {}+# Request threads must reserve and remove workers atomically.+_cache_scan_lock = threading.Lock()+++def _cache_hazard_within_deadline(name: str, path: str) -> "str | None":+ answer: list[str | None] = []++ def inspect() -> None:+ try:+ answer.append(_inspect_cache_component(name, path))+ except Exception as exc: # noqa: BLE001 - a launch never fails over this+ answer.append(f"could not be inspected: {exc}")++ with _cache_scan_lock:+ pending = _cache_scan_pending.get(path)+ if pending is not None:+ if pending.is_alive():+ return "was still being inspected when a previous launch gave up (a wedged mount?)"+ del _cache_scan_pending[path]+ worker = threading.Thread(target = inspect, name = f"unsloth-cache-scan-{name}", daemon = True)+ # Start under the lock, or another caller can replace the not-yet-alive worker.+ _cache_scan_pending[path] = worker+ worker.start()+ worker.join(_CACHE_INSPECT_SECONDS)+ if not answer:+ return f"could not be inspected within {_CACHE_INSPECT_SECONDS:.0f}s (a wedged mount?)"+ with _cache_scan_lock:+ # By identity: another caller may already have replaced it.+ if _cache_scan_pending.get(path) is worker:+ del _cache_scan_pending[path]+ return answer[0]+++def _model_cache_binds(workdir: str) -> dict[str, str]:+ """Inner cache subdirectory -> the host directory to share there.++ Each component is resolved separately because HF_HUB_CACHE=/mnt/models is NOT+ /mnt/models/hub.+ """+ binds: dict[str, str] = {}+ try:+ from utils.hf_cache_settings import get_hf_cache_paths++ paths = get_hf_cache_paths()+ home = os.path.abspath(str(paths.cache_home))+ resolved = {"hub": str(paths.hub_cache), "xet": str(paths.xet_cache)}+ except Exception: # noqa: BLE001 - a launch never fails over a cache lookup+ logger.debug("could not resolve the configured Hugging Face cache", exc_info = True)+ user_home = os.path.expanduser("~")+ if not os.path.isabs(user_home):+ return binds+ home = os.path.join(user_home, _MODEL_CACHE_RELPATH)+ resolved = {}+ for name in _MODEL_CACHE_SUBDIRS:+ path = os.path.abspath(resolved.get(name) or os.path.join(home, name))+ if _within(path, workdir):+ continue+ # Writable caches need the workdir's host-channel checks, including nested bind mounts.+ hazard = _cache_hazard_within_deadline(name, path)+ if hazard is not None:+ logger.warning("Not sharing the %s cache into the sandbox: it %s", name, hazard)+ continue+ binds[name] = path+ return binds+++def _make_cache_mountpoints(workdir: str, names: tuple[str, ...]) -> None:+ """bwrap would create a missing destination ON THE HOST, following a+ ``.cache`` symlink an earlier call pointed at the user's home, so this walks+ with ``O_NOFOLLOW``. Nothing is removed afterwards, so two overlapping calls+ in one session cannot unlink each other's mount points."""+ levels = _MODEL_CACHE_RELPATH.split(os.sep)+ fds: list[int] = [os.open(workdir, os.O_RDONLY | os.O_DIRECTORY)]+ try:+ for name in (*levels, *names):+ try:+ os.mkdir(name, dir_fd = fds[-1])+ except FileExistsError:+ pass+ except OSError as exc:+ raise WorkdirUnsafeError(+ f"the session workdir's model cache path cannot be prepared: {exc}"+ ) from exc+ # Verified even when not descended into: a leaf left as a file or+ # symlink fails --bind-try after Popen, where auto cannot fall back.+ try:+ opened = os.open(name, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd = fds[-1])+ except OSError as exc:+ raise WorkdirUnsafeError(+ f"the session workdir's model cache path is not a plain directory: {name}"+ ) from exc+ if name in levels:+ fds.append(opened)+ else:+ os.close(opened)+ finally:+ for fd in fds:+ try:+ os.close(fd)+ except OSError:+ pass+++def prepare(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:+ bwrap = shutil.which("bwrap")+ if bwrap is None:+ raise SandboxUnavailableError("bubblewrap (bwrap) is not installed on this host")+ if not plan.argv:+ raise SandboxUnavailableError("a sandboxed launch needs a command to run")+ workdir = _validate_workdir(plan.workdir)+ # The spelling the CALLER used, since tools.py built the scratch script path+ # from it. The canonical form stays the bind SOURCE and what is checked.+ inner = os.path.abspath(plan.workdir)+ system_roots = tuple(path for path in _SYSTEM_ROOTS if os.path.isdir(path))+ if os.path.isdir(_NIX_STORE) and _within(os.path.realpath(sys.executable), _NIX_STORE):+ system_roots += (_NIX_STORE,)+ runtime_paths = _runtime_read_paths(workdir, system_roots, inner)+ model_cache = _model_cache_binds(workdir)+ # A runtime under /tmp has to be restored after the tmpfs replaces it.+ tmp_runtime_paths = tuple(path for path in runtime_paths if _within(path, "/tmp"))+ workdir_runtime_paths = _runtime_paths_under(workdir)++ disable_userns = _bwrap_supports(bwrap, "--disable-userns")+ try:+ seccomp = sandbox_seccomp.filter_file(block_userns = not disable_userns)+ except RuntimeError as exc:+ raise SandboxUnavailableError(str(exc)) from exc+ try:+ identity_dir, passwd, group = _identity_files()+ except Exception:+ seccomp.close()+ raise+ # Owned by a PreparedSandboxLaunch that does not exist yet.+ try:+ argv: list[str] = [+ bwrap,+ "--die-with-parent",+ "--new-session",+ # Every namespace except the network one: tool calls download models.+ "--unshare-user",+ "--unshare-pid",+ "--unshare-ipc",+ "--unshare-uts",+ "--unshare-cgroup",+ # bwrap 0.6.1 (Ubuntu 22.04) predates this; the seccomp filter+ # refuses nested user namespaces there instead.+ *(("--disable-userns",) if disable_userns else ()),+ "--cap-drop",+ "ALL",+ "--seccomp",+ str(seccomp.fileno()),+ "--proc",+ "/proc",+ "--dev",+ "/dev",+ "--dir",+ "/dev/shm",+ "--dir",+ "/tmp",+ "--dir",+ "/etc",+ ]+ for root in system_roots:+ argv += ["--ro-bind-try", root, root]+ trusted = tuple(p for p in _ETC_FILES_IF_TRUSTED if _trusted_system_file(p))+ for path in (*_ETC_FILES, *trusted, *_NETWORK_FILES):+ argv += ["--ro-bind-try", path, path]+ argv += ["--ro-bind", passwd, "/etc/passwd", "--ro-bind", group, "/etc/group"]+ for path in runtime_paths:+ if path not in tmp_runtime_paths:+ argv += ["--ro-bind", path, path]+ # Mount points must exist before the root goes read-only. Both spellings+ # need one, since bwrap cannot create either once / is read-only.+ argv += ["--dir", workdir]+ if inner != workdir:+ argv += ["--dir", inner]+ argv += ["--remount-ro", "/"]+ argv += ["--tmpfs", "/dev/shm", "--tmpfs", "/tmp"]+ for path in tmp_runtime_paths:+ argv += ["--ro-bind", path, path]+ argv += ["--bind", workdir, inner]+ if inner != workdir:+ argv += ["--bind", workdir, workdir]+ # Protect both runtime spellings after both writable binds, so neither+ # hides the protection or leaves its mount point missing.+ for path in workdir_runtime_paths:+ argv += ["--ro-bind", path, path]+ if inner != workdir:+ argv += ["--ro-bind", path, inner + path[len(workdir) :]]+ argv += ["--chdir", inner]+ if model_cache:+ inner_cache = os.path.join(inner, _MODEL_CACHE_RELPATH)+ _make_cache_mountpoints(inner, tuple(model_cache))+ for name, host_path in model_cache.items():+ # --bind-try: it existed when this was resolved, and a failed+ # bind lands after Popen where auto has no fallback left.+ argv += ["--bind-try", host_path, os.path.join(inner_cache, name)]+ argv += ["--setenv", "HF_HOME", inner_cache]+ packages = os.path.join(inner, SESSION_PACKAGES_RELPATH)+ argv += [+ "--setenv",+ "HOME",+ inner,+ "--setenv",+ "TMPDIR",+ _tmpdir(plan, workdir),+ "--setenv",+ "PIP_TARGET",+ packages,+ "--setenv",+ "PYTHONPATH",+ _pythonpath(plan, packages),+ "--setenv",+ "PATH",+ _path(plan, packages),+ "--",+ ]+ argv += list(plan.argv)++ return PreparedSandboxLaunch(+ argv = tuple(argv),+ workdir = workdir,+ env = dict(plan.env),+ # --new-session covers the inner side only; tools.py kills with+ # killpg, so the plan's outer setsid still has to run.+ preexec_fn = sandbox_landlock.with_abstract_scope(plan.preexec_fn),+ backend = BACKEND_NAME,+ pass_fds = (seccomp.fileno(),),+ owned_files = [seccomp],+ cleanup_paths = [identity_dir],+ timeout_seconds = plan.timeout_seconds,+ close_fds = plan.close_fds,+ terminate_descendants = plan.terminate_descendants,+ )+ except Exception:+ seccomp.close()+ shutil.rmtree(identity_dir, ignore_errors = True)+ raise+++def probe_argv(+ workdir: str,+ payload_argv: tuple[str, ...],+ env: dict[str, str] | None = None,+) -> PreparedSandboxLaunch:+ """Built through the argv builder a real tool call uses, so the probe cannot+ qualify a sandbox nothing ever runs."""+ return prepare(+ ToolLaunchPlan(+ argv = tuple(payload_argv),+ workdir = workdir,+ env = dict(env or {}),+ requested_mode = "required",+ )+ )diff --git a/studio/backend/core/inference/sandbox_macos.py b/studio/backend/core/inference/sandbox_macos.pynew file mode 100644index 00000000000..2585ddafb38--- /dev/null+++ b/studio/backend/core/inference/sandbox_macos.py@@ -0,0 +1,746 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Default-deny Seatbelt profiles for sandbox-exec.++IP egress is unrestricted. AF_UNIX needs separate rules because file rules do+not block socket connections. SBPL grammar is tested on Darwin.+"""++from __future__ import annotations+import json+import os+import posixpath+import shutil+import site+import stat+import subprocess+import sys+import sysconfig+import tempfile+import threading++from .os_sandbox import (+ PROFILE_VERSION,+ SESSION_PACKAGES_RELPATH,+ PreparedSandboxLaunch,+ SandboxUnavailableError,+ ToolLaunchPlan,+ WorkdirUnsafeError,+ editable_import_roots,+ editable_source_roots,+ scan_workdir_for_host_channels,+)++BACKEND_NAME = "macos-seatbelt"+PROFILE_ID = f"macos-seatbelt-{PROFILE_VERSION}"+LIMITATIONS = (+ "deprecated_undocumented_sbpl",+ "unrestricted_network",+ # No /proc and no pidfds, so a descendant outliving the leader is not swept.+ "detached_descendant_cleanup_unverified",+ "pytorch_posix_shm_namespace_shared",+ # Unfiltered: a wrong guess at torch's and OpenMP's names breaks them.+ "posix_semaphore_namespace_shared",+ # kern.proc.pid./pgrp. leak host kinfo_proc, but that sysctl is how ps works.+ "host_process_metadata_readable",+ # Only RootDomainUserClient: the user clients Metal needs are too large an+ # authority to grant untested, so MPS cannot initialise.+ "gpu_devices_hidden",+)++SANDBOX_EXEC = "/usr/bin/sandbox-exec"++_REJECTED_CONTROLS = frozenset(chr(code) for code in (*range(0x20), 0x7F))+++_READ_ROOTS = (+ "/Library/Apple/System/Library/Frameworks",+ "/Library/Apple/System/Library/PrivateFrameworks",+ "/Library/Apple/usr/lib",+ "/System/Library/Frameworks",+ "/System/Library/PrivateFrameworks",+ "/System/Library/SubFrameworks",+ "/System/iOSSupport/System/Library/Frameworks",+ "/System/iOSSupport/System/Library/PrivateFrameworks",+ "/System/iOSSupport/System/Library/SubFrameworks",+ "/usr/lib",+ "/usr/share",+ "/bin",+ "/usr/bin",+ "/private/var/db/timezone",+ "/private/etc/localtime",+ "/private/etc/master.passwd",+ "/private/etc/passwd",+ "/private/etc/protocols",+ "/private/etc/services",+ "/private/etc/hosts",+ "/private/etc/resolv.conf",+ "/System/Library/CoreServices/.SystemVersionPlatform.plist",+ "/System/Library/CoreServices/SystemVersion.plist",+ # /usr/bin/git and friends are xcode-select shims and fail without these.+ "/private/var/db/xcode_select_link",+ "/Library/Developer/CommandLineTools",+ "/Applications/Xcode.app/Contents/Developer",+)+# SYSTEM keychains only; the login keychain stays unreadable.+_TLS_TRUST_PATHS = (+ # Allow public TLS components individually; /etc/ssl can also hold private keys.+ "/private/etc/ssl/cert.pem",+ "/private/etc/ssl/certs",+ "/private/etc/ssl/openssl.cnf",+ "/System/Library/Keychains",+ "/Library/Keychains",+ "/System/Library/Security",+ "/private/var/db/mds",+ "/Library/Preferences/com.apple.security.plist",+ "/Library/Preferences/com.apple.security.revocation.plist",+)+_OPTIONAL_READ_ROOTS = (+ "/usr/local/bin",+ "/usr/local/lib",+ "/usr/local/sbin",+ "/usr/local/opt",+ "/usr/local/Cellar",+ "/opt/homebrew/bin",+ "/opt/homebrew/lib",+ "/opt/homebrew/sbin",+ "/opt/homebrew/opt",+ "/opt/homebrew/Cellar",+)+# Homebrew prefixes are user-owned; check target containment rather than ownership.+_OPTIONAL_ROOT_PREFIXES = ("/usr/local", "/opt/homebrew")+# Allow missing optional files so git gets ENOENT, not a fatal EPERM.+# Existence-filtered rules would omit these paths.+_OPTIONAL_READ_LITERALS = (+ "/etc/gitconfig",+ "/etc/gitattributes",+ # A Homebrew git reads its own prefix, and the binary on PATH decides which.+ "/usr/local/etc/gitconfig",+ "/usr/local/etc/gitattributes",+ "/opt/homebrew/etc/gitconfig",+ "/opt/homebrew/etc/gitattributes",+ "/Library/Preferences/com.apple.dt.Xcode.plist",+)+# Exec-denied: each hands work to a daemon outside the sandbox.+_DENIED_EXECUTABLES = (+ "/usr/bin/open",+ "/usr/bin/osascript",+ "/usr/bin/security",+ "/bin/launchctl",+ "/usr/bin/sandbox-exec",+)+_DEVICES = ("/dev/null", "/dev/zero", "/dev/random", "/dev/urandom")+# connect() to a unix socket is network-outbound, not a file operation.+_MDNSRESPONDER_SOCKET = "/private/var/run/mDNSResponder"+# com.apple.SecurityServer is deliberately ABSENT: with it the login Keychain is+# readable through Security.framework despite /usr/bin/security being exec-denied.+_MACH_SERVICES = (+ "com.apple.system.opendirectoryd.libinfo",+ "com.apple.PowerManagement.control",+ "com.apple.trustd",+ "com.apple.trustd.agent",+ "com.apple.ocspd",+ "com.apple.dnssd.service",+ "com.apple.networkd",+ "com.apple.bsd.dirhelper",+ "com.apple.SystemConfiguration.configd",+ "com.apple.SystemConfiguration.DNSConfiguration",+)+# An omission is a silent breakage (empty os.cpu_count()), so be generous.+_SYSCTL_NAMES = (+ "hw.activecpu",+ "hw.busfrequency_compat",+ "hw.byteorder",+ "hw.cacheconfig",+ "hw.cachelinesize_compat",+ "hw.cpufamily",+ "hw.cpufrequency_compat",+ "hw.cputype",+ "hw.l1dcachesize_compat",+ "hw.l1icachesize_compat",+ "hw.l2cachesize_compat",+ "hw.l3cachesize_compat",+ "hw.logicalcpu",+ "hw.logicalcpu_max",+ "hw.machine",+ "hw.memsize",+ "hw.model",+ "hw.ncpu",+ "hw.nperflevels",+ "hw.packages",+ "hw.pagesize",+ "hw.pagesize_compat",+ "hw.physicalcpu",+ "hw.physicalcpu_max",+ "hw.tbfrequency_compat",+ "hw.vectorunit",+ "kern.argmax",+ "kern.hostname",+ "kern.maxfilesperproc",+ "kern.maxproc",+ "kern.osproductversion",+ "kern.osrelease",+ "kern.ostype",+ "kern.osvariant_status",+ "kern.osversion",+ "kern.secure_kernel",+ "kern.sysv.semmns",+ "kern.usrstack64",+ "kern.version",+ "machdep.cpu.brand_string",+ "sysctl.proc_cputype",+ "vm.loadavg",+)+_SYSCTL_PREFIXES = (+ "hw.optional.arm.",+ "hw.optional.armv8_",+ "hw.perflevel",+ "kern.proc.pgrp.",+ "kern.proc.pid.",+)+# fmt: on++_developer_paths_cache: tuple[str, ...] | None = None+_developer_paths_lock = threading.Lock()+++def available() -> tuple[bool, str]:+ """Presence and executability only; whether a profile loads is the probe's."""+ try:+ info = os.stat(SANDBOX_EXEC, follow_symlinks = False)+ except OSError as exc:+ return False, f"the system Seatbelt launcher is unavailable: {exc}"+ if (+ not stat.S_ISREG(info.st_mode)+ or info.st_uid != 0+ or info.st_mode & (stat.S_IWGRP | stat.S_IWOTH)+ ):+ return False, f"{SANDBOX_EXEC} is not a root-owned, non-user-writable regular file"+ if not os.access(SANDBOX_EXEC, os.X_OK):+ return False, f"{SANDBOX_EXEC} is not executable"+ return True, "the system Seatbelt launcher is present and executable"+++def _sbpl_string(value: str) -> str:+ r"""An SBPL string literal, non-ASCII left RAW: SBPL is TinyScheme, which knows+ \", \n, \r, \t and \xDD and no \u, so json's default turned /Users/José into a+ rule matching nothing and every macOS home with an accent lost isolation+ silently. The profile is an argv string, so the raw character arrives as the+ same UTF-8 bytes the path has."""+ return json.dumps(value, ensure_ascii = False)+++def _validated(path: str) -> str:+ if not path or not posixpath.isabs(path) or any(c in path for c in _REJECTED_CONTROLS):+ # TinyScheme cannot interpret JSON's \b, \f or \u00XX control escapes.+ raise SandboxUnavailableError(+ f"Seatbelt paths must be absolute and free of control characters: {path!r}"+ )+ try:+ # Reject surrogateescaped bytes before encoding the profile into argv.+ path.encode("utf-8")+ except UnicodeEncodeError as exc:+ raise SandboxUnavailableError(+ f"Seatbelt paths must be encodable as UTF-8: {path!r}"+ ) from exc+ return path+++def _within(path: str, root: str) -> bool:+ """Whether ``path`` is ``root`` or sits under it, by whole path components."""+ return path == root or path.startswith(root.rstrip("/") + "/")+++def _rule(operations: str, filters: list[str]) -> str:+ """A filterless rule is UNCONDITIONAL, so ``(allow file-write* )`` grants the+ whole filesystem. Every rule meant to be unconditional is written literally,+ so reaching here empty always means a list collapsed."""+ if not filters:+ raise SandboxUnavailableError(+ f"the Seatbelt profile would apply {operations!r} unconditionally: no paths survived"+ )+ return f"({operations} " + " ".join(filters) + ")"+++def _sbpl_spellings(path: str, *, resolve: bool = True) -> tuple[str, ...]:+ """Include both /private aliases even when the symlink is absent.++ Seatbelt needs both spellings. ``resolve = False`` excludes symlink targets,+ preventing an /etc/gitconfig link from granting access to home files.+ """+ selected = [posixpath.abspath(_validated(path))]+ if resolve:+ # Validated: a newline in a symlink target would end up inside a quoted string.+ canonical = _validated(os.path.realpath(path))+ if canonical not in selected:+ selected.append(canonical)+ for spelling in tuple(selected):+ for short in ("/etc", "/tmp", "/var"):+ if spelling == short or spelling.startswith(short + "/"):+ selected.append("/private" + spelling)+ private = "/private" + short+ if spelling == private or spelling.startswith(private + "/"):+ selected.append(spelling[len("/private") :])+ return tuple(dict.fromkeys(selected))+++def _path_filters(paths: tuple[str, ...]) -> list[str]:+ """``(literal ...)`` per spelling, plus ``(subpath ...)`` for directories."""+ filters: list[str] = []+ # Keyed by kind: a file and a directory can share a resolved spelling, and+ # one set would let the file's literal suppress the directory's subpath.+ seen: set[tuple[str, str]] = set()+ for path in paths:+ if not os.path.exists(path):+ continue+ kinds = ("literal", "subpath") if os.path.isdir(path) else ("literal",)+ for spelling in _sbpl_spellings(path):+ encoded = _sbpl_string(spelling)+ for kind in kinds:+ if (kind, encoded) not in seen:+ seen.add((kind, encoded))+ filters.append(f"({kind} {encoded})")+ return filters+++def _literal_filters(paths: tuple[str, ...], *, resolve: bool = True) -> list[str]:+ """Existence not required: for a deny, dropping the rule is the wrong way to fail."""+ filters: list[str] = []+ seen: set[str] = set()+ for path in paths:+ for spelling in _sbpl_spellings(path, resolve = resolve):+ encoded = _sbpl_string(spelling)+ if encoded not in seen:+ seen.add(encoded)+ filters.append(f"(literal {encoded})")+ return filters+++def _ancestor_filters(spellings: tuple[str, ...]) -> list[str]:+ """Allow ancestor metadata for path resolution, without directory listing.++ Accept caller-resolved spellings; use posixpath for Windows-hosted tests.+ """+ filters: list[str] = []+ seen: set[str] = set()+ for spelling in spellings:+ current = posixpath.dirname(_validated(spelling))+ while current:+ encoded = _sbpl_string(current)+ if encoded not in seen:+ seen.add(encoded)+ filters.append(f"(literal {encoded})")+ parent = posixpath.dirname(current)+ if parent == current:+ break+ current = parent+ return filters+++def _trusted_system_dir(path: str) -> bool:+ """Require a root-owned directory writable only by root before granting reads."""+ try:+ info = os.stat(path)+ except OSError:+ return False+ if not stat.S_ISDIR(info.st_mode):+ return False+ return info.st_uid == 0 and not info.st_mode & (stat.S_IWGRP | stat.S_IWOTH)+++def _developer_paths() -> tuple[str, ...]:+ """Versioned on many hosts (/Applications/Xcode_16.4.app), so the static list+ above cannot name it."""+ global _developer_paths_cache+ with _developer_paths_lock:+ if _developer_paths_cache is not None:+ return _developer_paths_cache+ found: list[str] = []+ if sys.platform == "darwin" and os.path.exists("/usr/bin/xcode-select"):+ try:+ # DEVELOPER_DIR could redirect this recursive grant into the user's home.+ environment = {k: v for k, v in os.environ.items() if k != "DEVELOPER_DIR"}+ result = subprocess.run(+ ["/usr/bin/xcode-select", "-p"],+ capture_output = True,+ text = True,+ encoding = "utf-8",+ timeout = 10,+ check = False,+ env = environment,+ )+ candidate = result.stdout.strip()+ if result.returncode == 0 and candidate and _trusted_system_dir(candidate):+ for spelling in (os.path.realpath(candidate), candidate):+ if spelling not in found:+ found.append(spelling)+ # xcselect validates against the enclosing app bundle.+ marker = ".app/Contents/Developer"+ for spelling in list(found):+ if spelling.endswith(marker):+ bundle = spelling[: -len(marker) + len(".app")]+ if os.path.isdir(bundle) and bundle not in found:+ found.append(bundle)+ except (OSError, subprocess.SubprocessError):+ pass+ _developer_paths_cache = tuple(found)+ return _developer_paths_cache+++def runtime_read_paths(workdir: str | None = None) -> tuple[str, ...]:+ """Exclude candidates under either workdir spelling before resolving them.++ Otherwise a venv/lib symlink could grant reads of an external target such as+ ~/.ssh. sys.prefix may retain either spelling, independently of the caller.+ """+ candidates: list[str] = [+ os.path.join(os.path.dirname(os.path.abspath(__file__)), "sandbox_site"),+ ]+ # Subdirectories, never the prefix: ``python -m venv .`` at a project root+ # makes sys.prefix the project root, and the network is open, so a readable+ # .env is an exportable one. All four prefixes, because for a uv-managed+ # interpreter base_prefix and base_exec_prefix differ and lib-dynload hangs+ # off the alias spelling.+ for prefix in (sys.prefix, sys.base_prefix, sys.exec_prefix, sys.base_exec_prefix):+ candidates.extend(+ posixpath.join(prefix, name)+ for name in ("bin", "include", "lib", "lib64", "libexec", "pyvenv.cfg", "ssl")+ )+ # A python.org framework build loads its dyld image from <prefix>/Python, a+ # FILE at the top of the prefix and under no read root.+ candidates.append(posixpath.join(prefix, "Python"))+ try:+ paths = sysconfig.get_paths()+ candidates.extend(+ paths[key]+ for key in ("stdlib", "platstdlib", "purelib", "platlib", "include", "platinclude")+ if paths.get(key)+ )+ except (KeyError, OSError):+ pass+ # Same as the Linux backend: an editable install's source root is outside+ # site-packages, and a candidate here inherits every guard below.+ candidates.extend(editable_source_roots())+ # Keep argv[0]'s spelling without granting its possibly private parent directory.+ # Last so existing prefix/bin grants cover ordinary venv, conda and uv layouts.+ candidates.append(sys.executable)+ try:+ candidates.extend(site.getsitepackages())+ except AttributeError:+ pass # some virtualenv layouts omit getsitepackages; sysconfig covers them++ selected: list[str] = []+ for candidate in candidates:+ if not candidate or not posixpath.isabs(candidate):+ continue+ written = posixpath.abspath(candidate)+ if workdir is not None and any(+ _within(written, root) for root in (workdir, os.path.realpath(workdir))+ ):+ continue+ # Both spellings: a venv reached through a symlink needs the link's own path+ # as well as the directory it lands on.+ for path in (posixpath.abspath(candidate), os.path.realpath(candidate)):+ # "/" or "/usr" as a read ROOT hands back most of the host.+ if path in ("/", "/usr") or not os.path.exists(path):+ continue+ if path not in selected and not any(_within(path, root) for root in selected):+ selected.append(path)+ return tuple(selected)+++def _contained_optional_roots() -> tuple[str, ...]:+ """Optional search roots whose target stays inside an approved prefix.++ Dropped rather than un-resolved: the whole point of a search root is that+ Homebrew's /usr/local/bin entries are symlinks into ../Cellar, so refusing to+ follow them would grant a directory of dangling names.+ """+ kept: list[str] = []+ for root in _OPTIONAL_READ_ROOTS:+ resolved = os.path.realpath(root)+ if any(+ _within(root, prefix) and _within(resolved, prefix)+ for prefix in _OPTIONAL_ROOT_PREFIXES+ ):+ kept.append(root)+ return tuple(kept)+++def runtime_paths_under(workdir: str) -> tuple[str, ...]:+ """Protect Studio's runtime under every writable spelling of the workdir.++ Resolved containment handles aliases in sys.prefix. External targets stay ungranted.+ """+ roots: list[str] = []+ for root in (posixpath.abspath(workdir), os.path.realpath(workdir)):+ if root not in roots:+ roots.append(root)+ canonical_root = os.path.realpath(workdir)+ inside: list[str] = []+ # Include standalone Python and the framework's top-level dyld image.+ candidates = [sys.executable, *editable_source_roots()]+ for prefix in (sys.prefix, sys.base_prefix, sys.exec_prefix, sys.base_exec_prefix):+ candidates.extend(+ posixpath.join(prefix, name)+ for name in ("bin", "include", "lib", "lib64", "libexec", "pyvenv.cfg", "ssl", "Python")+ )+ for candidate in candidates:+ if not os.path.exists(candidate):+ continue+ resolved = os.path.realpath(candidate)+ if not _within(resolved, canonical_root):+ if candidate is sys.executable and _within(+ posixpath.abspath(candidate), canonical_root+ ):+ # A replaceable interpreter alias would let the next host probe execute tool code.+ raise WorkdirUnsafeError(+ "the session workdir holds a link to the Python that runs Studio: "+ f"{posixpath.abspath(candidate)}"+ )+ continue+ # Protect the alias name as well as its target, or the tool can replace the alias.+ written = posixpath.abspath(candidate)+ for form in (resolved, written):+ if not _within(form, canonical_root):+ continue+ relative = posixpath.relpath(form, canonical_root)+ for other in roots:+ spelling = posixpath.join(other, relative)+ if spelling not in inside:+ inside.append(spelling)+ return tuple(inside)+++def build_profile(+ *,+ workdir: str,+ private_tmp: str,+ runtime_paths: tuple[str, ...],+ developer_paths: tuple[str, ...] = (),+) -> str:+ """Writes and spawns nothing, so a test can assert on the text anywhere."""+ readable_paths = (+ *_READ_ROOTS,+ *_TLS_TRUST_PATHS,+ *_contained_optional_roots(),+ *developer_paths,+ *_DEVICES,+ *runtime_paths,+ workdir,+ private_tmp,+ )+ read_filters = ['(literal "/")', *_path_filters(readable_paths)]+ # The optional literals may not exist but their parents still need stat, and+ # so does mDNSResponder's socket, resolved before the network rule applies.+ metadata_filters = _ancestor_filters(+ tuple(+ spelling+ for path in readable_paths + (_MDNSRESPONDER_SOCKET,)+ for spelling in _sbpl_spellings(path)+ )+ + tuple(+ spelling+ for path in _OPTIONAL_READ_LITERALS+ for spelling in _sbpl_spellings(path, resolve = False)+ )+ )+ write_filters = _path_filters((workdir, private_tmp))+ device_filters = _path_filters(_DEVICES)+ # The workdir too, since TMPDIR points into it and an AF_UNIX bind is+ # network-bind, not a file operation.+ tmp_subpaths = tuple(+ f"(subpath {_sbpl_string(spelling)})"+ for path in (private_tmp, workdir)+ for spelling in _sbpl_spellings(path)+ )+ mdns_filters = _literal_filters((_MDNSRESPONDER_SOCKET,))+ # resolve = False so an /etc/gitconfig symlinked into the home does not turn+ # a config read allowance into a home one.+ # The editable import roots ride here rather than in read_filters because a+ # literal grants the directory itself, which is all a listing needs, while+ # _path_filters would add the subpath and hand back the whole checkout.+ optional_filters = _literal_filters(+ _OPTIONAL_READ_LITERALS + editable_import_roots(), resolve = False+ )+ sysctl_filters = [+ *(f"(sysctl-name {_sbpl_string(name)})" for name in _SYSCTL_NAMES),+ *(f"(sysctl-name-prefix {_sbpl_string(name)})" for name in _SYSCTL_PREFIXES),+ ]+ lines = [+ "(version 1)",+ "(deny default)",+ # Scoped to the same Seatbelt instance, so a tool cannot poke at Studio.+ "(allow process-fork)",+ "(allow process-exec)",+ "(allow signal (target same-sandbox))",+ "(allow process-info* (target same-sandbox))",+ _rule("deny process-exec", _literal_filters(_DENIED_EXECUTABLES)),+ _rule("allow file-read-metadata", metadata_filters),+ _rule("allow file-read* file-test-existence", read_filters),+ _rule("allow file-read* file-test-existence", optional_filters),+ _rule("allow file-map-executable", read_filters),+ _rule("allow file-write*", write_filters),+ # AFTER the allowance, because Seatbelt is last-match-wins: Studio's own+ # runtime stays read-only even when it lives under the writable workdir.+ # See runtime_paths_under.+ *(+ [_rule("deny file-write*", _path_filters(runtime_under))]+ if (runtime_under := runtime_paths_under(workdir))+ else []+ ),+ _rule("allow file-read* file-test-existence file-write-data", device_filters),+ # bash process substitution hands the child /dev/fd/63, and opening it dups+ # a descriptor already held.+ '(allow file-read* (regex #"^/dev/fd/[0-9]+$"))',+ '(allow file-write* (regex #"^/dev/fd/[0-9]+$"))',+ _rule("allow file-ioctl", device_filters),+ "(allow ipc-posix-sem)",+ # write-data, here and below: libomp writes its registration into the segment+ # and OpenMP init fails without it. The probe loads no OpenMP.+ "(allow ipc-posix-shm-read-data ipc-posix-shm-write-create "+ "ipc-posix-shm-write-data ipc-posix-shm-write-unlink "+ '(ipc-posix-name-regex #"^/__KMP_REGISTERED_LIB_[0-9]+$"))',+ "(allow ipc-posix-shm-read-data ipc-posix-shm-write-create "+ "ipc-posix-shm-write-data ipc-posix-shm-write-unlink "+ '(ipc-posix-name-regex #"^/torch_[0-9]+_[0-9]+_[0-9]+$"))',+ # SharedMemory names its segment "/psm_" + token_hex(4), which the probe,+ # passing only descriptors, misses.+ "(allow ipc-posix-shm-read-data ipc-posix-shm-write-create "+ "ipc-posix-shm-write-data ipc-posix-shm-write-unlink "+ '(ipc-posix-name-regex #"^/psm_[0-9a-f]+$"))',+ # Gates socket() itself, unfiltered because a configd-backed resolver creates+ # route and AF_SYSTEM sockets. That admits raw and kernel-control sockets:+ # authority over the network, never over the filesystem.+ "(allow system-socket)",+ # IP only: an unfiltered bind covers AF_UNIX, creating a socket at a path no+ # file rule governs.+ '(allow network-bind (local ip "*:*"))',+ "(allow network-inbound)",+ # Filtered to ip because an unfiltered outbound covers AF_UNIX, and a+ # connect() to /var/run/docker.sock starts an unconfined container with host+ # bind mounts. "*:*" still leaves TCP and UDP, v4 and v6, open.+ '(allow network-outbound (remote ip "*:*"))',+ # The AF_UNIX destinations the ip filter no longer covers.+ # Each endpoint takes one path filter; combining paths breaks multiprocessing on macOS 15.+ *(f"(allow network-bind (local unix-socket {path}))" for path in tmp_subpaths),+ *(f"(allow network-outbound (remote unix-socket {path}))" for path in tmp_subpaths),+ # Both filter forms are accepted and which one a macOS release matches is+ # untestable here, while a missed DNS socket looks like a resolver bug.+ _rule("allow network-outbound", mdns_filters),+ *(f"(allow network-outbound (remote unix-socket {path}))" for path in mdns_filters),+ _rule("allow sysctl-read", sysctl_filters),+ '(allow iokit-open (iokit-registry-entry-class "RootDomainUserClient"))',+ "(allow mach-lookup\n"+ + "\n".join(f" (global-name {_sbpl_string(name)})" for name in _MACH_SERVICES)+ + ")",+ ]+ return "\n".join(lines) + "\n"+++def _tmpdir(env: dict[str, str], workdir: str, private_tmp: str) -> str:+ """The caller's TMPDIR when it is inside the workdir: ``private_tmp`` is+ deleted when the call ends, dropping what ``tempfile`` wrote."""+ requested = env.get("TMPDIR") or ""+ if requested and _within(posixpath.abspath(requested), workdir):+ return requested+ return private_tmp+++def _sandbox_environment(env: dict[str, str], workdir: str, private_tmp: str) -> dict[str, str]:+ """HOME and TMPDIR must point inside the sandbox: the real HOME is unreadable+ and the per-user /var/folders tmp unwritable. DYLD_* is the dynamic-linker+ injection surface."""+ sanitized = {+ key: value+ for key, value in env.items()+ if not key.startswith("DYLD_")+ and key+ not in {+ "DISPLAY",+ "SSH_AUTH_SOCK",+ "XPC_SERVICE_NAME",+ "WAYLAND_DISPLAY",+ "PULSE_SERVER",+ }+ }+ tmpdir = _tmpdir(env, workdir, private_tmp)+ # Appended, so an installed package cannot shadow the sandbox_site shim.+ packages = posixpath.join(workdir, SESSION_PACKAGES_RELPATH)+ sanitized.update(+ {+ "HOME": workdir,+ "TMPDIR": tmpdir,+ "TMP": tmpdir,+ "TEMP": tmpdir,+ "XDG_RUNTIME_DIR": private_tmp,+ "PIP_TARGET": packages,+ # <target>/bin holds pip's console entry points. LAST, since it is writable+ # by the tool call and a planted binary must not shadow a bare command the+ # approval logic treats as safe.+ "PATH": os.pathsep.join(+ part for part in (env.get("PATH") or "", posixpath.join(packages, "bin")) if part+ ),+ "PYTHONPATH": os.pathsep.join(+ part for part in (env.get("PYTHONPATH") or "", packages) if part+ ),+ }+ )+ developer_paths = _developer_paths()+ if developer_paths and "DEVELOPER_DIR" not in sanitized:+ # xcselect honours DEVELOPER_DIR before the xcode_select link.+ sanitized["DEVELOPER_DIR"] = developer_paths[0]+ return sanitized+++def prepare(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:+ ok, reason = available()+ if not ok:+ # argv[0] is about to be the launcher; a user-writable one must not reach a+ # Popen even if the probe should have caught it.+ raise SandboxUnavailableError(reason)+ try:+ workdir = _validated(os.path.abspath(plan.workdir))+ except SandboxUnavailableError as exc:+ # Workdir refusals must not select auto's unisolated fallback.+ raise WorkdirUnsafeError(str(exc)) from exc+ if not os.path.isdir(workdir):+ raise WorkdirUnsafeError(f"the session workdir does not exist: {workdir}")+ if posixpath.dirname(workdir) == workdir:+ # "/" would make the entire filesystem the writable set.+ raise WorkdirUnsafeError(f"the session workdir cannot be a filesystem root: {workdir}")+ # file-write* covers the workdir subpath, so a file hard-linked outside+ # writes through to the host inode.+ scan_workdir_for_host_channels(workdir)+ # /tmp, not /var/folders: the profile has to name this directory, and this+ # keeps it out of the confidential per-user container.+ private_tmp = tempfile.mkdtemp(+ prefix = "us-seatbelt-", dir = "/tmp" if sys.platform == "darwin" else None+ )+ try:+ # Passed in so the drop happens by origin as well as by target.+ runtime_paths = tuple(+ path for path in runtime_read_paths(workdir) if not _within(path, workdir)+ )+ profile = build_profile(+ workdir = workdir,+ private_tmp = private_tmp,+ runtime_paths = runtime_paths,+ developer_paths = _developer_paths(),+ )+ return PreparedSandboxLaunch(+ argv = (SANDBOX_EXEC, "-p", profile, "--", *plan.argv),+ workdir = workdir,+ env = _sandbox_environment(plan.env, workdir, private_tmp),+ # Preserved: it is the os.setsid() tools.py's killpg teardown signals.+ preexec_fn = plan.preexec_fn,+ backend = BACKEND_NAME,+ cleanup_paths = [private_tmp],+ timeout_seconds = plan.timeout_seconds,+ close_fds = plan.close_fds,+ terminate_descendants = plan.terminate_descendants,+ )+ except Exception:+ shutil.rmtree(private_tmp, ignore_errors = True)+ raisediff --git a/studio/backend/core/inference/sandbox_probe.py b/studio/backend/core/inference/sandbox_probe.pynew file mode 100644index 00000000000..985f596281d--- /dev/null+++ b/studio/backend/core/inference/sandbox_probe.py@@ -0,0 +1,414 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Validate isolation against controls first verified on the host.++Check outside writes from the host: a private tmpfs may accept a write without+changing host files, so requiring an exception would reject valid isolation.+"""++from __future__ import annotations+import ctypes+import os+import shutil+import socket+import subprocess+import sys+import tempfile+import threading+import time+from typing import Any+from loggers import get_logger++logger = get_logger(__name__)++_PR_SET_NO_NEW_PRIVS = 38+try:+ # Resolved at import: an import after the fork can deadlock on the import lock.+ _libc = ctypes.CDLL(None, use_errno = True) if sys.platform == "linux" else None+except OSError: # pragma: no cover - a libc that will not load+ _libc = None++# ALONE on stdout: a payload that printed it early and died would satisfy "in".+PROBE_TOKEN = "UNSLOTH_SANDBOX_PROBE_OK"+_SENTINEL_TOKEN = "unsloth-host-sentinel-must-not-be-readable"+# Finding this on the host afterwards is the escape; the write need not raise.+_OUTSIDE_WRITE_TOKEN = "unsloth-sandbox-escaped-to-the-host"++PROBE_TIMEOUT_SECONDS = 30.0+# Short enough that installing the AppArmor profile takes effect without a restart.+_CACHE_TTL_SECONDS = 60.0+_CACHE_MAX_ENTRIES = 8++# ``sun_path`` is 108 bytes and multiprocessing appends about 32 of its own, so a+# longer scratch root fails a POSITIVE control for a reason unrelated to isolation.+_MAX_PROBE_BASE_LEN = 59++_cache_lock = threading.Lock()+_cache: dict[tuple[str, str], tuple[float, bool, str]] = {}+++def reset_probe_cache() -> None:+ """Forget every cached verdict. For tests only."""+ with _cache_lock:+ _cache.clear()+++def _cache_get(key: tuple[str, str]) -> tuple[bool, str] | None:+ now = time.monotonic()+ with _cache_lock:+ entry = _cache.get(key)+ if entry is None:+ return None+ expires_at, available, reason = entry+ if expires_at <= now:+ _cache.pop(key, None)+ return None+ return available, reason+++def _cache_put(key: tuple[str, str], available: bool, reason: str) -> None:+ with _cache_lock:+ _cache[key] = (time.monotonic() + _CACHE_TTL_SECONDS, available, reason)+ while len(_cache) > _CACHE_MAX_ENTRIES:+ _cache.pop(next(iter(_cache)))+++_PREAMBLE = '''import multiprocessing.reduction, os, socket, subprocess, sys+++def must_raise(label, fn):+ """A negative control: the host proved it works, so in here it must not."""+ try:+ fn()+ except OSError:+ return+ raise AssertionError(label)+'''+++def _negative_controls(+ sentinel: str, escape: str, outside: str, interpreter_writable: bool, abstract: "bytes | None"+) -> str:+ """Attempt direct and symlink sentinel reads, an outside write, and socket access.++ The host checks write escapes; the socket check verifies applied Landlock scope.+ """+ interpreter_leg = ""+ if interpreter_writable:+ interpreter_leg = (+ '\nmust_raise("opened the interpreter for writing", '+ 'lambda: open(sys.executable, "ab").close())\n'+ )+ abstract_leg = ""+ if abstract is not None:+ abstract_leg = (+ f'\nmust_raise("connected to a host abstract unix socket", lambda: '+ f"socket.socket(socket.AF_UNIX, socket.SOCK_STREAM).connect({abstract!r}))\n"+ )+ return f"""+must_raise("read the host sentinel", lambda: open({sentinel!r}, "rb").close())+must_raise("followed a workdir symlink to the host sentinel",+ lambda: open({escape!r}, "rb").close())+{interpreter_leg}{abstract_leg}+# Allowed to succeed against a private tmpfs; the host checks afterwards that+# nothing arrived. See _negative_controls.+try:+ with open({outside!r}, "w", encoding = "utf-8") as handle:+ handle.write({_OUTSIDE_WRITE_TOKEN!r})+except OSError:+ pass+"""+++def _positive_controls(workdir: str) -> str:+ return f"""+private = os.path.join({workdir!r}, "private.txt")+with open(private, "w", encoding = "utf-8") as handle:+ handle.write("sandbox-write-ok")+with open(private, encoding = "utf-8") as handle:+ if handle.read() != "sandbox-write-ok":+ raise AssertionError("workdir read-back did not match what was written")++# numpy/torch in a tool call fork workers, and a sandbox that breaks the+# socketpair breaks every one of them.+left, right = socket.socketpair()+try:+ left.sendall(b"ping")+ if right.recv(4) != b"ping":+ raise AssertionError("socketpair did not deliver")+ right.sendall(b"pong")+ if left.recv(4) != b"pong":+ raise AssertionError("socketpair did not deliver in reverse")+ # Descriptor passing through the resource sharer, which is how Pool and+ # ProcessPoolExecutor hand file handles between processes.+ duplicated = multiprocessing.reduction.DupFd(left.fileno()).detach()+ os.close(duplicated)+finally:+ left.close()+ right.close()++# A child interpreter, because tool code shells out to python constantly.+child = subprocess.run(+ [sys.executable, "-I", "-S", "-c", "print(42)"],+ stdin = subprocess.DEVNULL,+ capture_output = True,+ timeout = 20,+)+if child.returncode != 0 or child.stdout.strip() != b"42":+ raise AssertionError("a python child could not run: " + repr(child.stderr[-200:]))+"""+++def _payload(+ workdir: str,+ sentinel: str,+ escape: str,+ outside: str,+ interpreter_writable: bool,+ abstract: "bytes | None",+) -> str:+ """Passed as source on the command line, not as a workdir file, so the probe+ does not depend on how the backend exposes the workdir."""+ return (+ _PREAMBLE+ + _negative_controls(sentinel, escape, outside, interpreter_writable, abstract)+ + _positive_controls(workdir)+ + f"\nprint({PROBE_TOKEN!r})\n"+ )+++def _no_new_privs() -> None:+ """Logged rather than swallowed: failing to set it makes the probe MORE+ permissive than the launch it stands in for."""+ if _libc is None:+ return+ if _libc.prctl(_PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0:+ logger.warning("The sandbox probe could not set PR_SET_NO_NEW_PRIVS")+++def _abstract_control() -> "tuple[bytes | None, Any]":+ """Nothing where there is no scope to test, and nothing when the host cannot+ connect to its own socket, since a refusal inside would then prove nothing."""+ if sys.platform != "linux":+ return None, None+ from . import sandbox_landlock++ if not sandbox_landlock.abstract_scope_supported():+ return None, None+ name = b"\0unsloth-probe-" + os.urandom(8).hex().encode()+ listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)+ try:+ listener.bind(name)+ listener.listen(1)+ control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)+ control.settimeout(5)+ try:+ control.connect(name)+ finally:+ control.close()+ except OSError:+ listener.close()+ return None, None+ return name, listener+++def _host_saw_the_write(outside: str) -> bool:+ """The only honest form of the escape check: a private tmpfs makes the write+ succeed inside while nothing arrives here."""+ try:+ with open(outside, encoding = "utf-8") as handle:+ return _OUTSIDE_WRITE_TOKEN in handle.read()+ except OSError:+ return False+++def _host_payload(workdir: str) -> str:+ return _PREAMBLE + _positive_controls(workdir) + f"\nprint({PROBE_TOKEN!r})\n"+++def _probe_base() -> str:+ """A scratch root short enough for the fd-passing control's AF_UNIX address;+ failing that, the host control below reports the problem."""+ # Private roots FIRST, the environment-derived default last. TMPDIR can point+ # inside a directory the backend deliberately exposes (/opt/tmp is enough),+ # which puts the host sentinel under a read-only bind: the negative read then+ # succeeds and a working backend is reported unavailable.+ roots: list[str | None] = [root for root in ("/tmp", "/var/tmp") if os.path.isdir(root)]+ roots.append(None) # None = the platform default+ fallback = None+ for root in roots:+ try:+ base = tempfile.mkdtemp(prefix = "unsloth-probe-", dir = root)+ except OSError:+ continue+ if len(base) <= _MAX_PROBE_BASE_LEN:+ if fallback is not None:+ shutil.rmtree(fallback, ignore_errors = True)+ return base+ if fallback is None:+ fallback = base+ else:+ shutil.rmtree(base, ignore_errors = True)+ if fallback is not None:+ return fallback+ return tempfile.mkdtemp(prefix = "unsloth-probe-")+++def _host_positive_controls(workdir: str, sentinel: str, outside: str, env: dict[str, str]) -> str:+ """Prove on the host that every control would otherwise come out the other+ way. Returns "" when the host is sane, else why the probe cannot conclude."""+ try:+ with open(sentinel, encoding = "utf-8") as handle:+ if handle.read() != _SENTINEL_TOKEN:+ return "the probe sentinel did not contain what was written to it"+ except OSError as exc:+ return f"the host itself could not read the probe sentinel ({exc})"+ try:+ with open(outside, "w", encoding = "utf-8") as handle:+ handle.write("host-write-ok")+ os.unlink(outside)+ except OSError as exc:+ return f"the host itself could not write outside the probe workdir ({exc})"+ try:+ completed = subprocess.run(+ (sys.executable, "-I", "-S", "-c", _host_payload(workdir)),+ cwd = workdir,+ env = env,+ stdin = subprocess.DEVNULL,+ stdout = subprocess.PIPE,+ stderr = subprocess.PIPE,+ timeout = PROBE_TIMEOUT_SECONDS,+ )+ except (OSError, subprocess.SubprocessError) as exc:+ return f"the host itself could not run the probe's positive controls ({exc})"+ if completed.returncode != 0 or completed.stdout.strip() != PROBE_TOKEN.encode():+ detail = (completed.stderr or b"").decode("utf-8", "replace").strip()[-300:]+ return (+ "the host itself could not pass the probe's positive controls "+ f"(exit {completed.returncode}): {detail or 'no output'}"+ )+ return ""+++def probe(backend: Any, *, force: bool = False) -> tuple[bool, str]:+ """Never raises: every failure becomes ``(False, reason)``, since the caller+ either falls back or refuses."""+ backend_name = str(getattr(backend, "BACKEND_NAME", "unknown"))+ try:+ from .os_sandbox import ToolLaunchPlan, _runtime_identity+ key = (backend_name, _runtime_identity())+ except Exception as exc: # noqa: BLE001 - a probe never breaks its caller+ return False, f"the sandbox probe could not identify this runtime: {exc}"++ if not force:+ cached = _cache_get(key)+ if cached is not None:+ return cached++ available, reason = _run_probe(backend, backend_name, ToolLaunchPlan)+ _cache_put(key, available, reason)+ return available, reason+++def _run_probe(backend: Any, backend_name: str, plan_cls: Any) -> tuple[bool, str]:+ """The verdict is reached INSIDE the try: the escape check reads a host file+ the process may have written, and a cleanup that ran first would report every+ escape as a pass."""+ base = None+ prepared = None+ abstract_listener = None+ try:+ base = _probe_base()+ workdir = os.path.join(base, "work")+ os.mkdir(workdir)+ # The host's would test a directory the sandbox must not expose.+ temp_dir = os.path.join(workdir, "tmp")+ os.mkdir(temp_dir)+ sentinel = os.path.join(base, "host-sentinel.txt")+ with open(sentinel, "w", encoding = "utf-8") as handle:+ handle.write(_SENTINEL_TOKEN)+ escape = os.path.join(workdir, "escape")+ os.symlink(sentinel, escape)+ outside = os.path.join(base, "outside-write.txt")+ env = {+ "PATH": os.environ.get("PATH", "/usr/local/bin:/usr/bin:/bin"),+ "HOME": workdir,+ "TMPDIR": temp_dir,+ "LANG": os.environ.get("LANG", "C.UTF-8"),+ "PYTHONIOENCODING": "utf-8",+ }++ blocked = _host_positive_controls(workdir, sentinel, outside, env)+ if blocked:+ return False, blocked++ # Asking a confined process to fail at appending to a file the host+ # cannot write either proves nothing, so that leg is dropped, not passed.+ interpreter_writable = os.access(sys.executable, os.W_OK)+ # Proven reachable from out here first, so a refusal inside is the scope.+ abstract, abstract_listener = _abstract_control()++ plan = plan_cls(+ argv = (+ sys.executable,+ "-I",+ "-S",+ "-c",+ _payload(workdir, sentinel, escape, outside, interpreter_writable, abstract),+ ),+ workdir = workdir,+ env = env,+ # A setuid bwrap cannot raise privileges once no_new_privs is set,+ # so without this the probe qualifies a backend whose every real+ # launch dies after Popen, where auto can no longer fall back.+ preexec_fn = _no_new_privs,+ requested_mode = "required",+ execution_kind = "python",+ )+ prepared = backend.prepare(plan)+ completed = subprocess.run(+ prepared.argv,+ cwd = prepared.workdir,+ env = prepared.env,+ stdin = subprocess.DEVNULL,+ stdout = subprocess.PIPE,+ stderr = subprocess.PIPE,+ preexec_fn = prepared.preexec_fn,+ pass_fds = tuple(prepared.pass_fds),+ timeout = PROBE_TIMEOUT_SECONDS,+ )+ if completed.returncode != 0 or completed.stdout.strip() != PROBE_TOKEN.encode():+ detail = (completed.stderr or b"").decode("utf-8", "replace").strip()[-300:]+ if not detail:+ detail = (completed.stdout or b"").decode("utf-8", "replace").strip()[-300:]+ return False, (+ f"the {backend_name} live probe failed "+ f"(exit {completed.returncode}): {detail or 'no output'}"+ )+ if _host_saw_the_write(outside):+ return False, (+ f"the {backend_name} live probe wrote through to the host: a file created "+ "outside the sandbox workdir arrived on the real filesystem"+ )+ caveat = (+ ""+ if interpreter_writable+ else " (the interpreter is not host-writable, so that leg was skipped)"+ )+ return True, f"the {backend_name} live isolation probe passed{caveat}"+ except subprocess.TimeoutExpired:+ return False, f"the {backend_name} live probe timed out after {PROBE_TIMEOUT_SECONDS:.0f}s"+ except Exception as exc: # noqa: BLE001 - see the docstring on probe()+ logger.debug("sandbox probe for %s could not run", backend_name, exc_info = True)+ return False, f"the {backend_name} live probe could not run: {type(exc).__name__}: {exc}"+ finally:+ if abstract_listener is not None:+ abstract_listener.close()+ if prepared is not None:+ try:+ prepared.cleanup()+ except Exception: # noqa: BLE001 - cleanup failure is not a verdict+ logger.debug("sandbox probe cleanup failed", exc_info = True)+ if base is not None:+ shutil.rmtree(base, ignore_errors = True)diff --git a/studio/backend/core/inference/sandbox_seccomp.py b/studio/backend/core/inference/sandbox_seccomp.pynew file mode 100644index 00000000000..9e63ea9f10e--- /dev/null+++ b/studio/backend/core/inference/sandbox_seccomp.py@@ -0,0 +1,124 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The seccomp program bubblewrap installs on a sandboxed tool process.++Four holes a mount namespace cannot close: AF_VSOCK (addresses a hypervisor, not+a path), io_uring (kernel thread holds credentials captured at setup time),+keyrings (not namespaced, inherited as a process credential), and nested user+namespaces (bwrap's ``--disable-userns`` only exists since 0.8.0; Ubuntu 22.04+ships 0.6.1). AF_UNIX and AF_INET stay allowed on purpose.+"""++from __future__ import annotations++import errno+import platform+import struct+import sys+import tempfile+from typing import BinaryIO++# machine -> (AUDIT_ARCH, socket, socketpair); anything else is refused.+_ABIS = {+ "x86_64": (0xC000003E, 41, 53),+ "amd64": (0xC000003E, 41, 53),+ "aarch64": (0xC00000B7, 198, 199),+ "arm64": (0xC00000B7, 198, 199),+}+_USERNS_SYSCALLS = {+ "x86_64": (56, 272, 435),+ "amd64": (56, 272, 435),+ "aarch64": (220, 97, 435),+ "arm64": (220, 97, 435),+}+_IO_URING = (425, 426, 427) # setup, enter, register; same on both ABIs+# machine -> (add_key, request_key, keyctl). Denied rather than joining an empty+# session keyring, since joining needs the very syscall being taken away.+_KEYRING_SYSCALLS = {+ "x86_64": (248, 249, 250),+ "amd64": (248, 249, 250),+ "aarch64": (217, 218, 219),+ "arm64": (217, 218, 219),+}+_X32_SYSCALL_BIT = 0x40000000+_CLONE_NEWUSER = 0x10000000+_AF_VSOCK = 40++# struct seccomp_data: nr@0, arch@4, args[0]@16.+_LOAD, _JEQ, _JSET, _RET = 0x20, 0x15, 0x45, 0x06+_KILL = 0x80000000+_EPERM = 0x00050000 | errno.EPERM+_ENOSYS = 0x00050000 | errno.ENOSYS+_ALLOW = 0x7FFF0000+++def program(machine: str, *, block_userns: bool = False) -> tuple[tuple[int, int, int, int], ...]:+ key = machine.lower()+ if key not in _ABIS or sys.byteorder != "little":+ raise RuntimeError(+ "the Studio sandbox seccomp filter requires little-endian x86_64 or aarch64"+ )+ audit_arch, socket_nr, socketpair_nr = _ABIS[key]+ code: list[tuple[int, int, int, int]] = [+ (_LOAD, 0, 0, 4),+ (_JEQ, 1, 0, audit_arch),+ (_RET, 0, 0, _KILL), # an ABI switch would read args at other offsets+ (_LOAD, 0, 0, 0),+ ]+ if block_userns:+ # clone3() must report ENOSYS so glibc falls back to clone(), whose flags+ # word is then checked. Other syscalls rejoin below with nr still in A.+ clone_nr, unshare_nr, clone3_nr = _USERNS_SYSCALLS[key]+ code += [+ # unshare() is read like clone() below, on its flags: this filter+ # exists to stop a NESTED USER NAMESPACE, and a blanket refusal also+ # took unshare(CLONE_FS) and unshare(CLONE_FILES), which have nothing+ # to do with that and work everywhere outside the jail.+ (_JEQ, 0, 4, unshare_nr),+ (_LOAD, 0, 0, 16),+ (_JSET, 0, 1, _CLONE_NEWUSER),+ (_RET, 0, 0, _EPERM),+ (_LOAD, 0, 0, 0),+ (_JEQ, 0, 1, clone3_nr),+ (_RET, 0, 0, _ENOSYS),+ (_JEQ, 0, 4, clone_nr),+ (_LOAD, 0, 0, 16),+ (_JSET, 0, 1, _CLONE_NEWUSER),+ (_RET, 0, 0, _EPERM),+ (_LOAD, 0, 0, 0),+ ]+ if key in ("x86_64", "amd64"):+ # x32 numbers alias the 64-bit table, so an unfiltered x32 call reaches a+ # syscall this filter believes it inspected.+ code += [(_JSET, 0, 1, _X32_SYSCALL_BIT), (_RET, 0, 0, _KILL)]+ for number in (*_IO_URING, *_KEYRING_SYSCALLS[key]):+ code += [(_JEQ, 0, 1, number), (_RET, 0, 0, _EPERM)]+ code += [+ (_JEQ, 1, 0, socket_nr),+ (_JEQ, 0, 3, socketpair_nr),+ (_LOAD, 0, 0, 16), # args[0]: the address family, low word+ (_JEQ, 0, 1, _AF_VSOCK),+ (_RET, 0, 0, _EPERM),+ (_RET, 0, 0, _ALLOW),+ ]+ return tuple(code)+++def program_bytes(*, block_userns: bool = False, machine: str | None = None) -> bytes:+ instructions = program(machine or platform.machine(), block_userns = block_userns)+ return b"".join(struct.pack("=HBBI", *instruction) for instruction in instructions)+++def filter_file(*, block_userns: bool = False) -> BinaryIO:+ """Rewound because bwrap reads ``--seccomp FD`` to EOF; the caller owns it+ until exec."""+ stream = tempfile.TemporaryFile(prefix = "unsloth-sandbox-seccomp-")+ try:+ stream.write(program_bytes(block_userns = block_userns))+ stream.flush()+ stream.seek(0)+ except Exception:+ stream.close()+ raise+ return streamdiff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.pyindex 993efc48c1b..4a587e951f8 100644--- a/studio/backend/core/inference/tools.py+++ b/studio/backend/core/inference/tools.py@@ -78,6 +78,8 @@ from core.inference.tool_confinement import ToolConfinementUnavailable, account_from pathlib import Pathfrom utils.paths.storage_roots import RetiredAccountError, ensure_dir+from . import os_sandbox+from loggers import get_loggerlogger = get_logger(__name__)@@ -9251,6 +9253,174 @@ def _bypass_preexec():pass+# Never read back by the executors, so a concurrent second call can only make+# this stale, never wrong.+_last_tool_execution_record: "os_sandbox.ToolExecutionRecord | None" = None+++def _note_tool_execution(record) -> None:+ """Built by ``os_sandbox`` from a live probe, never from anything a model+ said, which is what makes it safe to show as a badge."""+ global _last_tool_execution_record+ if record is None:+ return+ _last_tool_execution_record = record+ logger.info("tool execution mode: %s", record.as_dict())+++def _requested_execution_mode(tool_execution_mode: str, disable_sandbox: bool) -> str:+ """Require disable_sandbox for full access; it also selects env and software guards."""+ if disable_sandbox:+ return "full"+ if tool_execution_mode == "full":+ raise os_sandbox.SandboxUnavailableError(+ "TOOL_EXECUTION_MODE_INVALID: full access is not requestable through "+ "tool_execution_mode",+ remediation = "Full access is granted with disable_sandbox (Bypass Permissions).",+ )+ return tool_execution_mode+++def _with_session_packages(env: dict, workdir: str) -> dict:+ """Reuse existing session packages without letting their binaries shadow PATH."""+ packages = os.path.join(workdir, os_sandbox.SESSION_PACKAGES_RELPATH)+ if not os.path.isdir(packages):+ return env+ updated = dict(env)+ # Block planted usercustomize.py from running before analysis on later+ # unisolated calls. In safe mode, the trusted sitecustomize shim stays first+ # on PYTHONPATH so a planted copy cannot shadow it.+ updated["PYTHONNOUSERSITE"] = "1"+ for key, value in (+ ("PYTHONPATH", packages),+ ("PATH", os.path.join(packages, "bin")),+ ):+ updated[key] = os.pathsep.join(part for part in (updated.get(key, ""), value) if part)+ return updated+++def _software_safeguards_launch(plan, fault: str):+ """Prepare an unisolated launch, retaining session packages and recording *fault*."""+ full = plan.requested_mode == "full"+ return os_sandbox.PreparedSandboxLaunch(+ argv = plan.argv,+ workdir = plan.workdir,+ env = _with_session_packages(plan.env, plan.workdir),+ preexec_fn = plan.preexec_fn,+ backend = "software-safeguards",+ timeout_seconds = plan.timeout_seconds,+ close_fds = plan.close_fds,+ terminate_descendants = plan.terminate_descendants,+ execution_record = os_sandbox.ToolExecutionRecord(+ requested_mode = plan.requested_mode,+ # A record saying "software safeguards" about a launch that skipped+ # the analysis and the rlimits would claim more than the run got.+ effective_mode = "full" if full else "software_safeguards",+ environment = sys.platform,+ backend = "software-safeguards",+ profile_id = "full-access" if full else "software-safeguards-v1",+ probe_generation = "",+ os_isolation = False,+ retained_safeguards = tuple(+ item+ for item in (+ os_sandbox._FULL_SAFEGUARDS if full else os_sandbox._SOFTWARE_SAFEGUARDS+ )+ if item != "timeout" or plan.timeout_seconds is not None+ ),+ # Naming only the fault made the record depend on which door the+ # fallback came through.+ limitations = (+ ("security_restrictions_disabled", fault)+ if full+ else (*os_sandbox._software_only_limitations(), fault)+ ),+ ),+ )+++def _prepare_tool_launch(plan):+ """Allow auto fallback for unavailable backends or unexpected planner errors.++ Unsafe workdirs and backend construction failures always refuse execution.+ """+ try:+ prepared = os_sandbox.prepare_tool_launch(plan)+ if plan.preexec_fn is not None and prepared.preexec_fn is None:+ # Preserve setsid so timeout cleanup cannot kill the server's process group.+ logger.warning(+ "Sandbox backend %s dropped the launch pre-exec; restoring it",+ prepared.backend,+ )+ prepared.preexec_fn = plan.preexec_fn+ if prepared.execution_record is not None and not prepared.execution_record.os_isolation:+ # The other door: os_sandbox returns its own fallback through here.+ prepared.env = _with_session_packages(prepared.env, plan.workdir)+ return prepared+ except (os_sandbox.WorkdirUnsafeError, os_sandbox.SandboxBuildError):+ # These failures can be tool-induced; fallback would let code remove its own boundary.+ raise+ except os_sandbox.SandboxUnavailableError:+ # Any other refusal means the backend stopped being available.+ if plan.requested_mode == "required" or (+ plan.requested_mode not in os_sandbox.TOOL_EXECUTION_MODES+ ):+ raise+ logger.warning(+ "The sandbox backend is no longer available, running with software safeguards",+ exc_info = True,+ )+ return _software_safeguards_launch(plan, "sandbox_became_unavailable")+ except Exception as exc: # noqa: BLE001 - auto falls back on unexpected planner errors+ logger.warning("Sandbox planning failed, running with software safeguards", exc_info = True)+ if plan.requested_mode == "required":+ raise os_sandbox.SandboxUnavailableError(+ f"OS_ISOLATION_UNAVAILABLE: the sandbox planner failed: {exc}",+ remediation = os_sandbox.linux_unavailable_remediation()+ if sys.platform == "linux"+ else "This host cannot start an OS sandbox.",+ ) from exc+ return _software_safeguards_launch(plan, "sandbox_planner_error")+++def _forget_sandbox_capability_if_the_backend_failed(prepared, output: str) -> None:+ """``prepare()`` only builds an argv, so a stale probe verdict is not found+ until bwrap exits at exec. Dropping it bounds the damage to that one call+ instead of every call for the rest of the cache's life."""+ if prepared is None or prepared.backend == "software-safeguards":+ return+ if not output.startswith("Exit code ") or "bwrap: " not in output[:400]:+ return+ logger.warning("The sandbox backend failed at launch; re-probing the capability")+ try:+ from .sandbox_probe import reset_probe_cache+ reset_probe_cache()+ except Exception: # noqa: BLE001 - a cache reset never breaks a tool result+ logger.debug("could not reset the sandbox probe cache", exc_info = True)+++def _sandbox_refusal(exc) -> str:+ """The remediation is part of the answer, not a log line: the reader is the+ person who can fix the host."""+ remediation = getattr(exc, "remediation", "") or ""+ return _truncate(f"Execution error: {exc}{(' ' + remediation) if remediation else ''}")+++def _apply_prepared_launch(prepared, popen_kwargs: dict) -> dict:+ """``preexec_fn`` comes from the plan untouched on POSIX: every kill path here+ is killpg based, so the OUTER process must land in its own session or a+ timeout signals the Unsloth server's group instead of the tool's."""+ popen_kwargs["cwd"] = prepared.workdir+ popen_kwargs["env"] = prepared.env+ if sys.platform != "win32":+ popen_kwargs["preexec_fn"] = prepared.preexec_fn+ popen_kwargs["close_fds"] = prepared.close_fds+ if prepared.pass_fds:+ # Empty for every fallback, so Windows never sees a kwarg it rejects.+ popen_kwargs["pass_fds"] = tuple(prepared.pass_fds)+ return popen_kwargs++# Hardening the Unsloth parent is done once (PR_SET_DUMPABLE is process-global and sticky); guarded so repeated bypass# calls do not re-issue the prctl._parent_proc_hardened = False@@ -12501,6 +12671,8 @@ def execute_tool(context_tokens = _UNSET_CONTEXT_TOKENS,search_images: bool = False,result_budget_tokens: int | None = None,+ *,+ tool_execution_mode: str = "auto",) -> str:"""Execute a tool by name with the given arguments; returns a string.@@ -12513,7 +12685,10 @@ def execute_tool(tools; web_search / MCP are unchanged. ``output_callback``: optional ``callable(str)`` invokedwith incremental stdout/stderr chunks while python/terminal executions run. Purelyobservational: the returned result string is identical with or without it. ``website_policy``:- hidden server-validated domain limits for web_search.+ hidden server-validated domain limits for web_search. ``tool_execution_mode`` controls OS+ isolation for python/terminal: ``"auto"`` isolates when available and otherwise preserves+ existing behavior, while ``"required"`` refuses unisolated execution. Full access remains+ controlled by ``disable_sandbox``; ``"full"`` here is refused."""from state.tool_policy import require_tool_access@@ -12713,6 +12888,7 @@ def execute_tool(disable_sandbox = disable_sandbox,output_callback = output_callback,thread_id = thread_id,+ tool_execution_mode = tool_execution_mode,)if name == "terminal":with _session_in_flight(session_id):@@ -12724,6 +12900,7 @@ def execute_tool(disable_sandbox = disable_sandbox,output_callback = output_callback,thread_id = thread_id,+ tool_execution_mode = tool_execution_mode,)# Same in-flight guard as the two above: it writes into the session workdir, so a chat deleted mid-call must not# unlink it underneath.@@ -18031,11 +18208,14 @@ def _python_exec(disable_sandbox: bool = False,output_callback = None,thread_id: str | None = None,+ *,+ tool_execution_mode: str = "auto",) -> str:"""Execute Python code in a subprocess sandbox. disable_sandbox (Bypass Permissions): skip thesafety analysis and rlimit pre-exec, and use the host env minus secrets. output_callback:optional callable(str) streamed each stdout line as it is produced; the returned result is- unchanged."""+ unchanged. tool_execution_mode selects automatic or required OS isolation; disable_sandbox+ keeps full access as a separate explicit choice."""if not code or not code.strip():return "No code provided."@@ -18068,6 +18248,8 @@ def _python_exec(tmp_path = None_scratch_name = None+ # Bound before the try so the finally can release even when prepare raised.+ prepared = Nonetry:workdir = _get_workdir(session_id)confinement = _account_confinement()@@ -18102,25 +18284,53 @@ def _python_exec(popen_kwargs = dict(stdout = subprocess.PIPE,stderr = subprocess.STDOUT,+ # close_fds leaves 0, 1 and 2 alone, so an unset stdin is the+ # server's own and no path rule applies to an open descriptor.+ stdin = subprocess.DEVNULL,text = True,# Decode child output as utf-8 (it emits utf-8 via PYTHONIOENCODING); replace so non-ASCII output never# crashes the read on Windows.encoding = "utf-8",errors = "replace",- cwd = workdir,- env = safe_env,)- if sys.platform != "win32":- popen_kwargs["preexec_fn"] = _bypass_preexec if disable_sandbox else _sandbox_preexec- else:+ if sys.platform == "win32":popen_kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW# -u forces unbuffered child stdout so a bare print() streams live# instead of sitting in the pipe's block buffer until exit. Applied# unconditionally to stay byte-identical with and without streaming;# unlike PYTHONUNBUFFERED=1 it never pollutes the child's os.environ.- argv = _apply_confinement(confinement, popen_kwargs, [sys.executable, "-u", tmp_path])- proc = subprocess.Popen(argv, **popen_kwargs)+ requested_mode = _requested_execution_mode(tool_execution_mode, disable_sandbox)+ base_preexec = (+ None+ if sys.platform == "win32"+ else (_bypass_preexec if disable_sandbox else _sandbox_preexec)+ )+ # Managed accounts already have a fail-closed, account-specific boundary. Keep it+ # as the outer launch contract instead of stacking a generic sandbox around it:+ # on Linux its pre-exec Landlock policy would restrict bwrap before mount setup.+ if confinement is None:+ prepared = _prepare_tool_launch(+ os_sandbox.ToolLaunchPlan(+ argv = (sys.executable, "-u", tmp_path),+ workdir = workdir,+ env = safe_env,+ preexec_fn = base_preexec,+ requested_mode = requested_mode,+ timeout_seconds = timeout,+ execution_kind = "python",+ )+ )+ _note_tool_execution(prepared.execution_record)+ proc = os_sandbox.spawn_prepared_launch(+ prepared, **_apply_prepared_launch(prepared, popen_kwargs)+ )+ else:+ popen_kwargs.update(cwd = workdir, env = safe_env)+ if sys.platform != "win32":+ popen_kwargs["preexec_fn"] = base_preexec+ argv = _apply_confinement(confinement, popen_kwargs, [sys.executable, "-u", tmp_path])+ proc = subprocess.Popen(argv, **popen_kwargs)# Capture the group before any watcher can reap the leader (see _capture_process_group); None on Windows.pgid = _capture_process_group(proc)@@ -18180,8 +18390,11 @@ def _python_exec(if session_id:result += _created_file_sentinels(workdir, _before, _scratch_name, call_token)+ _forget_sandbox_capability_if_the_backend_failed(prepared, result)return result+ except os_sandbox.SandboxUnavailableError as e:+ return _sandbox_refusal(e)except Exception as e:# An exception message carries whatever the failure put in it, so it is capped like the result would have# been.@@ -18191,6 +18404,9 @@ def _python_exec(if _scratch_name:with _scratch_lock:_active_scratch.discard(_scratch_name)+ # Private mounts and descriptors, released on every exit path.+ if prepared is not None:+ prepared.cleanup()_forget_tool_pid(locals().get("proc"))if tmp_path and os.path.exists(tmp_path):try:@@ -18207,11 +18423,13 @@ def _bash_exec(disable_sandbox: bool = False,output_callback = None,thread_id: str | None = None,+ *,+ tool_execution_mode: str = "auto",) -> str:"""Execute a bash command in a subprocess sandbox. disable_sandbox (Bypass Permissions): skipthe command blocklist and rlimit pre-exec, and use the host env minus secrets.output_callback: optional callable(str) streamed each stdout line as it is produced; the- returned result is unchanged."""+ returned result is unchanged. tool_execution_mode follows _python_exec."""if not command or not command.strip():return "No command provided."@@ -18245,6 +18463,7 @@ def _bash_exec(spill_dir = Nonespill_scope = Nonecall_token = None+ prepared = None_scratch_name = Nonetry:try:@@ -18263,25 +18482,50 @@ def _bash_exec(popen_kwargs = dict(stdout = subprocess.PIPE,stderr = subprocess.STDOUT,+ # See _python_exec: the server's stdin must not reach a tool call.+ stdin = subprocess.DEVNULL,text = True,# Match _python_exec: decode utf-8 with "replace" so invalid output bytes never raise UnicodeDecodeError# (which the streaming reader thread would swallow), keeping both paths byte-identical.encoding = "utf-8",errors = "replace",- cwd = workdir,- env = safe_env,)- if sys.platform != "win32":- popen_kwargs["preexec_fn"] = _bypass_preexec if disable_sandbox else _sandbox_preexec- else:+ if sys.platform == "win32":popen_kwargs["creationflags"] = subprocess.CREATE_NO_WINDOWshell_argv, _scratch_name = _shell_argv(command, workdir, confinement)if _scratch_name:with _scratch_lock:_active_scratch.add(_scratch_name)- argv = _apply_confinement(confinement, popen_kwargs, shell_argv)- proc = subprocess.Popen(argv, **popen_kwargs)+ requested_mode = _requested_execution_mode(tool_execution_mode, disable_sandbox)+ base_preexec = (+ None+ if sys.platform == "win32"+ else (_bypass_preexec if disable_sandbox else _sandbox_preexec)+ )+ # Use the same mutually exclusive launch paths as the Python executor.+ if confinement is None:+ prepared = _prepare_tool_launch(+ os_sandbox.ToolLaunchPlan(+ argv = tuple(shell_argv),+ workdir = workdir,+ env = safe_env,+ preexec_fn = base_preexec,+ requested_mode = requested_mode,+ timeout_seconds = timeout,+ execution_kind = "terminal",+ )+ )+ _note_tool_execution(prepared.execution_record)+ proc = os_sandbox.spawn_prepared_launch(+ prepared, **_apply_prepared_launch(prepared, popen_kwargs)+ )+ else:+ popen_kwargs.update(cwd = workdir, env = safe_env)+ if sys.platform != "win32":+ popen_kwargs["preexec_fn"] = base_preexec+ argv = _apply_confinement(confinement, popen_kwargs, shell_argv)+ proc = subprocess.Popen(argv, **popen_kwargs)# Capture the group before any watcher can poll/reap the leader (see _python_exec); None on Windows.pgid = _capture_process_group(proc)@@ -18331,14 +18575,20 @@ def _bash_exec(# Only for a chat that has an id (see _python_exec).if session_id:result += _created_file_sentinels(workdir, _before, _scratch_name, call_token)+ _forget_sandbox_capability_if_the_backend_failed(prepared, result)return result+ except os_sandbox.SandboxUnavailableError as e:+ return _sandbox_refusal(e)except Exception as e:# An exception message carries whatever the failure put in it, so it is capped like the result would have# been.return _truncate(f"Execution error: {e}")finally:_call_finished(call_token)+ # Private mounts and descriptors, released on every exit path.+ if prepared is not None:+ prepared.cleanup()_forget_tool_pid(locals().get("proc"))if _scratch_name:with _scratch_lock:diff --git a/studio/backend/main.py b/studio/backend/main.pyindex 828e5ed7133..86e842c4573 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -651,11 +651,7 @@ def banner_autofill_available(app_state, environ) -> bool:def bootstrap_banner_lines(- username: str,- bootstrap_path,- password: Optional[str],- *,- autofill_available: bool,+ username: str, bootstrap_path, password: Optional[str], *, autofill_available: bool) -> "list[str]":"""The first-boot banner for a freshly created admin account.@@ -818,9 +814,7 @@ async def lifespan(app: FastAPI):# to serve with the default credential: never capture the bootstrap password into app.state._suppress_bootstrap = getattr(app.state, "suppress_bootstrap_injection", False)_created = storage.ensure_default_admin()- app.state.bootstrap_password = (- None if _suppress_bootstrap else storage.get_bootstrap_password()- )+ app.state.bootstrap_password = None if _suppress_bootstrap else storage.get_bootstrap_password()# A tunnel launch runs the pre-bind gate first and that gate seeds the account, so# _created is False there and the whole banner would be skipped on exactly the launch# that needs it. requires_password_change: the gate may also have taken a new passworddiff --git a/studio/backend/tests/test_account_tool_confinement.py b/studio/backend/tests/test_account_tool_confinement.pyindex 342b1f4d515..fc6314805b8 100644--- a/studio/backend/tests/test_account_tool_confinement.py+++ b/studio/backend/tests/test_account_tool_confinement.py@@ -254,9 +254,15 @@ def test_confined_child_keeps_interpreter_and_system_tools(tmp_path):@pytest.mark.skipif(not LANDLOCK, reason = "Landlock not available on this kernel")-def test_owner_child_remains_unconfined(tmp_path):+def test_owner_bypass_remains_unconfined(tmp_path):files = _seed(tmp_path)- out = run_as(OWNER, tools._bash_exec, f"cat {files['alice']}", session_id = "chat")+ out = run_as(+ OWNER,+ tools._bash_exec,+ f"cat {files['alice']}",+ session_id = "chat",+ disable_sandbox = True,+ )assert "ALICE_PRIVATE" in outdiff --git a/studio/backend/tests/test_bootstrap_banner_password_visibility.py b/studio/backend/tests/test_bootstrap_banner_password_visibility.pyindex 2dcdcd7a3d5..e1e30e3407e 100644--- a/studio/backend/tests/test_bootstrap_banner_password_visibility.py+++ b/studio/backend/tests/test_bootstrap_banner_password_visibility.py@@ -9,7 +9,6 @@ PATH = "/opt/unsloth-studio/auth/.bootstrap_password"def _lines(**kwargs):from main import bootstrap_banner_lines-return bootstrap_banner_lines("unsloth", PATH, PASSWORD, **kwargs)@@ -31,9 +30,7 @@ def test_a_launch_without_autofill_prints_the_password():def test_a_missing_password_falls_back_to_the_path():from main import bootstrap_banner_lines- body = "\n".join(- bootstrap_banner_lines("unsloth", PATH, None, autofill_available = False)- )+ body = "\n".join(bootstrap_banner_lines("unsloth", PATH, None, autofill_available = False))assert "password: None" not in bodyassert f"password saved to: {PATH}" in body@@ -69,9 +66,9 @@ def test_the_banner_guard_does_not_key_only_on_this_call_creating_the_admin():guard = _banner_guard()assert "admin_created_this_process" in guard- assert "requires_password_change" in guard, (- "the gate can take a new password at its prompt, which retires the bootstrap one"- )+ assert (+ "requires_password_change" in guard+ ), "the gate can take a new password at its prompt, which retires the bootstrap one"def test_the_creation_flag_survives_a_later_call_that_creates_nothing(monkeypatch):@@ -156,6 +153,6 @@ def test_run_server_resets_the_per_launch_flags_before_the_gate():]assert "app.state.api_only = api_only" in assigns- assert "app.state.suppress_bootstrap_injection = False" in assigns, (- "a sticky True from an earlier launch withholds the autofill that is available"- )+ assert (+ "app.state.suppress_bootstrap_injection = False" in assigns+ ), "a sticky True from an earlier launch withholds the autofill that is available"diff --git a/studio/backend/tests/test_bypass_permissions.py b/studio/backend/tests/test_bypass_permissions.pyindex 8ffbe26ba0d..cbd1a593e6b 100644--- a/studio/backend/tests/test_bypass_permissions.py+++ b/studio/backend/tests/test_bypass_permissions.py@@ -134,11 +134,21 @@ def captured_popen(monkeypatch):return cap+def _carries(preexec, expected) -> bool:+ """A backend that isolates COMPOSES its pre-exec onto the plan's rather than+ replacing it, so on such a host the identity is one closure down. Asserted+ rather than relaxed to "not the bypass one": the setsid every kill path here+ signals has to still be in there, and a backend that dropped it must fail."""+ if preexec is expected:+ return True+ return any(cell.cell_contents is expected for cell in (preexec.__closure__ or ()))++@_POSIX_ONLYdef test_python_sandboxed_uses_sandbox_preexec_and_safe_env(captured_popen, monkeypatch):monkeypatch.setenv("HF_TOKEN", "secret-abc")_python_exec("print(1)", None, 5, "t", disable_sandbox = False)- assert captured_popen["kwargs"]["preexec_fn"] is tools._sandbox_preexec+ assert _carries(captured_popen["kwargs"]["preexec_fn"], tools._sandbox_preexec)assert "HF_TOKEN" not in captured_popen["kwargs"]["env"]diff --git a/studio/backend/tests/test_sandbox_files_and_storage_roots.py b/studio/backend/tests/test_sandbox_files_and_storage_roots.pyindex 027b1b3f8b0..3ec8f48e468 100644--- a/studio/backend/tests/test_sandbox_files_and_storage_roots.py+++ b/studio/backend/tests/test_sandbox_files_and_storage_roots.py@@ -1277,7 +1277,9 @@ def test_the_executor_leaves_nothing_in_the_sandbox(tmp_path, monkeypatch):tools = _shared_setup_1(monkeypatch, tmp_path)workdir = Path(tools.get_sandbox_workdir("__LOCALID_scratch"))tools._python_exec("print('hi')", session_id = "__LOCALID_scratch")- assert sorted(p.name for p in workdir.iterdir()) == [+ # .cache is the sandbox's own model-cache mount point: a dot directory that+ # holds nothing, which is why the removal below still goes through.+ assert sorted(p.name for p in workdir.iterdir() if p.name != ".cache") == [tools._SANDBOX_MARKER,tools._SANDBOX_TEMP_DIRNAME,]@@ -1778,7 +1780,8 @@ def test_a_user_python_file_is_never_executor_scratch(tmp_path, monkeypatch):assert sorted(p.namefor p in workdir.iterdir()- if p.name not in tools._INTERNAL_SANDBOX_FILES and p.name != tools._SANDBOX_TEMP_DIRNAME+ if p.name not in tools._INTERNAL_SANDBOX_FILES+ and p.name not in (tools._SANDBOX_TEMP_DIRNAME, ".cache")) == ["studio_exec_results.py"]assert inference._sandbox_listing_names(str(workdir)) == ["studio_exec_results.py"]# And a delete without the opt-in will not quietly take it.@@ -1858,7 +1861,8 @@ def test_the_scratch_script_is_never_reported_as_a_file(tmp_path, monkeypatch):assert sorted(p.namefor p in workdir.iterdir()- if p.name not in tools._INTERNAL_SANDBOX_FILES and p.name != tools._SANDBOX_TEMP_DIRNAME+ if p.name not in tools._INTERNAL_SANDBOX_FILES+ and p.name not in (tools._SANDBOX_TEMP_DIRNAME, ".cache")) == ["studio_exec_results.py"]assert json.loads(files) == [{"name": "studio_exec_results.py", "size": 5}]diff --git a/studio/backend/tests/test_sandbox_linux.py b/studio/backend/tests/test_sandbox_linux.pynew file mode 100644index 00000000000..6711b8a60c8--- /dev/null+++ b/studio/backend/tests/test_sandbox_linux.py@@ -0,0 +1,1563 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""What the Linux sandbox argv and its seccomp program actually say.++Structure, not a live sandbox: most CI denies unprivileged user namespaces. The+seccomp program is verified by interpreting it, since a filter with the wrong+jump offset passes every length check and then allows what it meant to deny.+"""++from __future__ import annotations++import ast+import ctypes+import errno+import inspect+import json+import os+import platform+import shutil+import socket+import stat+import struct+import sys+import sysconfig+import tempfile+import threading+import time++import pytest++if sys.platform != "linux":+ pytest.skip("the bubblewrap backend is Linux only", allow_module_level = True)++from core.inference import ( # noqa: E402+ os_sandbox,+ sandbox_landlock,+ sandbox_linux,+ sandbox_seccomp,+)+from core.inference.os_sandbox import ( # noqa: E402+ SandboxUnavailableError,+ ToolLaunchPlan,+ WorkdirUnsafeError,+) # noqa: E402+++def _plan(+ workdir,+ argv = ("/bin/true",),+ **kwargs,+):+ return ToolLaunchPlan(argv = argv, workdir = str(workdir), env = {"PATH": "/usr/bin"}, **kwargs)+++def prepared(tmp_path):+ if sandbox_linux.shutil.which("bwrap") is None:+ pytest.skip("bubblewrap is not installed on this host")+ launch = sandbox_linux.prepare(_plan(tmp_path))+ yield launch+ launch.cleanup()+++def _pairs(argv, flag):+ return [+ (argv[index + 1], argv[index + 2])+ for index, token in enumerate(argv)+ if token == flag and index + 2 < len(argv)+ ]+++def test_the_launch_runs_bwrap_and_ends_with_the_payload_after_a_bare_separator(prepared):+ assert os.path.basename(prepared.argv[0]) == "bwrap"+ separator = prepared.argv.index("--")+ assert prepared.argv[separator + 1 :] == ("/bin/true",)+ # Everything past "--" is argv for the tool, so an appended option there+ # becomes a positional argument.+ assert prepared.argv.count("--") == 1+++def test_the_network_namespace_is_deliberately_left_alone(prepared):+ assert "--unshare-net" not in prepared.argv+ assert "--unshare-all" not in prepared.argv+++def test_every_namespace_except_the_network_one_is_unshared(prepared):+ for option in (+ "--unshare-user",+ "--unshare-pid",+ "--unshare-ipc",+ "--unshare-uts",+ "--unshare-cgroup",+ ):+ assert option in prepared.argv+++def test_the_session_dies_with_studio_and_detaches_from_the_terminal(prepared):+ assert "--die-with-parent" in prepared.argv+ # --new-session denies TIOCSTI injection back into Studio's terminal.+ assert "--new-session" in prepared.argv+++def test_capabilities_are_dropped_and_the_filter_arrives_as_an_inherited_descriptor(prepared):+ argv = prepared.argv+ assert argv[argv.index("--cap-drop") + 1] == "ALL"+ fd = int(argv[argv.index("--seccomp") + 1])+ assert prepared.pass_fds == (fd,)+ assert [handle.fileno() for handle in prepared.owned_files] == [fd]+ # The descriptor is rewound: bwrap reads from the current offset.+ assert os.lseek(fd, 0, os.SEEK_CUR) == 0+++def test_identity_is_synthesised_rather_than_bound_from_the_host(prepared):+ binds = {destination: source for source, destination in _pairs(prepared.argv, "--ro-bind")}+ passwd, group = binds["/etc/passwd"], binds["/etc/group"]+ assert passwd != "/etc/passwd" and group != "/etc/group"+ assert prepared.cleanup_paths == [os.path.dirname(passwd)]+ # A private 0700 directory, so no other account can swap the files.+ assert os.stat(os.path.dirname(passwd)).st_mode & 0o777 == 0o700+ with open(passwd, encoding = "utf-8") as stream:+ entries = stream.read().splitlines()+ assert len(entries) == 1 and entries[0].split(":")[2] == str(os.getuid())+++def test_the_writable_workdir_bind_lands_after_the_root_goes_read_only(prepared, tmp_path):+ argv = prepared.argv+ workdir = os.path.realpath(tmp_path)+ remount = argv.index("--remount-ro")+ assert argv[remount + 1] == "/"+ # --dir before, so the mount point exists in the read-only root; --bind after,+ # so the one writable directory survives the remount.+ assert argv.index("--dir", 0, remount) < remount+ assert (workdir, workdir) in _pairs(argv[remount:], "--bind")+ assert argv[argv.index("--chdir") + 1] == workdir+++def test_both_spellings_of_a_symlinked_workdir_get_a_mount_point(tmp_path):+ real = tmp_path / "real"+ real.mkdir()+ link = tmp_path / "link"+ link.symlink_to(real)++ launch = sandbox_linux.prepare(+ ToolLaunchPlan(+ argv = (sys.executable, "-c", "pass"),+ workdir = str(link),+ env = {"PATH": "/usr/bin:/bin"},+ )+ )+ try:+ argv = launch.argv+ remount = argv.index("--remount-ro")+ made = [argv[i + 1] for i, token in enumerate(argv[:remount]) if token == "--dir"]+ assert str(link) in made, "the caller's spelling has no mount point"+ assert os.path.realpath(link) in made, "the canonical spelling has no mount point"+ bound = _pairs(argv[remount:], "--bind")+ assert (os.path.realpath(link), str(link)) in bound+ finally:+ launch.cleanup()+++def test_the_private_tmpfs_replaces_the_shared_directories(prepared):+ argv = prepared.argv+ remount = argv.index("--remount-ro")+ tmpfs = [argv[i + 1] for i, token in enumerate(argv) if token == "--tmpfs"]+ assert tmpfs[:2] == ["/dev/shm", "/tmp"]+ # After the remount, or every temp file would fail on a read-only path.+ assert argv.index("--tmpfs") > remount+++def test_no_private_key_directory_enters_the_jail_at_all(prepared):+ sources = [source for source, _ in _pairs(prepared.argv, "--ro-bind-try")]+ assert "/etc/ssl/certs" in sources+ assert "/etc/ssl" not in sources and "/etc/pki" not in sources+ secret = "/etc/ssl/private"+ assert os.path.isdir(secret), "no private-key directory here, so this proves nothing"+ for source, _ in (*_pairs(prepared.argv, "--ro-bind-try"), *_pairs(prepared.argv, "--ro-bind")):+ assert not sandbox_linux._within(secret, source), source+++def test_pip_gets_a_writable_target_inside_the_workdir(prepared):+ argv = prepared.argv+ packages = os.path.join(prepared.workdir, sandbox_linux.SESSION_PACKAGES_RELPATH)+ assert argv[argv.index("PIP_TARGET") + 1] == packages+ pythonpath = argv[argv.index("PYTHONPATH") + 1].split(os.pathsep)+ assert packages in pythonpath+ # Appended, never first: the sandbox_site startup shim must stay unshadowable.+ assert pythonpath[-1] == packages+++def test_system_directories_are_bound_whole_and_never_file_by_file(prepared):+ sources = [source for source, _ in _pairs(prepared.argv, "--ro-bind-try")]+ assert "/usr/lib" in sources+ # Enumerating shared objects produces hundreds of binds and still misses+ # the one dlopen() wants, so every bind of a *file* has to be a named+ # config file or one of the two synthesised identities.+ named = {+ *sandbox_linux._ETC_FILES,+ *sandbox_linux._ETC_FILES_IF_TRUSTED,+ *sandbox_linux._NETWORK_FILES,+ }+ identity_dir = prepared.cleanup_paths[0]+ for flag in ("--bind", "--ro-bind", "--ro-bind-try"):+ for source, _ in _pairs(prepared.argv, flag):+ if os.path.isdir(source):+ continue+ if source in named or os.path.dirname(source) == identity_dir:+ continue+ # pyvenv.cfg is the one interpreter file with no directory of its own,+ # and without it a venv cannot find its base.+ assert os.path.basename(source) == "pyvenv.cfg", source+++def test_the_interpreter_contributes_only_a_handful_of_read_only_binds(prepared):+ runtime = [+ source+ for source, destination in _pairs(prepared.argv, "--ro-bind")+ if source == destination+ ]+ assert 0 < len(runtime) < 40, runtime+ system_roots = tuple(path for path in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(path))+ for path in runtime:+ assert not any(sandbox_linux._within(path, root) for root in system_roots)+++def test_nothing_binds_or_creates_the_filesystem_root(prepared):+ for flag in ("--bind", "--ro-bind", "--ro-bind-try"):+ for source, destination in _pairs(prepared.argv, flag):+ assert source != "/" and destination != "/"+ directories = [+ prepared.argv[index + 1] for index, token in enumerate(prepared.argv) if token == "--dir"+ ]+ assert "/" not in directories+++def test_a_launch_with_no_command_is_refused_rather_than_handed_to_bwrap(tmp_path):+ with pytest.raises(SandboxUnavailableError, match = "needs a command"):+ sandbox_linux.prepare(_plan(tmp_path, argv = ()))+++def test_the_workdir_is_the_only_writable_bind(prepared, tmp_path):+ workdir = os.path.realpath(tmp_path)+ assert _pairs(prepared.argv, "--bind") == [(workdir, workdir)]+++def test_the_model_cache_shares_its_data_subdirectories_and_nothing_else(tmp_path, monkeypatch):+ cache = tmp_path / "hostcache"+ for name in ("hub", "datasets", "modules", "xet", "assets"):+ (cache / name).mkdir(parents = True)+ (cache / "token").write_text("hf_A_REAL_LOOKING_TOKEN")+ (cache / "stored_tokens").write_text("{}")+ _share_cache(monkeypatch, cache)++ launch = sandbox_linux.prepare(_plan(tmp_path))+ try:+ workdir = os.path.realpath(tmp_path)+ inner = os.path.join(workdir, ".cache", "huggingface")+ shared = _pairs(launch.argv, "--bind-try")+ # Spelled out rather than built from _MODEL_CACHE_SUBDIRS: deriving the+ # expectation from the constant under test makes both sides move together.+ assert sorted(shared) == sorted(+ (os.path.join(str(cache), name), os.path.join(inner, name))+ for name in ("hub", "datasets", "xet", "assets")+ )+ # "modules" is remote code huggingface_hub writes and then imports, so+ # sharing it would let a sandboxed call leave a module an unisolated one+ # later imports.+ assert not any("modules" in destination for _, destination in shared)+ assert launch.argv[launch.argv.index("HF_HOME") + 1] == inner+ # No bind names the credentials, so HF_HOME/token resolves into the+ # writable session workdir and is empty.+ for credential in ("token", "stored_tokens"):+ host_path = os.path.join(str(cache), credential)+ assert not any(host_path in token for token in launch.argv), credential+ finally:+ launch.cleanup()+++def test_the_cache_mount_points_are_made_here_and_left(tmp_path, cache):+ workdir = tmp_path / "session"+ workdir.mkdir()++ launch = sandbox_linux.prepare(_plan(workdir))+ launch.cleanup()+ assert (workdir / ".cache" / "huggingface" / "hub").is_dir()+ second = sandbox_linux.prepare(_plan(workdir))+ second.cleanup()+ assert (workdir / ".cache" / "huggingface" / "hub").is_dir()+++def test_a_cache_directory_the_tool_call_wrote_is_left_alone(tmp_path, cache):+ workdir = tmp_path / "session"+ (workdir / ".cache" / "huggingface").mkdir(parents = True)+ (workdir / ".cache" / "notes.txt").write_text("the user's")++ launch = sandbox_linux.prepare(_plan(workdir))+ launch.cleanup()+ assert (workdir / ".cache" / "notes.txt").read_text() == "the user's"+++def test_the_model_cache_bind_is_absent_when_the_host_has_no_cache(tmp_path, monkeypatch):+ monkeypatch.setattr(sandbox_linux, "_model_cache_binds", lambda workdir: {})+ launch = sandbox_linux.prepare(_plan(tmp_path))+ try:+ assert "HF_HOME" not in launch.argv+ assert [+ p for p in _pairs(launch.argv, "--bind") if p[0] != os.path.realpath(tmp_path)+ ] == []+ finally:+ launch.cleanup()+++def test_the_inner_home_and_tmpdir_are_set_by_bwrap_not_by_the_caller_environment(prepared):+ argv = prepared.argv+ assert argv[argv.index("HOME") + 1] == prepared.workdir+ assert argv[argv.index("TMPDIR") + 1] == "/tmp"+ # bwrap --setenv owns the inner values, so rewriting the caller's env+ # here would desynchronise the two.+ assert prepared.env == {"PATH": "/usr/bin"}+++def test_a_tmpdir_inside_the_workdir_survives_into_the_jail(tmp_path):+ scratch = tmp_path / "unsloth-tmp"+ scratch.mkdir()+ plan = ToolLaunchPlan(+ argv = ("/bin/true",),+ workdir = str(tmp_path),+ env = {"PATH": "/usr/bin", "TMPDIR": str(scratch)},+ )+ launch = sandbox_linux.prepare(plan)+ try:+ assert launch.argv[launch.argv.index("TMPDIR") + 1] == str(scratch)+ finally:+ launch.cleanup()+++def test_a_tmpdir_outside_the_workdir_is_replaced_by_the_private_tmpfs(tmp_path):+ plan = ToolLaunchPlan(+ argv = ("/bin/true",),+ workdir = str(tmp_path),+ env = {"PATH": "/usr/bin", "TMPDIR": "/var/tmp/somewhere-else"},+ )+ launch = sandbox_linux.prepare(plan)+ try:+ assert launch.argv[launch.argv.index("TMPDIR") + 1] == "/tmp"+ finally:+ launch.cleanup()+++def test_the_outer_setsid_preexec_is_preserved(tmp_path):+ ran = []+ launch = sandbox_linux.prepare(_plan(tmp_path, preexec_fn = lambda: ran.append("plan")))+ try:+ # --new-session covers the inside of the jail only, and tools.py kills+ # with killpg. Composed with the Landlock scope, so what is asserted is+ # that it RUNS, and first, not that it is the same object.+ assert launch.preexec_fn is not None+ launch.preexec_fn()+ assert ran == ["plan"]+ finally:+ launch.cleanup()+++def test_the_plan_policy_fields_survive_preparation(tmp_path):+ launch = sandbox_linux.prepare(_plan(tmp_path, timeout_seconds = 42, terminate_descendants = False))+ try:+ assert launch.timeout_seconds == 42+ assert launch.terminate_descendants is False+ assert launch.backend == sandbox_linux.BACKEND_NAME+ finally:+ launch.cleanup()+++def test_cleanup_releases_the_descriptor_and_the_private_identity_directory(tmp_path):+ launch = sandbox_linux.prepare(_plan(tmp_path))+ identity_dir = launch.cleanup_paths[0]+ handle = launch.owned_files[0]+ launch.cleanup()+ assert handle.closed+ assert not os.path.exists(identity_dir)+ assert launch.cleanup_diagnostics == []+++def test_the_backend_declares_the_two_things_it_does_not_confine():+ assert "unrestricted_network" in sandbox_linux.LIMITATIONS+ assert "model_cache_writable" in sandbox_linux.LIMITATIONS+ assert isinstance(sandbox_linux.LIMITATIONS, tuple)+ assert sandbox_linux.BACKEND_NAME and sandbox_linux.PROFILE_ID+++def test_a_unix_socket_under_the_workdir_is_refused():+ # Not tmp_path: sun_path is 108 bytes and pytest's per-test directory+ # can exceed it on its own.+ workdir = tempfile.mkdtemp(prefix = "sbx-")+ listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)+ try:+ listener.bind(os.path.join(workdir, "s"))+ with pytest.raises(SandboxUnavailableError, match = "device or IPC node"):+ sandbox_linux._validate_workdir(workdir)+ finally:+ listener.close()+ shutil.rmtree(workdir, ignore_errors = True)+++def test_a_fifo_under_the_workdir_is_refused(tmp_path):+ os.mkfifo(str(tmp_path / "pipe"))+ with pytest.raises(SandboxUnavailableError, match = "device or IPC node"):+ sandbox_linux._validate_workdir(str(tmp_path))+++def test_a_file_hard_linked_from_outside_the_workdir_is_refused(tmp_path):+ outside, workdir = tmp_path / "outside", tmp_path / "session"+ outside.mkdir()+ workdir.mkdir()+ (outside / "secret").write_text("x")+ # The workdir is bound read-write, so this second name is a writable+ # path out of it.+ os.link(str(outside / "secret"), str(workdir / "innocent"))+ with pytest.raises(SandboxUnavailableError, match = "hard-linked from outside"):+ sandbox_linux._validate_workdir(str(workdir))+++def test_a_hard_link_wholly_inside_the_workdir_is_allowed(tmp_path):+ (tmp_path / "a").write_text("x")+ os.link(str(tmp_path / "a"), str(tmp_path / "b"))+ (tmp_path / "nested").mkdir()+ os.link(str(tmp_path / "a"), str(tmp_path / "nested" / "c"))+ assert sandbox_linux._validate_workdir(str(tmp_path)) == os.path.realpath(tmp_path)+++def test_a_nested_host_mount_under_the_workdir_is_refused(tmp_path, monkeypatch):+ nested = tmp_path / "mounted"+ nested.mkdir()+ real = os.path.ismount+ monkeypatch.setattr(+ os.path, "ismount", lambda path: os.path.samefile(path, nested) or real(path)+ )+ with pytest.raises(SandboxUnavailableError, match = "nested host mount"):+ sandbox_linux._validate_workdir(str(tmp_path))+++def test_the_workdir_itself_being_a_mount_point_is_allowed(tmp_path, monkeypatch):+ real = os.path.ismount+ monkeypatch.setattr(+ os.path, "ismount", lambda path: os.path.samefile(path, tmp_path) or real(path)+ )+ assert sandbox_linux._validate_workdir(str(tmp_path)) == os.path.realpath(tmp_path)+++def test_a_workdir_too_large_to_check_is_refused_rather_than_accepted_unchecked(+ tmp_path, monkeypatch+):+ monkeypatch.setattr(os_sandbox, "WORKDIR_SCAN_ENTRIES", 2)+ for name in ("a", "b", "c", "d"):+ (tmp_path / name).write_text("")+ with pytest.raises(SandboxUnavailableError, match = "too large"):+ sandbox_linux._validate_workdir(str(tmp_path))+++def test_a_workdir_that_is_not_a_directory_is_refused(tmp_path):+ with pytest.raises(SandboxUnavailableError, match = "not a safe canonical directory"):+ sandbox_linux._validate_workdir(str(tmp_path / "missing"))+ with pytest.raises(SandboxUnavailableError, match = "not a safe canonical directory"):+ sandbox_linux._validate_workdir("/")+++def test_a_symlinked_directory_under_the_workdir_is_not_followed(tmp_path):+ # Following it would scan /dev and refuse every launch; the bind does+ # not follow it either, so the link dangles inside the jail.+ (tmp_path / "escape").symlink_to("/dev")+ assert sandbox_linux._validate_workdir(str(tmp_path)) == os.path.realpath(tmp_path)+++def test_every_interpreter_path_this_python_imports_from_is_bound(prepared, tmp_path):+ bound = [+ source+ for flag in ("--ro-bind", "--ro-bind-try", "--bind")+ for source, _ in _pairs(prepared.argv, flag)+ ]+ prefixes = (sys.prefix, sys.base_prefix, sys.exec_prefix, sys.base_exec_prefix)+ paths = sysconfig.get_paths()+ needed = {+ paths[key] for key in ("stdlib", "platstdlib", "purelib", "platlib") if paths.get(key)+ }+ # Only sys.path entries in a lib directory of the interpreter; whatever+ # else PYTHONPATH inherited is deliberately left outside.+ needed |= {+ entry+ for entry in sys.path+ if entry and any(sandbox_linux._within(entry, os.path.join(p, "lib")) for p in prefixes)+ }+ for path in sorted(needed):+ if not os.path.exists(path):+ continue+ assert any(sandbox_linux._within(path, root) for root in bound), path+++def test_a_venv_at_a_project_root_does_not_expose_the_project(tmp_path, monkeypatch):+ project = tmp_path / "project"+ (project / "lib").mkdir(parents = True)+ (project / "bin").mkdir()+ (project / "src").mkdir()+ (project / ".env").write_text("TOKEN=secret")+ for name in ("prefix", "exec_prefix"):+ monkeypatch.setattr(sys, name, str(project))+ roots = tuple(path for path in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(path))+ paths = sandbox_linux._runtime_read_paths(os.path.realpath(tmp_path / "wd"), roots)+ assert os.path.realpath(project) not in paths+ assert os.path.realpath(project / "lib") in paths+ assert not any(sandbox_linux._within(str(project / "src"), path) for path in paths)+++def test_an_interpreter_path_resolving_to_the_filesystem_root_is_refused(tmp_path, monkeypatch):+ (tmp_path / "lib").symlink_to("/")+ monkeypatch.setattr(sys, "prefix", str(tmp_path))+ with pytest.raises(SandboxUnavailableError, match = "filesystem root"):+ sandbox_linux._runtime_read_paths(str(tmp_path / "wd"), ("/usr/lib",))+++def test_a_system_interpreter_adds_nothing_the_system_roots_do_not_already_cover(+ tmp_path, monkeypatch+):+ monkeypatch.setattr(sys, "prefix", "/usr")+ monkeypatch.setattr(sys, "base_prefix", "/usr")+ roots = tuple(path for path in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(path))+ for path in sandbox_linux._runtime_read_paths(str(tmp_path), roots):+ assert path not in ("/", "/usr", "/usr/local")+++def test_runtime_paths_inside_the_workdir_are_left_to_the_writable_bind(tmp_path, monkeypatch):+ inner = tmp_path / "venv"+ (inner / "lib").mkdir(parents = True)+ monkeypatch.setattr(sys, "prefix", str(inner))+ roots = tuple(path for path in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(path))+ paths = sandbox_linux._runtime_read_paths(os.path.realpath(tmp_path), roots)+ assert os.path.realpath(inner / "lib") not in paths+++def test_the_probe_builds_its_launch_through_the_real_argv_builder(tmp_path):+ if sandbox_linux.shutil.which("bwrap") is None:+ pytest.skip("bubblewrap is not installed on this host")+ launch = sandbox_linux.probe_argv(str(tmp_path), ("/bin/true", "-x"), {"PATH": "/usr/bin"})+ try:+ reference = sandbox_linux.prepare(_plan(tmp_path, argv = ("/bin/true", "-x")))+ try:++ def normalise(argv, launch):+ fd = str(argv[argv.index("--seccomp") + 1])+ identity = launch.cleanup_paths[0]+ return tuple(item.replace(identity, "<identity>") for item in argv if item != fd)++ assert normalise(launch.argv, launch) == normalise(reference.argv, reference)+ finally:+ reference.cleanup()+ finally:+ launch.cleanup()+++_AUDIT_ARCH = sandbox_seccomp._ABIS.get(platform.machine().lower(), (0,))[0]+_KILL, _ALLOW = 0x80000000, 0x7FFF0000+_EPERM, _ENOSYS = sandbox_seccomp._EPERM, sandbox_seccomp._ENOSYS++if _AUDIT_ARCH == 0:+ pytest.skip(f"no seccomp ABI for {platform.machine()}", allow_module_level = True)+++def _evaluate(+ instructions,+ *,+ nr,+ arch = _AUDIT_ARCH,+ args = (0,) * 6,+):+ data = struct.pack("=IIQ6Q", nr, arch, 0, *args)+ accumulator, counter = 0, 0+ for _ in range(len(instructions) * 4):+ code, jt, jf, k = instructions[counter]+ counter += 1+ if code == 0x20: # BPF_LD | BPF_W | BPF_ABS+ (accumulator,) = struct.unpack_from("=I", data, k)+ elif code == 0x15: # BPF_JMP | BPF_JEQ | BPF_K+ counter += jt if accumulator == k else jf+ elif code == 0x45: # BPF_JMP | BPF_JSET | BPF_K+ counter += jt if accumulator & k else jf+ elif code == 0x06: # BPF_RET | BPF_K+ return k+ else:+ raise AssertionError(f"unexpected BPF opcode {code:#x}")+ raise AssertionError("the program ran off the end without returning")+++def _share_cache(monkeypatch, cache):+ """Share every subdirectory of *cache* that exists, as the real resolver would.++ The real _model_cache_binds asks the cache-settings layer, which is a+ different unit and not what these tests are about.+ """+ monkeypatch.setattr(+ sandbox_linux,+ "_model_cache_binds",+ lambda workdir: {+ name: str(cache / name)+ for name in sandbox_linux._MODEL_CACHE_SUBDIRS+ if (cache / name).is_dir()+ },+ )+++def cache(tmp_path, monkeypatch):+ """A host cache with one populated subdirectory, shared into the jail."""+ directory = tmp_path / "hostcache"+ (directory / "hub").mkdir(parents = True)+ _share_cache(monkeypatch, directory)+ return directory++[email protected](params = [False, True], ids = ["userns_by_bwrap", "userns_by_seccomp"])+def program(request):+ return sandbox_seccomp.program(platform.machine(), block_userns = request.param)+++def _socket_nr():+ return sandbox_seccomp._ABIS[platform.machine().lower()][1]+++def test_a_vsock_socket_is_denied_because_no_mount_namespace_can_hide_it(program):+ assert _evaluate(program, nr = _socket_nr(), args = (40, 0, 0, 0, 0, 0)) == _EPERM+++def test_ordinary_sockets_stay_allowed_because_the_network_is_not_confined(program):+ for family in (1, 2, 10): # AF_UNIX, AF_INET, AF_INET6+ assert _evaluate(program, nr = _socket_nr(), args = (family, 0, 0, 0, 0, 0)) == _ALLOW+++def test_a_vsock_socketpair_is_denied_too(program):+ socketpair_nr = sandbox_seccomp._ABIS[platform.machine().lower()][2]+ assert _evaluate(program, nr = socketpair_nr, args = (40, 0, 0, 0, 0, 0)) == _EPERM+ assert _evaluate(program, nr = socketpair_nr, args = (1, 0, 0, 0, 0, 0)) == _ALLOW+++def test_io_uring_is_denied_on_all_three_entry_points(program):+ for number in (425, 426, 427):+ assert _evaluate(program, nr = number) == _EPERM+++def test_the_keyring_syscalls_are_denied(program):+ for number in sandbox_seccomp._KEYRING_SYSCALLS[platform.machine().lower()]:+ assert _evaluate(program, nr = number) == _EPERM+++def test_an_unrelated_syscall_is_allowed(program):+ assert _evaluate(program, nr = 1) == _ALLOW # write on x86_64, close on aarch64+++def test_a_foreign_abi_is_killed_rather_than_evaluated(program):+ # Argument offsets differ per ABI, so a guess inspects the wrong bytes.+ assert _evaluate(program, nr = 1, arch = 0xDEADBEEF) == _KILL++[email protected](platform.machine().lower() not in ("x86_64", "amd64"), reason = "x32 is x86 only")+def test_the_x32_syscall_table_is_killed(program):+ assert _evaluate(program, nr = 1 | 0x40000000) == _KILL+++def test_nested_user_namespaces_are_refused_only_when_bwrap_cannot_do_it():+ clone_nr, unshare_nr, clone3_nr = sandbox_seccomp._USERNS_SYSCALLS[platform.machine().lower()]+ blocking = sandbox_seccomp.program(platform.machine(), block_userns = True)+ assert _evaluate(blocking, nr = unshare_nr, args = (0x10000000, 0, 0, 0, 0, 0)) == _EPERM+ # unshare() is judged on its flags like clone() below. A blanket refusal also+ # took CLONE_FS and CLONE_FILES, which do not create a user namespace and+ # work everywhere outside the jail.+ for flags in (0x00000200, 0x00000400, 0x00040000): # CLONE_FS, CLONE_FILES, CLONE_SYSVSEM+ assert _evaluate(blocking, nr = unshare_nr, args = (flags, 0, 0, 0, 0, 0)) == _ALLOW+ # ENOSYS, so glibc falls back to clone() where the flags word is checkable.+ assert _evaluate(blocking, nr = clone3_nr) == _ENOSYS+ assert _evaluate(blocking, nr = clone_nr, args = (0x10000000, 0, 0, 0, 0, 0)) == _EPERM+ assert _evaluate(blocking, nr = clone_nr, args = (0x00000100, 0, 0, 0, 0, 0)) == _ALLOW++ permissive = sandbox_seccomp.program(platform.machine(), block_userns = False)+ assert _evaluate(permissive, nr = unshare_nr, args = (0x10000000, 0, 0, 0, 0, 0)) == _ALLOW+ assert _evaluate(permissive, nr = clone3_nr) == _ALLOW+++class _SockFilter(ctypes.Structure):+ _fields_ = [+ ("code", ctypes.c_ushort),+ ("jt", ctypes.c_ubyte),+ ("jf", ctypes.c_ubyte),+ ("k", ctypes.c_uint32),+ ]+++class _SockFprog(ctypes.Structure):+ _fields_ = [("len", ctypes.c_ushort), ("filter", ctypes.POINTER(_SockFilter))]+++def _kernel_verdicts(block_userns):+ """Each probe's errno from a fork child, 0 meaning allowed. Installed through+ prctl, not bwrap, which cannot start where user namespaces are denied."""+ clone3_nr, unshare_nr = 435, sandbox_seccomp._USERNS_SYSCALLS[platform.machine().lower()][1]+ keyctl_nr = sandbox_seccomp._KEYRING_SYSCALLS[platform.machine().lower()][2]+ read_fd, write_fd = os.pipe()+ pid = os.fork()+ if pid == 0: # pragma: no cover - this child never returns into pytest+ os.close(read_fd)+ try:+ instructions = sandbox_seccomp.program(platform.machine(), block_userns = block_userns)+ block = (_SockFilter * len(instructions))(+ *(_SockFilter(*item) for item in instructions)+ )+ libc = ctypes.CDLL(None, use_errno = True)+ libc.syscall.restype = ctypes.c_long+ if libc.prctl(38, 1, 0, 0, 0) != 0: # PR_SET_NO_NEW_PRIVS+ os._exit(97)+ fprog = _SockFprog(len(instructions), block)+ if libc.prctl(22, 2, ctypes.byref(fprog), 0, 0) != 0: # PR_SET_SECCOMP, FILTER+ os._exit(98)+ verdicts = {}+ for name, family in (("inet", socket.AF_INET), ("unix", socket.AF_UNIX), ("vsock", 40)):+ try:+ socket.socket(family, socket.SOCK_STREAM).close()+ verdicts[name] = 0+ except OSError as error:+ verdicts[name] = error.errno+ for name, number, argument in (+ ("io_uring", 425, 0),+ ("keyctl", keyctl_nr, 0),+ ("clone3", clone3_nr, None),+ ("unshare_userns", unshare_nr, 0x10000000),+ ("unshare_fs", unshare_nr, 0x00000200), # CLONE_FS: not a user namespace+ ):+ ctypes.set_errno(0)+ result = libc.syscall(number, argument, 0, 0)+ verdicts[name] = 0 if result >= 0 else ctypes.get_errno()+ child = os.fork() # an ordinary fork must survive the userns rules+ if child == 0:+ os._exit(0)+ verdicts["fork"] = os.waitpid(child, 0)[1]+ os.write(write_fd, json.dumps(verdicts).encode())+ os._exit(0)+ except BaseException:+ os._exit(99)+ os.close(write_fd)+ with os.fdopen(read_fd, "rb") as stream:+ payload = stream.read()+ status = os.waitpid(pid, 0)[1]+ assert status == 0, f"the seccomp child exited with status {status:#x}"+ return json.loads(payload)+++def test_the_kernel_loads_the_filter_and_denies_the_channels_it_names():+ for block_userns in (False, True):+ verdicts = _kernel_verdicts(block_userns)+ assert verdicts["vsock"] == errno.EPERM+ assert verdicts["io_uring"] == errno.EPERM+ assert verdicts["keyctl"] == errno.EPERM+ # A filter that broke sockets would make the "filesystem only" claim+ # false in the other direction.+ assert verdicts["inet"] == 0 and verdicts["unix"] == 0+ assert verdicts["fork"] == 0+++def test_the_kernel_refuses_nested_user_namespaces_only_in_the_fallback_filter():+ # clone3 is the honest discriminator: a host that denies user namespaces+ # returns EPERM from unshare anyway, but only this filter makes clone3+ # report ENOSYS.+ assert _kernel_verdicts(block_userns = True)["clone3"] == errno.ENOSYS+ blocking = _kernel_verdicts(block_userns = True)+ assert blocking["unshare_userns"] == errno.EPERM+ # And only that flag: the filter is for nested user namespaces, so an+ # unshare the host allows must not start failing inside the jail.+ assert blocking["unshare_fs"] == 0+ assert _kernel_verdicts(block_userns = False)["clone3"] != errno.ENOSYS+++def test_an_unreviewed_architecture_is_refused_rather_than_left_unfiltered():+ with pytest.raises(RuntimeError, match = "little-endian"):+ sandbox_seccomp.program("riscv64")+++def test_the_filter_file_holds_exactly_the_program_and_is_rewound():+ stream = sandbox_seccomp.filter_file(block_userns = True)+ try:+ assert stream.tell() == 0+ payload = stream.read()+ assert payload == sandbox_seccomp.program_bytes(block_userns = True)+ assert len(payload) % 8 == 0 # struct sock_filter is 8 bytes+ instructions = sandbox_seccomp.program(platform.machine(), block_userns = True)+ assert len(payload) // 8 == len(instructions)+ assert struct.unpack_from("=HBBI", payload, 0) == instructions[0]+ finally:+ stream.close()+++def test_a_runtime_path_symlinked_out_of_the_workdir_is_not_bound(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ workdir.mkdir()+ secret = tmp_path / "secrets"+ secret.mkdir()+ (secret / "id_rsa").write_text("PRIVATE KEY")+ venv = workdir / "venv"+ venv.mkdir()+ (venv / "lib").symlink_to(secret)+ monkeypatch.setattr(sys, "prefix", str(venv))++ paths = sandbox_linux._runtime_read_paths(str(workdir), ("/usr/lib",))+ assert str(secret) not in paths+ assert not any(sandbox_linux._within(str(secret), path) for path in paths)+++def test_a_symlinked_cache_ancestor_is_refused_rather_than_written_through(tmp_path, cache):+ """Refused rather than unlinked: symlinking a cache leaf at another volume is a+ legitimate layout."""+ workdir = tmp_path / "session"+ workdir.mkdir()+ outside = tmp_path / "outside"+ outside.mkdir()+ (workdir / ".cache").symlink_to(outside)++ with pytest.raises(SandboxUnavailableError, match = "not a plain directory"):+ sandbox_linux.prepare(_plan(workdir))+ assert (workdir / ".cache").is_symlink()+ assert sorted(entry.name for entry in outside.iterdir()) == []+++def test_the_probe_launch_sets_no_new_privs_like_a_real_one(tmp_path):+ from core.inference import sandbox_probe++ assert sandbox_probe._PR_SET_NO_NEW_PRIVS == 38+ source = inspect.getsource(sandbox_probe._run_probe)+ assert "preexec_fn = _no_new_privs" in source+ # Resolved at import: an import in the forked child can deadlock on a+ # lock a thread held at fork time.+ assert not [+ node+ for node in ast.walk(ast.parse(inspect.getsource(sandbox_probe._no_new_privs)))+ if isinstance(node, (ast.Import, ast.ImportFrom))+ ]+++def test_the_pip_targets_script_directory_is_last_on_path(prepared):+ argv = prepared.argv+ entries = argv[argv.index("PATH") + 1].split(os.pathsep)+ packages = os.path.join(prepared.workdir, sandbox_linux.SESSION_PACKAGES_RELPATH)+ assert entries[-1] == os.path.join(packages, "bin")+ assert "/usr/bin" in entries[:-1]+++def test_the_compiler_headers_a_source_build_needs_are_readable(prepared):+ sources = [source for source, _ in _pairs(prepared.argv, "--ro-bind-try")]+ assert "/usr/include" in sources+ assert "/usr/local/include" in sources+ include = sysconfig.get_paths().get("include")+ if include and os.path.isdir(include):+ bound = [source for source, _ in _pairs(prepared.argv, "--ro-bind")]+ assert any(sandbox_linux._within(include, path) for path in (*sources, *bound)), include+++def test_a_runtime_prefix_contributes_its_git_helpers(tmp_path, monkeypatch):+ prefix = tmp_path / "conda"+ for name in ("bin", "libexec", "lib"):+ (prefix / name).mkdir(parents = True)+ monkeypatch.setattr(sys, "prefix", str(prefix))+ paths = sandbox_linux._runtime_read_paths(str(tmp_path / "session"), ("/usr/lib",))+ assert str(prefix / "libexec") in paths+ assert str(prefix) not in paths+++def test_the_launch_pre_exec_scopes_abstract_sockets(tmp_path):+ if not sandbox_landlock.abstract_scope_supported():+ pytest.skip("this kernel predates the Landlock abstract-socket scope")+ name = b"\0unsloth-abstract-probe-" + os.urandom(6).hex().encode()+ listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)+ listener.bind(name)+ listener.listen(1)+ read_fd, write_fd = os.pipe()+ try:+ child = os.fork()+ if child == 0: # pragma: no cover - runs in the forked child+ try:+ os.close(read_fd)+ sandbox_landlock.with_abstract_scope(None)()+ probe = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)+ probe.settimeout(2)+ try:+ probe.connect(name)+ os.write(write_fd, b"connected")+ except OSError as error:+ os.write(write_fd, str(error.errno).encode())+ finally:+ os._exit(0)+ os.close(write_fd)+ write_fd = -1+ with os.fdopen(read_fd, "rb") as stream:+ verdict = stream.read()+ read_fd = -1+ os.waitpid(child, 0)+ assert verdict == str(errno.EPERM).encode(), verdict+ # Positive control: unscoped, this host reaches that socket, so the+ # refusal above is the scope and not an unreachable socket.+ control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)+ control.settimeout(2)+ control.connect(name)+ control.close()+ finally:+ for fd in (read_fd, write_fd):+ if fd >= 0:+ os.close(fd)+ listener.close()+++def test_the_backend_says_so_when_the_kernel_cannot_scope_them(monkeypatch):+ supported = sandbox_landlock.abstract_scope_supported()+ assert ("host_abstract_sockets_reachable" in sandbox_linux.LIMITATIONS) is not supported+++def test_a_landlock_probe_child_that_never_answers_does_not_hang_the_server(monkeypatch):+ if not sandbox_landlock._abi_reports_scope():+ pytest.skip("no Landlock ABI 6 here, so the fork under test never happens")++ def never_answers():+ # Holds the write end open and never writes, which is what a child+ # deadlocked after fork looks like from out here.+ time.sleep(30)++ monkeypatch.setattr(sandbox_landlock, "apply_abstract_scope", never_answers)+ monkeypatch.setattr(sandbox_landlock, "_PROBE_TIMEOUT_SECONDS", 1.0)+ sandbox_landlock.abstract_scope_supported.cache_clear()+ try:+ started = time.monotonic()+ assert sandbox_landlock.abstract_scope_supported() is False+ assert time.monotonic() - started < 10, "the deadline did not fire"+ finally:+ sandbox_landlock.abstract_scope_supported.cache_clear()+++def test_the_plan_pre_exec_still_runs_before_the_scope():+ ran = []+ composed = sandbox_landlock.with_abstract_scope(lambda: ran.append("plan"))+ composed()+ assert ran == ["plan"]+++def test_a_standalone_interpreter_in_the_workdir_is_re_bound_read_only(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ workdir.mkdir()+ executable = workdir / "python"+ executable.write_bytes(b"#!/bin/sh\nexit 0\n")+ executable.chmod(0o755)+ for attribute in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):+ monkeypatch.setattr(sys, attribute, str(workdir))+ monkeypatch.setattr(sys, "executable", str(executable))++ assert sandbox_linux._runtime_paths_under(str(workdir)) == (str(executable),)+ launch = sandbox_linux.prepare(_plan(workdir))+ try:+ argv = list(launch.argv)+ read_only = [argv[i + 1] for i, item in enumerate(argv) if item == "--ro-bind"]+ assert str(executable) in read_only, read_only+ # After the writable bind, or it is hidden by it.+ assert argv.index("--ro-bind", argv.index("--bind")) > argv.index("--bind")+ finally:+ launch.cleanup()+++def test_an_interpreter_in_a_home_directory_does_not_bind_the_home(tmp_path, monkeypatch):+ home = tmp_path / "alice"+ (home / ".ssh").mkdir(parents = True)+ (home / ".ssh" / "id_rsa").write_text("SECRET", encoding = "utf-8")+ executable = home / "python"+ executable.write_bytes(b"#!/bin/sh\nexit 0\n")+ executable.chmod(0o755)+ monkeypatch.setattr(sys, "executable", str(executable))++ paths = sandbox_linux._runtime_read_paths(str(tmp_path / "session"), ("/usr/lib",))+ assert str(home) not in paths, paths+ assert not any(_within_for_test(p, str(home / ".ssh")) for p in paths), paths+ # The interpreter itself is still reachable, or nothing runs in there.+ assert str(executable) in paths, paths+++def _within_for_test(path: str, root: str) -> bool:+ return path == root or path.startswith(root.rstrip("/") + "/")+++def test_a_runtime_origin_is_excluded_under_the_workdir_alias_too(tmp_path, monkeypatch):+ real = tmp_path / "real"+ real.mkdir()+ alias = tmp_path / "alias"+ alias.symlink_to(real)+ secret = tmp_path / "secrets"+ secret.mkdir()+ (secret / "id_rsa").write_text("SECRET", encoding = "utf-8")+ venv = real / "venv"+ (venv / "bin").mkdir(parents = True)+ (venv / "lib").symlink_to(secret)+ for attribute in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):+ monkeypatch.setattr(sys, attribute, str(alias / "venv"))++ paths = sandbox_linux._runtime_read_paths(os.path.realpath(real), ("/usr/lib",), str(alias))+ assert not any(os.path.realpath(p) == str(secret) for p in paths), paths+ # And through prepare(), which is what passes the second spelling.+ launch = sandbox_linux.prepare(_plan(alias))+ try:+ argv = list(launch.argv)+ bound = [+ argv[i + 1] for i, item in enumerate(argv) if item in ("--ro-bind", "--ro-bind-try")+ ]+ assert not any(os.path.realpath(p) == str(secret) for p in bound), bound+ finally:+ launch.cleanup()+++def test_an_editable_source_inside_the_workdir_is_re_bound_read_only(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ package = workdir / "mypkg"+ package.mkdir(parents = True)+ (package / "__init__.py").write_text("", encoding = "utf-8")+ monkeypatch.setattr(sandbox_linux, "editable_source_roots", lambda: (str(package),))++ assert str(package) in sandbox_linux._runtime_paths_under(str(workdir))+ assert str(package) not in sandbox_linux._runtime_read_paths(str(workdir), ("/usr/lib",))+++def test_the_interpreter_spelling_the_launch_execs_is_bound(tmp_path, monkeypatch):+ target_bin = tmp_path / "opt" / "py" / "bin"+ target_bin.mkdir(parents = True)+ target = target_bin / "python3.13"+ target.write_bytes(b"#!/bin/sh\nexit 0\n")+ target.chmod(0o755)+ user_bin = tmp_path / "home" / "bin"+ user_bin.mkdir(parents = True)+ link = user_bin / "python"+ link.symlink_to(target)+ monkeypatch.setattr(sys, "executable", str(link))+ for attribute in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):+ monkeypatch.setattr(sys, attribute, str(tmp_path / "opt" / "py"))++ paths = sandbox_linux._runtime_read_paths(str(tmp_path / "session"), ("/usr/lib",))+ assert str(link) in paths, paths+ assert str(target) in paths or str(target_bin) in paths, paths+ # The symlink's own directory is not granted, only the file in it.+ assert str(user_bin) not in paths, paths+++def test_a_wedged_cache_mount_drops_the_cache_instead_of_hanging_the_launch(tmp_path, monkeypatch):+ cache = tmp_path / "hostcache"+ (cache / "hub").mkdir(parents = True)+ _share_cache_paths(monkeypatch, cache)++ def wedged(path):+ time.sleep(30)+ raise AssertionError("the caller should not have waited for this")++ monkeypatch.setattr(sandbox_linux, "_inspect_cache_component", lambda name, path: wedged(path))+ monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 1.0)+ started = time.monotonic()+ binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))+ assert time.monotonic() - started < 10, "the deadline did not fire"+ assert binds == {}, binds+++def _share_cache_paths(monkeypatch, cache):+ import types++ class _Paths:+ cache_home = str(cache)+ hub_cache = str(cache / "hub")+ xet_cache = str(cache / "xet")++ module = types.ModuleType("utils.hf_cache_settings")+ module.get_hf_cache_paths = lambda: _Paths()+ monkeypatch.setitem(sys.modules, "utils.hf_cache_settings", module)+++def test_a_symlinked_runtime_entry_inside_the_workdir_is_protected_by_name(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ target = workdir / "real_pkg"+ target.mkdir(parents = True)+ (target / "__init__.py").write_text("", encoding = "utf-8")+ entry = workdir / "mypkg"+ entry.symlink_to(target)+ monkeypatch.setattr(sandbox_linux, "editable_source_roots", lambda: (str(entry),))++ protected = sandbox_linux._runtime_paths_under(str(workdir))+ assert str(entry) in protected, protected+ assert str(target) in protected, protected+++def test_a_wedged_cache_path_is_not_re_scanned_by_every_later_launch(tmp_path, monkeypatch):+ cache = tmp_path / "hostcache"+ (cache / "hub").mkdir(parents = True)+ _share_cache_paths(monkeypatch, cache)+ started: list[str] = []++ def wedged(name, path):+ started.append(path)+ time.sleep(30)++ monkeypatch.setattr(sandbox_linux, "_inspect_cache_component", wedged)+ monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 0.5)+ monkeypatch.setattr(sandbox_linux, "_cache_scan_pending", {})+ session = str(tmp_path / "session")+ assert sandbox_linux._model_cache_binds(session) == {}+ first = len(started)+ assert first > 0+ assert sandbox_linux._model_cache_binds(session) == {}+ assert len(started) == first, "a second launch started another worker on the same path"+++def test_a_runtime_entry_whose_target_leaves_the_workdir_gets_no_rule(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ workdir.mkdir()+ private = tmp_path / "private"+ private.mkdir()+ (private / "id_rsa").write_text("SECRET", encoding = "utf-8")+ (workdir / "venv").mkdir()+ (workdir / "venv" / "lib").symlink_to(private)+ monkeypatch.setattr(sys, "prefix", str(workdir / "venv"))+ monkeypatch.setattr(sys, "exec_prefix", str(workdir / "venv"))++ protected = sandbox_linux._runtime_paths_under(str(workdir))+ assert str(workdir / "venv" / "lib") not in protected, protected+ assert not any(os.path.realpath(p) == str(private) for p in protected), protected+++def test_an_interpreter_symlinked_out_of_the_workdir_fails_the_call(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ workdir.mkdir()+ outside = tmp_path / "elsewhere"+ outside.mkdir()+ real = outside / "python"+ real.write_bytes(b"#!/bin/sh\nexit 0\n")+ real.chmod(0o755)+ link = workdir / "python"+ link.symlink_to(real)+ monkeypatch.setattr(sys, "executable", str(link))++ with pytest.raises(WorkdirUnsafeError, match = "Python that runs Studio"):+ sandbox_linux._runtime_paths_under(str(workdir))+++def test_concurrent_launches_start_one_cache_worker_and_never_raise(tmp_path, monkeypatch):+ cache = tmp_path / "hostcache"+ (cache / "hub").mkdir(parents = True)+ _share_cache_paths(monkeypatch, cache)+ started: list[str] = []+ gate = threading.Event()++ def wedged(name, path):+ started.append(path)+ gate.wait(30)++ monkeypatch.setattr(sandbox_linux, "_inspect_cache_component", wedged)+ monkeypatch.setattr(sandbox_linux, "_CACHE_INSPECT_SECONDS", 0.5)+ monkeypatch.setattr(sandbox_linux, "_cache_scan_pending", {})+ session = str(tmp_path / "session")+ errors: list[BaseException] = []++ def launch() -> None:+ try:+ sandbox_linux._model_cache_binds(session)+ except BaseException as exc: # noqa: BLE001 - the point of the test+ errors.append(exc)++ threads = [threading.Thread(target = launch) for _ in range(8)]+ for thread in threads:+ thread.start()+ for thread in threads:+ thread.join(60)+ gate.set()+ assert not errors, errors+ # One worker per distinct component, never a second for a path already being+ # inspected: _model_cache_binds walks four components, so four is correct and+ # a duplicate is the race.+ assert started, "the inspection never ran"+ assert len(started) == len(set(started)), f"a path was scanned twice: {sorted(started)}"+++def test_a_runtime_prefix_contributes_its_certificate_store(tmp_path, monkeypatch):+ prefix = tmp_path / "conda"+ for name in ("bin", "ssl", "lib"):+ (prefix / name).mkdir(parents = True)+ monkeypatch.setattr(sys, "prefix", str(prefix))+ paths = sandbox_linux._runtime_read_paths(str(tmp_path / "session"), ("/usr/lib",))+ assert str(prefix / "ssl") in paths+++def test_a_workdir_reached_through_a_symlink_is_bound_at_the_spelling_the_caller_used(tmp_path):+ real = tmp_path / "real"+ real.mkdir()+ link = tmp_path / "link"+ link.symlink_to(real)+ launch = sandbox_linux.prepare(_plan(link))+ try:+ binds = _pairs(launch.argv, "--bind")+ assert (str(real), str(link)) in binds+ # The canonical spelling too, so a tool that resolved a path for itself+ # can still open it.+ assert (str(real), str(real)) in binds+ assert launch.argv[launch.argv.index("--chdir") + 1] == str(link)+ assert launch.argv[launch.argv.index("HOME") + 1] == str(link)+ finally:+ launch.cleanup()+++def test_a_cache_leaf_left_behind_as_a_file_is_refused_at_preparation(tmp_path, cache):+ workdir = tmp_path / "session"+ (workdir / ".cache" / "huggingface").mkdir(parents = True)+ (workdir / ".cache" / "huggingface" / "hub").write_text("not a directory")++ with pytest.raises(SandboxUnavailableError, match = "not a plain directory"):+ sandbox_linux.prepare(_plan(workdir))+ assert (workdir / ".cache" / "huggingface" / "hub").read_text() == "not a directory"+++ os.geteuid() == 0,+ reason = "root reads and writes regardless of the mode bits, so the premise is void",+)+def test_an_unreadable_directory_is_refused(tmp_path):+ """A mode-000 directory hides a link out from the scan, and the process that+ owns it can chmod it back."""+ locked = tmp_path / "locked"+ locked.mkdir()+ (locked / "inside").write_text("x")+ locked.chmod(0o000)+ try:+ with pytest.raises(SandboxUnavailableError, match = "cannot be fully inspected"):+ sandbox_linux._validate_workdir(str(tmp_path))+ finally:+ locked.chmod(0o700)+++def test_the_cache_studio_actually_uses_is_the_one_shared(tmp_path, monkeypatch):+ """HF_HUB_CACHE=/mnt/models is NOT /mnt/models/hub, so each component is bound+ where it actually is."""+ hub = tmp_path / "mnt" / "models"+ xet = tmp_path / "fast" / "xet"+ home = tmp_path / "cachehome"+ for path in (hub, xet, home / "datasets", home / "assets"):+ path.mkdir(parents = True)+ import utils.hf_cache_settings as cache_settings++ monkeypatch.setattr(+ cache_settings,+ "get_hf_cache_paths",+ lambda: cache_settings.HuggingFaceCachePaths(+ cache_home = home, hub_cache = hub, xet_cache = xet, source = "studio"+ ),+ )+ binds = sandbox_linux._model_cache_binds(str(tmp_path / "session"))+ assert binds["hub"] == str(hub)+ assert binds["xet"] == str(xet)+ assert binds["datasets"] == str(home / "datasets")+ # And a component that would sit inside the workdir is dropped.+ assert "hub" not in sandbox_linux._model_cache_binds(str(hub.parent))+++def _real_cache(monkeypatch, home):+ """Point the settings layer at *home* and let the REAL _model_cache_binds run.++ _share_cache replaces _model_cache_binds outright, so a test that used it+ would never reach the hazard check it is about.+ """+ import types++ paths = types.SimpleNamespace(cache_home = home, hub_cache = home / "hub", xet_cache = home / "xet")+ module = types.ModuleType("utils.hf_cache_settings")+ module.get_hf_cache_paths = lambda: paths+ monkeypatch.setitem(sys.modules, "utils.hf_cache_settings", module)+++def test_a_cache_component_holding_an_ipc_node_is_not_shared(tmp_path, monkeypatch):+ host = tmp_path / "hostcache"+ (host / "hub").mkdir(parents = True)+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)+ # Relative, because an AF_UNIX address is capped at ~108 bytes and pytest's+ # tmp_path alone can exceed it.+ monkeypatch.chdir(host / "hub")+ sock.bind("leftover.sock")+ try:+ _real_cache(monkeypatch, host)+ assert "hub" not in sandbox_linux._model_cache_binds(str(tmp_path / "session"))+ finally:+ sock.close()+++def test_a_cache_component_holding_an_external_hard_link_is_not_shared(tmp_path, monkeypatch):+ """Same inode under two names, one of them outside the cache. The bind is+ writable, so without this the file outside is writable through the cache name."""+ host = tmp_path / "hostcache"+ (host / "hub").mkdir(parents = True)+ outside = tmp_path / "private.txt"+ outside.write_text("secret")+ os.link(outside, host / "hub" / "innocent.bin")+ _real_cache(monkeypatch, host)+ assert "hub" not in sandbox_linux._model_cache_binds(str(tmp_path / "session"))+++def test_a_clean_cache_component_is_still_shared(tmp_path, monkeypatch):+ """The negative control: the check above must not simply drop everything."""+ host = tmp_path / "hostcache"+ (host / "hub" / "models--x").mkdir(parents = True)+ (host / "hub" / "models--x" / "weights.bin").write_text("w")+ _real_cache(monkeypatch, host)+ assert sandbox_linux._model_cache_binds(str(tmp_path / "session"))["hub"] == str(host / "hub")+++def test_a_hazardous_cache_drops_the_component_rather_than_failing_the_launch(+ tmp_path, monkeypatch+):+ host = tmp_path / "hostcache"+ (host / "hub").mkdir(parents = True)+ os.mkfifo(host / "hub" / "pipe")+ _real_cache(monkeypatch, host)+ workdir = tmp_path / "session"+ workdir.mkdir()+ launch = sandbox_linux.prepare(_plan(workdir))+ try:+ assert "HF_HOME" not in launch.argv+ finally:+ launch.cleanup()+++def test_a_trusted_system_gitconfig_is_bound_and_an_untrusted_one_is_not(tmp_path, monkeypatch):+ good = tmp_path / "gitconfig"+ good.write_text("[http]\n")+ link = tmp_path / "linked"+ link.symlink_to(good)+ assert sandbox_linux._trusted_system_file(str(link)) is False, "a symlink is followed"+ assert sandbox_linux._trusted_system_file(str(tmp_path / "absent")) is False+ os.chmod(good, 0o666)+ assert sandbox_linux._trusted_system_file(str(good)) is False, "world-writable accepted"+++def test_a_runtime_under_the_workdir_is_re_bound_read_only(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ venv = workdir / "venv"+ (venv / "lib").mkdir(parents = True)+ (venv / "bin").mkdir()+ monkeypatch.setattr(sys, "prefix", str(venv))+ monkeypatch.setattr(sys, "exec_prefix", str(venv))++ launch = sandbox_linux.prepare(_plan(workdir))+ try:+ argv = launch.argv+ writable = argv.index("--bind")+ ro_after = [+ source+ for index, source in enumerate(argv)+ if index > writable and argv[index - 1] == "--ro-bind"+ ]+ assert str(venv / "lib") in ro_after, "the runtime stayed writable"+ assert str(venv / "bin") in ro_after+ finally:+ launch.cleanup()+++def test_a_runtime_symlinked_out_of_the_workdir_is_not_re_bound(tmp_path, monkeypatch):+ """The negative control for the rule above. A <workdir>/venv/lib aimed at the+ user's home must NOT be bound by name; inside the jail it simply dangles."""+ workdir = tmp_path / "session"+ (workdir / "venv").mkdir(parents = True)+ secret = tmp_path / "home" / ".ssh"+ secret.mkdir(parents = True)+ (workdir / "venv" / "lib").symlink_to(secret)+ monkeypatch.setattr(sys, "prefix", str(workdir / "venv"))++ launch = sandbox_linux.prepare(_plan(workdir))+ try:+ for flag in ("--bind", "--ro-bind", "--ro-bind-try"):+ for source, _ in _pairs(launch.argv, flag):+ assert not sandbox_linux._within(str(secret), source), source+ finally:+ launch.cleanup()+++def test_a_nested_bind_mount_in_the_cache_is_caught_by_the_mount_table(tmp_path, monkeypatch):+ host = tmp_path / "hostcache"+ (host / "hub" / "nested").mkdir(parents = True)+ _real_cache(monkeypatch, host)+ monkeypatch.setattr(+ sandbox_linux, "_host_mount_points", lambda: (str(host / "hub" / "nested"),)+ )+ assert "hub" not in sandbox_linux._model_cache_binds(str(tmp_path / "session"))+++def _fake_editable(+ tmp_path,+ monkeypatch,+ source: str,+ top_level: str | None = None,+):+ """A dist-info recording an editable install, the way an installer writes it."""+ site_dir = tmp_path / "sitepkgs"+ info = site_dir / "demo-1.0.dist-info"+ info.mkdir(parents = True)+ (info / "METADATA").write_text("Name: demo\nVersion: 1.0\n", encoding = "utf-8")+ (info / "RECORD").write_text("", encoding = "utf-8")+ if top_level is not None:+ (info / "top_level.txt").write_text(top_level + "\n", encoding = "utf-8")+ (info / "direct_url.json").write_text(+ json.dumps({"url": f"file://{source}", "dir_info": {"editable": True}}),+ encoding = "utf-8",+ )+ monkeypatch.syspath_prepend(str(site_dir))+ os_sandbox.editable_source_roots.cache_clear()+ return site_dir+++def test_an_editable_installs_source_root_is_readable(tmp_path, monkeypatch):+ source = tmp_path / "checkout"+ package = source / "demo"+ package.mkdir(parents = True)+ (package / "__init__.py").write_text("", encoding = "utf-8")+ # A checkout holds more than its packages, and the sandbox keeps the network.+ (source / ".env").write_text("AWS_SECRET_ACCESS_KEY=real\n", encoding = "utf-8")+ (source / "fixtures").mkdir()+ _fake_editable(tmp_path, monkeypatch, str(source))+ try:+ granted = os_sandbox.editable_source_roots()+ assert str(package) in granted, granted+ assert str(source) not in granted, granted+ assert not any("fixtures" in path or ".env" in path for path in granted), granted+ roots = tuple(p for p in sandbox_linux._SYSTEM_ROOTS if os.path.isdir(p))+ read = sandbox_linux._runtime_read_paths(str(tmp_path / "wd"), roots)+ assert str(package) in read+ assert str(source) not in read+ finally:+ os_sandbox.editable_source_roots.cache_clear()+++def test_a_guessed_editable_import_root_gives_up_what_it_cannot_confirm(tmp_path, monkeypatch):+ source = tmp_path / "checkout"+ package = source / "demo"+ package.mkdir(parents = True)+ (package / "__init__.py").write_text("", encoding = "utf-8")+ (source / "deploy.py").write_text("AWS_SECRET_ACCESS_KEY = 'real'\n", encoding = "utf-8")+ (source / "conftest.py").write_text("", encoding = "utf-8")+ tests_pkg = source / "tests"+ tests_pkg.mkdir()+ (tests_pkg / "__init__.py").write_text("", encoding = "utf-8")+ (tests_pkg / "fixtures").mkdir()+ (tests_pkg / "fixtures" / "id_rsa").write_text("-----BEGIN-----", encoding = "utf-8")+ _fake_editable(tmp_path, monkeypatch, str(source), top_level = "demo")+ try:+ granted = os_sandbox.editable_source_roots()+ assert granted == (str(package),), granted+ finally:+ os_sandbox.editable_source_roots.cache_clear()+++def test_a_declared_package_symlinked_out_of_the_checkout_is_refused(tmp_path, monkeypatch):+ source = tmp_path / "checkout"+ source.mkdir()+ private = tmp_path / "private"+ private.mkdir()+ (private / "id_rsa").write_text("SECRET", encoding = "utf-8")+ (private / "__init__.py").write_text("", encoding = "utf-8")+ (source / "demo").symlink_to(private)+ _fake_editable(tmp_path, monkeypatch, str(source), top_level = "demo")+ try:+ granted = os_sandbox.editable_source_roots()+ assert granted == (), granted+ finally:+ os_sandbox.editable_source_roots.cache_clear()+++def test_an_editable_root_at_the_filesystem_root_is_refused(tmp_path, monkeypatch):+ """The negative control. An editable install rooted at / or /usr would hand+ back most of the host, which is the guard the runtime paths already apply."""+ _fake_editable(tmp_path, monkeypatch, "/usr")+ try:+ assert os_sandbox.editable_source_roots() == ()+ finally:+ os_sandbox.editable_source_roots.cache_clear()+++def test_a_runtime_under_a_symlinked_workdir_is_read_only_through_both_spellings(+ tmp_path, monkeypatch+):+ real = tmp_path / "real"+ venv = real / "venv"+ (venv / "lib").mkdir(parents = True)+ (venv / "bin").mkdir()+ alias = tmp_path / "alias"+ alias.symlink_to(real)+ monkeypatch.setattr(sys, "prefix", str(venv))+ monkeypatch.setattr(sys, "exec_prefix", str(venv))++ launch = sandbox_linux.prepare(_plan(alias))+ try:+ argv = list(launch.argv)+ binds = [i for i, item in enumerate(argv) if item == "--bind"]+ writable = [argv[i + 2] for i in binds]+ assert str(alias) in writable and str(real) in writable, writable+ last_bind = max(binds)+ for leg in ("lib", "bin"):+ for spelling in (real / "venv" / leg, alias / "venv" / leg):+ landed = [+ i+ for i in range(len(argv))+ if argv[i] == "--ro-bind" and argv[i + 2] == str(spelling)+ ]+ assert landed, f"{spelling} is not re-bound read-only"+ assert min(landed) > last_bind, f"{spelling} is bound before the last --bind"+ finally:+ launch.cleanup()+++def test_a_runtime_is_protected_when_sys_prefix_carries_the_workdir_alias(tmp_path, monkeypatch):+ real = tmp_path / "real"+ (real / "venv" / "lib").mkdir(parents = True)+ (real / "venv" / "bin").mkdir()+ alias = tmp_path / "alias"+ alias.symlink_to(real)+ # The alias spelling, exactly as an invoked venv reports it.+ monkeypatch.setattr(sys, "prefix", str(alias / "venv"))+ monkeypatch.setattr(sys, "exec_prefix", str(alias / "venv"))++ under = sandbox_linux._runtime_paths_under(str(real))+ assert str(real / "venv" / "lib") in under, under+ assert str(real / "venv" / "bin") in under, under++ launch = sandbox_linux.prepare(_plan(alias))+ try:+ argv = list(launch.argv)+ last_bind = max(i for i, item in enumerate(argv) if item == "--bind")+ for leg in ("lib", "bin"):+ for spelling in (real / "venv" / leg, alias / "venv" / leg):+ landed = [+ i+ for i in range(len(argv))+ if argv[i] == "--ro-bind" and argv[i + 2] == str(spelling)+ ]+ # The LAST one is what stands: an earlier read-only bind is fine+ # and is covered by the writable bind that follows it, which is+ # exactly why the post-bind one has to exist.+ assert landed and max(landed) > last_bind, f"{spelling} unprotected"+ finally:+ launch.cleanup()+++def test_an_editable_namespace_package_is_granted_without_an_init(tmp_path, monkeypatch):+ source = tmp_path / "checkout"+ namespace = source / "acme"+ (namespace / "widget").mkdir(parents = True)+ (namespace / "widget" / "__init__.py").write_text("", encoding = "utf-8")+ (source / ".env").write_text("AWS_SECRET_ACCESS_KEY=real\n", encoding = "utf-8")+ (source / "fixtures").mkdir()+ _fake_editable(tmp_path, monkeypatch, str(source), top_level = "acme")+ try:+ granted = os_sandbox.editable_source_roots()+ assert str(namespace) in granted, granted+ # Still only what the distribution declares, so the checkout does not+ # come with it.+ assert not any("fixtures" in path or ".env" in path for path in granted), granted+ assert str(source) not in granted, granted+ finally:+ os_sandbox.editable_source_roots.cache_clear()diff --git a/studio/backend/tests/test_sandbox_macos.py b/studio/backend/tests/test_sandbox_macos.pynew file mode 100644index 00000000000..12acd0d26e0--- /dev/null+++ b/studio/backend/tests/test_sandbox_macos.py@@ -0,0 +1,807 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Tests for the macOS Seatbelt profile generator, asserting on the profile TEXT.++``os.path.exists`` and ``os.path.isdir`` are stubbed for exactly two paths so the+dual-spelling assertions get a ``/tmp``-rooted workdir without creating one.+"""++from __future__ import annotations++import os+import re+import shlex+import subprocess+import sys+from pathlib import Path++import pytest++if sys.platform == "win32":+ pytest.skip("the Seatbelt profile generator is POSIX only", allow_module_level = True)++from core.inference import sandbox_macos as backend+from core.inference.os_sandbox import (+ SandboxUnavailableError,+ ToolLaunchPlan,+ WorkdirUnsafeError,+)++_WORKDIR = "/tmp/unsloth-session-abc123"+_PRIVATE_TMP = "/tmp/us-seatbelt-xyz789"+_READ_PREFIX = '(allow file-read* file-test-existence (literal "/") '+_OPTIONAL_PREFIX = '(allow file-read* file-test-existence (literal "/etc/gitconfig")'+# The devices rule and the /dev/fd regex rules share the file-write* operation.+_WRITE_PREFIX = "(allow file-write* (literal "+++def profile(monkeypatch) -> str:+ real_exists, real_isdir = os.path.exists, os.path.isdir+ named = {_WORKDIR, _PRIVATE_TMP}+ monkeypatch.setattr(os.path, "exists", lambda path: path in named or real_exists(path))+ monkeypatch.setattr(os.path, "isdir", lambda path: path in named or real_isdir(path))+ return backend.build_profile(workdir = _WORKDIR, private_tmp = _PRIVATE_TMP, runtime_paths = ())+++def launchable(monkeypatch):+ """Only the availability gate is stubbed; everything else runs for real."""+ monkeypatch.setattr(backend, "available", lambda: (True, "stubbed for this test"))+++def _rule(profile: str, prefix: str) -> str:+ matches = [line for line in profile.splitlines() if line.startswith(prefix)]+ assert len(matches) == 1, f"expected exactly one rule starting {prefix!r}, got {len(matches)}"+ return matches[0]+++def _subpaths(rule: str) -> set[str]:+ return set(re.findall(r'\(subpath "([^"]+)"\)', rule))+++def _literals(rule: str) -> set[str]:+ return set(re.findall(r'\(literal "([^"]+)"\)', rule))+++def test_module_imports_and_exports_the_backend_contract():+ assert backend.BACKEND_NAME == "macos-seatbelt"+ assert isinstance(backend.PROFILE_ID, str) and backend.PROFILE_ID+ assert isinstance(backend.LIMITATIONS, tuple)+ assert all(isinstance(item, str) for item in backend.LIMITATIONS)+ assert "unrestricted_network" in backend.LIMITATIONS+ assert callable(backend.prepare)+++def test_profile_is_deny_default(profile):+ lines = profile.splitlines()+ assert lines[0] == "(version 1)"+ assert lines[1] == "(deny default)"+ assert "(allow default)" not in profile+++def test_every_rule_is_a_balanced_s_expression(profile):+ depth = 0+ in_string = False+ for index, char in enumerate(profile):+ if in_string:+ if char == "\\":+ continue+ if char == '"':+ in_string = False+ continue+ if char == '"':+ in_string = True+ elif char == "(":+ depth += 1+ elif char == ")":+ depth -= 1+ assert depth >= 0, f"unbalanced ) at offset {index}"+ assert depth == 0, f"{depth} unclosed ( in the profile"+ assert not in_string, "the profile ends inside a quoted string"+ for line in profile.splitlines():+ assert line == "" or line.startswith("(") or line.startswith(" ("), line+++def test_login_keychain_mach_service_is_absent(profile):+ """com.apple.SecurityServer would make the login Keychain readable through+ Security.framework. TLS uses trustd and ocspd, not it."""+ assert "com.apple.SecurityServer" not in profile+ assert "com.apple.SecurityServer" not in backend._MACH_SERVICES+ assert 'com.apple.trustd"' in profile+ assert 'com.apple.ocspd"' in profile+ assert '(literal "/usr/bin/security")' in _rule(profile, "(deny process-exec ")+++def test_denied_executables_are_denied_after_the_exec_allowance(profile):+ """Seatbelt is last-match-wins, so the deny has to come after the allow."""+ lines = profile.splitlines()+ deny_index = next(+ index for index, line in enumerate(lines) if line.startswith("(deny process-exec ")+ )+ assert lines.index("(allow process-exec)") < deny_index+ denied = _literals(_rule(profile, "(deny process-exec "))+ for path in backend._DENIED_EXECUTABLES:+ assert path in denied, f"{path} is no longer exec-denied"+++def test_both_private_spellings_are_emitted_for_the_workdir(profile):+ for rule in (_rule(profile, _READ_PREFIX), _rule(profile, _WRITE_PREFIX)):+ for path in (_WORKDIR, f"/private{_WORKDIR}"):+ assert f'(literal "{path}")' in rule+ assert f'(subpath "{path}")' in rule+++def test_a_non_ascii_workdir_reaches_the_profile_unescaped(monkeypatch):+ workdir = "/tmp/unsloth-session-caf\u00e9"+ private_tmp = "/tmp/us-seatbelt-\u00fcber"+ real_exists, real_isdir = os.path.exists, os.path.isdir+ named = {workdir, private_tmp}+ monkeypatch.setattr(os.path, "exists", lambda path: path in named or real_exists(path))+ monkeypatch.setattr(os.path, "isdir", lambda path: path in named or real_isdir(path))+ text = backend.build_profile(workdir = workdir, private_tmp = private_tmp, runtime_paths = ())+ assert "\\u00" not in text+ for path in (workdir, private_tmp):+ assert f'(subpath "{path}")' in _rule(text, _WRITE_PREFIX)+++def test_optional_literals_are_allowed_even_though_they_do_not_exist(profile):+ literals = _literals(_rule(profile, _OPTIONAL_PREFIX))+ for path in backend._OPTIONAL_READ_LITERALS:+ assert path in literals, f"{path} lost its unconditional read allowance"+ assert {"/private/etc/gitconfig", "/private/etc/gitattributes"} <= literals+ # At least one does not exist here, so the existence-filtered path rules+ # could not have carried it.+ absent = [path for path in backend._OPTIONAL_READ_LITERALS if not os.path.exists(path)]+ assert absent, "no optional literal is absent here, so this test proved nothing"+++def test_optional_read_literals_never_follow_a_symlink(tmp_path):+ """Read literals must not resolve: an /etc/gitconfig symlinked at ~/dotfiles+ would put a home path in the read set. Denials still resolve."""+ target = tmp_path / "secret"+ target.write_text("")+ link = tmp_path / "link"+ link.symlink_to(target)+ # The target's absence is the assertion, not an exact list: _sbpl_spellings+ # emits the /private pair on both platforms, so the spelling is never alone.+ unresolved = backend._literal_filters((str(link),), resolve = False)+ assert f'(literal "{link}")' in unresolved+ assert not any(str(target) in filter_ for filter_ in unresolved), unresolved+ assert f'(literal "{target}")' in backend._literal_filters((str(link),))+++def test_ancestor_metadata_rules_are_emitted(profile):+ metadata_rule = _rule(profile, "(allow file-read-metadata ")+ metadata = _literals(metadata_rule)+ assert "/" in metadata+ for ancestor in (+ "/tmp",+ "/private/tmp",+ "/private",+ "/etc",+ "/private/etc",+ "/usr/local/etc",+ # Resolving mDNSResponder's socket for connect() walks these first.+ "/var/run",+ "/private/var/run",+ ):+ assert ancestor in metadata, f"{ancestor} has no file-read-metadata rule"+ # Metadata only: an ancestor must not become readable or listable.+ assert not _subpaths(metadata_rule)+++def test_workdir_and_private_tmp_are_the_only_writable_subpaths(profile):+ writable = _subpaths(_rule(profile, _WRITE_PREFIX))+ assert writable == {_WORKDIR, f"/private{_WORKDIR}", _PRIVATE_TMP, f"/private{_PRIVATE_TMP}"}+ for forbidden in ("/", "/usr", "/tmp", "/private/tmp", str(Path.home())):+ assert forbidden not in writable+ assert not _subpaths(_rule(profile, "(allow file-read* file-test-existence file-write-data "))+++def test_no_read_root_reaches_the_users_home(profile):+ """Asserted against the tables, not the profile text: the read roots are+ existence-filtered and mostly vanish on a non-macOS host."""+ home = str(Path.home())+ assert f'(subpath "{home}")' not in profile+ assert "/Users/" not in profile+ tables = (+ backend._READ_ROOTS+ + backend._TLS_TRUST_PATHS+ + backend._OPTIONAL_READ_ROOTS+ + backend._OPTIONAL_READ_LITERALS+ )+ for path in tables:+ assert not path.startswith("/Users/"), path+ assert "~" not in path, path+ # SYSTEM keychains only; the login one is under the unreadable home.+ assert "/System/Library/Keychains" in backend._TLS_TRUST_PATHS+ assert not any("Library/Keychains" in path and path.startswith(home) for path in tables)+++def test_ip_egress_is_unrestricted_but_unix_sockets_are_not(profile):+ lines = profile.splitlines()+ for rule in ("(allow system-socket)", "(allow network-inbound)"):+ assert rule in lines+ # Neither direction may be unconditional: an unfiltered grant covers AF_UNIX.+ assert "(allow network-outbound)" not in lines+ assert "(allow network-bind)" not in lines+ assert '(allow network-outbound (remote ip "*:*"))' in lines+ assert '(allow network-bind (local ip "*:*"))' in lines+ assert "localhost" not in profile+ assert "proxy" not in profile.lower()+ # connect()/bind() on a unix socket is network-outbound / network-bind, not a+ # file operation, so multiprocessing needs its own rule.+ for root in (_PRIVATE_TMP, _WORKDIR):+ for path in (root, f"/private{root}"):+ assert f'(allow network-outbound (remote unix-socket (subpath "{path}")))' in lines+ assert f'(allow network-bind (local unix-socket (subpath "{path}")))' in lines+ # Both spellings for the DNS socket: no test here can pick the right one.+ assert '(allow network-outbound (literal "/private/var/run/mDNSResponder")' in profile+ assert "(allow network-outbound (remote unix-socket (literal " in profile+ for path in ("/var/run/mDNSResponder", "/private/var/run/mDNSResponder"):+ assert f'(allow network-outbound (remote unix-socket (literal "{path}")))' in lines+ assert '(literal "/var/run/mDNSResponder")' in profile+ assert "docker.sock" not in profile+++def test_process_substitution_descriptors_are_readable(profile):+ """bash process substitution hands the child /dev/fd/63, so narrowing the rule+ to 0, 1 and 2 fails a command that works everywhere else."""+ assert '(allow file-read* (regex #"^/dev/fd/[0-9]+$"))' in profile+ assert '(allow file-write* (regex #"^/dev/fd/[0-9]+$"))' in profile+++def test_pip_gets_a_writable_target_inside_the_workdir():+ env = backend._sandbox_environment(+ {"PATH": "/usr/bin", "PYTHONPATH": "/shim"}, _WORKDIR, _PRIVATE_TMP+ )+ packages = f"{_WORKDIR}/{backend.SESSION_PACKAGES_RELPATH}"+ assert env["PIP_TARGET"] == packages+ assert env["PYTHONPATH"].split(os.pathsep) == ["/shim", packages]+++def test_openmp_can_write_its_registration_segment(profile):+ kmp = next(+ block for block in profile.split("(allow ipc-posix-shm") if "__KMP_REGISTERED_LIB_" in block+ )+ assert "ipc-posix-shm-write-data" in kmp, kmp+++def test_sysctl_and_shm_rules_survive(profile):+ sysctl = _rule(profile, "(allow sysctl-read ")+ for name in ("hw.ncpu", "hw.memsize", "kern.osproductversion"):+ assert f'(sysctl-name "{name}")' in sysctl+ assert "(allow ipc-posix-sem)" in profile+ assert "^/torch_[0-9]+_[0-9]+_[0-9]+$" in profile+++def test_runtime_read_paths_cover_the_interpreter_and_the_site_shim():+ paths = backend.runtime_read_paths()+ # normpath, because ``backend.__file__`` carries whatever spelling first+ # imported the module, so a direct comparison passes or fails on import order.+ shim = os.path.normpath(os.path.join(os.path.dirname(backend.__file__), "sandbox_site"))+ assert shim in paths+ # "/" or "/usr" as a read root hands back most of the host, so a system+ # interpreter reporting one as its prefix must be dropped.+ assert "/" not in paths and "/usr" not in paths+ for prefix in (sys.prefix, sys.exec_prefix, sys.base_prefix, sys.base_exec_prefix):+ for name in ("bin", "lib"):+ member = os.path.join(prefix, name)+ if not os.path.isdir(member):+ continue+ assert any(backend._within(member, root) for root in paths), member+ # lib-dynload hangs off the exec pair, which a uv interpreter spells through+ # an alias symlink base_prefix alone never names.+ dynload = os.path.join(+ sys.base_exec_prefix,+ "lib",+ f"python{sys.version_info.major}." f"{sys.version_info.minor}",+ "lib-dynload",+ )+ if os.path.isdir(dynload):+ assert any(backend._within(dynload, root) for root in paths), dynload+++def test_a_venv_at_a_project_root_does_not_put_the_project_in_the_read_set(monkeypatch, tmp_path):+ project = tmp_path / "project"+ (project / "bin").mkdir(parents = True)+ (project / "lib").mkdir()+ (project / ".git").mkdir()+ (project / ".env").write_text("OPENAI_API_KEY=sk-real")+ (project / "pyvenv.cfg").write_text("home = /usr/bin\n")+ for name in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):+ monkeypatch.setattr(sys, name, str(project))++ paths = backend.runtime_read_paths()+ assert str(project) not in paths+ assert not any(backend._within(str(project / ".env"), root) for root in paths)+ assert not any(backend._within(str(project / ".git"), root) for root in paths)+ assert str(project / "bin") in paths+ assert str(project / "lib") in paths+++def test_prepare_wraps_argv_and_preserves_the_preexec(tmp_path, launchable):+ """The preexec is tools.py's os.setsid(); losing it strands the process group."""+ workdir = tmp_path / "session"+ workdir.mkdir()++ def preexec() -> None:+ return None++ plan = ToolLaunchPlan(+ argv = ("/bin/echo", "hello"),+ workdir = str(workdir),+ env = {"PATH": "/usr/bin", "DYLD_INSERT_LIBRARIES": "/evil.dylib", "KEEP": "1"},+ preexec_fn = preexec,+ timeout_seconds = 30,+ )+ prepared = backend.prepare(plan)+ private_tmp = prepared.cleanup_paths[0]+ try:+ assert prepared.argv[0] == backend.SANDBOX_EXEC+ assert prepared.argv[1] == "-p"+ assert prepared.argv[2].startswith("(version 1)\n(deny default)")+ assert prepared.argv[3] == "--"+ assert prepared.argv[4:] == ("/bin/echo", "hello")+ assert prepared.preexec_fn is preexec+ assert prepared.backend == backend.BACKEND_NAME+ assert prepared.timeout_seconds == 30+ assert prepared.env["HOME"] == str(workdir)+ assert prepared.env["KEEP"] == "1"+ assert "DYLD_INSERT_LIBRARIES" not in prepared.env+ assert len(prepared.cleanup_paths) == 1+ assert prepared.env["TMPDIR"] == private_tmp+ assert os.path.isdir(private_tmp)+ writable = _subpaths(_rule(prepared.argv[2], _WRITE_PREFIX))+ assert str(workdir) in writable and private_tmp in writable+ finally:+ prepared.cleanup()+ assert not os.path.exists(private_tmp)+++def test_prepare_refuses_a_workdir_that_does_not_exist(tmp_path, launchable):+ plan = ToolLaunchPlan(argv = ("/bin/echo",), workdir = str(tmp_path / "missing"), env = {})+ with pytest.raises(SandboxUnavailableError, match = "does not exist"):+ backend.prepare(plan)+++def test_prepare_refuses_the_filesystem_root_as_a_workdir(launchable):+ plan = ToolLaunchPlan(argv = ("/bin/echo",), workdir = "/", env = {})+ with pytest.raises(SandboxUnavailableError, match = "filesystem root"):+ backend.prepare(plan)+++def test_prepare_refuses_when_the_launcher_is_missing(tmp_path):+ """No stub here: argv[0] must never become a sandbox-exec that is not there."""+ if os.path.exists(backend.SANDBOX_EXEC):+ pytest.skip("this host has a real /usr/bin/sandbox-exec, so the gate cannot be observed")+ workdir = tmp_path / "session"+ workdir.mkdir()+ plan = ToolLaunchPlan(argv = ("/bin/echo",), workdir = str(workdir), env = {})+ with pytest.raises(SandboxUnavailableError, match = "Seatbelt launcher"):+ backend.prepare(plan)+++def test_a_rule_that_lost_every_filter_raises_instead_of_granting_everything():+ with pytest.raises(SandboxUnavailableError, match = "unconditionally"):+ backend._rule("allow file-write*", [])+ with pytest.raises(SandboxUnavailableError, match = "unconditionally"):+ backend._rule("deny process-exec", [])+ rendered = backend._rule("allow file-write*", ['(literal "/x")'])+ assert rendered == '(allow file-write* (literal "/x"))'+ with pytest.raises(SandboxUnavailableError, match = "unconditionally"):+ backend.build_profile(+ workdir = "/tmp/does-not-exist-workdir",+ private_tmp = "/tmp/does-not-exist-tmp",+ runtime_paths = (),+ )+++def test_paths_must_be_absolute_and_free_of_newlines():+ with pytest.raises(SandboxUnavailableError):+ backend._validated("relative/path")+ with pytest.raises(SandboxUnavailableError):+ backend._validated("/tmp/with\nnewline")+ with pytest.raises(SandboxUnavailableError):+ backend._validated("/tmp/with\0nul")+++def test_available_never_raises_on_a_host_without_the_launcher():+ ok, reason = backend.available()+ assert isinstance(ok, bool) and isinstance(reason, str) and reason+ if not os.path.exists(backend.SANDBOX_EXEC):+ assert ok is False+ assert backend.SANDBOX_EXEC in reason+++# Everything above asserts on text; none of it proves the kernel accepts it.++_darwin_only = pytest.mark.skipif(+ sys.platform != "darwin" or not os.path.exists(backend.SANDBOX_EXEC),+ reason = "needs a real macOS host: only a Darwin kernel can compile or enforce an SBPL profile",+)+++@_darwin_only+def test_profile_compiles_under_sandbox_exec(tmp_path):+ workdir = tmp_path / "session"+ workdir.mkdir()+ plan = ToolLaunchPlan(argv = ("/usr/bin/true",), workdir = str(workdir), env = {})+ prepared = backend.prepare(plan)+ try:+ result = subprocess.run(+ prepared.argv, capture_output = True, text = True, timeout = 60, check = False+ )+ assert result.returncode == 0, result.stderr+ finally:+ prepared.cleanup()+++@_darwin_only+def test_home_is_unreadable_inside_the_sandbox(tmp_path):+ """The file is created and proven readable on the host first: a read that fails+ for an unrelated reason proves nothing."""+ workdir = tmp_path / "session"+ workdir.mkdir()+ canary = Path(os.path.expanduser("~")) / ".unsloth-seatbelt-canary"+ canary.write_text("UNSLOTH_CANARY_HOME_READABLE", encoding = "utf-8")+ try:+ argv = ("/bin/sh", "-c", f"cat {shlex.quote(str(canary))}")+ host = subprocess.run(argv, capture_output = True, text = True, timeout = 60, check = False)+ assert host.returncode == 0 and "UNSLOTH_CANARY_HOME_READABLE" in host.stdout, (+ "the canary is not readable even outside the sandbox, so the negative "+ f"control below would prove nothing: {host.stderr}"+ )++ prepared = backend.prepare(+ ToolLaunchPlan(argv = argv, workdir = str(workdir), env = {"PATH": "/usr/bin:/bin"})+ )+ try:+ result = subprocess.run(+ prepared.argv, capture_output = True, text = True, timeout = 60, check = False+ )+ assert result.returncode != 0+ assert "UNSLOTH_CANARY_HOME_READABLE" not in result.stdout+ finally:+ prepared.cleanup()+ finally:+ canary.unlink(missing_ok = True)+++def test_a_runtime_path_symlinked_out_of_the_workdir_is_not_readable(monkeypatch, tmp_path):+ workdir = tmp_path / "session"+ workdir.mkdir()+ secret = tmp_path / "secrets"+ secret.mkdir()+ (secret / "id_rsa").write_text("PRIVATE KEY")+ venv = workdir / "venv"+ (venv / "bin").mkdir(parents = True)+ (venv / "lib").symlink_to(secret)+ for name in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):+ monkeypatch.setattr(sys, name, str(venv))++ paths = backend.runtime_read_paths(str(workdir))+ assert str(secret) not in paths+ assert not any(backend._within(str(secret), path) for path in paths)+ # Asserted so the workdir parameter cannot be dropped silently.+ assert str(secret) in backend.runtime_read_paths()+++def test_pip_console_scripts_are_reachable_and_never_shadow_a_system_command():+ env = backend._sandbox_environment(+ {"PATH": "/usr/bin:/bin", "PYTHONPATH": "/shim"}, _WORKDIR, _PRIVATE_TMP+ )+ packages = f"{_WORKDIR}/{backend.SESSION_PACKAGES_RELPATH}"+ assert env["PATH"].split(os.pathsep) == ["/usr/bin", "/bin", f"{packages}/bin"]+++def test_the_semaphore_namespace_is_named_rather_than_narrowed(profile):+ assert "(allow ipc-posix-sem)" in profile+ assert "posix_semaphore_namespace_shared" in backend.LIMITATIONS+++def test_host_process_metadata_is_named_rather_than_withheld(profile):+ sysctl = _rule(profile, "(allow sysctl-read ")+ assert '(sysctl-name-prefix "kern.proc.pid.")' in sysctl+ assert "host_process_metadata_readable" in backend.LIMITATIONS+ assert "(allow process-info* (target same-sandbox))" in profile+++def test_a_framework_build_gets_its_dyld_image(monkeypatch, tmp_path):+ prefix = tmp_path / "Python.framework" / "Versions" / "3.13"+ (prefix / "bin").mkdir(parents = True)+ (prefix / "lib").mkdir()+ (prefix / "Python").write_bytes(b"\xcf\xfa\xed\xfe")+ for name in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):+ monkeypatch.setattr(sys, name, str(prefix))+ paths = backend.runtime_read_paths()+ assert str(prefix / "Python") in paths+ assert str(prefix) not in paths+++def test_a_runtime_under_the_workdir_is_denied_write_after_the_allowance(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ venv = workdir / "venv"+ (venv / "lib").mkdir(parents = True)+ (venv / "bin").mkdir()+ monkeypatch.setattr(sys, "prefix", str(venv))+ monkeypatch.setattr(sys, "exec_prefix", str(venv))++ profile = backend.build_profile(workdir = str(workdir), private_tmp = "/tmp/pt", runtime_paths = ())+ lines = profile.splitlines()+ allow = next(i for i, line in enumerate(lines) if line.startswith("(allow file-write* "))+ deny = next(i for i, line in enumerate(lines) if line.startswith("(deny file-write* "))+ assert deny > allow, "a deny before the allowance is overridden by it"+ denied = _subpaths(lines[deny]) | _literals(lines[deny])+ assert str(venv / "lib") in denied+ assert str(venv / "bin") in denied+++def test_no_write_denial_is_emitted_when_the_runtime_is_outside_the_workdir(tmp_path, monkeypatch):+ """The negative control: the rule above must not fire for an ordinary layout,+ where denying anything under the workdir would take away the one writable place."""+ workdir = tmp_path / "session"+ workdir.mkdir()+ monkeypatch.setattr(sys, "prefix", "/usr")+ monkeypatch.setattr(sys, "exec_prefix", "/usr")+ profile = backend.build_profile(workdir = str(workdir), private_tmp = "/tmp/pt", runtime_paths = ())+ assert not any(line.startswith("(deny file-write* ") for line in profile.splitlines())+++def test_the_openssl_directory_is_granted_by_component_not_whole(profile):+ for spelling in ("/etc/ssl", "/private/etc/ssl"):+ for line in profile.splitlines():+ if line.startswith("(allow file-read-metadata"):+ continue+ assert f'(subpath "{spelling}")' not in line, line+ assert f'(literal "{spelling}")' not in line, line+ # The components are optional paths, dropped from the profile on a host that+ # lacks them, so the trust list itself is what carries the assertion.+ assert "/private/etc/ssl" not in backend._TLS_TRUST_PATHS+ for component in ("cert.pem", "certs", "openssl.cnf"):+ assert f"/private/etc/ssl/{component}" in backend._TLS_TRUST_PATHS+++ os.geteuid() == 0,+ reason = "root reads and writes regardless of the mode bits, so the premise is void",+)+def test_a_toolchain_directory_the_user_can_write_is_not_trusted(tmp_path):+ mine = tmp_path / "FakeXcode.app" / "Contents" / "Developer"+ mine.mkdir(parents = True)+ assert backend._trusted_system_dir(str(mine)) is False+ assert backend._trusted_system_dir(str(tmp_path / "absent")) is False+ missing_file = tmp_path / "Developer"+ missing_file.write_text("", encoding = "utf-8")+ assert backend._trusted_system_dir(str(missing_file)) is False+ # The positive control, so the check above is not passing because it always+ # says no: a root-owned system directory is accepted.+ assert backend._trusted_system_dir("/usr") is True+++ os.geteuid() == 0,+ reason = "root reads and writes regardless of the mode bits, so the premise is void",+)+def test_the_developer_dir_variable_never_reaches_xcode_select(monkeypatch, tmp_path):+ mine = tmp_path / "Developer"+ mine.mkdir()+ monkeypatch.setenv("DEVELOPER_DIR", str(mine))+ monkeypatch.setattr(backend.sys, "platform", "darwin")+ monkeypatch.setattr(backend.os.path, "exists", lambda path: True)+ seen: dict = {}++ def fake_run(argv, **kwargs):+ seen.update(kwargs)+ return subprocess.CompletedProcess(argv, 0, stdout = str(mine), stderr = "")++ monkeypatch.setattr(backend.subprocess, "run", fake_run)+ monkeypatch.setattr(backend, "_developer_paths_cache", None)+ try:+ assert backend._developer_paths() == ()+ finally:+ backend._developer_paths_cache = None+ assert "DEVELOPER_DIR" not in seen["env"]+++def _profile_for(workdir, monkeypatch, prefix):+ monkeypatch.setattr(sys, "prefix", str(prefix))+ monkeypatch.setattr(sys, "exec_prefix", str(prefix))+ return backend.build_profile(workdir = str(workdir), private_tmp = _PRIVATE_TMP, runtime_paths = ())+++def test_a_runtime_under_a_symlinked_workdir_is_denied_through_both_spellings(+ tmp_path, monkeypatch+):+ real = tmp_path / "real"+ venv = real / "venv"+ (venv / "lib").mkdir(parents = True)+ (venv / "bin").mkdir()+ alias = tmp_path / "alias"+ alias.symlink_to(real)++ profile = _profile_for(alias, monkeypatch, venv)+ under = backend.runtime_paths_under(str(alias))+ for spelling in (real / "venv" / "lib", alias / "venv" / "lib"):+ assert str(spelling) in under, under+ deny = _rule(profile, "(deny file-write* ")+ for spelling in (real / "venv" / "lib", alias / "venv" / "lib"):+ assert f'(subpath "{spelling}")' in deny, deny+ # Last-match-wins, so the denial is worthless before the allowance.+ lines = profile.splitlines()+ assert lines.index(_rule(profile, _WRITE_PREFIX)) < lines.index(deny)+++def test_the_framework_python_image_is_denied_when_the_prefix_is_under_the_workdir(+ tmp_path, monkeypatch+):+ workdir = tmp_path / "session"+ prefix = workdir / "Python.framework" / "Versions" / "3.12"+ (prefix / "lib").mkdir(parents = True)+ image = prefix / "Python"+ image.write_bytes(b"\xcf\xfa\xed\xfe")++ deny = _rule(_profile_for(workdir, monkeypatch, prefix), "(deny file-write* ")+ assert str(image) in backend.runtime_paths_under(str(workdir))+ assert f'(literal "{image}")' in deny, deny+++def test_an_optional_search_root_that_resolves_out_of_its_prefix_is_dropped(monkeypatch):+ home = os.path.expanduser("~")+ real = os.path.realpath++ def resolves_home(path):+ return home if path == "/usr/local/bin" else real(path)++ monkeypatch.setattr(os.path, "realpath", resolves_home)+ kept = backend._contained_optional_roots()+ assert "/usr/local/bin" not in kept+ # The positive control: the siblings are untouched, so this is not passing by+ # dropping everything.+ assert "/opt/homebrew/bin" in kept and "/usr/local/lib" in kept+ # Through the profile as well, since the filter is worth nothing if+ # build_profile still reaches for the unfiltered list.+ named = {"/usr/local/bin", _WORKDIR, _PRIVATE_TMP}+ real_isdir, real_exists = os.path.isdir, os.path.exists+ monkeypatch.setattr(os.path, "isdir", lambda path: path in named or real_isdir(path))+ monkeypatch.setattr(os.path, "exists", lambda path: path in named or real_exists(path))+ profile = backend.build_profile(workdir = _WORKDIR, private_tmp = _PRIVATE_TMP, runtime_paths = ())+ assert f'(subpath "{home}")' not in profile+++def test_an_editable_checkout_is_listable_but_not_readable(tmp_path, monkeypatch):+ checkout = tmp_path / "checkout"+ package = checkout / "demo"+ package.mkdir(parents = True)+ (package / "__init__.py").write_text("", encoding = "utf-8")+ (checkout / ".env").write_text("AWS_SECRET_ACCESS_KEY=real\n", encoding = "utf-8")+ monkeypatch.setattr(backend, "editable_source_roots", lambda: (str(package),))+ monkeypatch.setattr(backend, "editable_import_roots", lambda: (str(checkout),))++ named = {_WORKDIR, _PRIVATE_TMP}+ real_isdir, real_exists = os.path.isdir, os.path.exists+ monkeypatch.setattr(os.path, "isdir", lambda path: path in named or real_isdir(path))+ monkeypatch.setattr(os.path, "exists", lambda path: path in named or real_exists(path))+ profile = backend.build_profile(+ workdir = _WORKDIR,+ private_tmp = _PRIVATE_TMP,+ runtime_paths = (str(package),),+ )+ assert f'(literal "{checkout}")' in profile+ assert f'(subpath "{checkout}")' not in profile+ assert f'(subpath "{package}")' in profile+++def test_a_standalone_interpreter_in_the_workdir_is_denied_writes(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ workdir.mkdir()+ executable = workdir / "python"+ executable.write_bytes(b"#!/bin/sh\nexit 0\n")+ executable.chmod(0o755)+ for attribute in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):+ monkeypatch.setattr(sys, attribute, str(workdir))+ monkeypatch.setattr(sys, "executable", str(executable))++ paths = backend.runtime_paths_under(str(workdir))+ assert str(executable) in paths+ assert all(os.path.samefile(path, executable) for path in paths)+ profile = backend.build_profile(+ workdir = str(workdir), private_tmp = str(tmp_path / "tmp"), runtime_paths = ()+ )+ deny = _rule(profile, "(deny file-write* ")+ assert f'(literal "{executable}")' in deny, deny+++def test_a_workdir_this_backend_cannot_name_fails_the_call_rather_than_de_isolating_it(+ tmp_path, launchable+):+ with pytest.raises(WorkdirUnsafeError):+ backend.prepare(+ ToolLaunchPlan(argv = ("/usr/bin/true",), workdir = "/tmp/session-\udcff", env = {})+ )+++def test_an_interpreter_in_a_home_directory_does_not_grant_the_home(tmp_path, monkeypatch):+ home = tmp_path / "alice"+ (home / ".ssh").mkdir(parents = True)+ executable = home / "python"+ executable.write_bytes(b"#!/bin/sh\nexit 0\n")+ executable.chmod(0o755)+ monkeypatch.setattr(sys, "executable", str(executable))+ paths = backend.runtime_read_paths(str(tmp_path / "session"))+ assert str(home) not in paths, paths+ assert str(executable) in paths, paths+++def test_a_runtime_origin_is_excluded_under_either_workdir_spelling(tmp_path, monkeypatch):+ real = tmp_path / "real"+ real.mkdir()+ alias = tmp_path / "alias"+ alias.symlink_to(real)+ secret = tmp_path / "secret"+ secret.mkdir()+ (secret / "id_rsa").write_text("SECRET", encoding = "utf-8")+ venv = real / "venv"+ (venv / "bin").mkdir(parents = True)+ (venv / "lib").symlink_to(secret)+ for attribute in ("prefix", "base_prefix", "exec_prefix", "base_exec_prefix"):+ monkeypatch.setattr(sys, attribute, str(real / "venv"))++ paths = backend.runtime_read_paths(str(alias))+ assert not any(os.path.realpath(p) == str(secret) for p in paths), paths+++def test_an_interpreter_symlinked_out_of_the_workdir_fails_the_call(tmp_path, monkeypatch):+ """The Linux twin's guard, on this backend."""+ workdir = tmp_path / "session"+ workdir.mkdir()+ outside = tmp_path / "elsewhere"+ outside.mkdir()+ real = outside / "python"+ real.write_bytes(b"#!/bin/sh\nexit 0\n")+ real.chmod(0o755)+ link = workdir / "python"+ link.symlink_to(real)+ monkeypatch.setattr(sys, "executable", str(link))++ with pytest.raises(WorkdirUnsafeError, match = "Python that runs Studio"):+ backend.runtime_paths_under(str(workdir))+++def test_a_path_with_a_control_byte_is_refused(tmp_path):+ for control in ("\x0c", "\x01", "\x7f"):+ with pytest.raises(SandboxUnavailableError, match = "control characters"):+ backend._validated(f"/tmp/session{control}x")+ # The positive control: an ordinary accented path is still accepted.+ assert backend._validated("/tmp/session-café") == "/tmp/session-café"+++def test_an_editable_source_inside_the_workdir_is_denied_writes(tmp_path, monkeypatch):+ workdir = tmp_path / "session"+ package = workdir / "mypkg"+ package.mkdir(parents = True)+ (package / "__init__.py").write_text("", encoding = "utf-8")+ monkeypatch.setattr(backend, "editable_source_roots", lambda: (str(package),))+ assert str(package) in backend.runtime_paths_under(str(workdir))+++def test_a_runtime_is_denied_when_sys_prefix_carries_the_workdir_alias(tmp_path, monkeypatch):+ real = tmp_path / "real"+ (real / "venv" / "lib").mkdir(parents = True)+ alias = tmp_path / "alias"+ alias.symlink_to(real)+ profile = _profile_for(alias, monkeypatch, alias / "venv")+ deny = _rule(profile, "(deny file-write* ")+ for spelling in (real / "venv" / "lib", alias / "venv" / "lib"):+ assert f'(subpath "{spelling}")' in deny, deny+++def test_a_path_that_cannot_be_encoded_is_refused_rather_than_carried():+ with pytest.raises(SandboxUnavailableError, match = "encodable as UTF-8"):+ backend._validated("/tmp/session-\udcff")+ # The positive control: an ordinary accented path is NOT refused, or this+ # guard would be undoing the fix it is protecting.+ assert backend._validated("/tmp/session-café") == "/tmp/session-café"+ assert "\\u00" not in backend._sbpl_string("/tmp/session-café")diff --git a/studio/backend/tests/test_sandbox_probe.py b/studio/backend/tests/test_sandbox_probe.pynew file mode 100644index 00000000000..f552e24b276--- /dev/null+++ b/studio/backend/tests/test_sandbox_probe.py@@ -0,0 +1,490 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""What the live probe is allowed to conclude, and from what.++The confining backend is simulated in-process rather than skipped: the CI host+cannot build a real sandbox, and "skipped" would leave the positive half of+every pairing untested.+"""++from __future__ import annotations++import inspect+import os+import pathlib+import re+import shutil+import subprocess+import sys+import tempfile++import pytest++if sys.platform == "win32":+ pytest.skip("the live probe is POSIX only", allow_module_level = True)++from core.inference import os_sandbox, sandbox_landlock, sandbox_probe+from core.inference.os_sandbox import PreparedSandboxLaunch, ToolLaunchPlan++[email protected](autouse = True)+def _clean_probe_cache():+ sandbox_probe.reset_probe_cache()+ yield+ sandbox_probe.reset_probe_cache()+++class _Backend:+ """A stand-in exporting exactly the three names ``os_sandbox`` reads."""++ PROFILE_ID = "test-profile"+ LIMITATIONS = ()++ def __init__(self, name, prepare):+ self.BACKEND_NAME = name+ self._prepare = prepare+ self.prepared = []+ self.calls = 0++ def prepare(self, plan):+ self.calls += 1+ prepared = self._prepare(plan)+ self.prepared.append(prepared)+ return prepared+++def _passthrough(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:+ return PreparedSandboxLaunch(+ argv = plan.argv,+ workdir = plan.workdir,+ env = plan.env,+ preexec_fn = plan.preexec_fn,+ backend = "passthrough",+ )+++_CONFINE_WRAPPER = """import builtins, os, sys+WORK = os.path.realpath({workdir!r})+# Both spellings, because on macOS the probe base is under /tmp, which is a+# symlink to /private/tmp. With RESOLVE off the guard compares the path AS+# WRITTEN, so resolving only one side made the double refuse its own workdir and+# the probe stopped before it reached the symlink leg it exists to exercise.+WORK_AS_GIVEN = os.path.abspath({workdir!r})+LEAK_WRITES = {leak_writes!r}+RESOLVE = {resolve!r}+_host_open = builtins.open+++def _confined(file, *args, **kwargs):+ # A stand-in for a filesystem namespace. RESOLVE picks whether the boundary+ # is drawn on the resolved TARGET (what a real mount namespace does) or on+ # the spelling of the path, which is the mistake the escape symlink catches.+ try:+ target = os.path.realpath(file) if RESOLVE else os.fspath(file)+ except (TypeError, ValueError):+ target = None+ outside = isinstance(target, str) and not any(+ target == root or target.startswith(root + os.sep)+ for root in (WORK, WORK_AS_GIVEN)+ )+ if outside:+ mode = args[0] if args else kwargs.get("mode", "r")+ writing = "r" not in mode or "+" in mode+ system_root = target == os.path.realpath(sys.executable)+ # LEAK_WRITES models a backend that confines reads and protects the+ # system root but lets an ordinary write through: every control the+ # sandboxed process can evaluate comes out right, and the host still+ # ends up with the file.+ if not (LEAK_WRITES and writing and not system_root):+ raise PermissionError(13, "confined to the workdir")+ return _host_open(file, *args, **kwargs)+++builtins.open = _confined+exec(compile({payload!r}, "<probe-payload>", "exec"), {{"__name__": "__main__"}})+"""+++def _wrap(+ plan: ToolLaunchPlan,+ name: str,+ *,+ resolve = True,+ leak_writes = False,+):+ """A builtins.open guard, not a kernel namespace. Import machinery uses+ io.open_code, so the payload's own imports still work."""+ wrapper = _CONFINE_WRAPPER.format(+ workdir = plan.workdir,+ payload = plan.argv[-1],+ resolve = resolve,+ leak_writes = leak_writes,+ )+ return PreparedSandboxLaunch(+ argv = plan.argv[:-1] + (wrapper,),+ workdir = plan.workdir,+ env = plan.env,+ # A backend claiming to confine has to carry the abstract-socket scope or+ # the probe refuses it, which is the point of that control.+ preexec_fn = sandbox_landlock.with_abstract_scope(plan.preexec_fn),+ backend = name,+ )+++def _confining(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:+ return _wrap(plan, "confining")+++def _spelling_only(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:+ return _wrap(plan, "spelling-only", resolve = False)+++def _leaking_writes(plan: ToolLaunchPlan) -> PreparedSandboxLaunch:+ return _wrap(plan, "leaky", leak_writes = True)+++def test_a_backend_that_confines_nothing_is_not_available():+ backend = _Backend("passthrough", _passthrough)+ available, reason = sandbox_probe.probe(backend)+ assert available is False+ assert "read the host sentinel" in reason, reason+++def test_a_backend_that_really_confines_is_available():+ backend = _Backend("confining", _confining)+ available, reason = sandbox_probe.probe(backend)+ assert available is True, reason+ assert "confining" in reason+++def test_the_symlink_leg_is_judged_on_the_target_not_the_path():+ """A spelling-only guard passes the sentinel leg and fails here, so the two legs+ are not redundant."""+ available, reason = sandbox_probe.probe(_Backend("spelling-only", _spelling_only))+ assert available is False+ assert "symlink" in reason, reason+++def test_a_write_that_reaches_the_host_fails_the_probe():+ """This backend passes every control the sandboxed process can evaluate and+ still lands a write on the host, which only the host can see."""+ available, reason = sandbox_probe.probe(_Backend("leaky", _leaking_writes))+ assert available is False+ assert "wrote through to the host" in reason, reason+++def test_the_escape_check_is_made_before_the_scratch_root_is_removed():+ text = inspect.getsource(sandbox_probe._run_probe)+ assert text.index("_host_saw_the_write") < text.index("shutil.rmtree")+++def test_the_positive_controls_run_before_anything_is_concluded(monkeypatch):+ backend = _Backend("confining", _confining)+ seen = []++ def broken(workdir, sentinel, outside, env):+ seen.append(sentinel)+ return "the host itself could not read the probe sentinel (simulated)"++ monkeypatch.setattr(sandbox_probe, "_host_positive_controls", broken)+ available, reason = sandbox_probe.probe(backend)+ assert available is False+ assert "the host itself could not read" in reason+ assert backend.calls == 0+ assert seen+++def test_the_host_control_really_reads_the_sentinel_it_was_given(tmp_path):+ work = tmp_path / "work"+ work.mkdir()+ env = {"PATH": os.environ.get("PATH", ""), "HOME": str(work), "TMPDIR": str(work)}+ missing = str(tmp_path / "absent.txt")+ assert "could not read" in sandbox_probe._host_positive_controls(+ str(work), missing, str(tmp_path / "out.txt"), env+ )+ sentinel = tmp_path / "sentinel.txt"+ sentinel.write_text("not the token", encoding = "utf-8")+ assert "did not contain" in sandbox_probe._host_positive_controls(+ str(work), str(sentinel), str(tmp_path / "out.txt"), env+ )+++def test_a_host_that_fails_the_positive_half_is_not_blamed_on_the_backend():+ backend = _Backend("confining", _confining)+ real = sandbox_probe._host_payload+ sandbox_probe._host_payload = lambda workdir: "raise SystemExit(7)"+ try:+ available, reason = sandbox_probe.probe(backend)+ finally:+ sandbox_probe._host_payload = real+ assert available is False+ assert reason.startswith("the host itself could not pass"), reason+ assert backend.calls == 0+++def test_the_scratch_root_fits_an_af_unix_address():+ base = sandbox_probe._probe_base()+ try:+ # base + "/work/tmp" + "/pymp-XXXXXXXX/listener-XXXXXXXX" must fit in+ # sun_path (108 bytes with the NUL).+ assert len(base) + len("/work/tmp") + 32 < 108, base+ finally:+ import shutil as _shutil+ _shutil.rmtree(base, ignore_errors = True)+++def test_a_backend_that_cannot_prepare_is_unavailable_not_an_exception():+ def explode(plan):+ raise RuntimeError("no user namespaces here")++ available, reason = sandbox_probe.probe(_Backend("exploding", explode))+ assert available is False+ assert "no user namespaces here" in reason+++def test_a_missing_backend_module_is_unavailable_not_an_exception():+ class Absent:+ pass++ available, reason = sandbox_probe.probe(Absent())+ assert available is False+ assert reason+++def test_a_wedged_backend_times_out_instead_of_hanging(monkeypatch):+ monkeypatch.setattr(sandbox_probe, "PROBE_TIMEOUT_SECONDS", 1.0)++ def sleeper(plan):+ return PreparedSandboxLaunch(+ argv = (sys.executable, "-I", "-S", "-c", "import time; time.sleep(120)"),+ workdir = plan.workdir,+ env = plan.env,+ preexec_fn = None,+ backend = "wedged",+ )++ available, reason = sandbox_probe.probe(_Backend("wedged", sleeper))+ assert available is False+ assert "timed out" in reason+++def test_a_backend_that_only_prints_the_token_is_not_believed():+ def liar(plan):+ return PreparedSandboxLaunch(+ argv = (sys.executable, "-I", "-S", "-c", f"print({sandbox_probe.PROBE_TOKEN!r})"),+ workdir = plan.workdir,+ env = plan.env,+ preexec_fn = None,+ backend = "liar",+ )++ # The probe cannot detect a backend that rewrites its own payload, so this+ # pins the weaker guarantee: the payload HANDED OVER carries every control.+ backend = _Backend("liar", liar)+ sandbox_probe.probe(backend)+ handed_over = backend.prepared and True+ assert handed_over+++def test_the_payload_handed_to_the_backend_carries_every_control():+ backend = _Backend("passthrough", _passthrough)+ sandbox_probe.probe(backend)+ payload = backend.prepared[0].argv[-1]+ for control in (+ "read the host sentinel",+ "followed a workdir symlink to the host sentinel",+ sandbox_probe._OUTSIDE_WRITE_TOKEN,+ "socketpair",+ "DupFd",+ "a python child could not run",+ sandbox_probe.PROBE_TOKEN,+ ):+ assert control in payload, control+ if os.access(sys.executable, os.W_OK):+ assert "opened the interpreter for writing" in payload+++def test_the_launch_is_built_by_the_backend_not_by_the_probe():+ backend = _Backend("confining", _confining)+ sandbox_probe.probe(backend)+ assert backend.calls == 1+ plan = None++ def capture(p):+ nonlocal plan+ plan = p+ return _confining(p)++ sandbox_probe.probe(_Backend("capture", capture))+ assert isinstance(plan, ToolLaunchPlan)+ # A real launch plan, so the probe exercises the code path that runs.+ assert plan.execution_kind == "python"+ assert plan.env["HOME"] == plan.workdir+ assert plan.env["TMPDIR"].startswith(plan.workdir + os.sep)+++def test_everything_the_probe_owns_is_released():+ released = []++ def prepare(plan):+ prepared = _confining(plan)+ prepared.cleanup_callbacks.append(lambda: released.append("backend"))+ return prepared++ backend = _Backend("confining", prepare)+ available, reason = sandbox_probe.probe(backend)+ assert available is True, reason+ assert released == ["backend"]+ assert not os.path.exists(backend.prepared[0].workdir)+++def test_cleanup_still_happens_when_the_launch_fails():+ released = []++ def prepare(plan):+ prepared = _passthrough(plan)+ prepared.cleanup_callbacks.append(lambda: released.append("backend"))+ return prepared++ backend = _Backend("passthrough", prepare)+ assert sandbox_probe.probe(backend)[0] is False+ assert released == ["backend"]+ assert not os.path.exists(backend.prepared[0].workdir)+++def test_the_verdict_is_cached_so_a_tool_call_does_not_re_probe():+ backend = _Backend("confining", _confining)+ first = sandbox_probe.probe(backend)+ second = sandbox_probe.probe(backend)+ assert first == second+ assert backend.calls == 1+++def test_force_re_probes():+ backend = _Backend("confining", _confining)+ sandbox_probe.probe(backend)+ sandbox_probe.probe(backend, force = True)+ assert backend.calls == 2+++def test_the_cache_is_keyed_on_the_backend_as_well_as_the_runtime():+ confining = _Backend("confining", _confining)+ passthrough = _Backend("passthrough", _passthrough)+ assert sandbox_probe.probe(confining)[0] is True+ assert sandbox_probe.probe(passthrough)[0] is False+++def test_an_expired_verdict_is_re_probed(monkeypatch):+ """Set before the first probe: the expiry is stamped when the verdict is stored."""+ monkeypatch.setattr(sandbox_probe, "_CACHE_TTL_SECONDS", 0.0)+ backend = _Backend("confining", _confining)+ sandbox_probe.probe(backend)+ sandbox_probe.probe(backend)+ assert backend.calls == 2+++def test_the_cache_cannot_grow_without_bound():+ for index in range(sandbox_probe._CACHE_MAX_ENTRIES * 3):+ sandbox_probe.probe(_Backend(f"passthrough-{index}", _passthrough))+ assert len(sandbox_probe._cache) <= sandbox_probe._CACHE_MAX_ENTRIES+++def test_this_host_reports_unavailable_with_something_actionable():+ if sys.platform != "linux":+ pytest.skip("the AppArmor condition is Linux only")+ capability = os_sandbox.capability_snapshot(force = True)+ if capability.available:+ pytest.skip("this host can build a real sandbox, so there is no fallback to check")+ assert capability.backend == "none"+ assert capability.limitations == ("no_os_isolation",)+ assert capability.remediation+ blocked = (+ subprocess.run(+ ["unshare", "--user", "--map-root-user", "true"],+ stdin = subprocess.DEVNULL,+ stdout = subprocess.DEVNULL,+ stderr = subprocess.DEVNULL,+ ).returncode+ != 0+ )+ if blocked and os.path.exists("/proc/sys/kernel/apparmor_restrict_unprivileged_userns"):+ assert "apparmor_restrict_unprivileged_userns" in capability.remediation+++def test_the_abstract_socket_control_is_paired_like_every_other(monkeypatch):+ from core.inference import sandbox_landlock, sandbox_probe++ monkeypatch.setattr(sandbox_landlock, "abstract_scope_supported", lambda: False)+ assert sandbox_probe._abstract_control() == (None, None)++ monkeypatch.setattr(sandbox_landlock, "abstract_scope_supported", lambda: True)+ if sys.platform != "linux":+ assert sandbox_probe._abstract_control() == (None, None)+ return+ name, listener = sandbox_probe._abstract_control()+ try:+ assert name is not None and name.startswith(b"\0unsloth-probe-")+ assert listener is not None+ finally:+ if listener is not None:+ listener.close()+++def test_the_payload_requires_the_abstract_socket_to_be_out_of_reach():+ from core.inference import sandbox_probe++ with_scope = sandbox_probe._payload(+ "/work", "/sentinel", "/escape", "/outside", False, b"\0host-socket"+ )+ assert "connected to a host abstract unix socket" in with_scope+ assert "AF_UNIX" in with_scope+ without = sandbox_probe._payload("/work", "/sentinel", "/escape", "/outside", False, None)+ assert "abstract" not in without+++def test_the_symlink_leg_survives_a_probe_base_reached_through_a_symlink(monkeypatch):+ # Short names, under the same short root the probe itself prefers: a base over+ # _MAX_PROBE_BASE_LEN makes the fd-passing control's AF_UNIX address too long+ # and the HOST half fails, which says nothing about the symlink.+ root = "/tmp" if os.path.isdir("/tmp") else tempfile.gettempdir()+ holder = tempfile.mkdtemp(prefix = "us-sym-", dir = root)+ real = pathlib.Path(holder) / "r"+ real.mkdir()+ alias = pathlib.Path(holder) / "a"+ alias.symlink_to(real)+ real_mkdtemp = sandbox_probe.tempfile.mkdtemp++ def through_the_symlink(prefix = None, dir = None):+ made = real_mkdtemp(prefix = prefix, dir = str(alias))+ return made.replace(str(real), str(alias))++ monkeypatch.setattr(sandbox_probe.tempfile, "mkdtemp", through_the_symlink)+ try:+ assert str(alias.resolve()) != str(alias) # the premise, not an assumption+ available, reason = sandbox_probe.probe(_Backend("spelling-only", _spelling_only))+ assert available is False+ assert "symlink" in reason, reason+ available, reason = sandbox_probe.probe(_Backend("confining", _confining))+ assert available is True, reason+ finally:+ shutil.rmtree(holder, ignore_errors = True)+++def test_the_landlock_helper_imports_where_it_will_never_be_used():+ source = pathlib.Path(sandbox_landlock.__file__).read_text(encoding = "utf-8")+ guard = re.search(r"except \(([^)]*)\):[^\n]*\n(?:\s*#[^\n]*\n)*\s*_libc = None", source)+ assert guard, "the CDLL(None) import guard moved; this test no longer checks it"+ assert "TypeError" in guard.group(1), guard.group(1)+ # Every entry point has to survive _libc being None, or the guard only moves+ # the failure from import to first use.+ real = sandbox_landlock._libc+ sandbox_landlock._libc = None+ sandbox_landlock.abstract_scope_supported.cache_clear()+ try:+ assert sandbox_landlock.abstract_scope_supported() is False+ sandbox_landlock.apply_abstract_scope() # a no-op, not a crash+ sandbox_landlock.with_abstract_scope(None)()+ finally:+ sandbox_landlock._libc = real+ sandbox_landlock.abstract_scope_supported.cache_clear()diff --git a/studio/backend/tests/test_tool_output_streaming.py b/studio/backend/tests/test_tool_output_streaming.pyindex 9f3605f82c1..0fb3bac7834 100644--- a/studio/backend/tests/test_tool_output_streaming.py+++ b/studio/backend/tests/test_tool_output_streaming.py@@ -87,6 +87,21 @@ def _gated_grandchild_sh(gate: Path, sentinel: Path) -> str:return f"while [ ! -f '{gate}' ]; do sleep {_GATE_POLL_S}; done; touch '{sentinel}'"+def _os_isolated_tools() -> bool:+ """Whether PID-namespace teardown reaps descendants when the leader exits.++ This changes expected output from timeout/cancellation to the leader's output,+ but descendants must die either way. Check the capability flag, not sandbox+ availability: Seatbelt has no PID namespace.+ """+ from core.inference import os_sandbox++ capability = os_sandbox.capability_snapshot()+ return capability.available and (+ "detached_processes_die_with_the_call" in capability.limitations+ )++def _assert_grandchild_was_killed(gate: Path, sentinel: Path) -> None:"""Open the gate, then require the sentinel to stay absent for the whole window."""gate.write_text("go")@@ -625,6 +640,10 @@ def test_bash_exec_invalid_utf8_identical_with_streaming():assert "".join(chunks) == "ok�bad\n"+ _os_isolated_tools(),+ reason = "a PID namespace kills the background job with the leader; see below",+)def test_bash_exec_unlimited_timeout_waits_for_grandchild_output():# A background grandchild holds the pipe open past the shell's exit and writes# ~7s later. With timeout=None the drain must wait for EOF like@@ -637,6 +656,28 @@ def test_bash_exec_unlimited_timeout_waits_for_grandchild_output():assert "late-grandchild-output" in "".join(chunks)+ not _os_isolated_tools(), reason = "no PID namespace here, so nothing reaps the job"+)+def test_bash_exec_unlimited_timeout_does_not_wait_for_a_job_the_namespace_reaps():+ # The isolated counterpart, and the reason "detached_processes_die_with_the+ # _call" is a LIMITATION: the background job goes down with the leader, so the+ # call returns at once rather than blocking on a pipe nothing will write to.+ from core.inference import tools as tools_module++ command = "( sleep 7; echo late-grandchild-output ) & echo parent-done"+ started = time.monotonic()+ result = _bash_exec(command, timeout = None, output_callback = lambda _t: None)+ elapsed = time.monotonic() - started+ assert "parent-done" in result+ # A capability can disappear after collection, for example if an earlier+ # launch invalidates a stale probe. Judge the launch that actually ran.+ if tools_module._last_tool_execution_record.os_isolation:+ assert elapsed < 5+ else:+ assert "late-grandchild-output" in result++def test_bash_exec_finite_timeout_kills_grandchild_holding_stdout(tmp_path):# A backgrounded grandchild holds the pipe open past the finite timeout, then# would write a sentinel. The parent shell has already exited, so killing only@@ -646,7 +687,8 @@ def test_bash_exec_finite_timeout_kills_grandchild_holding_stdout(tmp_path):gate = tmp_path / "gate"command = f"( {_gated_grandchild_sh(gate, sentinel)} ) & echo parent-done"result = _bash_exec(command, timeout = 1, output_callback = lambda _t: None)- assert "timed out" in result+ if not _os_isolated_tools():+ assert "timed out" in result_assert_grandchild_was_killed(gate, sentinel)@@ -660,7 +702,8 @@ def test_bash_exec_nonstreaming_timeout_kills_grandchild(tmp_path):gate = tmp_path / "gate"command = f"( {_gated_grandchild_sh(gate, sentinel)} ) & echo parent-done"result = _bash_exec(command, timeout = 1) # no output_callback -> communicate path- assert "timed out" in result+ if not _os_isolated_tools():+ assert "timed out" in result_assert_grandchild_was_killed(gate, sentinel)@@ -1309,7 +1352,11 @@ def test_bash_exec_nonstreaming_cancel_kills_grandchild_after_leader_exit(tmp_pafinally:timer.cancel()assert time.monotonic() - started < 2.5- assert result == "Execution cancelled."+ # The cancellation string only when tools.py does the teardown: in a PID+ # namespace both processes are gone before the cancel fires. Both paths owe+ # the same two things, and both are asserted.+ if not _os_isolated_tools():+ assert result == "Execution cancelled."_assert_grandchild_was_killed(gate, sentinel)@@ -1331,5 +1378,9 @@ def test_python_exec_nonstreaming_cancel_kills_grandchild_after_leader_exit(tmp_finally:timer.cancel()assert time.monotonic() - started < 2.5- assert result == "Execution cancelled."+ # The cancellation string only when tools.py does the teardown: in a PID+ # namespace both processes are gone before the cancel fires. Both paths owe+ # the same two things, and both are asserted.+ if not _os_isolated_tools():+ assert result == "Execution cancelled."_assert_grandchild_was_killed(gate, sentinel)diff --git a/studio/backend/tests/test_tool_sandbox_wiring.py b/studio/backend/tests/test_tool_sandbox_wiring.pynew file mode 100644index 00000000000..2a6e0dc5ae6--- /dev/null+++ b/studio/backend/tests/test_tool_sandbox_wiring.py@@ -0,0 +1,701 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The two tool launches, now routed through the OS-isolation planner.++Every assertion here must hold on BOTH paths, isolated and fallback. One that+only passes when the sandbox is unavailable is a check on the machine.+"""++from __future__ import annotations++import ast+import errno+import importlib+import inspect+import os+import shutil+import socket+import subprocess+import sys+import textwrap+import time++import pytest++from core.inference import os_sandbox, tools+from core.inference.os_sandbox import (+ PreparedSandboxLaunch,+ SandboxUnavailableError,+ ToolLaunchPlan,+)++_SESSION = "__LOCALID_sandbox_wiring"+++def test_execute_tool_keeps_every_parameter_it_had_in_the_same_order():+ parameters = inspect.signature(tools.execute_tool).parameters+ positional = [+ name+ for name, parameter in parameters.items()+ if parameter.kind is inspect.Parameter.POSITIONAL_OR_KEYWORD+ ]+ assert positional == [+ "name",+ "arguments",+ "cancel_event",+ "timeout",+ "session_id",+ "thread_id",+ "rag_scope",+ "disable_sandbox",+ "output_callback",+ "website_policy",+ "conversation_branch",+ "conversation_budget_tokens",+ "conversation_token_counter",+ "context_tokens",+ "search_images",+ "result_budget_tokens",+ ]+ assert parameters["tool_execution_mode"].kind is inspect.Parameter.KEYWORD_ONLY+ assert parameters["tool_execution_mode"].default == "auto"++[email protected]("function", [tools._python_exec, tools._bash_exec])+def test_the_executors_take_the_mode_keyword_only_and_default_it(function):+ parameters = inspect.signature(function).parameters+ assert parameters["tool_execution_mode"].kind is inspect.Parameter.KEYWORD_ONLY+ assert parameters["tool_execution_mode"].default == "auto"+ positional = [+ name+ for name, parameter in parameters.items()+ if parameter.kind is inspect.Parameter.POSITIONAL_OR_KEYWORD+ ]+ assert positional[:5] == [+ positional[0],+ "cancel_event",+ "timeout",+ "session_id",+ "disable_sandbox",+ ]+++def test_a_caller_that_passes_nothing_new_behaves_as_auto():+ tools._last_tool_execution_record = None+ assert "2" in tools._python_exec("print(1 + 1)", None, 60, _SESSION)+ assert tools._last_tool_execution_record.requested_mode == "auto"+++def test_disable_sandbox_still_means_full_access():+ tools._last_tool_execution_record = None+ assert "7" in tools._python_exec("print(7)", None, 60, _SESSION, disable_sandbox = True)+ record = tools._last_tool_execution_record+ assert record.requested_mode == "full"+ assert record.effective_mode == "full"+ assert record.limitations == ("security_restrictions_disabled",)+++def test_full_access_is_not_turned_into_a_refusal_by_a_stale_required():+ tools._last_tool_execution_record = None+ out = tools._python_exec(+ "print(11)", None, 60, _SESSION, disable_sandbox = True, tool_execution_mode = "required"+ )+ assert "11" in out+ assert tools._last_tool_execution_record.effective_mode == "full"+++def _fallback_host() -> bool:+ return not os_sandbox.capability_snapshot().available+++ not _fallback_host(), reason = "this host can isolate, so there is no fallback to observe"+)+class TestAutoFallsBackOnAHostThatCannotIsolate:+ def test_the_capability_is_unavailable_with_an_actionable_remediation(self):+ capability = os_sandbox.capability_snapshot()+ assert capability.available is False+ assert capability.remediation+ if sys.platform == "linux" and os.path.exists(+ "/proc/sys/kernel/apparmor_restrict_unprivileged_userns"+ ):+ blocked = (+ subprocess.run(+ ["unshare", "--user", "--map-root-user", "true"],+ stdin = subprocess.DEVNULL,+ stdout = subprocess.DEVNULL,+ stderr = subprocess.DEVNULL,+ ).returncode+ != 0+ )+ if blocked:+ assert "apparmor_restrict_unprivileged_userns" in capability.remediation+ assert "bwrap-userns-restrict" in capability.remediation++ @pytest.mark.parametrize(+ "run,expected",+ [+ (lambda: tools._python_exec("print(6 * 7)", None, 60, _SESSION), "42"),+ (lambda: tools._bash_exec("echo 42", None, 60, _SESSION), "42"),+ ],+ ids = ["python", "terminal"],+ )+ def test_auto_refuses_nothing(self, run, expected):+ tools._last_tool_execution_record = None+ assert expected in run()+ record = tools._last_tool_execution_record+ assert record.requested_mode == "auto"+ assert record.effective_mode == "software_safeguards"+ assert record.os_isolation is False+ assert "process_guard" in record.retained_safeguards+ assert "no_os_isolation" in record.limitations+ assert record.network_policy == "unrestricted"++ def test_required_refuses_and_runs_nothing(self):+ out = tools._python_exec(+ "print('SHOULD_NOT_RUN')", None, 60, _SESSION, tool_execution_mode = "required"+ )+ assert "SHOULD_NOT_RUN" not in out+ assert "OS_ISOLATION_UNAVAILABLE" in out+ assert os_sandbox.capability_snapshot().remediation.split(".")[0] in out++ def test_required_raises_out_of_the_planner_itself(self):+ plan = ToolLaunchPlan(+ argv = (sys.executable, "-c", "pass"),+ workdir = os.getcwd(),+ env = {},+ requested_mode = "required",+ )+ with pytest.raises(SandboxUnavailableError) as excinfo:+ os_sandbox.prepare_tool_launch(plan)+ assert excinfo.value.remediation++ def test_terminal_required_refuses_too(self):+ out = tools._bash_exec(+ "echo SHOULD_NOT_RUN", None, 60, _SESSION, tool_execution_mode = "required"+ )+ assert "SHOULD_NOT_RUN" not in out+ assert "OS_ISOLATION_UNAVAILABLE" in out+++def test_an_unknown_mode_is_reported_rather_than_silently_downgraded():+ out = tools._python_exec(+ "print('SHOULD_NOT_RUN')", None, 60, _SESSION, tool_execution_mode = "nonsense"+ )+ assert "SHOULD_NOT_RUN" not in out+ assert "nonsense" in out+++ sys.platform == "win32", reason = "pre-exec and pass_fds are POSIX; Windows keeps today's path"+)+def test_the_process_unsloth_holds_still_lands_in_its_own_session():+ """Asserted about the OUTER process: under bubblewrap the payload is not a+ session leader, so asking it about its own sid only passes on a fallback."""+ # Warm both executors before counting per-call spawns. Capability probes,+ # xcode-select and shell discovery are one-time initialization costs.+ tools._python_exec("pass", None, 60, _SESSION)+ tools._bash_exec("true", None, 60, _SESSION)++ seen = []+ real = subprocess.Popen++ def capture(argv, **kwargs):+ seen.append((argv, kwargs.get("preexec_fn")))+ return real(argv, **kwargs)++ subprocess.Popen = capture+ try:+ assert "5" in tools._python_exec("print(2 + 3)", None, 60, _SESSION)+ assert "6" in tools._bash_exec("echo 6", None, 60, _SESSION)+ finally:+ subprocess.Popen = real+ # Count pre-exec tool launches separately from known bookkeeping spawns,+ # including macOS's per-call ps liveness check.+ launches = [preexec for _, preexec in seen if preexec is not None]+ bookkeeping = [argv for argv, preexec in seen if preexec is None]+ # The argv is in the message because a count alone cannot say WHICH extra+ # spawn appeared, and this only ever fails on a runner nobody can attach to.+ assert len(launches) == 2, [argv for argv, _ in seen]+ assert all(tuple(argv)[:1] == ("ps",) for argv in bookkeeping), bookkeeping+ seen = launches+ # Asked by result, not identity: an isolated launch composes the plan's+ # pre-exec with the backend's, so the object differs either way.+ for preexec in seen:+ read_fd, write_fd = os.pipe()+ child = os.fork()+ if child == 0: # pragma: no cover - runs in the forked child+ try:+ os.close(read_fd)+ preexec()+ os.write(write_fd, b"1" if os.getsid(0) == os.getpid() else b"0")+ finally:+ os._exit(0)+ os.close(write_fd)+ with os.fdopen(read_fd, "rb") as stream:+ leads_its_session = stream.read()+ os.waitpid(child, 0)+ assert leads_its_session == b"1"+++def test_a_timeout_kills_the_tool_and_leaves_the_server_running():+ start = time.monotonic()+ out = tools._python_exec("import time; time.sleep(120)", None, 5, _SESSION)+ assert "timed out" in out.lower(), out+ assert time.monotonic() - start < 60+ assert "9" in tools._python_exec("print(4 + 5)", None, 60, _SESSION)+++ sys.platform == "win32", reason = "pre-exec and pass_fds are POSIX; Windows keeps today's path"+)+def test_the_plan_carries_the_pre_exec_the_kill_paths_depend_on():+ seen = []+ real = os_sandbox.prepare_tool_launch++ def capture(plan):+ seen.append(plan)+ return real(plan)++ os_sandbox.prepare_tool_launch = capture+ try:+ tools._python_exec("print(1)", None, 60, _SESSION)+ tools._bash_exec("echo 1", None, 60, _SESSION)+ tools._python_exec("print(1)", None, 60, _SESSION, disable_sandbox = True)+ finally:+ os_sandbox.prepare_tool_launch = real+ assert [plan.preexec_fn for plan in seen] == [+ tools._sandbox_preexec,+ tools._sandbox_preexec,+ tools._bypass_preexec,+ ]+ assert [plan.execution_kind for plan in seen] == ["python", "terminal", "python"]+++def test_sandbox_preexec_runs_no_imports_after_the_fork():+ for function in (tools._sandbox_preexec, tools._bypass_preexec):+ tree = ast.parse(textwrap.dedent(inspect.getsource(function)))+ offenders = [+ node for node in ast.walk(tree) if isinstance(node, (ast.Import, ast.ImportFrom))+ ]+ assert not offenders, f"{function.__name__} imports after the fork"++[email protected]("disable_sandbox", [False, True], ids = ["sandboxed", "full"])+def test_the_environment_the_shim_depends_on_survives(disable_sandbox):+ workdir = tools._get_workdir(_SESSION)+ out = tools._python_exec(+ "import os\n"+ "print('PYTHONPATH', os.environ.get('PYTHONPATH'))\n"+ "print('HOME', os.environ.get('HOME'))\n"+ "print('TMPDIR', os.environ.get('TMPDIR'))\n",+ None,+ 60,+ _SESSION,+ disable_sandbox = disable_sandbox,+ )+ lines = dict(line.split(" ", 1) for line in out.splitlines() if " " in line)+ assert tools._SANDBOX_SITE_DIR in lines["PYTHONPATH"]+ assert lines["HOME"] == workdir+ assert lines["TMPDIR"].startswith(workdir)+++def test_the_path_remap_shim_still_heals_an_invented_absolute_path():+ result = tools._python_exec(+ "open('/mnt/data/wiring_probe.csv', 'w').write('a,b\\n')", None, 60, _SESSION+ )+ assert "Error" not in result+ assert os.path.exists(os.path.join(tools._get_workdir(_SESSION), "wiring_probe.csv"))+++class _Recorder:+ def __init__(self):+ self.cleaned = 0+ self.spawn_kwargs = None+++def _echoing_prepare(recorder, **overrides):+ def prepare(plan):+ prepared = PreparedSandboxLaunch(+ argv = plan.argv,+ workdir = plan.workdir,+ env = plan.env,+ preexec_fn = plan.preexec_fn,+ backend = "test-double",+ **overrides,+ )+ prepared.cleanup_callbacks.append(+ lambda: setattr(recorder, "cleaned", recorder.cleaned + 1)+ )+ return prepared++ return prepare+++ "run",+ [+ lambda: tools._python_exec("print(1)", None, 60, _SESSION),+ lambda: tools._bash_exec("echo 1", None, 60, _SESSION),+ ],+ ids = ["python", "terminal"],+)+def test_the_launch_is_released_when_the_process_finishes(monkeypatch, run):+ recorder = _Recorder()+ monkeypatch.setattr(os_sandbox, "prepare_tool_launch", _echoing_prepare(recorder))+ run()+ assert recorder.cleaned == 1+++def test_the_launch_is_released_when_the_spawn_raises(monkeypatch):+ recorder = _Recorder()+ monkeypatch.setattr(os_sandbox, "prepare_tool_launch", _echoing_prepare(recorder))++ def explode(prepared, **kwargs):+ raise OSError("no fork for you")++ monkeypatch.setattr(os_sandbox, "spawn_prepared_launch", explode)+ assert "no fork for you" in tools._python_exec("print(1)", None, 60, _SESSION)+ assert recorder.cleaned == 1+++def test_the_launch_is_released_when_required_refuses(monkeypatch):+ def refuse(plan):+ raise SandboxUnavailableError("OS_ISOLATION_UNAVAILABLE: nope", remediation = "install it")++ monkeypatch.setattr(os_sandbox, "prepare_tool_launch", refuse)+ out = tools._python_exec("print(1)", None, 60, _SESSION, tool_execution_mode = "required")+ assert "install it" in out+++ sys.platform == "win32", reason = "pre-exec and pass_fds are POSIX; Windows keeps today's path"+)+def test_pass_fds_and_owned_files_reach_the_spawn(monkeypatch):+ read_fd, write_fd = os.pipe()+ holder = os.fdopen(write_fd, "wb")+ recorder = _Recorder()+ monkeypatch.setattr(+ os_sandbox,+ "prepare_tool_launch",+ _echoing_prepare(recorder, pass_fds = (read_fd,)),+ )++ real_spawn = os_sandbox.spawn_prepared_launch++ def capture(prepared, **kwargs):+ prepared.owned_files.append(holder)+ recorder.spawn_kwargs = dict(kwargs)+ return real_spawn(prepared, **kwargs)++ monkeypatch.setattr(os_sandbox, "spawn_prepared_launch", capture)+ try:+ out = tools._python_exec(+ f"import os; os.fstat({read_fd}); print('INHERITED')", None, 60, _SESSION+ )+ finally:+ os.close(read_fd)+ assert "INHERITED" in out, out+ assert recorder.spawn_kwargs["pass_fds"] == (read_fd,)+ assert recorder.spawn_kwargs["cwd"] == tools._get_workdir(_SESSION)+ assert recorder.spawn_kwargs["close_fds"] is True+ assert recorder.cleaned == 1+ assert holder.closed+++def test_auto_still_runs_when_the_planner_itself_breaks(monkeypatch):+ def explode(plan):+ raise ImportError("no module named sandbox_linux")++ monkeypatch.setattr(os_sandbox, "prepare_tool_launch", explode)+ tools._last_tool_execution_record = None+ assert "5" in tools._python_exec("print(5)", None, 60, _SESSION)+ record = tools._last_tool_execution_record+ assert record.effective_mode == "software_safeguards"+ assert "sandbox_planner_error" in record.limitations+++def test_full_access_keeps_its_own_label_even_when_the_planner_breaks(monkeypatch):+ def explode(plan):+ raise RuntimeError("planner down")++ monkeypatch.setattr(os_sandbox, "prepare_tool_launch", explode)+ tools._last_tool_execution_record = None+ assert "9" in tools._python_exec("print(9)", None, 60, _SESSION, disable_sandbox = True)+ record = tools._last_tool_execution_record+ assert record.effective_mode == "full"+ assert "security_restrictions_disabled" in record.limitations+ assert "command_and_code_analysis" not in record.retained_safeguards+++ sys.platform == "win32", reason = "pre-exec and pass_fds are POSIX; Windows keeps today's path"+)+def test_a_backend_that_drops_the_pre_exec_has_it_put_back(monkeypatch):+ def forgetful(plan):+ return PreparedSandboxLaunch(+ argv = plan.argv,+ workdir = plan.workdir,+ env = plan.env,+ preexec_fn = None, # the bug+ backend = "forgetful",+ )++ monkeypatch.setattr(os_sandbox, "prepare_tool_launch", forgetful)+ out = tools._python_exec(+ "import os; print('SID_MATCHES', os.getsid(0) == os.getpid())", None, 60, _SESSION+ )+ assert "SID_MATCHES True" in out, out+++def test_required_still_refuses_when_the_planner_itself_breaks(monkeypatch):+ def explode(plan):+ raise ImportError("no module named sandbox_linux")++ monkeypatch.setattr(os_sandbox, "prepare_tool_launch", explode)+ out = tools._python_exec(+ "print('SHOULD_NOT_RUN')", None, 60, _SESSION, tool_execution_mode = "required"+ )+ assert "SHOULD_NOT_RUN" not in out+ assert "OS_ISOLATION_UNAVAILABLE" in out++[email protected]("platform", ["win32", "cygwin", "aix"])+def test_a_platform_with_no_backend_gets_exactly_the_plan_it_handed_in(monkeypatch, platform):+ monkeypatch.setattr(sys, "platform", platform)++ def marker():+ return None++ env = {"PATH": "/usr/bin"}+ plan = ToolLaunchPlan(+ argv = ("prog", "arg"),+ workdir = "/work",+ env = env,+ preexec_fn = marker,+ requested_mode = "auto",+ timeout_seconds = 300,+ )+ prepared = os_sandbox.prepare_tool_launch(plan)+ assert prepared.argv == plan.argv+ assert prepared.env is env+ assert prepared.preexec_fn is marker+ assert prepared.workdir == "/work"+ assert prepared.pass_fds == ()+ assert prepared.close_fds is True+ assert prepared.execution_record.effective_mode == "software_safeguards"+ assert prepared.execution_record.os_isolation is False+++def test_a_platform_with_no_backend_still_refuses_in_required(monkeypatch):+ monkeypatch.setattr(sys, "platform", "win32")+ with pytest.raises(SandboxUnavailableError):+ os_sandbox.prepare_tool_launch(+ ToolLaunchPlan(argv = ("prog",), workdir = "/work", env = {}, requested_mode = "required")+ )+++def test_the_tool_descriptions_are_untouched_by_this_change():+ note = tools._build_sandbox_paths_note()+ assert "isolation" not in note.lower()+ full = tools._to_full_access(+ "Execute Python code in a sandbox and return stdout/stderr." + note, "python"+ )+ assert "in a sandbox" not in full+ assert "sandbox is disabled" in full+++def _declining_backend(+ monkeypatch,+ reason: str,+ unsafe: bool = True,+) -> None:+ """*unsafe* picks WHICH refusal; the two are told apart by type."""+ error = os_sandbox.WorkdirUnsafeError if unsafe else SandboxUnavailableError++ def decline(plan):+ raise error(reason)++ monkeypatch.setattr(os_sandbox, "prepare_tool_launch", decline)+++def test_an_unsafe_workdir_fails_the_call_rather_than_de_isolating_it(monkeypatch):+ _declining_backend(monkeypatch, "the session workdir contains a device node")+ tools._last_tool_execution_record = None+ out = tools._python_exec("print('SHOULD_NOT_RUN')", None, 60, _SESSION)+ assert "SHOULD_NOT_RUN" not in out+ assert "device node" in out+ assert tools._last_tool_execution_record is None+++def test_required_still_refuses_when_the_backend_declines_this_launch(monkeypatch):+ _declining_backend(monkeypatch, "the session workdir contains a device node")+ out = tools._python_exec(+ "print('SHOULD_NOT_RUN')", None, 60, _SESSION, tool_execution_mode = "required"+ )+ assert "SHOULD_NOT_RUN" not in out+ assert "device node" in out++[email protected](sys.platform == "win32", reason = "the workdir scan is POSIX only")+def test_tool_code_cannot_switch_the_boundary_off_for_the_next_call(monkeypatch):+ if not os_sandbox.capability_snapshot().available:+ pytest.skip("this host cannot isolate, so there is no boundary to switch off")+ workdir = tools._get_workdir(_SESSION)+ planted = os.path.join(workdir, "planted.sock")+ holder = socket.socket(socket.AF_UNIX)+ # Bound RELATIVE: an AF_UNIX address is capped at ~108 bytes, and under+ # `pytest -n 4` the studio home is a per-worker tmp_path that alone exceeds+ # it, so the absolute spelling raised "AF_UNIX path too long" instead of+ # planting anything. Backend CI runs -n 4.+ monkeypatch.chdir(workdir)+ holder.bind("planted.sock")+ try:+ tools._last_tool_execution_record = None+ out = tools._python_exec("print('SHOULD_NOT_RUN')", None, 60, _SESSION)+ assert "SHOULD_NOT_RUN" not in out+ assert "device or IPC node" in out+ assert tools._last_tool_execution_record is None+ finally:+ holder.close()+ os.unlink(planted)+ assert "42" in tools._python_exec("print(6 * 7)", None, 60, _SESSION)+ assert tools._last_tool_execution_record.os_isolation is True+++ "function,payload",+ [(tools._python_exec, "print('SHOULD_NOT_RUN')"), (tools._bash_exec, "echo SHOULD_NOT_RUN")],+ ids = ["python", "terminal"],+)+def test_full_is_not_requestable_through_the_mode(function, payload):+ tools._last_tool_execution_record = None+ out = function(payload, None, 60, _SESSION, tool_execution_mode = "full")+ assert "SHOULD_NOT_RUN" not in out+ assert "TOOL_EXECUTION_MODE_INVALID" in out+ assert "disable_sandbox" in out+ assert tools._last_tool_execution_record is None+++def test_disable_sandbox_is_still_the_way_to_full_access():+ tools._last_tool_execution_record = None+ assert "7" in tools._python_exec(+ "print(7)", None, 60, _SESSION, disable_sandbox = True, tool_execution_mode = "full"+ )+ assert tools._last_tool_execution_record.effective_mode == "full"+++ "run",+ [+ lambda: tools._python_exec(+ "import sys; print('IN', repr(sys.stdin.read()))", None, 60, _SESSION+ ),+ lambda: tools._bash_exec("printf 'IN %s\\n' \"$(cat)\"", None, 60, _SESSION),+ ],+ ids = ["python", "terminal"],+)+def test_a_tool_call_cannot_read_the_servers_stdin(run):+ read_fd, write_fd = os.pipe()+ os.write(write_fd, b"OPERATOR SECRET\n")+ os.close(write_fd)+ saved = os.dup(0)+ try:+ os.dup2(read_fd, 0)+ out = run()+ finally:+ os.dup2(saved, 0)+ os.close(saved)+ os.close(read_fd)+ assert "OPERATOR SECRET" not in out+ assert "IN" in out+++def test_the_fallback_never_claims_a_descendant_sweep_it_does_not_perform():+ limitations = os_sandbox._software_only_limitations()+ if sys.platform == "win32":+ assert "detached_descendant_cleanup_unverified" not in limitations+ else:+ assert "detached_descendant_cleanup_unverified" in limitations+ assert not hasattr(os_sandbox, "descendant_sweep_supported")+++def test_a_backend_that_has_just_stopped_being_available_still_falls_back(monkeypatch):+ _declining_backend(+ monkeypatch, "bubblewrap (bwrap) is not installed on this host", unsafe = False+ )+ tools._last_tool_execution_record = None+ assert "42" in tools._python_exec("print(6 * 7)", None, 60, _SESSION)+ record = tools._last_tool_execution_record+ assert record.os_isolation is False+ assert "sandbox_became_unavailable" in record.limitations+++def test_a_backend_that_fails_at_launch_drops_the_cached_verdict(monkeypatch):+ reset = []+ monkeypatch.setattr(+ "core.inference.sandbox_probe.reset_probe_cache", lambda: reset.append(True)+ )+ prepared = PreparedSandboxLaunch(+ argv = ("bwrap",), workdir = "/work", env = {}, preexec_fn = None, backend = "bubblewrap"+ )+ tools._forget_sandbox_capability_if_the_backend_failed(+ prepared, "Exit code 1:\nbwrap: setting up uid map: Permission denied\n"+ )+ assert reset == [True]++ tools._forget_sandbox_capability_if_the_backend_failed(+ prepared, "Exit code 1:\nTraceback (most recent call last):\n"+ )+ prepared.backend = "software-safeguards"+ tools._forget_sandbox_capability_if_the_backend_failed(+ prepared, "Exit code 1:\nbwrap: setting up uid map: Permission denied\n"+ )+ assert reset == [True]+++def test_a_planner_os_error_refuses_rather_than_running_unisolated(monkeypatch):+ backend = importlib.import_module(+ "core.inference.sandbox_linux"+ if sys.platform == "linux"+ else "core.inference.sandbox_macos"+ )++ def full_disk(plan):+ raise OSError(errno.ENOSPC, "No space left on device")++ # Through the real prepare_tool_launch, because the wrap that types this+ # lives in it.+ monkeypatch.setattr(+ os_sandbox,+ "capability_snapshot",+ lambda **kwargs: os_sandbox.SandboxCapability(+ backend = backend.BACKEND_NAME,+ available = True,+ reason = "probe passed",+ profile_id = backend.PROFILE_ID,+ ),+ )+ monkeypatch.setattr(backend, "prepare", full_disk)+ tools._last_tool_execution_record = None+ out = tools._python_exec("print('SHOULD_NOT_RUN')", None, 60, _SESSION)+ assert "SHOULD_NOT_RUN" not in out+ assert tools._last_tool_execution_record is None+++def test_an_unisolated_launch_cannot_be_hooked_by_a_planted_usercustomize(tmp_path):+ workdir = tmp_path / "session"+ (workdir / os_sandbox.SESSION_PACKAGES_RELPATH).mkdir(parents = True)+ env = tools._with_session_packages({"PATH": "/usr/bin"}, str(workdir))+ assert env["PYTHONNOUSERSITE"] == "1"+++def test_the_shipped_sitecustomize_is_found_before_the_session_packages(tmp_path):+ workdir = tmp_path / "session"+ (workdir / os_sandbox.SESSION_PACKAGES_RELPATH).mkdir(parents = True)+ env = tools._with_session_packages({"PYTHONPATH": tools._SANDBOX_SITE_DIR}, str(workdir))+ entries = env["PYTHONPATH"].split(os.pathsep)+ assert entries.index(tools._SANDBOX_SITE_DIR) < entries.index(+ str(workdir / os_sandbox.SESSION_PACKAGES_RELPATH)+ )diff --git a/tests/studio/playwright_queue_localization.py b/tests/studio/playwright_queue_localization.pyindex 8905c330509..becd6b0f556 100644--- a/tests/studio/playwright_queue_localization.py+++ b/tests/studio/playwright_queue_localization.py@@ -25,7 +25,13 @@ JA = {}-def seed(page, base, *, locale = "en", shortcut = "enter"):+def seed(+ page,+ base,+ *,+ locale = "en",+ shortcut = "enter",+):page.goto(base + PAGE)page.evaluate("""([locale, shortcut]) => {@@ -109,7 +115,7 @@ def check_escape_during_ime(page, base):seed(page, base)editor = open_editor(page, "More options for queued prompt 1", "Edit message")editor.fill("draft-survives-ime-escape")- for init in ('{isComposing: true}', '{keyCode: 229}'):+ for init in ("{isComposing: true}", "{keyCode: 229}"):page.evaluate(f"""() => {{const ta = document.querySelector('textarea[aria-label^="Edit queued prompt"]');@@ -155,9 +161,7 @@ def check_candidate_confirming_enter(page, base):)editor.press("Enter")expect(editor).to_have_count(0)- expect(page.locator("[data-queue-item-id]").first).to_contain_text(- "composition-in-progress"- )+ expect(page.locator("[data-queue-item-id]").first).to_contain_text("composition-in-progress")print("PASS: a candidate-confirming Enter does not save the edit", flush = True)@@ -182,9 +186,7 @@ def check_stuck_composition_recovers(page, base):page.wait_for_timeout(2800)editor.press("Enter")expect(editor).to_have_count(0)- expect(page.locator("[data-queue-item-id]").first).to_contain_text(- "recovers-after-timeout"- )+ expect(page.locator("[data-queue-item-id]").first).to_contain_text("recovers-after-timeout")# Blur is the other reset point.editor = open_editor(page, "More options for queued prompt 2", "Edit message")@@ -194,9 +196,7 @@ def check_stuck_composition_recovers(page, base):editor.focus()editor.press("Enter")expect(editor).to_have_count(0)- expect(page.locator("[data-queue-item-id]").nth(1)).to_contain_text(- "recovers-after-blur"- )+ expect(page.locator("[data-queue-item-id]").nth(1)).to_contain_text("recovers-after-blur")print("PASS: a stuck composition recovers on timeout and on blur", flush = True)