#!/usr/bin/env python3 """Fixed deny-only Core units: local build, Linux VM FIRST, then Windows VM. Separate from historical SharedCore profiles. No RMM jobs, daemon replacement, enrollment, server access or product-state writes. Lost replies allow readback only. Windows lists are expected until the real Windows process emits them. """ import argparse import base64 import hashlib import json import os from pathlib import Path import re import shutil import stat import subprocess import uuid import remote_client_bootstrap as linux import vm_mvp_acceptance as contract from windows import client_bootstrap as windows ROOT = Path(__file__).resolve().parents[1] STATE = ROOT / 'devstate/core-no-start' IMAGE = 'rust-discovery-durable-final-build:local' REMOTE = '/var/lib/rmm-core-no-start-units' COUNTS = {'linux': {'contracts': 7, 'helper-reconcile': 26, 'reconcile': 26, 'linux-helper': 21}, 'windows': {'contracts': 6, 'helper-reconcile': 26, 'reconcile': 26}} CORE = {'auth.rs': '348db410002e158cd1ec1af0200305ad52f9da5d5092b418bf2e28b83b477b52', 'ledger.rs': '119a341b8d4050fe99b973be0e7b5866c5d78b32fefc1cd5a741872ff34f022b', 'helper_reconcile.rs': '29ee5ba342b502e792894d2aca0662a55f4fecf35831ac2212adf7880cacbc1f'} require = contract.require def sha(path): return hashlib.sha256(Path(path).read_bytes()).hexdigest() def save(path, value): path.parent.mkdir(parents=True, mode=0o700, exist_ok=True) contract.write_private(path, value) def source_files(): paths = [ROOT / p for p in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', 'deploy/core_no_start_acceptance.py', 'deploy/remote_client_bootstrap.py', 'deploy/vm_access_check.py', 'deploy/vm_mvp_acceptance.py', 'deploy/windows/client_bootstrap.py', 'tools/apt-transaction/apt_transaction.py', 'tools/apt-transaction/hook', 'deploy/linux/rmm-apt-helper.service', 'tests/apt_transaction/fixtures/planning-canonical.json')] paths += list((ROOT / 'crates').glob('*/Cargo.toml')) for crate in ('agent-core', 'agent-linux', 'privileged-helper', 'domain', 'protocol'): paths += list((ROOT / 'crates' / crate).rglob('*.rs')) paths += list((ROOT / 'proto').glob('*.proto')) result = {str(p.relative_to(ROOT)): sha(p) for p in sorted(set(paths))} require(all(result['crates/agent-core/src/' + p] == h for p, h in CORE.items()), 'reviewed frozen Core required') return result def command(args, timeout=300): done = subprocess.run(args, capture_output=True, timeout=timeout) require(done.returncode == 0, 'local command failed: ' + done.stderr.decode(errors='replace')[-2500:]) return done.stdout def names(raw): return sorted(re.findall(r'^(.+): test\r?$', raw, re.M)) def build(): os.umask(0o077) folder = STATE / ('build-' + str(uuid.uuid4())) folder.mkdir(parents=True, mode=0o700) before = source_files() save(folder / 'source-before.json', before) image = command(['docker', 'image', 'inspect', '--format', '{{.Id}}', IMAGE]).decode().strip() require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable local image') target = STATE / 'target' target.mkdir(mode=0o700, exist_ok=True) common = ['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=512m', '-v', str(ROOT) + ':/src:ro', '-v', str(target) + ':/target', '-e', 'CARGO_TARGET_DIR=/target', '--entrypoint', 'cargo', image] artifacts = {'linux': {}, 'windows': {}} for platform in COUNTS: options = ['--manifest-path', '/src/Cargo.toml', '--locked', '--offline'] packages = ['-p', 'rmm-agent-core'] if platform == 'windows': options += ['--target', 'x86_64-pc-windows-gnu'] else: packages += ['-p', 'rmm-privileged-helper'] command(common + ['clippy'] + options + packages + ['--all-targets', '--', '-D', 'warnings']) core_features = ['--features', 'openssl-verifier'] if platform == 'linux' else [] raw = command(common + ['test'] + options + ['-p', 'rmm-agent-core'] + core_features + ['--tests', '--release', '--no-run', '--message-format=json']) if platform == 'linux': raw += command(common + ['test'] + options + ['-p', 'rmm-privileged-helper', '--test', 'reconcile', '--release', '--no-run', '--message-format=json']) for line in raw.splitlines(): try: value = json.loads(line) except ValueError: continue if value.get('reason') != 'compiler-artifact' or not value.get('executable') or value.get('target', {}).get('kind') != ['test']: continue name = value['target']['name'] if 'rmm-privileged-helper' in value['package_id']: kind = 'linux-helper' else: kind = {'contracts': 'contracts', 'helper_reconcile': 'helper-reconcile', 'reconcile': 'reconcile'}.get(name) require(kind in COUNTS[platform] and kind not in artifacts[platform], 'exact distinct test artifact') original = target / Path(value['executable']).relative_to('/target') destination = folder / platform destination.mkdir(mode=0o700, exist_ok=True) linked = destination / (kind + '.linked') copy = destination / (kind + ('.exe' if platform == 'windows' else '')) for path in (linked, copy): shutil.copyfile(original, path) path.chmod(0o600) linked_hash = sha(linked) strip = 'x86_64-w64-mingw32-strip' if platform == 'windows' else 'strip' command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', str(destination.resolve()) + ':/artifacts', '--entrypoint', strip, image, '--strip-debug', '/artifacts/' + copy.name]) require(sha(original) == sha(linked) == linked_hash, 'preserved original test link') binary = copy.read_bytes() if platform == 'linux': require(binary[:6] == b'\x7fELF\x02\x01' and binary[18:20] == b'\x3e\x00', 'Linux x64 ELF') else: offset = int.from_bytes(binary[60:64], 'little') require(binary[:2] == b'MZ' and binary[offset:offset + 6] == b'PE\0\0\x64\x86', 'Windows x64 PE') artifacts[platform][kind] = dict(path=str(copy.resolve()), sha256=sha(copy), bytes=copy.stat().st_size, original_link_path=str(linked.resolve()), original_link_sha256=linked_hash, debug_stripped_copy=True) require(set(artifacts[platform]) == set(COUNTS[platform]), 'all fixed platform artifacts') expected = {'linux': {}, 'windows': {}} for kind, item in artifacts['linux'].items(): raw = command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,exec,size=128m', '-v', item['path'] + ':/unit:ro', '--entrypoint', 'python3', image, '-c', 'import shutil,os,subprocess;shutil.copyfile("/unit","/tmp/unit");os.chmod("/tmp/unit",0o500);' 'subprocess.run(["/tmp/unit","--list","--format","terse"],check=True);' 'subprocess.run(["/tmp/unit","--test-threads=1"],check=True)']).decode() save(folder / ('local-linux-' + kind + '.json'), dict(stdout=raw)) expected['linux'][kind] = names(raw) require(len(expected['linux'][kind]) == COUNTS['linux'][kind], 'actual Linux artifact list count') require(re.search(r'test result: ok\. ' + str(COUNTS['linux'][kind]) + r' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', raw), 'all local Linux units execute') unix_only = 'openssl_and_portable_ed25519_agree_on_current_keys_bytes_and_tampering' require(unix_only in expected['linux']['contracts'], 'explicit Unix-only parity test') for kind in artifacts['windows']: expected['windows'][kind] = [n for n in expected['linux'][kind] if n != unix_only] require(len(expected['windows'][kind]) == COUNTS['windows'][kind], 'expected Windows compile-profile count') require(source_files() == before, 'identical sources and runner after build') manifest = dict(schema_version=1, profile='deny-only-no-start-v1', source_files=before, source_before_after_equal=True, artifacts=artifacts, test_names=expected, test_counts=COUNTS, linux_lists_executed_locally=True, linux_openssl_verifier=True, windows_lists_expected_until_native=True, local_linux_tests_passed=sum(COUNTS['linux'].values()), both_target_clippy_D_warnings=True, build_image_id=image, build_libc=command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '--entrypoint', 'ldd', image, '--version']).decode().splitlines()[0], native_linux_vm_executed=False, native_windows_vm_executed=False, rmm_jobs=False, product_state_writes=False) save(folder / 'manifest.json', manifest) print(json.dumps(dict(manifest=str((folder / 'manifest.json').resolve()), linux_tests=80, windows_tests_expected=58, native_executed=False))) def verified(path): value = contract.protected_json(path) require(value['schema_version'] == 1 and value['profile'] == 'deny-only-no-start-v1' and value['source_files'] == source_files() and value['source_before_after_equal'] is True and value['test_counts'] == COUNTS, 'frozen source/runner/profile binding') for platform, entries in value['artifacts'].items(): require(set(entries) == set(COUNTS[platform]), 'fixed artifacts') for kind, item in entries.items(): require(Path(item['path']) == path.resolve().parent / platform / (kind + ('.exe' if platform == 'windows' else '')) and Path(item['original_link_path']) == path.resolve().parent / platform / (kind + '.linked'), 'fixed unit artifact paths') for label, expected in [('path', item['sha256']), ('original_link_path', item['original_link_sha256'])]: file = Path(item[label]) require(not file.is_symlink() and file.stat().st_uid == os.getuid() and stat.S_IMODE(file.stat().st_mode) == 0o600 and sha(file) == expected, 'private exact original/stripped artifact') require(item['bytes'] == Path(item['path']).stat().st_size and len(value['test_names'][platform][kind]) == COUNTS[platform][kind], 'artifact bytes/list') return value def linux_case(p): import resource import stat require(os.geteuid() == 0 and os.uname().machine == 'x86_64', 'Linux x64 bootstrap unit qualification') ident = str(uuid.UUID(p['id'])) require(ident == p['id'] and p['action'] in ('prepare', 'run', 'read') and set(p['artifacts']) == {'contracts', 'helper-reconcile', 'reconcile', 'linux-helper'}, 'fixed native case') root = Path('/var/lib/rmm-core-no-start-units') def safe(path, directory): s = path.lstat() require(s.st_uid == 0 and s.st_mode & 0o077 == 0 and not stat.S_ISLNK(s.st_mode) and (stat.S_ISDIR(s.st_mode) if directory else stat.S_ISREG(s.st_mode)), 'private fixed unit path') def write(path, value): fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600) with os.fdopen(fd, 'w') as f: json.dump(value, f); f.flush(); os.fsync(f.fileno()) if not root.exists(): root.mkdir(mode=0o700) safe(root, True); case = root / ident if p['action'] == 'prepare': case.mkdir(mode=0o700); write(case / 'intent.json', p); return dict(prepared=True) safe(case, True); safe(case / 'intent.json', False) intent = json.loads((case / 'intent.json').read_bytes()) require(all(p[k] == intent[k] for k in ('id', 'manifest_sha256', 'artifacts', 'names')), 'same native source/artifact intent') if p['action'] == 'read': safe(case / 'result.json', False); return json.loads((case / 'result.json').read_bytes()) write(case / 'started.json', dict(start_once=True, manifest_sha256=p['manifest_sha256'])) tmp = case / 'tmp'; tmp.mkdir(mode=0o700) env = {'PATH': '/usr/bin:/bin', 'LANG': 'C.UTF-8', 'TMPDIR': str(tmp), 'RUST_BACKTRACE': '0'} require(subprocess.run(['/usr/bin/unshare', '--net', '--', '/usr/bin/true'], env=env, capture_output=True, timeout=3).returncode == 0, 'network isolation') def limits(): resource.setrlimit(resource.RLIMIT_CORE, (0, 0)); resource.setrlimit(resource.RLIMIT_FSIZE, (1048576, 1048576)); resource.setrlimit(resource.RLIMIT_CPU, (15, 15)) reports = {} for kind, item in p['artifacts'].items(): binary = case / kind; safe(binary, False) require(hashlib.sha256(binary.read_bytes()).hexdigest() == item['sha256'], 'pinned Linux unit bytes') binary.chmod(0o500) listed = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--list', '--format', 'terse'], env=env, capture_output=True, timeout=5) require(listed.returncode == 0 and len(listed.stdout) <= 65536, 'bounded native listing') actual = sorted(line[:-6] for line in listed.stdout.decode().splitlines() if line.endswith(': test')) require(actual == p['names'][kind], 'exact original unit names before execution') with (case / (kind + '-stdout.bin')).open('xb') as out, (case / (kind + '-stderr.bin')).open('xb') as err: os.chmod(out.name, 0o600); os.chmod(err.name, 0o600); timed = False try: code = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--test-threads=1'], env=env, stdin=subprocess.DEVNULL, stdout=out, stderr=err, timeout=25, preexec_fn=limits).returncode except subprocess.TimeoutExpired: timed = True; code = None output = [(case / (kind + '-' + label + '.bin')).read_bytes() for label in ('stdout', 'stderr')] require(all(len(v) <= 1048576 for v in output), 'bounded unit output') reports[kind] = dict(sha256=item['sha256'], names=actual, exit_code=code, timed_out=timed, stdout_b64=base64.b64encode(output[0]).decode(), stderr_b64=base64.b64encode(output[1]).decode()) write(case / (kind + '-result.json'), reports[kind]) result = dict(platform='linux', manifest_sha256=p['manifest_sha256'], reports=reports, libc=subprocess.run(['/usr/bin/ldd', '--version'], capture_output=True, check=True, timeout=3).stdout.decode().splitlines()[0], product_state_writes=False) write(case / 'result.json', result) return result WINDOWS = r''' $ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json) if([string]([guid]$p.id)-cne$p.id-or$p.action-notin@('run','read')-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.linux_proof_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed Linux-first unit case'} $root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('core-no-start-'+$p.id) function Safe($path,$dir){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$dir-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Private unit type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Unit owner'};$r=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$r.Count){throw 'Empty ACL'};foreach($e in $r){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Broad unit ACL'}}} function WriteNew($path,$v){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($v|ConvertTo-Json -Depth 12 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} Safe $root $true if($p.action-eq'read'){Safe $case $true;$i=Get-Content -LiteralPath (Join-Path $case 'intent.json') -Raw|ConvertFrom-Json;if($i.manifest_sha256-cne$p.manifest_sha256-or$i.linux_proof_sha256-cne$p.linux_proof_sha256){throw 'Original case'};Safe (Join-Path $case 'result.json') $false;Get-Content -LiteralPath (Join-Path $case 'result.json') -Raw;exit 0} if(Test-Path -LiteralPath $case){throw 'Existing native intent'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p function RunUnit($exe,$arguments){$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=$exe;$s.Arguments=$arguments;$s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$c=New-Object Diagnostics.Process;$c.StartInfo=$s;if(!$c.Start()){throw 'Unit start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$t=$false;if(!$c.WaitForExit(25000)){$t=$true;$c.Kill();$c.WaitForExit()};if($o.Result.Length-gt1048576-or$e.Result.Length-gt1048576){throw 'Unit output bound'};return @{exit_code=$c.ExitCode;timed_out=$t;stdout_b64=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($o.Result));stderr_b64=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($e.Result))}}finally{$c.Dispose()}} WriteNew (Join-Path $case 'started.json') @{start_once=$true;manifest_sha256=$p.manifest_sha256};$reports=@{} foreach($kind in @('contracts','helper-reconcile','reconcile')){$a=$p.artifacts.$kind;if($a.sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed unit hash'};$folder=Join-Path $root $a.sha256;Safe $folder $true;$exe=Join-Path $folder ('windows-core-'+$kind+'.exe');Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$a.sha256){throw 'Exact unit bytes'};$list=RunUnit $exe '--list --format terse';if($list.exit_code-ne0-or$list.timed_out){throw 'Native unit listing'};$text=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($list.stdout_b64));$actual=@($text-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''}|Sort-Object);if(($actual-join"`n")-cne(@($p.names.$kind|Sort-Object)-join"`n")){throw 'All exact native Windows test names before execution'};$value=RunUnit $exe '--test-threads=1';$value.sha256=$a.sha256;$value.names=$actual;WriteNew (Join-Path $case ($kind+'-result.json')) $value;$reports[$kind]=$value} $v=@{platform='windows';manifest_sha256=$p.manifest_sha256;linux_proof_sha256=$p.linux_proof_sha256;reports=$reports;bootstrap_administrator_units=$true;local_service_proof=$false;product_state_writes=$false};WriteNew (Join-Path $case 'result.json') $v;$v|ConvertTo-Json -Depth 12 -Compress ''' def check_result(manifest, platform, result): require(result['platform'] == platform and set(result['reports']) == set(COUNTS[platform]), 'actual platform/all artifacts') for kind, actual in result['reports'].items(): require(actual['sha256'] == manifest['artifacts'][platform][kind]['sha256'] and actual['names'] == manifest['test_names'][platform][kind] and actual['exit_code'] == 0 and actual['timed_out'] is False, 'actual native list/artifact/exit') out = base64.b64decode(actual['stdout_b64'], validate=True) err = base64.b64decode(actual['stderr_b64'], validate=True) require(len(out) <= 1048576 and len(err) <= 1048576 and re.search(rb'test result: ok\. ' + str(COUNTS[platform][kind]).encode() + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', out), 'all exact native units execute, none ignored') def execute(path, platform, case_id=None): manifest = verified(path) if case_id is None: case_id = str(uuid.uuid4()); folder = STATE / case_id folder.mkdir(mode=0o700) save(folder / 'intent.json', dict(id=case_id, platform=platform, manifest_path=str(path.resolve()), manifest_sha256=sha(path))) read = False else: case_id = str(uuid.UUID(case_id)); folder = STATE / case_id intent = contract.protected_json(folder / 'intent.json') require(intent['platform'] == platform and intent['manifest_sha256'] == sha(path) and (folder / 'started-local.json').exists(), 'same original started case; readback only') read = True payload = dict(id=case_id, manifest_sha256=sha(path), artifacts=manifest['artifacts'][platform], names=manifest['test_names'][platform]) if platform == 'linux': if not read: linux.rpc('linux-vm.conf', linux_case, dict(payload, action='prepare')) cfg = linux.access.load_config(ROOT / 'credentials/linux-vm.conf') args = ['sftp', '-F', '/dev/null', '-P', cfg.get('port', '22'), '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()), '-b', '-', cfg['username'] + '@' + cfg['address']] batch = ''.join('put ' + json.dumps(item['path']) + ' ' + json.dumps(REMOTE + '/' + case_id + '/' + kind) + '\n' for kind, item in payload['artifacts'].items()) transfer = subprocess.run(args, input=batch.encode(), capture_output=True, timeout=120) save(folder / 'upload.json', dict(returncode=transfer.returncode)) require(transfer.returncode == 0, 'private pinned unit transfer') save(folder / 'started-local.json', dict(run_once=True)) result = linux.rpc('linux-vm.conf', linux_case, dict(payload, action='read' if read else 'run')) else: proof_path = path.parent / 'linux-passed.json' proof = contract.protected_json(proof_path) require(proof['verified'] is True and proof['manifest_sha256'] == sha(path), 'real same-manifest Linux first') check_result(manifest, 'linux', proof['native']) payload['linux_proof_sha256'] = sha(proof_path) if not read: for kind, item in payload['artifacts'].items(): windows.upload(Path(item['path']), 'windows-core-' + kind + '.exe') save(folder / 'started-local.json', dict(run_once=True, linux_proof_sha256=payload['linux_proof_sha256'])) result = windows.powershell(WINDOWS, dict(payload, action='read' if read else 'run'), timeout=120) save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) require(result['manifest_sha256'] == sha(path), 'native original manifest') check_result(manifest, platform, result) if platform == 'windows': require(result['linux_proof_sha256'] == payload['linux_proof_sha256'], 'native Linux proof binding') proof = dict(verified=True, case_id=case_id, platform=platform, manifest_sha256=sha(path), native=result) destination = path.parent / (platform + '-passed.json') if destination.exists(): require(contract.protected_json(destination) == proof, 'retain exact original native proof') else: save(destination, proof) print(json.dumps(dict(verified=True, platform=platform, case_id=case_id, tests=sum(COUNTS[platform].values())))) def main(): os.umask(0o077) parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('action', choices=('build-local', 'linux', 'windows', 'readback-linux', 'readback-windows')) parser.add_argument('--manifest', type=Path) parser.add_argument('--case') parser.add_argument('--execute-native-tests', action='store_true') args = parser.parse_args() if args.action == 'build-local': require(not (args.manifest or args.case or args.execute_native_tests), 'local build only'); build() elif args.action.startswith('readback-'): require(args.manifest and args.case and not args.execute_native_tests, 'original case readback only') execute(args.manifest, args.action.removeprefix('readback-'), args.case) else: require(args.manifest and args.execute_native_tests and not args.case, 'explicit new native unit run') execute(args.manifest, args.action) if __name__ == '__main__': try: main() except Exception as error: print('CORE_NO_START_QUALIFICATION_INCOMPLETE:' + type(error).__name__) raise SystemExit(2) from None