feat: snapshot Linux-first RMM MVP and pause handover
Rust development slice / linux (push) Failing after 1m10s

This commit is contained in:
BuTzZ
2026-11-12 22:06:25 +01:00
commit 6c3203d725
194 changed files with 49234 additions and 0 deletions
@@ -0,0 +1,10 @@
VERSION 3
DPkg::Pre-Install-Pkgs::=/usr/lib/rmm-apt-transaction/hook
DPkg::Tools::Options::/usr/lib/rmm-apt-transaction/hook::Version=3
DPkg::Tools::Options::/usr/lib/rmm-apt-transaction/hook::InfoFD=0
Dir::Bin::dpkg=/usr/bin/dpkg
Dir::State::status=/var/lib/dpkg/status
DPkg::ConfigurePending=false
rmm-mvp-test - - none < 1.1 all none /var/lib/rmm-apt-transaction/archives/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.deb
rmm-mvp-test - - none < 1.1 all none **CONFIGURE**
@@ -0,0 +1 @@
{"apt_config_sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","binaries":{"/usr/bin/dpkg":{"sha256":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}},"schema_version":1,"versions":{"dpkg":"Debian dpkg version 1.22"}}
+308
View File
@@ -0,0 +1,308 @@
import hashlib
import importlib.util
import io
import json
import os
from pathlib import Path
import stat
import subprocess
import sys
import tarfile
import tempfile
from types import SimpleNamespace
import unittest
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[2]
SPEC = importlib.util.spec_from_file_location('apt_transaction', ROOT / 'tools/apt-transaction/apt_transaction.py')
apt = importlib.util.module_from_spec(SPEC)
sys.modules[SPEC.name] = apt
SPEC.loader.exec_module(apt)
def transaction(old=None):
sha = 'a' * 64
return {'schema_version': 1, 'recipe_id': 'rmm-mvp-test', 'recipe_version': '1',
'platform': 'ubuntu-24.04-amd64', 'resolver_config_version': apt.RESOLVER,
'no_reboot': True, 'status_fingerprint': 'b' * 64, 'baseline_sha256': 'c' * 64,
'packages': [{'name': 'rmm-mvp-test', 'architecture': 'all', 'old_version': old,
'new_version': '1.1', 'origin': 'rmm-test', 'sha256': sha, 'size': 100,
'archive_path': str(apt.BASE / 'archives' / (sha + '.deb'))}]}
def protocol(old=None):
old_fields = old + ' all none' if old else '- - none'
action = 'rmm-mvp-test ' + old_fields + ' < 1.1 all none '
return ('VERSION 3\nDPkg::Pre-Install-Pkgs::=' + apt.HOOK + '\n'
'DPkg::Tools::Options::' + apt.HOOK + '::Version=3\n'
'DPkg::Tools::Options::' + apt.HOOK + '::InfoFD=0\n'
'Dir::Bin::dpkg=/usr/bin/dpkg\nDir::State::status=/var/lib/dpkg/status\n'
'DPkg::ConfigurePending=false\n\n' + action + transaction()['packages'][0]['archive_path'] +
'\n' + action + '**CONFIGURE**\n').encode('ascii')
class ProtocolTests(unittest.TestCase):
def test_upstream_v3_new_install_and_update(self):
for old in (None, '1.0'):
tx = apt.ResolvedAptTransaction.parse(transaction(old))
config, actions = apt.parse_protocol3(protocol(old))
apt.check_hook_config(config)
apt.validate_actions(tx, actions)
def test_fixture_file(self):
data = (Path(__file__).parent / 'fixtures/install-v3.txt').read_bytes()
config, actions = apt.parse_protocol3(data)
apt.check_hook_config(config)
apt.validate_actions(apt.ResolvedAptTransaction.parse(transaction()), actions)
def test_not_five_field_or_fantasy_hash_protocol(self):
for data in (b'VERSION 2\n\npkg - < 1 /file\n',
b'VERSION 3\n\npkg - < 1 /file sha256=aaa\n'):
with self.assertRaises(apt.Veto):
apt.parse_protocol3(data)
def test_strict_malformed_and_bounds(self):
original = protocol()
for data in (b'', original[:-1], original.replace(b'VERSION 3', b'VERSION 4'),
original.replace(b'all none', b'all nonsense'),
original.replace(b'\n\n', b'\n'), original + b'\n',
original.replace(b'rmm-mvp-test ', b'rmm-mvp-test '),
original.replace(b'InfoFD=0', b'InfoFD=%GG'),
original.replace(b'InfoFD=0', b'InfoFD=0\x00'), b'x' * (apt.MAX_INPUT + 1)):
with self.subTest(data=data[:80]), self.assertRaises(apt.Veto):
apt.parse_protocol3(data)
def test_config_escaping_repeated_list_and_invalid_hook_binding(self):
config, _ = apt.parse_protocol3(protocol().replace(b'VERSION 3\n',
b'VERSION 3\nExample::=a%0Ab%25c\nExample::=two\n'))
self.assertIn(('Example::', 'a\nb%c'), config)
self.assertIn(('Example::', 'two'), config)
for injected in (b'DPkg::Pre-Invoke::=/bin/true\n',
('DPkg::Pre-Install-Pkgs::=' + apt.HOOK + '\n').encode()):
config, _ = apt.parse_protocol3(protocol().replace(b'\n\n', b'\n' + injected + b'\n'))
with self.assertRaises(apt.Veto):
apt.check_hook_config(config)
def test_entire_action_set_dependency_change_remove_configure(self):
tx = apt.ResolvedAptTransaction.parse(transaction())
original = protocol()
for data in (original.replace(b'**CONFIGURE**', b'**REMOVE**'),
original.replace(b'rmm-mvp-test -', b'other-dependency -'),
original.replace(b'1.1 all', b'1.2 all'),
original.replace(b'1.1 all', b'1.1 amd64'),
original.replace(b' < ', b' > '),
original.replace(b'a' * 64 + b'.deb', b'b' * 64 + b'.deb'),
original + original.split(b'\n')[-2] + b'\n',
b'\n'.join(original.split(b'\n')[:-2]) + b'\n'):
with self.subTest(data=data[-100:]), self.assertRaises(apt.Veto):
_, actions = apt.parse_protocol3(data)
apt.validate_actions(tx, actions)
def test_downgrade_and_version_order(self):
for old in ('1.1', '1.2', '1:1.0'):
with self.assertRaises(apt.Veto):
apt.ResolvedAptTransaction.parse(transaction(old))
apt.ResolvedAptTransaction.parse(transaction('1.1~rc1'))
def test_schema_no_removal_unknown_fields_and_no_reboot(self):
for key, value in (('no_reboot', False), ('packages', []),
('schema_version', True), ('platform', 'debian'),
('resolver_config_version', 'anything')):
obj = transaction()
obj[key] = value
with self.assertRaises(apt.Veto):
apt.ResolvedAptTransaction.parse(obj)
for key, value in (('new_version', None), ('name', 'linux-image-test'),
('archive_path', '/tmp/evil.deb'), ('origin', 'https://llm.invalid/x'),
('sha256', 'z' * 64), ('size', True), ('remove', True)):
obj = transaction()
obj['packages'][0][key] = value
with self.assertRaises(apt.Veto):
apt.ResolvedAptTransaction.parse(obj)
obj = transaction()
obj['packages'] *= 2
with self.assertRaises(apt.Veto):
apt.ResolvedAptTransaction.parse(obj)
def test_immutable_and_duplicate_json(self):
tx = apt.ResolvedAptTransaction.parse(transaction())
with self.assertRaises(AttributeError):
tx.packages[0].new_version = '2'
with self.assertRaises(apt.Veto):
apt.json_bytes(b'{"a":1,"a":2}')
class PreconditionsTests(unittest.TestCase):
def test_both_deadlines_and_boot_bound(self):
context = apt.VerifiedHelperContext.parse({
'transaction_sha256': 'a' * 64, 'effect_armed_ledger_id': 'ledger-1',
'grant_id': 'grant-1', 'not_before_ms': 1000, 'start_before_ms': 61000,
'boot_id': 'boot-1', 'monotonic_start_before_ns': 10000,
'schema_version': 2, 'job_payload_sha256': 'd' * 64, 'action': 'apt-execute',
'execution_profile': 'dev-test', 'job_id': 'job-1', 'attempt_id': 'attempt-1',
'authorization_id': 'authorization-1', 'plan_sha256': 'e' * 64})
context.check_deadline(1000, 1, 'boot-1')
for now, mono, boot in ((999, 1, 'boot-1'), (61000, 1, 'boot-1'),
(2000, 10000, 'boot-1'), (2000, 1, 'boot-2')):
with self.assertRaises(apt.Veto):
context.check_deadline(now, mono, boot)
def test_fingerprint_baseline_and_old_version(self):
baseline = {'rmm-mvp-test:all': {'version': '1.0', 'status': 'install ok installed'}}
obj = transaction('1.0')
obj['baseline_sha256'] = apt.digest(apt.canonical(baseline))
tx = apt.ResolvedAptTransaction.parse(obj)
with patch.object(apt, 'status_fingerprint', return_value=tx.status_fingerprint), \
patch.object(apt, 'installed_snapshot', return_value=baseline):
apt.check_preconditions(tx, baseline)
with self.assertRaises(apt.Veto):
apt.check_preconditions(tx, {})
with patch.object(apt, 'status_fingerprint', return_value='d' * 64):
with self.assertRaisesRegex(apt.Veto, 'STATUS_FINGERPRINT'):
apt.check_preconditions(tx, baseline)
with patch.object(apt, 'status_fingerprint', return_value=tx.status_fingerprint), \
patch.object(apt, 'installed_snapshot', return_value={}):
with self.assertRaisesRegex(apt.Veto, 'STATUS_BASELINE_CHANGED'):
apt.check_preconditions(tx, baseline)
def test_durable_once_double_hook(self):
with tempfile.TemporaryDirectory() as temp, patch.object(apt, 'secure_path'):
marker = Path(temp) / 'armed.json'
with patch.object(apt.os, 'fsync', wraps=os.fsync) as sync:
apt.durable_once(marker, {'ledger': 'one'})
self.assertEqual(sync.call_count, 2)
with self.assertRaisesRegex(apt.Veto, 'ALREADY_HANDED_OFF'):
apt.durable_once(marker, {'ledger': 'two'})
self.assertEqual(json.loads(marker.read_bytes()), {'ledger': 'one'})
def test_archive_root_mode_size_links(self):
valid = dict(st_uid=0, st_mode=stat.S_IFREG | 0o600, st_size=100, st_nlink=1)
apt.archive_metadata(SimpleNamespace(**valid), 100)
for key, value in (('st_uid', 1000), ('st_mode', stat.S_IFREG | 0o620),
('st_mode', stat.S_IFLNK | 0o777), ('st_size', 99), ('st_nlink', 2)):
with self.assertRaises(apt.Veto):
apt.archive_metadata(SimpleNamespace(**{**valid, key: value}), 100)
def test_symlink_and_user_owned_path_rejected(self):
with tempfile.TemporaryDirectory() as temp:
path = Path(temp) / 'file'
path.write_bytes(b'file')
link = Path(temp) / 'link'
link.symlink_to(path)
with self.assertRaises(apt.Veto):
apt.secure_path(link)
if os.getuid() != 0:
with self.assertRaises(apt.Veto):
apt.secure_path(path)
def test_postcondition_checks_entire_snapshot(self):
tx = apt.ResolvedAptTransaction.parse(transaction())
expected = {'rmm-mvp-test:all': {'version': '1.1', 'status': 'install ok installed'}}
with patch.object(apt, 'installed_snapshot', return_value=expected), \
patch.object(apt, 'run_read', return_value=b''):
self.assertEqual(apt.verify_after(tx, {})['packages'][0]['version'], '1.1')
for snapshot in ({}, {**expected, 'unexpected:all': {'version': '1', 'status': 'install ok installed'}}):
with patch.object(apt, 'installed_snapshot', return_value=snapshot):
with self.assertRaises(apt.Veto):
apt.verify_after(tx, {})
def test_unprivileged_execute_and_sandbox_fail_closed(self):
if os.geteuid() != 0:
self.assertEqual(apt.execute({})['code'], 'ROOT_REQUIRED')
with self.assertRaises(apt.Veto):
apt.inspect_sandbox()
def test_previous_launch_or_handoff_is_unknown_never_relaunched(self):
for marker in ('launch.json', 'hook-armed.json'):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
(base / marker).write_text('{}')
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', base), \
patch.object(apt, 'secure_path'), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'load_active', return_value=(None, None, {})), \
patch.object(apt.subprocess, 'Popen') as spawn:
result = apt.execute({'transaction_sha256': 'a' * 64,
'effect_armed_ledger_id': 'ledger-1'})
self.assertEqual(result['effect_state'], 'unknown')
self.assertEqual(result['code'], 'PRIOR_ATTEMPT_UNKNOWN')
spawn.assert_not_called()
def test_unprivileged_seccomp_load_in_disposable_child(self):
# Installs a filter only on this short-lived child, never the test
# runner/host. No reboot, mount or other effectful syscall is attempted.
code = '''import importlib.util, pathlib, sys
sys.dont_write_bytecode = True
s = importlib.util.spec_from_file_location('apt_transaction', sys.argv[1])
m = importlib.util.module_from_spec(s); sys.modules[s.name] = m; s.loader.exec_module(m)
m.seccomp_filter()
status = pathlib.Path('/proc/self/status').read_text()
assert 'NoNewPrivs:\\t1' in status and 'Seccomp:\\t2' in status
print('FILTER_LOADED')
'''
output = subprocess.check_output(['/usr/bin/python3', '-I', '-c', code,
str(ROOT / 'tools/apt-transaction/apt_transaction.py')])
self.assertEqual(output, b'FILTER_LOADED\n')
def test_exact_argv_config_and_fixed_hook(self):
tx = apt.ResolvedAptTransaction.parse(transaction())
argv = apt.apt_argv(tx)
self.assertIn('--no-download', argv)
self.assertIn('--no-remove', argv)
self.assertNotIn('update', argv)
self.assertEqual(argv[-1], tx.packages[0].archive_path)
self.assertEqual((ROOT / 'tools/apt-transaction/hook').read_bytes(), apt.hook_script())
self.assertIn(b'Dir::Etc::parts', apt.apt_config())
class FixtureTests(unittest.TestCase):
def test_actual_archive_hash_and_control_mismatch(self):
with tempfile.TemporaryDirectory() as temp:
records = json.loads(subprocess.check_output([
sys.executable, str(ROOT / 'tools/apt-transaction/build_fixture.py'), temp]))
record = records[0]
package = apt.PackageArchive('rmm-mvp-test', 'all', None, '1.0', 'rmm-test',
record['sha256'], record['size'], record['local_build_path'])
# Only root ownership is mocked for this unprivileged byte/control
# test. Production secure_path and archive_metadata are never relaxed.
with patch.object(apt, 'secure_path', side_effect=lambda path: Path(path)), \
patch.object(apt, 'archive_metadata'):
apt.verify_archive(package)
wrong_hash = apt.PackageArchive(**{**package.__dict__, 'sha256': '0' * 64})
with self.assertRaisesRegex(apt.Veto, 'ARCHIVE_HASH'):
apt.verify_archive(wrong_hash)
wrong_version = apt.PackageArchive(**{**package.__dict__, 'new_version': '2.0'})
with self.assertRaisesRegex(apt.Veto, 'DEB_CONTROL_OR_CONFLICT'):
apt.verify_archive(wrong_version)
def test_build_deb_versions_hash_control_payload_without_install(self):
with tempfile.TemporaryDirectory() as temp:
records = json.loads(subprocess.check_output([
sys.executable, str(ROOT / 'tools/apt-transaction/build_fixture.py'), temp]))
self.assertEqual([r['new_version'] for r in records], ['1.0', '1.1'])
for record in records:
path = Path(record['local_build_path'])
self.assertEqual(hashlib.sha256(path.read_bytes()).hexdigest(), record['sha256'])
self.assertEqual(path.stat().st_size, record['size'])
control = apt.run_read(['/usr/bin/dpkg-deb', '--show',
'--showformat=${Package}\n${Version}\n${Architecture}\n', str(path)])
self.assertEqual(control.decode().splitlines(), ['rmm-mvp-test', record['new_version'], 'all'])
payload = apt.run_read(['/usr/bin/dpkg-deb', '--fsys-tarfile', str(path)])
apt.validate_payload(payload)
with tarfile.open(fileobj=io.BytesIO(payload)) as archive:
self.assertEqual(archive.extractfile('./usr/share/rmm-mvp-test/version').read(),
(record['new_version'] + '\n').encode())
def test_protected_payload_paths_even_in_innocent_package(self):
for name in ('./boot/vmlinuz', './etc/cron.d/reboot', './usr/lib/systemd/system/a.service',
'../escape', '/absolute', './var/lib/rmm-apt-transaction/active/context.json'):
output = io.BytesIO()
with tarfile.open(fileobj=output, mode='w') as archive:
entry = tarfile.TarInfo(name)
entry.size = 1
archive.addfile(entry, io.BytesIO(b'x'))
with self.assertRaises(apt.Veto):
apt.validate_payload(output.getvalue())
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,87 @@
"""Regression: slow durable handoff must not extend a native startgrant."""
import io
import os
from pathlib import Path
import sys
import tempfile
from contextlib import ExitStack
from types import SimpleNamespace
from unittest import TestCase
from unittest.mock import Mock, patch
from test_guard import apt, protocol, transaction
from test_native_acceptance import context
class HookDeadlineTests(TestCase):
def test_deadline_after_fsync_vetoes_keeps_marker_and_prevents_reuse(self):
# Test both clocks together and each independently: neither clock can
# compensate for expiry of the other at the post-fsync gate.
for expired in ('both', 'utc', 'monotonic'):
with self.subTest(expired=expired), tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
active = Path(temp)
ctx = context('apt-execute')
tx = apt.ResolvedAptTransaction.parse(transaction())
clock = {'utc_ns': 2000 * 1000000, 'monotonic_ns': 100}
ready = apt.canonical({'transaction_sha256': ctx.transaction_sha256,
'sandbox_version': 1})
original_fsync = os.fsync
sync_count = 0
def slow_fsync(fd):
nonlocal sync_count
original_fsync(fd)
sync_count += 1
if sync_count == 2: # directory fsync finished successfully
if expired in ('both', 'utc'):
clock['utc_ns'] = ctx.start_before_ms * 1000000
if expired in ('both', 'monotonic'):
clock['monotonic_ns'] = ctx.monotonic_start_before_ns
dpkg_start = Mock()
def apt_handoff():
# Models the CLI's Veto -> nonzero contract and APT's
# mandatory zero-only gate. No real APT/dpkg is started.
try:
code = apt.main()
except apt.Veto as error:
return 100, str(error)
if code == 0:
dpkg_start()
return code, None
with ExitStack() as stack:
for replacement in (
patch.object(apt, 'ACTIVE', active),
patch.object(apt.os, 'geteuid', return_value=0),
patch.dict(os.environ, {'APT_HOOK_INFO_FD': '0', 'DPKG_FRONTEND_LOCKED': 'true'}),
patch.object(apt, 'load_active', return_value=(tx, ctx, {})),
patch.object(apt, 'load_signed_action', return_value={}),
patch.object(apt, 'runtime_identity', return_value={}),
patch.object(apt, 'check_native_gate'), patch.object(apt, 'inspect_sandbox'),
patch.object(apt, 'sealed_read', return_value=ready),
patch.object(apt, 'check_preconditions'), patch.object(apt, 'verify_archive'),
patch.object(apt, 'secure_path'),
patch.object(Path, 'read_text', return_value=ctx.boot_id),
patch.object(apt.time, 'time_ns', side_effect=lambda: clock['utc_ns']),
patch.object(apt.time, 'monotonic_ns', side_effect=lambda: clock['monotonic_ns']),
patch.object(apt.os, 'fsync', side_effect=slow_fsync),
patch.object(sys, 'argv', ['apt_transaction.py', 'hook']),
patch.object(sys, 'stdin', SimpleNamespace(buffer=io.BytesIO(protocol()))),
):
stack.enter_context(replacement)
self.assertEqual(apt_handoff(), (100, 'START_DEADLINE'))
self.assertEqual(sync_count, 2)
marker = active / 'hook-armed.json'
self.assertTrue(marker.is_file())
marker_bytes = marker.read_bytes()
dpkg_start.assert_not_called()
# Even a subsequently valid clock cannot reuse the durable
# marker: no reset/delete/retry shortcut exists.
clock.update(utc_ns=2000 * 1000000, monotonic_ns=100)
with patch.object(sys, 'stdin', SimpleNamespace(buffer=io.BytesIO(protocol()))):
self.assertEqual(apt_handoff(), (100, 'ALREADY_HANDED_OFF'))
self.assertEqual(marker.read_bytes(), marker_bytes)
self.assertEqual(sync_count, 2)
dpkg_start.assert_not_called()
@@ -0,0 +1,387 @@
"""No host package mutation: native root/child execution is mocked explicitly."""
import copy
import io
import json
import os
from pathlib import Path
import stat
import subprocess
import sys
import tarfile
import tempfile
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt, transaction, ROOT
def fixture_transaction(recipe='rmm-apt-fixture-install-1.0'):
old, new = apt.FIXTURE_RECIPES[recipe]
obj = transaction(old)
obj['recipe_id'] = recipe
obj['packages'][0].update(new_version=new, sha256=apt.FIXTURE_SHA256[new],
size=len(apt.fixture_archive(new)),
archive_path=str(apt.BASE / 'archives' / (apt.FIXTURE_SHA256[new] + '.deb')))
return obj
def context(action='apt-native-acceptance', profile='dev-test'):
return apt.VerifiedHelperContext.parse({
'schema_version': 2, 'transaction_sha256': 'a' * 64,
'effect_armed_ledger_id': 'ledger-1', 'grant_id': 'grant-1',
'not_before_ms': 1000, 'start_before_ms': 61000, 'boot_id': 'boot-1',
'monotonic_start_before_ns': 10000, 'job_payload_sha256': 'b' * 64,
'action': action, 'execution_profile': profile, 'job_id': 'job-1',
'attempt_id': 'attempt-1', 'authorization_id': 'approval-1', 'plan_sha256': 'c' * 64})
def action_manifest(ctx, obj, runtime):
return {'type': 'rmm.apt.helper-action', 'schema_version': 2,
'action': ctx.action, 'execution_profile': ctx.execution_profile,
'transaction_sha256': ctx.transaction_sha256, 'resolved_apt_transaction': obj,
'transaction_bytes': list(apt.canonical(obj)),
'provider_sha256': apt.digest(b'code'), 'hook_sha256': apt.digest(apt.hook_script()),
'apt_config_sha256': apt.digest(apt.apt_config()),
'runtime_sha256': apt.digest(apt.canonical(runtime)), 'test_result_id': 'native-test-1'}
def signed_job(ctx, manifest):
tx = manifest['resolved_apt_transaction']
return {'schema_version': 1, 'tenant_id': 'tenant-1', 'asset_id': 'asset-1',
'agent_id': 'agent-1', 'enrollment_generation': 1, 'assignment_version': 1,
'job_id': ctx.job_id, 'attempt_id': ctx.attempt_id, 'execution_identity': 'root',
'not_before': '2026-11-12T12:00:00Z', 'start_before': '2026-11-12T12:01:00Z',
'max_runtime_seconds': 300, 'max_output_bytes': 4096,
'action': {'action': 'apt_native_acceptance' if ctx.action == 'apt-native-acceptance' else 'ensure_package',
'recipe_id': tx['recipe_id'], 'recipe_version': tx['recipe_version'],
'apt_manifest': manifest}}
def prechecks(ctx):
return {'schema_version': 1, 'transaction_sha256': ctx.transaction_sha256,
'job_payload_sha256': ctx.job_payload_sha256, 'boot_id': ctx.boot_id,
'checks': {'namespaces_caps_nnp_seccomp': True, 'readonly_controlpaths': True,
'private_tmpfs_sockets': True,
'control_syscalls': {'invalid_reboot_magic': 'EPERM',
'invalid_kexec_load_flags': 'EPERM',
'invalid_kexec_file_fds_flags': 'EPERM'},
'frontend_lock': 'native-apt-check-conflict'}}
class BindingTests(TestCase):
def test_pinned_fixture_is_reproducible_and_has_no_scripts_or_dependencies(self):
for version in ('1.0', '1.1'):
first = apt.fixture_archive(version)
self.assertEqual(first, apt.fixture_archive(version))
self.assertEqual(apt.digest(first), apt.FIXTURE_SHA256[version])
# Inspect real deb control tar through dpkg-deb, never install.
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
path = Path(temp) / 'fixture.deb'
path.write_bytes(first)
data = apt.run_read(['/usr/bin/dpkg-deb', '--ctrl-tarfile', str(path)])
with tarfile.open(fileobj=io.BytesIO(data)) as archive:
self.assertEqual(archive.getnames(), ['./control'])
control = archive.extractfile('./control').read()
self.assertNotIn(b'Depends:', control)
self.assertIn(('Version: ' + version + '\n').encode(), control)
def test_fixture_scope_only_one_exact_package_and_two_transitions(self):
for recipe in apt.FIXTURE_RECIPES:
apt.fixture_scope(apt.ResolvedAptTransaction.parse(fixture_transaction(recipe)))
for field, value in (('name', 'another-package'), ('new_version', '9.0'),
('sha256', 'd' * 64), ('size', 1), ('origin', 'catalog:external'),
('architecture', 'amd64'), ('old_version', '0.5')):
obj = fixture_transaction()
obj['packages'][0][field] = value
if field == 'sha256':
obj['packages'][0]['archive_path'] = str(apt.BASE / 'archives' / (value + '.deb'))
with self.subTest(field=field), self.assertRaises(apt.Veto):
apt.fixture_scope(apt.ResolvedAptTransaction.parse(obj))
obj = fixture_transaction()
obj['recipe_id'] = 'ordinary-catalog-recipe'
with self.assertRaisesRegex(apt.Veto, 'FIXTURE_RECIPE'):
apt.fixture_scope(apt.ResolvedAptTransaction.parse(obj))
def test_context_schema1_cannot_authorize_bootstrap(self):
obj = dict(context().__dict__)
obj['schema_version'] = 1
with self.assertRaisesRegex(apt.Veto, 'CONTEXT_SCHEMA'):
apt.VerifiedHelperContext.parse(obj)
def test_signed_nested_manifest_is_mandatory_and_hash_bound(self):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
ctx = context()
ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'transaction_sha256': apt.digest(apt.canonical(obj))})
manifest = action_manifest(ctx, obj, {})
raw = apt.canonical(signed_job(ctx, manifest))
ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': apt.digest(raw)})
def read(path, *args):
return {apt.ACTIVE / 'signed-action.json': raw,
apt.ACTIVE / 'expectation.json': apt.canonical(obj),
apt.CODE: b'code', Path(apt.HOOK): apt.hook_script()}[path]
with patch.object(apt, 'sealed_read', side_effect=read):
self.assertEqual(apt.load_signed_action(tx, ctx), manifest)
wrong_ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': '0' * 64})
with self.assertRaisesRegex(apt.Veto, 'SIGNED_ACTION_HASH'):
apt.load_signed_action(tx, wrong_ctx)
for change in ('nested', 'action', 'code', 'profile'):
altered = copy.deepcopy(manifest)
if change == 'nested':
altered['resolved_apt_transaction']['packages'][0]['new_version'] = '9.0'
elif change == 'action':
altered['action'] = 'apt-execute'
elif change == 'code':
altered['provider_sha256'] = '0' * 64
else:
altered['execution_profile'] = 'production'
altered_raw = apt.canonical(signed_job(ctx, altered))
altered_ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': apt.digest(altered_raw)})
with patch.object(apt, 'sealed_read', side_effect=lambda path, *args:
altered_raw if path == apt.ACTIVE / 'signed-action.json' else read(path)):
with self.subTest(change=change), self.assertRaises(apt.Veto):
apt.load_signed_action(tx, altered_ctx)
def test_bootstrap_mode_cannot_replace_normal_or_production_action(self):
tx = apt.ResolvedAptTransaction.parse(fixture_transaction())
for mode, ctx in ((True, context('apt-execute')), (False, context()),
(True, context(profile='production')), (False, context('apt-execute', 'production'))):
with self.subTest(mode=mode, action=ctx.action), self.assertRaises(apt.Veto):
apt.check_native_gate(tx, ctx, action_manifest(ctx, fixture_transaction(), {}), {}, mode)
def test_normal_execution_requires_real_scoped_record(self):
tx = apt.ResolvedAptTransaction.parse(fixture_transaction())
ctx = context('apt-execute')
with patch.object(apt, 'sealed_read', side_effect=FileNotFoundError):
with self.assertRaises(FileNotFoundError):
apt.check_native_gate(tx, ctx, action_manifest(ctx, fixture_transaction(), {}), {}, False)
def test_runtime_changes_are_rejected_even_for_bootstrap(self):
tx = apt.ResolvedAptTransaction.parse(fixture_transaction())
ctx = context()
with self.assertRaisesRegex(apt.Veto, 'SIGNED_RUNTIME_BINDING'):
apt.check_native_gate(tx, ctx, action_manifest(ctx, fixture_transaction(), {}), {'new': True}, True)
class NativeProbeTests(TestCase):
def test_native_mount_introspection_and_checks_result_with_mocked_kernel(self):
self.inspect_kernel_scenario('valid')
def test_native_introspection_rejects_bad_caps_namespaces_mounts_and_sockets(self):
for scenario in ('capability', 'nnp', 'namespace', 'readonly', 'tmpfs', 'socket'):
with self.subTest(scenario=scenario), self.assertRaises(apt.Veto):
self.inspect_kernel_scenario(scenario)
def inspect_kernel_scenario(self, scenario):
mask = sum(1 << n for n in (0, 1, 3, 4, 6, 7, 8, 31))
if scenario == 'capability':
mask |= 1 << 22
status = 'NoNewPrivs: ' + ('0' if scenario == 'nnp' else '1') + '\nSeccomp: 2\n'
status += ''.join(key + ': ' + format(mask, 'x') + '\n' for key in ('CapEff', 'CapPrm', 'CapBnd'))
status += 'CapInh: 0\nCapAmb: 0\n'
mountinfo = []
for path in ('/sys', '/proc/sys', '/usr/lib/rmm-apt-transaction', str(apt.BASE / 'archives')):
flags = 'rw' if scenario == 'readonly' and path == '/sys' else 'ro'
mountinfo.append('1 0 0:1 / ' + path + ' ' + flags + ' - sysfs sysfs ro')
for path in ('/run', '/tmp', '/var/tmp', '/dev'):
kind = 'ext4' if scenario == 'tmpfs' and path == '/run' else 'tmpfs'
mountinfo.append('1 0 0:1 / ' + path + ' rw - ' + kind + ' tmpfs rw')
def text(path, *args, **kwargs):
return {'/proc/self/status': status, '/proc/self/mountinfo': '\n'.join(mountinfo)}[str(path)]
with patch.object(Path, 'read_text', text), \
patch.object(Path, 'read_bytes', return_value=b'/usr/bin/python3\x00-I\x00'), \
patch.object(Path, 'exists', side_effect=lambda: scenario == 'socket'), \
patch.object(Path, 'stat', return_value=type('MockStat', (), {'st_mode': stat.S_IFCHR})()), \
patch.object(apt, 'sealed_read', return_value=apt.canonical({'pid': 'host', 'mnt': 'host', 'net': 'host'})), \
patch.object(apt.os, 'readlink', return_value='host' if scenario == 'namespace' else 'child'), \
patch.object(apt, 'invalid_control_syscall_probes', return_value=prechecks(context())['checks']['control_syscalls']), \
patch.object(apt, 'frontend_lock_probe', return_value='native-apt-check-conflict'), \
patch.object(apt.VerifiedHelperContext, 'check_deadline'):
self.assertEqual(apt.native_prechecks(context()), prechecks(context()))
def test_safe_invalid_syscall_probes_in_unprivileged_filtered_child(self):
code = '''import importlib.util,sys,json
sys.dont_write_bytecode=True
s=importlib.util.spec_from_file_location('apt_transaction',sys.argv[1])
m=importlib.util.module_from_spec(s);sys.modules[s.name]=m;s.loader.exec_module(m)
m.seccomp_filter()
print(json.dumps(m.invalid_control_syscall_probes()))
'''
output = subprocess.check_output(['/usr/bin/python3', '-I', '-c', code,
str(ROOT / 'tools/apt-transaction/apt_transaction.py')])
self.assertEqual(set(json.loads(output).values()), {'EPERM'})
def test_frontend_probe_uses_native_check_with_actual_fcntl_lock_but_no_local_apt(self):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
lock = Path(temp) / 'native-lock'
lock.touch()
with patch.object(apt, 'secure_path', return_value=lock), \
patch.object(apt, 'run_read', return_value=b'E: Could not get lock /var/lib/dpkg/lock-frontend') as read:
self.assertEqual(apt.frontend_lock_probe(), 'native-apt-check-conflict')
self.assertEqual(read.call_args.args[0][-1], 'check')
self.assertEqual(read.call_args.kwargs['expected_returncode'], 100)
with patch.object(apt, 'secure_path', return_value=lock), \
patch.object(apt, 'run_read', return_value=b'an unrelated error'):
with self.assertRaisesRegex(apt.Veto, 'NATIVE_APT_LOCK_CONFLICT_NOT_PROVEN'):
apt.frontend_lock_probe()
class CompletionTests(TestCase):
def setup_files(self, base, ctx):
active = base / 'active'
active.mkdir()
(active / 'native-prechecks.json').write_bytes(apt.canonical(prechecks(ctx)))
(active / 'hook-armed.json').write_bytes(apt.canonical({'grant_id': ctx.grant_id}))
(base / 'bootstrap-attempt-rmm-apt-fixture-install-1.0.json').write_bytes(apt.canonical({
'transaction_sha256': ctx.transaction_sha256, 'job_payload_sha256': ctx.job_payload_sha256,
'grant_id': ctx.grant_id, 'attempt_id': ctx.attempt_id}))
return active
def test_record_after_complete_mocked_native_pipeline_is_scoped_and_fsynced(self):
ctx = context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = self.setup_files(base, ctx)
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt, 'secure_path'), patch.object(apt, 'sealed_read', side_effect=lambda p, *a: p.read_bytes()), \
patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt.os, 'fsync', wraps=os.fsync) as sync:
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
result = apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {},
{'reboot_required': False})
self.assertTrue(result['test_acceptance'])
self.assertTrue(result['noProductionEvidence'])
self.assertGreaterEqual(sync.call_count, 3)
proof = json.loads(apt.record_path(tx).read_bytes())
self.assertEqual(proof['job_payload_sha256'], ctx.job_payload_sha256)
self.assertEqual(proof['scope']['recipe_id'], tx.recipe_id)
self.assertEqual(proof['runtime'], {})
normal_ctx = context('apt-execute')
apt.check_native_gate(tx, normal_ctx, action_manifest(normal_ctx, obj, {}), {}, False)
with self.assertRaisesRegex(apt.Veto, 'ACCEPTANCE_ALREADY_RECORDED'):
apt.check_native_gate(tx, ctx, action_manifest(ctx, obj, {}), {}, True)
with self.assertRaises(apt.Veto):
apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {}, {'reboot_required': False})
def test_missing_partial_negative_or_wrong_prechecks_never_publish_record(self):
for variation in ('missing', 'partial', 'negative', 'binding', 'reboot', 'runtime'):
ctx = context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = self.setup_files(base, ctx)
checkfile = active / 'native-prechecks.json'
if variation == 'missing':
checkfile.unlink()
elif variation == 'partial':
checkfile.write_bytes(b'{')
elif variation in ('negative', 'binding'):
obj = prechecks(ctx)
if variation == 'negative':
obj['checks']['namespaces_caps_nnp_seccomp'] = False
else:
obj['transaction_sha256'] = '0' * 64
checkfile.write_bytes(apt.canonical(obj))
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt, 'secure_path'), patch.object(apt, 'sealed_read', side_effect=lambda p, *a: p.read_bytes()), \
patch.object(apt, 'runtime_identity', return_value={'changed': True} if variation == 'runtime' else {}):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
with self.subTest(variation=variation), self.assertRaises((apt.Veto, OSError)):
apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {},
{'reboot_required': variation == 'reboot'})
self.assertFalse(apt.record_path(tx).exists())
def test_staged_write_failure_never_publishes_final_record(self):
ctx = context()
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = self.setup_files(base, ctx)
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt, 'sealed_read', side_effect=lambda p, *a: p.read_bytes()), \
patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt, 'durable_once', side_effect=OSError('fsync failure')):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
with self.assertRaises(OSError):
apt.complete_acceptance(tx, ctx, action_manifest(ctx, obj, {}), {}, {'reboot_required': False})
self.assertFalse(apt.record_path(tx).exists())
class DriverTests(TestCase):
def test_entire_driver_mocked_child_success_and_redelivery_no_second_launch(self):
self.run_driver_scenario('success')
def test_failed_unknown_stale_or_partial_driver_never_creates_acceptance(self):
for scenario in ('child-failed', 'no-hook', 'missing-prechecks', 'postcondition-failed', 'stale'):
with self.subTest(scenario=scenario):
self.run_driver_scenario(scenario)
def run_driver_scenario(self, scenario):
# No unshare/apt/root execution: only the process boundary and root
# preflight functions are mocked. Actual gate/one-shot/fsync/record logic
# runs on private temporary files. Such files never leave this test dir.
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = base / 'active'
active.mkdir()
for name in ('empty', 'lists', 'cache'):
(active / name).mkdir()
(active / 'apt.conf').write_bytes(apt.apt_config())
ctx = context()
native_code = 100 if scenario == 'child-failed' else 0
def read(path, *args):
return apt.hook_script() if path == Path(apt.HOOK) else path.read_bytes()
def child(argv, **kwargs):
self.assertEqual(argv[0], '/usr/bin/unshare')
self.assertIn('--pid', argv)
if scenario != 'no-hook':
(active / 'hook-armed.json').write_bytes(apt.canonical({
'transaction_sha256': ctx.transaction_sha256,
'effect_armed_ledger_id': ctx.effect_armed_ledger_id, 'grant_id': ctx.grant_id}))
(active / 'sandbox-result.json').write_bytes(apt.canonical({
'transaction_sha256': ctx.transaction_sha256, 'apt_exit_code': native_code,
'reboot_required': False}))
if scenario != 'missing-prechecks':
(active / 'native-prechecks.json').write_bytes(apt.canonical(prechecks(ctx)))
return type('MockNativeChild', (), {'wait': lambda _: native_code})()
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt.os, 'geteuid', return_value=0), patch.object(apt, 'secure_path', side_effect=lambda p, **k: p), \
patch.object(apt, 'sealed_read', side_effect=read), patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt, 'check_current_baseline'), patch.object(apt, 'check_preconditions'), patch.object(apt, 'verify_archive'), \
patch.object(apt, 'run_read', return_value=b''), patch.object(apt, 'installed_snapshot', return_value={}), \
patch.object(apt.VerifiedHelperContext, 'check_deadline',
side_effect=apt.Veto('START_DEADLINE') if scenario == 'stale' else None), \
patch.object(apt.subprocess, 'Popen', side_effect=child) as spawn:
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
manifest = action_manifest(ctx, obj, {})
evidence = {'reboot_required': False, 'snapshot_sha256': 'e' * 64}
with patch.object(apt, 'load_active', return_value=(tx, ctx, {})), \
patch.object(apt, 'load_signed_action', return_value=manifest), \
patch.object(apt, 'verify_after', return_value=evidence,
side_effect=apt.Veto('POSTCONDITION_OR_UNEXPECTED_CHANGE')
if scenario == 'postcondition-failed' else None):
request = {'transaction_sha256': ctx.transaction_sha256,
'effect_armed_ledger_id': ctx.effect_armed_ledger_id}
result = apt.execute(request, native_acceptance=True)
self.assertTrue(result['evidence']['test_acceptance'])
self.assertTrue(result['evidence']['noProductionEvidence'])
if scenario == 'success':
self.assertEqual(result['effect_state'], 'verified')
self.assertTrue(apt.record_path(tx).is_file())
self.assertEqual(spawn.call_count, 1)
repeated = apt.execute(request, native_acceptance=True)
self.assertEqual(repeated['effect_state'], 'unknown')
self.assertEqual(spawn.call_count, 1)
else:
self.assertNotEqual(result['effect_state'], 'verified')
self.assertFalse(apt.record_path(tx).exists())
self.assertEqual(spawn.call_count, 0 if scenario == 'stale' else 1)
@@ -0,0 +1,198 @@
"""Harmless tmp fixtures only; root ownership/system state/providers mocked."""
import copy
import json
import os
from pathlib import Path
import stat
import tempfile
from types import SimpleNamespace
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt, ROOT
from test_native_acceptance import fixture_transaction, context, action_manifest
class PlanningTests(TestCase):
def run_planning(self, variation=None):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
(base / 'archives').mkdir()
(base / 'baselines').mkdir()
for version, sha in apt.FIXTURE_SHA256.items():
(base / 'archives' / (sha + '.deb')).write_bytes(apt.fixture_archive(version))
(base / ('prefetch-' + sha + '.json')).write_bytes(apt.canonical({
'schema_version': 1, 'sha256': sha, 'size': 20672, 'sealed_at_ms': 1000}))
pin = apt.FIXTURE_SHA256['1.0']
if variation == 'bytes':
(base / 'archives' / (pin + '.deb')).write_bytes(b'x' * 20672)
if variation == 'receipt':
(base / ('prefetch-' + pin + '.json')).write_bytes(apt.canonical({
'schema_version': 1, 'sha256': pin, 'size': 20672, 'sealed_at_ms': 10**30}))
if variation == 'missing':
(base / ('prefetch-' + pin + '.json')).unlink()
initial = {'rmm-mvp-test:all': {'version': '1.0', 'status': 'install ok installed'},
'private-business-package:amd64': {'version': 'secret', 'status': 'install ok installed'}}
current = copy.deepcopy(initial)
runtime = {'apt_config_sha256': apt.digest(apt.apt_config()), 'schema_version': 1}
def read(path, *args):
if path == apt.CODE:
return b'code'
if path == Path(apt.HOOK):
return apt.hook_script()
return path.read_bytes()
original_metadata = apt.archive_metadata
def metadata(st, size):
# Only ownership is simulated. Real bytes/size/link count are
# used, and production metadata validation remains exercised.
original_metadata(SimpleNamespace(st_uid=0,
st_mode=stat.S_IFREG | (0o622 if variation == 'writable' else 0o600),
st_size=st.st_size, st_nlink=st.st_nlink), size)
fingerprint_error = apt.Veto('DPKG_DIRTY_UPDATES') if variation == 'dirty' else None
if variation == 'drift':
fingerprint_error = ['b' * 64, 'c' * 64]
with patch.object(apt, 'BASE', base), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'secure_path', side_effect=lambda p, **k: p), \
patch.object(apt, 'sealed_read', side_effect=read), \
patch.object(apt, 'archive_metadata', side_effect=metadata), \
patch.object(apt, 'observe_host_frontend_lock', return_value=variation == 'locked'), \
patch.object(apt, 'status_fingerprint', return_value='b' * 64, side_effect=fingerprint_error), \
patch.object(apt, 'installed_snapshot', side_effect=lambda: copy.deepcopy(current)), \
patch.object(apt, 'runtime_identity', return_value=runtime), \
patch.object(apt.os, 'fsync', wraps=os.fsync) as sync:
first = apt.planning_status()
self.assertGreaterEqual(sync.call_count, 2)
self.assertNotIn('private-business-package', json.dumps(first))
sealed = base / 'baselines' / (first['baseline_sha256'] + '.json')
self.assertEqual(apt.digest(sealed.read_bytes()), first['baseline_sha256'])
self.assertEqual(json.loads(sealed.read_bytes()), initial)
self.assertEqual(first['baseline_sha256'], first['sealed_baseline_sha256'])
self.assertEqual(first['runtime_sha256'], apt.digest(apt.canonical(runtime)))
before = sealed.stat().st_mtime_ns
self.assertEqual(apt.planning_status()['baseline_sha256'], first['baseline_sha256'])
self.assertEqual(sealed.stat().st_mtime_ns, before)
current['rmm-mvp-test:all']['version'] = '1.1'
fresh = apt.planning_status()
self.assertNotEqual(first['baseline_sha256'], fresh['baseline_sha256'])
self.assertEqual(fresh['fixture_package']['version'], '1.1')
self.assertEqual(len(list((base / 'baselines').iterdir())), 2)
self.assertFalse((base / 'active').exists())
def test_passive_seal_is_content_addressed_fresh_and_private(self):
self.run_planning()
def test_bad_receipts_bytes_ownership_dirty_or_locked_are_not_observations(self):
for variation in ('bytes', 'receipt', 'missing', 'writable', 'dirty', 'locked'):
with self.subTest(variation=variation), self.assertRaises((apt.Veto, OSError)):
self.run_planning(variation)
def test_state_changes_during_sampling_cannot_publish(self):
with patch.object(apt, 'durable_once') as write:
with self.assertRaisesRegex(apt.Veto, 'PLANNING_STATE_CHANGED'):
self.run_planning('drift')
write.assert_not_called()
def test_shared_canonical_golden_bytes(self):
raw = (ROOT / 'tests/apt_transaction/fixtures/planning-canonical.json').read_bytes().rstrip(b'\n')
self.assertEqual(apt.canonical(json.loads(raw)), raw)
self.assertEqual(apt.digest(raw), 'f5c1f569110924eca83ad5d347882390b47e077fbef91c4eb86abb68621b92c4')
class NoChangeTests(TestCase):
def check_noop(self, recipe, version=None, status='install ok installed', locked=False, changed=False,
architecture='all', dirty=False, audit=b'', payload=None, expired=False):
obj = fixture_transaction(recipe)
desired = obj['packages'][0]['new_version']
baseline = {'rmm-mvp-test:' + architecture: {'version': version or desired, 'status': status}}
obj['baseline_sha256'] = apt.digest(apt.canonical(baseline))
obj['status_fingerprint'] = 'b' * 64
tx = apt.ResolvedAptTransaction.parse(obj)
ctx = context('apt-execute')
current = copy.deepcopy(baseline)
if changed:
current['rmm-mvp-test:all']['version'] = '9.0'
with patch.object(apt, 'installed_snapshot', return_value=current), \
patch.object(apt, 'status_fingerprint', return_value='b' * 64,
side_effect=apt.Veto('DPKG_PENDING_TRIGGERS') if dirty else None), \
patch.object(apt, 'observe_host_frontend_lock', return_value=locked), \
patch.object(apt, 'run_read', return_value=audit), \
patch.object(apt, 'sealed_read', return_value=payload if payload is not None else (desired + '\n').encode()), \
patch.object(apt.VerifiedHelperContext, 'check_deadline',
side_effect=apt.Veto('START_DEADLINE') if expired else None), \
patch.object(apt.subprocess, 'Popen') as spawn:
result = apt.verified_no_change(tx, baseline, ctx)
spawn.assert_not_called()
return result, tx, baseline
def test_install_and_update_already_desired_skip_only_old_version(self):
for recipe in apt.FIXTURE_RECIPES:
result, tx, baseline = self.check_noop(recipe)
self.assertFalse(result['changed'])
self.assertTrue(result['postcondition_already_met'])
self.assertFalse(result['transaction_executed'])
# Mutating preconditions still forbid a reinstall of that version.
with patch.object(apt, 'status_fingerprint', return_value='b' * 64), \
patch.object(apt, 'installed_snapshot', return_value=baseline):
with self.assertRaisesRegex(apt.Veto, 'PACKAGE_PRECONDITION'):
apt.check_preconditions(tx, baseline)
def test_missing_target_dirty_arch_or_newer_version_cannot_claim_no_change(self):
for version, status in (('1.0', 'install ok installed'), ('1.2', 'install ok installed'),
('1.1', 'deinstall ok config-files')):
result, _, _ = self.check_noop('rmm-apt-fixture-update-1.1', version, status)
self.assertIsNone(result)
for variation in ({'locked': True}, {'changed': True}):
with self.assertRaises(apt.Veto):
self.check_noop('rmm-apt-fixture-update-1.1', **variation)
result, _, _ = self.check_noop('rmm-apt-fixture-update-1.1', architecture='amd64')
self.assertIsNone(result)
def test_noop_dirty_state_audit_payload_and_expiry_are_not_success(self):
for variation in ({'dirty': True}, {'audit': b'pending package'}, {'payload': b'wrong\n'}, {'expired': True}):
with self.subTest(variation=variation), self.assertRaises(apt.Veto):
self.check_noop('rmm-apt-fixture-update-1.1', **variation)
def test_full_driver_no_launch_no_effect_markers_after_all_checks(self):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
active = base / 'active'
active.mkdir()
for directory in ('empty', 'lists', 'cache'):
(active / directory).mkdir()
(active / 'apt.conf').write_bytes(apt.apt_config())
obj = fixture_transaction('rmm-apt-fixture-update-1.1')
baseline = {'rmm-mvp-test:all': {'version': '1.1', 'status': 'install ok installed'}}
obj['baseline_sha256'] = apt.digest(apt.canonical(baseline))
obj['status_fingerprint'] = 'b' * 64
tx = apt.ResolvedAptTransaction.parse(obj)
ctx = context('apt-execute')
def read(path, *args, **kwargs):
if path == Path(apt.HOOK):
return apt.hook_script()
if str(path) == '/usr/share/rmm-mvp-test/version':
return b'1.1\n'
return path.read_bytes()
with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', active), \
patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'secure_path', side_effect=lambda p, **k: p), \
patch.object(apt, 'sealed_read', side_effect=read), \
patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt, 'load_active', return_value=(tx, ctx, baseline)), \
patch.object(apt, 'load_signed_action', return_value=action_manifest(ctx, obj, {})) as signed, \
patch.object(apt, 'check_native_gate') as gate, patch.object(apt, 'verify_archive') as archive, \
patch.object(apt, 'installed_snapshot', return_value=baseline), \
patch.object(apt, 'status_fingerprint', return_value='b' * 64), \
patch.object(apt, 'observe_host_frontend_lock', return_value=False), \
patch.object(apt, 'run_read', return_value=b''), \
patch.object(apt.VerifiedHelperContext, 'check_deadline'), \
patch.object(apt.subprocess, 'Popen') as spawn:
request = {'transaction_sha256': ctx.transaction_sha256,
'effect_armed_ledger_id': ctx.effect_armed_ledger_id}
result = apt.execute(request)
self.assertEqual(result['effect_state'], 'verified_no_change')
self.assertEqual(result['code'], 'POSTCONDITION_ALREADY_MET')
signed.assert_called_once()
gate.assert_called_once()
archive.assert_called_once()
spawn.assert_not_called()
self.assertEqual({p.name for p in active.iterdir()}, {'empty', 'lists', 'cache', 'apt.conf'})
@@ -0,0 +1,184 @@
"""Readiness isolation tests. No root setup/APT/host package effects are run."""
import json
import os
from pathlib import Path
import subprocess
import tempfile
from types import SimpleNamespace
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt, ROOT
from test_native_acceptance import prechecks, context
class ReadinessProbeTests(TestCase):
def test_controller_requires_actual_unit_caps_nnp_and_readonly_system(self):
mask = sum(1 << bit for bit in (0, 1, 3, 4, 6, 7, 8, 31, 21, 27))
for fault in ('none', 'boot-cap', 'nnp', 'filter', 'unit', 'writable', 'readonly-target', 'extra-write'):
current_mask = mask | (1 << 22) if fault == 'boot-cap' else mask
status = ('NoNewPrivs: ' + ('0' if fault == 'nnp' else '1') + '\n' +
'Seccomp: ' + ('0' if fault == 'filter' else '2') + '\n' +
''.join(key + ': ' + format(current_mask, 'x') + '\n' for key in ('CapEff', 'CapPrm', 'CapBnd')) +
'CapInh: 0\nCapAmb: 0\n')
cgroup = '0::/system.slice/' + ('other.service' if fault == 'unit' else 'rmm-apt-helper.service')
mounts = '1 0 0:1 / /usr ro - ext4 root ro\n'
if fault == 'extra-write':
mounts += '2 1 0:1 /usr/share /usr/share rw - ext4 root rw\n'
def text(path, *args, **kwargs):
return {'/proc/self/status': status, '/proc/self/cgroup': cgroup,
'/proc/self/mountinfo': mounts}[str(path)]
def flags(path):
ro = path in ('/usr', '/etc', '/var')
if fault == 'writable':
ro = False
if fault == 'readonly-target':
ro = True
return SimpleNamespace(f_flag=os.ST_RDONLY if ro else 0)
with self.subTest(fault=fault), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(Path, 'read_text', text), patch.object(apt.os, 'statvfs', side_effect=flags):
if fault == 'none':
apt.verify_controller_profile()
else:
with self.assertRaises(apt.Veto):
apt.verify_controller_profile()
def test_host_lock_observation_uses_only_readonly_getlk(self):
calls = []
def query(fd, operation, data):
calls.append(operation)
return data # same F_WRLCK query denotes a foreign owner; no acquisition
with patch.object(apt, 'secure_path', return_value=Path('/var/lib/dpkg/lock-frontend')), \
patch.object(apt.os, 'open', return_value=123) as opened, \
patch.object(apt.os, 'close'), patch.object(apt.fcntl, 'fcntl', side_effect=query), \
patch.object(apt.fcntl, 'lockf') as lockf, patch.object(apt.fcntl, 'flock') as flock:
self.assertTrue(apt.observe_host_frontend_lock())
self.assertEqual(calls, [apt.fcntl.F_GETLK])
flags = opened.call_args.args[1]
self.assertEqual(flags & os.O_ACCMODE, os.O_RDONLY)
lockf.assert_not_called()
flock.assert_not_called()
def test_probe_config_has_no_hooks_sources_or_install_command(self):
workspace = Path('/var/lib/rmm-apt-transaction/probes/00000000-0000-4000-8000-000000000001')
config = apt.probe_config(workspace)
self.assertNotIn(b'DPkg::Pre-Install-Pkgs', config)
self.assertNotIn(b'DPkg::Tools::Options', config)
self.assertIn(b'Dir::Etc::sourcelist "/dev/null"', config)
self.assertIn(str(workspace / 'log').encode(), config)
def test_probe_publishes_only_readiness_with_mocked_native_child(self):
self.probe_scenario('success')
def test_failed_or_partial_native_probe_never_publishes_readiness(self):
for scenario in ('child-failed', 'partial', 'syscalls', 'same-namespace'):
with self.subTest(scenario=scenario):
self.probe_scenario(scenario)
def probe_scenario(self, scenario):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
(base / 'probes').mkdir(mode=0o700)
host_status = b'Package: read-only-host-baseline\n'
original_open = os.open
def no_host_writes(path, flags, *args, **kwargs):
if flags & (os.O_WRONLY | os.O_RDWR | os.O_CREAT | os.O_TRUNC):
self.assertTrue(str(path).startswith(str(base) + '/'), str(path))
return original_open(path, flags, *args, **kwargs)
def sealed(path, *args):
if path == Path('/var/lib/dpkg/status'):
return host_status
return path.read_bytes()
def native(argv, **kwargs):
self.assertEqual(argv[-1], 'probe-worker')
self.assertNotIn('install', argv)
request = json.loads(kwargs['input'])
workspace = base / 'probes' / request['probe_id']
self.assertEqual((workspace / 'dpkg/status').read_bytes(), host_status)
self.assertFalse((workspace / 'context.json').exists())
if scenario != 'child-failed':
result = {'probe_id': request['probe_id'], 'checks': prechecks(context())['checks'],
'isolated_dpkg_check': True,
'namespaces': {name: name + ':[999999999]' for name in ('pid', 'mnt', 'net')}}
if scenario == 'partial':
result.pop('checks')
elif scenario == 'syscalls':
result['checks']['control_syscalls']['invalid_reboot_magic'] = 'EINVAL'
elif scenario == 'same-namespace':
result['namespaces'] = json.loads((workspace / 'host-namespaces.json').read_bytes())
(workspace / 'probe-result.json').write_bytes(apt.canonical(result))
return SimpleNamespace(returncode=100 if scenario == 'child-failed' else 0)
with patch.object(apt, 'BASE', base), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'secure_path'), patch.object(apt, 'runtime_identity', return_value={}), \
patch.object(apt, 'sealed_read', side_effect=sealed), \
patch.object(apt, 'observe_host_frontend_lock', return_value=False), \
patch.object(apt, 'verify_controller_profile'), \
patch.object(apt.os, 'open', side_effect=no_host_writes), \
patch.object(apt.subprocess, 'run', side_effect=native):
if scenario == 'success':
outcome = apt.probe_native()
self.assertTrue(outcome['readiness_only'])
self.assertFalse(outcome['native_acceptance'])
status = apt.readiness_status()
self.assertTrue(status['fixture_acceptance_ready'])
self.assertEqual(status['accepted_recipes'], [])
# A valid readiness report cannot promote a partial fixture
# acceptance file into an accepted package capability.
partial = base / ('acceptance-' + next(iter(apt.FIXTURE_RECIPES)) + '.json')
partial.write_bytes(b'{"schema_version":2,"test_acceptance":true}')
with self.assertRaises(apt.Veto):
apt.readiness_status()
partial.unlink() # only this artificially injected temp test file
else:
with self.assertRaises(apt.Veto):
apt.probe_native()
self.assertFalse((base / 'native-readiness.json').exists())
for pattern in ('**/hook-armed.json', '**/launch.json', '**/context.json',
'**/prefetch-*.json', '**/acceptance-*.json', '**/bootstrap-attempt-*.json'):
self.assertEqual(list(base.glob(pattern)), [])
def test_clone_namespace_flags_and_clone3_closed_but_fork_exec_works(self):
code = '''import ctypes,importlib.util,sys,subprocess,errno
sys.dont_write_bytecode=True
s=importlib.util.spec_from_file_location('apt_transaction',sys.argv[1])
m=importlib.util.module_from_spec(s);sys.modules[s.name]=m;s.loader.exec_module(m)
m.seccomp_filter()
lib=ctypes.CDLL('libseccomp.so.2');lib.seccomp_syscall_resolve_name.argtypes=[ctypes.c_char_p]
c=ctypes.CDLL(None,use_errno=True);c.syscall.restype=ctypes.c_long
clone=lib.seccomp_syscall_resolve_name(b'clone');clone3=lib.seccomp_syscall_resolve_name(b'clone3')
for bit in (0x80,0x20000,0x2000000,0x4000000,0x8000000,0x10000000,0x20000000,0x40000000):
ctypes.set_errno(0)
# Invalid exit signal ensures no child can be created even without a filter.
assert c.syscall(clone,ctypes.c_ulong(bit|255),0,0,0,0)==-1
assert ctypes.get_errno()==errno.EPERM
ctypes.set_errno(0);assert c.syscall(clone3,0,0)==-1 and ctypes.get_errno()==errno.ENOSYS
assert subprocess.check_output(['/usr/bin/python3','-I','-c','print("fork-ok")'])==b'fork-ok\\n'
print('CLONE_GAPS_CLOSED')
'''
result = subprocess.check_output(['/usr/bin/python3', '-I', '-c', code,
str(ROOT / 'tools/apt-transaction/apt_transaction.py')])
self.assertEqual(result, b'CLONE_GAPS_CLOSED\n')
def test_dedicated_unit_has_narrow_profile_without_weakening_general_unit(self):
unit = (ROOT / 'deploy/linux/rmm-apt-helper.service').read_text()
for required in ('NoNewPrivileges=true', 'PrivateNetwork=true', 'ProtectSystem=strict',
'RestrictNamespaces=mnt pid net', 'DevicePolicy=strict', 'PrivateDevices=no',
'AmbientCapabilities=', 'SystemCallArchitectures=native'):
self.assertIn(required, unit)
bounding = next(line for line in unit.splitlines() if line.startswith('CapabilityBoundingSet='))
self.assertIn('CAP_SYS_ADMIN', bounding)
self.assertIn('CAP_MKNOD', bounding)
self.assertNotIn('CAP_SYS_BOOT', bounding)
self.assertNotIn('CAP_SYS_CHROOT', bounding)
writable = next(line for line in unit.splitlines() if line.startswith('ReadWritePaths='))
self.assertEqual(writable.split('=', 1)[1].split(), ['/var/lib/dpkg', '/var/lib/rmm-helper',
'/var/lib/rmm-apt-transaction', '/usr/share/rmm-mvp-test'])
@@ -0,0 +1,69 @@
"""The provider consumes the exact signed domain job, never a detached manifest."""
import copy
import json
import tempfile
from pathlib import Path
from unittest import TestCase
from unittest.mock import patch
from test_guard import apt
from test_native_acceptance import context, fixture_transaction, action_manifest, signed_job
class SignedHelperEnvelopeTests(TestCase):
def test_pregrant_baselines_are_content_addressed_and_do_not_reset_artifacts(self):
with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp:
base = Path(temp)
(base / 'baselines').mkdir(mode=0o700)
snapshot = {'rmm-mvp-test:all': {'version': '1.0', 'status': 'install ok installed'}}
with patch.object(apt, 'BASE', base), patch.object(apt.os, 'geteuid', return_value=0), \
patch.object(apt, 'secure_path'), patch.object(apt, 'installed_snapshot', return_value=snapshot), \
patch.object(apt, 'status_fingerprint', return_value='a' * 64), \
patch.object(apt, 'sealed_read', side_effect=lambda path: path.read_bytes()):
result = apt.prepare_baseline_store()
path = base / 'baselines' / (result['baseline_sha256'] + '.json')
self.assertEqual(json.loads(path.read_bytes()), snapshot)
self.assertFalse(result['authorization_created'])
self.assertFalse(result['native_acceptance_created'])
initial = path.stat().st_mtime_ns
self.assertEqual(apt.prepare_baseline_store(), result)
self.assertEqual(path.stat().st_mtime_ns, initial)
snapshot['rmm-mvp-test:all']['version'] = '1.1'
newer = apt.prepare_baseline_store()
self.assertNotEqual(newer['baseline_sha256'], result['baseline_sha256'])
self.assertEqual(len(list((base / 'baselines').iterdir())), 2)
def test_detached_manifest_aliases_wrong_ids_and_nonexact_bytes_are_rejected(self):
obj = fixture_transaction()
tx = apt.ResolvedAptTransaction.parse(obj)
raw_tx = apt.canonical(obj)
ctx = context()
ctx = apt.VerifiedHelperContext(**{**ctx.__dict__, 'transaction_sha256': apt.digest(raw_tx)})
manifest = action_manifest(ctx, obj, {})
valid = signed_job(ctx, manifest)
variants = [manifest]
for fault in ('job-id', 'script-alias', 'transaction-byte', 'transaction-bool', 'schema', 'extra-root'):
changed = copy.deepcopy(valid)
if fault == 'job-id':
changed['job_id'] = 'another-job'
elif fault == 'script-alias':
changed['action']['action'] = 'run_script'
elif fault == 'schema':
changed['action']['apt_manifest']['schema_version'] = 1
elif fault == 'extra-root':
changed['unsigned_manifest'] = manifest
else:
changed['action']['apt_manifest']['transaction_bytes'][0] = True if fault == 'transaction-bool' else 0
variants.append(changed)
for value in variants:
raw = apt.canonical(value)
candidate = apt.VerifiedHelperContext(**{**ctx.__dict__, 'job_payload_sha256': apt.digest(raw)})
def read(path, *args):
return {apt.ACTIVE / 'signed-action.json': raw,
apt.ACTIVE / 'expectation.json': raw_tx,
apt.CODE: b'code', Path(apt.HOOK): apt.hook_script()}[path]
with self.subTest(value=value.get('type', 'job')), patch.object(apt, 'sealed_read', side_effect=read):
with self.assertRaises(apt.Veto):
apt.load_signed_action(tx, candidate)