Files
rust-discovery/deploy/server_time_source_chrony_generation_bootstrap.py
T
2026-11-15 11:42:32 +01:00

585 lines
32 KiB
Python

#!/usr/bin/env python3
"""Fixed one-time Chrony restart/public-pin CAS; requires explicit prior user approval.
Source-only preparation does not authorize a target call. host-preflight and
host-read never restart or write. Lost reply/failure permits original read only.
"""
import argparse
import ctypes
import errno
import fcntl
import hashlib
import importlib.util
import json
import math
import os
from pathlib import Path
import re
import select
import stat
import struct
import sys
import time
PROFILE = 'rmm.server-time-source.chrony-generation-bootstrap.v1'
CASE = 'c9a94f94-eecd-4c66-933d-c45c1a79c7a8'
BASE = Path('/var/lib/rmm-time-source-bootstrap') / CASE
ORIGINAL = Path('/var/lib/rmm-time-source-bootstrap/5b358997-3abc-43db-945f-f41855b4e3c0')
PIN_CONTROLLER_SHA = 'fac85dd0399009af36f625232daa5b67ab7fda391dbf95229d4237c307b142ca'
PIN_CONTROLLER = Path('/var/lib/rmm-time-source-operators') / PIN_CONTROLLER_SHA / 'pin_bootstrap.py'
OLD_PIN_SHA = '203f63f150026d345614ca53e641f248397166fbdeb1f28318fc5d7763317e0a'
OLD_PEER_RESULT_SHA = '71f212c5886b786f98d3dbe39f8a0adf76769e4f66ee516476db7df31cda413c'
CONFIG_RECEIPT_SHA = '7d5c7566a054ce353733155fbc2b98ef2a4d3f6d758bb85bd0e3ffd31bdeb22e'
CONFIG = {
'/etc/chrony/chrony.conf': (1622, '3d3bb53ddee1a78de319508913c08b34891b66e5e168356f2d9ca217ab85ba68'),
'/etc/chrony/conf.d/ubuntu-nts.conf': (389, '4355f44d20dc1bda2db04ea005910c7742cd244aa6f688a0da75aca3544c157a'),
'/etc/chrony/sources.d/ubuntu-ntp-pools.sources': (1553, 'e5eb19e7e5dd56164698bfd44b24a76d40f517de84245726b953c4a681c8f095'),
'/etc/default/chrony': (412, 'e93b5d3ecd73651a49e1e7c7984e026cd09271554cce23b851022aece3aa1547'),
'/usr/lib/systemd/system/chrony.service': (2003, 'a13aa0e542b57cc75a0b87f2b15785f53e4e86f03dc3db7b572ea056f31c2218'),
}
INCLUDE_DIRS = {'/etc/chrony/conf.d': ['README', 'ubuntu-nts.conf'],
'/etc/chrony/sources.d': ['README', 'ubuntu-ntp-pools.sources'], '/run/chrony-dhcp': None}
INCLUDE_RECEIPT_SHA = 'ee557a17e7188dcf2211cde5df575ad3c101a6412f564eb53dc385a77262c22d'
STARTER_SHA = '48ae26b9a01b359ac6a743c074265e4b19a3ed99bbc4c46953850c75edde8e1c'
DBUS_RECEIPT_SHA = '6ca943b071bb85c8c3f84ace00135e97acea48c6b0e0fcce3550b253aa67b092'
STARTER = '/usr/lib/systemd/scripts/chronyd-starter.sh'
EMPTY_EXEC = ('ExecStartPre', 'ExecStartPost', 'ExecStop', 'ExecStopPost')
MAIN = Path('/etc/apparmor.d/usr.sbin.chronyd')
MAIN_SHA = '214e509af9ab9b713e89b09bf6440c4b4248e97c64d4b0d884d2f869d406e1e2'
LOCAL = Path('/etc/apparmor.d/local/usr.sbin.chronyd')
LOCAL_SHA = '27e3708a34988fb778cbc8cfd892e0f222b8caf927034e472f391aab65349394'
POLICY = Path('/sys/kernel/security/apparmor/policy/profiles/usr.sbin.chronyd.159')
POLICY_SHA = 'a0dd0b80f5e074b3cf0350a618ca98855a3076c7e869d2a80d209b57b3b220fa'
PUBLIC_PIN = Path('/etc/rmm-time-source/chrony-pin.json')
TEMP_PIN = '.chrony-pin-generation-' + CASE + '.new'
ABSENT = tuple(Path(value) for value in ('/etc/rmm-time-source/signer.json',
'/etc/rmm-time-source/time-public-key.json', '/usr/local/libexec/rmm-time-broker',
'/etc/systemd/system/rmm-time-broker.service'))
RECORDS = ('intent.json', 'before.json', 'old-public-pin.bin', 'restart-intent.json',
'restart-exit.json', 'after-restart.json', 'readiness.json', 'cas-intent.json', 'new-public-pin.bin',
'after.json', 'result.json', 'failure.json', 'restart.log')
RECORDS += tuple('readiness-' + str(index).zfill(2) + '.json' for index in range(1, 26))
READINESS_SECONDS = 25
class GuardDenied(RuntimeError):
pass
def require(value, reason):
if not value:
raise GuardDenied(reason)
def failure_tag(error):
if isinstance(error, GuardDenied) and len(error.args) == 1 and type(error.args[0]) is str:
tag = error.args[0]
if len(tag) <= 128 and re.fullmatch(r'[a-z0-9_]+', tag):
return tag
return 'unexpected_generation_exception'
def sha(raw):
return hashlib.sha256(raw).hexdigest()
def load_pin_controller():
fd = os.open('/', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try:
for part in PIN_CONTROLLER.parent.parts[1:]:
info = os.fstat(fd)
require(info.st_uid == 0 and info.st_mode & 0o022 == 0, 'protected_controller_ancestry')
next_fd = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=fd)
os.close(fd)
fd = next_fd
info = os.fstat(fd)
require(info.st_uid == 0 and info.st_mode & 0o022 == 0, 'protected_controller_parent')
file_fd = os.open(PIN_CONTROLLER.name, os.O_RDONLY | os.O_NOFOLLOW, dir_fd=fd)
with os.fdopen(file_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, 'reviewed_root_controller_file')
raw = stream.read(128 * 1024 + 1)
stable = lambda s: tuple(getattr(s, 'st_' + k) for k 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=fd, follow_symlinks=False)) and
len(raw) == before.st_size and sha(raw) == PIN_CONTROLLER_SHA, 'exact_pin_controller_bytes')
finally:
os.close(fd)
pin = importlib.util.module_from_spec(importlib.util.spec_from_loader('rmm_peer_pin_guard', loader=None))
pin.__file__ = str(PIN_CONTROLLER)
exec(compile(raw, str(PIN_CONTROLLER), 'exec'), pin.__dict__)
held = pin.read_file(PIN_CONTROLLER, 128 * 1024)
require(held[0] == raw, 'same_loaded_pin_controller')
return pin, lambda: require(pin.read_file(PIN_CONTROLLER, 128 * 1024) == held, 'pin_controller_unchanged')
def bounded_kernel(path, maximum=262144):
# Fixed securityfs/proc paths, never caller-controlled or returned as bytes.
with path.open('rb') as stream:
raw = stream.read(maximum + 1)
require(len(raw) <= maximum, 'bounded_kernel_policy_metadata')
return raw
def create_record(parent, name, raw):
require(name in RECORDS and len(raw) <= 65536 and raw.isascii(), 'closed_bounded_generation_record')
fd = os.open(name, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600, dir_fd=parent)
try:
view = memoryview(raw)
while view:
count = os.write(fd, view)
require(count > 0, 'complete_create_only_generation_record')
view = view[count:]
os.fsync(fd)
finally:
os.close(fd)
os.fsync(parent)
def acl(fd, name):
try:
value = os.getxattr(fd, name)
require(len(value) <= 256, 'bounded_namespace_acl')
return value
except OSError as error:
require(error.errno == errno.ENODATA, 'acl_exact_absence_or_value')
return None
def include_directories(pin):
directories = {}
for name, expected in INCLUDE_DIRS.items():
if expected is None:
# The reviewed DHCP directory is absent, not an empty directory.
# Hold its protected parent and reject every new leaf, including a
# symlink. Do not create or silently adopt an existing namespace.
path = Path(name)
parent = pin.directory(path.parent)
try:
before = pin.stable_directory(os.fstat(parent))
try:
os.stat(path.name, dir_fd=parent, follow_symlinks=False)
except FileNotFoundError:
pass
else:
raise GuardDenied('reviewed_dhcp_namespace_must_remain_absent')
require(pin.stable_directory(os.fstat(parent)) == before,
'same_protected_absent_dhcp_parent')
directories[name] = dict(present=False, parent=before)
finally:
os.close(parent)
continue
fd = pin.directory(Path(name))
try:
require(sorted(os.listdir(fd)) == expected, 'closed_reviewed_chrony_include_directory')
# Chrony loads *.conf / *.sources; README is a package document,
# not another configuration file. Read only its protected metadata.
readme = os.stat('README', dir_fd=fd, follow_symlinks=False)
require(stat.S_ISREG(readme.st_mode) and readme.st_uid == readme.st_gid == 0
and readme.st_nlink == 1 and readme.st_mode & 0o022 == 0,
'ignored_readme_is_protected_regular_document')
directories[name] = dict(present=True, metadata=pin.stable_directory(os.fstat(fd)),
ignored_readme_metadata=pin.identity(readme))
finally:
os.close(fd)
return directories
def typed_commands(pin, operator):
commands = {}
for name in EMPTY_EXEC + ('ExecStartEx',):
raw = operator.run(['/usr/bin/busctl', '--system', '--json=short', 'get-property',
'org.freedesktop.systemd1', '/org/freedesktop/systemd1/unit/chrony_2eservice',
'org.freedesktop.systemd1.Service', name], timeout=3, limit=8192)
value = pin.parsed(raw)
require(type(value) is dict and set(value) == {'type', 'data'}, 'typed_dbus_command_property_shape')
if name in EMPTY_EXEC:
require(value == {'type': 'a(sasbttttuii)', 'data': []}, 'typed_dbus_exec_list_must_be_empty')
commands[name] = value
continue
rows = value['data']
require(value['type'] == 'a(sasasttttuii)' and type(rows) is list and len(rows) == 1
and type(rows[0]) is list and len(rows[0]) == 10, 'typed_dbus_one_exec_start_ex')
row = rows[0]
require(row[:3] == [STARTER, [STARTER, '-n', '$DAEMON_OPTS'], ['no-setuid']],
'typed_exact_exec_start_path_arguments_no_setuid_flag')
require(all(type(value) is int and 0 <= value < 2 ** 64 for value in row[3:7])
and type(row[7]) is int and 0 <= row[7] < 2 ** 32
and all(type(value) is int and -(2 ** 31) <= value < 2 ** 31 for value in row[8:]),
'typed_exec_start_runtime_fields')
# Timestamps, PID and exit status are runtime fields. The actual process
# generation is bound independently by the complete ecb observation.
commands[name] = dict(type=value['type'], path=row[0], argv=row[1], flags=row[2])
return commands
def configuration(pin, operator):
files = {}
for name, (size, expected) in CONFIG.items():
raw, metadata = pin.read_file(Path(name), 65536, 0o644)
require(len(raw) == size and sha(raw) == expected, 'exact_reviewed_public_configuration')
files[name] = dict(sha256=expected, metadata=metadata)
# The existing systemd starter is held, never executed by this controller.
starter = Path(STARTER)
raw, metadata = pin.read_file(starter, 65536, 0o755)
require(sha(raw) == STARTER_SHA, 'exact_reviewed_public_starter_script')
files[str(starter)] = dict(sha256=STARTER_SHA, metadata=metadata)
directories = include_directories(pin)
fields = ('FragmentPath', 'DropInPaths', 'EnvironmentFiles', 'User', 'Group', 'Restart', 'KillMode')
raw = operator.run(['/usr/bin/systemctl', 'show', '--all', '--property=' + ','.join(fields),
'--', 'chrony.service'], limit=16384)
unit = {}
for line in raw.decode('ascii').splitlines():
key, separator, value = line.partition('=')
require(separator and key in fields and key not in unit, 'unique_complete_loaded_unit_property')
unit[key] = value
require(set(unit) == set(fields) and unit['FragmentPath'] == '/usr/lib/systemd/system/chrony.service'
and unit['DropInPaths'] == '' and unit['EnvironmentFiles'] == '/etc/default/chrony (ignore_errors=yes)'
and unit['User'] == '_chrony' and unit['Group'] == '' and unit['Restart'] == 'no'
and unit['KillMode'] == 'control-group', 'exact_loaded_chrony_unit_properties_no_dropins')
commands = typed_commands(pin, operator)
return dict(files=files, include_directories=directories, unit=unit, commands=commands,
existing_makestep=dict(threshold_seconds=1, updates=3), configuration_changed=False)
def namespace(pin, daemon_pid):
result = {}
for path, expected in ((MAIN, MAIN_SHA), (LOCAL, LOCAL_SHA)):
raw, metadata = pin.read_file(path, 65536, 0o644)
require(sha(raw) == expected, 'reviewed_apparmor_source_unchanged')
result[str(path)] = dict(sha256=expected, metadata=metadata)
require(bounded_kernel(POLICY / 'name', 256).strip() == b'/usr/sbin/chronyd'
and bounded_kernel(POLICY / 'mode', 256).strip() == b'enforce'
and sha(bounded_kernel(POLICY / 'raw_data')) == POLICY_SHA
and bounded_kernel(Path('/proc') / str(daemon_pid) / 'attr/apparmor/current', 4096).strip()
== b'/usr/sbin/chronyd (enforce)', 'actual_same_enforcing_loaded_policy')
result['loaded_policy_sha256'] = POLICY_SHA
for name in ('/run/rmm-time-source', '/run/rmm-time-source/clients'):
fd = pin.directory(Path(name))
try:
metadata = pin.stable_directory(os.fstat(fd))
access = acl(fd, 'system.posix_acl_access')
default = acl(fd, 'system.posix_acl_default')
expected = struct.pack('<I', 2) + b''.join(struct.pack('<HHI', *entry) for entry in
((1, 7, 0xffffffff), (4, 1, 0xffffffff), (8, 1, 989), (16, 1, 0xffffffff), (32, 0, 0xffffffff)))
require(metadata['uid'] == 0 and stat.S_IMODE(metadata['mode']) == 0o710
and metadata['gid'] == (989 if name.endswith('clients') else 10001)
and default is None and access == (None if name.endswith('clients') else expected),
'exact_reviewed_peer_namespace_modes_and_acls')
result[name] = dict(metadata=metadata, access_acl=access.hex() if access is not None else None)
if name.endswith('clients'):
require(not os.listdir(fd), 'no_unknown_client_socket_leftovers')
finally:
os.close(fd)
return result
def no_broker(operator):
for path in ABSENT:
try:
path.lstat()
except FileNotFoundError:
continue
raise GuardDenied('no_broker_or_time_key_may_exist')
raw = operator.run(['/usr/bin/systemctl', 'show', '--all', '--property=LoadState,ActiveState,MainPID',
'--', 'rmm-time-broker.service'], limit=4096)
state = dict(line.split('=', 1) for line in raw.decode('ascii').splitlines())
require(state == {'LoadState': 'not-found', 'ActiveState': 'inactive', 'MainPID': '0'},
'no_loaded_or_active_time_broker')
return state
def tracking(operator, allow_unready=False):
raw = operator.run(['/usr/bin/chronyc', '-n', 'tracking'], timeout=3, limit=8192)
values = {}
for line in raw.decode('ascii').splitlines():
key, separator, value = line.partition(':')
require(separator and key.strip() not in values, 'unique_tracking_field')
values[key.strip()] = value.strip()
leap = values.get('Leap status')
require(leap in ('Normal', 'Insert second', 'Delete second', 'Not synchronised')
and re.fullmatch(r'[0-9]|1[0-6]', values.get('Stratum', '')), 'bounded_tracking_leap_and_stratum')
match = re.fullmatch(r'([0-9]+\.[0-9]+) seconds (?:slow|fast) of NTP time', values.get('System time', ''))
require(match and math.isfinite(float(match[1])), 'finite_tracking_system_offset')
offset = float(match[1])
ready = leap == 'Normal' and 1 <= int(values['Stratum']) <= 15 and offset <= 0.005
require(allow_unready or ready, 'tracking_normal_synchronized_offset_at_most_5ms')
return dict(output_sha256=sha(raw), leap=leap, stratum=int(values['Stratum']), ready=ready,
absolute_system_offset_seconds=offset, existing_makestep_preserved=True,
source_quality_qualified=False)
class Timex(ctypes.Structure):
# Linux asm-generic timex ABI for the fixed 64-bit aarch64/glibc host.
_fields_ = [('modes', ctypes.c_uint), ('offset', ctypes.c_long), ('freq', ctypes.c_long),
('maxerror', ctypes.c_long), ('esterror', ctypes.c_long), ('status', ctypes.c_int),
('constant', ctypes.c_long), ('precision', ctypes.c_long), ('tolerance', ctypes.c_long),
('time_sec', ctypes.c_long), ('time_usec', ctypes.c_long), ('tick', ctypes.c_long),
('ppsfreq', ctypes.c_long), ('jitter', ctypes.c_long), ('shift', ctypes.c_int),
('stabil', ctypes.c_long), ('jitcnt', ctypes.c_long), ('calcnt', ctypes.c_long),
('errcnt', ctypes.c_long), ('stbcnt', ctypes.c_long), ('tai', ctypes.c_int),
('reserved', ctypes.c_int * 11)]
def kernel_query(allow_unready=False):
require(ctypes.sizeof(ctypes.c_long) == 8 and ctypes.sizeof(Timex) == 208
and Timex.status.offset == 40 and Timex.maxerror.offset == 24, 'fixed_readonly_kernel_query_abi')
libc = ctypes.CDLL('libc.so.6', use_errno=True)
libc.adjtimex.argtypes = [ctypes.POINTER(Timex)]
libc.adjtimex.restype = ctypes.c_int
value = Timex() # All zeros, especially modes=0: query only.
require(value.modes == 0, 'kernel_query_never_sets_modes')
state = libc.adjtimex(ctypes.byref(value))
require(state in range(6) and 0 <= value.status < 2 ** 31 and 0 <= value.maxerror < 2 ** 63,
'bounded_native_kernel_query_values')
ready = state in range(5) and value.status & (0x40 | 0x1000) == 0 and value.maxerror <= 500000
require(allow_unready or ready, 'native_kernel_readiness_bounds')
return dict(state=state, status=value.status, maxerror_ns=value.maxerror * 1000,
modes=0, ready=ready, source_quality_qualified=False)
def await_readiness(operator, guard, save, binding):
started = time.monotonic()
deadline = started + READINESS_SECONDS
original_run = operator.run
def bounded_read(args, timeout=15, limit=1024 * 1024):
remaining = deadline - time.monotonic()
require(remaining > 0, 'readiness_read_command_budget_exhausted')
return original_run(args, timeout=min(timeout, remaining), limit=limit)
operator.run = bounded_read
attempts = 0
try:
for index in range(1, 26):
if time.monotonic() >= deadline:
break
# Guard failures are never swallowed as transient synchronization.
guard()
if time.monotonic() >= deadline:
break
observed_tracking = tracking(operator, allow_unready=True)
observed_kernel = kernel_query(allow_unready=True)
guard()
elapsed = time.monotonic() - started
ready = observed_tracking['ready'] and observed_kernel['ready'] and elapsed <= READINESS_SECONDS
save('readiness-' + str(index).zfill(2) + '.json', dict(binding, attempt=index,
elapsed_seconds=elapsed, deadline_seconds=READINESS_SECONDS, ready=ready,
tracking=observed_tracking, kernel=observed_kernel, no_restart=True,
no_pin_write=True, native_source_quality_qualified=False))
attempts = index
if ready:
save('readiness.json', dict(binding, attempts=attempts, elapsed_seconds=elapsed,
deadline_seconds=READINESS_SECONDS, ready=True, tracking=observed_tracking,
kernel=observed_kernel, native_source_quality_qualified=False))
return observed_tracking, observed_kernel
if time.monotonic() >= deadline:
break
time.sleep(max(0, min(1, deadline - time.monotonic())))
save('readiness.json', dict(binding, attempts=attempts, deadline_seconds=READINESS_SECONDS,
ready=False, budget_exhausted=True, no_restart_retry=True, no_pin_write=True,
native_source_quality_qualified=False))
raise GuardDenied('bounded_readonly_reacquisition_not_ready_original_read_only')
finally:
operator.run = original_run
def source_unchanged(pin, original):
original.operator_guard()
for phase, held in original.records.items():
require(pin.read_file(original.case / phase / 'result.json') == held, 'historical_source_phase_unchanged')
for path, held in original.source_records.items():
require(pin.read_file(path, 2 * 1024 * 1024) == held, 'historical_source_provenance_unchanged')
require(original.operator.verify_workspace(original.case)['manifest_sha256'] == original.manifest_sha,
'original_workspace_unchanged')
for path, recorded, metadata in original.artifacts.values():
raw, current = pin.read_file(path, 32 * 1024 * 1024, 0o500)
require(current == metadata and sha(raw) == recorded['sha256'], 'original_artifact_unchanged')
def generation(before, after):
for key in ('builder', 'runtime', 'boot_id', 'chrony_image_metadata'):
require(before[key] == after[key], 'same_boot_application_builder_daemon_image')
old, new = before['chrony_binding'], after['chrony_binding']
require(old['fixed_images'] == new['fixed_images'], 'same_actual_fixed_daemon_and_wrapper_images')
changed_unit = ('MainPID', 'InvocationID')
require({k: v for k, v in old['unit'].items() if k not in changed_unit}
== {k: v for k, v in new['unit'].items() if k not in changed_unit}
and old['unit']['InvocationID'] != new['unit']['InvocationID'], 'one_new_same_configuration_service_invocation')
require(old['socket_kernel_inode'] != new['socket_kernel_inode'], 'new_actual_command_socket')
require(len(new['processes']) == len(old['processes']) == 3
and min(p['birth_ticks'] for p in new['processes']) > max(p['birth_ticks'] for p in old['processes']),
'three_new_actual_process_births')
for key in ('schema_version', 'daemon_uid', 'daemon_gid', 'daemon_image_sha256'):
require(before['chrony_observation'][key] == after['chrony_observation'][key], 'same_pinned_daemon_role_and_image')
require(after['chrony_observation']['daemon_birth_ticks'] != before['chrony_observation']['daemon_birth_ticks'],
'new_actual_daemon_generation')
def host_read(pin):
records = {}
for name in RECORDS:
try:
raw, metadata = pin.read_file(BASE / name, 8 * 1024 * 1024 if name == 'restart.log' else 65536)
records[name] = dict(sha256=sha(raw), metadata=metadata,
record=pin.parsed(raw) if name.endswith('.json') else None)
except FileNotFoundError:
records[name] = None
print(pin.encoded(dict(profile=PROFILE, case=CASE, read_only=True, records=records,
public_pin_metadata=pin.targets_metadata(), no_restart=True, no_source_query=True)).decode(), end='')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('command', choices=('host-preflight', 'host-restart', 'host-read'))
args = parser.parse_args()
require(sys.flags.isolated == 1 and os.geteuid() == 0 and os.uname().machine == 'aarch64',
'isolated_root_server_arm64_only')
os.umask(0o077)
pin, pin_guard = load_pin_controller()
if args.command == 'host-read':
host_read(pin)
return
operator, operator_guard = pin.load_operator()
original = pin.Original(ORIGINAL, operator, operator_guard)
own_path = Path(__file__)
own = pin.read_file(own_path, 128 * 1024)
require(own_path.name == 'chrony_generation_bootstrap.py' and own_path.parent.name == sha(own[0]),
'fixed_protected_content_addressed_controller')
held_pin = pin.read_file(PUBLIC_PIN, 8192)
require(len(held_pin[0]) == 194 and sha(held_pin[0]) == OLD_PIN_SHA
and pin.parsed(held_pin[0]) == original.fresh(), 'exact_old_public_pin_and_generation')
old_peer = pin.read_file(ORIGINAL / 'probe-peer-acceptance-v1/result.json', 65536)
require(sha(old_peer[0]) == OLD_PEER_RESULT_SHA and pin.parsed(old_peer[0])['exit_code'] == 1
and pin.parsed(old_peer[0])['source_quality_qualified'] is False, 'retained_failed_peer_origin')
old_pids = [os.pidfd_open(value['pid']) for value in original.expected['chrony_binding']['processes']]
phase_fd = etc_fd = -1
try:
before = operator.preflight()
require(pin.observation_key(before) == original.expected, 'fresh_original_before_restart')
config = configuration(pin, operator)
peer = namespace(pin, before['chrony_observation']['daemon_pid'])
absent = no_broker(operator)
quality = tracking(operator)
kernel = kernel_query()
binding = dict(profile=PROFILE, case=CASE, original_case=ORIGINAL.name,
controller_sha256=sha(own[0]),
pin_controller_sha256=PIN_CONTROLLER_SHA, operator_sha256=pin.OPERATOR_SHA,
old_public_pin_sha256=OLD_PIN_SHA, failed_peer_result_sha256=OLD_PEER_RESULT_SHA,
config_receipt_sha256=CONFIG_RECEIPT_SHA, source_tar_sha256=original.source_sha,
manifest_sha256=original.manifest_sha, loaded_policy_sha256=POLICY_SHA,
include_receipt_sha256=INCLUDE_RECEIPT_SHA, dbus_abi_receipt_sha256=DBUS_RECEIPT_SHA,
starter_sha256=STARTER_SHA)
if args.command == 'host-preflight':
print(pin.encoded(dict(binding, read_only=True, proposed_restart_once=True,
existing_makestep_can_step_on_start=True, before=before, configuration=config,
namespace=peer, broker_absent=absent, tracking=quality, kernel=kernel,
user_approval_required=True)).decode(), end='')
return
parent = pin.directory(BASE.parent)
try:
pin.absence(parent, CASE)
original.fresh()
os.mkdir(CASE, 0o700, dir_fd=parent)
os.fsync(parent)
finally:
os.close(parent)
phase_fd = pin.directory(BASE)
save = lambda name, value: create_record(phase_fd, name, pin.encoded(value))
save('intent.json', dict(binding, restart_once=True, old_pin_cas_only=True,
existing_makestep_can_step_on_start=True, no_automatic_retry=True))
save('before.json', dict(binding, actual=before, configuration=config, namespace=peer,
broker_absent=absent, tracking=quality, kernel=kernel, public_pin_metadata=held_pin[1]))
pin.exclusive(phase_fd, 'old-public-pin.bin', held_pin[0])
def stable_common():
pin_guard()
require(pin.read_file(own_path, 128 * 1024) == own, 'generation_controller_unchanged')
source_unchanged(pin, original)
require(configuration(pin, operator) == config and no_broker(operator) == absent,
'same_configuration_no_broker_or_keys')
require(pin.read_file(ORIGINAL / 'probe-peer-acceptance-v1/result.json', 65536) == old_peer,
'failed_peer_origin_unchanged')
stable_common()
original.fresh()
etc_fd = pin.directory(PUBLIC_PIN.parent)
fcntl.flock(etc_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
require(pin.read_file(PUBLIC_PIN, 8192) == held_pin, 'original_pin_cas_precondition')
require(namespace(pin, before['chrony_observation']['daemon_pid']) == peer,
'original_actual_loaded_policy_namespace_before_restart')
save('restart-intent.json', dict(binding, argv=['/usr/bin/systemctl', 'restart', '--', 'chrony.service']))
code = operator.long_command(['/usr/bin/systemctl', 'restart', '--', 'chrony.service'], BASE / 'restart.log', 15)
save('restart-exit.json', dict(binding, exit_code=code))
require(code == 0, 'restart_completed_once_otherwise_original_read_only')
after = operator.preflight()
generation(before, after)
save('after-restart.json', dict(binding, actual=after, native_source_quality_qualified=False))
for fd in old_pids:
poll = select.poll()
poll.register(fd, select.POLLIN)
require(bool(poll.poll(0)), 'all_held_original_chrony_processes_exited')
stable_common()
require(namespace(pin, after['chrony_observation']['daemon_pid']) == peer,
'same_loaded_enforcing_policy_and_peer_namespace')
def same_new_generation():
stable_common()
require(pin.observation_key(operator.preflight()) == pin.observation_key(after)
and namespace(pin, after['chrony_observation']['daemon_pid']) == peer
and pin.read_file(PUBLIC_PIN, 8192) == held_pin,
'same_new_generation_boot_configuration_namespace_old_pin_during_readiness')
after_tracking, after_kernel = await_readiness(operator, same_new_generation, save, binding)
new_raw = pin.encoded(after['chrony_observation'])
require(set(after['chrony_observation']) == pin.PIN_KEYS and len(new_raw) <= 256,
'bounded_actual_six_field_new_public_pin')
pin.absence(etc_fd, TEMP_PIN)
require(pin.read_file(PUBLIC_PIN, 8192) == held_pin, 'old_public_pin_exact_cas_before_temp')
save('cas-intent.json', dict(binding, new_public_pin_sha256=sha(new_raw),
new_public_pin_bytes=len(new_raw), actual_new_observation=after['chrony_observation']))
pin.exclusive(etc_fd, TEMP_PIN, new_raw)
temp = pin.read_file(PUBLIC_PIN.parent / TEMP_PIN, 256)
stable_common()
require(pin.observation_key(operator.preflight()) == pin.observation_key(after)
and pin.read_file(PUBLIC_PIN, 8192) == held_pin
and temp == pin.read_file(PUBLIC_PIN.parent / TEMP_PIN, 256), 'held_new_generation_and_atomic_pin_cas')
os.replace(TEMP_PIN, PUBLIC_PIN.name, src_dir_fd=etc_fd, dst_dir_fd=etc_fd)
os.fsync(etc_fd)
actual_pin = pin.read_file(PUBLIC_PIN, 256)
require(actual_pin[0] == new_raw
and {k: v for k, v in actual_pin[1].items() if k != 'ctime_ns'}
== {k: v for k, v in temp[1].items() if k != 'ctime_ns'}
and actual_pin[1]['ctime_ns'] >= temp[1]['ctime_ns'],
'same_fresh_root_public_pin_after_atomic_replace')
pin.exclusive(phase_fd, 'new-public-pin.bin', new_raw)
stable_common()
require(pin.observation_key(operator.preflight()) == pin.observation_key(after)
and namespace(pin, after['chrony_observation']['daemon_pid']) == peer,
'final_actual_generation_runtime_and_policy_stable')
save('after.json', dict(binding, actual=after, tracking=after_tracking, kernel=after_kernel,
public_pin_sha256=sha(new_raw), public_pin_metadata=actual_pin[1], configuration=config, namespace=peer))
save('result.json', dict(binding, new_public_pin_sha256=sha(new_raw), new_public_pin_metadata=actual_pin[1],
new_chrony_observation=after['chrony_observation'], restart_exit_code=0,
runtime_boot_configuration_images_preserved=True, new_generation_pinned=True,
source_quality_qualified=False, native_source_sampled=False, signer_created=False,
signed_attestation=False, broker_started=False, no_rmm_jobs=True))
print(pin.encoded(dict(binding, completed=True, source_quality_qualified=False)).decode(), end='')
except Exception as error:
if phase_fd >= 0:
create_record(phase_fd, 'failure.json', pin.encoded(dict(profile=PROFILE, case=CASE,
failed=True, fixed_tag=failure_tag(error), state_unknown=True,
read_original_case_only=True, no_automatic_rollback=True)))
raise
finally:
for fd in old_pids + [phase_fd, etc_fd]:
if fd >= 0:
os.close(fd)
if __name__ == '__main__':
try:
main()
except Exception as error:
print(json.dumps(dict(profile=PROFILE, case=CASE, failed=True,
fixed_tag=failure_tag(error), read_original_case_only=True)))
raise SystemExit(1)