Files
rust-discovery/tests/apt_transaction/test_guard.py
T
BuTzZ 6c3203d725
Rust development slice / linux (push) Failing after 1m10s
feat: snapshot Linux-first RMM MVP and pause handover
2026-11-12 22:06:25 +01:00

309 lines
16 KiB
Python

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()