Record native ARM64 time binaries and add create-only public pin bootstrap
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -0,0 +1,374 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create only the public Chrony pin and private runtime directories; no signer/service.
|
||||
|
||||
Run with python3 -I. A failed or uncertain pin phase is never retried. host-read
|
||||
reads the original case and bounded target metadata without querying time.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import stat
|
||||
import struct
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
OPERATOR_SHA = 'ecb72b7cbcc1d036a0f4bd2f0a92a9b80339326068e49edd70d5627226833d61'
|
||||
OPERATOR = Path('/var/lib/rmm-time-source-operators') / OPERATOR_SHA / 'operator.py'
|
||||
CASES = Path('/var/lib/rmm-time-source-bootstrap')
|
||||
ETC = Path('/etc/rmm-time-source')
|
||||
RUN = Path('/run/rmm-time-source')
|
||||
PROFILE = 'rmm.public-chrony-pin.bootstrap.v1'
|
||||
PROVIDER_SHA = '3fd9c7a969a583abf97e29165135a8c1044db5c951ad8bd4dbfee3ee77e7ccdc'
|
||||
PIN_KEYS = {'schema_version', 'daemon_pid', 'daemon_birth_ticks', 'daemon_uid', 'daemon_gid', 'daemon_image_sha256'}
|
||||
|
||||
|
||||
class Denied(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def require(value, reason):
|
||||
if not value:
|
||||
raise Denied(reason)
|
||||
|
||||
|
||||
def encoded(value):
|
||||
return (json.dumps(value, sort_keys=True, separators=(',', ':'), ensure_ascii=True) + '\n').encode('ascii')
|
||||
|
||||
|
||||
def sha(raw):
|
||||
return hashlib.sha256(raw).hexdigest()
|
||||
|
||||
|
||||
def identity(info):
|
||||
return {name: getattr(info, 'st_' + name) for name in
|
||||
('dev', 'ino', 'uid', 'gid', 'mode', 'nlink', 'size', 'mtime_ns', 'ctime_ns')}
|
||||
|
||||
|
||||
def stable_directory(info):
|
||||
return {name: getattr(info, 'st_' + name) for name in ('dev', 'ino', 'uid', 'gid', 'mode')}
|
||||
|
||||
|
||||
def directory(path):
|
||||
require(path.is_absolute(), 'fixed_absolute_directory')
|
||||
fd = os.open('/', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
||||
try:
|
||||
require(os.fstat(fd).st_uid == 0 and os.fstat(fd).st_mode & 0o022 == 0, 'root_directory_protected')
|
||||
for part in path.parts[1:]:
|
||||
require(part not in ('', '.', '..'), 'canonical_directory')
|
||||
next_fd = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=fd)
|
||||
os.close(fd)
|
||||
fd = next_fd
|
||||
item = os.fstat(fd)
|
||||
require(item.st_uid == 0 and item.st_mode & 0o022 == 0, 'root_protected_directory')
|
||||
return fd
|
||||
except BaseException:
|
||||
os.close(fd)
|
||||
raise
|
||||
|
||||
|
||||
def read_file(path, limit=1024 * 1024, mode=0o600):
|
||||
parent = directory(path.parent)
|
||||
try:
|
||||
fd = os.open(path.name, os.O_RDONLY | os.O_NOFOLLOW, dir_fd=parent)
|
||||
try:
|
||||
before = identity(os.fstat(fd))
|
||||
require(stat.S_ISREG(before['mode']) and before['uid'] == before['gid'] == 0 and
|
||||
before['nlink'] == 1 and stat.S_IMODE(before['mode']) == mode and before['size'] <= limit,
|
||||
'bounded_original_root_file')
|
||||
raw = bytearray()
|
||||
while len(raw) <= limit:
|
||||
part = os.read(fd, min(65536, limit + 1 - len(raw)))
|
||||
if not part:
|
||||
break
|
||||
raw.extend(part)
|
||||
raw = bytes(raw)
|
||||
require(len(raw) == before['size'] and len(raw) <= limit and before == identity(os.fstat(fd)) ==
|
||||
identity(os.stat(path.name, dir_fd=parent, follow_symlinks=False)), 'original_file_stable')
|
||||
return raw, before
|
||||
finally:
|
||||
os.close(fd)
|
||||
finally:
|
||||
os.close(parent)
|
||||
|
||||
|
||||
def parsed(raw):
|
||||
def pairs(items):
|
||||
result = {}
|
||||
for key, value in items:
|
||||
require(key not in result, 'duplicate_json_field')
|
||||
result[key] = value
|
||||
return result
|
||||
return json.loads(raw.decode('ascii'), object_pairs_hook=pairs,
|
||||
parse_constant=lambda _: (_ for _ in ()).throw(Denied('finite_json')))
|
||||
|
||||
|
||||
def load_operator():
|
||||
raw, proof = read_file(OPERATOR, 128 * 1024)
|
||||
require(sha(raw) == OPERATOR_SHA, 'reviewed_operator_sha256')
|
||||
# Execute exactly the checked held bytes, avoiding a second import-path read.
|
||||
module = importlib.util.module_from_spec(importlib.util.spec_from_loader('rmm_reviewed_time_operator', loader=None))
|
||||
module.__file__ = str(OPERATOR)
|
||||
exec(compile(raw, str(OPERATOR), 'exec'), module.__dict__)
|
||||
module_guard = lambda: require(read_file(OPERATOR, 128 * 1024) == (raw, proof), 'reviewed_operator_unchanged')
|
||||
return module, module_guard
|
||||
|
||||
|
||||
def case_path(value):
|
||||
require(str(uuid.UUID(value)) == value and uuid.UUID(value).version == 4, 'canonical_original_case')
|
||||
path = CASES / value
|
||||
fd = directory(path)
|
||||
try:
|
||||
require(stat.S_IMODE(os.fstat(fd).st_mode) == 0o700, 'private_original_case')
|
||||
finally:
|
||||
os.close(fd)
|
||||
return path
|
||||
|
||||
|
||||
def observation_key(value):
|
||||
# All process, socket, pidfile, unit, fixed image and runtime metadata are
|
||||
# original witnesses. Never derive a new authority from only a caller PID.
|
||||
keys = ('builder', 'runtime', 'boot_id', 'chrony_observation', 'chrony_binding', 'chrony_image_metadata')
|
||||
require(all(key in value for key in keys), 'complete_original_observation')
|
||||
return parsed(encoded({key: value[key] for key in keys}))
|
||||
|
||||
|
||||
def source_items(items):
|
||||
require(type(items) is list and 12 <= len(items) <= 30, 'bounded_source_item_set')
|
||||
names = set()
|
||||
for item in items:
|
||||
require(type(item) is dict and set(item) == {'path', 'bytes', 'sha256'}, 'exact_source_item_fields')
|
||||
name = item['path']
|
||||
require(type(name) is str and (name in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml',
|
||||
'provenance/Cargo.toml', 'provenance/Cargo.lock',
|
||||
'crates/time-source-linux/examples/operator_probe.rs') or
|
||||
re.fullmatch(r'crates/(?:time-attestation|time-source-linux|time-broker-linux)/(?:Cargo\.toml|src/[a-z_]+\.rs)', name)) and
|
||||
name not in names and type(item['bytes']) is int and 0 < item['bytes'] <= 1024 * 1024 and
|
||||
type(item['sha256']) is str and re.fullmatch('[0-9a-f]{64}', item['sha256']),
|
||||
'closed_source_item_path_hash_size')
|
||||
names.add(name)
|
||||
return names
|
||||
|
||||
|
||||
class Original:
|
||||
def __init__(self, case, operator, operator_guard):
|
||||
self.case, self.operator, self.operator_guard = case, operator, operator_guard
|
||||
self.records = {}
|
||||
for name in ('preflight', 'stage', 'build'):
|
||||
require(not (case / name / 'failure.json').exists(), 'original_phase_failed')
|
||||
raw, proof = read_file(case / name / 'result.json')
|
||||
self.records[name] = (raw, proof)
|
||||
preflight, stage, build = (parsed(self.records[name][0]) for name in ('preflight', 'stage', 'build'))
|
||||
require(build.get('exit_code') == 0 and build.get('runtime_metadata_equal') is True and
|
||||
build.get('native_source_quality_tested') is False and build.get('artifacts') and
|
||||
set(build['artifacts']) == {'rmm-time-broker-linux', 'examples/operator_probe'}, 'successful_original_arm_build')
|
||||
self.expected = observation_key(preflight)
|
||||
require(observation_key(build['before']) == self.expected == observation_key(build['after']),
|
||||
'original_build_chrony_runtime_boot_stable')
|
||||
manifest_raw, _ = read_file(case / 'workspace/manifest.json')
|
||||
manifest = parsed(manifest_raw)
|
||||
source_items(manifest['source_files'])
|
||||
source_items(manifest['package_files'])
|
||||
require(manifest.get('operator_sha256') == OPERATOR_SHA and
|
||||
re.fullmatch('[0-9a-f]{40}', manifest.get('public_commit', '')) and
|
||||
sha(manifest_raw) == stage['manifest_sha256'] == build['staged_manifest_sha256'] and
|
||||
manifest['package_files'] == stage['package_files'] and
|
||||
manifest['public_commit'] == stage['public_commit'], 'original_source_manifest_binding')
|
||||
source = [item for item in manifest['source_files'] if item['path'] == 'crates/time-source-linux/src/native.rs']
|
||||
require(len(source) == 1 and source[0]['sha256'] == PROVIDER_SHA, 'reviewed_provider_permission_fix')
|
||||
tar_raw, _ = read_file(case / 'source.tar', 2 * 1024 * 1024)
|
||||
require(sha(tar_raw) == stage['source_tar_sha256'], 'original_source_archive_hash')
|
||||
self.source_sha = sha(tar_raw)
|
||||
self.manifest_sha = sha(manifest_raw)
|
||||
self.source_records = {case / 'source.tar': read_file(case / 'source.tar', 2 * 1024 * 1024),
|
||||
case / 'workspace/manifest.json': read_file(case / 'workspace/manifest.json')}
|
||||
require(self.source_records[case / 'source.tar'][0] == tar_raw and
|
||||
self.source_records[case / 'workspace/manifest.json'][0] == manifest_raw, 'original_source_read_stable')
|
||||
self.artifacts = {}
|
||||
for name, recorded in build['artifacts'].items():
|
||||
path = case / 'build/artifacts' / Path(name).name
|
||||
raw, proof = read_file(path, 32 * 1024 * 1024, 0o500)
|
||||
actual = {'bytes': len(raw), 'sha256': sha(raw), 'elf_machine': 183}
|
||||
require(raw[:6] == b'\x7fELF\x02\x01' and len(raw) >= 20 and
|
||||
struct.unpack('<H', raw[18:20])[0] == 183 and actual == recorded,
|
||||
'original_aarch64_artifact_binding')
|
||||
self.artifacts[name] = (path, actual, proof)
|
||||
self.fresh()
|
||||
|
||||
def fresh(self):
|
||||
self.operator_guard()
|
||||
for name, (raw, proof) in self.records.items():
|
||||
require(read_file(self.case / name / 'result.json') == (raw, proof), 'original_phase_record_unchanged')
|
||||
for path, record in self.source_records.items():
|
||||
require(read_file(path, 2 * 1024 * 1024) == record, 'original_source_provenance_unchanged')
|
||||
require(self.operator.verify_workspace(self.case)['manifest_sha256'] == self.manifest_sha,
|
||||
'original_workspace_source_bytes')
|
||||
for path, recorded, proof in self.artifacts.values():
|
||||
raw, current = read_file(path, 32 * 1024 * 1024, 0o500)
|
||||
require(current == proof and sha(raw) == recorded['sha256'], 'original_build_artifact_unchanged')
|
||||
value = self.operator.preflight()
|
||||
require(observation_key(value) == self.expected, 'fresh_original_chrony_runtime_boot_binding')
|
||||
pin = value['chrony_observation']
|
||||
require(set(pin) == PIN_KEYS and pin['schema_version'] == 1 and
|
||||
type(pin['daemon_pid']) is int and pin['daemon_pid'] > 1 and
|
||||
type(pin['daemon_birth_ticks']) is int and pin['daemon_birth_ticks'] > 0 and
|
||||
pin['daemon_uid'] == pin['daemon_gid'] == 989 and
|
||||
re.fullmatch('[0-9a-f]{64}', pin['daemon_image_sha256']), 'exact_public_chrony_pin_shape')
|
||||
return pin
|
||||
|
||||
|
||||
def absence(parent, name):
|
||||
try:
|
||||
os.stat(name, dir_fd=parent, follow_symlinks=False)
|
||||
except FileNotFoundError:
|
||||
return
|
||||
raise Denied('create_only_target_already_exists')
|
||||
|
||||
|
||||
def exclusive(parent, name, raw):
|
||||
require(len(raw) <= 16384 and raw.isascii(), 'bounded_ascii_receipt_or_public_pin')
|
||||
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_write')
|
||||
view = view[count:]
|
||||
os.fsync(fd)
|
||||
finally:
|
||||
os.close(fd)
|
||||
os.fsync(parent)
|
||||
|
||||
|
||||
def new_directory(parent, name, mode, gid, original):
|
||||
original.fresh()
|
||||
absence(parent, name)
|
||||
os.mkdir(name, mode=0o700, dir_fd=parent)
|
||||
os.fsync(parent)
|
||||
fd = os.open(name, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=parent)
|
||||
try:
|
||||
original.fresh()
|
||||
os.fchown(fd, 0, gid)
|
||||
original.fresh()
|
||||
os.fchmod(fd, mode)
|
||||
os.fsync(fd)
|
||||
require(stable_directory(os.fstat(fd)) == stable_directory(os.stat(name, dir_fd=parent, follow_symlinks=False)) and
|
||||
os.fstat(fd).st_uid == 0 and os.fstat(fd).st_gid == gid and
|
||||
stat.S_IMODE(os.fstat(fd).st_mode) == mode, 'exact_created_directory')
|
||||
return fd
|
||||
except BaseException:
|
||||
os.close(fd)
|
||||
raise
|
||||
|
||||
|
||||
def targets_metadata():
|
||||
result = {}
|
||||
for path in (ETC, ETC / 'chrony-pin.json', RUN, RUN / 'clients'):
|
||||
parent = -1
|
||||
try:
|
||||
parent = directory(path.parent)
|
||||
result[str(path)] = identity(os.stat(path.name, dir_fd=parent, follow_symlinks=False))
|
||||
except FileNotFoundError:
|
||||
result[str(path)] = None
|
||||
except (Denied, NotADirectoryError):
|
||||
result[str(path)] = {'inspection_denied': True}
|
||||
finally:
|
||||
if parent >= 0:
|
||||
os.close(parent)
|
||||
return result
|
||||
|
||||
|
||||
def host_pin(case, operator, operator_guard):
|
||||
root_etc = root_run = case_fd = phase_fd = etc_fd = run_fd = clients_fd = -1
|
||||
try:
|
||||
case_fd = directory(case)
|
||||
absence(case_fd, 'pin') # Reject an old phase before any new preflight.
|
||||
original = Original(case, operator, operator_guard)
|
||||
root_etc = directory(Path('/etc'))
|
||||
root_run = directory(Path('/run'))
|
||||
original.fresh()
|
||||
os.mkdir('pin', 0o700, dir_fd=case_fd)
|
||||
os.fsync(case_fd)
|
||||
phase_fd = os.open('pin', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=case_fd)
|
||||
original.fresh()
|
||||
intent = {'profile': PROFILE, 'case': case.name, 'operator_sha256': OPERATOR_SHA,
|
||||
'controller_sha256': sha(Path(__file__).read_bytes()), 'source_tar_sha256': original.source_sha,
|
||||
'manifest_sha256': original.manifest_sha, 'requires_targets_initially_absent': True,
|
||||
'chrony_observation': original.expected['chrony_observation'], 'boot_id': original.expected['boot_id']}
|
||||
exclusive(phase_fd, 'intent.json', encoded(intent))
|
||||
absence(root_etc, ETC.name)
|
||||
absence(root_run, RUN.name)
|
||||
# Every target namespace/permission/content write has an independent
|
||||
# fresh actual source observation. Failure leaves exact own partial state.
|
||||
etc_fd = new_directory(root_etc, ETC.name, 0o700, 0, original)
|
||||
pin = original.fresh()
|
||||
exclusive(etc_fd, 'chrony-pin.json', encoded(pin))
|
||||
run_fd = new_directory(root_run, RUN.name, 0o710, 10001, original)
|
||||
clients_fd = new_directory(run_fd, 'clients', 0o710, 989, original)
|
||||
original.fresh()
|
||||
actual, proof = read_file(ETC / 'chrony-pin.json', 8192)
|
||||
require(actual == encoded(pin) and parsed(actual) == pin, 'exact_final_public_pin')
|
||||
for path, fd in ((ETC, etc_fd), (RUN, run_fd), (RUN / 'clients', clients_fd)):
|
||||
require(stable_directory(path.lstat()) == stable_directory(os.fstat(fd)), 'final_created_directory_identity')
|
||||
result = {'profile': PROFILE, 'case': case.name, 'public_pin_sha256': sha(actual),
|
||||
'public_pin_metadata': proof, 'targets': targets_metadata(),
|
||||
'operator_sha256': OPERATOR_SHA, 'source_tar_sha256': original.source_sha,
|
||||
'manifest_sha256': original.manifest_sha, 'targets_initially_absent': True, 'runtime_boot_chrony_preserved': True,
|
||||
'source_quality_sampled': False, 'signer_created': False, 'services_started': False}
|
||||
original.fresh()
|
||||
exclusive(phase_fd, 'result.json', encoded(result))
|
||||
print(encoded({'case': case.name, 'phase': 'pin', 'result': result}).decode('ascii'), end='')
|
||||
except Exception as error:
|
||||
reason = str(error) if isinstance(error, Denied) else 'unexpected_pin_bootstrap_exception'
|
||||
failure = {'profile': PROFILE, 'case': case.name, 'failed': True, 'failure_reason': reason,
|
||||
'read_original_case_only': True, 'partial_objects_are_not_adoptable': True}
|
||||
if phase_fd >= 0:
|
||||
exclusive(phase_fd, 'failure.json', encoded(failure))
|
||||
print(encoded(failure).decode('ascii'), end='')
|
||||
raise SystemExit(1)
|
||||
finally:
|
||||
for fd in (clients_fd, run_fd, etc_fd, phase_fd, case_fd, root_run, root_etc):
|
||||
if fd >= 0:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def host_read(case):
|
||||
records = {}
|
||||
for name in ('intent', 'result', 'failure'):
|
||||
path = case / 'pin' / (name + '.json')
|
||||
try:
|
||||
raw, proof = read_file(path, 16384)
|
||||
records[name] = {'sha256': sha(raw), 'metadata': proof, 'record': parsed(raw)}
|
||||
except FileNotFoundError:
|
||||
records[name] = None
|
||||
# Only finite lstat metadata, no commands, preflight, sockets or source sample.
|
||||
print(encoded({'case': case.name, 'phase': 'pin', 'records': records,
|
||||
'target_metadata': targets_metadata(), 'read_only': True}).decode('ascii'), end='')
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('command', choices=('host-pin', 'host-read'))
|
||||
parser.add_argument('--case', required=True)
|
||||
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)
|
||||
case = case_path(args.case)
|
||||
if args.command == 'host-read':
|
||||
host_read(case)
|
||||
else:
|
||||
operator, operator_guard = load_operator()
|
||||
host_pin(case, operator, operator_guard)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except Exception:
|
||||
print(encoded({'failed': True, 'failure_reason': 'unexpected_pin_bootstrap_exception',
|
||||
'read_original_case_only': True}).decode('ascii'), end='')
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user