Files
rust-discovery/deploy/server_time_source_probe_acceptance.py
T
2026-11-15 09:21:24 +01:00

256 lines
14 KiB
Python

#!/usr/bin/env python3
"""One fixed source-quality acceptance after the original pre-exec GROUP failure.
No transport, provisioning, original-phase retry, keys or broker installation.
Run only as the root operator with python3 -I; uncertain execution is read only.
"""
import argparse
import grp
import hashlib
import importlib.util
import json
import os
from pathlib import Path
import stat
import sys
import uuid
CASE = '5b358997-3abc-43db-945f-f41855b4e3c0'
PHASE = 'probe-group-acceptance-v1'
PROFILE = 'rmm.server-time-source.group-acceptance.v1'
PIN_CONTROLLER_SHA = 'fac85dd0399009af36f625232daa5b67ab7fda391dbf95229d4237c307b142ca'
PIN_CONTROLLER = Path('/var/lib/rmm-time-source-operators') / PIN_CONTROLLER_SHA / 'pin_bootstrap.py'
PIN_RESULT_SHA = 'd0a9ac7b46bdf4247c420f01d959c41d6620f7081c40629016d6866828884c4b'
PUBLIC_PIN_SHA = '203f63f150026d345614ca53e641f248397166fbdeb1f28318fc5d7763317e0a'
GROUP_RECEIPT_SHA = '4977876b935bed8903ce4c880a0d9c77c49cd2c7110551fbf854baeccbe64ea7'
ARTIFACT = dict(bytes=848632, sha256='216b4d6a0f29ef47576f5ccc469954623141e55d7b43bd73175c1d956363bbbd', elf_machine=183)
BASE = Path('/var/lib/rmm-time-source-bootstrap') / CASE
IMAGE = BASE / 'build/artifacts/operator_probe'
OLD_UNIT = 'rmm-time-source-probe-' + CASE + '.service'
UNIT = 'rmm-time-source-probe-group-acceptance-' + CASE + '.service'
SAMPLE_KEYS = {'profile', 'clock_authority', 'signed_attestation', 'captured_utc_ns',
'server_error_ns', 'source_sync_age_ns', 'server_boot_id',
'captured_monotonic_ns', 'sampled_boottime_ns', 'kernel_state'}
def require(ok, reason):
if not ok:
raise RuntimeError(reason)
def sha(raw):
return hashlib.sha256(raw).hexdigest()
def load_pin_controller():
# Bootstrap the already reviewed reader without following import paths or
# executing a second, unchecked pathname read. All ancestors stay root-only writable.
parent = os.open('/', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try:
for part in PIN_CONTROLLER.parent.parts[1:]:
info = os.fstat(parent)
require(info.st_uid == 0 and info.st_mode & 0o022 == 0, 'protected_controller_ancestry')
child = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=parent)
os.close(parent)
parent = child
info = os.fstat(parent)
require(info.st_uid == 0 and info.st_mode & 0o022 == 0, 'protected_controller_directory')
fd = os.open(PIN_CONTROLLER.name, os.O_RDONLY | os.O_NOFOLLOW, dir_fd=parent)
with os.fdopen(fd, 'rb') as stream:
before = os.fstat(stream.fileno())
require(stat.S_ISREG(before.st_mode) and before.st_uid == before.st_gid == 0 and
before.st_nlink == 1 and stat.S_IMODE(before.st_mode) == 0o600 and
before.st_size <= 128 * 1024, 'fixed_root_controller_file')
raw = stream.read(128 * 1024 + 1)
stable = lambda item: tuple(getattr(item, 'st_' + key) for key in
('dev', 'ino', 'uid', 'gid', 'mode', 'nlink', 'size', 'mtime_ns', 'ctime_ns'))
require(stable(before) == stable(os.fstat(stream.fileno())) ==
stable(os.stat(PIN_CONTROLLER.name, dir_fd=parent, follow_symlinks=False)) and
len(raw) == before.st_size and
sha(raw) == PIN_CONTROLLER_SHA, 'exact_reviewed_pin_controller')
finally:
os.close(parent)
module = importlib.util.module_from_spec(importlib.util.spec_from_loader('rmm_fixed_pin_reader', loader=None))
module.__file__ = str(PIN_CONTROLLER)
exec(compile(raw, str(PIN_CONTROLLER), 'exec'), module.__dict__)
held = module.read_file(PIN_CONTROLLER, 128 * 1024)
require(held[0] == raw, 'same_loaded_controller_bytes')
return module, lambda: require(module.read_file(PIN_CONTROLLER, 128 * 1024) == held,
'pin_controller_unchanged')
def group_before_exec(old, log, rows, groups):
require(type(old.get('exit_code')) is int and old['exit_code'] == 216 and log == b'' and
old.get('sample') is None, 'original_216_empty_log_no_sample')
require(groups == [{'gid': 989, 'name': '_chrony', 'present': True},
{'gid': 10001, 'present': False}], 'actual_original_nss_group_condition')
messages = [row.get('MESSAGE') for row in rows]
require(any(type(text) is str and 'Failed to determine supplementary groups' in text for text in messages)
and any(type(text) is str and 'Failed at step GROUP spawning ' + str(IMAGE) in text for text in messages)
and any(str(row.get('EXIT_STATUS')) == '216' and row.get('EXIT_CODE') == 'exited' for row in rows),
'original_unit_journal_confirms_pre_exec_group_failure')
def sample_quality(sample, boot):
require(type(sample) is dict and set(sample) == SAMPLE_KEYS and
sample['profile'] == 'rmm.native-source-quality.once.v1' and
sample['clock_authority'] is False and sample['signed_attestation'] is False and
sample['server_boot_id'] == boot and str(uuid.UUID(boot)) == boot,
'exact_non_authoritative_native_sample_schema_and_boot')
for key, upper in (('captured_utc_ns', 2**63 - 1), ('server_error_ns', 500_000_000),
('source_sync_age_ns', 3_600_000_000_000), ('captured_monotonic_ns', 2**64 - 1),
('sampled_boottime_ns', 2**64 - 1), ('kernel_state', 4)):
require(type(sample[key]) is int and 0 <= sample[key] <= upper, 'bounded_numeric_quality_sample')
require(sample['captured_utc_ns'] > 0 and sample['captured_monotonic_ns'] > 0 and
sample['sampled_boottime_ns'] >= sample['captured_monotonic_ns'], 'positive_native_time_domains')
def probe_args():
return ['systemd-run', '--quiet', '--wait', '--pipe', '--collect', '--unit=' + UNIT,
'--property=Type=exec', '--property=User=root', '--property=Group=989',
'--property=SupplementaryGroups=', '--property=NoNewPrivileges=yes',
'--property=CapabilityBoundingSet=CAP_SYS_PTRACE', '--property=AmbientCapabilities=',
'--property=ProtectSystem=strict', '--property=ProtectHome=yes', '--property=PrivateTmp=yes',
'--property=PrivateDevices=yes', '--property=RestrictAddressFamilies=AF_UNIX',
'--property=IPAddressDeny=any',
'--property=SystemCallFilter=~@debug ptrace process_vm_readv process_vm_writev',
'--property=RuntimeMaxSec=3s', '--property=ReadWritePaths=/run/rmm-time-source/clients', str(IMAGE)]
def host_read(pin):
# No operator import, subprocess, NSS lookup, sockets, preflight or sample.
records = {}
for leaf in ('intent.json', 'before.json', 'group-before-exec.json', 'execution.json',
'after.json', 'result.json', 'failure.json', 'probe.log'):
try:
raw, proof = pin.read_file(BASE / PHASE / leaf, 16384)
records[leaf] = {'sha256': sha(raw), 'metadata': proof,
'record': raw.decode('ascii') if leaf == 'probe.log' else pin.parsed(raw)}
except FileNotFoundError:
records[leaf] = None
print(pin.encoded(dict(original_case=CASE, phase=PHASE, read_only=True, records=records)).decode('ascii'), end='')
def host_probe(pin, pin_guard):
case_fd = pin.directory(BASE)
phase_fd = -1
try:
pin.absence(case_fd, PHASE)
operator, operator_guard = pin.load_operator()
original = pin.Original(BASE, operator, operator_guard)
require(original.artifacts['examples/operator_probe'][:2] == (IMAGE, ARTIFACT), 'fixed_original_native_artifact')
pin_raw, pin_proof = pin.read_file(BASE / 'pin/result.json')
require(sha(pin_raw) == PIN_RESULT_SHA, 'actual_original_pin_result')
pin_result = pin.parsed(pin_raw)
old_raw, old_proof = pin.read_file(BASE / 'probe/result.json')
old_log, old_log_proof = pin.read_file(BASE / 'probe/probe.log', 16384)
old = pin.parsed(old_raw)
require(old.get('artifact') == ARTIFACT and old.get('probe_log_sha256') == sha(old_log),
'original_failed_probe_artifact_and_log')
def fresh():
pin_guard()
actual_pin = original.fresh()
require(pin.read_file(BASE / 'pin/result.json') == (pin_raw, pin_proof) and
pin.read_file(BASE / 'probe/result.json') == (old_raw, old_proof) and
pin.read_file(BASE / 'probe/probe.log', 16384) == (old_log, old_log_proof),
'original_pin_and_failed_probe_records_unchanged')
raw, proof = pin.read_file(pin.ETC / 'chrony-pin.json', 8192)
require(sha(raw) == PUBLIC_PIN_SHA == pin_result['public_pin_sha256'] and
raw == pin.encoded(actual_pin) and proof == pin_result['public_pin_metadata'],
'actual_unchanged_public_pin_and_original_metadata')
dirs = {}
for path in (pin.ETC, pin.RUN, pin.RUN / 'clients'):
fd = pin.directory(path)
try:
current = pin.stable_directory(os.fstat(fd))
finally:
os.close(fd)
recorded = pin_result['targets'][str(path)]
require(current == {key: recorded[key] for key in current}, 'original_runtime_directory_identity')
dirs[str(path)] = current
return dict(observation=original.expected, public_pin_sha256=PUBLIC_PIN_SHA,
public_pin_metadata=proof, target_directories=dirs)
before = fresh()
os.mkdir(PHASE, mode=0o700, dir_fd=case_fd)
os.fsync(case_fd)
phase_fd = os.open(PHASE, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=case_fd)
own_raw, _ = pin.read_file(Path(__file__).absolute(), 128 * 1024)
binding = dict(profile=PROFILE, original_case=CASE, phase=PHASE, unit=UNIT,
controller_sha256=sha(own_raw), operator_sha256=pin.OPERATOR_SHA,
pin_controller_sha256=PIN_CONTROLLER_SHA, public_pin_sha256=PUBLIC_PIN_SHA,
source_tar_sha256=original.source_sha, manifest_sha256=original.manifest_sha,
artifact=ARTIFACT, original_group_receipt_sha256=GROUP_RECEIPT_SHA,
original_probe_result_sha256=sha(old_raw), pin_result_sha256=PIN_RESULT_SHA)
pin.exclusive(phase_fd, 'intent.json', pin.encoded(dict(binding, once=True, no_retry=True)))
groups = []
for gid in (989, 10001):
try:
group = grp.getgrgid(gid)
groups.append(dict(gid=gid, name=group.gr_name, present=True))
except KeyError:
groups.append(dict(gid=gid, present=False))
boot = original.expected['boot_id']
rows_raw = operator.run(['journalctl', '--unit=' + OLD_UNIT, '--boot=' + uuid.UUID(boot).hex,
'--no-pager', '-n', '12', '-o', 'json'], timeout=10, limit=65536)
rows = [pin.parsed(row) for row in rows_raw.splitlines()]
group_before_exec(old, old_log, rows, groups)
pin.exclusive(phase_fd, 'group-before-exec.json', pin.encoded(dict(
unit=OLD_UNIT, boot_id=boot, original_receipt_sha256=GROUP_RECEIPT_SHA,
original_probe_result_sha256=sha(old_raw), group_resolution=groups,
journal_sha256=sha(rows_raw), pre_exec_group_failure=True)))
pin.exclusive(phase_fd, 'before.json', pin.encoded(dict(binding, witness=before)))
fresh() # Last source/runtime/pin guard before the single executable action.
code = operator.long_command(probe_args(), BASE / PHASE / 'probe.log', 10)
log_raw, _ = pin.read_file(BASE / PHASE / 'probe.log', 16384)
execution = dict(binding, exit_code=code, probe_log_sha256=sha(log_raw))
pin.exclusive(phase_fd, 'execution.json', pin.encoded(execution))
after = fresh()
require(before == after, 'all_original_runtime_boot_pin_source_witnesses_preserved')
pin.exclusive(phase_fd, 'after.json', pin.encoded(dict(binding, witness=after)))
sample = pin.parsed(log_raw) if code == 0 else None
if code == 0:
sample_quality(sample, boot)
links = {}
for name in ('before', 'after'):
raw, _ = pin.read_file(BASE / PHASE / (name + '.json'), 16384)
links[name + '_witness'] = dict(filename=name + '.json', sha256=sha(raw))
result = dict(binding, **links, exit_code=code, sample=sample, probe_log_sha256=sha(log_raw),
source_quality_qualified=code == 0, runtime_boot_pin_source_preserved=True,
clock_authority=False, signed_attestation=False, signer_created=False,
broker_started=False, http_deployed=False)
pin.exclusive(phase_fd, 'result.json', pin.encoded(result))
print(pin.encoded(result).decode('ascii'), end='')
except Exception as error:
if phase_fd >= 0:
pin.exclusive(phase_fd, 'failure.json', pin.encoded(dict(profile=PROFILE, original_case=CASE,
failed=True, exception_type=type(error).__name__, read_original_phase_only=True)))
raise
finally:
if phase_fd >= 0:
os.close(phase_fd)
os.close(case_fd)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('command', choices=('host-probe', 'host-read'))
args = parser.parse_args()
require(sys.flags.isolated == 1 and os.geteuid() == 0 and os.uname().sysname == 'Linux' and
os.uname().machine == 'aarch64', 'isolated_root_linux_aarch64_only')
os.umask(0o077)
pin, guard = load_pin_controller()
if args.command == 'host-read':
host_read(pin)
else:
host_probe(pin, guard)
if __name__ == '__main__':
try:
main()
except Exception:
print(json.dumps(dict(failed=True, read_original_phase_only=True)), flush=True)
raise SystemExit(1)