313 lines
21 KiB
Python
313 lines
21 KiB
Python
#!/usr/bin/env python3
|
|
"""Frozen three Windows clock guards on Linux only; ten other cases filtered; 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/windows-signed-clock/qualification-763baf69-10b1-4307-9c99-6942c0f8945f'
|
|
STATE = ROOT / 'devstate/windows-signed-clock-native-linux'
|
|
PROFILE = 'rmm.windows-signed-clock.three-linux-guards.v1'
|
|
RECEIPT_SHA = 'e013738caed2d8e02b98ffd1b41e47a80840c44d3dbf895e9a3952b4311ce927'
|
|
INPUTS_SHA = 'eb4609c5fca6a32ddb8ebf364775efdb59cd89f99a24b3b731212da08eac6a6e'
|
|
BINARY_SHA = '0896a3098f493b67b48e774de5cfe9bca682d2a19ff10971c62c9c7b9681ec14'
|
|
BINARY_BYTES = 3374576
|
|
TESTS = sorted(['signed_clock::tests::bracket_uses_actual_capture_width_and_refuses_unqualified_or_backward_counters', 'signed_clock::tests::job_key_cannot_be_reintroduced_as_a_time_key_with_different_hex_case', 'signed_clock::tests::retained_process_fence_rejects_old_challenge_and_boot_substitution'])
|
|
ALL_TESTS = sorted(['event_log::tests::job_mapping_is_only_fixed_unprivileged_live_logs_with_exact_limits', 'event_log::tests::only_fixed_source_window_and_finite_limits_are_queryable', 'event_log::tests::serialized_byte_budget_includes_xml_escaping_and_metadata', 'journal::tests::bounded_existing_journal_preserves_identity_sequence_and_unacked_bytes', 'journal::tests::durable_reopen_ack_identity_and_single_process', 'journal::tests::migrates_original_passive_schema_without_rewriting_pending_envelopes', 'journal::tests::unsupported_page_size_is_refused_without_mutating_existing_data', 'package::tests::exit_code_never_substitutes_for_independent_postconditions_or_reboot_authority', 'package::tests::late_exit_or_postcondition_cannot_retroactively_succeed_or_retry', 'package::tests::upgrade_and_noop_need_independent_exact_products_family_and_payload', 'signed_clock::tests::bracket_uses_actual_capture_width_and_refuses_unqualified_or_backward_counters', 'signed_clock::tests::job_key_cannot_be_reintroduced_as_a_time_key_with_different_hex_case', 'signed_clock::tests::retained_process_fence_rejects_old_challenge_and_boot_substitution'])
|
|
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 <= 4 * 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['all_listed_tests'] == ALL_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) == 184 and source == value['source_files'], 'exact frozen 184 compiler 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 / 'windows-clock-guard-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-windows-clock-guards-linux-' + 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']) == 4499436, 'bounded binary transfer')
|
|
raw = base64.b64decode(p['binary_b64'], validate=True)
|
|
require(len(raw) == 3374576 and hashlib.sha256(raw).hexdigest() == p['binary_sha256'], 'fixed three-guard library binary')
|
|
binary = folder / 'windows-clock-guard-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) == 13 and sorted(line[:-6] for line in lines if line.endswith(': test')) == p['all_listed_tests'], 'exact full thirteen-test list before three targeted tests')
|
|
create(folder / 'entered.json', enc({'id': ident, 'run_once': True, 'time_ns': time.time_ns()}))
|
|
code, stdout, stderr = run(command + ['signed_clock::tests::', '--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'3 passed; 0 failed; 0 ignored; 0 measured; 10 filtered out' in stdout
|
|
and all((b'test ' + name.encode() + b' ... ok') in stdout for name in p['tests']), 'three native guards with ten filtered and retained target state')
|
|
receipt = dict(id=ident, profile=p['profile'], passed=3, ignored=0, filtered=10, listed=13, 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) == 184
|
|
and receipt['kind'] == 'windows-signed-clock-source-qualification-v2'
|
|
and receipt['all_source_inputs_preserved'] is True
|
|
and receipt['independent_review']['source_sha256'] == '6b66612b7a53377c91bc8e41864ac8fb9393ad40b0a6a98aa6ce746fce091ddb'
|
|
and next(x for x in receipt['results'] if x['name'] == 'linux-three-targeted') == dict(name='linux-three-targeted',exit_code=0,passed=3,failed=0,ignored=0,filtered_other_crate_tests=10,tests=TESTS), 'reviewed three-guard 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-linux-tests'
|
|
support.require(receipt['artifacts'][0] == dict(name='qualified-linux-tests', platform='linux', bytes=BINARY_BYTES, sha256=BINARY_SHA), 'fixed original linked artifact')
|
|
copy_exact(binary, folder / 'windows-clock-guard-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, all_listed_tests=ALL_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, all_listed_tests=ALL_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 / 'windows-clock-guard-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()
|