#!/usr/bin/env python3 """Fixed frozen Linux unit pack: prepare/run once, same-case readback only. Uses existing strict bootstrap transport. No agent installation, RMM job, approval, policy, package, service or device-identity change. """ import argparse import base64 import hashlib import inspect import json from pathlib import Path import re import uuid ROOT = Path(__file__).resolve().parents[1] SUPPORT_FILES = { 'deploy/windows_msi_runtime_acceptance.py': '833ad8806c90a19342aa6fbc71c33e81975caffb1746550e9b0fbdca7fa0c7ab', 'deploy/vm_access_check.py': '5c8107ea9151a9f498b2530cdc61c1a7a6d745aff853a9b05710f3395838c0dd', } def verify_support(): for name, digest in SUPPORT_FILES.items(): path=ROOT/name if path.is_symlink() or hashlib.sha256(path.read_bytes()).hexdigest()!=digest: raise RuntimeError("Pinned bootstrap support source changed") # Check helpers before import and again before every target call/readback. verify_support() import windows_msi_runtime_acceptance as support if Path(support.__file__).resolve()!=ROOT/'deploy/windows_msi_runtime_acceptance.py' or Path(support.access.__file__).resolve()!=ROOT/'deploy/vm_access_check.py': raise RuntimeError("Unexpected bootstrap support module origin") PACK = ROOT / 'devstate/script-console-qualification/run-29461892-343f-42f4-b469-88703535425e' MANIFEST_SHA = 'ddd1a78d1cd289b02b1f98572f9652171ec00be84e01ce099fd722d689bcaacd' STATE = ROOT / 'devstate/script-console-native' REMOTE = '/var/lib/rmm-script-console-contracts' PROFILE = 'script-console-linux-units-v1' KINDS = ('domain-tests', 'linux-runtime-tests', 'helper-policy-tests', 'server-contract-tests') COUNTS = {'domain-tests': (22, 0), 'linux-runtime-tests': (16, 0), 'helper-policy-tests': (6, 0), 'server-contract-tests': (40, 5)} def require(value, code): if not value: raise RuntimeError(code) def native_case(p): import base64 import hashlib import json import os from pathlib import Path import re import resource import stat import subprocess import uuid kinds = ('domain-tests', 'linux-runtime-tests', 'helper-policy-tests', 'server-contract-tests') counts = {'domain-tests': (22, 0), 'linux-runtime-tests': (16, 0), 'helper-policy-tests': (6, 0), 'server-contract-tests': (40, 5)} ident = str(uuid.UUID(p['id'])) require(ident == p['id'] and p['action'] in ('prepare', 'run', 'read') and p['profile'] == 'script-console-linux-units-v1', 'fixed unit case') require(os.geteuid() == 0 and os.uname().machine == 'x86_64' and set(p['artifacts']) == set(kinds), 'fixed Linux bootstrap qualification') root = Path('/var/lib/rmm-script-console-contracts') def safe(path, directory): s = path.lstat() require(not stat.S_ISLNK(s.st_mode) and s.st_uid == 0 and stat.S_IMODE(s.st_mode) == (0o700 if directory else 0o600) and (stat.S_ISDIR(s.st_mode) if directory else stat.S_ISREG(s.st_mode) and s.st_nlink == 1), '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 out: json.dump(value, out); out.flush(); os.fsync(out.fileno()) os.umask(0o077) case = root / ident if p['action'] == 'read' and not case.exists(): return dict(id=ident, status='remote_case_absent') if p['action'] == 'prepare': if not root.exists(): root.mkdir(mode=0o700) safe(root, True); case.mkdir(mode=0o700); write(case / 'intent.json', p) return dict(id=ident, prepared=True) safe(root, True); safe(case, True); safe(case / 'intent.json', False) original = json.loads((case / 'intent.json').read_bytes()) require({k: v for k, v in p.items() if k != 'action'} == {k: v for k, v in original.items() if k != 'action'}, 'original complete intent') if p['action'] == 'read': if not (case / 'result.json').exists(): partial = {} for kind in kinds: for suffix in ('-list.json', '-result.json'): path = case / (kind + suffix) if path.exists(): safe(path, False); require(path.stat().st_size <= 3 * 1024 * 1024, 'bounded partial receipt') partial[kind + suffix] = json.loads(path.read_bytes()) return dict(id=ident, status='started_without_result' if (case / 'started.json').exists() else 'prepared_without_start', all_lists_verified=(case / 'all-lists-verified.json').exists(), tests_started=(case / 'tests-started.json').exists(), partial=partial) safe(case / 'result.json', False) require((case / 'result.json').stat().st_size <= 12 * 1024 * 1024, 'bounded final receipt') return json.loads((case / 'result.json').read_bytes()) write(case / 'started.json', dict(manifest_sha256=p['manifest_sha256'], execute_once=True)) temp = case / 'tmp'; temp.mkdir(mode=0o700) env = {'PATH': '/usr/sbin:/usr/bin:/sbin:/bin', 'LANG': 'C.UTF-8', 'LC_ALL': 'C.UTF-8', 'TMPDIR': str(temp), 'RUST_BACKTRACE': '0'} def limits(): resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) resource.setrlimit(resource.RLIMIT_CPU, (30, 30)) resource.setrlimit(resource.RLIMIT_FSIZE, (16 * 1024 * 1024, 16 * 1024 * 1024)) resource.setrlimit(resource.RLIMIT_AS, (768 * 1024 * 1024, 768 * 1024 * 1024)) require(subprocess.run(['/usr/bin/unshare', '--net', '--', '/usr/bin/true'], env=env, capture_output=True, timeout=3).returncode == 0, 'network namespace required') listings = {} for kind in kinds: binary = case / kind; safe(binary, False) raw = binary.read_bytes() require(len(raw) == p['artifacts'][kind]['bytes'] and len(raw) <= 64 * 1024 * 1024 and hashlib.sha256(raw).hexdigest() == p['artifacts'][kind]['sha256'] and raw[:6] == b'\x7fELF\x02\x01' and raw[18:20] == b'\x3e\0', 'exact Linux x64 ELF') binary.chmod(0o500) listed = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--list', '--format', 'terse'], cwd=case, env=env, capture_output=True, timeout=8, preexec_fn=limits) require(listed.returncode == 0 and len(listed.stdout) <= 65536 and len(listed.stderr) <= 65536, 'bounded actual listing') actual = sorted(line[:-6] for line in listed.stdout.decode().splitlines() if line.endswith(': test')) write(case / (kind + '-list.json'), dict(names=actual, exit_code=listed.returncode, sha256=p['artifacts'][kind]['sha256'], stdout_b64=base64.b64encode(listed.stdout).decode(), stderr_b64=base64.b64encode(listed.stderr).decode())) require(all(not line.strip() or re.fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in listed.stdout.decode().splitlines()) and actual == p['names'][kind] and len(actual) == sum(counts[kind]), 'all exact lists BEFORE any unit execution') listings[kind] = actual write(case / 'all-lists-verified.json', dict(all_lists_before_any_tests=True, manifest_sha256=p['manifest_sha256'])) write(case / 'tests-started.json', dict(execute_tests_once=True)) reports = {} for kind in kinds: timed = False with (case / (kind + '-stdout.bin')).open('xb') as out, (case / (kind + '-stderr.bin')).open('xb') as err: try: code = subprocess.run(['/usr/bin/unshare', '--net', '--', str(case / kind), '--test-threads=1'], cwd=case, env=env, stdin=subprocess.DEVNULL, stdout=out, stderr=err, timeout=45, preexec_fn=limits).returncode except subprocess.TimeoutExpired: timed = True; code = None out.flush(); err.flush(); os.fsync(out.fileno()); os.fsync(err.fileno()) raw, errors = [(case / (kind + '-' + stream + '.bin')).read_bytes() for stream in ('stdout', 'stderr')] require(len(raw) <= 1048576 and len(errors) <= 1048576, 'bounded native output') reports[kind] = dict(sha256=p['artifacts'][kind]['sha256'], names=listings[kind], exit_code=code, timed_out=timed, stdout_b64=base64.b64encode(raw).decode(), stderr_b64=base64.b64encode(errors).decode()) write(case / (kind + '-result.json'), reports[kind]) passed, ignored = counts[kind] require(code == 0 and not timed and re.search( rf'test result: ok\. {passed} passed; 0 failed; {ignored} ignored;', raw.decode()), 'exact native pass/ignored count; no retry') value = dict(id=ident, platform='linux', profile=p['profile'], manifest_sha256=p['manifest_sha256'], runner_sha256=p['runner_sha256'], support_files=p['support_files'], reports=reports, all_lists_before_any_tests=True, network_namespace=True, environment_cleared=True, product_state_writes=False, rmm_jobs=False, capabilities_activated=False, approvals=False, passed=84, ignored_database_cases=5) write(case / 'result.json', value) return value def rpc(prefix, payload, folder): verify_support() code = inspect.getsource(require) + '\n' + inspect.getsource(native_case) code += '\ntry:\n print(__import__("json").dumps(native_case(' + repr(payload) + ')))\nexcept Exception as e:\n print(__import__("json").dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n' return support.transport(prefix, 'python3 -', code.encode(), folder, timeout=230) def run(case_id=None): import os verify_support() os.umask(0o077); STATE.mkdir(mode=0o700, parents=True, exist_ok=True) support.private(STATE, True); support.private(PACK, True) manifest = support.load(PACK / 'manifest.json') require(support.sha(PACK / 'manifest.json') == MANIFEST_SHA and manifest['source_before_after_equal'] is True and manifest['type'] == 'rmm.script-console.local-source-qualification', 'frozen original qualification') if case_id is None: require(all(support.sha(ROOT / name) == digest for name, digest in manifest['source_files'].items()), 'new attempt uses exact frozen source') case_id = str(uuid.uuid4()); folder = STATE / case_id; folder.mkdir(mode=0o700) names = {}; artifacts = {} for kind in KINDS: artifact = PACK / kind; support.private(artifact) require(support.sha(artifact) == manifest['artifacts'][kind]['sha256'], 'original ELF bytes') log = PACK / (kind + '-list.log'); support.private(log) require(support.sha(log) == manifest['checks'][kind + '-list']['log_sha256'], 'original actual list') names[kind] = sorted(line[:-6] for line in log.read_text().splitlines() if line.endswith(': test')) require(len(names[kind]) == sum(COUNTS[kind]), 'fixed original test count') artifacts[kind] = {key: manifest['artifacts'][kind][key] for key in ('sha256', 'bytes')} payload = dict(id=case_id, profile=PROFILE, manifest_sha256=MANIFEST_SHA, runner_sha256=support.sha(__file__), support_files=SUPPORT_FILES, names=names, artifacts=artifacts) support.save(folder / 'intent.json', payload) support.save(folder / 'started-local.json', dict(create_once=True, no_retry=True)) prefix = support.access_settings(support.ACCESS_ROOT, 'linux') require(rpc(prefix, dict(payload, action='prepare'), folder) == dict(id=case_id, prepared=True), 'fresh fixed case prepared') for kind in KINDS: support.upload(prefix, dict(path=str(PACK / kind), **artifacts[kind]), REMOTE + '/' + case_id + '/' + kind, folder, kind) action = 'run' else: require(str(uuid.UUID(case_id)) == case_id, 'original canonical case id') folder = STATE / case_id; support.private(folder, True) payload = support.load(folder / 'intent.json'); support.load(folder / 'started-local.json') require(payload['id'] == case_id and payload['manifest_sha256'] == MANIFEST_SHA and payload['runner_sha256'] == support.sha(__file__) and payload['support_files'] == SUPPORT_FILES, 'immutable original readback only') prefix = support.access_settings(support.ACCESS_ROOT, 'linux') action = 'read' value = rpc(prefix, dict(payload, action=action), folder) support.save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), value) if value.get('status'): require(action == 'read', 'only same-case readback can report incomplete state') print(json.dumps(dict(case_id=case_id, verified=False, status=value['status'], retry_allowed=False))) return require(value['id'] == case_id and value['manifest_sha256'] == MANIFEST_SHA and value['runner_sha256'] == payload['runner_sha256'] and value['passed'] == 84 and value['ignored_database_cases'] == 5 and value['all_lists_before_any_tests'] is True and value['network_namespace'] is True and value['environment_cleared'] is True and value['support_files'] == SUPPORT_FILES and all(value.get(flag) is False for flag in ('product_state_writes','rmm_jobs','capabilities_activated','approvals')) and set(value['reports']) == set(KINDS), 'complete native receipt') for kind in KINDS: item = value['reports'][kind]; passed, ignored = COUNTS[kind] require(item['names'] == payload['names'][kind] and item['sha256'] == payload['artifacts'][kind]['sha256'] and item['exit_code'] == 0 and item['timed_out'] is False and re.search(rf'test result: ok\. {passed} passed; 0 failed; {ignored} ignored;', base64.b64decode(item['stdout_b64'], validate=True).decode()), 'each original native result') proof = dict(verified=True, case_id=case_id, manifest_sha256=MANIFEST_SHA, native=value, native_services_or_rmm_job_acceptance=False) path = folder / ('passed-' + str(uuid.uuid4()) + '.json'); support.save(path, proof) print(json.dumps(dict(case_id=case_id, verified=True, proof=str(path), sha256=support.sha(path), passed=84, ignored_database_cases=5, rmm_jobs=False, retry_allowed=False))) if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('mode', choices=('execute-linux', 'read-linux')) parser.add_argument('--case') args = parser.parse_args() require((args.mode == 'read-linux') == (args.case is not None), 'read needs original case; execute always new case') run(args.case)