"""Independent deterministic streaming model; candidate runs only in child processes.""" import json import os import random from pathlib import Path import subprocess import sys import tempfile SLOTS = 4 LIMIT = 96 def pattern(seed, index): return ((seed ^ (index * 29)) + index * index + 17) & 255 def digest(data): value = 2166136261 for byte in data: value = ((value ^ byte) * 16777619) & 0xffffffff return value class Model: def __init__(self): self.regions = [dict(live=False, pending=False, direction=0, length=0, generation=0, counters=[0] * 7, cpu=[], device=[], receipt=[], completed=0, owner=0) for _ in range(SLOTS)] self.ticket = 1 def ready(self, slot): if slot >= SLOTS: return 1 region = self.regions[slot] if not region['live']: return 3 return 4 if region['pending'] else 0 def finish(self, region): if region['direction'] in (2, 3): region['cpu'] = list(region['device']) region['counters'][6] += region['length'] region['owner'] = 1 region['counters'][3] += 1 region['completed'] += 1 region['pending'] = False return region['receipt'] + [digest(region['cpu']), region['completed']] @staticmethod def close(region): region.update(live=False, pending=False, direction=0, length=0, cpu=[], device=[], receipt=[], completed=0, owner=0) region['counters'][1] += 1 def apply(self, words): op, *args = words code, values = 0, [] slot = args[0] if args else 0 region = self.regions[slot] if slot < SLOTS else None if op == 'M': slot, length, direction, seed = args if slot >= SLOTS: code = 1 elif region['live']: code = 2 elif direction not in (1, 2, 3): code = 6 elif not 1 <= length <= LIMIT: code = 5 else: region['generation'] += 1 region['counters'][0] += 1 region.update(live=True, pending=False, direction=direction, length=length, completed=0, receipt=[], owner=1, cpu=[pattern(seed, i) for i in range(length)], device=[pattern(seed ^ 0xA5, i) for i in range(length)]) values = [region['generation']] elif op in ('W', 'R', 'B', 'U'): code = self.ready(slot) if code == 0 and op == 'U': self.close(region) elif code == 0: offset, count = args[1:3] if offset > region['length'] or count > region['length'] - offset or (op == 'B' and count == 0): code = 5 elif op == 'R': values = region['cpu'][offset:offset + count] elif op == 'W': seed = args[3] region['cpu'][offset:offset + count] = [pattern(seed, i) for i in range(offset, offset + count)] else: seed = args[3] direction = region['direction'] if direction in (1, 3): region['device'] = list(region['cpu']) region['counters'][5] += region['length'] region['counters'][2] += 1 region['owner'] = 2 before = digest(region['device'][offset:offset + count]) if direction != 2 else 0 if direction != 1: for index in range(offset, offset + count): mask = pattern(seed, index) region['device'][index] = mask if direction == 2 else region['device'][index] ^ mask after = digest(region['device'][offset:offset + count]) if direction != 1 else 0 region['receipt'] = [self.ticket, region['generation'], offset, count, before, after] self.ticket += 1 region['counters'][4] += 1 region['pending'] = True values = list(region['receipt']) elif op == 'F': if slot >= SLOTS: code = 1 elif not region['live']: code = 3 elif not region['pending'] or region['receipt'][0] != args[1]: code = 7 else: values = self.finish(region) elif op == 'X': finished = sum(int(r['pending']) for r in self.regions) closed = sum(int(r['live']) for r in self.regions) for r in self.regions: if r['pending']: self.finish(r) for r in self.regions: if r['live']: self.close(r) values = [finished, closed] else: raise AssertionError(op) return self.observe(code, values) def observe(self, code, values): slots = [] for r in self.regions: manager = [int(r['live']), int(r['pending']), r['direction'], r['length'], r['completed'], r['receipt'][0] if r['pending'] else 0] port = [int(r['live']), r['owner'], r['direction'], r['length'], r['generation']] + list(r['counters']) slots.append(dict(manager=manager, port=port, cpu=bytes(r['cpu']).hex(), device=bytes(r['device']).hex())) return dict(status=code, values=list(values), slots=slots, errors=0) class Cases: def __init__(self): self.model = Model() self.commands = [] self.expected = [] self.labels = [] def add(self, op, *args, label=''): words = [op, *args] result = self.model.apply(words) self.commands.append(' '.join(str(w) for w in words)) self.expected.append(result) self.labels.append(label or op) return result['values'] def cases(): c = Cases() c.add('M', 0, 8, 1, 9, label='public transmit demo') c.add('B', 0, 0, 8, 0) c.add('F', 0, 1) c.add('W', 0, 0, 8, 55) c.add('B', 0, 0, 8, 0, label='same mapping second handoff') c.add('F', 0, 2) c.add('X') for direction in (2, 3, 1): for length in (1, 9, LIMIT): slot = direction % SLOTS c.add('M', slot, length, direction, 0xffffffff - length, label='direction and length matrix') c.add('R', slot, length, 0) c.add('W', slot, length, 0, 41) c.add('B', slot, length, 0, 13, label='zero-length transfer rejected') c.add('R', slot, length, 1) for cycle in range(4): off = cycle % length count = length - off c.add('W', slot, off, count, 700 + direction * 19 + cycle) values = c.add('B', slot, off, count, cycle * 993 + length) ticket = values[0] c.add('F', slot, ticket + 500, label='wrong ticket cannot reclaim') c.add('R', slot, 0, length, label='CPU access while device owns map') c.add('W', slot, 0, 0, 11) c.add('B', slot, 0, 1, 12) c.add('U', slot) c.add('F', slot, ticket, label='directional completion') c.add('F', slot, ticket, label='duplicate completion rejected') c.add('R', slot, 0, length) c.add('U', slot) c.add('M', slot, length, direction, length + 37, label='remap resets per-map state') values = c.add('B', slot, 0, length, 888) c.add('F', slot, ticket, label='stale previous generation ticket') c.add('F', slot, values[0]) c.add('U', slot) for slot in (4, 19): c.add('M', slot, 0, 0, 0, label='invalid slot priority') c.add('W', slot, 0, 0, 0) c.add('B', slot, 0, 0, 0) c.add('R', slot, 0, 0) c.add('F', slot, 0) c.add('U', slot) c.add('M', 0, 0, 0, 0, label='direction before length validation') c.add('M', 0, 0, 1, 0) c.add('M', 0, LIMIT + 1, 3, 0) c.add('M', 0, 9, 4, 0) for slot in range(SLOTS): c.add('M', slot, slot + 7, slot % 3 + 1, slot + 101) c.add('M', slot, 0, 0, 0, label='existing map priority') c.add('B', slot, 1, slot + 3, slot + 90) c.add('X', label='drain pending mappings then release every slot') c.add('X', label='empty reset') rng = random.Random(0x5DA2026) for index in range(420): slot = rng.randrange(SLOTS) r = c.model.regions[slot] seed = rng.getrandbits(32) if index % 47 == 0: c.add('X', label='random drain') elif not r['live']: if index % 9 == 0: c.add('F', slot, rng.randrange(500), label='missing mapping') else: c.add('M', slot, rng.choice([1, 2, 15, 33, 95, 96]), rng.randrange(1, 4), seed) elif r['pending']: choice = rng.randrange(5) if choice <= 1: c.add('F', slot, r['receipt'][0]) elif choice == 2: c.add('F', slot, r['receipt'][0] + 1) elif choice == 3: c.add('B', slot, 0, 1, seed) else: c.add('R', slot, 0, r['length']) else: off = rng.randrange(r['length'] + 1) count = rng.randrange(r['length'] - off + 1) choice = rng.randrange(8) if choice <= 2: c.add('B', slot, off, count, seed) elif choice == 3: c.add('W', slot, off, count, seed) elif choice == 4: c.add('R', slot, off, count) elif choice == 5: c.add('U', slot) elif choice == 6: c.add('W', slot, r['length'] + 1, 0, seed) else: c.add('F', slot, rng.randrange(600)) c.add('X', label='final lifecycle cleanup') return c # Pure execution supervisor: no model, test generation, or expected observations. EXECUTOR = r""" import ctypes, errno, json, os, signal, subprocess, sys, time phase, limit, argv = json.loads(sys.argv[1]) identity = {'phase': phase, 'uid': os.getuid(), 'gid': os.getgid(), 'groups': os.getgroups(), 'pid': os.getpid()} print('EXEC_IDENTITY ' + json.dumps(identity), file=sys.stderr, flush=True) try: libc = ctypes.CDLL(None, use_errno=True) if libc.prctl(36, 1, 0, 0, 0) != 0: raise OSError(ctypes.get_errno(), 'PR_SET_CHILD_SUBREAPER') payload = sys.stdin.buffer.read() child = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True) except Exception as error: print('EXEC_LAUNCH_ERROR ' + repr(error), file=sys.stderr, flush=True) raise SystemExit(125) started = time.monotonic() timed_out = False out = err = b'' try: out, err = child.communicate(payload, timeout=limit) except subprocess.TimeoutExpired: timed_out = True try: os.killpg(child.pid, signal.SIGKILL) except ProcessLookupError: pass try: out, err = child.communicate(timeout=2) except subprocess.TimeoutExpired: print('EXEC_REAP_ERROR communicate did not finish after group kill', file=sys.stderr, flush=True) raise SystemExit(124) finally: # Also remove descendants of a normally exiting direct child. try: os.killpg(child.pid, signal.SIGKILL) except ProcessLookupError: pass adopted = [] reaped = child.poll() is not None until = time.monotonic() + 2 while True: try: pid, status = os.waitpid(-1, os.WNOHANG) except ChildProcessError: break if pid: adopted.append([pid, status]) elif time.monotonic() >= until: reaped = False break else: time.sleep(0.01) try: os.killpg(child.pid, 0) group_absent = False except ProcessLookupError: group_absent = True sys.stdout.buffer.write(out) sys.stdout.buffer.flush() sys.stderr.buffer.write(err) sys.stderr.buffer.flush() result = {'phase': phase, 'child_pid': child.pid, 'actual_child_rc': child.returncode, 'deadline_seconds': limit, 'timed_out': timed_out, 'reaped': reaped, 'adopted_reaped': adopted, 'group_absent': group_absent, 'elapsed_seconds': time.monotonic() - started} print('EXEC_CHILD ' + json.dumps(result), file=sys.stderr, flush=True) if timed_out or not reaped or not group_absent: raise SystemExit(124) raise SystemExit(child.returncode if child.returncode >= 0 else 128 - child.returncode) """ ABSENCE = r""" import json, os, sys absent = not os.path.lexists(sys.argv[1]) print(json.dumps({'absent': absent, 'uid': os.getuid(), 'gid': os.getgid(), 'groups': os.getgroups()})) raise SystemExit(0 if absent else 1) """ def child_process(phase, argv, deadline, cwd=None, payload=b''): # bos_verify drops this supervisor to the same UID as the process it owns. # The privileged parent never tries to signal a cross-UID candidate. process = subprocess.Popen([sys.executable, '-I', '-c', EXECUTOR, json.dumps([phase, deadline, argv])], cwd=cwd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate(payload) sys.stderr.write(stderr.decode('utf-8', 'replace')) print('EXEC_RESULT ' + json.dumps({'phase': phase, 'returncode': process.returncode}), file=sys.stderr, flush=True) return subprocess.CompletedProcess(argv, process.returncode, stdout, stderr) def execute(): source = Path(sys.argv[1]) support = json.loads((Path(__file__).parent / 'support.json').read_text()) candidate = (source / 'src/session.cpp').read_bytes() suite = cases() stdin = ('\n'.join(suite.commands) + '\n').encode('utf-8') scratch = Path(tempfile.mkdtemp(prefix='dev-stream-map-')) namespace = 'container' if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else 'host' status = Path('/proc/self/status').read_text() cap_eff = next(line.split(':', 1)[1].strip() for line in status.splitlines() if line.startswith('CapEff:')) print('EXEC_PARENT ' + json.dumps({'uid': os.getuid(), 'gid': os.getgid(), 'groups': os.getgroups(), 'cap_eff': cap_eff, 'scratch': str(scratch), 'namespace': namespace}), file=sys.stderr, flush=True) failure = None count = 0 try: build = scratch / 'build' build.mkdir(mode=0o777) # The wrapper already assigned scratch to UID65534; do not chmod its root. build.chmod(0o777) for name, body in support.items(): path = build / name path.parent.mkdir(parents=True, exist_ok=True) path.write_text(body) (build / 'src/session.cpp').write_bytes(candidate) for path in build.rglob('*'): if path.is_dir(): path.chmod(0o777) # Every input, source file, and expected observation is ready before the first child. # After it starts, the parent never reads or writes candidate-controlled paths. binary = str(build / 'stream-demo') compile_result = child_process('compile', ['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-Wpedantic', '-I', str(build / 'include'), str(build / 'src/session.cpp'), str(build / 'src/port.cpp'), str(build / 'src/main.cpp'), '-o', binary], 30, cwd=build) if compile_result.returncode != 0: failure = 'candidate build timeout' if compile_result.returncode == 124 else 'candidate build failed' else: run = child_process('candidate', [binary], 15, cwd=build, payload=stdin) if run.returncode != 0: failure = 'candidate child timeout' if run.returncode == 124 else 'candidate execution returned ' + str(run.returncode) else: try: lines = run.stdout.decode('utf-8').splitlines() except UnicodeDecodeError: lines = [] failure = 'invalid observation encoding' if failure is None and len(lines) != len(suite.expected): failure = 'observation count mismatch' elif failure is None: for index, (line, expected) in enumerate(zip(lines, suite.expected)): try: actual = json.loads(line) except (ValueError, TypeError): failure = 'invalid observation at command ' + str(index) break if actual != expected: failure = 'behavior mismatch at command %d (%s)' % (index, suite.labels[index]) print(failure, file=sys.stderr) print('command: ' + suite.commands[index], file=sys.stderr) print('observed: ' + line[:400], file=sys.stderr) print('expected: ' + json.dumps(expected)[:400], file=sys.stderr) break count += 1 except Exception as error: failure = 'candidate execution error: ' + repr(error) finally: rm_rc = probe_rc = None absent = False try: cleanup = child_process('cleanup', ['/bin/rm', '-rf', '--', str(scratch)], 10) rm_rc = cleanup.returncode # Independent same-UID probe; no privileged tree access after candidate execution. probe = child_process('absence', [sys.executable, '-I', '-c', ABSENCE, str(scratch)], 4) probe_rc = probe.returncode if probe_rc == 0: absent = json.loads(probe.stdout.decode('utf-8'))['absent'] is True if rm_rc != 0 or probe_rc != 0 or not absent: failure = 'candidate-identity cleanup failed' except Exception as error: failure = 'candidate-identity cleanup error: ' + repr(error) print('CLEANUP ' + json.dumps({'scratch': str(scratch), 'namespace': namespace, 'rm_rc': rm_rc, 'probe_rc': probe_rc, 'absent': absent}), file=sys.stderr, flush=True) if failure: print(failure) return 1 print('verified %d command observations' % count) print('PASS') return 0 if __name__ == '__main__': raise SystemExit(execute())