From 80906a3eb649d995a8fdc23c00c8277735e7f61e Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Fri, 13 Nov 2026 21:24:35 +0100 Subject: [PATCH] fix: disable stopped historical helper autostart before final VM reboot --- deploy/cloud_finalize_bootstrap_trust.py | 61 ++++++++++++++++++++++++ deploy/cloud_reboot_acceptance.py | 3 +- docs/reboot-acceptance.md | 16 +++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/deploy/cloud_finalize_bootstrap_trust.py b/deploy/cloud_finalize_bootstrap_trust.py index a27e3e7..e177fe0 100644 --- a/deploy/cloud_finalize_bootstrap_trust.py +++ b/deploy/cloud_finalize_bootstrap_trust.py @@ -227,3 +227,64 @@ def enable_general_autostart(payload): and retained() == original, 'enable only: same PIDs, boot, database inodes and Root consumption rows') return dict(report,applied=True,preflight=False,services_after=after,general_autostart_enabled=True, link_target=str(unit),state_preserved=True) + + +def legacy_helper_unit_metadata(payload): + import stat + p=Path('/etc/systemd/system/rmm-helper-dev-test.service');s=p.lstat() + require(stat.S_ISREG(s.st_mode) and s.st_uid==0 and not s.st_mode&0o022 and s.st_size<=16384,'owned historical unit') + raw=p.read_bytes();lines=[line.strip() for line in raw.decode().splitlines()] + commands=[line for line in lines if line.startswith('ExecStart=')] + shared='StateDirectory=rmm-helper' in lines + exact=commands==['ExecStart=/usr/local/libexec/rmm-privileged-helper'] + require(exact and shared,'known historical helper sharing current Root state') + return dict(unit_sha256=hashlib.sha256(raw).hexdigest(),root_owned=True, + fixed_helper_binary=exact,shared_root_state=shared, + clock_query_filters=[line for line in lines if line in ('SystemCallFilter=adjtimex clock_adjtime','SystemCallFilter=adjtimex')]) + + +def disable_legacy_helper_autostart(payload): + """Disable only the stopped historical dev helper; never stop/start a unit.""" + import contextlib + import sqlite3 + import stat + require(os.geteuid()==0,'root bootstrap required') + unit=Path('/etc/systemd/system/rmm-helper-dev-test.service') + s=unit.lstat() + require(stat.S_ISREG(s.st_mode) and s.st_uid==0 and not s.st_mode&0o022 + and hashlib.sha256(unit.read_bytes()).hexdigest()==payload['unit_sha256'], + 'exact owned historical helper unit') + state=Path('/var/lib/rmm-agent-remote') + require(json.loads((state/'identity.json').read_text())['agent_id']==payload['agent_id'], + 'permanent Cloud identity') + def inspect(): + services={} + for name in ('rmm-helper-dev-test.service','rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'): + raw=subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','MainPID','-p','UnitFileState'],capture_output=True,check=True,timeout=10).stdout.decode() + services[name]=dict(line.split('=',1) for line in raw.splitlines()) + result=dict(services=services,boot_id=Path('/proc/sys/kernel/random/boot_id').read_text().strip(),inodes={}) + for path in (state/'journal.sqlite',state/'consumption.sqlite',Path('/var/lib/rmm-helper/consumption.sqlite')): + m=path.stat();result['inodes'][str(path)]=[m.st_dev,m.st_ino] + with contextlib.closing(sqlite3.connect(path.as_uri()+'?mode=ro',uri=True)) as db: + require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','preserved journal integrity') + if path==state/'journal.sqlite': + require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'no active job during bootstrap') + else:require(db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0,'no unresolved consumption') + if path.parent==Path('/var/lib/rmm-helper'): + rows=db.execute('SELECT c.grant_id,c.result,e.start,e.outcome FROM consumed c JOIN execution_evidence e USING(grant_id) ORDER BY c.grant_id').fetchall() + result['root_rows_sha256']=hashlib.sha256(json.dumps([[hashlib.sha256(v).hexdigest() if isinstance(v,bytes) else v for v in row] for row in rows],separators=(',',':')).encode()).hexdigest() + return result + before=inspect();old=before['services']['rmm-helper-dev-test.service'] + require(old['ActiveState']=='inactive' and old['MainPID']=='0' and old['UnitFileState']=='enabled','only stopped enabled legacy helper') + require(all(before['services'][n]['ActiveState']=='active' and before['services'][n]['UnitFileState']=='enabled' + for n in ('rmm-agent-remote.service','rmm-helper-cloud.service')) + and before['services']['rmm-apt-helper.service']['ActiveState']=='inactive','current services already correct') + subprocess.run(['systemctl','disable','rmm-helper-dev-test.service'],capture_output=True,check=True,timeout=15) + after=inspect() + require(after['services']['rmm-helper-dev-test.service']['UnitFileState']=='disabled' + and all(after['services'][n]['MainPID']==v['MainPID'] and after['services'][n]['ActiveState']==v['ActiveState'] + for n,v in before['services'].items()) + and all(after[k]==before[k] for k in ('boot_id','inodes','root_rows_sha256')) + and hashlib.sha256(unit.read_bytes()).hexdigest()==payload['unit_sha256'],'disable only, original state preserved') + return dict(verified=True,before=before,after=after,jobs_created=False,service_restarted=False, + reboot_requested=False,legacy_autostart_disabled=True) diff --git a/deploy/cloud_reboot_acceptance.py b/deploy/cloud_reboot_acceptance.py index f8d0746..600cf8f 100644 --- a/deploy/cloud_reboot_acceptance.py +++ b/deploy/cloud_reboot_acceptance.py @@ -32,7 +32,8 @@ def vm_state(payload): require(not path.is_symlink() and hashlib.sha256(path.read_bytes()).hexdigest()==payload[key],'qualified current binaries') require(hashlib.sha256(Path('/etc/systemd/system/rmm-helper-cloud.service').read_bytes()).hexdigest()==payload['general_unit_sha256'],'reviewed General D-Bus service profile with direct reboot syscalls denied') services={} - for unit in ('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'): + for unit in ('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service', + 'rmm-agent.service','rmm-helper-dev-test.service'): raw=subprocess.run(['systemctl','show',unit,'-p','ActiveState','-p','MainPID','-p','UnitFileState'],capture_output=True,check=True,timeout=10).stdout.decode() services[unit]=dict(line.split('=',1) for line in raw.splitlines()) dropin=Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf') diff --git a/docs/reboot-acceptance.md b/docs/reboot-acceptance.md index 512482e..24e1511 100644 --- a/docs/reboot-acceptance.md +++ b/docs/reboot-acceptance.md @@ -1,5 +1,21 @@ # Letzte Linux-Cloud-VM-Rebootabnahme +Vor dem tatsächlichen Reboot neun bereits verifizierte frühere Gates an feste +JSON-/SHA-Referenzen gebunden. Root17/0offen/faultfrei, Zentrale29/29/25/0, +Agent/General aktiv+enabled, APTinactive/disabled und Endpunkt entfernt. +Zusätzliche tatsächliche Bootprüfung fand den **inaktiven, aber enabled** +historischen`rmm-helper-dev-test.service`, der denselben Root-StateDirectory +verwendet. Vor dem Reboot ausschließlich dessen Autostart deaktiviert, kein +Dienststart/-stop/-restart. Tatsächliche Root-ownedUnit/Binärpfad/Shared-State- +Metadaten zuerst geprüft; bestehende Anpassung`SystemCallFilter=adjtimex` +erklärt Abweichung vom Repo-UnitSHA. CAS mit unabhängig gelesenen tatsächlichen +Unitbytes, keine Änderung dieser Unit oder ihrer Konfiguration. Vor/nach +Disable gleicher Boot, aktuelle PIDs, drei DBinodes und Root-Verbrauchszeilen. +Private Belege unter`devstate/cloud-reboot/d015c3ef-f3aa-4522-a3c2-fcb6099df17b/` +mit`pre-reboot-unit-state.json`, `legacy-unit-metadata.json` und +`legacy-autostart-disabled.json`. Noch keine OS-Rebootwirkung durch diese +administrativen Vorbereitungen. + **Vorbereitet, noch kein nativer Reboot ausgeführt.** Dieser Schritt darf erst nach den APT-, Offline-, Crash-/Redelivery-, Transportfallback- und MCP-Gates folgen. Die frühere Autorisierung des einzelnen Test-VM-Reboots ersetzt keine