mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Let the MCP worker sandbox run Nix/Guix interpreters by mounting exactly the interpreter's resolved runtime closure, read-only (#521). - obliteratus/mcp_runtime_layout.py resolves the closure only when the interpreter's loader isn't already mounted: the interpreter, its loader, stdlib lib-dynload modules and every shared object listed in installed distributions' RECORD files, following DT_NEEDED with glibc's search order (DT_RPATH chain, LD_LIBRARY_PATH, DT_RUNPATH, loader directory; $ORIGIN only). - Each required /nix/store or /gnu/store path is mounted read-only at its original location; other libraries are mounted as single files. Never /, a store root, home, tmp, runtime dirs or the checkout; store names are validated and every mount stays fd-pinned. - The sandbox and the supervisor receive only the LD_LIBRARY_PATH entries the closure actually used, never the ambient value; FHS hosts get none and their mount set is unchanged. - Bounded parsing and resolution with precise fail-closed refusals; the #522 loader refusal still applies to anything unresolved. - The bubblewrap boundary test now runs and passes under the Nix flake, and the worker-boundary and crash-window tests pass there again.
699 lines
32 KiB
Python
699 lines
32 KiB
Python
"""Offline execution security/lifecycle contracts; never launch model runtimes."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from dataclasses import asdict, replace
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from obliteratus.mcp_execution import ExecutionTools, ModelInput, RunResources, build_arguments
|
|
from obliteratus.mcp_policy import EXEC_TOOLS, READ_TOOLS, McpPolicy, McpPolicyError
|
|
from obliteratus.mcp_worker import (
|
|
TAG, _environment, atomic_json, cancel_guarded, copy_checkpoint, locked,
|
|
recover, sandbox_command, supervise, worker_matches,
|
|
retain_evidence,
|
|
)
|
|
from obliteratus.read_boundary import open_directory
|
|
|
|
|
|
@pytest.fixture
|
|
def execution(tmp_path, monkeypatch):
|
|
archive = tmp_path / 'archive'
|
|
archive.mkdir(mode=0o700)
|
|
model = tmp_path / 'model'
|
|
model.mkdir()
|
|
(model / 'config.json').write_text('{}')
|
|
(model / 'model.safetensors').write_bytes(b'synthetic; no tensor loader needed')
|
|
monkeypatch.setattr('obliteratus.mcp_execution.sandbox_identity', lambda: (1, 2, 3, 4))
|
|
monkeypatch.setattr('obliteratus.mcp_worker.worker_matches', lambda *_: True)
|
|
monkeypatch.setattr('obliteratus.run_archive._process_start_ticks', lambda _: 123)
|
|
calls = []
|
|
|
|
def spawn(command, **kwargs):
|
|
run_id = command[command.index('--run-id') + 1]
|
|
assert json.loads((archive / run_id / 'manifest.json').read_text())['status'] == 'queued'
|
|
assert kwargs['stdin'] == kwargs['stdout'] == kwargs['stderr'] == subprocess.DEVNULL
|
|
assert kwargs['start_new_session'] is True
|
|
assert 'HF_TOKEN' not in kwargs['env']
|
|
calls.append((command, kwargs))
|
|
return SimpleNamespace(pid=900001)
|
|
|
|
monkeypatch.setattr('obliteratus.run_archive._spawn_mcp_worker', spawn)
|
|
tools = ExecutionTools(McpPolicy(archive_root=archive, allowed_roots=(model,), execution_enabled=True))
|
|
return tools, ModelInput('local', str(model)), calls
|
|
|
|
|
|
def plan(execution, **overrides):
|
|
tools, model, _ = execution
|
|
return tools.plan_run(model, 'basic', str(tools.policy.archive_root), RunResources(1000, **overrides))
|
|
|
|
|
|
def manifest(execution, run_id):
|
|
return execution[0].policy.archive_root / run_id / 'manifest.json'
|
|
|
|
|
|
def test_plan_has_actual_destination_estimate_effects_and_no_writes(execution):
|
|
tools, _, calls = execution
|
|
result = plan(execution)
|
|
assert result.destination == str(tools.policy.archive_root / result.run_id / 'checkpoint')
|
|
assert result.estimated_weight_bytes == 4000
|
|
assert result.expires_in_seconds == 60
|
|
assert result.side_effects and result.network is False
|
|
assert list(tools.policy.archive_root.iterdir()) == []
|
|
assert calls == []
|
|
|
|
|
|
def test_single_use_token_launches_allowlisted_argv_and_reconnects(execution):
|
|
tools, _, calls = execution
|
|
planned = plan(execution)
|
|
launched = tools.launch_run(planned.token)
|
|
assert launched.run_id == planned.run_id and len(calls) == 1
|
|
value = json.loads(manifest(execution, planned.run_id).read_text())
|
|
assert value['mcp_guard'] == TAG
|
|
assert value['arguments'][:5] == ['/tmp/mcp-model', '--method', 'basic', '--device', 'cpu']
|
|
assert '--trust-remote-code' not in value['arguments']
|
|
assert '--output-dir' not in value['arguments']
|
|
with pytest.raises(ValueError, match='token'):
|
|
tools.launch_run(planned.token)
|
|
reconnected = ExecutionTools(tools.policy)
|
|
assert reconnected.get_run_status(planned.run_id).status == 'queued'
|
|
assert reconnected.list_runs().runs[0].run_id == planned.run_id
|
|
with pytest.raises(ValueError):
|
|
reconnected.get_run_result(planned.run_id)
|
|
with pytest.raises(ValueError):
|
|
reconnected.launch_run(planned.token)
|
|
|
|
|
|
@pytest.mark.parametrize('resolved', [['/nix/store/x-gcc-lib/lib'], []],
|
|
ids=['closure-entries', 'fhs-none'])
|
|
def test_supervisor_receives_only_closure_resolved_library_path(execution, monkeypatch, resolved):
|
|
from obliteratus import mcp_worker
|
|
tools, _, calls = execution
|
|
# Ambient value is never forwarded as such; only entries the closure resolved.
|
|
monkeypatch.setenv('LD_LIBRARY_PATH', '/ambient/untrusted:/nix/store/x-gcc-lib/lib')
|
|
monkeypatch.setattr(mcp_worker, '_runtime_layout', lambda network: ([], list(resolved)))
|
|
tools.launch_run(plan(execution).token)
|
|
environment = calls[0][1]['env']
|
|
expected = ':'.join(resolved) or None
|
|
assert environment.get('LD_LIBRARY_PATH') == expected
|
|
assert '/ambient/untrusted' not in environment.get('LD_LIBRARY_PATH', '')
|
|
assert set(environment) <= {'PATH', 'PYTHONPATH', 'PYTHONDONTWRITEBYTECODE', 'LD_LIBRARY_PATH'}
|
|
|
|
|
|
def test_expiry_and_forgery_reject_without_spawn(execution, monkeypatch):
|
|
tools, _, calls = execution
|
|
now = [10.0]
|
|
monkeypatch.setattr('obliteratus.mcp_execution.time.monotonic', lambda: now[0])
|
|
planned = plan(execution)
|
|
now[0] = 71.0
|
|
for token in (planned.token, 'forged', 'x' * 129):
|
|
with pytest.raises(ValueError):
|
|
tools.launch_run(token)
|
|
assert not calls
|
|
|
|
|
|
@pytest.mark.parametrize('changes', [
|
|
{'parameters': 0}, {'parameters': True}, {'parameters': float('inf')},
|
|
{'parameters': 10**13 + 1}, {'memory_bytes': 1 << 41}, {'work_bytes': 1 << 40},
|
|
{'threads': 5}, {'runtime_seconds': 3601}, {'sequence_length': 513},
|
|
{'generation_tokens': 129},
|
|
])
|
|
def test_resource_validation(execution, changes):
|
|
tools, model, calls = execution
|
|
resources = replace(RunResources(1000), **changes)
|
|
with pytest.raises(ValueError):
|
|
tools.plan_run(model, 'basic', str(tools.policy.archive_root), resources)
|
|
assert not calls
|
|
|
|
|
|
@pytest.mark.parametrize('change', ['method', 'output', 'traversal', 'model_link', 'model_file_link', 'pickle', 'remote'])
|
|
def test_model_method_output_policy_rejects(execution, change):
|
|
tools, model, _ = execution
|
|
method, output = 'basic', str(tools.policy.archive_root)
|
|
path = Path(model.identifier)
|
|
if change == 'method':
|
|
method = 'basic; touch /tmp/forged'
|
|
elif change == 'output':
|
|
output = str(path.parent / 'unmanaged')
|
|
elif change == 'traversal':
|
|
model = ModelInput('local', str(path / '..' / path.name))
|
|
elif change == 'model_link':
|
|
link = path.parent / 'link'
|
|
link.symlink_to(path, target_is_directory=True)
|
|
model = ModelInput('local', str(link))
|
|
elif change == 'model_file_link':
|
|
(path / 'link').symlink_to('/etc/passwd')
|
|
elif change == 'pickle':
|
|
(path / 'pytorch_model.bin').write_bytes(b'unsafe')
|
|
else:
|
|
model = ModelInput('remote', 'org/model')
|
|
with pytest.raises((ValueError, OSError)):
|
|
tools.plan_run(model, method, output, RunResources(1000))
|
|
|
|
|
|
@pytest.mark.parametrize('replacement', ['root', 'root_link', 'model', 'model_contents', 'sandbox'])
|
|
def test_launch_revalidates_and_consumes_changed_plan(execution, replacement, monkeypatch):
|
|
tools, model, calls = execution
|
|
planned = plan(execution)
|
|
root = tools.policy.archive_root
|
|
if replacement == 'root':
|
|
root.rename(root.with_name('old'))
|
|
root.mkdir(mode=0o700)
|
|
elif replacement == 'root_link':
|
|
root.rename(root.with_name('old'))
|
|
root.symlink_to(root.with_name('old'), target_is_directory=True)
|
|
elif replacement == 'model':
|
|
source = Path(model.identifier)
|
|
source.rename(source.with_name('old-model'))
|
|
source.mkdir()
|
|
(source / 'config.json').write_text('{}')
|
|
(source / 'model.safetensors').write_bytes(b'replacement')
|
|
elif replacement == 'model_contents':
|
|
(Path(model.identifier) / 'config.json').write_text('{"changed": true}')
|
|
else:
|
|
monkeypatch.setattr('obliteratus.mcp_execution.sandbox_identity', lambda: (9, 9, 9, 9))
|
|
with pytest.raises((ValueError, OSError)):
|
|
tools.launch_run(planned.token)
|
|
with pytest.raises(ValueError, match='token'):
|
|
tools.launch_run(planned.token)
|
|
assert not calls
|
|
|
|
|
|
def test_concurrent_token_replay_only_one_submission(execution):
|
|
tools, _, calls = execution
|
|
planned = plan(execution)
|
|
|
|
def submit():
|
|
try:
|
|
return tools.launch_run(planned.token).run_id
|
|
except ValueError:
|
|
return None
|
|
|
|
with ThreadPoolExecutor(max_workers=2) as pool:
|
|
values = list(pool.map(lambda _: submit(), range(2)))
|
|
assert values.count(planned.run_id) == 1
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_archive_concurrency_ceiling_applies_across_server_instances(execution):
|
|
tools, model, calls = execution
|
|
first = plan(execution)
|
|
other = ExecutionTools(tools.policy)
|
|
second = other.plan_run(model, 'basic', str(tools.policy.archive_root), RunResources(1000))
|
|
tools.launch_run(first.token)
|
|
with pytest.raises(ValueError, match='concurrency'):
|
|
other.launch_run(second.token)
|
|
assert len(calls) == 1
|
|
assert not (tools.policy.archive_root / second.run_id).exists()
|
|
|
|
|
|
def test_admission_lock_is_independent_and_nonblocking(execution):
|
|
tools, _, calls = execution
|
|
planned = plan(execution)
|
|
with open_directory(tools.policy.archive_root) as fd, locked(fd):
|
|
with pytest.raises(BlockingIOError):
|
|
tools.launch_run(planned.token)
|
|
assert not calls
|
|
|
|
|
|
def test_pending_plans_are_bounded(execution):
|
|
for _ in range(64):
|
|
plan(execution)
|
|
with pytest.raises(ValueError, match='pending'):
|
|
plan(execution)
|
|
|
|
|
|
def test_spawn_failure_is_durable_and_fully_redacted(execution, monkeypatch):
|
|
tools, _, _ = execution
|
|
planned = plan(execution)
|
|
|
|
def fail(*_, **__):
|
|
raise OSError('password=private-token /private/path')
|
|
|
|
monkeypatch.setattr('obliteratus.run_archive._spawn_mcp_worker', fail)
|
|
tools.launch_run(planned.token)
|
|
value = tools.get_run_status(planned.run_id)
|
|
assert value.status == 'failed'
|
|
assert 'private' not in manifest(execution, planned.run_id).read_text()
|
|
assert tools.get_run_result(planned.run_id).run.result_available
|
|
|
|
|
|
def test_reconnect_recovers_vanished_worker(execution, monkeypatch):
|
|
tools, _, _ = execution
|
|
planned = plan(execution)
|
|
tools.launch_run(planned.token)
|
|
monkeypatch.setattr('obliteratus.mcp_worker.worker_matches', lambda *_: False)
|
|
reconnect = ExecutionTools(tools.policy)
|
|
assert reconnect.get_run_status(planned.run_id).status == 'failed'
|
|
value = json.loads(manifest(execution, planned.run_id).read_text())
|
|
assert value['phase'] == 'worker_lost'
|
|
assert reconnect.get_run_result(planned.run_id).metrics == {}
|
|
|
|
|
|
def test_unverifiable_worker_retains_admission_slot_and_cannot_be_signalled(execution, monkeypatch):
|
|
tools, _, calls = execution
|
|
first = plan(execution)
|
|
tools.launch_run(first.token)
|
|
monkeypatch.setattr('obliteratus.mcp_worker.worker_matches', lambda *_: None)
|
|
assert tools.get_run_status(first.run_id).status == 'queued'
|
|
with pytest.raises(ValueError, match='authorized'):
|
|
tools.cancel_run(first.run_id)
|
|
second = plan(execution)
|
|
with pytest.raises(ValueError, match='concurrency'):
|
|
tools.launch_run(second.token)
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_cancel_uses_pidfd_and_is_terminal_idempotent(execution, monkeypatch):
|
|
tools, _, _ = execution
|
|
planned = plan(execution)
|
|
tools.launch_run(planned.token)
|
|
signals = []
|
|
monkeypatch.setattr(os, 'pidfd_open', lambda _: os.open(os.devnull, os.O_RDONLY))
|
|
monkeypatch.setattr(signal, 'pidfd_send_signal', lambda fd, sig: signals.append((fd, sig)))
|
|
assert tools.cancel_run(planned.run_id).status == 'cancelling'
|
|
assert tools.cancel_run(planned.run_id).status == 'cancelling'
|
|
assert [number for _, number in signals] == [0, signal.SIGTERM, 0, signal.SIGTERM]
|
|
monkeypatch.setattr('obliteratus.mcp_worker.worker_matches', lambda *_: False)
|
|
assert tools.get_run_status(planned.run_id).status == 'cancelled'
|
|
assert tools.cancel_run(planned.run_id).status == 'cancelled'
|
|
assert len(signals) == 4 # Two self capability probes and two verified worker signals.
|
|
|
|
|
|
def test_forged_manifest_cannot_signal_unrelated_process(execution, monkeypatch):
|
|
tools, _, _ = execution
|
|
planned = plan(execution)
|
|
tools.launch_run(planned.token)
|
|
value = json.loads(manifest(execution, planned.run_id).read_text())
|
|
value['worker'] = {'pid': os.getpid(), 'uid': os.getuid(), 'start_ticks': 123}
|
|
manifest(execution, planned.run_id).write_text(json.dumps(value))
|
|
monkeypatch.setattr('obliteratus.mcp_worker.worker_matches', worker_matches)
|
|
monkeypatch.setattr(os, 'pidfd_open', lambda *_: pytest.fail('forged process signalled'))
|
|
assert tools.cancel_run(planned.run_id).status == 'failed'
|
|
|
|
|
|
def test_remote_models_and_downloads_have_separate_opt_ins(execution):
|
|
tools, _, _ = execution
|
|
from obliteratus.mcp_catalog import CATALOG
|
|
remote = ModelInput('remote', CATALOG['model'][0][0])
|
|
for remote_allowed, downloads_allowed in [(False, False), (True, False), (False, True)]:
|
|
policy = replace(tools.policy, network_enabled=True, remote_models_enabled=remote_allowed,
|
|
downloads_enabled=downloads_allowed)
|
|
with pytest.raises(ValueError, match='policy'):
|
|
ExecutionTools(policy).plan_run(remote, 'basic', str(policy.archive_root), RunResources(1000))
|
|
policy = replace(tools.policy, network_enabled=True, remote_models_enabled=True, downloads_enabled=True)
|
|
result = ExecutionTools(policy).plan_run(remote, 'basic', str(policy.archive_root), RunResources(1000))
|
|
assert result.network is True
|
|
|
|
|
|
@pytest.mark.parametrize('kwargs', [
|
|
{'downloads_enabled': True}, {'remote_models_enabled': True},
|
|
{'max_concurrent_runs': 17}, {'max_threads': True}, {'plan_ttl_seconds': 301},
|
|
{'execution_enabled': True, 'max_output_bytes': 1024},
|
|
{'max_memory_bytes': 100, 'max_work_bytes': 101},
|
|
{'max_input_bytes': (16 << 20) + 1}, {'max_output_bytes': (16 << 20) + 1},
|
|
])
|
|
def test_startup_policy_execution_bounds(tmp_path, kwargs):
|
|
with pytest.raises(McpPolicyError):
|
|
McpPolicy(archive_root=tmp_path, **kwargs)
|
|
|
|
|
|
def test_execution_registration_is_explicit_and_hints_match(execution):
|
|
pytest.importorskip('mcp')
|
|
from obliteratus.mcp_server import create_server
|
|
policy = execution[0].policy
|
|
|
|
async def scenario():
|
|
default = await create_server(replace(policy, execution_enabled=False)).list_tools()
|
|
enabled = await create_server(policy).list_tools()
|
|
assert {item.name for item in default} == set(READ_TOOLS)
|
|
assert {item.name for item in enabled} == set(READ_TOOLS + EXEC_TOOLS)
|
|
for item in enabled:
|
|
assert item.output_schema is not None
|
|
if item.name in {'launch_run', 'cancel_run'}:
|
|
assert item.annotations.read_only_hint is False
|
|
if item.name in {'get_run_status', 'get_run_result'}:
|
|
assert item.annotations.read_only_hint is False
|
|
server = create_server(policy)
|
|
request = {'model': asdict(execution[1]), 'method': 'basic',
|
|
'output_root': str(policy.archive_root), 'resources': {'parameters': 1000}}
|
|
result = await server.call_tool('plan_run', request)
|
|
assert not result.is_error
|
|
token = result.structured_content['token']
|
|
launched = await server.call_tool('launch_run', {'token': token})
|
|
assert not launched.is_error
|
|
replay = await server.call_tool('launch_run', {'token': token})
|
|
assert replay.is_error and token not in replay.model_dump_json()
|
|
request['resources'] = {'parameters': True}
|
|
assert (await server.call_tool('plan_run', request)).is_error
|
|
request['resources'] = {'parameters': 1000, 'raw_argv': '--unsafe'}
|
|
assert (await server.call_tool('plan_run', request)).is_error
|
|
|
|
asyncio.run(scenario())
|
|
|
|
|
|
def test_sandbox_command_and_environment_enforce_fixed_boundaries(execution, monkeypatch):
|
|
tools, _, _ = execution
|
|
planned = plan(execution)
|
|
spec = tools._plans[planned.token][1]
|
|
command = sandbox_command(spec, 10, 11, [(12, '/mcp-runtime/obliteratus')])
|
|
assert command[:4] == ['/usr/bin/bwrap', '--unshare-all', '--unshare-user', '--die-with-parent']
|
|
assert '--share-net' not in command
|
|
assert command[command.index('--size') + 1] == str(spec.resources.work_bytes)
|
|
assert command[command.index('--bind-fd') + 1:command.index('--bind-fd') + 3] == ['10', '/tmp/mcp-export']
|
|
assert '--ro-bind-fd' in command
|
|
assert '--ro-bind' not in command
|
|
assert '--remount-ro' in command
|
|
with pytest.raises(ValueError):
|
|
sandbox_command(spec, 10, 11, [])
|
|
with pytest.raises(ValueError):
|
|
sandbox_command(spec, 10, 11, [(12, '/')])
|
|
assert '--trust-remote-code' not in build_arguments(spec)
|
|
monkeypatch.setenv('HF_TOKEN', 'private')
|
|
monkeypatch.setenv('OPENAI_API_KEY', 'private')
|
|
monkeypatch.setenv('HTTP_PROXY', 'private')
|
|
environment = _environment(False)
|
|
assert not {'HF_TOKEN', 'OPENAI_API_KEY', 'HTTP_PROXY'}.intersection(environment)
|
|
assert environment['HF_HUB_OFFLINE'] == '1'
|
|
assert '--share-net' in sandbox_command(replace(spec, network=True), 10, None,
|
|
[(12, '/mcp-runtime/obliteratus')])
|
|
|
|
|
|
def test_runtime_mounts_pin_dependencies_without_host_or_ambient_path_mounts(tmp_path, monkeypatch):
|
|
from obliteratus import mcp_worker
|
|
from obliteratus.mcp_worker import _runtime_paths, runtime_mounts
|
|
# Mount construction is independent of the host loader layout, which
|
|
# tests/test_mcp_runtime_layout.py covers.
|
|
monkeypatch.setattr(mcp_worker, 'elf_interpreter', lambda _path: None)
|
|
monkeypatch.setenv('PYTHONPATH', str(tmp_path / 'ambient-secret'))
|
|
paths = _runtime_paths(False)
|
|
assert all(destination not in {'/', '/home', '/root', '/etc', '/usr', '/lib', '/tmp'}
|
|
for _, destination in paths)
|
|
assert all('ambient-secret' not in str(source) for source, _ in paths)
|
|
assert all(not destination.startswith('/etc/') for _, destination in paths)
|
|
assert any(destination == '/mcp-runtime/obliteratus' for _, destination in paths)
|
|
assert any(destination == '/mcp-runtime/purelib' for _, destination in paths)
|
|
source = tmp_path / 'runtime'
|
|
source.mkdir()
|
|
original = source.stat()
|
|
monkeypatch.setattr('obliteratus.mcp_worker._runtime_paths', lambda _: [(source, '/mcp-runtime/deps')])
|
|
with runtime_mounts(False) as mounts:
|
|
source.rename(tmp_path / 'pinned-runtime')
|
|
source.symlink_to(tmp_path / 'ambient-secret')
|
|
fd, destination = mounts[0]
|
|
assert destination == '/mcp-runtime/deps'
|
|
assert (os.fstat(fd).st_dev, os.fstat(fd).st_ino) == (original.st_dev, original.st_ino)
|
|
with pytest.raises(OSError):
|
|
os.fstat(fd)
|
|
with pytest.raises(OSError):
|
|
with runtime_mounts(False):
|
|
pytest.fail('symlink dependency accepted')
|
|
|
|
|
|
def test_supervisor_missing_runtime_fails_durably_without_host_fallback(execution, monkeypatch):
|
|
tools, _, _ = execution
|
|
planned = plan(execution)
|
|
tools.launch_run(planned.token)
|
|
directory = tools.policy.archive_root / planned.run_id
|
|
monkeypatch.setattr(signal, 'signal', lambda *_: None)
|
|
|
|
def unavailable(_):
|
|
raise OSError('private-runtime-path secret')
|
|
|
|
monkeypatch.setattr('obliteratus.mcp_worker._runtime_paths', unavailable)
|
|
monkeypatch.setattr('obliteratus.mcp_worker.subprocess.Popen',
|
|
lambda *_a, **_kw: pytest.fail('unsandboxed fallback'))
|
|
with open_directory(directory) as fd:
|
|
assert supervise(fd, planned.run_id, None) == 1
|
|
value = json.loads((directory / 'manifest.json').read_text())
|
|
assert value['status'] == 'failed' and value['phase'] == 'pipeline_failed'
|
|
assert 'private-runtime' not in json.dumps(value)
|
|
|
|
|
|
def test_checkpoint_copy_is_bounded_hashed_and_no_follow(tmp_path):
|
|
source, target = tmp_path / 'source', tmp_path / 'target'
|
|
source.mkdir()
|
|
target.mkdir()
|
|
(source / 'model.safetensors').write_bytes(b'1234')
|
|
with open_directory(source) as src, open_directory(target) as dst:
|
|
records = copy_checkpoint(src, dst, 4)
|
|
assert records[0]['sha256'] == hashlib.sha256(b'1234').hexdigest()
|
|
assert (target / 'model.safetensors').read_bytes() == b'1234'
|
|
other = tmp_path / 'other'
|
|
other.mkdir()
|
|
with open_directory(source) as src, open_directory(other) as dst:
|
|
with pytest.raises(ValueError, match='byte ceiling'):
|
|
copy_checkpoint(src, dst, 3)
|
|
(source / 'link').symlink_to('/etc/passwd')
|
|
third = tmp_path / 'third'
|
|
third.mkdir()
|
|
with open_directory(source) as src, open_directory(third) as dst:
|
|
with pytest.raises(ValueError):
|
|
copy_checkpoint(src, dst, 100)
|
|
|
|
|
|
@pytest.mark.parametrize('outcome', ['success', 'failure', 'timeout'])
|
|
def test_supervisor_terminal_paths_and_provenance(execution, monkeypatch, outcome):
|
|
from obliteratus import mcp_worker
|
|
monkeypatch.setattr(mcp_worker, 'elf_interpreter', lambda _path: None)
|
|
monkeypatch.setattr(signal, 'signal', lambda *_: None)
|
|
tools, _, _ = execution
|
|
planned = plan(execution, runtime_seconds=1)
|
|
tools.launch_run(planned.token)
|
|
directory = tools.policy.archive_root / planned.run_id
|
|
from obliteratus.checkpoint_provenance import ArtifactIdentity, ToolIdentity, build_provenance
|
|
provenance = build_provenance(
|
|
sources=(ArtifactIdentity('generated', 'synthetic', 'v1', 'sha256:' + 'a' * 64),),
|
|
converter=ToolIdentity('test', '1', 'c' * 40), obliteratus_commit='c' * 40,
|
|
configuration_digest=None, tokenizer=None, base_model=None, command=('synthetic',),
|
|
environment={'python': 'test', 'platform': 'cpu', 'packages': {}}, source_topology={},
|
|
lineage=(), input_digests=('sha256:' + 'a' * 64,), output_digests=('sha256:' + 'b' * 64,),
|
|
transformations=(), observed_scopes=('model_weights',), lost_state=())
|
|
(directory / 'checkpoint' / 'checkpoint-provenance.json').write_text(provenance.to_json())
|
|
checkpoint = directory / 'checkpoint'
|
|
(checkpoint / 'config.json').write_text('{}')
|
|
(checkpoint / 'model.safetensors').write_bytes(b'synthetic')
|
|
(checkpoint / 'abliteration_metadata.json').write_text(json.dumps({'quality_metrics': {'accuracy': .75}}))
|
|
inventory = [{'path': path.name, 'bytes': path.stat().st_size,
|
|
'sha256': hashlib.sha256(path.read_bytes()).hexdigest()} for path in checkpoint.iterdir()]
|
|
(checkpoint / 'mcp-artifact-inventory.json').write_text(json.dumps({'artifacts': inventory}))
|
|
killed = []
|
|
|
|
class Child:
|
|
pid = 900002
|
|
returncode = 0 if outcome == 'success' else 1 if outcome == 'failure' else None
|
|
|
|
def poll(self):
|
|
return self.returncode
|
|
|
|
def wait(self, timeout=None):
|
|
self.returncode = 1
|
|
return 1
|
|
|
|
monkeypatch.setattr('obliteratus.mcp_worker.subprocess.Popen', lambda *_a, **_kw: Child())
|
|
monkeypatch.setattr(os, 'killpg', lambda *args: killed.append(args))
|
|
ticks = iter([0., 2.])
|
|
if outcome == 'timeout':
|
|
monkeypatch.setattr('obliteratus.mcp_worker.time', SimpleNamespace(monotonic=lambda: next(ticks), sleep=lambda _: None))
|
|
with open_directory(directory) as fd:
|
|
code = supervise(fd, planned.run_id, None)
|
|
value = json.loads((directory / 'manifest.json').read_text())
|
|
assert code == (0 if outcome == 'success' else 1)
|
|
assert value['status'] == ('succeeded' if outcome == 'success' else 'failed')
|
|
if outcome == 'success':
|
|
retained = (directory / 'checkpoint-provenance.json').read_bytes()
|
|
assert value['artifact_id'] == provenance.artifact_id
|
|
assert value['checkpoint_provenance']['sha256'] == 'sha256:' + hashlib.sha256(retained).hexdigest()
|
|
assert value['result']['metrics'] == {'accuracy': .75}
|
|
if outcome == 'timeout':
|
|
assert killed and value['phase'] == 'runtime_ceiling'
|
|
|
|
|
|
def test_guarded_cancel_rejects_non_mcp_runs(execution):
|
|
tools, _, _ = execution
|
|
run_id = 'run-' + 'd' * 32
|
|
directory = tools.policy.archive_root / run_id
|
|
directory.mkdir()
|
|
with open_directory(directory) as fd:
|
|
atomic_json(fd, 'manifest.json', {'run_id': run_id, 'status': 'running'})
|
|
with pytest.raises(ValueError, match='not created'):
|
|
cancel_guarded(tools.policy.archive_root, run_id)
|
|
assert recover(tools.policy.archive_root, run_id)['status'] == 'running'
|
|
|
|
|
|
def test_process_identity_handles_spaces_and_parentheses_in_comm(monkeypatch):
|
|
from obliteratus.run_archive import _process_start_ticks
|
|
value = '123 (worker name) with spaces) S ' + ' '.join(['0'] * 18 + ['987'])
|
|
monkeypatch.setattr(Path, 'read_text', lambda *_a, **_kw: value)
|
|
assert _process_start_ticks(123) == 987
|
|
|
|
|
|
def test_actual_cli_provenance_without_optional_canonical_record_is_retained(execution):
|
|
tools, _, _ = execution
|
|
planned = plan(execution)
|
|
spec = tools._plans[planned.token][1]
|
|
directory = tools.policy.archive_root / planned.run_id
|
|
checkpoint = directory / 'checkpoint'
|
|
checkpoint.mkdir(parents=True)
|
|
(checkpoint / 'config.json').write_text('{}')
|
|
(checkpoint / 'model.safetensors').write_bytes(b'synthetic')
|
|
(checkpoint / 'abliteration_metadata.json').write_text(json.dumps({
|
|
'provenance': {'obliteratus_git_commit': None, 'private_key': 'sensitive-value'},
|
|
'quality_metrics': {'accuracy': .5, 'secret': 'sensitive-value'},
|
|
}))
|
|
inventory = [{'path': path.name, 'bytes': path.stat().st_size,
|
|
'sha256': hashlib.sha256(path.read_bytes()).hexdigest()} for path in checkpoint.iterdir()]
|
|
(checkpoint / 'mcp-artifact-inventory.json').write_text(json.dumps({'artifacts': inventory}))
|
|
with open_directory(directory) as fd, open_directory(checkpoint) as output:
|
|
evidence, metrics = retain_evidence(fd, output, spec)
|
|
assert 'artifact_id' not in evidence and 'checkpoint_provenance' not in evidence
|
|
assert metrics == {'accuracy': .5}
|
|
record = json.loads((directory / 'run-provenance.json').read_text())
|
|
assert record['pipeline_provenance']['obliteratus_git_commit'] is None
|
|
assert 'sensitive-value' not in json.dumps(record)
|
|
assert record['artifact_inventory_sha256'] == hashlib.sha256(
|
|
(checkpoint / 'mcp-artifact-inventory.json').read_bytes()).hexdigest()
|
|
|
|
|
|
def test_model_input_size_is_bounded_independently_of_declared_parameter_count(execution):
|
|
tools, model, _ = execution
|
|
(Path(model.identifier) / 'extra.safetensors').write_bytes(b'x' * 8192)
|
|
with pytest.raises(ValueError, match='input byte ceiling'):
|
|
tools.plan_run(model, 'basic', str(tools.policy.archive_root),
|
|
RunResources(1000, memory_bytes=4096, work_bytes=4096))
|
|
|
|
|
|
def test_job_applies_os_limits_and_copies_only_bounded_outputs(tmp_path, monkeypatch):
|
|
import resource
|
|
from obliteratus.mcp_worker import job
|
|
work, export = tmp_path / 'work', tmp_path / 'export'
|
|
work.mkdir()
|
|
export.mkdir()
|
|
monkeypatch.setattr('obliteratus.mcp_worker.JOB_ROOT', work)
|
|
monkeypatch.setattr('obliteratus.mcp_worker.EXPORT_ROOT', export)
|
|
limits = []
|
|
monkeypatch.setattr(resource, 'setrlimit', lambda *args: limits.append(args))
|
|
for name in ('OMP_NUM_THREADS', 'MKL_NUM_THREADS', 'OPENBLAS_NUM_THREADS', 'NUMEXPR_NUM_THREADS'):
|
|
monkeypatch.setenv(name, '1')
|
|
|
|
def synthetic(command, **kwargs):
|
|
assert command[-2:] == ['--output-dir', str(work / 'checkpoint')]
|
|
assert kwargs['stdout'] == kwargs['stderr'] == subprocess.DEVNULL
|
|
(work / 'checkpoint').mkdir()
|
|
(work / 'checkpoint' / 'model.safetensors').write_bytes(b'weights')
|
|
return SimpleNamespace(returncode=0)
|
|
|
|
monkeypatch.setattr('obliteratus.mcp_worker.subprocess.run', synthetic)
|
|
resources = RunResources(1000)
|
|
assert job(json.dumps(asdict(resources)), ['synthetic/model', '--device', 'cpu']) == 0
|
|
assert (export / 'model.safetensors').read_bytes() == b'weights'
|
|
assert (export / 'mcp-artifact-inventory.json').is_file()
|
|
assert (resource.RLIMIT_AS, (resources.memory_bytes, resources.memory_bytes)) in limits
|
|
assert (resource.RLIMIT_FSIZE, (resources.work_bytes, resources.work_bytes)) in limits
|
|
assert (resource.RLIMIT_CORE, (0, 0)) in limits
|
|
|
|
|
|
@pytest.mark.integration
|
|
def test_bubblewrap_real_synthetic_filesystem_network_and_quota_boundary(tmp_path, monkeypatch):
|
|
"""No model runtime. A skip is explicitly missing sandbox runtime evidence."""
|
|
import socket
|
|
from obliteratus.mcp_execution import BWRAP
|
|
if not BWRAP.is_file():
|
|
pytest.skip('Bubblewrap unavailable: sandbox runtime verification outstanding')
|
|
from obliteratus.mcp_worker import runtime_layout_supported
|
|
supported, reason = runtime_layout_supported()
|
|
if not supported:
|
|
pytest.skip(f'Sandbox runtime layout unsupported: {reason}')
|
|
archive, model, export = tmp_path / 'archive', tmp_path / 'model', tmp_path / 'export'
|
|
for directory in (archive, model, export):
|
|
directory.mkdir(mode=0o700)
|
|
(model / 'config.json').write_text('{}')
|
|
(model / 'model.safetensors').write_bytes(b'synthetic')
|
|
sentinel = tmp_path / 'outside-sentinel'
|
|
sentinel.write_text('unchanged')
|
|
archive_secret = archive / 'host-archive-secret'
|
|
archive_secret.write_text('not available to sandbox')
|
|
home_secret = tmp_path / 'ambient-home' / '.credentials'
|
|
home_secret.parent.mkdir()
|
|
home_secret.write_text('not available to sandbox')
|
|
monkeypatch.setenv('HOME', str(home_secret.parent))
|
|
tools = ExecutionTools(McpPolicy(archive_root=archive, allowed_roots=(model,), execution_enabled=True))
|
|
planned = tools.plan_run(ModelInput('local', str(model)), 'basic', str(archive),
|
|
RunResources(1000, memory_bytes=1 << 30, work_bytes=1 << 20))
|
|
spec = tools._plans[planned.token][1]
|
|
from obliteratus.mcp_worker import runtime_mounts
|
|
with (socket.socket() as listener, open_directory(export) as dst, open_directory(model) as src,
|
|
runtime_mounts(False) as mounts):
|
|
listener.bind(('127.0.0.1', 0))
|
|
listener.listen()
|
|
command = sandbox_command(spec, dst, src, mounts)
|
|
command = command[:command.index('--') + 1]
|
|
script = '''
|
|
import os, socket, sys
|
|
from pathlib import Path
|
|
assert os.statvfs('/').f_flag & os.ST_RDONLY
|
|
assert os.environ['HOME'] == '/tmp/mcp-work/home'
|
|
assert Path('/tmp/mcp-model/config.json').read_text() == '{}'
|
|
try:
|
|
Path('/tmp/mcp-model/config.json').write_text('escaped')
|
|
except OSError:
|
|
pass
|
|
else:
|
|
raise AssertionError('model writable')
|
|
for name in (sys.argv[1], sys.argv[3], sys.argv[4]):
|
|
try:
|
|
Path(name).read_bytes()
|
|
except OSError:
|
|
pass
|
|
else:
|
|
raise AssertionError('host canary readable: ' + name)
|
|
for fd in (int(value) for value in sys.argv[5].split(',')):
|
|
try:
|
|
os.fstat(fd)
|
|
except OSError:
|
|
pass
|
|
else:
|
|
raise AssertionError('host mount source descriptor leaked')
|
|
try:
|
|
Path(sys.argv[1]).write_text('escaped')
|
|
except OSError:
|
|
pass
|
|
else:
|
|
raise AssertionError('host root writable')
|
|
sock = socket.socket()
|
|
sock.settimeout(1)
|
|
try:
|
|
sock.connect(('127.0.0.1', int(sys.argv[2])))
|
|
except OSError:
|
|
pass
|
|
else:
|
|
raise AssertionError('host network reachable')
|
|
try:
|
|
Path('/tmp/mcp-work/overflow').write_bytes(b'x' * (2 << 20))
|
|
except OSError:
|
|
pass
|
|
else:
|
|
raise AssertionError('tmpfs quota absent')
|
|
Path('/tmp/mcp-export/synthetic-result').write_text('bounded')
|
|
'''
|
|
result = subprocess.run(command + [str(Path(os.sys.executable).resolve()), '-c', script, str(sentinel),
|
|
str(listener.getsockname()[1]), str(archive_secret), str(home_secret),
|
|
','.join(str(fd) for fd in (src, dst) + tuple(fd for fd, _ in mounts))],
|
|
pass_fds=(dst, src) + tuple(fd for fd, _ in mounts),
|
|
env=_environment(False), capture_output=True, text=True, timeout=20)
|
|
if result.returncode and any(message in result.stderr.lower() for message in (
|
|
'operation not permitted', 'no permissions to create', 'creating new namespace failed',
|
|
)):
|
|
pytest.skip('Host denies Bubblewrap namespaces: sandbox runtime verification outstanding')
|
|
assert result.returncode == 0, result.stderr
|
|
assert sentinel.read_text() == 'unchanged'
|
|
assert (export / 'synthetic-result').read_text() == 'bounded'
|