Files
rust-discovery/deploy/time_attestation_uri_linux_acceptance.py
T
2026-11-14 23:39:41 +01:00

310 lines
19 KiB
Python

#!/usr/bin/env python3
"""Frozen four Linux time-contract units only; no updater service or Apply.
Prepare copies the independently reviewed snapshot locally. Execute is one
root-only Cloud-VM fixture run. After any lost reply read the original case;
never execute it again or substitute another binary/source generation.
"""
import argparse
import base64
import hashlib
import inspect
import json
import os
from pathlib import Path
import stat
import uuid
import windows_msi_runtime_acceptance as support
ROOT = Path(__file__).resolve().parents[1]
ACCESS_ROOT = ROOT.parent / 'rust-discovery'
QUALIFICATION = ROOT / 'devstate/time-attestation-qualification/61dd70ca-3952-435e-9de2-dc2f40fedac7'
STATE = ROOT / 'devstate/time-attestation-uri-linux'
PROFILE = 'rmm.time-attestation.four-uri-contract-units.v1'
RECEIPT_SHA = 'b708000c9ca9ba8da2128003683ed26382d4349833c322663bdd393dd60ad751'
INPUTS_SHA = '989bcd56c96747ce8f2c4020423360ce4a8c9d8057f7c3deccba4e44fbc30c9a'
BINARY_SHA = '4dc10170500912c703f815f8c13f9ac28715afe919ad94020fe713ade8fbafdb'
BINARY_BYTES = 1515168
TESTS = sorted(['tests::foreign_reply_cannot_reenable_an_older_own_challenge', 'tests::signed_roundtrip_uses_entire_rtt_and_strict_deadline_interval', 'tests::discontinuity_invalidates_outstanding_and_requires_new_challenge', 'tests::tampering_wrong_role_nonce_key_and_delays_never_open'])
AGENT_SHA = '0a7ad6eefaefda79ee80b6a41b2df904e9e7f50ae6ebeed01b98c25b6ce5c06f'
HELPER_SHA = '79442727c3ba31cda02a8bc0e67481f14db9f39fc07520a880a90b64f10f1eaf'
def sha(raw):
return hashlib.sha256(raw).hexdigest()
def save_local(path, value):
support.save(path, value)
fd = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try:
os.fsync(fd)
finally:
os.close(fd)
def copy_exact(source, destination, digest, size):
metadata = source.lstat()
support.require(stat.S_ISREG(metadata.st_mode) and metadata.st_nlink == 1
and metadata.st_uid in (0, os.getuid()) and not metadata.st_mode & 0o002
and metadata.st_size == size and size <= 2 * 1024 * 1024, 'bounded regular frozen input')
raw = source.read_bytes()
support.require(sha(raw) == digest, 'frozen snapshot input hash')
destination.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
support.private(destination.parent, True)
fd = os.open(destination, os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW | os.O_WRONLY, 0o600)
with os.fdopen(fd, 'wb') as stream:
stream.write(raw); stream.flush(); os.fsync(stream.fileno())
fd = os.open(destination.parent, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try:
os.fsync(fd)
finally:
os.close(fd)
def verify_case(folder):
value = support.load(folder / 'manifest.json')
support.require(value['profile'] == PROFILE and value['tests'] == TESTS
and value['runner_sha256'] == sha(Path(__file__).read_bytes())
and value['receipt_sha256'] == RECEIPT_SHA and value['binary_sha256'] == BINARY_SHA
and value['binary_bytes'] == BINARY_BYTES, 'same original reviewed manifest')
support.require(sha((folder / 'receipt.json').read_bytes()) == RECEIPT_SHA
and sha((folder / 'inputs.json').read_bytes()) == INPUTS_SHA, 'original qualification provenance')
source = support.load(folder / 'inputs.json')
support.require(len(source) == 4 and source == value['source_files'], 'exact frozen four codec inputs')
for item in source:
path = Path(item['path'])
support.require(not path.is_absolute() and '..' not in path.parts, 'fixed snapshot relative path')
file = folder / 'source' / path
support.private(file)
support.require(file.stat().st_size == item['bytes'] and sha(file.read_bytes()) == item['sha256'], 'retained source freeze')
binary = folder / 'time-contract-tests'; support.private(binary)
support.require(binary.stat().st_size == BINARY_BYTES and sha(binary.read_bytes()) == BINARY_SHA, 'original qualified test bytes')
for path, digest in value['transport_sources'].items():
support.require(sha((ROOT / path).read_bytes()) == digest, 'original strict transport source')
return value
def remote(p):
import base64
import hashlib
import json
import os
from pathlib import Path
import selectors
import signal
import stat
import subprocess
import time
import uuid
def require(ok, message):
if not ok:
raise RuntimeError(message)
def enc(value):
return json.dumps(value, sort_keys=True, separators=(',', ':')).encode()
def create(path, raw, mode=0o600):
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode)
with os.fdopen(fd, 'wb') as stream:
stream.write(raw); stream.flush(); os.fsync(stream.fileno())
fd = os.open(path.parent, os.O_DIRECTORY | os.O_RDONLY | os.O_NOFOLLOW)
try:
os.fsync(fd)
finally:
os.close(fd)
def run(args, timeout=20):
# Keep the owned group leader unreaped until its entire group has been
# cleaned up. Its PID therefore cannot be recycled before killpg.
child = subprocess.Popen(args, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, start_new_session=True,
env={'PATH': '/usr/bin:/bin', 'LC_ALL': 'C', 'HOME': '/nonexistent'})
selector = selectors.DefaultSelector()
buffers = {'stdout': bytearray(), 'stderr': bytearray()}
deadline = time.monotonic() + timeout
try:
for stream, name in ((child.stdout, 'stdout'), (child.stderr, 'stderr')):
os.set_blocking(stream.fileno(), False); selector.register(stream, selectors.EVENT_READ, name)
while True:
require(time.monotonic() < deadline, 'owned native command timeout; read original case only')
for key, _ in selector.select(0.02):
raw = os.read(key.fileobj.fileno(), 4096)
if raw:
buffers[key.data].extend(raw)
require(len(buffers[key.data]) <= 65536, 'bounded native output')
else:
selector.unregister(key.fileobj)
ended = os.waitid(os.P_PID, child.pid, os.WEXITED | os.WNOHANG | os.WNOWAIT)
if ended is not None:
# Child remains a zombie until finally: no PID/group reuse.
os.killpg(child.pid, signal.SIGKILL)
for key in list(selector.get_map().values()):
while True:
try:
raw = os.read(key.fileobj.fileno(), 4096)
except BlockingIOError:
break
if not raw:
break
buffers[key.data].extend(raw)
require(len(buffers[key.data]) <= 65536, 'bounded native output')
code = ended.si_status if ended.si_code == os.CLD_EXITED else -ended.si_status
return code, bytes(buffers['stdout']), bytes(buffers['stderr'])
finally:
os.killpg(child.pid, signal.SIGKILL)
child.wait()
selector.close(); child.stdout.close(); child.stderr.close()
def metadata_identity(value):
return (value.st_dev, value.st_ino, value.st_uid, value.st_gid, value.st_mode,
value.st_nlink, value.st_size, value.st_mtime_ns, value.st_ctime_ns)
def hashed(path, maximum, process_image=False):
fd = os.open(path, os.O_RDONLY | os.O_CLOEXEC | (0 if process_image else os.O_NOFOLLOW))
with os.fdopen(fd, 'rb') as stream:
before = os.fstat(stream.fileno()); require(stat.S_ISREG(before.st_mode) and before.st_size <= maximum, 'bounded witness file')
digest = hashlib.sha256(); total = 0
while True:
raw = stream.read(32768)
if not raw:
break
total += len(raw); require(total <= maximum, 'bounded witness hash'); digest.update(raw)
after = os.fstat(stream.fileno())
require(metadata_identity(before) == metadata_identity(after) and total == before.st_size, 'stable witness hash')
return before, digest.hexdigest()
def witness():
out = {'boot_id': Path('/proc/sys/kernel/random/boot_id').read_text().strip(), 'services': {}, 'files': {}}
release = dict(line.split('=', 1) for line in Path('/etc/os-release').read_text().splitlines() if '=' in line)
require(release.get('ID', '').strip('"') == 'ubuntu' and release.get('VERSION_ID', '').strip('"') == '24.04', 'fixed native Ubuntu24.04')
out['os'] = {'id': 'ubuntu', 'version': '24.04', 'machine': os.uname().machine, 'kernel': os.uname().release}
for name, uid, binary in (
('rmm-agent-remote.service', 993, p['agent_sha256']),
('rmm-helper-cloud.service', 0, p['helper_sha256']),
('rmm-full-maintenance-helper.service', 0, p['helper_sha256'])):
code, stdout, _ = run(['/usr/bin/systemctl', 'show', name, '-p', 'ActiveState', '-p', 'MainPID', '-p', 'UnitFileState', '-p', 'InvocationID'])
require(code == 0, 'existing service read')
service = dict(line.split('=', 1) for line in stdout.decode().splitlines())
pid = int(service['MainPID']); require(service['ActiveState'] == 'active' and pid > 1, 'three existing services active')
proc = Path('/proc', str(pid)); birth = (proc / 'stat').read_text().rsplit(') ', 1)[1].split()[19]
ids = {line.split(':')[0]: line.split()[1:] for line in (proc / 'status').read_text().splitlines() if line.startswith(('Uid:', 'Gid:'))}
metadata, image = hashed(proc / 'exe', 256 * 1024 * 1024, process_image=True)
current = (proc / 'exe').stat()
require(ids['Uid'] == [str(uid)] * 4 and ids['Gid'] == ['984'] * 4 and image == binary
and (metadata.st_dev, metadata.st_ino) == (current.st_dev, current.st_ino)
and (proc / 'stat').read_text().rsplit(') ', 1)[1].split()[19] == birth, 'current generation/account/image')
out['services'][name] = dict(service, birth=birth, image=image, ids=ids)
code, stdout, _ = run(['/usr/bin/systemctl', 'show', 'rmm-apt-helper.service', '-p', 'ActiveState', '-p', 'UnitFileState'])
apt = dict(line.split('=', 1) for line in stdout.decode().splitlines())
require(code == 0 and apt == {'ActiveState': 'inactive', 'UnitFileState': 'disabled'}, 'APT remains closed')
out['apt'] = apt
for name in ('/etc/rmm-remote/helper-general.json', '/etc/rmm-remote/helper-full-maintenance.json',
'/etc/rmm-remote/full-maintenance-helper-proof.json', '/etc/systemd/system/rmm-full-maintenance-helper.service'):
path = Path(name); metadata = path.lstat()
require(not path.is_symlink() and stat.S_ISREG(metadata.st_mode) and metadata.st_uid == 0
and not metadata.st_mode & 0o022 and metadata.st_nlink == 1, 'protected known control')
current, digest = hashed(path, 65536)
require(metadata_identity(current) == metadata_identity(metadata), 'same known control file')
out['files'][name] = [metadata.st_dev, metadata.st_ino, metadata.st_uid, metadata.st_gid, metadata.st_mode,
metadata.st_size, metadata.st_mtime_ns, metadata.st_ctime_ns, digest]
for name in ('/var/lib/rmm-helper/consumption.sqlite', '/var/lib/rmm-helper/executor.lock',
'/var/lib/rmm-agent-remote/journal.sqlite', '/var/lib/rmm-agent-remote/consumption.sqlite'):
path = Path(name); metadata = path.lstat()
require(not path.is_symlink() and stat.S_ISREG(metadata.st_mode) and metadata.st_nlink == 1, 'existing state identity')
out['files'][name] = [metadata.st_dev, metadata.st_ino, metadata.st_uid, metadata.st_gid, metadata.st_mode]
return out
require(os.geteuid() == 0 and os.uname().machine == 'x86_64', 'fixed Cloud Linux root contract fixtures')
ident = str(uuid.UUID(p['id'])); require(ident == p['id'], 'canonical original case')
folder = Path('/var/tmp') / ('rmm-time-attestation-uri-tests-' + ident)
if p['action'] == 'read':
metadata = folder.lstat()
require(stat.S_ISDIR(metadata.st_mode) and not folder.is_symlink() and metadata.st_uid == 0
and stat.S_IMODE(metadata.st_mode) == 0o700, 'own original case only')
result = folder / 'result.json'
return {'read_only': True, 'id': ident, 'entered': (folder / 'entered.json').exists(),
'result': json.loads(result.read_bytes()) if result.exists() else None, 'current_witness': witness()}
require(p['action'] == 'execute', 'one fixed time contract action')
os.umask(0o077); folder.mkdir(mode=0o700)
create(folder / 'intent.json', enc({key: value for key, value in p.items() if key != 'binary_b64'}))
require(len(p['binary_b64']) == 2020224, 'bounded binary transfer')
raw = base64.b64decode(p['binary_b64'], validate=True)
require(len(raw) == 1515168 and hashlib.sha256(raw).hexdigest() == p['binary_sha256'], 'fixed four-unit binary')
binary = folder / 'time-contract-tests'; create(binary, raw, 0o700)
before = witness(); create(folder / 'before.json', enc(before))
command = ['/usr/bin/setpriv', '--reuid=0', '--regid=0', '--clear-groups', '--no-new-privs', str(binary)]
code, stdout, stderr = run(command + ['--list', '--format', 'terse'])
create(folder / 'list.stdout', stdout); create(folder / 'list.stderr', stderr)
require(code == 0 and stderr == b'', 'native list succeeded')
lines = stdout.decode().splitlines()
require(len(lines) == 4 and sorted(line[:-6] for line in lines if line.endswith(': test')) == p['tests'], 'exact four tests before any run')
create(folder / 'entered.json', enc({'id': ident, 'run_once': True, 'time_ns': time.time_ns()}))
code, stdout, stderr = run(command + ['--test-threads=1', '--nocapture'])
create(folder / 'run.stdout', stdout); create(folder / 'run.stderr', stderr)
after = witness(); create(folder / 'after.json', enc(after))
require(before == after and code == 0 and stderr == b''
and b'4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out' in stdout
and all((b'test ' + name.encode() + b' ... ok') in stdout for name in p['tests']), 'four native units and retained target state')
receipt = dict(id=ident, profile=p['profile'], passed=4, ignored=0, filtered=0, execution_uid=0, tests=p['tests'],
before=before, after=after, binary_sha256=p['binary_sha256'], source_receipt_sha256=p['receipt_sha256'],
source_manifest_sha256=p['manifest_sha256'], stdout_sha256=hashlib.sha256(stdout).hexdigest(),
no_rmm_jobs=True, no_apply=True, no_service_control=True)
create(folder / 'result.json', enc(receipt)); return receipt
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('action', choices=('prepare', 'execute', 'read')); parser.add_argument('--case')
args = parser.parse_args(); os.umask(0o077)
STATE.mkdir(mode=0o700, parents=True, exist_ok=True); support.private(STATE, True)
if args.action == 'prepare':
support.require(args.case is None, 'prepare one new local case')
receipt_path = QUALIFICATION / 'receipt.json'; support.private(receipt_path)
support.require(sha(receipt_path.read_bytes()) == RECEIPT_SHA, 'independently reviewed source receipt')
receipt = support.load(receipt_path); original = support.load(QUALIFICATION / 'inputs.json')
support.require(sha((QUALIFICATION / 'inputs.json').read_bytes()) == INPUTS_SHA
and receipt['inputs_sha256'] == INPUTS_SHA and len(original) == 4
and receipt['result'] == 'LOCAL_TIME4_URI_PASS_RELEASE' and receipt['tests'] == 4
and receipt['skips'] == 0 and receipt['filters'] == 0, 'reviewed four-test qualification')
ident = str(uuid.uuid4()); folder = STATE / ident; folder.mkdir(mode=0o700)
copy_exact(receipt_path, folder / 'receipt.json', RECEIPT_SHA, receipt_path.stat().st_size)
copy_exact(QUALIFICATION / 'inputs.json', folder / 'inputs.json', INPUTS_SHA, (QUALIFICATION / 'inputs.json').stat().st_size)
paths = set()
for item in original:
path = Path(item['path'])
support.require(not path.is_absolute() and '..' not in path.parts and str(path) not in paths, 'unique fixed frozen input')
paths.add(str(path)); copy_exact(QUALIFICATION / 'source' / path, folder / 'source' / path, item['sha256'], item['bytes'])
binary = QUALIFICATION / 'qualified-time-tests'
support.require(receipt['artifacts'][0] == dict(path='qualified-time-tests', bytes=BINARY_BYTES, sha256=BINARY_SHA), 'fixed original linked artifact')
copy_exact(binary, folder / 'time-contract-tests', BINARY_SHA, BINARY_BYTES)
transports = {path: sha((ROOT / path).read_bytes()) for path in ('deploy/windows_msi_runtime_acceptance.py', 'deploy/vm_access_check.py')}
save_local(folder / 'manifest.json', dict(id=ident, profile=PROFILE, runner_sha256=sha(Path(__file__).read_bytes()),
receipt_sha256=RECEIPT_SHA, binary_sha256=BINARY_SHA, binary_bytes=BINARY_BYTES,
source_files=original, tests=TESTS, transport_sources=transports))
verify_case(folder)
print(json.dumps({'id': ident, 'prepared_locally': True})); return
support.require(args.case is not None, 'original --case required')
ident = str(uuid.UUID(args.case)); support.require(ident == args.case, 'same canonical original case')
folder = STATE / ident; support.private(folder, True); verify_case(folder)
payload = dict(id=ident, action=args.action, profile=PROFILE, tests=TESTS, binary_sha256=BINARY_SHA,
receipt_sha256=RECEIPT_SHA, manifest_sha256=sha((folder / 'manifest.json').read_bytes()),
agent_sha256=AGENT_SHA, helper_sha256=HELPER_SHA)
if args.action == 'execute':
save_local(folder / 'execute-entered.json', {'once': True, 'no_retry': True})
payload['binary_b64'] = base64.b64encode((folder / 'time-contract-tests').read_bytes()).decode()
code = 'import json\n' + inspect.getsource(remote) + '\nprint(json.dumps(remote(' + repr(payload) + ')))\n'
response = support.transport(support.access_settings(ACCESS_ROOT, 'linux'), 'python3 -', code.encode(), folder)
save_local(folder / (args.action + '-' + str(uuid.uuid4()) + '.json'), response)
if args.action == 'read' and response['result'] is not None:
support.require(response['result']['source_manifest_sha256'] == sha((folder / 'manifest.json').read_bytes())
and response['current_witness'] == response['result']['after'], 'same-case retained native result')
print(json.dumps({'id': ident, 'action': args.action, 'passed': response.get('passed'),
'complete': response.get('result') is not None if args.action == 'read' else True, 'read_only': args.action == 'read'}))
if __name__ == '__main__':
main()