From 004aa142c1a30b300142d0273e975540837c7bd2 Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Sun, 15 Nov 2026 01:47:40 +0100 Subject: [PATCH] Bind Chrony preflight to actual service and command socket holder --- deploy/server_time_source_bootstrap.py | 256 +++++++++++++++++++++---- docs/server-time-source-bootstrap.md | 44 ++++- 2 files changed, 258 insertions(+), 42 deletions(-) diff --git a/deploy/server_time_source_bootstrap.py b/deploy/server_time_source_bootstrap.py index 5e7df0f..c81f721 100644 --- a/deploy/server_time_source_bootstrap.py +++ b/deploy/server_time_source_bootstrap.py @@ -53,9 +53,13 @@ fn main() { ''' +class OperatorDenied(RuntimeError): + """Only explicit operator guards may produce a public failure reason.""" + + def require(ok, reason): if not ok: - raise RuntimeError(reason) + raise OperatorDenied(reason) def digest(raw): @@ -103,7 +107,7 @@ def run(args, timeout=15, limit=1024 * 1024): result = subprocess.run(args, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout, check=False) require(len(result.stdout) <= limit and len(result.stderr) <= limit, 'bounded command output') - require(result.returncode == 0, 'fixed command failed: ' + args[0]) + require(result.returncode == 0, 'fixed command failed') return result.stdout @@ -341,50 +345,218 @@ def runtime_witness(): return result +def fd_metadata(fd): + item = os.fstat(fd) + return {key: getattr(item, 'st_' + key) for key in + ('dev', 'ino', 'uid', 'gid', 'mode', 'nlink', 'size', 'mtime_ns', 'ctime_ns')} + + +def held_bytes(fd, limit): + before = fd_metadata(fd) + require(stat.S_ISREG(before['mode']) and before['size'] <= limit, 'bounded held regular input') + raw = os.pread(fd, limit + 1, 0) + require(len(raw) <= limit and fd_metadata(fd) == before, 'stable held input') + return raw + + +def chrony_unit(): + fields = ('MainPID', 'ControlPID', 'ActiveState', 'SubState', 'Type', 'ControlGroup', 'InvocationID', 'Job') + raw = run(['systemctl', 'show', '--all', '--property=' + ','.join(fields), '--', 'chrony.service'], limit=4096) + values = {} + for line in raw.decode('ascii').splitlines(): + key, separator, value = line.partition('=') + require(separator and key in fields and key not in values, 'complete fixed Chrony unit properties') + values[key] = value + require(set(values) == set(fields) and values['MainPID'].isdigit() and int(values['MainPID']) > 1 and + values['ControlPID'] == '0' and values['ActiveState'] == 'active' and values['SubState'] == 'running' and + values['Type'] == 'notify' and values['ControlGroup'] == '/system.slice/chrony.service' and + re.fullmatch('[0-9a-f]{32}', values['InvocationID']) and int(values['InvocationID'], 16) > 0 and + not values['Job'], 'actual fixed active notify Chrony service') + return values + + +def named_matches(path, held): + named = metadata(path) + return all(held.get(key) == value for key, value in named.items()) + + +def cgroup_members(path): + raw = regular(path / 'cgroup.procs', 256) + require(re.fullmatch(rb'(?:[1-9][0-9]*\n){3}', raw) is not None, 'exact three Chrony cgroup processes') + members = sorted(int(value) for value in raw.splitlines()) + require(len(set(members)) == 3 and all(value > 1 for value in members), 'unique Chrony cgroup processes') + # No silently ignored descendant cgroup can hide additional service members. + with os.scandir(path) as entries: + require(not any(entry.is_dir(follow_symlinks=False) for entry in entries), + 'no descendant Chrony cgroup') + return members + + +class ChronyProcess: + def __init__(self, pid): + self.pid, self.fds = pid, [] + try: + self.pidfd = os.pidfd_open(pid) + self.fds.append(self.pidfd) + self.proc = Path('/proc') / str(pid) + self.procfd = os.open(self.proc, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + self.fds.append(self.procfd) + # The fixed proc exe magic link is followed once, then held and + # compared with the current executable before and after hashing. + self.image = os.open('exe', os.O_RDONLY | os.O_CLOEXEC, dir_fd=self.procfd) + self.fds.append(self.image) + self.original = self.observe() + except Exception: + self.close() + raise + + def close(self): + for fd in reversed(self.fds): + os.close(fd) + self.fds = [] + + def observe(self): + poll = __import__('select').poll() + poll.register(self.pidfd, __import__('select').POLLIN) + require(not poll.poll(0), 'held Chrony process alive') + status = regular(self.proc / 'status', 65536).decode('ascii') + ids = {} + for label in ('Uid', 'Gid'): + match = re.search('^' + label + r':\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$', status, re.MULTILINE) + require(match and len(set(match.groups())) == 1, 'fixed real effective process IDs') + ids[label.lower()] = int(match[1]) + first = regular(self.proc / 'stat', 8192).decode('ascii').rsplit(')', 1)[1].split() + require(len(first) >= 20 and first[19].isdigit() and first[1].isdigit(), 'process birth and parent') + require(regular(self.proc / 'cgroup', 4096) == b'0::/system.slice/chrony.service\n', + 'actual unified Chrony cgroup membership') + image = fd_metadata(self.image) + current = os.stat('exe', dir_fd=self.procfd) + require(stat.S_ISREG(image['mode']) and image['uid'] == 0 and image['mode'] & 0o022 == 0 and + (current.st_dev, current.st_ino) == (image['dev'], image['ino']), 'held protected actual process image') + image_hash = digest(held_bytes(self.image, 16 * 1024 * 1024)) + after = os.stat('exe', dir_fd=self.procfd) + require((after.st_dev, after.st_ino) == (image['dev'], image['ino']) and + fd_metadata(self.image) == image and regular(self.proc / 'stat', 8192).decode('ascii').rsplit(')', 1)[1].split()[19] == first[19] + and not poll.poll(0), 'actual process image and birth unchanged') + return {'pid': self.pid, 'birth_ticks': int(first[19]), 'parent_pid': int(first[1]), + 'uid': ids['uid'], 'gid': ids['gid'], 'image': image, 'image_sha256': image_hash} + + def socket_fds(self, inode): + fd = os.open('fd', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=self.procfd) + try: + names = os.listdir(fd) + require(len(names) <= 128 and all(value.isdigit() for value in names), 'bounded process descriptor set') + return sorted(int(value) for value in names if os.readlink(value, dir_fd=fd) == 'socket:[' + str(inode) + ']') + finally: + os.close(fd) + + +def command_socket_inode(): + raw = regular(Path('/proc/net/unix'), 1024 * 1024) + rows = [line.split() for line in raw.decode('ascii').splitlines()[1:]] + matches = [row for row in rows if len(row) == 8 and row[7] == '/run/chrony/chronyd.sock'] + require(len(matches) == 1 and matches[0][4] == '0002' and matches[0][6].isdigit(), + 'unique fixed Unix datagram command socket') + return int(matches[0][6]) + + +def chrony_observation(): + unit = chrony_unit() + cgroup = Path('/sys/fs/cgroup/system.slice/chrony.service') + protected_dir(cgroup) + group_identity = metadata(cgroup) + members = cgroup_members(cgroup) + processes = [] + pidfile = directory = -1 + try: + for pid in members: + processes.append(ChronyProcess(pid)) + main = int(unit['MainPID']) + by_pid = {process.pid: process for process in processes} + require(main in by_pid, 'unit wrapper belongs to actual service cgroup') + # /run/chrony is daemon-owned: a root-owned pidfile alone is NOT + # sufficient namespace authority. Bind the actual command-socket FD too. + protected_dir(Path('/run')) + directory = os.open('/run/chrony', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + dir_identity = fd_metadata(directory) + require(stat.S_ISDIR(dir_identity['mode']) and dir_identity['uid'] == 989 and + dir_identity['gid'] == 989 and stat.S_IMODE(dir_identity['mode']) == 0o755, + 'actual fixed protected Chrony socket directory') + pidfile = os.open('chronyd.pid', os.O_RDONLY | os.O_NOFOLLOW, dir_fd=directory) + pid_identity = fd_metadata(pidfile) + require(pid_identity['uid'] == 0 and pid_identity['gid'] == 0 and pid_identity['nlink'] == 1 and + stat.S_IMODE(pid_identity['mode']) == 0o644, 'root single link fixed Chrony pidfile') + pid_raw = held_bytes(pidfile, 32) + require(re.fullmatch(rb'[1-9][0-9]*\n', pid_raw) is not None, 'canonical actual Chrony pidfile PID') + daemon = int(pid_raw.strip()) + require(daemon in by_pid and daemon != main, 'pidfile daemon belongs to service cgroup') + child = next(pid for pid in members if pid not in (main, daemon)) + wrapper, actual, helper = (by_pid[pid].original for pid in (main, daemon, child)) + require(wrapper['uid'] == wrapper['gid'] == 0 and actual['uid'] == actual['gid'] == 989 and + helper['uid'] == helper['gid'] == 989 and actual['parent_pid'] == main and helper['parent_pid'] == daemon, + 'actual fixed wrapper daemon child identity chain') + fixed_images = {} + for role, path, observed in (('wrapper', '/usr/bin/dash', wrapper), ('daemon', '/usr/sbin/chronyd', actual)): + protected_dir(Path(path).parent) + fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW) + try: + fixed = fd_metadata(fd) + require(fixed == observed['image'] and digest(held_bytes(fd, 16 * 1024 * 1024)) == observed['image_sha256'] + and named_matches(Path(path), fixed), + 'actual process equals protected fixed executable') + fixed_images[role] = fixed + finally: + os.close(fd) + require(helper['image'] == actual['image'] and helper['image_sha256'] == actual['image_sha256'], + 'actual Chrony child shares protected daemon image') + socket_path = Path('/run/chrony/chronyd.sock') + socket_identity = metadata(socket_path) + require(stat.S_ISSOCK(socket_identity['mode']) and socket_identity['uid'] == socket_identity['gid'] == 989 and + stat.S_IMODE(socket_identity['mode']) == 0o755, 'actual fixed Chrony filesystem socket') + inode = command_socket_inode() + holders = {process.pid: process.socket_fds(inode) for process in processes} + require(bool(holders[daemon]) and all(not fds for pid, fds in holders.items() if pid != daemon), + 'unique actual service command socket holder') + for process in processes: + require(process.observe() == process.original, 'service process tuple unchanged') + require(chrony_unit() == unit and cgroup_members(cgroup) == members and metadata(cgroup) == group_identity and + fd_metadata(directory) == dir_identity and named_matches(Path('/run/chrony'), dir_identity) and + held_bytes(pidfile, 32) == pid_raw and fd_metadata(pidfile) == pid_identity and + named_matches(Path('/run/chrony/chronyd.pid'), pid_identity) and + metadata(socket_path) == socket_identity and command_socket_inode() == inode and + named_matches(Path('/usr/bin/dash'), fixed_images['wrapper']) and + named_matches(Path('/usr/sbin/chronyd'), fixed_images['daemon']) and + {process.pid: process.socket_fds(inode) for process in processes} == holders, + 'Chrony unit namespace process pidfile and socket unchanged') + return {'pin': {'schema_version': 1, 'daemon_pid': daemon, 'daemon_birth_ticks': actual['birth_ticks'], + 'daemon_uid': actual['uid'], 'daemon_gid': actual['gid'], 'daemon_image_sha256': actual['image_sha256']}, + 'unit': unit, 'processes': [process.original for process in processes], + 'cgroup_metadata': group_identity, 'pidfile_metadata': pid_identity, 'pidfile_sha256': digest(pid_raw), + 'directory_metadata': dir_identity, 'socket_metadata': socket_identity, + 'socket_kernel_inode': inode, 'service_socket_fds': holders, 'fixed_images': fixed_images} + finally: + if pidfile >= 0: + os.close(pidfile) + if directory >= 0: + os.close(directory) + for process in processes: + process.close() + + def preflight(): engine = run(['docker', 'version', '--format', '{{json .Server.Version}}']).decode().strip() image_values = json.loads(run(['docker', 'image', 'inspect', '--format', '[{{json .Id}},{{json .Architecture}},{{json .Os}}]', BUILDER])) image = dict(zip(('Id', 'Architecture', 'Os'), image_values, strict=True)) - # Inspect only public immutable image metadata, never Config/Env. require(image['Architecture'] == 'arm64' and image['Os'] == 'linux', 'cached ARM64 builder required') - pid = int(run(['systemctl', 'show', 'chrony.service', '--property=MainPID', '--value']).strip()) - require(pid > 1, 'existing Chrony service PID') - proc = Path('/proc') / str(pid) - pidfd = os.pidfd_open(pid) - try: - status = regular(proc / 'status', 65536).decode() - ids = {} - for label in ('Uid', 'Gid'): - match = re.search('^' + label + r':\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$', status, re.MULTILINE) - require(match and len(set(match.groups())) == 1, 'fixed real/effective daemon IDs') - ids[label.lower()] = int(match[1]) - birth = regular(proc / 'stat', 8192).decode().rsplit(')', 1)[1].split()[19] - image_path = Path('/usr/sbin/chronyd') - fixed = metadata(image_path) - require(stat.S_ISREG(fixed['mode']) and fixed['uid'] == 0 and fixed['mode'] & 0o022 == 0, - 'protected Chrony executable') - require(os.stat(proc / 'exe').st_ino == fixed['ino'] and os.stat(proc / 'exe').st_dev == fixed['dev'], - 'actual daemon fixed executable inode') - image_hash = digest(regular(image_path)) - require(metadata(image_path) == fixed and - regular(proc / 'stat', 8192).decode().rsplit(')', 1)[1].split()[19] == birth and - int(run(['systemctl', 'show', 'chrony.service', '--property=MainPID', '--value']).strip()) == pid, - 'Chrony identity unchanged during probe') - poll = __import__('select').poll() - poll.register(pidfd, __import__('select').POLLIN) - require(not poll.poll(0), 'Chrony process still alive') - finally: - os.close(pidfd) + observed = chrony_observation() boot = regular(Path('/proc/sys/kernel/random/boot_id'), 128).decode().strip() require(str(uuid.UUID(boot)) == boot, 'kernel boot identity') return {'docker_engine_version': json.loads(engine), 'builder': {key: image[key] for key in ('Id', 'Architecture', 'Os')}, 'runtime': runtime_witness(), 'boot_id': boot, - 'chrony_observation': {'schema_version': 1, 'daemon_pid': pid, - 'daemon_birth_ticks': int(birth), 'daemon_uid': ids['uid'], 'daemon_gid': ids['gid'], - 'daemon_image_sha256': image_hash}, - 'chrony_image_metadata': fixed, 'pins_provisioned': False, + 'chrony_observation': observed['pin'], 'chrony_binding': observed, + 'chrony_image_metadata': observed['fixed_images']['daemon'], 'pins_provisioned': False, 'quality_sampled': False, 'registry_cache_verified': False} @@ -637,8 +809,12 @@ def host(args): except Exception as error: # Fixed controlled messages only; never write exception repr, paths from secrets, nested errors or env. exclusive(phase / 'failure.json', encoded({'failed': True, 'exception_type': type(error).__name__, + 'failure_reason': str(error) if isinstance(error, OperatorDenied) else 'unexpected_operator_exception', 'read_original_case_only': True})) - raise + print(json.dumps({'case': case.name, 'phase': name, 'failed': True, + 'failure_reason': str(error) if isinstance(error, OperatorDenied) else 'unexpected_operator_exception', + 'read_original_case_only': True}, sort_keys=True)) + raise SystemExit(1) def main(): @@ -662,4 +838,10 @@ def main(): if __name__ == '__main__': - main() + try: + main() + except Exception: + # A failure writing the private receipt must not print nested exceptions. + print(json.dumps({'failed': True, 'failure_reason': 'unexpected_operator_exception', + 'read_original_case_only': True}, sort_keys=True)) + raise SystemExit(1) diff --git a/docs/server-time-source-bootstrap.md b/docs/server-time-source-bootstrap.md index 9325684..99eabd8 100644 --- a/docs/server-time-source-bootstrap.md +++ b/docs/server-time-source-bootstrap.md @@ -44,7 +44,7 @@ Archivhash separat. Ein neuer kanonischer UUIDv4-Fall liegt ausschließlich unte `/var/lib/rmm-time-source-bootstrap/` mit Root0700; jede Phase erzeugt ein eigenes Verzeichnis, einen create-only Intent und anschließend ein Ergebnis. Vorhandene Verzeichnisse/Intents werden nie wiederverwendet. Nach verlorener -Antwort ausschließlich `host-read --case ` benutzen, keine Wiederholung. +Antwort ausschließlich `python3 -I operator.py host-read --case ` benutzen, keine Wiederholung. 1. Lokal: `python3 deploy/server_time_source_bootstrap.py prepare --commit 2dfd791aa5703f42f0258d17bc9b5f6c9d71337f`. Das Ergebnis nennt den privaten @@ -101,10 +101,23 @@ und `10001:10001` verwenden. PostgreSQL darf keine publizierten Ports haben; sonstige publizierte Dockerports müssen ausschließlich IPv4-Loopback sein. Umgebungsvariablen, private Mountinhalte und Healthausgaben werden nicht gelesen. -Chrony wird über den vorhandenen `chrony.service`-MainPID identifiziert. -PIDFD, Prozessgeburtszeit, reale/effective UID/GID, tatsächlicher Executableinode, -geschütztes `/usr/sbin/chronyd` samt SHA256 und Boot-ID werden beobachtet und -erneut verglichen. Dies ist eine begrenzte frische Beobachtung für spätere +Der vorhandene `chrony.service` läuft hier als aktiver `Type=notify`-Dienst +mit einem Root-`/usr/bin/dash`-Wrapper als MainPID. Der Preflight bindet die +exakt drei tatsächlichen Prozesse seiner festen Cgroup +`/system.slice/chrony.service`: Wrapper, dessen Chronyd-Kind und dessen weiteres +Chronyd-Kind. Keine zusätzliche oder untergeordnete Cgroup wird übergangen. +Beide Chronyd-Prozesse müssen UID/GID989 und dieselben geschützten +`/usr/sbin/chronyd`-Originalbytes besitzen. Der Root:Root0644-single-link-Pidfile +`/run/chrony/chronyd.pid` bestimmt den Daemon; weil das Verzeichnis UID/GID989 +gehört, reicht diese Datei allein ausdrücklich nicht als Namensautorität. +Zusätzlich muss unter diesen drei Prozessen ausschließlich genau der gewählte +Daemon den tatsächlichen Kernelinode des festen Unix-Datagramsockets +`/run/chrony/chronyd.sock` halten. Diese begrenzte Beobachtung behauptet keine +globale Abwesenheit fremder FD-Kopien. Gehaltene pidfds, Proc-/Image-FDs, +Geburtszeiten, Elternbeziehungen, alle vier UID-/GID-Werte, Cgroupmitglieder, +Unitgeneration, Originaldateimetadaten und Socket-/Pidfileidentitäten werden +vorher/nachher verglichen. Unbekannte Topologien verweigern; kein Dienst wird +neugestartet oder automatisch neu übernommen. Boot-ID wird ebenfalls beobachtet. Dies ist eine begrenzte frische Beobachtung für spätere Root-Pinprovisionierung, **keine** Sourcequalitätsabnahme. Der Provider prüft später seine vollständigen gehaltenen Identitäten selbst. Das historische Kernelresultat aus früheren Fällen ist kein frischer Qualitätsbeleg. @@ -267,3 +280,24 @@ Dieser Metadatenbefund ersetzt keinen erfolgreichen vollständigen Preflight oder ARM64-Build. Kein Chrony-/RMM-Neustart, kein Zeitsetter, Pin, Schlüssel, Broker oder Serverdeploy wurde vorgenommen. Der fehlgeschlagene Fall bleibt erhalten und wird nicht erneut ausgeführt. + + +## Enger Preflightkorrekturstand, 2026-11-15 + +Der tatsächliche erste Server-Preflight des Falls +`d3be954b-518b-4232-bac4-5bf0a8f934cd` verweigerte korrekt bei +`actual daemon fixed executable inode`: Systemds MainPID war der legitime +Dash-Wrapper, nicht Chronyd. Anschließende ausschließlich lesende Rootbefunde +belegten die oben beschriebene Drei-Prozess-Topologie und den alleinigen +Command-Sockethalter innerhalb dieser Dienst-Cgroup. Der alte Fehlerfall bleibt +unverändert und darf nicht wiederholt werden. Die korrigierte Operatorquelle +benötigt ein neues geprüftes Paket und einen neuen Fall; daraus folgt noch +keine erfolgreiche ARM64-Ausführung oder Sourcequalitätsabnahme. + +Hostbefehle müssen mit `python3 -I /absoluter/pfad/operator.py ...` gestartet +werden: der Dateiname `operator.py` darf keine gleichnamigen Python-Standardmodule +über den Skriptpfad überschatten. Kontrollierte Guardfehler werden als feste +`failure_reason` in create-only `failure.json` und einer begrenzten JSON-Antwort +gespeichert; unerwartete Fehler liefern nur `unexpected_operator_exception`, +keinen Exceptiontext oder Traceback. Verlorene Antworten erlauben ausschließlich +Lesen des ursprünglichen Falls, keine Phasenwiederholung oder Pinheilung.