diff --git a/.gitignore b/.gitignore index 7034434..2f0d5b8 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ __pycache__/ /web/test-results/ /web/playwright-report/ /.playwright/ +/output/playwright/ +/.playwright-cli/ diff --git a/Rust_RMM_Implementierungsspezifikation.md b/Rust_RMM_Implementierungsspezifikation.md index 77a1b72..7505da6 100644 --- a/Rust_RMM_Implementierungsspezifikation.md +++ b/Rust_RMM_Implementierungsspezifikation.md @@ -1,6 +1,6 @@ # Rust RMM: Architektur und Implementierungsspezifikation -Version 1.18 · 2026-11-14 · Sprache: Deutsch +Version 1.20 · 2026-11-14 · Sprache: Deutsch **Zweck:** Vollständige, eigenständig nutzbare Grundlage für die Implementierung einer mandantenfähigen Plattform zur Inventarisierung, Überwachung und Fernadministration von Kundengeräten durch eine Coding-KI und ein Entwicklungsteam. @@ -15,6 +15,12 @@ Version 1.18 · 2026-11-14 · Sprache: Deutsch lokaler Helperpolicy präzisiert. Keine Vererbung von Linux-Rootrechten, keine Windowsfähigkeit aus einer bloßen Quellen-/Build-/Linux-Unitqualifikation. +1.18 → 1.19 (2026-11-14): Nächster Windows-M4-Schnitt als ausdrücklicher +Opt-in für zwei eigene feste MSI-Rezepte (1.0 und1.0→1.1) präzisiert: +unveränderlicher Tenantkatalog, exakte automatische Job-/Artefaktbindung, +getrennte SYSTEM-Paketerlaubnis und unabhängige Nachbedingungen. Kein freier +Installer, kein automatisches Update/Repair, keine implizite EXE-/Rebootfreigabe. + 1.17 → 1.18 (2026-11-14): Ausdrückliche Userfreigabe für die feste, vor der WebUI-Genehmigung sichtbare RemoteSigned-Option je Windows-PowerShell5.1- Jobprozess, gezieltes Starten des bestehenden W32Time ohne Konfigurationsänderung @@ -27,6 +33,14 @@ Abandon mit Originalgrant, unabhängiger Helper-Lineage und vollständiger Snapshotbindung präzisiert. Mehrdeutige Übergaben bleiben Unknown; keine Abschwächung der vorhandenen Sync-/Verbrauchsinvarianten. +1.19 → 1.20 (2026-11-14): Der User priorisiert die vollständige, einzeln +WebUI-genehmigte Shell-/PowerShell-Skriptkonsole für KI/MCP und Menschen. +Ein ausdrücklich gebundenes Profil `full_maintenance_v1` ermöglicht nach +getrennter Qualifikation Root-/SYSTEM-Hostwartung; bisherige begrenzte +Dienst-/Paketprofile erhalten dadurch keine zusätzliche Erlaubnis. Batchstatus +und abschließende begrenzte Rohbyteausgabe verwenden die bestehende Jobstrecke; +interaktive PTY-/ConPTY-Sitzungen bleiben späterer eigener Umfang. + ## Inhaltsverzeichnis - [0 Auftrag und verbindliche Produktanforderungen](#0-auftrag-und-verbindliche-produktanforderungen) @@ -116,6 +130,106 @@ Deploymentdaten, offene Konfiguration und reale Nachweise: ### 0.1 Auftrag an die implementierende KI +**Userentscheidung Version1.20 — vollständige genehmigte Skriptkonsole zuerst:** +Die KI darf über MCP gespeicherte Geräte-/Telemetriedaten im erlaubten Mandanten +lesen, vollständige Shell-/PowerShell-Skripte schreiben und feste Aufträge als +`pending_approval` beantragen. Ein berechtigter Mensch prüft und genehmigt in +der WebUI die gespeicherten exakten Skriptbytes, Interpreter, Argumente, Ziele, +Versuche, Identität und Grenzen. Erst dann darf der Agent ausführen. Der User +wählt zunächst die Batch-Skriptkonsole mit Zustandsverfolgung und abschließender +Ausgabe, ausdrücklich auch mit Root-/SYSTEM-Rechten zur vollständigen +Hostwartung. Diese Strecke hat Vorrang vor weiteren Paketfixture-Erweiterungen. +Die bisherige Einordnung interaktiver Terminals in M7 verschiebt diese +Batchkonsole nicht; PTY/ConPTY, neue Sitzungen und laufendes Outputstreaming +werden hierfür nicht eingeführt. Standardinventar/Heartbeats bleiben passiv; +angeforderte Live-Skripte bleiben Jobs, auch wenn sie nur Logs lesen. + +`run_script` erhält das additive optionale Stringfeld +`privileged_script_profile="full_maintenance_v1"`. Fehlend wird es vollständig +ausgelassen und erhält sämtliche alten Codec-/Hashbytes. Explizites `null`, +unbekannte Werte und ein Profil bei `agent_service` sind ungültig. Das Profil +gehört vor Genehmigung in die exakten Aktions-, Job- und Planbytes und wird von +Signatur/Grant, zentraler Policy und lokaler Helperprüfung gebunden. Keine +nachträgliche Hülle, implizite Profilauswahl oder Erweiterung alter Pläne. +Windows-SYSTEM-Skripte benötigen dieses Profil; alte SYSTEM-SCM-/MSI-Policies +erlauben sie weiterhin nicht. Historische Linux-Root-Skripte ohne Profil behalten +ihren bisherigen begrenzten Ausführungsvertrag und werden nicht Vollwartung. + +Planersteller und Freigeber benötigen jeweils aktuell `scripts:execute` und +zusätzlich genau `root:execute` beziehungsweise `system:execute`; beide +Identitäten bleiben getrennt. Das Profil braucht zusätzlich ein ausdrückliches, +versioniertes zentrales und lokales Opt-in samt exakten Geräte-/Generations-/ +Principal-/Helperhashbindungen. Zentrale Linux-Policyversion2 und lokale +General-Policyversion2 können `allowed_script_profiles` mit genau dem +bekannten Profil enthalten; Windows-SYSTEM-Policyversion3 kann es getrennt von +SCM und MSI enthalten. Fehlend bleibt deny für Vollwartung; vorhandene `null`- +oder unbekannte Profile verweigern. Qualifizierte Capability +`script.full_maintenance/1` kommt zusätzlich zu Aktion, Interpreter, +`root:run_script` beziehungsweise `system:run_script`, Helperhash und signiertem +Sync hinzu. Quelle, Build oder Policydatei allein veröffentlichen sie nicht. + +Für das neue Linux-Fullprofil ist zusätzlich `helper_binary_sha256` im +Schema2-Policy-/Pin-/zentralen Scope verpflichtend. Der Roothelper beobachtet +sein eigenes tatsächlich laufendes Image über einen gehaltenen Dateideskriptor +und prüft es vor Probe/neuer Zulassung gegen den geschützten Pin. Der Agent +verifiziert diese Rootpeer-Probe, ohne fremde Root-/proc-Dateien lesen zu müssen, +und meldet nur dann `helper.binary.sha256:`; die Zentrale verlangt denselben +Hash. Neue optionale Metadaten bleiben bei General/APT vollständig ausgelassen; +Schema1-Pins und deren alte Byte-/Hashverträge bleiben unverändert. + +Linux erhält hierfür ein getrenntes explizites Helper-Ausführungsprofil +`full_maintenance_v1`; bestehende General-/APT-Sandboxes bleiben erhalten. +Seine systemd-Start-/Dateisystem-/Netzwerk-/Capabilitybedingungen müssen die +zugesagte Hostwartung tatsächlich erlauben und vor Zieländerung überprüfbar +feststehen. Windows führt mit dem eigenen SYSTEM-Token (SID18) aus, niemals +mit einem nur umbenannten LocalService-Prozess (SID19). Beide verwenden weiter +die unabhängige Originalsignaturprüfung, Peer-/Clock-/Boot-/Sync-/Fenceprüfung +und dauerhaften atomaren Einmalverbrauch. Windows bewahrt zusätzlich Intent, +Dispatchmarker, unabhängigen RawMirror und deny-only Abandon ohne Dateierzeugung +oder Wirkung. Skriptdateien entstehen erst nach erfolgreichem Helperverbrauch +in einem vom jeweiligen Helper geschützten Bereich. Journalidentitäten, +File-IDs/Inodes, Schlüssel und vorhandene Verbräuche bleiben erhalten; keine +Ledgerresets zur Einführung des Profils. + +Die erste Vollwartungsstrecke erlaubt 1..32768 exakte UTF-8-Skriptbytes, +höchstens32 Argumente gemäß bestehendem Vertrag und insgesamt höchstens65536 +stdout-/stderr-Rohbytes. Laufzeit bleibt ausdrücklich begrenzt: Linux1..300s, +Windows1..120s, zusätzlich zur engeren genehmigten/lokalen Grenze. Interpreter +und argv bleiben fest (`/bin/sh` oder qualifiziertes `/bin/bash` mit privater +Datei; Windows PowerShell5.1 mit `-NoLogo -NoProfile -NonInteractive -File`). +Die getrennte explizite, vor Genehmigung gebundene Prozessoption `RemoteSigned` +aus1.18 darf hinzukommen; kein Executorfallback oder Maschinen-/GPO-Override. +Encoding/BOM/Zeilenenden werden nach Genehmigung nicht verändert. + +**Vollwartung ist volle Hostautorität:** Ein genehmigtes Root-/SYSTEM-Skript +kann Dateien, Netzwerk, Konten, Dienste, Agent und Schlüsselbestand ändern und +auch einen Neustart auslösen. Kein Sandbox-, No-Reboot-, Rollback- oder +Geheimnisschutzversprechen für beliebigen privilegierten Code. Nachgeladene +Programme/Netzwerkdaten sind ohne eigenen Vertrag nicht automatisch auf ihren +Inhalt festgelegt. Der Plan zeigt diese Autorität ausdrücklich. Dies ersetzt +keine einzelne Genehmigung und autorisiert keine unangekündigten destruktiven +Abnahmen. Ein Skriptneustart ist keine typisierte Rebootabnahme; fehlender +Abschluss bleibt `unknown` und wird nach Wiederkontakt nicht automatisch erneut +versucht. Exit0 belegt Prozessabschluss, keine beliebige fachliche Wirkung. + +Abbruch/Fence/Timeout müssen während der Ausführung beobachtet und ehrlich +berichtet werden. Nur eigene Prozessgruppe beziehungsweise atomar zugewiesenes +Windows-JobObject und erhaltene Handles dürfen beendet werden. Keine Übernahme +fremder PIDs, kein globaler Kill und kein Anspruch, dass ein privilegiertes +Skript keine unabhängigen Dienste/Tasks erzeugen kann. Bereits eingetretene +Teilwirkung bleibt möglich; unklarer Abschluss bleibt `unknown`. Eigene laufende +Kinder/Handles und lokale Sperre bleiben bis belegtem Abschluss kontrolliert; +ein spätes Exit0 heilt keinen Timeout/Abbruch. Bestehende finale Rohbytes samt +Trunkierung werden unverändert gespeichert; WebUI/MCP erhalten die gespeicherten +Plan-/Ergebnisdaten im berechtigten Mandanten. Vollständige Skriptanzeige vor +Genehmigung und sichere Textdarstellung der abschließenden Ausgabe sind Pflicht, +kein behauptetes Livestreaming. + +Dieser Abschnitt normiert den Vertrag und ersetzt keine Quellen-, Build-, +VM- oder Capabilitynachweise; aktuelle getrennte Belege stehen in +`deploy/linux/full-maintenance-README.md`. Zuerst Linux-Cloud-VM, +anschließend getrennte native Windows-VM-Abnahme; Notebook nur Entwicklung. + **Ausdrückliche Userentscheidungen Version1.18 — begrenzte Windowsfreigaben:** Für jeden zuvor mit seinem unveränderlichen Plan in der WebUI genehmigten Windows-PowerShell5.1-Jobprozess darf die feste Option `RemoteSigned` ausschließlich @@ -141,6 +255,50 @@ keinen Rebootjob-Abnahmenachweis. Dokumentierte Freigaben sind keine bereits ausgeführten Schritte. Neue Capabilities werden erst nach ihrer getrennten nativen Qualifikation eröffnet. +**Technische Präzisierung Version1.19 — enger Windows-M4-MSI-Schnitt:** +Nach dem getrennten SCM-Vertrag wird `ensure_package` ausschließlich für die +eigenen festen Machine-/x64-MSI-Rezepte1.0 und1.0→1.1 erweitert. Keine freie +SYSTEM-Shell, URL, Transformdatei, Propertyliste oder Ableitung von Linux-Root. +Ein optionales, bei Vorhandensein streng typisiertes `windows_msi_manifest` +bindet das vollständige kanonische Rezept samt Hash in der Action; fehlend bleibt +bei alten Linuxjobs ausgelassen. APT- und Windowsmanifest schließen einander aus. +Äußere Paket-/Versions-/Kontextfelder, Plattform und SYSTEM-Identität müssen +exakt zum Rezept passen. Sämtliche historischen Linux-Codecbytes bleiben erhalten. + +Der Windowskatalog ist eigenständig, unveränderlich und mandantengetrennt; seine +Artefaktreferenz benötigt denselben Tenant. Pro konkretem Job/Versuch entsteht +automatisch genau die eine Referenz aus dem genehmigten Rezept: Artefakt-ID, +SHA256 und Länge. Freie zusätzliche Artefakt-IDs sind im MSI-Pfad verweigert. +Fehlende, zusätzliche oder doppelte Referenzen verweigern Revalidation, +Startgrant und Download. Der bestehende geräte-mTLS-/Job-/Versuch-gebundene +Artefaktendpoint bleibt maßgeblich; Bootstrap-SFTP ist keine Paketabnahme. + +Zentrale und lokale SYSTEM-Policy erweitern ihre bisherige SCM-Erlaubnis nur +ausdrücklich versioniert um exakte MSI-Rezepthashes und eigene Paketrechte/ +Capabilities. Alte SCM-only-Profile erteilen keine Paketerlaubnis. Vor WebUI- +Genehmigung stehen SYSTEM/Machine/x64, Rezept-/Artefakthashes, Produkt-/Upgrade- +und beim1.1-Rezept PackageCode, feste Version/Nutzdatei, Vorgänger, feste Argumente und +No-Reboot-Vertrag sichtbar aus den unveränderlichen Planbytes fest. Clock, +signierter unabhängiger Helpersync, Plan/Grant und dauerhafter Einmalverbrauch +bleiben vor Installerwirkung zwingend. Der Helper kopiert ausschließlich gegen +das signierte Rezept geprüfte Bytes in seinen geschützten Cache; keine vom +Netzwerkagenten frei gewählten Pfade/Programme oder allgemeinen Schreiboperationen. + +Der getrennte Nichtdispatch-/Abandon-/RawMirror-Vertrag gilt additiv auch für +diese konkrete typisierte Action, ohne Cache-/Provider-/Installerwirkung durch +Abandon. Nach möglicher Übergabe gibt es keinen Execute-Retry. MSI0/3010 ist +nur mit unabhängiger innerhalb des ursprünglichen Budgets beobachteter +Nachbedingung erfolgreich;3010 meldet Neustartbedarf ohne Neustartstart. +1641 bleibt Unknown,1618 bekannter Konflikt ohne Retry. Unklarer Timeout/Abbruch +bleibt Unknown; kein globales Beenden von Windows Installer. Auch Noop benötigt +gültige Clock/Fence, Abbruchprüfung und rechtzeitigen unabhängigen Beleg. +Upgrade1.1 setzt genau den bekannten1.0-Vorgänger voraus; danach1.1 installiert, +1.0 entfernt und Nutzdatei exakt. Derselbe1.1-Rezept-/PackageCode erlaubt einen +separat genehmigten echten Noop, kein implizites Repair/Downgrade/Fresh1.1. +Quellen, Builds und Units eröffnen keine Capability oder Policy. Native +Installation/Upgrade/Noop erfolgen jeweils ausschließlich nach tatsächlicher +WebUI-Genehmigung auf der Windows-VM; Linux-first-Vertragsnachweise zuerst. + **Technische Präzisierung Version 1.17 — Nichtstart vor SYSTEM-Delegation:** Vor jeder Socketübergabe bindet ein dauerhafter Intent die unveränderten Originalrequest-/Job-/Grant-/Signaturbytes, Plan/Autorisierung, Gerät und diff --git a/crates/agent-core/src/auth.rs b/crates/agent-core/src/auth.rs index c321b13..736c965 100644 --- a/crates/agent-core/src/auth.rs +++ b/crates/agent-core/src/auth.rs @@ -33,8 +33,9 @@ impl Binding { ); ensure!( j.action.validate() - && (1..=3600).contains(&j.max_runtime_seconds) - && (1..=262144).contains(&j.max_output_bytes) + && j.action.valid_execution_identity(&j.execution_identity) + && j.action + .valid_job_limits(j.max_runtime_seconds, j.max_output_bytes) && j.not_before < j.start_before, "INVALID_JOB" ); @@ -72,8 +73,7 @@ impl Binding { ensure!(permit_window(&job, &permit), "PERMIT_TIME"); ensure!( job.execution_identity == identity.as_str() - && matches!(&job.action, rmm_domain::jobs::Action::SetServiceState { state, .. } - if matches!(state.as_str(), "running" | "stopped")), + && job.action.valid_helper_denial_shape(identity.as_str()), "DENIAL_HELPER_SCOPE" ); Ok(VerifiedHistoricalDenial { diff --git a/crates/agent-core/tests/contracts.rs b/crates/agent-core/tests/contracts.rs index 806c8b3..f6d05f2 100644 --- a/crates/agent-core/tests/contracts.rs +++ b/crates/agent-core/tests/contracts.rs @@ -40,6 +40,8 @@ fn fixture() -> (Binding, Request, ExecutionPermitPayload, DateTime) { script_utf8: "printf 'Hello\\n'\n".into(), arguments: vec![], expected_stdout: Some("Hello\n".into()), + windows_execution_profile: None, + privileged_script_profile: None, }, execution_identity: "agent_service".into(), not_before: now - Duration::minutes(1), @@ -170,6 +172,70 @@ fn exact_permit_bytes_time_and_all_identity_hash_bindings_are_required() { let mut changed = request; changed.signature.truncate(63); assert!(binding.verify(&changed, now).is_err()); + // Changing only the process policy is a payload mutation, even with the + // same script/attempt and a still valid original signature. + let (_, mut request, mut permit, _) = fixture(); + let mut job: JobPayload = serde_json::from_slice(&request.job_bytes).unwrap(); + job.action = Action::RunScript { + interpreter: "powershell".into(), + script_utf8: "Write-Output 'Hello'\r\n".into(), + arguments: vec![], + expected_stdout: Some("Hello\r\n".into()), + windows_execution_profile: None, + privileged_script_profile: None, + }; + request.job_bytes = encode(&job).unwrap(); + permit.job_payload_hash = hash(&request.job_bytes); + request.permit_bytes = encode(&permit).unwrap(); + request.signature = key.sign(&request.permit_bytes).to_bytes().to_vec(); + binding.verify(&request, now).unwrap(); + if let Action::RunScript { + windows_execution_profile, + .. + } = &mut job.action + { + *windows_execution_profile = + Some(rmm_domain::jobs::WindowsExecutionProfile::PowerShell51RemoteSigned); + } + request.job_bytes = encode(&job).unwrap(); + assert!(binding.verify(&request, now).is_err()); + permit.job_payload_hash = hash(&request.job_bytes); + request.permit_bytes = encode(&permit).unwrap(); + request.signature = key.sign(&request.permit_bytes).to_bytes().to_vec(); + binding.verify(&request, now).unwrap(); + // The exact fixed Event Log source/window/limits are original signed bytes. + job.action = Action::QueryLogs { + unit: rmm_domain::jobs::WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(), + since: now - chrono::Duration::hours(1), + until: now, + max_lines: 1000, + }; + request.job_bytes = encode(&job).unwrap(); + permit.action = job.action.name().into(); + permit.job_payload_hash = hash(&request.job_bytes); + request.permit_bytes = encode(&permit).unwrap(); + request.signature = key.sign(&request.permit_bytes).to_bytes().to_vec(); + binding.verify(&request, now).unwrap(); + let mut wrong_source = job.clone(); + if let Action::QueryLogs { unit, .. } = &mut wrong_source.action { + *unit = "Security".into(); + } + let mut changed = request.clone(); + changed.job_bytes = encode(&wrong_source).unwrap(); + assert!(binding.verify(&changed, now).is_err()); + for (runtime, bytes) in [(9, 4096), (121, 4096), (10, 1023), (10, 65537)] { + let mut changed_job = job.clone(); + changed_job.max_runtime_seconds = runtime; + changed_job.max_output_bytes = bytes; + let mut changed_permit = permit.clone(); + changed_permit.max_runtime_seconds = runtime; + let mut changed = request.clone(); + changed.job_bytes = encode(&changed_job).unwrap(); + changed_permit.job_payload_hash = hash(&changed.job_bytes); + changed.permit_bytes = encode(&changed_permit).unwrap(); + changed.signature = key.sign(&changed.permit_bytes).to_bytes().to_vec(); + assert!(binding.verify(&changed, now).is_err()); + } } #[cfg(unix)] @@ -416,3 +482,96 @@ fn completion_cas_preserves_original_bytes_and_recovers_only_verified_bound_evid .is_err() ); } + +#[test] +fn historical_msi_nonstart_keeps_full_signature_binding_and_never_becomes_root_authority() { + use rmm_agent_core::auth::HelperIdentity; + use rmm_domain::windows_execution::*; + let (binding, mut request, mut permit, now) = fixture(); + let mut job: JobPayload = serde_json::from_slice(&request.job_bytes).unwrap(); + let recipe = WindowsMsiRecipe { + schema_version: 1, + recipe_id: MSI_RECIPE_ID.into(), + recipe_version: "1".into(), + platform: "windows".into(), + upgrade_code: MSI_UPGRADE_CODE.into(), + architecture: Architecture::X64, + context: InstallContext::Machine, + command_profile: MsiCommandProfile::FixtureQuietNoRebootV1, + artifact: MsiArtifact { + artifact_id: Uuid::from_u128(19), + sha256: "ab".repeat(32), + bytes: 32, + }, + postcondition: MsiPostcondition { + product_code: MSI_PRODUCT_CODE.into(), + product_version: MSI_VERSION.into(), + payload_path: MSI_PAYLOAD.into(), + payload_sha256: MSI_PAYLOAD_10_SHA256.into(), + payload_bytes: MSI_PAYLOAD_BYTES, + }, + no_reboot: true, + package_code: None, + predecessor: None, + }; + let manifest = ResolvedWindowsMsi { + recipe_sha256: recipe.sha256().unwrap(), + recipe, + }; + job.action = Action::EnsurePackage { + provider: "msi".into(), + package: MSI_PACKAGE_ID.into(), + version: MSI_VERSION.into(), + architecture: "x64".into(), + recipe_version: "1".into(), + installation_context: "machine".into(), + recipe_id: Some(MSI_RECIPE_ID.into()), + apt_manifest: None, + windows_msi_manifest: Some(Box::new(manifest)), + }; + job.execution_identity = "system".into(); + request.job_bytes = encode(&job).unwrap(); + permit.action = "ensure_package".into(); + permit.execution_identity = "system".into(); + permit.job_payload_hash = hash(&request.job_bytes); + request.permit_bytes = encode(&permit).unwrap(); + request.signature = SigningKey::from_bytes(&[9; 32]) + .sign(&request.permit_bytes) + .to_bytes() + .to_vec(); + binding + .verify_historical_for_denial_with( + &request, + HelperIdentity::System, + &rmm_agent_core::crypto::DalekVerifier, + ) + .unwrap(); + assert!( + binding + .verify_historical_for_denial_with( + &request, + HelperIdentity::Root, + &rmm_agent_core::crypto::DalekVerifier + ) + .is_err() + ); + assert!(binding.verify(&request, now + Duration::hours(1)).is_err()); // historical denial is never live authorization + if let Action::EnsurePackage { + windows_msi_manifest: Some(m), + .. + } = &mut job.action + { + m.recipe.artifact.bytes += 1; + m.recipe_sha256 = m.recipe.sha256().unwrap(); + } + request.job_bytes = encode(&job).unwrap(); + assert!( + binding + .verify_historical_for_denial_with( + &request, + HelperIdentity::System, + &rmm_agent_core::crypto::DalekVerifier + ) + .is_err() + ); +} diff --git a/crates/agent-core/tests/helper_reconcile.rs b/crates/agent-core/tests/helper_reconcile.rs index dcd0943..c1f091d 100644 --- a/crates/agent-core/tests/helper_reconcile.rs +++ b/crates/agent-core/tests/helper_reconcile.rs @@ -867,6 +867,8 @@ fn script() -> Action { script_utf8: "printf hello".into(), arguments: vec![], expected_stdout: Some("hello".into()), + windows_execution_profile: None, + privileged_script_profile: None, } } fn success() -> ReconciledOutcome { diff --git a/crates/agent-core/tests/reconcile.rs b/crates/agent-core/tests/reconcile.rs index 2747938..df4b492 100644 --- a/crates/agent-core/tests/reconcile.rs +++ b/crates/agent-core/tests/reconcile.rs @@ -605,6 +605,8 @@ fn script() -> Action { script_utf8: "printf hello".into(), arguments: vec![], expected_stdout: Some("hello".into()), + windows_execution_profile: None, + privileged_script_profile: None, } } fn success() -> ReconciledOutcome { diff --git a/crates/agent-linux/src/actions.rs b/crates/agent-linux/src/actions.rs index 3bf4cf6..58a1f4b 100644 --- a/crates/agent-linux/src/actions.rs +++ b/crates/agent-linux/src/actions.rs @@ -85,6 +85,12 @@ impl ScriptRequest { } pub fn run_script(request: &ScriptRequest) -> Result { + run_script_controlled(request, || Ok(())) +} +pub fn run_script_controlled( + request: &ScriptRequest, + observe: impl FnMut() -> Result<()>, +) -> Result { ensure!( request.interpreter.available(), "UNSUPPORTED_CAPABILITY: interpreter unavailable" @@ -115,11 +121,12 @@ pub fn run_script(request: &ScriptRequest) -> Result { path.to_str().context("non-UTF8 temp path")?.to_owned(), ]; args.extend(request.arguments.clone()); - process::run( + process::run_controlled( request.interpreter.path(), &args, request.timeout, request.max_output_bytes, + observe, ) } diff --git a/crates/agent-linux/src/process.rs b/crates/agent-linux/src/process.rs index 9483afe..0b1a073 100644 --- a/crates/agent-linux/src/process.rs +++ b/crates/agent-linux/src/process.rs @@ -15,11 +15,19 @@ pub struct Output { pub stderr: Vec, pub truncated: bool, pub timed_out: bool, + pub interrupted: bool, } -// Nonblocking pipes avoid both unbounded output and reader threads held open by -// grandchildren. The child has a separate process group, cleaned even on success. pub(crate) fn run(program: &str, args: &[String], timeout: Duration, cap: usize) -> Result { + run_controlled(program, args, timeout, cap, || Ok(())) +} +pub(crate) fn run_controlled( + program: &str, + args: &[String], + timeout: Duration, + cap: usize, + mut observe: impl FnMut() -> Result<()>, +) -> Result { ensure!(!timeout.is_zero() && cap > 0, "invalid process limits"); let mut command = Command::new(program); command @@ -34,144 +42,260 @@ pub(crate) fn run(program: &str, args: &[String], timeout: Duration, cap: usize) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .process_group(0); + observe()?; let mut child = command .spawn() .with_context(|| format!("spawn {program}"))?; - struct Group(u32); + // Keep the group leader unreaped until AFTER the last signal and cleanup. + // Its reserved PID prevents the numerical PGID from becoming a foreign group. + struct Group { + pid: u32, + reserved: bool, + } + impl Group { + fn kill(&self) { + if self.reserved { + unsafe { + libc::kill(-(self.pid as i32), libc::SIGKILL); + } + } + } + } impl Drop for Group { fn drop(&mut self) { - // SAFETY: negative PID addresses only the group created for this child. - unsafe { - libc::kill(-(self.0 as i32), libc::SIGKILL); - } + self.kill(); } } - let group = Group(child.id()); + let mut group = Group { + pid: child.id(), + reserved: true, + }; let mut stdout = child.stdout.take().context("missing stdout")?; let mut stderr = child.stderr.take().context("missing stderr")?; - for fd in [stdout.as_raw_fd(), stderr.as_raw_fd()] { - // SAFETY: these are live owned pipe descriptors; flags are not pointers. - let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) }; - if flags < 0 || unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 { - drop(group); - let _ = child.wait(); - return Err(std::io::Error::last_os_error().into()); - } - } - fn drain( - pipe: &mut impl Read, - target: &mut Vec, - remaining: &mut usize, - truncated: &mut bool, - ) -> Result { - let mut bytes = [0; 4096]; - // Bound each pass too: a continuously writing child cannot starve timeout. - for _ in 0..16 { - match pipe.read(&mut bytes) { - Ok(0) => return Ok(true), - Ok(n) => { - let keep = n.min(*remaining); - target.extend_from_slice(&bytes[..keep]); - *remaining -= keep; - *truncated |= keep < n; - } - Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break, - Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue, - Err(e) => return Err(e.into()), - } - } - Ok(false) - } - let started = Instant::now(); - let mut result = Output { + let began = Instant::now(); + let mut output = Output { code: None, stdout: vec![], stderr: vec![], truncated: false, timed_out: false, + interrupted: false, }; let mut remaining = cap; - let status = loop { - let drained = drain( - &mut stdout, - &mut result.stdout, - &mut remaining, - &mut result.truncated, - ) - .and_then(|_| { + let mut failure = None; + for fd in [stdout.as_raw_fd(), stderr.as_raw_fd()] { + let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) }; + if flags < 0 || unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 { + failure = Some(anyhow::Error::from(std::io::Error::last_os_error())); + break; + } + } + if failure.is_none() { + loop { + if let Err(e) = drain( + &mut stdout, + &mut output.stdout, + &mut remaining, + &mut output.truncated, + ) + .and_then(|_| { + drain( + &mut stderr, + &mut output.stderr, + &mut remaining, + &mut output.truncated, + ) + }) { + failure = Some(e); + break; + } + // WNOWAIT deliberately observes without releasing this child's PID. + let mut info = std::mem::MaybeUninit::::zeroed(); + let state = unsafe { + libc::waitid( + libc::P_PID, + child.id(), + info.as_mut_ptr(), + libc::WEXITED | libc::WNOHANG | libc::WNOWAIT, + ) + }; + if state < 0 { + let e = std::io::Error::last_os_error(); + if e.kind() == std::io::ErrorKind::Interrupted { + continue; + } + if e.raw_os_error() == Some(libc::ECHILD) { + group.reserved = false; + } + failure = Some(e.into()); + break; + } + if unsafe { info.assume_init().si_pid() } != 0 { + break; + } + let fenced = observe().is_err(); + if began.elapsed() >= timeout || fenced { + output.timed_out = began.elapsed() >= timeout; + output.interrupted = fenced; + break; + } + thread::sleep(Duration::from_millis(5)); + } + } + if !group.reserved { + // An unexpected external reap destroyed the group identity proof. Never + // signal/adopt a numerical replacement; caller records Unknown. + return Err(failure.unwrap_or_else(|| anyhow::anyhow!("OWNED_GROUP_IDENTITY_LOST"))); + } + group.kill(); + let cleanup = Instant::now(); + loop { + // A setup error may have left a blocking pipe. It must not become an + // unbounded cleanup read; retain only group ownership in that path. + let out_closed = if failure.is_some() { + Ok(true) + } else { + drain( + &mut stdout, + &mut output.stdout, + &mut remaining, + &mut output.truncated, + ) + }; + let err_closed = if failure.is_some() { + Ok(true) + } else { drain( &mut stderr, - &mut result.stderr, + &mut output.stderr, &mut remaining, - &mut result.truncated, + &mut output.truncated, ) - }); - if let Err(e) = drained { - drop(group); - let _ = child.wait(); - return Err(e); + }; + if out_closed.is_err() || err_closed.is_err() { + output.truncated = true; } - match child.try_wait() { - Ok(Some(status)) => break status, - Ok(None) => {} - Err(e) => { - drop(group); - let _ = child.wait(); - return Err(e.into()); + // Only zombies may remain while the *owned* leader stays unreaped. + // They cannot execute or retain pipe writers. Unclear /proc state keeps + // this owner and the caller's executor lock instead of a replacement. + if group_has_no_live_members(group.pid) { + if out_closed.unwrap_or(true) && err_closed.unwrap_or(true) { + break; + } + // A privileged script may deliberately give a pipe to an unrelated + // process. That process is outside our ownership; never kill it. + if cleanup.elapsed() >= Duration::from_millis(250) { + output.truncated = true; + break; } } - if started.elapsed() >= timeout { - result.timed_out = true; - // SAFETY: this is the dedicated group created for this child. - unsafe { - libc::kill(-(child.id() as i32), libc::SIGKILL); - } - break child.wait()?; - } - thread::sleep(Duration::from_millis(5)); - }; - let pgid = child.id(); - drop(group); - // SIGKILL delivery is asynchronous. A single WouldBlock is not EOF and - // does not prove that descendants have stopped. Bound both cleanup waiting - // and output draining, and withhold a successful exit code if unconfirmed. - let cleanup_started = Instant::now(); - loop { - let stdout_closed = drain( - &mut stdout, - &mut result.stdout, - &mut remaining, - &mut result.truncated, - )?; - let stderr_closed = drain( - &mut stderr, - &mut result.stderr, - &mut remaining, - &mut result.truncated, - )?; - // SAFETY: signal zero only probes the dedicated process group. - let absent = unsafe { libc::kill(-(pgid as i32), 0) } == -1 - && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH); - if stdout_closed && stderr_closed && absent { - result.code = status.code(); - break; - } - if cleanup_started.elapsed() >= Duration::from_millis(250) { - result.truncated |= !stdout_closed || !stderr_closed; - // None explicitly means no confirmed successful completion. This - // can also occur for lingering zombies: never guess their state. - result.code = None; - break; + if cleanup.elapsed() >= Duration::from_millis(250) { + output.timed_out = true; } thread::sleep(Duration::from_millis(5)); } - Ok(result) + // Disarm BEFORE releasing the reserved leader PID: no later Drop can signal + // a reused PGID. All assigned living members have already ended. + group.reserved = false; + let status = child.wait()?; + if let Some(e) = failure { + return Err(e); + } + output.interrupted |= observe().is_err(); + output.code = if output.timed_out || began.elapsed() >= timeout { + None + } else { + status.code() + }; + Ok(output) +} +fn drain( + pipe: &mut impl Read, + target: &mut Vec, + remaining: &mut usize, + truncated: &mut bool, +) -> Result { + let mut bytes = [0; 4096]; + for _ in 0..16 { + match pipe.read(&mut bytes) { + Ok(0) => return Ok(true), + Ok(n) => { + let keep = n.min(*remaining); + target.extend_from_slice(&bytes[..keep]); + *remaining -= keep; + *truncated |= keep < n; + } + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break, + Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue, + Err(e) => return Err(e.into()), + } + } + Ok(false) +} +fn group_has_no_live_members(pgid: u32) -> bool { + let Ok(entries) = std::fs::read_dir("/proc") else { + return false; + }; + for entry in entries { + let Ok(entry) = entry else { + return false; + }; + if entry.file_name().to_string_lossy().parse::().is_err() { + continue; + } + let bytes = match std::fs::read_to_string(entry.path().join("stat")) { + Ok(v) => v, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue, + Err(_) => return false, + }; + let Some((_, rest)) = bytes.rsplit_once(") ") else { + return false; + }; + let fields: Vec<_> = rest.split_whitespace().take(3).collect(); + if fields.len() != 3 { + return false; + } + let Ok(group) = fields[2].parse::() else { + return false; + }; + if group == pgid && fields[0] != "Z" && fields[0] != "X" { + return false; + } + } + true } #[cfg(test)] mod tests { use super::*; + #[test] + fn cancellation_observation_terminates_only_owned_group_and_keeps_raw_bounded_output() { + let began = Instant::now(); + let output = run_controlled( + "/bin/sh", + &[ + "-c".into(), + "printf before; printf err >&2; sleep 10".into(), + ], + Duration::from_secs(5), + 64, + || { + ensure!( + began.elapsed() < Duration::from_millis(80), + "TEST_CANCELLED" + ); + Ok(()) + }, + ) + .unwrap(); + assert!(output.interrupted); + assert!(!output.timed_out); + assert_eq!(output.stdout, b"before"); + assert_eq!(output.stderr, b"err"); + assert_ne!(output.code, Some(0)); + assert!(began.elapsed() < Duration::from_secs(2)); + } #[test] fn exited_leader_does_not_leave_background_effects_or_open_pipes() { let directory = diff --git a/crates/agent-windows/Cargo.toml b/crates/agent-windows/Cargo.toml index f7e2edc..97f5e6c 100644 --- a/crates/agent-windows/Cargo.toml +++ b/crates/agent-windows/Cargo.toml @@ -39,4 +39,4 @@ rcgen = { version = "=0.14.10", default-features = false, features = ["crypto", cc = "=1.11.1" [target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_JobObjects", "Win32_System_Pipes", "Win32_System_SystemInformation", "Win32_System_Registry", "Win32_System_Memory", "Win32_System_Services", "Win32_System_Threading"] } +windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_ApplicationInstallationAndServicing", "Win32_System_EventLog", "Win32_System_Diagnostics_ToolHelp", "Win32_System_JobObjects", "Win32_System_Pipes", "Win32_System_SystemInformation", "Win32_System_Registry", "Win32_System_Memory", "Win32_System_Services", "Win32_System_Threading"] } diff --git a/crates/agent-windows/examples/clock_readiness.rs b/crates/agent-windows/examples/clock_readiness.rs index fde600e..317c501 100644 --- a/crates/agent-windows/examples/clock_readiness.rs +++ b/crates/agent-windows/examples/clock_readiness.rs @@ -157,6 +157,7 @@ mod service { let clock = super::clock::NativeClock::new(); STAGE.store(30, Ordering::SeqCst); let pipe = super::clock::named_pipe_diagnostic(); + let status_rpc = super::clock::status_rpc_diagnostic(); let first = clock.report(); std::thread::sleep(Duration::from_millis(750)); STAGE.store(31, Ordering::SeqCst); @@ -164,7 +165,7 @@ mod service { let identity_controls = super::clock::identity_controls(); STAGE.store(40, Ordering::SeqCst); let data = serde_json::to_vec_pretty( - &serde_json::json!({"schema":1,"case_id":case,"service_name":NAME,"token_sid":"S-1-5-19","elevated":token.elevation_flag!=0,"token":token,"endpoints":endpoints,"named_pipe_diagnostic":pipe,"identity_controls":identity_controls,"source":"MS-W32T:4.1:Opnum6:ncalrpc:SID19:W32Time:V5_descriptor:MUTUAL_AUTH:WINNT:PKT_PRIVACY","read_only":true,"samples":[first,second]}), + &serde_json::json!({"schema":1,"case_id":case,"service_name":NAME,"token_sid":"S-1-5-19","elevated":token.elevation_flag!=0,"token":token,"endpoints":endpoints,"named_pipe_diagnostic":pipe,"status_rpc_diagnostic":status_rpc,"identity_controls":identity_controls,"source":"MS-W32T:4.1:Opnum6:ncalrpc:SID19:W32Time:V5_descriptor:MUTUAL_AUTH:WINNT:PKT_PRIVACY","read_only":true,"samples":[first,second]}), )?; ensure!(data.len() < 16384, "READINESS_REPORT_SIZE"); STAGE.store(41, Ordering::SeqCst); @@ -259,6 +260,6 @@ fn main() -> anyhow::Result<()> { fn main() { println!( "{}", - serde_json::to_string(&serde_json::json!({"clock":clock::NativeClock::new().report(),"endpoints":clock::endpoint_report(),"named_pipe_diagnostic":clock::named_pipe_diagnostic(),"identity_controls":clock::identity_controls()})).unwrap() + serde_json::to_string(&serde_json::json!({"clock":clock::NativeClock::new().report(),"endpoints":clock::endpoint_report(),"named_pipe_diagnostic":clock::named_pipe_diagnostic(),"status_rpc_diagnostic":clock::status_rpc_diagnostic(),"identity_controls":clock::identity_controls()})).unwrap() ); } diff --git a/crates/agent-windows/native/clock_rpc.c b/crates/agent-windows/native/clock_rpc.c index dca1f1e..977b2a1 100644 --- a/crates/agent-windows/native/clock_rpc.c +++ b/crates/agent-windows/native/clock_rpc.c @@ -71,20 +71,47 @@ typedef struct { } RmmClockStatus; _Static_assert(sizeof(RmmClockStatus) == 64, "Rust numeric clock ABI"); _Static_assert(sizeof(RMM_W32TIME_STATUS) == 120, "MS-W32T x64 structure ABI"); -static uint32_t invoke_status(RPC_BINDING_HANDLE binding, RmmClockStatus *out) { +/* Diagnostic-only numeric ABI. No strings, names, pointers or remote arrays + * leave C. A zero native return is meaningful only when return_present is1. + * Stages:10 compose,20 binding,30 auth,40 call-timeout,50 arena, + * 60 invoking,61 returned,62 raw numeric status,70 local guard,80 mapped, + * 90 contained RPC/SEH exception. Never used by the clock trust evaluator. + */ +typedef struct { + uint32_t stage, native_rpc_return, native_rpc_return_present, rpc_exception; + uint32_t status_present, ul_size, c_entries, p_entries_present; +} RmmClockTrace; +_Static_assert(sizeof(RmmClockTrace) == 32, "Rust numeric diagnostic ABI"); +static uint32_t invoke_status(RPC_BINDING_HANDLE binding, RmmClockStatus *out, RmmClockTrace *trace) { RMM_W32TIME_STATUS *status = NULL; unsigned long result = 0; Arena arena = {0}; + if (trace) trace->stage = 50; if (!result && !InitOnceExecuteOnce(&arena_once, initialize_arena, NULL, NULL)) result = ERROR_NOT_ENOUGH_MEMORY; if (!result && TlsGetValue(arena_index)) result = ERROR_BUSY; if (!result && !TlsSetValue(arena_index, &arena)) result = GetLastError(); if (!result) { RpcTryExcept { + if (trace) trace->stage = 60; result = RmmW32TimeQueryStatus(binding, &status); + if (trace) { + trace->stage = 61; trace->native_rpc_return_present = 1; + trace->native_rpc_return = result; + } if (!result) { - if (!status || status->ulSize != sizeof(*status) || status->cEntries || status->pEntries) + if (trace) { + trace->stage = 62; trace->status_present = status != NULL; + if (status) { + trace->ul_size = status->ulSize; trace->c_entries = status->cEntries; + trace->p_entries_present = status->pEntries != NULL; + } + } + if (!status || status->ulSize != sizeof(*status) || status->cEntries || status->pEntries) { + if (trace) trace->stage = 70; result = RPC_X_BAD_STUB_DATA; + } else { + if (trace) trace->stage = 80; out->leap = status->eLeapIndicator; out->stratum = status->nStratum; out->state = status->ulLcState; out->flags = status->ulTSFlags; out->last_result = status->eLastSyncResult; out->size = status->ulSize; @@ -95,21 +122,24 @@ static uint32_t invoke_status(RPC_BINDING_HANDLE binding, RmmClockStatus *out) { } } RpcExcept(RpcExceptionFilter(RpcExceptionCode())) { result = RpcExceptionCode(); + if (trace) {trace->stage = 90; trace->rpc_exception = result;} } RpcEndExcept release_allocations(&arena); TlsSetValue(arena_index, NULL); } return result; } -uint32_t rmm_clock_query(RmmClockStatus *out) { +static uint32_t pipe_query(RmmClockStatus *out, RmmClockTrace *trace) { RPC_WSTR text = NULL; RPC_BINDING_HANDLE binding = NULL; RPC_SECURITY_QOS qos = {0}; unsigned long result; memset(out, 0, sizeof(*out)); + if (trace) trace->stage = 10; result = RpcStringBindingComposeW(NULL, (RPC_WSTR)L"ncacn_np", NULL, (RPC_WSTR)L"\\pipe\\W32TIME_ALT", NULL, &text); if (result) return result; + if (trace) trace->stage = 20; result = RpcBindingFromStringBindingW(text, &binding); RpcStringFreeW(&text); if (result) return result; @@ -117,13 +147,23 @@ uint32_t rmm_clock_query(RmmClockStatus *out) { qos.Capabilities = RPC_C_QOS_CAPABILITIES_DEFAULT; qos.IdentityTracking = RPC_C_QOS_IDENTITY_STATIC; qos.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY; + if (trace) trace->stage = 30; result = RpcBindingSetAuthInfoExW(binding, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN_WINNT, NULL, RPC_C_AUTHZ_NONE, &qos); - if (!result) result = RpcBindingSetOption(binding, RPC_C_OPT_CALL_TIMEOUT, 1500); - if (!result) result = invoke_status(binding, out); + if (!result) { + if (trace) trace->stage = 40; + result = RpcBindingSetOption(binding, RPC_C_OPT_CALL_TIMEOUT, 1500); + } + if (!result) result = invoke_status(binding, out, trace); RpcBindingFree(&binding); return result; } +uint32_t rmm_clock_query(RmmClockStatus *out) { return pipe_query(out, NULL); } +uint32_t rmm_clock_query_trace(RmmClockTrace *trace) { + RmmClockStatus ignored = {0}; + memset(trace, 0, sizeof(*trace)); + return pipe_query(&ignored, trace); +} /* Kernel-authenticated ALPC only. No automatic fallback to named pipes. * The expected SID is fixed locally, never adopted from endpoint metadata. */ @@ -188,7 +228,7 @@ static uint32_t alpc_query(RmmClockStatus *out, unsigned control) { qos.ServerSecurityDescriptor = &descriptor; result = RpcBindingSetAuthInfoExW(binding, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN_WINNT, NULL, RPC_C_AUTHZ_NONE, (RPC_SECURITY_QOS *)&qos); - if (!result) result = invoke_status(binding, out); + if (!result) result = invoke_status(binding, out, NULL); } RpcBindingFree(&binding); break; /* no retry, alternate identity, endpoint or weaker transport */ diff --git a/crates/agent-windows/native/w32time.idl b/crates/agent-windows/native/w32time.idl index cfb4eac..665fbe2 100644 --- a/crates/agent-windows/native/w32time.idl +++ b/crates/agent-windows/native/w32time.idl @@ -31,6 +31,11 @@ typedef struct { unsigned long cEntries; [size_is(cEntries)] RMM_W32TIME_ENTRY *pEntries; } RMM_W32TIME_STATUS; +/* Microsoft declares PW32TIME_STATUS_INFO separately. Keep the inner pointer + * explicitly unique; the out parameter's outer pointer defaults to ref. + * WIDL 7 propagates a parameter-level [ref] through a direct ** declaration. + */ +typedef [unique] RMM_W32TIME_STATUS *RMM_PW32TIME_STATUS; [uuid(8fb6d884-2388-11d0-8c35-00c04fda2795), version(4.1), pointer_default(unique)] interface RmmW32Time { unsigned long RmmUnusedSlot0([in] handle_t binding); @@ -39,5 +44,5 @@ interface RmmW32Time { unsigned long RmmUnusedSlot3([in] handle_t binding); unsigned long RmmUnusedSlot4([in] handle_t binding); unsigned long RmmUnusedSlot5([in] handle_t binding); - unsigned long RmmW32TimeQueryStatus([in] handle_t binding, [out, ref] RMM_W32TIME_STATUS **status); + unsigned long RmmW32TimeQueryStatus([in] handle_t binding, [out] RMM_PW32TIME_STATUS *status); } diff --git a/crates/agent-windows/src/actions.rs b/crates/agent-windows/src/actions.rs index 5734572..2b82d67 100644 --- a/crates/agent-windows/src/actions.rs +++ b/crates/agent-windows/src/actions.rs @@ -2,7 +2,7 @@ //! consumption are supplied by the shared core and the runtime, never bypassed. use anyhow::{Result, ensure}; use rmm_agent_core::auth::Outcome; -use rmm_domain::jobs::{Action, JobPayload, hash}; +use rmm_domain::jobs::{Action, JobPayload, WindowsExecutionProfile, hash}; use std::{ ffi::OsString, fs::{File, OpenOptions}, @@ -11,6 +11,43 @@ use std::{ path::{Path, PathBuf}, sync::atomic::AtomicBool, }; +/// Implemented contract name only. Runtime publication still requires its own +/// native clock/boot/job qualification; this constant grants no capability. +pub const REMOTE_SIGNED_PROFILE_CAPABILITY: &str = "script.powershell.process_remote_signed/1"; + +pub(crate) fn script_arguments(job: &JobPayload, path: &Path) -> Result> { + let Action::RunScript { + interpreter, + arguments, + windows_execution_profile, + .. + } = &job.action + else { + anyhow::bail!("SCRIPT_REQUIRED") + }; + ensure!( + (job.execution_identity == "agent_service" + || (job.execution_identity == "system" && job.action.full_maintenance())) + && job.action.valid_execution_identity(&job.execution_identity) + && interpreter == "powershell" + && job.action.validate(), + "WINDOWS_SCRIPT_PROFILE_BINDING" + ); + let mut args: Vec = ["-NoLogo", "-NoProfile", "-NonInteractive"] + .into_iter() + .map(OsString::from) + .collect(); + if let Some(WindowsExecutionProfile::PowerShell51RemoteSigned) = windows_execution_profile { + args.extend([ + OsString::from("-ExecutionPolicy"), + OsString::from("RemoteSigned"), + ]); + } + args.push(OsString::from("-File")); + args.push(path.as_os_str().into()); + args.extend(arguments.iter().map(OsString::from)); + Ok(args) +} use windows_sys::Win32::{ Storage::FileSystem::{ FILE_ATTRIBUTE_REPARSE_POINT, FILE_FLAG_OPEN_REPARSE_POINT, FILE_SHARE_READ, @@ -36,10 +73,45 @@ pub fn supported(job: &JobPayload) -> bool { interpreter == "powershell" && powershell().is_ok() } Action::RefreshInventory { scope } => scope == "windows.base", + Action::QueryLogs { .. } => crate::event_log::for_job(job).is_ok(), _ => false, } } +/// Each prepared operation retains its own typed binding; no non-script action +/// can fall through to inventory or borrow the PowerShell execution profile. +pub enum PreparedAction { + Script(Script), + Inventory, + EventLog(crate::event_log_job::Prepared), +} +impl PreparedAction { + pub fn prepare( + job: &JobPayload, + directory: &Path, + grant: uuid::Uuid, + request: &rmm_agent_core::auth::Request, + boot: uuid::Uuid, + ) -> Result { + ensure!(supported(job), "UNSUPPORTED_CAPABILITY"); + match &job.action { + Action::RunScript { .. } => Ok(Self::Script(Script::prepare(job, directory, grant)?)), + Action::RefreshInventory { scope } if scope == "windows.base" => Ok(Self::Inventory), + Action::QueryLogs { .. } => Ok(Self::EventLog( + crate::event_log_job::Prepared::prepare(job, directory, request, grant, boot)?, + )), + _ => anyhow::bail!("UNSUPPORTED_CAPABILITY"), + } + } + pub fn run(self, job: &JobPayload, cancel: &AtomicBool) -> Result { + match self { + Self::Script(script) => script.run(job, cancel), + Self::Inventory => inventory(job), + Self::EventLog(log) => log.run(job, cancel), + } + } +} + pub struct Script { executable: PathBuf, path: PathBuf, @@ -47,6 +119,7 @@ pub struct Script { _file: File, cwd: PathBuf, arguments: Vec, + profile: Option, } impl Script { /// Preparation creates only a protected copy of approved bytes, no child. @@ -56,7 +129,7 @@ impl Script { let Action::RunScript { interpreter, script_utf8, - arguments, + windows_execution_profile, .. } = &job.action else { @@ -95,18 +168,14 @@ impl Script { hash(&std::fs::read(&path)?) == hash(script_utf8.as_bytes()), "SCRIPT_BYTES_CHANGED" ); - let mut args: Vec = ["-NoLogo", "-NoProfile", "-NonInteractive", "-File"] - .into_iter() - .map(OsString::from) - .collect(); - args.push(path.clone().into_os_string()); - args.extend(arguments.iter().map(OsString::from)); + let args = script_arguments(job, &path)?; Ok(Self { executable: powershell()?, path, _file: file, cwd: directory.to_path_buf(), arguments: args, + profile: *windows_execution_profile, }) } pub fn run(self, job: &JobPayload, cancel: &AtomicBool) -> Result { @@ -115,6 +184,7 @@ impl Script { let Action::RunScript { script_utf8, expected_stdout, + windows_execution_profile, .. } = &job.action else { @@ -124,6 +194,11 @@ impl Script { std::fs::read(&self.path)? == script_utf8.as_bytes(), "SCRIPT_BYTES_CHANGED" ); + ensure!( + self.profile == *windows_execution_profile + && self.arguments == script_arguments(job, &self.path)?, + "WINDOWS_SCRIPT_PROFILE_CHANGED" + ); let output = crate::process::run( &self.executable, &self.arguments, @@ -159,6 +234,17 @@ impl Script { } else { "" }; + let mut evidence = serde_json::json!({"postconditions_passed":success,"interpreter":"powershell", + "script_sha256":hash(script_utf8.as_bytes()),"execution_sid":"S-1-5-19", + "output_encoding":"raw_native_bytes","comparison":"exact_utf8_bytes", + "execution_policy":"inherited","process_tree_bounded":true, + "cancel_requested":output.cancelled,"termination_does_not_prove_absence_of_effects":output.interrupted}); + if self.profile.is_some() { + evidence["windows_execution_profile"] = serde_json::to_value(self.profile)?; + evidence["execution_policy"] = serde_json::json!("process_remote_signed_requested"); + evidence["execution_policy_requested"] = serde_json::json!("RemoteSigned"); + evidence["execution_policy_scope"] = serde_json::json!("process"); + } Ok(Outcome { state: state.into(), stdout: output.stdout, @@ -167,11 +253,7 @@ impl Script { output_truncated: output.truncated, reboot_required: false, error_code: code.into(), - evidence: serde_json::json!({"postconditions_passed":success,"interpreter":"powershell", - "script_sha256":hash(script_utf8.as_bytes()),"execution_sid":"S-1-5-19", - "output_encoding":"raw_native_bytes","comparison":"exact_utf8_bytes", - "execution_policy":"inherited","process_tree_bounded":true, - "cancel_requested":output.cancelled,"termination_does_not_prove_absence_of_effects":output.interrupted}), + evidence, }) } } @@ -200,3 +282,89 @@ pub fn inventory(job: &JobPayload) -> Result { "observed_at":chrono::Utc::now(),"completeness":"partial","execution_sid":"S-1-5-19"}), }) } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn powershell_policy_is_an_exact_job_profile_before_file_not_a_mutable_default() { + let id = uuid::Uuid::new_v4(); + let now = chrono::Utc::now(); + let mut job = JobPayload { + schema_version: 1, + tenant_id: id, + asset_id: id, + agent_id: id, + enrollment_generation: 1, + assignment_version: 1, + job_id: id, + attempt_id: id, + action: Action::RunScript { + interpreter: "powershell".into(), + script_utf8: "Write-Output 'fixed'\r\n".into(), + arguments: vec!["-ExecutionPolicy".into(), "Bypass".into()], + expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, + }, + execution_identity: "agent_service".into(), + not_before: now, + start_before: now + chrono::Duration::seconds(60), + max_runtime_seconds: 30, + max_output_bytes: 4096, + }; + let path = Path::new(r"C:\fixed state\approved.ps1"); + let inherited = script_arguments(&job, path).unwrap(); + assert_eq!( + inherited, + vec![ + "-NoLogo", + "-NoProfile", + "-NonInteractive", + "-File", + r"C:\fixed state\approved.ps1", + "-ExecutionPolicy", + "Bypass" + ] + .into_iter() + .map(OsString::from) + .collect::>() + ); + if let Action::RunScript { + windows_execution_profile, + .. + } = &mut job.action + { + *windows_execution_profile = Some(WindowsExecutionProfile::PowerShell51RemoteSigned); + } + let args = script_arguments(&job, path).unwrap(); + assert_eq!( + args[..6], + [ + "-NoLogo", + "-NoProfile", + "-NonInteractive", + "-ExecutionPolicy", + "RemoteSigned", + "-File" + ] + .map(OsString::from) + ); + assert_eq!(args[6], path.as_os_str()); // approved arguments remain AFTER -File + assert_ne!(inherited, args); + let mut unknown = serde_json::to_value(&job.action).unwrap(); + unknown["windows_execution_profile"] = serde_json::json!("Bypass"); + assert!(serde_json::from_value::(unknown).is_err()); + for identity in ["root", "system"] { + job.execution_identity = identity.into(); + assert!(script_arguments(&job, path).is_err()); + } + job.execution_identity = "agent_service".into(); + for other in ["posix_sh", "bash", "pwsh"] { + if let Action::RunScript { interpreter, .. } = &mut job.action { + *interpreter = other.into(); + } + assert!(script_arguments(&job, path).is_err()); + } + } +} diff --git a/crates/agent-windows/src/clock.rs b/crates/agent-windows/src/clock.rs index 4db1572..e649e1c 100644 --- a/crates/agent-windows/src/clock.rs +++ b/crates/agent-windows/src/clock.rs @@ -259,6 +259,87 @@ pub fn named_pipe_diagnostic() -> ClockReport { } .report() } +/// Diagnostic-only ABI captured before the existing C layout guard. Presence +/// flags are numeric0/1; no remote pointers, names or returned entries escape. +#[derive(Clone, Copy, Debug, Default, Serialize)] +#[repr(C)] +pub struct StatusRpcTrace { + pub stage: u32, + pub native_rpc_return: u32, + pub native_rpc_return_present: u32, + pub rpc_exception: u32, + pub status_present: u32, + pub ul_size: u32, + pub c_entries: u32, + pub p_entries_present: u32, +} +#[derive(Debug, Serialize)] +pub struct StatusRpcDiagnostic { + pub rpc_code: u32, + pub independent_call: bool, + pub trace: Option, + pub diagnostic_only: bool, + pub adapter_qualified: bool, + pub trusted: bool, +} +/// The fixed authenticated named-pipe call is observed separately. It never +/// selects a production transport or supplies a trusted timestamp. One worker, +/// one outstanding call and no replacement/retry after a local timeout. +pub fn status_rpc_diagnostic() -> StatusRpcDiagnostic { + #[cfg(windows)] + let (rpc_code, trace) = { + type Reply = std::sync::mpsc::SyncSender<(u32, StatusRpcTrace)>; + type Requests = std::sync::mpsc::SyncSender; + static WORKER: std::sync::OnceLock>> = std::sync::OnceLock::new(); + let worker = WORKER.get_or_init(|| { + let (sender, receiver) = std::sync::mpsc::sync_channel::(1); + let created = std::thread::Builder::new() + .name("rmm-clock-status-diag".into()) + .spawn(move || { + #[link(name = "rmm_clock_rpc", kind = "static")] + #[link(name = "rpcrt4")] + #[link(name = "advapi32")] + unsafe extern "C" { + fn rmm_clock_query_trace(trace: *mut StatusRpcTrace) -> u32; + } + while let Ok(reply) = receiver.recv() { + let mut trace = StatusRpcTrace::default(); + // SAFETY: fixed8*u32 C ABI; SEH stays contained inside C. + let code = unsafe { rmm_clock_query_trace(&mut trace) }; + let _ = reply.send((code, trace)); + } + }) + .is_ok(); + Mutex::new(created.then_some(sender)) + }); + if let Ok(mut worker) = worker.try_lock() { + let (reply, receiver) = std::sync::mpsc::sync_channel(1); + let sent = worker + .as_ref() + .is_some_and(|sender| sender.try_send(reply).is_ok()); + if sent + && let Ok((code, trace)) = receiver.recv_timeout(std::time::Duration::from_secs(2)) + { + (code, Some(trace)) + } else { + *worker = None; + (1818, None) + } + } else { + (170, None) + } + }; + #[cfg(not(windows))] + let (rpc_code, trace) = (50, None); + StatusRpcDiagnostic { + rpc_code, + independent_call: true, + trace, + diagnostic_only: true, + adapter_qualified: false, + trusted: false, + } +} /// Read-only negative controls for the separate native readiness program. /// Neither control can produce clock authority, even if the OS accepts it. /// Rejection without a successful genuine status query is not identity proof. @@ -443,6 +524,7 @@ mod tests { } #[test] fn production_clock_stays_closed_until_native_adapter_qualification() { + assert_eq!(std::mem::size_of::(), 32); assert_eq!( NativeClock::new().now().unwrap_err().to_string(), "CLOCK_ADAPTER_UNQUALIFIED" diff --git a/crates/agent-windows/src/event_log.rs b/crates/agent-windows/src/event_log.rs index 9843093..d145053 100644 --- a/crates/agent-windows/src/event_log.rs +++ b/crates/agent-windows/src/event_log.rs @@ -1,4 +1,5 @@ -//! Bounded, local, fixture-only live Event Log query; not wired into any job. +//! Bounded, local, fixture-only Event Log provider. Only the authorized native +//! job worker may call the Windows provider; this module confers no authority. //! //! EvtQuery/EvtRender have no per-call timeout. The monotonic deadline below is //! cooperative between Win32 calls; the eventual executor must also supervise @@ -12,8 +13,9 @@ use chrono::{DateTime, SecondsFormat, Utc}; use serde::{Deserialize, Serialize}; use std::sync::atomic::AtomicBool; -pub const CHANNEL: &str = "Application"; -pub const PROVIDER: &str = "RustDiscovery-MVP-Test"; +pub use rmm_domain::jobs::{ + WINDOWS_EVENT_LOG_FIXTURE_CHANNEL as CHANNEL, WINDOWS_EVENT_LOG_FIXTURE_PROVIDER as PROVIDER, +}; const MAX_BYTES: usize = 256 * 1024; #[cfg(any(windows, test))] const REPORT_RESERVE: usize = 256; @@ -57,7 +59,42 @@ impl Query { } } -#[derive(Debug, Serialize)] +/// Pure job-to-provider binding. Never opens a channel or touches a bookmark. +pub fn for_job(job: &rmm_domain::jobs::JobPayload) -> Result { + use rmm_domain::jobs::{Action, WINDOWS_EVENT_LOG_FIXTURE_SOURCE}; + let Action::QueryLogs { + unit, + since, + until, + max_lines, + } = &job.action + else { + anyhow::bail!("EVENT_LOG_ACTION_REQUIRED") + }; + ensure!( + job.execution_identity == "agent_service" + && unit == WINDOWS_EVENT_LOG_FIXTURE_SOURCE + && job.action.validate() + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes), + "EVENT_LOG_JOB_BINDING" + ); + let q = Query { + channel: CHANNEL.into(), + provider: PROVIDER.into(), + start_utc: *since, + end_utc: *until, + max_events: *max_lines, + max_output_bytes: job.max_output_bytes, + timeout_ms: 5000, + }; + q.xpath()?; + Ok(q) +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] pub struct Event { pub channel: String, pub provider: String, @@ -501,6 +538,53 @@ mod tests { } } #[test] + fn job_mapping_is_only_fixed_unprivileged_live_logs_with_exact_limits() { + use rmm_domain::jobs::{Action, JobPayload, WINDOWS_EVENT_LOG_FIXTURE_SOURCE}; + let id = uuid::Uuid::new_v4(); + let now = DateTime::from_timestamp(1_700_000_000, 0).unwrap(); + let mut job = JobPayload { + schema_version: 1, + tenant_id: id, + asset_id: id, + agent_id: id, + enrollment_generation: 1, + assignment_version: 1, + job_id: id, + attempt_id: id, + action: Action::QueryLogs { + unit: WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(), + since: now, + until: now + chrono::Duration::hours(24), + max_lines: 1000, + }, + execution_identity: "agent_service".into(), + not_before: now, + start_before: now + chrono::Duration::seconds(60), + max_runtime_seconds: 10, + max_output_bytes: 65536, + }; + let q = for_job(&job).unwrap(); + assert_eq!( + (q.channel.as_str(), q.provider.as_str()), + (CHANNEL, PROVIDER) + ); + assert_eq!(q.max_output_bytes, 65536); + for identity in ["root", "system"] { + job.execution_identity = identity.into(); + assert!(for_job(&job).is_err()); + } + job.execution_identity = "agent_service".into(); + for bytes in [1023, 65537] { + job.max_output_bytes = bytes; + assert!(for_job(&job).is_err()); + } + job.max_output_bytes = 65536; + if let Action::QueryLogs { until, .. } = &mut job.action { + *until += chrono::Duration::nanoseconds(1); + } + assert!(for_job(&job).is_err()); + } + #[test] fn serialized_byte_budget_includes_xml_escaping_and_metadata() { let q = request(); let mut report = Report::new(&q); diff --git a/crates/agent-windows/src/event_log_job.rs b/crates/agent-windows/src/event_log_job.rs new file mode 100644 index 0000000..aeeacb0 --- /dev/null +++ b/crates/agent-windows/src/event_log_job.rs @@ -0,0 +1,778 @@ +//! Fixed, authorized Event Log subprocess. A command-line mode is not a live +//! query API: the child authenticates its real SCM parent, original signed +//! request, protected public trust, and an already committed consumption/start. +//! Request/grant bytes remain in a private, create-once job file, never argv. +use crate::{event_log, native}; +use anyhow::{Context, Result, ensure}; +use chrono::{DateTime, Utc}; +use rmm_agent_core::auth::{Binding, Outcome, Request}; +use rmm_domain::jobs::{ExecutionPermitPayload, JobPayload, hash}; +use serde::{Deserialize, Serialize}; +use std::{ + fs::{File, OpenOptions}, + io::{Read, Write}, + mem::{size_of, size_of_val, zeroed}, + os::windows::{ + fs::OpenOptionsExt, + io::{AsRawHandle, FromRawHandle, OwnedHandle}, + }, + path::{Path, PathBuf}, + ptr, + sync::atomic::AtomicBool, + time::Duration, +}; +use uuid::Uuid; +use windows_sys::Win32::{ + Foundation::*, + Security::{self, *}, + Storage::FileSystem::*, + System::{Diagnostics::ToolHelp::*, Services::*, Threading::*}, +}; + +pub const MODE: &str = "--eventlog-worker"; +const CONFIG: &str = r"C:\ProgramData\RustDiscovery\config.json"; +const TICKET_LIMIT: u64 = 128 * 1024; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct FileId { + volume: u64, + id: [u8; 16], +} +fn file_id(handle: HANDLE) -> Result { + let mut info: FILE_ID_INFO = unsafe { zeroed() }; + ensure!( + unsafe { + GetFileInformationByHandleEx( + handle, + FileIdInfo, + (&mut info as *mut FILE_ID_INFO).cast(), + size_of::() as u32, + ) + } != 0, + "EVENT_LOG_FILE_ID" + ); + Ok(FileId { + volume: info.VolumeSerialNumber, + id: info.FileId.Identifier, + }) +} +fn owned(handle: HANDLE) -> Result { + ensure!( + !handle.is_null() && handle != INVALID_HANDLE_VALUE, + "EVENT_LOG_HANDLE" + ); + Ok(unsafe { OwnedHandle::from_raw_handle(handle) }) +} +fn birth(handle: HANDLE) -> Result { + let (mut c, mut e, mut k, mut u) = unsafe { (zeroed(), zeroed(), zeroed(), zeroed()) }; + ensure!( + unsafe { GetProcessTimes(handle, &mut c, &mut e, &mut k, &mut u) } != 0, + "EVENT_LOG_PROCESS_TIME" + ); + Ok((u64::from(c.dwHighDateTime) << 32) | u64::from(c.dwLowDateTime)) +} +fn text(value: *const u16) -> Result { + ensure!(!value.is_null(), "EVENT_LOG_NATIVE_STRING"); + let mut length = 0; + while length < 32768 && unsafe { *value.add(length) } != 0 { + length += 1; + } + ensure!(length < 32768, "EVENT_LOG_NATIVE_STRING_SIZE"); + Ok(String::from_utf16(unsafe { + std::slice::from_raw_parts(value, length) + })?) +} +fn process_image(handle: HANDLE) -> Result { + let mut buffer = vec![0u16; 32768]; + let mut len = buffer.len() as u32; + ensure!( + unsafe { QueryFullProcessImageNameW(handle, 0, buffer.as_mut_ptr(), &mut len) } != 0, + "EVENT_LOG_PARENT_IMAGE" + ); + Ok(String::from_utf16(&buffer[..len as usize])?.into()) +} +struct Service(SC_HANDLE); +impl Drop for Service { + fn drop(&mut self) { + unsafe { + CloseServiceHandle(self.0); + } + } +} +fn agent_pid(image: &Path) -> Result { + let manager = Service(unsafe { OpenSCManagerW(ptr::null(), ptr::null(), SC_MANAGER_CONNECT) }); + ensure!(!manager.0.is_null(), "EVENT_LOG_SCM"); + let service = Service(unsafe { + OpenServiceW( + manager.0, + native::wide(crate::SERVICE_NAME).as_ptr(), + SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS, + ) + }); + ensure!(!service.0.is_null(), "EVENT_LOG_AGENT_SERVICE"); + let mut bytes = 0; + unsafe { + QueryServiceConfigW(service.0, ptr::null_mut(), 0, &mut bytes); + } + ensure!((1..=32768).contains(&bytes), "EVENT_LOG_AGENT_CONFIG_SIZE"); + let mut storage = vec![0usize; (bytes as usize).div_ceil(size_of::())]; + ensure!( + unsafe { QueryServiceConfigW(service.0, storage.as_mut_ptr().cast(), bytes, &mut bytes) } + != 0, + "EVENT_LOG_AGENT_CONFIG" + ); + let config = unsafe { &*storage.as_ptr().cast::() }; + ensure!( + config.dwServiceType == SERVICE_WIN32_OWN_PROCESS + && text(config.lpBinaryPathName)? + == format!("\"{}\" --service --config \"{CONFIG}\"", image.display()) + && text(config.lpServiceStartName)?.eq_ignore_ascii_case(r"NT AUTHORITY\LocalService"), + "EVENT_LOG_AGENT_CONFIG_BINDING" + ); + let mut status: SERVICE_STATUS_PROCESS = unsafe { zeroed() }; + ensure!( + unsafe { + QueryServiceStatusEx( + service.0, + SC_STATUS_PROCESS_INFO, + (&mut status as *mut SERVICE_STATUS_PROCESS).cast(), + size_of::() as u32, + &mut bytes, + ) + } != 0 + && status.dwCurrentState == SERVICE_RUNNING + && status.dwProcessId > 0, + "EVENT_LOG_AGENT_NOT_RUNNING" + ); + Ok(status.dwProcessId) +} +fn token_info(token: HANDLE, class: TOKEN_INFORMATION_CLASS) -> Result> { + let mut bytes = 0; + unsafe { + GetTokenInformation(token, class, ptr::null_mut(), 0, &mut bytes); + } + ensure!((1..=65536).contains(&bytes), "EVENT_LOG_TOKEN_SIZE"); + let mut data = vec![0usize; (bytes as usize).div_ceil(size_of::())]; + ensure!( + unsafe { GetTokenInformation(token, class, data.as_mut_ptr().cast(), bytes, &mut bytes) } + != 0, + "EVENT_LOG_TOKEN" + ); + Ok(data) +} +fn service_token(process: HANDLE) -> Result<()> { + let mut token = ptr::null_mut(); + ensure!( + unsafe { OpenProcessToken(process, TOKEN_QUERY, &mut token) } != 0, + "EVENT_LOG_TOKEN_OPEN" + ); + let token = owned(token)?; + let user = token_info(token.as_raw_handle(), TokenUser)?; + let mut service_sid = ptr::null_mut(); + ensure!( + unsafe { + Security::Authorization::ConvertStringSidToSidW( + native::wide(native::service_sid()?).as_ptr(), + &mut service_sid, + ) + } != 0, + "EVENT_LOG_SERVICE_SID" + ); + struct Sid(PSID); + impl Drop for Sid { + fn drop(&mut self) { + unsafe { + LocalFree(self.0); + } + } + } + let service_sid = Sid(service_sid); + let mut ls = [0usize; 16]; + let mut size = size_of_val(&ls) as u32; + ensure!( + unsafe { + CreateWellKnownSid( + WinLocalServiceSid, + ptr::null_mut(), + ls.as_mut_ptr().cast(), + &mut size, + ) + } != 0 + && unsafe { + EqualSid( + (*user.as_ptr().cast::()).User.Sid, + ls.as_ptr().cast_mut().cast(), + ) + } != 0, + "EVENT_LOG_LOCAL_SERVICE_REQUIRED" + ); + let groups = token_info(token.as_raw_handle(), TokenGroups)?; + let g = unsafe { &*groups.as_ptr().cast::() }; + let offset = std::mem::offset_of!(TOKEN_GROUPS, Groups); + ensure!( + g.GroupCount <= 1024 + && offset + g.GroupCount as usize * size_of::() + <= groups.len() * size_of::(), + "EVENT_LOG_TOKEN_GROUPS_SIZE" + ); + let entries = unsafe { std::slice::from_raw_parts(g.Groups.as_ptr(), g.GroupCount as usize) }; + let mut admins = [0usize; 16]; + let mut admin_bytes = size_of_val(&admins) as u32; + ensure!( + unsafe { + CreateWellKnownSid( + WinBuiltinAdministratorsSid, + ptr::null_mut(), + admins.as_mut_ptr().cast(), + &mut admin_bytes, + ) + } != 0, + "EVENT_LOG_ADMIN_SID" + ); + ensure!( + !entries + .iter() + .any(|entry| entry.Attributes & 0x0000_0004 != 0 + && unsafe { EqualSid(entry.Sid, admins.as_ptr().cast_mut().cast()) } != 0), + "EVENT_LOG_ADMINISTRATOR_FORBIDDEN" + ); + // TOKEN_GROUPS enabled/deny-only bits from winnt.h, as in the pipe peer proof. + ensure!( + entries + .iter() + .any(|entry| entry.Attributes & 0x0000_0004 != 0 + && entry.Attributes & 0x0000_0010 == 0 + && unsafe { EqualSid(entry.Sid, service_sid.0) } != 0), + "EVENT_LOG_AGENT_SID_REQUIRED" + ); + Ok(()) +} +fn parent_pid() -> Result { + let snapshot = owned(unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) })?; + let mut entry: PROCESSENTRY32W = unsafe { zeroed() }; + entry.dwSize = size_of::() as u32; + let mut ok = unsafe { Process32FirstW(snapshot.as_raw_handle(), &mut entry) }; + while ok != 0 { + if entry.th32ProcessID == std::process::id() { + return Ok(entry.th32ParentProcessID); + } + ok = unsafe { Process32NextW(snapshot.as_raw_handle(), &mut entry) }; + } + anyhow::bail!("EVENT_LOG_PARENT_UNVERIFIED") +} +fn open_private(path: &Path, share_write: bool) -> Result { + ensure!( + path.is_absolute() && !path.to_string_lossy().starts_with(r"\\"), + "EVENT_LOG_LOCAL_PATH" + ); + for parent in path.ancestors().skip(1) { + use std::os::windows::fs::MetadataExt; + ensure!( + std::fs::symlink_metadata(parent)?.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT + == 0, + "EVENT_LOG_REPARSE_ANCESTOR" + ); + } + native::private_path(path, false)?; + let file = OpenOptions::new() + .read(true) + .share_mode(FILE_SHARE_READ | if share_write { FILE_SHARE_WRITE } else { 0 }) + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) + .open(path)?; + use std::os::windows::fs::MetadataExt; + ensure!( + file.metadata()?.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT == 0, + "EVENT_LOG_REPARSE_FILE" + ); + Ok(file) +} +fn read(file: &mut File, limit: u64) -> Result> { + ensure!( + file.metadata()?.len() <= limit, + "EVENT_LOG_PRIVATE_FILE_LIMIT" + ); + let mut value = Vec::new(); + file.take(limit + 1).read_to_end(&mut value)?; + ensure!(value.len() as u64 <= limit, "EVENT_LOG_PRIVATE_FILE_LIMIT"); + Ok(value) +} +#[derive(Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct Ticket { + request: Request, + parent_pid: u32, + parent_birth: u64, + boot: Uuid, + ledger: FileId, +} +#[derive(Deserialize)] +struct WorkerConfig { + state_directory: PathBuf, + job_binding_file: PathBuf, + enable_jobs: bool, +} +fn ticket_name(permit: &ExecutionPermitPayload) -> String { + format!("eventlog-{}.json", hash(permit.grant_id.as_bytes())) +} +/// Prepared data only. No query, worker or Evt handle exists before run. +pub struct Prepared { + path: PathBuf, + _file: File, + ticket_hash: String, + job_bytes: Vec, +} +impl Prepared { + pub fn prepare( + job: &JobPayload, + directory: &Path, + request: &Request, + grant: Uuid, + boot: Uuid, + ) -> Result { + event_log::for_job(job)?; + native::require_local_service()?; + service_token(unsafe { GetCurrentProcess() })?; + let image = std::env::current_exe()?; + ensure!( + agent_pid(&image)? == std::process::id(), + "EVENT_LOG_REAL_AGENT_REQUIRED" + ); + let permit: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes)?; + ensure!( + permit.grant_id == grant && !boot.is_nil(), + "EVENT_LOG_GRANT_BINDING" + ); + native::private_path(directory, true)?; + let ledger = open_private( + &directory + .parent() + .context("EVENT_LOG_STATE_DIRECTORY")? + .join("consumption.sqlite"), + true, + )?; + let ticket = Ticket { + request: request.clone(), + parent_pid: std::process::id(), + parent_birth: birth(unsafe { GetCurrentProcess() })?, + boot, + ledger: file_id(ledger.as_raw_handle())?, + }; + let bytes = serde_json::to_vec(&ticket)?; + ensure!(bytes.len() as u64 <= TICKET_LIMIT, "EVENT_LOG_TICKET_LIMIT"); + let path = directory.join(ticket_name(&permit)); + let mut writer = OpenOptions::new() + .create_new(true) + .write(true) + .share_mode(0) + .open(&path)?; + writer.write_all(&bytes)?; + writer.sync_all()?; + drop(writer); + let mut file = open_private(&path, false)?; + ensure!( + read(&mut file, TICKET_LIMIT)? == bytes, + "EVENT_LOG_TICKET_CHANGED" + ); + Ok(Self { + path, + _file: file, + ticket_hash: hash(&bytes), + job_bytes: request.job_bytes.clone(), + }) + } + pub fn run(self, job: &JobPayload, cancel: &AtomicBool) -> Result { + event_log::for_job(job)?; + ensure!( + serde_json::from_slice::(&self.job_bytes)? + == serde_json::to_value(job)?, + "EVENT_LOG_JOB_CHANGED" + ); + let mut file = open_private(&self.path, false)?; + ensure!( + hash(&read(&mut file, TICKET_LIMIT)?) == self.ticket_hash, + "EVENT_LOG_TICKET_CHANGED" + ); + let output = crate::process::run( + &std::env::current_exe()?, + &[MODE.into(), self.path.as_os_str().into()], + self.path.parent().context("EVENT_LOG_JOB_DIRECTORY")?, + job.max_runtime_seconds, + job.max_output_bytes as usize, + cancel, + )?; + if output.interrupted { + return Ok(Outcome::unknown(if output.cancelled { + "EVENT_LOG_CANCELLED_RESULT_UNKNOWN" + } else { + "EVENT_LOG_TIMEOUT_RESULT_UNKNOWN" + })); + } + ensure!( + output.exit_code == Some(0) && !output.truncated && output.stderr.is_empty(), + "EVENT_LOG_WORKER_RESULT_UNVERIFIED" + ); + outcome(job, output.stdout) + } +} + +fn consumed( + db: &rusqlite::Connection, + binding: &Binding, + ticket: &Ticket, + permit: &ExecutionPermitPayload, +) -> Result<()> { + let length: u64 = db.query_row("SELECT length(e.start) FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?1", [permit.grant_id.to_string()], |r| r.get(0))?; + ensure!(length <= TICKET_LIMIT, "EVENT_LOG_START_SIZE"); + type Row = (String, u64, String, String, bool, Vec, bool); + let row: Row = db.query_row("SELECT c.agent,c.generation,c.job,c.attempt,c.result IS NULL,e.start,e.outcome IS NULL FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?1", + [permit.grant_id.to_string()], |r| Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?,r.get(5)?,r.get(6)?)))?; + ensure!( + row.0 == binding.agent_id.to_string() + && row.1 == binding.enrollment_generation + && row.2 == permit.job_id.to_string() + && row.3 == permit.attempt_id.to_string() + && row.4 + && row.6, + "EVENT_LOG_CONSUMPTION_BINDING" + ); + let start: serde_json::Value = serde_json::from_slice(&row.5)?; + ensure!( + start["previous_boot_id"] == ticket.boot.to_string() + && serde_json::to_vec(&serde_json::from_value::( + start["request"].clone() + )?)? == serde_json::to_vec(&ticket.request)?, + "EVENT_LOG_ORIGINAL_START_BINDING" + ); + let identity: String = db.query_row( + "SELECT identity FROM windows_binding WHERE length(identity)<=512", + [], + |r| r.get(0), + )?; + ensure!( + identity + == format!( + "{}:{}:{}:{}:{}", + binding.tenant_id, + binding.asset_id, + binding.agent_id, + binding.enrollment_generation, + binding.assignment_version + ), + "EVENT_LOG_LEDGER_BINDING" + ); + Ok(()) +} +fn validate_ticket_path(path: &Path, directory: &Path) -> Result<()> { + let name = path + .file_name() + .and_then(|s| s.to_str()) + .context("EVENT_LOG_TICKET_NAME")?; + let hash_name = name + .strip_prefix("eventlog-") + .and_then(|s| s.strip_suffix(".json")) + .context("EVENT_LOG_TICKET_NAME")?; + ensure!( + path.parent() == Some(directory) + && hash_name.len() == 64 + && hash_name + .bytes() + .all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c)), + "EVENT_LOG_TICKET_PATH_BINDING" + ); + Ok(()) +} + +/// Internal mode admission. Failure emits only the main executable's fixed +/// initialization error; no request, query XML or private Win32 error is logged. +pub fn worker(path: &Path) -> Result<()> { + native::require_local_service()?; + service_token(unsafe { GetCurrentProcess() })?; + let image = std::env::current_exe()?; + let actual_parent = parent_pid()?; + ensure!( + agent_pid(&image)? == actual_parent, + "EVENT_LOG_REAL_PARENT_REQUIRED" + ); + let parent = owned(unsafe { + OpenProcess( + PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE, + 0, + actual_parent, + ) + })?; + let parent_birth = birth(parent.as_raw_handle())?; + ensure!( + process_image(parent.as_raw_handle())? == image + && unsafe { WaitForSingleObject(parent.as_raw_handle(), 0) } == WAIT_TIMEOUT, + "EVENT_LOG_PARENT_INSTANCE" + ); + service_token(parent.as_raw_handle())?; + crate::job_binding::check_admin_readonly(Path::new(CONFIG))?; + let mut config_file = open_private(Path::new(CONFIG), false)?; + let config: WorkerConfig = serde_json::from_slice(&read(&mut config_file, 32768)?)?; + ensure!( + config.enable_jobs && config.state_directory.is_absolute(), + "EVENT_LOG_JOBS_NOT_ENABLED" + ); + let directory = config.state_directory.join("jobs"); + native::private_path(&directory, true)?; + validate_ticket_path(path, &directory)?; + // Only the exact jobs leaf is read; PKI/keys and arbitrary private files + // cannot be passed to this mode, even by another LocalService process. + let mut ticket_file = open_private(path, false)?; + let ticket: Ticket = serde_json::from_slice(&read(&mut ticket_file, TICKET_LIMIT)?)?; + ensure!( + ticket.parent_pid == actual_parent && ticket.parent_birth == parent_birth, + "EVENT_LOG_PARENT_INSTANCE" + ); + let binding = crate::job_binding::readonly_binding(&config.job_binding_file)?; + let clock = crate::clock::NativeClock::new(); + let now = clock.now()?; + let (job, permit) = binding.verify(&ticket.request, now - chrono::Duration::seconds(2))?; + binding.verify(&ticket.request, now + chrono::Duration::seconds(2))?; + let query = event_log::for_job(&job)?; + let expected = config + .state_directory + .join("jobs") + .join(ticket_name(&permit)); + ensure!(path == expected, "EVENT_LOG_TICKET_PATH_BINDING"); + let runtime = tokio::runtime::Runtime::new()?; + ensure!( + Uuid::parse_str(&runtime.block_on(native::boot_id())?)? == ticket.boot, + "EVENT_LOG_ORIGINAL_BOOT_REQUIRED" + ); + let ledger_path = config.state_directory.join("consumption.sqlite"); + let ledger_file = open_private(&ledger_path, true)?; + ensure!( + file_id(ledger_file.as_raw_handle())? == ticket.ledger, + "EVENT_LOG_LEDGER_FILE_CHANGED" + ); + let db = rusqlite::Connection::open_with_flags( + &ledger_path, + rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX, + )?; + db.busy_timeout(Duration::from_millis(100))?; + let mut sqlite_handle: HANDLE = ptr::null_mut(); + ensure!( + unsafe { + rusqlite::ffi::sqlite3_file_control( + db.handle(), + c"main".as_ptr(), + rusqlite::ffi::SQLITE_FCNTL_WIN32_GET_HANDLE, + (&mut sqlite_handle as *mut HANDLE).cast(), + ) + } == rusqlite::ffi::SQLITE_OK + && file_id(sqlite_handle)? == ticket.ledger, + "EVENT_LOG_CONNECTED_LEDGER_FILE" + ); + db.execute_batch("PRAGMA query_only=ON; BEGIN;")?; + consumed(&db, &binding, &ticket, &permit)?; + // Per-grant durable CAS prevents worker re-entry after a lost child reply. + let marker = path.with_extension("started"); + let mut once = OpenOptions::new() + .create_new(true) + .write(true) + .share_mode(0) + .open(&marker)?; + once.write_all(b"event-log-dispatch-v1\n")?; + once.sync_all()?; + native::private_path(&marker, false)?; + // File I/O, CIM and child scheduling cannot extend the signed start window. + let now = clock.now()?; + binding.verify(&ticket.request, now - chrono::Duration::seconds(2))?; + binding.verify(&ticket.request, now + chrono::Duration::seconds(2))?; + ensure!( + agent_pid(&image)? == actual_parent + && birth(parent.as_raw_handle())? == ticket.parent_birth + && unsafe { WaitForSingleObject(parent.as_raw_handle(), 0) } == WAIT_TIMEOUT, + "EVENT_LOG_PARENT_CHANGED" + ); + consumed(&db, &binding, &ticket, &permit)?; + let report = event_log::query(&query, &AtomicBool::new(false))?; + let bytes = serde_json::to_vec(&report)?; + ensure!( + bytes.len() <= job.max_output_bytes as usize, + "EVENT_LOG_OUTPUT_LIMIT" + ); + std::io::stdout().lock().write_all(&bytes)?; + Ok(()) +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct WireReport { + channel: String, + provider: String, + start_utc: DateTime, + end_utc: DateTime, + quality: String, + completeness: String, + truncated: bool, + error_code: Option, + win32_error: Option, + events: Vec, + bookmark_updated: bool, + deadline_enforcement: String, +} +fn outcome(job: &JobPayload, bytes: Vec) -> Result { + let q = event_log::for_job(job)?; + ensure!( + bytes.len() <= q.max_output_bytes as usize, + "EVENT_LOG_OUTPUT_LIMIT" + ); + let report: WireReport = serde_json::from_slice(&bytes)?; + ensure!( + report.channel == q.channel + && report.provider == q.provider + && report.start_utc == q.start_utc + && report.end_utc == q.end_utc + && !report.bookmark_updated + && report.events.len() <= q.max_events as usize + && report.deadline_enforcement == "cooperative_between_win32_calls", + "EVENT_LOG_REPORT_BINDING" + ); + let mut ids = std::collections::BTreeSet::new(); + for event in &report.events { + ensure!( + event.channel == q.channel + && event.provider == q.provider + && event.record_id > 0 + && ids.insert(event.record_id) + && event.created_utc >= q.start_utc + && event.created_utc < q.end_utc + && event.xml.len() <= 64 * 1024 + && !event.xml.contains('\0'), + "EVENT_LOG_EVENT_BINDING" + ); + } + let complete = + report.completeness == "complete" && !report.truncated && report.error_code.is_none(); + let partial = report.completeness == "partial" + && report.truncated + && matches!( + report.error_code.as_deref(), + Some("EVENT_LOG_EVENT_LIMIT" | "EVENT_LOG_BYTE_LIMIT" | "EVENT_LOG_EVENT_SIZE_LIMIT") + ); + let success = + report.quality == "valid" && report.win32_error.is_none() && (complete || partial); + let error = + report + .error_code + .as_deref() + .unwrap_or(if success { "" } else { "EVENT_LOG_INCOMPLETE" }); + Ok(Outcome { + state: if success { "succeeded" } else { "failed" }.into(), + stdout: bytes, + stderr: vec![], + exit_code: None, + output_truncated: report.truncated, + reboot_required: false, + error_code: if success { String::new() } else { error.into() }, + evidence: serde_json::json!({"postconditions_passed":success, + "source_id":rmm_domain::jobs::WINDOWS_EVENT_LOG_FIXTURE_SOURCE, + "channel":q.channel,"provider":q.provider,"start_utc":q.start_utc,"end_utc":q.end_utc, + "max_events":q.max_events,"max_output_bytes":q.max_output_bytes, + "quality":report.quality,"completeness":report.completeness,"truncated":report.truncated, + "error_code":report.error_code,"win32_error":report.win32_error, + "bookmark_updated":false,"execution_sid":"S-1-5-19","process_tree_bounded":true, + "cursor_status":"not_advanced_no_ack_contract","observed_at":Utc::now()}), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + fn job() -> JobPayload { + let id = Uuid::new_v4(); + let now = DateTime::from_timestamp(1_700_000_000, 0).unwrap(); + JobPayload { + schema_version: 1, + tenant_id: id, + asset_id: id, + agent_id: id, + enrollment_generation: 1, + assignment_version: 1, + job_id: id, + attempt_id: id, + action: rmm_domain::jobs::Action::QueryLogs { + unit: rmm_domain::jobs::WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(), + since: now, + until: now + chrono::Duration::minutes(5), + max_lines: 2, + }, + execution_identity: "agent_service".into(), + not_before: now, + start_before: now + chrono::Duration::seconds(60), + max_runtime_seconds: 10, + max_output_bytes: 4096, + } + } + #[test] + fn ticket_pre_read_boundary_excludes_keys_siblings_and_noncanonical_leaves() { + let directory = Path::new(r"C:\ProgramData\RustDiscovery\state\jobs"); + let leaf = format!("eventlog-{}.json", "a".repeat(64)); + assert!(validate_ticket_path(&directory.join(&leaf), directory).is_ok()); + for path in [ + PathBuf::from(r"C:\ProgramData\RustDiscovery\state\device-key.pem"), + directory.join("..\\jobs").join(&leaf), + directory.join(format!("eventlog-{}.json", "A".repeat(64))), + directory.join(format!("eventlog-{}.json", "a".repeat(63))), + directory.join(format!("eventlog-{}.json:stream", "a".repeat(64))), + directory.join("other.json"), + ] { + assert!( + validate_ticket_path(&path, directory).is_err(), + "{}", + path.display() + ); + } + } + #[test] + fn event_log_outcome_never_equates_valid_quality_with_complete_or_authorized_success() { + let job = job(); + let q = event_log::for_job(&job).unwrap(); + let mut report = serde_json::json!({"channel":q.channel,"provider":q.provider, + "start_utc":q.start_utc,"end_utc":q.end_utc,"quality":"valid", + "completeness":"complete","truncated":false,"error_code":null,"win32_error":null, + "events":[],"bookmark_updated":false,"deadline_enforcement":"cooperative_between_win32_calls"}); + assert_eq!( + outcome(&job, serde_json::to_vec(&report).unwrap()) + .unwrap() + .state, + "succeeded" + ); + for code in [ + "EVENT_LOG_TIME_LIMIT", + "EVENT_LOG_CANCELLED", + "EVENT_LOG_API_ERROR", + ] { + let mut partial = report.clone(); + partial["truncated"] = true.into(); + partial["completeness"] = "unknown".into(); + partial["error_code"] = code.into(); + assert_eq!( + outcome(&job, serde_json::to_vec(&partial).unwrap()) + .unwrap() + .state, + "failed" + ); + } + report["truncated"] = true.into(); + report["completeness"] = "partial".into(); + report["error_code"] = "EVENT_LOG_BYTE_LIMIT".into(); + let bounded = outcome(&job, serde_json::to_vec(&report).unwrap()).unwrap(); + assert_eq!(bounded.state, "succeeded"); + assert!(bounded.output_truncated); + report["quality"] = "permission_denied".into(); + report["win32_error"] = 5.into(); + assert_eq!( + outcome(&job, serde_json::to_vec(&report).unwrap()) + .unwrap() + .state, + "failed" + ); + report["provider"] = "another-provider".into(); + assert!(outcome(&job, serde_json::to_vec(&report).unwrap()).is_err()); + } +} diff --git a/crates/agent-windows/src/helper_intent.rs b/crates/agent-windows/src/helper_intent.rs index 5e30e2e..2bd0a46 100644 --- a/crates/agent-windows/src/helper_intent.rs +++ b/crates/agent-windows/src/helper_intent.rs @@ -16,16 +16,67 @@ pub const ROOT: &str = r"C:\ProgramData\RustDiscoverySystem\agent-intents"; pub const DB: &str = r"C:\ProgramData\RustDiscoverySystem\agent-intents\system-intents.sqlite"; const ACTOR_RW: u32 = 0x0012_019f; const SCHEMA:&str="CREATE TABLE IF NOT EXISTS system_intent_meta(id INTEGER PRIMARY KEY CHECK(id=1),file_identity BLOB NOT NULL); -CREATE TABLE IF NOT EXISTS system_intents(id TEXT PRIMARY KEY,job TEXT NOT NULL,attempt TEXT NOT NULL,grant_id TEXT NOT NULL UNIQUE,body BLOB NOT NULL CHECK(length(body)<=196608),digest TEXT NOT NULL,phase TEXT NOT NULL CHECK(phase IN ('prepared','known_no_dispatch','may_have_dispatched')),UNIQUE(job,attempt)); +CREATE TABLE IF NOT EXISTS system_intents(id TEXT PRIMARY KEY,job TEXT NOT NULL,attempt TEXT NOT NULL,grant_id TEXT NOT NULL UNIQUE,body BLOB NOT NULL CHECK(length(body)<=196608),digest TEXT NOT NULL,phase TEXT NOT NULL CHECK(phase IN ('prepared','known_no_dispatch','may_have_dispatched')),cancel_requested INTEGER NOT NULL DEFAULT 0 CHECK(cancel_requested IN (0,1)),UNIQUE(job,attempt)); CREATE TABLE IF NOT EXISTS system_intent_mirrors(id TEXT PRIMARY KEY,outcome_sha256 TEXT NOT NULL,mirror_observed_at TEXT NOT NULL); CREATE TABLE IF NOT EXISTS system_intent_denial_resumes(id TEXT NOT NULL,agent_instance TEXT NOT NULL,observed_at TEXT NOT NULL,PRIMARY KEY(id,agent_instance)); CREATE TRIGGER IF NOT EXISTS system_intents_no_delete BEFORE DELETE ON system_intents BEGIN SELECT RAISE(ABORT,'SYSTEM_INTENT_IMMUTABLE');END; -CREATE TRIGGER IF NOT EXISTS system_intents_monotonic BEFORE UPDATE ON system_intents WHEN NEW.id!=OLD.id OR NEW.job!=OLD.job OR NEW.attempt!=OLD.attempt OR NEW.grant_id!=OLD.grant_id OR NEW.body!=OLD.body OR NEW.digest!=OLD.digest OR OLD.phase!='prepared' OR NEW.phase NOT IN ('known_no_dispatch','may_have_dispatched') BEGIN SELECT RAISE(ABORT,'SYSTEM_INTENT_IMMUTABLE');END; +CREATE TRIGGER IF NOT EXISTS system_intents_monotonic BEFORE UPDATE ON system_intents WHEN NEW.id!=OLD.id OR NEW.job!=OLD.job OR NEW.attempt!=OLD.attempt OR NEW.grant_id!=OLD.grant_id OR NEW.body!=OLD.body OR NEW.digest!=OLD.digest OR NOT ((NEW.phase=OLD.phase AND NEW.cancel_requested=1 AND OLD.cancel_requested IN (0,1)) OR (OLD.phase='prepared' AND NEW.phase IN ('known_no_dispatch','may_have_dispatched') AND NEW.cancel_requested=OLD.cancel_requested)) BEGIN SELECT RAISE(ABORT,'SYSTEM_INTENT_IMMUTABLE');END; CREATE TRIGGER IF NOT EXISTS system_mirrors_no_delete BEFORE DELETE ON system_intent_mirrors BEGIN SELECT RAISE(ABORT,'SYSTEM_MIRROR_IMMUTABLE');END; CREATE TRIGGER IF NOT EXISTS system_mirrors_no_update BEFORE UPDATE ON system_intent_mirrors BEGIN SELECT RAISE(ABORT,'SYSTEM_MIRROR_IMMUTABLE');END; CREATE TRIGGER IF NOT EXISTS system_denial_resumes_no_delete BEFORE DELETE ON system_intent_denial_resumes BEGIN SELECT RAISE(ABORT,'SYSTEM_DENIAL_RESUME_IMMUTABLE');END; CREATE TRIGGER IF NOT EXISTS system_denial_resumes_no_update BEFORE UPDATE ON system_intent_denial_resumes BEGIN SELECT RAISE(ABORT,'SYSTEM_DENIAL_RESUME_IMMUTABLE');END;"; +const LEGACY_INTENTS_TABLE: &str = "CREATE TABLE IF NOT EXISTS system_intents(id TEXT PRIMARY KEY,job TEXT NOT NULL,attempt TEXT NOT NULL,grant_id TEXT NOT NULL UNIQUE,body BLOB NOT NULL CHECK(length(body)<=196608),digest TEXT NOT NULL,phase TEXT NOT NULL CHECK(phase IN ('prepared','known_no_dispatch','may_have_dispatched')),UNIQUE(job,attempt));"; +const LEGACY_MONOTONIC: &str = "CREATE TRIGGER IF NOT EXISTS system_intents_monotonic BEFORE UPDATE ON system_intents WHEN NEW.id!=OLD.id OR NEW.job!=OLD.job OR NEW.attempt!=OLD.attempt OR NEW.grant_id!=OLD.grant_id OR NEW.body!=OLD.body OR NEW.digest!=OLD.digest OR OLD.phase!='prepared' OR NEW.phase NOT IN ('known_no_dispatch','may_have_dispatched') BEGIN SELECT RAISE(ABORT,'SYSTEM_INTENT_IMMUTABLE');END;"; const CLAIM_RESUME: &str = "INSERT OR IGNORE INTO system_intent_denial_resumes(id,agent_instance,observed_at) VALUES(?1,?2,?3)"; +fn initialize_schema(db: &mut Connection) -> Result<()> { + let exists: bool = db.query_row( + "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='system_intents')", + [], + |r| r.get(0), + )?; + if exists { + let columns = db + .prepare("PRAGMA table_info(system_intents)")? + .query_map([], |r| r.get::<_, String>(1))? + .collect::>>()?; + if !columns.iter().any(|name| name == "cancel_requested") { + ensure!( + columns + == [ + "id", "job", "attempt", "grant_id", "body", "digest", "phase" + ], + "SYSTEM_INTENT_LEGACY_SCHEMA" + ); + let old:String=db.query_row("SELECT sql FROM sqlite_master WHERE type='trigger' AND name='system_intents_monotonic'",[],|r|r.get(0))?; + let normalize = |s: &str| { + s.replace("IF NOT EXISTS ", "") + .trim_end_matches(';') + .split_whitespace() + .collect::>() + .join(" ") + }; + ensure!( + normalize(&old) == normalize(LEGACY_MONOTONIC), + "SYSTEM_INTENT_LEGACY_TRIGGER" + ); + let table: String = db.query_row( + "SELECT sql FROM sqlite_master WHERE type='table' AND name='system_intents'", + [], + |r| r.get(0), + )?; + ensure!( + normalize(&table) == normalize(LEGACY_INTENTS_TABLE), + "SYSTEM_INTENT_LEGACY_SCHEMA" + ); + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + tx.execute_batch("ALTER TABLE system_intents ADD COLUMN cancel_requested INTEGER NOT NULL DEFAULT 0 CHECK(cancel_requested IN (0,1));DROP TRIGGER system_intents_monotonic;")?; + tx.execute_batch(SCHEMA)?; + tx.commit()?; + } + } + db.execute_batch(SCHEMA)?; + Ok(()) +} #[derive(Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Intent { @@ -148,14 +199,14 @@ impl Store { } else { OpenFlags::SQLITE_OPEN_READ_ONLY }; - let db = Connection::open_with_flags(DB, flags)?; + let mut db = Connection::open_with_flags(DB, flags)?; db.busy_timeout(Duration::from_millis(250))?; if write { rmm_agent_core::ledger::bounded_journal_capacity(&db)?; let mode: String = db.query_row("PRAGMA journal_mode=PERSIST", [], |r| r.get(0))?; ensure!(mode == "persist", "SYSTEM_INTENT_JOURNAL_MODE"); db.execute_batch("PRAGMA synchronous=FULL;PRAGMA temp_store=MEMORY;PRAGMA journal_size_limit=1048576;")?; - db.execute_batch(SCHEMA)?; + initialize_schema(&mut db)?; db.execute( "INSERT OR IGNORE INTO system_intent_meta VALUES(1,?1)", [serde_json::to_vec(&identity)?], @@ -241,10 +292,60 @@ impl Store { } pub fn cancel_before_dispatch(&mut self, id: Uuid) -> Result { self.check()?; + self.db.execute( + "UPDATE system_intents SET cancel_requested=1 WHERE id=?1", + [id.to_string()], + )?; let (_, phase) = advance(&mut self.db, id, Phase::KnownNoDispatch)?; self.check()?; Ok(phase) } + /// Denial only. Original phase/request/anchor never changes, even after IPC. + pub fn deny(&mut self, original: &Request, boot: Uuid) -> Result<()> { + self.check()?; + let permit: ExecutionPermitPayload = serde_json::from_slice(&original.permit_bytes)?; + let intent = self.by_grant(permit.grant_id)?; + ensure!( + serde_json::to_vec(&intent.request)? == serde_json::to_vec(original)? + && intent.original_boot_id == boot, + "SYSTEM_CANCEL_ORIGINAL_BINDING" + ); + self.db.execute( + "UPDATE system_intents SET cancel_requested=1 WHERE id=?1", + [intent.id.to_string()], + )?; + self.check() + } + /// Open a new ordinary RO connection for each observation. No long-lived + /// SQLite read transaction or immutable-mode shortcut may hide actor denial. + pub fn require_not_denied( + original: &Request, + boot: Uuid, + generation: u64, + policy: &str, + binary: &str, + ) -> Result<()> { + let mut store = Self::open(false)?; + let permit: ExecutionPermitPayload = serde_json::from_slice(&original.permit_bytes)?; + let intent = store.by_grant(permit.grant_id)?; + let (_, phase) = store.load(intent.id)?; + ensure!( + serde_json::to_vec(&intent.request)? == serde_json::to_vec(original)? + && intent.original_boot_id == boot + && intent.connection_generation == generation + && phase == Phase::MayHaveDispatched + && intent.policy_sha256 == policy + && intent.helper_binary_sha256 == binary, + "SYSTEM_CANCEL_ORIGINAL_BINDING" + ); + let denied: i64 = store.db.query_row( + "SELECT cancel_requested FROM system_intents WHERE id=?1", + [intent.id.to_string()], + |r| r.get(0), + )?; + ensure!(denied == 0, "SYSTEM_MSI_CANCELLED_OR_FENCED"); + store.check() + } pub fn pending(&self) -> Result> { let mut rows=self.db.prepare("SELECT i.id FROM system_intents i WHERE NOT EXISTS(SELECT 1 FROM system_intent_mirrors m WHERE m.id=i.id) ORDER BY i.rowid LIMIT 2")?; let ids = rows @@ -356,7 +457,7 @@ mod tests { let dispatched = Uuid::new_v4(); for id in [cancelled, dispatched] { db.execute( - "INSERT INTO system_intents VALUES(?1,?1,?1,?1,x'01','immutable','prepared')", + "INSERT INTO system_intents(id,job,attempt,grant_id,body,digest,phase) VALUES(?1,?1,?1,?1,x'01','immutable','prepared')", [id.to_string()], ) .unwrap(); @@ -377,6 +478,29 @@ mod tests { advance(&mut db, dispatched, Phase::KnownNoDispatch).unwrap(), (0, Phase::MayHaveDispatched) ); + for _ in 0..2 { + db.execute( + "UPDATE system_intents SET cancel_requested=1 WHERE id=?1", + [dispatched.to_string()], + ) + .unwrap(); + } + assert_eq!( + db.query_row( + "SELECT phase,cancel_requested FROM system_intents WHERE id=?1", + [dispatched.to_string()], + |r| Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)?)) + ) + .unwrap(), + ("may_have_dispatched".into(), 1) + ); + assert!( + db.execute( + "UPDATE system_intents SET cancel_requested=0 WHERE id=?1", + [dispatched.to_string()] + ) + .is_err() + ); assert!( db.execute( "UPDATE system_intents SET phase='prepared' WHERE id=?1", @@ -411,4 +535,45 @@ mod tests { assert_eq!(db.execute(CLAIM_RESUME, args).unwrap(), 1); assert_eq!(db.execute(CLAIM_RESUME, args).unwrap(), 0); } + #[test] + fn additive_denial_column_preserves_original_legacy_intents_and_rejects_unknown_trigger() { + let mut db = Connection::open_in_memory().unwrap(); + db.execute_batch(LEGACY_INTENTS_TABLE).unwrap(); + db.execute_batch(LEGACY_MONOTONIC).unwrap(); + db.execute("INSERT INTO system_intents VALUES('id','job','attempt','grant',x'0102','original','may_have_dispatched')",[]).unwrap(); + initialize_schema(&mut db).unwrap(); + initialize_schema(&mut db).unwrap(); + assert_eq!( + db.query_row( + "SELECT body,digest,phase,cancel_requested FROM system_intents", + [], + |r| Ok(( + r.get::<_, Vec>(0)?, + r.get::<_, String>(1)?, + r.get::<_, String>(2)?, + r.get::<_, i64>(3)? + )) + ) + .unwrap(), + ( + vec![1, 2], + "original".into(), + "may_have_dispatched".into(), + 0 + ) + ); + db.execute("UPDATE system_intents SET cancel_requested=1", []) + .unwrap(); + assert!( + db.execute("UPDATE system_intents SET body=x'03'", []) + .is_err() + ); + assert!( + db.execute("UPDATE system_intents SET cancel_requested=0", []) + .is_err() + ); + let mut foreign = Connection::open_in_memory().unwrap(); + foreign.execute_batch("CREATE TABLE system_intents(id,job,attempt,grant_id,body,digest,phase);CREATE TRIGGER system_intents_monotonic BEFORE UPDATE ON system_intents BEGIN SELECT 1;END;").unwrap(); + assert!(initialize_schema(&mut foreign).is_err()); + } } diff --git a/crates/agent-windows/src/helper_ipc.rs b/crates/agent-windows/src/helper_ipc.rs index 26268c6..b886761 100644 --- a/crates/agent-windows/src/helper_ipc.rs +++ b/crates/agent-windows/src/helper_ipc.rs @@ -7,6 +7,7 @@ use rmm_agent_core::{ }; use rmm_domain::jobs::hash; use serde::{Deserialize, Serialize, de::DeserializeOwned}; +use sha2::{Digest, Sha256}; use std::{ ffi::c_void, fs::File, @@ -29,7 +30,13 @@ use windows_sys::Win32::{ }; pub const HELPER_SERVICE: &str = "RmmDiscoverySystemHelper"; -pub const PIPE_NAME: &str = r"\\.\pipe\RustDiscovery-System-v1"; +// Framing v2 is deliberately a separate endpoint. Old peers cannot send a +// v1 call to this server or interpret the drain receipt as another reply. +pub const PIPE_NAME: &str = r"\\.\pipe\RustDiscovery-System-v2"; +const FRAME_TAG: [u8; 8] = *b"RDPIPC02"; +const DRAIN_TAG: [u8; 8] = *b"RDPACK02"; +const FRAME_HEADER_LEN: usize = 12; +const DRAIN_RECEIPT_LEN: usize = 56; pub const FRAME_LIMIT: usize = 1024 * 1024; pub const CLIENT_PIPE_RIGHTS: u32 = 0x0012_0083; // data R/W, attributes R, RC, synchronize; no bit4 const READ_MASK: u32 = 0x0012_00a9; @@ -44,6 +51,7 @@ pub enum Stage { ClientSendFrame, ClientReadReply, ClientReplyBinding, + ClientDrainReceipt, ClientReport, ServerConnect, ServerReadHeader, @@ -54,6 +62,7 @@ pub enum Stage { ServerCallBinding, ServerRecheckPeer, ServerReply, + ServerDrainReceipt, Unclassified, } impl Stage { @@ -66,6 +75,7 @@ impl Stage { Self::ClientSendFrame => "client_send_frame", Self::ClientReadReply => "client_read_reply", Self::ClientReplyBinding => "client_reply_binding", + Self::ClientDrainReceipt => "client_drain_receipt", Self::ClientReport => "client_report", Self::ServerConnect => "server_connect", Self::ServerReadHeader => "server_read_header", @@ -76,6 +86,7 @@ impl Stage { Self::ServerCallBinding => "server_call_binding", Self::ServerRecheckPeer => "server_recheck_peer", Self::ServerReply => "server_reply", + Self::ServerDrainReceipt => "server_drain_receipt", Self::Unclassified => "unclassified", } } @@ -123,6 +134,8 @@ fn known_code(text: &str) -> Option<&'static str> { "HELPER_FRAME_CAPACITY", "HELPER_FRAME_EMPTY", "HELPER_FRAME_LENGTH", + "HELPER_FRAME_VERSION", + "HELPER_DRAIN_RECEIPT", "HELPER_HANDLE", "HELPER_IMAGE_PARENT", "HELPER_IMAGE_SIZE", @@ -1189,27 +1202,60 @@ fn encode(value: &impl Serialize) -> Result> { ensure!(!writer.0.is_empty(), "HELPER_FRAME_EMPTY"); Ok(writer.0) } -fn send(pipe: HANDLE, value: &impl Serialize, deadline: Instant) -> Result<()> { - let mut bytes = encode(value)?; - let mut header = u32::try_from(bytes.len())?.to_be_bytes(); - transfer(pipe, &mut header, true, deadline)?; - transfer(pipe, &mut bytes, true, deadline) +fn frame_header(len: usize) -> Result<[u8; FRAME_HEADER_LEN]> { + ensure!((1..=FRAME_LIMIT).contains(&len), "HELPER_FRAME_LENGTH"); + let mut header = [0; FRAME_HEADER_LEN]; + header[..8].copy_from_slice(&FRAME_TAG); + header[8..].copy_from_slice(&u32::try_from(len)?.to_be_bytes()); + Ok(header) } -fn frame_length(header: [u8; 4]) -> Result { - let len = u32::from_be_bytes(header) as usize; +fn send_bytes(pipe: HANDLE, bytes: &mut [u8], deadline: Instant) -> Result<()> { + let mut header = frame_header(bytes.len())?; + transfer(pipe, &mut header, true, deadline)?; + transfer(pipe, bytes, true, deadline) +} +fn send(pipe: HANDLE, value: &impl Serialize, deadline: Instant) -> Result<()> { + send_bytes(pipe, &mut encode(value)?, deadline) +} +fn frame_length(header: [u8; FRAME_HEADER_LEN]) -> Result { + ensure!(header[..8] == FRAME_TAG, "HELPER_FRAME_VERSION"); + let len = u32::from_be_bytes(header[8..].try_into()?) as usize; ensure!((1..=FRAME_LIMIT).contains(&len), "HELPER_FRAME_LENGTH"); Ok(len) } fn length(pipe: HANDLE, deadline: Instant) -> Result { - let mut header = [0u8; 4]; + let mut header = [0u8; FRAME_HEADER_LEN]; transfer(pipe, &mut header, false, deadline)?; frame_length(header) } -fn body(pipe: HANDLE, len: usize, deadline: Instant) -> Result { +fn body_bytes(pipe: HANDLE, len: usize, deadline: Instant) -> Result> { ensure!((1..=FRAME_LIMIT).contains(&len), "HELPER_FRAME_LENGTH"); let mut bytes = vec![0u8; len]; transfer(pipe, &mut bytes, false, deadline)?; - Ok(serde_json::from_slice(&bytes)?) + Ok(bytes) +} +fn body(pipe: HANDLE, len: usize, deadline: Instant) -> Result { + Ok(serde_json::from_slice(&body_bytes(pipe, len, deadline)?)?) +} +// This fixed-size receipt only acknowledges receipt of these transport bytes. +// It never acknowledges durable evidence, clears state, or permits an effect. +fn drain_receipt(challenge: Uuid, reply_bytes: &[u8]) -> [u8; DRAIN_RECEIPT_LEN] { + let mut receipt = [0; DRAIN_RECEIPT_LEN]; + receipt[..8].copy_from_slice(&DRAIN_TAG); + receipt[8..24].copy_from_slice(challenge.as_bytes()); + receipt[24..].copy_from_slice(&Sha256::digest(reply_bytes)); + receipt +} +fn verify_drain_receipt( + receipt: &[u8; DRAIN_RECEIPT_LEN], + challenge: Uuid, + reply_bytes: &[u8], +) -> Result<()> { + ensure!( + *receipt == drain_receipt(challenge, reply_bytes), + "HELPER_DRAIN_RECEIPT" + ); + Ok(()) } pub struct Server { @@ -1305,13 +1351,24 @@ impl Server { peer.check().context(Stage::ServerRecheckPeer)?; Ok(Some(Received { call, peer })) } - pub fn reply(&self, value: &Reply) -> Result<()> { - send( - self.pipe.as_raw_handle(), - value, - Instant::now() + Duration::from_secs(5), - ) - .context(Stage::ServerReply) + pub fn reply(&self, value: &Reply, peer: &mut PeerProof) -> Result<()> { + // Do not disconnect after WriteFile merely copied bytes into a pipe + // buffer. Wait for a receipt from the same fully pinned peer within + // the original reply deadline, without an unbounded FlushFileBuffers. + let deadline = Instant::now() + Duration::from_secs(5); + peer.check().context(Stage::ServerRecheckPeer)?; + let mut bytes = encode(value).context(Stage::ServerReply)?; + send_bytes(self.pipe.as_raw_handle(), &mut bytes, deadline).context(Stage::ServerReply)?; + let mut receipt = [0; DRAIN_RECEIPT_LEN]; + transfer(self.pipe.as_raw_handle(), &mut receipt, false, deadline) + .context(Stage::ServerDrainReceipt)?; + verify_drain_receipt(&receipt, value.challenge, &bytes) + .context(Stage::ServerDrainReceipt)?; + peer.check().context(Stage::ServerRecheckPeer)?; + if Instant::now() >= deadline { + return Err(anyhow::anyhow!("HELPER_PIPE_TIMEOUT")).context(Stage::ServerDrainReceipt); + } + Ok(()) } pub fn disconnect(&self) { unsafe { @@ -1345,7 +1402,9 @@ pub fn exchange(pin: &Pin, call: &Call, timeout: Duration) -> Result { .context(Stage::ClientHelperPeer)?; send(pipe.as_raw_handle(), call, deadline).context(Stage::ClientSendFrame)?; let len = length(pipe.as_raw_handle(), deadline).context(Stage::ClientReadReply)?; - let reply: Reply = body(pipe.as_raw_handle(), len, deadline).context(Stage::ClientReadReply)?; + let reply_bytes = + body_bytes(pipe.as_raw_handle(), len, deadline).context(Stage::ClientReadReply)?; + let reply: Reply = serde_json::from_slice(&reply_bytes).context(Stage::ClientReadReply)?; peer.check().context(Stage::ClientHelperPeer)?; if !(reply.schema_version == 1 && reply.challenge == call.challenge @@ -1366,6 +1425,11 @@ pub fn exchange(pin: &Pin, call: &Call, timeout: Duration) -> Result { ) { return Err(anyhow::anyhow!("HELPER_REPLY_OPERATION")).context(Stage::ClientReplyBinding); } + // Only a complete, authenticated, bound and operation-checked reply gets + // this receipt. Hash the received bytes, never a JSON re-serialization. + let mut receipt = drain_receipt(call.challenge, &reply_bytes); + transfer(pipe.as_raw_handle(), &mut receipt, true, deadline) + .context(Stage::ClientDrainReceipt)?; Ok(reply) } @@ -1388,12 +1452,21 @@ mod tests { #[test] fn untrusted_lengths_are_rejected_before_body_allocation() { for n in [0, (FRAME_LIMIT + 1) as u32, u32::MAX] { - assert!(frame_length(n.to_be_bytes()).is_err()); + let mut header = [0; FRAME_HEADER_LEN]; + header[..8].copy_from_slice(&FRAME_TAG); + header[8..].copy_from_slice(&n.to_be_bytes()); + assert!(frame_length(header).is_err()); + assert!(frame_header(n as usize).is_err()); } - assert_eq!( - frame_length((FRAME_LIMIT as u32).to_be_bytes()).unwrap(), - FRAME_LIMIT - ); + let header = frame_header(FRAME_LIMIT).unwrap(); + assert_eq!(frame_length(header).unwrap(), FRAME_LIMIT); + // Neither a v1 length prefix nor another framing version is accepted. + let mut old = [0; FRAME_HEADER_LEN]; + old[..4].copy_from_slice(&1u32.to_be_bytes()); + assert!(frame_length(old).is_err()); + let mut foreign = header; + foreign[7] = b'1'; + assert!(frame_length(foreign).is_err()); } #[test] fn protocol_does_not_accept_a_shell_operation() { @@ -1417,6 +1490,22 @@ mod tests { assert_eq!(diagnostic(&caller).code, "UNCLASSIFIED_IO_OR_CODEC"); let remote = remote_denial(Stage::ClientReplyBinding, "caller-payload-private-marker"); assert_eq!(diagnostic(&remote).code, "UNCLASSIFIED_REMOTE_DENIAL"); + let challenge = Uuid::from_u128(1); + let raw = br#"{"a":1,"b":2}"#; + let receipt = drain_receipt(challenge, raw); + assert!(verify_drain_receipt(&receipt, challenge, raw).is_ok()); + // Same JSON meaning, different exact bytes must not drain this reply. + assert!(verify_drain_receipt(&receipt, challenge, br#"{"b":2,"a":1}"#).is_err()); + assert!(verify_drain_receipt(&receipt, Uuid::from_u128(2), raw).is_err()); + for offset in [0, 7, 8, 23, 24, DRAIN_RECEIPT_LEN - 1] { + let mut changed = receipt; + changed[offset] ^= 1; + let error = verify_drain_receipt(&changed, challenge, raw) + .unwrap_err() + .context(Stage::ServerDrainReceipt); + assert_eq!(diagnostic(&error).code, "HELPER_DRAIN_RECEIPT"); + assert_eq!(diagnostic(&error).stage, "server_drain_receipt"); + } } #[test] fn reconfigured_service_cannot_adopt_an_old_running_identity() { diff --git a/crates/agent-windows/src/helper_runtime.rs b/crates/agent-windows/src/helper_runtime.rs index 85e3837..765afd7 100644 --- a/crates/agent-windows/src/helper_runtime.rs +++ b/crates/agent-windows/src/helper_runtime.rs @@ -470,6 +470,21 @@ fn verified_mirror_row( Ok(outcome) } fn system_shape(job: &JobPayload) -> bool { + if job.action.full_maintenance() { + return job.execution_identity == "system" + && job.action.validate() + && job.action.valid_execution_identity("system") + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes); + } + if job.action.windows_msi().is_some() { + return job.execution_identity == "system" + && job.action.validate() + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes); + } job.execution_identity == "system" && (1..=120).contains(&job.max_runtime_seconds) && (1..=65536).contains(&job.max_output_bytes) @@ -487,7 +502,7 @@ pub(super) fn checked_outcome(job: &JobPayload, outcome: &Outcome) -> Result<()> matches!( outcome.state.as_str(), "succeeded" | "failed" | "cancelled" | "expired" | "unknown" - ) && !outcome.reboot_required + ) && (job.action.windows_msi().is_some() || !outcome.reboot_required) && outcome .stdout .len() @@ -496,6 +511,82 @@ pub(super) fn checked_outcome(job: &JobPayload, outcome: &Outcome) -> Result<()> "SYSTEM_EXECUTE_OUTCOME_SCOPE" ); if outcome.state == "succeeded" { + if let Some(m) = job.action.windows_msi() { + let e = &outcome.evidence; + let after: crate::package::Snapshot = serde_json::from_value(e["after"].clone())?; + let noop = e["already_desired"] == true; + ensure!( + job.action.validate() + && e["provider"] == "windows.msi.fixture/1" + && e["recipe_sha256"] == m.recipe_sha256 + && e["artifact_sha256"] == m.recipe.artifact.sha256 + && e["postconditions_passed"] == true + && e["state"] == "succeeded" + && e["exit_code"] == serde_json::to_value(outcome.exit_code)? + && e["already_desired"].is_boolean() + && !outcome.output_truncated + && e["reboot_initiated"] == false + && e["automatic_retry"] == false + && after.desired(m), + "SYSTEM_MSI_POSTCONDITION_BINDING" + ); + if noop { + let before: crate::package::Snapshot = serde_json::from_value(e["before"].clone())?; + ensure!( + outcome.exit_code.is_none() + && !outcome.reboot_required + && e["installer_process_started"] == false + && e["transaction_started"] == false + && e["installer_reboot_required"].is_null() + && before.desired(m), + "SYSTEM_MSI_NOOP_BINDING" + ); + } else { + ensure!( + matches!(outcome.exit_code, Some(0 | 3010)) + && e["installer_process_started"] == true + && outcome.reboot_required == (outcome.exit_code == Some(3010)) + && e["installer_reboot_required"] == outcome.reboot_required, + "SYSTEM_MSI_EXIT_BINDING" + ); + } + return Ok(()); + } + if job.action.full_maintenance() { + let Action::RunScript { + script_utf8, + expected_stdout, + windows_execution_profile, + .. + } = &job.action + else { + anyhow::bail!("SYSTEM_SCRIPT_REQUIRED") + }; + let e = &outcome.evidence; + ensure!( + system_shape(job) + && outcome.exit_code == Some(0) + && !outcome.output_truncated + && !outcome.reboot_required + && e["postconditions_passed"] == true + && e["privileged_script_profile"] == "full_maintenance_v1" + && e["execution_sid"] == "S-1-5-18" + && e["script_sha256"] == rmm_domain::jobs::hash(script_utf8.as_bytes()) + && expected_stdout + .as_ref() + .is_none_or(|s| outcome.stdout == s.as_bytes()), + "SYSTEM_SCRIPT_POSTCONDITION_BINDING" + ); + if windows_execution_profile.is_some() { + ensure!( + e["windows_execution_profile"] == "powershell_51_remote_signed" + && e["execution_policy_requested"] == "RemoteSigned" + && e["execution_policy_scope"] == "process", + "SYSTEM_SCRIPT_PROCESS_PROFILE_BINDING" + ); + } + return Ok(()); + } let Action::SetServiceState { unit, state } = &job.action else { anyhow::bail!("SYSTEM_TYPED_SERVICE_REQUIRED"); }; @@ -537,6 +628,86 @@ pub fn map_snapshot(s: &Snapshot) -> HelperSnapshot { .collect(), } } +#[cfg(test)] +mod script_tests { + use super::*; + #[test] + fn system_script_success_requires_exact_profile_identity_bytes_and_raw_output() { + let mut job = msi_test_job(); + job.action = Action::RunScript { + interpreter: "powershell".into(), + script_utf8: "Write-Output 'hi'\r\n".into(), + arguments: vec![], + expected_stdout: Some("hi\r\n".into()), + windows_execution_profile: Some( + rmm_domain::jobs::WindowsExecutionProfile::PowerShell51RemoteSigned, + ), + privileged_script_profile: Some( + rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ), + }; + assert!(system_shape(&job)); + let mut outcome = Outcome { + state: "succeeded".into(), + stdout: b"hi\r\n".to_vec(), + stderr: vec![], + exit_code: Some(0), + output_truncated: false, + reboot_required: false, + error_code: String::new(), + evidence: json!({"postconditions_passed":true,"privileged_script_profile":"full_maintenance_v1","execution_sid":"S-1-5-18","script_sha256":rmm_domain::jobs::hash(b"Write-Output 'hi'\r\n"),"windows_execution_profile":"powershell_51_remote_signed","execution_policy_requested":"RemoteSigned","execution_policy_scope":"process"}), + }; + checked_outcome(&job, &outcome).unwrap(); + outcome.evidence["execution_sid"] = "S-1-5-19".into(); + assert!(checked_outcome(&job, &outcome).is_err()); + outcome.evidence["execution_sid"] = "S-1-5-18".into(); + outcome.stdout = b"other".to_vec(); + assert!(checked_outcome(&job, &outcome).is_err()); + outcome.state = "unknown".into(); + outcome.evidence = serde_json::Value::Null; + checked_outcome(&job, &outcome).unwrap(); + job.execution_identity = "root".into(); + assert!(!system_shape(&job)); + } +} + +#[cfg(test)] +pub(crate) fn msi_test_job() -> JobPayload { + use rmm_domain::windows_execution::*; + let recipe:WindowsMsiRecipe=serde_json::from_value(json!({"schema_version":1,"recipe_id":MSI_RECIPE_ID,"recipe_version":"1","platform":"windows","upgrade_code":MSI_UPGRADE_CODE,"architecture":"x64","context":"machine","command_profile":"fixture_quiet_no_reboot_v1","artifact":{"artifact_id":uuid::Uuid::from_u128(10),"sha256":"ab".repeat(32),"bytes":32768},"postcondition":{"product_code":MSI_PRODUCT_CODE,"product_version":MSI_VERSION,"payload_path":MSI_PAYLOAD,"payload_sha256":MSI_PAYLOAD_10_SHA256,"payload_bytes":33},"no_reboot":true})).unwrap(); + let manifest = ResolvedWindowsMsi { + recipe_sha256: recipe.sha256().unwrap(), + recipe, + }; + let now = chrono::Utc::now(); + let id = uuid::Uuid::from_u128(1); + JobPayload { + schema_version: 1, + tenant_id: id, + asset_id: id, + agent_id: id, + enrollment_generation: 1, + assignment_version: 1, + job_id: id, + attempt_id: id, + action: Action::EnsurePackage { + provider: "msi".into(), + package: MSI_PACKAGE_ID.into(), + version: MSI_VERSION.into(), + architecture: "x64".into(), + recipe_version: "1".into(), + installation_context: "machine".into(), + recipe_id: Some(MSI_RECIPE_ID.into()), + apt_manifest: None, + windows_msi_manifest: Some(Box::new(manifest)), + }, + execution_identity: "system".into(), + not_before: now, + start_before: now + chrono::Duration::minutes(2), + max_runtime_seconds: 30, + max_output_bytes: 4096, + } +} fn write_current_probe(state: &Path, value: &serde_json::Value) -> Result<()> { native::private_path(state, true)?; let path = state.join(PROBE_FILE); @@ -574,6 +745,50 @@ fn write_current_probe(state: &Path, value: &serde_json::Value) -> Result<()> { mod tests { use super::*; #[test] + fn msi_scope_requires_bound_independent_postconditions_and_exact_noop_or_installer_exit() { + let job = msi_test_job(); + assert!(system_shape(&job)); + let m = job.action.windows_msi().unwrap(); + let p = &m.recipe.postcondition; + let snapshot = crate::package::Snapshot { + product_code: p.product_code.clone(), + related_products: vec![p.product_code.clone()], + product_state: Some("5".into()), + product_version: Some(p.product_version.clone()), + payload_sha256: Some(p.payload_sha256.clone()), + payload_bytes: Some(p.payload_bytes), + ..Default::default() + }; + let mut out = Outcome { + state: "succeeded".into(), + exit_code: Some(3010), + reboot_required: true, + evidence: json!({"provider":"windows.msi.fixture/1","recipe_sha256":m.recipe_sha256,"artifact_sha256":m.recipe.artifact.sha256,"postconditions_passed":true,"reboot_initiated":false,"automatic_retry":false,"already_desired":false,"state":"succeeded","exit_code":3010,"installer_process_started":true,"installer_reboot_required":true,"after":snapshot,"before":snapshot}), + ..Outcome::unknown("") + }; + assert!(checked_outcome(&job, &out).is_ok()); + out.reboot_required = false; + assert!(checked_outcome(&job, &out).is_err()); + out.reboot_required = true; + out.evidence["after"]["payload_sha256"] = "cd".repeat(32).into(); + assert!(checked_outcome(&job, &out).is_err()); + out.evidence["after"] = serde_json::to_value(&snapshot).unwrap(); + out.evidence["already_desired"] = true.into(); + out.evidence["exit_code"] = serde_json::Value::Null; + out.exit_code = None; + out.reboot_required = false; + out.evidence["installer_reboot_required"] = serde_json::Value::Null; + out.evidence["installer_process_started"] = false.into(); + out.evidence["transaction_started"] = false.into(); + assert!(checked_outcome(&job, &out).is_ok()); + out.exit_code = Some(0); + assert!(checked_outcome(&job, &out).is_err()); + out.state = "unknown".into(); + out.reboot_required = true; + out.exit_code = Some(1641); + assert!(checked_outcome(&job, &out).is_ok()); + } + #[test] fn system_route_binds_original_request_shape_budget_and_reply_scope() { let id = uuid::Uuid::new_v4(); let now = chrono::Utc::now(); diff --git a/crates/agent-windows/src/helper_system.rs b/crates/agent-windows/src/helper_system.rs index a50ec24..0020fbc 100644 --- a/crates/agent-windows/src/helper_system.rs +++ b/crates/agent-windows/src/helper_system.rs @@ -1,5 +1,6 @@ //! SYSTEM service with an independent signed-sync gate and durable ledger. -//! Only fixed, policy-pinned SCM state changes exist; no arbitrary SYSTEM CLI. +//! Versioned policy independently admits fixed SCM/MSI actions or the explicit +//! full_maintenance_v1 SYSTEM script profile; none inherit another's authority. //! //! Bootstrap contract (not performed by this module): install exactly //! `RmmDiscoverySystemHelper` as an OWN_PROCESS LocalSystem service with its @@ -69,11 +70,72 @@ pub struct Settings { pub max_runtime_seconds: u32, pub enable_effects: bool, pub allow_reboot: bool, + #[serde( + default, + skip_serializing_if = "Option::is_none", + deserialize_with = "strict_recipe_hashes" + )] + pub allowed_msi_recipe_sha256: Option>, + #[serde( + default, + skip_serializing_if = "Option::is_none", + deserialize_with = "rmm_domain::jobs::deserialize_script_profiles" + )] + pub allowed_script_profiles: Option>, +} +fn strict_recipe_hashes<'de, D: serde::Deserializer<'de>>( + d: D, +) -> std::result::Result>, D::Error> { + Vec::::deserialize(d).map(Some) } impl Settings { + fn permits_msi(&self, job: &JobPayload) -> bool { + self.schema_version >= 2 + && job.execution_identity == "system" + && job.action.validate() + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes) + && job.max_runtime_seconds <= self.max_runtime_seconds + && job.action.windows_msi().is_some_and(|m| { + self.allowed_msi_recipe_sha256 + .as_ref() + .is_some_and(|h| h.contains(&m.recipe_sha256)) + }) + } + fn permits_script(&self, job: &JobPayload) -> bool { + self.schema_version == 3 + && job.execution_identity == "system" + && job.action.full_maintenance() + && job.action.valid_execution_identity("system") + && job.action.validate() + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes) + && job.max_runtime_seconds <= self.max_runtime_seconds + && self.allowed_script_profiles.as_deref() + == Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]) + } pub fn validate(&self) -> Result<()> { ensure!( - self.schema_version == 1 + matches!(self.schema_version, 1..=3) + && (self.schema_version >= 2 || self.allowed_msi_recipe_sha256.is_none()) + && (self.schema_version == 3 || self.allowed_script_profiles.is_none()) + && self + .allowed_msi_recipe_sha256 + .as_ref() + .is_none_or(|hashes| { + hashes.len() <= 2 + && hashes.iter().all(|h| ipc::digest(h)) + && hashes + .iter() + .collect::>() + .len() + == hashes.len() + }) + && self.allowed_script_profiles.as_deref().is_none_or( + |v| v == [rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1] + ) && !self.allow_reboot && (1..=120).contains(&self.max_runtime_seconds) && self.allowed_services.len() <= 16, @@ -763,6 +825,11 @@ fn verified_window( Ok((job, permit, now)) } +struct UnsettledMsi { + grant: Uuid, + request_sha256: String, + installer: Box, +} pub struct SystemHelper { settings: Settings, pin: Pin, @@ -773,6 +840,7 @@ pub struct SystemHelper { boot: WindowsBoot, sync: Option>, last_diagnostic: Option, + unsettled_msi: Option, } impl SystemHelper { pub fn open() -> Result { @@ -796,6 +864,7 @@ impl SystemHelper { boot, sync, last_diagnostic: None, + unsettled_msi: None, }) } pub fn pin(&self) -> &Pin { @@ -972,7 +1041,9 @@ impl SystemHelper { ensure!( job.execution_identity == "system" && job.max_runtime_seconds <= self.settings.max_runtime_seconds - && matches!(&job.action,Action::SetServiceState{unit,state} if matches!(state.as_str(),"running"|"stopped")&&self.settings.allowed_services.iter().any(|r|r.name==*unit)), + && (matches!(&job.action,Action::SetServiceState{unit,state} if matches!(state.as_str(),"running"|"stopped")&&self.settings.allowed_services.iter().any(|r|r.name==*unit)) + || self.settings.permits_msi(&job) + || self.settings.permits_script(&job)), "SYSTEM_ABANDON_POLICY_DENIED" ); self.sync()?; @@ -1063,6 +1134,12 @@ impl SystemHelper { ), "SYSTEM_SIGNED_SYNC_REQUIRED" ); + if job.action.full_maintenance() { + return self.execute_script(generation, request, &job, &permit, peer); + } + if job.action.windows_msi().is_some() { + return self.execute_msi(generation, request, &job, &permit, peer); + } let mut provider = PreparedService::prepare(&self.settings, &job)?; self.recheck()?; peer.check()?; @@ -1101,6 +1178,283 @@ impl SystemHelper { ledger::finish(&self.db, &permit, &outcome)?; Ok(outcome) } + fn execute_script( + &mut self, + generation: u64, + request: &Request, + job: &JobPayload, + permit: &ExecutionPermitPayload, + peer: &mut ipc::PeerProof, + ) -> Result { + let began = Instant::now(); + ensure!( + self.settings.permits_script(job), + "SYSTEM_SCRIPT_POLICY_DENIED" + ); + self.observe_unsettled_msi()?; + ensure!( + self.unsettled_msi.is_none(), + "SYSTEM_MSI_PREVIOUS_PROCESS_UNSETTLED" + ); + self.recheck()?; + peer.check()?; + let (_, fresh, recorded_at) = + verified_window(&self.settings.binding, request, &self.clock)?; + ensure!( + &fresh == permit && !STOP.load(Ordering::SeqCst), + "SYSTEM_SCRIPT_START_FENCED" + ); + crate::helper_runtime::intent::Store::require_not_denied( + request, + self.boot.current_boot()?, + generation, + &self.pin.policy_sha256, + &self.pin.helper.binary_sha256, + )?; + ledger::begin_execution( + &mut self.db, + permit, + request, + self.boot.current_boot()?, + recorded_at, + )?; + self.sync.as_mut().unwrap().close(); + let helper_sid = self.settings.helper.service_sid.clone(); + let result = crate::system_script::execute( + job, + permit.grant_id, + &helper_sid, + began, + |before_start| { + self.recheck()?; + peer.check()?; + self.clock.now()?; + ensure!(!STOP.load(Ordering::SeqCst), "SYSTEM_SCRIPT_CANCELLED"); + crate::helper_runtime::intent::Store::require_not_denied( + request, + self.boot.current_boot()?, + generation, + &self.pin.policy_sha256, + &self.pin.helper.binary_sha256, + )?; + if before_start { + verified_window(&self.settings.binding, request, &self.clock)?; + } + Ok(()) + }, + ); + let outcome = + result.unwrap_or_else(|_| Outcome::unknown("SYSTEM_SCRIPT_EFFECT_UNRESOLVED")); + ledger::persist_outcome(&self.db, permit, &outcome)?; + self.guard.check(&self.db)?; + ledger::finish(&self.db, permit, &outcome)?; + Ok(outcome) + } + fn execute_msi( + &mut self, + generation: u64, + request: &Request, + job: &JobPayload, + permit: &ExecutionPermitPayload, + peer: &mut ipc::PeerProof, + ) -> Result { + use crate::package::{Prepared, Run}; + self.observe_unsettled_msi()?; + ensure!( + self.unsettled_msi.is_none(), + "SYSTEM_MSI_PREVIOUS_PROCESS_UNSETTLED" + ); + let began = Instant::now(); + let original_request_sha256 = hash(&serde_json::to_vec(request)?); + let timeout = Duration::from_secs(job.max_runtime_seconds.into()); + let m = job + .action + .windows_msi() + .context("SYSTEM_MSI_MANIFEST_REQUIRED")?; + ensure!( + self.settings.permits_msi(job), + "SYSTEM_MSI_RECIPE_NOT_ALLOWED" + ); + // No implicit bootstrap: reject absent/unprotected cache before consume. + let _cache = PinnedFile::open(Path::new(crate::msi_cache::CACHE), true, &[], &[], false) + .context("MSI_CACHE_NOT_CONFIGURED")?; + self.recheck()?; + peer.check()?; + ensure!( + self.sync.as_ref().unwrap().can_start( + &self.db, + &self.guard, + generation, + &self.clock, + &self.boot + ), + "SYSTEM_SIGNED_SYNC_REQUIRED" + ); + let (_, fresh, recorded_at) = + verified_window(&self.settings.binding, request, &self.clock)?; + ensure!( + &fresh == permit && !STOP.load(Ordering::SeqCst), + "SYSTEM_MSI_START_FENCED" + ); + ledger::begin_execution( + &mut self.db, + permit, + request, + self.boot.current_boot()?, + recorded_at, + )?; + self.sync.as_mut().unwrap().close(); + let result = (|| -> Result { + ensure!( + began.elapsed() < timeout, + "MSI_PREPARATION_TIMEOUT_NO_INSTALLER_START" + ); + crate::helper_runtime::intent::Store::require_not_denied( + request, + self.boot.current_boot()?, + generation, + &self.pin.policy_sha256, + &self.pin.helper.binary_sha256, + )?; + let _copied_archive = crate::msi_cache::import(m, permit.grant_id)?; + ensure!( + began.elapsed() < timeout, + "MSI_PREPARATION_TIMEOUT_NO_INSTALLER_START" + ); + let provider = Prepared::prepare(m, &m.recipe_sha256, began, timeout)?; + let run = provider.start(began, timeout, &STOP, || { + self.recheck()?; + peer.check()?; + ensure!( + !STOP.load(Ordering::SeqCst), + "SYSTEM_MSI_CANCELLED_BEFORE_START" + ); + verified_window(&self.settings.binding, request, &self.clock)?; + crate::helper_runtime::intent::Store::require_not_denied( + request, + self.boot.current_boot()?, + generation, + &self.pin.policy_sha256, + &self.pin.helper.binary_sha256, + )?; + Ok(()) + })?; + let observation = match run { + Run::Noop(value) => *value, // Noop variant can itself be Unknown. + Run::Installer(mut child) => loop { + // Continue observing this original transaction after start. + // A lost clock/peer/agent-denial observation cannot free the + // lane or retroactively produce success; never kill/retry. + let fenced = STOP.load(Ordering::SeqCst) + || self.clock.now().is_err() + || peer.check().is_err() + || self + .boot + .current_boot() + .and_then(|boot| { + crate::helper_runtime::intent::Store::require_not_denied( + request, + boot, + generation, + &self.pin.policy_sha256, + &self.pin.helper.binary_sha256, + ) + }) + .is_err(); + let cancelled = AtomicBool::new(fenced); + match child.poll(&cancelled) { + Ok(Some(mut value)) => { + if fenced { + value.state = "unknown"; + value.code = "MSI_RUNNING_OBSERVATION_FENCED"; + value.postconditions_passed = false; + } + if value.state == "unknown" { + self.unsettled_msi = Some(UnsettledMsi { + grant: permit.grant_id, + request_sha256: original_request_sha256.clone(), + installer: child, + }); + } + break value; + } + Ok(None) => std::thread::sleep(Duration::from_millis(50)), + Err(error) => { + let outcome = Outcome { + evidence: serde_json::json!({"provider":"windows.msi.fixture/1","recipe_sha256":m.recipe_sha256,"artifact_sha256":m.recipe.artifact.sha256,"postconditions_passed":false,"installer_process_started":true,"process_id":child.process_id(),"automatic_retry":false,"observer_error":safe_error(&error)}), + ..Outcome::unknown("MSI_OBSERVER_FAILED_EFFECT_UNRESOLVED") + }; + self.unsettled_msi = Some(UnsettledMsi { + grant: permit.grant_id, + request_sha256: original_request_sha256.clone(), + installer: child, + }); + return Ok(outcome); + } + } + }, + }; + let mut evidence = serde_json::to_value(&observation)?; + evidence["provider"] = "windows.msi.fixture/1".into(); + let mut outcome = Outcome { + state: observation.state.into(), + stdout: vec![], + stderr: vec![], + exit_code: observation.exit_code.map(|v| v as i32), + output_truncated: false, + reboot_required: observation.installer_reboot_required.unwrap_or(false), + error_code: if observation.state == "succeeded" { + String::new() + } else { + observation.code.into() + }, + evidence, + }; + if outcome.state == "succeeded" { + let fresh = self.clock.now().is_ok() && peer.check().is_ok(); + if !fresh || began.elapsed() >= timeout { + outcome.state = "unknown".into(); + outcome.error_code = "MSI_FINAL_OBSERVATION_FENCED".into(); + outcome.evidence["postconditions_passed"] = false.into(); + } + } + Ok(outcome) + })(); + let outcome = result.unwrap_or_else(|error| { + let code = safe_error(&error); + Outcome {state:if began.elapsed() >= timeout {"unknown"} else {"failed"}.into(), + evidence:serde_json::json!({"provider":"windows.msi.fixture/1","recipe_sha256":m.recipe_sha256,"artifact_sha256":m.recipe.artifact.sha256,"postconditions_passed":false,"installer_process_started":false,"transaction_started":false,"automatic_retry":false}), ..Outcome::unknown(&code)} + }); + ledger::persist_outcome(&self.db, permit, &outcome)?; + self.guard.check(&self.db)?; + ledger::finish(&self.db, permit, &outcome)?; + Ok(outcome) + } + fn observe_unsettled_msi(&mut self) -> Result<()> { + let Some(slot) = self.unsettled_msi.as_mut() else { + return Ok(()); + }; + self.guard.check(&self.db)?; + let (start, outcome): (Vec, Vec) = self.db.query_row( + "SELECT start,outcome FROM execution_evidence WHERE grant_id=?1", + [slot.grant.to_string()], + |r| Ok((r.get(0)?, r.get(1)?)), + )?; + let start: serde_json::Value = serde_json::from_slice(&start)?; + let original: Request = serde_json::from_value(start["request"].clone())?; + let saved: Outcome = serde_json::from_slice(&outcome)?; + ensure!( + hash(&serde_json::to_vec(&original)?) == slot.request_sha256 + && saved.state == "unknown", + "SYSTEM_MSI_UNSETTLED_ORIGINAL_BINDING" + ); + if slot.installer.process_exited()? { + self.unsettled_msi = None; + } + // No ledger/outcome update here. An observed late exit never revises + // the original deadline outcome or grants permission for another job. + Ok(()) + } } pub fn safe_error(error: &anyhow::Error) -> String { if error.chain().any(|cause| { @@ -1176,10 +1530,16 @@ unsafe extern "system" fn service_main(_: u32, _: *mut *mut u16) { let server = ipc::Server::bind(helper.pin().clone())?; scm_status(SERVICE_RUNNING, 0); while !STOP.load(Ordering::SeqCst) { + // A failed observation keeps the owned slot and its handles locked. + if helper.observe_unsettled_msi().is_err() + && let Some(sync) = &mut helper.sync + { + sync.close(); + } match server.receive() { Ok(Some(mut incoming)) => { let reply = helper.handle(&incoming.call, &mut incoming.peer); - if let Err(error) = server.reply(&reply) { + if let Err(error) = server.reply(&reply, &mut incoming.peer) { if let Some(sync) = &mut helper.sync { sync.close(); } @@ -1230,6 +1590,80 @@ pub fn dispatch() -> Result<()> { mod tests { use super::*; #[test] + fn old_settings_omit_new_scope_and_only_v2_exact_recipe_hash_and_budget_allow_msi() { + let job = crate::helper_runtime::msi_test_job(); + let m = job.action.windows_msi().unwrap(); + let peer = PeerPin { + service_name: "synthetic".into(), + service_sid: "synthetic".into(), + user_sid: "synthetic".into(), + binary: PathBuf::from("synthetic"), + binary_sha256: "ab".repeat(32), + }; + let mut settings = Settings { + schema_version: 1, + binding: Binding { + tenant_id: job.tenant_id, + asset_id: job.asset_id, + agent_id: job.agent_id, + enrollment_generation: 1, + assignment_version: 1, + public_key_hex: "ab".repeat(32), + }, + agent: peer.clone(), + helper: peer, + allowed_services: vec![], + max_runtime_seconds: 30, + enable_effects: false, + allow_reboot: false, + allowed_msi_recipe_sha256: None, + allowed_script_profiles: None, + }; + let legacy = serde_json::to_value(&settings).unwrap(); + assert!(legacy.get("allowed_msi_recipe_sha256").is_none()); + let mut null = legacy.clone(); + null["allowed_msi_recipe_sha256"] = serde_json::Value::Null; + assert!(serde_json::from_value::(null).is_err()); + assert!(!settings.permits_msi(&job)); + settings.allowed_msi_recipe_sha256 = Some(vec![m.recipe_sha256.clone()]); + assert!(!settings.permits_msi(&job)); + assert_eq!( + settings.validate().unwrap_err().to_string(), + "SYSTEM_POLICY_PROFILE" + ); + settings.schema_version = 2; + assert!(settings.permits_msi(&job)); + assert!(!settings.enable_effects); + settings.max_runtime_seconds = 29; + assert!(!settings.permits_msi(&job)); + settings.max_runtime_seconds = 30; + settings.allowed_msi_recipe_sha256 = Some(vec!["cd".repeat(32)]); + assert!(!settings.permits_msi(&job)); + let mut script = job.clone(); + script.action = Action::RunScript { + interpreter: "powershell".into(), + script_utf8: "Write-Output 'hi'\r\n".into(), + arguments: vec![], + expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: Some( + rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ), + }; + assert!(!settings.permits_script(&script)); + settings.allowed_script_profiles = Some(vec![ + rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ]); + assert!(!settings.permits_script(&script)); + settings.schema_version = 3; + assert!(settings.permits_script(&script)); + script.execution_identity = "root".into(); + assert!(!settings.permits_script(&script)); + let mut null = legacy; + null["allowed_script_profiles"] = serde_json::Value::Null; + assert!(serde_json::from_value::(null).is_err()); + } + #[test] fn service_adapter_exposes_only_implemented_states() { assert_eq!(desired_state("running").unwrap(), SERVICE_RUNNING); assert_eq!(desired_state("stopped").unwrap(), SERVICE_STOPPED); diff --git a/crates/agent-windows/src/job_binding.rs b/crates/agent-windows/src/job_binding.rs new file mode 100644 index 0000000..5e04a78 --- /dev/null +++ b/crates/agent-windows/src/job_binding.rs @@ -0,0 +1,122 @@ +//! Shared protected public signing binding; never learns trust from a worker ticket. +use crate::native; +use anyhow::{Context, Result, ensure}; +use rmm_agent_core::auth::Binding; +use std::path::Path; +fn read_private(path: &Path, limit: u64) -> Result> { + use std::{fs::OpenOptions, io::Read, os::windows::fs::OpenOptionsExt}; + use windows_sys::Win32::Storage::FileSystem::{FILE_FLAG_OPEN_REPARSE_POINT, FILE_SHARE_READ}; + native::private_path(path, false)?; + let mut file = OpenOptions::new() + .read(true) + .share_mode(FILE_SHARE_READ) + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) + .open(path)?; + ensure!(file.metadata()?.len() <= limit, "BINDING_SIZE"); + let mut bytes = Vec::new(); + (&mut file).take(limit + 1).read_to_end(&mut bytes)?; + ensure!(bytes.len() as u64 <= limit, "BINDING_SIZE"); + Ok(bytes) +} +pub fn check_admin_readonly(path: &Path) -> Result<()> { + use std::ptr; + use windows_sys::Win32::{ + Foundation::LocalFree, + Security::{ + self, ACCESS_ALLOWED_ACE, ACL, + Authorization::{ConvertSidToStringSidW, GetNamedSecurityInfoW, SE_FILE_OBJECT}, + DACL_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION, + }, + }; + unsafe fn sid(sid: Security::PSID) -> Result { + let mut raw = ptr::null_mut(); + ensure!( + unsafe { ConvertSidToStringSidW(sid, &mut raw) } != 0, + "BINDING_SID" + ); + let mut len = 0; + while unsafe { *raw.add(len) } != 0 { + len += 1; + ensure!(len < 256, "BINDING_SID_LIMIT"); + } + let value = String::from_utf16(unsafe { std::slice::from_raw_parts(raw, len) })?; + unsafe { + LocalFree(raw.cast()); + } + Ok(value) + } + ensure!(path.is_absolute(), "ABSOLUTE_JOB_BINDING_REQUIRED"); + // The immediate configuration directory must also deny agent replacement. + for (item, directory) in [ + (path, false), + (path.parent().context("BINDING_PARENT")?, true), + ] { + native::private_path(item, directory)?; + let mut owner = ptr::null_mut(); + let mut dacl: *mut ACL = ptr::null_mut(); + let mut descriptor = ptr::null_mut(); + let status = unsafe { + GetNamedSecurityInfoW( + native::wide(item).as_ptr(), + SE_FILE_OBJECT, + OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, + &mut owner, + ptr::null_mut(), + &mut dacl, + ptr::null_mut(), + &mut descriptor, + ) + }; + ensure!(status == 0, "BINDING_SECURITY"); + let check = (|| -> Result<()> { + ensure!(!owner.is_null() && !dacl.is_null(), "BINDING_NULL_SECURITY"); + ensure!( + matches!(unsafe { sid(owner) }?.as_str(), "S-1-5-18" | "S-1-5-32-544"), + "BINDING_ADMIN_OWNER_REQUIRED" + ); + for index in 0..unsafe { (*dacl).AceCount } { + let mut raw = ptr::null_mut(); + ensure!( + unsafe { Security::GetAce(dacl, u32::from(index), &mut raw) } != 0, + "BINDING_ACE" + ); + let ace = unsafe { &*raw.cast::() }; + ensure!( + ace.Header.AceType == 0 + && usize::from(ace.Header.AceSize) + >= std::mem::size_of::(), + "BINDING_ACE_TYPE" + ); + let who = unsafe { sid(ptr::addr_of!(ace.SidStart).cast_mut().cast()) }?; + if !matches!(who.as_str(), "S-1-5-18" | "S-1-5-32-544") { + // Generic write/all, owner/DACL/delete and all file/directory mutation bits. + ensure!(ace.Mask & 0x500d_0156 == 0, "BINDING_AGENT_WRITE_FORBIDDEN"); + } + } + Ok(()) + })(); + unsafe { + LocalFree(descriptor.cast()); + } + check?; + } + Ok(()) +} +pub fn readonly_binding(path: &Path) -> Result { + check_admin_readonly(path)?; + let binding: Binding = serde_json::from_slice(&read_private(path, 8192)?)?; + ensure!( + !binding.tenant_id.is_nil() + && !binding.agent_id.is_nil() + && !binding.asset_id.is_nil() + && binding.enrollment_generation > 0 + && binding.assignment_version > 0 + && binding.public_key_hex.len() == 64 + && binding + .public_key_hex + .bytes() + .all(|b| b.is_ascii_hexdigit()), + "JOB_BINDING_PROFILE" + ); + Ok(binding) +} diff --git a/crates/agent-windows/src/lib.rs b/crates/agent-windows/src/lib.rs index 2aa4213..064fa52 100644 --- a/crates/agent-windows/src/lib.rs +++ b/crates/agent-windows/src/lib.rs @@ -4,6 +4,9 @@ pub mod actions; #[cfg(windows)] pub mod clock; +pub mod event_log; +#[cfg(windows)] +pub mod event_log_job; #[cfg(all(windows, feature = "system-helper"))] pub mod helper_ipc; #[cfg(all(windows, feature = "system-helper"))] @@ -12,13 +15,20 @@ pub mod helper_runtime; pub mod helper_system; #[cfg(windows)] pub mod inventory; +#[cfg(windows)] +pub mod job_binding; pub mod journal; +#[cfg(all(windows, feature = "system-helper"))] +pub mod msi_cache; #[cfg(windows)] pub mod native; +pub mod package; #[cfg(windows)] pub mod process; #[cfg(windows)] pub mod service; +#[cfg(all(windows, feature = "system-helper"))] +mod system_script; pub const SERVICE_NAME: &str = "RmmDiscoveryWindows"; pub const CAPABILITIES: &[&str] = &["windows.base/1", "heartbeat/1"]; diff --git a/crates/agent-windows/src/msi_cache.rs b/crates/agent-windows/src/msi_cache.rs new file mode 100644 index 0000000..f2baa59 --- /dev/null +++ b/crates/agent-windows/src/msi_cache.rs @@ -0,0 +1,178 @@ +//! Fixed public-byte staging and independent SYSTEM cache import. No IPC path, +//! URL or byte-array authority. Directories must be provisioned separately; +//! this module never installs/repairs an ACL, directory, policy or service. +use crate::helper_ipc::{self as ipc, PinnedFile}; +use anyhow::{Context, Result, ensure}; +use rmm_domain::windows_execution::ResolvedWindowsMsi; +use sha2::{Digest, Sha256}; +use std::{ + fs::{File, OpenOptions}, + io::{Read, Write}, + mem::zeroed, + os::windows::{ + fs::OpenOptionsExt, + io::{AsRawHandle, FromRawHandle}, + }, + path::{Path, PathBuf}, + ptr, +}; +use uuid::Uuid; +use windows_sys::Win32::{Foundation::*, Storage::FileSystem::*}; + +pub const AGENT_STATE: &str = r"C:\ProgramData\RustDiscovery\state"; +pub const STAGE: &str = r"C:\ProgramData\RustDiscovery\state\msi-stage"; +pub const CACHE: &str = r"C:\ProgramData\RustDiscoverySystem\cache"; +fn name(m: &ResolvedWindowsMsi) -> Result { + ensure!(m.validate(), "MSI_CACHE_MANIFEST"); + Ok(format!("{}.msi", m.recipe.artifact.sha256)) +} +fn regular(file: &File, directory: bool) -> Result<()> { + let mut i: BY_HANDLE_FILE_INFORMATION = unsafe { zeroed() }; + ensure!( + unsafe { GetFileInformationByHandle(file.as_raw_handle(), &mut i) } != 0 + && i.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == 0 + && (i.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0) == directory + && (directory || i.nNumberOfLinks == 1), + "MSI_CACHE_NONREGULAR" + ); + Ok(()) +} +/// Keep every actual parent open without DELETE sharing throughout the copy. +/// Source data/ACL is untrusted; only the original signed digest authorizes bytes. +fn parents(path: &Path) -> Result> { + let mut held = Vec::new(); + for p in path.ancestors().skip(1) { + let raw = unsafe { + CreateFileW( + ipc::wide(p).as_ptr(), + FILE_READ_ATTRIBUTES, + FILE_SHARE_READ | FILE_SHARE_WRITE, + ptr::null(), + OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, + ptr::null_mut(), + ) + }; + ensure!( + raw != INVALID_HANDLE_VALUE, + "MSI_CACHE_PARENT_NOT_CONFIGURED" + ); + let file = unsafe { File::from_raw_handle(raw) }; + regular(&file, true)?; + held.push(file); + } + Ok(held) +} +fn checked_bytes(path: &Path, m: &ResolvedWindowsMsi) -> Result> { + let _parents = parents(path)?; + let mut file = OpenOptions::new() + .read(true) + .share_mode(FILE_SHARE_READ) + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) + .open(path) + .context("MSI_STAGE_NOT_CONFIGURED")?; + regular(&file, false)?; + let id = ipc::file_identity(&file)?; + ensure!( + file.metadata()?.len() == m.recipe.artifact.bytes && m.recipe.artifact.bytes <= 262144, + "MSI_CACHE_LENGTH" + ); + let mut bytes = Vec::new(); + (&mut file).take(262145).read_to_end(&mut bytes)?; + ensure!( + bytes.len() as u64 == m.recipe.artifact.bytes + && format!("{:x}", Sha256::digest(&bytes)) == m.recipe.artifact.sha256, + "MSI_CACHE_DIGEST" + ); + let again = OpenOptions::new() + .read(true) + .share_mode(FILE_SHARE_READ) + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) + .open(path)?; + regular(&again, false)?; + ensure!(ipc::file_identity(&again)? == id, "MSI_CACHE_FILE_REPLACED"); + Ok(bytes) +} +pub fn stage_configured() -> bool { + crate::native::private_path(Path::new(STAGE), true).is_ok() +} +/// Called by LocalService only for the already approved original job/attempt. +pub fn stage(m: &ResolvedWindowsMsi, bytes: &[u8]) -> Result<()> { + crate::native::require_local_service()?; + let path = Path::new(STAGE).join(name(m)?); + let _parents = parents(&path)?; + crate::native::private_path(Path::new(STAGE), true)?; + ensure!( + bytes.len() as u64 == m.recipe.artifact.bytes + && format!("{:x}", Sha256::digest(bytes)) == m.recipe.artifact.sha256, + "MSI_DOWNLOAD_DIGEST" + ); + if path.try_exists()? { + ensure!(checked_bytes(&path, m)? == bytes, "MSI_STAGE_CONFLICT"); + return Ok(()); + } + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .share_mode(FILE_SHARE_READ) + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) + .open(&path)?; + regular(&file, false)?; + let id = ipc::file_identity(&file)?; + file.write_all(bytes)?; + file.sync_all()?; + ensure!(ipc::file_identity(&file)? == id, "MSI_STAGE_FILE_CHANGED"); + drop(file); + crate::native::private_path(&path, false)?; + ensure!(checked_bytes(&path, m)? == bytes, "MSI_STAGE_CONFLICT"); + Ok(()) +} +/// Called ONLY after full independent helper authorization and durable consume. +/// Publish to the fixed SYS/Admin cache without replacing any existing entry. +/// Returned guards pin the copied archive until the provider holds its own. +pub fn import(m: &ResolvedWindowsMsi, grant: Uuid) -> Result> { + let file_name = name(m)?; + ensure!(!grant.is_nil(), "MSI_CACHE_GRANT"); + let destination = Path::new(CACHE).join(file_name); + let _parents = parents(&destination)?; + let root = PinnedFile::open(Path::new(CACHE), true, &[], &[], false) + .context("MSI_CACHE_NOT_CONFIGURED")?; + if destination.try_exists()? { + let archive = PinnedFile::open(&destination, false, &[], &[], false)?; + checked_bytes(&destination, m)?; + return Ok(vec![root, archive]); + } + let source: PathBuf = Path::new(STAGE).join(name(m)?); + let bytes = checked_bytes(&source, m)?; // Never trust actor ownership/data. + let pending = Path::new(CACHE).join(format!("{}.{}.pending", m.recipe.artifact.sha256, grant)); + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .share_mode(FILE_SHARE_READ) + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) + .open(&pending)?; + regular(&file, false)?; + file.write_all(&bytes)?; + file.sync_all()?; + drop(file); + // Cache directory was already independently validated SYS/Admin-only. New + // SYSTEM-created files inherit that boundary; no ACL repair is performed. + let copied = PinnedFile::open(&pending, false, &[], &[], false)?; + checked_bytes(&pending, m)?; + drop(copied); + ensure!( + unsafe { + MoveFileExW( + ipc::wide(&pending).as_ptr(), + ipc::wide(&destination).as_ptr(), + 0, + ) + } != 0, + "MSI_CACHE_CREATE_ONLY_PUBLICATION" + ); + let archive = PinnedFile::open(&destination, false, &[], &[], false)?; + checked_bytes(&destination, m)?; + Ok(vec![root, archive]) +} diff --git a/crates/agent-windows/src/package.rs b/crates/agent-windows/src/package.rs index b09fea0..6f0b0cd 100644 --- a/crates/agent-windows/src/package.rs +++ b/crates/agent-windows/src/package.rs @@ -4,8 +4,8 @@ //! attempt before start(). Cache population is a separate verified RMM artifact //! operation: this module has no network, URL, UNC, transform or free arguments. //! -//! Only first install/exact noop of v1.0.0 is implemented. Upgrade and EXE are -//! separate deliveries, not guessed modes of this MSI executor. +//! Only fixed1.0 first install/noop and fixed1.0→1.1 major-upgrade/noop. +//! No fresh1.1, downgrade, repair, arbitrary predecessor, or EXE mode. //! MSI metadata APIs are synchronous. The caller owns the outer observation //! deadline; a poll timeout is not a promise that Windows Installer stopped. //! Do not put this controller in a kill-on-close process-tree executor. @@ -15,33 +15,81 @@ //! https://learn.microsoft.com/en-us/windows/win32/msi/error-codes #[cfg(windows)] use anyhow::{Result, ensure}; -#[cfg(windows)] -use rmm_domain::windows_execution::ResolvedWindowsMsi; -use serde::Serialize; +#[cfg(any(windows, test))] +use rmm_domain::windows_execution::{ResolvedWindowsMsi, valid_package_code}; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] pub struct Snapshot { + pub product_code: String, + pub package_code: Option, + pub related_products: Vec, + pub predecessor_state: Option, + pub predecessor_version: Option, + pub predecessor_package_code: Option, pub product_state: Option, pub product_version: Option, pub payload_sha256: Option, pub payload_bytes: Option, } impl Snapshot { - #[cfg(windows)] - fn desired(&self, r: &ResolvedWindowsMsi) -> bool { + #[cfg(any(windows, test))] + pub(crate) fn desired(&self, r: &ResolvedWindowsMsi) -> bool { let p = &r.recipe.postcondition; - self.product_state.as_deref() == Some("5") + self.product_code == p.product_code + && self.related_products == [p.product_code.clone()] + && self.predecessor_state.is_none() + && self.predecessor_version.is_none() + && self.predecessor_package_code.is_none() + && r.recipe + .package_code + .as_ref() + .is_none_or(|code| self.package_code.as_ref() == Some(code)) + && self.product_state.as_deref() == Some("5") && self.product_version.as_deref() == Some(&p.product_version) && self.payload_sha256.as_deref() == Some(&p.payload_sha256) && self.payload_bytes == Some(p.payload_bytes) } #[cfg(any(windows, test))] fn absent(&self) -> bool { - self.product_state.is_none() + self.related_products.is_empty() + && self.package_code.is_none() + && self.predecessor_state.is_none() + && self.predecessor_version.is_none() + && self.predecessor_package_code.is_none() + && self.product_state.is_none() && self.product_version.is_none() && self.payload_sha256.is_none() && self.payload_bytes.is_none() } + #[cfg(any(windows, test))] + fn predecessor(&self, r: &ResolvedWindowsMsi) -> bool { + let Some(old) = &r.recipe.predecessor else { + return false; + }; + self.product_code == r.recipe.postcondition.product_code + && self.related_products == [old.product_code.clone()] + && self.product_state.is_none() + && self.product_version.is_none() + && self.package_code.is_none() + && self.predecessor_state.as_deref() == Some("5") + && self.predecessor_version.as_ref() == Some(&old.product_version) + && self.predecessor_package_code.as_ref().is_some_and(|code| { + valid_package_code(code) && Some(code) != r.recipe.package_code.as_ref() + }) + && self.payload_sha256.as_ref() == Some(&old.payload_sha256) + && self.payload_bytes == Some(old.payload_bytes) + } + #[cfg(any(windows, test))] + fn baseline_allowed(&self, r: &ResolvedWindowsMsi) -> bool { + self.desired(r) + || if r.recipe.predecessor.is_some() { + self.predecessor(r) + } else { + self.absent() + } + } } #[derive(Debug, Serialize)] pub struct Observation { @@ -93,6 +141,94 @@ fn classification( _ => ("unknown", "MSI_FAILURE_EFFECT_UNRESOLVED", None, false), } } +#[cfg(any(windows, test))] +fn within_budget(began: std::time::Instant, timeout: std::time::Duration) -> bool { + began.elapsed() < timeout +} +/// Classify only evidence actually observed inside the original budget. An +/// installer may already have changed the machine when a late poll sees exit0; +/// late evidence cannot retroactively turn that timed-out attempt into success. +#[cfg(any(windows, test))] +fn observed_classification( + exit: u32, + postconditions: bool, + within_deadline: bool, +) -> (&'static str, &'static str, Option, bool) { + let result = classification(exit, postconditions); + if within_deadline { + result + } else { + ( + "unknown", + "MSI_TIMEOUT_EFFECT_UNRESOLVED", + result.2, + result.3, + ) + } +} +#[cfg(any(windows, test))] +fn expected_upgrade_rows() -> Vec>> { + use rmm_domain::windows_execution::*; + let row = |min: &str, max: Option<&str>, attributes: &str, property: &str| { + vec![ + Some(MSI_UPGRADE_CODE.into()), + Some(min.into()), + max.map(str::to_owned), + None, + Some(attributes.into()), + Some("ALL".into()), + Some(property.into()), + ] + }; + // Both lists must contain exactly the one approved ProductCode in the + // LaunchCondition. OnlyDetect never removes an unexpected family member. + vec![ + row("0.0.0", None, "258", MSI_ALL_PRODUCTS), + row(MSI_VERSION, Some(MSI_VERSION), "768", MSI_OLD_PRODUCTS), + ] +} +#[cfg(any(windows, test))] +fn expected_sequence_rows(upgrade: bool) -> Vec>> { + let mut rows = vec![ + ("LaunchConditions", 100), + ("CostInitialize", 800), + ("FileCost", 900), + ("CostFinalize", 1000), + ("InstallValidate", 1400), + ("InstallInitialize", 1500), + ("ProcessComponents", 1600), + ("UnpublishFeatures", 1800), + ("RemoveFiles", 3500), + ("RemoveFolders", 3600), + ("CreateFolders", 3700), + ("InstallFiles", 4000), + ("RegisterUser", 6000), + ("RegisterProduct", 6100), + ("PublishFeatures", 6300), + ("PublishProduct", 6400), + ("InstallFinalize", 6600), + ]; + if upgrade { + rows.extend([ + ("FindRelatedProducts", 25), + ("RemoveExistingProducts", 1550), + ]); + } + rows.sort_by_key(|r| r.1); + rows.into_iter() + .map(|(action, seq)| { + vec![ + Some(action.into()), + if action == "RemoveExistingProducts" { + Some("NOT Installed".into()) + } else { + None + }, + Some(seq.to_string()), + ] + }) + .collect() +} /// Contract and locally protected policy must agree, independently of the job. /// The policy hash must come from the helper's protected settings, never a field /// copied back out of the job being checked. @@ -108,7 +244,7 @@ fn validate(r: &ResolvedWindowsMsi, allowed_recipe_sha256: &str) -> Result<()> { #[cfg(windows)] mod windows { use super::*; - use rmm_domain::windows_execution::{MSI_PAYLOAD, MSI_PRODUCT_CODE}; + use rmm_domain::windows_execution::*; use sha2::{Digest, Sha256}; use std::{ fs::{File, OpenOptions}, @@ -352,6 +488,106 @@ mod windows { ensure!(code == ERROR_NO_MORE_ITEMS, "MSI_DATABASE_NOT_UNIQUE"); Ok(String::from_utf16(&text[..len as usize])?) } + fn msi_rows(db: u32, query: &str, fields: u32) -> Result>>> { + let mut view = 0; + ensure!( + unsafe { MsiDatabaseOpenViewW(db, wide(query).as_ptr(), &mut view) } == 0, + "MSI_ROWS_VIEW" + ); + let view = Msi(view); + ensure!( + unsafe { MsiViewExecute(view.0, 0) } == 0, + "MSI_ROWS_EXECUTE" + ); + let mut rows = Vec::new(); + loop { + let mut record = 0; + let code = unsafe { MsiViewFetch(view.0, &mut record) }; + let record = Msi(record); + if code == ERROR_NO_MORE_ITEMS { + return Ok(rows); + } + ensure!( + code == 0 + && rows.len() < 64 + && unsafe { MsiRecordGetFieldCount(record.0) } == fields, + "MSI_ROWS_BOUND" + ); + let mut row = Vec::new(); + for field in 1..=fields { + if unsafe { MsiRecordIsNull(record.0, field) } != 0 { + row.push(None); + continue; + } + let mut text = [0u16; 256]; + let mut count = 255; + ensure!( + unsafe { MsiRecordGetStringW(record.0, field, text.as_mut_ptr(), &mut count) } + == 0 + && count < 256, + "MSI_ROW_STRING" + ); + row.push(Some(String::from_utf16(&text[..count as usize])?)); + } + rows.push(row); + } + } + fn inspect_upgrade(db: u32, r: &ResolvedWindowsMsi) -> Result<()> { + let upgrade = r.recipe.predecessor.is_some(); + let tables = msi_rows(db, "SELECT `Name` FROM `_Tables` WHERE `Name`='Upgrade'", 1)?; + if upgrade { + ensure!( + tables == vec![vec![Some("Upgrade".into())]], + "MSI_UPGRADE_TABLE_REQUIRED" + ); + ensure!( + msi_rows( + db, + "SELECT `UpgradeCode`,`VersionMin`,`VersionMax`,`Language`,`Attributes`,`Remove`,`ActionProperty` FROM `Upgrade` ORDER BY `ActionProperty`", + 7 + )? == expected_upgrade_rows(), + "MSI_EXACT_UPGRADE_TABLE_REQUIRED" + ); + ensure!( + msi_text( + db, + "SELECT `Value` FROM `Property` WHERE `Property`='SecureCustomProperties'" + )? == format!("{MSI_ALL_PRODUCTS};{MSI_OLD_PRODUCTS}"), + "MSI_SECURE_UPGRADE_PROPERTIES" + ); + ensure!( + msi_rows( + db, + "SELECT `Condition`,`Description` FROM `LaunchCondition` ORDER BY `Condition`", + 2 + )? == vec![ + vec![ + Some(MSI_UPGRADE_CONDITION.into()), + Some( + "Only the exact approved fixture1.0 predecessor may be upgraded." + .into() + ) + ], + vec![ + Some("VersionNT64".into()), + Some("This fixture requires 64-bit Windows.".into()) + ] + ], + "MSI_EXACT_UPGRADE_LAUNCH_CONDITIONS" + ); + } else { + ensure!(tables.is_empty(), "MSI_UNEXPECTED_UPGRADE_TABLE"); + } + ensure!( + msi_rows( + db, + "SELECT `Action`,`Condition`,`Sequence` FROM `InstallExecuteSequence` ORDER BY `Sequence`", + 3 + )? == expected_sequence_rows(upgrade), + "MSI_EXACT_EXECUTE_SEQUENCE_REQUIRED" + ); + Ok(()) + } fn no_custom_actions(db: u32) -> Result<()> { let mut view = 0; ensure!( @@ -409,6 +645,7 @@ mod windows { ); let db = Msi(db); no_custom_actions(db.0)?; + inspect_upgrade(db.0, r)?; let cabinet = msi_text(db.0, "SELECT `Cabinet` FROM `Media`")?; ensure!( cabinet.starts_with('#') @@ -420,7 +657,7 @@ mod windows { "MSI_EMBEDDED_CABINET_REQUIRED" ); for (field, value) in [ - ("ProductCode", MSI_PRODUCT_CODE), + ("ProductCode", r.recipe.postcondition.product_code.as_str()), ( "ProductVersion", r.recipe.postcondition.product_version.as_str(), @@ -467,15 +704,41 @@ mod windows { String::from_utf16(&text[..len as usize])?.starts_with("x64;"), "MSI_NOT_X64" ); + let mut len = 255; + ensure!( + unsafe { + MsiSummaryInfoGetPropertyW( + summary.0, + 9, + &mut kind, + &mut value, + &mut time, + text.as_mut_ptr(), + &mut len, + ) + } == 0 + && kind == 30 + && len < 256, + "MSI_PACKAGE_SUMMARY" + ); + let package_code = String::from_utf16(&text[..len as usize])?; + ensure!( + valid_package_code(&package_code) + && r.recipe + .package_code + .as_ref() + .is_none_or(|code| code == &package_code), + "MSI_PACKAGE_CODE_BINDING" + ); file.check()?; Ok(()) } - fn property(name: &str) -> Result> { + fn property(product: &str, name: &str) -> Result> { let mut value = [0u16; 256]; let mut len = 255; let code = unsafe { MsiGetProductInfoExW( - wide(MSI_PRODUCT_CODE).as_ptr(), + wide(product).as_ptr(), ptr::null(), MSIINSTALLCONTEXT_MACHINE, wide(name).as_ptr(), @@ -489,13 +752,58 @@ mod windows { ensure!(code == 0 && len < 256, "MSI_MACHINE_PRODUCT_QUERY"); Ok(Some(String::from_utf16(&value[..len as usize])?)) } - pub fn snapshot() -> Result { - let product_state = property("State")?; + fn snapshot_product(product: &str, predecessor: Option<&str>) -> Result { + let product_state = property(product, "State")?; let product_version = if product_state.as_deref() == Some("5") { - property("VersionString")? + property(product, "VersionString")? } else { None }; + let package_code = if product_state.as_deref() == Some("5") { + property(product, "PackageCode")? + } else { + None + }; + let predecessor_state = predecessor + .map(|p| property(p, "State")) + .transpose()? + .flatten(); + let predecessor_version = if predecessor_state.as_deref() == Some("5") { + property(predecessor.unwrap(), "VersionString")? + } else { + None + }; + let predecessor_package_code = if predecessor_state.as_deref() == Some("5") { + property(predecessor.unwrap(), "PackageCode")? + } else { + None + }; + let mut related_products = Vec::new(); + for index in 0..=16 { + let mut code = [0u16; 39]; + let status = unsafe { + MsiEnumRelatedProductsW( + wide(MSI_UPGRADE_CODE).as_ptr(), + 0, + index, + code.as_mut_ptr(), + ) + }; + if status == ERROR_NO_MORE_ITEMS { + break; + } + ensure!( + status == ERROR_SUCCESS && index < 16 && code[38] == 0, + "MSI_RELATED_PRODUCT_QUERY" + ); + let code = String::from_utf16(&code[..38])?; + ensure!( + !related_products.contains(&code), + "MSI_DUPLICATE_RELATED_PRODUCT" + ); + related_products.push(code); + } + related_products.sort(); let (payload_sha256, payload_bytes) = match std::fs::symlink_metadata(MSI_PAYLOAD) { Ok(_) => { let mut file = Held::open(Path::new(MSI_PAYLOAD), false)?; @@ -506,12 +814,32 @@ mod windows { Err(e) => return Err(e.into()), }; Ok(Snapshot { + product_code: product.into(), + package_code, + related_products, + predecessor_state, + predecessor_version, + predecessor_package_code, product_state, product_version, payload_sha256, payload_bytes, }) } + /// Retained1.0 read-only witness. No implicit choice of upgrade recipe. + pub fn snapshot() -> Result { + snapshot_product(MSI_PRODUCT_CODE, None) + } + pub fn snapshot_for(r: &ResolvedWindowsMsi) -> Result { + ensure!(r.validate(), "MSI_RECIPE_INVALID"); + snapshot_product( + &r.recipe.postcondition.product_code, + r.recipe + .predecessor + .as_ref() + .map(|old| old.product_code.as_str()), + ) + } fn system_root() -> Result { let mut value = [0u16; 32768]; let len = unsafe { GetSystemWindowsDirectoryW(value.as_mut_ptr(), value.len() as u32) }; @@ -527,8 +855,17 @@ mod windows { system_root: PathBuf, } impl Prepared { - pub fn prepare(r: &ResolvedWindowsMsi, allowed_recipe_sha256: &str) -> Result { + pub fn prepare( + r: &ResolvedWindowsMsi, + allowed_recipe_sha256: &str, + began: Instant, + timeout: Duration, + ) -> Result { validate(r, allowed_recipe_sha256)?; + ensure!( + within_budget(began, timeout), + "MSI_PREPARATION_TIMEOUT_NO_INSTALLER_START" + ); let mut roots = Vec::new(); for path in [CACHE, FIXTURES] { let file = OpenOptions::new() @@ -545,11 +882,19 @@ mod windows { false, )?; inspect_archive(&mut archive, r)?; + ensure!( + within_budget(began, timeout), + "MSI_PREPARATION_TIMEOUT_NO_INSTALLER_START" + ); let system_root = system_root()?; let msiexec = Held::open(&system_root.join("System32").join("msiexec.exe"), true)?; - let before = snapshot()?; + let before = snapshot_for(r)?; ensure!( - before.absent() || before.desired(r), + within_budget(began, timeout), + "MSI_PREPARATION_TIMEOUT_NO_INSTALLER_START" + ); + ensure!( + before.baseline_allowed(r), "MSI_EXISTING_PRODUCT_OR_PAYLOAD_CONFLICT" ); Ok(Self { @@ -561,10 +906,30 @@ mod windows { system_root, }) } + fn verification_timeout(&self, after: Option) -> Run { + Run::Noop(Box::new(Observation { + recipe_sha256: self.recipe.recipe_sha256.clone(), + artifact_sha256: self.recipe.recipe.artifact.sha256.clone(), + process_id: None, + state: "unknown", + code: "MSI_VERIFICATION_TIMEOUT_NO_INSTALLER_START", + exit_code: None, + postconditions_passed: false, + already_desired: false, + installer_process_started: false, + transaction_started: Some(false), + installer_reboot_required: None, + reboot_initiated: false, + before: self.before.clone(), + after, + automatic_retry: false, + })) + } /// Called only after durable original-grant consumption. No change of /// config, privilege, policy, clock, reboot or foreign process occurs. pub fn start( mut self, + began: Instant, timeout: Duration, cancelled: &AtomicBool, check_effect_fence: impl FnOnce() -> Result<()>, @@ -575,10 +940,34 @@ mod windows { && !cancelled.load(Ordering::SeqCst), "MSI_START_LIMIT_OR_CANCELLED" ); - inspect_archive(&mut self.archive, &self.recipe)?; - self.msiexec.check()?; - let before = snapshot()?; + // One original budget includes all synchronous verification and + // the eventual installer. A late Noop is not retroactive success. + let archive = inspect_archive(&mut self.archive, &self.recipe); + if !within_budget(began, timeout) { + return Ok(self.verification_timeout(None)); + } + archive?; + let executable = self.msiexec.check(); + if !within_budget(began, timeout) { + return Ok(self.verification_timeout(None)); + } + executable?; + let observation = snapshot_for(&self.recipe); + if !within_budget(began, timeout) { + return Ok(self.verification_timeout(observation.ok())); + } + let before = observation?; ensure!(before == self.before, "MSI_BASELINE_CHANGED"); + // Both a Noop and an installer decision require a fresh caller + // authorization fence; no bypass for the already-desired branch. + check_effect_fence()?; + if !within_budget(began, timeout) { + return Ok(self.verification_timeout(Some(before))); + } + ensure!( + !cancelled.load(Ordering::SeqCst), + "MSI_CANCELLED_BEFORE_START" + ); if before.desired(&self.recipe) { return Ok(Run::Noop(Box::new(Observation { recipe_sha256: self.recipe.recipe_sha256.clone(), @@ -598,14 +987,11 @@ mod windows { automatic_retry: false, }))); } - ensure!(before.absent(), "MSI_INSTALL_BASELINE_CONFLICT"); - use std::os::windows::process::CommandExt; - check_effect_fence()?; ensure!( - !cancelled.load(Ordering::SeqCst), - "MSI_CANCELLED_BEFORE_START" + before.baseline_allowed(&self.recipe), + "MSI_INSTALL_BASELINE_CONFLICT" ); - let began = Instant::now(); + use std::os::windows::process::CommandExt; let child = Command::new(&self.msiexec.path) .args([ std::ffi::OsStr::new("/i"), @@ -642,6 +1028,11 @@ mod windows { timeout: Duration, } impl Installer { + /// Observe only this retained process handle. Never adopt a PID, kill, + /// restart, or revise an already persisted timeout outcome. + pub fn process_exited(&mut self) -> Result { + Ok(self.child.try_wait()?.is_some()) + } pub fn process_id(&self) -> u32 { self.child.id() } @@ -657,14 +1048,21 @@ mod windows { }; if exit.is_none() && !cancelled.load(Ordering::SeqCst) - && self.began.elapsed() < self.timeout + && within_budget(self.began, self.timeout) { return Ok(None); } let mut after = None; let (state, error, reboot, initiated) = if let Some(code) = code { - after = snapshot().ok(); - if after.is_none() && matches!(code, 0 | 3010) { + // No extra synchronous MSI query once the original budget has + // expired. try_wait and snapshot can themselves return late. + if within_budget(self.began, self.timeout) { + after = snapshot_for(&self.prepared.recipe).ok(); + } + let within_deadline = within_budget(self.began, self.timeout); + if !within_deadline { + observed_classification(code, false, false) + } else if after.is_none() && matches!(code, 0 | 3010) { ( "unknown", "MSI_POSTCONDITION_QUERY_FAILED", @@ -672,11 +1070,12 @@ mod windows { false, ) } else { - classification( + observed_classification( code, after .as_ref() .is_some_and(|s| s.desired(&self.prepared.recipe)), + within_deadline, ) } } else { @@ -716,11 +1115,124 @@ mod windows { } } #[cfg(windows)] -pub use windows::{Installer, Prepared, Run, snapshot}; +pub use windows::{Installer, Prepared, Run, snapshot, snapshot_for}; #[cfg(test)] mod tests { use super::*; #[test] + fn upgrade_and_noop_need_independent_exact_products_family_and_payload() { + use rmm_domain::windows_execution::*; + let recipe = WindowsMsiRecipe { + schema_version: 1, + recipe_id: MSI_UPGRADE_RECIPE_ID.into(), + recipe_version: "1".into(), + platform: "windows".into(), + upgrade_code: MSI_UPGRADE_CODE.into(), + architecture: Architecture::X64, + context: InstallContext::Machine, + command_profile: MsiCommandProfile::FixtureQuietNoRebootV1, + artifact: MsiArtifact { + artifact_id: uuid::Uuid::new_v4(), + sha256: "ab".repeat(32), + bytes: 20480, + }, + postcondition: MsiPostcondition { + product_code: MSI_UPGRADE_PRODUCT_CODE.into(), + product_version: MSI_UPGRADE_VERSION.into(), + payload_path: MSI_PAYLOAD.into(), + payload_sha256: MSI_PAYLOAD_11_SHA256.into(), + payload_bytes: MSI_PAYLOAD_BYTES, + }, + no_reboot: true, + package_code: Some("{8475C4D1-6EE2-4C3A-B41D-D834A20F4135}".into()), + predecessor: Some(MsiPostcondition { + product_code: MSI_PRODUCT_CODE.into(), + product_version: MSI_VERSION.into(), + payload_path: MSI_PAYLOAD.into(), + payload_sha256: MSI_PAYLOAD_10_SHA256.into(), + payload_bytes: MSI_PAYLOAD_BYTES, + }), + }; + let r = ResolvedWindowsMsi { + recipe_sha256: recipe.sha256().unwrap(), + recipe, + }; + assert!(r.validate()); + let mut before = Snapshot { + product_code: MSI_UPGRADE_PRODUCT_CODE.into(), + related_products: vec![MSI_PRODUCT_CODE.into()], + predecessor_state: Some("5".into()), + predecessor_version: Some(MSI_VERSION.into()), + predecessor_package_code: Some("{D1C4ECE2-7025-4E16-A842-A20F320CE783}".into()), + payload_sha256: Some(MSI_PAYLOAD_10_SHA256.into()), + payload_bytes: Some(MSI_PAYLOAD_BYTES), + ..Snapshot::default() + }; + assert!(before.predecessor(&r)); + assert!(before.baseline_allowed(&r)); + assert!(!before.desired(&r)); + assert!(!Snapshot::default().baseline_allowed(&r)); // no fresh1.1 fallback + before + .related_products + .push("{11111111-1111-4111-8111-111111111111}".into()); + assert!(!before.baseline_allowed(&r)); + let mut after = Snapshot { + product_code: MSI_UPGRADE_PRODUCT_CODE.into(), + package_code: r.recipe.package_code.clone(), + related_products: vec![MSI_UPGRADE_PRODUCT_CODE.into()], + product_state: Some("5".into()), + product_version: Some(MSI_UPGRADE_VERSION.into()), + payload_sha256: Some(MSI_PAYLOAD_11_SHA256.into()), + payload_bytes: Some(MSI_PAYLOAD_BYTES), + ..Snapshot::default() + }; + assert!(after.desired(&r)); + after.predecessor_state = Some("5".into()); + assert!(!after.desired(&r)); + after.predecessor_state = None; + after.payload_sha256 = Some(MSI_PAYLOAD_10_SHA256.into()); + assert!(!after.desired(&r)); + let table = expected_upgrade_rows(); + assert_eq!(table.len(), 2); + assert_eq!(table[0][4].as_deref(), Some("258")); + assert_eq!(table[1][4].as_deref(), Some("768")); + let sequence = expected_sequence_rows(true); + let position = |name: &str| { + sequence + .iter() + .position(|r| r[0].as_deref() == Some(name)) + .unwrap() + }; + assert!(position("FindRelatedProducts") < position("LaunchConditions")); + assert!(position("InstallInitialize") < position("RemoveExistingProducts")); + assert!(position("RemoveExistingProducts") < position("ProcessComponents")); + } + #[test] + fn late_exit_or_postcondition_cannot_retroactively_succeed_or_retry() { + use std::time::{Duration, Instant}; + let original = Instant::now().checked_sub(Duration::from_secs(2)).unwrap(); + let timely = within_budget(Instant::now(), Duration::from_secs(60)); + let late = within_budget(original, Duration::from_secs(1)); + assert!(timely); + assert!(!late); + // This same budget gates the Noop decision and the installer poll; + // metadata returning late does not receive a fresh timer. + assert_eq!(observed_classification(0, true, late).0, "unknown"); + for exit in [0, 3010, 1618, 1603, 1641] { + let late = observed_classification(exit, true, false); + assert_eq!( + (late.0, late.1), + ("unknown", "MSI_TIMEOUT_EFFECT_UNRESOLVED") + ); + assert_eq!( + (late.2, late.3), + (classification(exit, true).2, classification(exit, true).3) + ); + } + assert_eq!(observed_classification(0, true, true).0, "succeeded"); + assert_eq!(observed_classification(0, false, true).0, "failed"); + } + #[test] fn exit_code_never_substitutes_for_independent_postconditions_or_reboot_authority() { assert_eq!( classification(0, true), @@ -752,6 +1264,7 @@ mod tests { product_version: None, payload_sha256: None, payload_bytes: None, + ..Snapshot::default() }; assert!(!advertised.absent()); } diff --git a/crates/agent-windows/src/process.rs b/crates/agent-windows/src/process.rs index cfdd377..087e9ea 100644 --- a/crates/agent-windows/src/process.rs +++ b/crates/agent-windows/src/process.rs @@ -194,9 +194,31 @@ pub fn run( seconds: u32, max_bytes: usize, cancel: &AtomicBool, +) -> Result { + run_observed( + executable, + arguments, + cwd, + Duration::from_secs(seconds.into()), + max_bytes, + cancel, + || Ok(()), + ) +} +pub(crate) fn run_observed( + executable: &Path, + arguments: &[OsString], + cwd: &Path, + timeout: Duration, + max_bytes: usize, + cancel: &AtomicBool, + mut observe: impl FnMut() -> Result<()>, ) -> Result { ensure!( - executable.is_absolute() && cwd.is_absolute() && seconds > 0 && max_bytes <= 512 * 1024, + executable.is_absolute() + && cwd.is_absolute() + && !timeout.is_zero() + && max_bytes <= 512 * 1024, "PROCESS_LIMITS" ); let (stdout, out_write) = pipe()?; @@ -268,7 +290,17 @@ pub fn run( // Minimal environment: no inherited tokens, proxy secrets, device paths, // PSModulePath overrides, user profile or caller-controlled executable PATH. let mut environment = Vec::new(); + let system_text = system + .to_str() + .ok_or_else(|| anyhow::anyhow!("SYSTEM_DIRECTORY_ENCODING"))?; + ensure!( + system_text.as_bytes().get(1) == Some(&b':') + && system_text.as_bytes()[0].is_ascii_alphabetic(), + "SYSTEM_DRIVE_REQUIRED" + ); + let system_drive = &system_text[..2]; for value in [ + format!("SystemDrive={system_drive}"), format!("PATH={}\\System32;{}", system.display(), system.display()), format!("SystemRoot={}", system.display()), format!("TEMP={}", cwd.display()), @@ -279,6 +311,11 @@ pub fn run( } environment.push(0); let mut process: PROCESS_INFORMATION = unsafe { zeroed() }; + observe()?; + ensure!( + !cancel.load(Ordering::SeqCst), + "PROCESS_CANCELLED_BEFORE_START" + ); ensure!( unsafe { CreateProcessW( @@ -303,32 +340,101 @@ pub fn run( drop(stdin); drop(out_write); drop(err_write); - let mut result = Output { - stdout: Vec::new(), - stderr: Vec::new(), - exit_code: None, - truncated: false, - interrupted: false, - cancelled: false, - }; - let began = Instant::now(); - let mut remaining = max_bytes; - loop { - drain( - &stdout, - &mut result.stdout, - &mut remaining, - &mut result.truncated, - )?; - drain( - &stderr, - &mut result.stderr, - &mut remaining, - &mut result.truncated, - )?; - let mut accounting: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = unsafe { zeroed() }; + let result = (|| -> Result { + let mut result = Output { + stdout: Vec::new(), + stderr: Vec::new(), + exit_code: None, + truncated: false, + interrupted: false, + cancelled: false, + }; + let began = Instant::now(); + let mut remaining = max_bytes; + loop { + drain( + &stdout, + &mut result.stdout, + &mut remaining, + &mut result.truncated, + )?; + drain( + &stderr, + &mut result.stderr, + &mut remaining, + &mut result.truncated, + )?; + let mut accounting: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = unsafe { zeroed() }; + ensure!( + unsafe { + QueryInformationJobObject( + job.as_raw_handle(), + JobObjectBasicAccountingInformation, + ptr::addr_of_mut!(accounting).cast(), + size_of_val(&accounting) as u32, + ptr::null_mut(), + ) + } != 0, + "PROCESS_TREE_STATE" + ); + if accounting.ActiveProcesses == 0 { + break; + } + let fenced = observe().is_err(); + if !result.interrupted + && (began.elapsed() >= timeout || cancel.load(Ordering::SeqCst) || fenced) + { + result.cancelled = cancel.load(Ordering::SeqCst) || fenced; + result.interrupted = true; + ensure!( + unsafe { TerminateJobObject(job.as_raw_handle(), 1) } != 0, + "PROCESS_TREE_TERMINATION" + ); + // This wait proves termination, not absence of effects. + ensure!( + unsafe { WaitForSingleObject(process_handle.as_raw_handle(), 5000) } == 0, + "PROCESS_TERMINATION_UNCONFIRMED" + ); + // Keep this JobObject/process handle owner until *all* assigned + // processes are observed inactive. Waiting for the leader alone + // cannot release the helper lane or heal an interrupted outcome. + } + std::thread::sleep(Duration::from_millis(5)); + } + // No writer can outlive job destruction. Drain the bounded OS pipe buffers. + for _ in 0..8 { + drain( + &stdout, + &mut result.stdout, + &mut remaining, + &mut result.truncated, + )?; + drain( + &stderr, + &mut result.stderr, + &mut remaining, + &mut result.truncated, + )?; + } + let mut code = 0; ensure!( - unsafe { + unsafe { GetExitCodeProcess(process_handle.as_raw_handle(), &mut code) } != 0 + && code != STILL_ACTIVE as u32, + "PROCESS_EXIT_UNCONFIRMED" + ); + result.exit_code = Some(code as i32); + Ok(result) + })(); + if result.is_err() { + // Keep exact owned handles and the caller's helper lane on any observer + // or I/O error. Termination/leader exit alone cannot prove the whole + // assigned tree inactive; never drop ownership and admit replacement. + unsafe { + TerminateJobObject(job.as_raw_handle(), 1); + } + loop { + let mut accounting: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = unsafe { zeroed() }; + let ok = unsafe { QueryInformationJobObject( job.as_raw_handle(), JobObjectBasicAccountingInformation, @@ -336,52 +442,14 @@ pub fn run( size_of_val(&accounting) as u32, ptr::null_mut(), ) - } != 0, - "PROCESS_TREE_STATE" - ); - if accounting.ActiveProcesses == 0 { - break; + }; + if ok != 0 && accounting.ActiveProcesses == 0 { + break; + } + std::thread::sleep(Duration::from_millis(50)); } - if began.elapsed() >= Duration::from_secs(seconds.into()) || cancel.load(Ordering::SeqCst) { - result.cancelled = cancel.load(Ordering::SeqCst); - result.interrupted = true; - ensure!( - unsafe { TerminateJobObject(job.as_raw_handle(), 1) } != 0, - "PROCESS_TREE_TERMINATION" - ); - // This wait proves termination, not absence of effects. - ensure!( - unsafe { WaitForSingleObject(process_handle.as_raw_handle(), 5000) } == 0, - "PROCESS_TERMINATION_UNCONFIRMED" - ); - break; - } - std::thread::sleep(Duration::from_millis(5)); } - // No writer can outlive job destruction. Drain the bounded OS pipe buffers. - drop(job); - for _ in 0..8 { - drain( - &stdout, - &mut result.stdout, - &mut remaining, - &mut result.truncated, - )?; - drain( - &stderr, - &mut result.stderr, - &mut remaining, - &mut result.truncated, - )?; - } - let mut code = 0; - ensure!( - unsafe { GetExitCodeProcess(process_handle.as_raw_handle(), &mut code) } != 0 - && code != STILL_ACTIVE as u32, - "PROCESS_EXIT_UNCONFIRMED" - ); - result.exit_code = Some(code as i32); - Ok(result) + result } #[cfg(test)] diff --git a/crates/agent-windows/src/runtime.rs b/crates/agent-windows/src/runtime.rs index bf8e85a..7b358cb 100644 --- a/crates/agent-windows/src/runtime.rs +++ b/crates/agent-windows/src/runtime.rs @@ -7,7 +7,7 @@ use rmm_agent_core::{ jobs, ledger, reconcile::{self, SyncClock}, }; -use rmm_agent_windows::{CAPABILITIES, journal::Journal, native}; +use rmm_agent_windows::{CAPABILITIES, job_binding::readonly_binding, journal::Journal, native}; use rmm_domain::jobs::{Action, ExecutionPermitPayload}; use rmm_protocol::*; use rusqlite::{Connection, OptionalExtension}; @@ -216,104 +216,6 @@ fn verified_window( binding.verify(request, now + chrono::Duration::seconds(2))?; Ok(result) } -fn readonly_binding(path: &Path) -> Result { - use std::ptr; - use windows_sys::Win32::{ - Foundation::LocalFree, - Security::{ - self, ACCESS_ALLOWED_ACE, ACL, - Authorization::{ConvertSidToStringSidW, GetNamedSecurityInfoW, SE_FILE_OBJECT}, - DACL_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION, - }, - }; - unsafe fn sid(sid: Security::PSID) -> Result { - let mut raw = ptr::null_mut(); - ensure!( - unsafe { ConvertSidToStringSidW(sid, &mut raw) } != 0, - "BINDING_SID" - ); - let mut len = 0; - while unsafe { *raw.add(len) } != 0 { - len += 1; - ensure!(len < 256, "BINDING_SID_LIMIT"); - } - let value = String::from_utf16(unsafe { std::slice::from_raw_parts(raw, len) })?; - unsafe { - LocalFree(raw.cast()); - } - Ok(value) - } - ensure!(path.is_absolute(), "ABSOLUTE_JOB_BINDING_REQUIRED"); - // The immediate configuration directory must also deny agent replacement. - for (item, directory) in [ - (path, false), - (path.parent().context("BINDING_PARENT")?, true), - ] { - native::private_path(item, directory)?; - let mut owner = ptr::null_mut(); - let mut dacl: *mut ACL = ptr::null_mut(); - let mut descriptor = ptr::null_mut(); - let status = unsafe { - GetNamedSecurityInfoW( - native::wide(item).as_ptr(), - SE_FILE_OBJECT, - OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, - &mut owner, - ptr::null_mut(), - &mut dacl, - ptr::null_mut(), - &mut descriptor, - ) - }; - ensure!(status == 0, "BINDING_SECURITY"); - let check = (|| -> Result<()> { - ensure!(!owner.is_null() && !dacl.is_null(), "BINDING_NULL_SECURITY"); - ensure!( - matches!(unsafe { sid(owner) }?.as_str(), "S-1-5-18" | "S-1-5-32-544"), - "BINDING_ADMIN_OWNER_REQUIRED" - ); - for index in 0..unsafe { (*dacl).AceCount } { - let mut raw = ptr::null_mut(); - ensure!( - unsafe { Security::GetAce(dacl, u32::from(index), &mut raw) } != 0, - "BINDING_ACE" - ); - let ace = unsafe { &*raw.cast::() }; - ensure!( - ace.Header.AceType == 0 - && usize::from(ace.Header.AceSize) - >= std::mem::size_of::(), - "BINDING_ACE_TYPE" - ); - let who = unsafe { sid(ptr::addr_of!(ace.SidStart).cast_mut().cast()) }?; - if !matches!(who.as_str(), "S-1-5-18" | "S-1-5-32-544") { - // Generic write/all, owner/DACL/delete and all file/directory mutation bits. - ensure!(ace.Mask & 0x500d_0156 == 0, "BINDING_AGENT_WRITE_FORBIDDEN"); - } - } - Ok(()) - })(); - unsafe { - LocalFree(descriptor.cast()); - } - check?; - } - let binding: Binding = serde_json::from_slice(&protected_read(path, 8192)?)?; - ensure!( - !binding.tenant_id.is_nil() - && !binding.agent_id.is_nil() - && !binding.asset_id.is_nil() - && binding.enrollment_generation > 0 - && binding.assignment_version > 0 - && binding.public_key_hex.len() == 64 - && binding - .public_key_hex - .bytes() - .all(|b| b.is_ascii_hexdigit()), - "JOB_BINDING_PROFILE" - ); - Ok(binding) -} fn import_legacy_inventory(journal: &mut Journal) -> Result<()> { let original = { let mut q = journal @@ -465,6 +367,25 @@ struct Agent { impl Agent { fn close_gate(&mut self) { self.fence.close(); + #[cfg(feature = "system-helper")] + if let Some(running) = &self.running + && running.grant.execution_identity == "system" + { + running.cancel.store(true, Ordering::SeqCst); + let denied = (|| -> Result<()> { + let mut store = rmm_agent_windows::helper_runtime::intent::Store::open(true)?; + let intent = store.by_grant(running.grant.grant_id)?; + ensure!( + intent.permit()? == running.grant, + "SYSTEM_CANCEL_GRANT_BINDING" + ); + store.deny(&intent.request, Uuid::parse_str(&self.boot)?) + })(); + if denied.is_err() { + self.helper_refused = true; + eprintln!("SYSTEM_CANCEL_PERSISTENCE_FAILED"); + } + } if let Some(sync) = &mut self.sync { sync.close(); } @@ -664,7 +585,11 @@ impl Agent { } #[cfg(feature = "system-helper")] { - Path::new(rmm_agent_windows::helper_runtime::intent::DB).is_file() + (job.action.windows_msi().is_none() + || (self.config.state_directory + == Path::new(rmm_agent_windows::msi_cache::AGENT_STATE) + && rmm_agent_windows::msi_cache::stage_configured())) + && Path::new(rmm_agent_windows::helper_runtime::intent::DB).is_file() && self .helper .as_ref() @@ -684,6 +609,8 @@ impl Agent { { caps.extend(["rmm.sync.v1", "refresh_inventory"].map(str::to_string)); if rmm_agent_windows::actions::powershell().is_ok() { + // The process_remote_signed profile is implemented but remains + // unpublished until its separate native qualification passes. caps.extend(["run_script", "script.powershell/1"].map(str::to_string)); } } @@ -1079,9 +1006,69 @@ impl Agent { let completed = self.completed_tx.clone(); let work_offer = offer.clone(); let work_grant = grant.clone(); + let msi = job.action.windows_msi().cloned(); + let address = self.config.connect_address; + let ca = self.ca.clone(); + let state = self.config.state_directory.clone(); + let request_for_stage = request.clone(); + let binding_for_stage = binding.clone(); let worker = tokio::spawn(async move { + let staged = if let Some(m) = msi { + let result = async { + ensure!( + !worker_cancel.load(Ordering::SeqCst) + && fence.allows_identity(generation, true, &clock), + "MSI_DOWNLOAD_FENCED" + ); + verified_window(&binding_for_stage, &request_for_stage, &clock)?; + let cert = protected_read(&state.join("device.pem"), 32768)?; + let key = protected_read(&state.join("device.key"), 16384)?; + let tls = crate::https_channel::mtls_client(address, &ca, &cert, &key)?; + let response = tls + .get(format!( + "https://{}:{}/agent/v1/artifacts/{}?job_id={}&attempt_id={}", + crate::https_channel::server_name()?, + address.port(), + m.recipe.artifact.artifact_id, + work_grant.job_id, + work_grant.attempt_id + )) + .send() + .await?; + ensure!( + response + .headers() + .get("x-artifact-sha256") + .and_then(|v| v.to_str().ok()) + == Some(m.recipe.artifact.sha256.as_str()), + "MSI_DOWNLOAD_METADATA" + ); + let bytes = crate::https_channel::bounded_body( + response, + m.recipe.artifact.bytes as usize, + "application/octet-stream", + ) + .await?; + ensure!( + !worker_cancel.load(Ordering::SeqCst) + && fence.allows_identity(generation, true, &clock), + "MSI_DOWNLOAD_FENCED" + ); + verified_window(&binding_for_stage, &request_for_stage, &clock)?; + tokio::task::spawn_blocking(move || { + rmm_agent_windows::msi_cache::stage(&m, &bytes) + }) + .await??; + Ok::<_, anyhow::Error>(()) + } + .await; + result.is_ok() + } else { + true + }; let _result = helper .delegate(intent, move || { + ensure!(staged, "MSI_DOWNLOAD_NOT_READY"); ensure!( !worker_cancel.load(Ordering::SeqCst), "CANCELLED_BEFORE_DISPATCH" @@ -1162,6 +1149,7 @@ impl Agent { let directory = self.config.state_directory.join("jobs"); let work_offer = offer.clone(); let work_grant = grant.clone(); + let original_boot = Uuid::parse_str(&self.boot)?; let worker = tokio::task::spawn_blocking(move || { let outcome = (|| -> Result { if !fence.allows(generation, &clock) @@ -1172,17 +1160,24 @@ impl Agent { "START_WINDOW_OR_CLOCK_INVALID_BEFORE_EFFECT", )); } - let script = if matches!(job.action, Action::RunScript { .. }) { - match rmm_agent_windows::actions::Script::prepare( - &job, - &directory, - work_grant.grant_id, - ) { - Ok(script) => Some(script), - Err(_) => return Ok(jobs::no_start("failed", "SCRIPT_PREPARATION_FAILED")), + let prepared = match rmm_agent_windows::actions::PreparedAction::prepare( + &job, + &directory, + work_grant.grant_id, + &request, + original_boot, + ) { + Ok(prepared) => prepared, + Err(_) => { + return Ok(jobs::no_start( + "failed", + if matches!(job.action, Action::RunScript { .. }) { + "SCRIPT_PREPARATION_FAILED" + } else { + "ACTION_PREPARATION_FAILED" + }, + )); } - } else { - None }; // File creation, pool scheduling and fsync never extend a start window. if worker_cancel.load(Ordering::SeqCst) { @@ -1196,11 +1191,7 @@ impl Agent { "START_WINDOW_OR_CLOCK_INVALID_BEFORE_EFFECT", )); } - if let Some(script) = script { - script.run(&job, &worker_cancel) - } else { - rmm_agent_windows::actions::inventory(&job) - } + prepared.run(&job, &worker_cancel) })() .unwrap_or_else(|_| Outcome::unknown("WINDOWS_EXECUTOR_EFFECT_UNKNOWN")); let _ = completed.blocking_send((work_offer, work_grant, outcome)); @@ -1390,7 +1381,7 @@ impl Agent { #[cfg(feature="system-helper")] if Path::new(rmm_agent_windows::helper_runtime::intent::DB).is_file() { let mut store=rmm_agent_windows::helper_runtime::intent::Store::open(true)?; - if let Some(intent)=store.pending()?.first() && intent.permit()?.job_id.to_string()==cancel.job_id && intent.permit()?.attempt_id.to_string()==cancel.attempt_id {store.cancel_before_dispatch(intent.id)?;} + if let Some(intent)=store.pending()?.first() && intent.permit()?.job_id.to_string()==cancel.job_id && intent.permit()?.attempt_id.to_string()==cancel.attempt_id {store.deny(&intent.request,Uuid::parse_str(&self.boot)?)?;store.cancel_before_dispatch(intent.id)?;} } if state=="accepted" {self.record(generation,&JobOffer::decode(bytes.as_slice())?,&grant,jobs::no_start("cancelled","CANCELLED_BEFORE_START"))?;needs_sync=true;} } @@ -1547,6 +1538,10 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> { } pub fn main() -> Result<()> { let args: Vec = std::env::args().skip(1).collect(); + if args.first().map(String::as_str) == Some(rmm_agent_windows::event_log_job::MODE) { + ensure!(args.len() == 2, "EVENT_LOG_WORKER_ARGUMENTS"); + return rmm_agent_windows::event_log_job::worker(Path::new(&args[1])); + } if args == ["--help"] { println!( "Development Windows service. --service --config ABSOLUTE_PATH. Jobs disabled by default; require qualified clock, provisioned signing binding, signed Sync and WebUI-authorized grant. No approval bypass." diff --git a/crates/agent-windows/src/system_script.rs b/crates/agent-windows/src/system_script.rs new file mode 100644 index 0000000..7c05cb7 --- /dev/null +++ b/crates/agent-windows/src/system_script.rs @@ -0,0 +1,133 @@ +//! Exact approved SYSTEM script bytes, created only after helper consumption. +use crate::helper_ipc::PinnedFile; +use anyhow::{Result, ensure}; +use rmm_agent_core::auth::Outcome; +use rmm_domain::jobs::{Action, JobPayload, WindowsExecutionProfile, hash}; +use std::{ + fs::OpenOptions, + io::Write, + os::windows::fs::OpenOptionsExt, + path::Path, + sync::atomic::AtomicBool, + time::{Duration, Instant}, +}; + +pub(crate) fn execute( + job: &JobPayload, + grant: uuid::Uuid, + helper_sid: &str, + began: Instant, + mut observe: impl FnMut(bool) -> Result<()>, +) -> Result { + ensure!( + job.execution_identity == "system" + && job.action.full_maintenance() + && job.action.valid_execution_identity("system") + && job.action.validate() + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes), + "SYSTEM_SCRIPT_PROFILE_REQUIRED" + ); + let Action::RunScript { + script_utf8, + expected_stdout, + windows_execution_profile, + .. + } = &job.action + else { + anyhow::bail!("SYSTEM_SCRIPT_REQUIRED") + }; + let directory = Path::new(crate::helper_system::STATE_ROOT); + let writers = [helper_sid.to_owned()]; + let mut root = PinnedFile::open(directory, true, &[], &writers, false)?; + let path = directory.join(format!("script-{}-{}.ps1", job.attempt_id, grant)); + let mut file = OpenOptions::new() + .create_new(true) + .read(true) + .write(true) + .share_mode(windows_sys::Win32::Storage::FileSystem::FILE_SHARE_READ) + .open(&path)?; + file.write_all(script_utf8.as_bytes())?; + file.sync_all()?; + drop(file); + struct Cleanup(std::path::PathBuf); + impl Drop for Cleanup { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } + } + let _cleanup = Cleanup(path.clone()); + let mut pinned = PinnedFile::open(&path, false, &[], &writers, false)?; + ensure!( + std::fs::read(&path)? == script_utf8.as_bytes(), + "SYSTEM_SCRIPT_BYTES_CHANGED" + ); + let executable = crate::actions::powershell()?; + let arguments = crate::actions::script_arguments(job, &path)?; + let timeout = Duration::from_secs(job.max_runtime_seconds.into()); + let remaining = timeout + .checked_sub(began.elapsed()) + .filter(|d| !d.is_zero()) + .ok_or_else(|| anyhow::anyhow!("SYSTEM_SCRIPT_PREPARATION_TIMEOUT"))?; + let mut first = true; + let output = crate::process::run_observed( + &executable, + &arguments, + directory, + remaining, + job.max_output_bytes as usize, + &AtomicBool::new(false), + || { + root.check()?; + pinned.check()?; + observe(first)?; + first = false; + Ok(()) + }, + )?; + let expected = expected_stdout + .as_ref() + .is_none_or(|s| output.stdout == s.as_bytes()); + let interrupted = output.interrupted || began.elapsed() >= timeout || observe(false).is_err(); + let success = !interrupted && output.exit_code == Some(0) && expected && !output.truncated; + let mut evidence = serde_json::json!({"postconditions_passed":success,"interpreter":"powershell", + "script_sha256":hash(script_utf8.as_bytes()),"privileged_script_profile":"full_maintenance_v1", + "execution_sid":"S-1-5-18","output_encoding":"raw_native_bytes","comparison":"exact_utf8_bytes", + "execution_policy":"inherited","own_job_object_tree_bounded":true,"full_host_effects_not_contained":true,"full_host_authority":true, + "cancel_or_fence_observed":output.cancelled,"termination_does_not_prove_absence_of_effects":interrupted}); + if windows_execution_profile == &Some(WindowsExecutionProfile::PowerShell51RemoteSigned) { + evidence["windows_execution_profile"] = "powershell_51_remote_signed".into(); + evidence["execution_policy"] = "process_remote_signed_requested".into(); + evidence["execution_policy_requested"] = "RemoteSigned".into(); + evidence["execution_policy_scope"] = "process".into(); + } + Ok(Outcome { + state: if interrupted { + "unknown" + } else if success { + "succeeded" + } else { + "failed" + } + .into(), + stdout: output.stdout, + stderr: output.stderr, + exit_code: output.exit_code, + output_truncated: output.truncated, + reboot_required: false, + error_code: if success { + "" + } else if interrupted { + "SYSTEM_SCRIPT_INTERRUPTED_EFFECT_UNKNOWN" + } else if output.truncated { + "OUTPUT_LIMIT_REACHED" + } else if !expected { + "STDOUT_POSTCONDITION_FAILED" + } else { + "POWERSHELL_PROCESS_FAILED" + } + .into(), + evidence, + }) +} diff --git a/crates/agent/src/apt.rs b/crates/agent/src/apt.rs index 82c5a6c..53e4711 100644 --- a/crates/agent/src/apt.rs +++ b/crates/agent/src/apt.rs @@ -13,6 +13,12 @@ pub fn socket_for( general: Option, apt: Option, ) -> Result { + if job.action.full_maintenance() { + return Ok(PathBuf::from( + std::env::var_os("RMM_FULL_MAINTENANCE_HELPER_SOCKET") + .context("FULL_MAINTENANCE_SOCKET_NOT_CONFIGURED")?, + )); + } select_socket(provider::is_apt_action(job), general, apt) } fn select_socket( @@ -216,6 +222,23 @@ pub fn invoke(binding: &Binding, job: &JobPayload, request: &Request) -> Result< rmm_privileged_helper::invoke(&socket, request, job.max_runtime_seconds) } +pub fn invoke_with_control( + binding: &Binding, + job: &JobPayload, + request: &Request, + observe: impl FnMut() -> Result<()>, +) -> Result { + if !job.action.full_maintenance() { + return invoke(binding, job, request); + } + ensure!( + crate::helper_sync::general_supported(binding, job), + "FULL_MAINTENANCE_POLICY_REQUIRED" + ); + let socket = socket_for(job, None, None)?; + rmm_privileged_helper::invoke_controlled(&socket, request, job.max_runtime_seconds, observe) +} + #[cfg(test)] mod tests { use super::*; @@ -353,6 +376,8 @@ mod tests { script_utf8: "true".into(), arguments: vec![], expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, }, execution_identity: "root".into(), not_before: now, diff --git a/crates/agent/src/helper_sync.rs b/crates/agent/src/helper_sync.rs index f866072..42068de 100644 --- a/crates/agent/src/helper_sync.rs +++ b/crates/agent/src/helper_sync.rs @@ -9,6 +9,7 @@ struct Endpoint { socket: PathBuf, profile: String, hash: String, + binary_hash: Option, } #[derive(Clone)] pub struct Helpers { @@ -39,24 +40,44 @@ fn map(s: &root::Snapshot) -> reconcile::HelperSnapshot { .collect(), } } -fn general(binding: &Binding) -> Result { - let socket = std::env::var_os("RMM_HELPER_SOCKET").context("NO_GENERAL_HELPER")?; - let path = std::env::var_os("RMM_HELPER_POLICY_FILE").context("GENERAL_POLICY_PIN_REQUIRED")?; +fn general(binding: &Binding, full: bool) -> Result { + let (socket_name, pin_name, profile) = if full { + ( + "RMM_FULL_MAINTENANCE_HELPER_SOCKET", + "RMM_FULL_MAINTENANCE_POLICY_FILE", + "full_maintenance_v1", + ) + } else { + ("RMM_HELPER_SOCKET", "RMM_HELPER_POLICY_FILE", "general") + }; + let socket = std::env::var_os(socket_name).context("NO_GENERAL_HELPER")?; + let path = std::env::var_os(pin_name).context("GENERAL_POLICY_PIN_REQUIRED")?; let pin = control::GeneralPin::read(Path::new(&path), binding)?; + ensure!(pin.profile == profile, "HELPER_PROFILE_ROUTE_MISMATCH"); control::general_probe(Path::new(&socket), binding, &pin) } impl Helpers { pub fn configured(binding: &Binding) -> Result { let mut endpoints = vec![]; - if let Some(socket) = std::env::var_os("RMM_HELPER_SOCKET") { - let path = std::env::var_os("RMM_HELPER_POLICY_FILE") - .context("GENERAL_POLICY_PIN_REQUIRED")?; - let pin = control::GeneralPin::read(Path::new(&path), binding)?; - endpoints.push(Endpoint { - socket: socket.into(), - profile: "general".into(), - hash: pin.policy_sha256, - }); + for (socket_name, pin_name, profile) in [ + ("RMM_HELPER_SOCKET", "RMM_HELPER_POLICY_FILE", "general"), + ( + "RMM_FULL_MAINTENANCE_HELPER_SOCKET", + "RMM_FULL_MAINTENANCE_POLICY_FILE", + "full_maintenance_v1", + ), + ] { + if let Some(socket) = std::env::var_os(socket_name) { + let path = std::env::var_os(pin_name).context("GENERAL_POLICY_PIN_REQUIRED")?; + let pin = control::GeneralPin::read(Path::new(&path), binding)?; + ensure!(pin.profile == profile, "HELPER_PROFILE_ROUTE_MISMATCH"); + endpoints.push(Endpoint { + socket: socket.into(), + profile: profile.into(), + hash: pin.policy_sha256, + binary_hash: pin.helper_binary_sha256, + }); + } } if let Some(socket) = std::env::var_os("RMM_APT_HELPER_SOCKET") { ensure!( @@ -70,6 +91,7 @@ impl Helpers { socket: socket.into(), profile: "apt_only".into(), hash: pin.provider_stack_sha256, + binary_hash: None, }); } Ok(Self { @@ -79,7 +101,9 @@ impl Helpers { } fn validate(&self, e: &Endpoint, r: &control::Response) -> Result<()> { ensure!( - r.profile == e.profile && r.policy_sha256 == e.hash, + r.profile == e.profile + && r.policy_sha256 == e.hash + && r.helper_binary_sha256 == e.binary_hash, "HELPER_PIN_PROFILE_MISMATCH" ); Ok(()) @@ -153,13 +177,39 @@ impl Helpers { } } pub fn general_capabilities(binding: &Binding) -> Vec { + let mut caps = profile_capabilities(binding, false); + caps.extend(profile_capabilities(binding, true)); + caps.sort(); + caps.dedup(); + caps +} +fn profile_capabilities(binding: &Binding, full: bool) -> Vec { (|| -> Result> { - let response = general(binding)?; + let response = general(binding, full)?; let policy = response.policy.context("NO_GENERAL_POLICY")?; let mut caps = vec![ "helper.root.v1".into(), format!("helper.policy.sha256:{}", response.policy_sha256), ]; + if full { + ensure!( + policy.schema_version == 2 + && policy.profile == "full_maintenance_v1" + && policy.allowed_script_profiles.as_deref() + == Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]), + "FULL_MAINTENANCE_POLICY_REQUIRED" + ); + let binary = policy + .helper_binary_sha256 + .as_ref() + .context("FULL_MAINTENANCE_BINARY_PIN_REQUIRED")?; + ensure!( + response.helper_binary_sha256.as_ref() == Some(binary), + "FULL_MAINTENANCE_BINARY_PROOF_REQUIRED" + ); + caps.push(format!("helper.binary.sha256:{binary}")); + caps.push(rmm_domain::jobs::FULL_MAINTENANCE_CAPABILITY.into()); + } let mut actions = vec![]; if policy.allow_root_scripts && rmm_agent_linux::actions::Interpreter::PosixSh.available() { actions.push("run_script"); @@ -180,10 +230,22 @@ pub fn general_capabilities(binding: &Binding) -> Vec { } pub fn general_supported(binding: &Binding, job: &rmm_domain::jobs::JobPayload) -> bool { (|| -> Result { - let p = general(binding)?.policy.context("NO_GENERAL_POLICY")?; + let p = general(binding, job.action.full_maintenance())? + .policy + .context("NO_GENERAL_POLICY")?; Ok(match &job.action { rmm_domain::jobs::Action::RunScript { interpreter, .. } => { p.allow_root_scripts + && job.action.valid_execution_identity(&job.execution_identity) + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes) + && (!job.action.full_maintenance() + || p.allowed_script_profiles.as_ref().is_some_and(|v| { + v.contains( + &rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ) + })) && match interpreter.as_str() { "posix_sh" => rmm_agent_linux::actions::Interpreter::PosixSh.available(), "bash" => rmm_agent_linux::actions::Interpreter::Bash.available(), diff --git a/crates/agent/src/main.rs b/crates/agent/src/main.rs index b8c8963..7c64279 100644 --- a/crates/agent/src/main.rs +++ b/crates/agent/src/main.rs @@ -50,6 +50,7 @@ struct Agent { inventory_tx: tokio::sync::mpsc::Sender, i64)>>, inventory_rx: tokio::sync::mpsc::Receiver, i64)>>, inventory_inflight: bool, + running_cancels: std::collections::HashMap<(String, String), Arc>, sync: Option, sync_clock: C, helpers: Option, @@ -61,6 +62,7 @@ struct Agent { } #[derive(Default)] struct EffectFence { + running_generation: AtomicU64, generation: AtomicU64, deadline: AtomicI64, root: AtomicBool, @@ -80,6 +82,14 @@ fn report_closed_gate(error: &anyhow::Error) { } impl EffectFence { fn close(&self) { + self.running_generation.store(0, Ordering::SeqCst); + self.close_starts(); + } + // A signed Sync/start lease authorizes NEW starts. Its natural expiry + // cannot retroactively stop an approved bounded process (spec22.3). + // Running effects instead observe their exact cancellation and current + // connection generation; transport/signed refusal closes both fences. + fn close_starts(&self) { self.generation.store(0, Ordering::SeqCst); self.root.store(false, Ordering::SeqCst); } @@ -117,7 +127,7 @@ impl Agent { }) } async fn start_sync(&mut self, wire: &wire_channel::Handle, generation: u64) -> Result<()> { - self.fence.close(); + self.fence.close_starts(); let helpers = self.helpers.clone(); let snapshot = tokio::task::spawn_blocking(move || match helpers { Some(h) => h.snapshot(generation), @@ -182,6 +192,9 @@ impl Agent { .as_ref() .and_then(|s| s.validity_deadline()) .context("SIGNED_LEASE_REQUIRED")?; + self.fence + .running_generation + .store(generation, Ordering::SeqCst); self.fence .deadline .store(deadline.timestamp_millis(), Ordering::SeqCst); @@ -189,6 +202,9 @@ impl Agent { .root .store(self.ready(generation, true), Ordering::SeqCst); self.fence.generation.store(generation, Ordering::SeqCst); + } else { + // A valid signed refusal is a fence too, not just parse/I/O errors. + self.fence.close(); } Ok(()) } @@ -322,6 +338,11 @@ impl Agent { })); jobs::event(&tx, &envelope)?; tx.commit()?; + let cancellation = Arc::new(AtomicBool::new(false)); + self.running_cancels.insert( + (offer.job_id.clone(), offer.attempt_id.clone()), + cancellation.clone(), + ); let completed = self.completed_tx.clone(); let clock = self.clock.clone(); let fence = self.fence.clone(); @@ -334,7 +355,16 @@ impl Agent { { jobs::no_start("failed", "START_WINDOW_OR_CLOCK_INVALID_BEFORE_EFFECT") } else if job.execution_identity == "root" { - apt::invoke(&binding, &job, &request).unwrap_or_else(|_| { + apt::invoke_with_control(&binding, &job, &request, || { + ensure!( + !cancellation.load(Ordering::SeqCst) + && fence.running_generation.load(Ordering::SeqCst) == generation + && clock.trusted(), + "SCRIPT_RUNNING_CANCEL_OR_FENCE" + ); + Ok(()) + }) + .unwrap_or_else(|_| { rmm_privileged_helper::Outcome::unknown("HELPER_TRANSPORT_EFFECT_UNKNOWN") }) } else { @@ -537,6 +567,7 @@ impl Agent { rusqlite::params![cancel.job_id,cancel.attempt_id], |r| Ok((r.get(0)?,r.get(1)?,r.get(2)?))).optional()? }; if let Some((bytes,state,grant)) = row { + if let Some(flag) = self.running_cancels.get(&(cancel.job_id.clone(),cancel.attempt_id.clone())) { flag.store(true,Ordering::SeqCst); } self.db.execute("UPDATE job_attempts SET cancel_requested=1 WHERE job=?1 AND attempt=?2", rusqlite::params![cancel.job_id,cancel.attempt_id])?; if state == "accepted" { self.record(generation, &JobOffer::decode(bytes.as_slice())?, &grant, jobs::no_start("cancelled", "CANCELLED_BEFORE_START"))?; } // Running script cancellation is not confirmed by an @@ -549,6 +580,7 @@ impl Agent { } } Some((offer, grant, outcome)) = self.completed_rx.recv() => { + self.running_cancels.remove(&(offer.job_id.clone(),offer.attempt_id.clone())); // First preserve the original completion without unlocking. // A startup can replay it if the journal transaction is lost. let state = outcome.state.clone(); @@ -807,6 +839,7 @@ async fn main() -> Result<()> { inventory_tx, inventory_rx, inventory_inflight: false, + running_cancels: std::collections::HashMap::new(), sync, sync_clock: reconcile::NativeClock::default(), helpers, diff --git a/crates/agent/src/runtime_tests.rs b/crates/agent/src/runtime_tests.rs index 6287f43..84dea28 100644 --- a/crates/agent/src/runtime_tests.rs +++ b/crates/agent/src/runtime_tests.rs @@ -50,6 +50,8 @@ fn new_process_persists_interruption_before_helper_io_without_unlock_or_repeat() script_utf8: "printf 'fixture only; never executed\\n'".into(), arguments: vec![], expected_stdout: Some("fixture only; never executed\n".into()), + windows_execution_profile: None, + privileged_script_profile: None, }, execution_identity: "root".into(), not_before: now, @@ -138,6 +140,7 @@ fn new_process_persists_interruption_before_helper_io_without_unlock_or_repeat() inventory_tx: itx, inventory_rx: irx, inventory_inflight: false, + running_cancels: std::collections::HashMap::new(), sync: Some(reconcile::ClientSync::new(binding.clone())), sync_clock: Clock(true), helpers: None, @@ -524,6 +527,7 @@ async fn run(mode: wire_channel::Mode, trusted: bool, forged: bool, rounds: u64) inventory_tx: itx, inventory_rx: irx, inventory_inflight: false, + running_cancels: std::collections::HashMap::new(), sync: Some(reconcile::ClientSync::new(binding)), sync_clock: Clock(trusted), helpers: None, diff --git a/crates/agent/tests/reconcile.rs b/crates/agent/tests/reconcile.rs index b9c59c0..3882a54 100644 --- a/crates/agent/tests/reconcile.rs +++ b/crates/agent/tests/reconcile.rs @@ -594,6 +594,8 @@ fn script() -> Action { script_utf8: "printf hello".into(), arguments: vec![], expected_stdout: Some("hello".into()), + windows_execution_profile: None, + privileged_script_profile: None, } } fn success() -> ReconciledOutcome { diff --git a/crates/domain/examples/script_console_golden.rs b/crates/domain/examples/script_console_golden.rs new file mode 100644 index 0000000..3d9f77e --- /dev/null +++ b/crates/domain/examples/script_console_golden.rs @@ -0,0 +1,48 @@ +//! Reproducible Rust-codec fixtures consumed directly by the WebUI tests. +use rmm_domain::jobs::*; +use serde_json::json; +use uuid::Uuid; +fn main() { + let cases = [ + ( + "legacy_linux_agent", + "agent_service", + "posix_sh", + "printf 'hello\\n'\n", + None, + None, + ), + ( + "linux_root_full", + "root", + "posix_sh", + "id\nprintf 'root stdout\\n'\nprintf 'root stderr\\n' >&2\n", + None, + Some(PrivilegedScriptProfile::FullMaintenanceV1), + ), + ( + "windows_system_full", + "system", + "powershell", + "Write-Output 'SYSTEM stdout'\r\n[Console]::Error.WriteLine('SYSTEM stderr')\r\n", + Some(WindowsExecutionProfile::PowerShell51RemoteSigned), + Some(PrivilegedScriptProfile::FullMaintenanceV1), + ), + ]; + let values: Vec<_> = cases.into_iter().enumerate().map(|(index, (name, identity, interpreter, script, windows_execution_profile, privileged_script_profile))| { + let tenant = Uuid::from_u128(1); + let job = JobPayload { + schema_version: 1, tenant_id: tenant, asset_id: Uuid::from_u128(2), agent_id: Uuid::from_u128(3), + enrollment_generation: 1, assignment_version: 1, job_id: Uuid::from_u128(4+index as u128*10), attempt_id: Uuid::from_u128(5+index as u128*10), + action: Action::RunScript { interpreter: interpreter.into(), script_utf8: script.into(), arguments: vec![], expected_stdout: None, windows_execution_profile, privileged_script_profile }, + execution_identity: identity.into(), not_before: "2026-11-14T12:00:00Z".parse().unwrap(), start_before: "2026-11-14T13:00:00Z".parse().unwrap(), max_runtime_seconds: 30, max_output_bytes: 4096, + }; + let job_bytes = encode(&job).unwrap(); + let job_hash = hash(&job_bytes); + let script_sha256 = hash(script.as_bytes()); + let plan = PlanPayload { schema_version: 1, plan_id: Uuid::from_u128(6+index as u128*10), tenant_id: tenant, principal: "simulated-dev-script-console".into(), reason: format!("Codec fixture: {name}; no native execution"), jobs: vec![PlannedJob { payload: job, job_payload_hash: job_hash.clone(), script_sha256: Some(script_sha256.clone()) }], max_parallel: 1, halt_after_failures: 1, recipe_references: vec![], artifact_references: vec![] }; + let bytes = encode(&plan).unwrap(); + json!({"name":name,"plan_payload_bytes":bytes,"plan_hash":hash(&bytes),"job_payload_bytes":job_bytes,"job_payload_hash":job_hash,"script_sha256":script_sha256}) + }).collect(); + println!("{}", serde_json::to_string_pretty(&values).unwrap()); +} diff --git a/crates/domain/src/jobs.rs b/crates/domain/src/jobs.rs index 4d6b217..e1a44aa 100644 --- a/crates/domain/src/jobs.rs +++ b/crates/domain/src/jobs.rs @@ -1,5 +1,6 @@ //! Version 1 codec: UTF-8 compact serde JSON of these structs, declaration field -//! order, explicit null options, ordered vectors, UTC RFC3339 timestamps. Hash the +//! order, explicit null legacy options (new opt-in fields omit None), ordered vectors, +//! UTC RFC3339 timestamps. Hash the //! stored bytes, never a reconstructed API view. No floating point/map payloads. use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; @@ -13,6 +14,106 @@ pub fn encode(value: &T) -> Result, serde_json::Error> { serde_json::to_vec(value) } +/// One explicitly qualified Event Log source, not an arbitrary channel/XPath. +pub const WINDOWS_EVENT_LOG_FIXTURE_SOURCE: &str = + "windows.eventlog.application.rustdiscovery-mvp-test"; +pub const WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY: &str = "logs.windows.eventlog.fixture/1"; +pub const WINDOWS_EVENT_LOG_FIXTURE_CHANNEL: &str = "Application"; +pub const WINDOWS_EVENT_LOG_FIXTURE_PROVIDER: &str = "RustDiscovery-MVP-Test"; + +/// Fixed per-job Windows PowerShell 5.1 option, never a machine/GPO policy. +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)] +pub enum WindowsExecutionProfile { + #[serde(rename = "powershell_51_remote_signed")] + PowerShell51RemoteSigned, +} + +/// A missing field uses serde's default None. A present field must be the exact +/// string: null and serde's externally-tagged enum object form are not profiles. +pub fn deserialize_windows_execution_profile<'de, D>( + deserializer: D, +) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + let value = String::deserialize(deserializer)?; + match value.as_str() { + "powershell_51_remote_signed" => { + Ok(Some(WindowsExecutionProfile::PowerShell51RemoteSigned)) + } + _ => Err(serde::de::Error::unknown_variant( + &value, + &["powershell_51_remote_signed"], + )), + } +} + +/// Explicit authority for a separately qualified, fully privileged script. +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)] +pub enum PrivilegedScriptProfile { + #[serde(rename = "full_maintenance_v1")] + FullMaintenanceV1, +} +pub const FULL_MAINTENANCE_CAPABILITY: &str = "script.full_maintenance/1"; +pub fn deserialize_privileged_script_profile<'de, D>( + d: D, +) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + let value = String::deserialize(d)?; + match value.as_str() { + "full_maintenance_v1" => Ok(Some(PrivilegedScriptProfile::FullMaintenanceV1)), + _ => Err(serde::de::Error::unknown_variant( + &value, + &["full_maintenance_v1"], + )), + } +} +/// Optional metadata pins preserve old codecs when missing, reject null or +/// noncanonical digests when explicitly supplied. +pub fn deserialize_optional_sha256<'de, D>(d: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + let value = String::deserialize(d)?; + if value.len() != 64 + || !value + .bytes() + .all(|b| b.is_ascii_digit() || b"abcdef".contains(&b)) + { + return Err(serde::de::Error::custom("lowercase SHA256 required")); + } + Ok(Some(value)) +} + +/// Policy lists are strict when present; omission is deny, never a wildcard. +pub fn deserialize_script_profiles<'de, D>( + d: D, +) -> Result>, D::Error> +where + D: serde::Deserializer<'de>, +{ + let values = Vec::::deserialize(d)?; + if values.len() != 1 || values[0] != "full_maintenance_v1" { + return Err(serde::de::Error::custom( + "exact full_maintenance_v1 profile required", + )); + } + Ok(Some(vec![PrivilegedScriptProfile::FullMaintenanceV1])) +} + +/// Missing preserves Linux codecs; explicit null/nonobject is never a manifest. +pub fn deserialize_windows_msi_manifest<'de, D>( + deserializer: D, +) -> Result>, D::Error> +where + D: serde::Deserializer<'de>, +{ + crate::windows_execution::ResolvedWindowsMsi::deserialize(deserializer) + .map(|m| Some(Box::new(m))) +} + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)] #[serde(tag = "action", rename_all = "snake_case", deny_unknown_fields)] pub enum Action { @@ -21,6 +122,20 @@ pub enum Action { script_utf8: String, arguments: Vec, expected_stdout: Option, + #[serde( + default, + deserialize_with = "deserialize_windows_execution_profile", + skip_serializing_if = "Option::is_none" + )] + #[schemars(with = "WindowsExecutionProfile")] + windows_execution_profile: Option, + #[serde( + default, + deserialize_with = "deserialize_privileged_script_profile", + skip_serializing_if = "Option::is_none" + )] + #[schemars(with = "PrivilegedScriptProfile")] + privileged_script_profile: Option, }, RefreshInventory { scope: String, @@ -46,6 +161,13 @@ pub enum Action { recipe_id: Option, #[serde(default, skip_serializing_if = "Option::is_none")] apt_manifest: Option, + #[serde( + default, + deserialize_with = "deserialize_windows_msi_manifest", + skip_serializing_if = "Option::is_none" + )] + #[schemars(with = "crate::windows_execution::ResolvedWindowsMsi")] + windows_msi_manifest: Option>, }, ApplyUpdates { provider: String, @@ -99,8 +221,11 @@ impl Action { script_utf8, arguments, expected_stdout, + windows_execution_profile, + .. } => { matches!(interpreter.as_str(), "posix_sh" | "bash" | "powershell") + && (windows_execution_profile.is_none() || interpreter == "powershell") && (interpreter != "powershell" || script_utf8.is_ascii() || script_utf8.starts_with('\u{feff}')) @@ -124,7 +249,8 @@ impl Action { } => { unit(u) && since < until - && (*until - *since).num_hours() <= 24 + && *until - *since <= chrono::Duration::hours(24) + && (u != WINDOWS_EVENT_LOG_FIXTURE_SOURCE || since.timestamp() >= 0) && *max_lines > 0 && *max_lines <= 1000 } @@ -140,6 +266,28 @@ impl Action { installation_context, recipe_id, apt_manifest, + windows_msi_manifest: Some(m), + } => { + m.validate() + && provider == "msi" + && package == crate::windows_execution::MSI_PACKAGE_ID + && version == &m.recipe.postcondition.product_version + && architecture == "x64" + && installation_context == "machine" + && recipe_version == &m.recipe.recipe_version + && recipe_id.as_ref() == Some(&m.recipe.recipe_id) + && apt_manifest.is_none() + } + Self::EnsurePackage { + provider, + package, + version, + architecture, + recipe_version, + installation_context, + recipe_id, + apt_manifest, + windows_msi_manifest: None, } => { provider == "apt" && unit(package) @@ -198,6 +346,63 @@ impl Action { } } } + pub fn windows_msi(&self) -> Option<&crate::windows_execution::ResolvedWindowsMsi> { + match self { + Self::EnsurePackage { + windows_msi_manifest: Some(m), + .. + } => Some(m.as_ref()), + _ => None, + } + } + pub fn valid_helper_denial_shape(&self, identity: &str) -> bool { + (matches!(self,Self::SetServiceState{state,..} if matches!(state.as_str(),"running"|"stopped")) + && self.validate()) + || (identity == "system" && self.windows_msi().is_some() && self.validate()) + || (identity == "system" + && self.full_maintenance() + && self.script_platform() == Some("windows") + && self.validate()) + } + pub fn full_maintenance(&self) -> bool { + matches!( + self, + Self::RunScript { + privileged_script_profile: Some(PrivilegedScriptProfile::FullMaintenanceV1), + .. + } + ) + } + pub fn valid_execution_identity(&self, identity: &str) -> bool { + !self.full_maintenance() + || matches!( + (identity, self.script_platform()), + ("root", Some("linux")) | ("system", Some("windows")) + ) + } + pub fn is_windows_event_log_fixture(&self) -> bool { + matches!(self, Self::QueryLogs { unit, .. } if unit == WINDOWS_EVENT_LOG_FIXTURE_SOURCE) + } + /// The outer process budget includes authenticated worker startup/cleanup; + /// its inner Event Log calls retain their own cooperative <=5s deadline. + pub fn valid_job_limits(&self, runtime_seconds: u32, output_bytes: u32) -> bool { + (1..=3600).contains(&runtime_seconds) + && (!self.full_maintenance() + || (output_bytes <= 65536 + && runtime_seconds + <= if self.script_platform() == Some("windows") { + 120 + } else { + 300 + })) + && (self.windows_msi().is_none() + || ((10..=120).contains(&runtime_seconds) + && (1024..=65536).contains(&output_bytes))) + && (1..=262144).contains(&output_bytes) + && (!self.is_windows_event_log_fixture() + || ((10..=120).contains(&runtime_seconds) + && (1024..=65536).contains(&output_bytes))) + } /// Interpreter identity is part of the immutable action, never inferred at execution. pub fn script_platform(&self) -> Option<&'static str> { match self { @@ -211,23 +416,45 @@ impl Action { } /// Shared plan/revalidation/start gate. Passive Windows never implies job support. pub fn available_for(&self, identity: &str, capabilities: &[String]) -> bool { - if identity == "system" && !matches!(self, Self::SetServiceState { .. }) { + if !self.valid_execution_identity(identity) + || (identity == "system" && !self.valid_helper_denial_shape(identity)) + { return false; } let has = |cap: &str| capabilities.iter().any(|c| c == cap); - if !has(self.name()) { + if !has(self.name()) + || (self.full_maintenance() + && !(has(FULL_MAINTENANCE_CAPABILITY) + && has("rmm.sync.v1") + && has(&format!("{}:run_script", identity)) + && has(if identity == "root" { + "helper.root.v1" + } else { + "helper.system.v1" + }))) + { return false; } match self { - Self::RunScript { interpreter, .. } if interpreter == "powershell" => { - identity == "agent_service" + Self::RunScript { + interpreter, + windows_execution_profile, + .. + } if interpreter == "powershell" => { + (identity == "agent_service" || (identity == "system" && self.full_maintenance())) && has("windows.base/1") && !has("linux.base/1") && has("rmm.sync.v1") && has("script.powershell/1") + && (windows_execution_profile.is_none() + || has("script.powershell.process_remote_signed/1")) } - Self::RunScript { .. } => { - self.script_platform() == Some("linux") + Self::RunScript { + windows_execution_profile, + .. + } => { + windows_execution_profile.is_none() + && self.script_platform() == Some("linux") && has("linux.base/1") && !has("windows.base/1") } @@ -237,6 +464,27 @@ impl Action { && !(has("linux.base/1") && has("windows.base/1")) && (scope != "windows.base" || has("rmm.sync.v1")) } + Self::EnsurePackage { .. } if self.windows_msi().is_some() || has("windows.base/1") => { + self.windows_msi().is_some() + && self.validate() + && identity == "system" + && has("windows.base/1") + && !has("linux.base/1") + && has("rmm.sync.v1") + && has("helper.system.v1") + && has("system:ensure_package") + && has(crate::windows_execution::MSI_CAPABILITY) + } + Self::QueryLogs { unit, .. } + if unit == WINDOWS_EVENT_LOG_FIXTURE_SOURCE || has("windows.base/1") => + { + identity == "agent_service" + && unit == WINDOWS_EVENT_LOG_FIXTURE_SOURCE + && has("windows.base/1") + && !has("linux.base/1") + && has("rmm.sync.v1") + && has(WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY) + } Self::SetServiceState { state, .. } if identity == "system" || has("windows.base/1") => { @@ -272,6 +520,7 @@ impl Action { installation_context, recipe_id: None, apt_manifest: None, + windows_msi_manifest: None, } => { provider == "apt" && !package.is_empty() @@ -496,6 +745,8 @@ mod tests { script_utf8: "Write-Output 'hi'\r\n".into(), arguments: vec![], expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, }; let mut all = caps; all.extend(["run_script".into(), "script.powershell/1".into()]); @@ -505,6 +756,8 @@ mod tests { script_utf8: "printf hi\n".into(), arguments: vec![], expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, }; let reboot = Action::Reboot { reason: "fixed test".into(), @@ -526,6 +779,8 @@ mod tests { script_utf8: "Write-Output 'Hello'\r\n".into(), arguments: vec![], expected_stdout: Some("Hello\r\n".into()), + windows_execution_profile: None, + privileged_script_profile: None, }; let caps: Vec = [ "windows.base/1", @@ -595,12 +850,144 @@ mod tests { )); } #[test] + fn full_maintenance_is_explicit_strict_identity_and_capability_bound() { + let legacy = br#"{"action":"run_script","interpreter":"posix_sh","script_utf8":"id\n","arguments":[],"expected_stdout":null}"#; + let mut action: Action = serde_json::from_slice(legacy).unwrap(); + assert_eq!(encode(&action).unwrap(), legacy); + for value in [ + serde_json::Value::Null, + serde_json::json!({"FullMaintenanceV1":null}), + serde_json::json!("latest"), + serde_json::json!(true), + ] { + let mut input: serde_json::Value = serde_json::from_slice(legacy).unwrap(); + input["privileged_script_profile"] = value; + assert!(serde_json::from_value::(input).is_err()); + } + if let Action::RunScript { + privileged_script_profile, + .. + } = &mut action + { + *privileged_script_profile = Some(PrivilegedScriptProfile::FullMaintenanceV1); + } + assert!(action.validate()); + assert!(action.valid_execution_identity("root")); + assert!(!action.valid_execution_identity("agent_service")); + assert!(!action.valid_execution_identity("system")); + assert!(action.valid_job_limits(300, 65536)); + assert!(!action.valid_job_limits(301, 65536)); + assert!(!action.valid_job_limits(300, 65537)); + let caps: Vec = [ + "linux.base/1", + "rmm.sync.v1", + "helper.root.v1", + "run_script", + "root:run_script", + FULL_MAINTENANCE_CAPABILITY, + ] + .into_iter() + .map(str::to_owned) + .collect(); + assert!(action.available_for("root", &caps)); + for missing in &caps { + assert!( + !action.available_for( + "root", + &caps + .iter() + .filter(|c| *c != missing) + .cloned() + .collect::>() + ) + ); + } + assert!(!action.available_for("agent_service", &caps)); + if let Action::RunScript { + interpreter, + script_utf8, + windows_execution_profile, + .. + } = &mut action + { + *interpreter = "powershell".into(); + *script_utf8 = "Write-Output 'hi'\r\n".into(); + *windows_execution_profile = Some(WindowsExecutionProfile::PowerShell51RemoteSigned); + } + assert!(action.valid_helper_denial_shape("system")); + assert!(!action.valid_helper_denial_shape("root")); + assert!(action.valid_job_limits(120, 65536)); + assert!(!action.valid_job_limits(121, 65536)); + let caps: Vec = [ + "windows.base/1", + "rmm.sync.v1", + "helper.system.v1", + "run_script", + "system:run_script", + "script.powershell/1", + "script.powershell.process_remote_signed/1", + FULL_MAINTENANCE_CAPABILITY, + ] + .into_iter() + .map(str::to_owned) + .collect(); + assert!(action.available_for("system", &caps)); + for missing in &caps { + assert!( + !action.available_for( + "system", + &caps + .iter() + .filter(|c| *c != missing) + .cloned() + .collect::>() + ) + ); + } + assert!(!action.available_for("root", &caps)); + } + #[test] + fn script_console_golden_plans_use_actual_rust_codec() { + let cases: Vec = serde_json::from_str(include_str!( + "../../../tests/script_console/codec-golden.json" + )) + .unwrap(); + assert_eq!(cases.len(), 3); + for case in cases { + let bytes: Vec = + serde_json::from_value(case["plan_payload_bytes"].clone()).unwrap(); + let plan: PlanPayload = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(encode(&plan).unwrap(), bytes); + assert_eq!(hash(&bytes), case["plan_hash"]); + let job = &plan.jobs[0]; + let job_bytes = encode(&job.payload).unwrap(); + assert_eq!( + serde_json::to_value(&job_bytes).unwrap(), + case["job_payload_bytes"] + ); + assert_eq!(hash(&job_bytes), job.job_payload_hash); + assert_eq!(job.job_payload_hash, case["job_payload_hash"]); + assert!(job.payload.action.validate()); + assert!( + job.payload + .action + .valid_execution_identity(&job.payload.execution_identity) + ); + let Action::RunScript { script_utf8, .. } = &job.payload.action else { + panic!("script fixture") + }; + assert_eq!(hash(script_utf8.as_bytes()), case["script_sha256"]); + } + } + #[test] fn codec_golden_and_exact_script_bytes() { let a = Action::RunScript { interpreter: "posix_sh".into(), script_utf8: "echo hi\n".into(), arguments: vec![], expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, }; assert_eq!( String::from_utf8(encode(&a).unwrap()).unwrap(), @@ -613,6 +1000,186 @@ mod tests { ); } #[test] + fn windows_remote_signed_profile_is_optional_exact_and_capability_bound() { + let schema = schemars::schema_for!(Action).to_value(); + let script_schema = schema["oneOf"] + .as_array() + .unwrap() + .iter() + .find(|variant| { + variant["properties"] + .get("windows_execution_profile") + .is_some() + }) + .unwrap(); + assert_eq!( + script_schema["properties"]["windows_execution_profile"], + serde_json::json!({"$ref":"#/$defs/WindowsExecutionProfile"}) + ); + assert!( + !script_schema["required"] + .as_array() + .unwrap() + .contains(&serde_json::json!("windows_execution_profile")) + ); + assert_eq!(schema["$defs"]["WindowsExecutionProfile"]["type"], "string"); + let legacy = br#"{"action":"run_script","interpreter":"powershell","script_utf8":"Write-Output 'hi'\r\n","arguments":[],"expected_stdout":null}"#; + let mut action: Action = serde_json::from_slice(legacy).unwrap(); + assert_eq!(encode(&action).unwrap(), legacy); + let inherited_hash = hash(legacy); + if let Action::RunScript { + windows_execution_profile, + .. + } = &mut action + { + *windows_execution_profile = Some(WindowsExecutionProfile::PowerShell51RemoteSigned); + } + let encoded = encode(&action).unwrap(); + assert_eq!( + String::from_utf8(encoded.clone()).unwrap(), + String::from_utf8(legacy[..legacy.len() - 1].to_vec()).unwrap() + + r#","windows_execution_profile":"powershell_51_remote_signed"}"# + ); + assert_ne!(hash(&encoded), inherited_hash); + assert_eq!(serde_json::from_slice::(&encoded).unwrap(), action); + let caps: Vec = [ + "windows.base/1", + "rmm.sync.v1", + "run_script", + "script.powershell/1", + "script.powershell.process_remote_signed/1", + ] + .into_iter() + .map(str::to_string) + .collect(); + assert!(action.validate()); + assert!(action.available_for("agent_service", &caps)); + for missing in 0..caps.len() { + let mut short = caps.clone(); + short.remove(missing); + assert!(!action.available_for("agent_service", &short)); + } + for identity in ["root", "system", "SYSTEM"] { + assert!(!action.available_for(identity, &caps)); + } + let mut mixed = caps.clone(); + mixed.push("linux.base/1".into()); + assert!(!action.available_for("agent_service", &mixed)); + for interpreter in ["posix_sh", "bash", "pwsh"] { + let mut invalid = action.clone(); + if let Action::RunScript { interpreter: i, .. } = &mut invalid { + *i = interpreter.into(); + } + assert!(!invalid.validate()); + assert!(!invalid.available_for("agent_service", &caps)); + assert!(!invalid.available_for("root", &["linux.base/1".into(), "run_script".into()])); + } + for profile in [ + serde_json::Value::Null, + serde_json::json!(false), + serde_json::json!(1), + serde_json::json!([]), + serde_json::json!({}), + serde_json::json!({"powershell_51_remote_signed": null}), + serde_json::json!("bypass"), + serde_json::json!("RemoteSigned"), + serde_json::json!("powershell_7_remote_signed"), + ] { + let mut bad = serde_json::to_value(&action).unwrap(); + bad["windows_execution_profile"] = profile.clone(); + assert!(serde_json::from_value::(bad).is_err(), "{profile}"); + // An opt-in profile is also never metadata on another action. + assert!( + serde_json::from_value::(serde_json::json!({ + "action":"refresh_inventory", "scope":"windows.base", + "windows_execution_profile":profile + })) + .is_err() + ); + } + } + #[test] + fn windows_event_log_fixture_scope_limits_and_exact_24h_preserve_linux_bytes() { + let since = DateTime::parse_from_rfc3339("2026-11-14T00:00:00Z") + .unwrap() + .with_timezone(&Utc); + let mut action = Action::QueryLogs { + unit: WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(), + since, + until: since + chrono::Duration::hours(24), + max_lines: 1000, + }; + let caps: Vec = [ + "windows.base/1", + "rmm.sync.v1", + "query_logs", + WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY, + ] + .into_iter() + .map(str::to_string) + .collect(); + assert!(action.validate()); + assert!(action.available_for("agent_service", &caps)); + assert!(action.valid_job_limits(10, 1024)); + assert!(action.valid_job_limits(120, 65536)); + for (runtime, bytes) in [(9, 1024), (121, 1024), (10, 1023), (10, 65537)] { + assert!(!action.valid_job_limits(runtime, bytes)); + } + for i in 0..caps.len() { + let mut short = caps.clone(); + short.remove(i); + assert!(!action.available_for("agent_service", &short)); + } + for identity in ["root", "system", "SYSTEM"] { + assert!(!action.available_for(identity, &caps)); + } + let mut mixed = caps.clone(); + mixed.push("linux.base/1".into()); + assert!(!action.available_for("agent_service", &mixed)); + assert!(!action.available_for("root", &["linux.base/1".into(), "query_logs".into()])); + for unit in [ + "Application", + "Security", + "windows.eventlog.application.other", + ] { + let mut wrong = action.clone(); + if let Action::QueryLogs { unit: u, .. } = &mut wrong { + *u = unit.into(); + } + assert!(!wrong.available_for("agent_service", &caps)); + } + if let Action::QueryLogs { until, .. } = &mut action { + *until += chrono::Duration::nanoseconds(1); + } + assert!(!action.validate()); + let linux = Action::QueryLogs { + unit: "rmm-test.service".into(), + since, + until: since + chrono::Duration::hours(24), + max_lines: 1000, + }; + assert_eq!( + String::from_utf8(encode(&linux).unwrap()).unwrap(), + r#"{"action":"query_logs","unit":"rmm-test.service","since":"2026-11-14T00:00:00Z","until":"2026-11-15T00:00:00Z","max_lines":1000}"# + ); + assert!(linux.validate()); + for identity in ["root", "agent_service"] { + assert!(linux.available_for(identity, &["linux.base/1".into(), "query_logs".into()])); + } + let mut too_long = linux; + if let Action::QueryLogs { until, .. } = &mut too_long { + *until += chrono::Duration::minutes(59); + } + assert!(!too_long.validate()); + let pre_epoch = Action::QueryLogs { + unit: WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(), + since: DateTime::from_timestamp(-1, 0).unwrap(), + until: DateTime::from_timestamp(0, 0).unwrap(), + max_lines: 1, + }; + assert!(!pre_epoch.validate()); + } + #[test] fn no_terminal_replay_or_unknown_restart() { assert!(!JobState::Succeeded.allows(JobState::Running)); assert!(!JobState::Unknown.allows(JobState::Running)); diff --git a/crates/domain/src/lib.rs b/crates/domain/src/lib.rs index 5f394f7..78a3fd1 100644 --- a/crates/domain/src/lib.rs +++ b/crates/domain/src/lib.rs @@ -4,6 +4,7 @@ pub mod checkpoint_repair; pub mod inventory_document; pub mod jobs; pub mod reconciliation; +pub mod windows_execution; pub const TENANT_A: &str = "11111111-1111-4111-8111-111111111111"; pub const TENANT_B: &str = "22222222-2222-4222-8222-222222222222"; diff --git a/crates/domain/src/windows_execution.rs b/crates/domain/src/windows_execution.rs index 0f135fd..bb89a1b 100644 --- a/crates/domain/src/windows_execution.rs +++ b/crates/domain/src/windows_execution.rs @@ -1,39 +1,84 @@ -//! Unexported Windows MSI fixture contract. No legacy Linux codec is changed. -//! This version supports first installation or independently proven exact noop; -//! an upgrade requires a separate reviewed recipe with explicit predecessor -//! products/UpgradeCode semantics. No implicit REINSTALL flags are inferred. +//! Windows MSI fixture contract. No legacy Linux codec is changed. +//! Fixed1.0 first installation/noop and separate1.0→1.1 major-upgrade/noop +//! recipes only. No implicit REINSTALL, repair, downgrade or fresh1.1 mode. use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use uuid::Uuid; +pub const MSI_PACKAGE_ID: &str = "rmm-windows-msi-fixture"; +pub const MSI_CAPABILITY: &str = "package.msi.fixture/1"; pub const MSI_RECIPE_ID: &str = "rmm-windows-msi-fixture-install-1.0"; pub const MSI_PRODUCT_CODE: &str = "{3F070B4B-9C08-4D65-BD41-13D81B884AEC}"; pub const MSI_UPGRADE_CODE: &str = "{C9BF1FB8-E31D-4BD7-BA0D-AB46539BBBAA}"; pub const MSI_VERSION: &str = "1.0.0"; pub const MSI_PAYLOAD: &str = r"C:\Program Files\RustDiscoveryFixtures\msi-payload.txt"; -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub const MSI_UPGRADE_RECIPE_ID: &str = "rmm-windows-msi-fixture-upgrade-1.1"; +pub const MSI_UPGRADE_PRODUCT_CODE: &str = "{D42BD387-079B-4BBF-A134-0DA7B7A97011}"; +pub const MSI_UPGRADE_VERSION: &str = "1.1.0"; +pub const MSI_PAYLOAD_BYTES: u64 = 33; +pub const MSI_PAYLOAD_10_SHA256: &str = + "0c90c4fc7233017cc88c0e925095d6662ac57abdb303c7527ef59d02b454fca0"; +pub const MSI_PAYLOAD_11_SHA256: &str = + "ba0c83deff9eed014009bcd45369b2a356ae909ebf0c1434dc836b0e6d759c49"; +pub const MSI_OLD_PRODUCTS: &str = "RMM_OLD_PRODUCTS"; +pub const MSI_ALL_PRODUCTS: &str = "RMM_ALL_PRODUCTS"; +pub const MSI_UPGRADE_CONDITION: &str = "Installed OR (RMM_OLD_PRODUCTS = \"{3F070B4B-9C08-4D65-BD41-13D81B884AEC}\" AND RMM_ALL_PRODUCTS = \"{3F070B4B-9C08-4D65-BD41-13D81B884AEC}\")"; +#[derive(Clone, Debug, PartialEq, Eq, Serialize, schemars::JsonSchema)] #[serde(rename_all = "snake_case")] pub enum Architecture { X64, } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, schemars::JsonSchema)] #[serde(rename_all = "snake_case")] pub enum InstallContext { Machine, } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, schemars::JsonSchema)] #[serde(rename_all = "snake_case")] pub enum MsiCommandProfile { FixtureQuietNoRebootV1, } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +impl<'de> Deserialize<'de> for Architecture { + fn deserialize>(deserializer: D) -> Result { + let value = String::deserialize(deserializer)?; + if value == "x64" { + Ok(Self::X64) + } else { + Err(serde::de::Error::unknown_variant(&value, &["x64"])) + } + } +} +impl<'de> Deserialize<'de> for InstallContext { + fn deserialize>(deserializer: D) -> Result { + let value = String::deserialize(deserializer)?; + if value == "machine" { + Ok(Self::Machine) + } else { + Err(serde::de::Error::unknown_variant(&value, &["machine"])) + } + } +} +impl<'de> Deserialize<'de> for MsiCommandProfile { + fn deserialize>(deserializer: D) -> Result { + let value = String::deserialize(deserializer)?; + if value == "fixture_quiet_no_reboot_v1" { + Ok(Self::FixtureQuietNoRebootV1) + } else { + Err(serde::de::Error::unknown_variant( + &value, + &["fixture_quiet_no_reboot_v1"], + )) + } + } +} +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)] #[serde(deny_unknown_fields)] pub struct MsiArtifact { pub artifact_id: Uuid, pub sha256: String, pub bytes: u64, } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)] #[serde(deny_unknown_fields)] pub struct MsiPostcondition { pub product_code: String, @@ -42,7 +87,7 @@ pub struct MsiPostcondition { pub payload_sha256: String, pub payload_bytes: u64, } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)] #[serde(deny_unknown_fields)] pub struct WindowsMsiRecipe { pub schema_version: u32, @@ -56,8 +101,13 @@ pub struct WindowsMsiRecipe { pub artifact: MsiArtifact, pub postcondition: MsiPostcondition, pub no_reboot: bool, + // Appended opt-in metadata: old1.0 recipes retain exactly their old bytes. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub package_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub predecessor: Option, } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)] #[serde(deny_unknown_fields)] pub struct ResolvedWindowsMsi { pub recipe: WindowsMsiRecipe, @@ -70,10 +120,16 @@ pub fn digest(value: &str) -> bool { .bytes() .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) } +pub fn valid_package_code(value: &str) -> bool { + Uuid::parse_str(value).is_ok_and(|id| { + !id.is_nil() + && value == format!("{{{}}}", id.to_string().to_uppercase()) + && ![MSI_PRODUCT_CODE, MSI_UPGRADE_PRODUCT_CODE, MSI_UPGRADE_CODE].contains(&value) + }) +} impl WindowsMsiRecipe { pub fn validate(&self) -> bool { self.schema_version == 1 - && self.recipe_id == MSI_RECIPE_ID && self.recipe_version == "1" && self.platform == "windows" && self.upgrade_code == MSI_UPGRADE_CODE @@ -81,11 +137,32 @@ impl WindowsMsiRecipe { && !self.artifact.artifact_id.is_nil() && digest(&self.artifact.sha256) && (1..=262144).contains(&self.artifact.bytes) - && self.postcondition.product_code == MSI_PRODUCT_CODE - && self.postcondition.product_version == MSI_VERSION && self.postcondition.payload_path == MSI_PAYLOAD && digest(&self.postcondition.payload_sha256) && (1..=65536).contains(&self.postcondition.payload_bytes) + && match self.recipe_id.as_str() { + MSI_RECIPE_ID => { + self.postcondition.product_code == MSI_PRODUCT_CODE + && self.postcondition.product_version == MSI_VERSION + && self.package_code.is_none() + && self.predecessor.is_none() + } + MSI_UPGRADE_RECIPE_ID => { + self.postcondition.product_code == MSI_UPGRADE_PRODUCT_CODE + && self.postcondition.product_version == MSI_UPGRADE_VERSION + && self.postcondition.payload_sha256 == MSI_PAYLOAD_11_SHA256 + && self.postcondition.payload_bytes == MSI_PAYLOAD_BYTES + && self.package_code.as_deref().is_some_and(valid_package_code) + && self.predecessor.as_ref().is_some_and(|old| { + old.product_code == MSI_PRODUCT_CODE + && old.product_version == MSI_VERSION + && old.payload_path == MSI_PAYLOAD + && old.payload_sha256 == MSI_PAYLOAD_10_SHA256 + && old.payload_bytes == MSI_PAYLOAD_BYTES + }) + } + _ => false, + } } pub fn sha256(&self) -> Result { Ok(format!("{:x}", Sha256::digest(serde_json::to_vec(self)?))) @@ -127,6 +204,156 @@ mod tests { payload_bytes: 20, }, no_reboot: true, + package_code: None, + predecessor: None, + } + } + #[test] + fn ensure_msi_action_is_strict_opt_in_and_system_windows_only_without_linux_byte_changes() { + use crate::jobs::{Action, encode}; + let old=br#"{"action":"ensure_package","provider":"apt","package":"rmm-test","version":"1.0","architecture":"amd64","recipe_version":"1","installation_context":"system"}"#; + let legacy: Action = serde_json::from_slice(old).unwrap(); + assert!(legacy.validate_plan_shape()); + assert!(legacy.windows_msi().is_none()); + assert_eq!(encode(&legacy).unwrap(), old); + let recipe = recipe(); + let m = ResolvedWindowsMsi { + recipe_sha256: recipe.sha256().unwrap(), + recipe, + }; + let action = Action::EnsurePackage { + provider: "msi".into(), + package: MSI_PACKAGE_ID.into(), + version: MSI_VERSION.into(), + architecture: "x64".into(), + recipe_version: "1".into(), + installation_context: "machine".into(), + recipe_id: Some(MSI_RECIPE_ID.into()), + apt_manifest: None, + windows_msi_manifest: Some(Box::new(m)), + }; + assert!(action.validate()); + assert!(action.valid_helper_denial_shape("system")); + assert!(!action.valid_helper_denial_shape("root")); + let caps: Vec = [ + "ensure_package", + "windows.base/1", + "rmm.sync.v1", + "helper.system.v1", + "system:ensure_package", + MSI_CAPABILITY, + ] + .into_iter() + .map(str::to_owned) + .collect(); + assert!(action.available_for("system", &caps)); + for identity in ["root", "agent_service"] { + assert!(!action.available_for(identity, &caps)); + } + for i in 0..caps.len() { + let mut short = caps.clone(); + short.remove(i); + assert!(!action.available_for("system", &short)); + } + let mut mixed = caps; + mixed.push("linux.base/1".into()); + assert!(!action.available_for("system", &mixed)); + for (key, value) in [ + ("provider", serde_json::json!("apt")), + ("architecture", serde_json::json!("amd64")), + ("installation_context", serde_json::json!("system")), + ("version", serde_json::json!("1.1.0")), + ] { + let mut bad = serde_json::to_value(&action).unwrap(); + bad[key] = value; + assert!(!serde_json::from_value::(bad).unwrap().validate()); + } + for value in [ + serde_json::Value::Null, + serde_json::json!(false), + serde_json::json!(1), + serde_json::json!([]), + serde_json::json!({}), + ] { + let mut bad = serde_json::to_value(&action).unwrap(); + bad["windows_msi_manifest"] = value; + assert!(serde_json::from_value::(bad).is_err()); + } + for (key, value) in [ + ("architecture", "x64"), + ("context", "machine"), + ("command_profile", "fixture_quiet_no_reboot_v1"), + ] { + let mut bad = serde_json::to_value(&action).unwrap(); + bad["windows_msi_manifest"]["recipe"][key] = serde_json::json!({value:null}); + assert!(serde_json::from_value::(bad).is_err()); + } + for (runtime, bytes) in [(9, 4096), (121, 4096), (10, 1023), (10, 65537)] { + assert!(!action.valid_job_limits(runtime, bytes)); + } + let schema = schemars::schema_for!(Action).to_value(); + let pkg = schema["oneOf"] + .as_array() + .unwrap() + .iter() + .find(|v| v["properties"].get("windows_msi_manifest").is_some()) + .unwrap(); + assert_eq!( + pkg["properties"]["windows_msi_manifest"], + serde_json::json!({"$ref":"#/$defs/ResolvedWindowsMsi"}) + ); + assert!( + !pkg["required"] + .as_array() + .unwrap() + .contains(&serde_json::json!("windows_msi_manifest")) + ); + } + #[test] + fn major_upgrade_requires_explicit_exact_predecessor_new_package_and_same_family() { + let old = recipe(); + let old_bytes = serde_json::to_vec(&old).unwrap(); + assert!( + !String::from_utf8(old_bytes.clone()) + .unwrap() + .contains("predecessor") + ); + assert_eq!( + serde_json::to_vec(&serde_json::from_slice::(&old_bytes).unwrap()) + .unwrap(), + old_bytes + ); + let mut r = old.clone(); + r.recipe_id = MSI_UPGRADE_RECIPE_ID.into(); + r.package_code = Some("{8475C4D1-6EE2-4C3A-B41D-D834A20F4135}".into()); + r.postcondition.product_code = MSI_UPGRADE_PRODUCT_CODE.into(); + r.postcondition.product_version = MSI_UPGRADE_VERSION.into(); + r.postcondition.payload_sha256 = MSI_PAYLOAD_11_SHA256.into(); + r.postcondition.payload_bytes = MSI_PAYLOAD_BYTES; + r.predecessor = Some(MsiPostcondition { + product_code: MSI_PRODUCT_CODE.into(), + product_version: MSI_VERSION.into(), + payload_path: MSI_PAYLOAD.into(), + payload_sha256: MSI_PAYLOAD_10_SHA256.into(), + payload_bytes: MSI_PAYLOAD_BYTES, + }); + assert!(r.validate()); + assert_ne!(r.sha256().unwrap(), old.sha256().unwrap()); + for case in 0..8 { + let mut bad = r.clone(); + match case { + 0 => bad.predecessor = None, + 1 => bad.package_code = None, + 2 => bad.package_code = Some(MSI_PRODUCT_CODE.into()), + 3 => bad.upgrade_code = Uuid::new_v4().to_string(), + 4 => { + bad.predecessor.as_mut().unwrap().product_code = MSI_UPGRADE_PRODUCT_CODE.into() + } + 5 => bad.predecessor.as_mut().unwrap().payload_sha256 = "aa".repeat(32), + 6 => bad.postcondition.product_version = "1.2.0".into(), + _ => bad.postcondition.payload_sha256 = MSI_PAYLOAD_10_SHA256.into(), + }; + assert!(!bad.validate()); } } #[test] diff --git a/crates/privileged-helper/src/apt/tests.rs b/crates/privileged-helper/src/apt/tests.rs index ca31c1c..fc9cb23 100644 --- a/crates/privileged-helper/src/apt/tests.rs +++ b/crates/privileged-helper/src/apt/tests.rs @@ -481,6 +481,8 @@ fn signed_request() -> ( script_utf8: "true".into(), arguments: vec![], expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, }, execution_identity: "root".into(), not_before: now - chrono::Duration::seconds(1), diff --git a/crates/privileged-helper/src/control.rs b/crates/privileged-helper/src/control.rs index 7840eb4..c32a5b8 100644 --- a/crates/privileged-helper/src/control.rs +++ b/crates/privileged-helper/src/control.rs @@ -69,6 +69,18 @@ pub struct Policy { pub allowed_services: Vec, pub allow_root_scripts: bool, pub allow_reboot: bool, + #[serde( + default, + skip_serializing_if = "Option::is_none", + deserialize_with = "rmm_domain::jobs::deserialize_script_profiles" + )] + pub allowed_script_profiles: Option>, + #[serde( + default, + skip_serializing_if = "Option::is_none", + deserialize_with = "rmm_domain::jobs::deserialize_optional_sha256" + )] + pub helper_binary_sha256: Option, } impl Policy { pub fn sha256(&self) -> Result { @@ -90,6 +102,12 @@ pub struct Response { pub ledger_inode: u64, pub lock_device: u64, pub lock_inode: u64, + #[serde( + default, + skip_serializing_if = "Option::is_none", + deserialize_with = "rmm_domain::jobs::deserialize_optional_sha256" + )] + pub helper_binary_sha256: Option, } #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -98,6 +116,12 @@ pub struct GeneralPin { pub profile: String, pub identity: Identity, pub policy_sha256: String, + #[serde( + default, + skip_serializing_if = "Option::is_none", + deserialize_with = "rmm_domain::jobs::deserialize_optional_sha256" + )] + pub helper_binary_sha256: Option, } impl GeneralPin { pub fn read(path: &Path, binding: &Binding) -> Result { @@ -105,8 +129,12 @@ impl GeneralPin { ensure!(fs::metadata(path)?.len() <= 8192, "POLICY_PIN_LIMIT"); let pin: Self = serde_json::from_slice(&fs::read(path)?)?; ensure!( - pin.schema_version == 1 + ((pin.schema_version == 1 && pin.profile == "general" + && pin.helper_binary_sha256.is_none()) + || (pin.schema_version == 2 + && pin.profile == "full_maintenance_v1" + && pin.helper_binary_sha256.as_ref().is_some_and(|h| digest(h)))) && pin.identity == Identity::from_binding(binding)? && digest(&pin.policy_sha256), "POLICY_PIN_BINDING" @@ -199,9 +227,20 @@ pub fn validate_response(request: &Request, response: &Response, binding: &Bindi && response.challenge == challenge && &response.identity == identity && response.identity == Identity::from_binding(binding)? - && matches!(response.profile.as_str(), "general" | "apt_only") + && matches!( + response.profile.as_str(), + "general" | "apt_only" | "full_maintenance_v1" + ) && response.ledger_inode != 0 - && response.lock_inode != 0, + && response.lock_inode != 0 + && if response.profile == "full_maintenance_v1" { + response + .helper_binary_sha256 + .as_ref() + .is_some_and(|h| digest(h)) + } else { + response.helper_binary_sha256.is_none() + }, "HELPER_CONTROL_RESPONSE_BINDING" ); Ok(()) @@ -214,8 +253,23 @@ pub fn general_probe(socket: &Path, binding: &Binding, pin: &GeneralPin) -> Resu let response = call(socket, binding, &request)?; let policy = response.policy.as_ref().context("GENERAL_POLICY_MISSING")?; ensure!( - response.profile == "general" - && policy.profile == "general" + response.profile == pin.profile + && policy.profile == pin.profile + && ((policy.schema_version == 1 + && policy.profile == "general" + && policy.allowed_script_profiles.is_none() + && policy.helper_binary_sha256.is_none() + && pin.helper_binary_sha256.is_none()) + || (policy.schema_version == 2 + && policy.profile == "full_maintenance_v1" + && policy.allowed_script_profiles.as_deref() + == Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]) + && policy.helper_binary_sha256 == pin.helper_binary_sha256 + && response.helper_binary_sha256 == pin.helper_binary_sha256 + && pin.helper_binary_sha256.is_some() + && policy.allow_root_scripts + && !policy.allow_reboot + && policy.allowed_services.is_empty())) && response.policy_sha256 == pin.policy_sha256 && policy.sha256()? == pin.policy_sha256 && policy.identity == pin.identity, @@ -264,8 +318,21 @@ mod tests { ledger_inode: 2, lock_device: 1, lock_inode: 3, + helper_binary_sha256: None, }; validate_response(&q, &r, &b).unwrap(); + let legacy = serde_json::to_value(&r).unwrap(); + assert!(legacy.get("helper_binary_sha256").is_none()); + let mut null = legacy.clone(); + null["helper_binary_sha256"] = serde_json::Value::Null; + assert!(serde_json::from_value::(null).is_err()); + r.profile = "full_maintenance_v1".into(); + assert!(validate_response(&q, &r, &b).is_err()); + r.helper_binary_sha256 = Some("ab".repeat(32)); + validate_response(&q, &r, &b).unwrap(); + r.profile = "general".into(); + assert!(validate_response(&q, &r, &b).is_err()); + r.helper_binary_sha256 = None; r.challenge = Uuid::new_v4(); assert!(validate_response(&q, &r, &b).is_err()); r.challenge = challenge; @@ -313,6 +380,8 @@ mod tests { allowed_services: vec!["rmm-acceptance.service".into()], allow_root_scripts: false, allow_reboot: false, + allowed_script_profiles: None, + helper_binary_sha256: None, }; let old = p.sha256().unwrap(); let mut changed = p.clone(); diff --git a/crates/privileged-helper/src/image.rs b/crates/privileged-helper/src/image.rs new file mode 100644 index 0000000..03357c4 --- /dev/null +++ b/crates/privileged-helper/src/image.rs @@ -0,0 +1,91 @@ +//! Root observes its own *running* executable. An agent-readable configured +//! pathname or a build claim is never substituted for this descriptor. +use anyhow::{Result, ensure}; +use openssl::sha::Sha256; +use std::{ + fs::File, + io::{Read, Seek, SeekFrom}, + os::unix::fs::MetadataExt, +}; + +pub struct RunningImage { + file: File, + expected: String, +} +impl RunningImage { + pub fn current(expected: &str) -> Result { + let mut value = Self { + file: File::open("/proc/self/exe")?, + expected: expected.into(), + }; + value.verify()?; + Ok(value) + } + pub fn verify(&mut self) -> Result { + let before = self.file.metadata()?; + ensure!( + before.is_file() + && before.uid() == 0 + && before.mode() & 0o022 == 0 + && before.len() > 0 + && before.len() <= 128 * 1024 * 1024, + "RUNNING_HELPER_IMAGE_METADATA" + ); + let identity = |m: &std::fs::Metadata| { + ( + m.dev(), + m.ino(), + m.len(), + m.uid(), + m.mode(), + m.mtime(), + m.mtime_nsec(), + m.ctime(), + m.ctime_nsec(), + ) + }; + self.file.seek(SeekFrom::Start(0))?; + let mut hash = Sha256::new(); + let mut buffer = [0u8; 65536]; + let mut length = 0u64; + loop { + let n = self.file.read(&mut buffer)?; + if n == 0 { + break; + } + length += n as u64; + ensure!( + length <= before.len(), + "RUNNING_HELPER_IMAGE_LENGTH_CHANGED" + ); + hash.update(&buffer[..n]); + } + let actual = hex::encode(hash.finish()); + ensure!( + length == before.len() + && identity(&before) == identity(&self.file.metadata()?) + && actual == self.expected, + "RUNNING_HELPER_IMAGE_PIN_MISMATCH" + ); + Ok(actual) + } +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn actual_running_image_is_independent_of_a_claimed_path_or_digest() { + let metadata = std::fs::metadata("/proc/self/exe").unwrap(); + let bytes = std::fs::read("/proc/self/exe").unwrap(); + let actual = hex::encode(openssl::sha::sha256(&bytes)); + if metadata.uid() == 0 && metadata.mode() & 0o022 == 0 { + let mut image = RunningImage::current(&actual).unwrap(); + assert_eq!(image.verify().unwrap(), actual); + } else { + // An untrusted-owned/writable executable must fail even if its + // content hash is correct. This is a tested denial, never a skip. + assert!(RunningImage::current(&actual).is_err()); + } + assert!(RunningImage::current(&"00".repeat(32)).is_err()); + } +} diff --git a/crates/privileged-helper/src/lib.rs b/crates/privileged-helper/src/lib.rs index 0f90fec..3d56776 100644 --- a/crates/privileged-helper/src/lib.rs +++ b/crates/privileged-helper/src/lib.rs @@ -124,12 +124,17 @@ pub fn save_start( } pub fn script(job: &JobPayload) -> Result { - use rmm_agent_linux::actions::{Interpreter, ScriptRequest, run_script}; + script_controlled(job, || Ok(())) +} +pub fn script_controlled(job: &JobPayload, observe: impl FnMut() -> Result<()>) -> Result { + use rmm_agent_linux::actions::{Interpreter, ScriptRequest, run_script_controlled}; let rmm_domain::jobs::Action::RunScript { interpreter, script_utf8, arguments, expected_stdout, + windows_execution_profile: None, + .. } = &job.action else { anyhow::bail!("NOT_SCRIPT") @@ -146,14 +151,15 @@ pub fn script(job: &JobPayload) -> Result { std::time::Duration::from_secs(job.max_runtime_seconds.into()), job.max_output_bytes as usize, )?; - let output = run_script(&request)?; + let output = run_script_controlled(&request, observe)?; let passed = output.code == Some(0) && !output.timed_out + && !output.interrupted && expected_stdout .as_ref() .is_none_or(|s| !output.truncated && output.stdout == s.as_bytes()); Ok(Outcome { - state: if output.timed_out || output.code.is_none() { + state: if output.timed_out || output.interrupted || output.code.is_none() { "unknown" } else if passed { "succeeded" @@ -166,9 +172,11 @@ pub fn script(job: &JobPayload) -> Result { exit_code: output.code, output_truncated: output.truncated, reboot_required: false, - evidence: serde_json::json!({"postconditions_passed":passed}), + evidence: serde_json::json!({"postconditions_passed":passed,"script_sha256":rmm_domain::jobs::hash(script_utf8.as_bytes()),"privileged_script_profile":if job.action.full_maintenance(){Some("full_maintenance_v1")}else{None},"execution_uid":unsafe{libc::geteuid()},"output_encoding":"raw_native_bytes","cancel_or_fence_observed":output.interrupted,"termination_does_not_prove_absence_of_effects":output.interrupted||output.timed_out}), error_code: if passed { "" + } else if output.interrupted { + "CANCEL_OR_FENCE_EFFECT_UNKNOWN" } else if output.timed_out { "TIMEOUT_EFFECT_UNKNOWN" } else { @@ -209,6 +217,14 @@ pub struct TrustedClock { /// Local client authenticates the root process as well as the root-owned socket. /// A malicious same-user Unix server must not be able to forge helper success. pub fn invoke(socket: &Path, request: &Request, max_runtime_seconds: u32) -> Result { + invoke_controlled(socket, request, max_runtime_seconds, || Ok(())) +} +pub fn invoke_controlled( + socket: &Path, + request: &Request, + max_runtime_seconds: u32, + mut observe: impl FnMut() -> Result<()>, +) -> Result { use std::{ io::{Read, Write}, os::{ @@ -255,16 +271,50 @@ pub fn invoke(socket: &Path, request: &Request, max_runtime_seconds: u32) -> Res stream.set_read_timeout(Some(Duration::from_secs( u64::from(max_runtime_seconds) + 10, )))?; + observe()?; let bytes = serde_json::to_vec(request)?; ensure!(bytes.len() <= 512 * 1024, "HELPER_REQUEST_SIZE"); stream.write_all(&(u32::try_from(bytes.len())?).to_be_bytes())?; stream.write_all(&bytes)?; + stream.set_read_timeout(Some(Duration::from_millis(50)))?; + let started = std::time::Instant::now(); + let mut closed = false; + let mut read = |stream: &mut UnixStream, mut target: &mut [u8]| -> Result<()> { + while !target.is_empty() { + if !closed && observe().is_err() { + // Half-close only this exact authenticated request stream. The + // helper treats its EOF as cancellation; result reads continue. + stream.shutdown(std::net::Shutdown::Write)?; + closed = true; + } + ensure!( + started.elapsed() < Duration::from_secs(u64::from(max_runtime_seconds) + 10), + "HELPER_RESPONSE_TIMEOUT_EFFECT_UNKNOWN" + ); + match stream.read(target) { + Ok(0) => anyhow::bail!("HELPER_RESPONSE_CLOSED_EFFECT_UNKNOWN"), + Ok(n) => target = &mut target[n..], + Err(e) + if matches!( + e.kind(), + std::io::ErrorKind::WouldBlock + | std::io::ErrorKind::TimedOut + | std::io::ErrorKind::Interrupted + ) => + { + continue; + } + Err(e) => return Err(e.into()), + } + } + Ok(()) + }; let mut length = [0; 4]; - stream.read_exact(&mut length)?; + read(&mut stream, &mut length)?; let length = u32::from_be_bytes(length) as usize; ensure!(length <= 2 * 1024 * 1024, "HELPER_RESPONSE_SIZE"); let mut bytes = vec![0; length]; - stream.read_exact(&mut bytes)?; + read(&mut stream, &mut bytes)?; let outcome: Outcome = serde_json::from_slice(&bytes)?; ensure!( outcome.stdout.len() + outcome.stderr.len() <= 262144, diff --git a/crates/privileged-helper/src/main.rs b/crates/privileged-helper/src/main.rs index 8aa7e9c..1bce65c 100644 --- a/crates/privileged-helper/src/main.rs +++ b/crates/privileged-helper/src/main.rs @@ -1,3 +1,4 @@ +mod image; use anyhow::{Result, ensure}; use rmm_domain::jobs::Action; use rmm_privileged_helper::{ @@ -33,6 +34,16 @@ struct Config { allowed_services: Vec, allow_reboot: bool, allow_root_scripts: bool, + #[serde( + default, + deserialize_with = "rmm_domain::jobs::deserialize_script_profiles" + )] + allowed_script_profiles: Option>, + #[serde( + default, + deserialize_with = "rmm_domain::jobs::deserialize_optional_sha256" + )] + helper_binary_sha256: Option, #[serde(default)] apt_provider: Option, } @@ -42,16 +53,28 @@ enum HelperProfile { #[default] General, AptOnly, + FullMaintenanceV1, } impl Config { fn general_policy(&self) -> Result { Ok(control::Policy { - schema_version: 1, - profile: "general".into(), + schema_version: if self.profile == HelperProfile::FullMaintenanceV1 { + 2 + } else { + 1 + }, + profile: if self.profile == HelperProfile::FullMaintenanceV1 { + "full_maintenance_v1" + } else { + "general" + } + .into(), identity: rmm_privileged_helper::readiness::Identity::from_binding(&self.binding)?, allowed_services: self.allowed_services.clone(), allow_root_scripts: self.allow_root_scripts, allow_reboot: self.allow_reboot, + allowed_script_profiles: self.allowed_script_profiles.clone(), + helper_binary_sha256: self.helper_binary_sha256.clone(), }) } fn validate_profile(&self) -> Result<()> { @@ -60,7 +83,19 @@ impl Config { == std::path::Path::new(rmm_privileged_helper::readiness::SHARED_STATE), "SHARED_HELPER_LEDGER_REQUIRED" ); - if self.profile == HelperProfile::AptOnly { + if self.profile == HelperProfile::FullMaintenanceV1 { + ensure!( + self.apt_provider.is_none() + && self.allowed_services.is_empty() + && !self.allow_reboot + && self.allow_root_scripts + && self.allowed_script_profiles.as_deref() + == Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]) + && self.socket.as_path() + != std::path::Path::new(rmm_privileged_helper::readiness::SOCKET), + "FULL_MAINTENANCE_PROFILE" + ); + } else if self.profile == HelperProfile::AptOnly { ensure!( self.agent_uid == 993 && self.socket.as_path() @@ -79,6 +114,19 @@ impl Config { "GENERAL_HELPER_CANNOT_RUN_APT" ); } + ensure!( + self.profile == HelperProfile::FullMaintenanceV1 + || self.allowed_script_profiles.is_none(), + "FULL_MAINTENANCE_EXPLICIT_PROFILE_REQUIRED" + ); + ensure!( + if self.profile == HelperProfile::FullMaintenanceV1 { + self.helper_binary_sha256.is_some() + } else { + self.helper_binary_sha256.is_none() + }, + "FULL_MAINTENANCE_BINARY_PIN_REQUIRED" + ); Ok(()) } } @@ -138,9 +186,21 @@ fn policy(config: &Config, job: &rmm_domain::jobs::JobPayload) -> Result<()> { config.profile != HelperProfile::AptOnly, "APT_ONLY_CROSS_ACTION_DENIED" ); + ensure!( + config.profile != HelperProfile::FullMaintenanceV1 || job.action.full_maintenance(), + "FULL_MAINTENANCE_ONLY_SCRIPT" + ); match &job.action { Action::RunScript { interpreter, .. } => { ensure!(config.allow_root_scripts, "ROOT_SCRIPT_DENIED"); + ensure!( + job.action.full_maintenance() + == (config.profile == HelperProfile::FullMaintenanceV1) + && job.action.valid_execution_identity(&job.execution_identity) + && (!job.action.full_maintenance() + || (job.max_runtime_seconds <= 300 && job.max_output_bytes <= 65536)), + "ROOT_SCRIPT_PROFILE_DENIED" + ); let available = match interpreter.as_str() { "posix_sh" => rmm_agent_linux::actions::Interpreter::PosixSh.available(), "bash" => rmm_agent_linux::actions::Interpreter::Bash.available(), @@ -267,6 +327,7 @@ fn handle( clock: &rmm_privileged_helper::TrustedClock, sync: &mut reconcile::HelperSync, sync_clock: &reconcile::NativeClock, + mut image: Option<&mut image::RunningImage>, ) -> Result<()> { use rusqlite::OptionalExtension; peer_allowed(stream, config.agent_uid)?; @@ -278,6 +339,11 @@ fn handle( ensure!(size <= 8 * 1024 * 1024, "REQUEST_SIZE"); let mut bytes = vec![0; size]; stream.read_exact(&mut bytes)?; + let binary_sha256 = image.as_mut().map(|i| i.verify()).transpose()?; + ensure!( + binary_sha256 == config.helper_binary_sha256, + "RUNNING_HELPER_BINARY_CHANGED" + ); let request: Request = match serde_json::from_slice::(&bytes)? { Incoming::Job(request) => { ensure!(size <= 512 * 1024, "JOB_REQUEST_SIZE"); @@ -328,11 +394,11 @@ fn handle( unlocked = applied.unlocked_grants; } control::Request::Policy { .. } => ensure!( - config.profile == HelperProfile::General, + config.profile != HelperProfile::AptOnly, "GENERAL_POLICY_PROFILE" ), } - let general = config.profile == HelperProfile::General; + let general = config.profile != HelperProfile::AptOnly; let policy = if general { Some(config.general_policy()?) } else { @@ -355,7 +421,14 @@ fn handle( schema_version: 1, challenge, identity: identity.clone(), - profile: if general { "general" } else { "apt_only" }.into(), + profile: if config.profile == HelperProfile::FullMaintenanceV1 { + "full_maintenance_v1" + } else if general { + "general" + } else { + "apt_only" + } + .into(), policy_sha256, policy, snapshot, @@ -364,6 +437,7 @@ fn handle( ledger_inode: ledger.ino(), lock_device: lock.dev(), lock_inode: lock.ino(), + helper_binary_sha256: binary_sha256.clone(), }; let bytes = serde_json::to_vec(&response)?; ensure!(bytes.len() <= 8 * 1024 * 1024, "CONTROL_RESPONSE_LIMIT"); @@ -441,6 +515,10 @@ fn handle( if let Some(apt) = &apt { apt.verify_retry_history(db)?; } + ensure!( + image.as_mut().map(|i| i.verify()).transpose()? == config.helper_binary_sha256, + "RUNNING_HELPER_BINARY_CHANGED" + ); rmm_privileged_helper::begin_execution(db, &permit, &request)?; let mut outcome = if !clock.trusted() || config.binding.verify(&request, chrono::Utc::now()).is_err() { @@ -454,7 +532,31 @@ fn handle( apt.execute(db) .unwrap_or_else(|_| Outcome::unknown("APT_HELPER_EFFECT_UNKNOWN")) } else { - execute(&job).unwrap_or_else(|_| Outcome::unknown("HELPER_EFFECT_UNKNOWN")) + if job.action.full_maintenance() { + rmm_privileged_helper::script_controlled(&job, || { + ensure!(clock.trusted(), "CLOCK_UNTRUSTED"); + peer_allowed(stream, config.agent_uid)?; + let mut byte = 0u8; + let read = unsafe { + libc::recv( + stream.as_raw_fd(), + (&mut byte as *mut u8).cast(), + 1, + libc::MSG_PEEK | libc::MSG_DONTWAIT, + ) + }; + ensure!( + read < 0 + && std::io::Error::last_os_error().kind() + == std::io::ErrorKind::WouldBlock, + "SCRIPT_CALLER_CANCELLED_OR_DISCONNECTED" + ); + Ok(()) + }) + .unwrap_or_else(|_| Outcome::unknown("HELPER_SCRIPT_EFFECT_UNKNOWN")) + } else { + execute(&job).unwrap_or_else(|_| Outcome::unknown("HELPER_EFFECT_UNKNOWN")) + } } }; if rmm_privileged_helper::apt::is_apt_action(&job) { @@ -497,7 +599,7 @@ fn run() -> Result<()> { let args: Vec<_> = std::env::args_os().skip(1).collect(); if args == [std::ffi::OsString::from("--help")] { println!( - "rmm-privileged-helper [--help|--inspect-general-profile|--inspect-apt-profile|--probe-native|--install-apt-provider]\nRMM_HELPER_CONFIG=/etc/rmm/helper.json (root-owned 0600).\nDaemon mode accepts only authenticated typed IPC; all effects need independent signed Sync and a fresh consumed grant. Administrative install/probe modes are explicit bootstrap actions, never network commands." + "rmm-privileged-helper [--help|--inspect-general-profile|--inspect-full-maintenance-profile|--inspect-apt-profile|--probe-native|--install-apt-provider]\nRMM_HELPER_CONFIG=/etc/rmm/helper.json (root-owned 0600).\nDaemon mode accepts only authenticated typed IPC; all effects need independent signed Sync and a fresh consumed grant. Administrative install/probe modes are explicit bootstrap actions, never network commands." ); return Ok(()); } @@ -510,7 +612,11 @@ fn run() -> Result<()> { args.is_empty() || args == [std::ffi::OsString::from("--probe-native")] || args == [std::ffi::OsString::from("--inspect-apt-profile")] - || args == [std::ffi::OsString::from("--inspect-general-profile")], + || args == [std::ffi::OsString::from("--inspect-general-profile")] + || args + == [std::ffi::OsString::from( + "--inspect-full-maintenance-profile" + )], "UNKNOWN_HELPER_MODE" ); let path = PathBuf::from( @@ -533,17 +639,38 @@ fn run() -> Result<()> { config.binding.public_key_hex = key.public_key_hex; } config.validate_profile()?; - if args == [std::ffi::OsString::from("--inspect-general-profile")] { + let mut running_image = config + .helper_binary_sha256 + .as_deref() + .map(image::RunningImage::current) + .transpose()?; + if args == [std::ffi::OsString::from("--inspect-general-profile")] + || args + == [std::ffi::OsString::from( + "--inspect-full-maintenance-profile", + )] + { ensure!( - config.profile == HelperProfile::General, + (config.profile == HelperProfile::General + && args == [std::ffi::OsString::from("--inspect-general-profile")]) + || (config.profile == HelperProfile::FullMaintenanceV1 + && args + == [std::ffi::OsString::from( + "--inspect-full-maintenance-profile" + )]), "GENERAL_POLICY_PROFILE" ); let policy = config.general_policy()?; let pin = control::GeneralPin { - schema_version: 1, - profile: "general".into(), + schema_version: if config.profile == HelperProfile::FullMaintenanceV1 { + 2 + } else { + 1 + }, + profile: policy.profile.clone(), identity: policy.identity.clone(), policy_sha256: policy.sha256()?, + helper_binary_sha256: running_image.as_mut().map(|i| i.verify()).transpose()?, }; println!("{}", serde_json::to_string(&pin)?); return Ok(()); @@ -627,6 +754,7 @@ fn run() -> Result<()> { &clock, &mut sync, &sync_clock, + running_image.as_mut(), ) { let code = safe_error_code(&error); eprintln!("helper request rejected: {code}"); @@ -704,6 +832,34 @@ mod tests { assert!(config.validate_profile().is_err()); } #[test] + fn full_maintenance_is_a_separate_explicit_profile_and_shared_ledger() { + let id = uuid::Uuid::new_v4(); + let now = chrono::Utc::now(); + let mut config:Config=serde_json::from_value(serde_json::json!({"profile":"general","binding":{"tenant_id":id,"asset_id":id,"agent_id":id,"enrollment_generation":1,"assignment_version":1,"public_key_hex":"00".repeat(32)},"agent_uid":993,"socket_gid":993,"socket":"/run/rmm-helper-maintenance/control.sock","state":rmm_privileged_helper::readiness::SHARED_STATE,"allowed_services":[],"allow_reboot":false,"allow_root_scripts":true})).unwrap(); + config.validate_profile().unwrap(); + let mut job:rmm_domain::jobs::JobPayload=serde_json::from_value(serde_json::json!({"schema_version":1,"tenant_id":id,"asset_id":id,"agent_id":id,"enrollment_generation":1,"assignment_version":1,"job_id":id,"attempt_id":id,"execution_identity":"root","not_before":now,"start_before":now+chrono::Duration::minutes(1),"max_runtime_seconds":30,"max_output_bytes":4096,"action":{"action":"run_script","interpreter":"posix_sh","script_utf8":"id\n","arguments":[],"expected_stdout":null,"privileged_script_profile":"full_maintenance_v1"}})).unwrap(); + assert!(policy(&config, &job).is_err()); + config.allowed_script_profiles = Some(vec![ + rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ]); + assert!(config.validate_profile().is_err()); + config.profile = HelperProfile::FullMaintenanceV1; + assert!(config.validate_profile().is_err()); + config.helper_binary_sha256 = Some("ab".repeat(32)); + config.validate_profile().unwrap(); + assert!(policy(&config, &job).is_ok()); + let proof = config.general_policy().unwrap(); + assert_eq!(proof.schema_version, 2); + assert_eq!(proof.profile, "full_maintenance_v1"); + job.action = Action::SetServiceState { + unit: "rmm-mvp-test.service".into(), + state: "running".into(), + }; + assert!(policy(&config, &job).is_err()); + config.state = "/var/lib/another-ledger".into(); + assert!(config.validate_profile().is_err()); + } + #[test] fn peer_acl_does_not_trust_request_identity() { let (a, _b) = UnixStream::pair().unwrap(); // SAFETY: geteuid has no preconditions. @@ -733,6 +889,8 @@ mod tests { allowed_services: vec!["rmm-acceptance.service".into()], allow_reboot: false, allow_root_scripts: false, + allowed_script_profiles: None, + helper_binary_sha256: None, apt_provider: None, }; let now = chrono::Utc::now(); @@ -750,6 +908,8 @@ mod tests { script_utf8: "true".into(), arguments: vec![], expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, }, execution_identity: "agent_service".into(), not_before: now, @@ -762,6 +922,13 @@ mod tests { assert!(policy(&config, &job).is_err()); config.allow_root_scripts = true; assert!(policy(&config, &job).is_ok()); + // The additive full profile must not alter historical local General + // ceilings; the independent central legacy policy remains unchanged. + job.max_runtime_seconds = 301; + job.max_output_bytes = 65537; + assert!(policy(&config, &job).is_ok()); + job.max_runtime_seconds = 180; + job.max_output_bytes = 4096; job.action = Action::SetServiceState { unit: "not-approved.service".into(), state: "running".into(), diff --git a/crates/privileged-helper/src/tests.rs b/crates/privileged-helper/src/tests.rs index 51061e3..8b062d8 100644 --- a/crates/privileged-helper/src/tests.rs +++ b/crates/privileged-helper/src/tests.rs @@ -33,6 +33,8 @@ fn fixture() -> ( script_utf8: "printf 'Hello World\\n'".into(), arguments: vec![], expected_stdout: Some("Hello World\n".into()), + windows_execution_profile: None, + privileged_script_profile: None, }, execution_identity: "agent_service".into(), not_before: now - chrono::Duration::seconds(60), diff --git a/crates/privileged-helper/tests/reconcile.rs b/crates/privileged-helper/tests/reconcile.rs index 1b3bc0b..42f8292 100644 --- a/crates/privileged-helper/tests/reconcile.rs +++ b/crates/privileged-helper/tests/reconcile.rs @@ -432,6 +432,8 @@ fn script() -> Action { script_utf8: "printf hello".into(), arguments: vec![], expected_stdout: Some("hello".into()), + windows_execution_profile: None, + privileged_script_profile: None, } } fn success() -> ReconciledOutcome { diff --git a/crates/server/src/agent_artifacts.rs b/crates/server/src/agent_artifacts.rs index 455f95d..22529fc 100644 --- a/crates/server/src/agent_artifacts.rs +++ b/crates/server/src/agent_artifacts.rs @@ -36,13 +36,20 @@ pub async fn download( return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR")); } let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?; - let row=sqlx::query("SELECT j.payload,p.payload AS plan_payload,p.plan_hash,i.asset_id,i.enrollment_generation,i.assignment_version FROM jobs j JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id JOIN authorizations z ON z.tenant_id=o.tenant_id AND z.authorization_id=o.authorization_id JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id JOIN operation_plans p ON p.tenant_id=r.tenant_id AND p.plan_id=r.plan_id JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id WHERE j.agent_id=$1 AND j.job_id=$2 AND j.attempt_id=$3 AND j.state IN ('queued','accepted','running','verifying','awaiting_reboot') AND r.approval_state='approved' AND z.revoked_at IS NULL AND p.expires_at>now() AND i.revoked_at IS NULL").bind(a).bind(q.job_id).bind(q.attempt_id).fetch_optional(&mut *tx).await?.ok_or(Error(StatusCode::NOT_FOUND,"NOT_FOUND"))?; + let row=sqlx::query("SELECT j.payload,p.payload AS plan_payload,p.plan_hash,z.principal AS approver,i.asset_id,i.enrollment_generation,i.assignment_version FROM jobs j JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id JOIN authorizations z ON z.tenant_id=o.tenant_id AND z.authorization_id=o.authorization_id JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id JOIN operation_plans p ON p.tenant_id=r.tenant_id AND p.plan_id=r.plan_id JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id WHERE j.agent_id=$1 AND j.job_id=$2 AND j.attempt_id=$3 AND j.state IN ('queued','accepted','running','verifying','awaiting_reboot') AND r.approval_state='approved' AND z.revoked_at IS NULL AND p.expires_at>now() AND i.revoked_at IS NULL").bind(a).bind(q.job_id).bind(q.attempt_id).fetch_optional(&mut *tx).await?.ok_or(Error(StatusCode::NOT_FOUND,"NOT_FOUND"))?; let job: JobPayload = serde_json::from_slice(&row.get::, _>("payload"))?; let plan_bytes: Vec = row.get("plan_payload"); if rmm_domain::jobs::hash(&plan_bytes) != row.get::("plan_hash") { return Err(Error(StatusCode::CONFLICT, "PLAN_HASH_MISMATCH")); } let plan: PlanPayload = serde_json::from_slice(&plan_bytes)?; + if job.action.windows_msi().is_some() { + crate::windows_msi_catalog::validate_job_binding(&plan, &job)?; + crate::catalog::check(&mut tx, &job.action).await?; + crate::root_policy::authorize(&mut tx, &plan.principal, &job).await?; + crate::root_policy::authorize(&mut tx, &row.get::("approver"), &job).await?; + } + if job.asset_id != row.get::("asset_id") || job.enrollment_generation != row.get::("enrollment_generation") as u64 || job.assignment_version != row.get::("assignment_version") as u64 diff --git a/crates/server/src/catalog.rs b/crates/server/src/catalog.rs index 3716eb1..413f513 100644 --- a/crates/server/src/catalog.rs +++ b/crates/server/src/catalog.rs @@ -24,6 +24,10 @@ pub struct AptRecipe { } pub fn routes(r: Router) -> Router { r.route( + "/api/v1/tenants/{tenant}/windows-msi-recipes", + get(crate::windows_msi_catalog::list).post(crate::windows_msi_catalog::import), + ) + .route( "/api/v1/tenants/{tenant}/package-recipes", get(list).post(import), ) @@ -43,6 +47,15 @@ pub async fn references( tx: &mut Transaction<'_, Postgres>, action: &rmm_domain::jobs::Action, ) -> Result, Error> { + if let Some(manifest) = action.windows_msi() { + if !action.validate() { + return Err(Error( + StatusCode::BAD_REQUEST, + "INVALID_WINDOWS_MSI_MANIFEST", + )); + } + return crate::windows_msi_catalog::references(tx, manifest).await; + } let manifest = match action { rmm_domain::jobs::Action::EnsurePackage { apt_manifest: Some(m), @@ -138,7 +151,7 @@ pub async fn references( references.dedup(); Ok(references) } -async fn context<'a>( +pub(crate) async fn context<'a>( s: &'a crate::State, h: &HeaderMap, t: Uuid, @@ -182,6 +195,7 @@ pub async fn import( installation_context: recipe.installation_context.clone(), recipe_id: None, apt_manifest: None, + windows_msi_manifest: None, }; if recipe.schema_version != 1 || recipe.reboot_policy != "never" diff --git a/crates/server/src/dev_delivery_faults.rs b/crates/server/src/dev_delivery_faults.rs index 63ed1e6..134045f 100644 --- a/crates/server/src/dev_delivery_faults.rs +++ b/crates/server/src/dev_delivery_faults.rs @@ -518,6 +518,8 @@ mod tests { script_utf8: "printf hello\\n".into(), arguments: vec![], expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, }, execution_identity: "agent_service".into(), not_before: c.issued_at, diff --git a/crates/server/src/job_gateway.rs b/crates/server/src/job_gateway.rs index e59c3ae..f309032 100644 --- a/crates/server/src/job_gateway.rs +++ b/crates/server/src/job_gateway.rs @@ -191,6 +191,8 @@ pub async fn event( ensure!(r.stdout == expected.as_bytes(), "expected output mismatch"); } } + script_profile_evidence(&payload.action, &evidence)?; + windows_event_log_success_evidence(&payload, r, &evidence)?; if matches!(payload.action, Action::Reboot { .. }) { crate::synchronization::verify_reboot_success( tx, tenant, agent, e, &payload, r, &evidence, @@ -224,6 +226,242 @@ pub async fn event( .await?; Ok(()) } +fn windows_event_log_success_evidence( + job: &JobPayload, + result: &JobResult, + evidence: &Value, +) -> Result<()> { + if !job.action.is_windows_event_log_fixture() { + return Ok(()); + } + let Action::QueryLogs { + unit, + since, + until, + max_lines, + } = &job.action + else { + anyhow::bail!("fixed event log action required") + }; + let observed_since: chrono::DateTime = serde_json::from_value( + evidence + .get("start_utc") + .cloned() + .context("event log start missing")?, + )?; + let observed_until: chrono::DateTime = serde_json::from_value( + evidence + .get("end_utc") + .cloned() + .context("event log end missing")?, + )?; + let complete = evidence.get("completeness") == Some(&json!("complete")) + && evidence.get("truncated") == Some(&json!(false)) + && evidence.get("error_code") == Some(&Value::Null); + let partial = evidence.get("completeness") == Some(&json!("partial")) + && evidence.get("truncated") == Some(&json!(true)) + && matches!( + evidence.get("error_code").and_then(Value::as_str), + Some("EVENT_LOG_EVENT_LIMIT" | "EVENT_LOG_BYTE_LIMIT" | "EVENT_LOG_EVENT_SIZE_LIMIT") + ); + ensure!( + !result.reboot_required + && evidence.get("truncated") == Some(&json!(result.output_truncated)) + && job.execution_identity == "agent_service" + && job.action.validate() + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes) + && evidence.get("source_id") == Some(&json!(unit)) + && evidence.get("channel") == Some(&json!(WINDOWS_EVENT_LOG_FIXTURE_CHANNEL)) + && evidence.get("provider") == Some(&json!(WINDOWS_EVENT_LOG_FIXTURE_PROVIDER)) + && observed_since == *since + && observed_until == *until + && evidence.get("max_events") == Some(&json!(max_lines)) + && evidence.get("max_output_bytes") == Some(&json!(job.max_output_bytes)) + && evidence.get("quality") == Some(&json!("valid")) + && evidence.get("win32_error") == Some(&Value::Null) + && evidence.get("bookmark_updated") == Some(&json!(false)) + && evidence.get("process_tree_bounded") == Some(&json!(true)) + && evidence.get("execution_sid") == Some(&json!("S-1-5-19")) + && (complete || partial), + "fixed Windows Event Log evidence mismatch" + ); + Ok(()) +} + +// The profile records the requested process option, never an effective GPO policy. +fn script_profile_evidence(action: &Action, evidence: &Value) -> Result<()> { + if action.full_maintenance() { + let Action::RunScript { script_utf8, .. } = action else { + unreachable!() + }; + ensure!( + evidence["privileged_script_profile"] == "full_maintenance_v1" + && evidence["script_sha256"] == hash(script_utf8.as_bytes()) + && match action.script_platform() { + Some("linux") => evidence["execution_uid"] == 0, + Some("windows") => evidence["execution_sid"] == "S-1-5-18", + _ => false, + }, + "full maintenance identity/profile evidence mismatch" + ); + } + if let Action::RunScript { + script_utf8, + windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned), + .. + } = action + { + ensure!( + evidence.get("windows_execution_profile") + == Some(&json!("powershell_51_remote_signed")) + && evidence.get("execution_policy") + == Some(&json!("process_remote_signed_requested")) + && evidence.get("execution_policy_requested") == Some(&json!("RemoteSigned")) + && evidence.get("execution_policy_scope") == Some(&json!("process")) + && evidence.get("script_sha256") == Some(&json!(hash(script_utf8.as_bytes()))), + "requested Windows process profile evidence mismatch" + ); + } + Ok(()) +} + +#[cfg(test)] +mod script_profile_tests { + use super::*; + #[test] + fn windows_event_log_success_binds_request_and_keeps_limit_partial_honest() { + let id = Uuid::new_v4(); + let now = Utc::now(); + let job = JobPayload { + schema_version: 1, + tenant_id: id, + asset_id: id, + agent_id: id, + enrollment_generation: 1, + assignment_version: 1, + job_id: id, + attempt_id: id, + action: Action::QueryLogs { + unit: WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(), + since: now - Duration::hours(1), + until: now, + max_lines: 1000, + }, + execution_identity: "agent_service".into(), + not_before: now, + start_before: now + Duration::minutes(1), + max_runtime_seconds: 30, + max_output_bytes: 4096, + }; + let evidence = json!({"source_id":WINDOWS_EVENT_LOG_FIXTURE_SOURCE,"channel":"Application","provider":"RustDiscovery-MVP-Test","start_utc":now-Duration::hours(1),"end_utc":now,"max_events":1000,"max_output_bytes":4096,"quality":"valid","completeness":"complete","truncated":false,"error_code":null,"win32_error":null,"bookmark_updated":false,"process_tree_bounded":true,"execution_sid":"S-1-5-19"}); + let result = JobResult::default(); + windows_event_log_success_evidence(&job, &result, &evidence).unwrap(); + for conflicting in [ + JobResult { + reboot_required: true, + ..Default::default() + }, + JobResult { + output_truncated: true, + ..Default::default() + }, + ] { + assert!(windows_event_log_success_evidence(&job, &conflicting, &evidence).is_err()); + } + for field in [ + "source_id", + "channel", + "provider", + "start_utc", + "end_utc", + "max_events", + "max_output_bytes", + "quality", + "win32_error", + "bookmark_updated", + "process_tree_bounded", + "execution_sid", + ] { + let mut bad = evidence.clone(); + bad[field] = json!("different"); + assert!( + windows_event_log_success_evidence(&job, &result, &bad).is_err(), + "{field}" + ); + } + for code in [ + "EVENT_LOG_EVENT_LIMIT", + "EVENT_LOG_BYTE_LIMIT", + "EVENT_LOG_EVENT_SIZE_LIMIT", + ] { + let mut partial = evidence.clone(); + partial["completeness"] = json!("partial"); + partial["truncated"] = json!(true); + partial["error_code"] = json!(code); + let partial_result = JobResult { + output_truncated: true, + ..Default::default() + }; + windows_event_log_success_evidence(&job, &partial_result, &partial).unwrap(); + assert!(windows_event_log_success_evidence(&job, &result, &partial).is_err()); + partial["error_code"] = json!("EVENT_LOG_TIMEOUT"); + assert!(windows_event_log_success_evidence(&job, &partial_result, &partial).is_err()); + partial["quality"] = json!("permission_denied"); + assert!(windows_event_log_success_evidence(&job, &partial_result, &partial).is_err()); + } + let mut permission_denied = evidence.clone(); + permission_denied["win32_error"] = json!(5); + assert!(windows_event_log_success_evidence(&job, &result, &permission_denied).is_err()); + let mut false_complete = evidence.clone(); + false_complete["truncated"] = json!(true); + assert!(windows_event_log_success_evidence(&job, &result, &false_complete).is_err()); + } + #[test] + fn requested_profile_success_evidence_is_exact_without_effective_policy_claim() { + let mut action = Action::RunScript { + interpreter: "powershell".into(), + script_utf8: "Write-Output 'hi'\r\n".into(), + arguments: vec![], + expected_stdout: None, + windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned), + privileged_script_profile: None, + }; + let evidence = json!({"windows_execution_profile":"powershell_51_remote_signed", + "execution_policy":"process_remote_signed_requested", "execution_policy_requested":"RemoteSigned", + "execution_policy_scope":"process", "script_sha256":hash(b"Write-Output 'hi'\r\n")}); + script_profile_evidence(&action, &evidence).unwrap(); + for field in [ + "windows_execution_profile", + "execution_policy", + "execution_policy_requested", + "execution_policy_scope", + "script_sha256", + ] { + let mut changed = evidence.clone(); + changed.as_object_mut().unwrap().remove(field); + assert!( + script_profile_evidence(&action, &changed).is_err(), + "{field}" + ); + changed[field] = json!("different"); + assert!( + script_profile_evidence(&action, &changed).is_err(), + "{field}" + ); + } + if let Action::RunScript { + windows_execution_profile, + .. + } = &mut action + { + *windows_execution_profile = None; + } + script_profile_evidence(&action, &json!({})).unwrap(); + } +} + async fn locked_job( tx: &mut Transaction<'_, Postgres>, agent: Uuid, @@ -269,6 +507,9 @@ pub async fn permit( let payload: JobPayload = serde_json::from_slice(&row.get::, _>("payload"))?; let plan: PlanPayload = serde_json::from_slice(&row.get::, _>("plan_payload"))?; let capabilities: Vec = serde_json::from_value(row.get("capabilities"))?; + if crate::windows_msi_catalog::validate_job_binding(&plan, &payload).is_err() { + return Ok(denied("WINDOWS_MSI_BINDING_MISMATCH")); + } if row.get::("bound_asset") != payload.asset_id || !payload .action @@ -314,6 +555,12 @@ pub async fn permit( return Ok(denied("BINDING_MISMATCH")); } let now = Utc::now(); + if !payload + .action + .valid_job_limits(payload.max_runtime_seconds, payload.max_output_bytes) + { + return Ok(denied("INVALID_JOB_LIMITS")); + } if !payload.action.validate() { return Ok(denied("APT_MANIFEST_REQUIRED")); } @@ -397,7 +644,8 @@ pub async fn permit( if !crate::permissions::allowed(&mut tx, &approver, "plans:approve").await? || !crate::permissions::allowed(&mut tx, &plan.principal, "operations:submit").await? || (matches!(payload.action, Action::RunScript { .. }) - && !crate::permissions::allowed(&mut tx, &plan.principal, "scripts:execute").await?) + && (!crate::permissions::allowed(&mut tx, &plan.principal, "scripts:execute").await? + || !crate::permissions::allowed(&mut tx, &approver, "scripts:execute").await?)) { return Ok(denied("AUTHORIZATION_REVOKED")); } @@ -508,9 +756,26 @@ pub async fn next( }; let payload: JobPayload = serde_json::from_slice(&row.get::, _>("payload"))?; let caps: Vec = serde_json::from_value(row.get("capabilities"))?; + if payload.action.windows_msi().is_some() { + let raw: Vec = row.get("plan_payload"); + let plan: PlanPayload = serde_json::from_slice(&raw)?; + if hash(&raw) != row.get::("plan_hash") + || crate::windows_msi_catalog::validate_job_binding(&plan, &payload).is_err() + || crate::catalog::check(&mut tx, &payload.action) + .await + .is_err() + { + tx.commit().await?; + return Ok(None); + } + } + if !payload .action .available_for(&payload.execution_identity, &caps) + || !payload + .action + .valid_job_limits(payload.max_runtime_seconds, payload.max_output_bytes) { tx.commit().await?; return Ok(None); diff --git a/crates/server/src/jobs.rs b/crates/server/src/jobs.rs index 16bec52..2d83716 100644 --- a/crates/server/src/jobs.rs +++ b/crates/server/src/jobs.rs @@ -151,10 +151,24 @@ pub struct Variant { interpreter: String, encoding: String, script_utf8: String, + #[serde( + default, + deserialize_with = "rmm_domain::jobs::deserialize_windows_execution_profile", + skip_serializing_if = "Option::is_none" + )] + #[schemars(with = "WindowsExecutionProfile")] + windows_execution_profile: Option, } #[derive(Clone, Deserialize, Serialize, schemars::JsonSchema)] #[serde(deny_unknown_fields)] struct WebParameters { + #[serde( + default, + deserialize_with = "deserialize_privileged_script_profile", + skip_serializing_if = "Option::is_none" + )] + #[schemars(with = "PrivilegedScriptProfile")] + privileged_script_profile: Option, execution_identity: String, timeout_seconds: u32, max_output_bytes: u32, @@ -174,6 +188,12 @@ struct WebParameters { recipe_version: Option, recipe_id: Option, apt_manifest: Option, + #[serde( + default, + deserialize_with = "rmm_domain::jobs::deserialize_windows_msi_manifest" + )] + #[schemars(with = "rmm_domain::windows_execution::ResolvedWindowsMsi")] + windows_msi_manifest: Option>, installation_context: Option, packages: Option>, reboot_policy: Option, @@ -198,6 +218,9 @@ impl PlanRequest { return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR")); } let p = w.parameters; + if p.windows_msi_manifest.is_some() && w.action != "ensure_package" { + return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR")); + } let required = |value: Option| value.ok_or(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR")); let action = match w.action.as_str() { @@ -213,6 +236,8 @@ impl PlanRequest { script_utf8: v.script_utf8.clone(), arguments: p.arguments.clone().unwrap_or_default(), expected_stdout: p.expected_stdout.clone(), + windows_execution_profile: v.windows_execution_profile, + privileged_script_profile: p.privileged_script_profile, } } "refresh_inventory" => Action::RefreshInventory { @@ -243,6 +268,7 @@ impl PlanRequest { installation_context: required(p.installation_context)?, recipe_id: p.recipe_id, apt_manifest: p.apt_manifest, + windows_msi_manifest: p.windows_msi_manifest, }, "apply_updates" => Action::ApplyUpdates { provider: required(p.provider)?, @@ -310,6 +336,8 @@ fn validate_variants(variants: &[Variant]) -> Result<()> { script_utf8: v.script_utf8.clone(), arguments: vec![], expected_stdout: None, + windows_execution_profile: v.windows_execution_profile, + privileged_script_profile: None, }; if v.encoding != "utf-8" || action.script_platform() != Some(v.os_family.as_str()) @@ -327,6 +355,15 @@ fn capabilities(value: Value) -> Result> { serde_json::from_value(value).map_err(|_| fail("UNSUPPORTED_CAPABILITY")) } fn target_action(input: &PlanInput, caps: &[String]) -> Result { + if !input + .action + .valid_execution_identity(&input.execution_identity) + || (input.execution_identity == "system" + && matches!(input.action, Action::RunScript { .. }) + && !input.action.full_maintenance()) + { + return Err(fail("VALIDATION_ERROR")); + } if input.script_variants.is_empty() { return Ok(input.action.clone()); } @@ -336,12 +373,22 @@ fn target_action(input: &PlanInput, caps: &[String]) -> Result { script_utf8, arguments, expected_stdout, + windows_execution_profile, + privileged_script_profile, } = &input.action else { return Err(fail("VALIDATION_ERROR")); }; let first = &input.script_variants[0]; - if interpreter != &first.interpreter || script_utf8 != &first.script_utf8 { + if interpreter != &first.interpreter + || script_utf8 != &first.script_utf8 + || windows_execution_profile != &first.windows_execution_profile + || (input.execution_identity == "root" + && input + .script_variants + .iter() + .any(|v| v.windows_execution_profile.is_some())) + { return Err(fail("VALIDATION_ERROR")); } let platform = match ( @@ -362,6 +409,8 @@ fn target_action(input: &PlanInput, caps: &[String]) -> Result { script_utf8: v.script_utf8.clone(), arguments: arguments.clone(), expected_stdout: expected_stdout.clone(), + windows_execution_profile: v.windows_execution_profile, + privileged_script_profile: *privileged_script_profile, }) } async fn policy(tx: &mut Transaction<'_, Postgres>) -> Result<()> { @@ -384,6 +433,9 @@ pub async fn plan( if input.targets.asset_ids.is_empty() || input.targets.asset_ids.len() > 100 || !input.action.validate_plan_shape() + || !input + .action + .valid_execution_identity(&input.execution_identity) || !matches!( input.execution_identity.as_str(), "agent_service" | "root" | "system" @@ -416,6 +468,14 @@ pub async fn plan( { return Err(Error(StatusCode::FORBIDDEN, "ROOT_IDENTITY_REQUIRED")); } + if input.action.windows_msi().is_some() + && (input.execution_identity != "system" || !input.artifact_ids.is_empty()) + { + return Err(Error( + StatusCode::BAD_REQUEST, + "WINDOWS_MSI_EXACT_ARTIFACT_REQUIRED", + )); + } // Conservative policy: reboot is always serial, no provider-triggered reboot. if matches!(input.action, Action::Reboot { .. }) && input.rollout.max_parallel != 1 { return Err(fail("POLICY_DENIED")); @@ -439,7 +499,11 @@ pub async fn plan( crate::permissions::require_principal(&mut tx, p, "logs:read").await?; } let recipe_references = crate::catalog::references(&mut tx, &input.action).await?; - let artifacts = crate::catalog::resolve_artifacts(&mut tx, tenant, &input.artifact_ids).await?; + let artifacts = if input.action.windows_msi().is_some() { + crate::windows_msi_catalog::automatic_artifacts(&mut tx, tenant, &input.action).await? + } else { + crate::catalog::resolve_artifacts(&mut tx, tenant, &input.artifact_ids).await? + }; let mut jobs = Vec::new(); for asset in ids { let rows=sqlx::query("SELECT agent_id,enrollment_generation,assignment_version,capabilities FROM agent_installations WHERE asset_id=$1 AND revoked_at IS NULL ORDER BY agent_id FOR SHARE").bind(asset).fetch_all(&mut *tx).await?; @@ -453,6 +517,9 @@ pub async fn plan( let row = &rows[0]; let caps = capabilities(row.get("capabilities"))?; let action = target_action(&input, &caps)?; + if !action.valid_job_limits(input.max_runtime_seconds, input.max_output_bytes) { + return Err(fail("VALIDATION_ERROR")); + } if !action.available_for(&input.execution_identity, &caps) { return Err(fail("UNSUPPORTED_CAPABILITY")); } @@ -509,6 +576,7 @@ pub async fn plan( recipe_references, artifact_references, }; + crate::windows_msi_catalog::validate_plan_bindings(&payload)?; let bytes = encode(&payload)?; let plan_hash = hash(&bytes); sqlx::query("INSERT INTO operation_plans(tenant_id,plan_id,principal,payload,plan_hash,expires_at) VALUES($1,$2,$3,$4,$5,$6)").bind(tenant).bind(payload.plan_id).bind(&p.name).bind(&bytes).bind(&plan_hash).bind(input.schedule.start_before).execute(&mut *tx).await?; @@ -543,6 +611,7 @@ async fn load_plan( Ok((serde_json::from_slice(&bytes)?, h, bytes)) } async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Result<()> { + crate::windows_msi_catalog::validate_plan_bindings(plan)?; policy(tx).await?; let current_artifacts = crate::catalog::resolve_artifacts( tx, @@ -580,6 +649,12 @@ async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Resu } let mut references = Vec::new(); for job in &plan.jobs { + if !job.payload.action.valid_job_limits( + job.payload.max_runtime_seconds, + job.payload.max_output_bytes, + ) { + return Err(fail("VALIDATION_ERROR")); + } if !job.payload.action.validate() { return Err(Error(StatusCode::CONFLICT, "APT_MANIFEST_REQUIRED")); } @@ -769,6 +844,9 @@ async fn authorize_http( return Err(fail("AUTHORIZATION_REVOKED")); } for job in &plan.jobs { + if matches!(job.payload.action, Action::RunScript { .. }) { + crate::permissions::require_principal(&mut tx, &p, "scripts:execute").await?; + } if let Some(permission) = crate::root_policy::execution_permission(&job.payload.execution_identity) { @@ -1004,6 +1082,7 @@ async fn operations_http( #[cfg(test)] mod windows_contract_tests { use super::*; + use chrono::Duration; fn input() -> PlanInput { let tenant = Uuid::new_v4(); let now = Utc::now(); @@ -1035,6 +1114,134 @@ mod windows_contract_tests { i.script_variants.remove(1); assert!(target_action(&i, &["windows.base/1".into()]).is_err()); } + #[test] + fn fixed_windows_log_web_contract_denies_extra_source_parameters() { + let tenant = Uuid::new_v4(); + let now = Utc::now(); + let value = json!({"tenant_id":tenant,"action":"query_logs","targets":{"asset_ids":[Uuid::new_v4()]},"parameters":{"execution_identity":"agent_service","timeout_seconds":30,"max_output_bytes":4096,"unit":WINDOWS_EVENT_LOG_FIXTURE_SOURCE,"since":now-Duration::hours(1),"until":now,"max_lines":1000},"schedule":{"not_before":now,"start_before":now+Duration::minutes(10),"offline_policy":"wait_until_deadline"},"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"fixed source"}); + let input = serde_json::from_value::(value.clone()) + .unwrap() + .resolve(tenant) + .unwrap(); + assert!(input.action.is_windows_event_log_fixture()); + assert!(input.action.validate()); + assert!( + input + .action + .valid_job_limits(input.max_runtime_seconds, input.max_output_bytes) + ); + assert!(input.script_variants.is_empty()); + for field in ["channel", "provider", "xpath", "bookmark"] { + let mut bad = value.clone(); + bad["parameters"][field] = json!("arbitrary"); + assert!( + serde_json::from_value::(bad) + .map(|request| request.resolve(tenant).is_err()) + .unwrap_or(true) + ); + } + } + #[test] + fn full_maintenance_web_profile_is_action_wide_strict_and_identity_bound() { + let request = serde_json::json!({"tenant_id":Uuid::nil(),"action":"run_script","targets":{"asset_ids":[Uuid::nil()]},"parameters":{"execution_identity":"system","timeout_seconds":30,"max_output_bytes":4096,"privileged_script_profile":"full_maintenance_v1","variants":[{"os_family":"windows","interpreter":"powershell","encoding":"utf-8","script_utf8":"Write-Output 'hi'\r\n","windows_execution_profile":"powershell_51_remote_signed"}]},"schedule":{"not_before":"2026-11-14T12:00:00Z","start_before":"2026-11-14T13:00:00Z","offline_policy":"wait_until_deadline"},"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"codec only"}); + let resolved = serde_json::from_value::(request.clone()) + .unwrap() + .resolve(Uuid::nil()) + .unwrap(); + let action = target_action(&resolved, &["windows.base/1".into()]).unwrap(); + assert!(action.full_maintenance()); + let mut invalid = resolved.clone(); + invalid.execution_identity = "agent_service".into(); + assert!(target_action(&invalid, &["windows.base/1".into()]).is_err()); + for value in [ + serde_json::Value::Null, + serde_json::json!("unknown"), + serde_json::json!({"full_maintenance_v1":null}), + ] { + let mut request = request.clone(); + request["parameters"]["privileged_script_profile"] = value; + assert!(serde_json::from_value::(request).is_err()); + } + } + #[test] + fn remote_signed_fixed_variants_bind_profile_without_linux_inheritance() { + let mut i = input(); + let legacy_linux = encode(&i.action).unwrap(); + i.script_variants[1].windows_execution_profile = + Some(WindowsExecutionProfile::PowerShell51RemoteSigned); + assert_eq!( + encode(&target_action(&i, &["linux.base/1".into()]).unwrap()).unwrap(), + legacy_linux + ); + let windows = target_action(&i, &["windows.base/1".into()]).unwrap(); + assert!(matches!( + windows, + Action::RunScript { + windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned), + privileged_script_profile: None, + .. + } + )); + assert!(!windows.available_for( + "agent_service", + &[ + "windows.base/1".into(), + "rmm.sync.v1".into(), + "run_script".into(), + "script.powershell/1".into() + ] + )); + for identity in ["root", "system"] { + i.execution_identity = identity.into(); + assert!(target_action(&i, &["linux.base/1".into()]).is_err()); + } + i.execution_identity = "agent_service".into(); + // Even the first Action must match the optional profile byte-for-byte. + i.script_variants.reverse(); + i.action = windows; + assert!(target_action(&i, &["windows.base/1".into()]).is_ok()); + if let Action::RunScript { + windows_execution_profile, + .. + } = &mut i.action + { + *windows_execution_profile = None; + } + assert!(target_action(&i, &["windows.base/1".into()]).is_err()); + i.script_variants[1].windows_execution_profile = + Some(WindowsExecutionProfile::PowerShell51RemoteSigned); + assert!(validate_variants(&i.script_variants).is_err()); + let schema = schemars::schema_for!(Variant).to_value(); + assert_eq!( + schema["properties"]["windows_execution_profile"], + json!({"$ref":"#/$defs/WindowsExecutionProfile"}) + ); + assert!( + !schema["required"] + .as_array() + .unwrap() + .contains(&json!("windows_execution_profile")) + ); + assert_eq!(schema["$defs"]["WindowsExecutionProfile"]["type"], "string"); + let legacy = json!({"os_family":"windows", "interpreter":"powershell", "encoding":"utf-8", "script_utf8":"hi"}); + let inherited: Variant = serde_json::from_value(legacy.clone()).unwrap(); + assert!(inherited.windows_execution_profile.is_none()); + assert_eq!(serde_json::to_value(inherited).unwrap(), legacy); + for profile in [ + Value::Null, + json!(false), + json!(1), + json!([]), + json!({}), + json!({"powershell_51_remote_signed":null}), + json!("bypass"), + json!("powershell_7_remote_signed"), + ] { + assert!(serde_json::from_value::(json!({"os_family":"windows", "interpreter":"powershell", "encoding":"utf-8", "script_utf8":"hi", "windows_execution_profile":profile})).is_err()); + assert!(serde_json::from_value::(json!({"execution_identity":"agent_service", "timeout_seconds":60, "max_output_bytes":4096, "windows_execution_profile":profile})).is_err()); + } + assert!(serde_json::from_value::(json!({"execution_identity":"agent_service", "timeout_seconds":60, "max_output_bytes":4096, "windows_execution_profile":"powershell_51_remote_signed"})).is_err()); + } #[tokio::test] #[ignore = "requires dedicated isolated PostgreSQL; tests/windows_plan_database.py"] async fn windows_plan_caps_persisted_database() { @@ -1175,6 +1382,75 @@ mod windows_contract_tests { "PLAN_HASH_MISMATCH" ); tx.rollback().await.unwrap(); + // A process-profile plan needs the additional current capability. Its + // presence is rechecked at submit/approval just like interpreter support. + let mut profiled = request.clone(); + profiled.script_variants[1].windows_execution_profile = + Some(WindowsExecutionProfile::PowerShell51RemoteSigned); + assert_eq!( + plan(&state, &principal, tenant, profiled.clone()) + .await + .unwrap_err() + .1, + "UNSUPPORTED_CAPABILITY" + ); + let mut profile_caps = windows_caps.as_array().unwrap().clone(); + profile_caps.push(json!("script.powershell.process_remote_signed/1")); + sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1") + .bind(agents[1]) + .bind(json!(profile_caps)) + .execute(&admin) + .await + .unwrap(); + let view = plan(&state, &principal, tenant, profiled).await.unwrap(); + let profile_id = Uuid::parse_str(view["plan_id"].as_str().unwrap()).unwrap(); + let bytes: Vec = + sqlx::query_scalar("SELECT payload FROM operation_plans WHERE plan_id=$1") + .bind(profile_id) + .fetch_one(&admin) + .await + .unwrap(); + let profile_bound: PlanPayload = serde_json::from_slice(&bytes).unwrap(); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + recheck(&mut tx, &profile_bound).await.unwrap(); + tx.rollback().await.unwrap(); + let mut changed = profile_bound.clone(); + let windows_job = changed + .jobs + .iter_mut() + .find(|j| j.payload.asset_id == assets[1]) + .unwrap(); + if let Action::RunScript { + windows_execution_profile, + .. + } = &mut windows_job.payload.action + { + *windows_execution_profile = None; + } + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert_eq!( + recheck(&mut tx, &changed).await.unwrap_err().1, + "PLAN_HASH_MISMATCH" + ); + tx.rollback().await.unwrap(); + sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1") + .bind(agents[1]) + .bind(&windows_caps) + .execute(&admin) + .await + .unwrap(); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert_eq!( + recheck(&mut tx, &profile_bound).await.unwrap_err().1, + "UNSUPPORTED_CAPABILITY" + ); + tx.rollback().await.unwrap(); request.script_variants.clear(); assert_eq!( plan(&state, &principal, tenant, request.clone()) @@ -1198,6 +1474,298 @@ mod windows_contract_tests { .unwrap() .is_none() ); + let now = Utc::now(); + let mut logs = input(); + logs.targets.asset_ids = vec![assets[1]]; + logs.script_variants.clear(); + logs.max_runtime_seconds = 30; + logs.action = Action::QueryLogs { + unit: WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(), + since: now - Duration::hours(1), + until: now, + max_lines: 1000, + }; + assert_eq!( + plan(&state, &principal, tenant, logs.clone()) + .await + .unwrap_err() + .1, + "FORBIDDEN" + ); + sqlx::query("UPDATE memberships SET permissions=array_append(permissions,'logs:read') WHERE principal=$1").bind(&principal.name).execute(&admin).await.unwrap(); + assert_eq!( + plan(&state, &principal, tenant, logs.clone()) + .await + .unwrap_err() + .1, + "UNSUPPORTED_CAPABILITY" + ); + let mut log_caps = windows_caps.as_array().unwrap().clone(); + log_caps.extend([ + json!("query_logs"), + json!(WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY), + ]); + sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1") + .bind(agents[1]) + .bind(json!(log_caps)) + .execute(&admin) + .await + .unwrap(); + let view = plan(&state, &principal, tenant, logs).await.unwrap(); + let log_id = Uuid::parse_str(view["plan_id"].as_str().unwrap()).unwrap(); + let bytes: Vec = + sqlx::query_scalar("SELECT payload FROM operation_plans WHERE plan_id=$1") + .bind(log_id) + .fetch_one(&admin) + .await + .unwrap(); + let log_bound: PlanPayload = serde_json::from_slice(&bytes).unwrap(); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + recheck(&mut tx, &log_bound).await.unwrap(); + tx.rollback().await.unwrap(); + let pending = submit( + &state, + &principal, + tenant, + log_id, + Submit { + idempotency_key: "fixed-event-log".into(), + }, + ) + .await + .unwrap(); + assert_eq!(pending["approval_state"], "pending_approval"); + assert_eq!(pending["job_count"], 0); + let mut changed = log_bound.clone(); + if let Action::QueryLogs { max_lines, .. } = &mut changed.jobs[0].payload.action { + *max_lines = 999; + } + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert_eq!( + recheck(&mut tx, &changed).await.unwrap_err().1, + "PLAN_HASH_MISMATCH" + ); + tx.rollback().await.unwrap(); + let mut changed = log_bound.clone(); + changed.jobs[0].payload.max_output_bytes = 65537; + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert_eq!( + recheck(&mut tx, &changed).await.unwrap_err().1, + "VALIDATION_ERROR" + ); + tx.rollback().await.unwrap(); + log_caps.retain(|cap| cap != WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY); + sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1") + .bind(agents[1]) + .bind(json!(log_caps)) + .execute(&admin) + .await + .unwrap(); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert_eq!( + recheck(&mut tx, &log_bound).await.unwrap_err().1, + "UNSUPPORTED_CAPABILITY" + ); + tx.rollback().await.unwrap(); + sqlx::query("UPDATE memberships SET permissions=array_remove(permissions,'logs:read') WHERE principal=$1").bind(&principal.name).execute(&admin).await.unwrap(); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert_eq!( + recheck(&mut tx, &log_bound).await.unwrap_err().1, + "FORBIDDEN" + ); + tx.rollback().await.unwrap(); + // MSI metadata only: real migration/RLS/FKs and pending-only planning. + // This ignored test runs alone in the dedicated isolated PostgreSQL process. + // Opaque bytes deliberately are NOT an authored/installed MSI fixture. + use crate::artifact_store::ArtifactStore; + use std::os::unix::fs::PermissionsExt; + let key_path = temp.join("key.pem"); + let policy_path = temp.join("system-msi.json"); + let settings = [ + ("RMM_PROFILE", "development".to_string()), + ("RMM_JOB_KEY_FILE", key_path.display().to_string()), + ("RMM_DEV_SYSTEM_ENABLED", "true".to_string()), + ( + "RMM_DEV_SYSTEM_POLICY_FILE", + policy_path.display().to_string(), + ), + ]; + let previous = settings + .iter() + .map(|(key, _)| (*key, std::env::var_os(key))) + .collect::>(); + for (key, value) in &settings { + unsafe { + std::env::set_var(key, value); + } + } + let artifact_id = Uuid::new_v4(); + let opaque = b"metadata-contract-not-an-executable-msi"; + let artifact_sha = crate::catalog::store() + .unwrap() + .put(tenant, opaque) + .unwrap(); + let manifest = crate::windows_msi_catalog::test_manifest( + artifact_id, + &artifact_sha, + opaque.len() as u64, + ); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + sqlx::query("INSERT INTO artifacts(tenant_id,artifact_id,sha256,size_bytes,media_type) VALUES($1,$2,$3,$4,'application/octet-stream')") + .bind(tenant).bind(artifact_id).bind(&artifact_sha).bind(opaque.len() as i64).execute(&mut *tx).await.unwrap(); + let catalog_id = crate::windows_msi_catalog::store_recipe(&mut tx, tenant, &manifest) + .await + .unwrap(); + tx.commit().await.unwrap(); + let other = Uuid::new_v4(); + sqlx::query("INSERT INTO tenants(tenant_id,name) VALUES($1,'other MSI tenant')") + .bind(other) + .execute(&admin) + .await + .unwrap(); + let mut foreign = rmm_persistence::tenant_tx(&state.pool, other) + .await + .unwrap(); + let count: i64 = sqlx::query_scalar("SELECT count(*) FROM windows_msi_recipes") + .fetch_one(&mut *foreign) + .await + .unwrap(); + assert_eq!(count, 0); + assert!( + crate::windows_msi_catalog::references(&mut foreign, &manifest) + .await + .is_err() + ); + foreign.rollback().await.unwrap(); + let row=sqlx::query("INSERT INTO windows_msi_recipes(tenant_id,catalog_id,recipe_id,recipe_version,recipe_sha256,artifact_id,artifact_sha256,artifact_bytes,payload) VALUES($1,$2,$3,'1',$4,$5,$6,$7,$8)") + .bind(other).bind(Uuid::new_v4()).bind(&manifest.recipe.recipe_id).bind(&manifest.recipe_sha256) + .bind(artifact_id).bind(&artifact_sha).bind(opaque.len() as i64).bind(encode(&manifest.recipe).unwrap()).execute(&admin).await; + assert!(row.is_err()); // database-level composite tenant/artifact FK + assert!( + sqlx::query("UPDATE windows_msi_recipes SET recipe_sha256=$2 WHERE catalog_id=$1") + .bind(catalog_id) + .bind("cd".repeat(32)) + .execute(&admin) + .await + .is_err() + ); + assert!( + sqlx::query("DELETE FROM windows_msi_recipes WHERE catalog_id=$1") + .bind(catalog_id) + .execute(&admin) + .await + .is_err() + ); + sqlx::query("UPDATE memberships SET permissions=array_append(permissions,'system:execute') WHERE tenant_id=$1 AND principal=$2").bind(tenant).bind(&principal.name).execute(&admin).await.unwrap(); + let caps = json!([ + "windows.base/1", + "rmm.sync.v1", + "helper.system.v1", + "ensure_package", + "system:ensure_package", + rmm_domain::windows_execution::MSI_CAPABILITY, + format!("helper.policy.sha256:{}", "ab".repeat(32)), + format!("helper.binary.sha256:{}", "cd".repeat(32)) + ]); + sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1") + .bind(agents[1]) + .bind(&caps) + .execute(&admin) + .await + .unwrap(); + let system = json!({"schema_version":2,"profile":"development","expires_at":Utc::now()+Duration::minutes(10),"scopes":[{ + "tenant_id":tenant,"asset_id":assets[1],"agent_id":agents[1],"enrollment_generation":1,"assignment_version":1, + "principals":[principal.name],"helper_policy_sha256":"ab".repeat(32),"helper_binary_sha256":"cd".repeat(32), + "acceptance_evidence_id":Uuid::new_v4(),"allowed_services":[],"max_runtime_seconds":120,"allowed_msi_recipe_sha256":[manifest.recipe_sha256]}]}); + std::fs::write(&policy_path, encode(&system).unwrap()).unwrap(); + std::fs::set_permissions(&policy_path, std::fs::Permissions::from_mode(0o600)).unwrap(); + let mut package = input(); + package.targets.asset_ids = vec![assets[1]]; + package.script_variants.clear(); + package.execution_identity = "system".into(); + package.action = crate::windows_msi_catalog::test_action(manifest.clone()); + package.max_runtime_seconds = 30; + package.max_output_bytes = 4096; + let mut extra = package.clone(); + extra.artifact_ids = vec![artifact_id]; + assert_eq!( + plan(&state, &principal, tenant, extra).await.unwrap_err().1, + "WINDOWS_MSI_EXACT_ARTIFACT_REQUIRED" + ); + let view = plan(&state, &principal, tenant, package).await.unwrap(); + let package_id = Uuid::parse_str(view["plan_id"].as_str().unwrap()).unwrap(); + let raw: Vec = + sqlx::query_scalar("SELECT payload FROM operation_plans WHERE plan_id=$1") + .bind(package_id) + .fetch_one(&admin) + .await + .unwrap(); + let fixed: PlanPayload = serde_json::from_slice(&raw).unwrap(); + assert_eq!(fixed.artifact_references.len(), 1); + assert_eq!( + fixed.artifact_references[0].artifact.artifact_id, + artifact_id + ); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + recheck(&mut tx, &fixed).await.unwrap(); + tx.rollback().await.unwrap(); + for case in 0..4 { + let mut bad = fixed.clone(); + match case { + 0 => bad.artifact_references.clear(), + 1 => bad + .artifact_references + .push(bad.artifact_references[0].clone()), + 2 => bad.artifact_references[0].artifact.size_bytes += 1, + _ => bad.artifact_references[0].artifact.artifact_id = Uuid::new_v4(), + }; + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert!(recheck(&mut tx, &bad).await.is_err()); + tx.rollback().await.unwrap(); + } + let pending = submit( + &state, + &principal, + tenant, + package_id, + Submit { + idempotency_key: "fixed-msi-metadata".into(), + }, + ) + .await + .unwrap(); + assert_eq!(pending["approval_state"], "pending_approval"); + assert_eq!(pending["job_count"], 0); + sqlx::query("UPDATE memberships SET permissions=array_remove(permissions,'system:execute') WHERE tenant_id=$1 AND principal=$2").bind(tenant).bind(&principal.name).execute(&admin).await.unwrap(); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert_eq!(recheck(&mut tx, &fixed).await.unwrap_err().1, "FORBIDDEN"); + tx.rollback().await.unwrap(); + for (key, value) in previous { + unsafe { + match value { + Some(value) => std::env::set_var(key, value), + None => std::env::remove_var(key), + } + } + } let counts:(i64,i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM jobs WHERE tenant_id=$1),(SELECT count(*) FROM outbox WHERE tenant_id=$1),(SELECT count(*) FROM execution_permits WHERE tenant_id=$1)").bind(tenant).fetch_one(&admin).await.unwrap(); assert_eq!(counts, (0, 0, 0)); let after: Vec = diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index 5e47fc8..042ce15 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -21,6 +21,7 @@ mod root_policy; mod scheduler; mod synchronization; mod tls; +mod windows_msi_catalog; mod windows_policy; use anyhow::{Context, Result, ensure}; diff --git a/crates/server/src/root_policy.rs b/crates/server/src/root_policy.rs index 7c467b3..5b24733 100644 --- a/crates/server/src/root_policy.rs +++ b/crates/server/src/root_policy.rs @@ -28,9 +28,19 @@ struct RootScope { assignment_version: u64, principals: Vec, helper_policy_sha256: String, + #[serde( + default, + deserialize_with = "rmm_domain::jobs::deserialize_optional_sha256" + )] + helper_binary_sha256: Option, acceptance_evidence_id: String, allowed_services: Vec, allow_root_scripts: bool, + #[serde( + default, + deserialize_with = "rmm_domain::jobs::deserialize_script_profiles" + )] + allowed_script_profiles: Option>, allow_reboot: bool, #[serde(default)] apt: Option, @@ -96,6 +106,9 @@ impl RootPolicy { } fn required_permissions(job: &JobPayload, sensitive_log: bool) -> Vec<&'static str> { let mut permissions = vec!["root:execute"]; + if matches!(job.action, Action::RunScript { .. }) { + permissions.push("scripts:execute"); + } if matches!(job.action, Action::AptNativeAcceptance { .. }) { permissions.push("apt:native_acceptance"); } @@ -205,7 +218,21 @@ impl RootScope { && (20..=3600).contains(&job.max_runtime_seconds) && (1..=65536).contains(&job.max_output_bytes) } - Action::RunScript { .. } => self.allow_root_scripts && job.max_runtime_seconds <= 300, + Action::RunScript { .. } => { + self.allow_root_scripts + && job.max_runtime_seconds <= 300 + && job.action.valid_execution_identity(&job.execution_identity) + && (!job.action.full_maintenance() + || (self.helper_binary_sha256.as_ref().is_some_and(|h| { + capabilities.contains(&format!("helper.binary.sha256:{h}")) + }) && self.allowed_script_profiles.as_ref().is_some_and(|v| { + v.contains( + &rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ) + }) && capabilities + .iter() + .any(|c| c == rmm_domain::jobs::FULL_MAINTENANCE_CAPABILITY))) + } Action::SetServiceState { unit, state } => { self.allowed_services.contains(unit) && matches!(state.as_str(), "running" | "stopped") @@ -265,7 +292,12 @@ fn load() -> Result { } let policy: RootPolicy = serde_json::from_slice(&fs::read(path).map_err(|_| disabled())?).map_err(|_| disabled())?; - if policy.schema_version != 1 + if !matches!(policy.schema_version, 1 | 2) + || (policy.schema_version == 1 + && policy + .scopes + .iter() + .any(|s| s.allowed_script_profiles.is_some() || s.helper_binary_sha256.is_some())) || policy.profile != "development" || policy.expires_at <= Utc::now() || policy.scopes.is_empty() @@ -342,7 +374,7 @@ mod tests { fn no_capability_or_relabel_shortcut() { let id = Uuid::new_v4(); let now = Utc::now(); - let job = JobPayload { + let mut job = JobPayload { schema_version: 1, tenant_id: id, asset_id: id, @@ -356,6 +388,8 @@ mod tests { script_utf8: "true\n".into(), arguments: vec![], expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, }, execution_identity: "root".into(), not_before: now, @@ -363,7 +397,7 @@ mod tests { max_runtime_seconds: 60, max_output_bytes: 1024, }; - let scope = RootScope { + let mut scope = RootScope { tenant_id: id, asset_id: id, agent_id: id, @@ -371,9 +405,11 @@ mod tests { assignment_version: 1, principals: vec!["selected".into()], helper_policy_sha256: "ab".repeat(32), + helper_binary_sha256: None, acceptance_evidence_id: "native-test-record".into(), allowed_services: vec![], allow_root_scripts: true, + allowed_script_profiles: None, allow_reboot: false, apt: None, root_logs: None, @@ -388,6 +424,26 @@ mod tests { assert!(scope.permits("selected", &job, &caps, None)); assert!(!scope.permits("automation", &job, &caps, None)); assert!(!scope.permits("selected", &job, &caps[..4], None)); + if let Action::RunScript { + privileged_script_profile, + .. + } = &mut job.action + { + *privileged_script_profile = + Some(rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1); + } + assert!(!scope.permits("selected", &job, &caps, None)); + scope.allowed_script_profiles = Some(vec![ + rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ]); + assert!(!scope.permits("selected", &job, &caps, None)); + let mut caps = caps; + caps.push(rmm_domain::jobs::FULL_MAINTENANCE_CAPABILITY.into()); + assert!(!scope.permits("selected", &job, &caps, None)); + scope.helper_binary_sha256 = Some("cd".repeat(32)); + assert!(!scope.permits("selected", &job, &caps, None)); + caps.push(format!("helper.binary.sha256:{}", "cd".repeat(32))); + assert!(scope.permits("selected", &job, &caps, None)); let mut windows = caps; windows.push("windows.base/1".into()); assert!(!scope.permits("selected", &job, &windows, None)); diff --git a/crates/server/src/windows_msi_catalog.rs b/crates/server/src/windows_msi_catalog.rs new file mode 100644 index 0000000..d273c89 --- /dev/null +++ b/crates/server/src/windows_msi_catalog.rs @@ -0,0 +1,380 @@ +//! Immutable tenant MSI recipe/one-artifact binding. Catalog import is metadata, +//! never job approval, helper provisioning, an installer, or a capability grant. +use crate::{jobs::Error, pagination}; +use axum::{ + Json, + extract::{Path, Query, State}, + http::{HeaderMap, StatusCode}, +}; +use rmm_domain::{ + jobs::{Action, ArtifactReference, JobPayload, PlanPayload, RecipeReference, encode, hash}, + windows_execution::{MSI_PACKAGE_ID, ResolvedWindowsMsi, WindowsMsiRecipe}, +}; +use serde_json::{Value, json}; +use sqlx::{Postgres, Row, Transaction}; +use uuid::Uuid; +fn conflict() -> Error { + Error(StatusCode::CONFLICT, "WINDOWS_MSI_BINDING_MISMATCH") +} +fn reference(m: &ResolvedWindowsMsi) -> RecipeReference { + RecipeReference { + package: MSI_PACKAGE_ID.into(), + version: m.recipe.postcondition.product_version.clone(), + architecture: "x64".into(), + recipe_version: m.recipe.recipe_version.clone(), + sha256: m.recipe_sha256.clone(), + } +} +fn artifact(m: &ResolvedWindowsMsi) -> ArtifactReference { + ArtifactReference { + artifact_id: m.recipe.artifact.artifact_id, + sha256: m.recipe.artifact.sha256.clone(), + size_bytes: m.recipe.artifact.bytes, + } +} +async fn checked( + tx: &mut Transaction<'_, Postgres>, + m: &ResolvedWindowsMsi, +) -> Result<(Uuid, ArtifactReference), Error> { + if !m.validate() { + return Err(Error( + StatusCode::BAD_REQUEST, + "INVALID_WINDOWS_MSI_MANIFEST", + )); + } + // Catalog rows cannot be updated/deleted. Avoid a row lock here: PostgreSQL + // requires UPDATE privilege for FOR SHARE, which this immutable reader lacks. + let row=sqlx::query("SELECT tenant_id,payload,recipe_sha256,artifact_id,artifact_sha256,artifact_bytes FROM windows_msi_recipes WHERE recipe_id=$1 AND recipe_version=$2") + .bind(&m.recipe.recipe_id).bind(&m.recipe.recipe_version).fetch_optional(&mut **tx).await? + .ok_or(Error(StatusCode::CONFLICT,"WINDOWS_MSI_RECIPE_NOT_CONFIGURED"))?; + let bytes: Vec = row.get("payload"); + let a = artifact(m); + if encode(&m.recipe)? != bytes + || hash(&bytes) != m.recipe_sha256 + || row.get::("recipe_sha256") != m.recipe_sha256 + || row.get::("artifact_id") != a.artifact_id + || row.get::("artifact_sha256") != a.sha256 + || row.get::("artifact_bytes") as u64 != a.size_bytes + { + return Err(conflict()); + } + let tenant: Uuid = row.get("tenant_id"); + let actual = crate::catalog::resolve_artifacts(tx, tenant, &[a.artifact_id]).await?; + if actual != [a.clone()] { + return Err(conflict()); + } + Ok((tenant, a)) +} +pub async fn references( + tx: &mut Transaction<'_, Postgres>, + m: &ResolvedWindowsMsi, +) -> Result, Error> { + checked(tx, m).await?; + Ok(vec![reference(m)]) +} +pub async fn automatic_artifacts( + tx: &mut Transaction<'_, Postgres>, + tenant: Uuid, + action: &Action, +) -> Result, Error> { + let m = action.windows_msi().ok_or_else(conflict)?; + if !action.validate() { + return Err(conflict()); + } + let (actual, a) = checked(tx, m).await?; + if actual != tenant { + return Err(conflict()); + } + Ok(vec![a]) +} +/// Set equality/count checks are deliberate. contains()/dedup() would accept +/// missing, additional, or duplicate per-attempt Windows artifact authorizations. +pub fn validate_plan_bindings(plan: &PlanPayload) -> Result<(), Error> { + let mut has_msi = false; + let mut recipes = Vec::new(); + for job in &plan.jobs { + let Some(m) = job.payload.action.windows_msi() else { + continue; + }; + has_msi = true; + recipes.push(reference(m)); + if !job.payload.action.validate() + || job.payload.execution_identity != "system" + || !job.payload.action.valid_job_limits( + job.payload.max_runtime_seconds, + job.payload.max_output_bytes, + ) + || plan.tenant_id != job.payload.tenant_id + || plan + .jobs + .iter() + .filter(|other| { + other.payload.job_id == job.payload.job_id + && other.payload.attempt_id == job.payload.attempt_id + }) + .count() + != 1 + { + return Err(conflict()); + } + let refs = plan + .artifact_references + .iter() + .filter(|r| r.job_id == job.payload.job_id && r.attempt_id == job.payload.attempt_id) + .collect::>(); + if refs.len() != 1 + || refs[0].artifact != artifact(m) + || plan + .recipe_references + .iter() + .filter(|r| **r == reference(m)) + .count() + != 1 + { + return Err(conflict()); + } + } + if has_msi { + recipes.sort(); + recipes.dedup(); + // A package plan resolves one common MSI action for all its targets. + // Never accept extra recipe authority or a mixed-action plan at a later + // delivery/permit/download boundary merely because contains() succeeds. + if recipes != plan.recipe_references + || plan + .jobs + .iter() + .any(|j| j.payload.action.windows_msi().is_none()) + || plan.artifact_references.iter().any(|r| { + !plan + .jobs + .iter() + .any(|j| j.payload.job_id == r.job_id && j.payload.attempt_id == r.attempt_id) + }) + { + return Err(conflict()); + } + } + Ok(()) +} +pub fn validate_job_binding(plan: &PlanPayload, job: &JobPayload) -> Result<(), Error> { + if job.action.windows_msi().is_none() { + return Ok(()); + } + validate_plan_bindings(plan)?; + let bytes = encode(job)?; + let matches = plan + .jobs + .iter() + .filter(|j| j.payload.job_id == job.job_id && j.payload.attempt_id == job.attempt_id) + .collect::>(); + if matches.len() != 1 + || matches[0].job_payload_hash != hash(&bytes) + || encode(&matches[0].payload)? != bytes + { + return Err(conflict()); + } + Ok(()) +} +pub(crate) async fn store_recipe( + tx: &mut Transaction<'_, Postgres>, + t: Uuid, + m: &ResolvedWindowsMsi, +) -> Result { + if !m.validate() { + return Err(Error( + StatusCode::BAD_REQUEST, + "INVALID_WINDOWS_MSI_MANIFEST", + )); + } + let actual = crate::catalog::resolve_artifacts(tx, t, &[m.recipe.artifact.artifact_id]).await?; + if actual != [artifact(m)] { + return Err(conflict()); + } + let bytes = encode(&m.recipe)?; + if bytes.len() > 65536 { + return Err(Error(StatusCode::PAYLOAD_TOO_LARGE, "QUOTA_EXCEEDED")); + } + let id = Uuid::new_v4(); + sqlx::query("INSERT INTO windows_msi_recipes(tenant_id,catalog_id,recipe_id,recipe_version,recipe_sha256,artifact_id,artifact_sha256,artifact_bytes,payload) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)") + .bind(t).bind(id).bind(&m.recipe.recipe_id).bind(&m.recipe.recipe_version).bind(&m.recipe_sha256) + .bind(m.recipe.artifact.artifact_id).bind(&m.recipe.artifact.sha256).bind(m.recipe.artifact.bytes as i64).bind(bytes).execute(&mut **tx).await?; + Ok(id) +} +pub async fn import( + State(s): State, + h: HeaderMap, + Path(t): Path, + Json(m): Json, +) -> Result, Error> { + let (p, mut tx) = crate::catalog::context(&s, &h, t, true).await?; + let id = store_recipe(&mut tx, t, &m).await?; + sqlx::query("INSERT INTO audit_events VALUES($1,$2,$3,'windows_msi_recipe_imported',$4,now())") + .bind(t) + .bind(Uuid::new_v4()) + .bind(p.name) + .bind( + json!({"catalog_id":id,"recipe_id":m.recipe.recipe_id,"recipe_sha256":m.recipe_sha256, + "artifact_id":m.recipe.artifact.artifact_id,"testsimulation":p.testsimulation}), + ) + .execute(&mut *tx) + .await?; + tx.commit().await?; + Ok(Json( + json!({"catalog_id":id,"recipe_id":m.recipe.recipe_id,"sha256":m.recipe_sha256,"manifest":m}), + )) +} +pub async fn list( + State(s): State, + h: HeaderMap, + Path(t): Path, + Query(query): Query, +) -> Result, Error> { + let (p, mut tx) = crate::catalog::context(&s, &h, t, false).await?; + let mut page = pagination::Cursor::parse(&s, &p, "catalog:windows-msi:catalog_id:asc", query) + .map_err(|status| Error(status, "VALIDATION_ERROR"))?; + let rows=sqlx::query("SELECT catalog_id,payload,recipe_sha256 FROM windows_msi_recipes WHERE catalog_id>$1 AND created_at<=$2 ORDER BY catalog_id LIMIT $3") + .bind(page.last).bind(page.as_of).bind(i64::from(page.limit)+1).fetch_all(&mut *tx).await?; + let truncated = rows.len() > usize::from(page.limit); + let next = if truncated { + Some( + page.next(&s, rows[usize::from(page.limit) - 1].get("catalog_id")) + .map_err(|status| Error(status, "VALIDATION_ERROR"))?, + ) + } else { + None + }; + let mut items = Vec::new(); + for row in rows.iter().take(usize::from(page.limit)) { + let bytes: Vec = row.get("payload"); + let m = ResolvedWindowsMsi { + recipe: serde_json::from_slice::(&bytes)?, + recipe_sha256: row.get("recipe_sha256"), + }; + if !m.validate() || encode(&m.recipe)? != bytes { + return Err(conflict()); + } + items.push(json!({"catalog_id":row.get::("catalog_id"),"recipe_id":m.recipe.recipe_id,"sha256":m.recipe_sha256,"manifest":m})); + } + tx.commit().await?; + Ok(Json( + json!({"items":items,"next_cursor":next,"completeness":if truncated{"partial"}else{"complete"},"truncated":truncated,"as_of":page.as_of}), + )) +} + +#[cfg(test)] +pub(crate) fn test_manifest(id: Uuid, sha: &str, bytes: u64) -> ResolvedWindowsMsi { + use rmm_domain::windows_execution::*; + let recipe = WindowsMsiRecipe { + schema_version: 1, + recipe_id: MSI_RECIPE_ID.into(), + recipe_version: "1".into(), + platform: "windows".into(), + upgrade_code: MSI_UPGRADE_CODE.into(), + architecture: Architecture::X64, + context: InstallContext::Machine, + command_profile: MsiCommandProfile::FixtureQuietNoRebootV1, + artifact: MsiArtifact { + artifact_id: id, + sha256: sha.into(), + bytes, + }, + postcondition: MsiPostcondition { + product_code: MSI_PRODUCT_CODE.into(), + product_version: MSI_VERSION.into(), + payload_path: MSI_PAYLOAD.into(), + payload_sha256: MSI_PAYLOAD_10_SHA256.into(), + payload_bytes: MSI_PAYLOAD_BYTES, + }, + no_reboot: true, + package_code: None, + predecessor: None, + }; + ResolvedWindowsMsi { + recipe_sha256: recipe.sha256().unwrap(), + recipe, + } +} +#[cfg(test)] +pub(crate) fn test_action(m: ResolvedWindowsMsi) -> Action { + Action::EnsurePackage { + provider: "msi".into(), + package: MSI_PACKAGE_ID.into(), + version: m.recipe.postcondition.product_version.clone(), + architecture: "x64".into(), + recipe_version: m.recipe.recipe_version.clone(), + installation_context: "machine".into(), + recipe_id: Some(m.recipe.recipe_id.clone()), + apt_manifest: None, + windows_msi_manifest: Some(Box::new(m)), + } +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn exact_per_attempt_artifact_set_never_deduplicates_missing_extra_or_foreign_authority() { + use rmm_domain::jobs::{JobArtifactReference, PlannedJob}; + let id = Uuid::new_v4(); + let now = chrono::Utc::now(); + let m = test_manifest(Uuid::new_v4(), &"ab".repeat(32), 32); + let job = JobPayload { + schema_version: 1, + tenant_id: id, + asset_id: id, + agent_id: id, + enrollment_generation: 1, + assignment_version: 1, + job_id: Uuid::new_v4(), + attempt_id: Uuid::new_v4(), + action: test_action(m.clone()), + execution_identity: "system".into(), + not_before: now, + start_before: now + chrono::Duration::minutes(1), + max_runtime_seconds: 30, + max_output_bytes: 4096, + }; + let plan = PlanPayload { + schema_version: 1, + plan_id: Uuid::new_v4(), + tenant_id: id, + principal: "test".into(), + reason: "metadata only".into(), + jobs: vec![PlannedJob { + job_payload_hash: hash(&encode(&job).unwrap()), + payload: job.clone(), + script_sha256: None, + }], + max_parallel: 1, + halt_after_failures: 1, + recipe_references: vec![reference(&m)], + artifact_references: vec![JobArtifactReference { + job_id: job.job_id, + attempt_id: job.attempt_id, + artifact: artifact(&m), + }], + }; + validate_job_binding(&plan, &job).unwrap(); + for case in 0..9 { + let mut bad = plan.clone(); + match case { + 0 => bad.artifact_references.clear(), + 1 => bad + .artifact_references + .push(bad.artifact_references[0].clone()), + 2 => bad.artifact_references[0].artifact.artifact_id = Uuid::new_v4(), + 3 => bad.artifact_references[0].artifact.size_bytes += 1, + 4 => bad.artifact_references[0].attempt_id = Uuid::new_v4(), + 5 => bad.jobs[0].payload.max_output_bytes += 1, + 6 => bad.recipe_references.clear(), + 7 => bad.recipe_references.push(bad.recipe_references[0].clone()), + _ => { + let mut extra = bad.recipe_references[0].clone(); + extra.sha256 = "ef".repeat(32); + bad.recipe_references.push(extra); + } + } + assert!(validate_job_binding(&bad, &job).is_err()); + } + } +} diff --git a/crates/server/src/windows_policy.rs b/crates/server/src/windows_policy.rs index 6cef667..d65b5fb 100644 --- a/crates/server/src/windows_policy.rs +++ b/crates/server/src/windows_policy.rs @@ -37,7 +37,20 @@ struct SystemScope { // Administrative evidence pin, never an approval or authorization ID. acceptance_evidence_id: Uuid, allowed_services: Vec, + #[serde(default, deserialize_with = "strict_optional_hashes")] + allowed_msi_recipe_sha256: Option>, max_runtime_seconds: u32, + #[serde( + default, + deserialize_with = "rmm_domain::jobs::deserialize_script_profiles" + )] + allowed_script_profiles: Option>, +} +fn strict_optional_hashes<'de, D>(deserializer: D) -> Result>, D::Error> +where + D: serde::Deserializer<'de>, +{ + Vec::::deserialize(deserializer).map(Some) } fn digest(value: &str) -> bool { value.len() == 64 @@ -86,9 +99,32 @@ impl SystemScope { .collect::>() .len() == self.allowed_services.len() + && self + .allowed_msi_recipe_sha256 + .as_ref() + .is_none_or(|hashes| { + hashes.len() <= 2 + && hashes.iter().all(|h| digest(h)) + && hashes + .iter() + .collect::>() + .len() + == hashes.len() + }) + && self + .allowed_script_profiles + .as_deref() + .is_none_or(|v| v == [rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]) && (1..=120).contains(&self.max_runtime_seconds) } - fn permits(&self, principal: &str, job: &JobPayload, capabilities: &[String]) -> bool { + fn permits( + &self, + principal: &str, + job: &JobPayload, + capabilities: &[String], + allow_msi: bool, + allow_scripts: bool, + ) -> bool { let has = |cap: &str| capabilities.iter().any(|c| c == cap); self.valid() && job.execution_identity == "system" @@ -110,17 +146,59 @@ impl SystemScope { "helper.binary.sha256:{}", self.helper_binary_sha256 )) - && has("system:set_service_state") - && has("set_service_state") && (1..=self.max_runtime_seconds).contains(&job.max_runtime_seconds) && (1..=65536).contains(&job.max_output_bytes) - && matches!(&job.action,Action::SetServiceState{unit,state} - if self.allowed_services.contains(unit) && matches!(state.as_str(),"running"|"stopped")) + && match &job.action { + Action::SetServiceState { unit, state } => { + self.allowed_services.contains(unit) + && matches!(state.as_str(), "running" | "stopped") + && has("system:set_service_state") + && has("set_service_state") + } + Action::RunScript { .. } => { + allow_scripts + && job.action.full_maintenance() + && self.allowed_script_profiles.as_ref().is_some_and(|v| { + v.contains( + &rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ) + }) + && job.action.validate() + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes) + && job.action.available_for("system", capabilities) + } + _ if allow_msi => job.action.windows_msi().is_some_and(|m| { + job.action.validate() + && job + .action + .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes) + && self + .allowed_msi_recipe_sha256 + .as_ref() + .is_some_and(|hashes| hashes.contains(&m.recipe_sha256)) + && has("ensure_package") + && has("system:ensure_package") + && has(rmm_domain::windows_execution::MSI_CAPABILITY) + }), + _ => false, + } } } impl SystemPolicy { fn valid(&self, now: DateTime) -> bool { - self.schema_version == 1 + matches!(self.schema_version, 1..=3) + && (self.schema_version >= 2 + || self + .scopes + .iter() + .all(|scope| scope.allowed_msi_recipe_sha256.is_none())) + && (self.schema_version == 3 + || self + .scopes + .iter() + .all(|scope| scope.allowed_script_profiles.is_none())) && self.profile == "development" && self.expires_at > now && !self.scopes.is_empty() @@ -135,10 +213,15 @@ impl SystemPolicy { now: DateTime, ) -> bool { self.valid(now) - && self - .scopes - .iter() - .any(|scope| scope.permits(principal, job, capabilities)) + && self.scopes.iter().any(|scope| { + scope.permits( + principal, + job, + capabilities, + self.schema_version >= 2, + self.schema_version == 3, + ) + }) } } fn disabled() -> Error { @@ -261,6 +344,9 @@ pub async fn authorize( } // Independent DB permission; root:execute never implies this scope. crate::permissions::require(tx, principal, "system:execute").await?; + if matches!(job.action, Action::RunScript { .. }) { + crate::permissions::require(tx, principal, "scripts:execute").await?; + } let policy = load()?; let caps:serde_json::Value=sqlx::query_scalar("SELECT capabilities FROM agent_installations WHERE tenant_id=$1 AND agent_id=$2 AND asset_id=$3 AND revoked_at IS NULL AND enrollment_generation=$4 AND assignment_version=$5").bind(job.tenant_id).bind(job.agent_id).bind(job.asset_id).bind(i64::try_from(job.enrollment_generation).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?).bind(i64::try_from(job.assignment_version).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?).fetch_optional(&mut **tx).await?.ok_or(Error(StatusCode::CONFLICT,"AUTHORIZATION_REVOKED"))?; let capabilities: Vec = serde_json::from_value(caps)?; @@ -373,6 +459,42 @@ mod tests { assert!(!policy.permits("selected", &job, &caps, policy.expires_at)); } #[test] + fn full_script_requires_v3_explicit_scope_and_does_not_inherit_scm_or_msi() { + let (mut policy, mut job, mut caps) = fixture(); + job.action = Action::RunScript { + interpreter: "powershell".into(), + script_utf8: "Write-Output 'hi'\r\n".into(), + arguments: vec![], + expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: Some( + rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ), + }; + caps.extend([ + "run_script".into(), + "system:run_script".into(), + "script.powershell/1".into(), + rmm_domain::jobs::FULL_MAINTENANCE_CAPABILITY.into(), + ]); + for version in [1, 2, 3] { + policy.schema_version = version; + assert!(!policy.permits("selected", &job, &caps, Utc::now())); + } + policy.scopes[0].allowed_script_profiles = Some(vec![ + rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, + ]); + for version in [1, 2] { + policy.schema_version = version; + assert!(!policy.permits("selected", &job, &caps, Utc::now())); + } + policy.schema_version = 3; + assert!(policy.permits("selected", &job, &caps, Utc::now())); + assert!(!policy.permits("foreign", &job, &caps, Utc::now())); + caps.retain(|c| c != rmm_domain::jobs::FULL_MAINTENANCE_CAPABILITY); + assert!(!policy.permits("selected", &job, &caps, Utc::now())); + } + #[test] fn narrow_scm_ceiling_denies_unreviewed_effects_and_provider_inputs() { let (mut policy, mut job, caps) = fixture(); let now = Utc::now(); @@ -393,6 +515,8 @@ mod tests { script_utf8: "Write-Output 'hello'".into(), arguments: vec![], expected_stdout: None, + windows_execution_profile: None, + privileged_script_profile: None, }; assert!(!policy.permits("selected", &job, &caps, now)); job.action = Action::SetServiceState { @@ -409,6 +533,29 @@ mod tests { assert!(serde_json::from_value::(invalid).is_err()); } #[test] + fn only_explicit_v2_hash_scope_and_package_caps_allow_fixed_msi() { + let (mut policy, mut job, mut caps) = fixture(); + let m = crate::windows_msi_catalog::test_manifest(Uuid::new_v4(), &"ab".repeat(32), 32); + job.action = crate::windows_msi_catalog::test_action(m.clone()); + caps.extend([ + "ensure_package".into(), + "system:ensure_package".into(), + rmm_domain::windows_execution::MSI_CAPABILITY.into(), + ]); + assert!(!policy.permits("selected", &job, &caps, Utc::now())); + policy.scopes[0].allowed_msi_recipe_sha256 = Some(vec![m.recipe_sha256.clone()]); + assert!(!policy.permits("selected", &job, &caps, Utc::now())); // v1 remains SCM-only + policy.schema_version = 2; + assert!(policy.permits("selected", &job, &caps, Utc::now())); + for index in caps.len() - 3..caps.len() { + let mut short = caps.clone(); + short.remove(index); + assert!(!policy.permits("selected", &job, &short, Utc::now())); + } + policy.scopes[0].allowed_msi_recipe_sha256 = Some(vec!["cd".repeat(32)]); + assert!(!policy.permits("selected", &job, &caps, Utc::now())); + } + #[test] fn policy_file_refuses_symlinks_public_mode_wrong_owner_and_writable_parent() { use std::os::unix::fs::symlink; let directory = std::env::temp_dir().join(format!("rmm-windows-policy-{}", Uuid::new_v4())); diff --git a/deploy/cloud_script_console_upgrade_v1.py b/deploy/cloud_script_console_upgrade_v1.py new file mode 100644 index 0000000..41e9c1b --- /dev/null +++ b/deploy/cloud_script_console_upgrade_v1.py @@ -0,0 +1,692 @@ +#!/usr/bin/env python3 +"""One frozen Script1.20/MSI1.19 server upgrade; no jobs, approvals or policies. + +prepare is offline. inspect/status are remote read-only. build/deploy each launch +one durable, create-once case worker. Lost replies permit status only, not retry. +The notebook never runs an RMM server/client. Existing deployment tools remain +unchanged. See docs/script-console-cloud-upgrade-v1.md before actual use. +""" +import argparse +import base64 +import hashlib +import inspect +import io +import json +import os +from pathlib import Path +import re +import tarfile +import uuid + +import remote_client_bootstrap as bootstrap + +ROOT = Path(__file__).resolve().parents[1] +STATE = ROOT / 'devstate/script-console-cloud-upgrade-v1' +FROZEN = ROOT / 'devstate/script-console-qualification/run-29461892-343f-42f4-b469-88703535425e/manifest.json' +FROZEN_SHA = 'ddd1a78d1cd289b02b1f98572f9652171ec00be84e01ce099fd722d689bcaacd' +OLD_IMAGE = 'sha256:25697f948325a11f755893e16ec5be2da138c3333c8e60f75a18f973d6ac26e2' +EXTRA = { + 'deploy/Dockerfile': 'd41b4ec902c11a9e25c22b1c1d3d60d15afc207f2598ecf559cf3bdae4f8e782', + '.dockerignore': 'fdb5cba5967249f5dac35d45c5edf0be2a4e1594c94d04a2ed416d89821ace75', + 'deploy/server.compose.yaml': 'd2044b0ee6d584f360990635bec149511d5a0623ec562976541900d751998818', +} +WEB = { + 'index.html': '95d6f9058cc713c6af36fbd22ccc60678f081765a853b53d19ac6f0d219804f8', + 'assets/index-Be1zm1OZ.css': '916e8870d4e9a9553de452b3ff6c71671bb6a7a22355de0aff432db516a9f3e7', + 'assets/index-CTgvl2jr.js': 'f611662042eb4cc44f25c7d5ddc0d2d0562e32be267d3d5fb1db31b0d310f879', +} +SUPPORT = { + 'deploy/remote_client_bootstrap.py': '785c43a51dfe3f93e39798dbd7611c353752ae2fc64b22e41ba168e221620d30', + 'deploy/vm_access_check.py': '5c8107ea9151a9f498b2530cdc61c1a7a6d745aff853a9b05710f3395838c0dd', +} +require = bootstrap.require + + +def sha(raw): + return hashlib.sha256(raw).hexdigest() + + +def encoded(value): + return json.dumps(value, sort_keys=True, separators=(',', ':')).encode() + + +def create(path, raw): + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) + with os.fdopen(fd, 'wb') as stream: + stream.write(raw) + stream.flush() + os.fsync(stream.fileno()) + fd = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + try: + os.fsync(fd) + finally: + os.close(fd) + + +def remote(payload): + """Self-contained function transported by the existing strict-hostkey RPC.""" + import datetime as dt + import fcntl + import http.client + import io + import platform + import socket + import ssl + import stat + import subprocess + import tarfile + import time + import traceback + os.umask(0o077) + root = Path('/opt/rust-discovery') + action = payload['action'] + require(os.geteuid() == 0 and platform.machine() == 'aarch64', 'fixed root ARM64 server') + require(action in ('inspect', 'build', 'build-worker', 'deploy', 'deploy-worker', 'status'), 'fixed upgrade action') + ident = str(uuid.UUID(payload['id'])) + require(ident == payload['id'], 'canonical case UUID') + folder = root / 'devstate' / ('script-console-upgrade-v1-' + ident) + compose = ['docker', 'compose', '--env-file', str(root / 'devstate/.env'), + '-f', str(root / 'deploy/server.compose.yaml')] + + def digest(raw): + return hashlib.sha256(raw).hexdigest() + + def enc(value): + return json.dumps(value, sort_keys=True, separators=(',', ':')).encode() + + def run(args, timeout=20, **kwargs): + result = subprocess.run(args, capture_output=True, timeout=timeout, **kwargs) + require(result.returncode == 0, 'fixed server command incomplete; inspect private case') + return result.stdout + + def owned(path, directory=False): + value = path.lstat() + require(value.st_uid == 0 and not value.st_mode & 0o022 and not path.is_symlink() + and (stat.S_ISDIR(value.st_mode) if directory else stat.S_ISREG(value.st_mode)), 'root-owned fixed path') + return value + + def raw(path, limit=2 * 1024 * 1024): + value = owned(path) + require(value.st_size <= limit and value.st_nlink == 1, 'bounded private regular file') + return path.read_bytes() + + def syncdir(path): + fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + try: + os.fsync(fd) + finally: + os.close(fd) + + def once(path, data, mode=0o600): + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode) + with os.fdopen(fd, 'wb') as stream: + stream.write(data) + stream.flush() + os.fchmod(stream.fileno(), mode) + os.fsync(stream.fileno()) + syncdir(path.parent) + + def load(path): + return json.loads(raw(path, 8 * 1024 * 1024)) + + def image(tag): + # Separate fields avoid ever retrieving environment variables/secrets. + value = run(['docker', 'image', 'inspect', tag, '--format', '{{.Id}} {{.Architecture}} {{.Os}}']).decode().split() + require(len(value) == 3 and value[1:] == ['arm64', 'linux'] and value[0].startswith('sha256:'), 'native ARM64 image') + return value[0] + + def closed_root_policy(): + # Existing MVP state: flag=true, but this exact historical policy is + # expired. Neither the flag nor the policy is changed by deployment. + directory = root / 'devstate/root-policy' + path = directory / 'policy.json' + parent = directory.lstat(); value = path.lstat() + require(not directory.is_symlink() and stat.S_ISDIR(parent.st_mode) + and parent.st_uid == 10001 and not parent.st_mode & 0o022 + and not path.is_symlink() and stat.S_ISREG(value.st_mode) + and value.st_uid == 10001 and stat.S_IMODE(value.st_mode) == 0o600 + and value.st_nlink == 1 and 0 < value.st_size <= 65536, 'fixed protected closed Root policy path') + data = path.read_bytes() + expected = 'cf15cbb68b590475df6def28e7dcffb7d88e0ca2d2555a06174fb654eeff05f4' + require(digest(data) == expected, 'exact historical closed Root policy hash') + policy = json.loads(data) + require(isinstance(policy, dict) and set(policy) == {'schema_version', 'profile', 'expires_at', 'scopes'} + and type(policy['schema_version']) is int and policy['schema_version'] == 1 + and policy['profile'] == 'development' and isinstance(policy['expires_at'], str) + and isinstance(policy['scopes'], list) and 1 <= len(policy['scopes']) <= 64 + and all(isinstance(scope, dict) for scope in policy['scopes']), 'exact legacy Root policy schema') + expiry = dt.datetime.fromisoformat(policy['expires_at']) + require(expiry.utcoffset() is not None and expiry <= dt.datetime.now(dt.timezone.utc), 'expired Root policy denies every scope') + return {'sha256': expected, 'schema_version': 1, 'profile': 'development', + 'expires_at': policy['expires_at'], 'closed': True} + + def container(service): + values = run(compose + ['ps', '-a', '-q', service]).decode().split() + require(len(values) == 1, 'one fixed compose service') + cid = values[0] + out = {'id': cid} + for name, fmt in (('image', '.Image'), ('user', '.Config.User'), ('running', '.State.Running'), + ('mounts', '.Mounts'), ('ports', '.HostConfig.PortBindings')): + out[name] = json.loads(run(['docker', 'inspect', cid, '--format', '{{json ' + fmt + '}}'])) + if service in ('api', 'gateway'): + # Filter inside Docker's template: credential env values never + # enter Python/stdout. No .env, token or private key is opened. + names = ('RMM_PROFILE', 'RMM_DEV_AUTH', 'RMM_SERVER_ROLE', 'RMM_UI_ORIGIN', + 'RMM_DEV_ROOT_ENABLED', 'RMM_DEV_SYSTEM_ENABLED', 'RMM_DEV_DELIVERY_FAULTS', + 'RMM_DEV_DELIVERY_FAULTS_FILE', 'RMM_DEV_SYSTEM_POLICY_FILE', 'RMM_DEV_ROOT_POLICY_FILE') + select = ' '.join('(eq $key "' + name + '")' for name in names) + fmt = '{{range .Config.Env}}{{$key := index (split . "=") 0}}{{if or ' + select + '}}{{println .}}{{end}}{{end}}' + env = dict(line.split('=', 1) for line in run(['docker', 'inspect', cid, '--format', fmt]).decode().splitlines() if '=' in line) + out['settings'] = {key: env.get(key) for key in names} + require(env.get('RMM_PROFILE') == 'development' and env.get('RMM_DEV_AUTH') == 'true' + and env.get('RMM_SERVER_ROLE') == service and env.get('RMM_UI_ORIGIN') == 'https://localhost:18443' + and env.get('RMM_DEV_ROOT_ENABLED') == 'true' + and env.get('RMM_DEV_ROOT_POLICY_FILE') == '/root-policy/policy.json' + and env.get('RMM_DEV_SYSTEM_ENABLED') is None and env.get('RMM_DEV_DELIVERY_FAULTS') == 'false' + and env.get('RMM_DEV_DELIVERY_FAULTS_FILE') is None and env.get('RMM_DEV_SYSTEM_POLICY_FILE') is None, + 'unchanged private dev/test role with Root/SYSTEM/fault gates closed') + mounts = [mount for mount in out['mounts'] if mount['Destination'] == '/root-policy'] + require(len(mounts) == 1 and mounts[0]['Type'] == 'bind' and mounts[0]['RW'] is False + and mounts[0]['Source'] == str(root / 'devstate/root-policy'), 'exact read-only Root policy mount') + out['closed_root_policy_sha256'] = closed_root_policy()['sha256'] + require(out['user'] == '10001:10001', 'unprivileged application role') + port = '18443' if service == 'api' else '18444' + require(out['ports'] == {'18443/tcp': [{'HostIp': '127.0.0.1', 'HostPort': port}]}, 'loopback-only role') + else: + require(not out['ports'], 'PostgreSQL has no published port') + return out + + def controls(): + names = ('deploy/Dockerfile', '.dockerignore', 'deploy/server.compose.yaml') + files = {name: digest(raw(root / name)) for name in names} + for name in ('rmm-server-ingress.socket', 'rmm-server-ingress.service'): + files['/etc/systemd/system/' + name] = digest(raw(Path('/etc/systemd/system') / name)) + # Metadata-only identity of secret trees. Private material is never read. + identities = {} + for name in ('devstate/.env', 'devstate/pki', 'devstate/api-pki', 'devstate/jobs', 'devstate/root-policy'): + path = root / name + paths = [path, *sorted(path.rglob('*'))] if path.is_dir() else [path] + for item in paths: + value = item.lstat() + require(not item.is_symlink() and (stat.S_ISREG(value.st_mode) or stat.S_ISDIR(value.st_mode)), 'preserved state object') + identities[str(item.relative_to(root))] = [value.st_dev, value.st_ino, value.st_uid, value.st_gid, + value.st_mode, value.st_size if stat.S_ISREG(value.st_mode) else None, + value.st_mtime_ns if stat.S_ISREG(value.st_mode) else None] + ingress = run(['systemctl', 'show', 'rmm-server-ingress.socket', '-p', 'ActiveState', '-p', 'UnitFileState']).decode() + require('ActiveState=active' in ingress, 'existing public socket ingress stays active') + return {'files': files, 'protected_metadata': identities, 'ingress': ingress, + 'root_policy': closed_root_policy()} + + def sql(query): + return json.loads(run(compose + ['exec', '-T', 'postgres', 'psql', '-X', '-qAt', '-v', + 'ON_ERROR_STOP=1', '-U', 'postgres', '-d', 'rmm'], timeout=30, input=query.encode())) + + def database(): + # One repeatable-read snapshot, only counts and SHA256 values leave PG. + tables = ('tenants', 'assets', 'operation_plans', 'approval_requests', 'authorizations', 'operations', + 'jobs', 'outbox', 'execution_permits', 'job_receipts', 'audit_events', 'operation_submissions', + 'live_query_requests', 'memberships', 'tenant_limits', 'oidc_principals', 'package_recipes', 'artifacts') + pairs = [] + for table in tables: + pairs.append("'" + table + "',(SELECT json_build_object('count',count(*),'sha256',encode(sha256(convert_to(" + "coalesce(string_agg(to_jsonb(t)::text,E'\\n' ORDER BY to_jsonb(t)::text),''),'UTF8')),'hex')) FROM " + table + ' t)') + # Stable device identity excludes expected changing passive Sync/contact. + identity = "(SELECT coalesce(json_agg(q ORDER BY tenant_id,agent_id),'[]'::json) FROM (SELECT tenant_id,agent_id,asset_id,enrollment_generation,assignment_version,certificate_sha256,revoked_at FROM agent_installations) q)" + migrations = "(SELECT json_agg(q ORDER BY version) FROM (SELECT version,description,installed_on,success,encode(checksum,'hex') AS checksum,execution_time FROM _sqlx_migrations) q)" + role = "(SELECT json_build_object('superuser',rolsuper,'bypassrls',rolbypassrls) FROM pg_roles WHERE rolname='rmm_app')" + query = "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY; SELECT json_build_object('history',json_build_object(" + ','.join(pairs) + "),'identities'," + identity + ",'migrations'," + migrations + ",'app_role'," + role + ",'unresolved',(SELECT count(*) FROM jobs WHERE state NOT IN ('succeeded','failed','cancelled','expired'))); COMMIT;" + out = sql(query) + require(out['unresolved'] == 0 and out['app_role'] == {'superuser': False, 'bypassrls': False}, 'settled history and no RLS bypass') + require(all(row['success'] for row in out['migrations']), 'successful SQLx ledger') + return out + + def witness(running=True): + current = {'runtime_image': image('rust-discovery-server:local'), + 'controls': controls(), 'containers': {name: container(name) for name in ('postgres', 'api', 'gateway')}, + 'index_sha256': digest(raw(root / 'web/dist/index.html')), 'database': database()} + pg = current['containers']['postgres'] + mounts = [m for m in pg['mounts'] if m['Destination'] == '/var/lib/postgresql/data'] + require(len(mounts) == 1 and mounts[0]['Type'] == 'volume' and pg['running'], 'existing PG data volume') + value = Path(mounts[0]['Source']).lstat() + require(stat.S_ISDIR(value.st_mode) and not Path(mounts[0]['Source']).is_symlink(), 'same PG data directory') + current['pg_volume'] = {'mount': mounts[0], 'device': value.st_dev, 'inode': value.st_ino} + for name in ('api', 'gateway'): + require(current['containers'][name]['running'] == running, 'fixed application running state') + require(current['containers'][name]['image'] == current['runtime_image'], 'both services run runtime tag') + return current + + def before_guard(now, expected, stopped=False): + require(now['runtime_image'] == expected['runtime_image'] == payload['old_image'], 'runtime image CAS') + for key in ('controls', 'index_sha256', 'database', 'pg_volume'): + require(now[key] == expected[key], 'preserved ' + key + ' CAS') + for name in ('postgres', 'api', 'gateway'): + current = dict(now['containers'][name]) + old = dict(expected['containers'][name]) + if stopped and name != 'postgres': + current.pop('running'); old.pop('running') + require(current == old, 'fixed prior service/container CAS') + + def migration_guard(record, after=False): + expected = record['migration_checksums'] + require([row['version'] for row in record['baseline']['database']['migrations']] == list(range(1, 14)), 'exact prior migrations 1..13') + for row in record['baseline']['database']['migrations']: + require(row['checksum'] == expected[str(row['version'])], 'frozen old migration checksum') + if after: + now = database() + require(now['migrations'][:13] == record['baseline']['database']['migrations'] + and [row['version'] for row in now['migrations']] == list(range(1, 15)) + and now['migrations'][13]['checksum'] == expected['14'], 'only additive frozen migration14') + for key in ('history', 'identities', 'app_role', 'unresolved'): + require(now[key] == record['baseline']['database'][key], 'original database rows unchanged') + check = sql("BEGIN READ ONLY; SELECT json_build_object('count',(SELECT count(*) FROM windows_msi_recipes)," + "'rls',(SELECT relrowsecurity AND relforcerowsecurity FROM pg_class WHERE oid='windows_msi_recipes'::regclass)," + "'select',has_table_privilege('rmm_app','windows_msi_recipes','SELECT')," + "'insert',has_table_privilege('rmm_app','windows_msi_recipes','INSERT')," + "'update',has_table_privilege('rmm_app','windows_msi_recipes','UPDATE')," + "'delete',has_table_privilege('rmm_app','windows_msi_recipes','DELETE')," + "'trigger',EXISTS(SELECT 1 FROM pg_trigger WHERE tgrelid='windows_msi_recipes'::regclass AND tgname='immutable_content' AND tgenabled='O')); COMMIT;") + require(check == {'count': 0, 'rls': True, 'select': True, 'insert': True, 'update': False, 'delete': False, 'trigger': True}, 'empty immutable MSI tenant catalog with FORCE RLS') + return now + + def check_source(record): + require(all(digest(raw(folder / 'context' / name)) == value for name, value in record['source_files'].items()), 'frozen build context unchanged') + + def https(port, path, host='localhost'): + require((port, host) in ((18443, 'localhost'), (18444, 'localhost'), + (443, 'discovery.ease-technologies.de')), 'fixed role TLS endpoint and name') + ca = root / ('devstate/api-pki/ca.pem' if port == 18443 else 'devstate/pki/ca.pem') + class HTTPS(http.client.HTTPSConnection): + def connect(self): + self.sock = self._context.wrap_socket(socket.create_connection(('127.0.0.1', port), timeout=5), server_hostname=self.host) + client = HTTPS(host, port, timeout=5, context=ssl.create_default_context(cafile=str(ca))) + try: + client.request('GET', path) + response = client.getresponse() + body = response.read(2 * 1024 * 1024 + 1) + require(len(body) <= 2 * 1024 * 1024, 'bounded HTTPS body') + return {'status': response.status, 'sha256': digest(body), 'body': body} + finally: + client.close() + + def health(): + for port in (18443, 18444): + deadline = time.monotonic() + 60 + while True: + try: + result = https(port, '/healthz') + require(result['status'] == 200 and result['body'] == b'ok', 'healthy exact server role') + break + except Exception: + require(time.monotonic() < deadline, 'TLS role health deadline') + time.sleep(1) + auth = https(18443, '/auth/config') + require(auth['status'] == 200 and json.loads(auth['body']) == + {'oidc_configured': False, 'oidc_login_url': None, 'dev_login_enabled': True}, 'honest private dev/test authentication') + for path in ('/', '/auth/config', '/auth/session', '/auth/dev/login', '/mcp', '/api/v1/tenants'): + require(https(443, path, 'discovery.ease-technologies.de')['status'] == 404, 'public gateway has no UI/auth/MCP/admin API') + require(https(443, '/api/v1/job-signing-key', 'discovery.ease-technologies.de')['status'] == 401, 'public signing key requires authenticated agent') + + def replace_public(path, data): + require(path.parent.is_dir() and not path.is_symlink(), 'existing served directory, no symlink') + next_path = path.with_name(path.name + '.script-console-' + ident) + once(next_path, data, 0o644) + os.replace(next_path, path) + syncdir(path.parent) + + def after_guard(built, index): + now = witness() + require(now['runtime_image'] == built['image_id'], 'exact candidate remains runtime') + for key in ('controls', 'pg_volume'): + require(now[key] == record['baseline'][key], 'preserved post-deploy ' + key) + require(now['index_sha256'] == index, 'exact post-deploy index') + require(now['containers']['postgres'] == record['baseline']['containers']['postgres'], 'same PG container') + for name in ('api', 'gateway'): + before = dict(record['baseline']['containers'][name]) + after = dict(now['containers'][name]) + for key in ('id', 'image'): + before.pop(key); after.pop(key) + require(before == after, 'same application role, mounts, ports and closed gates') + migration_guard(record, after=True) + + # inspect and status never mkdir, upload, launch, migrate, retag or publish. + if action == 'inspect': + return witness() + if action == 'build': + record = payload['record'] + require(record['id'] == ident and record['old_image'] == payload['old_image'], 'fixed prepared case') + before_guard(witness(), record['baseline']) + migration_guard(record) + require(record['baseline']['controls']['files']['deploy/Dockerfile'] == record['source_files']['deploy/Dockerfile'] + and record['baseline']['controls']['files']['.dockerignore'] == record['source_files']['.dockerignore'] + and record['baseline']['controls']['files']['deploy/server.compose.yaml'] == record['source_files']['deploy/server.compose.yaml'], 'preserving unchanged deploy controls') + # Create once before the first write; an incomplete case is status-only. + owned(root, True); owned(root / 'devstate', True) + folder.mkdir(mode=0o700); syncdir(folder.parent) + once(folder / 'intent.json', enc(record)) + context = folder / 'context'; context.mkdir(mode=0o700) + archive_raw = base64.b64decode(payload['source_archive'], validate=True) + require(len(archive_raw) <= 20 * 1024 * 1024 and digest(archive_raw) == record['source_archive_sha256'], 'source selection archive') + found = set() + with tarfile.open(fileobj=io.BytesIO(archive_raw), mode='r:') as archive: + for member in archive.getmembers(): + name = member.name + require(member.isfile() and name in record['source_files'] and name not in found + and not Path(name).is_absolute() and '..' not in Path(name).parts + and member.size <= 2 * 1024 * 1024, 'fixed complete source selection') + data = archive.extractfile(member).read() + require(digest(data) == record['source_files'][name], 'source member hash') + dest = context / name; dest.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + once(dest, data); found.add(name) + require(found == set(record['source_files']), 'complete frozen source selection') + web = base64.b64decode(payload['web_archive'], validate=True) + require(digest(web) == record['web_archive_sha256'] and len(web) < 2 * 1024 * 1024, 'qualified Web49 archive') + once(folder / 'web.tar', web) + worker = payload['worker_source'].encode() + require(digest(worker) == record['worker_source_sha256'], 'fixed worker source') + once(folder / 'worker.py', worker) + once(folder / 'build-started.json', enc({'id': ident, 'intent_sha256': digest(enc(record))})) + run(['systemd-run', '--unit=rmm-script-build-' + ident, '--property=UMask=0077', + '--property=WorkingDirectory=' + str(folder), '/usr/bin/python3', str(folder / 'worker.py'), 'build-worker'], timeout=15) + return {'id': ident, 'build_launched': True, 'deployed': False, 'lost_reply_action': 'status only'} + + if action == 'status': + if not folder.exists(): + require(not folder.is_symlink(), 'no case symlink') + return {'id': ident, 'case_present': False, 'read_only': True, 'retry_permitted': False} + owned(folder, True) + receipts = {} + for path in sorted(folder.glob('*.json')): + if path.name != 'intent.json': + try: + receipts[path.name] = load(path) + except Exception as error: + receipts[path.name] = {'read_error_class': type(error).__name__} + result = {'id': ident, 'case_present': True, 'receipts': receipts, 'read_only': True, 'retry_permitted': False} + # Readback must survive an interrupted upload, lost launch reply, failed + # migration or stopped roles. Report partial evidence; never heal it. + checks = {'runtime_image': lambda: image('rust-discovery-server:local'), 'database': database, + 'containers': lambda: {name: container(name) for name in ('postgres', 'api', 'gateway')}, + 'controls': controls, 'index_sha256': lambda: digest(raw(root / 'web/dist/index.html'))} + if (folder / 'intent.json').exists(): + checks['intent_sha256'] = lambda: digest(raw(folder / 'intent.json', 8 * 1024 * 1024)) + checks['source_unchanged'] = lambda: all((folder / 'context' / name).is_file() + and digest(raw(folder / 'context' / name)) == value + for name, value in load(folder / 'intent.json')['source_files'].items()) + def served_web(): + values = {} + for name in load(folder / 'intent.json')['web_files']: + got = https(18443, '/' + name if name != 'index.html' else '/') + values[name] = {'status': got['status'], 'sha256': got['sha256']} + return values + checks['private_TLS_web'] = served_web + for name, check in checks.items(): + try: + result[name] = check() + except Exception as error: + result[name] = {'read_error_class': type(error).__name__} + for name in ('build', 'deploy'): + path = folder / (name + '.log') + result[name + '_log_bytes'] = path.stat().st_size if path.exists() else None + return result + + owned(folder, True) + record = load(folder / 'intent.json') + require(record['id'] == ident and record['old_image'] == payload['old_image'], 'same case intent') + require(digest(raw(folder / 'worker.py')) == record['worker_source_sha256'], 'retained worker unchanged') + def deploy_guard(): + check_source(record) + before_guard(witness(), record['baseline']) + migration_guard(record) + built = load(folder / 'build-result.json') + require(built['passed'] and built['image_id'] == image(record['candidate_tag']), 'exact successful candidate image') + require(not (folder / 'deploy-started.json').exists(), 'create-once deployment; status only after any launch') + return built + + if action == 'deploy': + built = deploy_guard() + once(folder / 'deploy-started.json', enc({'id': ident, 'image_id': built['image_id'], 'intent_sha256': digest(enc(record))})) + run(['systemd-run', '--unit=rmm-script-deploy-' + ident, '--property=UMask=0077', + '--property=WorkingDirectory=' + str(folder), '/usr/bin/python3', str(folder / 'worker.py'), 'deploy-worker'], timeout=15) + return {'id': ident, 'deploy_launched': True, 'completed': False, 'lost_reply_action': 'status only'} + + # The retained worker, never caller-supplied shell code, performs each phase + # once. A host-wide lock serializes this runner's build/deployment workers. + require(action in ('build-worker', 'deploy-worker'), 'retained worker action') + lock = os.open(root / 'devstate/.script-console-upgrade-v1.lock', os.O_WRONLY | os.O_CREAT | os.O_NOFOLLOW, 0o600) + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + phase = 'build' if action == 'build-worker' else 'deploy' + try: + once(folder / (phase + '-worker-started.json'), enc({'id': ident})) + if phase == 'build': + before_guard(witness(), record['baseline']) + check_source(record) + require(not run(['docker', 'image', 'ls', '-q', record['candidate_tag']]).strip(), 'new candidate tag only') + with (folder / 'build.log').open('xb') as log: + result = subprocess.run(['docker', 'build', '--platform=linux/arm64', '--file', str(folder / 'context/deploy/Dockerfile'), + '--tag', record['candidate_tag'], str(folder / 'context')], stdout=log, stderr=subprocess.STDOUT, timeout=3600) + log.flush(); os.fsync(log.fileno()) + require(result.returncode == 0, 'native ARM64 image build failed') + check_source(record) + before_guard(witness(), record['baseline']) + outcome = {'passed': True, 'image_id': image(record['candidate_tag']), 'source_files_equal': True, + 'runtime_unchanged': True, 'migration_executed': False, 'jobs_or_approvals_created': False} + else: + check_source(record) + built = load(folder / 'build-result.json') + require(built['passed'] and built['image_id'] == image(record['candidate_tag']), 'fixed built image') + before_guard(witness(), record['baseline']) + migration_guard(record) + # Intent already durable. Stop both writers before checking/backup; + # failures deliberately leave an incomplete case for operator readback. + run(compose + ['stop', '--timeout', '20', 'api', 'gateway'], timeout=50) + once(folder / '01-services-stopped.json', enc({'id': ident})) + before_guard(witness(running=False), record['baseline'], stopped=True) + active = sql("BEGIN READ ONLY; SELECT json_build_object('other_sessions',count(*)) FROM pg_stat_activity WHERE datname=current_database() AND pid<>pg_backend_pid() AND backend_type='client backend' AND application_name<>'pg_isready'; COMMIT;") + require(active['other_sessions'] == 0, 'no other database writer during upgrade') + with (folder / 'postgres-before.dump').open('xb') as dump: + result = subprocess.run(compose + ['exec', '-T', 'postgres', 'pg_dump', '-U', 'postgres', '-d', 'rmm', '--format=custom'], + stdout=dump, stderr=subprocess.PIPE, timeout=120) + dump.flush(); os.fsync(dump.fileno()) + require(result.returncode == 0, 'private pre-migration database backup') + once(folder / 'index-before.html', raw(root / 'web/dist/index.html')) + once(folder / '02-backup-complete.json', enc({'id': ident, 'backup_private': True})) + # Parse/check the entire UI archive before publishing any asset. + web_raw = raw(folder / 'web.tar') + require(digest(web_raw) == record['web_archive_sha256'], 'unchanged UI archive') + web = {} + with tarfile.open(fileobj=io.BytesIO(web_raw), mode='r:') as archive: + for member in archive.getmembers(): + require(member.isfile() and member.name in record['web_files'] and member.name not in web + and member.size <= 2 * 1024 * 1024, 'exact qualified UI members') + data = archive.extractfile(member).read() + require(digest(data) == record['web_files'][member.name], 'qualified UI bytes') + web[member.name] = data + require(set(web) == set(record['web_files']), 'complete qualified UI') + dist = root / 'web/dist'; owned(dist, True); owned(dist / 'assets', True) + for name, data in web.items(): + if name == 'index.html': + continue + dest = dist / name + if dest.exists(): + require(digest(raw(dest)) == record['web_files'][name], 'immutable asset filename collision') + else: + once(dest, data, 0o644) + once(folder / '03-assets-published.json', enc({'id': ident, 'index_unchanged': digest(raw(dist / 'index.html')) == record['baseline']['index_sha256']})) + require(digest(raw(dist / 'index.html')) == record['baseline']['index_sha256'], 'old index CAS before migration') + require(image('rust-discovery-server:local') == record['old_image'], 'runtime tag CAS before transition') + run(['docker', 'tag', built['image_id'], 'rust-discovery-server:local']) + once(folder / '04-runtime-tagged.json', enc({'id': ident, 'image_id': built['image_id']})) + # Named migration container remains inspectable even on failure/lost + # reply. Never remove it or issue a second migration automatically. + with (folder / 'deploy.log').open('xb') as log: + result = subprocess.run(compose + ['run', '--name', 'rmm-script-migrate-' + ident, + '--no-deps', 'migrate'], stdout=log, stderr=subprocess.STDOUT, timeout=120) + log.flush(); os.fsync(log.fileno()) + require(result.returncode == 0, 'migration incomplete; inspect retained named container') + migration_guard(record, after=True) + once(folder / '05-migration14-verified.json', enc({'id': ident, 'original_ledger_and_history_equal': True})) + run(compose + ['up', '-d', '--no-deps', '--no-build', 'api', 'gateway'], timeout=60) + health() + after_guard(built, record['baseline']['index_sha256']) + once(folder / '06-backend-ready.json', enc({'id': ident, 'TLS_healthy': True, 'public_admin_routes_closed': True})) + # Directory inode and all old hashed assets survive. Only now switch + # the entry document, atomically and against the exact old index. + require(digest(raw(dist / 'index.html')) == record['baseline']['index_sha256'], 'final old index CAS') + replace_public(dist / 'index.html', web['index.html']) + for name, expected in record['web_files'].items(): + served = https(18443, '/' + name if name != 'index.html' else '/') + require(served['status'] == 200 and served['sha256'] == expected, 'actual private TLS UI byte hash') + after_guard(built, record['web_files']['index.html']) + once(folder / '07-index-published.json', enc({'id': ident, 'web_files': record['web_files']})) + outcome = {'passed': True, 'image_id': built['image_id'], 'migration14': True, 'web_files': record['web_files'], + 'prior_history_equal': True, 'jobs_or_approvals_created': False, 'root_system_fault_gates_closed': True, + 'production_oidc_qualified': False, 'native_client_acceptance': False} + once(folder / (phase + '-result.json'), enc(outcome)) + return outcome + except Exception as error: + # No raw exception/command/DB output can escape to the caller or log. + failure = {'passed': False, 'error_class': type(error).__name__, 'retry_permitted': False, + 'frames': [{'function': frame.name, 'line': frame.lineno} for frame in traceback.extract_tb(error.__traceback__)]} + once(folder / (phase + '-failure.json'), enc(failure)) + raise RuntimeError('case incomplete; status/readback only') from None + finally: + os.close(lock) + + +def archive(files): + stream = io.BytesIO() + with tarfile.open(fileobj=stream, mode='w:') as tar: + for name, raw in sorted(files.items()): + item = tarfile.TarInfo(name) + item.size = len(raw) + item.mode = 0o644 + tar.addfile(item, io.BytesIO(raw)) + return stream.getvalue() + + +def worker_source(ident): + prefix = 'import os,json,subprocess,base64,hashlib,uuid,sys\nfrom pathlib import Path\n' + return (prefix + inspect.getsource(bootstrap.require) + '\n' + inspect.getsource(remote) + + '\nif __name__ == "__main__":\n remote(dict(action=sys.argv[1],id=' + repr(ident) + ',old_image=' + repr(OLD_IMAGE) + '))\n').encode() + + +def check_support(): + require(all(sha((ROOT / name).read_bytes()) == expected for name, expected in SUPPORT.items()), 'pinned SSH support sources') + + +def prepare(baseline_path): + require(sha(FROZEN.read_bytes()) == FROZEN_SHA, 'frozen158 qualification manifest') + frozen = json.loads(FROZEN.read_bytes()) + require(frozen['source_before_after_equal'] and len(frozen['source_files']) == 158 + and all(check['exit_code'] == 0 for check in frozen['checks'].values()), 'passing exact qualification') + source_raw = (FROZEN.parent / 'reviewed-sources.tar.gz').read_bytes() + require(sha(source_raw) == frozen['source_archive_sha256'], 'frozen source archive') + sources = {} + with tarfile.open(fileobj=io.BytesIO(source_raw), mode='r:gz') as tar: + for item in tar.getmembers(): + require(item.isfile() and item.name in frozen['source_files'] and item.name not in sources + and item.size <= 2 * 1024 * 1024 and not Path(item.name).is_absolute() + and '..' not in Path(item.name).parts, 'frozen source selection') + data = tar.extractfile(item).read() + require(sha(data) == frozen['source_files'][item.name], 'frozen member hash') + sources[item.name] = data + require(set(sources) == set(frozen['source_files']), 'all158 frozen source inputs retained') + for name, expected in EXTRA.items(): + path = ROOT / name + require(not path.is_symlink() and sha(path.read_bytes()) == expected, 'separate deployment source pin') + sources[name] = path.read_bytes() + web = {} + for name, expected in WEB.items(): + path = ROOT / 'web/dist' / name + require(not path.is_symlink() and sha(path.read_bytes()) == expected, 'qualified Web49 byte pin') + web[name] = path.read_bytes() + # A complete context is manifest-selected, not copied from a live checkout. + for forbidden in ('credentials/', 'devstate/', '.git/', 'target/', 'web/node_modules/'): + require(not any(name.startswith(forbidden) or '/' + forbidden in name for name in sources), 'private/cache path excluded') + require(all(name in sources for name in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', + 'crates/oidc-auth/sql/001_oidc_login_states.sql', 'tools/apt-transaction/apt_transaction.py', + 'tools/apt-transaction/hook', 'deploy/linux/rmm-apt-helper.service', 'migrations/0014_windows_msi_catalog.sql')), 'Docker COPY/include closure') + baseline_raw = baseline_path.read_bytes() + baseline = json.loads(baseline_raw) + require(baseline['runtime_image'] == OLD_IMAGE and baseline['database']['unresolved'] == 0 + and [row['version'] for row in baseline['database']['migrations']] == list(range(1, 14)), 'fresh intended prior runtime/schema') + ident = str(uuid.uuid4()) + STATE.mkdir(mode=0o700, parents=True, exist_ok=True) + folder = STATE / ident + folder.mkdir(mode=0o700) + data = archive(sources); web_data = archive(web); worker = worker_source(ident) + checksums = {str(int(Path(name).name.split('_', 1)[0])): hashlib.sha384(value).hexdigest() + for name, value in sources.items() if re.fullmatch(r'migrations/\d{4}_[a-z0-9_]+\.sql', name)} + require(set(checksums) == {str(n) for n in range(1, 15)}, 'exact migration1..14 closure') + record = {'schema_version': 1, 'type': 'rmm.script-console.cloud-upgrade.v1', 'id': ident, + 'frozen_manifest_sha256': FROZEN_SHA, 'old_image': OLD_IMAGE, 'baseline': baseline, + 'baseline_sha256': sha(baseline_raw), 'source_files': {name: sha(value) for name, value in sources.items()}, + 'source_archive_sha256': sha(data), 'web_files': WEB, 'web_archive_sha256': sha(web_data), + 'worker_source_sha256': sha(worker), 'runner_sha256': sha(Path(__file__).read_bytes()), + 'support_sha256': SUPPORT, 'migration_checksums': checksums, + 'candidate_tag': 'rust-discovery-server:script-v1-' + ident} + for row in baseline['database']['migrations']: + require(row['success'] and row['checksum'] == checksums[str(row['version'])], 'unchanged SQLx1..13 bytes') + for name, raw in (('source.tar', data), ('web.tar', web_data), ('worker.py', worker), + ('baseline.json', baseline_raw), ('manifest.json', encoded(record))): + create(folder / name, raw) + return {'id': ident, 'manifest': str(folder / 'manifest.json'), 'manifest_sha256': sha(encoded(record)), + 'source_count': len(sources), 'remote_operations': False, 'build_started': False} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('action', choices=('inspect', 'prepare', 'build', 'deploy', 'status')) + parser.add_argument('--id') + parser.add_argument('--baseline', type=Path) + parser.add_argument('--access-root', type=Path, default=ROOT, + help='existing main checkout containing protected server.conf; SSH only') + parser.add_argument('--execute-cloud-build', action='store_true') + parser.add_argument('--execute-cloud-deploy', action='store_true') + args = parser.parse_args() + os.umask(0o077) + check_support() + if args.action == 'prepare': + require(args.baseline is not None, 'explicit saved remote baseline required') + print(json.dumps(prepare(args.baseline))) + return + # Keep the reviewed worktree's source inputs, but reuse the existing main + # checkout's protected SSH transport configuration when explicitly selected. + bootstrap.ROOT = args.access_root.resolve() + bootstrap.access.ROOT = bootstrap.ROOT + config = bootstrap.access.load_config(bootstrap.ROOT / 'credentials/server.conf') + require(config['address'] in ('88.99.80.202', 'discovery.ease-technologies.de') + and config['username'] == 'root' and config.get('port', '22') == '22', 'fixed existing cloud server bootstrap target') + if args.action == 'inspect': + result = bootstrap.rpc('server.conf', remote, {'action': 'inspect', 'id': str(uuid.uuid4()), 'old_image': OLD_IMAGE}) + require(result['runtime_image'] == OLD_IMAGE, 'known actual prior server image') + STATE.mkdir(mode=0o700, parents=True, exist_ok=True) + path = STATE / ('baseline-' + str(uuid.uuid4()) + '.json') + create(path, encoded(result)) + print(json.dumps({'baseline': str(path), 'sha256': sha(encoded(result)), 'read_only_remote': True})) + return + ident = str(uuid.UUID(args.id)) + require(ident == args.id, 'canonical case UUID') + folder = STATE / ident + manifest = json.loads((folder / 'manifest.json').read_bytes()) + require(manifest['id'] == ident and manifest['runner_sha256'] == sha(Path(__file__).read_bytes()) + and manifest['support_sha256'] == SUPPORT and manifest['worker_source_sha256'] == sha(worker_source(ident)), 'retained exact runner/support/worker') + payload = {'action': args.action, 'id': ident, 'old_image': OLD_IMAGE} + if args.action in ('build', 'deploy'): + require(args.execute_cloud_build if args.action == 'build' else args.execute_cloud_deploy, 'explicit bounded cloud action required') + # Local once intent BEFORE SSH: uncertain/lost replies cannot launch twice. + create(folder / (args.action + '-requested.json'), encoded({'id': ident, 'manifest_sha256': sha(encoded(manifest))})) + if args.action == 'build': + payload.update(record=manifest, source_archive=base64.b64encode((folder / 'source.tar').read_bytes()).decode(), + web_archive=base64.b64encode((folder / 'web.tar').read_bytes()).decode(), worker_source=(folder / 'worker.py').read_text()) + result = bootstrap.rpc('server.conf', remote, payload) + create(folder / (args.action + '-reply-' + str(uuid.uuid4()) + '.json'), encoded(result)) + print(json.dumps(result)) + + +if __name__ == '__main__': + try: + main() + except Exception as error: + print('SCRIPT_CONSOLE_UPGRADE_INCOMPLETE:' + type(error).__name__ + '; preserve case, status only after any request') + raise SystemExit(2) from None diff --git a/deploy/cloud_script_console_upgrade_v2.py b/deploy/cloud_script_console_upgrade_v2.py new file mode 100644 index 0000000..602699c --- /dev/null +++ b/deploy/cloud_script_console_upgrade_v2.py @@ -0,0 +1,711 @@ +#!/usr/bin/env python3 +"""Version2: exact preserved cloud upgrade, with deterministic Docker mounts. + +prepare is offline. inspect/status are remote read-only. build/deploy each launch +one durable, create-once case worker. Lost replies permit status only, not retry. +The notebook never runs an RMM server/client. Existing deployment tools remain +unchanged. See docs/script-console-cloud-upgrade-v2.md before actual use. +""" +import argparse +import base64 +import hashlib +import inspect +import io +import json +import os +from pathlib import Path +import re +import tarfile +import uuid + +import remote_client_bootstrap as bootstrap + +ROOT = Path(__file__).resolve().parents[1] +STATE = ROOT / 'devstate/script-console-cloud-upgrade-v2' +FROZEN = ROOT / 'devstate/script-console-qualification/run-29461892-343f-42f4-b469-88703535425e/manifest.json' +FROZEN_SHA = 'ddd1a78d1cd289b02b1f98572f9652171ec00be84e01ce099fd722d689bcaacd' +OLD_IMAGE = 'sha256:25697f948325a11f755893e16ec5be2da138c3333c8e60f75a18f973d6ac26e2' +EXTRA = { + 'deploy/Dockerfile': 'd41b4ec902c11a9e25c22b1c1d3d60d15afc207f2598ecf559cf3bdae4f8e782', + '.dockerignore': 'fdb5cba5967249f5dac35d45c5edf0be2a4e1594c94d04a2ed416d89821ace75', + 'deploy/server.compose.yaml': 'd2044b0ee6d584f360990635bec149511d5a0623ec562976541900d751998818', +} +WEB = { + 'index.html': '95d6f9058cc713c6af36fbd22ccc60678f081765a853b53d19ac6f0d219804f8', + 'assets/index-Be1zm1OZ.css': '916e8870d4e9a9553de452b3ff6c71671bb6a7a22355de0aff432db516a9f3e7', + 'assets/index-CTgvl2jr.js': 'f611662042eb4cc44f25c7d5ddc0d2d0562e32be267d3d5fb1db31b0d310f879', +} +SUPPORT = { + 'deploy/remote_client_bootstrap.py': '785c43a51dfe3f93e39798dbd7611c353752ae2fc64b22e41ba168e221620d30', + 'deploy/vm_access_check.py': '5c8107ea9151a9f498b2530cdc61c1a7a6d745aff853a9b05710f3395838c0dd', +} +PREVIOUS_FAILED_CASE = { + 'id': '499e13fb-7433-405d-9a98-0c27ca864246', + 'manifest_sha256': '305de92d11b90184589210fd2b0f301ecaf5d9a75467bff5e81c8518588ad416', + 'worker_sha256': '6d056e6385ba67c00414de3dc626a51b4f022947bee6381d92908acce3a2b4a9', + 'readback_sha256': 'ed2fcd82cdca7692d9c5d122e4cccc1b5aa5a5dc6fec4ac9035ecef647b8e8ef', + 'failure_before_docker_build': True, +} +require = bootstrap.require + + +def sha(raw): + return hashlib.sha256(raw).hexdigest() + + +def encoded(value): + return json.dumps(value, sort_keys=True, separators=(',', ':')).encode() + + +def create(path, raw): + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) + with os.fdopen(fd, 'wb') as stream: + stream.write(raw) + stream.flush() + os.fsync(stream.fileno()) + fd = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + try: + os.fsync(fd) + finally: + os.close(fd) + + +def remote(payload): + """Self-contained function transported by the existing strict-hostkey RPC.""" + import datetime as dt + import fcntl + import http.client + import io + import platform + import socket + import ssl + import stat + import subprocess + import tarfile + import time + import traceback + os.umask(0o077) + root = Path('/opt/rust-discovery') + action = payload['action'] + require(os.geteuid() == 0 and platform.machine() == 'aarch64', 'fixed root ARM64 server') + require(action in ('inspect', 'build', 'build-worker', 'deploy', 'deploy-worker', 'status'), 'fixed upgrade action') + ident = str(uuid.UUID(payload['id'])) + require(ident == payload['id'], 'canonical case UUID') + folder = root / 'devstate' / ('script-console-upgrade-v2-' + ident) + compose = ['docker', 'compose', '--env-file', str(root / 'devstate/.env'), + '-f', str(root / 'deploy/server.compose.yaml')] + + def digest(raw): + return hashlib.sha256(raw).hexdigest() + + def enc(value): + return json.dumps(value, sort_keys=True, separators=(',', ':')).encode() + + def run(args, timeout=20, **kwargs): + result = subprocess.run(args, capture_output=True, timeout=timeout, **kwargs) + require(result.returncode == 0, 'fixed server command incomplete; inspect private case') + return result.stdout + + def owned(path, directory=False): + value = path.lstat() + require(value.st_uid == 0 and not value.st_mode & 0o022 and not path.is_symlink() + and (stat.S_ISDIR(value.st_mode) if directory else stat.S_ISREG(value.st_mode)), 'root-owned fixed path') + return value + + def raw(path, limit=2 * 1024 * 1024): + value = owned(path) + require(value.st_size <= limit and value.st_nlink == 1, 'bounded private regular file') + return path.read_bytes() + + def syncdir(path): + fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + try: + os.fsync(fd) + finally: + os.close(fd) + + def once(path, data, mode=0o600): + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode) + with os.fdopen(fd, 'wb') as stream: + stream.write(data) + stream.flush() + os.fchmod(stream.fileno(), mode) + os.fsync(stream.fileno()) + syncdir(path.parent) + + def load(path): + return json.loads(raw(path, 8 * 1024 * 1024)) + + def image(tag): + # Separate fields avoid ever retrieving environment variables/secrets. + value = run(['docker', 'image', 'inspect', tag, '--format', '{{.Id}} {{.Architecture}} {{.Os}}']).decode().split() + require(len(value) == 3 and value[1:] == ['arm64', 'linux'] and value[0].startswith('sha256:'), 'native ARM64 image') + return value[0] + + def closed_root_policy(): + # Existing MVP state: flag=true, but this exact historical policy is + # expired. Neither the flag nor the policy is changed by deployment. + directory = root / 'devstate/root-policy' + path = directory / 'policy.json' + parent = directory.lstat(); value = path.lstat() + require(not directory.is_symlink() and stat.S_ISDIR(parent.st_mode) + and parent.st_uid == 10001 and not parent.st_mode & 0o022 + and not path.is_symlink() and stat.S_ISREG(value.st_mode) + and value.st_uid == 10001 and stat.S_IMODE(value.st_mode) == 0o600 + and value.st_nlink == 1 and 0 < value.st_size <= 65536, 'fixed protected closed Root policy path') + data = path.read_bytes() + expected = 'cf15cbb68b590475df6def28e7dcffb7d88e0ca2d2555a06174fb654eeff05f4' + require(digest(data) == expected, 'exact historical closed Root policy hash') + policy = json.loads(data) + require(isinstance(policy, dict) and set(policy) == {'schema_version', 'profile', 'expires_at', 'scopes'} + and type(policy['schema_version']) is int and policy['schema_version'] == 1 + and policy['profile'] == 'development' and isinstance(policy['expires_at'], str) + and isinstance(policy['scopes'], list) and 1 <= len(policy['scopes']) <= 64 + and all(isinstance(scope, dict) for scope in policy['scopes']), 'exact legacy Root policy schema') + expiry = dt.datetime.fromisoformat(policy['expires_at']) + require(expiry.utcoffset() is not None and expiry <= dt.datetime.now(dt.timezone.utc), 'expired Root policy denies every scope') + return {'sha256': expected, 'schema_version': 1, 'profile': 'development', + 'expires_at': policy['expires_at'], 'closed': True} + + def canonical_mounts(mounts): + # Docker does not promise inspection order. Preserve every object and + # attribute, reject ambiguous destinations, then compare canonical lists. + require(isinstance(mounts, list) and all(isinstance(mount, dict) + and isinstance(mount.get('Destination'), str) and mount['Destination'].startswith('/') + and isinstance(mount.get('Type'), str) and bool(mount['Type']) + and isinstance(mount.get('Source'), str) for mount in mounts), 'typed complete Docker mounts') + require(len({mount['Destination'] for mount in mounts}) == len(mounts), 'unique Docker mount destinations') + return sorted(mounts, key=lambda mount: (mount['Destination'], mount['Type'], mount['Source'])) + + def container(service): + values = run(compose + ['ps', '-a', '-q', service]).decode().split() + require(len(values) == 1, 'one fixed compose service') + cid = values[0] + out = {'id': cid} + for name, fmt in (('image', '.Image'), ('user', '.Config.User'), ('running', '.State.Running'), + ('mounts', '.Mounts'), ('ports', '.HostConfig.PortBindings')): + out[name] = json.loads(run(['docker', 'inspect', cid, '--format', '{{json ' + fmt + '}}'])) + out['mounts'] = canonical_mounts(out['mounts']) + if service in ('api', 'gateway'): + # Filter inside Docker's template: credential env values never + # enter Python/stdout. No .env, token or private key is opened. + names = ('RMM_PROFILE', 'RMM_DEV_AUTH', 'RMM_SERVER_ROLE', 'RMM_UI_ORIGIN', + 'RMM_DEV_ROOT_ENABLED', 'RMM_DEV_SYSTEM_ENABLED', 'RMM_DEV_DELIVERY_FAULTS', + 'RMM_DEV_DELIVERY_FAULTS_FILE', 'RMM_DEV_SYSTEM_POLICY_FILE', 'RMM_DEV_ROOT_POLICY_FILE') + select = ' '.join('(eq $key "' + name + '")' for name in names) + fmt = '{{range .Config.Env}}{{$key := index (split . "=") 0}}{{if or ' + select + '}}{{println .}}{{end}}{{end}}' + env = dict(line.split('=', 1) for line in run(['docker', 'inspect', cid, '--format', fmt]).decode().splitlines() if '=' in line) + out['settings'] = {key: env.get(key) for key in names} + require(env.get('RMM_PROFILE') == 'development' and env.get('RMM_DEV_AUTH') == 'true' + and env.get('RMM_SERVER_ROLE') == service and env.get('RMM_UI_ORIGIN') == 'https://localhost:18443' + and env.get('RMM_DEV_ROOT_ENABLED') == 'true' + and env.get('RMM_DEV_ROOT_POLICY_FILE') == '/root-policy/policy.json' + and env.get('RMM_DEV_SYSTEM_ENABLED') is None and env.get('RMM_DEV_DELIVERY_FAULTS') == 'false' + and env.get('RMM_DEV_DELIVERY_FAULTS_FILE') is None and env.get('RMM_DEV_SYSTEM_POLICY_FILE') is None, + 'unchanged private dev/test role with Root/SYSTEM/fault gates closed') + mounts = [mount for mount in out['mounts'] if mount['Destination'] == '/root-policy'] + require(len(mounts) == 1 and mounts[0]['Type'] == 'bind' and mounts[0]['RW'] is False + and mounts[0]['Source'] == str(root / 'devstate/root-policy'), 'exact read-only Root policy mount') + out['closed_root_policy_sha256'] = closed_root_policy()['sha256'] + require(out['user'] == '10001:10001', 'unprivileged application role') + port = '18443' if service == 'api' else '18444' + require(out['ports'] == {'18443/tcp': [{'HostIp': '127.0.0.1', 'HostPort': port}]}, 'loopback-only role') + else: + require(not out['ports'], 'PostgreSQL has no published port') + return out + + def controls(): + names = ('deploy/Dockerfile', '.dockerignore', 'deploy/server.compose.yaml') + files = {name: digest(raw(root / name)) for name in names} + for name in ('rmm-server-ingress.socket', 'rmm-server-ingress.service'): + files['/etc/systemd/system/' + name] = digest(raw(Path('/etc/systemd/system') / name)) + # Metadata-only identity of secret trees. Private material is never read. + identities = {} + for name in ('devstate/.env', 'devstate/pki', 'devstate/api-pki', 'devstate/jobs', 'devstate/root-policy'): + path = root / name + paths = [path, *sorted(path.rglob('*'))] if path.is_dir() else [path] + for item in paths: + value = item.lstat() + require(not item.is_symlink() and (stat.S_ISREG(value.st_mode) or stat.S_ISDIR(value.st_mode)), 'preserved state object') + identities[str(item.relative_to(root))] = [value.st_dev, value.st_ino, value.st_uid, value.st_gid, + value.st_mode, value.st_size if stat.S_ISREG(value.st_mode) else None, + value.st_mtime_ns if stat.S_ISREG(value.st_mode) else None] + ingress = run(['systemctl', 'show', 'rmm-server-ingress.socket', '-p', 'ActiveState', '-p', 'UnitFileState']).decode() + require('ActiveState=active' in ingress, 'existing public socket ingress stays active') + return {'files': files, 'protected_metadata': identities, 'ingress': ingress, + 'root_policy': closed_root_policy()} + + def sql(query): + return json.loads(run(compose + ['exec', '-T', 'postgres', 'psql', '-X', '-qAt', '-v', + 'ON_ERROR_STOP=1', '-U', 'postgres', '-d', 'rmm'], timeout=30, input=query.encode())) + + def database(): + # One repeatable-read snapshot, only counts and SHA256 values leave PG. + tables = ('tenants', 'assets', 'operation_plans', 'approval_requests', 'authorizations', 'operations', + 'jobs', 'outbox', 'execution_permits', 'job_receipts', 'audit_events', 'operation_submissions', + 'live_query_requests', 'memberships', 'tenant_limits', 'oidc_principals', 'package_recipes', 'artifacts') + pairs = [] + for table in tables: + pairs.append("'" + table + "',(SELECT json_build_object('count',count(*),'sha256',encode(sha256(convert_to(" + "coalesce(string_agg(to_jsonb(t)::text,E'\\n' ORDER BY to_jsonb(t)::text),''),'UTF8')),'hex')) FROM " + table + ' t)') + # Stable device identity excludes expected changing passive Sync/contact. + identity = "(SELECT coalesce(json_agg(q ORDER BY tenant_id,agent_id),'[]'::json) FROM (SELECT tenant_id,agent_id,asset_id,enrollment_generation,assignment_version,certificate_sha256,revoked_at FROM agent_installations) q)" + migrations = "(SELECT json_agg(q ORDER BY version) FROM (SELECT version,description,installed_on,success,encode(checksum,'hex') AS checksum,execution_time FROM _sqlx_migrations) q)" + role = "(SELECT json_build_object('superuser',rolsuper,'bypassrls',rolbypassrls) FROM pg_roles WHERE rolname='rmm_app')" + query = "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY; SELECT json_build_object('history',json_build_object(" + ','.join(pairs) + "),'identities'," + identity + ",'migrations'," + migrations + ",'app_role'," + role + ",'unresolved',(SELECT count(*) FROM jobs WHERE state NOT IN ('succeeded','failed','cancelled','expired'))); COMMIT;" + out = sql(query) + require(out['unresolved'] == 0 and out['app_role'] == {'superuser': False, 'bypassrls': False}, 'settled history and no RLS bypass') + require(all(row['success'] for row in out['migrations']), 'successful SQLx ledger') + return out + + def witness(running=True): + current = {'runtime_image': image('rust-discovery-server:local'), + 'controls': controls(), 'containers': {name: container(name) for name in ('postgres', 'api', 'gateway')}, + 'index_sha256': digest(raw(root / 'web/dist/index.html')), 'database': database()} + pg = current['containers']['postgres'] + mounts = [m for m in pg['mounts'] if m['Destination'] == '/var/lib/postgresql/data'] + require(len(mounts) == 1 and mounts[0]['Type'] == 'volume' and pg['running'], 'existing PG data volume') + value = Path(mounts[0]['Source']).lstat() + require(stat.S_ISDIR(value.st_mode) and not Path(mounts[0]['Source']).is_symlink(), 'same PG data directory') + current['pg_volume'] = {'mount': mounts[0], 'device': value.st_dev, 'inode': value.st_ino} + for name in ('api', 'gateway'): + require(current['containers'][name]['running'] == running, 'fixed application running state') + require(current['containers'][name]['image'] == current['runtime_image'], 'both services run runtime tag') + return current + + def before_guard(now, expected, stopped=False): + require(now['runtime_image'] == expected['runtime_image'] == payload['old_image'], 'runtime image CAS') + for key in ('controls', 'index_sha256', 'database', 'pg_volume'): + require(now[key] == expected[key], 'preserved ' + key + ' CAS') + for name in ('postgres', 'api', 'gateway'): + current = dict(now['containers'][name]) + old = dict(expected['containers'][name]) + if stopped and name != 'postgres': + current.pop('running'); old.pop('running') + require(current == old, 'fixed prior service/container CAS') + + def migration_guard(record, after=False): + expected = record['migration_checksums'] + require([row['version'] for row in record['baseline']['database']['migrations']] == list(range(1, 14)), 'exact prior migrations 1..13') + for row in record['baseline']['database']['migrations']: + require(row['checksum'] == expected[str(row['version'])], 'frozen old migration checksum') + if after: + now = database() + require(now['migrations'][:13] == record['baseline']['database']['migrations'] + and [row['version'] for row in now['migrations']] == list(range(1, 15)) + and now['migrations'][13]['checksum'] == expected['14'], 'only additive frozen migration14') + for key in ('history', 'identities', 'app_role', 'unresolved'): + require(now[key] == record['baseline']['database'][key], 'original database rows unchanged') + check = sql("BEGIN READ ONLY; SELECT json_build_object('count',(SELECT count(*) FROM windows_msi_recipes)," + "'rls',(SELECT relrowsecurity AND relforcerowsecurity FROM pg_class WHERE oid='windows_msi_recipes'::regclass)," + "'select',has_table_privilege('rmm_app','windows_msi_recipes','SELECT')," + "'insert',has_table_privilege('rmm_app','windows_msi_recipes','INSERT')," + "'update',has_table_privilege('rmm_app','windows_msi_recipes','UPDATE')," + "'delete',has_table_privilege('rmm_app','windows_msi_recipes','DELETE')," + "'trigger',EXISTS(SELECT 1 FROM pg_trigger WHERE tgrelid='windows_msi_recipes'::regclass AND tgname='immutable_content' AND tgenabled='O')); COMMIT;") + require(check == {'count': 0, 'rls': True, 'select': True, 'insert': True, 'update': False, 'delete': False, 'trigger': True}, 'empty immutable MSI tenant catalog with FORCE RLS') + return now + + def check_source(record): + require(all(digest(raw(folder / 'context' / name)) == value for name, value in record['source_files'].items()), 'frozen build context unchanged') + + def https(port, path, host='localhost'): + require((port, host) in ((18443, 'localhost'), (18444, 'localhost'), + (443, 'discovery.ease-technologies.de')), 'fixed role TLS endpoint and name') + ca = root / ('devstate/api-pki/ca.pem' if port == 18443 else 'devstate/pki/ca.pem') + class HTTPS(http.client.HTTPSConnection): + def connect(self): + self.sock = self._context.wrap_socket(socket.create_connection(('127.0.0.1', port), timeout=5), server_hostname=self.host) + client = HTTPS(host, port, timeout=5, context=ssl.create_default_context(cafile=str(ca))) + try: + client.request('GET', path) + response = client.getresponse() + body = response.read(2 * 1024 * 1024 + 1) + require(len(body) <= 2 * 1024 * 1024, 'bounded HTTPS body') + return {'status': response.status, 'sha256': digest(body), 'body': body} + finally: + client.close() + + def health(): + for port in (18443, 18444): + deadline = time.monotonic() + 60 + while True: + try: + result = https(port, '/healthz') + require(result['status'] == 200 and result['body'] == b'ok', 'healthy exact server role') + break + except Exception: + require(time.monotonic() < deadline, 'TLS role health deadline') + time.sleep(1) + auth = https(18443, '/auth/config') + require(auth['status'] == 200 and json.loads(auth['body']) == + {'oidc_configured': False, 'oidc_login_url': None, 'dev_login_enabled': True}, 'honest private dev/test authentication') + for path in ('/', '/auth/config', '/auth/session', '/auth/dev/login', '/mcp', '/api/v1/tenants'): + require(https(443, path, 'discovery.ease-technologies.de')['status'] == 404, 'public gateway has no UI/auth/MCP/admin API') + require(https(443, '/api/v1/job-signing-key', 'discovery.ease-technologies.de')['status'] == 401, 'public signing key requires authenticated agent') + + def replace_public(path, data): + require(path.parent.is_dir() and not path.is_symlink(), 'existing served directory, no symlink') + next_path = path.with_name(path.name + '.script-console-' + ident) + once(next_path, data, 0o644) + os.replace(next_path, path) + syncdir(path.parent) + + def after_guard(built, index): + now = witness() + require(now['runtime_image'] == built['image_id'], 'exact candidate remains runtime') + for key in ('controls', 'pg_volume'): + require(now[key] == record['baseline'][key], 'preserved post-deploy ' + key) + require(now['index_sha256'] == index, 'exact post-deploy index') + require(now['containers']['postgres'] == record['baseline']['containers']['postgres'], 'same PG container') + for name in ('api', 'gateway'): + before = dict(record['baseline']['containers'][name]) + after = dict(now['containers'][name]) + for key in ('id', 'image'): + before.pop(key); after.pop(key) + require(before == after, 'same application role, mounts, ports and closed gates') + migration_guard(record, after=True) + + # inspect and status never mkdir, upload, launch, migrate, retag or publish. + if action == 'inspect': + return witness() + if action == 'build': + record = payload['record'] + require(record['id'] == ident and record['old_image'] == payload['old_image'], 'fixed prepared case') + before_guard(witness(), record['baseline']) + migration_guard(record) + require(record['baseline']['controls']['files']['deploy/Dockerfile'] == record['source_files']['deploy/Dockerfile'] + and record['baseline']['controls']['files']['.dockerignore'] == record['source_files']['.dockerignore'] + and record['baseline']['controls']['files']['deploy/server.compose.yaml'] == record['source_files']['deploy/server.compose.yaml'], 'preserving unchanged deploy controls') + # Create once before the first write; an incomplete case is status-only. + owned(root, True); owned(root / 'devstate', True) + folder.mkdir(mode=0o700); syncdir(folder.parent) + once(folder / 'intent.json', enc(record)) + context = folder / 'context'; context.mkdir(mode=0o700) + archive_raw = base64.b64decode(payload['source_archive'], validate=True) + require(len(archive_raw) <= 20 * 1024 * 1024 and digest(archive_raw) == record['source_archive_sha256'], 'source selection archive') + found = set() + with tarfile.open(fileobj=io.BytesIO(archive_raw), mode='r:') as archive: + for member in archive.getmembers(): + name = member.name + require(member.isfile() and name in record['source_files'] and name not in found + and not Path(name).is_absolute() and '..' not in Path(name).parts + and member.size <= 2 * 1024 * 1024, 'fixed complete source selection') + data = archive.extractfile(member).read() + require(digest(data) == record['source_files'][name], 'source member hash') + dest = context / name; dest.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + once(dest, data); found.add(name) + require(found == set(record['source_files']), 'complete frozen source selection') + web = base64.b64decode(payload['web_archive'], validate=True) + require(digest(web) == record['web_archive_sha256'] and len(web) < 2 * 1024 * 1024, 'qualified Web49 archive') + once(folder / 'web.tar', web) + worker = payload['worker_source'].encode() + require(digest(worker) == record['worker_source_sha256'], 'fixed worker source') + once(folder / 'worker.py', worker) + once(folder / 'build-started.json', enc({'id': ident, 'intent_sha256': digest(enc(record))})) + run(['systemd-run', '--unit=rmm-script-build-' + ident, '--property=UMask=0077', + '--property=WorkingDirectory=' + str(folder), '/usr/bin/python3', str(folder / 'worker.py'), 'build-worker'], timeout=15) + return {'id': ident, 'build_launched': True, 'deployed': False, 'lost_reply_action': 'status only'} + + if action == 'status': + if not folder.exists(): + require(not folder.is_symlink(), 'no case symlink') + return {'id': ident, 'case_present': False, 'read_only': True, 'retry_permitted': False} + owned(folder, True) + receipts = {} + for path in sorted(folder.glob('*.json')): + if path.name != 'intent.json': + try: + receipts[path.name] = load(path) + except Exception as error: + receipts[path.name] = {'read_error_class': type(error).__name__} + result = {'id': ident, 'case_present': True, 'receipts': receipts, 'read_only': True, 'retry_permitted': False} + # Readback must survive an interrupted upload, lost launch reply, failed + # migration or stopped roles. Report partial evidence; never heal it. + checks = {'runtime_image': lambda: image('rust-discovery-server:local'), 'database': database, + 'containers': lambda: {name: container(name) for name in ('postgres', 'api', 'gateway')}, + 'controls': controls, 'index_sha256': lambda: digest(raw(root / 'web/dist/index.html'))} + if (folder / 'intent.json').exists(): + checks['intent_sha256'] = lambda: digest(raw(folder / 'intent.json', 8 * 1024 * 1024)) + checks['source_unchanged'] = lambda: all((folder / 'context' / name).is_file() + and digest(raw(folder / 'context' / name)) == value + for name, value in load(folder / 'intent.json')['source_files'].items()) + def served_web(): + values = {} + for name in load(folder / 'intent.json')['web_files']: + got = https(18443, '/' + name if name != 'index.html' else '/') + values[name] = {'status': got['status'], 'sha256': got['sha256']} + return values + checks['private_TLS_web'] = served_web + for name, check in checks.items(): + try: + result[name] = check() + except Exception as error: + result[name] = {'read_error_class': type(error).__name__} + for name in ('build', 'deploy'): + path = folder / (name + '.log') + result[name + '_log_bytes'] = path.stat().st_size if path.exists() else None + return result + + owned(folder, True) + record = load(folder / 'intent.json') + require(record['id'] == ident and record['old_image'] == payload['old_image'], 'same case intent') + require(digest(raw(folder / 'worker.py')) == record['worker_source_sha256'], 'retained worker unchanged') + def deploy_guard(): + check_source(record) + before_guard(witness(), record['baseline']) + migration_guard(record) + built = load(folder / 'build-result.json') + require(built['passed'] and built['image_id'] == image(record['candidate_tag']), 'exact successful candidate image') + require(not (folder / 'deploy-started.json').exists(), 'create-once deployment; status only after any launch') + return built + + if action == 'deploy': + built = deploy_guard() + once(folder / 'deploy-started.json', enc({'id': ident, 'image_id': built['image_id'], 'intent_sha256': digest(enc(record))})) + run(['systemd-run', '--unit=rmm-script-deploy-' + ident, '--property=UMask=0077', + '--property=WorkingDirectory=' + str(folder), '/usr/bin/python3', str(folder / 'worker.py'), 'deploy-worker'], timeout=15) + return {'id': ident, 'deploy_launched': True, 'completed': False, 'lost_reply_action': 'status only'} + + # The retained worker, never caller-supplied shell code, performs each phase + # once. A host-wide lock serializes this runner's build/deployment workers. + require(action in ('build-worker', 'deploy-worker'), 'retained worker action') + lock = os.open(root / 'devstate/.script-console-upgrade-v1.lock', os.O_WRONLY | os.O_CREAT | os.O_NOFOLLOW, 0o600) + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + phase = 'build' if action == 'build-worker' else 'deploy' + try: + once(folder / (phase + '-worker-started.json'), enc({'id': ident})) + if phase == 'build': + before_guard(witness(), record['baseline']) + check_source(record) + require(not run(['docker', 'image', 'ls', '-q', record['candidate_tag']]).strip(), 'new candidate tag only') + with (folder / 'build.log').open('xb') as log: + result = subprocess.run(['docker', 'build', '--platform=linux/arm64', '--file', str(folder / 'context/deploy/Dockerfile'), + '--tag', record['candidate_tag'], str(folder / 'context')], stdout=log, stderr=subprocess.STDOUT, timeout=3600) + log.flush(); os.fsync(log.fileno()) + require(result.returncode == 0, 'native ARM64 image build failed') + check_source(record) + before_guard(witness(), record['baseline']) + outcome = {'passed': True, 'image_id': image(record['candidate_tag']), 'source_files_equal': True, + 'runtime_unchanged': True, 'migration_executed': False, 'jobs_or_approvals_created': False} + else: + check_source(record) + built = load(folder / 'build-result.json') + require(built['passed'] and built['image_id'] == image(record['candidate_tag']), 'fixed built image') + before_guard(witness(), record['baseline']) + migration_guard(record) + # Intent already durable. Stop both writers before checking/backup; + # failures deliberately leave an incomplete case for operator readback. + run(compose + ['stop', '--timeout', '20', 'api', 'gateway'], timeout=50) + once(folder / '01-services-stopped.json', enc({'id': ident})) + before_guard(witness(running=False), record['baseline'], stopped=True) + active = sql("BEGIN READ ONLY; SELECT json_build_object('other_sessions',count(*)) FROM pg_stat_activity WHERE datname=current_database() AND pid<>pg_backend_pid() AND backend_type='client backend' AND application_name<>'pg_isready'; COMMIT;") + require(active['other_sessions'] == 0, 'no other database writer during upgrade') + with (folder / 'postgres-before.dump').open('xb') as dump: + result = subprocess.run(compose + ['exec', '-T', 'postgres', 'pg_dump', '-U', 'postgres', '-d', 'rmm', '--format=custom'], + stdout=dump, stderr=subprocess.PIPE, timeout=120) + dump.flush(); os.fsync(dump.fileno()) + require(result.returncode == 0, 'private pre-migration database backup') + once(folder / 'index-before.html', raw(root / 'web/dist/index.html')) + once(folder / '02-backup-complete.json', enc({'id': ident, 'backup_private': True})) + # Parse/check the entire UI archive before publishing any asset. + web_raw = raw(folder / 'web.tar') + require(digest(web_raw) == record['web_archive_sha256'], 'unchanged UI archive') + web = {} + with tarfile.open(fileobj=io.BytesIO(web_raw), mode='r:') as archive: + for member in archive.getmembers(): + require(member.isfile() and member.name in record['web_files'] and member.name not in web + and member.size <= 2 * 1024 * 1024, 'exact qualified UI members') + data = archive.extractfile(member).read() + require(digest(data) == record['web_files'][member.name], 'qualified UI bytes') + web[member.name] = data + require(set(web) == set(record['web_files']), 'complete qualified UI') + dist = root / 'web/dist'; owned(dist, True); owned(dist / 'assets', True) + for name, data in web.items(): + if name == 'index.html': + continue + dest = dist / name + if dest.exists(): + require(digest(raw(dest)) == record['web_files'][name], 'immutable asset filename collision') + else: + once(dest, data, 0o644) + once(folder / '03-assets-published.json', enc({'id': ident, 'index_unchanged': digest(raw(dist / 'index.html')) == record['baseline']['index_sha256']})) + require(digest(raw(dist / 'index.html')) == record['baseline']['index_sha256'], 'old index CAS before migration') + require(image('rust-discovery-server:local') == record['old_image'], 'runtime tag CAS before transition') + run(['docker', 'tag', built['image_id'], 'rust-discovery-server:local']) + once(folder / '04-runtime-tagged.json', enc({'id': ident, 'image_id': built['image_id']})) + # Named migration container remains inspectable even on failure/lost + # reply. Never remove it or issue a second migration automatically. + with (folder / 'deploy.log').open('xb') as log: + result = subprocess.run(compose + ['run', '--name', 'rmm-script-migrate-' + ident, + '--no-deps', 'migrate'], stdout=log, stderr=subprocess.STDOUT, timeout=120) + log.flush(); os.fsync(log.fileno()) + require(result.returncode == 0, 'migration incomplete; inspect retained named container') + migration_guard(record, after=True) + once(folder / '05-migration14-verified.json', enc({'id': ident, 'original_ledger_and_history_equal': True})) + run(compose + ['up', '-d', '--no-deps', '--no-build', 'api', 'gateway'], timeout=60) + health() + after_guard(built, record['baseline']['index_sha256']) + once(folder / '06-backend-ready.json', enc({'id': ident, 'TLS_healthy': True, 'public_admin_routes_closed': True})) + # Directory inode and all old hashed assets survive. Only now switch + # the entry document, atomically and against the exact old index. + require(digest(raw(dist / 'index.html')) == record['baseline']['index_sha256'], 'final old index CAS') + replace_public(dist / 'index.html', web['index.html']) + for name, expected in record['web_files'].items(): + served = https(18443, '/' + name if name != 'index.html' else '/') + require(served['status'] == 200 and served['sha256'] == expected, 'actual private TLS UI byte hash') + after_guard(built, record['web_files']['index.html']) + once(folder / '07-index-published.json', enc({'id': ident, 'web_files': record['web_files']})) + outcome = {'passed': True, 'image_id': built['image_id'], 'migration14': True, 'web_files': record['web_files'], + 'prior_history_equal': True, 'jobs_or_approvals_created': False, 'root_system_fault_gates_closed': True, + 'production_oidc_qualified': False, 'native_client_acceptance': False} + once(folder / (phase + '-result.json'), enc(outcome)) + return outcome + except Exception as error: + # No raw exception/command/DB output can escape to the caller or log. + failure = {'passed': False, 'error_class': type(error).__name__, 'retry_permitted': False, + 'frames': [{'function': frame.name, 'line': frame.lineno} for frame in traceback.extract_tb(error.__traceback__)]} + once(folder / (phase + '-failure.json'), enc(failure)) + raise RuntimeError('case incomplete; status/readback only') from None + finally: + os.close(lock) + + +def archive(files): + stream = io.BytesIO() + with tarfile.open(fileobj=stream, mode='w:') as tar: + for name, raw in sorted(files.items()): + item = tarfile.TarInfo(name) + item.size = len(raw) + item.mode = 0o644 + tar.addfile(item, io.BytesIO(raw)) + return stream.getvalue() + + +def worker_source(ident): + prefix = 'import os,json,subprocess,base64,hashlib,uuid,sys\nfrom pathlib import Path\n' + return (prefix + inspect.getsource(bootstrap.require) + '\n' + inspect.getsource(remote) + + '\nif __name__ == "__main__":\n remote(dict(action=sys.argv[1],id=' + repr(ident) + ',old_image=' + repr(OLD_IMAGE) + '))\n').encode() + + +def check_support(): + require(all(sha((ROOT / name).read_bytes()) == expected for name, expected in SUPPORT.items()), 'pinned SSH support sources') + + +def prepare(baseline_path): + require(sha(FROZEN.read_bytes()) == FROZEN_SHA, 'frozen158 qualification manifest') + frozen = json.loads(FROZEN.read_bytes()) + require(frozen['source_before_after_equal'] and len(frozen['source_files']) == 158 + and all(check['exit_code'] == 0 for check in frozen['checks'].values()), 'passing exact qualification') + source_raw = (FROZEN.parent / 'reviewed-sources.tar.gz').read_bytes() + require(sha(source_raw) == frozen['source_archive_sha256'], 'frozen source archive') + sources = {} + with tarfile.open(fileobj=io.BytesIO(source_raw), mode='r:gz') as tar: + for item in tar.getmembers(): + require(item.isfile() and item.name in frozen['source_files'] and item.name not in sources + and item.size <= 2 * 1024 * 1024 and not Path(item.name).is_absolute() + and '..' not in Path(item.name).parts, 'frozen source selection') + data = tar.extractfile(item).read() + require(sha(data) == frozen['source_files'][item.name], 'frozen member hash') + sources[item.name] = data + require(set(sources) == set(frozen['source_files']), 'all158 frozen source inputs retained') + for name, expected in EXTRA.items(): + path = ROOT / name + require(not path.is_symlink() and sha(path.read_bytes()) == expected, 'separate deployment source pin') + sources[name] = path.read_bytes() + web = {} + for name, expected in WEB.items(): + path = ROOT / 'web/dist' / name + require(not path.is_symlink() and sha(path.read_bytes()) == expected, 'qualified Web49 byte pin') + web[name] = path.read_bytes() + # A complete context is manifest-selected, not copied from a live checkout. + for forbidden in ('credentials/', 'devstate/', '.git/', 'target/', 'web/node_modules/'): + require(not any(name.startswith(forbidden) or '/' + forbidden in name for name in sources), 'private/cache path excluded') + require(all(name in sources for name in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', + 'crates/oidc-auth/sql/001_oidc_login_states.sql', 'tools/apt-transaction/apt_transaction.py', + 'tools/apt-transaction/hook', 'deploy/linux/rmm-apt-helper.service', 'migrations/0014_windows_msi_catalog.sql')), 'Docker COPY/include closure') + baseline_raw = baseline_path.read_bytes() + baseline = json.loads(baseline_raw) + require(baseline['runtime_image'] == OLD_IMAGE and baseline['database']['unresolved'] == 0 + and [row['version'] for row in baseline['database']['migrations']] == list(range(1, 14)), 'fresh intended prior runtime/schema') + ident = str(uuid.uuid4()) + STATE.mkdir(mode=0o700, parents=True, exist_ok=True) + folder = STATE / ident + folder.mkdir(mode=0o700) + data = archive(sources); web_data = archive(web); worker = worker_source(ident) + checksums = {str(int(Path(name).name.split('_', 1)[0])): hashlib.sha384(value).hexdigest() + for name, value in sources.items() if re.fullmatch(r'migrations/\d{4}_[a-z0-9_]+\.sql', name)} + require(set(checksums) == {str(n) for n in range(1, 15)}, 'exact migration1..14 closure') + record = {'schema_version': 1, 'type': 'rmm.script-console.cloud-upgrade.v2', 'id': ident, + 'frozen_manifest_sha256': FROZEN_SHA, 'old_image': OLD_IMAGE, 'baseline': baseline, + 'baseline_sha256': sha(baseline_raw), 'source_files': {name: sha(value) for name, value in sources.items()}, + 'source_archive_sha256': sha(data), 'web_files': WEB, 'web_archive_sha256': sha(web_data), + 'worker_source_sha256': sha(worker), 'runner_sha256': sha(Path(__file__).read_bytes()), + 'support_sha256': SUPPORT, 'migration_checksums': checksums, + 'candidate_tag': 'rust-discovery-server:script-v2-' + ident} + record['supersedes_failed_case'] = PREVIOUS_FAILED_CASE + for row in baseline['database']['migrations']: + require(row['success'] and row['checksum'] == checksums[str(row['version'])], 'unchanged SQLx1..13 bytes') + for name, raw in (('source.tar', data), ('web.tar', web_data), ('worker.py', worker), + ('baseline.json', baseline_raw), ('manifest.json', encoded(record))): + create(folder / name, raw) + return {'id': ident, 'manifest': str(folder / 'manifest.json'), 'manifest_sha256': sha(encoded(record)), + 'source_count': len(sources), 'remote_operations': False, 'build_started': False} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('action', choices=('inspect', 'prepare', 'build', 'deploy', 'status')) + parser.add_argument('--id') + parser.add_argument('--baseline', type=Path) + parser.add_argument('--access-root', type=Path, default=ROOT, + help='existing main checkout containing protected server.conf; SSH only') + parser.add_argument('--execute-cloud-build', action='store_true') + parser.add_argument('--execute-cloud-deploy', action='store_true') + args = parser.parse_args() + os.umask(0o077) + check_support() + if args.action == 'prepare': + require(args.baseline is not None, 'explicit saved remote baseline required') + print(json.dumps(prepare(args.baseline))) + return + # Keep the reviewed worktree's source inputs, but reuse the existing main + # checkout's protected SSH transport configuration when explicitly selected. + bootstrap.ROOT = args.access_root.resolve() + bootstrap.access.ROOT = bootstrap.ROOT + config = bootstrap.access.load_config(bootstrap.ROOT / 'credentials/server.conf') + require(config['address'] in ('88.99.80.202', 'discovery.ease-technologies.de') + and config['username'] == 'root' and config.get('port', '22') == '22', 'fixed existing cloud server bootstrap target') + if args.action == 'inspect': + result = bootstrap.rpc('server.conf', remote, {'action': 'inspect', 'id': str(uuid.uuid4()), 'old_image': OLD_IMAGE}) + require(result['runtime_image'] == OLD_IMAGE, 'known actual prior server image') + STATE.mkdir(mode=0o700, parents=True, exist_ok=True) + path = STATE / ('baseline-' + str(uuid.uuid4()) + '.json') + create(path, encoded(result)) + print(json.dumps({'baseline': str(path), 'sha256': sha(encoded(result)), 'read_only_remote': True})) + return + ident = str(uuid.UUID(args.id)) + require(ident == args.id, 'canonical case UUID') + folder = STATE / ident + manifest = json.loads((folder / 'manifest.json').read_bytes()) + require(manifest['id'] == ident and manifest['runner_sha256'] == sha(Path(__file__).read_bytes()) + and manifest['support_sha256'] == SUPPORT and manifest['worker_source_sha256'] == sha(worker_source(ident)), 'retained exact runner/support/worker') + payload = {'action': args.action, 'id': ident, 'old_image': OLD_IMAGE} + if args.action in ('build', 'deploy'): + require(args.execute_cloud_build if args.action == 'build' else args.execute_cloud_deploy, 'explicit bounded cloud action required') + # Local once intent BEFORE SSH: uncertain/lost replies cannot launch twice. + create(folder / (args.action + '-requested.json'), encoded({'id': ident, 'manifest_sha256': sha(encoded(manifest))})) + if args.action == 'build': + payload.update(record=manifest, source_archive=base64.b64encode((folder / 'source.tar').read_bytes()).decode(), + web_archive=base64.b64encode((folder / 'web.tar').read_bytes()).decode(), worker_source=(folder / 'worker.py').read_text()) + result = bootstrap.rpc('server.conf', remote, payload) + create(folder / (args.action + '-reply-' + str(uuid.uuid4()) + '.json'), encoded(result)) + print(json.dumps(result)) + + +if __name__ == '__main__': + try: + main() + except Exception as error: + print('SCRIPT_CONSOLE_UPGRADE_INCOMPLETE:' + type(error).__name__ + '; preserve case, status only after any request') + raise SystemExit(2) from None diff --git a/deploy/linux/full-maintenance-README.md b/deploy/linux/full-maintenance-README.md new file mode 100644 index 0000000..885ffe7 --- /dev/null +++ b/deploy/linux/full-maintenance-README.md @@ -0,0 +1,241 @@ +# Explizite Hostwartung mit full_maintenance_v1 + +Stand 2026-11-14, Spezifikation1.20. Die Batch-Skriptkonsole und der unabhängig +geprüfte Fullhelper-Binärpinfix sind implementiert, neu quellqualifiziert und nach +tatsächlichem Linux-Unitlauf erhaltend auf der Cloud-Linux-VM bereitgestellt. +Agent, General und Full laufen mit den geprüften Images; signierter Folgeabgleich +und WebUI-genehmigter Fulljob bleiben separat offen. Tatsächliche Teilfehler, +Fortsetzungen und unveränderte Originalbelege: +[Bootstrap-Ergebnisse](../../docs/script-console-bootstrap-results.md). +Cloud-Linux-VM zuerst, Windowsgast separat; Notebook ausschließlich Entwicklung. + +## Aktueller korrigierter lokaler Nachweis + +Der neue eigenständige Pack liegt privat unter +`devstate/script-console-qualification/run-29461892-343f-42f4-b469-88703535425e/`. +Manifest-SHA256: +`ddd1a78d1cd289b02b1f98572f9652171ec00be84e01ce099fd722d689bcaacd`. +158 vollständige Compiler-/Include-/Fixture-/Deploymentinputs vor/nach identisch. +Sourcearchiv-SHA256: +`fcb57f69ad81ea4acde15af6022d5c2da32f5699ebd39886c530c77882b369cc`. + +Tatsächlich ausgeführt: Formatprüfung, Linux-Workspace-Alltargets-Clippy mit +`-D warnings`,285 lokale Tests bestanden (5DB-Fälle zunächst ausgelassen), danach +sämtliche45 Serververträge inklusive dieser5 gegen frisches isoliertes PostgreSQL +bestanden (0ignored). Windows-GNU-Alltargets-Clippy mit -Dwarnings im Default- +und SYSTEM-Helperprofil bestanden. Kein geänderter Test unterdrückt; die neue +Imageprüfung führt auch bei nicht vertrauenswürdigem Dateibesitz echte +Verweigerungsassertions aus, keinen stillen Early-return-Skip. + +Neue tatsächliche Linux-Unitlisten: Domain22, Linuxruntime16, Helperpolicy6, +Serververträge45. Auf Cloud-Linux ohne isolierte DB erwartet:84PASS und5explizite +DBignored; diese5 sind separat im lokalen45er-DB-Lauf tatsächlich bestanden. +Alle vier vollständigen Listen müssen vor irgendeinem Unitstart geprüft sein. +Linux-Release-Helper SHA256 +`79442727c3ba31cda02a8bc0e67481f14db9f39fc07520a880a90b64f10f1eaf`; +Agent `0a7ad6eefaefda79ee80b6a41b2df904e9e7f50ae6ebeed01b98c25b6ce5c06f`; +Server `610017637a9f9e55a6cb3cd8264937b953996af84e86dd49f2ea00c8e59739cd`. +Keine dieser neuen Binaries ist durch den Builder auf einem Ziel installiert. +Native Cloud-Linux-Units bestanden anschließend tatsächlich; siehe den separaten +Beleg unten. Auch die späteren 36+7 Windows-Units bestanden separat nativ: +[Windows-Nachweis und Grenzen](../../docs/script-console-windows-link.md). +Laufende Windows-SYSTEM-Runtime und WebUI-genehmigter FullMaintenancejob sind +eigenständige, dadurch nicht ersetzte Nachweise. + +## Frühere lokale Nachweise und der danach behobene Binärpinbefund + +Das eingefrorene Paket liegt ausschließlich privat unter +`devstate/script-console-qualification/run-de34d47c-985e-460c-b18c-3f0d1b3fd315/`. +Manifest-SHA256: +`60a50262f9f8d85c11eb58e1a9e0f6e03be6cd5db7a59bc1745fb7d9dc8a6f65`. +157 Compiler-, Include-, Fixture- und Deploymentinputs wurden vor/nach dem +vollständigen Lauf identisch geprüft. `reviewed-sources.tar.gz`: +`ef822b2e2ec6f8ad3d828f0923b2aa886d462fba328affb61bd80e98acf2c357`. + +Tatsächlich lokal ausgeführt: Formatprüfung; Linux-Workspace-Clippy mit allen +Targets und `-D warnings`; Workspace284PASS bei fünf zunächst ausgelassenen +DB-Fällen; anschließend sämtliche45 Servertests mit `--include-ignored` in +frischem isoliertem PostgreSQL17.6:45PASS,0ignored. DB ohne veröffentlichten Port, +Netzwerknamespace ausschließlich intern, frische Rollen/Migrationen, danach +entfernt. Keine Produktionsdatenbank. Windows-GNU-Alltargets-Clippy separat im +Default- und SYSTEM-Helperprofil jeweils bestanden; keine native Windowsausführung. +Linux-Releasebinaries und vier native Linux-Testprogramme dieses früheren +Quellenstands sind gebaut/gehasht, noch nicht auf der Cloud-VM ausgeführt oder +installiert. **Nachträglicher unabhängiger Reviewblocker:** Dieser Pack bindet +noch nicht den tatsächlichen laufenden Linux-Fullhelpercodehash. Er berechtigt +keine Fullprofilaktivierung. Der danach separat geprüfte und im neuen Pack qualifizierte enge Corefix ergänzt ausschließlich +im Fullprofil tatsächliches gehaltenes `/proc/self/exe`, geschützten Binärpin, +authentisierte Probe, zusätzliche Binärcapability und zentrale Hashprüfung. +General/APT lassen neue Metadaten vollständig aus. Der neue lokale Stand steht +oben; dieser alte Pack bleibt ausdrücklich der ursprüngliche Beleg. + +Die ersten Packvorbereitungen erzeugten kein qualifiziertes Manifest: Der erste +before/after-Schutz bemerkte die währenddessen vervollständigte Builderinputliste; +der zweite hielt bei PostgreSQLs temporärer Initialisierungsphase an. Der finale +oben genannte Lauf wartet auf den endgültigen TCP-Endpunkt und besteht vollständig. +Diese Vorbereitungsläufe werden nicht zu bestandenen Abnahmen umgedeutet. + +## Separater Linux-first-Unitrunner + +Der Runner `deploy/script_console_acceptance.py` ist separat reviewbar und kein +Teil der158 eingefrorenen Compilerinputs; er baut und verändert diese nicht. +Er ist ausdrücklich auf den neuen Pack29461892 und dessen Manifestddd1a78d… +gebunden; bei veränderten Compilerinputs wird ein neuer Start verweigert. Die +ursprüngliche Vorbereitung für Packde34 bleibt ein früherer, nie ausgeführter +Runnerstand. Die jetzigen Counts beruhen auf den tatsächlich erzeugten Listen. +Runnerquellhash: +`2b3ba8d548d18627643a9f73c9a4f7bacce4ac2cd5495745c6760d088e078145`. +Supportquellen werden vor Import und jedem Zielaufruf inklusive Readback exakt +geprüft und in Intent/Payload/Result gebunden: Runtime-Bootstraphelper +`833ad8806c90a19342aa6fbc71c33e81975caffb1746550e9b0fbdca7fa0c7ab`, +Zugangsparser +`5c8107ea9151a9f498b2530cdc61c1a7a6d745aff853a9b05710f3395838c0dd`. +Der finale Readback verlangt product_state_writes, rmm_jobs, +capabilities_activated und approvals ausdrücklich jeweils false. +Er verwendet ausschließlich den vorhandenen strikten Bootstraptransport und +`credentials/linux-vm.conf` im ursprünglichen Hauptcheckout, ausschließlich als root auf der festgelegten Linux-Test-VM, mit vorhandenem +SSH-Key und strenger Hostprüfung. Keine +Zugangsdatenkopie ins Scriptworktree, keine alternative IP oder Passwortfallback. + +**Tatsächlich ausgeführt:** Alle vier Listen wurden vor jedem Unitstart geprüft; +84 Tests bestanden, fünf ausdrücklich ausgelassene DB-Verträge waren zuvor im +isolierten lokalen PostgreSQL-Lauf bestanden. Fall +`7b61ce46-7913-492b-9c6b-414a0c2b6ffc`, Beleg-SHA256 +`a831a70a3cf76a3e8a9bf5820dcca7100d2ef9860dd9388539fe9c13f949f470`. +Der verwendete Aufruf war: + +```bash +python3 deploy/script_console_acceptance.py execute-linux +``` + +Es entstehen nur neue private Scratchdateien unter +`/var/lib/rmm-script-console-contracts//`. Erst werden alle vier exakten +ELF-Hashes und vollständigen Listen geprüft; kein Unitstart vor dem gesamten +Listenbeleg. Danach feste Testargumente, geleerte Umgebung, eigenes TMPDIR, +eigener Netzwerknamespace und CPU-/Speicher-/Dateilimits. Erwartet werden84PASS +und fünf ausdrücklich ausgelassene DB-Fälle auf der VM ohne DB; dieselben fünf +DB-Verträge sind bereits im isolierten lokalen45er-Lauf belegt. Keine Installation, +Enrollment-, Dienst-, Policy-, Paket-, Schlüssel-, Journal- oder Capabilityänderung. + +Bei verlorenem Ergebnis ausschließlich denselben ursprünglichen Fall lesen: + +```bash +python3 deploy/script_console_acceptance.py read-linux --case +``` + +Readback erzeugt keinen zweiten Remote-Start. Ein vorhandener Create-once- +Startmarker erlaubt kein erneutes `run`; Teilergebnisse bleiben erhalten. Ein +unvollständiger/fehlender Beleg ist keine bestandene Abnahme und wird nicht blind +wiederholt. Native Units sind keine RMM-Dienst-/WebUI-/FullMaintenanceabnahme. + +## Bootstrapvorbereitung nach gesondertem Erhaltungsreview + +Der ursprüngliche Erhaltungsplan und die weiterhin geltenden Schutzgrenzen +folgen unten; tatsächliche Ausführung samt fehlgeschlagenen Teilphasen und +separaten Erststartfortsetzungen steht in den +[Bootstrap-Ergebnissen](../../docs/script-console-bootstrap-results.md). +Aktuelle Cloudbelege, Geräte-/Boot-/ +Journalidentitäten, Inodes, Verbrauchszeilen und Dienstzustände werden vor einer +Änderung durch den verantwortlichen Rootprozess unabhängig gegengeprüft. Die +bisherigen General-/APT-Units und Pins sowie deren Historie bleiben erhalten; +keine Ledgerkopie, kein Reset und kein stilles Öffnen alter Scopes. + +Die neue Unit ist `deploy/linux/rmm-full-maintenance-helper.service` mit eigenem +Runtimeverzeichnis `/run/rmm-helper-maintenance`. Sie verweist auf +`/etc/rmm-remote/helper-full-maintenance.json`. Diese Konfiguration erhält exakt +die bestehende provisionierte öffentliche Bindingidentität und dieselben +Root-Sharedledger-/Lockdateien; neue Auswahl: + +```json +{ + "profile": "full_maintenance_v1", + "allowed_services": [], + "allow_reboot": false, + "allow_root_scripts": true, + "allowed_script_profiles": ["full_maintenance_v1"], + "helper_binary_sha256": "" +} +``` + +Das ist ein Ausschnitt, keine vollständige einsatzfähige Konfiguration. Socket, +Agent-UID/GID, Zustandspfad und bestehendes Binding müssen aus dem tatsächlich +geprüften Bestand kommen. Kein `apt_provider`. `allow_reboot=false` verweigert +nur die typisierte Rebootaction; genehmigter Rootcode besitzt volle Hostautorität. + +Der separat geschützte Policy-Pin bindet Profil `full_maintenance_v1`, bestehende +Identität, tatsächlichen lokalen Policyhash und unabhängig beobachteten +laufenden Binärhash. Das neue Fullprofil benötigt Schema2 im Pin; der Helper +kann diesen nach selbst geprüftem Rootimage mit dem ausdrücklich getrennten +Bootstrapmodus `--inspect-full-maintenance-profile` ausgeben. Generalpin +Schema1 bleibt unverändert. Agentumgebung ausschließlich +additiv um `RMM_FULL_MAINTENANCE_HELPER_SOCKET` und +`RMM_FULL_MAINTENANCE_POLICY_FILE` ergänzen. Kein Fallback auf General/APT und +keine Änderung deren bestehender Umgebung. Die zentrale Rootpolicy braucht +Schema2 und einen ausdrücklich begrenzten Scope mit neuen exakten Helperpins +sowie `allowed_script_profiles=["full_maintenance_v1"]`. Aktuelle Rechte beider +Planakteure, Helper-Sync, TrustedClock, ursprünglicher Grant und Einmalverbrauch +bleiben zusätzlich erforderlich. Eine Policy ist keine Jobgenehmigung. + +Vor Installation werden zusammengesetzte systemd-Unit und etwaige Drop-ins +geprüft: Vollwartung darf keine General-Sandbox erben, die Hostdateisystem oder +Netzwerk unerwartet sperrt. Dienst- und Binärwechsel nur mit qualifizierten +Artefakten und explizitem Erhaltungsplan. Insbesondere darf eine neue allgemeine +Helperbinary nicht still die alten APT-Codepins überschreiben/öffnen. Gegebenenfalls +eigenen qualifizierten Binärpfad für die neue FullMaintenanceunit wählen und die +bestehenden Providerpfade unverändert erhalten. Native Erhaltung und Sync nachweisen, +bevor neue Capabilities/Scopes geöffnet werden. + +Erster tatsächlicher FullMaintenancejob nur nach bereitgestellter WebUI und +expliziter Genehmigung der unveränderlichen Skript-/Profil-/Ziel-/Rechtebytes. +Abgestimmter Scope: harmlose Identität/stdout/stderr und kontrollierte lesende +Dateisystem-/Netzwerkbeobachtung; kein Betriebssystemupdate oder Neustart. Volle +Hostautorität wird sichtbar erklärt; kein Sandbox-, No-Reboot- oder Rollbackversprechen. +Windows-SYSTEM folgt nach eigenem Clock-/Pipe-/Profilnachweis. Die vorhandene +PipeV2-Datei ist unverändert SHA256 +`ce525dab23f2e4faa2c9816280c1a5a8b40e56d0e63a350256e83b2df1d9229d`; +ursprüngliche native Pipebelege werden nicht als Belege der neuen Runtime ausgegeben. + +## Anschließend auf der tatsächlichen Cloud-Linux-VM bestanden + +Root führte ausschließlich den korrigierten Pack29461892 nativ aus: Fall +`7b61ce46-7913-492b-9c6b-414a0c2b6ffc`, **84PASS,5explizite DBignored**. +Die vier vollständigen tatsächlichen Listen wurden vor irgendeinem Unitstart +exakt geprüft; anschließend liefen alle vier festen ELFs im eigenen Scratch, +mit geleerter Umgebung, Netzwerknamespace und Ressourcenlimits. Die5DB-Verträge +bleiben diesem VM-Lauf ausdrücklich nicht zugerechnet; sie sind im separaten +isolierten45er-PostgreSQL-Lauf belegt. Kein stiller Testskip oder Produktjob. + +Beleg `passed-e214cd9b-21ac-48ce-82dc-d10364329d04.json`, SHA256 +`a831a70a3cf76a3e8a9bf5820dcca7100d2ef9860dd9388539fe9c13f949f470`. +Keine Agent-/Helperinstallation, Änderung bestehender Dienste/Policies/PKI/ +Datenbanken, veröffentlichte Capability oder WebUI-Genehmigung durch diesen +Unitlauf. Der oben ursprünglich geplante Unitaufruf ist damit abgeschlossen. +Der inzwischen erhaltend ausgeführte Bootstrap steht in den separaten +[Bootstrap-Ergebnissen](../../docs/script-console-bootstrap-results.md); +signierter Folgeabgleich und tatsächlicher FullMaintenancejob sind dadurch +noch nicht abgenommen. + +## Verbindlicher Generationsentscheid vor dem Bootstrap + +Ein eigener neuer Fullhelper bei weiterlaufendem altem Generalhelper reicht +nicht: Alle aktiven Leser des gemeinsamen Root-Ledgers müssen neue +`privileged_script_profile`-Jobbytes unabhängig dekodieren und prüfen können. +Alte Decoder verweigern selbst geschlossene neue Zeilen und können beim +Snapshot/Sync einen sticky Fault schreiben. Deshalb neuer Agent und aktive +General-/Fullhelper gemeinsam auf die neue qualifizierte Generation; bestehende +Generalunit/Sandbox/Config/Policy-/Pinbytes unverändert. APT bleibt disabled/ +inactive, ohne Endpoint und mit geschlossenen Scopes. Seine aktuelle Generation +war bereits laut vorherigem Runtimeindex nicht qualifiziert; historische +Abnahmen bleiben erhalten. Vor späterer Wiedereröffnung eigener APT-Stackpin- +/Generationnachweis, kein alter Helper gegen neue Ledgerzeilen. + +Der Rootprozess hat die84 native Cloud-Linux-Units tatsächlich ausgeführt: +Fall `7b61ce46-7913-492b-9c6b-414a0c2b6ffc`, Proof-SHA256 +`a831a70a3cf76a3e8a9bf5820dcca7100d2ef9860dd9388539fe9c13f949f470`, +84PASS und5 ausdrücklich ausgelassene DB-Fälle. Diese5 sind im isolierten lokalen +45er-Lauf bestanden. Keine RMM-Jobs oder neue Capabilities aus diesem Unitlauf; +der inzwischen ausgeführte Runtimeaustausch ist separat belegt. Tatsächlicher +signierter Folgeabgleich und genehmigter Fulljob bleiben eigene offene Schritte. + +Details und Nach-MVP-Entdeckung: +[script-console-helper-generations.md](../../docs/script-console-helper-generations.md). diff --git a/deploy/linux/rmm-full-maintenance-helper.service b/deploy/linux/rmm-full-maintenance-helper.service new file mode 100644 index 0000000..6ff8577 --- /dev/null +++ b/deploy/linux/rmm-full-maintenance-helper.service @@ -0,0 +1,31 @@ +[Unit] +Description=RMM explicitly approved full host maintenance script helper +After=time-sync.target + +[Service] +Type=simple +# Deliberate full host authority for separately approved full_maintenance_v1 +# scripts. This is NOT a sandbox and cannot promise no reboot or agent changes. +User=root +Group=rmm-agent +StateDirectory=rmm-helper +StateDirectoryMode=0700 +RuntimeDirectory=rmm-helper-maintenance +RuntimeDirectoryMode=0750 +UMask=0077 +Environment=RMM_HELPER_CONFIG=/etc/rmm-remote/helper-full-maintenance.json +ExecStart=/usr/local/libexec/rmm-privileged-helper +Restart=on-failure +RestartSec=5 +# Retain the host filesystem, network, home paths, capabilities and syscalls. +# General/APT units keep their existing restrictions. No inherited hardening +# drop-in may silently contradict this profile; inspect merged unit predeploy. +PrivateTmp=false +PrivateNetwork=false +ProtectSystem=false +ProtectHome=false +NoNewPrivileges=false +KillMode=control-group + +[Install] +WantedBy=multi-user.target diff --git a/deploy/script_console_acceptance.py b/deploy/script_console_acceptance.py new file mode 100644 index 0000000..c6634de --- /dev/null +++ b/deploy/script_console_acceptance.py @@ -0,0 +1,251 @@ +#!/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) diff --git a/deploy/script_console_bootstrap.py b/deploy/script_console_bootstrap.py new file mode 100644 index 0000000..da9e596 --- /dev/null +++ b/deploy/script_console_bootstrap.py @@ -0,0 +1,245 @@ +#!/usr/bin/env python3 +"""Prepare the exact preserving bootstrap intent and a READ-ONLY target CAS. + +There is deliberately no install/stop/start/policy operation in this preflight. +A runtime index is provenance, never a fresh target observation. Root reviews +this source before invoking snapshot; no client is installed on the notebook. +""" +import argparse +import base64 +import hashlib +import inspect +import json +from pathlib import Path +import uuid + +ROOT = Path(__file__).resolve().parents[1] +ACCESS_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery') +SUPPORT = { + 'deploy/script_console_acceptance.py': '2b3ba8d548d18627643a9f73c9a4f7bacce4ac2cd5495745c6760d088e078145', + 'deploy/windows_msi_runtime_acceptance.py': '833ad8806c90a19342aa6fbc71c33e81975caffb1746550e9b0fbdca7fa0c7ab', + 'deploy/vm_access_check.py': '5c8107ea9151a9f498b2530cdc61c1a7a6d745aff853a9b05710f3395838c0dd', +} +for name, digest in SUPPORT.items(): + path = ROOT / name + if path.is_symlink() or hashlib.sha256(path.read_bytes()).hexdigest() != digest: + raise RuntimeError('Pinned preflight support source changed') +import script_console_acceptance as native +support = native.support +INDEX = ACCESS_ROOT / 'devstate/cloud-shared-core-current-runtime.json' +INDEX_SHA = 'bf058d30ec7252071acbf39aa09007a083a0d237054a1f0815caf8e8510ce145' +PRIOR = ACCESS_ROOT / 'devstate/shared-core/runtime-upgrade-v2/a2423612-721f-4a6c-a832-b9eef549c41e/intent.json' +PRIOR_SHA = '4a1792151d95b26245464ed9df22a439d0979026c5d88052926b76985495bfc4' +NATIVE = ROOT / 'devstate/script-console-native/7b61ce46-7913-492b-9c6b-414a0c2b6ffc/passed-e214cd9b-21ac-48ce-82dc-d10364329d04.json' +NATIVE_SHA = 'a831a70a3cf76a3e8a9bf5820dcca7100d2ef9860dd9388539fe9c13f949f470' +STATE = ROOT / 'devstate/script-console-bootstrap' + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def snapshot_vm(p): + import base64 + import contextlib + import hashlib + import json + import os + from pathlib import Path + import sqlite3 + import stat + import subprocess + require(os.geteuid() == 0 and os.uname().machine == 'x86_64' + and p['type'] == 'rmm.script-console.preserving-preflight-v1', 'fixed root preflight') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip() == p['boot_id'], 'same actual boot') + + def sha(data): + return hashlib.sha256(data).hexdigest() + + def file(path, owner=0): + path = Path(path); s = path.lstat() + require(not stat.S_ISLNK(s.st_mode) and stat.S_ISREG(s.st_mode) + and s.st_uid == owner and s.st_nlink == 1 and not s.st_mode & 0o022, 'safe fixed file') + return dict(device=s.st_dev, inode=s.st_ino, mode=stat.S_IMODE(s.st_mode), uid=s.st_uid, + gid=s.st_gid, bytes=s.st_size, sha256=sha(path.read_bytes())) + + def service(name): + result = subprocess.run(['systemctl', 'show', name, '-p', 'ActiveState', '-p', 'MainPID', + '-p', 'UnitFileState', '-p', 'LoadState'], capture_output=True, check=False, timeout=10) + value=dict(line.split('=',1) for line in result.stdout.decode().splitlines()) + if name == 'rmm-full-maintenance-helper.service': + require(result.returncode in (0,1) and value.get('LoadState') == 'not-found' + and value.get('ActiveState') == 'inactive' and value.get('MainPID') == '0', + 'only exact new absent unit admits rc0/1') + else: + require(result.returncode == 0, 'known unit query strictly succeeds') + return value + + units = {name: service(name) for name in ('rmm-agent-remote.service', 'rmm-helper-cloud.service', + 'rmm-apt-helper.service', 'rmm-agent.service', 'rmm-helper-dev-test.service', + 'rmm-full-maintenance-helper.service')} + require(all(units[n]['ActiveState'] == 'active' for n in ('rmm-agent-remote.service', 'rmm-helper-cloud.service')), + 'known original active pair') + require(all(units[n]['ActiveState'] == 'inactive' and units[n]['UnitFileState'] == 'disabled' + for n in ('rmm-apt-helper.service', 'rmm-agent.service', 'rmm-helper-dev-test.service')), + 'APT and legacy daemons remain disabled/inactive') + require(units['rmm-full-maintenance-helper.service']['LoadState'] == 'not-found', 'new Full unit absent') + processes = {} + for name, key, uid in (('rmm-agent-remote.service', 'agent', 993), ('rmm-helper-cloud.service', 'helper', 0)): + pid = int(units[name]['MainPID']); require(pid > 1, 'actual live service PID') + proc=Path('/proc',str(pid)) + def start_ticks(): + raw=(proc/'stat').read_text(); _,fields=raw.rsplit(') ',1) + return int(fields.split()[19]) + before_ticks=start_ticks() + status = (proc/'status').read_text().splitlines() + ids = {line.split(':')[0]: list(map(int, line.split()[1:])) for line in status if line.startswith(('Uid:', 'Gid:'))} + require(ids['Uid'] == [uid] * 4 and ids['Gid'] == [984] * 4, 'actual qualified account identity') + image = Path('/proc', str(pid), 'exe') + image_hash = sha(image.read_bytes()) + require(image_hash == p['old_artifacts'][key], 'actual running original code generation') + require(start_ticks() == before_ticks and int(service(name)['MainPID']) == pid, + 'same service PID and startticks through code observation') + processes[key] = dict(pid=pid, start_ticks=before_ticks, uids=ids['Uid'], gids=ids['Gid'], sha256=image_hash) + fixed = { + '/usr/local/libexec/rmm-agent-remote': p['old_artifacts']['agent'], + '/usr/local/libexec/rmm-privileged-helper': p['old_artifacts']['helper'], + '/usr/lib/rmm-apt-transaction/apt_transaction.py': p['old_artifacts']['provider'], + '/etc/rmm-remote/helper-general.json': p['general_config_sha256'], + '/etc/rmm-remote/general-helper-proof.json': p['general_pin_sha256'], + '/etc/systemd/system/rmm-agent-remote.service': p['agent_unit_sha256'], + '/etc/systemd/system/rmm-helper-cloud.service': p['general_unit_sha256'], + } + controls = {} + for path, digest in fixed.items(): + value = file(path); require(value['sha256'] == digest, 'exact original runtime/control CAS') + controls[path] = value + for path in ('/etc/rmm/apt-helper.json', '/etc/rmm-remote/apt-helper-proof.json', + '/etc/systemd/system/rmm-apt-helper.service', '/usr/lib/rmm-apt-transaction/hook', + '/var/lib/dpkg/status', '/usr/share/rmm-mvp-test/version'): + controls[path] = file(path) + environment = subprocess.run(['systemctl', 'show', 'rmm-agent-remote.service', '-p', 'Environment', '--value'], + capture_output=True, check=True, timeout=10).stdout + require(b'RMM_APT_HELPER_SOCKET=' not in environment and b'RMM_APT_HELPER_PROOF_FILE=' not in environment, + 'APT endpoint remains absent') + # Preserve the exact previously proven hardlink exception, never generalize it. + aliases = [Path('/var/lib/rmm-apt-transaction') / name for name in + ('acceptance-rmm-apt-fixture-update-1.1.json', 'pending-acceptance-rmm-apt-fixture-update-1.1.json')] + alias_identity = None + for path in aliases: + s = path.lstat() + require(not path.is_symlink() and stat.S_ISREG(s.st_mode) and s.st_nlink == 2 + and stat.S_IMODE(s.st_mode) == 0o600 and s.st_uid == 0 and s.st_gid == 984 + and sha(path.read_bytes()) == p['apt_alias_sha256'], 'exact known historical APT alias') + identity = (s.st_dev, s.st_ino) + require(identity == (2049,1028362), 'unchanged independently proven APT alias inode') + require(alias_identity is None or identity == alias_identity, 'same original alias inode') + alias_identity = identity + databases = {} + for path, expected in p['database_inodes'].items(): + path = Path(path); s = path.lstat(); owner = 0 if path.parent.name == 'rmm-helper' else 993 + require(not path.is_symlink() and stat.S_ISREG(s.st_mode) and s.st_nlink == 1 and s.st_uid == owner + and [s.st_dev, s.st_ino] == expected, 'same original database inode/owner') + require(all(not Path(str(path) + suffix).exists() and not Path(str(path) + suffix).is_symlink() + for suffix in ('-wal', '-shm', '-journal')), 'settled DELETE journal required') + with contextlib.closing(sqlite3.connect(path.as_uri() + '?mode=ro', uri=True)) as db: + db.execute('PRAGMA query_only=ON'); db.execute('BEGIN') + require(db.execute('PRAGMA journal_mode').fetchone()[0] == 'delete' + and db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok', 'actual query-only journal integrity') + schemas = dict(db.execute("SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name")) + if path.name == 'journal.sqlite': + require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0] == 0, + 'no unfinished agent attempt') + require(db.execute('SELECT count(*) FROM reconcile_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'no agent fault') + if path.parent.name == 'rmm-helper': + rows = db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY grant_id').fetchall() + row_hashes = {r[0]: sha(json.dumps([dict(sha256=sha(v), size=len(v)) if isinstance(v, bytes) else v for v in r], separators=(',', ':')).encode()) for r in rows} + require(row_hashes == p['root_rows'] and len(rows) == 18 and all(r[5] is not None for r in rows), 'exact original closed Root18') + require(db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'no helper fault') + databases[str(path)] = dict(device=s.st_dev, inode=s.st_ino, schema_sha256=sha(json.dumps(schemas, sort_keys=True).encode()), integrity='ok') + require([path.stat().st_dev, path.stat().st_ino] == expected, 'journal was not replaced during read') + binding = json.loads(Path('/var/lib/rmm-agent-remote/binding.json').read_bytes()) + require(all(binding[k] == v for k, v in p['target'].items()) + and all(binding[k] == p['helper_identity'][k] for k in ('enrollment_generation','assignment_version')) + and sha(bytes.fromhex(binding['public_key_hex'])) == p['helper_identity']['signing_key_id'], 'same enrolled target/generations/public signing identity') + config = json.loads(Path('/etc/rmm-remote/helper-general.json').read_bytes()) + require(config['agent_uid'] == 993 and config['socket_gid'] == 984 + and config['state'] == '/var/lib/rmm-helper' and config['allow_reboot'] is False, 'unchanged existing helper identity/profile') + for path in ('/etc/rmm-remote/helper-full-maintenance.json','/etc/rmm-remote/full-maintenance-helper-proof.json', + '/etc/systemd/system/rmm-agent-remote.service.d/60-full-maintenance.conf'): + require(not os.path.lexists(path), 'new Full paths absent; do not overwrite partial bootstrap') + full = dict(profile='full_maintenance_v1', binding=config['binding'], agent_uid=993, socket_gid=984, + socket='/run/rmm-helper-maintenance/control.sock', state='/var/lib/rmm-helper', + allowed_services=[], allow_reboot=False, allow_root_scripts=True, + allowed_script_profiles=['full_maintenance_v1'], helper_binary_sha256=p['new_artifacts']['helper']) + if config.get('signing_key_file') is not None: + full['signing_key_file'] = config['signing_key_file'] + full_bytes = json.dumps(full, separators=(',', ':')).encode() + return dict(type=p['type'], id=p['id'], boot_id=p['boot_id'], target=p['target'], processes=processes, + controls=controls, databases=databases, root_consumed=18, root_unresolved=0, + apt_endpoint_enabled=False, apt_current_runtime_qualified=False, + full_config_candidate=dict(path='/etc/rmm-remote/helper-full-maintenance.json', + mode=0o600, uid=0, gid=0, bytes_b64=base64.b64encode(full_bytes).decode(), sha256=sha(full_bytes)), + server_gate_verified=False, fresh_target_cas=True, install_authorized=False, + services_changed=False, product_state_writes=False, rmm_jobs=False, grants_created=False, + capabilities_activated=False, approvals=False, reboot_requested=False, package_effects=False) + + +def local_intent(case_id=None): + import os + os.umask(0o077); STATE.mkdir(mode=0o700, parents=True, exist_ok=True) + support.private(STATE, True) + for path, digest in ((INDEX, INDEX_SHA), (PRIOR, PRIOR_SHA), (NATIVE, NATIVE_SHA)): + require(support.sha(path) == digest, 'exact prior provenance/native proof') + index = support.load(INDEX); old = support.load(PRIOR); proof = support.load(NATIVE) + require(index['verified_passive_runtime_upgrade'] is True and index['apt_endpoint_enabled'] is False + and index['apt_current_runtime_qualified'] is False and index['root_consumed'] == 18 + and index['root_unresolved'] == 0 and index['central_counts'] == dict(jobs=30, outbox=30, permits=26, unresolved=0), 'known closed prior index, not a fresh target proof') + require(proof['verified'] is True and proof['manifest_sha256'] == native.MANIFEST_SHA + and proof['native']['passed'] == 84 and proof['native']['ignored_database_cases'] == 5, 'actual Linux-first84 proof') + manifest = support.load(native.PACK / 'manifest.json') + require(support.sha(native.PACK / 'manifest.json') == native.MANIFEST_SHA + and all(support.sha(ROOT / name) == value for name, value in manifest['source_files'].items()), 'frozen158 source qualification') + if case_id is not None: + require(str(uuid.UUID(case_id)) == case_id, 'canonical original local case') + folder = STATE / case_id; intent = support.load(folder / 'intent.json') + require(intent['runner_sha256'] == support.sha(__file__) and intent['support_files'] == SUPPORT + and intent['index_sha256'] == INDEX_SHA and intent['manifest_sha256'] == native.MANIFEST_SHA, + 'original immutable preflight intent') + return folder, intent + case_id = str(uuid.uuid4()); folder = STATE / case_id; folder.mkdir(mode=0o700) + intent = dict(type='rmm.script-console.preserving-preflight-v1', id=case_id, runner_sha256=support.sha(__file__), + support_files=SUPPORT, index_sha256=INDEX_SHA, previous_intent_sha256=PRIOR_SHA, + manifest_sha256=native.MANIFEST_SHA, native_proof_sha256=NATIVE_SHA, + boot_id=index['boot_id'], target=index['target'], helper_identity=old['general_pin']['identity'], old_artifacts=index['artifact_sha256'], + new_artifacts={key: manifest['artifacts'][name]['sha256'] for key, name in + (('agent', 'rmm-agent'), ('helper', 'rmm-privileged-helper'))}, + root_rows=old['root_rows'], database_inodes=old['database_inodes'], apt_alias_sha256=old['alias_sha256'], + general_config_sha256=old['config_sha256'], general_pin_sha256=old['pin_sha256'], + agent_unit_sha256=old['agent_unit_sha256'], general_unit_sha256=old['general_unit_sha256'], + require_fresh_target_cas=True, require_fresh_server_gate=True, install_authorized=False) + support.save(folder / 'intent.json', intent) + return folder, intent + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('mode', choices=('prepare-local', 'snapshot', 'read-local')) + parser.add_argument('--case') + args = parser.parse_args() + require((args.mode == 'prepare-local') == (args.case is None), 'prepare new local intent; snapshot/read need original case') + folder, intent = local_intent(args.case) + if args.mode == 'snapshot': + for name, digest in SUPPORT.items(): + require(support.sha(ROOT / name) == digest, 'support unchanged before target read') + code = inspect.getsource(require) + '\n' + inspect.getsource(snapshot_vm) + code += '\ntry:\n print(__import__("json").dumps(snapshot_vm(' + repr(intent) + ')))\nexcept Exception as e:\n print(__import__("json").dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n' + prefix = support.access_settings(ACCESS_ROOT, 'linux') + report = support.transport(prefix, 'python3 -', code.encode(), folder, timeout=60) + support.save(folder / ('snapshot-' + str(uuid.uuid4()) + '.json'), report) + require(report['fresh_target_cas'] is True and report['install_authorized'] is False + and all(report.get(k) is False for k in ('services_changed', 'product_state_writes', 'rmm_jobs', + 'grants_created', 'capabilities_activated', 'approvals', 'reboot_requested', 'package_effects')), 'strict read-only preflight scope') + print(json.dumps(dict(case_id=intent['id'], intent=str(folder / 'intent.json'), sha256=support.sha(folder / 'intent.json'), + mode=args.mode, install_authorized=False, target_called=args.mode == 'snapshot'))) diff --git a/deploy/script_console_build.py b/deploy/script_console_build.py new file mode 100644 index 0000000..befdb54 --- /dev/null +++ b/deploy/script_console_build.py @@ -0,0 +1,152 @@ +#!/usr/bin/env python3 +"""Offline source qualification and Linux delivery pack. No SSH or deployment. + +This tool never enrolls an agent, starts a daemon, edits a policy or approves a +job. Linux/Windows VM execution and full-profile activation remain separate. +""" +import datetime as dt +import hashlib +import json +import os +from pathlib import Path +import shutil +import subprocess +import tarfile +import time +import uuid + +ROOT = Path(__file__).resolve().parents[1] +LINUX_IMAGE = 'sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6' +WINDOWS_IMAGE = 'sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9' +POSTGRES_IMAGE = 'sha256:567d5d704a3e5a4864393b623a0d50eb1b5a4ad134cdac7cf19555cb2d8ffe9e' + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def inputs(): + paths = [ROOT / p for p in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', + 'tests/backend_root_logs.rs', 'tests/script_console/codec-golden.json', + 'tests/apt_transaction/fixtures/planning-canonical.json', + 'tools/apt-transaction/apt_transaction.py', 'tools/apt-transaction/hook', + 'deploy/linux/rmm-apt-helper.service', + 'deploy/linux/rmm-full-maintenance-helper.service', 'deploy/script_console_build.py')] + paths += [p for p in (ROOT / 'crates').rglob('*') + if p.is_file() and p.suffix in ('.rs', '.toml', '.c', '.h', '.idl', '.sql')] + paths += [p for p in (ROOT / 'tests').rglob('*.rs')] + paths += [p for p in (ROOT / 'migrations').rglob('*.sql')] + paths += [p for p in (ROOT / 'proto').rglob('*') if p.is_file() and p.suffix == '.proto'] + for path in paths: + if path.is_symlink(): + raise RuntimeError('Symlink compiler input refused: ' + str(path.relative_to(ROOT))) + return {str(p.relative_to(ROOT)): sha(p) for p in sorted(set(paths))} + + +def main(): + os.umask(0o077) + state = ROOT / 'devstate/script-console-qualification' + state.mkdir(mode=0o700, parents=True, exist_ok=True) + out = state / ('run-' + str(uuid.uuid4())) + out.mkdir(mode=0o700) + frozen = inputs() + checks = {} + + def command(label, args): + result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False) + log = out / (label + '.log') + log.write_bytes(result.stdout) + checks[label] = {'exit_code': result.returncode, 'log_sha256': sha(log)} + if result.returncode: + raise RuntimeError(label + ' failed; inspect private log ' + str(log)) + return result.stdout + + def cargo(label, image, *args): + target = ROOT / ('devstate/script-console-windows-target' if image == WINDOWS_IMAGE else 'devstate/script-console-target') + target.mkdir(mode=0o700, exist_ok=True) + return command(label, ['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, *args]) + + common = ['--manifest-path', '/src/Cargo.toml', '--locked', '--offline'] + cargo('format', LINUX_IMAGE, 'fmt', '--manifest-path', '/src/Cargo.toml', '--all', '--check') + cargo('linux-clippy', LINUX_IMAGE, 'clippy', *common, '--workspace', '--all-targets', '--', '-D', 'warnings') + cargo('linux-tests', LINUX_IMAGE, 'test', *common, '--workspace', '--all-targets') + # Existing ignored DB contracts use their own fresh PostgreSQL, internal + # network namespace only. No published port, production URL or real device. + pg_name='rmm-script-contract-'+uuid.uuid4().hex[:12] + command('postgres-start',['docker','run','-d','--network','none','--name',pg_name,'-e','POSTGRES_HOST_AUTH_METHOD=trust',POSTGRES_IMAGE]) + try: + for _ in range(100): + ready=subprocess.run(['docker','exec',pg_name,'pg_isready','-h','127.0.0.1','-U','postgres'],capture_output=True) + if ready.returncode == 0: break + time.sleep(.2) + else: raise RuntimeError('Isolated PostgreSQL did not become ready') + sql='CREATE ROLE rmm_app LOGIN NOINHERIT NOSUPERUSER NOBYPASSRLS;\n'+''.join(p.read_text()+'\n' for p in sorted((ROOT/'migrations').glob('*.sql'))) + result=subprocess.run(['docker','exec','-i',pg_name,'psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres'],input=sql.encode(),capture_output=True) + (out/'postgres-migrations.log').write_bytes(result.stdout+result.stderr) + if result.returncode: raise RuntimeError('Isolated migrations failed') + command('postgres-contracts',['docker','run','--rm','--network','container:'+pg_name,'--read-only','--tmpfs','/tmp:rw,size=512m','-v',str(ROOT)+':/src:ro','-v',str(ROOT/'devstate/script-console-target')+':/target','-e','CARGO_TARGET_DIR=/target','-e','RMM_TEST_REPAIR_ADMIN_URL=postgres://postgres@127.0.0.1/postgres','-e','RMM_TEST_REPAIR_APP_URL=postgres://rmm_app@127.0.0.1/postgres','--entrypoint','cargo',LINUX_IMAGE,'test',*common,'-p','rmm-server','--bin','rmm-server','--','--include-ignored','--test-threads=1']) + finally: + command('postgres-remove',['docker','rm','-f',pg_name]) + for feature in ('default', 'system-helper'): + cargo('windows-' + feature + '-clippy', WINDOWS_IMAGE, 'clippy', *common, + '--target', 'x86_64-pc-windows-gnu', '-p', 'rmm-agent-windows', '--all-targets', + *(['--features', 'system-helper'] if feature == 'system-helper' else []), '--', '-D', 'warnings') + raw = cargo('linux-unit-link', LINUX_IMAGE, 'test', *common, '--workspace', '--all-targets', + '--target', 'x86_64-unknown-linux-gnu', '--release', '--no-run', '--message-format=json') + selected = {'rmm_domain': 'domain-tests', 'rmm_agent_linux': 'linux-runtime-tests', + 'rmm-privileged-helper': 'helper-policy-tests', 'rmm-server': 'server-contract-tests'} + artifacts = {} + for line in raw.splitlines(): + try: + value = json.loads(line) + except ValueError: + continue + target = value.get('target', {}) + name = target.get('name') + if (value.get('reason') == 'compiler-artifact' and value.get('executable') + and value.get('profile', {}).get('test') and name in selected + and target.get('kind') in (['lib'], ['bin'])): + label = selected[name] + if label in artifacts: + raise RuntimeError('Duplicate selected test artifact: ' + label) + source = ROOT / 'devstate/script-console-target' / Path(value['executable']).relative_to('/target') + artifact = out / label + shutil.copyfile(source, artifact) + artifacts[label] = {'sha256': sha(artifact), 'bytes': artifact.stat().st_size, + 'native_linux_executed': False, 'native_windows_executed': False} + if set(artifacts) != set(selected.values()): + raise RuntimeError('Incomplete Linux unit artifact set') + for label in artifacts: + command(label + '-list', ['docker', 'run', '--rm', '--network', 'none', '--read-only', + '--tmpfs', '/tmp:rw,exec,size=128m', '-v', str(out / label) + ':/unit:ro', + '--entrypoint', 'python3', LINUX_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,cwd="/tmp")']) + cargo('linux-binary-link', LINUX_IMAGE, 'build', *common, '--release', + '--target', 'x86_64-unknown-linux-gnu', '-p', 'rmm-agent', '-p', 'rmm-privileged-helper', '-p', 'rmm-server', '--bins') + for name in ('rmm-agent', 'rmm-privileged-helper', 'rmm-server'): + artifact = out / name + shutil.copyfile(ROOT / 'devstate/script-console-target/x86_64-unknown-linux-gnu/release' / name, artifact) + artifacts[name] = {'sha256': sha(artifact), 'bytes': artifact.stat().st_size, + 'installed': False, 'native_executed': False} + if inputs() != frozen: + raise RuntimeError('Compiler/deployment inputs changed during qualification; no deployable manifest') + archive = out / 'reviewed-sources.tar.gz' + with tarfile.open(archive, 'w:gz') as tar: + for name in frozen: + tar.add(ROOT / name, arcname=name, recursive=False) + manifest = {'schema_version': 1, 'type': 'rmm.script-console.local-source-qualification', + 'created_at': dt.datetime.now(dt.timezone.utc).isoformat(), 'profile': 'full_maintenance_v1', + 'images': {'linux': LINUX_IMAGE, 'windows_cross': WINDOWS_IMAGE, 'isolated_postgres': POSTGRES_IMAGE}, 'source_files': frozen, + 'source_before_after_equal': True, 'checks': checks, 'artifacts': artifacts, + 'source_archive_sha256': sha(archive), 'native_linux_executed': False, + 'native_windows_executed': False, 'target_state_writes': False, 'rmm_jobs': False, + 'capabilities_activated': False, 'approvals': False} + path = out / 'manifest.json' + path.write_text(json.dumps(manifest, indent=2) + '\n') + print(json.dumps({'manifest': str(path), 'sha256': sha(path), 'native_executed': False})) + + +if __name__ == '__main__': + main() diff --git a/deploy/script_console_continue.py b/deploy/script_console_continue.py new file mode 100644 index 0000000..ea96ee2 --- /dev/null +++ b/deploy/script_console_continue.py @@ -0,0 +1,257 @@ +#!/usr/bin/env python3 +"""One preserving continuation of the installed, never-started Linux Full pair. + +No stop, swap, install, rollback, SQL write, job or approval. Parent executes after +review; absent original restart intent and exact fresh pre-start CAS required. +""" +import argparse +import base64 +import hashlib +import inspect +import json +import os +from pathlib import Path +import uuid + +ROOT=Path(__file__).resolve().parents[1] +INSTALL_SHA='667b56f7e5f085db249f27a7094919c7c0e6055dabc9f0600cb261c34760cd26' +if hashlib.sha256((ROOT/'deploy/script_console_install.py').read_bytes()).hexdigest()!=INSTALL_SHA: + raise RuntimeError('Original install source must stay unchanged') +import script_console_install as old +support=old.support +ORIGINAL=ROOT/'devstate/script-console-install/2e27c312-58cc-444d-92a6-863a11ac6ffd' +ORIGINAL_SHA='8aec7bf7df13bc4968f47ca1554c813406409530613ce95241e74a81204cd02e' +READBACK=ORIGINAL/'readback-36f94d52-caf4-4b63-ac07-827edda4f806.json' +READBACK_SHA='b8964cd12ffd57226081980edfa1d33c99718fcb470979a29aa02a8af8fbd9ba' +STATE=ROOT/'devstate/script-console-continue' + + +def require(value,code): + if not value:raise RuntimeError(code) + + +def guards(): + require(support.sha(ROOT/'deploy/script_console_install.py')==INSTALL_SHA + and support.sha(ROOT/'deploy/script_console_bootstrap.py')==old.PREFLIGHT_SHA + and support.sha(ORIGINAL/'intent.json')==ORIGINAL_SHA and support.sha(READBACK)==READBACK_SHA, + 'exact original install/readback/source') + old.pre.native.verify_support() + manifest=support.load(old.pre.native.PACK/'manifest.json') + require(support.sha(old.pre.native.PACK/'manifest.json')==old.pre.native.MANIFEST_SHA + and support.sha(old.pre.NATIVE)==old.pre.NATIVE_SHA and len(manifest['source_files'])==158 + and all(support.sha(ROOT/n)==h for n,h in manifest['source_files'].items()),'unchanged158 inputs and native Linux84') + + +def continue_vm(request): + import base64 + import datetime as dt + import fcntl + import hashlib + import json + import os + from pathlib import Path + import sqlite3 + import stat + import subprocess + import traceback + import uuid + os.umask(0o077) + p=request['original'];ident=p['id'];recovery=str(uuid.UUID(request['id'])) + require(recovery==request['id'] and ident=='2e27c312-58cc-444d-92a6-863a11ac6ffd' + and request['action'] in ('continue','read') and os.geteuid()==0 and os.uname().machine=='x86_64','fixed root continuation') + case=Path('/var/lib/rmm-bootstrap-history')/('script-console-v1-'+ident) + # Exact reviewed original helpers are copied below, no installation branch. + def encode(v):return json.dumps(v,sort_keys=True,separators=(',',':')).encode() + def sha(b):return hashlib.sha256(b).hexdigest() + def fsync_dir(path): + fd=os.open(path,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW) + try:os.fsync(fd) + finally:os.close(fd) + def create(path,raw,mode=0o600,gid=0): + fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode) + with os.fdopen(fd,'wb') as out: + out.write(raw);out.flush();os.fchmod(out.fileno(),mode);os.fchown(out.fileno(),0,gid);os.fsync(out.fileno()) + fsync_dir(path.parent) + def safe(path,directory=False,owner=0): + m=path.lstat();require(not path.is_symlink() and m.st_uid==owner and not m.st_mode&0o022 + and (stat.S_ISDIR(m.st_mode) if directory else stat.S_ISREG(m.st_mode) and m.st_nlink==1),'safe owned path') + return m + def run(args,**kwargs):return subprocess.run(args,capture_output=True,check=True,timeout=25,**kwargs).stdout + def svc(name): + return dict(line.split('=',1) for line in run(['systemctl','show',name,'-p','ActiveState','-p','MainPID','-p','UnitFileState']).decode().splitlines()) + def retained_files(): + # Never open private device keys. Retain all file identities, sizes, + # modes and change timestamps; SQL content has its own full row witness. + value={} + for root in (Path('/var/lib/rmm-agent-remote'),Path('/var/lib/rmm-helper')): + for path in [root,*sorted(root.rglob('*'))]: + m=path.lstat();require(not path.is_symlink() and (stat.S_ISDIR(m.st_mode) or stat.S_ISREG(m.st_mode)),'no unexpected retained state object') + if str(path).endswith(('-journal','-wal','-shm')):raise RuntimeError('Settled journal required') + value[str(path)]=[m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_size,m.st_mtime_ns,m.st_ctime_ns] + return value + def sql_witness(): + result={} + for name,identity in p['preflight']['database_inodes'].items(): + path=Path(name);m=path.lstat();require([m.st_dev,m.st_ino]==identity and not path.is_symlink(),'preserved DB identity') + require(all(not os.path.lexists(str(path)+suffix) for suffix in ('-wal','-shm','-journal')),'no live SQLite sidecars') + db=sqlite3.connect(path.as_uri()+'?mode=ro',uri=True) + try: + db.execute('PRAGMA query_only=ON');db.execute('BEGIN') + require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','retained SQL integrity') + tables=dict(db.execute("SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name")) + rows={} + for table in tables: + require(table.replace('_','').isalnum(),'fixed SQLite identifier') + rows[table]=[sha(encode([dict(blob_sha256=sha(v),bytes=len(v)) if isinstance(v,bytes) else v for v in row])) + for row in db.execute('SELECT rowid,* FROM "'+table+'" ORDER BY rowid')] + result[name]=dict(schema=tables,rows=rows,identity=identity) + if path.name=='journal.sqlite': + require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'no running job') + if path.parent.name=='rmm-helper': + current=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY grant_id').fetchall() + exact={r[0]:sha(json.dumps([dict(sha256=sha(v),size=len(v)) if isinstance(v,bytes) else v for v in r],separators=(',',':')).encode()) for r in current} + require(exact==p['preflight']['root_rows'] and len(exact)==18 and all(r[5] is not None for r in current),'exact retained Root18') + finally:db.close() + return result + def description(path): + if not os.path.lexists(path):return dict(present=False) + m=path.lstat();require(not path.is_symlink() and stat.S_ISREG(m.st_mode),'fixed regular public file') + return dict(present=True,device=m.st_dev,inode=m.st_ino,uid=m.st_uid,gid=m.st_gid, + mode=stat.S_IMODE(m.st_mode),bytes=m.st_size,symlink=False,regular=True,sha256=sha(path.read_bytes())) + def receipt(path): + safe(path);require(path.stat().st_size<=8*1024*1024,'bounded private receipt') + return json.loads(path.read_bytes()) + def services(): + value={} + for name in ('rmm-helper-cloud.service','rmm-full-maintenance-helper.service','rmm-agent-remote.service','rmm-apt-helper.service'): + reply=subprocess.run(['systemctl','show',name,'-p','LoadState','-p','ActiveState','-p','SubState','-p','MainPID','-p','UnitFileState'],capture_output=True,timeout=25) + value[name]=dict(exit_code=reply.returncode,properties=dict(line.split('=',1) for line in reply.stdout.decode().splitlines() if '=' in line)) + return value + safe(case,True);safe(case/'intent.json') + require((case/'intent.json').read_bytes()==encode(p),'complete original remote intent') + marker=case/'continuation-start-intent.json';result_path=case/'continuation-result.json' + if request['action']=='read': + return dict(id=recovery,readback_only=True,original_case=ident,services=services(), + original_report_historical=receipt(case/'report.json'), + original_restart_intent_present=os.path.lexists(case/'restart-intent.json'), + continuation_entered=os.path.lexists(marker),continuation_intent=receipt(marker) if os.path.lexists(marker) else None, + historical_continuation_result=receipt(result_path) if os.path.lexists(result_path) else None, + current_files={name:description(Path(path)) for name,path in (('agent','/usr/local/libexec/rmm-agent-remote'),('helper','/usr/local/libexec/rmm-privileged-helper'))}, + state_writes=False,rmm_jobs=False,approvals=False,retry_allowed=False) + gate=request['fresh_server_gate'] + require(gate['closed'] is True and gate['counts']==dict(jobs=30,outbox=30,permits=26,unresolved=0) + and gate['policy_sha256']==p['central_policy_sha256'],'fresh central scope still closed') + observed_at=dt.datetime.fromisoformat(gate['observed_at']) + require(observed_at.utcoffset() is not None and -5<=(dt.datetime.now(dt.timezone.utc)-observed_at).total_seconds()<=60,'fresh bounded server gate') + require(not os.path.lexists(case/'restart-intent.json') and not os.path.lexists(marker) + and not os.path.lexists(result_path),'no prior original or continuation start') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==p['preflight']['boot_id'],'same boot before continuation') + observed=request['original_readback'] + for name,expected in observed['markers'].items(): + current=description(case/(name+'.json')) + require(current=={k:v for k,v in expected.items() if k!='historical_content'},'original markers retained exactly') + require(description(case/'report.json')=={k:v for k,v in observed['historical_report'].items() if k!='historical_content'},'original failed report retained exactly') + report=receipt(case/'report.json') + require(report['stage']=='incomplete_manual_review' and report['preserved_before_restart'] is True + and report['completed_preserving_bootstrap'] is False and report['full_job_accepted'] is False,'exact installed-before-first-start phase') + states=services() + for name,item in states.items(): + require(item['exit_code']==0 and item['properties']==observed['services'][name]['properties'] + and item['properties']['ActiveState']=='inactive' and item['properties']['MainPID']=='0','all four services still inactive') + binaries={'agent':Path('/usr/local/libexec/rmm-agent-remote'),'helper':Path('/usr/local/libexec/rmm-privileged-helper')} + for key,path in binaries.items(): + require(description(path)==observed['files'][key+'_current'] and sha(path.read_bytes())==p['artifacts'][key]['sha256'],'same qualified installed image') + require(description(case/(key+'-previous'))==observed['files'][key+'_previous'],'same preserved old image') + require(not os.path.lexists(case/(key+'-next')),'no leftover image exchange') + for name,expected in p['baseline']['controls'].items(): + if name in [str(x) for x in binaries.values()]:continue + path=Path(name);m=path.lstat() + require(not path.is_symlink() and dict(device=m.st_dev,inode=m.st_ino,mode=stat.S_IMODE(m.st_mode),uid=m.st_uid,gid=m.st_gid,bytes=m.st_size,sha256=sha(path.read_bytes()))==expected,'all General/APT/package controls unchanged') + # GeneralPin/Identity serde declaration order is the original inspector's + # order. The historical report uses sorted object keys and is not the raw + # pin-file encoding. Reconstruct the original exact compact bytes explicitly. + full_pin={k:report['full_pin'][k] for k in ('schema_version','profile','identity','policy_sha256','helper_binary_sha256')} + full_pin['identity']={k:full_pin['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','signing_key_id')} + new_files={ + '/etc/rmm-remote/helper-full-maintenance.json':(base64.b64decode(p['baseline']['full_config_candidate']['bytes_b64'],validate=True),0o600,0), + '/etc/rmm-remote/full-maintenance-helper-proof.json':(json.dumps(full_pin,separators=(',',':')).encode(),0o640,984), + '/etc/systemd/system/rmm-full-maintenance-helper.service':(base64.b64decode(p['unit_bytes_b64'],validate=True),0o644,0), + '/etc/systemd/system/rmm-agent-remote.service.d/60-full-maintenance.conf':(b'[Service]\nEnvironment=RMM_FULL_MAINTENANCE_HELPER_SOCKET=/run/rmm-helper-maintenance/control.sock\nEnvironment=RMM_FULL_MAINTENANCE_POLICY_FILE=/etc/rmm-remote/full-maintenance-helper-proof.json\n',0o644,0)} + for name,(raw,mode,gid) in new_files.items(): + path=Path(name);m=safe(path) + require(m.st_gid==gid and stat.S_IMODE(m.st_mode)==mode and path.read_bytes()==raw,'exact new Full control bytes/modes') + env={'PATH':'/usr/sbin:/usr/bin:/sbin:/bin','LANG':'C','LC_ALL':'C','RMM_HELPER_CONFIG':'/etc/rmm-remote/helper-full-maintenance.json'} + pin=json.loads(run([str(binaries['helper']),'--inspect-full-maintenance-profile'],env=env)) + require(pin==report['full_pin'] and pin['helper_binary_sha256']==p['artifacts']['helper']['sha256'] + and pin['identity']==p['general_pin']['identity'],'actual protected Full image/policy/identity') + env['RMM_HELPER_CONFIG']='/etc/rmm-remote/helper-general.json' + require(json.loads(run([str(binaries['helper']),'--inspect-general-profile'],env=env))==p['general_pin'],'preserved General profile') + names=('User','Group','NoNewPrivileges','PrivateNetwork','PrivateTmp','ProtectSystem','ProtectHome', + 'ProtectKernelTunables','ProtectKernelModules','ProtectControlGroups','RestrictSUIDSGID', + 'RestrictAddressFamilies','SystemCallFilter','RootDirectory','RootImage','NeedDaemonReload','FragmentPath','DropInPaths') + effective=dict(line.split('=',1) for line in run(['systemctl','show','rmm-full-maintenance-helper.service',*[a for n in names for a in ('-p',n)]]).decode().splitlines()) + require(effective['User']=='root' and effective['Group']=='rmm-agent' + and all(effective[n]=='no' for n in names[2:11]) + and all(effective[n] in ('','~') for n in names[11:13]) + and effective['RootDirectory']==effective['RootImage']==effective['DropInPaths']=='' + and effective['NeedDaemonReload']=='no' and effective['FragmentPath']=='/etc/systemd/system/rmm-full-maintenance-helper.service','exact unrestricted loaded Fullunit; empty allow/deny lists only') + locks=[] + for path in (Path('/var/lib/rmm-agent-remote/agent.lock'),Path('/var/lib/rmm-helper/executor.lock')): + fd=os.open(path,os.O_RDONLY|os.O_NOFOLLOW);fcntl.flock(fd,fcntl.LOCK_EX|fcntl.LOCK_NB);locks.append(fd) + try: + before_sql=receipt(case/'sql-before.json');before_files=receipt(case/'files-before.json') + require(sql_witness()==before_sql and retained_files()==before_files,'all original SQL rows/inodes/PKI metadata preserved before first start') + require(services()==states and not os.path.lexists(case/'restart-intent.json'),'fresh final stopped-state CAS') + first_start_not_before=dt.datetime.now(dt.timezone.utc).isoformat() + create(marker,encode(dict(id=recovery,original_case=ident,runner_sha256=request['runner_sha256'],first_start_not_before=first_start_not_before, + original_report_sha256=description(case/'report.json')['sha256'],sql_before_sha256=sha((case/'sql-before.json').read_bytes()), + files_before_sha256=sha((case/'files-before.json').read_bytes()),first_start_once=True,no_retry=True, + fresh_server_gate=gate,effective_full_unit=effective))) + finally: + for fd in reversed(locks):fcntl.flock(fd,fcntl.LOCK_UN);os.close(fd) + outcome=dict(id=recovery,original_case=ident,first_start_entered=True,completed=False,first_start_not_before=first_start_not_before, + preserved_before_first_start=True,signed_sync_verified=False,full_job_accepted=False, + direct_product_state_writes=False,rmm_jobs=False,approvals=False,package_effects=False,reboot_requested=False,started=[]) + try: + for name,key,uid in (('rmm-helper-cloud.service','general',0),('rmm-full-maintenance-helper.service','full',0),('rmm-agent-remote.service','agent',993)): + run(['systemctl','start',name]);state=svc(name);require(state['ActiveState']=='active' and int(state['MainPID'])>0,'exact first service start active') + pid=int(state['MainPID']);proc=Path('/proc',str(pid));ticks=(proc/'stat').read_text().rsplit(')',1)[1].split()[19] + status=(proc/'status').read_text().splitlines();uids=next(x.split()[1:] for x in status if x.startswith('Uid:'));gids=next(x.split()[1:] for x in status if x.startswith('Gid:')) + image=sha((proc/'exe').read_bytes()) + require(uids==[str(uid)]*4 and gids==['984']*4 and image==p['artifacts']['agent' if key=='agent' else 'helper']['sha256'] + and (proc/'stat').read_text().rsplit(')',1)[1].split()[19]==ticks and svc(name)['MainPID']==str(pid),'actual same new process/code/account') + evidence=dict(name=name,pid=pid,start_ticks=ticks,sha256=image,uids=uids,gids=gids) + create(case/('continuation-'+key+'-started.json'),encode(evidence));outcome['started'].append(evidence) + require(svc('rmm-apt-helper.service')['ActiveState']=='inactive' and svc('rmm-apt-helper.service')['UnitFileState']=='disabled','APT remains closed') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==p['preflight']['boot_id'],'same final boot') + outcome['completed']=True + except Exception as e:outcome.update(error_class=type(e).__name__,frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(e.__traceback__)]) + create(result_path,encode(outcome));return outcome + + +def main(): + parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('mode',choices=('prepare-local','continue','read'));parser.add_argument('--case');args=parser.parse_args() + os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);guards() + if args.mode=='prepare-local': + require(args.case is None,'new local-only continuation intent');case=str(uuid.uuid4());folder=STATE/case;folder.mkdir(mode=0o700) + intent=dict(id=case,runner_sha256=support.sha(__file__),install_sha256=INSTALL_SHA,original=support.load(ORIGINAL/'intent.json'),original_readback=support.load(READBACK)) + support.save(folder/'intent.json',intent) + else: + case=str(uuid.UUID(args.case));require(case==args.case,'original canonical recovery case');folder=STATE/case;intent=support.load(folder/'intent.json') + require(intent==dict(id=case,runner_sha256=support.sha(__file__),install_sha256=INSTALL_SHA,original=support.load(ORIGINAL/'intent.json'),original_readback=support.load(READBACK)),'exact original same-source recovery JSON') + guards() + if args.mode=='continue': + support.save(folder/'continue-entered-local.json',dict(start_once=True,no_retry=True)) + gate=old.rpc(old.server_prefix(),old.server_gate,intent['original'],folder);support.save(folder/'server-gate.json',gate) + guards();result=old.rpc(support.access_settings(old.pre.ACCESS_ROOT,'linux'),continue_vm,dict(intent,action='continue',fresh_server_gate=gate),folder) + support.save(folder/'continue-reply.json',result) + require(result['completed'] is True and result['preserved_before_first_start'] is True + and result['signed_sync_verified'] is False and result['full_job_accepted'] is False + and result['rmm_jobs'] is False and result['approvals'] is False,'continuation incomplete; readback only') + else: + result=old.rpc(support.access_settings(old.pre.ACCESS_ROOT,'linux'),continue_vm,dict(intent,action='read'),folder) + support.save(folder/('readback-'+str(uuid.uuid4())+'.json'),result) + print(json.dumps(dict(case_id=case,mode=args.mode,intent=str(folder/'intent.json'),intent_sha256=support.sha(folder/'intent.json'),target_called=args.mode!='prepare-local',rmm_jobs=False,approvals=False))) + + +if __name__=='__main__':main() diff --git a/deploy/script_console_finish_start.py b/deploy/script_console_finish_start.py new file mode 100644 index 0000000..fd4a65b --- /dev/null +++ b/deploy/script_console_finish_start.py @@ -0,0 +1,300 @@ +#!/usr/bin/env python3 +"""Finish the two never-started services; never start/stop/restart General. + +Exact original installation + first-start receipts preserved. Each remaining +service has its own durable intent before one start, with a bounded native probe. +""" +import argparse +import hashlib +import inspect +import json +import os +from pathlib import Path +import uuid + +ROOT=Path(__file__).resolve().parents[1] +PRIOR_SHA='c0c0ecf1e1bcb2d2f9ce0bcf6973aa430e9e28ba86823ca5ad0954744a589238' +if hashlib.sha256((ROOT/'deploy/script_console_continue.py').read_bytes()).hexdigest()!=PRIOR_SHA: + raise RuntimeError('Original continuation source stays unchanged') +import script_console_continue as prior +old=prior.old +support=prior.support +PREVIOUS=ROOT/'devstate/script-console-continue/43dae370-6862-4cf4-a83c-0b2612af6c79' +PREVIOUS_INTENT_SHA='f9059e74b3abca44f49216cbde45e036ac4701354d76d0cc9cca8acc41aaff0c' +PREVIOUS_READBACK=PREVIOUS/'readback-b49d10b0-fda3-459a-b286-e6911c9c37eb.json' +PREVIOUS_READBACK_SHA='5aac963e78fa5e587d9137ddd7394f6fde19cd1cfedbbb8528517b0d15e18006' +STATE=ROOT/'devstate/script-console-finish-start' + + +def require(value,code): + if not value:raise RuntimeError(code) + + +def guards(): + prior.guards() + require(support.sha(ROOT/'deploy/script_console_continue.py')==PRIOR_SHA + and support.sha(PREVIOUS/'intent.json')==PREVIOUS_INTENT_SHA + and support.sha(PREVIOUS_READBACK)==PREVIOUS_READBACK_SHA,'exact original continuation/source/readback') + + +def finish_vm(request): + import base64 + import datetime as dt + import fcntl + import time + import hashlib + import json + import os + from pathlib import Path + import sqlite3 + import stat + import subprocess + import traceback + import uuid + os.umask(0o077) + p=request['original'];ident=p['id'];recovery=str(uuid.UUID(request['id'])) + require(recovery==request['id'] and ident=='2e27c312-58cc-444d-92a6-863a11ac6ffd' + and request['action'] in ('finish','read') and os.geteuid()==0 and os.uname().machine=='x86_64','fixed root continuation') + case=Path('/var/lib/rmm-bootstrap-history')/('script-console-v1-'+ident) + # Exact reviewed original helpers are copied below, no installation branch. + def encode(v):return json.dumps(v,sort_keys=True,separators=(',',':')).encode() + def sha(b):return hashlib.sha256(b).hexdigest() + def fsync_dir(path): + fd=os.open(path,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW) + try:os.fsync(fd) + finally:os.close(fd) + def create(path,raw,mode=0o600,gid=0): + fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode) + with os.fdopen(fd,'wb') as out: + out.write(raw);out.flush();os.fchmod(out.fileno(),mode);os.fchown(out.fileno(),0,gid);os.fsync(out.fileno()) + fsync_dir(path.parent) + def safe(path,directory=False,owner=0): + m=path.lstat();require(not path.is_symlink() and m.st_uid==owner and not m.st_mode&0o022 + and (stat.S_ISDIR(m.st_mode) if directory else stat.S_ISREG(m.st_mode) and m.st_nlink==1),'safe owned path') + return m + def run(args,**kwargs):return subprocess.run(args,capture_output=True,check=True,timeout=25,**kwargs).stdout + def svc(name): + return dict(line.split('=',1) for line in run(['systemctl','show',name,'-p','ActiveState','-p','MainPID','-p','UnitFileState']).decode().splitlines()) + def retained_files(): + # Never open private device keys. Retain all file identities, sizes, + # modes and change timestamps; SQL content has its own full row witness. + value={} + for root in (Path('/var/lib/rmm-agent-remote'),Path('/var/lib/rmm-helper')): + for path in [root,*sorted(root.rglob('*'))]: + m=path.lstat();require(not path.is_symlink() and (stat.S_ISDIR(m.st_mode) or stat.S_ISREG(m.st_mode)),'no unexpected retained state object') + if str(path).endswith(('-journal','-wal','-shm')):raise RuntimeError('Settled journal required') + value[str(path)]=[m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_size,m.st_mtime_ns,m.st_ctime_ns] + return value + def sql_witness(): + result={} + for name,identity in p['preflight']['database_inodes'].items(): + path=Path(name);m=path.lstat();require([m.st_dev,m.st_ino]==identity and not path.is_symlink(),'preserved DB identity') + require(all(not os.path.lexists(str(path)+suffix) for suffix in ('-wal','-shm','-journal')),'no live SQLite sidecars') + db=sqlite3.connect(path.as_uri()+'?mode=ro',uri=True) + try: + db.execute('PRAGMA query_only=ON');db.execute('BEGIN') + require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','retained SQL integrity') + tables=dict(db.execute("SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name")) + rows={} + for table in tables: + require(table.replace('_','').isalnum(),'fixed SQLite identifier') + rows[table]=[sha(encode([dict(blob_sha256=sha(v),bytes=len(v)) if isinstance(v,bytes) else v for v in row])) + for row in db.execute('SELECT rowid,* FROM "'+table+'" ORDER BY rowid')] + result[name]=dict(schema=tables,rows=rows,identity=identity) + if path.name=='journal.sqlite': + require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'no running job') + if path.parent.name=='rmm-helper': + current=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY grant_id').fetchall() + exact={r[0]:sha(json.dumps([dict(sha256=sha(v),size=len(v)) if isinstance(v,bytes) else v for v in r],separators=(',',':')).encode()) for r in current} + require(exact==p['preflight']['root_rows'] and len(exact)==18 and all(r[5] is not None for r in current),'exact retained Root18') + finally:db.close() + return result + def description(path): + if not os.path.lexists(path):return dict(present=False) + m=path.lstat();require(not path.is_symlink() and stat.S_ISREG(m.st_mode),'fixed regular public file') + return dict(present=True,device=m.st_dev,inode=m.st_ino,uid=m.st_uid,gid=m.st_gid, + mode=stat.S_IMODE(m.st_mode),bytes=m.st_size,symlink=False,regular=True,sha256=sha(path.read_bytes())) + def receipt(path): + safe(path);require(path.stat().st_size<=8*1024*1024,'bounded private receipt') + return json.loads(path.read_bytes()) + def services(): + value={} + for name in ('rmm-helper-cloud.service','rmm-full-maintenance-helper.service','rmm-agent-remote.service','rmm-apt-helper.service'): + reply=subprocess.run(['systemctl','show',name,'-p','LoadState','-p','ActiveState','-p','SubState','-p','MainPID','-p','UnitFileState'],capture_output=True,timeout=25) + value[name]=dict(exit_code=reply.returncode,properties=dict(line.split('=',1) for line in reply.stdout.decode().splitlines() if '=' in line)) + return value + safe(case,True);safe(case/'intent.json') + require((case/'intent.json').read_bytes()==encode(p),'complete original remote intent') + marker=case/'finish-start-intent.json';result_path=case/'finish-result.json' + if request['action']=='read': + return dict(id=recovery,readback_only=True,original_case=ident,services=services(), + original_report_historical=receipt(case/'report.json'), + original_restart_intent_present=os.path.lexists(case/'restart-intent.json'), + finish_entered=os.path.lexists(marker),finish_intent=receipt(marker) if os.path.lexists(marker) else None, + first_continuation_report_historical=receipt(case/'continuation-result.json'), + historical_finish_result=receipt(result_path) if os.path.lexists(result_path) else None, + current_files={name:description(Path(path)) for name,path in (('agent','/usr/local/libexec/rmm-agent-remote'),('helper','/usr/local/libexec/rmm-privileged-helper'))}, + state_writes=False,rmm_jobs=False,approvals=False,retry_allowed=False) + previous=request['first_continuation_readback'] + require((case/'continuation-start-intent.json').read_bytes()==encode(previous['continuation_intent']) + and (case/'continuation-result.json').read_bytes()==encode(previous['historical_continuation_result']), + 'original first-start marker/result retained byte-for-byte') + require(previous['historical_continuation_result']['completed'] is False + and previous['historical_continuation_result']['started']==[],'known first General probe stopped the sequence') + for label in ('general','full','agent'): + require(not os.path.lexists(case/('continuation-'+label+'-started.json')) + and not os.path.lexists(case/('finish-'+label+'-start-intent.json')) + and not os.path.lexists(case/('finish-'+label+'-started.json')),'no previous confirmed or remaining-service start') + def probe(name,uid): + state=svc(name);require(state['ActiveState']=='active' and int(state['MainPID'])>0,'service process not ready') + pid=int(state['MainPID']);proc=Path('/proc',str(pid));ticks=(proc/'stat').read_text().rsplit(')',1)[1].split()[19] + status=(proc/'status').read_text().splitlines();uids=next(x.split()[1:] for x in status if x.startswith('Uid:'));gids=next(x.split()[1:] for x in status if x.startswith('Gid:')) + image=sha((proc/'exe').read_bytes());key='agent' if name=='rmm-agent-remote.service' else 'helper' + require(uids==[str(uid)]*4 and gids==['984']*4 and image==p['artifacts'][key]['sha256'] + and (proc/'stat').read_text().rsplit(')',1)[1].split()[19]==ticks and svc(name)['MainPID']==str(pid) + and Path('/proc/sys/kernel/random/boot_id').read_text().strip()==p['preflight']['boot_id'],'same actual process/account/image/boot') + return dict(name=name,pid=pid,start_ticks=ticks,sha256=image,uids=uids,gids=gids) + def exact_general(): + value=probe('rmm-helper-cloud.service',0) + require(value['pid']==65562 and value['start_ticks']=='7308498','original already-running General birth') + return value + def poll_started(name,uid): + deadline=time.monotonic()+5;first_birth=None;settled=None + while time.monotonic()0: + try:birth=(pid,Path('/proc',str(pid),'stat').read_text().rsplit(')',1)[1].split()[19]) + except FileNotFoundError:birth=None + if birth is not None: + if first_birth is None:first_birth=birth + require(birth==first_birth,'no service PID/birth replacement during first start') + try:value=probe(name,uid) + except (RuntimeError,FileNotFoundError,ProcessLookupError):value=None + if value is not None and value==settled:return value + settled=value;time.sleep(0.05) + raise RuntimeError('First-start process did not settle in five seconds; readback only') + gate=request['fresh_server_gate'] + require(gate['closed'] is True and gate['counts']==dict(jobs=30,outbox=30,permits=26,unresolved=0) + and gate['policy_sha256']==p['central_policy_sha256'],'fresh central scope still closed') + observed_at=dt.datetime.fromisoformat(gate['observed_at']) + require(observed_at.utcoffset() is not None and -5<=(dt.datetime.now(dt.timezone.utc)-observed_at).total_seconds()<=60,'fresh bounded server gate') + require(not os.path.lexists(case/'restart-intent.json') and not os.path.lexists(marker) + and not os.path.lexists(result_path),'no prior original or continuation start') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==p['preflight']['boot_id'],'same boot before continuation') + observed=request['original_readback'] + for name,expected in observed['markers'].items(): + current=description(case/(name+'.json')) + require(current=={k:v for k,v in expected.items() if k!='historical_content'},'original markers retained exactly') + require(description(case/'report.json')=={k:v for k,v in observed['historical_report'].items() if k!='historical_content'},'original failed report retained exactly') + report=receipt(case/'report.json') + require(report['stage']=='incomplete_manual_review' and report['preserved_before_restart'] is True + and report['completed_preserving_bootstrap'] is False and report['full_job_accepted'] is False,'exact installed-before-first-start phase') + states=services() + for name,item in states.items(): + require(item['exit_code']==0 and item['properties']==previous['services'][name]['properties'] + and ((name=='rmm-helper-cloud.service' and item['properties']['ActiveState']=='active' and item['properties']['MainPID']=='65562') + or (name!='rmm-helper-cloud.service' and item['properties']['ActiveState']=='inactive' and item['properties']['MainPID']=='0')),'exact General running and remaining services inactive') + general_before=exact_general() + binaries={'agent':Path('/usr/local/libexec/rmm-agent-remote'),'helper':Path('/usr/local/libexec/rmm-privileged-helper')} + for key,path in binaries.items(): + require(description(path)==observed['files'][key+'_current'] and sha(path.read_bytes())==p['artifacts'][key]['sha256'],'same qualified installed image') + require(description(case/(key+'-previous'))==observed['files'][key+'_previous'],'same preserved old image') + require(not os.path.lexists(case/(key+'-next')),'no leftover image exchange') + for name,expected in p['baseline']['controls'].items(): + if name in [str(x) for x in binaries.values()]:continue + path=Path(name);m=path.lstat() + require(not path.is_symlink() and dict(device=m.st_dev,inode=m.st_ino,mode=stat.S_IMODE(m.st_mode),uid=m.st_uid,gid=m.st_gid,bytes=m.st_size,sha256=sha(path.read_bytes()))==expected,'all General/APT/package controls unchanged') + # GeneralPin/Identity serde declaration order is the original inspector's + # order. The historical report uses sorted object keys and is not the raw + # pin-file encoding. Reconstruct the original exact compact bytes explicitly. + full_pin={k:report['full_pin'][k] for k in ('schema_version','profile','identity','policy_sha256','helper_binary_sha256')} + full_pin['identity']={k:full_pin['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','signing_key_id')} + new_files={ + '/etc/rmm-remote/helper-full-maintenance.json':(base64.b64decode(p['baseline']['full_config_candidate']['bytes_b64'],validate=True),0o600,0), + '/etc/rmm-remote/full-maintenance-helper-proof.json':(json.dumps(full_pin,separators=(',',':')).encode(),0o640,984), + '/etc/systemd/system/rmm-full-maintenance-helper.service':(base64.b64decode(p['unit_bytes_b64'],validate=True),0o644,0), + '/etc/systemd/system/rmm-agent-remote.service.d/60-full-maintenance.conf':(b'[Service]\nEnvironment=RMM_FULL_MAINTENANCE_HELPER_SOCKET=/run/rmm-helper-maintenance/control.sock\nEnvironment=RMM_FULL_MAINTENANCE_POLICY_FILE=/etc/rmm-remote/full-maintenance-helper-proof.json\n',0o644,0)} + for name,(raw,mode,gid) in new_files.items(): + path=Path(name);m=safe(path) + require(m.st_gid==gid and stat.S_IMODE(m.st_mode)==mode and path.read_bytes()==raw,'exact new Full control bytes/modes') + env={'PATH':'/usr/sbin:/usr/bin:/sbin:/bin','LANG':'C','LC_ALL':'C','RMM_HELPER_CONFIG':'/etc/rmm-remote/helper-full-maintenance.json'} + pin=json.loads(run([str(binaries['helper']),'--inspect-full-maintenance-profile'],env=env)) + require(pin==report['full_pin'] and pin['helper_binary_sha256']==p['artifacts']['helper']['sha256'] + and pin['identity']==p['general_pin']['identity'],'actual protected Full image/policy/identity') + env['RMM_HELPER_CONFIG']='/etc/rmm-remote/helper-general.json' + require(json.loads(run([str(binaries['helper']),'--inspect-general-profile'],env=env))==p['general_pin'],'preserved General profile') + names=('User','Group','NoNewPrivileges','PrivateNetwork','PrivateTmp','ProtectSystem','ProtectHome', + 'ProtectKernelTunables','ProtectKernelModules','ProtectControlGroups','RestrictSUIDSGID', + 'RestrictAddressFamilies','SystemCallFilter','RootDirectory','RootImage','NeedDaemonReload','FragmentPath','DropInPaths') + effective=dict(line.split('=',1) for line in run(['systemctl','show','rmm-full-maintenance-helper.service',*[a for n in names for a in ('-p',n)]]).decode().splitlines()) + require(effective['User']=='root' and effective['Group']=='rmm-agent' + and all(effective[n]=='no' for n in names[2:11]) + and all(effective[n] in ('','~') for n in names[11:13]) + and effective['RootDirectory']==effective['RootImage']==effective['DropInPaths']=='' + and effective['NeedDaemonReload']=='no' and effective['FragmentPath']=='/etc/systemd/system/rmm-full-maintenance-helper.service','exact unrestricted loaded Fullunit; empty allow/deny lists only') + locks=[] + for path in (Path('/var/lib/rmm-agent-remote/agent.lock'),Path('/var/lib/rmm-helper/executor.lock')): + fd=os.open(path,os.O_RDONLY|os.O_NOFOLLOW);fcntl.flock(fd,fcntl.LOCK_EX|fcntl.LOCK_NB);locks.append(fd) + try: + before_sql=receipt(case/'sql-before.json');before_files=receipt(case/'files-before.json') + require(sha((case/'sql-before.json').read_bytes())==previous['continuation_intent']['sql_before_sha256'] + and sha((case/'files-before.json').read_bytes())==previous['continuation_intent']['files_before_sha256'],'exact original preserved state witnesses') + require(sql_witness()==before_sql and retained_files()==before_files,'all original SQL rows/inodes/PKI metadata preserved before first start') + require(services()==states and exact_general()==general_before + and not os.path.lexists(case/'restart-intent.json'),'fresh General-birth and remaining-stopped CAS') + first_start_not_before=previous['continuation_intent']['first_start_not_before'] + remaining_start_not_before=dt.datetime.now(dt.timezone.utc).isoformat() + create(marker,encode(dict(id=recovery,original_case=ident,runner_sha256=request['runner_sha256'],first_start_not_before=first_start_not_before,remaining_start_not_before=remaining_start_not_before, + general_before=general_before,original_report_sha256=description(case/'report.json')['sha256'],sql_before_sha256=sha((case/'sql-before.json').read_bytes()), + files_before_sha256=sha((case/'files-before.json').read_bytes()),first_start_once=True,no_retry=True, + fresh_server_gate=gate,effective_full_unit=effective))) + finally: + for fd in reversed(locks):fcntl.flock(fd,fcntl.LOCK_UN);os.close(fd) + outcome=dict(id=recovery,original_case=ident,first_start_entered=True,completed=False,first_start_not_before=first_start_not_before,remaining_start_not_before=remaining_start_not_before, + general_before=general_before, + preserved_before_first_start=True,signed_sync_verified=False,full_job_accepted=False, + direct_product_state_writes=False,rmm_jobs=False,approvals=False,package_effects=False,reboot_requested=False,started=[]) + try: + for name,key,uid in (('rmm-full-maintenance-helper.service','full',0),('rmm-agent-remote.service','agent',993)): + require(exact_general()==general_before,'General remains the same process; never start it again') + create(case/('finish-'+key+'-start-intent.json'),encode(dict(id=recovery,name=name,start_once=True,no_retry=True, + observed_at=dt.datetime.now(dt.timezone.utc).isoformat()))) + run(['systemctl','start',name]) + evidence=poll_started(name,uid) + create(case/('finish-'+key+'-started.json'),encode(evidence));outcome['started'].append(evidence) + require(exact_general()==general_before,'same original General after remaining starts') + require(svc('rmm-apt-helper.service')['ActiveState']=='inactive' and svc('rmm-apt-helper.service')['UnitFileState']=='disabled','APT remains closed') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==p['preflight']['boot_id'],'same final boot') + outcome['completed']=True + except Exception as e:outcome.update(error_class=type(e).__name__,frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(e.__traceback__)]) + create(result_path,encode(outcome));return outcome + + +def main(): + parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('mode',choices=('prepare-local','finish','read'));parser.add_argument('--case');args=parser.parse_args() + os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);guards() + before=support.load(PREVIOUS/'intent.json') + def expected(case):return dict(id=case,runner_sha256=support.sha(__file__),prior_sha256=PRIOR_SHA, + original=before['original'],original_readback=before['original_readback'],first_continuation_readback=support.load(PREVIOUS_READBACK)) + if args.mode=='prepare-local': + require(args.case is None,'new local-only remaining-start intent');case=str(uuid.uuid4());folder=STATE/case;folder.mkdir(mode=0o700) + support.save(folder/'intent.json',expected(case)) + else: + case=str(uuid.UUID(args.case));require(case==args.case,'original canonical finish case');folder=STATE/case;intent=support.load(folder/'intent.json') + require(intent==expected(case),'complete original same-source JSON for every finish/read') + guards() + if args.mode=='finish': + support.save(folder/'finish-entered-local.json',dict(start_once=True,no_retry=True)) + gate=old.rpc(old.server_prefix(),old.server_gate,intent['original'],folder);support.save(folder/'server-gate.json',gate) + guards();result=old.rpc(support.access_settings(old.pre.ACCESS_ROOT,'linux'),finish_vm,dict(intent,action='finish',fresh_server_gate=gate),folder) + support.save(folder/'finish-reply.json',result) + require(result['completed'] is True and result['preserved_before_first_start'] is True + and result['signed_sync_verified'] is False and result['full_job_accepted'] is False + and result['rmm_jobs'] is False and result['approvals'] is False,'finish incomplete; readback only') + else: + result=old.rpc(support.access_settings(old.pre.ACCESS_ROOT,'linux'),finish_vm,dict(intent,action='read'),folder) + support.save(folder/('readback-'+str(uuid.uuid4())+'.json'),result) + print(json.dumps(dict(case_id=case,mode=args.mode,intent=str(folder/'intent.json'),intent_sha256=support.sha(folder/'intent.json'),target_called=args.mode!='prepare-local',rmm_jobs=False,approvals=False))) + + +if __name__=='__main__':main() diff --git a/deploy/script_console_install.py b/deploy/script_console_install.py new file mode 100644 index 0000000..8382262 --- /dev/null +++ b/deploy/script_console_install.py @@ -0,0 +1,351 @@ +#!/usr/bin/env python3 +"""Reviewable preserving Linux runtime installation; parent executes only. + +All active shared-ledger readers become the same new generation. General rights +and controls remain byte-identical. APT stays already closed/unqualified. No RMM +job/approval or automatic install retry. This source never installs the notebook. +""" +import argparse +import base64 +import hashlib +import inspect +import json +import os +from pathlib import Path +import uuid + +ROOT = Path(__file__).resolve().parents[1] +PREFLIGHT_SHA = '0e210b857d3f25e0ff228bbbd4a6533dadd91c9d21dbcba5923b26c2fe72eec9' +if hashlib.sha256((ROOT / 'deploy/script_console_bootstrap.py').read_bytes()).hexdigest() != PREFLIGHT_SHA: + raise RuntimeError('Reviewed preflight source changed') +import script_console_bootstrap as pre +support = pre.support +STATE = ROOT / 'devstate/script-console-install' +REMOTE = '/var/lib/rmm-bootstrap-history/script-console-v1-' + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def server_gate(p): + import datetime as dt + import hashlib + import json + from pathlib import Path + import subprocess + import uuid + tenant = str(uuid.UUID(p['target']['tenant_id'])); agent = str(uuid.UUID(p['target']['agent_id'])) + require(tenant == p['target']['tenant_id'] and agent == p['target']['agent_id'], 'canonical exact target') + scope = "tenant_id='" + tenant + "' AND agent_id='" + agent + "'" + query = ("SELECT json_build_object('jobs',(SELECT count(*) FROM jobs WHERE " + scope + ")," + "'outbox',(SELECT count(*) FROM outbox o JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE " + scope.replace('tenant_id=', 'j.tenant_id=').replace('agent_id=', 'j.agent_id=') + ")," + "'permits',(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE " + scope.replace('tenant_id=', 'j.tenant_id=').replace('agent_id=', 'j.agent_id=') + ")," + "'unresolved',(SELECT count(*) FROM jobs WHERE " + scope + " AND state NOT IN ('succeeded','failed','cancelled','expired')));") + root = Path('/opt/rust-discovery') + cmd = ['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'), + 'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm'] + value = json.loads(subprocess.run(cmd, input=query.encode(), capture_output=True, check=True, timeout=20).stdout) + require(value == dict(jobs=30,outbox=30,permits=26,unresolved=0), 'same central closed count') + path = root/'devstate/root-policy/policy.json'; raw=path.read_bytes(); meta=path.lstat() + require(not path.is_symlink() and meta.st_uid==10001 and meta.st_mode&0o777==0o600 + and hashlib.sha256(raw).hexdigest()==p['central_policy_sha256'], 'unchanged protected central policy') + require(dt.datetime.fromisoformat(json.loads(raw)['expires_at']) <= dt.datetime.now(dt.timezone.utc), 'Root scope remains closed') + return dict(counts=value,policy_sha256=p['central_policy_sha256'],closed=True,observed_at=dt.datetime.now(dt.timezone.utc).isoformat(),jobs_created=False,approvals=False) + + +def vm(p): + import base64 + import fcntl + import hashlib + import json + import os + from pathlib import Path + import sqlite3 + import stat + import subprocess + import traceback + import uuid + import datetime as dt + os.umask(0o077) + ident=str(uuid.UUID(p['id'])); require(ident==p['id'] and os.geteuid()==0,'fixed root installation case') + require(p['action'] in ('prepare','install','read') and p['type']=='rmm.script-console.preserving-install-v1','fixed operation') + parent=Path('/var/lib/rmm-bootstrap-history'); case=parent/('script-console-v1-'+ident) + intent={k:v for k,v in p.items() if k not in ('action','fresh_server_gate')} + def encode(v):return json.dumps(v,sort_keys=True,separators=(',',':')).encode() + def sha(b):return hashlib.sha256(b).hexdigest() + def fsync_dir(path): + fd=os.open(path,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW) + try:os.fsync(fd) + finally:os.close(fd) + def create(path,raw,mode=0o600,gid=0): + fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode) + with os.fdopen(fd,'wb') as out: + out.write(raw);out.flush();os.fchmod(out.fileno(),mode);os.fchown(out.fileno(),0,gid);os.fsync(out.fileno()) + fsync_dir(path.parent) + def safe(path,directory=False,owner=0): + m=path.lstat();require(not path.is_symlink() and m.st_uid==owner and not m.st_mode&0o022 + and (stat.S_ISDIR(m.st_mode) if directory else stat.S_ISREG(m.st_mode) and m.st_nlink==1),'safe owned path') + return m + def run(args,**kwargs):return subprocess.run(args,capture_output=True,check=True,timeout=25,**kwargs).stdout + def svc(name): + return dict(line.split('=',1) for line in run(['systemctl','show',name,'-p','ActiveState','-p','MainPID','-p','UnitFileState']).decode().splitlines()) + def marker(name, **fields): + create(case/(name+'.json'),encode(dict(id=ident,phase=name,**fields))) + def live_readback(): + # This path never repairs, restarts, replaces or retries anything. A + # receipt is historical; only independently observed markers and native + # state describe what may have happened after an interrupted install. + started=dt.datetime.now(dt.timezone.utc).isoformat() + def observed_file(path, content=False): + if not os.path.lexists(path):return dict(present=False) + try: + m=path.lstat();value=dict(present=True,device=m.st_dev,inode=m.st_ino, + uid=m.st_uid,gid=m.st_gid,mode=stat.S_IMODE(m.st_mode),bytes=m.st_size, + symlink=stat.S_ISLNK(m.st_mode),regular=stat.S_ISREG(m.st_mode)) + if not stat.S_ISREG(m.st_mode) or path.is_symlink() or m.st_size>256*1024*1024: + value['inspection_error']='unexpected_or_unbounded_file';return value + raw=path.read_bytes();value['sha256']=sha(raw) + if content: + require(m.st_uid==0 and not m.st_mode&0o022 and m.st_nlink==1 + and m.st_size<8*1024*1024,'safe bounded historical receipt') + value['historical_content']=json.loads(raw) + return value + except Exception as e:return dict(present=True,inspection_error=type(e).__name__) + phase_names=('install-entered','stop-intent','stopped','agent-exchange-intent','agent-exchanged', + 'helper-exchange-intent','helper-exchanged','full-files-installed','restart-intent', + 'general-started','full-started','agent-started') + markers={name:observed_file(case/(name+'.json'),True) for name in phase_names} + services={} + for name in ('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-full-maintenance-helper.service','rmm-apt-helper.service'): + try: + reply=subprocess.run(['systemctl','show',name,'-p','LoadState','-p','ActiveState', + '-p','SubState','-p','MainPID','-p','UnitFileState'],capture_output=True,timeout=25) + value=dict(exit_code=reply.returncode,properties=dict(line.split('=',1) + for line in reply.stdout.decode().splitlines() if '=' in line)) + # A missing named Full unit is an observable partial state, + # not a reason to discard the other independent observations. + if reply.returncode!=0 and not (name=='rmm-full-maintenance-helper.service' + and reply.returncode==1 and value['properties'].get('LoadState')=='not-found' + and value['properties'].get('ActiveState')=='inactive' + and value['properties'].get('MainPID')=='0'): + value['inspection_error']='systemctl_show_failed' + pid=int(value['properties'].get('MainPID','0')) + if pid>0: + try: + proc=Path('/proc',str(pid));before=(proc/'stat').read_text() + status=(proc/'status').read_text().splitlines() + image=sha((proc/'exe').read_bytes()) + require((proc/'stat').read_text().rsplit(')',1)[1].split()[19] + ==before.rsplit(')',1)[1].split()[19],'same observed process start') + value['process']=dict(pid=pid,start_ticks=before.rsplit(')',1)[1].split()[19],sha256=image, + uids=next(x.split()[1:] for x in status if x.startswith('Uid:')), + gids=next(x.split()[1:] for x in status if x.startswith('Gid:'))) + except Exception as e:value['process']=dict(pid=pid,inspection_error=type(e).__name__) + services[name]=value + except Exception as e:services[name]=dict(inspection_error=type(e).__name__) + files={} + for key,path in (('agent',Path('/usr/local/libexec/rmm-agent-remote')),('helper',Path('/usr/local/libexec/rmm-privileged-helper'))): + files[key+'_current']=observed_file(path) + for generation in ('previous','next'):files[key+'_'+generation]=observed_file(case/(key+'-'+generation)) + historical=observed_file(case/'report.json',True) + return dict(id=ident,readback_only=True,observed_from=started, + observed_until=dt.datetime.now(dt.timezone.utc).isoformat(),install_entered=markers['install-entered']['present'], + services_may_have_changed=markers['stop-intent']['present'], + stop_confirmed=markers['stopped']['present'],markers=markers,services=services,files=files, + historical_report=historical,repair_attempted=False,retry_allowed=False, + product_state_writes=False,rmm_jobs=False,approvals=False) + def retained_files(): + # Never open private device keys. Retain all file identities, sizes, + # modes and change timestamps; SQL content has its own full row witness. + value={} + for root in (Path('/var/lib/rmm-agent-remote'),Path('/var/lib/rmm-helper')): + for path in [root,*sorted(root.rglob('*'))]: + m=path.lstat();require(not path.is_symlink() and (stat.S_ISDIR(m.st_mode) or stat.S_ISREG(m.st_mode)),'no unexpected retained state object') + if str(path).endswith(('-journal','-wal','-shm')):raise RuntimeError('Settled journal required') + value[str(path)]=[m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_size,m.st_mtime_ns,m.st_ctime_ns] + return value + def sql_witness(): + result={} + for name,identity in p['preflight']['database_inodes'].items(): + path=Path(name);m=path.lstat();require([m.st_dev,m.st_ino]==identity and not path.is_symlink(),'preserved DB identity') + require(all(not os.path.lexists(str(path)+suffix) for suffix in ('-wal','-shm','-journal')),'no live SQLite sidecars') + db=sqlite3.connect(path.as_uri()+'?mode=ro',uri=True) + try: + db.execute('PRAGMA query_only=ON');db.execute('BEGIN') + require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','retained SQL integrity') + tables=dict(db.execute("SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name")) + rows={} + for table in tables: + require(table.replace('_','').isalnum(),'fixed SQLite identifier') + rows[table]=[sha(encode([dict(blob_sha256=sha(v),bytes=len(v)) if isinstance(v,bytes) else v for v in row])) + for row in db.execute('SELECT rowid,* FROM "'+table+'" ORDER BY rowid')] + result[name]=dict(schema=tables,rows=rows,identity=identity) + if path.name=='journal.sqlite': + require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'no running job') + if path.parent.name=='rmm-helper': + current=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY grant_id').fetchall() + exact={r[0]:sha(json.dumps([dict(sha256=sha(v),size=len(v)) if isinstance(v,bytes) else v for v in r],separators=(',',':')).encode()) for r in current} + require(exact==p['preflight']['root_rows'] and len(exact)==18 and all(r[5] is not None for r in current),'exact retained Root18') + finally:db.close() + return result + safe(parent,True) + if p['action']=='prepare': + before=snapshot_vm(p['preflight']) + require(before['controls']==p['baseline']['controls'] and before['processes']==p['baseline']['processes'] + and before['databases']==p['baseline']['databases'],'fresh original preflight still exact') + case.mkdir(mode=0o700);create(case/'intent.json',encode(intent)) + return dict(id=ident,prepared=True,services_changed=False,product_state_writes=False) + safe(case,True);safe(case/'intent.json') + require((case/'intent.json').read_bytes()==encode(intent),'original immutable install intent') + if p['action']=='read': + return live_readback() + require(not os.path.lexists(case/'install-entered.json'),'same case install must never repeat') + gate=p['fresh_server_gate'] + require(gate['closed'] is True and gate['counts']==dict(jobs=30,outbox=30,permits=26,unresolved=0) + and gate['policy_sha256']==p['central_policy_sha256'],'fresh separate closed server gate') + before=snapshot_vm(p['preflight']) + require(before['controls']==p['baseline']['controls'] and before['processes']==p['baseline']['processes'] + and before['databases']==p['baseline']['databases'],'exact pre-stop CAS including PIDs/startticks') + binaries={'agent':Path('/usr/local/libexec/rmm-agent-remote'),'helper':Path('/usr/local/libexec/rmm-privileged-helper')} + for key,path in binaries.items(): + upload=case/(key+'-next');safe(upload) + require(sha(upload.read_bytes())==p['artifacts'][key]['sha256'] and upload.stat().st_size==p['artifacts'][key]['bytes'],'exact native-qualified upload') + marker('install-entered',install_once=True) + report=dict(id=ident,stage='entered',completed_preserving_bootstrap=False,signed_sync_verified=False, + services_changed=False,services_may_have_changed=False,root_consumed=18,root_unresolved=0,rmm_jobs=False,approvals=False, + package_effects=False,reboot_requested=False,apt_endpoint_enabled=False,apt_current_runtime_qualified=False, + full_job_accepted=False) + def save(): + pending=case/'report-next';create(pending,encode(report));os.replace(pending,case/'report.json');fsync_dir(case) + locks=[] + try: + save();marker('stop-intent',services_may_have_changed=True) + report.update(stage='stop_requested',services_may_have_changed=True);save() + run(['systemctl','stop','rmm-agent-remote.service','rmm-helper-cloud.service']) + require(all(svc(n)['ActiveState']=='inactive' for n in ('rmm-agent-remote.service','rmm-helper-cloud.service')),'both old daemons stopped') + marker('stopped',confirmed=True);report['services_changed']=True;save() + for path in (Path('/var/lib/rmm-agent-remote/agent.lock'),Path('/var/lib/rmm-helper/executor.lock')): + fd=os.open(path,os.O_RDWR|os.O_NOFOLLOW);fcntl.flock(fd,fcntl.LOCK_EX|fcntl.LOCK_NB);locks.append(fd) + frozen_sql=sql_witness();frozen_files=retained_files() + create(case/'sql-before.json',encode(frozen_sql));create(case/'files-before.json',encode(frozen_files)) + for key,path in binaries.items(): + safe(path);require(sha(path.read_bytes())==p['preflight']['old_artifacts'][key],'unchanged old binary immediately before exchange') + require(path.stat().st_dev==case.stat().st_dev,'same-filesystem preserving exchange') + marker(key+'-exchange-intent',may_have_changed=True) + os.rename(path,case/(key+'-previous'));fsync_dir(path.parent);fsync_dir(case) + os.chmod(case/(key+'-next'),0o755);os.rename(case/(key+'-next'),path);fsync_dir(path.parent);fsync_dir(case) + marker(key+'-exchanged',confirmed=True) + report['stage']=key+'_exchanged';save() + helper=binaries['helper'];env={'PATH':'/usr/sbin:/usr/bin:/sbin:/bin','LANG':'C','LC_ALL':'C','RMM_HELPER_CONFIG':'/etc/rmm-remote/helper-general.json'} + general=json.loads(run([str(helper),'--inspect-general-profile'],env=env)) + require(general==p['general_pin'],'new General generation retains identical public policy pin') + for path,expected in p['baseline']['controls'].items(): + if path in [str(x) for x in binaries.values()]:continue + item=Path(path);m=item.lstat() + actual=dict(device=m.st_dev,inode=m.st_ino,mode=stat.S_IMODE(m.st_mode),uid=m.st_uid,gid=m.st_gid,bytes=m.st_size,sha256=sha(item.read_bytes())) + require(not item.is_symlink() and actual==expected,'General/APT/package control bytes/inodes/modes unchanged') + full=p['baseline']['full_config_candidate'];raw=base64.b64decode(full['bytes_b64'],validate=True) + require(sha(raw)==full['sha256'] and json.loads(raw)['helper_binary_sha256']==p['artifacts']['helper']['sha256'],'exact reviewed Fullconfig') + config=Path('/etc/rmm-remote/helper-full-maintenance.json');create(config,raw,0o600) + env['RMM_HELPER_CONFIG']=str(config) + pin=json.loads(run([str(helper),'--inspect-full-maintenance-profile'],env=env)) + require(pin['schema_version']==2 and pin['profile']=='full_maintenance_v1' and pin['identity']==general['identity'] + and pin['helper_binary_sha256']==p['artifacts']['helper']['sha256'],'actual new image/profile/identity proof') + pinpath=Path('/etc/rmm-remote/full-maintenance-helper-proof.json');create(pinpath,json.dumps(pin,separators=(',',':')).encode(),0o640,984) + unit=Path('/etc/systemd/system/rmm-full-maintenance-helper.service');create(unit,base64.b64decode(p['unit_bytes_b64'],validate=True),0o644) + dropin=Path('/etc/systemd/system/rmm-agent-remote.service.d/60-full-maintenance.conf') + safe(dropin.parent,True);create(dropin,b'[Service]\nEnvironment=RMM_FULL_MAINTENANCE_HELPER_SOCKET=/run/rmm-helper-maintenance/control.sock\nEnvironment=RMM_FULL_MAINTENANCE_POLICY_FILE=/etc/rmm-remote/full-maintenance-helper-proof.json\n',0o644) + require(sql_witness()==frozen_sql and retained_files()==frozen_files,'all preexisting SQL rows/bytes/inodes/keys unchanged before restart') + marker('full-files-installed',preserved_before_restart=True) + report.update(stage='installed_preserved',general_pin=general,full_pin=pin,preserved_before_restart=True);save() + for fd in reversed(locks):fcntl.flock(fd,fcntl.LOCK_UN);os.close(fd) + locks.clear();run(['systemctl','daemon-reload']);run(['systemd-analyze','verify',str(unit)]) + names=('User','Group','NoNewPrivileges','PrivateNetwork','PrivateTmp','ProtectSystem','ProtectHome', + 'ProtectKernelTunables','ProtectKernelModules','ProtectControlGroups','RestrictSUIDSGID', + 'RestrictAddressFamilies','SystemCallFilter','RootDirectory','RootImage') + full_unit=dict(line.split('=',1) for line in run(['systemctl','show','rmm-full-maintenance-helper.service', + *[argument for name in names for argument in ('-p',name)]]).decode().splitlines()) + require(full_unit['User']=='root' and full_unit['Group']=='rmm-agent' + and all(full_unit[name]=='no' for name in names[2:11]) + and all(full_unit[name]=='' for name in names[11:]),'effective Full unit must not inherit General sandbox') + report['effective_full_unit']=full_unit;save() + marker('restart-intent',services_may_have_changed=True) + for name,label in (('rmm-helper-cloud.service','general'),('rmm-full-maintenance-helper.service','full'),('rmm-agent-remote.service','agent')): + run(['systemctl','start',name]);require(svc(name)['ActiveState']=='active','new exact service active') + marker(label+'-started',confirmed=True) + processes={} + for name,key,uid in (('rmm-helper-cloud.service','general',0),('rmm-full-maintenance-helper.service','full',0),('rmm-agent-remote.service','agent',993)): + pid=int(svc(name)['MainPID']);status=Path('/proc',str(pid),'status').read_text().splitlines() + uids=next(x.split()[1:] for x in status if x.startswith('Uid:'));gids=next(x.split()[1:] for x in status if x.startswith('Gid:')) + image=sha(Path('/proc',str(pid),'exe').read_bytes());expected=p['artifacts']['agent' if key=='agent' else 'helper']['sha256'] + require(uids==[str(uid)]*4 and gids==['984']*4 and image==expected,'actual new service accounts/code generation') + processes[key]=dict(pid=pid,uids=uids,gids=gids,sha256=image) + require(svc('rmm-apt-helper.service')['ActiveState']=='inactive' and svc('rmm-apt-helper.service')['UnitFileState']=='disabled','APT remains already closed') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==p['preflight']['boot_id'],'no boot transition') + report.update(stage='new_generations_started',completed_preserving_bootstrap=True,processes=processes,boot_id=p['preflight']['boot_id']);save() + except Exception as e: + report.update(stage='incomplete_manual_review',error_class=type(e).__name__,frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(e.__traceback__)]);save() + finally: + for fd in reversed(locks):fcntl.flock(fd,fcntl.LOCK_UN);os.close(fd) + return report + + +def rpc(prefix, function, payload, folder): + code=inspect.getsource(require)+'\n'+inspect.getsource(pre.snapshot_vm)+'\n'+inspect.getsource(function) + code+='\ntry:\n print(__import__("json").dumps('+function.__name__+'('+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 server_prefix(): + cfg=support.access.load_config(pre.ACCESS_ROOT/'credentials/server.conf') + require(cfg['address']=='88.99.80.202' and cfg['username']=='root' and cfg.get('port','22')=='22' and cfg.get('ssh_key_path'),'fixed server bootstrap transport') + return ['ssh','-F','/dev/null','-T','-p','22','-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','-i',str(Path(cfg['ssh_key_path']).expanduser()),'root@88.99.80.202'] + + +if __name__=='__main__': + parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('mode',choices=('prepare-local','stage','install','read')) + parser.add_argument('--snapshot',type=Path);parser.add_argument('--snapshot-sha');parser.add_argument('--case');args=parser.parse_args() + os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);support.private(STATE,True) + if args.mode=='prepare-local': + require(args.case is None and args.snapshot is not None and args.snapshot_sha,'fresh reviewed snapshot required') + baseline=support.load(args.snapshot);require(support.sha(args.snapshot)==args.snapshot_sha and baseline['fresh_target_cas'] is True + and baseline['install_authorized'] is False and baseline['root_consumed']==18 and baseline['root_unresolved']==0,'exact fresh read-only snapshot') + prepath=args.snapshot.parent/'intent.json';previous=support.load(prepath) + require(previous['runner_sha256']==PREFLIGHT_SHA and previous['manifest_sha256']==pre.native.MANIFEST_SHA,'reviewed preflight generation') + case=str(uuid.uuid4());folder=STATE/case;folder.mkdir(mode=0o700) + manifest=support.load(pre.native.PACK/'manifest.json');old=support.load(pre.PRIOR) + require(support.sha(pre.native.PACK/'manifest.json')==pre.native.MANIFEST_SHA + and support.sha(pre.PRIOR)==pre.PRIOR_SHA and support.sha(pre.NATIVE)==pre.NATIVE_SHA + and all(support.sha(ROOT/n)==h for n,h in manifest['source_files'].items()),'actual frozen source/native provenance') + intent=dict(type='rmm.script-console.preserving-install-v1',id=case,runner_sha256=support.sha(__file__),preflight_source_sha256=PREFLIGHT_SHA, + manifest_sha256=pre.native.MANIFEST_SHA,snapshot_sha256=args.snapshot_sha,snapshot_path=str(args.snapshot.absolute()),preflight=previous,baseline=baseline, + target=previous['target'],central_policy_sha256=old['central_policy_sha256'],general_pin=old['general_pin'], + artifacts={k:{field:manifest['artifacts'][name][field] for field in ('sha256','bytes')} for k,name in (('agent','rmm-agent'),('helper','rmm-privileged-helper'))}, + unit_bytes_b64=base64.b64encode((ROOT/'deploy/linux/rmm-full-maintenance-helper.service').read_bytes()).decode()) + support.save(folder/'intent.json',intent) + else: + require(args.case is not None and args.snapshot is None and args.snapshot_sha is None,'original case only') + case=str(uuid.UUID(args.case));require(case==args.case,'canonical case');folder=STATE/case;intent=support.load(folder/'intent.json') + require(intent['runner_sha256']==support.sha(__file__) and intent['preflight_source_sha256']==PREFLIGHT_SHA,'original same-source intent') + require(support.sha(pre.native.PACK/'manifest.json')==pre.native.MANIFEST_SHA + and all(support.sha(ROOT/n)==h for n,h in support.load(pre.native.PACK/'manifest.json')['source_files'].items()),'frozen158 inputs remain exact') + prefix=support.access_settings(pre.ACCESS_ROOT,'linux') + if args.mode=='stage': + support.save(folder/'stage-entered-local.json',dict(stage_once=True)) + result=rpc(prefix,vm,dict(intent,action='prepare'),folder);support.save(folder/'prepared-remote.json',result) + require(result['prepared'] is True,'new exact remote case') + for key,name in (('agent','rmm-agent'),('helper','rmm-privileged-helper')): + support.upload(prefix,dict(path=str(pre.native.PACK/name),**intent['artifacts'][key]),REMOTE+case+'/'+key+'-next',folder,key) + elif args.mode=='install': + support.load(folder/'prepared-remote.json');support.save(folder/'install-entered-local.json',dict(install_once=True,no_retry=True)) + gate=rpc(server_prefix(),server_gate,intent,folder);support.save(folder/'fresh-server-gate.json',gate) + result=rpc(prefix,vm,dict(intent,action='install',fresh_server_gate=gate),folder);support.save(folder/'install-reply.json',result) + require(result.get('completed_preserving_bootstrap') is True and result.get('signed_sync_verified') is False + and result.get('full_job_accepted') is False and result.get('rmm_jobs') is False and result.get('approvals') is False, + 'installation incomplete; same-case readback/manual review only') + else: + result=rpc(prefix,vm,dict(intent,action='read'),folder);support.save(folder/('readback-'+str(uuid.uuid4())+'.json'),result) + print(json.dumps(dict(case_id=case,intent=str(folder/'intent.json'),intent_sha256=support.sha(folder/'intent.json'),mode=args.mode, + target_called=args.mode!='prepare-local',rmm_jobs=False,approvals=False))) diff --git a/deploy/script_console_job_acceptance.py b/deploy/script_console_job_acceptance.py new file mode 100644 index 0000000..59ea7a2 --- /dev/null +++ b/deploy/script_console_job_acceptance.py @@ -0,0 +1,472 @@ +#!/usr/bin/env python3 +"""One exact benign Cloud Linux script: MCP plan/submit and read-only evidence. + +No approval endpoint, browser, rights/policy mutation, direct job SQL write, +cancel, retry, interpreter execution or local agent. Parent executes after review. +""" +import argparse +import base64 +import datetime as dt +import hashlib +import inspect +import json +import os +from pathlib import Path +import re +import time +import uuid + +ROOT=Path(__file__).resolve().parents[1] +INSTALL_SHA='667b56f7e5f085db249f27a7094919c7c0e6055dabc9f0600cb261c34760cd26' +if hashlib.sha256((ROOT/'deploy/script_console_install.py').read_bytes()).hexdigest()!=INSTALL_SHA: + raise RuntimeError('Reviewed bootstrap transport source changed') +import script_console_install as old +support=old.support +TEMPLATE=ROOT/'devstate/output/playwright-private/console-full-v1/mcp-plan-input.template.json' +TEMPLATE_SHA='66f684ab9c5c31be6583fe04f090ba9d7b8724d6e6c920dd9c67c484a339245e' +ORIGINAL=ROOT/'devstate/script-console-install/2e27c312-58cc-444d-92a6-863a11ac6ffd/intent.json' +ORIGINAL_SHA='8aec7bf7df13bc4968f47ca1554c813406409530613ce95241e74a81204cd02e' +PATTERN_SHA='71ec226ef98acdd7a59cd1cc8648cce910ef143efad08b8e937afde7f26b6d72' +SCRIPT_SHA='c878ba5b5c37fa3f1b9485c6994d6c57f6f37d01d99d4239c64d264d4ed95336' +CREATOR='automation:11111111-1111-4111-8111-111111111111' +APPROVER='simulated-testprincipal-a' +STDOUT=b'0\nRMM_FULL_CONSOLE_LINUX_V1\n' +STDERR=b'RMM_FULL_CONSOLE_STDERR_V1\n' +STATE=ROOT/'devstate/script-console-job-acceptance' + + +def require(value,code): + if not value:raise RuntimeError(code) + + +def encode(value):return json.dumps(value,ensure_ascii=False,separators=(',',':')).encode() + + +def timestamp_ns(value): + """Strict RFC3339 calendar/offset -> integer epoch nanoseconds, no rounding.""" + import datetime as dt + import re + require(isinstance(value,str),'RFC3339 timestamp string') + match=re.fullmatch(r'([0-9]{4})-([0-9]{2})-([0-9]{2})[Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]{1,9}))?([Zz]|[+-][0-9]{2}:[0-9]{2})',value) + require(match is not None,'exact RFC3339 with at most nine fractional digits') + year,month,day,hour,minute,second=(int(match[i]) for i in range(1,7)) + require(hour<24 and minute<60 and second<60,'valid bounded RFC3339 clock; leap-second spelling unsupported') + try:days=(dt.date(year,month,day)-dt.date(1970,1,1)).days + except ValueError:raise RuntimeError('valid RFC3339 calendar date') from None + offset=match[8];offset_seconds=0 + if offset not in ('Z','z'): + offset_hours=int(offset[1:3]);offset_minutes=int(offset[4:6]) + require(offset_hours<24 and offset_minutes<60,'valid RFC3339 UTC offset') + offset_seconds=(offset_hours*60+offset_minutes)*60*(1 if offset[0]=='+' else -1) + seconds=((days*24+hour)*60+minute)*60+second-offset_seconds + return seconds*1_000_000_000+int((match[7] or '').ljust(9,'0')) + + +def guards(): + require(support.sha(ROOT/'deploy/script_console_install.py')==INSTALL_SHA + and support.sha(ROOT/'deploy/script_console_bootstrap.py')==old.PREFLIGHT_SHA + and support.sha(ROOT/'deploy/cloud_mvp_acceptance.py')==PATTERN_SHA + and support.sha(TEMPLATE)==TEMPLATE_SHA and support.sha(ORIGINAL)==ORIGINAL_SHA,'exact reviewed template/transport/target') + old.pre.native.verify_support();manifest=support.load(old.pre.native.PACK/'manifest.json') + require(support.sha(old.pre.native.PACK/'manifest.json')==old.pre.native.MANIFEST_SHA + and len(manifest['source_files'])==158 and all(support.sha(ROOT/n)==h for n,h in manifest['source_files'].items()),'frozen158 inputs unchanged') + template=support.load(TEMPLATE);script=template['parameters']['variants'][0]['script_utf8'].encode() + require(len(script)==130 and hashlib.sha256(script).hexdigest()==SCRIPT_SHA,'exact benign130 script bytes') + return template,support.load(ORIGINAL) + + +def stored_json_slice(raw,path): + """Slice a nested value directly from stored UTF-8 JSON, never reencode it.""" + text=raw.decode('utf-8');decoder=json.JSONDecoder(object_pairs_hook=support.object_pairs);start=0 + for component in path: + while text[start].isspace():start+=1 + opening=text[start];cursor=start+1;found=None + while True: + while text[cursor].isspace() or text[cursor]==',':cursor+=1 + if text[cursor] in '}]':break + if opening=='{': + key,cursor=decoder.raw_decode(text,cursor) + while text[cursor].isspace():cursor+=1 + require(text[cursor]==':','stored object separator');cursor+=1 + while text[cursor].isspace():cursor+=1 + else:key=0 if found is None else found + begin=cursor;_,cursor=decoder.raw_decode(text,cursor) + if key==component:start=begin;break + if opening=='[':found=key+1 + else:raise RuntimeError('Stored JSON path') + require(key==component,'stored value path present') + _,end=decoder.raw_decode(text,start);return text[start:end].encode() + + +def checked_plan(record,intent): + raw=base64.b64decode(record['payload_b64'],validate=True);require(len(raw)<=65536,'bounded original plan') + plan=json.loads(raw,object_pairs_hook=support.object_pairs);require(hashlib.sha256(raw).hexdigest()==record['plan_hash'],'hash original stored plan bytes') + require(plan['plan_id']==record['plan_id'] and str(uuid.UUID(plan['plan_id']))==plan['plan_id'] + and plan['schema_version']==1 and plan['tenant_id']==intent['target']['tenant_id'] and plan['principal']==CREATOR + and plan['reason']==intent['input']['reason'] and plan['max_parallel']==plan['halt_after_failures']==1 + and len(plan['jobs'])==1 and plan.get('recipe_references',[])==plan.get('artifact_references',[])==[], 'one exact creator/target/rollout plan') + entry=plan['jobs'][0];job=entry['payload'];job_raw=stored_json_slice(raw,['jobs',0,'payload']) + expected=dict(schema_version=1,**intent['target'],job_id=job['job_id'],attempt_id=job['attempt_id'], + action=dict(action='run_script',interpreter='posix_sh',script_utf8=intent['input']['parameters']['variants'][0]['script_utf8'],arguments=[],expected_stdout=STDOUT.decode(),privileged_script_profile='full_maintenance_v1'), + execution_identity='root',not_before=job['not_before'],start_before=job['start_before'],max_runtime_seconds=30,max_output_bytes=4096) + require(job==expected and all(str(uuid.UUID(job[k]))==job[k] for k in ('job_id','attempt_id')) + and timestamp_ns(job['not_before'])==timestamp_ns(intent['input']['schedule']['not_before']) + and timestamp_ns(job['start_before'])==timestamp_ns(intent['input']['schedule']['start_before']) + and hashlib.sha256(job_raw).hexdigest()==entry['job_payload_hash'] and entry['script_sha256']==SCRIPT_SHA,'exact stored job bytes/IDs/identity/profile/script/limits') + view=dict(plan_id=plan['plan_id'],plan_hash=record['plan_hash'],payload=plan,plan_payload=plan,plan_payload_bytes=list(raw), + jobs=[dict(job_id=job['job_id'],attempt_id=job['attempt_id'],asset_id=job['asset_id'],agent_id=job['agent_id'],job_payload_hash=entry['job_payload_hash'],script_sha256=SCRIPT_SHA,payload=job)], + resolved_targets=[dict(asset_id=job['asset_id'],agent_id=job['agent_id'])],excluded_targets=[],authorization_status='required',approval_state=None,expires_at=job['start_before']) + return dict(record=record,view=view,job=job,job_bytes_b64=base64.b64encode(job_raw).decode(),job_payload_hash=entry['job_payload_hash']) + + +def server(request): + import base64 + import datetime as dt + import hashlib + import http.client as http_client + import json + import os + from pathlib import Path + import socket + import ssl + import stat + import subprocess + import time + import uuid + p=request['intent'];case_id=str(uuid.UUID(p['id']));require(case_id==p['id'] and request['action'] in ('plan','submit','read','mcp-result','isolation'),'fixed MCP/evidence case') + target=p['target'];tenant=str(uuid.UUID(target['tenant_id']));agent=str(uuid.UUID(target['agent_id']));asset=str(uuid.UUID(target['asset_id'])) + require(tenant=='11111111-1111-4111-8111-111111111111' and target['agent_id']=='e9d85af1-067f-4cbe-a547-3a63b70a8d2e' and asset=='30aad079-86b3-41f2-a1e9-34275e7c08fc','only actual Cloud Linux target') + creator='automation:'+tenant;approver='simulated-testprincipal-a';root=Path('/opt/rust-discovery') + def quote(text):return "'"+text.replace("'","''")+"'" + def sql(query): + cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm'] + done=subprocess.run(cmd,input=('BEGIN READ ONLY; '+query+'; COMMIT;').encode(),capture_output=True,check=True,timeout=25) + require(len(done.stdout)<=4*1024*1024,'bounded fixed read-only SQL evidence');return json.loads(done.stdout) + def current_rights(): + rows=sql("SELECT coalesce(json_agg(row_to_json(m)), '[]') FROM (SELECT principal,kind,permissions,revoked_at FROM memberships WHERE tenant_id="+quote(tenant)+" AND principal IN ("+quote(creator)+","+quote(approver)+") ORDER BY principal)m") + roles={r['principal']:r for r in rows};require(set(roles)=={creator,approver} and all(r['revoked_at'] is None for r in rows),'existing active actors') + require(roles[creator]['kind']=='automation' and roles[approver]['kind']=='test_human' + and set(roles[creator]['permissions'])=={'inventory:read','logs:read','operations:plan','operations:submit','operations:read','jobs:cancel','scripts:execute','root:execute'} + and 'plans:approve' not in roles[creator]['permissions'] + and {'plans:approve','scripts:execute','root:execute'}<=set(roles[approver]['permissions']),'current exact actor kinds and required rights before MCP mutation') + return rows + def policy_guard(): + path=root/'devstate/root-policy/policy.json';m=path.lstat();raw=path.read_bytes() + require(not path.is_symlink() and m.st_uid==10001 and stat.S_IMODE(m.st_mode)==0o600 and hashlib.sha256(raw).hexdigest()==p['policy_sha256'],'explicit reviewed protected Root policy') + policy=json.loads(raw);require(policy['schema_version']==2 and policy['profile']=='development' and len(policy['scopes'])==1,'single reviewed development Fullscope') + scope=policy['scopes'][0] + require(all(scope[k]==v for k,v in target.items()) and set(scope['principals'])=={creator,approver} + and scope['helper_binary_sha256']=='79442727c3ba31cda02a8bc0e67481f14db9f39fc07520a880a90b64f10f1eaf' + and scope['helper_policy_sha256']=='8101afd12b303c23bad57339729569262d4c1d66c741da9d6d4a72ebf3495ece' + and scope['allow_root_scripts'] is True and scope['allowed_script_profiles']==['full_maintenance_v1'] + and scope['allowed_services']==[] and scope['allow_reboot'] is False and 'apt' not in scope,'exact narrow Fullscope/code/policy/actors') + expiry=timestamp_ns(policy['expires_at']);deadline=timestamp_ns(p['input']['schedule']['start_before']) + require(expiry>=deadline>time.time_ns(),'explicit current start deadline within policy expiry') + return dict(sha256=p['policy_sha256'],expires_at=policy['expires_at']) + def http(body,headers,principal): + class HTTPS(http_client.HTTPSConnection): + def connect(self):self.sock=self._context.wrap_socket(socket.create_connection(('127.0.0.1',18443),timeout=15),server_hostname='localhost') + require(principal in ('a','b'),'fixed existing server-only credential') + token_path=root/('devstate/operator-'+principal+'.token');m=token_path.lstat();require(not token_path.is_symlink() and stat.S_ISREG(m.st_mode) and m.st_uid in (0,10001) and not m.st_mode&0o077 and m.st_size<=8192,'existing private server-only operator credential') + client=HTTPS('localhost',18443,timeout=15,context=ssl.create_default_context(cafile=str(root/'devstate/api-pki/ca.pem'))) + try: + client.request('POST','/mcp',body=json.dumps(body),headers={'Content-Type':'application/json','Authorization':'Bearer '+token_path.read_text().strip(),**headers}) + response=client.getresponse();session=response.getheader('Mcp-Session-Id');status=response.status;data=None + if status!=202: + if 'text/event-stream' in response.getheader('Content-Type',''): + parts=[];total=0 + for _ in range(256): + line=response.readline(1024*1024+1);total+=len(line);require(total<=1024*1024,'bounded MCP SSE') + if not line:break + if line.startswith(b'data:'):parts.append(line[5:].lstrip(b' ').rstrip(b'\r\n')) + if not line.strip() and parts: + value=json.loads(b'\n'.join(parts));parts=[] + if 'result' in value or 'error' in value:data=value;break + else: + raw=response.read(1024*1024+1);require(len(raw)<=1024*1024,'bounded MCP JSON');data=json.loads(raw) if raw else None + return status,session,data + finally:client.close() + def mcp(name,args,principal='a',expect_denied=False): + require((principal=='a' and not expect_denied and name in ('operations.plan','operations.submit','jobs.get')) + or (principal=='b' and expect_denied and name in ('jobs.get','inventory.get_device')),'only fixed mutation or affected readonly MCP methods; no approvals/cancel') + headers={'Accept':'application/json, text/event-stream','MCP-Protocol-Version':'2025-11-25'} + status,session,data=http(dict(jsonrpc='2.0',id=1,method='initialize',params=dict(protocolVersion='2025-11-25',capabilities={},clientInfo=dict(name='script-console-cloud-only',version='1'))),headers,principal) + require(status==200 and session and data['jsonrpc']=='2.0' and data['id']==1 + and data['result']['protocolVersion']=='2025-11-25','existing verified MCP initialize') + headers['Mcp-Session-Id']=session + require(http(dict(jsonrpc='2.0',method='notifications/initialized'),headers,principal)[0]==202,'MCP initialized notification') + status,_,data=http(dict(jsonrpc='2.0',id=2,method='tools/call',params=dict(name=name,arguments=args)),headers,principal) + if expect_denied: + require(status==200 and data and data.get('id')==2 and data.get('jsonrpc')=='2.0' + and data.get('result',{}).get('isError') is True,'actual authenticated MCP tool denial required') + denial=data['result']['structuredContent'] + require(set(denial)=={'code','retryable','message','correlation_id','details'} and denial['code'] in ('FORBIDDEN','NOT_FOUND') + and denial['retryable'] is False and denial['details']=={},'no successful other-tenant response or leaked details') + return dict(tool=name,http_status=status,valid_authenticated_initialize=True,is_error=True,code=denial['code'],details={}) + require(status==200 and data and data['jsonrpc']=='2.0' and data['id']==2 + and 'result' in data and not data['result'].get('isError'),'MCP call incomplete; retain case and read only') + return data['result']['structuredContent'] + def safe(path,directory=False): + m=path.lstat();require(not path.is_symlink() and m.st_uid==0 and stat.S_IMODE(m.st_mode)==(0o700 if directory else 0o600) + and (stat.S_ISDIR(m.st_mode) if directory else stat.S_ISREG(m.st_mode) and m.st_nlink==1),'private bootstrap case') + def once(path,value): + raw=json.dumps(value,sort_keys=True,separators=(',',':')).encode();fd=os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600) + with os.fdopen(fd,'wb') as out:out.write(raw);out.flush();os.fsync(out.fileno()) + fd=os.open(path.parent,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW) + try:os.fsync(fd) + finally:os.close(fd) + parent=root/'devstate/script-console-job-acceptance';case=parent/case_id + if request['action'] in ('plan','submit'): + rights=current_rights();policy=policy_guard() + os.umask(0o077) + if request['action']=='plan': + if not parent.exists():parent.mkdir(mode=0o700) + safe(parent,True);case.mkdir(mode=0o700);once(case/'intent.json',p) + safe(parent,True);safe(case,True);safe(case/'intent.json') + require(json.loads((case/'intent.json').read_bytes())==p,'original immutable remote intent') + if request['action']=='plan': + once(case/'plan-entered.json',dict(plan_once=True,rights=rights,policy=policy)) + result=mcp('operations.plan',dict(tenant_id=tenant,input=p['input']));once(case/'plan-result.json',result);return dict(plan=result,rights=rights,policy=policy) + stored=request['plan'];plan_id=str(uuid.UUID(stored['plan_id'])) + require(plan_id==stored['plan_id'],'canonical stored plan ID') + actual=sql("SELECT json_build_object('payload_b64',replace(encode(payload,'base64'),chr(10),''),'principal',principal,'reason',convert_from(payload,'UTF8')::jsonb->>'reason') FROM operation_plans WHERE tenant_id="+quote(tenant)+" AND plan_id="+quote(plan_id)) + require(actual['principal']==creator and actual['reason']==p['input']['reason'] + and hashlib.sha256(base64.b64decode(actual['payload_b64'],validate=True)).hexdigest()==stored['plan_hash'],'actual original plan before submit') + once(case/'submit-entered.json',dict(submit_once=True,plan_id=plan_id,plan_hash=stored['plan_hash'],idempotency_key=p['idempotency_key'],rights=rights,policy=policy)) + result=mcp('operations.submit',dict(tenant_id=tenant,plan_id=plan_id,idempotency_key=p['idempotency_key']));once(case/'submit-result.json',result);return dict(pending=result,rights=rights,policy=policy) + if request['action'] in ('mcp-result','isolation'): + stored=request['plan'];plan_id=str(uuid.UUID(stored['plan_id'])) + actual=sql("SELECT json_build_object('payload_b64',replace(encode(payload,'base64'),chr(10),''),'principal',principal) FROM operation_plans WHERE tenant_id="+quote(tenant)+" AND plan_id="+quote(plan_id)) + raw=base64.b64decode(actual['payload_b64'],validate=True) + require(actual['principal']==creator and hashlib.sha256(raw).hexdigest()==stored['plan_hash'],'same actual stored A-plan for readonly HTTP proof') + plan=json.loads(raw);require(plan['reason']==p['input']['reason'] and len(plan['jobs'])==1,'same one fixed acceptance plan') + job=plan['jobs'][0]['payload'];job_id=str(uuid.UUID(job['job_id'])) + require(job['tenant_id']==tenant and job['agent_id']==agent and job['asset_id']==asset,'same existing A-job/asset for readonly HTTP proof') + if request['action']=='mcp-result':return dict(result=mcp('jobs.get',dict(tenant_id=tenant,job_id=job_id)),read_only=True) + return dict(read_only=True,credential='existing server-only operator-b',tests=[ + mcp('jobs.get',dict(tenant_id=tenant,job_id=job_id),'b',True), + mcp('inventory.get_device',dict(tenant_id=tenant,asset_id=asset),'b',True)]) + # All recovery/status queries are read-only. In particular do not use the + # operation getter, which may expire a pending request in application code. + reason=quote(p['input']['reason']);key=quote(p['idempotency_key']);scope="tenant_id="+quote(tenant) + plans=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT plan_id,plan_hash,replace(encode(payload,'base64'),chr(10),'') AS payload_b64 FROM operation_plans WHERE "+scope+" AND principal="+quote(creator)+" AND convert_from(payload,'UTF8')::jsonb->>'reason'="+reason+" ORDER BY created_at LIMIT 2)q") + require(len(plans)<=1,'ambiguous stored plan; no retry') + markers={} + if case.exists(): + safe(parent,True);safe(case,True);safe(case/'intent.json');require(json.loads((case/'intent.json').read_bytes())==p,'original case readback') + for name in ('plan-entered','submit-entered'):markers[name]=os.path.lexists(case/(name+'.json')) + result=dict(plans=plans,markers=markers,read_only=True,approvals_performed=False) + if not plans:return result + plan_id=str(uuid.UUID(plans[0]['plan_id']));plan=json.loads(base64.b64decode(plans[0]['payload_b64']));job=plan['jobs'][0]['payload'];job_id=str(uuid.UUID(job['job_id']));attempt=str(uuid.UUID(job['attempt_id'])) + require(job['agent_id']==agent and job['asset_id']==asset and job['tenant_id']==tenant,'fixed target in stored plan') + selected=scope+' AND job_id='+quote(job_id)+' AND attempt_id='+quote(attempt) + result['requests']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT r.operation_id,r.plan_id,r.principal,r.approval_state,r.idempotency_key,r.request_hash FROM approval_requests r JOIN operation_submissions s USING(tenant_id,operation_id) WHERE r.tenant_id="+quote(tenant)+" AND r.plan_id="+quote(plan_id)+" AND s.principal="+quote(creator)+" AND s.idempotency_key="+key+")q") + result['counts']=sql("SELECT json_build_object('jobs',(SELECT count(*) FROM jobs WHERE "+selected+"),'outbox',(SELECT count(*) FROM outbox WHERE "+selected+"),'permits',(SELECT count(*) FROM execution_permits WHERE "+selected+"),'authorizations',(SELECT count(*) FROM authorizations WHERE "+scope+" AND plan_id="+quote(plan_id)+"))") + result['jobs']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT job_id,attempt_id,operation_id,agent_id,asset_id,payload_hash,replace(encode(payload,'base64'),chr(10),'') AS payload_b64,state,result FROM jobs WHERE "+selected+")q") + result['permits']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT grant_id,replace(encode(payload,'base64'),chr(10),'') AS payload_b64,replace(encode(signature,'base64'),chr(10),'') AS signature_b64 FROM execution_permits WHERE "+selected+")q") + result['authorizations']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT authorization_id,operation_id,plan_id,plan_hash,principal,approval_origin,testsimulation FROM authorizations WHERE "+scope+" AND plan_id="+quote(plan_id)+")q") + result['audit']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT event_id,principal,event_type,data,created_at FROM audit_events WHERE "+scope+" AND event_type='plan_authorization' AND data->>'plan_id'="+quote(plan_id)+" ORDER BY created_at)q") + return result + + +def native_evidence(p): + import base64 + import hashlib + import json + import os + from pathlib import Path + import sqlite3 + import subprocess + import uuid + job=p['job'];require(os.geteuid()==0 and str(uuid.UUID(job['job_id']))==job['job_id'] and str(uuid.UUID(job['attempt_id']))==job['attempt_id'],'fixed native evidence binding') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==p['boot_id'],'same native boot') + processes={} + for name,key,uid in (('rmm-full-maintenance-helper.service','helper',0),('rmm-agent-remote.service','agent',993)): + pid=int(subprocess.run(['systemctl','show',name,'-p','MainPID','--value'],capture_output=True,check=True,timeout=10).stdout) + proc=Path('/proc',str(pid));birth=(proc/'stat').read_text().rsplit(')',1)[1].split()[19];status=(proc/'status').read_text().splitlines() + require(next(x.split()[1:] for x in status if x.startswith('Uid:'))==[str(uid)]*4 + and next(x.split()[1:] for x in status if x.startswith('Gid:'))==['984']*4 + and hashlib.sha256((proc/'exe').read_bytes()).hexdigest()==p['artifacts'][key]['sha256'] + and (proc/'stat').read_text().rsplit(')',1)[1].split()[19]==birth,'actual qualified process image/accounts/birth') + processes[name]=dict(pid=pid,start_ticks=birth,sha256=p['artifacts'][key]['sha256']) + path=Path('/var/lib/rmm-helper/consumption.sqlite');m=path.lstat() + require(not path.is_symlink() and [m.st_dev,m.st_ino]==p['database_identity'],'original Root database inode') + import time + original_ids=sorted(p['original_rows']);require(len(original_ids)==18,'exact eighteen original Root IDs') + started=time.monotonic_ns();db=sqlite3.connect(path.as_uri()+'?mode=ro',uri=True,timeout=0.25) + try: + db.set_progress_handler(lambda:int(time.monotonic_ns()-started>1_000_000_000),1000) + db.execute('PRAGMA query_only=ON');db.execute('BEGIN') + require(db.execute('PRAGMA journal_mode').fetchone()[0]=='delete','qualified readonly DELETE journal') + total,unresolved=db.execute('SELECT count(*),coalesce(sum(result IS NULL),0) FROM consumed').fetchone() + require(total in (18,19),'at most one new Root consumption') + placeholders=','.join('?' for _ in original_ids) + rows=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ' + +'WHERE c.grant_id IN ('+placeholders+') OR (c.job=? AND c.attempt=?) ORDER BY c.grant_id LIMIT 20', + [*original_ids,job['job_id'],job['attempt_id']]).fetchall() + finally: + # Materialize only the known eighteen plus this one attempt; close the + # shared read transaction before hashing/decoding any retained BLOBs. + try:db.rollback() + finally:db.close() + elapsed=time.monotonic_ns()-started;m=path.lstat() + require(elapsed<=1_000_000_000,'short native read transaction within accepted one-second budget') + require(not path.is_symlink() and [m.st_dev,m.st_ino]==p['database_identity'],'same Root inode after short read') + def digest(row):return hashlib.sha256(json.dumps([dict(sha256=hashlib.sha256(v).hexdigest(),size=len(v)) if isinstance(v,bytes) else v for v in row],separators=(',',':')).encode()).hexdigest() + original={row[0]:digest(row) for row in rows if row[0] in p['original_rows']} + require(original==p['original_rows'] and len(rows)==total,'all original18 bytes retained, only this possible new consumption') + found=[row for row in rows if row[3]==job['job_id'] and row[4]==job['attempt_id']] + require(len(found)<=1 and total==18+len(found),'no unrelated new consumption') + records=[] + for row in found: + require(row[1]==job['agent_id'] and row[2]==job['enrollment_generation'],'native consumption target/generation') + records.append(dict(grant_id=row[0],result_b64=base64.b64encode(row[5]).decode() if row[5] is not None else None, + start_b64=base64.b64encode(row[6]).decode() if row[6] is not None else None,outcome_b64=base64.b64encode(row[7]).decode() if row[7] is not None else None)) + return dict(read_only=True,original_root18_unchanged=True,consumed=total,unresolved=unresolved,records=records,processes=processes, + read_transaction_ns=elapsed,read_transaction_budget_ns=1_000_000_000,sqlite_busy_timeout_ms=250,sqlite_integrity_scan_performed=False) + + + +def rpc(intent,action,folder,**extra): + guards();require(intent['runner_sha256']==support.sha(__file__),'same original runner before every call/read') + require(support.load(folder/'intent-hash.json')==dict(intent_sha256=support.sha(folder/'intent.json')),'original complete local intent bytes before every target call') + code=inspect.getsource(require)+'\n'+inspect.getsource(timestamp_ns)+'\n'+inspect.getsource(server) + code+='\ntry:\n print(__import__("json").dumps(server('+repr(dict(intent=intent,action=action,**extra))+')))\nexcept Exception as e:\n print(__import__("json").dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n' + return support.transport(old.server_prefix(),'python3 -',code.encode(),folder,timeout=230) + + +def retain_plan(folder,record,intent): + plan=checked_plan(record,intent);path=folder/'stored-plan.json' + if path.exists():require(support.load(path)==plan,'original plan bytes/IDs/hash never replaced') + else:support.save(path,plan) + return plan + + +def read_case(intent,folder): + evidence=rpc(intent,'read',folder);support.save(folder/('server-read-'+str(uuid.uuid4())+'.json'),evidence) + if not evidence['plans']:return dict(state='plan_effect_unknown' if (folder/'plan-entered.json').exists() else 'not_planned',evidence=evidence) + plan=retain_plan(folder,evidence['plans'][0],intent);_,original=guards() + require(intent['runner_sha256']==support.sha(__file__) + and support.load(folder/'intent-hash.json')==dict(intent_sha256=support.sha(folder/'intent.json')),'same original source/intent before native read') + identity=original['preflight']['database_inodes']['/var/lib/rmm-helper/consumption.sqlite'] + native=old.rpc(support.access_settings(old.pre.ACCESS_ROOT,'linux'),native_evidence,dict(job=plan['job'],boot_id=original['preflight']['boot_id'],artifacts=original['artifacts'],database_identity=identity,original_rows=original['preflight']['root_rows']),folder) + support.save(folder/('native-read-'+str(uuid.uuid4())+'.json'),native) + return dict(plan=plan,evidence=evidence,native=native) + + +def pending_checkpoint(intent,folder,value): + evidence=value['evidence'];require(len(evidence['requests'])==1,'one immutable submission lookup') + pending=evidence['requests'][0];require(pending['approval_state']=='pending_approval' and pending['principal']==CREATOR + and pending['idempotency_key']==intent['idempotency_key'] and evidence['counts']==dict(jobs=0,outbox=0,permits=0,authorizations=0) + and evidence['jobs']==evidence['permits']==evidence['authorizations']==evidence['audit']==[] + and value['native']['records']==[] and value['native']['consumed']==18 and value['native']['unresolved']==0,'pending has zero executable artifacts and no native consumption') + plan=value['plan'];require(pending['plan_id']==plan['record']['plan_id'],'same pending plan') + checkpoint=dict(profile='dev/test',case='script-console-linux-full-v1',**intent['target'],expected_principal=APPROVER, + plan=plan['view'],job=plan['view']['jobs'][0],request=intent['input'],operation_id=pending['operation_id'], + plan_hash=plan['record']['plan_hash'],plan_payload_bytes=plan['view']['plan_payload_bytes'],job_payload=plan['job'],job_payload_hash=plan['job_payload_hash'], + preapproval=evidence['counts'],operation=dict(operation_id=pending['operation_id'],plan_id=pending['plan_id'],plan_hash=plan['record']['plan_hash'], + approval_state='pending_approval',planned_job_count=1,job_count=0,execution_state=None,jobs=[]), + operation_view_origin='read_only_scoped_SQL',stage='pending_approval',script_sha256=SCRIPT_SHA,no_retry=True,approval_performed=False) + path=folder/'pending-checkpoint.json' + if path.exists():require(support.load(path)==checkpoint,'original pending checkpoint remains immutable') + else:support.save(path,checkpoint) + return checkpoint + + +def verify_result(intent,value): + plan=value['plan'];evidence=value['evidence'];native=value['native'] + require(evidence['counts']==dict(jobs=1,outbox=1,permits=1,authorizations=1) + and len(evidence['requests'])==len(evidence['jobs'])==len(evidence['permits'])==len(evidence['authorizations'])==len(evidence['audit'])==1,'one job/outbox/grant/authorization only') + request=evidence['requests'][0];job=evidence['jobs'][0];permit=evidence['permits'][0];auth=evidence['authorizations'][0];audit=evidence['audit'][0] + require(request['approval_state']=='approved' and request['principal']==CREATOR and request['idempotency_key']==intent['idempotency_key'] + and auth['principal']==APPROVER and auth['approval_origin']=='dev_test_browser_simulation' and auth['testsimulation'] is True + and auth['plan_hash']==plan['record']['plan_hash'] and auth['operation_id']==request['operation_id']==job['operation_id'] + and auth['plan_id']==request['plan_id']==plan['record']['plan_id'] + and audit['principal']==APPROVER and audit['data']['approval_origin']=='dev_test_browser_simulation' and audit['data']['testsimulation'] is True + and audit['data']['decision']=='approve' and audit['data']['profile']=='development' + and audit['data']['plan_id']==plan['record']['plan_id'] and audit['data']['plan_hash']==plan['record']['plan_hash'] and audit['data']['operation_id']==request['operation_id'],'actual simulated WebUI audit binding, never production human approval') + raw_job=base64.b64decode(plan['job_bytes_b64'],validate=True) + require(job['state']=='succeeded' and job['job_id']==plan['job']['job_id'] and job['attempt_id']==plan['job']['attempt_id'] + and job['agent_id']==plan['job']['agent_id'] and job['asset_id']==plan['job']['asset_id'] + and job['payload_hash']==plan['job_payload_hash'] and base64.b64decode(job['payload_b64'],validate=True)==raw_job,'same successful stored job/attempt bytes') + result=job['result'];require(result['exit_code']==0 and result['output_truncated'] is False and result['reboot_required'] is False + and bytes(result['stdout'])==STDOUT and bytes(result['stderr'])==STDERR and result['evidence']['postconditions_passed'] is True + and result['evidence']['execution_uid']==0 and result['evidence']['privileged_script_profile']=='full_maintenance_v1' + and result['evidence']['script_sha256']==SCRIPT_SHA,'exact native root/profile/stdout/stderr result') + require(native['original_root18_unchanged'] is True and native['consumed']==19 and native['unresolved']==0 and len(native['records'])==1,'one finished new Root consumption') + record=native['records'][0];start=json.loads(base64.b64decode(record['start_b64'],validate=True));outcome=json.loads(base64.b64decode(record['outcome_b64'],validate=True));finished=json.loads(base64.b64decode(record['result_b64'],validate=True));request=start['request'] + permit_raw=base64.b64decode(permit['payload_b64'],validate=True);grant=json.loads(permit_raw) + require(record['grant_id']==permit['grant_id']==grant['grant_id']==result['grant_id'] + and bytes(request['job_bytes'])==raw_job and bytes(request['permit_bytes'])==permit_raw + and bytes(request['signature'])==base64.b64decode(permit['signature_b64'],validate=True) + and request['plan_hash']==grant['plan_hash']==plan['record']['plan_hash'] and request['authorization_id']==grant['authorization_id']==auth['authorization_id'] + and grant['job_payload_hash']==plan['job_payload_hash'] and all(grant[k]==plan['job'][k] for k in (*intent['target'].keys(),'job_id','attempt_id','execution_identity','max_runtime_seconds')) + and grant['domain']=='rmm.execution-permit' and grant['schema_version']==1 + and grant['action']=='run_script' and grant['key_id']==intent['signing_key_id'] + and timestamp_ns(plan['job']['not_before'])<=timestamp_ns(grant['not_before']) + <=timestamp_ns(start['reserved_at']) integer epoch nanoseconds, no rounding.""" + import datetime as dt + import re + require(isinstance(value,str),'RFC3339 timestamp string') + match=re.fullmatch(r'([0-9]{4})-([0-9]{2})-([0-9]{2})[Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]{1,9}))?([Zz]|[+-][0-9]{2}:[0-9]{2})',value) + require(match is not None,'exact RFC3339 with at most nine fractional digits') + year,month,day,hour,minute,second=(int(match[i]) for i in range(1,7)) + require(hour<24 and minute<60 and second<60,'valid bounded RFC3339 clock; leap-second spelling unsupported') + try:days=(dt.date(year,month,day)-dt.date(1970,1,1)).days + except ValueError:raise RuntimeError('valid RFC3339 calendar date') from None + offset=match[8];offset_seconds=0 + if offset not in ('Z','z'): + offset_hours=int(offset[1:3]);offset_minutes=int(offset[4:6]) + require(offset_hours<24 and offset_minutes<60,'valid RFC3339 UTC offset') + offset_seconds=(offset_hours*60+offset_minutes)*60*(1 if offset[0]=='+' else -1) + seconds=((days*24+hour)*60+minute)*60+second-offset_seconds + return seconds*1_000_000_000+int((match[7] or '').ljust(9,'0')) + + +def guards(): + require(support.sha(ROOT/'deploy/script_console_install.py')==INSTALL_SHA + and support.sha(ROOT/'deploy/script_console_bootstrap.py')==old.PREFLIGHT_SHA + and support.sha(ROOT/'deploy/cloud_mvp_acceptance.py')==PATTERN_SHA + and support.sha(TEMPLATE)==TEMPLATE_SHA and support.sha(ORIGINAL)==ORIGINAL_SHA,'exact reviewed template/transport/target') + old.pre.native.verify_support();manifest=support.load(old.pre.native.PACK/'manifest.json') + require(support.sha(old.pre.native.PACK/'manifest.json')==old.pre.native.MANIFEST_SHA + and len(manifest['source_files'])==158 and all(support.sha(ROOT/n)==h for n,h in manifest['source_files'].items()),'frozen158 inputs unchanged') + template=support.load(TEMPLATE);script=template['parameters']['variants'][0]['script_utf8'].encode() + require(len(script)==130 and hashlib.sha256(script).hexdigest()==SCRIPT_SHA,'exact benign130 script bytes') + return template,support.load(ORIGINAL) + + +def stored_json_slice(raw,path): + """Slice a nested value directly from stored UTF-8 JSON, never reencode it.""" + text=raw.decode('utf-8');decoder=json.JSONDecoder(object_pairs_hook=support.object_pairs);start=0 + for component in path: + while text[start].isspace():start+=1 + opening=text[start];cursor=start+1;found=None + while True: + while text[cursor].isspace() or text[cursor]==',':cursor+=1 + if text[cursor] in '}]':break + if opening=='{': + key,cursor=decoder.raw_decode(text,cursor) + while text[cursor].isspace():cursor+=1 + require(text[cursor]==':','stored object separator');cursor+=1 + while text[cursor].isspace():cursor+=1 + else:key=0 if found is None else found + begin=cursor;_,cursor=decoder.raw_decode(text,cursor) + if key==component:start=begin;break + if opening=='[':found=key+1 + else:raise RuntimeError('Stored JSON path') + require(key==component,'stored value path present') + _,end=decoder.raw_decode(text,start);return text[start:end].encode() + + +def checked_plan(record,intent): + raw=base64.b64decode(record['payload_b64'],validate=True);require(len(raw)<=65536,'bounded original plan') + plan=json.loads(raw,object_pairs_hook=support.object_pairs);require(hashlib.sha256(raw).hexdigest()==record['plan_hash'],'hash original stored plan bytes') + require(plan['plan_id']==record['plan_id'] and str(uuid.UUID(plan['plan_id']))==plan['plan_id'] + and plan['schema_version']==1 and plan['tenant_id']==intent['target']['tenant_id'] and plan['principal']==CREATOR + and plan['reason']==intent['input']['reason'] and plan['max_parallel']==plan['halt_after_failures']==1 + and len(plan['jobs'])==1 and plan.get('recipe_references',[])==plan.get('artifact_references',[])==[], 'one exact creator/target/rollout plan') + entry=plan['jobs'][0];job=entry['payload'];job_raw=stored_json_slice(raw,['jobs',0,'payload']) + expected=dict(schema_version=1,**intent['target'],job_id=job['job_id'],attempt_id=job['attempt_id'], + action=dict(action='run_script',interpreter='posix_sh',script_utf8=intent['input']['parameters']['variants'][0]['script_utf8'],arguments=[],expected_stdout=STDOUT.decode(),privileged_script_profile='full_maintenance_v1'), + execution_identity='root',not_before=job['not_before'],start_before=job['start_before'],max_runtime_seconds=30,max_output_bytes=4096) + require(job==expected and all(str(uuid.UUID(job[k]))==job[k] for k in ('job_id','attempt_id')) + and timestamp_ns(job['not_before'])==timestamp_ns(intent['input']['schedule']['not_before']) + and timestamp_ns(job['start_before'])==timestamp_ns(intent['input']['schedule']['start_before']) + and hashlib.sha256(job_raw).hexdigest()==entry['job_payload_hash'] and entry['script_sha256']==SCRIPT_SHA,'exact stored job bytes/IDs/identity/profile/script/limits') + view=dict(plan_id=plan['plan_id'],plan_hash=record['plan_hash'],payload=plan,plan_payload=plan,plan_payload_bytes=list(raw), + jobs=[dict(job_id=job['job_id'],attempt_id=job['attempt_id'],asset_id=job['asset_id'],agent_id=job['agent_id'],job_payload_hash=entry['job_payload_hash'],script_sha256=SCRIPT_SHA,payload=job)], + resolved_targets=[dict(asset_id=job['asset_id'],agent_id=job['agent_id'])],excluded_targets=[],authorization_status='required',approval_state=None,expires_at=job['start_before']) + return dict(record=record,view=view,job=job,job_bytes_b64=base64.b64encode(job_raw).decode(),job_payload_hash=entry['job_payload_hash']) + + +def server(request): + import base64 + import datetime as dt + import hashlib + import http.client as http_client + import json + import os + from pathlib import Path + import socket + import ssl + import stat + import subprocess + import time + import uuid + p=request['intent'];case_id=str(uuid.UUID(p['id']));require(case_id==p['id'] and request['action'] in ('plan','submit','read','mcp-result','isolation'),'fixed MCP/evidence case') + target=p['target'];tenant=str(uuid.UUID(target['tenant_id']));agent=str(uuid.UUID(target['agent_id']));asset=str(uuid.UUID(target['asset_id'])) + require(tenant=='11111111-1111-4111-8111-111111111111' and target['agent_id']=='e9d85af1-067f-4cbe-a547-3a63b70a8d2e' and asset=='30aad079-86b3-41f2-a1e9-34275e7c08fc','only actual Cloud Linux target') + creator='automation:'+tenant;approver='simulated-testprincipal-a';root=Path('/opt/rust-discovery') + def quote(text):return "'"+text.replace("'","''")+"'" + def sql(query): + cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm'] + done=subprocess.run(cmd,input=('BEGIN READ ONLY; '+query+'; COMMIT;').encode(),capture_output=True,check=True,timeout=25) + require(len(done.stdout)<=4*1024*1024,'bounded fixed read-only SQL evidence');return json.loads(done.stdout) + def current_rights(): + rows=sql("SELECT coalesce(json_agg(row_to_json(m)), '[]') FROM (SELECT principal,kind,permissions,revoked_at FROM memberships WHERE tenant_id="+quote(tenant)+" AND principal IN ("+quote(creator)+","+quote(approver)+") ORDER BY principal)m") + roles={r['principal']:r for r in rows};require(set(roles)=={creator,approver} and all(r['revoked_at'] is None for r in rows),'existing active actors') + require(roles[creator]['kind']=='automation' and roles[approver]['kind']=='test_human' + and set(roles[creator]['permissions'])=={'inventory:read','logs:read','operations:plan','operations:submit','operations:read','jobs:cancel','scripts:execute','root:execute'} + and 'plans:approve' not in roles[creator]['permissions'] + and {'plans:approve','scripts:execute','root:execute'}<=set(roles[approver]['permissions']),'current exact actor kinds and required rights before MCP mutation') + return rows + def policy_guard(): + path=root/'devstate/root-policy/policy.json';m=path.lstat();raw=path.read_bytes() + require(not path.is_symlink() and m.st_uid==10001 and stat.S_IMODE(m.st_mode)==0o600 and hashlib.sha256(raw).hexdigest()==p['policy_sha256'],'explicit reviewed protected Root policy') + policy=json.loads(raw);require(policy['schema_version']==2 and policy['profile']=='development' and len(policy['scopes'])==1,'single reviewed development Fullscope') + scope=policy['scopes'][0] + require(all(scope[k]==v for k,v in target.items()) and set(scope['principals'])=={creator,approver} + and scope['helper_binary_sha256']=='79442727c3ba31cda02a8bc0e67481f14db9f39fc07520a880a90b64f10f1eaf' + and scope['helper_policy_sha256']=='8101afd12b303c23bad57339729569262d4c1d66c741da9d6d4a72ebf3495ece' + and scope['allow_root_scripts'] is True and scope['allowed_script_profiles']==['full_maintenance_v1'] + and scope['allowed_services']==[] and scope['allow_reboot'] is False and 'apt' not in scope,'exact narrow Fullscope/code/policy/actors') + expiry=timestamp_ns(policy['expires_at']);deadline=timestamp_ns(p['input']['schedule']['start_before']) + require(expiry>=deadline>time.time_ns(),'explicit current start deadline within policy expiry') + return dict(sha256=p['policy_sha256'],expires_at=policy['expires_at']) + def http(body,headers,principal): + class HTTPS(http_client.HTTPSConnection): + def connect(self):self.sock=self._context.wrap_socket(socket.create_connection(('127.0.0.1',18443),timeout=15),server_hostname='localhost') + require(principal in ('a','b'),'fixed existing server-only credential') + token_path=root/('devstate/operator-'+principal+'.token');m=token_path.lstat();require(not token_path.is_symlink() and stat.S_ISREG(m.st_mode) and m.st_uid in (0,10001) and not m.st_mode&0o077 and m.st_size<=8192,'existing private server-only operator credential') + client=HTTPS('localhost',18443,timeout=15,context=ssl.create_default_context(cafile=str(root/'devstate/api-pki/ca.pem'))) + try: + client.request('POST','/mcp',body=json.dumps(body),headers={'Content-Type':'application/json','Authorization':'Bearer '+token_path.read_text().strip(),**headers}) + response=client.getresponse();session=response.getheader('Mcp-Session-Id');status=response.status;data=None + if status!=202: + if 'text/event-stream' in response.getheader('Content-Type',''): + parts=[];total=0 + for _ in range(256): + line=response.readline(1024*1024+1);total+=len(line);require(total<=1024*1024,'bounded MCP SSE') + if not line:break + if line.startswith(b'data:'): + chunk=line[5:].rstrip(b'\r\n') + if chunk.startswith(b' '):chunk=chunk[1:] + parts.append(chunk) + if not line.strip() and parts: + frame=b'\n'.join(parts);parts=[] + if frame==b'':continue + value=json.loads(frame) + if 'result' in value or 'error' in value:data=value;break + else: + raw=response.read(1024*1024+1);require(len(raw)<=1024*1024,'bounded MCP JSON');data=json.loads(raw) if raw else None + return status,session,data + finally:client.close() + def mcp(name,args,principal='a',expect_denied=False): + require((principal=='a' and not expect_denied and name in ('operations.plan','operations.submit','jobs.get')) + or (principal=='b' and expect_denied and name in ('jobs.get','inventory.get_device')),'only fixed mutation or affected readonly MCP methods; no approvals/cancel') + headers={'Accept':'application/json, text/event-stream','MCP-Protocol-Version':'2025-11-25'} + status,session,data=http(dict(jsonrpc='2.0',id=1,method='initialize',params=dict(protocolVersion='2025-11-25',capabilities={},clientInfo=dict(name='script-console-cloud-only',version='1'))),headers,principal) + require(status==200 and session and data['jsonrpc']=='2.0' and data['id']==1 + and data['result']['protocolVersion']=='2025-11-25','existing verified MCP initialize') + headers['Mcp-Session-Id']=session + require(http(dict(jsonrpc='2.0',method='notifications/initialized'),headers,principal)[0]==202,'MCP initialized notification') + status,_,data=http(dict(jsonrpc='2.0',id=2,method='tools/call',params=dict(name=name,arguments=args)),headers,principal) + if expect_denied: + require(status==200 and data and data.get('id')==2 and data.get('jsonrpc')=='2.0' + and data.get('result',{}).get('isError') is True,'actual authenticated MCP tool denial required') + denial=data['result']['structuredContent'] + require(set(denial)=={'code','retryable','message','correlation_id','details'} and denial['code'] in ('FORBIDDEN','NOT_FOUND') + and denial['retryable'] is False and denial['details']=={},'no successful other-tenant response or leaked details') + return dict(tool=name,http_status=status,valid_authenticated_initialize=True,is_error=True,code=denial['code'],details={}) + require(status==200 and data and data['jsonrpc']=='2.0' and data['id']==2 + and 'result' in data and not data['result'].get('isError'),'MCP call incomplete; retain case and read only') + return data['result']['structuredContent'] + def safe(path,directory=False): + m=path.lstat();require(not path.is_symlink() and m.st_uid==0 and stat.S_IMODE(m.st_mode)==(0o700 if directory else 0o600) + and (stat.S_ISDIR(m.st_mode) if directory else stat.S_ISREG(m.st_mode) and m.st_nlink==1),'private bootstrap case') + def once(path,value): + raw=json.dumps(value,sort_keys=True,separators=(',',':')).encode();fd=os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600) + with os.fdopen(fd,'wb') as out:out.write(raw);out.flush();os.fsync(out.fileno()) + fd=os.open(path.parent,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW) + try:os.fsync(fd) + finally:os.close(fd) + parent=root/'devstate/script-console-job-acceptance';case=parent/case_id + if request['action'] in ('plan','submit'): + rights=current_rights();policy=policy_guard() + os.umask(0o077) + if request['action']=='plan': + if not parent.exists():parent.mkdir(mode=0o700) + safe(parent,True);case.mkdir(mode=0o700);once(case/'intent.json',p) + safe(parent,True);safe(case,True);safe(case/'intent.json') + require(json.loads((case/'intent.json').read_bytes())==p,'original immutable remote intent') + if request['action']=='plan': + once(case/'plan-entered.json',dict(plan_once=True,rights=rights,policy=policy)) + result=mcp('operations.plan',dict(tenant_id=tenant,input=p['input']));once(case/'plan-result.json',result);return dict(plan=result,rights=rights,policy=policy) + stored=request['plan'];plan_id=str(uuid.UUID(stored['plan_id'])) + require(plan_id==stored['plan_id'],'canonical stored plan ID') + actual=sql("SELECT json_build_object('payload_b64',replace(encode(payload,'base64'),chr(10),''),'principal',principal,'reason',convert_from(payload,'UTF8')::jsonb->>'reason') FROM operation_plans WHERE tenant_id="+quote(tenant)+" AND plan_id="+quote(plan_id)) + require(actual['principal']==creator and actual['reason']==p['input']['reason'] + and hashlib.sha256(base64.b64decode(actual['payload_b64'],validate=True)).hexdigest()==stored['plan_hash'],'actual original plan before submit') + once(case/'submit-entered.json',dict(submit_once=True,plan_id=plan_id,plan_hash=stored['plan_hash'],idempotency_key=p['idempotency_key'],rights=rights,policy=policy)) + result=mcp('operations.submit',dict(tenant_id=tenant,plan_id=plan_id,idempotency_key=p['idempotency_key']));once(case/'submit-result.json',result);return dict(pending=result,rights=rights,policy=policy) + if request['action'] in ('mcp-result','isolation'): + stored=request['plan'];plan_id=str(uuid.UUID(stored['plan_id'])) + actual=sql("SELECT json_build_object('payload_b64',replace(encode(payload,'base64'),chr(10),''),'principal',principal) FROM operation_plans WHERE tenant_id="+quote(tenant)+" AND plan_id="+quote(plan_id)) + raw=base64.b64decode(actual['payload_b64'],validate=True) + require(actual['principal']==creator and hashlib.sha256(raw).hexdigest()==stored['plan_hash'],'same actual stored A-plan for readonly HTTP proof') + plan=json.loads(raw);require(plan['reason']==p['input']['reason'] and len(plan['jobs'])==1,'same one fixed acceptance plan') + job=plan['jobs'][0]['payload'];job_id=str(uuid.UUID(job['job_id'])) + require(job['tenant_id']==tenant and job['agent_id']==agent and job['asset_id']==asset,'same existing A-job/asset for readonly HTTP proof') + if request['action']=='mcp-result':return dict(result=mcp('jobs.get',dict(tenant_id=tenant,job_id=job_id)),read_only=True) + return dict(read_only=True,credential='existing server-only operator-b',tests=[ + mcp('jobs.get',dict(tenant_id=tenant,job_id=job_id),'b',True), + mcp('inventory.get_device',dict(tenant_id=tenant,asset_id=asset),'b',True)]) + # All recovery/status queries are read-only. In particular do not use the + # operation getter, which may expire a pending request in application code. + reason=quote(p['input']['reason']);key=quote(p['idempotency_key']);scope="tenant_id="+quote(tenant) + plans=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT plan_id,plan_hash,replace(encode(payload,'base64'),chr(10),'') AS payload_b64 FROM operation_plans WHERE "+scope+" AND principal="+quote(creator)+" AND convert_from(payload,'UTF8')::jsonb->>'reason'="+reason+" ORDER BY created_at LIMIT 2)q") + require(len(plans)<=1,'ambiguous stored plan; no retry') + markers={} + if case.exists(): + safe(parent,True);safe(case,True);safe(case/'intent.json');require(json.loads((case/'intent.json').read_bytes())==p,'original case readback') + for name in ('plan-entered','submit-entered'):markers[name]=os.path.lexists(case/(name+'.json')) + result=dict(plans=plans,markers=markers,read_only=True,approvals_performed=False) + if not plans:return result + plan_id=str(uuid.UUID(plans[0]['plan_id']));plan=json.loads(base64.b64decode(plans[0]['payload_b64']));job=plan['jobs'][0]['payload'];job_id=str(uuid.UUID(job['job_id']));attempt=str(uuid.UUID(job['attempt_id'])) + require(job['agent_id']==agent and job['asset_id']==asset and job['tenant_id']==tenant,'fixed target in stored plan') + selected=scope+' AND job_id='+quote(job_id)+' AND attempt_id='+quote(attempt) + result['requests']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT r.operation_id,r.plan_id,r.principal,r.approval_state,r.idempotency_key,r.request_hash FROM approval_requests r JOIN operation_submissions s USING(tenant_id,operation_id) WHERE r.tenant_id="+quote(tenant)+" AND r.plan_id="+quote(plan_id)+" AND s.principal="+quote(creator)+" AND s.idempotency_key="+key+")q") + result['counts']=sql("SELECT json_build_object('jobs',(SELECT count(*) FROM jobs WHERE "+selected+"),'outbox',(SELECT count(*) FROM outbox WHERE "+selected+"),'permits',(SELECT count(*) FROM execution_permits WHERE "+selected+"),'authorizations',(SELECT count(*) FROM authorizations WHERE "+scope+" AND plan_id="+quote(plan_id)+"))") + result['jobs']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT job_id,attempt_id,operation_id,agent_id,asset_id,payload_hash,replace(encode(payload,'base64'),chr(10),'') AS payload_b64,state,result FROM jobs WHERE "+selected+")q") + result['permits']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT grant_id,replace(encode(payload,'base64'),chr(10),'') AS payload_b64,replace(encode(signature,'base64'),chr(10),'') AS signature_b64 FROM execution_permits WHERE "+selected+")q") + result['authorizations']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT authorization_id,operation_id,plan_id,plan_hash,principal,approval_origin,testsimulation FROM authorizations WHERE "+scope+" AND plan_id="+quote(plan_id)+")q") + result['audit']=sql("SELECT coalesce(json_agg(row_to_json(q)),'[]') FROM (SELECT event_id,principal,event_type,data,created_at FROM audit_events WHERE "+scope+" AND event_type='plan_authorization' AND data->>'plan_id'="+quote(plan_id)+" ORDER BY created_at)q") + return result + + +def native_evidence(p): + import base64 + import hashlib + import json + import os + from pathlib import Path + import sqlite3 + import subprocess + import uuid + job=p['job'];require(os.geteuid()==0 and str(uuid.UUID(job['job_id']))==job['job_id'] and str(uuid.UUID(job['attempt_id']))==job['attempt_id'],'fixed native evidence binding') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==p['boot_id'],'same native boot') + processes={} + for name,key,uid in (('rmm-full-maintenance-helper.service','helper',0),('rmm-agent-remote.service','agent',993)): + pid=int(subprocess.run(['systemctl','show',name,'-p','MainPID','--value'],capture_output=True,check=True,timeout=10).stdout) + proc=Path('/proc',str(pid));birth=(proc/'stat').read_text().rsplit(')',1)[1].split()[19];status=(proc/'status').read_text().splitlines() + require(next(x.split()[1:] for x in status if x.startswith('Uid:'))==[str(uid)]*4 + and next(x.split()[1:] for x in status if x.startswith('Gid:'))==['984']*4 + and hashlib.sha256((proc/'exe').read_bytes()).hexdigest()==p['artifacts'][key]['sha256'] + and (proc/'stat').read_text().rsplit(')',1)[1].split()[19]==birth,'actual qualified process image/accounts/birth') + processes[name]=dict(pid=pid,start_ticks=birth,sha256=p['artifacts'][key]['sha256']) + path=Path('/var/lib/rmm-helper/consumption.sqlite');m=path.lstat() + require(not path.is_symlink() and [m.st_dev,m.st_ino]==p['database_identity'],'original Root database inode') + import time + original_ids=sorted(p['original_rows']);require(len(original_ids)==18,'exact eighteen original Root IDs') + started=time.monotonic_ns();db=sqlite3.connect(path.as_uri()+'?mode=ro',uri=True,timeout=0.25) + try: + db.set_progress_handler(lambda:int(time.monotonic_ns()-started>1_000_000_000),1000) + db.execute('PRAGMA query_only=ON');db.execute('BEGIN') + require(db.execute('PRAGMA journal_mode').fetchone()[0]=='delete','qualified readonly DELETE journal') + total,unresolved=db.execute('SELECT count(*),coalesce(sum(result IS NULL),0) FROM consumed').fetchone() + require(total in (18,19),'at most one new Root consumption') + placeholders=','.join('?' for _ in original_ids) + rows=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ' + +'WHERE c.grant_id IN ('+placeholders+') OR (c.job=? AND c.attempt=?) ORDER BY c.grant_id LIMIT 20', + [*original_ids,job['job_id'],job['attempt_id']]).fetchall() + finally: + # Materialize only the known eighteen plus this one attempt; close the + # shared read transaction before hashing/decoding any retained BLOBs. + try:db.rollback() + finally:db.close() + elapsed=time.monotonic_ns()-started;m=path.lstat() + require(elapsed<=1_000_000_000,'short native read transaction within accepted one-second budget') + require(not path.is_symlink() and [m.st_dev,m.st_ino]==p['database_identity'],'same Root inode after short read') + def digest(row):return hashlib.sha256(json.dumps([dict(sha256=hashlib.sha256(v).hexdigest(),size=len(v)) if isinstance(v,bytes) else v for v in row],separators=(',',':')).encode()).hexdigest() + original={row[0]:digest(row) for row in rows if row[0] in p['original_rows']} + require(original==p['original_rows'] and len(rows)==total,'all original18 bytes retained, only this possible new consumption') + found=[row for row in rows if row[3]==job['job_id'] and row[4]==job['attempt_id']] + require(len(found)<=1 and total==18+len(found),'no unrelated new consumption') + records=[] + for row in found: + require(row[1]==job['agent_id'] and row[2]==job['enrollment_generation'],'native consumption target/generation') + records.append(dict(grant_id=row[0],result_b64=base64.b64encode(row[5]).decode() if row[5] is not None else None, + start_b64=base64.b64encode(row[6]).decode() if row[6] is not None else None,outcome_b64=base64.b64encode(row[7]).decode() if row[7] is not None else None)) + return dict(read_only=True,original_root18_unchanged=True,consumed=total,unresolved=unresolved,records=records,processes=processes, + read_transaction_ns=elapsed,read_transaction_budget_ns=1_000_000_000,sqlite_busy_timeout_ms=250,sqlite_integrity_scan_performed=False) + + + +def rpc(intent,action,folder,**extra): + guards();require(intent['runner_sha256']==support.sha(__file__),'same original runner before every call/read') + require(support.load(folder/'intent-hash.json')==dict(intent_sha256=support.sha(folder/'intent.json')),'original complete local intent bytes before every target call') + code=inspect.getsource(require)+'\n'+inspect.getsource(timestamp_ns)+'\n'+inspect.getsource(server) + code+='\ntry:\n print(__import__("json").dumps(server('+repr(dict(intent=intent,action=action,**extra))+')))\nexcept Exception as e:\n print(__import__("json").dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n' + return support.transport(old.server_prefix(),'python3 -',code.encode(),folder,timeout=230) + + +def retain_plan(folder,record,intent): + plan=checked_plan(record,intent);path=folder/'stored-plan.json' + if path.exists():require(support.load(path)==plan,'original plan bytes/IDs/hash never replaced') + else:support.save(path,plan) + return plan + + +def read_case(intent,folder): + evidence=rpc(intent,'read',folder);support.save(folder/('server-read-'+str(uuid.uuid4())+'.json'),evidence) + if not evidence['plans']:return dict(state='plan_effect_unknown' if (folder/'plan-entered.json').exists() else 'not_planned',evidence=evidence) + plan=retain_plan(folder,evidence['plans'][0],intent);_,original=guards() + require(intent['runner_sha256']==support.sha(__file__) + and support.load(folder/'intent-hash.json')==dict(intent_sha256=support.sha(folder/'intent.json')),'same original source/intent before native read') + identity=original['preflight']['database_inodes']['/var/lib/rmm-helper/consumption.sqlite'] + native=old.rpc(support.access_settings(old.pre.ACCESS_ROOT,'linux'),native_evidence,dict(job=plan['job'],boot_id=original['preflight']['boot_id'],artifacts=original['artifacts'],database_identity=identity,original_rows=original['preflight']['root_rows']),folder) + support.save(folder/('native-read-'+str(uuid.uuid4())+'.json'),native) + return dict(plan=plan,evidence=evidence,native=native) + + +def pending_checkpoint(intent,folder,value): + evidence=value['evidence'];require(len(evidence['requests'])==1,'one immutable submission lookup') + pending=evidence['requests'][0];require(pending['approval_state']=='pending_approval' and pending['principal']==CREATOR + and pending['idempotency_key']==intent['idempotency_key'] and evidence['counts']==dict(jobs=0,outbox=0,permits=0,authorizations=0) + and evidence['jobs']==evidence['permits']==evidence['authorizations']==evidence['audit']==[] + and value['native']['records']==[] and value['native']['consumed']==18 and value['native']['unresolved']==0,'pending has zero executable artifacts and no native consumption') + plan=value['plan'];require(pending['plan_id']==plan['record']['plan_id'],'same pending plan') + checkpoint=dict(profile='dev/test',case='script-console-linux-full-v1',**intent['target'],expected_principal=APPROVER, + plan=plan['view'],job=plan['view']['jobs'][0],request=intent['input'],operation_id=pending['operation_id'], + plan_hash=plan['record']['plan_hash'],plan_payload_bytes=plan['view']['plan_payload_bytes'],job_payload=plan['job'],job_payload_hash=plan['job_payload_hash'], + preapproval=evidence['counts'],operation=dict(operation_id=pending['operation_id'],plan_id=pending['plan_id'],plan_hash=plan['record']['plan_hash'], + approval_state='pending_approval',planned_job_count=1,job_count=0,execution_state=None,jobs=[]), + operation_view_origin='read_only_scoped_SQL',stage='pending_approval',script_sha256=SCRIPT_SHA,no_retry=True,approval_performed=False) + path=folder/'pending-checkpoint.json' + if path.exists():require(support.load(path)==checkpoint,'original pending checkpoint remains immutable') + else:support.save(path,checkpoint) + return checkpoint + + +def verify_result(intent,value): + plan=value['plan'];evidence=value['evidence'];native=value['native'] + require(evidence['counts']==dict(jobs=1,outbox=1,permits=1,authorizations=1) + and len(evidence['requests'])==len(evidence['jobs'])==len(evidence['permits'])==len(evidence['authorizations'])==len(evidence['audit'])==1,'one job/outbox/grant/authorization only') + request=evidence['requests'][0];job=evidence['jobs'][0];permit=evidence['permits'][0];auth=evidence['authorizations'][0];audit=evidence['audit'][0] + require(request['approval_state']=='approved' and request['principal']==CREATOR and request['idempotency_key']==intent['idempotency_key'] + and auth['principal']==APPROVER and auth['approval_origin']=='dev_test_browser_simulation' and auth['testsimulation'] is True + and auth['plan_hash']==plan['record']['plan_hash'] and auth['operation_id']==request['operation_id']==job['operation_id'] + and auth['plan_id']==request['plan_id']==plan['record']['plan_id'] + and audit['principal']==APPROVER and audit['data']['approval_origin']=='dev_test_browser_simulation' and audit['data']['testsimulation'] is True + and audit['data']['decision']=='approve' and audit['data']['profile']=='development' + and audit['data']['plan_id']==plan['record']['plan_id'] and audit['data']['plan_hash']==plan['record']['plan_hash'] and audit['data']['operation_id']==request['operation_id'],'actual simulated WebUI audit binding, never production human approval') + raw_job=base64.b64decode(plan['job_bytes_b64'],validate=True) + require(job['state']=='succeeded' and job['job_id']==plan['job']['job_id'] and job['attempt_id']==plan['job']['attempt_id'] + and job['agent_id']==plan['job']['agent_id'] and job['asset_id']==plan['job']['asset_id'] + and job['payload_hash']==plan['job_payload_hash'] and base64.b64decode(job['payload_b64'],validate=True)==raw_job,'same successful stored job/attempt bytes') + result=job['result'];require(result['exit_code']==0 and result['output_truncated'] is False and result['reboot_required'] is False + and bytes(result['stdout'])==STDOUT and bytes(result['stderr'])==STDERR and result['evidence']['postconditions_passed'] is True + and result['evidence']['execution_uid']==0 and result['evidence']['privileged_script_profile']=='full_maintenance_v1' + and result['evidence']['script_sha256']==SCRIPT_SHA,'exact native root/profile/stdout/stderr result') + require(native['original_root18_unchanged'] is True and native['consumed']==19 and native['unresolved']==0 and len(native['records'])==1,'one finished new Root consumption') + record=native['records'][0];start=json.loads(base64.b64decode(record['start_b64'],validate=True));outcome=json.loads(base64.b64decode(record['outcome_b64'],validate=True));finished=json.loads(base64.b64decode(record['result_b64'],validate=True));request=start['request'] + permit_raw=base64.b64decode(permit['payload_b64'],validate=True);grant=json.loads(permit_raw) + require(record['grant_id']==permit['grant_id']==grant['grant_id']==result['grant_id'] + and bytes(request['job_bytes'])==raw_job and bytes(request['permit_bytes'])==permit_raw + and bytes(request['signature'])==base64.b64decode(permit['signature_b64'],validate=True) + and request['plan_hash']==grant['plan_hash']==plan['record']['plan_hash'] and request['authorization_id']==grant['authorization_id']==auth['authorization_id'] + and grant['job_payload_hash']==plan['job_payload_hash'] and all(grant[k]==plan['job'][k] for k in (*intent['target'].keys(),'job_id','attempt_id','execution_identity','max_runtime_seconds')) + and grant['domain']=='rmm.execution-permit' and grant['schema_version']==1 + and grant['action']=='run_script' and grant['key_id']==intent['signing_key_id'] + and timestamp_ns(plan['job']['not_before'])<=timestamp_ns(grant['not_before']) + <=timestamp_ns(start['reserved_at']) 0 for key in ('enrollment_generation', 'assignment_version')), + 'exact qualified cloud target/generations') + for sample in samples: + native = sample['vm']; stored = sample['server']; current = stored['installation'] + require(native['case_id'] == INSTALL_CASE and native['full_pin'] == pin + and native['verified_state_preservation'] is True and native['root_unresolved'] == 0 + and native['read_only'] is True and native['product_state_writes'] is False + and native['rmm_jobs'] is False and native['approvals'] is False + and native['processes']['full']['sha256'] == pin['helper_binary_sha256'] + and native['processes']['full']['uids'] == ['0'] * 4 + and stored['central_root_scope_closed'] is True and current['sync_ready'] is True + and current['helper_sync_ready'] is True and current['revoked_at'] is None, + 'accepted actual native root process and closed stored server state') + needed = ('rmm.sync.v1', 'helper.root.v1', 'root:run_script', 'run_script', 'script.full_maintenance/1', + 'helper.policy.sha256:' + pin['policy_sha256'], 'helper.binary.sha256:' + pin['helper_binary_sha256']) + require(all(cap in current['capabilities'] for cap in needed), 'accepted actual Full capability publication') + require(all(intent['install']['target'][key] == target[key] for key in ('tenant_id', 'asset_id', 'agent_id')), + 'accepted installation targets agree') + return {'target': target, 'full_policy_sha256': pin['policy_sha256'], + 'helper_binary_sha256': pin['helper_binary_sha256'], 'boot_id': samples[-1]['vm']['boot_id'], + 'connection_generation': samples[-1]['vm']['generation'], 'attestation_sha256': expected, + 'attestation_runner_sha256': attested['runner_sha256'], 'sample_sha256': attested['sample_sha256']} + + +def remote(p): + """Fixed server-only operations. Policy data is public; no private key/token.""" + import datetime as dt + import fcntl + import http.client + import socket + import ssl + import stat + import subprocess + os.umask(0o077) + require(os.geteuid() == 0 and p['action'] in ('open', 'read', 'close', 'provision-mcp-root'), 'fixed server bootstrap operation') + intent = p['intent']; ident = str(uuid.UUID(intent['id'])) + require(ident == intent['id'] and intent['type'] in ('rmm.script-console.policy-scope.v1', + 'rmm.script-console.mcp-root-provision.v1'), 'canonical own policy intent') + provision = intent['type'] == 'rmm.script-console.mcp-root-provision.v1' + require(p['action'] in ('provision-mcp-root', 'read') if provision else p['action'] in ('open', 'read', 'close'), + 'separate provision and scope operations; no automatic side effects') + root = Path('/opt/rust-discovery'); directory = root / 'devstate/root-policy'; policy_path = directory / 'policy.json' + case = root / 'devstate' / (('script-console-mcp-root-provision-' if provision else 'script-console-policy-scope-') + ident) + compose = ['docker', 'compose', '--env-file', str(root / 'devstate/.env'), '-f', str(root / 'deploy/server.compose.yaml')] + old_hash = 'cf15cbb68b590475df6def28e7dcffb7d88e0ca2d2555a06174fb654eeff05f4' + def enc(value): return json.dumps(value, sort_keys=True, separators=(',', ':')).encode() + def hash_bytes(raw): return hashlib.sha256(raw).hexdigest() + def now(): return dt.datetime.now(dt.timezone.utc) + def stamp(value): + parsed = dt.datetime.fromisoformat(value.replace('Z', '+00:00')) + require(parsed.utcoffset() is not None, 'explicit UTC/timezone bound') + return parsed + def run(args, timeout=15, **kwargs): + result = subprocess.run(args, capture_output=True, timeout=timeout, **kwargs) + require(result.returncode == 0, 'fixed server command incomplete; private details withheld') + return result.stdout + def syncdir(path): + fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + try: os.fsync(fd) + finally: os.close(fd) + def once(path, raw, uid=0, gid=0, mode=0o600, times=None): + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode) + with os.fdopen(fd, 'wb') as out: + out.write(raw); out.flush(); os.fchown(out.fileno(), uid, gid); os.fchmod(out.fileno(), mode) + if times is not None: os.utime(out.fileno(), ns=times) + os.fsync(out.fileno()) + syncdir(path.parent) + def owned(path, directory=False): + value = path.lstat() + require(not path.is_symlink() and value.st_uid == 0 and stat.S_IMODE(value.st_mode) == (0o700 if directory else 0o600) + and (stat.S_ISDIR(value.st_mode) if directory else stat.S_ISREG(value.st_mode) and value.st_nlink == 1), + 'own protected policy history') + return value + def load(path): + value = owned(path); require(value.st_size <= 8 * 1024 * 1024, 'bounded history receipt') + return json.loads(path.read_bytes()) + def read_policy(): + parent = directory.lstat() + require(not directory.is_symlink() and stat.S_ISDIR(parent.st_mode) and parent.st_uid == 10001 + and not parent.st_mode & 0o022, 'preserved protected policy parent') + fd = os.open(policy_path, os.O_RDONLY | os.O_NOFOLLOW) + try: + value = os.fstat(fd) + require(stat.S_ISREG(value.st_mode) and value.st_uid == 10001 and stat.S_IMODE(value.st_mode) == 0o600 + and value.st_nlink == 1 and 0 < value.st_size <= 65536, 'protected bounded current policy') + raw = os.read(fd, 65537); after = os.fstat(fd); named = policy_path.lstat() + require(len(raw) == value.st_size and (value.st_dev, value.st_ino, value.st_size, value.st_mtime_ns, value.st_ctime_ns) + == (after.st_dev, after.st_ino, after.st_size, after.st_mtime_ns, after.st_ctime_ns) + and (named.st_dev, named.st_ino) == (value.st_dev, value.st_ino), 'same complete named policy through read') + metadata = {'device': value.st_dev, 'inode': value.st_ino, 'uid': value.st_uid, 'gid': value.st_gid, + 'mode': stat.S_IMODE(value.st_mode), 'bytes': value.st_size, 'atime_ns': value.st_atime_ns, + 'mtime_ns': value.st_mtime_ns, 'ctime_ns': value.st_ctime_ns} + return raw, metadata + finally: os.close(fd) + def summary(raw, metadata): + out = {'sha256': hash_bytes(raw), 'metadata': metadata} + try: + policy = json.loads(raw) + out.update(schema_version=policy['schema_version'], profile=policy['profile'], expires_at=policy['expires_at'], + scope_count=len(policy['scopes']), expired=stamp(policy['expires_at']) <= now()) + except Exception as error: out['parse_error_class'] = type(error).__name__ + return out + def candidate(): + raw = base64.b64decode(intent['candidate_bytes_b64'], validate=True) + require(len(raw) <= 65536 and hash_bytes(raw) == intent['candidate_sha256'], 'immutable exact candidate bytes') + body = json.loads(raw); target = intent['qualification']['target'] + require(set(body) == {'schema_version', 'profile', 'expires_at', 'scopes'} and body['schema_version'] == 2 + and type(body['schema_version']) is int and body['profile'] == 'development' + and body['expires_at'] == intent['expires_at'] and len(body['scopes']) == 1, 'one schema2 development scope') + expected = dict(target, principals=intent['principals'], helper_policy_sha256=intent['qualification']['full_policy_sha256'], + helper_binary_sha256=intent['qualification']['helper_binary_sha256'], + acceptance_evidence_id='script-console-full-linux-attested:' + intent['qualification']['attestation_sha256'], + allowed_services=[], allow_root_scripts=True, allowed_script_profiles=['full_maintenance_v1'], allow_reboot=False) + require(body['scopes'] == [expected] and 'apt' not in expected and 'root_logs' not in expected, + 'exact target/principals/Full pins; no service/APT/log/reboot scope') + require(stamp(intent['expires_at']) - stamp(intent['created_at']) == dt.timedelta(seconds=900), 'fixed fifteen-minute intent') + return raw + def server_gate(): + target = intent['qualification']['target']; tenant = str(uuid.UUID(target['tenant_id'])); agent = str(uuid.UUID(target['agent_id'])) + require(tenant == '11111111-1111-4111-8111-111111111111' and tenant == target['tenant_id'] and agent == target['agent_id'], 'fixed target UUIDs') + scope = "tenant_id='" + tenant + "' AND agent_id='" + agent + "'" + # Only fixed public names, roles, permissions, capability/status fields. + principals = intent['principals']; automation = 'automation:' + tenant + require(principals == [automation, 'simulated-testprincipal-a'], 'exact existing MCP creator and simulated UI approver IDs') + selected = ','.join("'" + name + "'" for name in principals) + query = ("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY; SELECT json_build_object('observed_at',now()," + "'installation',(SELECT json_build_object('tenant_id',tenant_id,'asset_id',asset_id,'agent_id',agent_id," + "'enrollment_generation',enrollment_generation,'assignment_version',assignment_version,'capabilities',capabilities," + "'connection_generation',connection_generation,'connection_boot_id',connection_boot_id,'last_contact_at',last_contact_at," + "'certificate_sha256',certificate_sha256,'last_authenticated_certificate_sha256',last_authenticated_certificate_sha256," + "'revoked_at',revoked_at,'sync_ready',sync_ready,'helper_sync_ready',helper_sync_ready,'restore_blocked',restore_blocked," + "'sequence_regressed',sequence_regressed,'sync_recovery_required',sync_recovery_required) FROM agent_installations WHERE " + scope + ")," + "'memberships',(SELECT COALESCE(json_agg(q ORDER BY principal),'[]'::json) FROM (SELECT principal,kind,permissions,revoked_at FROM memberships WHERE tenant_id='" + tenant + "' AND principal IN (" + selected + ")) q)," + "'jobs',(SELECT count(*) FROM jobs WHERE " + scope + ")," + "'outbox',(SELECT count(*) FROM outbox o JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE " + scope.replace('tenant_id=', 'j.tenant_id=').replace('agent_id=', 'j.agent_id=') + ")," + "'permits',(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE " + scope.replace('tenant_id=', 'j.tenant_id=').replace('agent_id=', 'j.agent_id=') + ")," + "'unresolved',(SELECT count(*) FROM jobs WHERE tenant_id='" + tenant + "' AND state NOT IN ('succeeded','failed','cancelled','expired'))," + "'migrations',(SELECT json_agg(version ORDER BY version) FROM _sqlx_migrations WHERE success)); COMMIT;") + data = json.loads(run(compose + ['exec', '-T', 'postgres', 'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', + '-U', 'postgres', '-d', 'rmm'], input=query.encode())) + installed = data['installation'] + require(installed is not None and all(installed[key] == value for key, value in target.items()), 'same stored enrolled target/generations') + require(data['migrations'] == list(range(1, 15)), 'actual additive migration14 server prerequisite') + require([data[key] for key in ('jobs', 'outbox', 'permits')] == [30, 30, 26], 'same target-scoped closed30/30/26 before scope open') + require(data['unresolved'] == 0 and installed['revoked_at'] is None and installed['sync_ready'] is True + and installed['helper_sync_ready'] is True and all(installed[key] is False for key in + ('restore_blocked', 'sequence_regressed', 'sync_recovery_required')) + and installed['connection_boot_id'] == intent['qualification']['boot_id'] + and installed['connection_generation'] >= intent['qualification']['connection_generation'] + and installed['certificate_sha256'] == installed['last_authenticated_certificate_sha256'] + and 0 <= (now() - stamp(installed['last_contact_at'])).total_seconds() <= 45, 'fresh authenticated ready stored Cloud client') + needed = ('rmm.sync.v1', 'helper.root.v1', 'root:run_script', 'run_script', 'script.full_maintenance/1', + 'helper.policy.sha256:' + intent['qualification']['full_policy_sha256'], + 'helper.binary.sha256:' + intent['qualification']['helper_binary_sha256']) + require(all(value in installed['capabilities'] for value in needed), 'fresh stored Full policy/code capability binding') + rows = {row['principal']: row for row in data['memberships']} + require(set(rows) == set(principals), 'all explicitly bound existing principals') + for name in principals: + row = rows[name]; permissions = {'root:execute', 'scripts:execute', 'operations:plan', 'operations:submit', 'operations:read'} + permissions.add('plans:approve' if name == 'simulated-testprincipal-a' else 'operations:plan') + require(row['revoked_at'] is None and row['kind'] == ('test_human' if name == 'simulated-testprincipal-a' else 'automation') + and permissions <= set(row['permissions']), 'existing principal lacks required scope; never mutate memberships') + if name == automation: + exact = ['inventory:read', 'logs:read', 'operations:read', 'operations:plan', + 'operations:submit', 'jobs:cancel', 'scripts:execute', 'root:execute'] + require(row['permissions'] == exact, 'exact provisioned automation rights; no approval/revoke/SYSTEM authority') + return data + def roles_and_auth(): + out = {} + names = ('RMM_PROFILE', 'RMM_DEV_AUTH', 'RMM_SERVER_ROLE', 'RMM_UI_ORIGIN', 'RMM_DEV_ROOT_ENABLED', + 'RMM_DEV_ROOT_POLICY_FILE', 'RMM_DEV_SYSTEM_ENABLED', 'RMM_DEV_SYSTEM_POLICY_FILE', + 'RMM_DEV_DELIVERY_FAULTS', 'RMM_DEV_DELIVERY_FAULTS_FILE') + select = ' '.join('(eq $key "' + name + '")' for name in names) + template = '{{range .Config.Env}}{{$key := index (split . "=") 0}}{{if or ' + select + '}}{{println .}}{{end}}{{end}}' + for role in ('api', 'gateway'): + cid = run(compose + ['ps', '-q', role]).decode().strip(); require(cid and '\n' not in cid, 'one actual role') + image = run(['docker', 'inspect', cid, '--format', '{{.Image}}']).decode().strip() + require(image == intent['expected_server_image'], 'exact Root-reviewed deployed server image') + env = {} + for line in run(['docker', 'inspect', cid, '--format', template]).decode().splitlines(): + if not line: + continue + key, separator, value = line.partition('=') + require(separator == '=' and key in names and key not in env, 'strict unique selected public environment field') + env[key] = value + require(env.get('RMM_PROFILE') == 'development' and env.get('RMM_DEV_AUTH') == 'true' + and env.get('RMM_SERVER_ROLE') == role and env.get('RMM_UI_ORIGIN') == 'https://localhost:18443' + and env.get('RMM_DEV_ROOT_ENABLED') == 'true' and env.get('RMM_DEV_ROOT_POLICY_FILE') == '/root-policy/policy.json' + and env.get('RMM_DEV_SYSTEM_ENABLED') is None and env.get('RMM_DEV_SYSTEM_POLICY_FILE') is None + and env.get('RMM_DEV_DELIVERY_FAULTS') == 'false' and env.get('RMM_DEV_DELIVERY_FAULTS_FILE') is None, + 'unchanged existing dev/test roles/flags; no auth or server flag changes') + mounts = json.loads(run(['docker', 'inspect', cid, '--format', '{{json .Mounts}}'])) + policy_mounts = [mount for mount in mounts if mount['Destination'] == '/root-policy'] + require(len(policy_mounts) == 1 and policy_mounts[0]['Type'] == 'bind' and policy_mounts[0]['RW'] is False + and policy_mounts[0]['Source'] == str(directory), 'actual unchanged read-only Root policy mount') + out[role] = {'container_id': cid, 'image_id': image, 'settings': env, 'policy_mount': policy_mounts[0]} + class HTTPS(http.client.HTTPSConnection): + def connect(self): + self.sock = self._context.wrap_socket(socket.create_connection(('127.0.0.1', 18443), timeout=5), server_hostname=self.host) + client = HTTPS('localhost', 18443, timeout=5, context=ssl.create_default_context(cafile=str(root / 'devstate/api-pki/ca.pem'))) + try: + client.request('GET', '/auth/config'); response = client.getresponse(); raw = response.read(4097) + require(response.status == 200 and len(raw) <= 4096 and json.loads(raw) == + {'oidc_configured': False, 'oidc_login_url': None, 'dev_login_enabled': True}, 'honest private dev/test auth config') + finally: client.close() + return out + def provision_state(): + tenant = '11111111-1111-4111-8111-111111111111'; actor = 'automation:' + tenant + query = ("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY; SELECT json_build_object('actor'," + "(SELECT row_to_json(q) FROM (SELECT tenant_id,principal,kind,permissions,revoked_at FROM memberships WHERE tenant_id='" + tenant + "' AND principal='" + actor + "') q)," + "'jobs',(SELECT count(*) FROM jobs WHERE tenant_id='" + tenant + "')," + "'outbox',(SELECT count(*) FROM outbox WHERE tenant_id='" + tenant + "')," + "'permits',(SELECT count(*) FROM execution_permits WHERE tenant_id='" + tenant + "')," + "'unresolved',(SELECT count(*) FROM jobs WHERE tenant_id='" + tenant + "' AND state NOT IN ('succeeded','failed','cancelled','expired'))," + "'other_memberships_sha256',(SELECT encode(sha256(convert_to(coalesce(string_agg(to_jsonb(m)::text,E'\\n' ORDER BY tenant_id,principal),''),'UTF8')),'hex') FROM memberships m WHERE NOT(tenant_id='" + tenant + "' AND principal='" + actor + "'))); COMMIT;") + return json.loads(run(compose + ['exec', '-T', 'postgres', 'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', + '-U', 'postgres', '-d', 'rmm'], input=query.encode())) + if provision: + before_rights = ['inventory:read', 'logs:read', 'operations:read', 'operations:plan', + 'operations:submit', 'jobs:cancel', 'scripts:execute'] + actor = 'automation:11111111-1111-4111-8111-111111111111' + tenant = '11111111-1111-4111-8111-111111111111' + expected_actor = {'tenant_id': tenant, 'principal': actor, 'kind': 'automation', + 'permissions': before_rights, 'revoked_at': None} + require(intent['actor_before'] == expected_actor and intent['actor_after'] == dict(expected_actor, + permissions=before_rights + ['root:execute']) and intent['old_policy_sha256'] == old_hash + and hash_bytes(enc(intent)) == p['intent_sha256'], 'exact one existing actor/rights transition') + if p['action'] == 'read': + out = {'id': ident, 'read_only': True, 'case_present': case.exists(), 'retry_allowed': False} + for key, fn in (('database', provision_state), ('roles', roles_and_auth)): + try: out[key] = fn() + except Exception as error: out[key] = {'read_error_class': type(error).__name__} + try: + current, metadata = read_policy(); out['current_policy'] = summary(current, metadata) + except Exception as error: out['policy_read_error_class'] = type(error).__name__ + if case.exists(): + owned(case, True) + for name in ('intent.json', 'before.json', 'provision-entered.json', 'sql-result.json', 'after.json', 'provision-result.json'): + if (case / name).exists(): + try: out[name] = load(case / name) + except Exception as error: out[name] = {'read_error_class': type(error).__name__} + if (case / 'intent.json').exists(): require(load(case / 'intent.json') == intent, 'same remote provision intent') + return out + lock = os.open(root / 'devstate/.script-console-policy-scope.lock', os.O_WRONLY | os.O_CREAT | os.O_NOFOLLOW, 0o600) + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + try: + require(not case.exists() and not case.is_symlink() and stamp(intent['created_at']) <= now() + < stamp(intent['provision_before']) <= now() + dt.timedelta(seconds=900), 'new short-lived explicit provision case') + roles = roles_and_auth(); current, metadata = read_policy(); body = json.loads(current) + require(hash_bytes(current) == old_hash and body['schema_version'] == 1 and body['profile'] == 'development' + and stamp(body['expires_at']) <= now(), 'same exact expired closed Root policy before provisioning') + before = provision_state() + require(before['actor'] == expected_actor and {key: before[key] for key in ('jobs', 'outbox', 'permits', 'unresolved')} + == {'jobs': 30, 'outbox': 30, 'permits': 26, 'unresolved': 0}, 'exact current seven-right actor and closed30/30/26') + owned(root / 'devstate', True); case.mkdir(mode=0o700); syncdir(case.parent) + once(case / 'intent.json', enc(intent)); once(case / 'before.json', enc({'database': before, + 'roles': roles, 'policy_sha256': old_hash, 'policy_metadata': metadata})) + once(case / 'provision-entered.json', enc({'id': ident, 'intent_sha256': p['intent_sha256'], + 'adds_only': 'root:execute', 'no_retry': True})) + latest, _ = read_policy(); require(latest == current, 'closed policy CAS immediately before SQL') + # Exact existing row, tenant context, closed-job predicate and a + # RETURNING count assertion in the transaction. ON_ERROR_STOP means + # count0 aborts before COMMIT; connection close rolls back. + rights_sql = 'ARRAY[' + ','.join("'" + value + "'" for value in before_rights) + ']::text[]' + query = ("BEGIN ISOLATION LEVEL SERIALIZABLE; SET LOCAL app.tenant_id='" + tenant + "'; " + "WITH changed AS (UPDATE memberships SET permissions=array_append(permissions,'root:execute') " + "WHERE tenant_id='" + tenant + "' AND principal='" + actor + "' AND kind='automation' AND revoked_at IS NULL " + "AND permissions=" + rights_sql + " AND (SELECT count(*) FROM jobs WHERE tenant_id='" + tenant + "')=30 " + "AND (SELECT count(*) FROM outbox WHERE tenant_id='" + tenant + "')=30 " + "AND (SELECT count(*) FROM execution_permits WHERE tenant_id='" + tenant + "')=26 " + "AND NOT EXISTS(SELECT 1 FROM jobs WHERE tenant_id='" + tenant + "' AND state NOT IN ('succeeded','failed','cancelled','expired')) " + "RETURNING tenant_id,principal,kind,permissions,revoked_at) " + "SELECT json_build_object('changed_count',count(*),'assert_one',1/count(*),'after',(json_agg(changed))->0) FROM changed; COMMIT;") + result = json.loads(run(compose + ['exec', '-T', 'postgres', 'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', + '-U', 'postgres', '-d', 'rmm'], input=query.encode())) + require(result == {'changed_count': 1, 'assert_one': 1, 'after': intent['actor_after']}, 'exact committed public actor row') + once(case / 'sql-result.json', enc(result)) + after = provision_state(); once(case / 'after.json', enc(after)) + require(after == dict(before, actor=intent['actor_after']), 'only this one actor permission changed; all other memberships and counts retained') + latest, latest_metadata = read_policy() + require(latest == current and latest_metadata['inode'] == metadata['inode'] and roles_and_auth() == roles, + 'closed policy and server flags/roles unchanged after provisioning') + final = {'id': ident, 'provisioned': True, 'actor': actor, 'added_permission': 'root:execute', + 'other_memberships_unchanged': True, 'policy_remains_closed': True, 'jobs_created': False, + 'approvals_created': False, 'tokens_or_sessions_created': False, 'automatic_scope_open': False} + once(case / 'provision-result.json', enc(final)); return final + finally: os.close(lock) + raw_candidate = candidate() + require(intent['old_policy_sha256'] == old_hash and hash_bytes(enc(intent)) == p['intent_sha256'], 'own complete immutable policy intent') + if p['action'] == 'read': + out = {'id': ident, 'read_only': True, 'case_present': case.exists(), 'retry_allowed': False} + try: + raw, metadata = read_policy(); out['current_policy'] = summary(raw, metadata) + out['matches_original_closed'] = hash_bytes(raw) == old_hash + out['matches_own_candidate'] = hash_bytes(raw) == intent['candidate_sha256'] + except Exception as error: out['policy_read_error_class'] = type(error).__name__ + if case.exists(): + owned(case, True) + for name in ('intent.json', 'original-metadata.json', 'open-entered.json', 'open-result.json', 'close-entered.json', 'close-result.json'): + if (case / name).exists(): + try: out[name] = load(case / name) + except Exception as error: out[name] = {'read_error_class': type(error).__name__} + if (case / 'intent.json').exists(): require(load(case / 'intent.json') == intent, 'same remote original intent') + return out + # Policy writers using this source serialize. Other root maintenance must be + # coordinated; unexpected current bytes/inode always fail the final CAS. + lock = os.open(root / 'devstate/.script-console-policy-scope.lock', os.O_WRONLY | os.O_CREAT | os.O_NOFOLLOW, 0o600) + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + try: + if p['action'] == 'open': + require(not case.exists() and not case.is_symlink(), 'existing policy case; read only') + require(stamp(intent['created_at']) <= now() < stamp(intent['expires_at']) <= now() + dt.timedelta(seconds=900), 'fresh fixed bounded candidate expiry') + gate = server_gate(); roles = roles_and_auth(); previous, metadata = read_policy() + body = json.loads(previous) + require(hash_bytes(previous) == old_hash and set(body) == {'schema_version', 'profile', 'expires_at', 'scopes'} + and type(body['schema_version']) is int and body['schema_version'] == 1 and body['profile'] == 'development' + and isinstance(body['scopes'], list) and stamp(body['expires_at']) <= now(), 'exact historical closed schema1 policy CAS') + parent = root / 'devstate'; owned(parent, True) + case.mkdir(mode=0o700); syncdir(parent) + once(case / 'intent.json', enc(intent)); once(case / 'original-policy.json', previous) + once(case / 'original-metadata.json', enc(metadata)); once(case / 'before-server.json', enc({'gate': gate, 'roles': roles})) + # Durable one-use marker BEFORE any policy exposure or rename. + once(case / 'open-entered.json', enc({'id': ident, 'intent_sha256': p['intent_sha256'], 'candidate_sha256': intent['candidate_sha256']})) + next_path = directory / ('script-full-' + ident + '.open-next') + once(next_path, raw_candidate, uid=10001, gid=10001) + current, current_metadata = read_policy() + require(hash_bytes(current) == old_hash and current_metadata['device'] == metadata['device'] + and current_metadata['inode'] == metadata['inode'] and current_metadata['mtime_ns'] == metadata['mtime_ns'], 'last exact closed policy/inode CAS') + require(now() < stamp(intent['expires_at']), 'candidate still unexpired immediately before exposure') + os.replace(next_path, policy_path); syncdir(directory) + current, current_metadata = read_policy(); require(current == raw_candidate, 'actual exact new policy bytes') + result = {'id': ident, 'opened': True, 'policy_sha256': hash_bytes(current), 'expires_at': intent['expires_at'], + 'metadata': current_metadata, 'server_flags_unchanged': roles_and_auth() == roles, + 'jobs_created': False, 'approvals_created': False, 'database_mutations': False} + require(result['server_flags_unchanged'], 'same roles and flags after bounded policy replacement') + once(case / 'open-result.json', enc(result)); return result + owned(case, True); require(load(case / 'intent.json') == intent, 'close only original own remote case') + require((case / 'open-entered.json').exists() and not (case / 'close-entered.json').exists(), 'one close after own open; read only on uncertainty') + owned(case / 'original-policy.json'); previous = (case / 'original-policy.json').read_bytes() + metadata = load(case / 'original-metadata.json') + require(hash_bytes(previous) == old_hash and metadata['uid'] == 10001 and metadata['mode'] == 0o600, 'original private closed backup and metadata') + current, current_metadata = read_policy(); require(current == raw_candidate, 'foreign or changed policy; never overwrite') + once(case / 'close-entered.json', enc({'id': ident, 'expected_current_sha256': intent['candidate_sha256'], 'restore_sha256': old_hash})) + next_path = directory / ('script-full-' + ident + '.close-next') + once(next_path, previous, uid=metadata['uid'], gid=metadata['gid'], mode=metadata['mode'], times=(metadata['atime_ns'], metadata['mtime_ns'])) + last, last_metadata = read_policy() + require(last == raw_candidate and last_metadata['device'] == current_metadata['device'] + and last_metadata['inode'] == current_metadata['inode'], 'last exact own policy/inode CAS') + os.replace(next_path, policy_path); syncdir(directory) + restored, restored_metadata = read_policy() + require(restored == previous and all(restored_metadata[key] == metadata[key] for key in ('uid', 'gid', 'mode', 'bytes', 'mtime_ns')) + and stamp(json.loads(restored)['expires_at']) <= now(), 'exact old bytes/owner/mode/mtime restored and closed') + result = {'id': ident, 'closed': True, 'restored_sha256': hash_bytes(restored), 'metadata': restored_metadata, + 'historical_original_inode_retained_in_receipt': True, 'restored_inode_is_atomic_replacement': True, + 'jobs_created': False, 'approvals_created': False, 'database_mutations': False} + once(case / 'close-result.json', enc(result)); return result + finally: os.close(lock) + + +def prepare(args): + qualification = accepted_native(args.attestation.resolve(), args.accepted_attestation_sha) + require(re.fullmatch('sha256:[a-f0-9]{64}', args.expected_server_image) is not None, 'explicit reviewed deployed image ID') + principals = ['automation:' + TENANT, SIMULATED] + created = dt.datetime.now(dt.timezone.utc).replace(microsecond=0); expiry = created + dt.timedelta(minutes=15) + scope = dict(qualification['target'], principals=principals, helper_policy_sha256=qualification['full_policy_sha256'], + helper_binary_sha256=qualification['helper_binary_sha256'], + acceptance_evidence_id='script-console-full-linux-attested:' + qualification['attestation_sha256'], + allowed_services=[], allow_root_scripts=True, allowed_script_profiles=['full_maintenance_v1'], allow_reboot=False) + candidate = encode({'schema_version': 2, 'profile': 'development', 'expires_at': expiry.isoformat(), 'scopes': [scope]}) + ident = str(uuid.uuid4()); STATE.mkdir(mode=0o700, parents=True, exist_ok=True) + private_directory(STATE); folder = STATE / ident; folder.mkdir(mode=0o700) + intent = {'type': 'rmm.script-console.policy-scope.v1', 'id': ident, 'runner_sha256': sha(Path(__file__).read_bytes()), + 'remote_source_sha256': sha(inspect.getsource(remote).encode()), 'support_sha256': SUPPORT, + 'qualification': qualification, 'attestation_path': str(args.attestation.resolve()), 'principals': principals, + 'expected_server_image': args.expected_server_image, 'old_policy_sha256': OLD_POLICY_SHA, + 'created_at': created.isoformat(), 'expires_at': expiry.isoformat(), 'candidate_sha256': sha(candidate), + 'candidate_bytes_b64': base64.b64encode(candidate).decode(), 'no_jobs_or_approvals': True} + create(folder / 'intent.json', encode(intent)); create(folder / 'candidate-policy.json', candidate) + return {'id': ident, 'intent': str(folder / 'intent.json'), 'intent_sha256': sha(encode(intent)), + 'candidate_sha256': sha(candidate), 'expires_at': intent['expires_at'], 'target_called': False} + + +def prepare_actor(args): + require(re.fullmatch('sha256:[a-f0-9]{64}', args.expected_server_image) is not None, 'explicit reviewed deployed image ID') + actor = {'tenant_id': TENANT, 'principal': 'automation:' + TENANT, 'kind': 'automation', + 'permissions': AUTOMATION_RIGHTS, 'revoked_at': None} + created = dt.datetime.now(dt.timezone.utc).replace(microsecond=0) + ident = str(uuid.uuid4()); STATE.mkdir(mode=0o700, parents=True, exist_ok=True) + private_directory(STATE); folder = STATE / ident; folder.mkdir(mode=0o700) + intent = {'type': 'rmm.script-console.mcp-root-provision.v1', 'id': ident, + 'runner_sha256': sha(Path(__file__).read_bytes()), 'remote_source_sha256': sha(inspect.getsource(remote).encode()), + 'support_sha256': SUPPORT, 'expected_server_image': args.expected_server_image, + 'old_policy_sha256': OLD_POLICY_SHA, 'actor_before': actor, + 'actor_after': dict(actor, permissions=AUTOMATION_RIGHTS + ['root:execute']), + 'created_at': created.isoformat(), 'provision_before': (created + dt.timedelta(minutes=15)).isoformat(), + 'authorization_basis': 'Explicit user AI/MCP Root maintenance objective; separately reviewed development actor provisioning', + 'no_jobs_or_approvals': True, 'no_scope_open': True} + create(folder / 'intent.json', encode(intent)) + return {'id': ident, 'intent': str(folder / 'intent.json'), 'intent_sha256': sha(encode(intent)), + 'adds_only': 'root:execute', 'automatic_scope_open': False, 'target_called': False} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('phase', choices=('prepare-local', 'open', 'read', 'close', 'prepare-mcp-root-local', 'provision-mcp-root')) + parser.add_argument('--id'); parser.add_argument('--attestation', type=Path) + parser.add_argument('--accepted-attestation-sha'); parser.add_argument('--expected-server-image') + parser.add_argument('--access-root', type=Path, default=Path('/home/kevin/Schreibtisch/Coding/rust-discovery')) + parser.add_argument('--execute-policy-open', action='store_true'); parser.add_argument('--execute-policy-close', action='store_true') + parser.add_argument('--execute-mcp-root-provision', action='store_true') + args = parser.parse_args(); os.umask(0o077) + require(all(sha((ROOT / name).read_bytes()) == value for name, value in SUPPORT.items()), 'exact existing strict SSH support') + if args.phase == 'prepare-local': + require(args.id is None and args.attestation is not None and args.accepted_attestation_sha is not None + and args.expected_server_image is not None, 'explicit Root accepted native proof and deployed image') + print(json.dumps(prepare(args))); return + if args.phase == 'prepare-mcp-root-local': + require(args.id is None and args.expected_server_image is not None, 'new explicitly reviewed actor provision intent') + print(json.dumps(prepare_actor(args))); return + ident = str(uuid.UUID(args.id)); require(ident == args.id, 'canonical original case UUID') + private_directory(STATE); folder = STATE / ident; private_directory(folder) + intent = json.loads(protected(folder / 'intent.json')) + require(intent['id'] == ident and intent['runner_sha256'] == sha(Path(__file__).read_bytes()) + and intent['remote_source_sha256'] == sha(inspect.getsource(remote).encode()) and intent['support_sha256'] == SUPPORT, + 'original same-source immutable policy case') + provision = intent['type'] == 'rmm.script-console.mcp-root-provision.v1' + require(args.phase in ('provision-mcp-root', 'read') if provision else args.phase in ('open', 'read', 'close'), 'same original intent operation kind') + if args.phase in ('open', 'close', 'provision-mcp-root'): + allowed = {'open': args.execute_policy_open, 'close': args.execute_policy_close, + 'provision-mcp-root': args.execute_mcp_root_provision}[args.phase] + require(allowed, 'explicit separate bounded bootstrap operation') + if args.phase == 'open': + require(accepted_native(Path(intent['attestation_path']), intent['qualification']['attestation_sha256']) == intent['qualification'], 'accepted evidence unchanged before open') + create(folder / (args.phase + '-requested.json'), encode({'id': ident, 'intent_sha256': sha(encode(intent)), 'retry_allowed': False})) + import remote_client_bootstrap as bootstrap + bootstrap.ROOT = args.access_root.resolve(); bootstrap.access.ROOT = bootstrap.ROOT + settings = bootstrap.access.load_config(bootstrap.ROOT / 'credentials/server.conf') + require(settings['address'] in ('88.99.80.202', 'discovery.ease-technologies.de') and settings['username'] == 'root' + and settings.get('port', '22') == '22', 'fixed independently trusted cloud server') + result = bootstrap.rpc('server.conf', remote, {'action': args.phase, 'intent': intent, 'intent_sha256': sha(encode(intent))}) + create(folder / (args.phase + '-reply-' + str(uuid.uuid4()) + '.json'), encode(result)) + print(json.dumps(result)) + + +if __name__ == '__main__': + try: main() + except Exception as error: + print('SCRIPT_POLICY_SCOPE_INCOMPLETE:' + type(error).__name__ + '; preserve case, read only after any requested mutation') + raise SystemExit(2) from None diff --git a/deploy/script_console_post_attestation.py b/deploy/script_console_post_attestation.py new file mode 100644 index 0000000..ea579f4 --- /dev/null +++ b/deploy/script_console_post_attestation.py @@ -0,0 +1,443 @@ +#!/usr/bin/env python3 +"""Read-only post-start evidence; Root executes after a separate source review. + +No target writes, service commands, helper IPC, enrollment, jobs or approval. +Three separate observe invocations are required: baseline and two subsequent +stored heartbeat advances. Missing evidence fails closed; never retry an install. +""" +import argparse +import base64 +import hashlib +import inspect +import json +import os +from pathlib import Path +import uuid + +ROOT = Path(__file__).resolve().parents[1] +PREFLIGHT_SHA = '0e210b857d3f25e0ff228bbbd4a6533dadd91c9d21dbcba5923b26c2fe72eec9' +if hashlib.sha256((ROOT/'deploy/script_console_bootstrap.py').read_bytes()).hexdigest() != PREFLIGHT_SHA: + raise RuntimeError('Reviewed preflight source changed') +import script_console_bootstrap as pre +support = pre.support +CASE = '2e27c312-58cc-444d-92a6-863a11ac6ffd' +STATE = ROOT/'devstate/script-console-post-attestation' +DROPIN = b'[Service]\nEnvironment=RMM_FULL_MAINTENANCE_HELPER_SOCKET=/run/rmm-helper-maintenance/control.sock\nEnvironment=RMM_FULL_MAINTENANCE_POLICY_FILE=/etc/rmm-remote/full-maintenance-helper-proof.json\n' + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def vm(payload): + """Only fixed file/SQLite reads, process metadata and public-key verification.""" + import contextlib + import datetime as dt + import hashlib + import json + import os + from pathlib import Path + import sqlite3 + import stat + import subprocess + import base64 + import time + read_transactions={} + require(os.geteuid()==0 and os.uname().machine=='x86_64' and payload['case_id']=='2e27c312-58cc-444d-92a6-863a11ac6ffd', 'fixed root read-only case') + intent=payload['install']; before=Path('/var/lib/rmm-bootstrap-history/script-console-v1-'+payload['case_id']) + state=Path('/var/lib/rmm-agent-remote'); helper=Path('/var/lib/rmm-helper') + def sha(raw):return hashlib.sha256(raw).hexdigest() + def encode(value):return json.dumps(value,sort_keys=True,separators=(',',':')).encode() + def protected(path,limit=8*1024*1024): + m=path.lstat();require(stat.S_ISREG(m.st_mode) and not path.is_symlink() and m.st_uid==0 + and not m.st_mode&0o022 and m.st_nlink==1 and m.st_size<=limit,'protected bounded public/control evidence') + return path.read_bytes() + require(protected(before/'intent.json')==encode(intent),'original immutable installation intent') + old_sql=json.loads(protected(before/'sql-before.json',64*1024*1024)) + old_files=json.loads(protected(before/'files-before.json')) + original_report=protected(before/'report.json') + historical=json.loads(original_report) + require(historical['preserved_before_restart'] is True and historical['full_pin']['helper_binary_sha256']==intent['artifacts']['helper']['sha256'], 'original preserved file installation') + continuation=json.loads(protected(before/'continuation-start-intent.json')) + require(continuation['original_case']==payload['case_id'],'same original first-start case') + first_start=dt.datetime.fromisoformat(continuation['first_start_not_before'].replace('Z','+00:00')) + require(first_start.tzinfo is not None,'explicit durable first-start timestamp') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==intent['preflight']['boot_id'],'same boot') + observed_from=dt.datetime.now(dt.timezone.utc).isoformat() + processes={} + for name,key,uid in (('rmm-agent-remote.service','agent',993),('rmm-helper-cloud.service','general',0),('rmm-full-maintenance-helper.service','full',0)): + raw=subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','MainPID'],capture_output=True,check=True,timeout=15).stdout + unit=dict(line.split('=',1) for line in raw.decode().splitlines());pid=int(unit['MainPID']) + require(unit['ActiveState']=='active' and pid>1,'all three actual services active') + proc=Path('/proc',str(pid));ticks=(proc/'stat').read_text().rsplit(') ',1)[1].split()[19] + status=(proc/'status').read_text().splitlines() + uids=next(x.split()[1:] for x in status if x.startswith('Uid:'));gids=next(x.split()[1:] for x in status if x.startswith('Gid:')) + image=sha((proc/'exe').read_bytes());expected=intent['artifacts']['agent' if key=='agent' else 'helper']['sha256'] + require(uids==[str(uid)]*4 and gids==['984']*4 and image==expected + and (proc/'stat').read_text().rsplit(') ',1)[1].split()[19]==ticks,'actual same process birth/account/image') + processes[key]=dict(pid=pid,start_ticks=ticks,uids=uids,gids=gids,sha256=image) + for name in ('rmm-apt-helper.service','rmm-agent.service','rmm-helper-dev-test.service'): + raw=subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','UnitFileState'],capture_output=True,check=True,timeout=15).stdout + unit=dict(line.split('=',1) for line in raw.decode().splitlines()) + require(unit['ActiveState']=='inactive' and unit['UnitFileState']=='disabled','APT/legacy remain closed') + for key,path in (('agent',Path('/usr/local/libexec/rmm-agent-remote')),('helper',Path('/usr/local/libexec/rmm-privileged-helper'))): + raw=protected(path,256*1024*1024) + require(sha(raw)==intent['artifacts'][key]['sha256'] and len(raw)==intent['artifacts'][key]['bytes'] + and stat.S_IMODE(path.lstat().st_mode)==0o755,'qualified installed executable bytes/mode') + prior=before/(key+'-previous');m=prior.lstat();old=intent['baseline']['controls'][str(path)] + require(dict(device=m.st_dev,inode=m.st_ino,mode=stat.S_IMODE(m.st_mode),uid=m.st_uid,gid=m.st_gid,bytes=m.st_size,sha256=sha(protected(prior,256*1024*1024)))==old,'original binary backup identities/code preserved') + for path,old in intent['baseline']['controls'].items(): + if path.startswith('/usr/local/libexec/'):continue + path=Path(path);m=path.lstat();actual=dict(device=m.st_dev,inode=m.st_ino,mode=stat.S_IMODE(m.st_mode),uid=m.st_uid,gid=m.st_gid,bytes=m.st_size,sha256=sha(protected(path))) + require(actual==old,'unchanged General/APT/package control bytes and identities') + full=base64.b64decode(intent['baseline']['full_config_candidate']['bytes_b64'],validate=True) + config=Path('/etc/rmm-remote/helper-full-maintenance.json');pinpath=Path('/etc/rmm-remote/full-maintenance-helper-proof.json') + require(protected(config)==full and stat.S_IMODE(config.lstat().st_mode)==0o600 and config.lstat().st_gid==0,'exact Full configuration') + pin=json.loads(protected(pinpath));require(pin==historical['full_pin'] and stat.S_IMODE(pinpath.lstat().st_mode)==0o640 + and pinpath.lstat().st_gid==984,'exact protected Full policy/image/identity pin') + ordered_pin={k:pin[k] for k in ('schema_version','profile','identity','policy_sha256','helper_binary_sha256')} + ordered_pin['identity']={k:pin['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','signing_key_id')} + require(protected(pinpath)==json.dumps(ordered_pin,separators=(',',':')).encode(),'exact original inspector pin bytes') + for path,raw in ((Path('/etc/systemd/system/rmm-full-maintenance-helper.service'),base64.b64decode(intent['unit_bytes_b64'],validate=True)), + (Path('/etc/systemd/system/rmm-agent-remote.service.d/60-full-maintenance.conf'),base64.b64decode(payload['dropin_b64'],validate=True))): + require(protected(path)==raw and stat.S_IMODE(path.lstat().st_mode)==0o644 and path.lstat().st_gid==0,'exact Full unit and additive agent dropin bytes/modes') + # Do not open a private key. Database bytes legitimately change under Sync; + # every preexisting non-DB file retains its complete metadata witness. + current_paths=set() + for root in (state,helper): + for path in [root,*sorted(root.rglob('*'))]: + if str(path).endswith(('-wal','-shm','-journal')):raise RuntimeError('Settled DELETE-mode snapshot required') + m=path.lstat();require(not path.is_symlink() and (stat.S_ISDIR(m.st_mode) or stat.S_ISREG(m.st_mode)),'retained state object type') + current_paths.add(str(path));old=old_files.get(str(path)) + require(old is not None,'no unexplained new state path') + value=[m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_size,m.st_mtime_ns,m.st_ctime_ns] + require(value[:5]==old[:5] if str(path) in old_sql or stat.S_ISDIR(m.st_mode) else value==old,'state/PKI/key metadata and inodes preserved') + require(current_paths==set(old_files),'all old state paths retained') + @contextlib.contextmanager + def database(name): + path=Path(name);m=path.lstat();require(not path.is_symlink() and stat.S_ISREG(m.st_mode) and m.st_nlink==1 + and [m.st_dev,m.st_ino]==old_sql[name]['identity'],'same actual SQLite inode') + require(all(not os.path.lexists(name+s) for s in ('-wal','-shm','-journal')),'no SQLite sidecars') + # Never hold a shared DELETE-mode read lock while hashing rows or + # running OpenSSL. A cooperative one-second SQLite progress budget + # limits reads; this is not a hard real-time OS/I/O guarantee. + started=time.monotonic() + with contextlib.closing(sqlite3.connect(path.as_uri()+'?mode=ro',uri=True,timeout=0.25)) as db: + db.set_progress_handler(lambda:int(time.monotonic()-started>1.0),1000) + try: + db.execute('PRAGMA query_only=ON');db.execute('BEGIN') + require(db.execute('PRAGMA journal_mode').fetchone()[0]=='delete','qualified DELETE journal') + yield db + finally: + db.rollback() + elapsed=int((time.monotonic()-started)*1000) + timing=read_transactions.setdefault(name,dict(count=0,total_ms=0,max_ms=0)) + timing['count']+=1;timing['total_ms']+=elapsed;timing['max_ms']=max(timing['max_ms'],elapsed) + require([path.stat().st_dev,path.stat().st_ino]==old_sql[name]['identity'],'SQLite inode unchanged through read') + def row_hash(row):return sha(encode([dict(blob_sha256=sha(v),bytes=len(v)) if isinstance(v,bytes) else v for v in row])) + journal=str(state/'journal.sqlite');ledger=str(helper/'consumption.sqlite');public=json.loads((state/'binding.json').read_bytes()) + require(all(public[k]==intent['general_pin']['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')),'retained enrollment/binding') + key=bytes.fromhex(public['public_key_hex']);require(len(key)==32 and sha(key)==pin['identity']['signing_key_id'],'same public signing key') + verified={} + def signed(decision_id): + if decision_id in verified:return verified[decision_id] + with database(ledger) as db:row=db.execute('SELECT rowid,* FROM helper_sync_decisions WHERE decision_id=?',[decision_id]).fetchone() + require(row is not None,'retained helper-applied signed decision') + response=json.loads(row[3]);raw=bytes(response['decision_bytes']);signature=bytes(response['signature']);require(0=128:out.append((v&127)|128);v>>=7 + out.append(v);return bytes(out) + wire=b'\x0a'+varint(len(raw))+raw+b'\x12\x40'+signature + with database(journal) as db:req=db.execute('SELECT request,generation,response FROM reconcile_requests WHERE id=?',[d['request_id']]).fetchone() + require(req is not None and req[0]==row[4] and sha(req[0])==d['request_hash'] and req[1]==d['connection_generation'] and req[2]==wire,'same agent/helper-applied request and signed response bytes') + original=row_hash(row) in old_sql[ledger]['rows']['helper_sync_decisions'] + verified[decision_id]=dict(decision=d,payload_sha256=sha(raw),signature_sha256=sha(signature),original=original) + return verified[decision_id] + def original_rows(name,table,count): + # There is deliberately no encompassing transaction. Each page closes + # before yield, so hashing/crypto cannot block a DELETE-mode writer. + require(table.replace('_','').isalnum(),'fixed SQLite identifier') + last=None;remaining=count + while remaining: + size=min(16,remaining) + with database(name) as db: + if last is None:page=db.execute('SELECT rowid,* FROM "'+table+'" ORDER BY rowid LIMIT ?',[size]).fetchall() + else:page=db.execute('SELECT rowid,* FROM "'+table+'" WHERE rowid>? ORDER BY rowid LIMIT ?',[last,size]).fetchall() + require(len(page)==size,'all original SQL prefix rows retained') + for row in page: + require(last is None or row[0]>last,'monotonic original SQLite rowid traversal') + last=row[0];remaining-=1;yield row + old_candidates=[] + # Only original IDs are materialized, never all historical request/response + # BLOBs or new append-only Sync rows. Their full hashes are checked below. + total_original=len(old_sql[ledger]['rows']['helper_sync_decisions']);last=None + while len(old_candidates)? ORDER BY rowid LIMIT ?',[last,size]).fetchall() + require(len(page)==size,'all original signed decision IDs retained') + old_candidates.extend(row[1] for row in page);last=page[-1][0] + old_candidates.reverse() + reference_changes=[];summary={} + for name,previous in old_sql.items(): + with database(name) as db: + schema=dict(db.execute("SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name"));require(schema==previous['schema'],'exact unchanged SQL schema') + counts={} + for table,old_hashes in previous['rows'].items(): + require(table.replace('_','').isalnum(),'fixed SQLite identifier') + with database(name) as db:count=db.execute('SELECT count(*) FROM "'+table+'"').fetchone()[0] + counts[table]=count + require(count>=len(old_hashes),'all original SQL rows retained') + if table in ('job_attempts','consumed','execution_evidence','reconcile_outcomes'): + require(count==len(old_hashes),'no new job/effect/history rows during bootstrap') + if table in ('reconcile_meta','helper_sync_meta'):continue + if table=='counter': + with database(name) as db:current=db.execute('SELECT rowid,* FROM counter ORDER BY rowid LIMIT 2').fetchall() + require(len(current)==len(old_hashes)==1 and any(row_hash((1,1,n))==old_hashes[0] for n in range(max(0,current[0][2]-4096),current[0][2]+1)),'monotonic counter reconstructed against exact old hash within4096 bound');continue + if table in ('pending','job_events'): + require(not old_hashes,'no ambiguous preexisting queued messages');continue + for index,row in enumerate(original_rows(name,table,len(old_hashes))): + old_hash=old_hashes[index] + if row_hash(row)==old_hash:continue + if table=='reconcile_requests': + require(row[-1] is not None and row_hash((*row[:-1],None))==old_hash,'only original NULL response may fill') + with database(ledger) as helper_db:candidates=list(helper_db.execute('SELECT decision_id FROM helper_sync_decisions WHERE request=?',[row[2]])) + require(len(candidates)==1,'filled response has independent helper-applied original request') + signed(candidates[0][0]) + continue + require(table=='reconcile_outcomes' and name==journal,'immutable original SQL row changed') + # Keep original rowid/job/attempt/outcome bytes; only its + # reference to an independently applied signed decision renews. + original_id=next((d for d in old_candidates if row_hash((*row[:-1],d))==old_hash),None) + require(original_id is not None,'exact old complete outcome-row witness') + old=signed(original_id);new=signed(row[-1]);require(old['original'],'original decision existed before restart') + selected=[] + for proof in (old,new): + found=[a for a in proof['decision']['attempts'] if a['job_id']==row[1] and a['attempt_id']==row[2]] + require(len(found)==1 and found[0]['outcome_sha256']==sha(row[3]) and (not found[0]['outcome_bytes'] or bytes(found[0]['outcome_bytes'])==row[3]),'original signed outcome bytes');selected.append(found[0]) + require(all(selected[0][k]==selected[1][k] for k in ('job_payload_hash','grant_id','authorization_id','plan_hash','action','execution_identity','outcome_state','outcome_sha256','original_boot_id')),'same exact authorized outcome') + a=old['decision'];b=new['decision'];require(a['journal_id']==b['journal_id'] and a['helper_journal_id']==b['helper_journal_id'] and b['checkpoint']>=a['checkpoint'] and b['helper_checkpoint']>=a['helper_checkpoint'] and b['connection_generation']>=a['connection_generation'],'monotonic signed reference renewal') + reference_changes.append(dict(rowid=row[0],original_sha256=old_hash,old_decision_id=original_id,new_decision_id=row[-1])) + summary[name]=dict(identity=previous['identity'],schema_preserved=True,initial_row_counts=counts, + original_row_counts={t:len(v) for t,v in previous['rows'].items()}) + # Recheck fixed effect/job counts after the whole paged witness. New passive + # captures may append throughout; a cross-table snapshot is not claimed. + for name,previous in old_sql.items(): + for table in ('job_attempts','consumed','execution_evidence','reconcile_outcomes'): + if table in previous['rows']: + with database(name) as db:count=db.execute('SELECT count(*) FROM "'+table+'"').fetchone()[0] + require(count==len(previous['rows'][table]),'final exact no-new-effect/job row counts') + with database(ledger) as db: + roots=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY grant_id LIMIT 19').fetchall() + names=[r[1] for r in db.execute('PRAGMA table_info(helper_sync_meta)')];helper_meta=dict(zip(names,db.execute('SELECT * FROM helper_sync_meta WHERE id=1').fetchone())) + exact={r[0]:sha(json.dumps([dict(sha256=sha(v),size=len(v)) if isinstance(v,bytes) else v for v in r],separators=(',',':')).encode()) for r in roots} + require(exact==intent['preflight']['root_rows'] and len(exact)==18 and all(r[5] is not None for r in roots),'final exact original18Root request/grant/signature/start/outcome rows') + with database(journal) as db: + require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'no unfinished agent job') + names=[r[1] for r in db.execute('PRAGMA table_info(reconcile_meta)')];agent_meta=dict(zip(names,db.execute('SELECT * FROM reconcile_meta WHERE id=1').fetchone())) + inventory=payload.get('inventory') + inventory_queued=bool(db.execute('SELECT EXISTS(SELECT 1 FROM job_events WHERE id=?)',[inventory['message_id']]).fetchone()[0]) if inventory is not None else None + require(agent_meta['fault'] is None and helper_meta['fault'] is None and agent_meta['last_decision']==helper_meta['last_decision'] + and agent_meta['generation']==helper_meta['generation'] and agent_meta['journal_id']==helper_meta['agent_journal_id'] + and agent_meta['helper_id']==helper_meta['journal_id'],'fault-free shared current applied decision and journal lineage') + ordered_binding={k:public[k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','public_key_hex')} + require(helper_meta['binding_sha256']==sha(json.dumps(ordered_binding,separators=(',',':')).encode()),'helper ledger independently bound to exact same enrollment') + proof=signed(agent_meta['last_decision']);decision=proof['decision'];now=dt.datetime.now(dt.timezone.utc) + issued=dt.datetime.fromisoformat(decision['issued_at'].replace('Z','+00:00'));until=dt.datetime.fromisoformat(decision['valid_until'].replace('Z','+00:00')) + require(issued<=now=original_basis[k] for k in ('checkpoint','helper_checkpoint','connection_generation')),'original signed journal lineage retained with monotonic checkpoint/generation') + # Specific server-persisted inventory must have left the durable local queue; + # this is code-bound queue convergence, not a captured network ACK frame. + inventory=payload.get('inventory') + inventory_ack=None + if inventory is not None: + require(inventory_queued is False and inventory['boot_id']==decision['boot_id'],'specific stored inventory no longer queued') + inventory_ack=dict(message_id=inventory['message_id'],server_payload_sha256=inventory['payload_sha256'],local_queue_absent=True,wire_ack_captured=False) + require(protected(before/'report.json')==original_report,'original interrupted report retained unchanged') + return dict(case_id=payload['case_id'],observed_from=observed_from,observed_until=now.isoformat(),processes=processes, + boot_id=decision['boot_id'],generation=decision['connection_generation'],first_start_not_before=first_start.isoformat(),root_consumed=18,root_unresolved=0, + verified_state_preservation=True,snapshot_consistency='per_page_then_final_same_decision_and_fixed_effect_counts',sqlite_integrity_scan_performed=False,database_witness=summary,verified_outcome_reference_changes=reference_changes, + agent_meta=agent_meta,helper_meta=helper_meta,signed_sync={k:proof[k] for k in ('payload_sha256','signature_sha256')}, + decision={k:decision[k] for k in ('decision_id','request_id','request_hash','connection_generation','boot_id','journal_id','helper_journal_id','checkpoint','helper_checkpoint','issued_at','valid_until','ready','helper_ready')}, + full_pin=pin,inventory_queue_convergence=inventory_ack,read_transactions=read_transactions,read_transaction_budget_seconds=1.0,read_only=True,product_state_writes=False,rmm_jobs=False,approvals=False) + + +def server(payload): + """Stored server state and RLS denial only; no HTTP/live collection or writes.""" + import datetime as dt + import hashlib + import json + from pathlib import Path + import subprocess + import uuid + tenant=str(uuid.UUID(payload['install']['target']['tenant_id'])) + agent=str(uuid.UUID(payload['install']['target']['agent_id'])) + require(tenant=='11111111-1111-4111-8111-111111111111','fixed development tenant') + foreign='22222222-2222-4222-8222-222222222222' + scope="tenant_id='"+tenant+"' AND agent_id='"+agent+"'" + query=("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY; SELECT json_build_object('observed_at',now()," + "'installation',(SELECT json_build_object('connection_generation',connection_generation,'connection_boot_id',connection_boot_id," + "'last_contact_at',last_contact_at,'last_sequence',last_sequence,'last_authenticated_at',last_authenticated_at," + "'certificate_sha256',certificate_sha256,'last_authenticated_certificate_sha256',last_authenticated_certificate_sha256," + "'revoked_at',revoked_at,'capabilities',capabilities,'sync_ready',sync_ready,'helper_sync_ready',helper_sync_ready," + "'restore_blocked',restore_blocked,'sequence_regressed',sequence_regressed,'sync_recovery_required',sync_recovery_required) FROM agent_installations WHERE "+scope+")," + "'head',(SELECT row_to_json(s) FROM agent_sync_state s WHERE "+scope+")," + "'decisions',(SELECT COALESCE(json_agg(x),'[]'::json) FROM (SELECT decision_id,request_id,request_hash,connection_generation," + "encode(payload,'hex') AS payload_hex,encode(signature,'hex') AS signature_hex FROM sync_checkpoints WHERE "+scope+" ORDER BY created_at DESC,decision_id DESC LIMIT 16) x)," + "'inventories',(SELECT COALESCE(json_agg(x),'[]'::json) FROM (SELECT message_id,boot_id,sequence,observed_at,received_at,source_id,payload_sha256 FROM observations WHERE "+scope+" ORDER BY received_at DESC LIMIT 16) x)," + "'jobs',(SELECT COALESCE(json_agg(x),'[]'::json) FROM (SELECT job_id,attempt_id,state,encode(sha256(convert_to(to_jsonb(j)::text,'UTF8')),'hex') AS row_sha256 FROM jobs j WHERE "+scope+" ORDER BY job_id,attempt_id LIMIT 31) x)," + "'outbox_count',(SELECT count(*) FROM outbox o JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE "+scope.replace('tenant_id=','j.tenant_id=').replace('agent_id=','j.agent_id=')+")," + "'permit_count',(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE "+scope.replace('tenant_id=','j.tenant_id=').replace('agent_id=','j.agent_id=')+")); COMMIT;") + root=Path('/opt/rust-discovery') + cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm'] + result=json.loads(subprocess.run(cmd,input=query.encode(),capture_output=True,check=True,timeout=25).stdout) + require(len(result['jobs'])==30 and result['outbox_count']==30 and result['permit_count']==26 + and all(j['state'] in ('succeeded','failed','cancelled','expired') for j in result['jobs']),'same closed30/30/26 central job counts') + policy=root/'devstate/root-policy/policy.json';m=policy.lstat();raw=policy.read_bytes() + require(not policy.is_symlink() and m.st_uid==10001 and m.st_mode&0o777==0o600 + and hashlib.sha256(raw).hexdigest()==payload['install']['central_policy_sha256'] + and dt.datetime.fromisoformat(json.loads(raw)['expires_at'])<=dt.datetime.now(dt.timezone.utc),'central Root scope remains protected and closed') + for row in result['decisions']: + raw=bytes.fromhex(row.pop('payload_hex'));signature=bytes.fromhex(row.pop('signature_hex'));d=json.loads(raw) + require(len(raw)<=512*1024 and len(signature)==64,'bounded stored signed decision') + row['payload_sha256']=hashlib.sha256(raw).hexdigest();row['signature_sha256']=hashlib.sha256(signature).hexdigest() + row['decision']={k:d[k] for k in ('decision_id','request_id','request_hash','connection_generation','boot_id','journal_id','helper_journal_id','checkpoint','helper_checkpoint','issued_at','valid_until','ready','helper_ready')} + denial=("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY; SET LOCAL ROLE rmm_app; SET LOCAL app.tenant_id='"+foreign+"'; " + "SELECT json_build_object('installations',(SELECT count(*) FROM agent_installations WHERE agent_id='"+agent+"')," + "'observations',(SELECT count(*) FROM observations WHERE agent_id='"+agent+"')," + "'jobs',(SELECT count(*) FROM jobs WHERE agent_id='"+agent+"')); COMMIT;") + denied=json.loads(subprocess.run(cmd,input=denial.encode(),capture_output=True,check=True,timeout=25).stdout) + require(denied==dict(installations=0,observations=0,jobs=0),'actual foreign-tenant application-role stored-data denial') + result.update(tenant_denial=denied,tenant_denial_scope='postgres_rmm_app_rls_not_http',central_root_scope_closed=True,read_only=True,rmm_jobs=False,approvals=False) + return result + + +def rpc(prefix,function,payload,folder): + code=inspect.getsource(require)+'\n'+inspect.getsource(function) + code+='\ntry:\n print(__import__("json").dumps('+function.__name__+'('+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=240) + + +def server_prefix(): + cfg=support.access.load_config(pre.ACCESS_ROOT/'credentials/server.conf') + require(cfg['address']=='88.99.80.202' and cfg['username']=='root' and cfg.get('port','22')=='22' and cfg.get('ssh_key_path'),'fixed existing server bootstrap transport') + return ['ssh','-F','/dev/null','-T','-p','22','-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','-i',str(Path(cfg['ssh_key_path']).expanduser()),'root@88.99.80.202'] + + +def parse_stored_time(value): + """PostgreSQL RFC3339 at microsecond precision; pad, never round/truncate.""" + import datetime as dt + import re + require(isinstance(value,str),'stored UTC timestamp string') + matched=re.fullmatch(r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,6}))?(Z|[+-]\d{2}:\d{2})',value) + require(matched is not None,'exact bounded PostgreSQL RFC3339 timestamp') + fraction=('.'+matched[2].ljust(6,'0')) if matched[2] else '' + zone='+00:00' if matched[3]=='Z' else matched[3] + return dt.datetime.fromisoformat(matched[1]+fraction+zone) + + +def compare_samples(samples): + import datetime as dt + require(len(samples)==3,'exact baseline and two subsequent passive samples') + baseline=samples[0];last=baseline + for sample in samples: + current=sample['vm'];stored=sample['server'];installation=stored['installation'];d=current['decision'] + require(current['read_only'] is True and current['verified_state_preservation'] is True + and current['product_state_writes'] is False and current['rmm_jobs'] is False and current['approvals'] is False,'every native VM preservation proof') + require(stored['read_only'] is True and stored['rmm_jobs'] is False and stored['approvals'] is False + and stored['central_root_scope_closed'] is True and stored['tenant_denial']==dict(installations=0,observations=0,jobs=0) + and stored['tenant_denial_scope']=='postgres_rmm_app_rls_not_http' + and len(stored['jobs'])==30 and stored['outbox_count']==30 and stored['permit_count']==26,'actual stored closed server/tenant-denial probes') + require(installation['connection_generation']==current['generation']==baseline['vm']['generation'] + and installation['connection_boot_id']==current['boot_id']==baseline['vm']['boot_id'] + and current['processes']==baseline['vm']['processes'],'stable actual new process pair/Full helper and connection generation') + require(installation['sync_ready'] is True and installation['helper_sync_ready'] is True + and all(installation[k] is False for k in ('restore_blocked','sequence_regressed','sync_recovery_required')) + and installation['revoked_at'] is None and installation['certificate_sha256']==installation['last_authenticated_certificate_sha256'],'stored authenticated current ready channel') + found=[r for r in stored['decisions'] if r['decision_id']==d['decision_id']] + require(len(found)==1 and found[0]['decision']==d and all(found[0][k]==current['signed_sync'][k] for k in ('payload_sha256','signature_sha256')),'same original signed decision retained at server/agent/helper') + caps=installation['capabilities'];pin=current['full_pin'] + require(all(c in caps for c in ('rmm.sync.v1','script.full_maintenance/1','root:run_script', + 'helper.policy.sha256:'+pin['policy_sha256'],'helper.binary.sha256:'+pin['helper_binary_sha256'])),'normal fresh agent publication of authenticated Full peer/policy/image capability') + require(stored['jobs']==baseline['server']['jobs'],'all existing server job state/row hashes unchanged') + inventory=current['inventory_queue_convergence'];require(inventory is not None,'specific persisted inventory and local queue convergence') + inventories=[i for i in stored['inventories'] if i['message_id']==inventory['message_id']] + require(len(inventories)==1 and inventories[0]['payload_sha256']==inventory['server_payload_sha256'] + and inventories[0]['boot_id']==current['boot_id'] and inventories[0]['source_id']=='linux.base' + and parse_stored_time(inventories[0]['received_at'])>=parse_stored_time(current['first_start_not_before']),'concrete fresh passive stored inventory identity') + contact=parse_stored_time(installation['last_contact_at']) + require(contact>=parse_stored_time(current['first_start_not_before']) + and 0<=(parse_stored_time(stored['observed_at'])-contact).total_seconds()<=45,'fresh stored heartbeat after first start') + if sample is not baseline: + require(contact>parse_stored_time(last['server']['installation']['last_contact_at']) + and installation['last_sequence']>last['server']['installation']['last_sequence'],'two distinct subsequent stored heartbeat advances') + last=sample + return dict(verified_preservation=True,verified_normal_signed_sync=True,stored_heartbeat_advances=2, + specific_inventory_queue_convergence=True,wire_inventory_ack_captured=False,tenant_rls_denial=True, + tenant_http_denial_tested=False,read_only=True,rmm_jobs=False,approvals=False,full_job_accepted=False) + + +def main(): + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('mode',choices=('prepare-local','observe','finish')) + parser.add_argument('--id') + args=parser.parse_args() + os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);support.private(STATE,True) + manifest_path=pre.native.PACK/'manifest.json';manifest=support.load(manifest_path) + require(support.sha(manifest_path)==pre.native.MANIFEST_SHA and support.sha(pre.NATIVE)==pre.NATIVE_SHA + and all(support.sha(ROOT/name)==digest for name,digest in manifest['source_files'].items()),'unchanged frozen158 inputs and original Linux-first native proof') + if args.mode=='prepare-local': + require(args.id is None,'new local evidence intent only') + install_path=ROOT/'devstate/script-console-install'/CASE/'intent.json';install=support.load(install_path) + require(install['id']==CASE and install['preflight_source_sha256']==PREFLIGHT_SHA and install['manifest_sha256']==pre.native.MANIFEST_SHA,'exact reviewed installation provenance') + folder=STATE/str(uuid.uuid4());folder.mkdir(mode=0o700) + support.save(folder/'intent.json',dict(type='rmm.script-console.read-only-post-attestation-v1',case_id=CASE, + runner_sha256=support.sha(__file__),install_intent_sha256=support.sha(install_path),install=install, + dropin_b64=base64.b64encode(DROPIN).decode(),read_only_target_calls=True)) + else: + require(args.id is not None and str(uuid.UUID(args.id))==args.id,'canonical original local evidence case') + folder=STATE/args.id;intent=support.load(folder/'intent.json') + require(intent['runner_sha256']==support.sha(__file__) and intent['case_id']==CASE,'original same-source observation intent') + require(support.sha(ROOT/'devstate/script-console-install'/CASE/'intent.json')==intent['install_intent_sha256'],'original local installation intent retained') + if args.mode=='observe': + number=len(list(folder.glob('sample-*.json')));require(number<3,'three distinct passive samples only') + sample_id=str(uuid.uuid4());support.save(folder/('read-intent-'+sample_id+'.json'),dict(read_only=True)) + linux=support.access_settings(pre.ACCESS_ROOT,'linux') + initial=rpc(linux,vm,intent,folder) + stored=rpc(server_prefix(),server,intent,folder) + import datetime as dt + fresh=[i for i in stored['inventories'] if i['boot_id']==initial['boot_id'] and i['source_id']=='linux.base' + and parse_stored_time(i['received_at'])>=parse_stored_time(initial['first_start_not_before'])] + require(fresh,'no fresh startup inventory stored yet; read-only evidence incomplete') + current=rpc(linux,vm,dict(intent,inventory=fresh[0]),folder) + stored=rpc(server_prefix(),server,intent,folder) + support.save(folder/('sample-'+str(number)+'.json'),dict(vm=current,server=stored)) + else: + samples=[support.load(folder/('sample-'+str(i)+'.json')) for i in range(3)] + result=compare_samples(samples);support.save(folder/'attested.json',dict(result,case_id=CASE, + runner_sha256=support.sha(__file__),sample_sha256=[support.sha(folder/('sample-'+str(i)+'.json')) for i in range(3)])) + print(json.dumps(dict(id=folder.name,intent=str(folder/'intent.json'),mode=args.mode,target_called=args.mode=='observe',read_only=True,rmm_jobs=False,approvals=False))) + + +if __name__=='__main__': + main() diff --git a/deploy/script_console_post_attestation_v2.py b/deploy/script_console_post_attestation_v2.py new file mode 100644 index 0000000..4828843 --- /dev/null +++ b/deploy/script_console_post_attestation_v2.py @@ -0,0 +1,481 @@ +#!/usr/bin/env python3 +"""Read-only post-start evidence; Root executes after a separate source review. + +No target writes, service commands, helper IPC, enrollment, jobs or approval. +Three separate observe invocations are required: baseline and two subsequent +stored heartbeat advances. Missing evidence fails closed; never retry an install. +""" +import argparse +import ast +import base64 +import hashlib +import inspect +import json +import os +from pathlib import Path +import uuid + +ROOT = Path(__file__).resolve().parents[1] +PREFLIGHT_SHA = '0e210b857d3f25e0ff228bbbd4a6533dadd91c9d21dbcba5923b26c2fe72eec9' +if hashlib.sha256((ROOT/'deploy/script_console_bootstrap.py').read_bytes()).hexdigest() != PREFLIGHT_SHA: + raise RuntimeError('Reviewed preflight source changed') +import script_console_bootstrap as pre +support = pre.support +CASE = '2e27c312-58cc-444d-92a6-863a11ac6ffd' +STATE = ROOT/'devstate/script-console-post-attestation' +DROPIN = b'[Service]\nEnvironment=RMM_FULL_MAINTENANCE_HELPER_SOCKET=/run/rmm-helper-maintenance/control.sock\nEnvironment=RMM_FULL_MAINTENANCE_POLICY_FILE=/etc/rmm-remote/full-maintenance-helper-proof.json\n' + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def vm(payload): + """Only fixed file/SQLite reads, process metadata and public-key verification.""" + import contextlib + import datetime as dt + import hashlib + import json + import os + from pathlib import Path + import sqlite3 + import stat + import subprocess + import base64 + import time + read_transactions={} + require(os.geteuid()==0 and os.uname().machine=='x86_64' and payload['case_id']=='2e27c312-58cc-444d-92a6-863a11ac6ffd', 'fixed root read-only case') + intent=payload['install']; before=Path('/var/lib/rmm-bootstrap-history/script-console-v1-'+payload['case_id']) + state=Path('/var/lib/rmm-agent-remote'); helper=Path('/var/lib/rmm-helper') + def sha(raw):return hashlib.sha256(raw).hexdigest() + def encode(value):return json.dumps(value,sort_keys=True,separators=(',',':')).encode() + def protected(path,limit=8*1024*1024): + m=path.lstat();require(stat.S_ISREG(m.st_mode) and not path.is_symlink() and m.st_uid==0 + and not m.st_mode&0o022 and m.st_nlink==1 and m.st_size<=limit,'protected bounded public/control evidence') + return path.read_bytes() + require(protected(before/'intent.json')==encode(intent),'original immutable installation intent') + old_sql=json.loads(protected(before/'sql-before.json',64*1024*1024)) + old_files=json.loads(protected(before/'files-before.json')) + original_report=protected(before/'report.json') + historical=json.loads(original_report) + require(historical['preserved_before_restart'] is True and historical['full_pin']['helper_binary_sha256']==intent['artifacts']['helper']['sha256'], 'original preserved file installation') + continuation=json.loads(protected(before/'continuation-start-intent.json')) + require(continuation['original_case']==payload['case_id'],'same original first-start case') + first_start=dt.datetime.fromisoformat(continuation['first_start_not_before'].replace('Z','+00:00')) + require(first_start.tzinfo is not None,'explicit durable first-start timestamp') + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==intent['preflight']['boot_id'],'same boot') + observed_from=dt.datetime.now(dt.timezone.utc).isoformat() + processes={} + for name,key,uid in (('rmm-agent-remote.service','agent',993),('rmm-helper-cloud.service','general',0),('rmm-full-maintenance-helper.service','full',0)): + raw=subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','MainPID'],capture_output=True,check=True,timeout=15).stdout + unit=dict(line.split('=',1) for line in raw.decode().splitlines());pid=int(unit['MainPID']) + require(unit['ActiveState']=='active' and pid>1,'all three actual services active') + proc=Path('/proc',str(pid));ticks=(proc/'stat').read_text().rsplit(') ',1)[1].split()[19] + status=(proc/'status').read_text().splitlines() + uids=next(x.split()[1:] for x in status if x.startswith('Uid:'));gids=next(x.split()[1:] for x in status if x.startswith('Gid:')) + image=sha((proc/'exe').read_bytes());expected=intent['artifacts']['agent' if key=='agent' else 'helper']['sha256'] + require(uids==[str(uid)]*4 and gids==['984']*4 and image==expected + and (proc/'stat').read_text().rsplit(') ',1)[1].split()[19]==ticks,'actual same process birth/account/image') + processes[key]=dict(pid=pid,start_ticks=ticks,uids=uids,gids=gids,sha256=image) + for name in ('rmm-apt-helper.service','rmm-agent.service','rmm-helper-dev-test.service'): + raw=subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','UnitFileState'],capture_output=True,check=True,timeout=15).stdout + unit=dict(line.split('=',1) for line in raw.decode().splitlines()) + require(unit['ActiveState']=='inactive' and unit['UnitFileState']=='disabled','APT/legacy remain closed') + for key,path in (('agent',Path('/usr/local/libexec/rmm-agent-remote')),('helper',Path('/usr/local/libexec/rmm-privileged-helper'))): + raw=protected(path,256*1024*1024) + require(sha(raw)==intent['artifacts'][key]['sha256'] and len(raw)==intent['artifacts'][key]['bytes'] + and stat.S_IMODE(path.lstat().st_mode)==0o755,'qualified installed executable bytes/mode') + prior=before/(key+'-previous');m=prior.lstat();old=intent['baseline']['controls'][str(path)] + require(dict(device=m.st_dev,inode=m.st_ino,mode=stat.S_IMODE(m.st_mode),uid=m.st_uid,gid=m.st_gid,bytes=m.st_size,sha256=sha(protected(prior,256*1024*1024)))==old,'original binary backup identities/code preserved') + for path,old in intent['baseline']['controls'].items(): + if path.startswith('/usr/local/libexec/'):continue + path=Path(path);m=path.lstat();actual=dict(device=m.st_dev,inode=m.st_ino,mode=stat.S_IMODE(m.st_mode),uid=m.st_uid,gid=m.st_gid,bytes=m.st_size,sha256=sha(protected(path))) + require(actual==old,'unchanged General/APT/package control bytes and identities') + full=base64.b64decode(intent['baseline']['full_config_candidate']['bytes_b64'],validate=True) + config=Path('/etc/rmm-remote/helper-full-maintenance.json');pinpath=Path('/etc/rmm-remote/full-maintenance-helper-proof.json') + require(protected(config)==full and stat.S_IMODE(config.lstat().st_mode)==0o600 and config.lstat().st_gid==0,'exact Full configuration') + pin=json.loads(protected(pinpath));require(pin==historical['full_pin'] and stat.S_IMODE(pinpath.lstat().st_mode)==0o640 + and pinpath.lstat().st_gid==984,'exact protected Full policy/image/identity pin') + ordered_pin={k:pin[k] for k in ('schema_version','profile','identity','policy_sha256','helper_binary_sha256')} + ordered_pin['identity']={k:pin['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','signing_key_id')} + require(protected(pinpath)==json.dumps(ordered_pin,separators=(',',':')).encode(),'exact original inspector pin bytes') + for path,raw in ((Path('/etc/systemd/system/rmm-full-maintenance-helper.service'),base64.b64decode(intent['unit_bytes_b64'],validate=True)), + (Path('/etc/systemd/system/rmm-agent-remote.service.d/60-full-maintenance.conf'),base64.b64decode(payload['dropin_b64'],validate=True))): + require(protected(path)==raw and stat.S_IMODE(path.lstat().st_mode)==0o644 and path.lstat().st_gid==0,'exact Full unit and additive agent dropin bytes/modes') + # Do not open a private key. Database bytes legitimately change under Sync; + # every preexisting non-DB file retains its complete metadata witness. + known_databases={str(state/'journal.sqlite'),str(state/'consumption.sqlite'),str(helper/'consumption.sqlite')} + require(set(old_sql)==known_databases,'exact three known DELETE-mode databases') + known_journals={name+'-journal':name for name in known_databases} + journal_observations=[] + def await_known_delete_journal(name): + require(name in known_databases,'only known DELETE rollback journal paths') + started=time.monotonic_ns();observed=False + while True: + require(all(not os.path.lexists(name+s) for s in ('-wal','-shm')),'WAL/SHM sidecars always denied') + journal=Path(name+'-journal') + if not os.path.lexists(journal): + elapsed=time.monotonic_ns()-started + require(not observed or elapsed<=250_000_000,'known DELETE rollback journal absent within250ms') + if observed:journal_observations.append(dict(database=name,wait_ns=elapsed,observed_naturally_absent=True)) + return + observed=True + try:m=journal.lstat() + except FileNotFoundError:continue + owner=old_files[name] + require(stat.S_ISREG(m.st_mode) and not journal.is_symlink() and m.st_nlink==1 + and m.st_uid==owner[2] and m.st_gid==owner[3] and not m.st_mode&0o022,'known regular protected DELETE rollback journal only') + elapsed=time.monotonic_ns()-started + require(elapsed<250_000_000,'known DELETE rollback journal remained250ms') + time.sleep(min(0.005,(250_000_000-elapsed)/1_000_000_000)) + current_paths=set() + for root in (state,helper): + for path in [root,*sorted(root.rglob('*'))]: + if str(path) in known_journals: + await_known_delete_journal(known_journals[str(path)]) + require(not os.path.lexists(path),'known DELETE rollback journal still absent before state-path exclusion') + continue + if str(path).endswith(('-wal','-shm','-journal')):raise RuntimeError('Settled DELETE-mode snapshot required') + m=path.lstat();require(not path.is_symlink() and (stat.S_ISDIR(m.st_mode) or stat.S_ISREG(m.st_mode)),'retained state object type') + current_paths.add(str(path));old=old_files.get(str(path)) + require(old is not None,'no unexplained new state path') + value=[m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_size,m.st_mtime_ns,m.st_ctime_ns] + require(value[:5]==old[:5] if str(path) in old_sql or stat.S_ISDIR(m.st_mode) else value==old,'state/PKI/key metadata and inodes preserved') + require(current_paths==set(old_files),'all old state paths retained') + @contextlib.contextmanager + def database(name): + path=Path(name);m=path.lstat();require(not path.is_symlink() and stat.S_ISREG(m.st_mode) and m.st_nlink==1 + and [m.st_dev,m.st_ino]==old_sql[name]['identity'],'same actual SQLite inode') + await_known_delete_journal(name) + require(all(not os.path.lexists(name+s) for s in ('-wal','-shm','-journal')),'no SQLite sidecars') + # Never hold a shared DELETE-mode read lock while hashing rows or + # running OpenSSL. A cooperative one-second SQLite progress budget + # limits reads; this is not a hard real-time OS/I/O guarantee. + started=time.monotonic() + with contextlib.closing(sqlite3.connect(path.as_uri()+'?mode=ro',uri=True,timeout=0.25)) as db: + require(all(not os.path.lexists(name+s) for s in ('-wal','-shm','-journal')),'no SQLite sidecars before any SQL after readonly open') + db.set_progress_handler(lambda:int(time.monotonic()-started>1.0),1000) + try: + db.execute('PRAGMA query_only=ON');db.execute('BEGIN') + require(db.execute('PRAGMA journal_mode').fetchone()[0]=='delete','qualified DELETE journal') + yield db + finally: + db.rollback() + elapsed=int((time.monotonic()-started)*1000) + timing=read_transactions.setdefault(name,dict(count=0,total_ms=0,max_ms=0)) + timing['count']+=1;timing['total_ms']+=elapsed;timing['max_ms']=max(timing['max_ms'],elapsed) + require([path.stat().st_dev,path.stat().st_ino]==old_sql[name]['identity'],'SQLite inode unchanged through read') + def row_hash(row):return sha(encode([dict(blob_sha256=sha(v),bytes=len(v)) if isinstance(v,bytes) else v for v in row])) + journal=str(state/'journal.sqlite');ledger=str(helper/'consumption.sqlite');public=json.loads((state/'binding.json').read_bytes()) + require(all(public[k]==intent['general_pin']['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')),'retained enrollment/binding') + key=bytes.fromhex(public['public_key_hex']);require(len(key)==32 and sha(key)==pin['identity']['signing_key_id'],'same public signing key') + verified={} + def signed(decision_id): + if decision_id in verified:return verified[decision_id] + with database(ledger) as db:row=db.execute('SELECT rowid,* FROM helper_sync_decisions WHERE decision_id=?',[decision_id]).fetchone() + require(row is not None,'retained helper-applied signed decision') + response=json.loads(row[3]);raw=bytes(response['decision_bytes']);signature=bytes(response['signature']);require(0=128:out.append((v&127)|128);v>>=7 + out.append(v);return bytes(out) + wire=b'\x0a'+varint(len(raw))+raw+b'\x12\x40'+signature + with database(journal) as db:req=db.execute('SELECT request,generation,response FROM reconcile_requests WHERE id=?',[d['request_id']]).fetchone() + require(req is not None and req[0]==row[4] and sha(req[0])==d['request_hash'] and req[1]==d['connection_generation'] and req[2]==wire,'same agent/helper-applied request and signed response bytes') + original=row_hash(row) in old_sql[ledger]['rows']['helper_sync_decisions'] + verified[decision_id]=dict(decision=d,payload_sha256=sha(raw),signature_sha256=sha(signature),original=original) + return verified[decision_id] + def original_rows(name,table,count): + # There is deliberately no encompassing transaction. Each page closes + # before yield, so hashing/crypto cannot block a DELETE-mode writer. + require(table.replace('_','').isalnum(),'fixed SQLite identifier') + last=None;remaining=count + while remaining: + size=min(16,remaining) + with database(name) as db: + if last is None:page=db.execute('SELECT rowid,* FROM "'+table+'" ORDER BY rowid LIMIT ?',[size]).fetchall() + else:page=db.execute('SELECT rowid,* FROM "'+table+'" WHERE rowid>? ORDER BY rowid LIMIT ?',[last,size]).fetchall() + require(len(page)==size,'all original SQL prefix rows retained') + for row in page: + require(last is None or row[0]>last,'monotonic original SQLite rowid traversal') + last=row[0];remaining-=1;yield row + old_candidates=[] + # Only original IDs are materialized, never all historical request/response + # BLOBs or new append-only Sync rows. Their full hashes are checked below. + total_original=len(old_sql[ledger]['rows']['helper_sync_decisions']);last=None + while len(old_candidates)? ORDER BY rowid LIMIT ?',[last,size]).fetchall() + require(len(page)==size,'all original signed decision IDs retained') + old_candidates.extend(row[1] for row in page);last=page[-1][0] + old_candidates.reverse() + reference_changes=[];summary={} + for name,previous in old_sql.items(): + with database(name) as db: + schema=dict(db.execute("SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name"));require(schema==previous['schema'],'exact unchanged SQL schema') + counts={} + for table,old_hashes in previous['rows'].items(): + require(table.replace('_','').isalnum(),'fixed SQLite identifier') + with database(name) as db:count=db.execute('SELECT count(*) FROM "'+table+'"').fetchone()[0] + counts[table]=count + require(count>=len(old_hashes),'all original SQL rows retained') + if table in ('job_attempts','consumed','execution_evidence','reconcile_outcomes'): + require(count==len(old_hashes),'no new job/effect/history rows during bootstrap') + if table in ('reconcile_meta','helper_sync_meta'):continue + if table=='counter': + with database(name) as db:current=db.execute('SELECT rowid,* FROM counter ORDER BY rowid LIMIT 2').fetchall() + require(len(current)==len(old_hashes)==1 and any(row_hash((1,1,n))==old_hashes[0] for n in range(max(0,current[0][2]-4096),current[0][2]+1)),'monotonic counter reconstructed against exact old hash within4096 bound');continue + if table in ('pending','job_events'): + require(not old_hashes,'no ambiguous preexisting queued messages');continue + for index,row in enumerate(original_rows(name,table,len(old_hashes))): + old_hash=old_hashes[index] + if row_hash(row)==old_hash:continue + if table=='reconcile_requests': + require(row[-1] is not None and row_hash((*row[:-1],None))==old_hash,'only original NULL response may fill') + with database(ledger) as helper_db:candidates=list(helper_db.execute('SELECT decision_id FROM helper_sync_decisions WHERE request=?',[row[2]])) + require(len(candidates)==1,'filled response has independent helper-applied original request') + signed(candidates[0][0]) + continue + require(table=='reconcile_outcomes' and name==journal,'immutable original SQL row changed') + # Keep original rowid/job/attempt/outcome bytes; only its + # reference to an independently applied signed decision renews. + original_id=next((d for d in old_candidates if row_hash((*row[:-1],d))==old_hash),None) + require(original_id is not None,'exact old complete outcome-row witness') + old=signed(original_id);new=signed(row[-1]);require(old['original'],'original decision existed before restart') + selected=[] + for proof in (old,new): + found=[a for a in proof['decision']['attempts'] if a['job_id']==row[1] and a['attempt_id']==row[2]] + require(len(found)==1 and found[0]['outcome_sha256']==sha(row[3]) and (not found[0]['outcome_bytes'] or bytes(found[0]['outcome_bytes'])==row[3]),'original signed outcome bytes');selected.append(found[0]) + require(all(selected[0][k]==selected[1][k] for k in ('job_payload_hash','grant_id','authorization_id','plan_hash','action','execution_identity','outcome_state','outcome_sha256','original_boot_id')),'same exact authorized outcome') + a=old['decision'];b=new['decision'];require(a['journal_id']==b['journal_id'] and a['helper_journal_id']==b['helper_journal_id'] and b['checkpoint']>=a['checkpoint'] and b['helper_checkpoint']>=a['helper_checkpoint'] and b['connection_generation']>=a['connection_generation'],'monotonic signed reference renewal') + reference_changes.append(dict(rowid=row[0],original_sha256=old_hash,old_decision_id=original_id,new_decision_id=row[-1])) + summary[name]=dict(identity=previous['identity'],schema_preserved=True,initial_row_counts=counts, + original_row_counts={t:len(v) for t,v in previous['rows'].items()}) + # Recheck fixed effect/job counts after the whole paged witness. New passive + # captures may append throughout; a cross-table snapshot is not claimed. + for name,previous in old_sql.items(): + for table in ('job_attempts','consumed','execution_evidence','reconcile_outcomes'): + if table in previous['rows']: + with database(name) as db:count=db.execute('SELECT count(*) FROM "'+table+'"').fetchone()[0] + require(count==len(previous['rows'][table]),'final exact no-new-effect/job row counts') + with database(ledger) as db: + roots=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY grant_id LIMIT 19').fetchall() + names=[r[1] for r in db.execute('PRAGMA table_info(helper_sync_meta)')];helper_meta=dict(zip(names,db.execute('SELECT * FROM helper_sync_meta WHERE id=1').fetchone())) + exact={r[0]:sha(json.dumps([dict(sha256=sha(v),size=len(v)) if isinstance(v,bytes) else v for v in r],separators=(',',':')).encode()) for r in roots} + require(exact==intent['preflight']['root_rows'] and len(exact)==18 and all(r[5] is not None for r in roots),'final exact original18Root request/grant/signature/start/outcome rows') + with database(journal) as db: + require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'no unfinished agent job') + names=[r[1] for r in db.execute('PRAGMA table_info(reconcile_meta)')];agent_meta=dict(zip(names,db.execute('SELECT * FROM reconcile_meta WHERE id=1').fetchone())) + inventory=payload.get('inventory') + inventory_queued=bool(db.execute('SELECT EXISTS(SELECT 1 FROM job_events WHERE id=?)',[inventory['message_id']]).fetchone()[0]) if inventory is not None else None + require(agent_meta['fault'] is None and helper_meta['fault'] is None and agent_meta['last_decision']==helper_meta['last_decision'] + and agent_meta['generation']==helper_meta['generation'] and agent_meta['journal_id']==helper_meta['agent_journal_id'] + and agent_meta['helper_id']==helper_meta['journal_id'],'fault-free shared current applied decision and journal lineage') + ordered_binding={k:public[k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','public_key_hex')} + require(helper_meta['binding_sha256']==sha(json.dumps(ordered_binding,separators=(',',':')).encode()),'helper ledger independently bound to exact same enrollment') + proof=signed(agent_meta['last_decision']);decision=proof['decision'];now=dt.datetime.now(dt.timezone.utc) + issued=dt.datetime.fromisoformat(decision['issued_at'].replace('Z','+00:00'));until=dt.datetime.fromisoformat(decision['valid_until'].replace('Z','+00:00')) + require(issued<=now=original_basis[k] for k in ('checkpoint','helper_checkpoint','connection_generation')),'original signed journal lineage retained with monotonic checkpoint/generation') + # Specific server-persisted inventory must have left the durable local queue; + # this is code-bound queue convergence, not a captured network ACK frame. + inventory=payload.get('inventory') + inventory_ack=None + if inventory is not None: + require(inventory_queued is False and inventory['boot_id']==decision['boot_id'],'specific stored inventory no longer queued') + inventory_ack=dict(message_id=inventory['message_id'],server_payload_sha256=inventory['payload_sha256'],local_queue_absent=True,wire_ack_captured=False) + require(protected(before/'report.json')==original_report,'original interrupted report retained unchanged') + return dict(case_id=payload['case_id'],observed_from=observed_from,observed_until=now.isoformat(),processes=processes, + boot_id=decision['boot_id'],generation=decision['connection_generation'],first_start_not_before=first_start.isoformat(),root_consumed=18,root_unresolved=0, + verified_state_preservation=True,snapshot_consistency='per_page_then_final_same_decision_and_fixed_effect_counts',sqlite_integrity_scan_performed=False,database_witness=summary,verified_outcome_reference_changes=reference_changes, + agent_meta=agent_meta,helper_meta=helper_meta,signed_sync={k:proof[k] for k in ('payload_sha256','signature_sha256')}, + decision={k:decision[k] for k in ('decision_id','request_id','request_hash','connection_generation','boot_id','journal_id','helper_journal_id','checkpoint','helper_checkpoint','issued_at','valid_until','ready','helper_ready')}, + full_pin=pin,inventory_queue_convergence=inventory_ack,read_transactions=read_transactions,read_transaction_budget_seconds=1.0, + transient_delete_journal_observations=journal_observations,rollback_journal_observation_budget_ms=250, + read_only=True,product_state_writes=False,rmm_jobs=False,approvals=False) + + +def server(payload): + """Stored server state and RLS denial only; no HTTP/live collection or writes.""" + import datetime as dt + import hashlib + import json + from pathlib import Path + import subprocess + import uuid + tenant=str(uuid.UUID(payload['install']['target']['tenant_id'])) + agent=str(uuid.UUID(payload['install']['target']['agent_id'])) + require(tenant=='11111111-1111-4111-8111-111111111111','fixed development tenant') + foreign='22222222-2222-4222-8222-222222222222' + scope="tenant_id='"+tenant+"' AND agent_id='"+agent+"'" + query=("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY; SELECT json_build_object('observed_at',now()," + "'installation',(SELECT json_build_object('connection_generation',connection_generation,'connection_boot_id',connection_boot_id," + "'last_contact_at',last_contact_at,'last_sequence',last_sequence,'last_authenticated_at',last_authenticated_at," + "'certificate_sha256',certificate_sha256,'last_authenticated_certificate_sha256',last_authenticated_certificate_sha256," + "'revoked_at',revoked_at,'capabilities',capabilities,'sync_ready',sync_ready,'helper_sync_ready',helper_sync_ready," + "'restore_blocked',restore_blocked,'sequence_regressed',sequence_regressed,'sync_recovery_required',sync_recovery_required) FROM agent_installations WHERE "+scope+")," + "'head',(SELECT row_to_json(s) FROM agent_sync_state s WHERE "+scope+")," + "'decisions',(SELECT COALESCE(json_agg(x),'[]'::json) FROM (SELECT decision_id,request_id,request_hash,connection_generation," + "encode(payload,'hex') AS payload_hex,encode(signature,'hex') AS signature_hex FROM sync_checkpoints WHERE "+scope+" ORDER BY created_at DESC,decision_id DESC LIMIT 16) x)," + "'inventories',(SELECT COALESCE(json_agg(x),'[]'::json) FROM (SELECT message_id,boot_id,sequence,observed_at,received_at,source_id,payload_sha256 FROM observations WHERE "+scope+" ORDER BY received_at DESC LIMIT 16) x)," + "'jobs',(SELECT COALESCE(json_agg(x),'[]'::json) FROM (SELECT job_id,attempt_id,state,encode(sha256(convert_to(to_jsonb(j)::text,'UTF8')),'hex') AS row_sha256 FROM jobs j WHERE "+scope+" ORDER BY job_id,attempt_id LIMIT 31) x)," + "'outbox_count',(SELECT count(*) FROM outbox o JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE "+scope.replace('tenant_id=','j.tenant_id=').replace('agent_id=','j.agent_id=')+")," + "'permit_count',(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE "+scope.replace('tenant_id=','j.tenant_id=').replace('agent_id=','j.agent_id=')+")); COMMIT;") + root=Path('/opt/rust-discovery') + cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm'] + result=json.loads(subprocess.run(cmd,input=query.encode(),capture_output=True,check=True,timeout=25).stdout) + require(len(result['jobs'])==30 and result['outbox_count']==30 and result['permit_count']==26 + and all(j['state'] in ('succeeded','failed','cancelled','expired') for j in result['jobs']),'same closed30/30/26 central job counts') + policy=root/'devstate/root-policy/policy.json';m=policy.lstat();raw=policy.read_bytes() + require(not policy.is_symlink() and m.st_uid==10001 and m.st_mode&0o777==0o600 + and hashlib.sha256(raw).hexdigest()==payload['install']['central_policy_sha256'] + and dt.datetime.fromisoformat(json.loads(raw)['expires_at'])<=dt.datetime.now(dt.timezone.utc),'central Root scope remains protected and closed') + for row in result['decisions']: + raw=bytes.fromhex(row.pop('payload_hex'));signature=bytes.fromhex(row.pop('signature_hex'));d=json.loads(raw) + require(len(raw)<=512*1024 and len(signature)==64,'bounded stored signed decision') + row['payload_sha256']=hashlib.sha256(raw).hexdigest();row['signature_sha256']=hashlib.sha256(signature).hexdigest() + row['decision']={k:d[k] for k in ('decision_id','request_id','request_hash','connection_generation','boot_id','journal_id','helper_journal_id','checkpoint','helper_checkpoint','issued_at','valid_until','ready','helper_ready')} + denial=("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY; SET LOCAL ROLE rmm_app; SET LOCAL app.tenant_id='"+foreign+"'; " + "SELECT json_build_object('installations',(SELECT count(*) FROM agent_installations WHERE agent_id='"+agent+"')," + "'observations',(SELECT count(*) FROM observations WHERE agent_id='"+agent+"')," + "'jobs',(SELECT count(*) FROM jobs WHERE agent_id='"+agent+"')); COMMIT;") + denied=json.loads(subprocess.run(cmd,input=denial.encode(),capture_output=True,check=True,timeout=25).stdout) + require(denied==dict(installations=0,observations=0,jobs=0),'actual foreign-tenant application-role stored-data denial') + result.update(tenant_denial=denied,tenant_denial_scope='postgres_rmm_app_rls_not_http',central_root_scope_closed=True,read_only=True,rmm_jobs=False,approvals=False) + return result + + +def rpc(prefix,function,payload,folder): + source=inspect.getsource(function);labels=set() + for node in ast.walk(ast.parse(source)): + if not isinstance(node,ast.Call) or not isinstance(node.func,ast.Name):continue + index=1 if node.func.id=='require' else 0 if node.func.id=='RuntimeError' else None + if index is not None and len(node.args)>index and isinstance(node.args[index],ast.Constant) and isinstance(node.args[index].value,str):labels.add(node.args[index].value) + code=inspect.getsource(require)+'\n'+source + code+='\ntry:\n print(__import__("json").dumps('+function.__name__+'('+repr(payload)+')))\nexcept Exception as e:\n import traceback\n label=str(e) if isinstance(e,RuntimeError) and str(e) in '+repr(sorted(labels))+' else None\n print(__import__("json").dumps(dict(bootstrap_error=True,error_class=type(e).__name__,guard_code=label,frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(e.__traceback__)],sqlite_errorcode=getattr(e,"sqlite_errorcode",None),sqlite_errorname=getattr(e,"sqlite_errorname",None))))\n' + return support.transport(prefix,'python3 -',code.encode(),folder,timeout=240) + + +def server_prefix(): + cfg=support.access.load_config(pre.ACCESS_ROOT/'credentials/server.conf') + require(cfg['address']=='88.99.80.202' and cfg['username']=='root' and cfg.get('port','22')=='22' and cfg.get('ssh_key_path'),'fixed existing server bootstrap transport') + return ['ssh','-F','/dev/null','-T','-p','22','-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','-i',str(Path(cfg['ssh_key_path']).expanduser()),'root@88.99.80.202'] + + +def parse_stored_time(value): + """PostgreSQL RFC3339 at microsecond precision; pad, never round/truncate.""" + import datetime as dt + import re + require(isinstance(value,str),'stored UTC timestamp string') + matched=re.fullmatch(r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,6}))?(Z|[+-]\d{2}:\d{2})',value) + require(matched is not None,'exact bounded PostgreSQL RFC3339 timestamp') + fraction=('.'+matched[2].ljust(6,'0')) if matched[2] else '' + zone='+00:00' if matched[3]=='Z' else matched[3] + return dt.datetime.fromisoformat(matched[1]+fraction+zone) + + +def compare_samples(samples): + import datetime as dt + require(len(samples)==3,'exact baseline and two subsequent passive samples') + baseline=samples[0];last=baseline + for sample in samples: + current=sample['vm'];stored=sample['server'];installation=stored['installation'];d=current['decision'] + require(current['read_only'] is True and current['verified_state_preservation'] is True + and current['product_state_writes'] is False and current['rmm_jobs'] is False and current['approvals'] is False,'every native VM preservation proof') + require(stored['read_only'] is True and stored['rmm_jobs'] is False and stored['approvals'] is False + and stored['central_root_scope_closed'] is True and stored['tenant_denial']==dict(installations=0,observations=0,jobs=0) + and stored['tenant_denial_scope']=='postgres_rmm_app_rls_not_http' + and len(stored['jobs'])==30 and stored['outbox_count']==30 and stored['permit_count']==26,'actual stored closed server/tenant-denial probes') + require(installation['connection_generation']==current['generation']==baseline['vm']['generation'] + and installation['connection_boot_id']==current['boot_id']==baseline['vm']['boot_id'] + and current['processes']==baseline['vm']['processes'],'stable actual new process pair/Full helper and connection generation') + require(installation['sync_ready'] is True and installation['helper_sync_ready'] is True + and all(installation[k] is False for k in ('restore_blocked','sequence_regressed','sync_recovery_required')) + and installation['revoked_at'] is None and installation['certificate_sha256']==installation['last_authenticated_certificate_sha256'],'stored authenticated current ready channel') + found=[r for r in stored['decisions'] if r['decision_id']==d['decision_id']] + require(len(found)==1 and found[0]['decision']==d and all(found[0][k]==current['signed_sync'][k] for k in ('payload_sha256','signature_sha256')),'same original signed decision retained at server/agent/helper') + caps=installation['capabilities'];pin=current['full_pin'] + require(all(c in caps for c in ('rmm.sync.v1','script.full_maintenance/1','root:run_script', + 'helper.policy.sha256:'+pin['policy_sha256'],'helper.binary.sha256:'+pin['helper_binary_sha256'])),'normal fresh agent publication of authenticated Full peer/policy/image capability') + require(stored['jobs']==baseline['server']['jobs'],'all existing server job state/row hashes unchanged') + inventory=current['inventory_queue_convergence'];require(inventory is not None,'specific persisted inventory and local queue convergence') + inventories=[i for i in stored['inventories'] if i['message_id']==inventory['message_id']] + require(len(inventories)==1 and inventories[0]['payload_sha256']==inventory['server_payload_sha256'] + and inventories[0]['boot_id']==current['boot_id'] and inventories[0]['source_id']=='linux.base' + and parse_stored_time(inventories[0]['received_at'])>=parse_stored_time(current['first_start_not_before']),'concrete fresh passive stored inventory identity') + contact=parse_stored_time(installation['last_contact_at']) + require(contact>=parse_stored_time(current['first_start_not_before']) + and 0<=(parse_stored_time(stored['observed_at'])-contact).total_seconds()<=45,'fresh stored heartbeat after first start') + if sample is not baseline: + require(contact>parse_stored_time(last['server']['installation']['last_contact_at']) + and installation['last_sequence']>last['server']['installation']['last_sequence'],'two distinct subsequent stored heartbeat advances') + last=sample + return dict(verified_preservation=True,verified_normal_signed_sync=True,stored_heartbeat_advances=2, + specific_inventory_queue_convergence=True,wire_inventory_ack_captured=False,tenant_rls_denial=True, + tenant_http_denial_tested=False,read_only=True,rmm_jobs=False,approvals=False,full_job_accepted=False) + + +def main(): + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('mode',choices=('prepare-local','observe','finish')) + parser.add_argument('--id') + args=parser.parse_args() + os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);support.private(STATE,True) + manifest_path=pre.native.PACK/'manifest.json';manifest=support.load(manifest_path) + require(support.sha(manifest_path)==pre.native.MANIFEST_SHA and support.sha(pre.NATIVE)==pre.NATIVE_SHA + and all(support.sha(ROOT/name)==digest for name,digest in manifest['source_files'].items()),'unchanged frozen158 inputs and original Linux-first native proof') + if args.mode=='prepare-local': + require(args.id is None,'new local evidence intent only') + install_path=ROOT/'devstate/script-console-install'/CASE/'intent.json';install=support.load(install_path) + require(install['id']==CASE and install['preflight_source_sha256']==PREFLIGHT_SHA and install['manifest_sha256']==pre.native.MANIFEST_SHA,'exact reviewed installation provenance') + folder=STATE/str(uuid.uuid4());folder.mkdir(mode=0o700) + support.save(folder/'intent.json',dict(type='rmm.script-console.read-only-post-attestation-v1',case_id=CASE, + runner_sha256=support.sha(__file__),install_intent_sha256=support.sha(install_path),install=install, + dropin_b64=base64.b64encode(DROPIN).decode(),read_only_target_calls=True)) + else: + require(args.id is not None and str(uuid.UUID(args.id))==args.id,'canonical original local evidence case') + folder=STATE/args.id;intent=support.load(folder/'intent.json') + require(intent['runner_sha256']==support.sha(__file__) and intent['case_id']==CASE,'original same-source observation intent') + require(support.sha(ROOT/'devstate/script-console-install'/CASE/'intent.json')==intent['install_intent_sha256'],'original local installation intent retained') + if args.mode=='observe': + number=len(list(folder.glob('sample-*.json')));require(number<3,'three distinct passive samples only') + sample_id=str(uuid.uuid4());support.save(folder/('read-intent-'+sample_id+'.json'),dict(read_only=True)) + linux=support.access_settings(pre.ACCESS_ROOT,'linux') + initial=rpc(linux,vm,intent,folder) + stored=rpc(server_prefix(),server,intent,folder) + import datetime as dt + fresh=[i for i in stored['inventories'] if i['boot_id']==initial['boot_id'] and i['source_id']=='linux.base' + and parse_stored_time(i['received_at'])>=parse_stored_time(initial['first_start_not_before'])] + require(fresh,'no fresh startup inventory stored yet; read-only evidence incomplete') + current=rpc(linux,vm,dict(intent,inventory=fresh[0]),folder) + stored=rpc(server_prefix(),server,intent,folder) + support.save(folder/('sample-'+str(number)+'.json'),dict(vm=current,server=stored)) + else: + samples=[support.load(folder/('sample-'+str(i)+'.json')) for i in range(3)] + result=compare_samples(samples);support.save(folder/'attested.json',dict(result,case_id=CASE, + runner_sha256=support.sha(__file__),sample_sha256=[support.sha(folder/('sample-'+str(i)+'.json')) for i in range(3)])) + print(json.dumps(dict(id=folder.name,intent=str(folder/'intent.json'),mode=args.mode,target_called=args.mode=='observe',read_only=True,rmm_jobs=False,approvals=False))) + + +if __name__=='__main__': + main() diff --git a/deploy/script_console_windows_acceptance.py b/deploy/script_console_windows_acceptance.py new file mode 100644 index 0000000..ff65f7e --- /dev/null +++ b/deploy/script_console_windows_acceptance.py @@ -0,0 +1,226 @@ +#!/usr/bin/env python3 +"""Current Windows units: list-only first, then one explicit list-hash-bound run. + +Parent executes only after source review. No deployment, RMM/UI jobs, service, +SQL, helper, capability, time-policy or production-clock changes. +""" +import argparse +import base64 +import hashlib +import json +import os +from pathlib import Path +import re +import uuid + +ROOT = Path(__file__).resolve().parents[1] +SUPPORT = { + 'deploy/script_console_acceptance.py': '2b3ba8d548d18627643a9f73c9a4f7bacce4ac2cd5495745c6760d088e078145', + 'deploy/windows_msi_runtime_acceptance.py': '833ad8806c90a19342aa6fbc71c33e81975caffb1746550e9b0fbdca7fa0c7ab', + 'deploy/vm_access_check.py': '5c8107ea9151a9f498b2530cdc61c1a7a6d745aff853a9b05710f3395838c0dd', + 'deploy/script_console_windows_link.py': '7c770c4537f1e0bd4e7583f4af5fab0934065970d2f2aec9d7a42ecdc98d4089', +} + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def support_guards(): + require(all(not (ROOT/n).is_symlink() and sha(ROOT/n)==h for n,h in SUPPORT.items()), 'reviewed bootstrap sources unchanged') + + +support_guards() +import script_console_acceptance as native +support = native.support +require(Path(native.__file__).resolve()==ROOT/'deploy/script_console_acceptance.py' + and Path(support.__file__).resolve()==ROOT/'deploy/windows_msi_runtime_acceptance.py' + and Path(support.access.__file__).resolve()==ROOT/'deploy/vm_access_check.py','exact pinned transport module origins') +PACK = ROOT/'devstate/script-console-windows-link/run-d0b7c844-6770-4930-ac87-4380c87fbad7' +PACK_SHA = '4b31e6a997248f91db00cf56d7d6043a6feb840be237b9a5a5de5eb71ee4b80c' +LINUX = ROOT/'devstate/script-console-native/7b61ce46-7913-492b-9c6b-414a0c2b6ffc/passed-e214cd9b-21ac-48ce-82dc-d10364329d04.json' +LINUX_SHA = 'a831a70a3cf76a3e8a9bf5820dcca7100d2ef9860dd9388539fe9c13f949f470' +STATE = ROOT/'devstate/script-console-windows-native' +PROFILE = 'script-console-windows-units-v1' +KINDS = {'lib':'windows-lib-tests.exe','bin':'windows-agent-tests.exe'} +REMOTE = '/C:/ProgramData/RustDiscoveryBootstrap/script-console-units-' +FLAGS = ('product_state_writes','rmm_jobs','approvals','capabilities_activated','clock_qualified','helper_runtime_qualified','sql_changed') + + +SCRIPT = r''' +$ErrorActionPreference='Stop';$request=([Console]::In.ReadLine()|ConvertFrom-Json) +function HashBytes([byte[]]$b){$h=[Security.Cryptography.SHA256]::Create();try{return ([BitConverter]::ToString($h.ComputeHash($b))).Replace('-','').ToLowerInvariant()}finally{$h.Dispose()}} +$raw=[Convert]::FromBase64String($request.intent_bytes_b64);if($raw.Length-gt196608-or(HashBytes $raw)-cne$request.intent_sha256){throw 'Exact bounded original intent bytes'} +$p=[Text.Encoding]::UTF8.GetString($raw)|ConvertFrom-Json +if([string]([guid]$p.id)-cne$p.id-or$p.profile-cne'script-console-windows-units-v1'-or$request.action-notin@('prepare','list','run','read')-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.runner_sha256-notmatch'^[a-f0-9]{64}$'-or$p.script_sha256-notmatch'^[a-f0-9]{64}$'-or$p.linux_proof_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed Windows script-console unit intent'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('script-console-units-'+$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 'Fixed unit path type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private unit owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private unit ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private unit ACL'}}} +function WriteBytes($path,[byte[]]$b){$f=[IO.File]::Open($path,'CreateNew','Write','Read');try{$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +function WriteNew($path,$value){WriteBytes $path ([Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 24 -Compress)))} +function ReadPrivate($path){Safe $path $false;if((Get-Item -LiteralPath $path).Length-gt8388608){throw 'Bounded unit receipt'};return (Get-Content -LiteralPath $path -Raw|ConvertFrom-Json)} +if($request.action-eq'read'-and!(Test-Path -LiteralPath $case)){@{id=$p.id;readback_only=$true;case_present=$false}|ConvertTo-Json -Compress;exit 0} +Safe $root $true +if($request.action-eq'prepare'){if(Test-Path -LiteralPath $case){throw 'Existing case: readback only'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteBytes (Join-Path $case 'intent.json') $raw;@{id=$p.id;prepared=$true}|ConvertTo-Json -Compress;exit 0} +Safe $case $true;Safe (Join-Path $case 'intent.json') $false;if((Get-FileHash -LiteralPath (Join-Path $case 'intent.json') -Algorithm SHA256).Hash.ToLowerInvariant()-cne$request.intent_sha256){throw 'Original complete intent retained'} +if($request.action-eq'read'){ + $partial=@{};foreach($name in @('lists.json','list-lib.json','list-bin.json','run-lib.json','run-bin.json','result.json')){$path=Join-Path $case $name;if(Test-Path -LiteralPath $path){$partial[$name]=ReadPrivate $path}} + $listPath=Join-Path $case 'lists.json';$listBytes=$(if(Test-Path -LiteralPath $listPath){[IO.File]::ReadAllBytes($listPath)}else{$null}) + @{id=$p.id;readback_only=$true;case_present=$true;list_entered=(Test-Path -LiteralPath (Join-Path $case 'list-entered.json'));run_entered=(Test-Path -LiteralPath (Join-Path $case 'run-entered.json'));lists_sha256=$(if($null-ne$listBytes){HashBytes $listBytes}else{$null});lists_bytes_b64=$(if($null-ne$listBytes){[Convert]::ToBase64String($listBytes)}else{$null});partial=$partial;tests_repeated=$false}|ConvertTo-Json -Depth 24 -Compress;exit 0 +} +if(!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){throw 'Bootstrap administrator unit context required'} +# Native OS directory + a checked Windows environment, never arbitrary inherited +# variables. SystemDrive is required by the native inventory unit and retained. +$system32=[Environment]::SystemDirectory;$windows=[IO.Directory]::GetParent($system32).FullName;$drive=[IO.Path]::GetPathRoot($windows).TrimEnd('\') +if($drive-notmatch'^[A-Za-z]:$'-or$windows-ine($drive+'\Windows')-or$system32-ine($windows+'\System32')-or$env:WINDIR-ine$windows-or$env:SystemRoot-ine$windows-or($env:SystemDrive-and$env:SystemDrive-ine$drive)-or!(Test-Path -LiteralPath $system32 -PathType Container)){throw 'Checked native SystemDrive/WINDIR/SystemRoot required'} +$temp=Join-Path $case 'tmp' +if($request.action-eq'list'){WriteNew (Join-Path $case 'list-entered.json') @{list_once=$true;intent_sha256=$request.intent_sha256};New-Item -ItemType Directory -Path $temp|Out-Null};Safe $temp $true +function RunUnit($exe,[string]$arguments){$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=$exe;$s.Arguments=$arguments;$s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.WorkingDirectory=$case;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$s.EnvironmentVariables.Clear();$s.EnvironmentVariables['SystemRoot']=$windows;$s.EnvironmentVariables['WINDIR']=$windows;$s.EnvironmentVariables['SystemDrive']=$drive;$s.EnvironmentVariables['PATH']=$system32;$s.EnvironmentVariables['TEMP']=$temp;$s.EnvironmentVariables['TMP']=$temp;$s.EnvironmentVariables['RUST_BACKTRACE']='0';$c=New-Object Diagnostics.Process;$c.StartInfo=$s;if(!$c.Start()){throw 'Unit start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$timed=$false;if(!$c.WaitForExit(30000)){$timed=$true;$c.Kill();if(!$c.WaitForExit(2000)){throw 'Own unit process did not exit'}};if(!$o.Wait(2000)-or!$e.Wait(2000)-or$o.Result.Length-gt1048576-or$e.Result.Length-gt1048576){throw 'Bounded unit output'};return @{exit_code=$c.ExitCode;timed_out=$timed;stdout_b64=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($o.Result));stderr_b64=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($e.Result))}}finally{$c.Dispose()}} +$paths=@{} +foreach($kind in @('lib','bin')){$a=$p.artifacts.$kind;$exe=Join-Path $case ($kind+'.exe');Safe $exe $false;if($a.sha256-notmatch'^[a-f0-9]{64}$'-or(Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$a.sha256-or(Get-Item -LiteralPath $exe).Length-ne$a.bytes){throw 'Exact current Windows unit delivery'};$paths[$kind]=$exe} +if($request.action-eq'list'){ + $lists=@{};foreach($kind in @('lib','bin')){$listed=RunUnit $paths[$kind] '--list --format terse';$text=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($listed.stdout_b64));[string[]]$lines=@($text-split'\r?\n'|Where-Object{$_.Trim()});if($listed.exit_code-ne0-or$listed.timed_out-or@($lines|Where-Object{$_-notmatch'^[A-Za-z0-9_:]+: test$'}).Count){throw 'Only complete actual native terse test entries'};[string[]]$names=@($lines|ForEach-Object{$_-replace': test$',''});[Array]::Sort($names,[StringComparer]::Ordinal);if($names.Count-lt1-or$names.Count-gt512-or@($names|Select-Object -Unique).Count-ne$names.Count){throw 'Bounded distinct complete names'};$value=@{sha256=$p.artifacts.$kind.sha256;names=$names;listed=$listed};WriteNew (Join-Path $case ('list-'+$kind+'.json')) $value;$lists[$kind]=$value} + $value=@{id=$p.id;profile=$p.profile;manifest_sha256=$p.manifest_sha256;intent_sha256=$request.intent_sha256;linux_proof_sha256=$p.linux_proof_sha256;lists=$lists;actual_native_lists=$true;tests_executed=$false;environment=@{SystemRoot=$windows;WINDIR=$windows;SystemDrive=$drive};environment_cleared=$true;product_state_writes=$false;rmm_jobs=$false;approvals=$false;capabilities_activated=$false;clock_qualified=$false;helper_runtime_qualified=$false;sql_changed=$false};$path=Join-Path $case 'lists.json';WriteNew $path $value;$bytes=[IO.File]::ReadAllBytes($path);@{id=$p.id;lists_sha256=(HashBytes $bytes);lists_bytes_b64=[Convert]::ToBase64String($bytes);tests_executed=$false}|ConvertTo-Json -Depth 24 -Compress;exit 0 +} +if($request.lists_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Explicit list SHA required'};$path=Join-Path $case 'lists.json';Safe $path $false;if((Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()-cne$request.lists_sha256){throw 'Original complete list bytes'};$lists=ReadPrivate $path +if($lists.id-cne$p.id-or$lists.intent_sha256-cne$request.intent_sha256-or$lists.manifest_sha256-cne$p.manifest_sha256-or!$lists.actual_native_lists-or$lists.tests_executed){throw 'Actual original list-only proof'} +WriteNew (Join-Path $case 'run-entered.json') @{execute_tests_once=$true;lists_sha256=$request.lists_sha256};$reports=@{} +foreach($kind in @('lib','bin')){if($lists.lists.$kind.sha256-cne$p.artifacts.$kind.sha256){throw 'Exact list executable binding'};$run=RunUnit $paths[$kind] '--test-threads=1';$value=@{sha256=$p.artifacts.$kind.sha256;names=$lists.lists.$kind.names;result=$run};WriteNew (Join-Path $case ('run-'+$kind+'.json')) $value;$reports[$kind]=$value;if($run.exit_code-ne0-or$run.timed_out){throw 'Native unit failed; original case readback only'}} +$value=@{id=$p.id;manifest_sha256=$p.manifest_sha256;intent_sha256=$request.intent_sha256;lists_sha256=$request.lists_sha256;reports=$reports;all_lists_before_any_tests=$true;environment_cleared=$true;bootstrap_administrator_units=$true;local_service_proof=$false;native_pipe_proof=$false;product_state_writes=$false;rmm_jobs=$false;approvals=$false;capabilities_activated=$false;clock_qualified=$false;helper_runtime_qualified=$false;sql_changed=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 24 -Compress +''' + + +def guards(): + support_guards();native.verify_support() + require(sha(PACK/'manifest.json')==PACK_SHA and sha(LINUX)==LINUX_SHA, 'current pack and Linux84 receipt exact') + manifest=support.load(PACK/'manifest.json');proof=support.load(LINUX) + require(manifest['source_manifest_sha256']==native.MANIFEST_SHA and manifest['linux_native_proof_sha256']==LINUX_SHA + and manifest['linux_actual_passed']==84 and manifest['linux_explicit_database_ignored']==5 + and manifest['source_before_after_equal'] is True and manifest['windows_target']=='x86_64-pc-windows-gnu' + and manifest['pipe_v2_sha256']=='ce525dab23f2e4faa2c9816280c1a5a8b40e56d0e63a350256e83b2df1d9229d' + and all(manifest[f] is False for f in ('windows_native_lists_executed','windows_native_tests_executed','clock_qualified','capabilities_activated','deployed','rmm_jobs','approvals')), + 'current qualified Windows link-only generation') + require(proof['verified'] is True and proof['manifest_sha256']==native.MANIFEST_SHA + and proof['native']['passed']==84 and proof['native']['ignored_database_cases']==5 + and all(proof['native'][f] is False for f in ('product_state_writes','rmm_jobs','capabilities_activated','approvals')), 'actual Linux84 scope') + require(sha(native.PACK/'manifest.json')==native.MANIFEST_SHA and len(manifest['source_files'])==158 + and manifest['source_files']==support.load(native.PACK/'manifest.json')['source_files'] + and all(not (ROOT/n).is_symlink() and sha(ROOT/n)==h for n,h in manifest['source_files'].items()), 'all frozen158 inputs unchanged') + require(set(manifest['artifacts'])=={'windows-lib-tests.exe','windows-agent-tests.exe','rmm-helper-windows.exe','rmm-agent-windows.exe'}, 'all four current PE deliveries') + for name,item in manifest['artifacts'].items(): + for suffix,key,size in (('', 'sha256','bytes'),('.original','original_sha256','original_bytes')): + path=PACK/(name+suffix);support.private(path) + require(sha(path)==item[key] and path.stat().st_size==item[size], 'all exact delivery/original PE bytes') + return manifest + + +def encoded(intent): + return json.dumps(intent,sort_keys=True,separators=(',',':')).encode() + + +def rpc(intent,action,folder,lists_sha=None): + guards() + require(intent['runner_sha256']==sha(__file__) and intent['script_sha256']==hashlib.sha256(SCRIPT.encode()).hexdigest() + and intent['support_sha256']==SUPPORT,'same original runner/script/support before every target call') + raw=encoded(intent);payload=dict(intent_bytes_b64=base64.b64encode(raw).decode(),intent_sha256=hashlib.sha256(raw).hexdigest(),action=action) + if lists_sha is not None:payload['lists_sha256']=lists_sha + wrapper='try {\n'+SCRIPT+'\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}' + command='powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand '+base64.b64encode(wrapper.encode('utf-16le')).decode() + return support.transport(support.access_settings(support.ACCESS_ROOT,'windows'),command,(json.dumps(payload)+'\n').encode(),folder,timeout=110) + + +def checked_lists(raw,intent,expected_sha): + require(hashlib.sha256(raw).hexdigest()==expected_sha and len(raw)<=8*1024*1024,'exact bounded immutable list bytes') + value=json.loads(raw,object_pairs_hook=support.object_pairs) + require(value['id']==intent['id'] and value['profile']==PROFILE and value['manifest_sha256']==PACK_SHA + and value['intent_sha256']==hashlib.sha256(encoded(intent)).hexdigest() and value['linux_proof_sha256']==LINUX_SHA + and value['actual_native_lists'] is True and value['tests_executed'] is False + and value['environment_cleared'] is True and set(value['lists'])==set(KINDS) + and all(value[f] is False for f in FLAGS),'list-only provenance and scope') + env=value['environment'];require(set(env)=={'SystemRoot','WINDIR','SystemDrive'} and re.fullmatch(r'[A-Za-z]:',env['SystemDrive']) + and env['SystemRoot'].lower()==env['WINDIR'].lower()==(env['SystemDrive']+'\\Windows').lower(),'checked minimal native test environment') + for kind,item in value['lists'].items(): + listed=item['listed'];text=base64.b64decode(listed['stdout_b64'],validate=True).decode();lines=[line for line in text.splitlines() if line.strip()] + require(item['sha256']==intent['artifacts'][kind]['sha256'] and listed['exit_code']==0 and listed['timed_out'] is False + and all(re.fullmatch(r'[A-Za-z0-9_:]+: test',line) for line in lines) + and sorted(line[:-6] for line in lines)==item['names'] and len(set(item['names']))==len(lines) + and 1<=len(lines)<=512,'actual complete distinct named native list') + return value + + +def checked_result(value,intent,lists,lists_sha): + require(value['id']==intent['id'] and value['manifest_sha256']==PACK_SHA + and value['intent_sha256']==hashlib.sha256(encoded(intent)).hexdigest() and value['lists_sha256']==lists_sha + and set(value['reports'])==set(KINDS) and value['all_lists_before_any_tests'] is True + and value['environment_cleared'] is True and value['bootstrap_administrator_units'] is True + and value['local_service_proof'] is False and value['native_pipe_proof'] is False + and all(value[f] is False for f in FLAGS),'exact narrow final unit scope') + for kind,report in value['reports'].items(): + result=report['result'];text=base64.b64decode(result['stdout_b64'],validate=True).decode();names=lists['lists'][kind]['names'];count=len(names) + require(report['sha256']==intent['artifacts'][kind]['sha256'] and report['names']==names + and result['exit_code']==0 and result['timed_out'] is False + and sorted(re.findall(r'^test (.+) \.\.\. ok\r?$',text,re.M))==names + and re.search(r'^test result: ok\. '+str(count)+r' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;',text,re.M), + 'all exact current named tests passed, no skips or filtering') + + +def retain_original_lists(folder,raw,intent,digest): + value=checked_lists(raw,intent,digest);path=folder/'lists-original.json' + if path.exists(): + support.private(path);require(path.read_bytes()==raw,'original native list bytes never replaced') + else: + with path.open('xb') as out:out.write(raw);out.flush();os.fsync(out.fileno()) + return value + + +def retain_passed(folder,result,intent,lists,digest): + checked_result(result,intent,lists,digest);guards() + proof=dict(verified=True,manifest_sha256=PACK_SHA,linux_native_proof_sha256=LINUX_SHA,lists_sha256=digest,native=result) + path=folder/'passed.json' + if path.exists():require(support.load(path)==proof,'original native proof never replaced') + else:support.save(path,proof) + + +def main(): + parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('mode',choices=('prepare-local','list','run','read'));parser.add_argument('--case');parser.add_argument('--lists-sha');args=parser.parse_args() + os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);support.private(STATE,True);manifest=guards() + if args.mode=='prepare-local': + require(args.case is None and args.lists_sha is None,'new local intent only');case=str(uuid.uuid4());folder=STATE/case;folder.mkdir(mode=0o700) + intent=dict(id=case,profile=PROFILE,manifest_sha256=PACK_SHA,linux_proof_sha256=LINUX_SHA,runner_sha256=sha(__file__),script_sha256=hashlib.sha256(SCRIPT.encode()).hexdigest(),support_sha256=SUPPORT, + artifacts={kind:{key:manifest['artifacts'][name][key] for key in ('sha256','bytes')} for kind,name in KINDS.items()}) + support.save(folder/'intent.json',intent) + else: + case=str(uuid.UUID(args.case));require(case==args.case,'canonical original case');folder=STATE/case;intent=support.load(folder/'intent.json') + expected=dict(id=case,profile=PROFILE,manifest_sha256=PACK_SHA,linux_proof_sha256=LINUX_SHA,runner_sha256=sha(__file__),script_sha256=hashlib.sha256(SCRIPT.encode()).hexdigest(),support_sha256=SUPPORT, + artifacts={kind:{key:manifest['artifacts'][name][key] for key in ('sha256','bytes')} for kind,name in KINDS.items()}) + require(intent==expected,'complete original same-source JSON case for every readback') + if args.mode=='list': + require(args.lists_sha is None,'list-only step');support.save(folder/'list-entered-local.json',dict(list_once=True,no_retry=True)) + result=rpc(intent,'prepare',folder);support.save(folder/'prepared-remote.json',result);require(result['prepared'] is True,'new exact remote unit case') + for kind,name in KINDS.items(): + guards();support.upload(support.access_settings(support.ACCESS_ROOT,'windows'),dict(path=str(PACK/name),**intent['artifacts'][kind]),REMOTE+case+'/'+kind+'.exe',folder,kind) + result=rpc(intent,'list',folder);support.save(folder/'list-reply.json',result) + raw=base64.b64decode(result['lists_bytes_b64'],validate=True);retain_original_lists(folder,raw,intent,result['lists_sha256']) + print(json.dumps(dict(case_id=case,mode='list',lists_sha256=result['lists_sha256'],counts={k:len(v['names']) for k,v in json.loads(raw)['lists'].items()},tests_executed=False)));return + elif args.mode=='run': + require(args.lists_sha is not None and re.fullmatch('[a-f0-9]{64}',args.lists_sha),'explicit immutable native lists SHA required') + path=folder/'lists-original.json';support.private(path);lists=checked_lists(path.read_bytes(),intent,args.lists_sha) + support.save(folder/'run-entered-local.json',dict(run_once=True,lists_sha256=args.lists_sha,no_retry=True)) + result=rpc(intent,'run',folder,args.lists_sha);support.save(folder/'run-reply.json',result);retain_passed(folder,result,intent,lists,args.lists_sha) + else: + require(args.lists_sha is None,'readback never executes or needs a new list choice');result=rpc(intent,'read',folder);support.save(folder/('readback-'+str(uuid.uuid4())+'.json'),result) + require(result['id']==case and result['readback_only'] is True + and result.get('tests_repeated',False) is False,'readback scope and case') + if result.get('lists_bytes_b64') is not None: + raw=base64.b64decode(result['lists_bytes_b64'],validate=True) + lists=retain_original_lists(folder,raw,intent,result['lists_sha256']) + if 'result.json' in result['partial']: + retain_passed(folder,result['partial']['result.json'],intent,lists,result['lists_sha256']) + print(json.dumps(dict(case_id=case,mode=args.mode,intent=str(folder/'intent.json'),intent_sha256=sha(folder/'intent.json'),target_called=args.mode!='prepare-local',rmm_jobs=False,approvals=False))) + + +if __name__=='__main__': + main() diff --git a/deploy/script_console_windows_link.py b/deploy/script_console_windows_link.py new file mode 100644 index 0000000..044f277 --- /dev/null +++ b/deploy/script_console_windows_link.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +"""Link current Windows runtime/units after the actual matching Linux84 proof. + +No VM access, unit execution, enrollment, capability activation or deployment. +Windows native lists remain explicitly pending; old42 names are never relabelled. +""" +import hashlib +import json +import os +from pathlib import Path +import shutil +import subprocess +import uuid + +ROOT = Path(__file__).resolve().parents[1] +PACK = ROOT / 'devstate/script-console-qualification/run-29461892-343f-42f4-b469-88703535425e' +PACK_SHA = 'ddd1a78d1cd289b02b1f98572f9652171ec00be84e01ce099fd722d689bcaacd' +LINUX = ROOT / 'devstate/script-console-native/7b61ce46-7913-492b-9c6b-414a0c2b6ffc/passed-e214cd9b-21ac-48ce-82dc-d10364329d04.json' +LINUX_SHA = 'a831a70a3cf76a3e8a9bf5820dcca7100d2ef9860dd9388539fe9c13f949f470' +IMAGE = 'sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9' +PIPE_SHA = 'ce525dab23f2e4faa2c9816280c1a5a8b40e56d0e63a350256e83b2df1d9229d' + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def load(path): + require(not path.is_symlink() and path.stat().st_uid == os.getuid() + and path.stat().st_mode & 0o777 == 0o600, 'private source evidence') + return json.loads(path.read_bytes()) + + +def main(): + os.umask(0o077) + require(sha(PACK / 'manifest.json') == PACK_SHA and sha(LINUX) == LINUX_SHA, 'exact current source and actual Linux84 proof') + source = load(PACK / 'manifest.json'); proof = load(LINUX) + require(proof['verified'] is True and proof['manifest_sha256'] == PACK_SHA + and proof['native']['passed'] == 84 and proof['native']['ignored_database_cases'] == 5 + and proof['native']['all_lists_before_any_tests'] is True + and all(proof['native'][flag] is False for flag in ('product_state_writes','rmm_jobs','capabilities_activated','approvals')), + 'actual bounded Linux-first proof, not an old fixture count') + frozen = source['source_files'] + require(len(frozen) == 158 and all(sha(ROOT / name) == value for name, value in frozen.items()) + and sha(ROOT / 'crates/agent-windows/src/helper_ipc.rs') == PIPE_SHA, 'unchanged current source including actual PipeV2') + state = ROOT / 'devstate/script-console-windows-link'; state.mkdir(mode=0o700, parents=True, exist_ok=True) + out = state / ('run-' + str(uuid.uuid4())); out.mkdir(mode=0o700) + target = ROOT / 'devstate/script-console-windows-target'; target.mkdir(mode=0o700, exist_ok=True) + checks = {} + + def command(label, args): + value = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False) + log = out / (label + '.log'); log.write_bytes(value.stdout) + checks[label] = dict(exit_code=value.returncode, log_sha256=sha(log)) + require(value.returncode == 0, 'link failed: inspect private ' + str(log)) + return value.stdout + + docker = ['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] + common = ['--manifest-path','/src/Cargo.toml','--locked','--offline','--target','x86_64-pc-windows-gnu', + '-p','rmm-agent-windows','--features','system-helper','--release'] + units = command('windows-current-unit-link',docker+['test',*common,'--lib','--bin','rmm-agent-windows','--no-run','--message-format=json']) + binaries = command('windows-current-runtime-link',docker+['build',*common,'--bins','--message-format=json']) + artifacts = {} + for kind, raw in (('units', units), ('runtime', binaries)): + for line in raw.splitlines(): + try: + value = json.loads(line) + except ValueError: + continue + if value.get('reason') != 'compiler-artifact' or not value.get('executable'): + continue + name = value['target']['name'] + if kind == 'units': + label = {'rmm_agent_windows':'windows-lib-tests.exe','rmm-agent-windows':'windows-agent-tests.exe'}.get(name) + require(value.get('profile',{}).get('test') is True, 'test artifact only') + else: + label = {'rmm-helper-windows':'rmm-helper-windows.exe','rmm-agent-windows':'rmm-agent-windows.exe'}.get(name) + if label is None: + continue + require(label not in artifacts, 'exact unique PE artifact') + linked = target / Path(value['executable']).relative_to('/target') + original = out / (label + '.original'); delivery = out / label + shutil.copyfile(linked, original); shutil.copyfile(linked, delivery) + command(label+'-strip', ['docker','run','--rm','--network','none','--read-only','-v',str(out)+':/artifacts', + '--entrypoint','x86_64-w64-mingw32-strip',IMAGE,'--strip-debug','/artifacts/'+label]) + data = delivery.read_bytes(); offset = int.from_bytes(data[60:64], 'little') + require(data[:2] == b'MZ' and data[offset:offset+6] == b'PE\0\0\x64\x86', 'actual Windows AMD64 PE') + require(sha(linked) == sha(original), 'original linked bytes retained') + artifacts[label] = dict(sha256=sha(delivery),bytes=delivery.stat().st_size,original_sha256=sha(original), + original_bytes=original.stat().st_size,debug_stripped_delivery=True,native_executed=False) + require(set(artifacts) == {'windows-lib-tests.exe','windows-agent-tests.exe','rmm-helper-windows.exe','rmm-agent-windows.exe'}, 'complete current runtime/unit set') + require(all(sha(ROOT/name) == digest for name,digest in frozen.items()) and sha(LINUX)==LINUX_SHA, 'same158 inputs and actual Linux proof before/after') + result = dict(type='rmm.script-console.windows-link-pack',schema_version=1,source_manifest_sha256=PACK_SHA, + source_files=frozen,source_before_after_equal=True,linux_native_proof_sha256=LINUX_SHA,linux_actual_passed=84, + linux_explicit_database_ignored=5,image_id=IMAGE,windows_target='x86_64-pc-windows-gnu',pipe_v2_sha256=PIPE_SHA, + checks=checks,artifacts=artifacts,windows_native_lists_executed=False,windows_native_tests_executed=False, + require_all_actual_native_lists_before_any_test=True,reuses_old_42_test_expectation=False, + clock_qualified=False,capabilities_activated=False,deployed=False,rmm_jobs=False,approvals=False) + path = out/'manifest.json'; path.write_text(json.dumps(result,indent=2)+'\n') + print(json.dumps(dict(manifest=str(path),sha256=sha(path),windows_native_executed=False,capabilities_activated=False))) + + +if __name__=='__main__': + main() diff --git a/deploy/windows/Build-MsiFixture.ps1 b/deploy/windows/Build-MsiFixture.ps1 index 5bf51bc..320948e 100644 --- a/deploy/windows/Build-MsiFixture.ps1 +++ b/deploy/windows/Build-MsiFixture.ps1 @@ -1,9 +1,9 @@ #requires -Version 5.1 <# -Build-only, fixed MSI 1.0.0 fixture. Writes only a NEW private child directory. +Build-only, fixed MSI1.0.0 or1.1.0 major-upgrade fixture. Writes only a NEW private child directory. Never calls InstallProduct/OpenPackage/msiexec, modifies installed products, services, execution policy, certificate trust or reboot state. No downloads. -No upgrade/EXE package is authored by this version. +1.1 removes only the exact known1.0 predecessor; no EXE/repair mode. Primary API/format contracts: https://learn.microsoft.com/en-us/windows/win32/msi/installer-opendatabase @@ -17,7 +17,8 @@ https://learn.microsoft.com/en-us/windows/win32/msi/package-codes [CmdletBinding()] param( [Parameter(Mandatory=$true)][string]$OutputParent, - [Guid]$CaseId=([Guid]::NewGuid()) + [Guid]$CaseId=([Guid]::NewGuid()), + [ValidateSet('1.0.0','1.1.0')][string]$FixtureVersion='1.0.0' ) Set-StrictMode -Version Latest $ErrorActionPreference='Stop' @@ -25,14 +26,18 @@ $case=$null;$created=$false;$installer=$null;$database=$null;$summary=$null;$mak $product='{3F070B4B-9C08-4D65-BD41-13D81B884AEC}' $upgrade='{C9BF1FB8-E31D-4BD7-BA0D-AB46539BBBAA}' $component='{4D1CA80F-9AF8-4F54-B426-450AFBAFF92E}' -$version='1.0.0' +$version=$FixtureVersion +$isUpgrade=$version -ceq '1.1.0' +$oldProduct=$product +if($isUpgrade){$product='{D42BD387-079B-4BBF-A134-0DA7B7A97011}'} +$upgradeCondition='Installed OR (RMM_OLD_PRODUCTS = "{3F070B4B-9C08-4D65-BD41-13D81B884AEC}" AND RMM_ALL_PRODUCTS = "{3F070B4B-9C08-4D65-BD41-13D81B884AEC}")' # Product/family/component identities are fixed. Nonidentical MSI bytes must # have distinct PackageCodes, so each new authoring case gets its own code. $package='{'+([Guid]::NewGuid().ToString().ToUpperInvariant())+'}' $artifact=[Guid]::NewGuid().ToString() $report=[ordered]@{schema_version=1;case_id=$CaseId.ToString();completed=$false;stage='preflight'; build_only=$true;native_installation_tested=$false;product_changes=$false;services_changed=$false; - reboot_requested=$false;trust_changed=$false;downloads=$false;upgrade_authored=$false; + reboot_requested=$false;trust_changed=$false;downloads=$false;upgrade_authored=$isUpgrade; product_code=$product;upgrade_code=$upgrade;component_code=$component;package_code=$package;version=$version} function Require([bool]$Value,[string]$Code) {if (!$Value) {throw $Code}} @@ -169,7 +174,7 @@ try { $report.tools=$tools $report.stage='payload_and_cabinet' $payload=Join-Path $case 'msi-payload.txt' - $payloadBytes=[Text.Encoding]::ASCII.GetBytes("RustDiscovery MSI fixture 1.0.0`r`n") + $payloadBytes=[Text.Encoding]::ASCII.GetBytes("RustDiscovery MSI fixture $version`r`n") Write-New $payload $payloadBytes $ddf=Join-Path $case 'fixture.ddf' $ddfText=@" @@ -190,7 +195,7 @@ try { $cab=Join-Path $case 'fixture.cab';No-Reparse $cab Require ((Get-Item -LiteralPath $cab).Length -gt 0 -and (Get-Item -LiteralPath $cab).Length -le 65536) 'MSI_CABINET_LIMIT' $report.stage='database_authoring:com_activation' - $msi=Join-Path $case 'RustDiscoveryFixture-1.0.0-x64.msi';Require (!(Test-Path -LiteralPath $msi)) 'MSI_OUTPUT_EXISTS' + $msi=Join-Path $case ('RustDiscoveryFixture-'+$version+'-x64.msi');Require (!(Test-Path -LiteralPath $msi)) 'MSI_OUTPUT_EXISTS' $installer=New-Object -ComObject WindowsInstaller.Installer $report.stage='database_authoring:open_create' $database=Com-Method $installer 'OpenDatabase' @($msi,[int]3) @@ -205,9 +210,11 @@ try { InstallExecuteSequence='`Action` CHAR(72) NOT NULL, `Condition` CHAR(255), `Sequence` SHORT PRIMARY KEY `Action`' LaunchCondition='`Condition` CHAR(255) NOT NULL, `Description` CHAR(255) NOT NULL LOCALIZABLE PRIMARY KEY `Condition`' } + if($isUpgrade){$tables.Upgrade='`UpgradeCode` CHAR(38) NOT NULL, `VersionMin` CHAR(20), `VersionMax` CHAR(20), `Language` CHAR(255), `Attributes` SHORT NOT NULL, `Remove` CHAR(255), `ActionProperty` CHAR(72) NOT NULL PRIMARY KEY `UpgradeCode`, `VersionMin`, `VersionMax`, `Language`, `Attributes`'} foreach ($table in $tables.Keys) {$report.stage='database_authoring:create_table:'+$table;Sql ('CREATE TABLE `'+$table+'` ('+$tables[$table]+')')} $report.stage='database_authoring:insert_rows' $properties=[ordered]@{ProductCode=$product;UpgradeCode=$upgrade;ProductVersion=$version;ProductLanguage='1033';ProductName='RustDiscovery MSI Fixture';Manufacturer='Ease Technologies';ALLUSERS='1';INSTALLLEVEL='1';REBOOT='ReallySuppress'} + if($isUpgrade){$properties.SecureCustomProperties='RMM_ALL_PRODUCTS;RMM_OLD_PRODUCTS'} foreach ($key in $properties.Keys) {Sql ("INSERT INTO ``Property`` (``Property``,``Value``) VALUES ('$key','$($properties[$key])')")} Sql 'INSERT INTO `Directory` (`Directory`,`DefaultDir`) VALUES (''TARGETDIR'',''SourceDir'')' Sql 'INSERT INTO `Directory` (`Directory`,`Directory_Parent`,`DefaultDir`) VALUES (''ProgramFiles64Folder'',''TARGETDIR'',''.'')' @@ -218,8 +225,17 @@ try { Sql ("INSERT INTO ``File`` (``File``,``Component_``,``FileName``,``FileSize``,``Attributes``,``Sequence``) VALUES ('PayloadFile','PayloadComponent','MSIPAY~1.TXT|msi-payload.txt',$($payloadBytes.Length),16896,1)") Sql 'INSERT INTO `Media` (`DiskId`,`LastSequence`,`Cabinet`) VALUES (1,1,''#fixture.cab'')' Sql 'INSERT INTO `LaunchCondition` (`Condition`,`Description`) VALUES (''VersionNT64'',''This fixture requires 64-bit Windows.'')' + if($isUpgrade){ + Sql ("INSERT INTO ``Upgrade`` (``UpgradeCode``,``VersionMin``,``VersionMax``,``Attributes``,``Remove``,``ActionProperty``) VALUES ('$upgrade','1.0.0','1.0.0',768,'ALL','RMM_OLD_PRODUCTS')") + Sql ("INSERT INTO ``Upgrade`` (``UpgradeCode``,``VersionMin``,``Attributes``,``Remove``,``ActionProperty``) VALUES ('$upgrade','0.0.0',258,'ALL','RMM_ALL_PRODUCTS')") + Sql ("INSERT INTO ``LaunchCondition`` (``Condition``,``Description``) VALUES ('$upgradeCondition','Only the exact approved fixture1.0 predecessor may be upgraded.')") + } $sequence=[ordered]@{LaunchConditions=100;CostInitialize=800;FileCost=900;CostFinalize=1000;InstallValidate=1400;InstallInitialize=1500;ProcessComponents=1600;UnpublishFeatures=1800;RemoveFiles=3500;RemoveFolders=3600;CreateFolders=3700;InstallFiles=4000;RegisterUser=6000;RegisterProduct=6100;PublishFeatures=6300;PublishProduct=6400;InstallFinalize=6600} - foreach ($action in $sequence.Keys) {Sql ("INSERT INTO ``InstallExecuteSequence`` (``Action``,``Sequence``) VALUES ('$action',$($sequence[$action]))")} + if($isUpgrade){$sequence.FindRelatedProducts=25;$sequence.RemoveExistingProducts=1550} + foreach ($action in $sequence.Keys) { + if($action -ceq 'RemoveExistingProducts'){Sql ("INSERT INTO ``InstallExecuteSequence`` (``Action``,``Condition``,``Sequence``) VALUES ('$action','NOT Installed',$($sequence[$action]))")} + else {Sql ("INSERT INTO ``InstallExecuteSequence`` (``Action``,``Sequence``) VALUES ('$action',$($sequence[$action]))")} + } $view=Com-Method $database 'OpenView' @('INSERT INTO `_Streams` (`Name`,`Data`) VALUES (?,?)');$record=$null try {$record=Com-Method $installer 'CreateRecord' @([int]2);Com-Set $record 'StringData' @([int]1,'fixture.cab');[void](Com-Method $record 'SetStream' @([int]2,$cab));[void](Com-Method $view 'Execute' @($record));[void](Com-Method $view 'Close')} finally {Release-Com $record;Release-Com $view} @@ -236,7 +252,23 @@ try { foreach ($key in $properties.Keys) {$row=Rows ("SELECT ``Value`` FROM ``Property`` WHERE ``Property``='$key'");Require ($row.Count -eq 1 -and $row[0][0] -ceq $properties[$key]) 'MSI_PROPERTY_READBACK'} $allTables=Rows 'SELECT `Name` FROM `_Tables`';$names=@($allTables|ForEach-Object {$_[0]}) Require ($names.Count -eq $tables.Count -and @($names|Where-Object {$_ -notin $tables.Keys}).Count -eq 0) 'MSI_UNEXPECTED_TABLE' - Require ('CustomAction' -notin $names -and 'Upgrade' -notin $names -and 'Registry' -notin $names -and 'ServiceInstall' -notin $names) 'MSI_FORBIDDEN_TABLE' + Require ('CustomAction' -notin $names -and 'Registry' -notin $names -and 'ServiceInstall' -notin $names -and ($isUpgrade -or 'Upgrade' -notin $names)) 'MSI_FORBIDDEN_TABLE' + # Independently read committed upgrade/sequence metadata; no install APIs. + foreach($action in $sequence.Keys){ + $row=Rows ("SELECT ``Condition``,``Sequence`` FROM ``InstallExecuteSequence`` WHERE ``Action``='$action'") + $condition=if($action -ceq 'RemoveExistingProducts'){'NOT Installed'}else{''} + Require ($row.Count -eq 1 -and $row[0][0] -ceq $condition -and $row[0][1] -ceq [string]$sequence[$action]) 'MSI_SEQUENCE_READBACK' + } + if($isUpgrade){ + $rows=Rows 'SELECT `UpgradeCode`,`VersionMin`,`VersionMax`,`Attributes`,`Remove`,`ActionProperty` FROM `Upgrade` WHERE `Language` IS NULL ORDER BY `ActionProperty`' + Require ($rows.Count -eq 2) 'MSI_UPGRADE_ROW_COUNT' + $expected=@(@($upgrade,'0.0.0','','258','ALL','RMM_ALL_PRODUCTS'),@($upgrade,'1.0.0','1.0.0','768','ALL','RMM_OLD_PRODUCTS')) + for($i=0;$i -lt 2;$i++){for($j=0;$j -lt 6;$j++){Require ($rows[$i][$j] -ceq $expected[$i][$j]) 'MSI_UPGRADE_ROW_READBACK'}} + $nullMax=Rows 'SELECT `ActionProperty` FROM `Upgrade` WHERE `VersionMax` IS NULL' + Require ($nullMax.Count -eq 1 -and $nullMax[0][0] -ceq 'RMM_ALL_PRODUCTS') 'MSI_UPGRADE_NULL_MAX_READBACK' + $launch=Rows ("SELECT ``Description`` FROM ``LaunchCondition`` WHERE ``Condition``='$upgradeCondition'") + Require ($launch.Count -eq 1 -and $launch[0][0] -ceq 'Only the exact approved fixture1.0 predecessor may be upgraded.' -and $readback.LaunchCondition.Count -eq 2) 'MSI_UPGRADE_LAUNCH_READBACK' + } $summary=Com-Get $database 'SummaryInformation' @([int]0) Require ((Com-Get $summary 'Property' @([int]7)) -ceq 'x64;1033' -and (Com-Get $summary 'Property' @([int]9)) -ceq $package -and [int](Com-Get $summary 'Property' @([int]15)) -eq 2) 'MSI_SUMMARY_READBACK' Release-Com $summary;$summary=$null @@ -253,12 +285,17 @@ try { $extra=Com-Method $view 'Fetch';Require ($null -eq $extra) 'MSI_DUPLICATE_CAB_STREAM';[void](Com-Method $view 'Close') } finally {Release-Com $record;Release-Com $view} Release-Com $database;$database=$null - $recipe=[ordered]@{schema_version=1;recipe_id='rmm-windows-msi-fixture-install-1.0';recipe_version='1';platform='windows';upgrade_code=$upgrade;architecture='x64';context='machine';command_profile='fixture_quiet_no_reboot_v1';artifact=[ordered]@{artifact_id=$artifact;sha256=(Hash $msi);bytes=$length};postcondition=[ordered]@{product_code=$product;product_version=$version;payload_path='C:\Program Files\RustDiscoveryFixtures\msi-payload.txt';payload_sha256=(Hash $payload);payload_bytes=$payloadBytes.Length};no_reboot=$true} + $recipe=[ordered]@{schema_version=1;recipe_id=$(if($isUpgrade){'rmm-windows-msi-fixture-upgrade-1.1'}else{'rmm-windows-msi-fixture-install-1.0'});recipe_version='1';platform='windows';upgrade_code=$upgrade;architecture='x64';context='machine';command_profile='fixture_quiet_no_reboot_v1';artifact=[ordered]@{artifact_id=$artifact;sha256=(Hash $msi);bytes=$length};postcondition=[ordered]@{product_code=$product;product_version=$version;payload_path='C:\Program Files\RustDiscoveryFixtures\msi-payload.txt';payload_sha256=(Hash $payload);payload_bytes=$payloadBytes.Length};no_reboot=$true} + if($isUpgrade){ + $recipe.package_code=$package + $oldBytes=[Text.Encoding]::ASCII.GetBytes("RustDiscovery MSI fixture 1.0.0`r`n") + $recipe.predecessor=[ordered]@{product_code=$oldProduct;product_version='1.0.0';payload_path=$recipe.postcondition.payload_path;payload_sha256=(Hash-Bytes $oldBytes);payload_bytes=$oldBytes.Length} + } $recipeJson=$recipe|ConvertTo-Json -Depth 12 -Compress Write-New (Join-Path $case 'recipe.json') ([Text.Encoding]::UTF8.GetBytes($recipeJson)) Write-Json (Join-Path $case 'tables.json') $readback $report.artifact=[ordered]@{path=$msi;sha256=(Hash $msi);bytes=$length;artifact_id_proposal=$artifact} - $report.payload=[ordered]@{sha256=(Hash $payload);bytes=$payloadBytes.Length;content_ascii="RustDiscovery MSI fixture 1.0.0`r`n";installed_path=$recipe.postcondition.payload_path} + $report.payload=[ordered]@{sha256=(Hash $payload);bytes=$payloadBytes.Length;content_ascii="RustDiscovery MSI fixture $version`r`n";installed_path=$recipe.postcondition.payload_path} $report.cabinet=[ordered]@{sha256=(Hash $cab);bytes=(Get-Item -LiteralPath $cab).Length;embedded_hash_verified=$true;stream='fixture.cab'} $report.tables_sha256=Hash (Join-Path $case 'tables.json');$report.table_names=$names $report.resolved_recipe=[ordered]@{recipe=$recipe;recipe_sha256=(Hash (Join-Path $case 'recipe.json'))} @@ -267,7 +304,7 @@ try { Require ((Hash $PSCommandPath) -ceq $report.builder_sha256) 'MSI_BUILDER_CHANGED' $report.completed=$true;$report.stage='authored_and_read_back_not_installed' Write-Json (Join-Path $case 'result.json') $report - [ordered]@{completed=$true;build_only=$true;report_path=(Join-Path $case 'result.json');artifact=$report.artifact;native_installation_tested=$false;upgrade_authored=$false}|ConvertTo-Json -Depth 5 + [ordered]@{completed=$true;build_only=$true;report_path=(Join-Path $case 'result.json');artifact=$report.artifact;native_installation_tested=$false;upgrade_authored=$isUpgrade}|ConvertTo-Json -Depth 5 } catch { $report.completed=$false;$report.error_type=$_.Exception.GetType().FullName;$report.error_hresult=$_.Exception.HResult $report.error_line=$_.InvocationInfo.ScriptLineNumber diff --git a/deploy/windows/system_pipe_acceptance.py b/deploy/windows/system_pipe_acceptance.py new file mode 100644 index 0000000..d1288d8 --- /dev/null +++ b/deploy/windows/system_pipe_acceptance.py @@ -0,0 +1,368 @@ +#!/usr/bin/env python3 +"""Source-bound Pipe-v2 pack and create-once native units, never deployment. + +The access root only supplies the existing pinned SSH transport and immutable +native Core proofs. No credentials/devstate symlinks, copies, services or jobs. +""" +import argparse +import base64 +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import stat +import subprocess +import sys +import uuid + +CODE = Path(__file__).resolve().parents[2] +ACCESS = Path('/home/kevin/Schreibtisch/Coding/rust-discovery') +sys.path.insert(0, str(ACCESS / 'deploy/windows')) +sys.path.insert(0, str(ACCESS / 'deploy')) +import client_bootstrap as cb +import auto_transport_acceptance as auto +import runtime_upgrade as previous + +require = cb.contract.require +BUILD = CODE / 'devstate/windows-pipe-drain-build' +NATIVE = CODE / 'devstate/windows-client/native-pipe-v2' +# These imported transports read their original protected access configuration. +# Their new local upload/error receipts belong to this isolated checkout. +cb.EVIDENCE = CODE / 'devstate/windows-client' +CORE = ACCESS / 'devstate/core-no-start/build-de37ee99-11bf-465b-9519-45ad8f6e5b87/manifest.json' +CORE_SHA = '1bc082031fcad20d5a519b0004f63d499dbba2ea9ce25d56230d48d2556d474b' +LOCAL = CODE / 'devstate/windows-pipe-drain/qualification-1e0f2b22-1e74-41af-b8ed-0a356be054ba/qualification.json' +LOCAL_SHA = 'a7e3c604d4ad19feed0323ac23aa7a0290b6fb93bc57649cafbadbe5f293b1d4' +TARGET = 'x86_64-pc-windows-gnu' +NAMES = {'agent': 'rmm-agent-windows.exe', 'helper': 'rmm-helper-windows.exe', + 'probe': 'RuntimeStateProbe.exe', 'system_probe': 'SystemStateProbe.exe', + 'lib': 'windows-tests-lib.exe', 'bin': 'windows-tests-bin.exe'} +# Expected V2 native lists: unchanged names, two existing codec cases extended. +TESTS = { + 'lib': sorted([ + 'clock::tests::gate_requires_all_status_and_uncertainty_evidence', + 'clock::tests::production_clock_stays_closed_until_native_adapter_qualification', + 'clock::tests::suspend_closes_until_actual_post_resume_sync', + 'clock::tests::timeout_worker_stays_closed_without_a_second_call', + 'clock::tests::wall_step_and_overflow_fail_closed', + 'helper_ipc::tests::pipe_client_rights_cannot_create_an_instance', + 'helper_ipc::tests::protocol_does_not_accept_a_shell_operation', + 'helper_ipc::tests::reconfigured_service_cannot_adopt_an_old_running_identity', + 'helper_ipc::tests::serialization_refuses_oversized_frame', + 'helper_ipc::tests::untrusted_lengths_are_rejected_before_body_allocation', + 'helper_runtime::intent::tests::cancel_and_dispatch_cas_are_exclusive_and_history_cannot_be_rewritten', + 'helper_runtime::tests::system_route_binds_original_request_shape_budget_and_reply_scope', + 'helper_system::tests::completed_scm_state_cannot_override_elapsed_runtime', + 'helper_system::tests::fixture_recovery_cannot_add_commands_reboots_or_restart_attempts', + 'helper_system::tests::service_adapter_exposes_only_implemented_states', + 'helper_system::tests::single_fixture_cannot_start_services_or_groups_as_dependencies', + 'journal::tests::bounded_existing_journal_preserves_identity_sequence_and_unacked_bytes', + 'journal::tests::durable_reopen_ack_identity_and_single_process', + 'journal::tests::migrates_original_passive_schema_without_rewriting_pending_envelopes', + 'journal::tests::unsupported_page_size_is_refused_without_mutating_existing_data', + 'native::tests::boot_identity_survives_independent_native_queries', + 'native::tests::native_inventory_has_real_windows_sources_and_no_fake_inodes', + 'process::tests::arguments_never_become_shell_code_and_trailing_slashes_are_quoted']), + 'bin': sorted([ + 'https_channel::endpoint_tests::configured_dns_identity_cannot_inject_a_url_or_disable_name_checks', + 'runtime::tests::bound_helper_unknown_repairs_generic_crash_gap_without_ack_or_duplicate_result', + 'runtime::tests::legacy_inventory_keeps_original_bytes_until_concrete_core_ack', + 'runtime::tests::old_passive_configuration_cannot_enable_jobs_or_read_the_clock', + 'runtime::tests::worker_fence_requires_same_generation_and_entire_clock_interval', + 'wire_channel::tests::auto_fallback_classifies_upgrade_refusal_but_never_authentication', + 'wire_channel::tests::explicit_transport_names_cannot_enable_verification_bypasses'])} +COUNTS = {'lib': 23, 'bin': 7} + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def save(path, value): + path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + cb.contract.write_private(path, value) + + +def private_file(path): + path = Path(path) + require(not path.is_symlink() and not path.parent.is_symlink(), 'private regular path') + info = path.lstat() + require(stat.S_ISREG(info.st_mode) and info.st_uid == os.getuid() + and stat.S_IMODE(info.st_mode) == 0o600, 'private regular owner/mode') + return path + + +def sources(): + require(ACCESS.is_dir() and not ACCESS.is_symlink() and cb.ROOT == ACCESS, + 'original access checkout only; no copied configuration') + names = {'Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', 'proto/agent.proto', + 'deploy/windows/system_pipe_acceptance.py', 'docs/windows-helper-pipe-drain.md'} + names.update(str(p.relative_to(CODE)) for p in (CODE / 'crates').glob('*/Cargo.toml')) + for crate in ('agent-windows', 'agent-core', 'domain', 'protocol'): + names.update(str(p.relative_to(CODE)) for p in (CODE / 'crates' / crate).rglob('*') + if p.is_file() and p.suffix in ('.rs', '.c', '.h', '.idl', '.toml')) + names.update('crates/agent/src/' + n + '.rs' for n in ('https_channel', 'transport', 'wire_channel')) + require(all((CODE / n).is_file() and not (CODE / n).is_symlink() for n in names), 'complete fixed build sources') + transport = {} + for module in tuple(sys.modules.values()): + raw = getattr(module, '__file__', None) + if raw: + path = Path(raw).resolve() + if path.is_relative_to(ACCESS / 'deploy') and path.suffix == '.py': + transport[str(path.relative_to(ACCESS))] = sha(path) + require('deploy/windows/client_bootstrap.py' in transport, 'actual transport source binding') + return {'code': {n: sha(CODE / n) for n in sorted(names)}, 'access_transport': dict(sorted(transport.items()))} + + +def core_proof(): + require(sha(CORE) == CORE_SHA, 'exact original Linux-first Core manifest') + manifest = cb.contract.protected_json(CORE) + result = {'manifest_sha256': CORE_SHA} + for platform, count in (('linux', 80), ('windows', 58)): + path = CORE.parent / (platform + '-passed.json') + proof = cb.contract.protected_json(path) + require(proof['verified'] is True and proof['platform'] == platform + and proof['manifest_sha256'] == CORE_SHA, 'actual same-manifest native Core proof') + actual = proof['native']['reports'] + require(set(actual) == set(manifest['test_counts'][platform]) + and sum(len(v['names']) for v in actual.values()) == count, 'all native Core reports') + for kind, item in actual.items(): + n = manifest['test_counts'][platform][kind] + output = base64.b64decode(item['stdout_b64'], validate=True) + require(item['exit_code'] == 0 and item['timed_out'] is False + and item['names'] == manifest['test_names'][platform][kind] + and item['sha256'] == manifest['artifacts'][platform][kind]['sha256'] + and re.search(rb'test result: ok\. ' + str(n).encode() + + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', output), + 'complete actual native Core result') + result[platform] = {'proof_sha256': sha(path), 'case_id': proof['case_id']} + for name, digest in manifest['source_files'].items(): + if name.startswith(('crates/agent-core/', 'crates/domain/', 'crates/protocol/', 'proto/')): + require(sha(CODE / name) == digest, 'isolated common source equals actual native Core proof') + return result + + +def reused_qualification(): + require(sha(LOCAL) == LOCAL_SHA, 'exact retained local qualification') + value = cb.contract.protected_json(LOCAL) + require(value['type'] == 'rmm.windows.pipe-drain-local-qualification' + and value['source_before_after_equal'] is True and value['native_executed'] is False + and value['clock_qualified'] is False and value['native_counts_expected'] == COUNTS + and len(value['commands']) == 4 and all(c['exit_code'] == 0 for c in value['commands']), + 'actual previous cross-Clippy/release/unit-link qualification') + for name, digest in value['source_files'].items(): + require(sha(CODE / name) == digest, 'all original qualified inputs remain unchanged') + for item in list(value['artifacts'].values()) + list(value['unit_links'].values()): + path = Path(item['path']) + target = LOCAL.parent / 'target' + require(path.is_relative_to(target), 'qualified link under private target') + current = target + for part in ('',) + path.relative_to(target).parts: + if part: + current = current / part + info = current.lstat() + require(not stat.S_ISLNK(info.st_mode) and info.st_uid in (0, os.getuid()) + and info.st_mode & 0o022 == 0, 'protected original compiler output') + require(stat.S_ISREG(path.lstat().st_mode) and sha(path) == item['sha256'] + and path.stat().st_size == item['bytes'], 'retained exact qualified links') + require(re.search(r'^pub const ADAPTER_QUALIFIED: bool = false;$', + (CODE / 'crates/agent-windows/src/clock.rs').read_text(), re.M), 'production clock remains closed') + return value + + +def command(args, folder, name): + with (folder / (name + '.stdout')).open('xb') as out, (folder / (name + '.stderr')).open('xb') as err: + result = subprocess.run(args, stdout=out, stderr=err) + save(folder / (name + '.json'), {'exit_code': result.returncode, 'local_only': True, 'arguments': args}) + require(result.returncode == 0, 'local build failed; original stdout/stderr retained') + + +def build(): + before, common, prior = sources(), core_proof(), reused_qualification() + folder = BUILD / ('build-' + str(uuid.uuid4())) + folder.mkdir(mode=0o700, parents=True) + save(folder / 'source-before.json', before) + save(folder / 'reused-qualification.json', {'path': str(LOCAL), 'sha256': LOCAL_SHA, 'value': prior}) + image = prior['build_image'] + require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable compiler image') + target = LOCAL.parent / 'target' + # Only these two RELEASE examples were missing. Original Clippy, EXEs and + # unit links are hash-bound above and are deliberately not rerun/rebuilt. + command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=512m', + '-v', str(CODE) + ':/src:ro', '-v', str(target) + ':/target', '-e', 'CARGO_TARGET_DIR=/target', + '--entrypoint', 'cargo', image, 'build', '--manifest-path', '/src/Cargo.toml', '--locked', '--offline', + '--target', TARGET, '-p', 'rmm-agent-windows', '--features', 'system-helper', '--release', + '--example', 'runtime_state', '--example', 'system_state'], folder, 'release-examples') + selected = {'agent': Path(prior['artifacts']['agent']['path']), 'helper': Path(prior['artifacts']['helper']['path']), + 'lib': Path(prior['unit_links']['lib']['path']), 'bin': Path(prior['unit_links']['bin']['path']), + 'probe': target / TARGET / 'release/examples/runtime_state.exe', + 'system_probe': target / TARGET / 'release/examples/system_state.exe'} + artifacts = {} + for kind, original in selected.items(): + output, retained = folder / NAMES[kind], folder / (NAMES[kind] + '.linked') + original_sha = sha(original) + for destination in (retained, output): + shutil.copyfile(original, destination) + destination.chmod(0o600) + if kind in COUNTS: + command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', str(folder) + ':/artifacts', + '--entrypoint', 'x86_64-w64-mingw32-strip', image, '--strip-debug', '/artifacts/' + NAMES[kind]], + folder, 'strip-' + kind) + require(sha(original) == sha(retained) == original_sha, 'original links retained unchanged') + data = output.read_bytes() + offset = int.from_bytes(data[60:64], 'little') + require(data[:2] == b'MZ' and data[offset:offset + 6] == b'PE\0\0\x64\x86', 'Windows AMD64 PE') + artifacts[kind] = {'path': str(output), 'name': NAMES[kind], 'sha256': sha(output), 'bytes': len(data), + 'original_link_path': str(retained), 'original_link_sha256': original_sha, + 'release_build': kind not in COUNTS, 'debug_stripped': kind in COUNTS} + require(sources() == before and core_proof() == common and reused_qualification() == prior, + 'compiler inputs, original links and native Core proof unchanged before/after') + manifest = {'schema_version': 1, 'type': 'rmm.windows.pipe-v2-build', 'source_files': before, + 'source_before_after_equal': True, 'code_root': str(CODE), 'access_root': str(ACCESS), + 'artifacts': artifacts, 'test_names_expected': TESTS, 'native_counts_expected': COUNTS, + 'core_native_proofs': common, 'reused_local_qualification': {'path': str(LOCAL), 'sha256': LOCAL_SHA}, + 'build_image_id': image, 'transport_version': 2, 'raw_v1_controls_qualified': False, + 'production_clock_qualified': False, 'native_executed': False} + save(folder / 'manifest.json', manifest) + print(json.dumps({'manifest': str(folder / 'manifest.json'), 'native_executed': False})) + + +SCRIPT = 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}$'){throw 'Fixed Pipe V2 unit case'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('native-pipe-v2-'+$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'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty unit ACL'};foreach($r in $rules){if($r.AccessControlType-ne'Allow'-or$r.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Unit ACL'}}} +function WriteNew($path,$value){$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 16 -Compress));$f=[IO.File]::Open($path,'CreateNew','Write','Read');try{$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +function ReadPrivate($path){Safe $path $false;if((Get-Item -LiteralPath $path).Length-gt196608){throw 'Unit report bound'};return (Get-Content -LiteralPath $path -Raw|ConvertFrom-Json)} +Safe $root $true +if($p.action-eq'read'){ + if(!(Test-Path -LiteralPath $case)){@{case_exists=$false;completed=$false;readback_only=$true}|ConvertTo-Json -Compress;exit 0} + Safe $case $true;$i=ReadPrivate (Join-Path $case 'intent.json');if($i.manifest_sha256-cne$p.manifest_sha256){throw 'Original unit intent'} + $result=Join-Path $case 'result.json';if(Test-Path -LiteralPath $result){ReadPrivate $result|ConvertTo-Json -Depth 16 -Compress;exit 0} + $lists=@{};$reports=@{};foreach($kind in @('lib','bin')){$file=Join-Path $case ('list-'+$kind+'.json');if(Test-Path -LiteralPath $file){$lists[$kind]=ReadPrivate $file};$file=Join-Path $case ('report-'+$kind+'.json');if(Test-Path -LiteralPath $file){$reports[$kind]=ReadPrivate $file}} + @{case_exists=$true;completed=$false;readback_only=$true;started=(Test-Path -LiteralPath (Join-Path $case 'started.json'));lists=$lists;reports=$reports}|ConvertTo-Json -Depth 16 -Compress;exit 0 +} +if(Test-Path -LiteralPath $case){throw 'Existing unit case: readback only'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p +function Run($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 process start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$timed=$false;if(!$c.WaitForExit(30000)){$timed=$true;$c.Kill();if(!$c.WaitForExit(2000)){throw 'Own unit process did not exit'}};if(!$o.Wait(2000)-or!$e.Wait(2000)-or$o.Result.Length-gt65536-or$e.Result.Length-gt65536){throw 'Bounded unit output collection'};return @{exit_code=$c.ExitCode;timed_out=$timed;stdout=$o.Result;stderr=$e.Result}}finally{$c.Dispose()}} +$lists=@{};$paths=@{} +foreach($kind in @('lib','bin')){ + $a=$p.artifacts.$kind;if($a.sha256-notmatch'^[a-f0-9]{64}$'-or$a.name-cne('windows-tests-'+$kind+'.exe')){throw 'Fixed test artifact'};$folder=Join-Path $root $a.sha256;Safe $folder $true;$exe=Join-Path $folder $a.name;Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$a.sha256-or(Get-Item -LiteralPath $exe).Length-ne$a.bytes){throw 'Exact unit artifact'} + $listed=Run $exe '--list --format terse';[string[]]$names=@($listed.stdout-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''});[Array]::Sort($names,[StringComparer]::Ordinal) + $value=@{sha256=$a.sha256;names=$names;listed=$listed};WriteNew (Join-Path $case ('list-'+$kind+'.json')) $value + if($listed.exit_code-ne0-or$listed.timed_out-or$names.Count-ne$p.counts.$kind-or(($names-join"`n")-cne(@($p.test_names.$kind)-join"`n"))){throw 'Exact complete native list before ANY tests'};$lists[$kind]=$value;$paths[$kind]=$exe +} +WriteNew (Join-Path $case 'both-lists-verified.json') @{manifest_sha256=$p.manifest_sha256;both_lists_verified_before_any_test=$true} +WriteNew (Join-Path $case 'started.json') @{start_once=$true};$reports=@{} +foreach($kind in @('lib','bin')){$run=Run $paths[$kind] '--test-threads=1';$value=@{sha256=$lists[$kind].sha256;names=$lists[$kind].names;listed=$lists[$kind].listed;result=$run};WriteNew (Join-Path $case ('report-'+$kind+'.json')) $value;$reports[$kind]=$value;if($run.exit_code-ne0-or$run.timed_out){throw 'Native unit execution failed; readback only'}} +$value=@{completed=$true;manifest_sha256=$p.manifest_sha256;reports=$reports;both_lists_verified_before_any_test=$true;bootstrap_administrator_units=$true;local_service_proof=$false;native_pipe_proof=$false;rmm_jobs=$false;product_state_writes=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 16 -Compress +''' + + +def verified(path, current=True): + path = private_file(path) + require(path.parent.parent == BUILD and path.parent.name.startswith('build-') + and str(uuid.UUID(path.parent.name[6:])) == path.parent.name[6:], 'fixed private build namespace') + value = cb.contract.protected_json(path) + require(value['type'] == 'rmm.windows.pipe-v2-build' and value['schema_version'] == 1 + and value['source_before_after_equal'] is True and value['production_clock_qualified'] is False + and value['transport_version'] == 2 and value['raw_v1_controls_qualified'] is False + and value['native_executed'] is False + and value['reused_local_qualification'] == {'path': str(LOCAL), 'sha256': LOCAL_SHA} + and value['native_counts_expected'] == COUNTS and value['test_names_expected'] == TESTS + and value['code_root'] == str(CODE) and value['access_root'] == str(ACCESS), 'exact V2 build profile') + if current: + require(value['source_files'] == sources() and value['core_native_proofs'] == core_proof(), 'current source/Core proof binding') + require(set(value['artifacts']) == set(NAMES), 'six exact artifacts') + for kind, item in value['artifacts'].items(): + output, original = private_file(item['path']), private_file(item['original_link_path']) + require(output == path.parent / NAMES[kind] and original == path.parent / (NAMES[kind] + '.linked') + and sha(output) == item['sha256'] and output.stat().st_size == item['bytes'] + and sha(original) == item['original_link_sha256'] + and item['release_build'] is (kind not in COUNTS) + and item['debug_stripped'] is (kind in COUNTS), 'exact retained delivery and original link') + return value + + +def native(path, ident=None): + path = path.resolve() + manifest = verified(path, current=ident is None) + if ident is None: + ident = str(uuid.uuid4());folder = NATIVE / ident;folder.mkdir(mode=0o700, parents=True) + save(folder / 'intent.json', {'id': ident, 'manifest_path': str(path), 'manifest_sha256': sha(path), + 'runner_sha256': sha(__file__), 'script_sha256': hashlib.sha256(SCRIPT.encode()).hexdigest()}) + before = {'counts': auto.scopes(), 'flags': auto.status()} + require(auto.flags_disabled(before['flags']), 'closed fault flags before native units') + save(folder / 'server-before.json', before) + for kind in COUNTS: + previous.upload(manifest['artifacts'][kind]) + save(folder / 'started-local.json', {'start_once': True}) + action = 'run' + else: + require(str(uuid.UUID(ident)) == ident, 'canonical original native case') + folder = NATIVE / ident;intent = cb.contract.protected_json(folder / 'intent.json') + require(intent['manifest_sha256'] == sha(path) and intent['manifest_path'] == str(path) + and intent['runner_sha256'] == sha(__file__) + and intent['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(), 'same original case; readback only') + action = 'read' + result = cb.powershell(SCRIPT, {'id': ident, 'action': action, 'manifest_sha256': sha(path), + 'artifacts': {k: manifest['artifacts'][k] for k in COUNTS}, + 'counts': COUNTS, 'test_names': TESTS}, timeout=150) + save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) + if result.get('completed') is not True: + print(json.dumps({'verified': False, 'case_id': ident, 'readback_only': True, 'completed': False, + 'case_exists': result.get('case_exists'), 'tests_repeated': False})) + return + require(result['manifest_sha256'] == sha(path) and set(result['reports']) == set(COUNTS) + and result['both_lists_verified_before_any_test'] is True + and result['rmm_jobs'] is False and result['product_state_writes'] is False + and result['local_service_proof'] is False and result['native_pipe_proof'] is False, + 'actual narrow unit result, no runtime proof') + for kind, actual in result['reports'].items(): + run, listed = actual['result'], actual['listed'] + names = re.findall(r'^(.+): test\r?$', listed['stdout'], re.M) + require(actual['sha256'] == manifest['artifacts'][kind]['sha256'] + and names == actual['names'] == TESTS[kind] and len(set(names)) == COUNTS[kind] + and listed['exit_code'] == run['exit_code'] == 0 and not listed['timed_out'] and not run['timed_out'] + and re.search(r'test result: ok\. ' + str(COUNTS[kind]) + + r' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', run['stdout']), + 'every exact named native unit passed, none skipped/filtered') + before = cb.contract.protected_json(folder / 'server-before.json') + require(auto.scopes() == before['counts'] and auto.status() == before['flags'], 'both central counts and closed flags unchanged') + if action == 'run': + verified(path) # Full before/after source and retained-artifact check. + proof = {'verified': True, 'case_id': ident, 'manifest_path': str(path), 'manifest_sha256': sha(path), + 'native': result, 'no_jobs': True, 'clock_adapter_qualified': False, + 'local_service_proof': False, 'native_pipe_proof': False} + destination = folder / 'passed.json' + if destination.exists(): + require(cb.contract.protected_json(destination) == proof, 'retain exact original proof') + else: + save(destination, proof) + print(json.dumps({'verified': True, 'case_id': ident, 'tests': COUNTS, 'proof': str(destination)})) + + +def main(): + os.umask(0o077) + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('phase', choices=('build-local', 'native-units', 'readback')) + parser.add_argument('--access-root', required=True, type=Path) + parser.add_argument('--manifest', type=Path) + parser.add_argument('--case') + args = parser.parse_args() + require(args.access_root == ACCESS and not args.access_root.is_symlink(), 'exact existing access root') + if args.phase == 'build-local': + require(args.manifest is None and args.case is None, 'build only') + build() + else: + require(args.manifest is not None and ((args.phase == 'readback') == bool(args.case)), 'explicit manifest and original readback case') + native(args.manifest, args.case) + + +if __name__ == '__main__': + try: + main() + except Exception as error: + print('WINDOWS_PIPE_V2_QUALIFICATION_INCOMPLETE:' + type(error).__name__) + raise SystemExit(2) from None diff --git a/deploy/windows_eventlog_acceptance.py b/deploy/windows_eventlog_acceptance.py new file mode 100644 index 0000000..47755d4 --- /dev/null +++ b/deploy/windows_eventlog_acceptance.py @@ -0,0 +1,503 @@ +#!/usr/bin/env python3 +"""M3 Event Log pure contracts: Linux VM FIRST, then Windows VM. + +Only two fixed test executables run in separate bootstrap scratch directories. +No RMM job, daemon, live database, PKI, policy, enrollment or capability change. +Native execution is explicit; any lost reply permits same-case readback only. +""" +import argparse +import base64 +import datetime as dt +import hashlib +import inspect +import json +import os +from pathlib import Path +import re +import shlex +import shutil +import stat +import subprocess +import uuid + +import vm_access_check as access + +ROOT = Path(__file__).resolve().parents[1] +STATE = ROOT / 'devstate/windows-eventlog-contracts' +IMAGE = 'rust-discovery-durable-final-build:local' +PROFILE = 'windows-eventlog-contracts-v1' +LINUX_REMOTE = '/var/lib/rmm-windows-eventlog-contracts' +KINDS = ('domain', 'contracts') +REVIEWED_M3 = {'crates/agent-core/src/auth.rs': '360e6af153d7d5b31717e0f8fbc0e988850c7fa032eaa3a657962187f5a657e4', + 'crates/agent-core/tests/contracts.rs': '2482928f45e3236ae079329668849f5ccd2e01503e56cd3b8e37579af3f21e9c', + 'crates/agent-windows/Cargo.toml': '886f0ee1bc8b02133325bc1f1c4db76db3f0194435fba151eb1f3ca42e0660dc', + 'crates/agent-windows/src/actions.rs': 'a13e3746ee05619b993b6828e9c7b103fa5d3f9d47f1b52de595da3c57910180', + 'crates/agent-windows/src/event_log.rs': '00756bc178c5e67eca0de64fe55f6a1b3865e27d33f3e1c87fa575b5be81706d', + 'crates/agent-windows/src/event_log_job.rs': '08798cf194c405e93cd5874517857a06649aa47a0cc8c933fbd924018670113e', + 'crates/agent-windows/src/job_binding.rs': '488bab9f74b84023cf5b8f35f6f624903d98a82e886ef78d7ab93c8d99281e26', + 'crates/agent-windows/src/lib.rs': '391764708bfd6a8ab5546ef299d40f54dc4eefa02d4334dead7a16e46a22cd66', + 'crates/agent-windows/src/runtime.rs': '21c09cbc2a0b4caf5a4e273a20eac63dc870bb942a3683fb5b7ba6f7b36b4674', + 'crates/domain/src/jobs.rs': 'b9950270fc77086d06b3f7aa177aba2ad13155c10653b3ee73a2be436365cf1f', + 'crates/server/src/job_gateway.rs': '234542f4783c95cfcfd567495bfb7233f2c05a1b88dd87fb85a916d32eae5fc5', + 'crates/server/src/jobs.rs': '5bff6fe61fce601cd03721cebd82bca05b5af83d3640bf862ba6d603ceb7e3e9'} +COUNTS = {'linux': {'domain': 17, 'contracts': 7}, 'windows': {'domain': 17, 'contracts': 6}} +DOMAIN_NAMES = sorted(('apt_transaction::tests::closure_bytes_archives_and_code_are_bound', + 'apt_transaction::tests::helper_schema_two_roundtrip_and_strict_action_binding', + 'inventory_document::tests::additive_metadata_and_bounds', + 'inventory_document::tests::exact_linux_recovery_scope_does_not_admit_other_hyphens_or_windows', + 'inventory_document::tests::windows_scope_does_not_weaken_linux_or_accept_platform_spoofing', + 'jobs::tests::additive_recipe_refs_preserve_legacy_plan_bytes', 'jobs::tests::codec_golden_and_exact_script_bytes', + 'jobs::tests::no_terminal_replay_or_unknown_restart', 'jobs::tests::permit_codec_golden_all_bindings', + 'jobs::tests::updates_require_exact_versions_and_never_implicit_reboot', + 'jobs::tests::windows_event_log_fixture_scope_limits_and_exact_24h_preserve_linux_bytes', + 'jobs::tests::windows_interpreter_and_capability_contract', + 'jobs::tests::windows_remote_signed_profile_is_optional_exact_and_capability_bound', + 'jobs::tests::windows_system_scm_is_explicit_and_preserves_linux_service_bytes', + 'reconciliation::tests::outcome_codec_keeps_raw_output_and_sorts_evidence', + 'reconciliation::tests::reconciliation_cannot_be_a_permit_or_unbounded_ticket', + 'tests::production_never_accepts_development_auth')) +CONTRACT_NAMES = sorted(('atomic_begin_execution_rolls_back_both_rows_on_missing_witness_or_start_insert_failure', + 'completion_cas_preserves_original_bytes_and_recovers_only_verified_bound_evidence', + 'consumed_attempt_and_unknown_lock_survive_reopen_without_reexecution', + 'exact_permit_bytes_time_and_all_identity_hash_bindings_are_required', + 'historical_request_and_binding_codecs_keep_field_order_arrays_and_strictness', + 'openssl_and_portable_ed25519_agree_on_current_keys_bytes_and_tampering', + 'portable_acceptance_requires_platform_capability_and_exact_redelivery')) +UNIX_ONLY = 'openssl_and_portable_ed25519_agree_on_current_keys_bytes_and_tampering' +EXPECTED = {'linux': {'domain': DOMAIN_NAMES, 'contracts': CONTRACT_NAMES}, + 'windows': {'domain': DOMAIN_NAMES, 'contracts': [x for x in CONTRACT_NAMES if x != UNIX_ONLY]}} + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def object_pairs(items): + value = {} + for key, item in items: + require(key not in value, 'duplicate JSON field') + value[key] = item + return value + + +def private(path, directory=False): + item = Path(path).lstat() + require(not stat.S_ISLNK(item.st_mode) and item.st_uid == os.getuid() + and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600) + and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1), + 'private owned regular path') + + +def load(path): + private(path) + require(Path(path).stat().st_size <= 2 * 1024 * 1024, 'bounded private JSON') + return json.loads(Path(path).read_bytes(), object_pairs_hook=object_pairs) + + +def save(path, value): + private(Path(path).parent, True) + fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW | os.O_WRONLY, 0o600) + with os.fdopen(fd, 'w') as stream: + json.dump(value, stream, indent=2); stream.write('\n'); stream.flush(); os.fsync(stream.fileno()) + + +def setup_state(): + os.umask(0o077) + STATE.parent.mkdir(mode=0o700, exist_ok=True) + metadata = STATE.parent.lstat() + require(stat.S_ISDIR(metadata.st_mode) and metadata.st_uid == os.getuid() and not metadata.st_mode & 0o002, + 'owned non-world-writable devstate parent') + STATE.mkdir(mode=0o700, exist_ok=True) + private(STATE, True) + + +def source_files(): + paths = [ROOT / p for p in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', + 'deploy/windows_eventlog_acceptance.py', 'deploy/remote_client_bootstrap.py', + 'deploy/vm_access_check.py', 'deploy/vm_mvp_acceptance.py', 'deploy/windows/client_bootstrap.py')] + paths += list((ROOT / 'crates').rglob('Cargo.toml')) + for name in ('domain', 'agent-core', 'protocol', 'server', 'agent-windows'): + paths += [p for p in (ROOT / 'crates' / name).rglob('*') if p.is_file()] + paths += [p for p in (ROOT / 'proto').rglob('*') if p.is_file()] + for path in paths: + require(not path.is_symlink() and path.is_file(), 'regular source inputs only') + value = {str(p.relative_to(ROOT)): sha(p) for p in sorted(set(paths))} + require(all(value[name] == digest for name, digest in REVIEWED_M3.items()), 'exact frozen reviewed M3 sources') + return value + + +def names(raw): + return sorted(re.findall(r'^([A-Za-z0-9_:]+): test\r?$', raw, re.M)) + + +def check_list(raw, expected): + require(names(raw) == expected and len(set(expected)) == len(expected), 'all exact test names before execution') + require(all(not line.strip() or re.fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in raw.splitlines()), + 'terse list contains only exact tests, no benchmarks or extra entries') + + +def check_output(raw, count): + require(re.search(rb'^test result: ok\. ' + str(count).encode() + + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;[^\r\n]*\r?$', raw, re.M), + 'all tests passed without skipped or filtered tests') + + +def command(args, folder, timeout=300): + index = len(list(folder.glob('command-*.json'))) + try: + done = subprocess.run(args, capture_output=True, timeout=timeout) + save(folder / ('command-%03d.json' % index), dict(returncode=done.returncode, + stdout_b64=base64.b64encode(done.stdout).decode(), stderr_b64=base64.b64encode(done.stderr).decode())) + require(done.returncode == 0, 'local qualification command failed; private output retained') + return done.stdout + except subprocess.TimeoutExpired as error: + save(folder / ('command-%03d.json' % index), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('local qualification timed out; no automatic retry') from None + + +def scripts(): + return {'linux_case_sha256': hashlib.sha256(inspect.getsource(linux_case).encode()).hexdigest(), + 'windows_script_sha256': hashlib.sha256(WINDOWS.encode()).hexdigest()} + + +def build(): + setup_state() + folder = STATE / ('build-' + str(uuid.uuid4())); folder.mkdir(mode=0o700) + before = source_files(); save(folder / 'source-before.json', before) + image = command(['docker', 'image', 'inspect', '--format', '{{.Id}}', IMAGE], folder).decode().strip() + require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable local build image') + target = STATE / 'target'; target.mkdir(mode=0o700, exist_ok=True); private(target, 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, triple in [('linux', 'x86_64-unknown-linux-gnu'), ('windows', 'x86_64-pc-windows-gnu')]: + for kind in KINDS: + package, selection = ('rmm-domain', ['--lib']) if kind == 'domain' else ('rmm-agent-core', ['--test', 'contracts']) + options = ['--manifest-path', '/src/Cargo.toml', '--locked', '--offline', '--target', triple, '-p', package] + command(common + ['clippy'] + options + selection + ['--', '-D', 'warnings'], folder) + raw = command(common + ['test'] + options + selection + ['--release', '--no-run', '--message-format=json'], folder) + linked = [] + for line in raw.splitlines(): + try: item = json.loads(line) + except ValueError: continue + expected_name = 'rmm_domain' if kind == 'domain' else 'contracts' + if item.get('reason') == 'compiler-artifact' and item.get('executable') and item.get('profile', {}).get('test') and item['target']['name'] == expected_name: + linked.append(target / Path(item['executable']).relative_to('/target')) + require(len(linked) == 1, 'exact selected unit executable') + destination = folder / platform; destination.mkdir(mode=0o700, exist_ok=True) + original = destination / (kind + '.linked') + delivery = destination / (kind + ('.exe' if platform == 'windows' else '')) + for path in (original, delivery): shutil.copyfile(linked[0], path); path.chmod(0o600) + linked_sha = sha(original) + tool = 'x86_64-w64-mingw32-strip' if platform == 'windows' else 'strip' + command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', str(destination) + ':/artifacts', + '--entrypoint', tool, image, '--strip-debug', '/artifacts/' + delivery.name], folder) + require(sha(linked[0]) == sha(original) == linked_sha, 'unstripped original link preserved') + data = delivery.read_bytes() + if platform == 'linux': + require(data[:6] == b'\x7fELF\x02\x01' and data[18:20] == b'\x3e\0', 'Linux x64 ELF') + else: + offset = int.from_bytes(data[60:64], 'little') + require(data[:2] == b'MZ' and data[offset:offset + 6] == b'PE\0\0\x64\x86', 'Windows x64 PE') + artifacts[platform][kind] = dict(path=str(delivery.resolve()), sha256=sha(delivery), bytes=len(data), + original_link_path=str(original.resolve()), original_link_sha256=linked_sha, debug_stripped_delivery=True) + # Run exactly the deliverable Linux bytes in an offline disposable container. + for kind, item in artifacts['linux'].items(): + invoke = ['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,exec,size=128m', + '-v', item['path'] + ':/unit:ro', '--entrypoint', 'python3', image, '-c'] + for mode in ('list', 'run'): + args = ['--list', '--format', 'terse'] if mode == 'list' else ['--test-threads=1'] + script = 'import shutil,os,subprocess;shutil.copyfile("/unit","/tmp/unit");os.chmod("/tmp/unit",0o500);subprocess.run(["/tmp/unit"]+' + repr(args) + ',check=True,cwd="/tmp",env={"PATH":"/usr/bin:/bin","LANG":"C.UTF-8","TMPDIR":"/tmp","RUST_BACKTRACE":"0"})' + raw = command(invoke + [script], folder) + save(folder / ('local-linux-' + kind + '-' + mode + '.json'), dict(stdout_b64=base64.b64encode(raw).decode())) + if mode == 'list': check_list(raw.decode(), EXPECTED['linux'][kind]) + else: check_output(raw, COUNTS['linux'][kind]) + dynamic = command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', item['path'] + ':/unit:ro', + '--entrypoint', 'readelf', image, '-d', '/unit'], folder).decode() + versions = command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', item['path'] + ':/unit:ro', + '--entrypoint', 'readelf', image, '--version-info', '/unit'], folder).decode() + glibc = sorted(set(re.findall(r'GLIBC_(\d+\.\d+)', versions)), key=lambda x: tuple(map(int, x.split('.')))) + require(glibc and tuple(map(int, glibc[-1].split('.'))) <= (2, 39), 'VM-compatible glibc requirement') + item['glibc_max'] = glibc[-1]; item['needed'] = sorted(re.findall(r'Shared library: \[([^\]]+)\]', dynamic)) + require(source_files() == before, 'source and runner unchanged through qualification') + manifest = dict(schema_version=1, profile=PROFILE, source_files=before, source_before_after_equal=True, + reviewed_m3_source_pins=REVIEWED_M3, m3_adapter_sources_hash_bound_only=True, live_eventlog_query=False, + runner_sha256=sha(__file__), scripts=scripts(), test_names=EXPECTED, test_counts=COUNTS, artifacts=artifacts, + build_image_id=image, both_targets_clippy_D_warnings=True, linux_lists_actual=True, + windows_lists_expected_until_native=True, local_linux_tests_passed=24, native_linux_executed=False, + native_windows_executed=False, product_state_writes=False, rmm_jobs=False) + save(folder / 'manifest.json', manifest) + print(json.dumps(dict(manifest=str((folder / 'manifest.json').resolve()), manifest_sha256=sha(folder / 'manifest.json'), + linux_tests=24, windows_tests_expected=23, native_executed=False))) + + +def verified(path, new_run): + path = path.absolute(); private(path.parent, True) + require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}', path.parent.name), 'fixed build manifest path') + value = load(path) + require(value['schema_version'] == 1 and value['profile'] == PROFILE and value['runner_sha256'] == sha(__file__) + and value['scripts'] == scripts() and value['test_names'] == EXPECTED and value['test_counts'] == COUNTS + and value['source_before_after_equal'] is True and value['both_targets_clippy_D_warnings'] is True + and value['reviewed_m3_source_pins'] == REVIEWED_M3 and value['m3_adapter_sources_hash_bound_only'] is True + and value['live_eventlog_query'] is False, + 'original manifest/runner/remote scripts/profile binding') + if new_run: require(value['source_files'] == source_files(), 'new execution requires same current qualified source') + for platform in COUNTS: + require(set(value['artifacts'][platform]) == set(KINDS), 'exact two artifacts') + for kind, item in value['artifacts'][platform].items(): + suffix = '.exe' if platform == 'windows' else '' + require(Path(item['path']) == path.parent / platform / (kind + suffix) + and Path(item['original_link_path']) == path.parent / platform / (kind + '.linked'), 'fixed private artifact paths') + for field, digest in [('path', item['sha256']), ('original_link_path', item['original_link_sha256'])]: + private(item[field]); require(sha(item[field]) == digest, 'immutable original/delivery bytes') + require(Path(item['path']).stat().st_size == item['bytes'], 'artifact size unchanged') + return value + + +def access_settings(root, platform): + """Read strictly parsed existing access only when the parent executes native.""" + require(root is not None and not root.is_symlink(), 'explicit real access-root required') + root = root.absolute(); require(root.is_dir() and root.resolve() == root, 'canonical access-root') + if platform == 'linux': + cfg = access.load_config(root / 'credentials/linux-vm.conf') + require(cfg['address'] == '116.203.20.85' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed Cloud Linux bootstrap target') + prefix = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()), 'root@116.203.20.85'] + else: + cfg = access.load_config(root / 'credentials/server.conf') + require(cfg['address'] == '88.99.80.202' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed server reverse-jump only') + credentials = root / 'credentials'; private(credentials, True) + record = load(credentials / 'windows-dev-access.json') + require(set(record) == {'ssh_prefix', 'expires_at'}, 'known Windows access schema') + expires = dt.datetime.fromisoformat(record['expires_at']) + require(expires.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expires, 'existing dev access still valid') + key = str(Path(cfg['ssh_key_path']).expanduser()) + jump = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-i', key, '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'ForwardAgent=no', '-o', 'IdentitiesOnly=yes', '-W', '127.0.0.1:22240', 'root@88.99.80.202'] + prefix = ['ssh', '-F', '/dev/null', '-T', '-i', key, '-o', 'ProxyCommand=' + shlex.join(jump), + '-o', 'HostKeyAlias=rmm-windows-dev', '-o', 'UserKnownHostsFile=' + str(credentials / 'windows-dev.known_hosts'), + '-o', 'GlobalKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', 'rmm-dev@127.0.0.1'] + require(record['ssh_prefix'] == prefix, 'exact registered pinned reverse SSH prefix') + private(credentials / 'windows-dev.known_hosts') + return prefix + + +def transport(prefix, command_text, data, folder, timeout=60): + """No stdout/stderr escapes the private case directory on transport failure.""" + try: + result = subprocess.run(prefix + [command_text], input=data, capture_output=True, timeout=timeout) + save(folder / ('transport-' + str(uuid.uuid4()) + '.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'native transport incomplete; same-case readback only') + value = json.loads(result.stdout.decode('utf-8-sig'), object_pairs_hook=object_pairs) + require(isinstance(value, dict) and 'bootstrap_error' not in value, 'native bootstrap incomplete; private receipt retained') + return value + except subprocess.TimeoutExpired as error: + save(folder / ('transport-timeout-' + str(uuid.uuid4()) + '.json'), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('native reply lost; read original case only') from None + + +def linux_rpc(prefix, payload, folder): + code = 'import os,json,subprocess,base64,hashlib,uuid\nfrom pathlib import Path\n' + inspect.getsource(require) + '\n' + inspect.getsource(linux_case) + code += '\ntry:\n print(json.dumps(linux_case(' + repr(payload) + ')))\nexcept Exception as e:\n print(json.dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n' + return transport(prefix, 'python3 -', code.encode(), folder) + + +def windows_rpc(prefix, payload, folder): + script = 'try {\n' + WINDOWS + '\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}' + command_text = 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand ' + base64.b64encode(script.encode('utf-16le')).decode() + return transport(prefix, command_text, (json.dumps(payload) + '\n').encode(), folder) + + +def upload(prefix, item, destination, folder, kind): + args = ['sftp'] + [part for part in prefix[1:-1] if part != '-T'] + ['-b', '-', prefix[-1]] + # Linux sftp uses uppercase -P; Windows prefix has no -p option. + if '-p' in args: args[args.index('-p')] = '-P' + batch = ('put ' + json.dumps(item['path']) + ' ' + json.dumps(destination) + '\n').encode() + save(folder / ('transfer-' + kind + '-intent.json'), dict(sha256=item['sha256'], bytes=item['bytes'])) + try: + result = subprocess.run(args, input=batch, capture_output=True, timeout=60) + save(folder / ('transfer-' + kind + '-result.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'fixed artifact transfer failed; no retry') + except subprocess.TimeoutExpired as error: + save(folder / ('transfer-' + kind + '-timeout.json'), dict(timed_out=True, + stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('artifact transfer incomplete; no retry') from None + + +def linux_case(p): + import resource + import stat + ident = str(uuid.UUID(p['id'])) + require(ident == p['id'] and p['action'] in ('prepare', 'run', 'read') and p['profile'] == 'windows-eventlog-contracts-v1', 'fixed unit case') + require(os.geteuid() == 0 and os.uname().machine == 'x86_64' and set(p['artifacts']) == {'domain', 'contracts'}, 'fixed Linux root qualification') + root = Path('/var/lib/rmm-windows-eventlog-contracts') + def safe(path, directory): + s = path.lstat() + require(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 unit intent') + if p['action'] == 'read': + if not (case / 'result.json').exists(): return dict(id=ident, status='started_without_result' if (case / 'started.json').exists() else 'prepared_without_start') + safe(case / 'result.json', False); 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/bin:/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, (15, 15)); resource.setrlimit(resource.RLIMIT_FSIZE, (1048576, 1048576)) + 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 ('domain', 'contracts'): + binary = case / kind; safe(binary, False) + require(hashlib.sha256(binary.read_bytes()).hexdigest() == p['artifacts'][kind]['sha256'], 'exact Linux delivery') + binary.chmod(0o500) + listed = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--list', '--format', 'terse'], cwd=case, env=env, capture_output=True, timeout=5, 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')) + require(all(not line.strip() or __import__('re').fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in listed.stdout.decode().splitlines()) + and actual == p['names'][kind] and len(actual) == {'domain': 17, 'contracts': 7}[kind], 'exact native test names before ANY test execution') + listings[kind] = actual + reports = {} + for kind in ('domain', 'contracts'): + with (case / (kind + '-stdout.bin')).open('xb') as out, (case / (kind + '-stderr.bin')).open('xb') as err: + timed = False + 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=18, 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]) + value = dict(id=ident, platform='linux', profile=p['profile'], manifest_sha256=p['manifest_sha256'], runner_sha256=p['runner_sha256'], + scripts=p['scripts'], reports=reports, network_namespace=True, environment_cleared=True, product_state_writes=False, rmm_jobs=False) + write(case / 'result.json', value); return value + + +WINDOWS = r''' +$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json) +if([string]([guid]$p.id)-cne$p.id-or$p.action-notin@('prepare','run','read')-or$p.profile-cne'windows-eventlog-contracts-v1'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.linux_proof_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed Linux-first profile case'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('windows-eventlog-contracts-'+$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 'Unit path type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private unit owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private unit ACL'}}} +function WriteNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 16 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +function MatchIntent($old){if($old.id-cne$p.id-or$old.profile-cne$p.profile-or$old.manifest_sha256-cne$p.manifest_sha256-or$old.runner_sha256-cne$p.runner_sha256-or$old.linux_proof_sha256-cne$p.linux_proof_sha256-or$old.scripts.linux_case_sha256-cne$p.scripts.linux_case_sha256-or$old.scripts.windows_script_sha256-cne$p.scripts.windows_script_sha256){throw 'Original profile intent'};foreach($kind in @('domain','contracts')){if($old.artifacts.$kind.sha256-cne$p.artifacts.$kind.sha256-or$old.artifacts.$kind.bytes-ne$p.artifacts.$kind.bytes-or(@($old.names.$kind)-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'Original artifact/test tuple'}}} +if($p.action-eq'read'-and!(Test-Path -LiteralPath $case)){@{id=$p.id;status='remote_case_absent'}|ConvertTo-Json -Compress;exit 0} +Safe $root $true +if($p.action-eq'prepare'){if(Test-Path -LiteralPath $case){throw 'Existing case retained'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p;@{id=$p.id;prepared=$true}|ConvertTo-Json -Compress;exit 0} +Safe $case $true;$intent=Join-Path $case 'intent.json';Safe $intent $false;MatchIntent (Get-Content -LiteralPath $intent -Raw|ConvertFrom-Json) +if($p.action-eq'read'){$result=Join-Path $case 'result.json';if(!(Test-Path -LiteralPath $result)){$status=if(Test-Path -LiteralPath (Join-Path $case 'started.json')){'started_without_result'}else{'prepared_without_start'};@{id=$p.id;status=$status}|ConvertTo-Json -Compress;exit 0};Safe $result $false;Get-Content -LiteralPath $result -Raw;exit 0} +function RunUnit($exe,[string]$arguments){$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=$exe;$s.Arguments=$arguments;$s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.WorkingDirectory=$case;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$systemRoot=$env:WINDIR;$s.EnvironmentVariables.Clear();$s.EnvironmentVariables['SystemRoot']=$systemRoot;$s.EnvironmentVariables['WINDIR']=$systemRoot;$s.EnvironmentVariables['PATH']=$systemRoot+'\System32';$s.EnvironmentVariables['TEMP']=$script:temp;$s.EnvironmentVariables['TMP']=$script:temp;$s.EnvironmentVariables['RUST_BACKTRACE']='0';$c=New-Object Diagnostics.Process;$c.StartInfo=$s;if(!$c.Start()){throw 'Unit start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$timed=$false;if(!$c.WaitForExit(18000)){$timed=$true;$c.Kill();if(!$c.WaitForExit(2000)){throw 'Own unit process did not exit'}};if(!$o.Wait(2000)-or!$e.Wait(2000)-or$o.Result.Length-gt1048576-or$e.Result.Length-gt1048576){throw 'Bounded unit output'};return @{exit_code=$c.ExitCode;timed_out=$timed;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') @{execute_once=$true;manifest_sha256=$p.manifest_sha256};$script:temp=Join-Path $case 'tmp';New-Item -ItemType Directory -Path $script:temp|Out-Null;Safe $script:temp $true +$lists=@{};$executables=@{} +foreach($kind in @('domain','contracts')){$item=$p.artifacts.$kind;if($item.sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed unit hash'};$exe=Join-Path $case ($kind+'.exe');Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$item.sha256-or(Get-Item -LiteralPath $exe).Length-ne$item.bytes){throw 'Exact unit bytes'};$list=RunUnit $exe '--list --format terse';if($list.exit_code-ne0-or$list.timed_out){throw 'Actual Windows listing'};$text=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($list.stdout_b64));$unexpected=@($text-split'\r?\n'|Where-Object{$_.Trim()-and$_-notmatch'^[A-Za-z0-9_:]+: test$'});if($unexpected.Count){throw 'Only exact terse test entries'};$actual=@($text-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''}|Sort-Object);$count=if($kind-eq'domain'){17}else{6};if($actual.Count-ne$count-or($actual-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'All exact names BEFORE any tests'};$lists[$kind]=$actual;$executables[$kind]=$exe} +$reports=@{};foreach($kind in @('domain','contracts')){$value=RunUnit $executables[$kind] '--test-threads=1';$value.sha256=$p.artifacts.$kind.sha256;$value.names=$lists[$kind];WriteNew (Join-Path $case ($kind+'-result.json')) $value;$reports[$kind]=$value} +$value=@{id=$p.id;platform='windows';profile=$p.profile;manifest_sha256=$p.manifest_sha256;runner_sha256=$p.runner_sha256;scripts=$p.scripts;linux_proof_sha256=$p.linux_proof_sha256;reports=$reports;bootstrap_administrator_units=$true;local_service_proof=$false;environment_cleared=$true;product_state_writes=$false;rmm_jobs=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 16 -Compress +''' + + +def check_result(manifest, platform, value, payload): + require(value.get('id') == payload['id'] and value.get('platform') == platform and value.get('profile') == PROFILE + and value.get('manifest_sha256') == payload['manifest_sha256'] and value.get('runner_sha256') == manifest['runner_sha256'] + and value.get('scripts') == manifest['scripts'] and value.get('product_state_writes') is False and value.get('rmm_jobs') is False + and value.get('environment_cleared') is True and set(value['reports']) == set(KINDS), 'complete native original binding') + if platform == 'linux': require(value.get('network_namespace') is True, 'actual Linux netns proof') + else: require(value.get('linux_proof_sha256') == payload['linux_proof_sha256'] and value.get('local_service_proof') is False, 'original Linux proof and honest account scope') + for kind, item in value['reports'].items(): + require(item['names'] == EXPECTED[platform][kind] and item['sha256'] == manifest['artifacts'][platform][kind]['sha256'] + and item['exit_code'] == 0 and item['timed_out'] is False, 'all native exact test names/artifacts/exits') + raw = base64.b64decode(item['stdout_b64'], validate=True); errors = base64.b64decode(item['stderr_b64'], validate=True) + require(len(raw) <= 1048576 and len(errors) <= 1048576, 'bounded native outputs') + check_output(raw, COUNTS[platform][kind]) + + +def execute(path, platform, access_root, case_id=None): + setup_state(); path = path.absolute(); manifest = verified(path, new_run=case_id is None) + prefix = access_settings(access_root, platform) + if case_id is None: + case_id = str(uuid.uuid4()); folder = STATE / case_id; folder.mkdir(mode=0o700) + payload = dict(id=case_id, profile=PROFILE, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], scripts=manifest['scripts'], + artifacts={kind: {k: manifest['artifacts'][platform][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS}, names=EXPECTED[platform]) + if platform == 'windows': + proof_path = path.parent / 'linux-passed.json'; proof = load(proof_path) + require(proof['verified'] is True and proof['manifest_sha256'] == sha(path), 'same-manifest real Linux proof FIRST') + linux_payload = dict(payload, id=proof['case_id'], artifacts={kind: {k: manifest['artifacts']['linux'][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS}, names=EXPECTED['linux']) + check_result(manifest, 'linux', proof['native'], linux_payload) + payload['linux_proof_sha256'] = sha(proof_path) + intent = dict(id=case_id, platform=platform, manifest_path=str(path), manifest_sha256=sha(path), runner_sha256=sha(__file__), + scripts=scripts(), access_root_sha256=hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), payload=payload) + save(folder / 'intent.json', intent) + save(folder / 'started-local.json', dict(create_once=True, no_retry=True)) + run_read = False + else: + require(str(uuid.UUID(case_id)) == case_id, 'original canonical case id') + folder = STATE / case_id; private(folder, True); intent = load(folder / 'intent.json'); load(folder / 'started-local.json') + require(intent['id'] == case_id and intent['platform'] == platform and intent['manifest_path'] == str(path) + and intent['manifest_sha256'] == sha(path) and intent['runner_sha256'] == sha(__file__) and intent['scripts'] == scripts() + and intent['access_root_sha256'] == hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), 'immutable same-case readback') + payload = intent['payload']; run_read = True + rpc = linux_rpc if platform == 'linux' else windows_rpc + if not run_read: + result = rpc(prefix, dict(payload, action='prepare'), folder) + save(folder / 'prepared-remote.json', result); require(result == dict(id=case_id, prepared=True), 'fresh fixed case prepared') + for kind in KINDS: + destination = LINUX_REMOTE + '/' + case_id + '/' + kind if platform == 'linux' else '/C:/ProgramData/RustDiscoveryBootstrap/windows-eventlog-contracts-' + case_id + '/' + kind + '.exe' + upload(prefix, manifest['artifacts'][platform][kind], destination, folder, kind) + result = rpc(prefix, dict(payload, action='read' if run_read else 'run'), folder) + save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) + if result.get('status'): + require(run_read and result.get('id') == case_id and result['status'] in ('remote_case_absent', 'prepared_without_start', 'started_without_result'), 'fixed read-only incomplete status') + print(json.dumps(dict(verified=False, platform=platform, case_id=case_id, status=result['status'], retry_allowed=False))); return + check_result(manifest, platform, result, payload) + proof = dict(verified=True, case_id=case_id, platform=platform, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], native=result) + destination = path.parent / (platform + '-passed.json') + if destination.exists(): require(load(destination) == proof, 'preserve identical native proof') + else: save(destination, proof) + save(folder / 'verified.json', dict(verified=True, proof_sha256=sha(destination))) if not (folder / 'verified.json').exists() else require(load(folder / 'verified.json') == dict(verified=True, proof_sha256=sha(destination)), 'same verified receipt') + print(json.dumps(dict(verified=True, platform=platform, case_id=case_id, tests=sum(COUNTS[platform].values()), + manifest_sha256=sha(path), proof_sha256=sha(destination)))) + + +def main(): + 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('--access-root', type=Path) + 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.access_root or args.execute_native_tests), 'local build only'); build() + elif args.action.startswith('readback-'): + require(args.manifest and args.case and args.access_root and not args.execute_native_tests, 'original case readback only') + execute(args.manifest, args.action.removeprefix('readback-'), args.access_root, args.case) + else: + require(args.manifest and args.access_root and args.execute_native_tests and not args.case, 'explicit new native platform run') + execute(args.manifest, args.action, args.access_root) + + +if __name__ == '__main__': + try: main() + except Exception as error: + print(json.dumps(dict(error='WINDOWS_EVENTLOG_CONTRACT_QUALIFICATION_INCOMPLETE', error_class=type(error).__name__, details_suppressed=True))) + raise SystemExit(2) from None diff --git a/deploy/windows_msi_contract_acceptance.py b/deploy/windows_msi_contract_acceptance.py new file mode 100644 index 0000000..1dfcb18 --- /dev/null +++ b/deploy/windows_msi_contract_acceptance.py @@ -0,0 +1,692 @@ +#!/usr/bin/env python3 +"""M4 MSI pure common contracts: Linux VM FIRST, then Windows VM. + +Only fixed common test executables run in separate bootstrap scratch directories. +No RMM job, daemon, live database, PKI, policy, enrollment or capability change. +Native execution is explicit; any lost reply permits same-case readback only. +""" +import argparse +import base64 +import datetime as dt +import hashlib +import inspect +import json +import os +from pathlib import Path +import re +import shlex +import shutil +import stat +import subprocess +import uuid + +import vm_access_check as access + +ROOT = Path(__file__).resolve().parents[1] +STATE = ROOT / 'devstate/windows-msi-contracts' +IMAGE = 'rust-discovery-durable-final-build:local' +PROFILE = 'windows-msi-contracts-v1' +LINUX_REMOTE = '/var/lib/rmm-windows-msi-contracts' +ACCESS_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery') +BACKEND = ROOT / 'devstate/windows-msi-integration/local-qualification.json' +BACKEND_SHA = 'b7bced0a341ffb88af8b584cbe8355ff1021add541004347f609f6136a684b74' +# Actual local Linux RELEASE --list was obtained before fixing these lists. +# Windows lists remain EXPECTED until the native Windows process emits them. +REVIEWED_BACKEND = {'crates/domain/src/lib.rs': '7919c099a8b568259b9b197d1742cdc11844523c64654325b6c142ae437dda1f', + 'crates/domain/src/jobs.rs': 'ff7f086a28e5e17832fa4afc974ce8395c7887360c3291d27d1cc7ae12bab117', + 'crates/domain/src/windows_execution.rs': 'aa2c5916c9d6c9d9ab8e9bd0d1051920fcb2ae9ecf4c18cf6ee89c4668b9b460', + 'crates/agent-core/src/auth.rs': '86cda2e10321bcb3591013880f11e53a8bcb01c53521bbcc28d38d6b1f5402bd', + 'crates/agent-core/tests/contracts.rs': 'f57fe9b938b8e563229b9da7c77b6df7b419f0ba41ef05abb1f7f9e3ff2e61e9', + 'crates/server/src/agent_artifacts.rs': '5f68ea4a26de529d814b7365b85f978ced7715c7da8fa2a435021e3e953b506e', + 'crates/server/src/catalog.rs': 'c4c293271a36aba80fac00b72e2936ed2c11bccf49e511d7003bd95574fb0e04', + 'crates/server/src/job_gateway.rs': '421672d7a283fcda5bc00854b53eb86c4236621336d5cf6b72c43d288fad74de', + 'crates/server/src/jobs.rs': '725a2f9c61180c4d938bcf7383ee78c5d50b83b703665195502548140126fce6', + 'crates/server/src/main.rs': 'c8ddc2a1a7cd29122f5590902f25c2711eef6e2f5488e2e7869c034ddf27394e', + 'crates/server/src/windows_policy.rs': 'd0ffd67f4f2c56a681c44617962b3834f02cbbc6ba076461b856f4e87ce6c222', + 'crates/server/src/windows_msi_catalog.rs': 'dcb704b02a5d5d5bfff5b8e34a712cf5a0b9c32cb21af242252976d0c0b0ad84', + 'migrations/0014_windows_msi_catalog.sql': '893440fc75914d4e7e3a62055f693562c6a093924eab77a20f6b5428f63d20d0'} +EXPECTED = {'linux': {'domain': ['apt_transaction::tests::closure_bytes_archives_and_code_are_bound', + 'apt_transaction::tests::helper_schema_two_roundtrip_and_strict_action_binding', + 'inventory_document::tests::additive_metadata_and_bounds', + 'inventory_document::tests::exact_linux_recovery_scope_does_not_admit_other_hyphens_or_windows', + 'inventory_document::tests::windows_scope_does_not_weaken_linux_or_accept_platform_spoofing', + 'jobs::tests::additive_recipe_refs_preserve_legacy_plan_bytes', + 'jobs::tests::codec_golden_and_exact_script_bytes', + 'jobs::tests::no_terminal_replay_or_unknown_restart', + 'jobs::tests::permit_codec_golden_all_bindings', + 'jobs::tests::updates_require_exact_versions_and_never_implicit_reboot', + 'jobs::tests::windows_event_log_fixture_scope_limits_and_exact_24h_preserve_linux_bytes', + 'jobs::tests::windows_interpreter_and_capability_contract', + 'jobs::tests::windows_remote_signed_profile_is_optional_exact_and_capability_bound', + 'jobs::tests::windows_system_scm_is_explicit_and_preserves_linux_service_bytes', + 'reconciliation::tests::outcome_codec_keeps_raw_output_and_sorts_evidence', + 'reconciliation::tests::reconciliation_cannot_be_a_permit_or_unbounded_ticket', + 'tests::production_never_accepts_development_auth', + 'windows_execution::tests::ensure_msi_action_is_strict_opt_in_and_system_windows_only_without_linux_byte_changes', + 'windows_execution::tests::immutable_fixture_recipe_rejects_reboot_url_transform_and_implicit_upgrade', + 'windows_execution::tests::major_upgrade_requires_explicit_exact_predecessor_new_package_and_same_family'], + 'contracts': ['atomic_begin_execution_rolls_back_both_rows_on_missing_witness_or_start_insert_failure', + 'completion_cas_preserves_original_bytes_and_recovers_only_verified_bound_evidence', + 'consumed_attempt_and_unknown_lock_survive_reopen_without_reexecution', + 'exact_permit_bytes_time_and_all_identity_hash_bindings_are_required', + 'historical_msi_nonstart_keeps_full_signature_binding_and_never_becomes_root_authority', + 'historical_request_and_binding_codecs_keep_field_order_arrays_and_strictness', + 'openssl_and_portable_ed25519_agree_on_current_keys_bytes_and_tampering', + 'portable_acceptance_requires_platform_capability_and_exact_redelivery'], + 'helper-reconcile': ['abandon_after_expiry_records_deny_only_evidence_and_requires_normal_signed_unlock', + 'abandon_preserves_existing_known_unknown_missing_start_and_foreign_request_rows', + 'abandon_rejects_wrong_signature_scope_clock_boot_lineage_instance_and_dispatch_proof', + 'abandon_storage_failure_rolls_back_consumption_start_and_outcome_together', + 'both_live_profiles_apply_one_signature_and_checkpoint_without_inheriting_readiness', + 'checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor', + 'checkpoint_rollback_is_sticky_and_cannot_be_fixed_by_omission', + 'clean_ready_shared_lock_and_reopen_start_closed', + 'definitive_unlock_is_transactional_idempotent_and_keeps_tombstones', + 'exact_signatures_fake_key_wrong_domain_and_all_identity_bindings', + 'false_clock_expiry_and_invalid_independent_key_stay_closed', + 'general_and_apt_instances_share_fencing_but_never_inherit_a_process_gate', + 'independently_remembered_agent_journal_cannot_be_replaced_by_server_ready', + 'malformed_original_protobuf_or_digest_does_not_open_gate', + 'missing_signed_checkpoint_history_closes_the_live_gate_stickily', + 'mutated_root_ledger_after_capture_is_not_the_signed_snapshot', + 'old_process_response_fails_after_fresh_capture_even_inside_ttl', + 'omitted_unknown_root_row_is_sticky_despite_signed_ready', + 'real_local_boot_must_match_signed_reboot_completion', + 'root_and_system_are_distinct_explicit_helper_domains', + 'shared_database_only_and_missing_local_start_fail_closed', + 'signed_noncanonical_outcome_bytes_do_not_unlock_root_consumption', + 'stale_process_connection_does_not_poison_a_new_fenced_process', + 'targeted_final_unlock_does_not_release_an_unrelated_unknown', + 'unknown_code_or_no_result_never_unlocks_even_signed_ready', + 'unrelated_action_plan_and_previous_outcome_cannot_unlock'], + 'reconcile': ['accepted_unstarted_keeps_ids_and_never_consumes_or_executes', + 'authenticated_helper_outcome_is_imported_as_new_durable_evidence_not_reexecuted', + 'clean_reconnect_and_reopen_require_new_signed_challenge', + 'crash_after_durable_outcome_before_result_recovers_original_then_signed_sync_closes', + 'crash_after_result_commit_before_finish_retains_exact_result_and_local_prior', + 'crash_before_running_event_recovers_real_consumption_without_inventing_completion', + 'definite_nonstart_result_is_unconsumed_not_ambiguous', + 'exact_signatures_all_identity_channel_checkpoint_and_request_bindings', + 'final_result_preserves_original_envelope_outputs_and_exact_acks', + 'foreign_ack_and_noncanonical_outcome_are_transactionally_rejected', + 'helper_gate_requires_exact_signed_instruction_and_applied_checkpoint', + 'helper_omitting_a_consumed_root_attempt_is_a_sticky_restore_fault', + 'independent_helper_checkpoint_regression_is_sticky', + 'independent_local_outcome_mismatch_never_unlocks', + 'missing_server_job_fault_is_sticky_even_if_attempt_disappears_later', + 'negative_server_revision_never_rolls_back_local_unknown_revision', + 'original_completion_after_reconnect_unknown_is_recovered_without_reexecuting', + 'reboot_keeps_lock_until_signed_new_authenticated_boot_inventory_evidence', + 'requesting_running_and_omitted_attempts_cannot_regain_readiness', + 'rollback_and_false_clock_close_mutations_but_do_not_destroy_journal', + 'signed_attempt_mutation_or_terminal_regression_is_rejected', + 'signed_terminal_nonstart_stays_unconsumed_across_sync_and_pending_denial', + 'signed_unlock_still_rejects_a_missing_local_outcome_in_legacy_crash_window', + 'system_helper_import_requires_explicit_system_identity_and_never_root_alias', + 'terminal_labels_never_override_grant_consumption_or_unclear_attempts', + 'uncertain_request_preserves_pending_request_and_last_applied_anchor'], + 'linux-helper': ['both_live_profiles_apply_one_signature_and_checkpoint_without_inheriting_readiness', + 'checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor', + 'checkpoint_rollback_is_sticky_and_cannot_be_fixed_by_omission', + 'clean_ready_shared_lock_and_reopen_start_closed', + 'definitive_unlock_is_transactional_idempotent_and_keeps_tombstones', + 'exact_signatures_fake_key_wrong_domain_and_all_identity_bindings', + 'false_clock_expiry_and_invalid_independent_key_stay_closed', + 'general_and_apt_instances_share_fencing_but_never_inherit_a_process_gate', + 'independently_remembered_agent_journal_cannot_be_replaced_by_server_ready', + 'malformed_original_protobuf_or_digest_does_not_open_gate', + 'missing_signed_checkpoint_history_closes_the_live_gate_stickily', + 'mutated_root_ledger_after_capture_is_not_the_signed_snapshot', + 'old_process_response_fails_after_fresh_capture_even_inside_ttl', + 'omitted_unknown_root_row_is_sticky_despite_signed_ready', + 'real_local_boot_must_match_signed_reboot_completion', + 'shared_database_only_and_missing_local_start_fail_closed', + 'signed_noncanonical_outcome_bytes_do_not_unlock_root_consumption', + 'stale_process_connection_does_not_poison_a_new_fenced_process', + 'targeted_final_unlock_does_not_release_an_unrelated_unknown', + 'unknown_code_or_no_result_never_unlocks_even_signed_ready', + 'unrelated_action_plan_and_previous_outcome_cannot_unlock']}, + 'windows': {'domain': ['apt_transaction::tests::closure_bytes_archives_and_code_are_bound', + 'apt_transaction::tests::helper_schema_two_roundtrip_and_strict_action_binding', + 'inventory_document::tests::additive_metadata_and_bounds', + 'inventory_document::tests::exact_linux_recovery_scope_does_not_admit_other_hyphens_or_windows', + 'inventory_document::tests::windows_scope_does_not_weaken_linux_or_accept_platform_spoofing', + 'jobs::tests::additive_recipe_refs_preserve_legacy_plan_bytes', + 'jobs::tests::codec_golden_and_exact_script_bytes', + 'jobs::tests::no_terminal_replay_or_unknown_restart', + 'jobs::tests::permit_codec_golden_all_bindings', + 'jobs::tests::updates_require_exact_versions_and_never_implicit_reboot', + 'jobs::tests::windows_event_log_fixture_scope_limits_and_exact_24h_preserve_linux_bytes', + 'jobs::tests::windows_interpreter_and_capability_contract', + 'jobs::tests::windows_remote_signed_profile_is_optional_exact_and_capability_bound', + 'jobs::tests::windows_system_scm_is_explicit_and_preserves_linux_service_bytes', + 'reconciliation::tests::outcome_codec_keeps_raw_output_and_sorts_evidence', + 'reconciliation::tests::reconciliation_cannot_be_a_permit_or_unbounded_ticket', + 'tests::production_never_accepts_development_auth', + 'windows_execution::tests::ensure_msi_action_is_strict_opt_in_and_system_windows_only_without_linux_byte_changes', + 'windows_execution::tests::immutable_fixture_recipe_rejects_reboot_url_transform_and_implicit_upgrade', + 'windows_execution::tests::major_upgrade_requires_explicit_exact_predecessor_new_package_and_same_family'], + 'contracts': ['atomic_begin_execution_rolls_back_both_rows_on_missing_witness_or_start_insert_failure', + 'completion_cas_preserves_original_bytes_and_recovers_only_verified_bound_evidence', + 'consumed_attempt_and_unknown_lock_survive_reopen_without_reexecution', + 'exact_permit_bytes_time_and_all_identity_hash_bindings_are_required', + 'historical_msi_nonstart_keeps_full_signature_binding_and_never_becomes_root_authority', + 'historical_request_and_binding_codecs_keep_field_order_arrays_and_strictness', + 'portable_acceptance_requires_platform_capability_and_exact_redelivery'], + 'helper-reconcile': ['abandon_after_expiry_records_deny_only_evidence_and_requires_normal_signed_unlock', + 'abandon_preserves_existing_known_unknown_missing_start_and_foreign_request_rows', + 'abandon_rejects_wrong_signature_scope_clock_boot_lineage_instance_and_dispatch_proof', + 'abandon_storage_failure_rolls_back_consumption_start_and_outcome_together', + 'both_live_profiles_apply_one_signature_and_checkpoint_without_inheriting_readiness', + 'checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor', + 'checkpoint_rollback_is_sticky_and_cannot_be_fixed_by_omission', + 'clean_ready_shared_lock_and_reopen_start_closed', + 'definitive_unlock_is_transactional_idempotent_and_keeps_tombstones', + 'exact_signatures_fake_key_wrong_domain_and_all_identity_bindings', + 'false_clock_expiry_and_invalid_independent_key_stay_closed', + 'general_and_apt_instances_share_fencing_but_never_inherit_a_process_gate', + 'independently_remembered_agent_journal_cannot_be_replaced_by_server_ready', + 'malformed_original_protobuf_or_digest_does_not_open_gate', + 'missing_signed_checkpoint_history_closes_the_live_gate_stickily', + 'mutated_root_ledger_after_capture_is_not_the_signed_snapshot', + 'old_process_response_fails_after_fresh_capture_even_inside_ttl', + 'omitted_unknown_root_row_is_sticky_despite_signed_ready', + 'real_local_boot_must_match_signed_reboot_completion', + 'root_and_system_are_distinct_explicit_helper_domains', + 'shared_database_only_and_missing_local_start_fail_closed', + 'signed_noncanonical_outcome_bytes_do_not_unlock_root_consumption', + 'stale_process_connection_does_not_poison_a_new_fenced_process', + 'targeted_final_unlock_does_not_release_an_unrelated_unknown', + 'unknown_code_or_no_result_never_unlocks_even_signed_ready', + 'unrelated_action_plan_and_previous_outcome_cannot_unlock'], + 'reconcile': ['accepted_unstarted_keeps_ids_and_never_consumes_or_executes', + 'authenticated_helper_outcome_is_imported_as_new_durable_evidence_not_reexecuted', + 'clean_reconnect_and_reopen_require_new_signed_challenge', + 'crash_after_durable_outcome_before_result_recovers_original_then_signed_sync_closes', + 'crash_after_result_commit_before_finish_retains_exact_result_and_local_prior', + 'crash_before_running_event_recovers_real_consumption_without_inventing_completion', + 'definite_nonstart_result_is_unconsumed_not_ambiguous', + 'exact_signatures_all_identity_channel_checkpoint_and_request_bindings', + 'final_result_preserves_original_envelope_outputs_and_exact_acks', + 'foreign_ack_and_noncanonical_outcome_are_transactionally_rejected', + 'helper_gate_requires_exact_signed_instruction_and_applied_checkpoint', + 'helper_omitting_a_consumed_root_attempt_is_a_sticky_restore_fault', + 'independent_helper_checkpoint_regression_is_sticky', + 'independent_local_outcome_mismatch_never_unlocks', + 'missing_server_job_fault_is_sticky_even_if_attempt_disappears_later', + 'negative_server_revision_never_rolls_back_local_unknown_revision', + 'original_completion_after_reconnect_unknown_is_recovered_without_reexecuting', + 'reboot_keeps_lock_until_signed_new_authenticated_boot_inventory_evidence', + 'requesting_running_and_omitted_attempts_cannot_regain_readiness', + 'rollback_and_false_clock_close_mutations_but_do_not_destroy_journal', + 'signed_attempt_mutation_or_terminal_regression_is_rejected', + 'signed_terminal_nonstart_stays_unconsumed_across_sync_and_pending_denial', + 'signed_unlock_still_rejects_a_missing_local_outcome_in_legacy_crash_window', + 'system_helper_import_requires_explicit_system_identity_and_never_root_alias', + 'terminal_labels_never_override_grant_consumption_or_unclear_attempts', + 'uncertain_request_preserves_pending_request_and_last_applied_anchor']}} +COUNTS = {platform: {kind: len(names) for kind, names in lists.items()} for platform, lists in EXPECTED.items()} +KINDS = {'linux': ('domain','contracts','helper-reconcile','reconcile','linux-helper'), + 'windows': ('domain','contracts','helper-reconcile','reconcile')} + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def object_pairs(items): + value = {} + for key, item in items: + require(key not in value, 'duplicate JSON field') + value[key] = item + return value + + +def private(path, directory=False): + item = Path(path).lstat() + require(not stat.S_ISLNK(item.st_mode) and item.st_uid == os.getuid() + and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600) + and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1), + 'private owned regular path') + + +def load(path): + private(path) + require(Path(path).stat().st_size <= 2 * 1024 * 1024, 'bounded private JSON') + return json.loads(Path(path).read_bytes(), object_pairs_hook=object_pairs) + + +def save(path, value): + private(Path(path).parent, True) + fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW | os.O_WRONLY, 0o600) + with os.fdopen(fd, 'w') as stream: + json.dump(value, stream, indent=2); stream.write('\n'); stream.flush(); os.fsync(stream.fileno()) + + +def setup_state(): + os.umask(0o077) + STATE.parent.mkdir(mode=0o700, exist_ok=True) + metadata = STATE.parent.lstat() + require(stat.S_ISDIR(metadata.st_mode) and metadata.st_uid == os.getuid() and not metadata.st_mode & 0o002, + 'owned non-world-writable devstate parent') + STATE.mkdir(mode=0o700, exist_ok=True) + private(STATE, True) + + +def source_files(): + require(sha(BACKEND) == BACKEND_SHA, 'exact reviewed local backend qualification') + reviewed = load(BACKEND) + require(reviewed['source_files'] == REVIEWED_BACKEND, 'reviewed thirteen backend/common pins') + names = {'Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', 'deploy/windows_msi_contract_acceptance.py', + 'deploy/vm_access_check.py', 'deploy/vm_mvp_acceptance.py', 'deploy/remote_client_bootstrap.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'} + names.update(str(p.relative_to(ROOT)) for p in (ROOT / 'crates').glob('*/Cargo.toml')) + for crate in ('domain', 'agent-core', 'agent-linux', 'privileged-helper', 'protocol'): + names.update(str(p.relative_to(ROOT)) for p in (ROOT / 'crates' / crate).rglob('*') + if p.is_file() and p.suffix in ('.rs', '.toml', '.c', '.h', '.idl', '.proto')) + names.update(str(p.relative_to(ROOT)) for p in (ROOT / 'proto').rglob('*') if p.is_file()) + names.update(REVIEWED_BACKEND) + for name in names: require((ROOT / name).is_file() and not (ROOT / name).is_symlink(), 'regular fixed source inputs') + result = {name: sha(ROOT / name) for name in sorted(names)} + require(all(result[name] == digest for name, digest in REVIEWED_BACKEND.items()), 'reviewed backend sources unchanged') + # No web source is a compiler input or part of this common-contract proof. + return result + + +def names(raw): + return sorted(re.findall(r'^([A-Za-z0-9_:]+): test\r?$', raw, re.M)) + + +def check_list(raw, expected): + require(names(raw) == expected and len(set(expected)) == len(expected), 'all exact test names before execution') + require(all(not line.strip() or re.fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in raw.splitlines()), + 'terse list contains only exact tests, no benchmarks or extra entries') + + +def check_output(raw, count): + require(re.search(rb'^test result: ok\. ' + str(count).encode() + + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;[^\r\n]*\r?$', raw, re.M), + 'all tests passed without skipped or filtered tests') + + +def command(args, folder, timeout=300): + index = len(list(folder.glob('command-*.json'))) + try: + done = subprocess.run(args, capture_output=True, timeout=timeout) + save(folder / ('command-%03d.json' % index), dict(returncode=done.returncode, + stdout_b64=base64.b64encode(done.stdout).decode(), stderr_b64=base64.b64encode(done.stderr).decode())) + require(done.returncode == 0, 'local qualification command failed; private output retained') + return done.stdout + except subprocess.TimeoutExpired as error: + save(folder / ('command-%03d.json' % index), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('local qualification timed out; no automatic retry') from None + + +def scripts(): + return {'linux_case_sha256': hashlib.sha256(inspect.getsource(linux_case).encode()).hexdigest(), + 'windows_script_sha256': hashlib.sha256(WINDOWS.encode()).hexdigest()} + + +def build(): + setup_state() + folder = STATE / ('build-' + str(uuid.uuid4())); folder.mkdir(mode=0o700) + before = source_files(); save(folder / 'source-before.json', before) + image = command(['docker', 'image', 'inspect', '--format', '{{.Id}}', IMAGE], folder).decode().strip() + require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable compiler image') + target = STATE / 'target'; target.mkdir(mode=0o700, exist_ok=True); private(target, 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, triple in [('linux','x86_64-unknown-linux-gnu'),('windows','x86_64-pc-windows-gnu')]: + options = ['--manifest-path','/src/Cargo.toml','--locked','--offline','--target',triple] + packages = ['-p','rmm-domain','-p','rmm-agent-core'] + (['-p','rmm-privileged-helper'] if platform == 'linux' else []) + command(common + ['clippy'] + options + packages + ['--all-targets','--','-D','warnings'], folder) + selections = [(['-p','rmm-domain','--lib'], 'domain'), + (['-p','rmm-agent-core'] + (['--features','openssl-verifier'] if platform == 'linux' else []) + ['--tests'], 'core')] + if platform == 'linux': selections.append((['-p','rmm-privileged-helper','--test','reconcile'], 'linux-helper')) + links = {} + for selection, group in selections: + raw = command(common + ['test'] + options + selection + ['--release','--no-run','--message-format=json'], folder) + for line in raw.splitlines(): + try: item = json.loads(line) + except ValueError: continue + if item.get('reason') != 'compiler-artifact' or not item.get('executable') or not item.get('profile',{}).get('test'): continue + name = item['target']['name'] + kind = 'domain' if group == 'domain' else 'linux-helper' if group == 'linux-helper' else {'contracts':'contracts','helper_reconcile':'helper-reconcile','reconcile':'reconcile'}.get(name) + if kind is None: continue # empty Core lib harness is not this profile + require(kind not in links and kind in KINDS[platform], 'exact distinct selected test link') + links[kind] = target / Path(item['executable']).relative_to('/target') + require(set(links) == set(KINDS[platform]), 'all selected native artifacts') + destination = folder / platform; destination.mkdir(mode=0o700) + for kind in KINDS[platform]: + original = destination / (kind + '.linked'); delivery = destination / (kind + ('.exe' if platform == 'windows' else '')) + for path in (original, delivery): shutil.copyfile(links[kind], path); path.chmod(0o600) + linked_sha = sha(original) + tool = 'x86_64-w64-mingw32-strip' if platform == 'windows' else 'strip' + command(['docker','run','--rm','--network','none','--read-only','-v',str(destination)+':/artifacts', + '--entrypoint',tool,image,'--strip-debug','/artifacts/'+delivery.name], folder) + require(sha(links[kind]) == sha(original) == linked_sha, 'original link preserved') + data = delivery.read_bytes() + if platform == 'linux': require(data[:6] == b'\x7fELF\x02\x01' and data[18:20] == b'\x3e\0', 'Linux x64 ELF') + else: + offset = int.from_bytes(data[60:64],'little'); require(data[:2] == b'MZ' and data[offset:offset+6] == b'PE\0\0\x64\x86', 'Windows x64 PE') + artifacts[platform][kind] = dict(path=str(delivery),sha256=sha(delivery),bytes=len(data), + original_link_path=str(original),original_link_sha256=linked_sha,debug_stripped_delivery=True) + # All local Linux lists precede any local tests, using exact delivery bytes. + def invoke(item, args): + script = 'import shutil,os,subprocess;shutil.copyfile("/unit","/tmp/unit");os.chmod("/tmp/unit",0o500);subprocess.run(["/tmp/unit"]+' + repr(args) + ',check=True,cwd="/tmp",env={"PATH":"/usr/bin:/bin","LANG":"C.UTF-8","TMPDIR":"/tmp","RUST_BACKTRACE":"0"})' + return command(['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,exec,size=128m', + '-v',item['path']+':/unit:ro','--entrypoint','python3',image,'-c',script], folder) + for kind, item in artifacts['linux'].items(): + raw = invoke(item, ['--list','--format','terse']); check_list(raw.decode(), EXPECTED['linux'][kind]) + save(folder / ('local-linux-'+kind+'-list.json'),dict(stdout_b64=base64.b64encode(raw).decode())) + for kind, item in artifacts['linux'].items(): + raw = invoke(item, ['--test-threads=1']); check_output(raw, COUNTS['linux'][kind]) + save(folder / ('local-linux-'+kind+'-run.json'),dict(stdout_b64=base64.b64encode(raw).decode())) + versions = command(['docker','run','--rm','--network','none','--read-only','-v',item['path']+':/unit:ro', + '--entrypoint','readelf',image,'--version-info','/unit'],folder).decode() + dynamic = command(['docker','run','--rm','--network','none','--read-only','-v',item['path']+':/unit:ro', + '--entrypoint','readelf',image,'-d','/unit'],folder).decode() + glibc = sorted(set(re.findall(r'GLIBC_(\d+\.\d+)',versions)),key=lambda n:tuple(map(int,n.split('.')))) + require(glibc and tuple(map(int,glibc[-1].split('.'))) <= (2,34), 'GLIBC at most 2.34 delivery requirement') + item['glibc_max'] = glibc[-1]; item['needed'] = sorted(re.findall(r'Shared library: \[([^\]]+)\]',dynamic)) + require(source_files() == before, 'all sources and backend pins unchanged through build') + manifest = dict(schema_version=1,profile=PROFILE,source_files=before,source_before_after_equal=True, + reviewed_backend_source_pins=REVIEWED_BACKEND,backend_qualification_sha256=BACKEND_SHA, + backend_sources_hash_bound_only=True,native_postgresql_executed=False,live_installer_executed=False, + runner_sha256=sha(__file__),scripts=scripts(),test_names=EXPECTED,test_counts=COUNTS,artifacts=artifacts, + build_image_id=image,both_targets_clippy_D_warnings=True,linux_lists_actual=True, + windows_lists_expected_until_native=True,local_linux_tests_passed=sum(COUNTS['linux'].values()), + native_linux_executed=False,native_windows_executed=False,product_state_writes=False,rmm_jobs=False) + save(folder / 'manifest.json',manifest) + print(json.dumps(dict(manifest=str(folder/'manifest.json'),manifest_sha256=sha(folder/'manifest.json'), + linux_tests=sum(COUNTS['linux'].values()),windows_tests_expected=sum(COUNTS['windows'].values()),native_executed=False))) + + +def verified(path, new_run): + path = path.absolute(); private(path.parent, True) + require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}', path.parent.name), 'fixed build manifest path') + value = load(path) + require(value['schema_version'] == 1 and value['profile'] == PROFILE and value['runner_sha256'] == sha(__file__) + and value['scripts'] == scripts() and value['test_names'] == EXPECTED and value['test_counts'] == COUNTS + and value['source_before_after_equal'] is True and value['both_targets_clippy_D_warnings'] is True + and value['reviewed_backend_source_pins'] == REVIEWED_BACKEND and value['backend_qualification_sha256'] == BACKEND_SHA + and value['backend_sources_hash_bound_only'] is True and value['native_postgresql_executed'] is False + and value['live_installer_executed'] is False, + 'original manifest/runner/remote scripts/profile binding') + if new_run: require(value['source_files'] == source_files(), 'new execution requires same current qualified source') + for platform in COUNTS: + require(set(value['artifacts'][platform]) == set(KINDS[platform]), 'all exact fixed artifacts') + for kind, item in value['artifacts'][platform].items(): + suffix = '.exe' if platform == 'windows' else '' + require(Path(item['path']) == path.parent / platform / (kind + suffix) + and Path(item['original_link_path']) == path.parent / platform / (kind + '.linked'), 'fixed private artifact paths') + for field, digest in [('path', item['sha256']), ('original_link_path', item['original_link_sha256'])]: + private(item[field]); require(sha(item[field]) == digest, 'immutable original/delivery bytes') + require(Path(item['path']).stat().st_size == item['bytes'], 'artifact size unchanged') + return value + + +def access_settings(root, platform): + """Read strictly parsed existing access only when the parent executes native.""" + require(root == ACCESS_ROOT and not root.is_symlink(), 'original Main access-root only; no copied credentials') + root = root.absolute(); require(root.is_dir() and root.resolve() == root, 'canonical access-root') + if platform == 'linux': + cfg = access.load_config(root / 'credentials/linux-vm.conf') + require(cfg['address'] == '116.203.20.85' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed Cloud Linux bootstrap target') + prefix = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()), 'root@116.203.20.85'] + else: + cfg = access.load_config(root / 'credentials/server.conf') + require(cfg['address'] == '88.99.80.202' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed server reverse-jump only') + credentials = root / 'credentials'; private(credentials, True) + record = load(credentials / 'windows-dev-access.json') + require(set(record) == {'ssh_prefix', 'expires_at'}, 'known Windows access schema') + expires = dt.datetime.fromisoformat(record['expires_at']) + require(expires.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expires, 'existing dev access still valid') + key = str(Path(cfg['ssh_key_path']).expanduser()) + jump = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-i', key, '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'ForwardAgent=no', '-o', 'IdentitiesOnly=yes', '-W', '127.0.0.1:22240', 'root@88.99.80.202'] + prefix = ['ssh', '-F', '/dev/null', '-T', '-i', key, '-o', 'ProxyCommand=' + shlex.join(jump), + '-o', 'HostKeyAlias=rmm-windows-dev', '-o', 'UserKnownHostsFile=' + str(credentials / 'windows-dev.known_hosts'), + '-o', 'GlobalKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', 'rmm-dev@127.0.0.1'] + require(record['ssh_prefix'] == prefix, 'exact registered pinned reverse SSH prefix') + private(credentials / 'windows-dev.known_hosts') + return prefix + + +def transport(prefix, command_text, data, folder, timeout=60): + """No stdout/stderr escapes the private case directory on transport failure.""" + try: + result = subprocess.run(prefix + [command_text], input=data, capture_output=True, timeout=timeout) + save(folder / ('transport-' + str(uuid.uuid4()) + '.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'native transport incomplete; same-case readback only') + value = json.loads(result.stdout.decode('utf-8-sig'), object_pairs_hook=object_pairs) + require(isinstance(value, dict) and 'bootstrap_error' not in value, 'native bootstrap incomplete; private receipt retained') + return value + except subprocess.TimeoutExpired as error: + save(folder / ('transport-timeout-' + str(uuid.uuid4()) + '.json'), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('native reply lost; read original case only') from None + + +def linux_rpc(prefix, payload, folder): + code = 'import os,json,subprocess,base64,hashlib,uuid\nfrom pathlib import Path\n' + inspect.getsource(require) + '\n' + inspect.getsource(linux_case) + code += '\ntry:\n print(json.dumps(linux_case(' + repr(payload) + ')))\nexcept Exception as e:\n print(json.dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n' + return transport(prefix, 'python3 -', code.encode(), folder) + + +def windows_rpc(prefix, payload, folder): + script = 'try {\n' + WINDOWS + '\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}' + command_text = 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand ' + base64.b64encode(script.encode('utf-16le')).decode() + return transport(prefix, command_text, (json.dumps(payload) + '\n').encode(), folder) + + +def upload(prefix, item, destination, folder, kind): + args = ['sftp'] + [part for part in prefix[1:-1] if part != '-T'] + ['-b', '-', prefix[-1]] + # Linux sftp uses uppercase -P; Windows prefix has no -p option. + if '-p' in args: args[args.index('-p')] = '-P' + batch = ('put ' + json.dumps(item['path']) + ' ' + json.dumps(destination) + '\n').encode() + save(folder / ('transfer-' + kind + '-intent.json'), dict(sha256=item['sha256'], bytes=item['bytes'])) + try: + result = subprocess.run(args, input=batch, capture_output=True, timeout=60) + save(folder / ('transfer-' + kind + '-result.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'fixed artifact transfer failed; no retry') + except subprocess.TimeoutExpired as error: + save(folder / ('transfer-' + kind + '-timeout.json'), dict(timed_out=True, + stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('artifact transfer incomplete; no retry') from None + + +def linux_case(p): + import resource + import stat + ident = str(uuid.UUID(p['id'])) + require(ident == p['id'] and p['action'] in ('prepare', 'run', 'read') and p['profile'] == 'windows-msi-contracts-v1', 'fixed unit case') + require(os.geteuid() == 0 and os.uname().machine == 'x86_64' and set(p['artifacts']) == {'domain','contracts','helper-reconcile','reconcile','linux-helper'}, 'fixed Linux root qualification') + root = Path('/var/lib/rmm-windows-msi-contracts') + def safe(path, directory): + s = path.lstat() + require(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 unit intent') + if p['action'] == 'read': + if not (case / 'result.json').exists(): + partial = {} + for kind in ('domain','contracts','helper-reconcile','reconcile','linux-helper'): + for suffix in ('-list.json','-result.json'): + path = case / (kind + suffix) + if path.exists(): + safe(path, False); require(path.stat().st_size <= 2*1024*1024, 'bounded original 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', + both_lists_verified=(case/'all-lists-verified.json').exists(),tests_started=(case/'tests-started.json').exists(),partial=partial) + safe(case / 'result.json', False); 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/bin:/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, (15, 15)); resource.setrlimit(resource.RLIMIT_FSIZE, (1048576, 1048576)) + 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 ('domain','contracts','helper-reconcile','reconcile','linux-helper'): + binary = case / kind; safe(binary, False) + require(hashlib.sha256(binary.read_bytes()).hexdigest() == p['artifacts'][kind]['sha256'], 'exact Linux delivery') + binary.chmod(0o500) + listed = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--list', '--format', 'terse'], cwd=case, env=env, capture_output=True, timeout=5, 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 __import__('re').fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in listed.stdout.decode().splitlines()) + and actual == p['names'][kind] and len(actual) == {'domain':20,'contracts':8,'helper-reconcile':26,'reconcile':26,'linux-helper':21}[kind], 'exact native test names before ANY test 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 ('domain','contracts','helper-reconcile','reconcile','linux-helper'): + with (case / (kind + '-stdout.bin')).open('xb') as out, (case / (kind + '-stderr.bin')).open('xb') as err: + timed = False + 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=18, 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]) + value = dict(id=ident, platform='linux', profile=p['profile'], manifest_sha256=p['manifest_sha256'], runner_sha256=p['runner_sha256'], + scripts=p['scripts'], reports=reports, all_lists_before_any_tests=True, network_namespace=True, environment_cleared=True, product_state_writes=False, rmm_jobs=False) + write(case / 'result.json', value); return value + + +WINDOWS = r''' +$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json) +if([string]([guid]$p.id)-cne$p.id-or$p.action-notin@('prepare','run','read')-or$p.profile-cne'windows-msi-contracts-v1'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.linux_proof_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed Linux-first profile case'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('windows-msi-contracts-'+$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 'Unit path type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private unit owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private unit ACL'}}} +function WriteNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 16 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +function MatchIntent($old){if($old.id-cne$p.id-or$old.profile-cne$p.profile-or$old.manifest_sha256-cne$p.manifest_sha256-or$old.runner_sha256-cne$p.runner_sha256-or$old.linux_proof_sha256-cne$p.linux_proof_sha256-or$old.scripts.linux_case_sha256-cne$p.scripts.linux_case_sha256-or$old.scripts.windows_script_sha256-cne$p.scripts.windows_script_sha256){throw 'Original profile intent'};foreach($kind in @('domain','contracts','helper-reconcile','reconcile')){if($old.artifacts.$kind.sha256-cne$p.artifacts.$kind.sha256-or$old.artifacts.$kind.bytes-ne$p.artifacts.$kind.bytes-or(@($old.names.$kind)-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'Original artifact/test tuple'}}} +if($p.action-eq'read'-and!(Test-Path -LiteralPath $case)){@{id=$p.id;status='remote_case_absent'}|ConvertTo-Json -Compress;exit 0} +Safe $root $true +if($p.action-eq'prepare'){if(Test-Path -LiteralPath $case){throw 'Existing case retained'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p;@{id=$p.id;prepared=$true}|ConvertTo-Json -Compress;exit 0} +Safe $case $true;$intent=Join-Path $case 'intent.json';Safe $intent $false;MatchIntent (Get-Content -LiteralPath $intent -Raw|ConvertFrom-Json) +if($p.action-eq'read'){$result=Join-Path $case 'result.json';if(!(Test-Path -LiteralPath $result)){$status=if(Test-Path -LiteralPath (Join-Path $case 'started.json')){'started_without_result'}else{'prepared_without_start'};$partial=@{};foreach($kind in @('domain','contracts','helper-reconcile','reconcile')){foreach($suffix in @('-list.json','-result.json')){$file=Join-Path $case ($kind+$suffix);if(Test-Path -LiteralPath $file){Safe $file $false;if((Get-Item -LiteralPath $file).Length-gt2097152){throw 'Partial report bound'};$partial[$kind+$suffix]=Get-Content -LiteralPath $file -Raw|ConvertFrom-Json}}};@{id=$p.id;status=$status;all_lists_verified=(Test-Path -LiteralPath (Join-Path $case 'all-lists-verified.json'));tests_started=(Test-Path -LiteralPath (Join-Path $case 'tests-started.json'));partial=$partial}|ConvertTo-Json -Depth 16 -Compress;exit 0};Safe $result $false;Get-Content -LiteralPath $result -Raw;exit 0} +function RunUnit($exe,[string]$arguments){$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=$exe;$s.Arguments=$arguments;$s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.WorkingDirectory=$case;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$systemRoot=$env:WINDIR;$s.EnvironmentVariables.Clear();$s.EnvironmentVariables['SystemRoot']=$systemRoot;$s.EnvironmentVariables['WINDIR']=$systemRoot;$s.EnvironmentVariables['PATH']=$systemRoot+'\System32';$s.EnvironmentVariables['TEMP']=$script:temp;$s.EnvironmentVariables['TMP']=$script:temp;$s.EnvironmentVariables['RUST_BACKTRACE']='0';$c=New-Object Diagnostics.Process;$c.StartInfo=$s;if(!$c.Start()){throw 'Unit start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$timed=$false;if(!$c.WaitForExit(18000)){$timed=$true;$c.Kill();if(!$c.WaitForExit(2000)){throw 'Own unit process did not exit'}};if(!$o.Wait(2000)-or!$e.Wait(2000)-or$o.Result.Length-gt1048576-or$e.Result.Length-gt1048576){throw 'Bounded unit output'};return @{exit_code=$c.ExitCode;timed_out=$timed;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') @{execute_once=$true;manifest_sha256=$p.manifest_sha256};$script:temp=Join-Path $case 'tmp';New-Item -ItemType Directory -Path $script:temp|Out-Null;Safe $script:temp $true +$lists=@{};$executables=@{} +foreach($kind in @('domain','contracts','helper-reconcile','reconcile')){$item=$p.artifacts.$kind;if($item.sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed unit hash'};$exe=Join-Path $case ($kind+'.exe');Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$item.sha256-or(Get-Item -LiteralPath $exe).Length-ne$item.bytes){throw 'Exact unit bytes'};$list=RunUnit $exe '--list --format terse';if($list.exit_code-ne0-or$list.timed_out){throw 'Actual Windows listing'};$text=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($list.stdout_b64));$unexpected=@($text-split'\r?\n'|Where-Object{$_.Trim()-and$_-notmatch'^[A-Za-z0-9_:]+: test$'});if($unexpected.Count){throw 'Only exact terse test entries'};[string[]]$actual=@($text-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''});[Array]::Sort($actual,[StringComparer]::Ordinal);$counts=@{domain=20;contracts=7;'helper-reconcile'=26;reconcile=26};$count=$counts[$kind];if($actual.Count-ne$count-or($actual-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'All exact names BEFORE any tests'};WriteNew (Join-Path $case ($kind+'-list.json')) @{names=$actual;sha256=$item.sha256;listed=$list};$lists[$kind]=$actual;$executables[$kind]=$exe} +WriteNew (Join-Path $case 'all-lists-verified.json') @{all_lists_before_any_tests=$true;manifest_sha256=$p.manifest_sha256};WriteNew (Join-Path $case 'tests-started.json') @{execute_tests_once=$true} +$reports=@{};foreach($kind in @('domain','contracts','helper-reconcile','reconcile')){$value=RunUnit $executables[$kind] '--test-threads=1';$value.sha256=$p.artifacts.$kind.sha256;$value.names=$lists[$kind];WriteNew (Join-Path $case ($kind+'-result.json')) $value;$reports[$kind]=$value} +$value=@{id=$p.id;platform='windows';profile=$p.profile;manifest_sha256=$p.manifest_sha256;runner_sha256=$p.runner_sha256;scripts=$p.scripts;linux_proof_sha256=$p.linux_proof_sha256;reports=$reports;all_lists_before_any_tests=$true;bootstrap_administrator_units=$true;local_service_proof=$false;environment_cleared=$true;product_state_writes=$false;rmm_jobs=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 16 -Compress +''' + + +def check_result(manifest, platform, value, payload): + require(value.get('id') == payload['id'] and value.get('platform') == platform and value.get('profile') == PROFILE + and value.get('manifest_sha256') == payload['manifest_sha256'] and value.get('runner_sha256') == manifest['runner_sha256'] + and value.get('scripts') == manifest['scripts'] and value.get('product_state_writes') is False and value.get('rmm_jobs') is False + and value.get('environment_cleared') is True and value.get('all_lists_before_any_tests') is True and set(value['reports']) == set(KINDS[platform]), 'complete native original binding') + if platform == 'linux': require(value.get('network_namespace') is True, 'actual Linux netns proof') + else: require(value.get('linux_proof_sha256') == payload['linux_proof_sha256'] and value.get('local_service_proof') is False, 'original Linux proof and honest account scope') + for kind, item in value['reports'].items(): + require(item['names'] == EXPECTED[platform][kind] and item['sha256'] == manifest['artifacts'][platform][kind]['sha256'] + and item['exit_code'] == 0 and item['timed_out'] is False, 'all native exact test names/artifacts/exits') + raw = base64.b64decode(item['stdout_b64'], validate=True); errors = base64.b64decode(item['stderr_b64'], validate=True) + require(len(raw) <= 1048576 and len(errors) <= 1048576, 'bounded native outputs') + check_output(raw, COUNTS[platform][kind]) + + +def execute(path, platform, access_root, case_id=None): + setup_state(); path = path.absolute(); manifest = verified(path, new_run=case_id is None) + prefix = access_settings(access_root, platform) + if case_id is None: + case_id = str(uuid.uuid4()); folder = STATE / case_id; folder.mkdir(mode=0o700) + payload = dict(id=case_id, profile=PROFILE, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], scripts=manifest['scripts'], + artifacts={kind: {k: manifest['artifacts'][platform][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS[platform]}, names=EXPECTED[platform]) + if platform == 'windows': + proof_path = path.parent / 'linux-passed.json'; proof = load(proof_path) + require(proof['verified'] is True and proof['manifest_sha256'] == sha(path), 'same-manifest real Linux proof FIRST') + linux_payload = dict(payload, id=proof['case_id'], artifacts={kind: {k: manifest['artifacts']['linux'][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS['linux']}, names=EXPECTED['linux']) + check_result(manifest, 'linux', proof['native'], linux_payload) + payload['linux_proof_sha256'] = sha(proof_path) + intent = dict(id=case_id, platform=platform, manifest_path=str(path), manifest_sha256=sha(path), runner_sha256=sha(__file__), + scripts=scripts(), access_root_sha256=hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), payload=payload) + save(folder / 'intent.json', intent) + save(folder / 'started-local.json', dict(create_once=True, no_retry=True)) + run_read = False + else: + require(str(uuid.UUID(case_id)) == case_id, 'original canonical case id') + folder = STATE / case_id; private(folder, True); intent = load(folder / 'intent.json'); load(folder / 'started-local.json') + require(intent['id'] == case_id and intent['platform'] == platform and intent['manifest_path'] == str(path) + and intent['manifest_sha256'] == sha(path) and intent['runner_sha256'] == sha(__file__) and intent['scripts'] == scripts() + and intent['access_root_sha256'] == hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), 'immutable same-case readback') + payload = intent['payload']; run_read = True + rpc = linux_rpc if platform == 'linux' else windows_rpc + if not run_read: + result = rpc(prefix, dict(payload, action='prepare'), folder) + save(folder / 'prepared-remote.json', result); require(result == dict(id=case_id, prepared=True), 'fresh fixed case prepared') + for kind in KINDS[platform]: + destination = LINUX_REMOTE + '/' + case_id + '/' + kind if platform == 'linux' else '/C:/ProgramData/RustDiscoveryBootstrap/windows-msi-contracts-' + case_id + '/' + kind + '.exe' + upload(prefix, manifest['artifacts'][platform][kind], destination, folder, kind) + result = rpc(prefix, dict(payload, action='read' if run_read else 'run'), folder) + save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) + if result.get('status'): + require(run_read and result.get('id') == case_id and result['status'] in ('remote_case_absent', 'prepared_without_start', 'started_without_result'), 'fixed read-only incomplete status') + print(json.dumps(dict(verified=False, platform=platform, case_id=case_id, status=result['status'], retry_allowed=False))); return + check_result(manifest, platform, result, payload) + proof = dict(verified=True, case_id=case_id, platform=platform, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], native=result) + destination = path.parent / (platform + '-passed.json') + if destination.exists(): require(load(destination) == proof, 'preserve identical native proof') + else: save(destination, proof) + save(folder / 'verified.json', dict(verified=True, proof_sha256=sha(destination))) if not (folder / 'verified.json').exists() else require(load(folder / 'verified.json') == dict(verified=True, proof_sha256=sha(destination)), 'same verified receipt') + print(json.dumps(dict(verified=True, platform=platform, case_id=case_id, tests=sum(COUNTS[platform].values()), + manifest_sha256=sha(path), proof_sha256=sha(destination)))) + + +def main(): + 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('--access-root', type=Path) + 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.access_root or args.execute_native_tests), 'local build only'); build() + elif args.action.startswith('readback-'): + require(args.manifest and args.case and args.access_root and not args.execute_native_tests, 'original case readback only') + execute(args.manifest, args.action.removeprefix('readback-'), args.access_root, args.case) + else: + require(args.manifest and args.access_root and args.execute_native_tests and not args.case, 'explicit new native platform run') + execute(args.manifest, args.action, args.access_root) + + +if __name__ == '__main__': + try: main() + except Exception as error: + print(json.dumps(dict(error='WINDOWS_MSI_CONTRACT_QUALIFICATION_INCOMPLETE', error_class=type(error).__name__, details_suppressed=True))) + raise SystemExit(2) from None diff --git a/deploy/windows_msi_runtime_acceptance.py b/deploy/windows_msi_runtime_acceptance.py new file mode 100644 index 0000000..1d42862 --- /dev/null +++ b/deploy/windows_msi_runtime_acceptance.py @@ -0,0 +1,559 @@ +#!/usr/bin/env python3 +"""Windows MSI runtime Library/Binary units: Linux VM FIRST, then Windows VM. + +Only fixed common test executables run in separate bootstrap scratch directories. +No RMM job, daemon, live database, PKI, policy, enrollment or capability change. +Native execution is explicit; any lost reply permits same-case readback only. +""" +import argparse +import base64 +import datetime as dt +import hashlib +import inspect +import json +import os +from pathlib import Path +import re +import shlex +import shutil +import stat +import subprocess +import uuid + +import vm_access_check as access + +ROOT = Path(__file__).resolve().parents[1] +STATE = ROOT / 'devstate/windows-msi-runtime-units' +IMAGE = 'rust-discovery-durable-final-build:local' +PROFILE = 'windows-msi-runtime-units-v1' +LINUX_REMOTE = '/var/lib/rmm-windows-msi-runtime-units' +ACCESS_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery') +QUALIFICATION = ROOT / 'devstate/windows-msi-runtime/qualified-ee6681c7-8d9d-4af9-8947-5325e1ea2e0f/qualification.json' +QUALIFICATION_SHA = '17f3eb2258773a22be26afd9681d271f69136cfb52dbda31aac2a0ef577b3816' +COMMON_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery-windows-msi-integration') +COMMON = COMMON_ROOT / 'devstate/windows-msi-contracts/build-484a3761-6c84-426f-968d-25a638e9c6f7/manifest.json' +COMMON_SHA = '2d96a07be72f3d71a8097510eb5f647ced6577086a45417c599b772018c29a53' +COMMON_PROOFS = {'linux':'09fa24e32cd3f3563e80e91265c6cc7c612321b0fdd1febe7aef3ab6de53cf2a', + 'windows':'666ed196953abd0723de259a898b46c08ac84feee0140f1bb31d9db1fa1232c4'} +# Linux ten names were actually executed in the frozen local qualification; +# final Linux delivery --list/run is checked below. Windows is EXPECTED only. +EXPECTED = {'linux': {'lib': ['event_log::tests::job_mapping_is_only_fixed_unprivileged_live_logs_with_exact_limits', + 'event_log::tests::only_fixed_source_window_and_finite_limits_are_queryable', + 'event_log::tests::serialized_byte_budget_includes_xml_escaping_and_metadata', + 'journal::tests::bounded_existing_journal_preserves_identity_sequence_and_unacked_bytes', + 'journal::tests::durable_reopen_ack_identity_and_single_process', + 'journal::tests::migrates_original_passive_schema_without_rewriting_pending_envelopes', + 'journal::tests::unsupported_page_size_is_refused_without_mutating_existing_data', + 'package::tests::exit_code_never_substitutes_for_independent_postconditions_or_reboot_authority', + 'package::tests::late_exit_or_postcondition_cannot_retroactively_succeed_or_retry', + 'package::tests::upgrade_and_noop_need_independent_exact_products_family_and_payload']}, + 'windows': {'lib': ['actions::tests::powershell_policy_is_an_exact_job_profile_before_file_not_a_mutable_default', + 'clock::tests::gate_requires_all_status_and_uncertainty_evidence', + 'clock::tests::production_clock_stays_closed_until_native_adapter_qualification', + 'clock::tests::suspend_closes_until_actual_post_resume_sync', + 'clock::tests::timeout_worker_stays_closed_without_a_second_call', + 'clock::tests::wall_step_and_overflow_fail_closed', + 'event_log::tests::job_mapping_is_only_fixed_unprivileged_live_logs_with_exact_limits', + 'event_log::tests::only_fixed_source_window_and_finite_limits_are_queryable', + 'event_log::tests::serialized_byte_budget_includes_xml_escaping_and_metadata', + 'event_log_job::tests::event_log_outcome_never_equates_valid_quality_with_complete_or_authorized_success', + 'event_log_job::tests::ticket_pre_read_boundary_excludes_keys_siblings_and_noncanonical_leaves', + 'helper_ipc::tests::pipe_client_rights_cannot_create_an_instance', + 'helper_ipc::tests::protocol_does_not_accept_a_shell_operation', + 'helper_ipc::tests::reconfigured_service_cannot_adopt_an_old_running_identity', + 'helper_ipc::tests::serialization_refuses_oversized_frame', + 'helper_ipc::tests::untrusted_lengths_are_rejected_before_body_allocation', + 'helper_runtime::intent::tests::additive_denial_column_preserves_original_legacy_intents_and_rejects_unknown_trigger', + 'helper_runtime::intent::tests::cancel_and_dispatch_cas_are_exclusive_and_history_cannot_be_rewritten', + 'helper_runtime::tests::msi_scope_requires_bound_independent_postconditions_and_exact_noop_or_installer_exit', + 'helper_runtime::tests::system_route_binds_original_request_shape_budget_and_reply_scope', + 'helper_system::tests::completed_scm_state_cannot_override_elapsed_runtime', + 'helper_system::tests::fixture_recovery_cannot_add_commands_reboots_or_restart_attempts', + 'helper_system::tests::old_settings_omit_new_scope_and_only_v2_exact_recipe_hash_and_budget_allow_msi', + 'helper_system::tests::service_adapter_exposes_only_implemented_states', + 'helper_system::tests::single_fixture_cannot_start_services_or_groups_as_dependencies', + 'journal::tests::bounded_existing_journal_preserves_identity_sequence_and_unacked_bytes', + 'journal::tests::durable_reopen_ack_identity_and_single_process', + 'journal::tests::migrates_original_passive_schema_without_rewriting_pending_envelopes', + 'journal::tests::unsupported_page_size_is_refused_without_mutating_existing_data', + 'native::tests::boot_identity_survives_independent_native_queries', + 'native::tests::native_inventory_has_real_windows_sources_and_no_fake_inodes', + 'package::tests::exit_code_never_substitutes_for_independent_postconditions_or_reboot_authority', + 'package::tests::late_exit_or_postcondition_cannot_retroactively_succeed_or_retry', + 'package::tests::upgrade_and_noop_need_independent_exact_products_family_and_payload', + 'process::tests::arguments_never_become_shell_code_and_trailing_slashes_are_quoted'], + 'bin': ['https_channel::endpoint_tests::configured_dns_identity_cannot_inject_a_url_or_disable_name_checks', + 'runtime::tests::bound_helper_unknown_repairs_generic_crash_gap_without_ack_or_duplicate_result', + 'runtime::tests::legacy_inventory_keeps_original_bytes_until_concrete_core_ack', + 'runtime::tests::old_passive_configuration_cannot_enable_jobs_or_read_the_clock', + 'runtime::tests::worker_fence_requires_same_generation_and_entire_clock_interval', + 'wire_channel::tests::auto_fallback_classifies_upgrade_refusal_but_never_authentication', + 'wire_channel::tests::explicit_transport_names_cannot_enable_verification_bypasses']}} +COUNTS = {platform:{kind:len(names) for kind,names in values.items()} for platform,values in EXPECTED.items()} +KINDS = {'linux':('lib',),'windows':('lib','bin')} + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def object_pairs(items): + value = {} + for key, item in items: + require(key not in value, 'duplicate JSON field') + value[key] = item + return value + + +def private(path, directory=False): + item = Path(path).lstat() + require(not stat.S_ISLNK(item.st_mode) and item.st_uid == os.getuid() + and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600) + and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1), + 'private owned regular path') + + +def load(path): + private(path) + require(Path(path).stat().st_size <= 2 * 1024 * 1024, 'bounded private JSON') + return json.loads(Path(path).read_bytes(), object_pairs_hook=object_pairs) + + +def save(path, value): + private(Path(path).parent, True) + fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW | os.O_WRONLY, 0o600) + with os.fdopen(fd, 'w') as stream: + json.dump(value, stream, indent=2); stream.write('\n'); stream.flush(); os.fsync(stream.fileno()) + + +def setup_state(): + os.umask(0o077) + STATE.parent.mkdir(mode=0o700, exist_ok=True) + metadata = STATE.parent.lstat() + require(stat.S_ISDIR(metadata.st_mode) and metadata.st_uid == os.getuid() and not metadata.st_mode & 0o002, + 'owned non-world-writable devstate parent') + STATE.mkdir(mode=0o700, exist_ok=True) + private(STATE, True) + + +def source_qualification(): + require(sha(QUALIFICATION)==QUALIFICATION_SHA,'exact independently reviewed Runtime qualification') + value=load(QUALIFICATION) + require(value['type']=='rmm.windows.msi-runtime.local-source-qualification' + and value['source_before_after_equal'] is True and value['native_execution'] is False + and value['checks']['windows_default_alltargets_clippy']=='passed' + and value['checks']['windows_system_helper_alltargets_clippy']=='passed' + and value['checks']['windows_lib_bin_unit_link']=='passed_not_executed','real frozen source/Clippy/link qualification') + require(all(sha(ROOT/name)==digest for name,digest in value['source_files'].items()),'all 82 qualified compiler inputs unchanged') + for name,item in value['artifacts'].items(): + file=QUALIFICATION.parent/name;private(file) + require(sha(file)==item['sha256'] and file.stat().st_size==item['bytes'],'unchanged qualified original/delivery artifact') + return value + + +def common_proof(): + require(sha(COMMON)==COMMON_SHA,'exact existing common build, never relabelled') + manifest=load(COMMON) + require(manifest['profile']=='windows-msi-contracts-v1' and manifest['source_before_after_equal'] is True, + 'original common contract profile') + for name,digest in manifest['source_files'].items(): + require(sha(COMMON_ROOT/name)==digest,'preserved complete common source archive') + if name.startswith(('crates/agent-core/','crates/domain/','crates/protocol/','proto/')) or name in ('Cargo.toml','Cargo.lock'): + require(sha(ROOT/name)==digest,'Runtime uses the same actual native-qualified common source') + result={'manifest_sha256':COMMON_SHA} + for platform,total in (('linux',101),('windows',79)): + path=COMMON.parent/(platform+'-passed.json');require(sha(path)==COMMON_PROOFS[platform],'exact actual original common native proof') + proof=load(path);actual=proof['native'] + require(proof['verified'] is True and proof['platform']==platform and proof['manifest_sha256']==COMMON_SHA + and actual['manifest_sha256']==COMMON_SHA and actual['runner_sha256']==manifest['runner_sha256'] + and actual['scripts']==manifest['scripts'] and actual['product_state_writes'] is False and actual['rmm_jobs'] is False + and actual['all_lists_before_any_tests'] is True and set(actual['reports'])==set(manifest['test_counts'][platform]),'actual complete common profile proof') + for kind,item in actual['reports'].items(): + require(item['names']==manifest['test_names'][platform][kind] + and item['sha256']==manifest['artifacts'][platform][kind]['sha256'] + and item['exit_code']==0 and item['timed_out'] is False,'every original common native artifact/list/exit') + check_output(base64.b64decode(item['stdout_b64'],validate=True),manifest['test_counts'][platform][kind]) + require(sum(len(item['names']) for item in actual['reports'].values())==total,'exact original native count') + result[platform]={'proof_sha256':sha(path),'case_id':proof['case_id'],'tests':total} + return result + + +def source_files(): + value=source_qualification() + result=dict(value['source_files']) + for name in ('deploy/windows_msi_runtime_acceptance.py','deploy/vm_access_check.py','deploy/vm_mvp_acceptance.py'): + require(not (ROOT/name).is_symlink(),'regular local driver/transport input');result[name]=sha(ROOT/name) + return dict(sorted(result.items())) + + +def names(raw): + return sorted(re.findall(r'^([A-Za-z0-9_:]+): test\r?$', raw, re.M)) + + +def check_list(raw, expected): + require(names(raw) == expected and len(set(expected)) == len(expected), 'all exact test names before execution') + require(all(not line.strip() or re.fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in raw.splitlines()), + 'terse list contains only exact tests, no benchmarks or extra entries') + + +def check_output(raw, count): + require(re.search(rb'^test result: ok\. ' + str(count).encode() + + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;[^\r\n]*\r?$', raw, re.M), + 'all tests passed without skipped or filtered tests') + + +def command(args, folder, timeout=300): + index = len(list(folder.glob('command-*.json'))) + try: + done = subprocess.run(args, capture_output=True, timeout=timeout) + save(folder / ('command-%03d.json' % index), dict(returncode=done.returncode, + stdout_b64=base64.b64encode(done.stdout).decode(), stderr_b64=base64.b64encode(done.stderr).decode())) + require(done.returncode == 0, 'local qualification command failed; private output retained') + return done.stdout + except subprocess.TimeoutExpired as error: + save(folder / ('command-%03d.json' % index), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('local qualification timed out; no automatic retry') from None + + +def scripts(): + return {'linux_case_sha256': hashlib.sha256(inspect.getsource(linux_case).encode()).hexdigest(), + 'windows_script_sha256': hashlib.sha256(WINDOWS.encode()).hexdigest()} + + +def build(): + setup_state();folder=STATE/('build-'+str(uuid.uuid4()));folder.mkdir(mode=0o700) + before=source_files();qualified=source_qualification();common=common_proof();save(folder/'source-before.json',before) + image=qualified['image_id'];require(re.fullmatch(r'sha256:[a-f0-9]{64}',image),'immutable source-qualified compiler image') + target=STATE/'target';target.mkdir(mode=0o700,exist_ok=True);private(target,True) + docker=['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] + options=['--manifest-path','/src/Cargo.toml','--locked','--offline','--target','x86_64-unknown-linux-gnu','-p','rmm-agent-windows','--lib'] + command(docker+['clippy']+options+['--','-D','warnings'],folder) + raw=command(docker+['test']+options+['--release','--no-run','--message-format=json'],folder) + links=[] + for line in raw.splitlines(): + try:item=json.loads(line) + except ValueError:continue + if item.get('reason')=='compiler-artifact' and item.get('executable') and item.get('profile',{}).get('test') and item['target']['name']=='rmm_agent_windows':links.append(target/Path(item['executable']).relative_to('/target')) + require(len(links)==1,'exact Linux library test link') + artifacts={'linux':{},'windows':{}} + for platform in KINDS: + destination=folder/platform;destination.mkdir(mode=0o700) + for kind in KINDS[platform]: + linked=links[0] if platform=='linux' else QUALIFICATION.parent/('windows-'+kind+'-tests.original.exe') + delivery_source=linked if platform=='linux' else QUALIFICATION.parent/('windows-'+kind+'-tests.exe') + original=destination/(kind+'.linked');output=destination/(kind+('.exe' if platform=='windows' else '')) + for source,dest in ((linked,original),(delivery_source,output)):shutil.copyfile(source,dest);dest.chmod(0o600) + original_sha=sha(linked) + if platform=='linux':command(['docker','run','--rm','--network','none','--read-only','-v',str(destination)+':/artifacts','--entrypoint','strip',image,'--strip-debug','/artifacts/lib'],folder) + require(sha(linked)==sha(original)==original_sha,'unchanged original linked bytes') + data=output.read_bytes() + if platform=='linux':require(data[:6]==b'\x7fELF\x02\x01' and data[18:20]==b'\x3e\0','Linux x64 ELF') + else: + offset=int.from_bytes(data[60:64],'little');require(data[:2]==b'MZ' and data[offset:offset+6]==b'PE\0\0\x64\x86','Windows x64 PE') + require(all(name.encode() in data for name in EXPECTED['windows'][kind]),'expected native names present in linked Windows bytes, not an executed list') + artifacts[platform][kind]=dict(path=str(output),sha256=sha(output),bytes=len(data),original_link_path=str(original),original_link_sha256=original_sha,debug_stripped_delivery=True) + item=artifacts['linux']['lib'] + for mode,args in (('list',['--list','--format','terse']),('run',['--test-threads=1'])): + code='import shutil,os,subprocess;shutil.copyfile("/unit","/tmp/unit");os.chmod("/tmp/unit",0o500);subprocess.run(["/tmp/unit"]+'+repr(args)+',check=True,cwd="/tmp",env={"PATH":"/usr/bin:/bin","LANG":"C.UTF-8","TMPDIR":"/tmp","RUST_BACKTRACE":"0"})' + raw=command(['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,exec,size=128m','-v',item['path']+':/unit:ro','--entrypoint','python3',image,'-c',code],folder) + save(folder/('local-linux-lib-'+mode+'.json'),dict(stdout_b64=base64.b64encode(raw).decode())) + if mode=='list':check_list(raw.decode(),EXPECTED['linux']['lib']) + else:check_output(raw,10) + versions=command(['docker','run','--rm','--network','none','--read-only','-v',item['path']+':/unit:ro','--entrypoint','readelf',image,'--version-info','/unit'],folder).decode() + glibc=sorted(set(re.findall(r'GLIBC_(\d+\.\d+)',versions)),key=lambda x:tuple(map(int,x.split('.'))));require(glibc and tuple(map(int,glibc[-1].split('.'))) <= (2,34),'GLIBC at most 2.34') + item['glibc_max']=glibc[-1] + require(source_files()==before and common_proof()==common,'all sources, original qualification and common proofs unchanged') + manifest=dict(schema_version=1,profile=PROFILE,source_files=before,source_before_after_equal=True, + source_qualification=dict(path=str(QUALIFICATION),sha256=QUALIFICATION_SHA),common_native_proofs=common, + original_windows_clippy_and_units_reused=True,backend_and_installer_native_claim=False, + runner_sha256=sha(__file__),scripts=scripts(),test_names=EXPECTED,test_counts=COUNTS,artifacts=artifacts, + build_image_id=image,both_targets_clippy_D_warnings=True,linux_lists_actual=True,windows_lists_expected_until_native=True, + local_linux_tests_passed=10,native_linux_executed=False,native_windows_executed=False,product_state_writes=False,rmm_jobs=False) + save(folder/'manifest.json',manifest);print(json.dumps(dict(manifest=str(folder/'manifest.json'),manifest_sha256=sha(folder/'manifest.json'),linux_tests=10,windows_tests_expected=42,native_executed=False))) + + +def verified(path, new_run): + path = path.absolute(); private(path.parent, True) + require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}', path.parent.name), 'fixed build manifest path') + value = load(path) + require(value['schema_version'] == 1 and value['profile'] == PROFILE and value['runner_sha256'] == sha(__file__) + and value['scripts'] == scripts() and value['test_names'] == EXPECTED and value['test_counts'] == COUNTS + and value['source_before_after_equal'] is True and value['both_targets_clippy_D_warnings'] is True + and value['source_qualification'] == dict(path=str(QUALIFICATION),sha256=QUALIFICATION_SHA) + and value['original_windows_clippy_and_units_reused'] is True and value['backend_and_installer_native_claim'] is False, + 'original manifest/runner/remote scripts/profile binding') + if new_run: require(value['source_files'] == source_files() and value['common_native_proofs'] == common_proof(), 'new execution requires exact runtime/common source proofs') + for platform in COUNTS: + require(set(value['artifacts'][platform]) == set(KINDS[platform]), 'all exact fixed artifacts') + for kind, item in value['artifacts'][platform].items(): + suffix = '.exe' if platform == 'windows' else '' + require(Path(item['path']) == path.parent / platform / (kind + suffix) + and Path(item['original_link_path']) == path.parent / platform / (kind + '.linked'), 'fixed private artifact paths') + for field, digest in [('path', item['sha256']), ('original_link_path', item['original_link_sha256'])]: + private(item[field]); require(sha(item[field]) == digest, 'immutable original/delivery bytes') + require(Path(item['path']).stat().st_size == item['bytes'], 'artifact size unchanged') + return value + + +def access_settings(root, platform): + """Read strictly parsed existing access only when the parent executes native.""" + require(root == ACCESS_ROOT and not root.is_symlink(), 'original Main access-root only; no copied credentials') + root = root.absolute(); require(root.is_dir() and root.resolve() == root, 'canonical access-root') + if platform == 'linux': + cfg = access.load_config(root / 'credentials/linux-vm.conf') + require(cfg['address'] == '116.203.20.85' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed Cloud Linux bootstrap target') + prefix = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()), 'root@116.203.20.85'] + else: + cfg = access.load_config(root / 'credentials/server.conf') + require(cfg['address'] == '88.99.80.202' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed server reverse-jump only') + credentials = root / 'credentials'; private(credentials, True) + record = load(credentials / 'windows-dev-access.json') + require(set(record) == {'ssh_prefix', 'expires_at'}, 'known Windows access schema') + expires = dt.datetime.fromisoformat(record['expires_at']) + require(expires.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expires, 'existing dev access still valid') + key = str(Path(cfg['ssh_key_path']).expanduser()) + jump = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-i', key, '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'ForwardAgent=no', '-o', 'IdentitiesOnly=yes', '-W', '127.0.0.1:22240', 'root@88.99.80.202'] + prefix = ['ssh', '-F', '/dev/null', '-T', '-i', key, '-o', 'ProxyCommand=' + shlex.join(jump), + '-o', 'HostKeyAlias=rmm-windows-dev', '-o', 'UserKnownHostsFile=' + str(credentials / 'windows-dev.known_hosts'), + '-o', 'GlobalKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', 'rmm-dev@127.0.0.1'] + require(record['ssh_prefix'] == prefix, 'exact registered pinned reverse SSH prefix') + private(credentials / 'windows-dev.known_hosts') + return prefix + + +def transport(prefix, command_text, data, folder, timeout=60): + """No stdout/stderr escapes the private case directory on transport failure.""" + try: + result = subprocess.run(prefix + [command_text], input=data, capture_output=True, timeout=timeout) + save(folder / ('transport-' + str(uuid.uuid4()) + '.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'native transport incomplete; same-case readback only') + value = json.loads(result.stdout.decode('utf-8-sig'), object_pairs_hook=object_pairs) + require(isinstance(value, dict) and 'bootstrap_error' not in value, 'native bootstrap incomplete; private receipt retained') + return value + except subprocess.TimeoutExpired as error: + save(folder / ('transport-timeout-' + str(uuid.uuid4()) + '.json'), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('native reply lost; read original case only') from None + + +def linux_rpc(prefix, payload, folder): + code = 'import os,json,subprocess,base64,hashlib,uuid\nfrom pathlib import Path\n' + inspect.getsource(require) + '\n' + inspect.getsource(linux_case) + code += '\ntry:\n print(json.dumps(linux_case(' + repr(payload) + ')))\nexcept Exception as e:\n print(json.dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n' + return transport(prefix, 'python3 -', code.encode(), folder) + + +def windows_rpc(prefix, payload, folder): + script = 'try {\n' + WINDOWS + '\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}' + command_text = 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand ' + base64.b64encode(script.encode('utf-16le')).decode() + return transport(prefix, command_text, (json.dumps(payload) + '\n').encode(), folder) + + +def upload(prefix, item, destination, folder, kind): + args = ['sftp'] + [part for part in prefix[1:-1] if part != '-T'] + ['-b', '-', prefix[-1]] + # Linux sftp uses uppercase -P; Windows prefix has no -p option. + if '-p' in args: args[args.index('-p')] = '-P' + batch = ('put ' + json.dumps(item['path']) + ' ' + json.dumps(destination) + '\n').encode() + save(folder / ('transfer-' + kind + '-intent.json'), dict(sha256=item['sha256'], bytes=item['bytes'])) + try: + result = subprocess.run(args, input=batch, capture_output=True, timeout=60) + save(folder / ('transfer-' + kind + '-result.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'fixed artifact transfer failed; no retry') + except subprocess.TimeoutExpired as error: + save(folder / ('transfer-' + kind + '-timeout.json'), dict(timed_out=True, + stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('artifact transfer incomplete; no retry') from None + + +def linux_case(p): + import resource + import stat + ident = str(uuid.UUID(p['id'])) + require(ident == p['id'] and p['action'] in ('prepare', 'run', 'read') and p['profile'] == 'windows-msi-runtime-units-v1', 'fixed unit case') + require(os.geteuid() == 0 and os.uname().machine == 'x86_64' and set(p['artifacts']) == {'lib'}, 'fixed Linux root qualification') + root = Path('/var/lib/rmm-windows-msi-runtime-units') + def safe(path, directory): + s = path.lstat() + require(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 unit intent') + if p['action'] == 'read': + if not (case / 'result.json').exists(): + partial = {} + for kind in ('lib',): + for suffix in ('-list.json','-result.json'): + path = case / (kind + suffix) + if path.exists(): + safe(path, False); require(path.stat().st_size <= 2*1024*1024, 'bounded original 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', + both_lists_verified=(case/'all-lists-verified.json').exists(),tests_started=(case/'tests-started.json').exists(),partial=partial) + safe(case / 'result.json', False); 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/bin:/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, (15, 15)); resource.setrlimit(resource.RLIMIT_FSIZE, (1048576, 1048576)) + 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 ('lib',): + binary = case / kind; safe(binary, False) + require(hashlib.sha256(binary.read_bytes()).hexdigest() == p['artifacts'][kind]['sha256'], 'exact Linux delivery') + binary.chmod(0o500) + listed = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--list', '--format', 'terse'], cwd=case, env=env, capture_output=True, timeout=5, 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 __import__('re').fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in listed.stdout.decode().splitlines()) + and actual == p['names'][kind] and len(actual) == {'lib':10}[kind], 'exact native test names before ANY test 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 ('lib',): + with (case / (kind + '-stdout.bin')).open('xb') as out, (case / (kind + '-stderr.bin')).open('xb') as err: + timed = False + 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=18, 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]) + value = dict(id=ident, platform='linux', profile=p['profile'], manifest_sha256=p['manifest_sha256'], runner_sha256=p['runner_sha256'], + scripts=p['scripts'], reports=reports, all_lists_before_any_tests=True, network_namespace=True, environment_cleared=True, product_state_writes=False, rmm_jobs=False) + write(case / 'result.json', value); return value + + +WINDOWS = r''' +$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json) +if([string]([guid]$p.id)-cne$p.id-or$p.action-notin@('prepare','run','read')-or$p.profile-cne'windows-msi-runtime-units-v1'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.linux_proof_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed Linux-first profile case'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('windows-msi-runtime-units-'+$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 'Unit path type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private unit owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private unit ACL'}}} +function WriteNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 16 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +function MatchIntent($old){if($old.id-cne$p.id-or$old.profile-cne$p.profile-or$old.manifest_sha256-cne$p.manifest_sha256-or$old.runner_sha256-cne$p.runner_sha256-or$old.linux_proof_sha256-cne$p.linux_proof_sha256-or$old.scripts.linux_case_sha256-cne$p.scripts.linux_case_sha256-or$old.scripts.windows_script_sha256-cne$p.scripts.windows_script_sha256){throw 'Original profile intent'};foreach($kind in @('lib','bin')){if($old.artifacts.$kind.sha256-cne$p.artifacts.$kind.sha256-or$old.artifacts.$kind.bytes-ne$p.artifacts.$kind.bytes-or(@($old.names.$kind)-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'Original artifact/test tuple'}}} +if($p.action-eq'read'-and!(Test-Path -LiteralPath $case)){@{id=$p.id;status='remote_case_absent'}|ConvertTo-Json -Compress;exit 0} +Safe $root $true +if($p.action-eq'prepare'){if(Test-Path -LiteralPath $case){throw 'Existing case retained'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p;@{id=$p.id;prepared=$true}|ConvertTo-Json -Compress;exit 0} +Safe $case $true;$intent=Join-Path $case 'intent.json';Safe $intent $false;MatchIntent (Get-Content -LiteralPath $intent -Raw|ConvertFrom-Json) +if($p.action-eq'read'){$result=Join-Path $case 'result.json';if(!(Test-Path -LiteralPath $result)){$status=if(Test-Path -LiteralPath (Join-Path $case 'started.json')){'started_without_result'}else{'prepared_without_start'};$partial=@{};foreach($kind in @('lib','bin')){foreach($suffix in @('-list.json','-result.json')){$file=Join-Path $case ($kind+$suffix);if(Test-Path -LiteralPath $file){Safe $file $false;if((Get-Item -LiteralPath $file).Length-gt2097152){throw 'Partial report bound'};$partial[$kind+$suffix]=Get-Content -LiteralPath $file -Raw|ConvertFrom-Json}}};@{id=$p.id;status=$status;all_lists_verified=(Test-Path -LiteralPath (Join-Path $case 'all-lists-verified.json'));tests_started=(Test-Path -LiteralPath (Join-Path $case 'tests-started.json'));partial=$partial}|ConvertTo-Json -Depth 16 -Compress;exit 0};Safe $result $false;Get-Content -LiteralPath $result -Raw;exit 0} +function RunUnit($exe,[string]$arguments){$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=$exe;$s.Arguments=$arguments;$s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.WorkingDirectory=$case;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$systemRoot=$env:WINDIR;$s.EnvironmentVariables.Clear();$s.EnvironmentVariables['SystemRoot']=$systemRoot;$s.EnvironmentVariables['WINDIR']=$systemRoot;$s.EnvironmentVariables['PATH']=$systemRoot+'\System32';$s.EnvironmentVariables['TEMP']=$script:temp;$s.EnvironmentVariables['TMP']=$script:temp;$s.EnvironmentVariables['RUST_BACKTRACE']='0';$c=New-Object Diagnostics.Process;$c.StartInfo=$s;if(!$c.Start()){throw 'Unit start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$timed=$false;if(!$c.WaitForExit(18000)){$timed=$true;$c.Kill();if(!$c.WaitForExit(2000)){throw 'Own unit process did not exit'}};if(!$o.Wait(2000)-or!$e.Wait(2000)-or$o.Result.Length-gt1048576-or$e.Result.Length-gt1048576){throw 'Bounded unit output'};return @{exit_code=$c.ExitCode;timed_out=$timed;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') @{execute_once=$true;manifest_sha256=$p.manifest_sha256};$script:temp=Join-Path $case 'tmp';New-Item -ItemType Directory -Path $script:temp|Out-Null;Safe $script:temp $true +$lists=@{};$executables=@{} +foreach($kind in @('lib','bin')){$item=$p.artifacts.$kind;if($item.sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed unit hash'};$exe=Join-Path $case ($kind+'.exe');Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$item.sha256-or(Get-Item -LiteralPath $exe).Length-ne$item.bytes){throw 'Exact unit bytes'};$list=RunUnit $exe '--list --format terse';if($list.exit_code-ne0-or$list.timed_out){throw 'Actual Windows listing'};$text=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($list.stdout_b64));$unexpected=@($text-split'\r?\n'|Where-Object{$_.Trim()-and$_-notmatch'^[A-Za-z0-9_:]+: test$'});if($unexpected.Count){throw 'Only exact terse test entries'};[string[]]$actual=@($text-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''});[Array]::Sort($actual,[StringComparer]::Ordinal);$counts=@{lib=35;bin=7};$count=$counts[$kind];if($actual.Count-ne$count-or($actual-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'All exact names BEFORE any tests'};WriteNew (Join-Path $case ($kind+'-list.json')) @{names=$actual;sha256=$item.sha256;listed=$list};$lists[$kind]=$actual;$executables[$kind]=$exe} +WriteNew (Join-Path $case 'all-lists-verified.json') @{all_lists_before_any_tests=$true;manifest_sha256=$p.manifest_sha256};WriteNew (Join-Path $case 'tests-started.json') @{execute_tests_once=$true} +$reports=@{};foreach($kind in @('lib','bin')){$value=RunUnit $executables[$kind] '--test-threads=1';$value.sha256=$p.artifacts.$kind.sha256;$value.names=$lists[$kind];WriteNew (Join-Path $case ($kind+'-result.json')) $value;$reports[$kind]=$value} +$value=@{id=$p.id;platform='windows';profile=$p.profile;manifest_sha256=$p.manifest_sha256;runner_sha256=$p.runner_sha256;scripts=$p.scripts;linux_proof_sha256=$p.linux_proof_sha256;reports=$reports;all_lists_before_any_tests=$true;bootstrap_administrator_units=$true;local_service_proof=$false;environment_cleared=$true;product_state_writes=$false;rmm_jobs=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 16 -Compress +''' + + +def check_result(manifest, platform, value, payload): + require(value.get('id') == payload['id'] and value.get('platform') == platform and value.get('profile') == PROFILE + and value.get('manifest_sha256') == payload['manifest_sha256'] and value.get('runner_sha256') == manifest['runner_sha256'] + and value.get('scripts') == manifest['scripts'] and value.get('product_state_writes') is False and value.get('rmm_jobs') is False + and value.get('environment_cleared') is True and value.get('all_lists_before_any_tests') is True and set(value['reports']) == set(KINDS[platform]), 'complete native original binding') + if platform == 'linux': require(value.get('network_namespace') is True, 'actual Linux netns proof') + else: require(value.get('linux_proof_sha256') == payload['linux_proof_sha256'] and value.get('local_service_proof') is False, 'original Linux proof and honest account scope') + for kind, item in value['reports'].items(): + require(item['names'] == EXPECTED[platform][kind] and item['sha256'] == manifest['artifacts'][platform][kind]['sha256'] + and item['exit_code'] == 0 and item['timed_out'] is False, 'all native exact test names/artifacts/exits') + raw = base64.b64decode(item['stdout_b64'], validate=True); errors = base64.b64decode(item['stderr_b64'], validate=True) + require(len(raw) <= 1048576 and len(errors) <= 1048576, 'bounded native outputs') + check_output(raw, COUNTS[platform][kind]) + + +def execute(path, platform, access_root, case_id=None): + setup_state(); path = path.absolute(); manifest = verified(path, new_run=case_id is None) + prefix = access_settings(access_root, platform) + if case_id is None: + case_id = str(uuid.uuid4()); folder = STATE / case_id; folder.mkdir(mode=0o700) + payload = dict(id=case_id, profile=PROFILE, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], scripts=manifest['scripts'], + artifacts={kind: {k: manifest['artifacts'][platform][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS[platform]}, names=EXPECTED[platform]) + if platform == 'windows': + proof_path = path.parent / 'linux-passed.json'; proof = load(proof_path) + require(proof['verified'] is True and proof['manifest_sha256'] == sha(path), 'same-manifest real Linux proof FIRST') + linux_payload = dict(payload, id=proof['case_id'], artifacts={kind: {k: manifest['artifacts']['linux'][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS['linux']}, names=EXPECTED['linux']) + check_result(manifest, 'linux', proof['native'], linux_payload) + payload['linux_proof_sha256'] = sha(proof_path) + intent = dict(id=case_id, platform=platform, manifest_path=str(path), manifest_sha256=sha(path), runner_sha256=sha(__file__), + scripts=scripts(), access_root_sha256=hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), payload=payload) + save(folder / 'intent.json', intent) + save(folder / 'started-local.json', dict(create_once=True, no_retry=True)) + run_read = False + else: + require(str(uuid.UUID(case_id)) == case_id, 'original canonical case id') + folder = STATE / case_id; private(folder, True); intent = load(folder / 'intent.json'); load(folder / 'started-local.json') + require(intent['id'] == case_id and intent['platform'] == platform and intent['manifest_path'] == str(path) + and intent['manifest_sha256'] == sha(path) and intent['runner_sha256'] == sha(__file__) and intent['scripts'] == scripts() + and intent['access_root_sha256'] == hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), 'immutable same-case readback') + payload = intent['payload']; run_read = True + rpc = linux_rpc if platform == 'linux' else windows_rpc + if not run_read: + result = rpc(prefix, dict(payload, action='prepare'), folder) + save(folder / 'prepared-remote.json', result); require(result == dict(id=case_id, prepared=True), 'fresh fixed case prepared') + for kind in KINDS[platform]: + destination = LINUX_REMOTE + '/' + case_id + '/' + kind if platform == 'linux' else '/C:/ProgramData/RustDiscoveryBootstrap/windows-msi-runtime-units-' + case_id + '/' + kind + '.exe' + upload(prefix, manifest['artifacts'][platform][kind], destination, folder, kind) + result = rpc(prefix, dict(payload, action='read' if run_read else 'run'), folder) + save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) + if result.get('status'): + require(run_read and result.get('id') == case_id and result['status'] in ('remote_case_absent', 'prepared_without_start', 'started_without_result'), 'fixed read-only incomplete status') + print(json.dumps(dict(verified=False, platform=platform, case_id=case_id, status=result['status'], retry_allowed=False))); return + check_result(manifest, platform, result, payload) + proof = dict(verified=True, case_id=case_id, platform=platform, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], native=result) + destination = path.parent / (platform + '-passed.json') + if destination.exists(): require(load(destination) == proof, 'preserve identical native proof') + else: save(destination, proof) + save(folder / 'verified.json', dict(verified=True, proof_sha256=sha(destination))) if not (folder / 'verified.json').exists() else require(load(folder / 'verified.json') == dict(verified=True, proof_sha256=sha(destination)), 'same verified receipt') + print(json.dumps(dict(verified=True, platform=platform, case_id=case_id, tests=sum(COUNTS[platform].values()), + manifest_sha256=sha(path), proof_sha256=sha(destination)))) + + +def main(): + 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('--access-root', type=Path) + 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.access_root or args.execute_native_tests), 'local build only'); build() + elif args.action.startswith('readback-'): + require(args.manifest and args.case and args.access_root and not args.execute_native_tests, 'original case readback only') + execute(args.manifest, args.action.removeprefix('readback-'), args.access_root, args.case) + else: + require(args.manifest and args.access_root and args.execute_native_tests and not args.case, 'explicit new native platform run') + execute(args.manifest, args.action, args.access_root) + + +if __name__ == '__main__': + try: main() + except Exception as error: + print(json.dumps(dict(error='WINDOWS_MSI_RUNTIME_UNIT_QUALIFICATION_INCOMPLETE', error_class=type(error).__name__, details_suppressed=True))) + raise SystemExit(2) from None diff --git a/deploy/windows_profile_acceptance.py b/deploy/windows_profile_acceptance.py new file mode 100644 index 0000000..767bf79 --- /dev/null +++ b/deploy/windows_profile_acceptance.py @@ -0,0 +1,485 @@ +#!/usr/bin/env python3 +"""Package24 pure profile contracts: Linux VM FIRST, then Windows VM. + +Only two fixed test executables run in separate bootstrap scratch directories. +No RMM job, daemon, live database, PKI, policy, enrollment or capability change. +Native execution is explicit; any lost reply permits same-case readback only. +""" +import argparse +import base64 +import datetime as dt +import hashlib +import inspect +import json +import os +from pathlib import Path +import re +import shlex +import shutil +import stat +import subprocess +import uuid + +import vm_access_check as access + +ROOT = Path(__file__).resolve().parents[1] +STATE = ROOT / 'devstate/windows-profile' +IMAGE = 'rust-discovery-durable-final-build:local' +PROFILE = 'windows-process-profile-v1' +LINUX_REMOTE = '/var/lib/rmm-windows-profile-units' +KINDS = ('domain', 'contracts') +COUNTS = {'linux': {'domain': 16, 'contracts': 7}, 'windows': {'domain': 16, 'contracts': 6}} +DOMAIN_NAMES = sorted(('apt_transaction::tests::closure_bytes_archives_and_code_are_bound', + 'apt_transaction::tests::helper_schema_two_roundtrip_and_strict_action_binding', + 'inventory_document::tests::additive_metadata_and_bounds', + 'inventory_document::tests::exact_linux_recovery_scope_does_not_admit_other_hyphens_or_windows', + 'inventory_document::tests::windows_scope_does_not_weaken_linux_or_accept_platform_spoofing', + 'jobs::tests::additive_recipe_refs_preserve_legacy_plan_bytes', 'jobs::tests::codec_golden_and_exact_script_bytes', + 'jobs::tests::no_terminal_replay_or_unknown_restart', 'jobs::tests::permit_codec_golden_all_bindings', + 'jobs::tests::updates_require_exact_versions_and_never_implicit_reboot', + 'jobs::tests::windows_interpreter_and_capability_contract', + 'jobs::tests::windows_remote_signed_profile_is_optional_exact_and_capability_bound', + 'jobs::tests::windows_system_scm_is_explicit_and_preserves_linux_service_bytes', + 'reconciliation::tests::outcome_codec_keeps_raw_output_and_sorts_evidence', + 'reconciliation::tests::reconciliation_cannot_be_a_permit_or_unbounded_ticket', + 'tests::production_never_accepts_development_auth')) +CONTRACT_NAMES = sorted(('atomic_begin_execution_rolls_back_both_rows_on_missing_witness_or_start_insert_failure', + 'completion_cas_preserves_original_bytes_and_recovers_only_verified_bound_evidence', + 'consumed_attempt_and_unknown_lock_survive_reopen_without_reexecution', + 'exact_permit_bytes_time_and_all_identity_hash_bindings_are_required', + 'historical_request_and_binding_codecs_keep_field_order_arrays_and_strictness', + 'openssl_and_portable_ed25519_agree_on_current_keys_bytes_and_tampering', + 'portable_acceptance_requires_platform_capability_and_exact_redelivery')) +UNIX_ONLY = 'openssl_and_portable_ed25519_agree_on_current_keys_bytes_and_tampering' +EXPECTED = {'linux': {'domain': DOMAIN_NAMES, 'contracts': CONTRACT_NAMES}, + 'windows': {'domain': DOMAIN_NAMES, 'contracts': [x for x in CONTRACT_NAMES if x != UNIX_ONLY]}} + + +def require(value, code): + if not value: + raise RuntimeError(code) + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def object_pairs(items): + value = {} + for key, item in items: + require(key not in value, 'duplicate JSON field') + value[key] = item + return value + + +def private(path, directory=False): + item = Path(path).lstat() + require(not stat.S_ISLNK(item.st_mode) and item.st_uid == os.getuid() + and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600) + and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1), + 'private owned regular path') + + +def load(path): + private(path) + require(Path(path).stat().st_size <= 2 * 1024 * 1024, 'bounded private JSON') + return json.loads(Path(path).read_bytes(), object_pairs_hook=object_pairs) + + +def save(path, value): + private(Path(path).parent, True) + fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW | os.O_WRONLY, 0o600) + with os.fdopen(fd, 'w') as stream: + json.dump(value, stream, indent=2); stream.write('\n'); stream.flush(); os.fsync(stream.fileno()) + + +def setup_state(): + os.umask(0o077) + STATE.parent.mkdir(mode=0o700, exist_ok=True) + metadata = STATE.parent.lstat() + require(stat.S_ISDIR(metadata.st_mode) and metadata.st_uid == os.getuid() and not metadata.st_mode & 0o002, + 'owned non-world-writable devstate parent') + STATE.mkdir(mode=0o700, exist_ok=True) + private(STATE, True) + + +def source_files(): + paths = [ROOT / p for p in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', + 'deploy/windows_profile_acceptance.py', 'deploy/remote_client_bootstrap.py', + 'deploy/vm_access_check.py', 'deploy/vm_mvp_acceptance.py', 'deploy/windows/client_bootstrap.py')] + paths += list((ROOT / 'crates').rglob('Cargo.toml')) + for name in ('domain', 'agent-core', 'protocol'): + paths += [p for p in (ROOT / 'crates' / name).rglob('*') if p.is_file()] + paths += [p for p in (ROOT / 'proto').rglob('*') if p.is_file()] + for path in paths: + require(not path.is_symlink() and path.is_file(), 'regular source inputs only') + return {str(p.relative_to(ROOT)): sha(p) for p in sorted(set(paths))} + + +def names(raw): + return sorted(re.findall(r'^([A-Za-z0-9_:]+): test\r?$', raw, re.M)) + + +def check_list(raw, expected): + require(names(raw) == expected and len(set(expected)) == len(expected), 'all exact test names before execution') + require(all(not line.strip() or re.fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in raw.splitlines()), + 'terse list contains only exact tests, no benchmarks or extra entries') + + +def check_output(raw, count): + require(re.search(rb'^test result: ok\. ' + str(count).encode() + + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;[^\r\n]*\r?$', raw, re.M), + 'all tests passed without skipped or filtered tests') + + +def command(args, folder, timeout=300): + index = len(list(folder.glob('command-*.json'))) + try: + done = subprocess.run(args, capture_output=True, timeout=timeout) + save(folder / ('command-%03d.json' % index), dict(returncode=done.returncode, + stdout_b64=base64.b64encode(done.stdout).decode(), stderr_b64=base64.b64encode(done.stderr).decode())) + require(done.returncode == 0, 'local qualification command failed; private output retained') + return done.stdout + except subprocess.TimeoutExpired as error: + save(folder / ('command-%03d.json' % index), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('local qualification timed out; no automatic retry') from None + + +def scripts(): + return {'linux_case_sha256': hashlib.sha256(inspect.getsource(linux_case).encode()).hexdigest(), + 'windows_script_sha256': hashlib.sha256(WINDOWS.encode()).hexdigest()} + + +def build(): + setup_state() + folder = STATE / ('build-' + str(uuid.uuid4())); folder.mkdir(mode=0o700) + before = source_files(); save(folder / 'source-before.json', before) + image = command(['docker', 'image', 'inspect', '--format', '{{.Id}}', IMAGE], folder).decode().strip() + require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable local build image') + target = STATE / 'target'; target.mkdir(mode=0o700, exist_ok=True); private(target, 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, triple in [('linux', 'x86_64-unknown-linux-gnu'), ('windows', 'x86_64-pc-windows-gnu')]: + for kind in KINDS: + package, selection = ('rmm-domain', ['--lib']) if kind == 'domain' else ('rmm-agent-core', ['--test', 'contracts']) + options = ['--manifest-path', '/src/Cargo.toml', '--locked', '--offline', '--target', triple, '-p', package] + command(common + ['clippy'] + options + selection + ['--', '-D', 'warnings'], folder) + raw = command(common + ['test'] + options + selection + ['--release', '--no-run', '--message-format=json'], folder) + linked = [] + for line in raw.splitlines(): + try: item = json.loads(line) + except ValueError: continue + expected_name = 'rmm_domain' if kind == 'domain' else 'contracts' + if item.get('reason') == 'compiler-artifact' and item.get('executable') and item.get('profile', {}).get('test') and item['target']['name'] == expected_name: + linked.append(target / Path(item['executable']).relative_to('/target')) + require(len(linked) == 1, 'exact selected unit executable') + destination = folder / platform; destination.mkdir(mode=0o700, exist_ok=True) + original = destination / (kind + '.linked') + delivery = destination / (kind + ('.exe' if platform == 'windows' else '')) + for path in (original, delivery): shutil.copyfile(linked[0], path); path.chmod(0o600) + linked_sha = sha(original) + tool = 'x86_64-w64-mingw32-strip' if platform == 'windows' else 'strip' + command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', str(destination) + ':/artifacts', + '--entrypoint', tool, image, '--strip-debug', '/artifacts/' + delivery.name], folder) + require(sha(linked[0]) == sha(original) == linked_sha, 'unstripped original link preserved') + data = delivery.read_bytes() + if platform == 'linux': + require(data[:6] == b'\x7fELF\x02\x01' and data[18:20] == b'\x3e\0', 'Linux x64 ELF') + else: + offset = int.from_bytes(data[60:64], 'little') + require(data[:2] == b'MZ' and data[offset:offset + 6] == b'PE\0\0\x64\x86', 'Windows x64 PE') + artifacts[platform][kind] = dict(path=str(delivery.resolve()), sha256=sha(delivery), bytes=len(data), + original_link_path=str(original.resolve()), original_link_sha256=linked_sha, debug_stripped_delivery=True) + # Run exactly the deliverable Linux bytes in an offline disposable container. + for kind, item in artifacts['linux'].items(): + invoke = ['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,exec,size=128m', + '-v', item['path'] + ':/unit:ro', '--entrypoint', 'python3', image, '-c'] + for mode in ('list', 'run'): + args = ['--list', '--format', 'terse'] if mode == 'list' else ['--test-threads=1'] + script = 'import shutil,os,subprocess;shutil.copyfile("/unit","/tmp/unit");os.chmod("/tmp/unit",0o500);subprocess.run(["/tmp/unit"]+' + repr(args) + ',check=True,cwd="/tmp",env={"PATH":"/usr/bin:/bin","LANG":"C.UTF-8","TMPDIR":"/tmp","RUST_BACKTRACE":"0"})' + raw = command(invoke + [script], folder) + save(folder / ('local-linux-' + kind + '-' + mode + '.json'), dict(stdout_b64=base64.b64encode(raw).decode())) + if mode == 'list': check_list(raw.decode(), EXPECTED['linux'][kind]) + else: check_output(raw, COUNTS['linux'][kind]) + dynamic = command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', item['path'] + ':/unit:ro', + '--entrypoint', 'readelf', image, '-d', '/unit'], folder).decode() + versions = command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', item['path'] + ':/unit:ro', + '--entrypoint', 'readelf', image, '--version-info', '/unit'], folder).decode() + glibc = sorted(set(re.findall(r'GLIBC_(\d+\.\d+)', versions)), key=lambda x: tuple(map(int, x.split('.')))) + require(glibc and tuple(map(int, glibc[-1].split('.'))) <= (2, 39), 'VM-compatible glibc requirement') + item['glibc_max'] = glibc[-1]; item['needed'] = sorted(re.findall(r'Shared library: \[([^\]]+)\]', dynamic)) + require(source_files() == before, 'source and runner unchanged through qualification') + manifest = dict(schema_version=1, profile=PROFILE, source_files=before, source_before_after_equal=True, + runner_sha256=sha(__file__), scripts=scripts(), test_names=EXPECTED, test_counts=COUNTS, artifacts=artifacts, + build_image_id=image, both_targets_clippy_D_warnings=True, linux_lists_actual=True, + windows_lists_expected_until_native=True, local_linux_tests_passed=23, native_linux_executed=False, + native_windows_executed=False, product_state_writes=False, rmm_jobs=False) + save(folder / 'manifest.json', manifest) + print(json.dumps(dict(manifest=str((folder / 'manifest.json').resolve()), manifest_sha256=sha(folder / 'manifest.json'), + linux_tests=23, windows_tests_expected=22, native_executed=False))) + + +def verified(path, new_run): + path = path.absolute(); private(path.parent, True) + require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}', path.parent.name), 'fixed build manifest path') + value = load(path) + require(value['schema_version'] == 1 and value['profile'] == PROFILE and value['runner_sha256'] == sha(__file__) + and value['scripts'] == scripts() and value['test_names'] == EXPECTED and value['test_counts'] == COUNTS + and value['source_before_after_equal'] is True and value['both_targets_clippy_D_warnings'] is True, + 'original manifest/runner/remote scripts/profile binding') + if new_run: require(value['source_files'] == source_files(), 'new execution requires same current qualified source') + for platform in COUNTS: + require(set(value['artifacts'][platform]) == set(KINDS), 'exact two artifacts') + for kind, item in value['artifacts'][platform].items(): + suffix = '.exe' if platform == 'windows' else '' + require(Path(item['path']) == path.parent / platform / (kind + suffix) + and Path(item['original_link_path']) == path.parent / platform / (kind + '.linked'), 'fixed private artifact paths') + for field, digest in [('path', item['sha256']), ('original_link_path', item['original_link_sha256'])]: + private(item[field]); require(sha(item[field]) == digest, 'immutable original/delivery bytes') + require(Path(item['path']).stat().st_size == item['bytes'], 'artifact size unchanged') + return value + + +def access_settings(root, platform): + """Read strictly parsed existing access only when the parent executes native.""" + require(root is not None and not root.is_symlink(), 'explicit real access-root required') + root = root.absolute(); require(root.is_dir() and root.resolve() == root, 'canonical access-root') + if platform == 'linux': + cfg = access.load_config(root / 'credentials/linux-vm.conf') + require(cfg['address'] == '116.203.20.85' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed Cloud Linux bootstrap target') + prefix = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()), 'root@116.203.20.85'] + else: + cfg = access.load_config(root / 'credentials/server.conf') + require(cfg['address'] == '88.99.80.202' and cfg['username'] == 'root' and cfg.get('port', '22') == '22' + and cfg.get('ssh_key_path'), 'fixed server reverse-jump only') + credentials = root / 'credentials'; private(credentials, True) + record = load(credentials / 'windows-dev-access.json') + require(set(record) == {'ssh_prefix', 'expires_at'}, 'known Windows access schema') + expires = dt.datetime.fromisoformat(record['expires_at']) + require(expires.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expires, 'existing dev access still valid') + key = str(Path(cfg['ssh_key_path']).expanduser()) + jump = ['ssh', '-F', '/dev/null', '-T', '-p', '22', '-i', key, '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'ForwardAgent=no', '-o', 'IdentitiesOnly=yes', '-W', '127.0.0.1:22240', 'root@88.99.80.202'] + prefix = ['ssh', '-F', '/dev/null', '-T', '-i', key, '-o', 'ProxyCommand=' + shlex.join(jump), + '-o', 'HostKeyAlias=rmm-windows-dev', '-o', 'UserKnownHostsFile=' + str(credentials / 'windows-dev.known_hosts'), + '-o', 'GlobalKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', 'rmm-dev@127.0.0.1'] + require(record['ssh_prefix'] == prefix, 'exact registered pinned reverse SSH prefix') + private(credentials / 'windows-dev.known_hosts') + return prefix + + +def transport(prefix, command_text, data, folder, timeout=60): + """No stdout/stderr escapes the private case directory on transport failure.""" + try: + result = subprocess.run(prefix + [command_text], input=data, capture_output=True, timeout=timeout) + save(folder / ('transport-' + str(uuid.uuid4()) + '.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'native transport incomplete; same-case readback only') + value = json.loads(result.stdout.decode('utf-8-sig'), object_pairs_hook=object_pairs) + require(isinstance(value, dict) and 'bootstrap_error' not in value, 'native bootstrap incomplete; private receipt retained') + return value + except subprocess.TimeoutExpired as error: + save(folder / ('transport-timeout-' + str(uuid.uuid4()) + '.json'), dict(timed_out=True, + stdout_b64=base64.b64encode(error.stdout or b'').decode(), stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('native reply lost; read original case only') from None + + +def linux_rpc(prefix, payload, folder): + code = 'import os,json,subprocess,base64,hashlib,uuid\nfrom pathlib import Path\n' + inspect.getsource(require) + '\n' + inspect.getsource(linux_case) + code += '\ntry:\n print(json.dumps(linux_case(' + repr(payload) + ')))\nexcept Exception as e:\n print(json.dumps(dict(bootstrap_error=True,error_class=type(e).__name__)))\n' + return transport(prefix, 'python3 -', code.encode(), folder) + + +def windows_rpc(prefix, payload, folder): + script = 'try {\n' + WINDOWS + '\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}' + command_text = 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand ' + base64.b64encode(script.encode('utf-16le')).decode() + return transport(prefix, command_text, (json.dumps(payload) + '\n').encode(), folder) + + +def upload(prefix, item, destination, folder, kind): + args = ['sftp'] + [part for part in prefix[1:-1] if part != '-T'] + ['-b', '-', prefix[-1]] + # Linux sftp uses uppercase -P; Windows prefix has no -p option. + if '-p' in args: args[args.index('-p')] = '-P' + batch = ('put ' + json.dumps(item['path']) + ' ' + json.dumps(destination) + '\n').encode() + save(folder / ('transfer-' + kind + '-intent.json'), dict(sha256=item['sha256'], bytes=item['bytes'])) + try: + result = subprocess.run(args, input=batch, capture_output=True, timeout=60) + save(folder / ('transfer-' + kind + '-result.json'), dict(returncode=result.returncode, + stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + require(result.returncode == 0, 'fixed artifact transfer failed; no retry') + except subprocess.TimeoutExpired as error: + save(folder / ('transfer-' + kind + '-timeout.json'), dict(timed_out=True, + stderr_b64=base64.b64encode(error.stderr or b'').decode())) + raise RuntimeError('artifact transfer incomplete; no retry') from None + + +def linux_case(p): + import resource + import stat + ident = str(uuid.UUID(p['id'])) + require(ident == p['id'] and p['action'] in ('prepare', 'run', 'read') and p['profile'] == 'windows-process-profile-v1', 'fixed unit case') + require(os.geteuid() == 0 and os.uname().machine == 'x86_64' and set(p['artifacts']) == {'domain', 'contracts'}, 'fixed Linux root qualification') + root = Path('/var/lib/rmm-windows-profile-units') + def safe(path, directory): + s = path.lstat() + require(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 unit intent') + if p['action'] == 'read': + if not (case / 'result.json').exists(): return dict(id=ident, status='started_without_result' if (case / 'started.json').exists() else 'prepared_without_start') + safe(case / 'result.json', False); 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/bin:/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, (15, 15)); resource.setrlimit(resource.RLIMIT_FSIZE, (1048576, 1048576)) + 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 ('domain', 'contracts'): + binary = case / kind; safe(binary, False) + require(hashlib.sha256(binary.read_bytes()).hexdigest() == p['artifacts'][kind]['sha256'], 'exact Linux delivery') + binary.chmod(0o500) + listed = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--list', '--format', 'terse'], cwd=case, env=env, capture_output=True, timeout=5, 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')) + require(all(not line.strip() or __import__('re').fullmatch(r'[A-Za-z0-9_:]+: test', line) for line in listed.stdout.decode().splitlines()) + and actual == p['names'][kind] and len(actual) == {'domain': 16, 'contracts': 7}[kind], 'exact native test names before ANY test execution') + listings[kind] = actual + reports = {} + for kind in ('domain', 'contracts'): + with (case / (kind + '-stdout.bin')).open('xb') as out, (case / (kind + '-stderr.bin')).open('xb') as err: + timed = False + 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=18, 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]) + value = dict(id=ident, platform='linux', profile=p['profile'], manifest_sha256=p['manifest_sha256'], runner_sha256=p['runner_sha256'], + scripts=p['scripts'], reports=reports, network_namespace=True, environment_cleared=True, product_state_writes=False, rmm_jobs=False) + write(case / 'result.json', value); return value + + +WINDOWS = r''' +$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json) +if([string]([guid]$p.id)-cne$p.id-or$p.action-notin@('prepare','run','read')-or$p.profile-cne'windows-process-profile-v1'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.linux_proof_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed Linux-first profile case'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('windows-profile-'+$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 'Unit path type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private unit owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private unit ACL'}}} +function WriteNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 16 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +function MatchIntent($old){if($old.id-cne$p.id-or$old.profile-cne$p.profile-or$old.manifest_sha256-cne$p.manifest_sha256-or$old.runner_sha256-cne$p.runner_sha256-or$old.linux_proof_sha256-cne$p.linux_proof_sha256-or$old.scripts.linux_case_sha256-cne$p.scripts.linux_case_sha256-or$old.scripts.windows_script_sha256-cne$p.scripts.windows_script_sha256){throw 'Original profile intent'};foreach($kind in @('domain','contracts')){if($old.artifacts.$kind.sha256-cne$p.artifacts.$kind.sha256-or$old.artifacts.$kind.bytes-ne$p.artifacts.$kind.bytes-or(@($old.names.$kind)-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'Original artifact/test tuple'}}} +if($p.action-eq'read'-and!(Test-Path -LiteralPath $case)){@{id=$p.id;status='remote_case_absent'}|ConvertTo-Json -Compress;exit 0} +Safe $root $true +if($p.action-eq'prepare'){if(Test-Path -LiteralPath $case){throw 'Existing case retained'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p;@{id=$p.id;prepared=$true}|ConvertTo-Json -Compress;exit 0} +Safe $case $true;$intent=Join-Path $case 'intent.json';Safe $intent $false;MatchIntent (Get-Content -LiteralPath $intent -Raw|ConvertFrom-Json) +if($p.action-eq'read'){$result=Join-Path $case 'result.json';if(!(Test-Path -LiteralPath $result)){$status=if(Test-Path -LiteralPath (Join-Path $case 'started.json')){'started_without_result'}else{'prepared_without_start'};@{id=$p.id;status=$status}|ConvertTo-Json -Compress;exit 0};Safe $result $false;Get-Content -LiteralPath $result -Raw;exit 0} +function RunUnit($exe,[string]$arguments){$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=$exe;$s.Arguments=$arguments;$s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.WorkingDirectory=$case;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$systemRoot=$env:WINDIR;$s.EnvironmentVariables.Clear();$s.EnvironmentVariables['SystemRoot']=$systemRoot;$s.EnvironmentVariables['WINDIR']=$systemRoot;$s.EnvironmentVariables['PATH']=$systemRoot+'\System32';$s.EnvironmentVariables['TEMP']=$script:temp;$s.EnvironmentVariables['TMP']=$script:temp;$s.EnvironmentVariables['RUST_BACKTRACE']='0';$c=New-Object Diagnostics.Process;$c.StartInfo=$s;if(!$c.Start()){throw 'Unit start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$timed=$false;if(!$c.WaitForExit(18000)){$timed=$true;$c.Kill();if(!$c.WaitForExit(2000)){throw 'Own unit process did not exit'}};if(!$o.Wait(2000)-or!$e.Wait(2000)-or$o.Result.Length-gt1048576-or$e.Result.Length-gt1048576){throw 'Bounded unit output'};return @{exit_code=$c.ExitCode;timed_out=$timed;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') @{execute_once=$true;manifest_sha256=$p.manifest_sha256};$script:temp=Join-Path $case 'tmp';New-Item -ItemType Directory -Path $script:temp|Out-Null;Safe $script:temp $true +$lists=@{};$executables=@{} +foreach($kind in @('domain','contracts')){$item=$p.artifacts.$kind;if($item.sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed unit hash'};$exe=Join-Path $case ($kind+'.exe');Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$item.sha256-or(Get-Item -LiteralPath $exe).Length-ne$item.bytes){throw 'Exact unit bytes'};$list=RunUnit $exe '--list --format terse';if($list.exit_code-ne0-or$list.timed_out){throw 'Actual Windows listing'};$text=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($list.stdout_b64));$unexpected=@($text-split'\r?\n'|Where-Object{$_.Trim()-and$_-notmatch'^[A-Za-z0-9_:]+: test$'});if($unexpected.Count){throw 'Only exact terse test entries'};$actual=@($text-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''}|Sort-Object);$count=if($kind-eq'domain'){16}else{6};if($actual.Count-ne$count-or($actual-join"`n")-cne(@($p.names.$kind)-join"`n")){throw 'All exact names BEFORE any tests'};$lists[$kind]=$actual;$executables[$kind]=$exe} +$reports=@{};foreach($kind in @('domain','contracts')){$value=RunUnit $executables[$kind] '--test-threads=1';$value.sha256=$p.artifacts.$kind.sha256;$value.names=$lists[$kind];WriteNew (Join-Path $case ($kind+'-result.json')) $value;$reports[$kind]=$value} +$value=@{id=$p.id;platform='windows';profile=$p.profile;manifest_sha256=$p.manifest_sha256;runner_sha256=$p.runner_sha256;scripts=$p.scripts;linux_proof_sha256=$p.linux_proof_sha256;reports=$reports;bootstrap_administrator_units=$true;local_service_proof=$false;environment_cleared=$true;product_state_writes=$false;rmm_jobs=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 16 -Compress +''' + + +def check_result(manifest, platform, value, payload): + require(value.get('id') == payload['id'] and value.get('platform') == platform and value.get('profile') == PROFILE + and value.get('manifest_sha256') == payload['manifest_sha256'] and value.get('runner_sha256') == manifest['runner_sha256'] + and value.get('scripts') == manifest['scripts'] and value.get('product_state_writes') is False and value.get('rmm_jobs') is False + and value.get('environment_cleared') is True and set(value['reports']) == set(KINDS), 'complete native original binding') + if platform == 'linux': require(value.get('network_namespace') is True, 'actual Linux netns proof') + else: require(value.get('linux_proof_sha256') == payload['linux_proof_sha256'] and value.get('local_service_proof') is False, 'original Linux proof and honest account scope') + for kind, item in value['reports'].items(): + require(item['names'] == EXPECTED[platform][kind] and item['sha256'] == manifest['artifacts'][platform][kind]['sha256'] + and item['exit_code'] == 0 and item['timed_out'] is False, 'all native exact test names/artifacts/exits') + raw = base64.b64decode(item['stdout_b64'], validate=True); errors = base64.b64decode(item['stderr_b64'], validate=True) + require(len(raw) <= 1048576 and len(errors) <= 1048576, 'bounded native outputs') + check_output(raw, COUNTS[platform][kind]) + + +def execute(path, platform, access_root, case_id=None): + setup_state(); path = path.absolute(); manifest = verified(path, new_run=case_id is None) + prefix = access_settings(access_root, platform) + if case_id is None: + case_id = str(uuid.uuid4()); folder = STATE / case_id; folder.mkdir(mode=0o700) + payload = dict(id=case_id, profile=PROFILE, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], scripts=manifest['scripts'], + artifacts={kind: {k: manifest['artifacts'][platform][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS}, names=EXPECTED[platform]) + if platform == 'windows': + proof_path = path.parent / 'linux-passed.json'; proof = load(proof_path) + require(proof['verified'] is True and proof['manifest_sha256'] == sha(path), 'same-manifest real Linux proof FIRST') + linux_payload = dict(payload, id=proof['case_id'], artifacts={kind: {k: manifest['artifacts']['linux'][kind][k] for k in ('sha256', 'bytes')} for kind in KINDS}, names=EXPECTED['linux']) + check_result(manifest, 'linux', proof['native'], linux_payload) + payload['linux_proof_sha256'] = sha(proof_path) + intent = dict(id=case_id, platform=platform, manifest_path=str(path), manifest_sha256=sha(path), runner_sha256=sha(__file__), + scripts=scripts(), access_root_sha256=hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), payload=payload) + save(folder / 'intent.json', intent) + save(folder / 'started-local.json', dict(create_once=True, no_retry=True)) + run_read = False + else: + require(str(uuid.UUID(case_id)) == case_id, 'original canonical case id') + folder = STATE / case_id; private(folder, True); intent = load(folder / 'intent.json'); load(folder / 'started-local.json') + require(intent['id'] == case_id and intent['platform'] == platform and intent['manifest_path'] == str(path) + and intent['manifest_sha256'] == sha(path) and intent['runner_sha256'] == sha(__file__) and intent['scripts'] == scripts() + and intent['access_root_sha256'] == hashlib.sha256(str(access_root.absolute()).encode()).hexdigest(), 'immutable same-case readback') + payload = intent['payload']; run_read = True + rpc = linux_rpc if platform == 'linux' else windows_rpc + if not run_read: + result = rpc(prefix, dict(payload, action='prepare'), folder) + save(folder / 'prepared-remote.json', result); require(result == dict(id=case_id, prepared=True), 'fresh fixed case prepared') + for kind in KINDS: + destination = LINUX_REMOTE + '/' + case_id + '/' + kind if platform == 'linux' else '/C:/ProgramData/RustDiscoveryBootstrap/windows-profile-' + case_id + '/' + kind + '.exe' + upload(prefix, manifest['artifacts'][platform][kind], destination, folder, kind) + result = rpc(prefix, dict(payload, action='read' if run_read else 'run'), folder) + save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) + if result.get('status'): + require(run_read and result.get('id') == case_id and result['status'] in ('remote_case_absent', 'prepared_without_start', 'started_without_result'), 'fixed read-only incomplete status') + print(json.dumps(dict(verified=False, platform=platform, case_id=case_id, status=result['status'], retry_allowed=False))); return + check_result(manifest, platform, result, payload) + proof = dict(verified=True, case_id=case_id, platform=platform, manifest_sha256=sha(path), runner_sha256=manifest['runner_sha256'], native=result) + destination = path.parent / (platform + '-passed.json') + if destination.exists(): require(load(destination) == proof, 'preserve identical native proof') + else: save(destination, proof) + save(folder / 'verified.json', dict(verified=True, proof_sha256=sha(destination))) if not (folder / 'verified.json').exists() else require(load(folder / 'verified.json') == dict(verified=True, proof_sha256=sha(destination)), 'same verified receipt') + print(json.dumps(dict(verified=True, platform=platform, case_id=case_id, tests=sum(COUNTS[platform].values()), + manifest_sha256=sha(path), proof_sha256=sha(destination)))) + + +def main(): + 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('--access-root', type=Path) + 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.access_root or args.execute_native_tests), 'local build only'); build() + elif args.action.startswith('readback-'): + require(args.manifest and args.case and args.access_root and not args.execute_native_tests, 'original case readback only') + execute(args.manifest, args.action.removeprefix('readback-'), args.access_root, args.case) + else: + require(args.manifest and args.access_root and args.execute_native_tests and not args.case, 'explicit new native platform run') + execute(args.manifest, args.action, args.access_root) + + +if __name__ == '__main__': + try: main() + except Exception as error: + print(json.dumps(dict(error='WINDOWS_PROFILE_QUALIFICATION_INCOMPLETE', error_class=type(error).__name__, details_suppressed=True))) + raise SystemExit(2) from None diff --git a/docs/development.md b/docs/development.md index 4af9fae..87933e9 100644 --- a/docs/development.md +++ b/docs/development.md @@ -1,6 +1,6 @@ # Linux-first-Entwicklung und reale VM-Abnahme -Stand: 2026-11-14 · Spezifikation 1.18. Das begrenzte Linux-MVP ist inzwischen +Stand: 2026-11-14 · Spezifikation 1.20. Das begrenzte Linux-MVP ist inzwischen dokumentiert abgenommen; aktuelle Nachweise stehen in [handover.md](handover.md). Die getrennte Windows-SYSTEM-Grenze ist in Spezifikation0.1 und [windows-system-policy.md](windows-system-policy.md) präzisiert; Quellen-/Build- @@ -17,6 +17,47 @@ Deploymentnachweise: [server-deployment.md](server-deployment.md). Die folgenden PC-Server-/Reverse-Tunnel-Abläufe und Nachweise sind historisch. TLS/mTLS, Credentialschutz, Linux-first und WebUI-Plangenehmigung gelten weiter. +## Vorrang: genehmigte Batch-Skriptkonsole (Version1.20) + +Der User priorisiert vollständige Shell-/PowerShell-Skripte über MCP/UI mit +expliziten Root-/SYSTEM-Rechten. Kapitel0.1 bindet das neue optionale +`privileged_script_profile="full_maintenance_v1"` vor Genehmigung in Payload, +Policy und WebUI. Alte Pläne/Profile bleiben unverändert; SCM/APT/MSI dürfen +keine Vollwartung durch eine stille Lockerung erhalten. Bisherige M7-Aussagen +zu Terminal/Sitzungen verschieben diese Batchkonsole nicht. Agent-Selbstupdates +werden separat behandelt; Paketfixture-Abnahmen ersetzen sie nicht. + +Geplanter, noch nicht ausgeführter Ablauf: additive bytekompatible Verträge und +Policygrenzen implementieren; lokale betroffene Kompatibilitäts-/Fehlerprüfungen; +reviewbare explizite Helper-/Policymigration unter Erhalt aller Identitäten, +Journaldateien und Verbräuche; qualifizierte Linux-Cloud-Artefakte; neuer fester +WebUI-genehmigter harmloser Vollwartungsplan auf der Linux-Cloud-VM; erst danach +getrennte native Windows-SYSTEM-Abnahme auf dem Windowsgast. Kein RMM-Agent oder +Server auf dem Notebook. Vor Zieländerungen konkrete Startbedingungen/Umfang +festhalten, keine Ledgerresets oder impliziten Reboots/Updates. + +Abnahme muss gespeicherte vollständige Skriptbytes samt Profil/Identität/Limits +vor tatsächlicher WebUI-Genehmigung zeigen, vor Genehmigung Job-/Outbox-/Grant- +Abwesenheit belegen und danach ehrliche Zustände/finale begrenzte stdout-/stderr- +Rohbytes mit Trunkierung erfassen. Keine laufende Ausgabe vortäuschen: bestehende +Jobstrecke liefert vorerst Abschlussausgabe. LinuxUID0 und WindowsSID18 sowie +Clock/Sync/Peer-/Originalsignatur-/Einmalverbrauchsgrenzen getrennt nachweisen. +Abbruch/Fence/Timeout prüfen nur im abgestimmten harmlosen Scope; unklare Wirkung +bleibt unknown, kein blindes Wiederholen. Root/SYSTEM kann auch eigene Schutz- +mechanismen ändern; keine Sandbox-/No-Reboot- oder vollständige Kindprozess- +Eindämmungszusage. Ein Skript-Reboot ersetzt keine typisierte Rebootabnahme. + +Die Norm ist beschlossen; lokale Quellen-/Buildnachweise und deren Grenzen +stehen in [deploy/linux/full-maintenance-README.md](../deploy/linux/full-maintenance-README.md). +Ein unabhängiger Review fand im ersten eingefrorenen Pack die fehlende tatsächliche +Linux-Helperbinarybindung; dieser Pack bleibt nur früherer Beleg. Der enge +Full-only-Corefix wurde unabhängig geprüft und im neuen Pack29461892 mit158 +unveränderten Inputs,285 lokalen Tests und45 tatsächlichen isolierten DB-Verträgen +neu qualifiziert. Der separate Native-Runner ist vorbereitet, noch nicht +Zielaktivierung oder VM-Abnahme; diese bleiben offen. Die bestehende begrenzte +Linux-MVP-Abnahme wird nicht nachträglich zur Vollwartungsabnahme umgedeutet. +Details/technischer Entwurf: [script-console-contract-proposal.md](script-console-contract-proposal.md). + ## Begrenzte Windowsfreigaben (2026-11-14) Der User erlaubt `RemoteSigned` ausschließlich je zuvor WebUI-genehmigtem diff --git a/docs/handover.md b/docs/handover.md index a156cb3..07ee81a 100644 --- a/docs/handover.md +++ b/docs/handover.md @@ -1,6 +1,22 @@ # Übergabe — Linux-MVP abgeschlossen, Windows wird fortgesetzt -**Aktueller Betriebsstand der Skriptkonsole2026-11-14:** Cloudserver und WebUI +**Fortschreibung2026-11-14 nach tatsächlicher Skriptabnahme:** Die vollständige +MCP→WebUI→Rootskriptstrecke ist auf der Cloud-Linux-VM bestanden. V2-Fall +ced97b4d, ursprüngliche Plan-/Skriptbytes in der WebUI verglichen, +SIMULIERT-Freigabeklick, genau ein erfolgreicher UID0-Lauf; stdout28/stderr27 +Byte auch tatsächlich über die WebUI heruntergeladen und verglichen. +MCP-Ergebnis identisch, zwei authentisierte Fremdmandantenabfragen verweigert. +Root19/0, ServerLinux31Jobs/31Outbox/27Grants/0ungeklärt; Windows weiter0/0/0/0. +Beide temporären Rootscopes geschlossen, Originalpolicycf15cbb6 lesend +bestätigt. Details und Grenzen: [Linux-Skriptresultat](script-console-linux-full-results.md), +Gitea76440e1. Vollständiger dreiteiliger passiver Nachbeleg ebenfalls bestanden, +Giteaefd310b. Automation hat explizit root:execute erhalten, keine +Genehmigungsrechte. Noch offen bleiben Windowsclock/PowerShell/SYSTEM, +Selbstupdater-Runtime und native Applynachweise sowie konsistente private +aktuelle Linux-/Serverruntimeindizes. Ältere Angaben darunter sind historische +Zwischenstände. Kein vollständiger Windows-/Updaterabschluss. + +**Vorheriger Betriebsstand der Skriptkonsole2026-11-14:** Cloudserver und WebUI sind im separaten Fall14dcc27d tatsächlich erhaltend bereitgestellt (ARM64-Image 6f8383f7, Migration14; alte Geschichte/Identitäten unverändert). Die drei neuen Linux-Dienste Agent0a7ad6ee sowie General/Full79442727 laufen nach einmaligen diff --git a/docs/parallel-mvp-status.md b/docs/parallel-mvp-status.md index d02361d..e16dac2 100644 --- a/docs/parallel-mvp-status.md +++ b/docs/parallel-mvp-status.md @@ -50,13 +50,24 @@ Aktuelle Windowsunits36+7 im Fall76bb47dd danach vollständig bestanden, keineSkips/Filter, separatesReadback. [Beleg5b2f07b](https://gitea.incoso.eu/kevin_janssen/rust-discovery/src/commit/5b2f07b/docs/script-console-windows-link.md). Das ersetzt keine laufende Windows-Runtime-/Clock-/SYSTEM-Abnahme. -Neue WebUI privat mit tatsächlich geprüftem TLS und aktiver Chromium-Sandbox -sichtbar als simulierter Testprincipal angemeldet. Noch kein neuer Plan/Job oder -Approval; die vollständige native passive Nachattestation wird vervollständigt. -Für MCP-Rootplanung fehlt dem bestehenden Automation-Testprincipal noch exakt -root:execute; der konkrete begrenzte Provisionierungsschritt wird vorbereitet, -ohne Genehmigungsrechte zu übertragen. Die folgenden älteren Zwischenstände -sind historische Belege und werden durch diesen Absatz aktualisiert. +Danach vollständige native passive Nachattestation mit drei Beobachtungen +bestanden (efd310b), ursprüngliche Identität/PKI/DBinodes/Root18 sowie +SignedSync und gespeicherte Kontakt-/Inventarfortschritte nachgewiesen. +Automation erhielt ausdrücklich nur das fehlende root:execute, keine +Genehmigungsrechte (afccf95). + +**Die erste vollständige Skriptkonsole ist auf Cloud-Linux nativ bestanden +(76440e1):** Einmal MCPplan/submit, persistentes pending_approval mit null +ausführbaren Artefakten; echte WebUI mit geprüftem TLS und Chromium-Sandbox, +Originalplanbytes vollständig verglichen, sichtbarer SIMULIERT-Freigabeklick. +Ein Root/Fullskript, Exit0, stdout28/stderr27Byte ungekürzt und bytegenau auch +über echte WebUI-Downloads geprüft; MCP-Ergebnis gleich. Zwei authentisierte +MCP-Fremdmandantenabfragen verweigert. Native Root19/0 mit18 unveränderten +Originalzeilen, ServerLinux31/31/27/0, Windows0/0/0/0. Der Zwischenzustand +running wurde im Browser verpasst und nicht durch Wiederholung erzeugt. +Temporäre Scopes geschlossen; Originalpolicycf15cbb6 separat lesend bestätigt. +[Genaue Ergebnisse und Grenzen](script-console-linux-full-results.md). +Die folgenden älteren Zwischenstände bleiben historische Belege. ## Fortschreibung: reale Quellenprüfungen und Bereitstellungsgrenzen @@ -99,9 +110,9 @@ Fortschreibung hat bei widersprechenden Build-/Betriebsangaben Vorrang. | Thema | Tatsächlich umgesetzt/geprüft | Noch offen | |---|---|---| -| Skriptkonsole | Eigener Branchcodex/script-console; additive feste Vollwartungsprofile, Domainbytes/Policies/Executor und Webansicht in Arbeit. WebTypecheck/Vite und49lokale Tests bestanden;22Domainfälle lokal bestanden. | Gesamter Runtime-Compile-/Fehlerpfadreview, eigener Linux-Vollwartungsbootstrap mit erhaltenem General/APTprofil, nativeLinux-first-Prüfungen, Server-/Clientbereitstellung und tatsächlich WebUI-genehmigte Shell-/SYSTEM-PowerShell-Jobs samt Ausgabe/Abbruch/Unknown. Noch nicht bereitgestellt. | +| Skriptkonsole | Vertrag1.20 implementiert/geprüft, Cloudserver/WebUI und Linuxruntime bereitgestellt. Nativ CloudLinux84, Windows43 und separatPG45 bestanden; erste tatsächliche MCP/WebUI/Rootskriptstrecke samt Rohbyteausgabe und Mandantengrenze bestanden. | Native Windows-PowerShell/SYSTEM-Ausführung; weitere erforderliche aktuelle Abbruch-/Unknown-Gates, abschließende Integration und aktuelle private Runtimeindizes. | | Windowslaufzeit | Positive echte Pipe-v2-Peerstrecke und passive Erhaltung; portable Runtime-/Domainunits nativ. | Tatsächliche TrustedClock-Abnahme, synchrone Helper-/Intent-/PERSIST-Strecke, genehmigte Skriptausführung und verbleibende Windows-MVP-Gates. | -| Agent-Selbstupdater | Reiner Verifier separat implementiert/unabhängig geprüft: kanonisches signiertes Manifest, getrennte Trustpins, Plattform/Zeit/Floor und begrenztes Streaming. OfflineDockerFmt/Clippy sowie9Vertragsprüfungen bestanden. [Commitcc5c935](https://gitea.incoso.eu/kevin_janssen/rust-discovery/commit/cc5c935). | NativeLinux-/Windowsabnahme; Releasekatalog und eigener begrenzter Streamingtransport, feste Updatepläne/WebUI, geschützte Zustandsablage und unabhängiger Dienst für Austausch/Erhaltung/Wiederanlauf. Kein aktiver Autoupdater und kein stilles periodisches Apply. | +| Agent-Selbstupdater | Reiner signierter Verifier auf CloudLinux9 und Windows9 nativ bestanden. Neuer isolierter Handoff-/Fullrenderer-/Healthkernel lokal8/8 und unabhängig geprüft; noch keine Applyruntime. | Atomare bestehende Ledgertransaktion, Inflight-/Serviceownership und authentisierter Healthanschluss, Katalog/Streaming, feste WebUI/MCP-Updatepläne, OSaustausch/Erhaltung/zulässiger Rollback und native Applynachweise. Kein aktiver Autoupdater. | APT-/MSI-Paketfixtures prüfen Verwaltung anderer Software; sie implementieren keinen Agent-Selbstupdater. Weitere Paketfeatures sind der Skriptkonsole diff --git a/docs/project-decisions.md b/docs/project-decisions.md index a84d90e..d137cda 100644 --- a/docs/project-decisions.md +++ b/docs/project-decisions.md @@ -1,6 +1,77 @@ # Verbindliche Projektentscheidungen -Stand: **2026-11-14**, Spezifikation **1.18** (`Rust_RMM_Implementierungsspezifikation.md`). Die ausdrücklichen normativen Entscheidungen in Kapitel 0 haben Vorrang vor früheren Architekturannahmen. Version 1.6 ersetzt die lokale Servertopologie durch den dedizierten Internetserver; Version 1.7 präzisiert das Notebook als reinen Entwicklungsrechner; Version 1.8 erlaubt vorgezogene parallele Windows-Entwicklung. +Stand: **2026-11-14**, Spezifikation **1.20** (`Rust_RMM_Implementierungsspezifikation.md`). Die ausdrücklichen normativen Entscheidungen in Kapitel 0 haben Vorrang vor früheren Architekturannahmen. Version 1.6 ersetzt die lokale Servertopologie durch den dedizierten Internetserver; Version 1.7 präzisiert das Notebook als reinen Entwicklungsrechner; Version 1.8 erlaubt vorgezogene parallele Windows-Entwicklung. + +## Skriptkonsole und vollständige Hostwartung (Version1.20) + +Der User stellt die Batch-Skriptkonsole vor weitere Paketfixture-Erweiterungen: +KI/MCP schreibt vollständige Shell-/PowerShell-Skripte und beantragt feste +Aufträge; der Mensch genehmigt exakt gespeicherte Bytes, Ziele, Interpreter, +Argumente, Rechte und Limits in der WebUI. Status und abschließende begrenzte +Rohbyteausgabe verwenden die bestehende Jobstrecke. Keine neue PTY-/ConPTY- +Sitzung und kein behauptetes laufendes Streaming. Regelmäßiges passives +Inventar/Heartbeat ist getrennt; Live-Erhebung per Skript bleibt Job. + +Das neue additive Feld `privileged_script_profile="full_maintenance_v1"` +bindet die ausdrücklich gewünschte Root-/SYSTEM-Hostwartung vor Genehmigung. +Fehlend bleibt es aus alten Bytes ausgelassen; null/unbekannt/agent_service mit +Profil verweigert. Bestehende Linux-Root-Skripte ohne Profil behalten ihre +bisherige begrenzte Runtime. Windows-SYSTEM-Skripte brauchen das neue Profil; +SCM-/MSI-Policies erben keine Skripterlaubnis. Die historische Aussage1.18 +„SYSTEM-Skripte außerhalb des engen SCM-Vertrags“ bleibt für dessen Profile +richtig; sie verbietet nicht den nun gesondert beauftragten Vollwartungspfad. + +Zentrale/lokale Policy erlauben Vollwartung nur ausdrücklich versioniert über +`allowed_script_profiles`: Linuxversion2, Windowsversion3; fehlend bedeutet deny, +vorhandene null/unbekannte Profile verweigern. `scripts:execute` plus jeweiliges +`root:execute`/`system:execute` gelten für Ersteller und Freigeber aktuell; +zusätzlich exakte Geräte-/Helperpins, qualifizierte Capability +`script.full_maintenance/1`, TrustedClock und unabhängiger signierter Sync. +Linux bekommt ein eigenes explizites Helperprofil; bisherige General-/APT- +Sandboxprofile bleiben erhalten. Windows verwendet SID18 und einen eigenständig +geprüften SYSTEM-Pfad. Volle Originalsignaturen, dauerhafter Einmalverbrauch, +Intent/Dispatch/RawMirror/Abandon und Journalidentitäten werden weiterverwendet. + +Vollwartung ist volle Hostautorität, einschließlich möglicher Agent-/PKI- +Änderungen und Neustarts. Keine Sandbox-/No-Reboot-/Rollbackzusage für beliebigen +Root-/SYSTEM-Code; nachgeladener Inhalt ist nicht automatisch gebunden. Jede +Wirkung braucht ihren festen WebUI-Plan. Kein ungeplanter destruktiver Test, +keine Rebootabnahme aus einem Skript. Timeout/Abbruch/unklare Wirkung bleiben +ehrlich `unknown`, kein automatischer Retry oder nachträgliches Heilen durch +Exit0. Eigene Prozesse werden kontrolliert beendet, fremde nicht übernommen. +Die konkreten Bounds und Kompatibilitätsregeln stehen in Spezifikation0.1. + +Dies ändert den Produktauftrag, behauptet keine Implementierung oder native +Abnahme. Linux-Cloud-VM zuerst; Windows-VM separat; Notebook nur Entwicklung. +Die zusätzliche tatsächliche Linux-Helperbinarybindung des Fullprofils ist +verpflichtend: geschützter Schema2-Pin, gehaltenes laufendes Rootimage, +authentisierte Probe und passende zusätzliche Binärcapability; keine +Agent-/proc-Lesebehauptung und keine Änderung der General-/APT-Legacybytes. +Lokale frühere Qualifikation und noch offener separater Korrekturdurchlauf: +[deploy/linux/full-maintenance-README.md](../deploy/linux/full-maintenance-README.md). +Agent-Selbstupdates sind ein getrennter signierter Updatepfad; APT/MSI- +Paketfixtures und Vollwartungsskripte sind keine fertigen Agent-Autoupdater. + +## Enger Windows-M4-MSI-Schnitt (Version1.19) + +Die beauftragte Windows-MVP-Fortsetzung erhält als nächsten getrennten Schnitt +zwei feste eigene MSI-Rezepte1.0 und1.0→1.1. Dies ist eine explizite technische +Präzisierung innerhalb des bereits beauftragten M4-Umfangs, keine neue pauschale +Userfreigabe für Paketwirkungen. Kapitel0 bindet vollständiges optionales +Windowsmanifest, unveränderlichen Tenantkatalog, exakte automatisch abgeleitete +Job-/Versuch-/Artefaktreferenz, neue getrennte SYSTEM-Paketerlaubnis und sichtbare +Planbytes. Alte SCM-only-Policies erteilen keine Paketrechte. Native +Installation/Upgrade/Noop bleiben jeweils eigene WebUI-genehmigte Jobs. + +APT-/Windowsmanifest schließen sich aus; alte Linuxbytes und freie Linux- +Artefaktpfade behalten ihre bisherigen Verträge. Kein freier Installer oder +Rezeptargumentvektor, kein Repair/Downgrade/Fresh1.1, keine EXE-/Skript-/Reboot- +Erlaubnis in diesem Schnitt. Cache-/Provider-/Installerwirkung bleibt bei +Abandon ausgeschlossen. Noop und tatsächliche Nachbedingungen müssen innerhalb +des ursprünglichen Budgets unter gültigem Clock-/Fence-/Abbruchkontext liegen. +Ein später belegter Zustand heilt keinen Timeout. Die ursprünglichen MSI- +Artefakt- und Nativeberichte bleiben unverändert. Quelle/Build/Unitnachweis +öffnet keine Capability und ist keine tatsächliche Paketabnahme. ## Begrenzte Windowsfreigaben des Users (Version1.18) diff --git a/docs/script-console-bootstrap-results.md b/docs/script-console-bootstrap-results.md new file mode 100644 index 0000000..c73f25d --- /dev/null +++ b/docs/script-console-bootstrap-results.md @@ -0,0 +1,110 @@ +# Tatsächliche Linux-Bereitstellung der Skriptkonsole + +Stand 2026-11-14, Spezifikation 1.20. Ausschließlich die Cloud-Linux-Test-VM +wurde als Client verwendet. Der Entwicklungsrechner erhielt keinen RMM-Dienst. +Sämtliche Zielaufrufe führte der übergeordnete Entwickleragent nach unabhängiger +Quellprüfung aus. Diese Bootstrapnachweise ersetzen keine WebUI-Jobgenehmigung. + +Der korrigierte Quellpack mit 158 unveränderten Eingaben ist Manifest +`ddd1a78d1cd289b02b1f98572f9652171ec00be84e01ce099fd722d689bcaacd`. +Auf der Cloud-VM bestanden tatsächlich 84 Units; fünf Datenbankfälle blieben +ausdrücklich ausgelassen. Dieselben fünf waren bereits im isolierten lokalen +PostgreSQL-Lauf mit allen 45 Servertests bestanden. Der native Beleg des Falls +`7b61ce46-7913-492b-9c6b-414a0c2b6ffc` hat SHA256 +`a831a70a3cf76a3e8a9bf5820dcca7100d2ef9860dd9388539fe9c13f949f470`. +Kein RMM-Job, keine Genehmigung oder Produktzustandsänderung gehörte zum Unitlauf. + +Der getrennte lesende Vorher-CAS band bestehende Boot-/Geräteidentität, +tatsächliche alte Prozessimages, General-/APT-Kontrollen und drei +Datenbankinodes. Für die Bereitstellung wurden sämtliche aktiven Leser des +gemeinsamen Root-Ledgers auf die neue decoderfähige Generation gebracht; +[Begründung und APT-Grenze](script-console-helper-generations.md) bleiben bindend. + +## Originalinstallation und erhaltene Fehlerbelege + +Der Originalfall `2e27c312-58cc-444d-92a6-863a11ac6ffd` verwendete +`deploy/script_console_install.py`, SHA256 +`667b56f7e5f085db249f27a7094919c7c0e6055dabc9f0600cb261c34760cd26`. +Nach frischem geschlossenem Servergate wurden Agent und General angehalten, +die alten Dateien erhalten und die qualifizierten neuen Images eingesetzt. +Generalunit, Sandbox, Konfiguration und bestehende Pins blieben unverändert. +Fullkonfiguration, geschützter Binär-/Policypin, eigene Unit und Agentdropin +wurden additiv angelegt. Alle ursprünglichen SQL-Zeilen und Dateimetadaten +einschließlich PKI blieben vor dem Wiederstart erhalten; insbesondere drei +Datenbankinodes und die ursprünglichen 18 abgeschlossenen Rootverbräuche mit +Originalrequest-/Grant-/Start-/Outcomebytes. Es gab keinen Ledgerreset. + +Die Installation hielt danach vor `restart-intent` an: Der ursprüngliche +Unitguard erwartete leere Filterwerte, tatsächlich lieferte systemd bei +`SystemCallFilter` und `RestrictAddressFamilies` jeweils `~`, also eine leere +Ausschlussliste. Die übrigen Fullunitwerte entsprachen der beabsichtigten vollen +Hostautorität. Der unmittelbare Readback zeigte alle drei Dienste inaktiv mit +PID 0. Der Originalbericht bleibt ein fehlgeschlagener Installationsbericht; +er wurde nach späterer Fortsetzung nicht als erfolgreicher Bericht überschrieben. + +Die separate Fortsetzung `43dae370-6862-4cf4-a83c-0b2612af6c79` verwendete +`deploy/script_console_continue.py`, SHA256 +`c0c0ecf1e1bcb2d2f9ce0bcf6973aa430e9e28ba86823ca5ad0954744a589238`. +Sie ließ ausschließlich die beiden exakten leeren Filterdarstellungen `''` +und `'~'` zu, prüfte alle bestehenden Kontrollen und SQL-/PKI-Zeugen frisch +und schrieb vor dem erstmaligen Start einen dauerhaften eigenen Startintent. +Der Generalstart wurde ausgeführt; seine sofortige Prozessprüfung verweigerte +jedoch die Fortsetzung. Full und Agent wurden noch nicht gestartet. Der +Bericht enthält deshalb `started:[]`; daraus folgt ausdrücklich nicht, dass +kein Startbefehl ausgeführt wurde. Ein zusätzlicher frischer Readback belegte +den tatsächlich laufenden richtigen Generalprozess. Eine Beobachtung vor +abgeschlossenem `exec` bei `Type=simple` ist eine plausible Erklärung, wurde +aber nicht als bewiesene Ursache ausgegeben. + +## Einmalige verbleibende Starts und aktueller Beleg + +Ein neuer getrennter Fall `66a537bb-e676-421e-80dc-f653b069738a` verwendete +`deploy/script_console_finish_start.py`, SHA256 +`ebe6170d489689ad0ffaf23eeba60db7979dd243d386d3a5c33885212f5c086b`. +Er erhielt beide vorherigen Quellen, Intents, Berichte und Startmarker unverändert. +Der bereits laufende General wurde frisch an PID, Prozessgeburt, Image und +Konten gebunden und weder gestartet, gestoppt noch neu gestartet. Vor jedem +verbleibenden Full-/Agentstart wurde ein eigener dauerhafter Einmalintent +geschrieben. Prozessbeobachtung führte keinen weiteren Start aus. + +Der Poll verwendet eine fünfsekündige Beobachtungsdeadline und verlangt zwei +übereinstimmende korrekte Prozessbeobachtungen; der erste positive PID-/Geburtsbezug +darf nicht wechseln. **Grenze:** Einzelne geerbte `systemctl`-Abfragen haben +weiterhin 25 Sekunden Timeout. Es gibt deshalb keine harte Zusage von fünf +Sekunden Gesamtzeit. Diese Grenze betrifft den Bootstrapbeobachter, nicht die +Laufzeit genehmigter Skriptjobs. Die bereits verwendete Quelle bleibt unverändert. + +Tatsächliches Ergebnis: erfolgreiche verbleibende Starts und zusätzlicher +rein lesender Readback aller drei aktiven Dienste, gleicher Boot und korrekte +Konten/Images: + +| Dienst | PID / Startticks | UID / GID | +| --- | --- | --- | +| General | `65562 / 7308498` unverändert | `0 / 984` | +| Full | `66122 / 7344457` | `0 / 984` | +| Agent | `66132 / 7344482` | `993 / 984` | + +Helperimage SHA256 +`79442727c3ba31cda02a8bc0e67481f14db9f39fc07520a880a90b64f10f1eaf`, +Agentimage +`0a7ad6eefaefda79ee80b6a41b2df904e9e7f50ae6ebeed01b98c25b6ce5c06f`. +Der tatsächliche Ergebnisbeleg liegt privat unter +`devstate/script-console-finish-start/66a537bb-e676-421e-80dc-f653b069738a/finish-reply.json`, +SHA256 `85c98d39d666ce05abc7c3747cd58978496fc059b1f833997ed4d6eff5229153`. +Der zusätzliche Readback hat SHA256 +`5d94f79b29c7be6310fbbeec4178c0dadb849566506239b01b8db3d48c622316`. +Erster General-Startintent: `2026-11-14T16:44:24.389303+00:00`; +Intent für die verbleibenden Starts: `2026-11-14T16:50:23.921400+00:00`. + +APT blieb disabled/inactive, ohne Agentendpoint und bereits zuvor nicht aktuell +qualifiziert. Keine Paketwirkung, kein Reboot, kein neuer Rootverbrauch und kein +RMM-Job oder Approval wurden durch diese Bootstrapquellen angefordert. Normale +Dienstaktivität nach dem Start ist separat zu attestieren; der Startbeleg setzt +deshalb `signed_sync_verified:false` und `full_job_accepted:false`. + +**Noch offen:** Frischer signierter Abgleich mit kryptografisch geprüfter +Erhaltung der ursprünglichen Root-/Agenthistorie, aktuelle passive Meldung an +den festgelegten Server und danach der tatsächlich über die WebUI genehmigte +begrenzte FullMaintenance-Skriptauftrag. Dienststart, Unitprüfung und bestehende +Policy sind keine Jobgenehmigung. Fehlende oder unklare Ergebnisse dürfen weder +automatisch wiederholt noch durch Journal-/Datenbankänderungen repariert werden. diff --git a/docs/script-console-browser-acceptance.md b/docs/script-console-browser-acceptance.md new file mode 100644 index 0000000..e497403 --- /dev/null +++ b/docs/script-console-browser-acceptance.md @@ -0,0 +1,332 @@ +# Geplante Browserabnahme der Linux-Full-Skriptkonsole + +Stand: **Opsvorbereitung, keine ausgeführte Browser-/Jobabnahme**. Der Subagent +hat weder Browser, SSH, MCP, Anmeldung, Job noch Policyänderung ausgeführt und +keine Zugangsdaten gelesen. Deployment-v2 und Frozen158 bleiben unverändert. +Neue Playwright-Testdateien oder ein weiteres Testframework wurden nicht angelegt. + +Die Vorlagen liegen geschützt unter +`devstate/output/playwright-private/console-full-v1/`: + +| Datei | Zweck | +| --- | --- | +| `browser-cli.config.template.json` | CLI-Konfiguration mit echten TLS-Prüfungen und privatem Profil | +| `launch-chromium.sh.template` | ausschließlich Browser-Namespace, kein HOME-Override | +| `linux-full-script.sh` | exakte geplante benigne Jobbytes, nicht lokal ausführen | +| `mcp-plan-input.template.json` | einzelner fester Linux-Antrag, Ziel/Zeiten noch einzutragen | +| `acceptance-required.json` | erwartete Pending-, Audit-, Identitäts- und Ausgabewerte | +| `root-policy.template.json` | nicht ausführbare Schema2-Vorlage mit ungelösten Pflichtpins | +| `root-policy-change.review.json` | einmaliger Policy-CAS-/History-/Schließungsplan; kein Mutator | +| `prepared.json` | Einzelhashes und ausdrücklicher Vorbereitungsstatus | + +## Voraussetzungen und Vertrauensbindung + +Zuerst müssen der neue Serverbuild, Migration14 und Web49 tatsächlich gesund +bereitgestellt sein. Die zentrale Rootpolicy ist dabei noch die exakt bekannte +abgelaufene `cf15cbb68b590475df6def28e7dcffb7d88e0ca2d2555a06174fb654eeff05f4`. +Ein Serverbuild allein qualifiziert weder Agent noch Fullprofil. + +Die Cloud-Linux-VM muss separat die neue laufende Agent-/Helpergeneration, +Fullhelper-Readiness, tatsächlichen gehaltenen Helpercodehash, lokale Fullpolicy, +unveränderte Geräteidentität, vertrauenswürdige Uhr und signierten Sync belegen. +Gespeicherte Capabilities müssen insbesondere `script.full_maintenance/1`, den +konkreten `helper.policy.sha256:` und +`helper.binary.sha256:` enthalten. Aus Source, UI-Auswahl oder +Buildartefakten werden keine tatsächlichen Capabilities abgeleitet. Notebook und +Windows-VM sind kein Ziel dieses ersten Abnahmeschritts. + +Vor Browserstart wird die Authkonfiguration ausschließlich lesend über den +geprüften privaten API-TLS-Weg kontrolliert: development/dev/test, Devlogin +aktiviert und Produktions-OIDC ehrlich nicht konfiguriert. Für API18443 ist +**`devstate/api-pki/ca.pem`** die richtige Vertrauensquelle; ein vorhandenes +öffentliches `credentials/server-ca.pem` darf nur bei unabhängig festgestellter +Byte-/Fingerprintgleichheit benutzt werden. Alte HTTP-Helfer wie +`cloud_mvp_acceptance.server(kind=http)` hardcoden noch `pki/ca.pem` und sind +hierfür nicht ungeprüft wiederzuverwenden. Keine Zertifikats-/Namensausnahme. + +Der bestehende strikt geprüfte SSH-Tunnel bindet nur +`127.0.0.1:18443 → Server 127.0.0.1:18443`. Die Browserorigin bleibt exakt +`https://localhost:18443`, passend zum dokumentierten SAN und zur CSRF-Origin. +Ein Tunnel ist weder lokaler RMM-Server noch Enrollment des Notebooks. + +## CLI und Browser ohne Änderung von HOME oder globalem Trust + +Der [Playwright-Skill](/home/kevin/.codex/skills/playwright/SKILL.md) wurde gelesen. +`npx` ist vorhanden. Die lokal gecachte CLI0.1.24 mit Playwright1.66-Alpha wurde +anhand ihrer installierten Quellen geprüft: Config, `userDataDir`, eigener +`executablePath`, `downloadsPath` und redigierte Secret-Namen werden unterstützt. +Der tatsächliche Browser ist der vorhandene Chromium141.0.7390.37 unter +`/home/kevin/Schreibtisch/Coding/rust-discovery/web/node_modules/.cache/playwright-browsers/chromium-1194/chrome-linux/chrome`. +Die Kombination ist noch nicht durch einen Browserstart qualifiziert. + +Runtimeartefakte gehören unter ein neues privates +`output/playwright//`, Modus0700. Dieses Verzeichnis und +`/.playwright-cli/` sind ausgeschlossen. Vorlagen mit `@CASE_DIR@` sind noch nicht +startfähig; Root materialisiert sie auf genau dieses absolute Caseverzeichnis. +Der CLI-Skillwrapper wird **mit `bash`** aufgerufen, nicht ausführbar gemacht. + +Die konfigurierte Browser-Executable ist dieser eigene private Wrapper: + +```bash +#!/usr/bin/env bash +set -euo pipefail +rmm_browser_rw='@CASE_DIR@/browser-rw' +rmm_chromium='/home/kevin/Schreibtisch/Coding/rust-discovery/web/node_modules/.cache/playwright-browsers/chromium-1194/chrome-linux/chrome' +exec /usr/bin/bwrap --die-with-parent --new-session --ro-bind / / \ + --bind "$rmm_browser_rw/nss-root" /home/kevin/.pki \ + --bind "$rmm_browser_rw" "$rmm_browser_rw" \ + --tmpfs /tmp --dev /dev -- "$rmm_chromium" "$@" +``` + +`HOME` bleibt unverändert `/home/kevin`. Nur im Browser-Mountnamespace verdeckt +der private NSS-Baum den bereits vorhandenen Pfad `/home/kevin/.pki`. +Globale Trustdaten werden nicht geöffnet oder verändert. Der einzige zusätzliche +Schreibbind ist `browser-rw`; Rootprofil, Downloads und CLI-Cache liegen darin. +Ein expliziter `userDataDir` und `downloadsPath` vermeiden, dass der Browser +Ausgaben in einem nur für ihn sichtbaren `/tmp` ablegt, auf die der CLI-Treiber +anschließend nicht zugreifen könnte. Netzwerk bleibt für den lokalen Tunnel +erreichbar. Die Konfiguration setzt `ignoreHTTPSErrors:false` und +`chromiumSandbox:true`; bei einem tatsächlichen Startproblem keine stillen +TLS-/Sandboxausnahmen ergänzen. + +Für die spätere lokale Materialisierung sind die erforderlichen Verzeichnisse +`browser-rw/{profile,downloads,cache,nss-root/nssdb}` und `evidence` privat +anzulegen. Konfiguration und Wrapper werden aus den Vorlagen mit dem festen +Casepfad erzeugt; nur der neu erzeugte Browserwrapper erhält0700. Der öffentliche +**API-CA-Beleg** wird per bestehendem geprüften Transport beschafft und unter +`trust/api-ca.pem` abgelegt; dieser Schritt wurde hier nicht ausgeführt. + +Geplante NSS-Einrichtung, noch nicht ausgeführt: + +```bash +rmm_certutil='/home/kevin/Schreibtisch/Coding/rust-discovery/web/node_modules/.cache/nss-tools/root/usr/bin/certutil' +"$rmm_certutil" -N -d "sql:$rmm_case/browser-rw/nss-root/nssdb" --empty-password +"$rmm_certutil" -A -d "sql:$rmm_case/browser-rw/nss-root/nssdb" -n 'RMM fixed API CA' -t 'C,,' -i "$rmm_case/trust/api-ca.pem" +"$rmm_certutil" -V -d "sql:$rmm_case/browser-rw/nss-root/nssdb" -n 'RMM fixed API CA' -u L +``` + +Vor Anmeldung müssen eine leere separate NSS-Fixture die untrusted-CA-Verbindung +und die vertrauende Fixture einen falschen TLS-Namen verweigern. Der positive +Aufruf zu `https://localhost:18443` muss ohne Zertifikatsfehler funktionieren. +Keine Warnseite wegklicken, kein `ignore-certificate-errors`, kein SPKI-Bypass. + +Der bestehende geschützte Login wird erst durch Root zur tatsächlichen Anmeldung +lokal verarbeitet. Die CLI unterstützt `PLAYWRIGHT_MCP_SECRETS_FILE` als private +Dotenv-Datei mit den Namen `RMM_LOGIN_USER` und `RMM_LOGIN_PASSWORD`. Root erzeugt +sie ausschließlich aus dem geprüften bestehenden Login-JSON, ohne Werte zu +loggen oder als CLI-Argumente zu übergeben. Hier wurde sie nicht erzeugt. +CLI-Fill erhält nur den Secret-Namen; die installierte CLI löst und redigiert ihn. +Session-/Cookie-/CSRF-Daten dürfen nicht als ganze Dumps ausgegeben werden. + +Geplante CLI-Funktion, noch nicht ausgeführt; `rmm_case` und `rmm_session` sind +die vorher explizit gesetzten Aufgabenvariablen: + +```bash +rmm_cli() { + env npm_config_offline=true \ + XDG_CACHE_HOME="$rmm_case/browser-rw/cache" \ + PLAYWRIGHT_MCP_SECRETS_FILE="$rmm_case/login.env" \ + bash /home/kevin/.codex/skills/playwright/scripts/playwright_cli.sh \ + --session "$rmm_session" --config "$rmm_case/cli.json" "$@" +} +rmm_cli open https://localhost:18443/ +rmm_cli snapshot +``` + +Keine Zuweisung an `HOME`, `home` oder `CODEX_HOME`. Traces, Videos und vollständige +Netzwerk-/Credentialaufzeichnungen bleiben aus. Artefakte sind private Belege. + +## Fester Antrag über MCP, Genehmigung ausschließlich durch UI-Klick + +Geplante Jobbytes, **nicht lokal ausführen**: + +```sh +set -eu +/usr/bin/id -u +/usr/bin/sleep 12 +printf '%s\n' 'RMM_FULL_CONSOLE_LINUX_V1' +printf '%s\n' 'RMM_FULL_CONSOLE_STDERR_V1' >&2 +``` + +Das sind130UTF-8-Bytes ohne BOM mit SHA256 +`c878ba5b5c37fa3f1b9485c6994d6c57f6f37d01d99d4239c64d264d4ed95336`. +Der begrenzte12-Sekunden-Warteaufruf lässt Zeit, den gespeicherten Runningstatus +über das bestehende4-Sekunden-Polling zu beobachten. Keine Installation, +Dateischreibwirkung, Dienständerung, Downloads oder Neustarts sind enthalten. + +Die Vorlage bindet ausschließlich das bestätigte Cloud-Linux-Asset, +`execution_identity:root`, `privileged_script_profile:full_maintenance_v1`, +`posix_sh`, `arguments:[]`,30Sekunden Laufzeit,4096Ausgabebytes, Parallelität1 und +Stopp nach1Fehler. UTC-Start/Frist werden kurz vor Planung festgelegt und müssen +innerhalb der konkret geprüften Policygültigkeit liegen. Erwartete stdout- +Nachbedingung ist exakt `0\nRMM_FULL_CONSOLE_LINUX_V1\n`; stderr wird anschließend +separat auf `RMM_FULL_CONSOLE_STDERR_V1\n` geprüft. Kein erfundenes +`expected_stderr`-Wirefeld wird hinzugefügt. + +Nach sichtbarer simulierter Anmeldung und separat geprüftem temporärem +Policy-Opt-in ruft Root über die vorhandene autorisierte MCP-Automation nur +`operations.plan(tenant_id,input)` und +`operations.submit(tenant_id,plan_id,idempotency_key)` auf. Einmaliger privater +Checkpoint erhält tatsächliche Planbytes, SHA256, alle Job-/Versuchs-/Ziel-IDs, +Skriptbytes, Zeitfenster und die Submitantwort. Kein Approvaltool oder Browser- +Token darf der MCP-Automation bereitgestellt werden. + +**Vor dem Klick:** `pending_approval`, `job_count:0`, `execution_state:null` sowie +für genau dieses neue Job-/Versuchspaar Jobs0, Outbox0, Permits0 und +Authorizations0 unabhängig lesend nachweisen. Alte Gesamtzähler sind kein +Ersatz. Eine Plan-Zielpreview ist kein bereits ausführbarer Job. Kein Retry. + +## Konkreter UI-Ref-Ablauf + +Alle folgenden `e…` sind ausdrücklich **Platzhalter aus dem unmittelbar vorher +gelesenen Snapshot**, keine erfundenen tatsächlichen Refs. Nach Navigation oder +geändertem DOM erneut `snapshot` und aktuelle Refs benutzen. + +1. Bei frischem Snapshot Benutzername/Passwort-Felder nach ihren tatsächlichen + Labels identifizieren. `rmm_cli fill eUSER RMM_LOGIN_USER`, + `rmm_cli fill ePASSWORD RMM_LOGIN_PASSWORD`, dann tatsächlichen Button + „Anmelden“ klicken. Danach neuer Snapshot: `SIMULIERT`, + `simulated-testprincipal-a` und „kein menschlicher Produktions-Freigabenachweis“ + müssen sichtbar sein. Richtigen Mandanten bestätigen. Kein Loginscreenshot + mit gefüllten Feldern, keine Session-/CSRF-Geheimnisse ausgeben. +2. Nach MCP-Submit und unabhängigem Pending0-Nachweis die **exakte neue + Operation-ID** in „Anträge und Ausführungen“ klicken. Neuer Snapshot: + „Antrag: pending_approval“, Jobanzahl0 und kein Ausführungszustand. Dann + „Unveränderlichen Plan zur Prüfung öffnen“ klicken; erneut Snapshot. +3. Alle festen Ziel-/Agent-/Job-/Versuchs-IDs, `root`, Interpreter, Fullprofil, + Script-/Job-/Planhashes, komplettes Skript, stdout-Nachbedingung, Budgets und + Fristen mit dem MCP-Checkpoint vergleichen. Vollwartungswarnung muss explizit + volle Hostautorität einschließlich Agent/PKI/Rebootmöglichkeiten erklären. + Keine Sandbox- oder No-Reboot-Zusage daraus ableiten. +4. Den tatsächlichen gespeicherten JSON-Block + `pre[aria-label^="Alle festen Zielpreviews"]` verwenden, **nicht `pre.first()`**: + die Konsole enthält zusätzliche Skript-`pre`. Falls der Snapshot dafür keine + Ref liefert, ist ein ausschließlich lesendes + `run-code "async page => ({plan: JSON.parse(await page.locator('pre[aria-label^=\"Alle festen Zielpreviews\"]').innerText())})"` + möglich. Kein `fetch`, `page.request`, Routing, Datenmutation oder künstliches + DOM-/React-State-Umschalten. Exakte zurückgelesene Planbytes unabhängig hashen + und byteweise mit dem MCP-Checkpoint vergleichen; Script- und Jobhash ebenfalls. +5. Der Button **„SIMULIERT: Diesen festen Plan ausdrücklich genehmigen“** muss + zunächst deaktiviert sein. Erst nach den bytegenauen Prüfungen die echte + Checkbox „Ich habe sämtliche festen Ziele, Aktionen, Bytes/Hashes … geprüft“ + per `rmm_cli check eCONFIRM` setzen. Neuer Snapshot, dann exakt diesen jetzt + freigegebenen Button einmal per `rmm_cli click eAPPROVE` klicken. Keine direkte + POST-/DB-Genehmigung, keine erneute Wirkung bei verlorener Antwort. +6. Sofort Operation und „Job “ öffnen. Neuer Snapshot soll während + der begrenzten Wartezeit den tatsächlichen gespeicherten Runningstatus zeigen. + Danach über normale Snapshots/Polling bis zum endgültigen Ergebnis beobachten. + Ein verpasster Running-Snapshot wird ehrlich als nicht beobachtet dokumentiert; + er wird nicht durch einen neuen Job nur für ein Bild ersetzt. +7. „Konsolenergebnis“ muss Zustand, feste Attempt-ID, Revision, Exitcode0, + Trunkierungfalse und getrennte stdout-/stderr-Textansichten zeigen. Es gibt + keinen Live-Ausgabestream. Die vollständigen Originaldaten bleiben darunter. + Optional können die beiden echten Rohbyte-Downloadbuttons mit der vorbereiteten + privaten Downloadablage geprüft und deren Hashes mit dem Checkpoint verglichen + werden. Der endgültige Erfolgsbeleg umfasst immer die gespeicherten Rohbytes. +8. Über den tatsächlichen Button „Abmelden“ ausloggen und nur die eigene + CLI-Session schließen. Keine Browser-/Server-/Agentenprozesse pauschal stoppen. + +## Ergebnis-, Audit- und Schließungsbelege + +Read-only-MCP `jobs.get` muss genau das ursprüngliche Job-/Versuchspaar melden: +`succeeded`, Exit0, keine Trunkierung, `postconditions_passed:true`, +`execution_uid:0`, `privileged_script_profile:full_maintenance_v1` und exakt den +Skripthash. stdout und stderr müssen bytegenau stimmen. Zustände `unknown`, +Timeout, Cancel/Fence oder fehlende Ausgabe sind kein Erfolg und kein Retrygrund. +Die Helper-/Agentjournale müssen denselben genehmigten Versuch und genau eine +Consumption/Wirkung belegen; bestehende History und Identitäten bleiben erhalten. + +Die unabhängige lesende Auditprobe verlangt genau eine passende Authorization, +`approval_origin:dev_test_browser_simulation`, `testsimulation:true`, +`principal:simulated-testprincipal-a` und den exakten Planhash. Für dieses +Job-/Versuchspaar werden Jobs1, Outbox1 und Permits1 erwartet. Der UI-Klick ist ein +Entwicklungsnachweis und keine menschliche Produktionsgenehmigung. Belegstage +„geplant“, „pending“, „UI-simuliert genehmigt“, „native Wirkung“ und +„Ergebnis/Audit geprüft“ getrennt dokumentieren und nach dem Abnahmeschritt pushen. + +Der vorbereitete Policyplan verlangt exakt alten `cf15…`-CAS, unverändertes +Rootflag, ein neues Create-once-Historyverzeichnis, feste Kandidatbytes/hash, +maximal15Minuten, genau ein Cloudziel/Generationen, Fullpolicy- und laufenden +Helpercodehash und ausschließlich den simulierten Principal. Servicesliste leer, +Rebootfalse, keine separaten typisierten APT-/Logfreigaben. Diese Parameter +begrenzen typisierte Aktionen; ein ausdrücklich genehmigtes Fullskript behält +die in der UI angekündigte volle Hostautorität. Originalbytes werden vor der einen atomaren +Ersetzung erhalten; Antwortverlust erlaubt nur Readback desselben Cases. Die +geschützte Policy erhält weiterhin Eigentümer10001/0600. Keine Rollen-/Flag-/ +Ingressänderung, kein Service-Neustart, kein SQL- oder API-Approval ist Teil dieses +Scopewechsels. Das später separat beauftragte MCP-Actor-Provisioning ergänzt +höchstens dessen Root-Planungsrecht; siehe den getrennten Opsvertrag unten. +Nach eindeutig geschlossenem Ergebnis wird über einen separat geprüften +Kandidathash-CAS genau die ursprüngliche abgelaufene `cf15…`-Policy wiederhergestellt +und die Schließung belegt. Bis dahin begrenzt die feste Ablaufzeit neue Starts; +niemals still verlängern. Die Vorlage wurde inzwischen als getrenntes +[Policy-/Actor-Opswerkzeug](script-console-policy-scope.md) umgesetzt, vor jeglicher +Zielaktion vom Hauptagenten zu prüfen. Dadurch wird weder eine tatsächlich +geöffnete Policy noch ein MCP-/UI-Aufruf oder Jobnachweis behauptet. + +## Quellenprüfung und nach dem MVP vorgemerkte Grenze + +Das feste `Action::RunScript.privileged_script_profile` ist Teil der gehashten +Job-/Planbytes. Ein Entfernen/Umdeuten verändert den genehmigten Plan. Policy- und +Codehash sind zusätzliche Qualifikations-/Startgates, keine erfundenen weiteren +Jobpayloadfelder. `agent/src/helper_sync.rs` wählt mit +`general(binding,job.action.full_maintenance())` ausschließlich den richtigen +Fullpin/Profilweg; `agent/src/apt.rs::socket_for` und `invoke_with_control` +erzwingen den Fullsocket und die erneute Fullprobe ohne General-/APT-Fallback. +`privileged-helper/src/control.rs::general_probe` bindet Schema2, Identität, +Policysha, Profil und Binarysha. Der Helper verweigert Fulljobs auf General sowie +nicht passende Jobs auf Full; die gehaltene tatsächliche Codeidentität wird vor +Consume erneut geprüft. Beim Lesen dieser Quellen wurde keine konkrete Umgehung +der Job-/Helperpinbindung festgestellt; das ersetzt keinen nativen Nachweis. + +Die zentrale `RootScope::permits`-Semantik ist derzeit additiv: ein Schema2-Scope +mit `allowed_script_profiles:[full_maintenance_v1]` ermöglicht Full, schließt bei +`allow_root_scripts:true` aber nicht automatisch jedes begrenzte Legacy-Rootskript +aus. Daher keine allgemeine Full-only-Ausschlusszusage. Der konkrete genehmigte +Testplan ist ausdrücklich Full; General und APT bleiben lokal unverändert +gesperrt/gebunden. Strengere zentrale Profil-Ausschlusssemantik ist transparent +für nach dem MVP vorgemerkt; die eingefrorenen158Dateien werden dafür jetzt nicht +verändert. Falls eine tatsächliche Job-/Helperpinumgehung entdeckt wird, ist sie +als Blocker gesondert zu behandeln. + +## Tatsächlich lokal geprüft + +`bwrap --ro-bind / / -- /usr/bin/true` sowie eine separate öffentliche +Marker-/Pipefixture liefen erfolgreich. Der nur im Namespace gebundene private +NSS-Pfad funktioniert, `HOME` bleibt `/home/kevin`, geerbte Pipe-FDs bleiben offen +und die globalen `.pki`-Verzeichnismetadaten bleiben identisch. Dafür wurde kein +Browser gestartet und kein tatsächlicher Trust-/Login-/Serverinhalt gelesen. +Config-, Wrapper- und JSONsyntax sowie die geplanten Script-/Ausgabebytes werden +lokal geprüft. Browserkompatibilität, NSS-/TLS-Verhalten im echten Chromium, +Authentifizierung, Ref-Klick, Fulljob, Audit und Schließung sind **noch offen**. + +### Anschließender tatsächlicher Harnessbefund des Hauptagenten + +Der Hauptagent hat später den privaten Browsercase +`output/playwright/51c274c9-9ddc-42db-8684-5fec7877977d` vorbereitet. Er meldete +tatsächlichen geprüften öffentlichen API-CA-Bezug, Devlogintrue/OIDCfalse, +NSS-Erfolg und den privaten Loopback-SSH-Tunnel. Es wurden keine Secrets ausgegeben. +Diese Befunde stammen vom Hauptagenten, nicht aus einem Browserlauf des +vorbereitenden Subagenten. + +Erster Chromiumstart: Crashpad scheiterte vor Datenbank-/RMM-Wirkung. Ein privates +zusätzliches `XDG_CONFIG_HOME` im bereits vorbereiteten `browser-rw`-Schreibbaum +behebt dessen fehlenden Schreibpfad, ohne HOME zu ändern. + +Zweiter Start: Die Chromium-Sandbox verweigerte verschachtelte Usernamespaces. +Der tatsächliche Grund war `/proc/self/uid_map: Read-only file system` im +Bubblewrap-Namespace; außerhalb funktionierte die schmale `unshare -Ur true`- +Probe. Der Hauptagent ergänzte im **eigenen materialisierten Casewrapper** +`--unshare-pid --proc /proc`. Die entsprechende verschachtelte Namespaceprobe +bestand danach. Der dritte Browserstart wurde mit aktiviertem Chromium-Sandbox- +und TLS-Verifikationsschutz begonnen; daraus folgt noch kein Login-, +Genehmigungs- oder Joberfolg. + +Die vorbereiteten Vorlagendateien wurden während dieses laufenden Root-Cases +bewusst nicht verändert oder nachträglich als dessen tatsächlich verwendete +Wrapperbytes ausgegeben. Dessen konkrete Config-/Wrapperhashes und Ergebnisse +sind im tatsächlichen Case separat zu erhalten. Bei einem neuen Harnesscase ist +die eng belegte `/proc`-/private-XDG-Korrektur ausdrücklich einzubeziehen. Keine +Zertifikats-, Browser-Sandbox-, Root-/Job- oder Helpergrenze wurde dafür +abgeschaltet. Bis zu diesem gemeldeten Harnessstand war keine Policy geöffnet +und kein RMM-Job ausgeführt. diff --git a/docs/script-console-cloud-upgrade-v1.md b/docs/script-console-cloud-upgrade-v1.md new file mode 100644 index 0000000..cb74483 --- /dev/null +++ b/docs/script-console-cloud-upgrade-v1.md @@ -0,0 +1,230 @@ +# Begrenzter Cloudserver-Nachfolger für Skriptkonsole 1.20 + +`deploy/cloud_script_console_upgrade_v1.py` ist eine neue, einmalige +Bereitstellungsquelle für den festgelegten ARM64-Cloudserver. Bestehende Runner, +Compose, Dockerfile, Ingress, Policies und Produktquellen werden nicht verändert. +Die Quelle ist lokal geprüft; ein echter Servercase, SSH-Aufruf, ARM64-Build, +Migrationslauf und eine Bereitstellung wurden damit noch **nicht ausgeführt**. + +## Feste Quellen und Ausgangslage + +- Frozen `run-29461892-343f-42f4-b469-88703535425e`, Manifest-SHA256 + `ddd1a78d1cd289b02b1f98572f9652171ec00be84e01ce099fd722d689bcaacd`: + alle 158 nachgewiesenen Compiler-/Vertragsdateien, einschließlich des + Full-Helper-Binarypins und Migration `0014_windows_msi_catalog.sql`. +- Zusätzlich separat gepinnte `deploy/Dockerfile`, `.dockerignore` und + `deploy/server.compose.yaml`: insgesamt **161 Dateien** in einem eigenen + ausgewählten Quellarchiv. Kein Live-Checkout als Buildkontext und kein + Überschreiben von `/opt/rust-discovery/crates` oder alten Quellen. +- Dockerfile-COPYs und produktive Includes sind enthalten: Cargo-Manifeste und + Lockfile, sämtliche Crates einschließlich OIDC-SQL, Migrationen, APT-Python, + Hook und eingebettete APT-Unit. Der bestehende `.dockerignore` filtert den + Kontext zusätzlich. Credentials, Devstate, Git, Target und Node-Module gelangen + nicht in diesen Kontext. Der ARM64-Build verwendet den unveränderten Dockerfile; + lokale x86-64-Binärdateien werden nicht als Serverruntime eingesetzt. +- Genau drei Dateien des bereits lokal geprüften Web49-Bundles. Index-SHA256 + `95d6f9058cc713c6af36fbd22ccc60678f081765a853b53d19ac6f0d219804f8`, + JS-SHA256 `f611662042eb4cc44f25c7d5ddc0d2d0562e32be267d3d5fb1db31b0d310f879`, + CSS-SHA256 `916e8870d4e9a9553de452b3ff6c71671bb6a7a22355de0aff432db516a9f3e7`. + Der Nachfolger baut die UI nicht erneut. +- Erwartetes bisheriges Runtimeimage + `sha256:25697f948325a11f755893e16ec5be2da138c3333c8e60f75a18f973d6ac26e2` + aus dem dokumentierten 31278be-/9058f6a3-Stand. Dies ersetzt keine frische + Read-only-Baseline; ein anderer tatsächlicher Stand wird verweigert. + +Das lokale Manifest bindet außerdem Runner, vollständige erzeugte Workerquelle, +beide SSH-Supportquellen, Quellarchiv, Webarchiv, alle Einzeldateien und die +SHA384-Checksummen der SQLx-Migrationen. Der bestehende geprüfte SSH-Key-/Hostkeyweg +wird wiederverwendet. `--access-root` bezeichnet nur den vorhandenen Hauptcheckout +mit geschützter `credentials/server.conf`; dessen Quellen werden nicht gebaut. +Ziel ist ausschließlich Root-Bootstrap auf 88.99.80.202 bzw. dessen festem Namen. + +TLS verwendet dieselbe getrennte Vertrauenszuordnung wie die bestehenden +Compose-Mounts: API18443 mit Name `localhost` und +`devstate/api-pki/ca.pem`; Gateway18444 mit Name `localhost` sowie öffentlicher +Gateway443 mit Name `discovery.ease-technologies.de` und jeweils +`devstate/pki/ca.pem`. Der dokumentierte Server-SAN und die bestehende +Renewalquelle enthalten beide Namen. Keine CA wird kopiert, ersetzt oder +rekonfiguriert, und keine Zertifikats-/Namensprüfung wird abgeschaltet. Fehlendes +CA-Material verweigert den jeweiligen TLS-Aufruf; Status weist den Teilfehler aus +und erhält die übrigen Readbackdaten. `inspect` ist ausschließlich die beschriebene +Metadaten-/Datenbankbaseline und behauptet keinen TLS-Healthnachweis. + +## Ablauf und Nachweise + +`inspect` liest ohne Zielveränderung eine aktuelle Baseline. Sie enthält die +Runtimeimage-ID, alte API-/Gateway-/PG-Container, nur erlaubte nicht geheime +Rolleneinstellungen, Loopbackports, Mounts, den tatsächlichen PG-Volume-Pfad samt +Inode/Device und den alten UI-Indexhash. Compose/Dockerfile/Dockerignore und +Ingressunits werden gehasht. Für `.env`, PKI und Jobschlüssel werden nur +Dateimetadaten aufgenommen; private Inhalte werden nicht geöffnet. Für die +nicht geheime Rootpolicy werden zusätzlich der unten beschriebene feste Hash, +das Schema und die bereits abgelaufene Deny-all-Grenze geprüft. Die +Docker-Templateauswahl gibt nur ausdrücklich benannte nicht geheime Einstellungen +aus. Keine vollständige Containerumgebung gelangt in Python oder einen Bericht. + +Die Datenbankprobe läuft als feste Read-only-Abfrage in einem konsistenten +Snapshot. Aus allen dauerhaften Plan-, Genehmigungs-, Job-, Outbox-, Permit-, +Receipt-, Audit-, Rollen- und Katalogtabellen verlassen nur Anzahl und +SHA256-Zeilenfingerprints PostgreSQL. Stabile Geräte-/Zertifikatsidentitäten und +die vollständige bestehende SQLx-Ledger 1–13 werden gebunden. Passive +Kontakt-/Synczähler und Beobachtungen dürfen fortschreiten. Bereits offene, +unbekannte oder laufende Jobs verweigern diesen Nachfolger. + +`prepare` arbeitet ausschließlich lokal aus dem geprüften Archiv und der explizit +angegebenen Baseline. Ein neues privates Caseverzeichnis wird genau einmal +angelegt. Der Offline-Paketcheck ersetzt keine reale Linux-Abnahme. + +`build` legt zuerst einen lokalen einmaligen Aufrufintent an. Auf dem Server +prüft der Runner erneut die vollständige Baseline, Migrationchecksummen und +unveränderte Deploymentkontrollen. Erst dann legt er den privaten Zielcase an, +schreibt Intent und Artefakte und startet einen festen systemd-Worker. Dieser +baut nativ für `linux/arm64` in seinem eigenen Kontext und einem neuen +`rust-discovery-server:script-v1-`-Tag. Er verändert weder Runtime-Tag, +Services, Datenbank noch UI. Nach dem Build müssen Quellen und Baseline erneut +übereinstimmen; erst dann entsteht ein erfolgreicher Buildbeleg. Ein +prozessübergreifender Lock serialisiert die Worker dieses Nachfolgers; er ersetzt +keine Koordination mit älteren Deploymenttools. + +`deploy` hat einen eigenen lokalen und entfernten Create-once-Intent. Der Worker +prüft alle Ausgangswerte und das tatsächliche erfolgreiche Candidateimage erneut. +Er hält API und Gateway kurz an; PostgreSQL und öffentlicher TCP-Ingress laufen +weiter. Nach dem Stop werden Historie, Container, PG-Volume und Index nochmals +gegen die Baseline geprüft; weitere Datenbank-Clientverbindungen sind unzulässig. +Dadurch können während der Migration keine normalen UI-/MCP-Schreibvorgänge oder +Agent-Jobstarts stattfinden. Ein rootprivates `pg_dump` und der alte Index werden +vor der Migration erhalten. Backupinhalte werden nicht zurückgegeben. + +Die feste Reihenfolge ist: + +1. Vollständiges Webarchiv vor jedem Publish prüfen; neue Hashassets create-only + ablegen. Existierende gleiche Hashdateien müssen exakt dieselben Bytes haben. + Alter Index, alte Assets und der bindgemountete Dist-Verzeichnisinode bleiben. +2. Runtime-Tag einmal auf die feste Candidateimage-ID setzen. Genau einen + Migrationscontainer mit festem Case-Namen starten und erhalten, auch bei + Fehlern. Keine automatische Entfernung oder zweite Migration. +3. SQLx-Zeilen 1–13 müssen vollständig unverändert sein; ausschließlich Version14 + mit erwarteter SHA384 darf hinzukommen. Sämtliche ursprünglichen + Historienfingerprints und Identitäten müssen unverändert bleiben. Der neue + MSI-Katalog ist leer, FORCE-RLS-geschützt, für `rmm_app` nur SELECT/INSERT und + mit aktivem Immutable-Trigger versehen. +4. Nur API und Gateway aus dem festen neuen Image starten. Verifizierte TLS- + Healthchecks und ehrliche private Authkonfiguration prüfen. Öffentlicher + Gateway liefert für UI, Login, Session, MCP und Verwaltungsrouten weiterhin404; + der Signierschlüssel verlangt weiterhin ein authentifiziertes Gerät. UID, + Ports, Mounts und geschlossene Root-/SYSTEM-/Faultgates müssen erhalten sein. +5. Erst nach diesen Backendnachweisen den bisherigen Indexhash abschließend + vergleichen und den neuen Index atomar veröffentlichen. Alle drei tatsächlich + privat über TLS ausgelieferten Dateien müssen ihre festen Bundlehashes haben. + Datenbank, geschützte Metadaten, PG-Volume und Rollen nochmals prüfen. + +Nachweise werden als einzelne fsync-gesicherte, unveränderliche Phasenbelege +erhalten. `passed` entsteht ausschließlich nach den Prüfungen. Die Quelle erzeugt +keine Jobs, Genehmigungen, neuen Versuche, Katalogrezepte oder Policyfreigaben. +WebUI-Login/CSRF/Plangenehmigung bleiben normale Produktpfade; kein Login- oder +Approvaltoken wird für diesen Ablauf gelesen oder erzeugt. Die spätere native +Skriptabnahme braucht weiterhin MCP → pending ohne Ausführungsartefakte → echte +simulierte Dev/Test-UI-Interaktion → Ergebnis, zuerst auf der Cloud-Linux-VM. + +## Geplante Bedienung, nicht ausgeführt + +Nach Rootreview, aktuellem Frozen-/Linuxnachweis und gesicherter Ausgangslage: + +```text +python3 deploy/cloud_script_console_upgrade_v1.py inspect --access-root /pfad/zum/hauptcheckout +python3 deploy/cloud_script_console_upgrade_v1.py prepare --baseline /pfad/zur/gespeicherten/baseline.json +python3 deploy/cloud_script_console_upgrade_v1.py build --id CASE --access-root /pfad/zum/hauptcheckout --execute-cloud-build +python3 deploy/cloud_script_console_upgrade_v1.py status --id CASE --access-root /pfad/zum/hauptcheckout +python3 deploy/cloud_script_console_upgrade_v1.py deploy --id CASE --access-root /pfad/zum/hauptcheckout --execute-cloud-deploy +python3 deploy/cloud_script_console_upgrade_v1.py status --id CASE --access-root /pfad/zum/hauptcheckout +``` + +Ein Aufruffehler oder verlorene Antwort erlaubt **nur `status` desselben Cases**. +Nicht `build`/`deploy` wiederholen, keinen neuen Case als Reparatur anlegen, keinen +Migrationscontainer entfernen und nicht pauschal auf das alte Image zurückrollen. +Readback funktioniert auch bei fehlendem oder nur teilweise hochgeladenem Case; +es erzeugt keine Zielpfade und startet keine Prozesse mit Wirkung. Neben den +erhaltenen Belegen liest es tatsächlich aktuelles Image, Historie, Container, +Kontrollmetadaten und privat ausgelieferte UI-Hashes. Einzelne Lesefehler werden +als solche ausgewiesen. Es repariert keinen Zustand. + +Ein Fehler nach dem Service-Stop kann beide Rollen angehalten lassen. Ein Fehler +nach dem Tag-/Migrationswechsel kann bereits Schema14 bedeuten. Das ist ein +bewusst erhaltener unvollständiger Case für eine konkret geprüfte manuelle +Fortsetzung, keine automatische Wiederholung oder Datenbankrücksetzung. Ein +späterer Rollback muss insbesondere neu gespeicherte 1.20-Pläne berücksichtigen. + +Die alten Quellen unter `/opt/rust-discovery` bleiben historisch erhalten. Der +neue Runtimebezug ist der Casekontext mit seinen Hashes, nicht die Behauptung, +dass dieser alte Checkout nun die laufenden Produktbytes darstellt. Ein späterer +Deploymentlauf muss diese tatsächliche Runtimebasis berücksichtigen. + +## Tatsächliche lokale Prüfung und Grenzen + +Python-Syntax und die erzeugte vollständige Workerquelle sind kompilierbar. +Ein ausschließlich temporäres, danach verworfenes Paket mit ausdrücklich +synthetischer Baseline enthielt exakt158Frozen+3Deploymentdateien sowie die drei +richtigen Webdateien. Alle Einzelhashes stimmen. Create-once verweigert den +zweiten Schreibversuch. Die tatsächliche CAS-Funktion verweigert unabhängig +veränderte Runtimeimage-, PG-Volume-, Migrations-, Jobhistorien-, Index-, Kontroll- +und Containerwerte. Die tatsächliche `status`-Funktion auf einem fehlenden Case +legt nichts an und startet keinen Unterprozess. Frozen158 blieb unverändert. +Beleg: `devstate/script-console-cloud-upgrade-v1-local-review/guards.json`. + +Die vorhandene Compose-CLI-Hilfe wurde tatsächlich lokal gelesen; insbesondere +wird das dort bei `run` nicht vorhandene `--no-build` nicht verwendet. Es wurden +keine SQL-Abfragen gegen eine echte Datenbank und keine Cloudaktionen durch den +Ersteller dieses Nachfolgers ausgeführt. Die SQL-, ARM64-, Stillstands-, Backup-, +Migrations-, Ingress- und tatsächlichen UI-Nachweise sind **noch offen** und +werden erst durch den echten Case belegt. Produktions-OIDC, Clientinstallation, +Fullprofileaktivierung und eine native Skriptwirkung sind keine Ergebnisse eines +Serverbuilds oder dieser Bereitstellung. + +### Tatsächlicher Inspect-Befund und erhaltene Rootschließung + +Beim ersten durch den Hauptagenten ausgeführten **rein lesenden** Serverinspect +verweigerte der erste Guard die tatsächliche bestehende Konfiguration: +`RMM_DEV_ROOT_ENABLED=true` gilt unverändert seit der MVP-Abnahme für API und +Gateway. Root ist durch die abgelaufene Policy geschlossen, nicht durch ein +falsches Flag. Dieser Guardfehler war kein Build-, Deployment-, Tag- oder +Migrationserfolg; es wurde kein Cloud-Buildcase erzeugt. + +Die korrigierte Quelle verlangt genau diesen belegten Bestand: Rootflag `true`, +Rootpolicydatei `/root-policy/policy.json` im bereits bestehenden schreibgeschützten +Bindmount, SYSTEM-Flag und SYSTEM-/Faultdateieinstellungen nicht vorhanden, +DeliveryFaults exakt `false`. Zusätzlich liest sie ausschließlich die bekannte +nicht geheime Policy und verlangt SHA256 +`cf15cbb68b590475df6def28e7dcffb7d88e0ca2d2555a06174fb654eeff05f4`, +übereinstimmend mit dem Main-MVP-Closeout und dem aktuellen System-Readback des +Hauptagenten. Geschützter Eigentümer10001, reguläre Datei0600, Einzelhardlink, +Höchstgröße64KiB und der sichere Elternpfad werden geprüft. Schema1, +Developmentprofil, feste oberste Felder, Scope-Liste und ein expliziter +Zeitzonenbezug der Ablaufzeit sind erforderlich; die Ablaufzeit muss bereits +vergangen sein. Damit verweigern sowohl bisherige als auch neue Serverversion +sämtliche Rootscopes. Der volle feste Hash bindet auch alle historischen +Scopeinhalte; die Quelle akzeptiert keine beliebige abgelaufene Ersatzpolicy. + +Flag, Policy und Mount werden weder verändert noch neu angelegt. Die gleichen +Prüfungen gelten für neue Baseline, Build-CAS, Deploy-CAS und Nachbelege. Bei +Readback werden Policy-Lesefehler als Teilfehler erhalten. Ein neuer Inspect nach +dem korrigierten Sourcefreeze sowie alle tatsächlichen Cloud-Build-/Deployment- +Nachweise stehen weiter aus. Die frühere allgemeine Aussage „keine Cloudaktion +durch den Ersteller“ bezeichnet weiterhin diese Subagentenarbeit; der hier +dokumentierte Read-only-Inspect wurde vom Hauptagenten ausgeführt. + +Der zusätzliche lokale Guard prüfte die tatsächlichen Funktionen mit synthetischen +Dateimetadaten und nicht geheimen Umgebungswerten: unbekannter Policyhash, +unbewiesenes Rootflag, geänderte Flags/Mountbindung sowie zwölf Schema-, Zeit- und +Metadatenabweichungen werden verweigert. Um die nachgelagerten Schema-/Zeitguards +isoliert zu erreichen, wurde ausschließlich in diesen Fixturefällen die +Digestfunktion ersetzt; dies ist kein Nachweis über tatsächliche Serverpolicybytes. +Die echte Hashprüfung wurde separat mit unverändertem SHA256 ausgeführt und +verweigerte die fremden Fixturebytes. Beide bestehenden Rollen benötigen den +geschlossenen Policybeleg. Syntax/Workerquelle und Frozen158 blieben gültig. +Beleg: `devstate/script-console-cloud-upgrade-v1-local-review/closed-policy-guard.json`. + +Der gezielte TLS-Guard ruft die tatsächliche HTTPS-Hilfsfunktion mit gemockten +Verbindungsobjekten für alle drei festen Endpunkte auf: jeweils richtige CA, +TCP-Loopbackziel und unverändertes SNI, keine echte Verbindung. Fehlende API-CA +wirft einen Lesefehler ohne Fallback auf die Gateway-CA; falsche Port-/Namenspaare +werden verweigert. Dieser lokale Guard ersetzt keine echte TLS-Abnahme. diff --git a/docs/script-console-cloud-upgrade-v2.md b/docs/script-console-cloud-upgrade-v2.md new file mode 100644 index 0000000..9ea4c5a --- /dev/null +++ b/docs/script-console-cloud-upgrade-v2.md @@ -0,0 +1,131 @@ +# Cloudserver-Nachfolger v2: kanonische Docker-Mountreihenfolge + +`deploy/cloud_script_console_upgrade_v2.py` ist ein neuer, getrennter Nachfolger. +Der bisherige v1-Runner mit SHA256 +`e88b3d841bac70ebb0bf0d9696bab745c84a17aee9a947a99ff48a60dc18565e` +und Case `499e13fb-7433-405d-9a98-0c27ca864246` bleiben unverändert erhalten. +Die Bereitstellungsgrenzen, Quellen, TLS-CA-Zuordnung, Rootpolicybindung und +Vor-/Nachbelege aus [v1](script-console-cloud-upgrade-v1.md) gelten weiterhin. +Es gibt keine allgemeine CAS-Lockerung und keinen automatischen Wiederholpfad. + +## Tatsächlicher Fehler des ersten Cases + +Der Hauptagent hat den v1-Buildworker einmal gestartet. Das erhaltene +`build-failure.json` meldet `RuntimeError`, Workerzeile402 → +`before_guard`208 → `require`5. Zeile208 vergleicht vollständige +Containerobjekte einschließlich der unsortierten Mountlisten. Docker lieferte +dieselben Mountobjekte bei späterem Inspect in einer anderen Reihenfolge. + +Der lokale, rein lesende Vergleich der Originalbaseline und des tatsächlichen +Status ergab: + +- API vorher: `/root-policy`, `/web`, `/pki`, `/jobs`; nachher: `/jobs`, + `/root-policy`, `/web`, `/pki`. +- Gateway vorher: `/pki`, `/jobs`, `/root-policy`; nachher: `/jobs`, `/root-policy`, + `/pki`. +- Nach Sortieren sind **sämtliche vollständigen Mountobjekte identisch**. + Container-IDs, Images, Einstellungen und alle übrigen Containerattribute sind + identisch; PostgreSQL wurde auch in seiner Listenreihenfolge nicht verändert. + Controls, Datenbank und alter Index stimmen mit der Baseline überein. +- `build_log_bytes` ist `null`. Der Worker scheiterte am vorgeschalteten CAS, + bevor `build.log` geöffnet und `docker build` aufgerufen wurden. Es gibt weder + einen erfolgreichen Candidatebuild noch Deploy-, Tag- oder Migrationswirkung. + +Erhaltene Originalbezüge: + +| Beleg | SHA256 | +| --- | --- | +| Case-Manifest | `305de92d11b90184589210fd2b0f301ecaf5d9a75467bff5e81c8518588ad416` | +| Originalworker | `6d056e6385ba67c00414de3dc626a51b4f022947bee6381d92908acce3a2b4a9` | +| Tatsächlicher Status | `ed2fcd82cdca7692d9c5d122e4cccc1b5aa5a5dc6fec4ac9035ecef647b8e8ef` | + +Diese Bezüge werden auch im v2-Manifest als `supersedes_failed_case` gebunden. +Der alte Case bleibt ausschließlich lesbar und wird nicht fortgesetzt oder +erneut gestartet. + +## Enger Fix + +`canonical_mounts` verlangt eine Liste vollständiger Objekte mit absolutem +`Destination` und Zeichenketten für `Type` und `Source`. Doppelte +Zieldestinationen werden verweigert. Anschließend wird ausschließlich die +Reihenfolge anhand `(Destination, Type, Source)` stabilisiert. Alle Objekte und +Attribute einschließlich `RW`, `Mode`, `Propagation`, `Driver` und `Name` bleiben +vollständig in der Darstellung und damit im strikten CAS enthalten. + +Die Kanonisierung erfolgt direkt nach dem Docker-Read in `container()`. Sie gilt +somit für den ersten Inspect, jeden Worker-/Deploy-Snapshot, Nachbelege und den +Status. `before_guard` und `after_guard` behalten ihre vollständigen +Objektvergleiche. Eine geänderte Quelle, ein Schreibrecht, Volume, Mounttyp oder +beliebiges weiteres Attribut wird weiterhin verweigert. + +Nur neue lokale/entfernte v2-Cases und Candidate-Tags werden verwendet. Der +prozessübergreifende Workerlock bleibt mit v1 geteilt. Frozen29461892 mit158 +Dateien, drei separate Deploymentdateien, Web49 mit drei Dateien, unverändertes +Dockerfile, Compose, Ingress und Rootpolicy bleiben erhalten. Es werden weder +RMM-Jobs noch Genehmigungen oder Client-/Policyänderungen vorbereitet. + +## Geplante Fortsetzung, nicht ausgeführt + +Nach unabhängigem Rootreview ist zuerst ein **frischer Read-only-Inspect mit v2** +erforderlich. Keine alte Baseline umschreiben und keinen v1-Case umdeuten. Erst +mit dem neuen Baselinebeleg darf ein neuer v2-Case offline vorbereitet und nach +gesonderter konkreter Prüfung einmal gestartet werden: + +```text +python3 deploy/cloud_script_console_upgrade_v2.py inspect --access-root /pfad/zum/hauptcheckout +python3 deploy/cloud_script_console_upgrade_v2.py prepare --baseline /pfad/zur/neuen/v2-baseline.json +python3 deploy/cloud_script_console_upgrade_v2.py build --id NEUER_CASE --access-root /pfad/zum/hauptcheckout --execute-cloud-build +python3 deploy/cloud_script_console_upgrade_v2.py status --id NEUER_CASE --access-root /pfad/zum/hauptcheckout +``` + +Ein Build- oder Deployaufruf nach Antwortverlust bleibt verboten; ausschließlich +Status desselben Cases. Der historische v1-Case wird weiterhin ausschließlich +mit dem eingefrorenen v1-Runner gelesen. Dieser neue v2-Quellstand hat durch den +Subagenten weder einen SSH-Aufruf noch einen neuen Cloudcase, Build, Tagwechsel, +Migrationslauf oder Deployment ausgeführt. + +## Tatsächliche lokale Prüfgrenzen + +Der gezielte Guard nutzt die erhaltenen echten Originalbaseline-/Statusbytes: +alle Containervergleiche stimmen nach der Kanonisierung überein; alle möglichen +Reihenfolgen der bestehenden Mountlisten liefern dieselbe vollständige Liste. +Geänderte Source-, RW-, Mode-, Propagation-, Type-, Driver- und Namewerte bleiben +abweichend und werden durch den tatsächlichen CAS verweigert. Duplikate und +falsche Typen werden vor dem Vergleich verweigert. Python und erzeugte +Workerquelle sind kompilierbar; Originalrunner, Originalcase und Frozen158 bleiben +unverändert. Der lokale temporäre Paketcheck bindet weiterhin exakt161Quell- und +drei Webdateien sowie den Originalfehlerbezug. Dies ersetzt keine neuen nativen +Servernachweise. + + +## Tatsächlich gestarteter neuer Fall + +Nach Rootreview wurde ein neuer Remote-Readonly-Inspect ausgeführt, +Baseline-SHA256 +`ad4e0ee543cb4f9d995f57696829ab868422be7d57aba6a404f0a49a5cbd4d4a`. +Danach separater neuer Fall `14dcc27d-e3ba-4089-9a43-74fd63a9382d`, +Manifest-SHA256 +`43d4af86a81578ccd4ab46c99a50c6e4e4030178a1379784cbcacb55d52c3717`. +Root startete dessen Buildworker einmal; tatsächlicher Status bestätigt +Buildlog und unveränderte161 Eingaben. Build noch ohne Erfolgsbeleg; bestehendes +Serverimage unverändert, kein Deploy-/Migrationsaufruf. Weitere Beobachtung +verwendet ausschließlich `status` dieses Cases. + + +Getrennter tatsächlicher Status desselben Falls bestätigt nun **Build bestanden**: +ARM64-Candidateimage +`sha256:6f8383f7c762ac77ed1013c94eeafa2b76838c00aec358a375f5847645ea87ec`. +Alle161Quelldateien unverändert, laufendes Image weiterhin25697f94, +keine Migration und keine Jobs/Genehmigungen. Der Buildbeleg ist kein +Deployment- oder WebUI-/Client-Abnahmenachweis. + + +Die anschließende einmalige Bereitstellung wurde separat gestartet und ihr +Status zurückgelesen: **Deployment bestanden**. API/Gateway laufen mit dem +oben genannten ARM64-Image als UID10001 und weiterhin ausschließlich an den +privaten Loopbackports. Datenbankbackup bleibt privat; Migration14 ist angewandt, +Checksummen1–13 sowie ursprüngliche Ledger-/Job-/Audit-/Identitätsgeschichte +identisch. Alle drei Webdateien sind per geprüftem TLS bytegleich ausgeliefert; +Index wurde zuletzt atomar veröffentlicht. Öffentliche Verwaltungsrouten bleiben +geschlossen. Keine Jobs, Freigaben oder Root-/SYSTEM-Policyöffnung entstanden. +Das qualifiziert die Bereitstellung, noch keinen nativen Skriptauftrag. diff --git a/docs/script-console-contract-proposal.md b/docs/script-console-contract-proposal.md new file mode 100644 index 0000000..b0ffe06 --- /dev/null +++ b/docs/script-console-contract-proposal.md @@ -0,0 +1,277 @@ +# Skriptkonsole: Root-/SYSTEM-Vertrag1.20 und tatsächliche Nachweise + +> **Aktualisierung1.20, 2026-11-14:** Der User und Rootreview haben den nun in +> Spezifikation0.1, project-decisions und development dokumentierten Vollwartungs- +> vertrag beschlossen. Der folgende ursprüngliche Entwurf ist als historische +> Herleitung zu lesen: Insbesondere „kein neues Wirefeld“ und die Beibehaltung +> des beschränkten Generalprofils als Wartungsruntime sind überstimmt. Aktuell +> gilt das additive `privileged_script_profile="full_maintenance_v1"`, getrennte +> explizite Policyversionen und eine eigene Linux-Helperroute. Kein stilles +> Erweitern von General/APT/SCM/MSI. Quelle/Build/Unit/Native weiterhin getrennt. + + +**Ursprünglicher Entwurfsstand**, Basis `287a784`: Damals noch nicht +beschlossen oder implementiert. Der aktuelle beschlossene Vertrag und die +späteren tatsächlichen Quellen-/Prüfnachweise stehen oben und am Dateiende. +Dieser neue Fokus ersetzt die EXE-Priorität; der EXE-Normpatch bleibt ein +unbeschlossener separater Archivstand. Vollständige Pflichtdokumente wurden +gelesen; Spec1.19/PD/Dev sind bytegleich zum zuvor vollständig gelesenen Stand. +Hier werden nur vorhandene Pfade auditiert und konkrete Änderungen vorgeschlagen. +Kein Produkt-/Norm-/APIcode, Build, VMkontakt oder Ausführungsjob geändert. + +## Bereits vorhanden und tatsächliche Lücken + +`web/src/main.tsx`75–90 hat bereits vollständigen UTF-8-Editor, Linux/Windows- +Plattformwahl, feste Interpreter, erwartete stdout-Nachbedingung und explizite +Identitätsauswahl. Planansicht/Recheck/Submit/Approval bestehen. Operations- und +Jobdetails152–165 pollen tatsächlich Zustand/Ergebnis und bieten Abbruch an. +Ausgabe erscheint bisher nur im allgemeinen JSON, nicht als lesbare Konsole. +SYSTEM ist für RunScript in UI, Domain, Backendpolicy und Helper bewusst gesperrt. + +Linux-Rootskripte existieren: zentral `root_policy.rs` mit `allow_root_scripts`, +`root:execute` und den allgemeinen Skriptrechten; lokal unabhängiges +`allow_root_scripts`, NativeClock/RootSync und atomarer Originalgrantverbrauch. +`privileged-helper::script` nutzt den vorhandenen Linux-Adapter. Dessen +`process::run` hat Prozessgruppe, feste Umgebung, Timeout und beschränkte Pipes, +aber **keine laufende Cancel-/Fenceabfrage**. Ein Timeout oder unbelegtes Ende +wird Unknown; ein Kill beweist keine rückgängig gemachten Wirkungen. + +Windows hat einen sicheren PowerShell-Datei-/JobObject-Pfad in `actions.rs` und +`process.rs`, ist aber ausdrücklich LocalService/SID19. Ein Umbenennen der +Identität wäre falsch. SYSTEM muss unter dem tatsächlich authentisierten +SYSTEM-Helper/SID18 starten und dessen privaten Arbeitsbereich verwenden. +Intent/Abandon/RawMirror sind vorhanden; WindowsClock bleibt ohne echten +TrustedClock-Nachweis geschlossen. Linuxbelege ersetzen diesen nicht. + +Der aktuelle Protobufvertrag hat nur `JobResult.stdout/stderr`; keinen +JobOutputChunk und keinen implementierten JobProgress-Outputstream. Kleinster +vollständiger Batch-Skriptkonsolenpfad: während Ausführung echte Statusanzeige, +Laufzeit und Cancel-Anforderung; begrenzte stdout/stderr mit Rohbytes nach +dauerhaftem Ergebnis. **Keine behauptete Liveausgabe oder interaktive PTY.** +Falls Root/User Ausgabe bereits während der Ausführung verlangt, ist dafür ein +separat zu prüfender begrenzter dauerhafter Chunkvertrag erforderlich; kein +UI-Polling von flüchtigen Helperdateien oder vorgetäuschtes Streaming. + +## Enger Funktions- und Autoritätsvertrag + +Weiterhin genau `RunScript`, kein neues Actionformat, universeller Execute- +Endpoint oder Terminalserver. Default bleibt `agent_service`; Root/SYSTEM nur +ausdrücklich gewählt. Die kompletten eingegebenen UTF-8-Bytes, Interpreter, +Argumente, Ausgabe-/Zeitlimit, Identität, Plattformvariante und gegebenenfalls +vorhandenes PowerShell-RemoteSigned-Profil werden **vor** Hash/Planbindung +festgeschrieben. Kein BOM-/Zeilenenden-/Prolognachrüsten nach Genehmigung. +Nicht-ASCII-PowerShell braucht das bestehende explizite BOM im Editorinhalt. +UI darf Argumente zunächst als feste leere Liste halten; keine freien +Executablepfade, URLs oder `-Command`-/EncodedCommand-Launcherparameter. +Linux verwendet `/bin/sh -- ` oder nachgewiesenes `/bin/bash`; +Windows fest WindowsPowerShell5.1 mit `-NoLogo -NoProfile -NonInteractive`, +optional genehmigtem prozessbezogenen RemoteSigned und `-File `. +Kein pwsh-Fallback oder ExecutionPolicy-Abschalten, GPO bleibt vorrangig. + +Jeder Auftrag bleibt pending_approval ohne Jobs/Outbox/Offers/Grants bis zum +tatsächlichen WebUI-Klick. Ersteller **und** Freigeber benötigen ihre aktuellen +Operationsrechte und `scripts:execute`, zusätzlich `root:execute` bzw. +`system:execute`. Keine neuen allgemeinen Rechtebezeichner, kein Autoapprove. +Zentrale geräte-/principal-/generations-/helperhashgebundene Policy und lokale +Helperpolicy müssen privilegierte Skripte zusätzlich ausdrücklich zulassen. +Linux verwendet das bestehende Opt-in; Windows erhält ein versioniertes +`allow_system_scripts`-Opt-in, alte SCM/MSI-Settings verweigern weiter Skripte. +Interpreter und Maximalbudget sind feste überprüfbare Grenzen, keine +Befehlsblockliste. Erforderlich bleiben bisherige Plattform-/Interpretercaps, +Helperpins und signedSync; SYSTEM braucht zusätzlich die enge +`system:run_script`-Capability. Nichts davon aus Build oder Bootstrap publizieren. + +Kleinste erste Grenzen: Skript1–32768Bytes, insgesamt höchstens65536Bytes +stdout+stderr, Laufzeit1–300s für Linux-Root wie der vorhandene zentrale Deckel; +SYSTEM zunächst1–120s wie die vorhandene Helper-Lane. UI validiert diese +tatsächlichen Grenzen vor Planung. Größere Budgets sind keine stillen Defaults. +Keine Wire-/JobPayload-/Grantcodecänderung nötig; Linux- und bisherige +agent_service-/PowerShell-RemoteSigned-Goldens bleiben erhalten. + +## Tatsächliche Privilegien, Sandbox und Reboot + +**Ein genehmigtes allgemeines Root-/SYSTEM-Skript ist bewusste Volladministration +im tatsächlich gestarteten Dienstkontext, keine sichere Standardaktion.** +Es kann Dateien/PKI/Journale, Konten, Dienste, Paketmanager, Egress und gegebenenfalls +den OS-Neustart beeinflussen oder andere Programme nachladen. Der gebundene +Skripttext pinnt nicht automatisch alle später von ihm gelesenen Programme und +Netzbytes. Keine Befehlsblockliste kann das zu einem harmlosen Profil machen. +Ein Angreifer mit genehmigtem Rootcode ist außerhalb der Integritätsgarantie +dieses lokalen Helpers; Benutzer mit diesem Recht müssen als Hostadministratoren +vertrauenswürdig sein. Die Freigabe ist niemals eine KI-Dauerberechtigung. + +Der aktuelle Linux-Generalhelper ist tatsächlich UID0, hat aber unter anderem +PrivateNetwork, ProtectSystem, begrenzte Capabilities und Seccomp. Diese Grenzen +bleiben im ersten Integrationsslice **unverändert**; manche Skripte werden damit +ehrlich fehlschlagen. Sie sind kein zugesicherter Sandboxschutz gegen allgemeinen +Rootcode, insbesondere nicht gegen Delegation an vorhandene privilegierte +Betriebssystemdienste. UI muss UID0/SID18 und den konkreten qualifizierten +Runtime-/Policykontext zeigen, nicht „uneingeschränkte Hostrechte“ erfinden. +Falls uneingeschränkter Hostkontext benötigt wird, braucht er einen gesonderten +explizit geprüften und gepinnten Bootstrapprofilwechsel; keine stille Entgrenzung +der vorhandenen Helper-/APT-/MSI-Dienste im Skriptslice. + +Eine Skriptgenehmigung erlaubt die sichtbaren Skriptbytes und deren mögliche +administrative Wirkung, **keinen zusätzlichen RMM-Reboot-/Paketjob**. Skript- +Exit0 beweist nur Prozess-/gebundene stdout-Nachbedingung, keine Paketabnahme. +Ein vom Skript ausgelöster Neustart ist kein erfolgreicher typisierter Rebootjob: +ohne dessen eigenen unabhängigen Vorher-/Nachher-/Fristbeleg bleibt der +Skriptversuch bei fehlendem Ergebnis Unknown. Keine implizite OS-Rebootabnahme, +kein nochmaliges Ausführen nach Reconnect und keine aus einem Warntext abgeleitete +allgemeine Erlaubnis, tatsächliche Testreboots/Updates durchzuführen. + +## Dauerhaftigkeit, Abbruch und lesbare Ausgabe + +Dieselbe OriginalRequest-/Plan-/Grantprüfung, ein einziger atomarer Start, +Outcome zuerst dauerhaft, dann Jobjournal/Ergebnis und normaler SignedSync. +Windows verwendet genau den vorhandenen Intent→MayHaveDispatched→Helper-/ +RawFullTupleMirror-Pfad. Abandon ausschließlich bei belegtem Nichtdispatch, +ohne Script-/Temporärdateiwirkung; Originalgrant nie leeren, Unknown/Missing- +Guards nie lockern. Keine zweite Zustandsmaschine oder Ausführungswiederholung. + +Linuxprozess erhält einen tatsächlichen kooperativen Cancel-/Fencecallback +innerhalb der bestehenden Poll-/Pipe-Schleife. Windows nutzt den vorhandenen +JobObject-Prozessbaum mit tatsächlichem SYSTEM-Token und entsprechender +Fence-/Cancelprüfung. Nur der eigene beobachtete Prozessbaum darf begrenzt +beendet werden; kein globales Kill. Abbruch/Timeout nach möglicher Wirkung +bleibt Unknown. Kernelbestätigtes Prozessende ist kein NoEffect-/Rollbackbeweis. +Ein durch Root/SYSTEM abgespalteter Dienst/Task kann außerhalb des beobachteten +Baums weiterwirken; keine vollständige Prozesskonfinierung behaupten. Wenn +Beendigung nicht bestätigt ist, behält der Helper eigene Child-/Handlebindung +und die Ressourcensperre; kein neuer Start aus bloßem Pipe-/Channelclose. + +UI zeigt getrennte stdout/stderr, Exitcode, Trunkierung, tatsächliche Identität, +Zustand/Unknown und Cancelstatus. Rohbytes bleiben unverändert gespeichert; +Textdarstellung bekommt explizite Decoder-/Encodingangabe und optional Byteansicht, +keine stillen Ersatzzeichen als Originaloutput ausgeben. Nur Reacttext, kein +HTML/ANSI-Scriptinterpretieren oder zustandsändernde Aktion aus Logtext. +Anzeigenaliase und später mutierte APIobjekte müssen gegen gespeicherte +Planpayloadbytes geprüft werden, auch für Linuxroot und WindowsSYSTEM. + +## Reihenfolge und kleinste Dateigrenzen nach Rootreview + +1. Root beschließt den Skriptvertrag1.20; EXE1.20 wird nicht übernommen. +2. UI-Konsole/Rootpfad und Cancel-/Outcomeownership lokal qualifizieren; + danach tatsächlicher Cloud-Linux-RunScript mit WebUI-Genehmigung, stdout/stderr, + Rootidentität, einmaligem Grant und verlorenem Reply/Abbruch korrekt nachweisen. +3. SYSTEMpfad sourceimplementieren, portable Guards Linux-first→Windows; + erst nach echter Clock-/Peer-/Helper-/PERSISTqualifikation Windows-Skriptjobs + per WebUI. Keine Windowsfähigkeit aus dem Linuxnachweis ableiten. + +| Scope | Engste Änderungen nach Freigabe | +| --- | --- | +| Norm | Root: Spec1.20/PD/Dev für Skripte statt EXE; explizite Administrator-/Rebootgrenzen | +| Web | bestehende main/api/safety+betroffene Tests: vorhandenen Editor behalten, SYSTEM-Option für RunScript, verifiedScriptProjection, lesbare Ausgabe/Cancelstatus | +| Linux | agent-linux actions/process; privileged-helper script/main/control und Agenthelper-Routing nur soweit Cancel/gebundene Ownership nötig; keine stille Unitlockerung | +| Windows | actions/process um explizite getrennte LocalService-/SYSTEM-Einstiege; helper_system/helper_runtime/runtime um RunScript unter bestehenden Intent-/Mirrorgrenzen; private gehaltene SYSTEM-Skriptdatei | +| Domain/Backend | jobs.available_for/valid_helper_denial_shape; windows_policy und vorhandene DTO-/Gateprüfungen, bisheriger Rootpfad erhalten; neue lokale bool-Settings nur versioniert und None-omit | +| Core/Protocol/SQL | keine neue Zustandsmaschine oder Job-/Grantfelder geplant; bestehende Codecs/Unlocks unverändert, Chunktransport ausdrücklich nicht Bestandteil des ersten Batchpfads | + +Alle Angaben oberhalb sind Sourceaudit und Entwurf. Tatsächliche privilegierte +Skript-/Abbruch-/Runtimeabnahmen bleiben offen, bis Root sie getrennt ausführt. + +## Implementierungsstand des genehmigten1.20-Schnitts + +Lokal implementiert: additives strikt nichtnull Profil am Ende der RunScript- +Action, Identitäts-/Capability-/Laufzeit-/Outputgrenzen und unveränderte Legacy- +Bytes; drei echte Rust-Codecplanfamilien in `tests/script_console/codec-golden.json`. +Zentrale Rootversion2 und SYSTEMversion3 erfordern zusätzliche explizite +`allowed_script_profiles`; beide Akteure benötigen aktuelle Skript- und jeweilige +Privilegrechte. Eigene Linuxroute `RMM_FULL_MAINTENANCE_HELPER_SOCKET` mit +`RMM_FULL_MAINTENANCE_POLICY_FILE`, lokalem FullMaintenanceprofil und unverändertem +Sharedledger; bestehende General-/APT-Dienste werden nicht geändert. + +`deploy/linux/rmm-full-maintenance-helper.service` ist ein reviewbarer, noch +nicht installierter vollständiger Hostkontext ohne die General-Sandbox. Vor +Zieländerung sind tatsächliche zusammengesetzte Unit, Gruppen-/Socket-/Pinpfade, +Rootbesitz, Journale und Inodes zu prüfen. Der eigene Socket und Pin müssen +explizit auf das FullMaintenanceprofil zeigen; kein Fallback auf General/APT. +Eine passende Bootstrapkonfiguration enthält `profile=full_maintenance_v1`, +`allow_root_scripts=true`, `allowed_script_profiles=["full_maintenance_v1"]`, +leere allowed_services, allow_reboot=false und keinen apt_provider. Das letzte +Flag begrenzt nur die typisierte Helperaction, nicht den genehmigten Rootcode. + +Laufender Linuxabbruch benutzt ausschließlich EOF/Halbschließen des vorhandenen +originalrequestgebundenen, beidseitig peergeprüften Unixstreams. Der Agent +beobachtet konkreten Attempt-Cancel, aktuelle Kanalgeneration und lokale Clock; +der Helper beobachtet Peer/EOF/Clock. Ein abgelaufenes Startticket beendet keinen +bereits gültig begonnenen Prozess. Eigene Prozessgruppe wird beendet und unter +beibehaltener lokaler Effektlock bis zum belegten Ende lebender Mitglieder +beobachtet. Zombieeinträge werden anhand ihres tatsächlichen /proc-Status von +lebender Wirkung unterschieden, bei unklarer Beobachtung bleibt die Lock erhalten. + +Windows besitzt einen gesonderten SYSTEM-Skriptpfad nach atomarem Verbrauch, +mit privater gehaltenem Skriptdateihandle, exakt erhaltenen Bytes, festem +PowerShell5.1-argv und JobObject. Clock/Peer/Intent/STOP bleiben beobachtet, +Originalgrant wird unmittelbar vor Prozessstart frisch geprüft. Nach Start +bleibt seine Startfrist eine Startfrist. Unklare Prozessbereinigung behält +JobObject/Prozesshandles und Helperlane bis zum belegten Ende. SID18 und Profil- +/Skripthash werden im Ergebnis gebunden; LocalService-Admission bleibt separat. +Keine neue Windowscapability ist durch diese Quellen öffentlich aktiviert. + +Tatsächliche Zwischenprüfung:22 Domain-Tests lokal im unveränderten Offline- +Builder9a76ad37… bestanden, einschließlich alter Bytegoldens und neuer Profile. +Weitere Prüfungen und native Cloud-Linux-/Windows-Abnahmen sind noch offen; +kein neues RMM-Zielprofil installiert, kein Job gestartet oder genehmigt. + +## Eingefrorene lokale Qualifikation1.20 + +Der abschließende lokale Lauf ist vollständig:157 Compiler-/Include-/Fixture-/ +Deploymentinputs unverändert vor/nach; Manifest +`60a50262f9f8d85c11eb58e1a9e0f6e03be6cd5db7a59bc1745fb7d9dc8a6f65`. +Linuxfmt/Workspace-Alltargets-Clippy mit -Dwarnings bestanden;284 lokale Tests +bestanden und zunächst5DB-Fälle ausgelassen, anschließend sämtliche45Servertests +inklusive dieser5 in frischem internem PostgreSQL bestanden (0ignored). WindowsGNU +Default/Systemhelper-Alltargets-Clippy bestanden; keine native Windowsprüfung. +Release-Linux-Unitlisten und Agent-/Helper-/Serverbinaries sind gebunden gebaut, +noch nicht auf Zielen ausgeführt oder installiert. Vorherige unvollständige +Packvorbereitungen sind separat protokolliert, keine bestandenen Qualifikationen. + +Der separate Create-once-Linuxrunner ist vorbereitet, noch nicht ausgeführt; +kein Compilerinput wurde dafür verändert. Konkrete lokale Belege, geplante +Befehle, Erwartung83PASS/5expliziteDBignored auf Cloud-Linux und die noch nötige +Erhaltungs-/Bootstrap-/WebUI-Abnahme stehen in +[deploy/linux/full-maintenance-README.md](../deploy/linux/full-maintenance-README.md). + + +## Separater Reviewblocker nach der ersten Qualifikation + +Ein unabhängiger Review fand nach Packde34 die fehlende tatsächliche laufende +Linux-Fullhelperbinarybindung. Dieser Pack bleibt unverändert früherer Beleg +und berechtigt keine Fullaktivierung. Ein enger separater Corefix bindet +`helper_binary_sha256` ausschließlich im neuen Fullschema/Pin/Policy/Probe und +zentralen Scope; Root hält/prüft sein echtes laufendes Image, Agent993 benötigt +keinen fremden Root-/proc-Zugriff. General/APT lassen die neuen None-Felder +vollständig aus. Core-Review erfolgt vor neuem Build; neue tatsächliche +Quellen-/Unit-/VM-Belege werden separat ergänzt, nicht vom alten Pack abgeleitet. +Der Native-Runner ist vorbereitet und lokal geparst, noch nicht ausgeführt. + + +## Neue tatsächliche lokale Qualifikation nach Binärpinfix + +Der neue Pack29461892 ist ein eigener Beleg; de34 und der ursprüngliche Reviewdiff +bleiben erhalten. Manifest +`ddd1a78d1cd289b02b1f98572f9652171ec00be84e01ce099fd722d689bcaacd`, +158 Inputs unverändert vor/nach. Format und Linux-/beide Windows-GNU-Clippyprofile +mit-Dwarnings bestanden;285 lokale Tests bestanden, anschließend alle45Servertests +mit tatsächlichem isoliertem PostgreSQL einschließlich vorheriger5DBignored +bestanden (0ignored). Der neue Rootimage-Test prüft in jedem Eigentümerpfad +Sicherheitsassertions, kein stilles Skip. Native Linuxlisten22/16/6/45 gebaut, +auf der Cloud-VM noch nicht ausgeführt; neue Erwartung84PASS/5expliziteDBignored. +Der Native-Runner ist separat ausdrücklich auf diesen neuen Pack gebunden und +lokal geparst; seine Supportquellen/Scopetatsachen sind in Intent und Readback +streng geprüft. Bisher keine SSH-, VM-, Enrollment-, Dienst-, Policy-, Job- oder +Capabilityänderung durch diesen Strang. Aktueller Primärstand und noch geplante +Bootstrap-/WebUI-Abnahmen: `deploy/linux/full-maintenance-README.md`. + +## Tatsächlich nativ auf Cloud-Linux geprüft + +Der korrigierte158-Input-Pack29461892/ddd1a78d wurde durch Root nach beiden +Source-Reviews auf der tatsächlichen Cloud-Linux-Test-VM ausgeführt. Fall +`7b61ce46-7913-492b-9c6b-414a0c2b6ffc`: Domain22, Linuxruntime16, +Helperpolicy6, Serververträge40PASS plus5explizite DBignored; insgesamt84PASS. +Alle vollständigen Listen wurden zuerst geprüft. Die fünf DBfälle sind nur +im separaten isolierten45er-PostgreSQL-Lauf nachgewiesen. NativeproofSHA256 +`a831a70a3cf76a3e8a9bf5820dcca7100d2ef9860dd9388539fe9c13f949f470`. +Keine Zielruntime bereitgestellt oder Job-/Capability-/Policyfreigabe. Die +tatsächliche WebUI-genehmigte volle Wartung bleibt ein eigener offener Schritt. diff --git a/docs/script-console-helper-generations.md b/docs/script-console-helper-generations.md new file mode 100644 index 0000000..460febb --- /dev/null +++ b/docs/script-console-helper-generations.md @@ -0,0 +1,83 @@ +# Gemeinsames Root-Ledger und Helpergenerationen + +Entscheidung2026-11-14, Spezifikation1.20. Dieser Nachtrag erklärt eine im +Bootstrapreview gefundene Voraussetzung der neuen Skriptkonsole. Dieser +Entscheidungstext ersetzt keinen Ausführungsbeleg und ändert keinen eingefrorenen +Compilerinput. Der inzwischen tatsächlich erhaltend ausgeführte Paarwechsel mit +unveränderten ursprünglichen Fehlerberichten und getrennten Erststartfortsetzungen +ist in den [Bootstrap-Ergebnissen](script-console-bootstrap-results.md) belegt. +Agent, General und Full laufen; signierter Folgeabgleich und WebUI-genehmigter +Fulljob sind dadurch noch nicht abgenommen. + +`consumed` ist kein bloßer Pool opaker Hashes: `execution_evidence.start` enthält +den vollständigen Originalrequest mit exakten Jobbytes, Grantbytes und Signatur. +`helper_reconcile::load_rows` prüft bei jedem Snapshot sämtliche gespeicherten +Versuche über `Binding::verify_with` und `Binding::job`. Dabei wird die Action +mit `deny_unknown_fields` dekodiert. Ein alter Helper kennt +`privileged_script_profile` nicht und verweigert deshalb auch eine vollständig +abgeschlossene neue FullMaintenancezeile. Ein solcher Snapshotfehler kann einen +sticky Journalfault schreiben. Ein eigener neuer Fullhelper neben einem alten +weiterbenutzten Generalhelper ist daher keine kompatible Lösung. Dasselbe gilt +für einen später wieder gestarteten alten APT-Helper am gemeinsamen Ledger. + +Beschlossener enger MVP-Weg: Neuer Agent und alle **aktiven** Root-Ledger- +Teilnehmer General und Full verwenden die neue, decoderfähige qualifizierte +Codegeneration. Bestehende Generalunit, Sandbox, Konfiguration und Policy-/Pinbytes +bleiben erhalten; daraus entstehen keine neuen Generalrechte. Native Units +beweisen noch keinen laufenden Generationswechsel. Erforderlich bleiben frischer +Vorher-CAS, kontrollierter erhaltender Austausch, tatsächliche neue Prozessimages, +Erhalt aller drei Datenbankinodes und ursprünglichen18 Rootverbräuche samt +Request-/Grant-/Signatur-/Start-/Outcomebytes, danach normaler signierter Abgleich. +Keine Historyreparatur, kein Ledgerreset oder manuelles Löschen einer Sperre. + +APT bleibt ausdrücklich disabled/inactive, ohne Agentendpoint und mit geschlossenen +Scopes. Der vorhandene private Runtimeindex bestätigt, dass die aktuelle APT- +Generation **bereits vor diesem Vorhaben** nicht mehr als qualifiziert galt. +Hier wird kein neuer Statusverlust einer aktuell bereit gemeldeten APT-Fähigkeit +behauptet. Die alten Abnahmebelege und der bekannte Acceptance-Aliaszweilinkfall +bleiben unveränderte historische Nachweise. + +Der APT-Provider bindet in `runtime_identity()` auch den tatsächlichen globalen +Helperbinaryhash, neben Provider/Hook, OS/Kernel, Pakettools, Konfiguration und +Unit. Ein neuer globaler Helper bedeutet eine andere Runtime. Vor jeder späteren +APT-Wiedereröffnung ist daher ein eigener versionierter Runtime-/Stackpinupgrade +mit tatsächlicher Generationqualifikation erforderlich; alte Accepted-Belege +werden nicht als aktuelle Startberechtigung ausgegeben. Ein alter Decoder darf +nicht versehentlich wieder gegen das nun erweiterte Ledger starten. + +Bootstrapvorbereitung verwendet den tatsächlich belegten aktuellen Runtimeindex +und einen zusätzlichen frischen lesenden VM-CAS; ein alter lokaler Index allein +ist kein aktueller Zielbeleg. Die neue Fullkonfiguration, der geprüfte Schema2- +Code-/Policypin, eigener Socket und Agentdropin werden als konkrete neue Dateien +reviewt. Kein Fulljob vor tatsächlicher WebUI-Bereitstellung und Genehmigung. +Paketwirkungen, Updates oder Reboots gehören nicht zu dieser Bootstrapabnahme. + +Nach dem MVP vormerken: explizite unterstützte Leser-/Schreibergenerationsmatrix +und geregelte Capability-/Aktionsmigrationen für geteilte Journale. Historische +Originalbytes dürfen für ältere Leser nicht still umgeschrieben oder ungeprüft +als undurchsichtige Daten akzeptiert werden. + + +## Tatsächlicher Bootstrapzwischenstand + +Root hat Snapshotde2452d0 lesend geprüft (SHA97c9f50f) und den eigenen +Installfall `2e27c312-58cc-444d-92a6-863a11ac6ffd` mit unverändertem Runner +`667b56f7…` genau einmal ausgeführt. Staging und Austausch der neuen +Agent-/Helpergeneration0a7ad6ee/79442727 sowie Generalpin und neuer Fullpin +wurden tatsächlich geprüft. Alle ursprünglichen SQL-/Datei-/PKIzeugen waren +vor Neustart identisch. Kein Job oder Reboot. + +Der ursprüngliche Installbericht bleibt **fehlgeschlagen**: SHA256 +`f752258c02e30e268d6a76d1e9fa98ec56e287ca75d3c8a8314a9034d6109544`. +Vor `restart-intent` stoppte die effektive Full-unit-Prüfung. Ein separater +lesender `systemctl show` bestätigt sämtliche erwarteten Konten/no-Werte und +leere RootDirectory/RootImage; ausschließlich SystemCallFilter und +RestrictAddressFamilies zeigen `~`. Das bezeichnet eine leere Denyliste; +[systemd dokumentiert die Tilde als Denylistenmodus](https://freedesktop.org/software/systemd/man/latest/systemd.exec.html). +Der Installer verlangte dort einen leeren String. + +Unabhängiges Readback bestätigt Agent, General und Full inaktiv/PID0, +`restart-intent` und alle drei Startmarker abwesend, aktuelle und erhaltene alte +Binärhashes korrekt, APT weiterdisabled. Eine eng begrenzte erste Startfortsetzung +wird separat geprüft. Der ursprüngliche Installaufruf wird nicht wiederholt; +SignedSync-/Heartbeat-/Inventory- und Fulljob-Abnahme sind weiterhin offen. diff --git a/docs/script-console-job-acceptance-v2.md b/docs/script-console-job-acceptance-v2.md new file mode 100644 index 0000000..9d0d198 --- /dev/null +++ b/docs/script-console-job-acceptance-v2.md @@ -0,0 +1,53 @@ +# MCP-Abnahmerunner V2: leeres SSE-Initialframe + +Der erste tatsächliche Planmodus mit der bisherigen Quelle `dcccd789c2eab18…` +im Fall `b8a4ea33-ab19-45a0-b838-c5dd6cf6d75a` hielt während MCP-Initialize mit +`JSONDecodeError` an. Der separate strikt lesende Lookup belegte null gespeicherte +Pläne und Anträge, `plan-entered:true`, `submit-entered:false`. Der Fall wird nicht +für einen erneuten Plan-/Submitversuch verwendet; ursprüngliche Intents, +Marker, Zeiten und Inputbytes bleiben unverändert. + +Die danach tatsächlich durch Root ausgeführte Initialize-only-Diagnose belegt: +HTTP 200, `text/event-stream`, vorhandener Sessionheader, zuerst eine leere +Datazeile mit sieben Byte, zwei Eventmetadatenzeilen und ein Frameabschluss; +anschließend eine gültige JSON-Datazeile mit 307 Byte. Es wurde kein Toolcall +gesendet. Beleg `initialize-frame-shape-readonly.json`, SHA256 +`40790d612b840c6073684a5ea2ee308b0dba5c42beea29c0d43922a8f7e0c3b8`. +Der alte Parser übergab das vollständige leere Datenframe an `json.loads(b'')`. +Das ist ein Fehler des Bootstrap-SSE-Parsers, kein Produktjobfehler. + +Die neue separate Quelle ist `deploy/script_console_job_acceptance_v2.py`, SHA256 +`c7f42df0a7e89274b7e255ac728ec4198a02278926348113c5b7e363e8983757`. +Die ursprüngliche Datei bleibt unverändert als ausführbarer Reader für ihren +Originalfall erhalten. Zusätzlich wurden Quelle und alle neun damaligen +B8-Belege create-only unter +`devstate/source-history/job-ops-dccc-sse-dcc82959-51cd-424e-84ab-6dd56de36689/` +archiviert; Manifest-SHA256 +`c9a1dee9e3ca71539c94b59433297dfd3be5e1687a212ad030226eb09765280d`. + +Die einzige geänderte Funktionsfamilie ist SSE-Framing: Erst ein vollständig +abgeschlossenes Datenframe bilden, den Puffer zurücksetzen und ausschließlich +die leere Bytefolge `b''` vor dem JSON-Parse überspringen. Nach `data:` wird nur +die eine im SSE-Format optionale Trennspace entfernt; weitere Whitespacebytes +bleiben erhalten. Dadurch werden nichtleere Whitespaceframes und anderes +ungültiges JSON weiterhin als Fehler verweigert. Mehrzeilige gültige JSON- +Datenframes funktionieren weiterhin. Byte-/Zeilenlimits bleiben unverändert. + +HTTP/TLS/CA/SNI, Tokenbehandlung, MCP-Initialize-/Session-/ID-Prüfungen, +Actor-/Rechte-/Policyguards, Ganzzahl-Nanosekunden, kurze SQLite-Reads, +Originalplan-/Job-/Grantbindungen und Wiederholungssperren sind unverändert. +Es gibt weiterhin keinen Approval-, Browserautomations-, Rechte-, Policy-, +Cancel- oder Retrypfad; der [Grundvertrag](script-console-job-acceptance.md) +bleibt bestehen. Insbesondere wird der fehlgeschlagene B8-Fall nicht umetikettiert. + +Tatsächlich lokal geprüft: Python-Syntax; der genaue neue Parser-AST mit einem +Frameverlauf entsprechend dem tatsächlichen Rootbeleg; anschließend gültiges +Initialize-JSON. Nichtleeres `not-json`, `{invalid}` und ein Frame aus drei +Whitespacebytes wurden jeweils als JSON-Fehler verweigert. Mehrzeiliges gültiges +JSON bestand. Kein Zielaufruf und kein neuer Fall wurden dabei angelegt. + +**Danach tatsächlich ausgeführt:** Root prüfte Quelle und Nichtwirkungsbeleg +des alten Falls, schloss dessen temporären Scope und erzeugte unter einem +frischen Scope den separaten V2-Fall `ced97b4d…`. MCP-Plan/Submit, WebUI-Grenze +und genau ein Rootlauf sind [nativ bestanden](script-console-linux-full-results.md). +Kein alter Planaufruf wurde wiederholt oder dessen Deadline geändert. diff --git a/docs/script-console-job-acceptance.md b/docs/script-console-job-acceptance.md new file mode 100644 index 0000000..d398f62 --- /dev/null +++ b/docs/script-console-job-acceptance.md @@ -0,0 +1,181 @@ +# Vorbereiteter MCP-Abnahmerunner für das Linux-Rootskript + +Stand: Grundvertrag des engen Jobrunners. Der erste tatsächliche Aufruf im +Planmodus hielt bei MCP-Initialize an; der strikt lesende Lookup belegt null +persistierte Pläne/Anträge und keinen Job. Die separate Initialize-Diagnose +und der eng korrigierte V2-Parser sind im [V2-Nachtrag](script-console-job-acceptance-v2.md) +belegt. Der ursprüngliche Reader und Fall bleiben unverändert. Kein Approval +oder Browseraufruf gehört zu diesem Runner. +Die 158 eingefrorenen Produktinputs und alle bisherigen Opsquellen bleiben +unverändert. Der neue Runner ist `deploy/script_console_job_acceptance.py`. + +Er verwendet ausschließlich die tatsächliche Cloud-Linux-Geräteidentität aus +dem unveränderten Installationsfall `2e27c312-58cc-444d-92a6-863a11ac6ffd`. +Notebook und Windows-VM sind keine Ziele. Die private Inputvorlage ist +`devstate/output/playwright-private/console-full-v1/mcp-plan-input.template.json`, +SHA256 `66f684ab9c5c31be6583fe04f090ba9d7b8724d6e6c920dd9c67c484a339245e`. +Die Skriptbytes bleiben exakt 130 Byte, SHA256 +`c878ba5b5c37fa3f1b9485c6994d6c57f6f37d01d99d4239c64d264d4ed95336`: + +```sh +set -eu +/usr/bin/id -u +/usr/bin/sleep 12 +printf '%s\n' 'RMM_FULL_CONSOLE_LINUX_V1' +printf '%s\n' 'RMM_FULL_CONSOLE_STDERR_V1' >&2 +``` + +Diese Darstellung ist keine Aufforderung zur lokalen Ausführung. Der genaue +ursprüngliche UTF-8-Inhalt einschließlich abschließender Zeile wird gehasht. +Fest gebunden sind `posix_sh`, Root, `full_maintenance_v1`, keine Argumente, +30 Sekunden Laufzeit, 4096 Byte Gesamtausgabe, ein Job mit genau einem Versuch, +`max_parallel:1` und `halt_after_failures:1`. Erwartete rohe Ausgabe ist +`0\nRMM_FULL_CONSOLE_LINUX_V1\n` auf stdout und +`RMM_FULL_CONSOLE_STDERR_V1\n` auf stderr. Der harmlose Warteabschnitt von zwölf Sekunden +ermöglicht die Beobachtung des tatsächlichen laufenden Jobs in der WebUI. + +## Voraussetzungen und Grenze + +Der verantwortliche Rootagent prüft vor der tatsächlichen Benutzung getrennt +den erhaltenden Runtime-/Serverwechsel, frischen signierten Sync und Fullhelper- +Nachweis, Browservertrauen und die konkrete neue Rootpolicy. Vorlagen oder +dieser Runner ersetzen diese Abnahmen nicht. Eine ausdrücklich angegebene +Policy-SHA256 und ein fester künftiger UTC-Startzeitraum werden im unveränderlichen +Intent gebunden; die Deadline muss innerhalb der überprüften Policygültigkeit +liegen. Der Runner öffnet oder schließt selbst keine Policy. + +Vor jedem `operations.plan` und `operations.submit` liest der Runner die +tatsächlichen aktuellen Mitgliedschaften ausschließlich auf dem Server. +Der bestehende Creator ist +`automation:11111111-1111-4111-8111-111111111111`, Art `automation`; +der Testfreigeber ist `simulated-testprincipal-a`, Art `test_human`. +Beide dürfen nicht widerrufen sein. Automation muss exakt die vorhandenen sieben +Rechte plus `root:execute` haben; `plans:approve` ist dort ausdrücklich verboten. +Der Simulator benötigt seine bestehenden Planfreigabe-, Skript- und Rootrechte. +Fehlende Rechte verweigern den Aufruf **vor** jeder MCP-Mutation. Das gesondert +reviewte Provisionieren des zusätzlichen Automation-Rootrechts gehört nicht +zu diesem Runner. Er verändert weder Rechte noch Actorart. + +Die zentrale Policy muss genau einen Scope für dieses Gerät, diese Generation, +beide Akteure, den tatsächlichen Fullpolicyhash und den qualifizierten Helpercode +enthalten. Nur Fullskripte, keine Service-, APT- oder typisierte Rebootfreigabe. +Das Profil gibt genehmigtem Rootcode volle Hostautorität; daraus wird keine +Sandbox- oder generelle No-Reboot-Zusage abgeleitet. + +Serverzugang nutzt ausschließlich den vorhandenen gepinnten SSH-Bootstrapweg. +Der Token `/opt/rust-discovery/devstate/operator-a.token` wird ausschließlich auf +dem Server für dessen eigene HTTPS-Anfrage gelesen. Er gelangt weder zum +Entwicklungsrechner noch in Logs oder Ergebnisse. Der HTTP-Endpunkt ist fest +`127.0.0.1:18443`, TLS-Name/SNI `localhost`, Vertrauen ausschließlich über +`/opt/rust-discovery/devstate/api-pki/ca.pem`. MCP-Initialize, Initialisierungs- +Notification, Sessionbindung und begrenzte JSON-/SSE-Antworten folgen dem +bestehenden Cloud-MVP-Muster; dessen veralteter privater API-CA-Pfad wird hier +gezielt nicht übernommen. + +## Bedienweg und getrennte tatsächliche Abnahme + +Die folgende Tabelle beschreibt die Modi. Mit dem separat korrigierten +V2-Runner wurde anschließend genau ein [nativer Rootlauf bestanden](script-console-linux-full-results.md). +Der ursprüngliche Fehlversuch bleibt erhalten; die Modusquelle selbst +enthält weiterhin keinen Genehmigungsweg. + +| Modus | Einzige zugelassene Wirkung | +| --- | --- | +| `prepare-local` | Neuer privater lokaler Intent samt unveränderlichem vollständigem Hashzeugen; explizite Policy-SHA und UTC-Zeiten erforderlich | +| `plan --case ` | Einmalig MCP `operations.plan`, erst nach lokalem und serverseitigem dauerhaften Startmarker | +| `submit --case ` | Einmalig MCP `operations.submit` mit festem Idempotenzschlüssel; danach lesender Pending-0/0/0/0-Nachweis | +| `pending --case ` | Bestehende Anfrage und native Nichtverbräuche lesen; niemals einreichen oder genehmigen | +| `read --case ` | Originalfälle, eindeutige Plan-/Idempotenzlookupdaten und vorhandene Resultate lesen | +| `verify-result --case ` | Vorhandenes Endergebnis, tatsächlichen UI-Audit, native Originalbytes und betroffene MCP-Mandantengrenze lesen und prüfen | + +`prepare-local` setzt nur Asset- und Zeitplatzhalter ein. An den Grund wird eine +eindeutige `acceptance_case=`-Kennung angehängt, damit ein verlorener +Planreply rein lesend eindeutig gefunden werden kann. Skript, Identität, +Interpreter, Argumente und Limits werden nicht verändert. Sämtliche Vorlagen-, +Quell- und vollständigen Intentbindungen werden vor jedem Aufruf und Readback +geprüft. Bestehende Pläne, Versuche und Intents werden nicht überschrieben. + +Die einzigen mutierenden MCP-Methoden sind Plan und Submit. Es existiert kein +Approval-API-, Browserautomations-, Cancel-, Retry- oder SQL-Schreibpfad. Auf dem +Server entstehen nur eigene private Bootstrapintents und Einmalmarker sowie die +durch MCP selbst erzeugten Pläne/Anträge. Alle direkten SQL-Abfragen laufen in +`BEGIN READ ONLY`; der Operation-Getter wird bewusst nicht aufgerufen, weil +dieser beim Lesen einen abgelaufenen Pendingantrag aktualisieren kann. + +Nach Submit müssen die gespeicherte Anfrage `pending_approval`, null Jobs, +Outboxeinträge, Grants und Autorisierungen sowie null native Verbräuche des +neuen Versuchs belegt sein. Der lokale `pending-checkpoint.json` enthält die +tatsächlichen ursprünglichen Planbytes, Plan-/Job-/Skripthashes, Job-/Versuch- und +Operation-IDs, Ziel, Rechteprofil und Grenzen für die separate Browserprüfung. +Seine zusätzliche Operationsansicht ist als aus lesendem SQL abgeleitet +gekennzeichnet. **Erst danach darf der separat berechtigte Akteur den exakten +gespeicherten Plan tatsächlich in der WebUI genehmigen.** + +Bei verlorenem Reply niemals denselben Plan-/Submitaufruf erneut starten. +Nur `read` verwenden: Originalcase und eindeutige gespeicherte Reason-/ +Idempotenzbindung werden gelesen. Ein gefundener Originalplan darf als genau +dieser Plan erhalten werden; es wird kein Ersatzplan erzeugt. Ein fehlender oder +mehrdeutiger Nachweis bleibt unklar und sperrt Wiederholung. + +## Geplanter Ergebnisnachweis und seine Grenzen + +Der Endnachweis verlangt genau einen Job, Outboxeintrag, Grant und eine +Autorisierung. Er prüft tatsächlichen `dev_test_browser_simulation`-Audit mit +Simulatorprincipal, `testsimulation:true`, Developmentprofil sowie unveränderten +Plan-/Operationbindungen. Das ist ausdrücklich kein Produktionsnachweis einer +echten menschlichen Identität. Erfolgreicher Zustand, Exit 0, ungekürzte rohe +stdout-/stderr-Bytes, Root-UID, Fullprofil und Skripthash müssen übereinstimmen. + +Die native Abfrage öffnet ausschließlich das vorhandene Root-Ledger lesend, +prüft Inode und alle ursprünglichen 18 Verbrauchszeilen bytegebunden und erlaubt +genau einen neuen abgeschlossenen Verbrauch für genau diesen Job/Versuch. +Sie materialisiert ausschließlich diese 18 bekannten IDs und den neuen Versuch +sowie die beiden Verbrauchszähler. Der Leseabschnitt wird vor BLOB-Hashing und +Dekodierung vollständig zurückgerollt/geschlossen. SQLite-Busy-Timeout ist +250 Millisekunden, das kooperative Fortschrittsbudget eine Sekunde; längere +gemessene Transaktionen werden als Nachweis verweigert. Dies ist keine harte +OS-/I/O-Echtzeitgarantie. Es läuft kein vollständiger Integritätsscan. +Originalstart, Jobbytes, Grantbytes, Signaturbytes, Autorisierung, Boot und +Ausführungsausgabe werden mit den gespeicherten Serverdaten verglichen. +Skriptbytes werden direkt aus den ursprünglichen Planbytes ausgeschnitten, +nicht aus einer API-Ansicht neu serialisiert. Die primäre Ed25519-Prüfung bleibt +beim qualifizierten Produkthelper; dieser enge Opsrunner ist kein zweiter +unabhängiger Signaturverifizierer und behauptet dies nicht. + +Zeitvergleiche verwenden einen strikten RFC3339-Parser mit ganzzahligen +Epoch-Nanosekunden: null bis neun Nachkommastellen werden exakt auf neun Stellen +aufgefüllt, Z- und numerische UTC-Offsets korrekt verrechnet. Es gibt weder +Gleitkommazahlen noch Rundung/Abschneiden. Ungültige Kalender-/Offsetwerte, +fehlende Zone, mehr als neun Nachkommastellen und die hier nicht unterstützte +Schaltsekunden-Schreibweise werden verweigert. Eingabe und Policy dürfen weiter +Mikrosekunden verwenden; die ursprünglichen Produktbytes bleiben unverändert. +Grant-/Native-Start-/Deadlinegrenzen werden bis zur letzten Nanosekunde verglichen. + +Zusätzlich muss derselbe bestehende Automation-A-Token den bereits gespeicherten +eigenen Job über den tatsächlichen MCP/HTTPS-Weg vollständig lesen können; +Ergebnis und rohe Ausgabe müssen zu SQL und nativen Originalen passen. +Mit dem ausschließlich serverseitigen bestehenden `operator-b.token` werden +danach genau zwei lesende MCP-Abfragen ausgeführt: der neue A-Job und dessen +gespeichertes A-Assetinventar. Gültige Authentisierung/Initialize und tatsächliche +strukturierte `FORBIDDEN`-/`NOT_FOUND`-Toolantworten ohne Daten/Details sind Pflicht. +Eine ungültige B-Anmeldung gilt nicht als Mandantennachweis. Dafür entstehen +weder neue Jobs noch weitere Scopes. Der frühere Datenbank-RLS-Beleg ersetzt +diesen ausdrücklich geplanten HTTP-/MCP-Authnachweis nicht. + +Tatsächlich lokal geprüft: Python-Syntax und Help; sämtliche eingefrorenen +Quell-/Templatebindungen; exakte Jobbyteausschnitte aus allen drei tatsächlichen +Rust-Goldenplänen; synthetischer zulässiger Plan und Verweigerung geänderter +Skriptbytes, Limits, Creatoridentität und mehrerer Versuche. Kein Test wurde als +nativer Job- oder Browsernachweis ausgegeben. Der danach tatsächlich vorbereitete +ursprüngliche Fall hielt während MCP-Initialize vor dem Plan-Toolcall an; +sein Nichtwirkungsbeleg und die separate V2-Korrektur stehen im V2-Nachtrag. + +Der ursprüngliche unbenutzte Runner `c958db665…` bleibt create-only archiviert. +Unabhängiger Review fand dort den zu langen Leseabschnitt über einen vollständigen +SQLite-Integritätsscan und Python-3.10-inkompatible beziehungsweise für Rust- +Nanosekunden ungeeignete `fromisoformat`-Vergleiche. Beide wurden vor jeder +Jobvorbereitung eng korrigiert. Tatsächliche gezielte lokale Prüfungen bestanden: +Z, null bis neun Nachkommastellen, positive/negative Offsets, Zeitpunkt eine +Nanosekunde vor Epoch und die echte Grantgrenzbedingung mit Ein-Nanosekunden- +Abständen. Ein separater temporärer SQLite-Zeuge konnte einen unabhängigen +Schreiber vor jedem Rowhash erwerben; kein Produkt-/VM-Ledger wurde dafür benutzt. diff --git a/docs/script-console-linux-full-results.md b/docs/script-console-linux-full-results.md new file mode 100644 index 0000000..064430f --- /dev/null +++ b/docs/script-console-linux-full-results.md @@ -0,0 +1,89 @@ +# Tatsächliche Linux-Abnahme der vollständigen Skriptkonsole + +2026-11-14, ausschließlich Cloud-Linux gegen `discovery.ease-technologies.de`. +Fall `ced97b4d-c352-4f0c-b34f-59e4d69ca192`, Runner-V2 +`c7f42df0a7e89274b7e255ac728ec4198a02278926348113c5b7e363e8983757`. +**Die einzelne vollständige MCP → WebUI → Rootskript → Ergebnisstrecke ist +nativ bestanden.** Dies schließt weder Windows noch den Selbstupdater ab. + +Voraussetzung war der tatsächlich bestandene dreiteilige passive +Linux-Nachbeleg `4c425416…`, SHA256 +`594fdf94261aa882df7ea771dda3f04779533dfdac768cf4a63c83448e1f27d7`. +Die zuvor geöffnete Policy `71660e4c…` wurde ohne neuen Job geschlossen. +Ein neuer, unveränderlicher 15-Minuten-Scope `992a6639…` band ausschließlich +das tatsächlich qualifizierte Cloudgerät, beide berechtigten Akteure, +Fullpolicy `8101afd1…` und Helpercode `79442727…`. Er erteilte keine +Plangenehmigung. SYSTEM, Dienst-, APT- und typisierte Rebootfreigaben wurden +nicht hinzugefügt. Volle Rootautorität des genehmigten Skripts bleibt ausdrücklich +ohne Sandbox- oder generelle No-Reboot-Zusage. + +## Tatsächlich durchlaufene Grenze + +1. Der bestehende Automation-Akteur legte den festen Plan einmal über MCP an + und reichte ihn einmal ein. Plan `259ff8f7-9b91-4707-8c3c-678573576ce3`, + Planhash `070ed2a5aa98acb461719492590b85574e0450739d7ea7a715d2dc3d5ec4966f`. + Antrag `25af50d7-b1fe-46f7-991f-23a4f186b46f` war dauerhaft + `pending_approval`: null Jobs, Outboxeinträge, Grants, Autorisierungen + und native Verbräuche dieses Versuchs. +2. In der echten WebUI wurde genau dieser Antrag geöffnet und dessen + unveränderlicher Plan angezeigt. DOM-Auslesung der vollständigen + Zielpreviews und lokaler Vergleich bestätigten sämtliche originalen + Planbytes, Ziel-/Job-/Versuchs-IDs, Skript- und Jobhashes, Root/ + `full_maintenance_v1`, Argumente, Zeitfenster und Limits. Vor dem + angehakten Prüfkästchen war der Freigabebutton deaktiviert. +3. Ein tatsächlicher Klick auf **„SIMULIERT: Diesen festen Plan ausdrücklich + genehmigen“** erzeugte die Freigabe. Gespeicherte Autorisierung und Audit + binden `simulated-testprincipal-a`, `dev_test_browser_simulation`, + `testsimulation:true`, Entwicklungsprofil und denselben Plan/Antrag. + Dies ist kein Freigabenachweis eines menschlichen Produktionsbenutzers. +4. Genau Job `b014fb7a-c23e-440f-8bb6-8212c3af2b1f`, Versuch + `cc56ea51-a548-4bde-93a5-ef49a31d110d`, wurde erfolgreich abgeschlossen. + Je genau ein Job, Outboxeintrag, Grant und Autorisierungsdatensatz sind + belegt. Der Rootledger enthält die unveränderten ursprünglichen18 Zeilen + plus genau einen abgeschlossenen Verbrauch:19 verbraucht,0 ungeklärt. + +Das [feste harmlose Skript](script-console-job-acceptance.md) umfasst130 +UTF-8-Bytes, SHA256 `c878ba5b5c37fa3f1b9485c6994d6c57f6f37d01d99d4239c64d264d4ed95336`. +Es liest die UID, wartet zwölf Sekunden und schreibt feste Markierungen. +Belegt: UID0, Exit0, erwartetes Fullprofil, bestandene stdout-Nachbedingung, +keine Trunkierung und kein angeforderter Reboot; Limits30s/4096Byte. + +| Unveränderte Ausgabe | Bytes | SHA256 des tatsächlichen WebUI-Downloads | +| --- | ---: | --- | +| stdout: `0\nRMM_FULL_CONSOLE_LINUX_V1\n` | 28 | `056c51deff395a12541cc977f62ad20dad80ccf2dff9f81c2ad8a2bdf09c3c7a` | +| stderr: `RMM_FULL_CONSOLE_STDERR_V1\n` | 27 | `4b810603627a8d0836bc820087c6d566a503a6c1ca5bd06fff7e6f8910fa3311` | + +Die Ergebnisse wurden tatsächlich in der WebUI geöffnet; beide Rohbytebuttons +wurden angeklickt und die heruntergeladenen Dateien bytegenau verglichen. +Der tatsächliche MCP-Getter liefert dasselbe vollständige Ergebnis. +Zwei authentisierte MCP-Abfragen mit dem bestehenden anderen Mandanten +(`jobs.get`, `inventory.get_device`) ergaben jeweils `NOT_FOUND`, leere +Details, HTTP200 mit MCP-Fehler. Dafür wurde kein weiterer Job erzeugt. + +## Abschluss und Grenzen + +Der Scope wurde nach Erfolg geschlossen. Ein separater lesender Readback +bestätigt wieder die originalen abgelaufenen Policybytes +`cf15cbb68b590475df6def28e7dcffb7d88e0ca2d2555a06174fb654eeff05f4`, +Eigentümer10001:10001, Modus0600 und ursprüngliche mtime. Die atomare +Wiederherstellung hat einen neuen Inode; der historische Inode bleibt im +Originalbeleg erhalten. Keine dauerhafte zentrale Fullfreigabe bleibt offen. + +Privater Endbeleg `passed.json`, SHA256 +`89dcb583fb62d8af6cb87e9a386e973451016be6bd7077ac1a8e5f8db3fa2128`. +Browserbelege liegen unter `output/playwright/51c274c9…/evidence/`; +Rohdaten, Zugangsdaten und Browserzustand bleiben gitignoriert. + +Im Browser tatsächlich beobachtet wurden `pending_approval`, `queued` und +`succeeded`; der laufende Zwischenzustand wurde trotz des Warteabschnitts +verpasst. Kein zweiter Lauf wurde dafür erzeugt. Der Nachbeleg bindet die +Originalgrant-/Signatur-/Request-/Outcomebytes; er enthält keine zweite +unabhängige Ed25519-Verifikation und keinen separat erfassten Kindprozess- +Startzeitpunkt. Die feste Frist ist anhand der nativen Startreservierung +belegt. Timeout, Abbruch, Offlinebetrieb und eine fremde Linux-Prozessgruppe +sind durch diesen erfolgreichen Einzelfall nicht zusätzlich nativ abgenommen. +Der ältere SSE-Fehlversuch bleibt [fehlgeschlagen und unverändert](script-console-job-acceptance-v2.md). + +Weiter offen: Windowsclock und native PowerShell-/SYSTEM-Abnahme, +Selbstupdater-Runtime und deren Linux-/Windows-Applynachweise sowie +abschließende Integration und konsistente aktuelle Runtimeindizes. diff --git a/docs/script-console-policy-scope.md b/docs/script-console-policy-scope.md new file mode 100644 index 0000000..77fb9e4 --- /dev/null +++ b/docs/script-console-policy-scope.md @@ -0,0 +1,256 @@ +# Begrenzter Policy- und MCP-Root-Bootstrap für die Linux-Skriptkonsole + +`deploy/script_console_policy_scope.py` ist eine getrennte Opsquelle. Sie verändert +keine158Frozen-Produktdateien, Migrationen, Agenten, Units, Serverflags, Images, +Tokens oder Sitzungen. **Aktueller korrigierter Quellstand lokal geprüft, noch +nicht auf dem Server ausgeführt; früherer Vorwirkungsabbruch unten dokumentiert.** +Kein tatsächlicher Scope oder Actor wurde durch den Subagenten +geändert; Native-Attestation und Zielbelege werden nicht erfunden. + +## Konkrete Autorisierungsentscheidung + +Der User hat AI/MCP-Skriptplanung mit ausdrücklich erlaubten Rootrechten und +separater menschlicher WebUI-Genehmigung beauftragt. Der Hauptagent stellte +öffentlich und rein lesend fest: Der bestehende Actor +`automation:11111111-1111-4111-8111-111111111111` besitzt sieben Standardrechte, +jedoch kein `root:execute`. Der simulierte UI-Actor besitzt Root- und +Genehmigungsrechte. `jobs::plan` verlangt Rootberechtigung und passenden Rootscope +bereits vom Planersteller; spätere Prüfungen beziehen sowohl ursprünglichen +Planersteller als auch UI-Genehmiger ein. Ein ausschließlich auf den simulierten +Principal begrenzter Scope kann deshalb den beauftragten MCP-Rootablauf nicht +tragen. + +Der Hauptagent hat auf Grundlage dieses Userauftrags den separaten, konkreten +Bootstrap zur Ergänzung ausschließlich `root:execute` für genau diesen bestehenden +Dev/Test-Automation-Actor angeordnet. Dies ist MVP-Konfiguration und keine +Jobgenehmigung. Kein allgemeines Rollenupdate und kein Defaultwechsel anderer +Tenants wird implementiert. Der Token bleibt derselbe; `plans:approve`, +`plans:revoke`, `system:execute`, Sessions und Tokens werden nicht hinzugefügt. +Die interaktive Genehmigungsgrenze bleibt auch bei vorhandener Root-Planungs- +berechtigung bestehen. + +## Zwei bewusst getrennte Vorgänge + +1. `prepare-mcp-root-local` und `provision-mcp-root`: einmalige Ergänzung genau + eines Rechts, ausschließlich bei geschlossenem bekannten Rootpolicybestand. + Kein automatisches Öffnen eines Scopes. +2. `prepare-local`, `open`, `read`, `close`: separat geprüftes15-Minuten-Fenster + für genau ein tatsächlich qualifiziertes Cloud-Linuxziel, beide bestehenden + Actor-IDs und exakt den Fullpolicy-/Helpercodepin. `open` verändert keine + Memberships. `close` restauriert ausschließlich die eigenen gesicherten alten + Policybytes. + +Jeder Vorgang hat eigenen unveränderlichen lokalen/entfernten Intent, UUID, +Source-/Supporthashes, lokalen Aufrufmarker vor SSH und entfernte Einmalmarker +vor Wirkung. Antwortverlust oder Fehler nach einem Aufruf erlaubt ausschließlich +`read` desselben Cases, keine Wiederholung, keine neue Frist und keinen neuen +Case als automatische Reparatur. + +## Separates MCP-Recht: genaue Transaktion + +Die feste Vorbedingung ist exakt der vorhandene Actor mit Kind `automation`, +`revoked_at:null` und dieser unveränderten geordneten Rechtefolge: + +```text +inventory:read, logs:read, operations:read, operations:plan, +operations:submit, jobs:cancel, scripts:execute +``` + +Der Bootstrap verlangt das explizit von Root überprüfte tatsächliche Runtimeimage +auf beiden Rollen, Development-/Devlogin-Konfiguration, unveränderte Rootflags +und das bekannte Read-only-Policymount. `/auth/config` wird privat über korrekt +verifiziertes API-TLS mit `api-pki/ca.pem` und Name `localhost` gelesen. Die +Rootpolicy muss exakt Hash +`cf15cbb68b590475df6def28e7dcffb7d88e0ca2d2555a06174fb654eeff05f4`, +Schema1/Development und bereits abgelaufen sein. Der Tenant hat genau30Jobs, +30Outboxzeilen,26Permits und keinen offenen/unklaren Job. + +Ein neues rootprivates Historyverzeichnis erhält Intent und vollständigen +öffentlichen Vorherbeleg. Der entfernte `provision-entered`-Marker wird vor SQL +fsync-gesichert. Danach läuft genau eine feste Transaktion: + +- `BEGIN ISOLATION LEVEL SERIALIZABLE`, expliziter Tenantkontext. +- `UPDATE memberships SET permissions=array_append(permissions,'root:execute')` + ausschließlich mit festem Tenant/Actor, Kind, Revocation-NULL und exaktem + Array-CAS sowie erneutem30/30/26-/Closed-Prädikat im Statement. +- `RETURNING` liefert nur öffentliche Actorfelder. Ein Count0 führt über den + tatsächlichen Countassert zu einem Transaktionsfehler vor `COMMIT`; + `ON_ERROR_STOP` beendet psql, die Verbindung rollt zurück. Genau eine Zeile ist + aufgrund des vorhandenen Tenant-/Principal-Primärschlüssels möglich. +- Nach Commit müssen ausschließlich die fest erwarteten acht Actorrechte + vorliegen. Alle anderen Membershipzeilen werden über einen konsistenten + SHA256-Fingerprint verglichen; Job-/Outbox-/Permitzähler und geschlossene + Policybytes/Inode sowie Rollen/Flags bleiben erhalten. + +SQL-/Vorher-/Nachher-/Ergebnisbelege sind privat und create-once. Eine verlorene +Commitantwort wird niemals als fehlender Commit interpretiert; `read` zeigt +tatsächlichen Actorzustand und erhaltene Marker. Es gibt keinen Schreibpfad zu +Authorizations, Jobs, Outbox, Grants oder Auditgenehmigungen. + +## Temporärer Fullscope und seine Belege + +`prepare-local` verlangt den Pfad zu einer tatsächlichen `attested.json` und den +vom Hauptagenten ausdrücklich akzeptierten vollständigen SHA256. Der Loader +verlangt die bestehende Post-Attestation-Form mit drei Originalsamples samt +Hashbindung, bestätigter Erhaltung, normalem signiertem Sync, zwei gespeicherten +Heartbeatfortschritten, Queuekonvergenz und Tenant-RLS-Verweigerung. Tatsächliche +Rootprozesse, Fullpin und Codehash in den Samples müssen zusammenpassen. Ohne +diesen fertigen akzeptierten Beleg entsteht kein lokaler Scopeintent. + +Die Policy hat genau einen Schema2-Scope: konkreter Tenant/Asset/Agent und +Generationen aus dem nativen Fullpin, die zwei festen Principal-IDs, tatsächlicher +Fullpolicyhash und laufender Helpercodehash, `allow_root_scripts:true`, +`allowed_script_profiles:[full_maintenance_v1]`, leere Servicesliste, +`allow_reboot:false`, keine APT-/Rootlogfelder. Die Frist wird bei lokaler +Vorbereitung unveränderlich auf900Sekunden festgelegt und beim Öffnen nochmals +gegen die Serverzeit geprüft. Vergangene, künftig beginnende oder längere +Freigaben sowie Scope0 werden verweigert. + +`open` verlangt zusätzlich frisch gespeicherte authentifizierte Online-/Sync- +Zustände, den gleichen Boot und passende Generationen, beide Fullcap-Pins, +Migrationen1–14 und keine ungeschlossenen Jobs. Beide tatsächlichen Memberships +werden erneut geprüft. Automation muss exakt die separat bereitgestellten acht +Rechte haben; der simulierte Genehmiger braucht seine bestehenden Plan-, Skript-, +Root- und UI-Genehmigungsrechte. Es werden keine Rechte erzeugt, wenn sie fehlen. + +Vor dem Öffnen wird der alte geschützte Policybestand erneut auf genauen +`cf15…`-Hash und abgelaufenes Schema1 verglichen. Rootprivate Originalbytes und +vollständige Dateimetadaten werden erhalten, anschließend der Einmalmarker +geschrieben. Die unveränderlichen Kandidatbytes werden mit Eigentümer10001 und +Modus0600 auf demselben Dateisystem vorbereitet; unmittelbar vor einer einzigen +atomaren Ersetzung werden alte Bytes und Inode nochmals verglichen. Danach +werden tatsächliche neue Bytes und unveränderte Rollen/Flags belegt. Keine +Dienstaktion und keine Datenbankschreiboperation ist Teil von `open`. + +`close` benötigt keinen noch online erreichbaren Client und keine frischen Caps: +eine Schließung darf auch nach Ablauf oder bei Offlinezustand erfolgen. Es +akzeptiert nur den eigenen unveränderten Remoteintent, den erhaltenen `open`- +Marker, exakte eigene aktuelle Kandidatbytes und den eigenen originalen +`cf15…`-Backupbestand. Fremde oder nachträglich geänderte Policies werden niemals +pauschal ersetzt. Der Close-Marker wird vor Wirkung gesichert; ein weiterer +Bytes-/Inode-CAS erfolgt vor der atomaren Restaurierung. Alte Policybytes, +Eigentümer, Gruppe, Modus und mtime werden wiederhergestellt und ihre abgelaufene +Grenze geprüft. Die atomare Ersatzdatei hat nachvollziehbar einen neuen Inode; +historische Inode-/ctimewerte bleiben im unveränderten Beleg, nicht als behauptete +Wiederherstellung. Alle Historydateien und Runtime-Tags bleiben erhalten. + +`read` erstellt weder Casepfad noch Lock und führt keine Reparatur aus. Auch ein +fehlender Case oder Policy-Lesefehler erlaubt Readback anderer erhaltener Felder. +Ein gleichnamiger fremder Intent wird verweigert. Die Worker dieser Quelle +serialisieren Policy-/Membership-Änderungen über einen geschützten Lock; parallele +ältere Root-Policyschreiber müssen durch den Hauptagenten ausgeschlossen werden. +Der Lock behauptet keine Kontrolle über andere Rootprogramme. + +## Geplante Bedienung, nicht ausgeführt + +Erst nach Rootreview der eingefrorenen Quelle und des konkreten Intents: + +```text +python3 deploy/script_console_policy_scope.py prepare-mcp-root-local --expected-server-image sha256:ROOT_REVIEWED_ACTUAL_IMAGE +python3 deploy/script_console_policy_scope.py provision-mcp-root --id ACTOR_CASE --execute-mcp-root-provision +python3 deploy/script_console_policy_scope.py read --id ACTOR_CASE + +python3 deploy/script_console_policy_scope.py prepare-local --attestation /path/to/actual/attested.json --accepted-attestation-sha ROOT_ACCEPTED_SHA256 --expected-server-image sha256:ROOT_REVIEWED_ACTUAL_IMAGE +python3 deploy/script_console_policy_scope.py open --id SCOPE_CASE --execute-policy-open +python3 deploy/script_console_policy_scope.py read --id SCOPE_CASE +# Danach separat: MCP-Antrag → pending0 → tatsächlicher UI-SIM-Klick → Ergebnis. +python3 deploy/script_console_policy_scope.py close --id SCOPE_CASE --execute-policy-close +python3 deploy/script_console_policy_scope.py read --id SCOPE_CASE +``` + +Platzhalter sind absichtlich nicht ausführbar. Der bestehende Hauptcheckout mit +geschützter `credentials/server.conf` ist der Standard-SSH-Zugangsroot; es gibt +keinen Linux-VM-Aufrufpfad. Ausschließlich der bereits unabhängig vertraute +Cloudserver88.99.80.202 bzw. dessen fester Name ist zulässig. SSH ist +Bootstraptransport, keine Job- oder Approvalautorität. + +## Semantikgrenzen + +Ein Scope begrenzt/verweigert, genehmigt aber niemals einen Job. Die zentrale +Profilzulassung bleibt additiv und wird nicht als allgemeiner Ausschluss aller +Legacy-Rootskripte dargestellt; dies ist nach dem MVP vorgemerkt. General/APT +bleiben lokal unverändert gebunden/gesperrt. Leere typisierte Service-/Reboot-/ +APT-Rechte sind keine Sandbox eines ausdrücklich genehmigten Fullskripts. Der +konkrete später genehmigte Plan bindet vollständige Skriptbytes, Fullprofil, +Identität, Ziel/Versuch, Budgets und Fristen. Root-/Helperpins, Uhr, Peer, +Consumption, Startgrant und WebUI-Genehmigung werden weiterhin unabhängig geprüft. + +## Tatsächliche lokale Prüfungen + +- Python-Syntax und beide getrennten Intentformen geprüft, nur temporäre + synthetische Attestationsfixtures. Unbekannter akzeptierter Hash und geänderte + Originalsamplebytes werden verweigert. Kein tatsächlicher Nativebeleg wurde + dadurch erzeugt oder als akzeptiert markiert. +- Acht Kandidatenabweichungen, vergangene/künftige/zu lange Öffnungsfenster, + fremde Close-Policy, Wiederholungsmarker und rein lesendes Verhalten bei + fehlendem Case geprüft. Frozen158 und Deployment-v2 bleiben byteidentisch. +- Die **tatsächliche aus der Quelle extrahierte** SQL-Transaktion wurde auf einer + frischen isolierten PostgreSQL-Fixture mit `--network none`, ohne Ports oder + reale Zugangsdaten ausgeführt: exakt ein7→8-Rechte-Commit erfolgreich; zweiter + Aufruf, abweichende Ausgangsrechte und offener Job abortieren vor Commit ohne + Überschreiben. Anderer Tenantactor unverändert. Die Fixture bildet nur das + benötigte Schemasubset ab und ist kein RLS-/Produktionsnachweis; der eigene + Testcontainer samt Testvolume wurde anschließend entfernt. + +Belege stehen unter `devstate/script-console-policy-scope-local-review/`. +Reale Native-Attestation, Serverrollen, Membership-CAS, Policyöffnung/-schließung, +UI-Genehmigung und Jobwirkung sind erst durch den Hauptagenten auszuführen und +getrennt nachzuweisen. Die lokalen Tests geben keine Serverfreigabe vorweg. + +### Tatsächlicher erster Provisionierungsversuch und Parserkorrektur + +Der Hauptagent führte den ersten expliziten Provisionierungsversuch +`6ba73b0b-b42f-42d6-966a-62d523315ee8` mit Source +`cb7f0984a0f98065018e8f190bedba1454bf53347c3280751e4d0ccf39a21624` +aus. Er brach vor entferntem Case und vor SQL ab. Der unabhängige Rootreadback +meldete Casefehlend, Actor weiterhin exakt sieben Rechte, Policy unverändert +`cf15…` mit Inode649002 und Zähler30/30/26/0; Rollenprobe meldete ValueError. +Allenfalls der eigene Serialisierungslock wurde angelegt. Keine Scopeöffnung, +Membership-, Policy-, Job- oder Kernelwirkung wird daraus behauptet. + +Die Ursache wurde anschließend lokal am unveränderten alten Parser reproduziert: +Docker fügt nach dem ausgewählten Go-Template eine zusätzliche Leerzeile hinzu. +Dies wurde tatsächlich mit demselben Template gegen ein öffentliches lokales +Toolimage ohne ausgewählte RMM-Werte beobachtet; keine Zielumgebung oder Secrets +wurden dafür gelesen. Der bisherige dict-Generator verweigerte diese Zeile mit +ValueError. + +Die enge Korrektur ignoriert ausschließlich leere Zeilen. Jede nichtleere Zeile +muss weiterhin ein Gleichheitszeichen, einen ausdrücklich erlaubten Schlüssel +und einen noch nicht vorhandenen Schlüssel enthalten; unbekannte, doppelte, +fehlformatierte und bloß aus Leerzeichen bestehende Zeilen werden verweigert. +Werte werden nicht getrimmt oder normalisiert. Alle Flags, Valuechecks, +Zählergates, Rechte-CAS und Mutationspfade bleiben unverändert. + +Vor der Änderung wurden alte Sourcebytes und sämtliche fünf vorhandenen lokalen +Case-Dateien create-only unter +`devstate/script-console-policy-scope-source-history/` erhalten. Der alte Case +wird nicht wiederholt oder mit dem neuen Sourcehash umgedeutet. Ein neuer Case +ist erst nach Rootreview zulässig. Bei Bedarf kann Root den unveränderten alten +Reader anhand des archivierten Hashes unter deploy rekonstruieren; ein +Readbackfehler wird nicht als fehlende oder erfolgreiche Mutation ausgelegt. + + +## Tatsächlich provisioniertes MCP-Planungsrecht + +Nach Rootreview der Quelle11b4450a wurde der separate neue Fall +`0daffbc0-64df-486c-8df1-8ae5e0a0215c` genau einmal ausgeführt und zusätzlich +lesend geprüft: der bestehende Automation-Testprincipal von DevelopmentA hat +nun genau seine bisherigen sieben Rechte plus `root:execute`. Andere +Mitgliedschaften blieben bytegleich; der Ausdruck erlaubt keine Genehmigung. +Keine Approval-/SYSTEM-/Token-/Sessionrechte wurden ergänzt, kein Job oder +Startgrant erzeugt. Zentrale Rootpolicy bleibt exaktcf15 und abgelaufen, +gleicher Inode; alle drei Zähler30/30/26 und ungeklärt0 blieben erhalten. + +Der frühere Fall6ba73b0b bleibt ein unveränderter Vorwirkungsfehler: tatsächliche +Docker-Ausgabe enthält nach den sieben ausgewählten öffentlichen Envzeilen eine +Leerzeile. Der alte dict-Parser verweigerte davor mitValueError; Remote-Case +fehlte, Actor hatte nachweislich weiterhin sieben Rechte. Die neue Quelle +überspringt ausschließlich leere Zeilen und verweigert unzulässige, doppelte oder +unbekannte nichtleere Felder. Kein Wiederholen des alten Mutationsfalls. + +Ein temporärer Full-Ausführungsscope wurde damit noch nicht geöffnet. Der erste +Root-MCP-Auftrag benötigt weiterhin den akzeptierten aktuellen Nativebeleg, +separate begrenzte Policyöffnung und die tatsächliche interaktive WebUI-Freigabe +seines unveränderlichen Plans. Der gesonderte Provisionierungsschritt folgt dem +expliziten Userauftrag für AI/MCP-Rootwartung und ist keine Jobgenehmigung. diff --git a/docs/script-console-post-attestation-v2.md b/docs/script-console-post-attestation-v2.md new file mode 100644 index 0000000..92073f6 --- /dev/null +++ b/docs/script-console-post-attestation-v2.md @@ -0,0 +1,97 @@ +# Enger lesender Observer für kurzlebige DELETE-Rollbackjournale + +Stand: lokale Opsvorbereitung, kein nativer V2-Aufruf und noch kein neuer +Abnahmeintent. `deploy/script_console_post_attestation_v2.py` ist eine getrennte +Quelle; die ursprüngliche Datei `script_console_post_attestation.py` bleibt +bytegleich SHA256 +`076e1c22c1849c20827317e3cbec7a3769f14f5fd9fc7691ab2e0f0cb80fa1dc`. +Alle ursprünglichen Belege des Falls +`3319e212-2188-4b00-bb82-e13173a3b6b2` bleiben unverändert. Zusätzlich wurden +Quelle und sämtliche 34 zu diesem Zeitpunkt vorhandenen Fallbelege create-only +unter `devstate/source-history/postattn-076-ff0606b7-4740-4dab-9a26-d7679e8d630d/` +archiviert; Manifest-SHA256 +`46d922abd242c56887752ad193343e717292eb4040013c8ca1c69fa4b9f60280`. + +Ein konkreter angereicherter Read-only-Fehlerbeleg identifiziert den verweigerten +Guard `no SQLite sidecars` im Pfad `vm → original_rows → database`: +`transport-88f45793-8bb2-4c31-9804-1efb740e8071.json`, SHA256 +`b7db1bfce80d6cb8ed3eea678c0f102b2f6b7d45e7e7cd97f95bb10343a906f5`. +Das belegt diese eine Sidecarverweigerung. Es belegt weder eine Runtime-/ +Historybeschädigung noch die Ursache sämtlicher früherer RuntimeErrors ohne +Guardnamen. Zwei zusätzliche ausdrücklich autorisierte lesende Diagnosen mit +unveränderter 076-VMfunktion bestanden, auch der vorhandene Server→Inventar→VM- +Pfad; sie wurden nicht zu Abnahmesamples oder einem finalen Attest hochgestuft. + +V2 verändert ausschließlich die Beobachtung der drei exakt bekannten +SQLite-DELETE-Datenbanken: Agentjournal, Agentverbrauchsledger und gemeinsames +Rootverbrauchsledger. Für genau deren drei `-journal`-Pfade beobachtet der +Observer höchstens 250 ms, ob ein reguläres, passend geschütztes Rollbackjournal +natürlich verschwindet. Während es vorhanden ist, öffnet dieser Wartepfad keine +Datenbank, führt keine SQL-Abfrage aus und verändert keine Datei. Der Observer +löscht, benennt, repariert oder recovered nichts. Ein länger vorhandenes Journal, +falscher Objekttyp oder fremder Eigentümer verweigert den Nachweis. WAL und SHM +werden weiterhin sofort verweigert, ebenso jeder unbekannte Suffixpfad. + +Im Zustandsverzeichnis dürfen ausschließlich diese drei transienten +Rollbackjournalpfade nach konkret beobachteter Abwesenheit aus dem Dateipfadzeugen +ausgelassen werden. Es gibt keine allgemeine Suffixausnahme. Alle ursprünglichen +PKI-/Zustandspfade, Metadaten, Inodes und Raw-Rowhashes bleiben unverändert +gefordert. Vor jedem DB-Open bleibt der ursprüngliche Sidecarguard aktiv; direkt +nach dem Read-only-Open wird vor jeder SQL-Anweisung nochmals geprüft. Eine +erneute Sidecarentstehung verweigert die Beobachtung, statt sie zu ignorieren. +Eine atomare Dateisystem-/Datenbankaufnahme wird nicht behauptet. + +`mode=ro`, `query_only`, tatsächlicher DELETE-Journalmodus, 250-ms-Busy-Timeout, +kooperatives einsekündiges Lesebudget und das Schließen jeder Leseperiode vor +Rowhashing/Signaturprüfung bleiben bestehen. Signed-Decision-, Server-, +Schema-/Rowhash- und abschließende Agent-/Helper-Metaguards werden nicht gelockert. +Der Sourcevergleich bestätigt unveränderte ASTs der bestehenden Funktionen +`signed`, `original_rows`, `row_hash`, `server`, `compare_samples` und +`parse_stored_time`. Es gibt weder `immutable=1`, unbegrenztes Busy-Warten, +Helper-IPC, Diensteingriff, Job, Approval, Rechte- noch Policyänderung. + +Auf Fehlern liefert der neue Formatter ausschließlich Fehlerklasse, einen im +Quelltext enthaltenen statischen RuntimeError-Guardnamen, Funktions-/Zeilenframes +und SQLite-Fehlercodes. Er exportiert weder Exceptionlocals noch SQL-stdout, +Token- oder Schlüsselwerte. Normale fehlgeschlagene Rohtransportbelege bleiben +erhalten. Ein solcher Fehler wird niemals als erfolgreicher Sample behandelt. + +Tatsächlich lokal geprüft, ausschließlich mit temporären Testdatenbanken und +den unveränderten echten V2-Helper-/Dateipfadwalk-Funktionen: Ein SQLitewriter +hielt sein Rollbackjournal kurz offen und schloss es selbst durch Commit; nach +rund 42 ms beobachtete der Observer das natürliche Verschwinden, danach war der +Read-only-Read zulässig. Ein persistierendes Journal wurde nach rund 250 ms +verweigert und blieb erhalten. WAL, SHM und ein unbekannter Journal-Suffixpfad +wurden verweigert und blieben erhalten. Der Observer führte keine Mutation aus. +Python-Syntax bestand; Produktinputs und alle archivierten Originalbelege blieben +unverändert. Das ist kein neuer nativer Drei-Sample-Nachweis. + +**Geplant, nicht ausgeführt:** Erst nach gesondertem Rootreview einen neuen +lokalen Intent mit dem V2-Quellhash erzeugen und drei neue tatsächliche +Beobachtungen samt abschließendem Vergleich durchführen. Die beiden ursprünglichen +076-Samples werden nicht umetikettiert oder mit einem V2-Sample zu einem neuen +Attest zusammengemischt. Die tatsächliche WebUI-Jobgenehmigung bleibt ein +getrennter späterer Schritt. + + +## Tatsächliche dreiteilige Cloud-Abnahme + +Root hat nach Quellreview einen neuen ausschließlich lesenden Fall +`4c425416-5a04-426a-a23a-f5801968f02d` vorbereitet und drei getrennte Beobachtungen +auf der tatsächlichen Cloud-Linux-VM und dem festgelegten Server ausgeführt. +Anschließender lokaler Vergleich: **bestanden**. Finaler geschützter Beleg +`attested.json`, SHA256 +`594fdf94261aa882df7ea771dda3f04779533dfdac768cf4a63c83448e1f27d7`. + +Erhaltung der ursprünglichen Geräte-/Boot-/PKI-/DB-Inodes/Root18-Rohbytes, +tatsächliche neue Agent-/General-/Fullimages und Konten sowie aktueller gemeinsam +angewandter kryptografisch geprüfter signierter Sync wurden nachgewiesen. +Zwei gespeicherte Kontakt-/Sequenzfortschritte, konkrete neue Inventar-ID mit +quellgebundenem Queueabschluss und RLS-Tenantverweigerung bestanden. Unveränderte +30Jobs/30Outbox/26Permits, ungeklärt0; keine neuen Jobs oder Freigaben. + +Die Aussage bleibt seitenweise Erhaltung über das Beobachtungsintervall mit +abschließendem SameDecision-/Effektzahlcheck, kein atomarer Gesamtsnapshot oder +neuer Vollintegritätsscan. Kein mitgeschnittener Wire-ACK und kein neuer +HTTP-Tenantnachweis; diese Grenzen sind im Ergebnis explizitfalse. Die Full- +Skriptwirkung bleibt separat WebUI-genehmigungspflichtig und noch offen. diff --git a/docs/script-console-post-attestation.md b/docs/script-console-post-attestation.md new file mode 100644 index 0000000..54d27f7 --- /dev/null +++ b/docs/script-console-post-attestation.md @@ -0,0 +1,201 @@ +# Lesender Nachweis nach dem ersten Skriptkonsolen-Dienststart + +Stand2026-11-14: neue reviewbare Quelle +`deploy/script_console_post_attestation.py`; durch ihren Ersteller **kein +Zielaufruf ausgeführt**. Keine Änderung der158 eingefrorenen Produktinputs. +Installationsfall `2e27c312-58cc-444d-92a6-863a11ac6ffd` und dessen ursprünglicher +Fehlerbericht bleiben unverändert. Die separate eng geprüfte Fortsetzung startet +die bereits ausgetauschten Dienste; dieser Nachweis darf das nicht übernehmen. + +## Feste Proben und Erhaltungsgrenzen + +Der VM-Reader öffnet ausschließlich vorhandene feste Kontroll-/Metadatendateien +und die drei ursprünglichen SQLite-Dateien im URI-Modus `mode=ro` mit +`query_only` und Lesetransaktion. Er erzeugt keine Zielpfade, keine temporären +Dateien und keine SQLite-Sidecars. Vorgefundene WAL/SHM/Journaldateien verweigern +die Probe; keine Bereinigung. systemctl wird ausschließlich mit `show` benutzt. +Die zusätzliche OpenSSL-Prüfung verwendet öffentliche Signaturdaten in anonymen +memfd-Dateideskriptoren, keine privaten Schlüssel. Kein Helper-IPC und keine +neue Clock-/Inventory-/Jobanforderung. + +Geprüft werden alle drei tatsächlichen Agent-/General-/Full-Prozessimages, +UID/GID, Startticks, unveränderte Boot-ID, installierte Codehashes und erhaltene +alte Binärinodes/-bytes. Alle General-/APT-/Paketkontrollen des ursprünglichen +Preflightzeugen bleiben exakt; APT und alte Dienste bleiben disabled/inactive. +Fullkonfiguration, ursprüngliche Inspector-Pinbytes, Unit und additives +Agentdropin müssen den geprüften ursprünglichen Bytes/Modes entsprechen. + +Alle ursprünglichen Zustandsobjekte bleiben vorhanden; private Schlüssel werden +nicht geöffnet. Nicht-DB-Dateien behalten vollständige Inode-/Besitz-/Mode-/ +Größen-/Zeitmetadaten. Für Datenbanken dürfen Inhalt und Größe fortschreiten; +ihre Inodes/Besitzer/Modes bleiben. Bei Zustandsverzeichnissen dürfen Größe und +Änderungszeit wegen normaler DELETE-SQLite-Journalerzeugung fortschreiten; +Identität/Besitz/Modes bleiben. Unbekannte neue persistente Zustandsobjekte +werden verweigert. + +`sql-before.json` enthält vollständige geordnete Rowhashlisten einschließlich +rowid. Die Quelle erhält jedes alte Schema und jeden alten Historienhash. +Neue Job-/Verbrauchs-/Wirkungs-/Outcomereihen sind ausgeschlossen. Nur feste +passive Sync-Ausnahmen gelten: Metazeilen, monotoner Sequenzzähler, eine zuvor +NULL gewesene und anschließend unabhängig signiert angewandte Antwort sowie +eine erneuerte Outcome-Entscheidungsreferenz. Letztere verlangt den vollständigen +alten Rowhash, erhaltene exakt gleiche Job-/Versuch-/Outcomebytes und beide +ursprünglich signierten, Agent-/Helper-gebundenen Entscheidungen mit monotoner +Lineage. Keine pauschale Freigabe veränderter Historientabellen. Alte Queuezeilen +müssen im Vorherzeugen fehlen; eine mehrdeutige alte Queue verweigert diese +Quelle. Der ursprüngliche Zähler wird aus seinem Hash in einem höchstens4096 +Sequenzschritte umfassenden Bereich bis zum aktuellen Wert rekonstruiert. Reicht +das nicht, fehlt der Nachweis; kein Quellenfallback und keine Zielreparatur. + +Alle18 ursprünglichen Rootrequests/-grants/-signaturen/-starts/-outcomes bleiben +exakt gleich. Aktuelle Agent-/Helper-Metadaten müssen faultfrei dieselbe +angewandte Entscheidung, Generation, Journalbindung und unveränderte öffentliche +Bindingidentität belegen. Die tatsächliche Ed25519-Signatur und die originalen +Request-/Protobufantwortbytes werden unabhängig geprüft. Die neue Entscheidung +muss innerhalb ihrer ursprünglichen höchstens60s-Lease liegen und eine +ursprüngliche vor dem Bootstrap erhaltene signierte Journal-Lineage fortsetzen. +Das belegt normale NativeClock-/Peer-/Applyprüfungen der gepinnten Runtime; +der Reader erzeugt keine neue Probe und erteilt keine Jobstartberechtigung. + +## Passive Serverkontakte und konkrete Nachweisgrenzen + +Der Serverreader führt ausschließlich gespeicherte Datenabfragen in +Read-only-Transaktionen aus. Er liest keine Anmeldetokens oder privaten +Umgebungs-/PKI-Dateien. Rootpolicyhash und geschlossener Ablauf bleiben gebunden; +30Jobs/30Outbox/26Permits/keine offenen Jobs bleiben erhalten. Die30 vollständigen +Jobzeilenfingerprints müssen über alle drei Beobachtungen gleich bleiben. +Die alte Jobsammlung ist zusätzlich durch die VM-Vorherhistorie gebunden; dieser +neue Reader rekonstruiert keinen früher nicht vorhandenen Server-Rowsnapshot. + +Jede Beobachtung bindet dieselbe tatsächlich gespeicherte signierte Entscheidung +an Server, Agent und Helper sowie die normal veröffentlichte Full-Policy-/ +Binärcapability. Ein RLS-Aufruf als `rmm_app` im anderen Testtenant muss für das +feste Gerät, dessen Inventar und Jobs jeweils null Zeilen liefern. Das ist ein +echter gespeicherter Datenbank-RLS-Nachweis, **keine neue HTTP-/MCP-Authabnahme**. + +Drei ausdrücklich getrennte `observe`-Aufrufe liefern Baseline und zwei danach +streng fortgeschrittene gespeicherte Heartbeatzeitpunkte samt Sequenz. Prozesse, +Boot und Verbindungsgeneration müssen über alle drei Reads gleich bleiben; +der aktuelle Kontakt darf beim Read höchstens45s alt sein. Frische Kontakte und +Inventar werden an den dauerhaften `first_start_not_before`-Zeitpunkt der +separaten `continuation-start-intent.json` gebunden. Kein Sleep, Agentneustart +oder angeforderter Refresh zur Erzeugung dieser Daten. + +Ein konkreter neuer `linux.base`-Inventar-Message-ID-/Boot-/Payloadbeleg muss +serverseitig vorliegen und denselben lokalen dauerhaften Queueeintrag beendet +haben. Die Quelle bezeichnet dies ausdrücklich als **quellgebundenen +Queueabschluss**, `wire_inventory_ack_captured=false`: Der bestehende Agent +speichert rohe ACKframes nicht dauerhaft. Aus dieser Probe darf kein angeblich +mitgeschnittener ACKframe entstehen. Der normale ACK-/signierte Sync-ACK-Pfad +der gepinnten Runtime entfernt den Eintrag; der Reader löscht nichts. + +## Geplante Bedienung, nicht ausgeführt + +Nach Rootreview und erfolgreicher separater erster Dienststartfortsetzung: + +```text +python3 deploy/script_console_post_attestation.py prepare-local +python3 deploy/script_console_post_attestation.py observe --id POSTCASE +python3 deploy/script_console_post_attestation.py observe --id POSTCASE +python3 deploy/script_console_post_attestation.py observe --id POSTCASE +python3 deploy/script_console_post_attestation.py finish --id POSTCASE +``` + +Die Beobachtungen zeitlich über zwei normale Heartbeats verteilen. Unveränderte +Kontakte oder unvollständige Signatur-/Queue-/Readinessbelege ergeben kein PASS. +`finish` liest ausschließlich die lokalen drei erhaltenen Belege; seine Aussage +betrifft diesen erhaltenden Runtime-/Sync-/Passivnachweis. Keine RMM-Jobs, +Genehmigungen, Paketwirkung oder Reboots. Der eigentliche FullMaintenancejob +bleibt ein separat unveränderlich beantragter und WebUI-genehmigter Plan. + +## Tatsächliche lokale Prüfung und offene Zielnachweise + +Python-AST der neuen Quelle ist geprüft. Die tatsächliche lokale Vergleichs- +funktion bestand zunächst eine ausdrücklich synthetische Dreierbaseline und +zwölf unabhängige Manipulationsverweigerungen; nach Ergänzung der ausdrücklichen +Serverbelegpflicht bestand dieselbe tatsächliche Funktion eine neue synthetische +Dreierbaseline und15 unabhängige Verweigerungen. Keine VM-Daten wurden hierfür +erzeugt. Die tatsächliche Serverreaderfunktion wurde zusätzlich mit gemockten +Datei-/Subprozessgrenzen auf genau zwei Read-only-Abfragen einschließlich +`rmm_app`-RLSkontext geprüft; kein echtes PostgreSQL wurde aufgerufen. +Das ist keine native Abnahme. +Ziel-SQL, SSH, OpenSSL auf der VM, reale Signatur-/Erhaltungs-/Heartbeat-/ +Inventar-/RLSbelege sind durch diesen Subagenten nicht ausgeführt. + +## Getrennter Transport-/Leselock-Nachfolger + +Root hat mit dem ursprünglichen Runner89121b42 im Fall77d0b7c1 tatsächlich einen +VM-Erhaltungs-/SignedSyncread bestanden; die anschließende Serververbindung +scheiterte an einem für den DNSnamen fehlenden SSH-Hosteintrag. Daraus entsteht +kein vollständiger Server-/Passivnachweis. Originalquelle und alle vier +ursprünglichen Case-/Transportdateien bleiben unverändert; die Quelle liegt +zusätzlich create-only in `devstate/script-console-post-attestation-source-history/`. +Der Nachfolger verwendet dieselbe bereits verifizierte Server-IP und dieselbe +strikte Hostprüfung wie die vorhandenen Deploymentwerkzeuge. Keine neuen +Hostschlüssel oder Änderungen an known_hosts. + +Der Review der ursprünglich14s dauernden VM-Probe fand einen vermeidbaren +Leselock: Rowhash-/Referenzsuche und externe Signaturprüfung lagen im äußeren +SQLite-Lesekontext. Der erste Nachfolger materialisierte die ursprünglichen +Zeugen innerhalb einer Lesetransaktion und schloss sie vor Hash-/Signaturarbeit. +Die echte nachfolgende Root-Probe mit Runner1e172625 und lokalem Fall5a53ced7 +verweigerte beim Materialisieren unter dem1s-Limit mit `OperationalError`; das +ist kein PASS. Auch diese Originalquelle und ihre sieben ursprünglichen +Casedateien bleiben create-only archiviert beziehungsweise am Originalort. + +Der zweite Nachfolger liest deshalb **höchstens16 ursprüngliche Zeilen pro +Lesetransaktion**, nach rowid geordnet. Jede Seite schließt vor Hash-, +Referenzsuche oder Signaturprüfung. Ursprüngliche Hashprefixe werden vollständig +geprüft; neu angehängte Sync-BLOBs werden nicht vollständig eingelesen. Nur die +ursprünglichen Entscheidungs-IDs werden zusätzlich als kleine Metadatenliste +gehalten. Anfangsanzahlen/Schema und abschließend feste Job-/Wirkungsanzahlen, +Root18-Originalbytes und frische gleiche Agent-/Helperentscheidung werden +separat geprüft. Dies ist ein seitenweiser Erhaltungsbeleg über das berichtete +Beobachtungsintervall, **kein tabellenübergreifender atomarer Snapshot**. +Ein neuer vollständiger SQLite-Integritätsscan wird nicht behauptet +(`sqlite_integrity_scan_performed=false`); Schema und alle ursprünglichen +Zeilenbytes werden unabhängig erhalten und die normale signierte Runtime- +Lineage bleibt zwingend. Keine pauschale Ausnahme für historische Tabellen. + +Zusätzliche einzelne Signaturreferenzabfragen haben eigene kurze Lesekontexte. +Ein kooperatives +SQLite-Progressbudget von1s und0,25s Busytimeout begrenzen die Abfragen; das ist +keine harte Echtzeitgarantie für blockierendes Betriebssystem-I/O. Ein Timeout +bricht den Nachweis ab und schließt/rollt ausschließlich die eigene Lesetransaktion +zurück; keine Produktzeile wird geändert oder ignoriert. + +Tatsächlich lokal mit der extrahierten neuen Lesekontextfunktion geprüft: +2000 synthetische Zeilen materialisiert, anschließend sofortiger Writercommit +in0,93ms; eine absichtlich lange reine SELECT-Rekursion wurde nach1000,13ms +unterbrochen, danach Writercommit erfolgreich. AST-Prüfung belegt keine +Rowhash-/Signatur-/Subprozessarbeit innerhalb eines `database`-Kontexts. Das +ist ein lokaler SQLite-Guard, kein neuer VM- oder Produktions-Locknachweis. + +Der tatsächliche zusätzliche lokale Pagingguard materialisierte50 ursprüngliche +Zeilen mit je64KiB über vier kurze Seiten, während zwischen den geschlossenen +Seiten zusätzliche passive Testzeilen angehängt wurden. Neue Anhänge wurden +nicht mitmaterialisiert; Writercommits zwischen den Seiten dauerten höchstens +0,94ms. Die SELECT-Budgetprobe unterbrach nach1000,10ms und ließ danach einen +weiteren Writercommit zu. Auch dies ist ausschließlich ein synthetischer lokaler +SQLite-Nachweis; kein weiterer Zielaufruf durch diesen Subagenten. + + +## Lokaler Zeitparser und aktueller Prüfzwischenstand + +Der erste Nachfolger2786c218 las nativ VM und Server, scheiterte anschließend +im lokalen Python3.10-Parser an einem gültigen PostgreSQL-RFC3339-Wert mit vier +Nachkommastellen. Eine lokale Korrektur akzeptiert SQL-Zeitstempel mit0–6 +Nachkommastellen und füllt ausschließlich fehlende Nullen auf; keine Rundung, +Trunkierung oder Änderung von VM-/Serverdaten. Ursprüngliche Quelle und Case8614 +wurden create-only erhalten. Quelle076e1c22 verwendet dieselben unveränderten +VM-/Serverreader und einen neuen rein lesenden Fall3319e212. + +Zwei vollständige zeitversetzte Beobachtungen dieses Falls sind tatsächlich +bestanden (Generation131, unveränderte18Rootverbräuche). Der dritte Beleg ist +noch offen. Zusätzliche Fehlerformatierung ließ alle Prüfbedingungen und +Ergebnisdaten unverändert und exportierte ausschließlich statischen Guardcode, +Frames und SQLitecode. Damit ist für eine konkrete Verweigerung +`no SQLite sidecars` nachgewiesen; welche Prüfbedingung die älteren generischen +RuntimeError-Belege auslöste, wird nicht nachträglich erfunden. Separate lesende +Kontrollaufrufe bestätigten weiterhin Erhaltung/SignedSync und konkrete +Inventar-Queuekonvergenz. Bisher kein dreiteiliger Gesamt-PASS und kein Job. diff --git a/docs/script-console-windows-link.md b/docs/script-console-windows-link.md new file mode 100644 index 0000000..f94b62b --- /dev/null +++ b/docs/script-console-windows-link.md @@ -0,0 +1,124 @@ +# Windows-Skriptkonsole: native Units bestanden, Runtimeabnahme offen + +Tatsächlicher Stand2026-11-14, Spezifikation1.20: Nach dem tatsächlichen +Linux-Unitnachweis mit84 bestandenen Tests und5 ausdrücklich ausgelassenen +Datenbankfällen wurden die Windows-Release-Unitprogramme und beide +Windows-Runtimeprogramme aus denselben unveränderten158 Compilerinputs +erfolgreich gelinkt. Der separate isolierte PostgreSQL-Lauf des Linuxpacks +hatte bereits alle45 Servertests einschließlich der5 Datenbankfälle bestanden. + +Das neue Linkmanifest liegt privat unter +`devstate/script-console-windows-link/run-d0b7c844-6770-4930-ac87-4380c87fbad7/manifest.json`, +SHA256 `4b31e6a997248f91db00cf56d7d6043a6feb840be237b9a5a5de5eb71ee4b80c`. +Es bindet das Quellmanifest +`ddd1a78d1cd289b02b1f98572f9652171ec00be84e01ce099fd722d689bcaacd` +und den tatsächlichen Linux-Nativbeleg +`a831a70a3cf76a3e8a9bf5820dcca7100d2ef9860dd9388539fe9c13f949f470`. +Der GNU-Crosslinker lief im unveränderlichen Offline-Dockerimage +`sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9` +für `x86_64-pc-windows-gnu`. Die ursprünglichen Linkbytes und die zur +Bereitstellung von Debugdaten bereinigten PE-Dateien werden getrennt erhalten. + +| Artefakt | SHA256 der Bereitstellungsdatei | +| --- | --- | +| `windows-lib-tests.exe` | `d4e37cbec44766d6d520e4d358a43f5f58779659261172084ad1746640a73993` | +| `windows-agent-tests.exe` | `eaa0cc0331d5e5a48090de7c8f904c13f27bfe4d1a029e6638f5d96573f262b8` | +| `rmm-helper-windows.exe` | `89482ed2914465ffbe06062fe828539edd6a3bed2c9491e6f75be9e538ce286d` | +| `rmm-agent-windows.exe` | `7e8c3cc0045584cf551ded7a71531b771bb34cde915bff5f5ab60f54f4365960` | + +Der PipeV2-Quellhash bleibt +`ce525dab23f2e4faa2c9816280c1a5a8b40e56d0e63a350256e83b2df1d9229d`. +Der Build verändert keine Dienste, Geräteidentitäten, Journale, Policies oder +Capabilities. Er verwendet keine Zugangsdaten und startet keine Windows- +Testprogramme oder RMM-Dienste auf der VM oder dem Entwicklungsnotebook. + +**Tatsächlich nativ bestanden:** Beide vollständigen Windows-Listen wurden vor +jedem Testlauf festgehalten; alle 36 Bibliotheks- und sieben Agenttests bestanden +ohne Fehler, Auslassung oder Filter. Frühere 23+7- oder 35+7-Abnahmen anderer +Quellstände werden nicht auf dieses Pack übertragen. Der Administrator-Unitlauf +ersetzt weder laufende LocalService-/SYSTEM-Dienstnachweise noch die native +Zeitfreigabe oder einen WebUI-genehmigten Skriptauftrag. Die neue Windowsruntime +wurde dadurch nicht bereitgestellt. Es wurden keine neuen Windows-Capabilities +aktiviert; die Runtime-Zeitfreigabe bleibt geschlossen. + +Der dafür vorbereitete separate Runner ist +`deploy/script_console_windows_acceptance.py`. Seine Python-Syntax, +vollständigen lokalen Herkunftsprüfungen und gezielten synthetischen +Listen-/Ergebnisvalidatorfälle wurden geprüft: beide positiven Fälle und sechs +Verweigerungsfälle für falsche Herkunft, doppelte beziehungsweise fehlende +Testnamen und ausgelassene Tests bestanden. Diese künstlichen Daten sind kein +Windows-Ausführungsbeleg. + +Der anschließende lesende Vergleich mit dem tatsächlich geprüften Harness aus +Commit `1136707` fand einen Fehler im neuen Bootstrapcode: fünf ungültige +`-cine`-Operatoren statt `-ine`. Python-Syntax- und JSON-Validatorprüfungen hatten +die eingebettete PowerShell-Syntax nicht geprüft. Der ursprüngliche Stand +`46f62b54fce5a0411b6ced1322bf20ebe315877df559185e6aa1714d62718659` +und sein unbenutzter lokaler Intent bleiben erhalten. Der vorhandene lokale +PowerShell-Parser wurde anschließend über seinen bestehenden Clock-Harnesspfad, +Dateieigentümer und Toolhashes gebunden; sämtliche Tooldateihashes blieben vor +und nach der Prüfung gleich. Tatsächliche PowerShell 7.7.0 `ParseFile` und +`ParseInput` meldeten für das ursprüngliche vollständige Skript jeweils 14 +Parserfehler, nach dem engen Operatorfix jeweils null. Es wurde ausschließlich +geparst, kein Inhalt des Skripts ausgeführt. + +Der korrigierte Stand +`8f0684053f998733607ec28bb0c5e17cb313e2209bd8a276589881783852b1f7` +wurde danach durch den übergeordneten Entwickleragenten tatsächlich bis zum +Bootstrap-Prepare auf der Windows-VM ausgeführt. Der erste SFTP-Upload scheiterte +vor der Übertragung: Dem Zielpfad fehlte der führende Slash, deshalb wurde er +relativ zum Benutzerverzeichnis aufgelöst. Der tatsächliche lesende Readback +des Falls `4e59cc83-df75-4a65-9679-c8c8b48a4e5a` bestätigte vorhandenen +Prepare-Fall, `list_entered:false`, `run_entered:false` und keine Teilberichte. +Das war ein Harnessfehler; kein Windows-Unittest wurde ausgeführt. Quellstand, +Intent und Readback bleiben unverändert erhalten. + +Aktueller Runner: +`41fc85342ec40fb48b075717ccf7a4c76f788296f1f69fb5c5bdc10ddb78a9c5`. +Die zusätzliche Korrektur setzt ausschließlich den absoluten SFTP-Zielpräfix +`/C:/ProgramData/...`; das PowerShell-Skript ist bytegleich zum Operatorfix. +Vollständiges Skript und tatsächlicher Transportwrapper bestanden erneut sowohl +`ParseFile` als auch `ParseInput` mit null Fehlern. Zunächst ausschließlich lokal +vorbereiteter und danach nativ verwendeter Fall: +`76bb47dd-9334-46ff-ab64-a6b173d95e24`, Intent-SHA256 +`e350c3eb46dea9107fc2478981a624f5767c1fbd3c171523dbbf7dfdbc18057c`. +Der übergeordnete Entwickleragent führte für diesen Fall anschließend die +Listenphase tatsächlich erfolgreich aus: 36 Bibliotheks- und sieben Agenttests, +beide vollständigen Listen vor jeder Testausführung. SHA256 der unveränderten +Original-Listenbytes: +`13288f7c50db7ed5a38b439cda17d9e00cdbe1355b584498e7a4ce8f0d8b58e9`. +Dies sind die tatsächlichen neuen Listen mit insgesamt 43 Fällen, keine +Umetikettierung der älteren 42 Fälle. Der separate einmalige Lauf gegen genau +diesen SHA bestand mit 36+7 Tests, jeweils null Fehlern, Auslassungen und +herausgefilterten Tests. Der anschließende Readback validierte denselben +unveränderten Ergebnisbeleg ohne Wiederholung. Privater Beleg: +`devstate/script-console-windows-native/76bb47dd-9334-46ff-ab64-a6b173d95e24/passed.json`, +SHA256 `6d0fea175d63a27eaf754ee4ab921b220e3b910a4d74c44f5e560518d6b7b512`. +Der ursprüngliche Run-Reply hat SHA256 +`e458af4b84d61809b9c614de73773c3b15ee47a0a37c0a5a88400285342e5ef7`. +Manifest-, Quell-, Linux- und Listenbindungen sowie sämtliche Scopeflags wurden +erneut geprüft. Keine Produktinstallation, Clock-/SYSTEM-Freigabe oder +RMM-Jobausführung folgte aus dieser Unitabnahme. + +`prepare-local` erstellt einen +privaten unveränderlichen Fall mit Quell-, Skript-, Unterstützungsquell-, +Artefakt- und Linux-Belegbindung. Im tatsächlichen Ablauf führte nach unabhängiger +Quellprüfung `list --case ` auf der Windows-VM ausschließlich beide +vollständigen `--list`-Aufrufe aus. Die tatsächlichen Listen wurden als +unveränderte Originalbytes mit SHA256 erhalten. Erst ein separater Aufruf +`run --case --lists-sha ` führte genau diese beiden +Unitprogramme jeweils einmal ohne Filter aus. Ein vorheriger dauerhafter +Startmarker verhindert Wiederholung. `read --case ` las allein die +ursprünglichen Teil-/Endbelege; eine verlorene Antwort löst keinen erneuten +Testlauf aus. Alle 158 Quellbindungen und sämtliche vier ursprünglichen und +bereitgestellten PE-Dateien werden auch vor jedem Readback geprüft. + +Das native Testenvironment ist geleert; `WINDIR` und `SystemRoot` werden gegen +das native OS-Systemverzeichnis geprüft und `SystemDrive` daraus abgeleitet und +mit einem gegebenen vorhandenen Wert verglichen. Die Unitprogramme verwenden +einen privaten temporären Fallpfad. Keine SQL-, Helper-, Dienst-, Zeitrichtlinien-, +Capability- oder RMM-Jobänderung gehört zu dieser Prüfung. Ein erfolgreicher +Administrator-Unitlauf ist kein LocalService-/SYSTEM-Runtimenachweis. +Die Flags zu unveränderten SQL-/Produktdaten beziehen sich auf bestehende +Produktdatenbanken und Journale; private temporäre Unitdatenbanken gehören zu +den Bootstrap-Testartefakten. diff --git a/docs/windows-clock-status-diagnostic.md b/docs/windows-clock-status-diagnostic.md new file mode 100644 index 0000000..f0f194a --- /dev/null +++ b/docs/windows-clock-status-diagnostic.md @@ -0,0 +1,69 @@ +# Numerische Windows-Zeitdienstdiagnose + +Stand2026-11-14: Diagnosequelle implementiert und unabhängig vom Produktions- +Clockpfad gebaut. Keine Lockerung von Statusguard, ALPC-/Identitätsprüfung, +`NativeClock.now()` oder `ADAPTER_QUALIFIED=false`. Keine neue Clockauthority. +Die feste zusätzliche Named-Pipe-Statusabfrage liefert ausschließlich acht +numerische Felder: Phase, tatsächliche RPC-Rückgabe samt Presenceflag, +aufgefangene Ausnahme und Strukturgrößen-/Zeigerpräsenz. Keine Quellen-/Peer- +Namen, Pointerwerte oder zusätzlichen Rückgabeeinträge werden veröffentlicht. +Eigener begrenzter Diagnoseworker; nach Timeout kein Ersatz/Retry. + +Lokal tatsächlich bestanden: Windows-Example-Clippy mit Warnungen als Fehler, +fünf Linux-Clocktests, Windows-Testlink (nicht nativ ausgeführt) und Release- +Link.125Quellinputs vorher/nachher identisch. Originale Werkzeugabweichung +(Buildimage ohne WIDL) privat erhalten; erfolgreicher Build mit vorhandenem +unveränderlichem Clockimage. Artefakt `ecad703ad7208d55681c63ba9513fcf4c73b671951d4d1b5db1af9df49f43bdf`, +1779541Bytes. Privater Build `release-ca9a63b8-127c-4933-a45d-9e82449d6980`, +Qualifikation `36ce552e…`. + +Danach tatsächlich auf der Windows-VM unter dem separaten LocalService- +Bootstrapdienst ausgeführt, Fall `2996df8c-b261-466a-8963-a526babf7f67`. +NativeReport und getrennter Readback identisch, eigener Dienst entfernt. +ALPC weiter1753/kein Endpunkt, keine erfolgreichen Identitätsnegativkontrollen. +Zusätzliche unabhängige Abfrage:1783, `stage=90`, `rpc_exception=1783`, +`native_rpc_return_present=0`, `status_present=0`. **Eine aufgefangene RPC-/ +NDR-Ausnahme vor normaler Stub-Rückkehr ist belegt; der eigene nachgelagerte +Strukturguard wurde nicht erreicht.** Das Nullwertfeld `native_rpc_return=0` +ist ohne Presenceflag ausdrücklich kein erfolgreicher nativer Return. + +Die genaue RPC-/NDR-Ursache bleibt offen. Source-/IDL-Prüfung folgt anhand +des [Microsoft-IDL](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-w32t/5793e908-22ef-4cea-962f-fca8a72c485a). +W32Time wurde für diese Diagnose nicht umkonfiguriert, keine Uhr gesetzt und +kein RMM-Job ausgeführt. Zentrale Windows0/0/0/0 und Linux30/30/26/0 samt +geschlossenen Testhooks/Rootpolicy erhalten. Dieses Ergebnis qualifiziert +weder Named-Pipe-Serveridentität noch eine vertrauenswürdige Uhr. Erst recht +öffnet es keinen Grant-/Jobpfad. + +## Enger WIDL-Zeigervertrag: lokal korrigierter Kandidat + +Die erhaltene generierte WIDL7-Typbeschreibung kodiert den ursprünglichen +Status-Ausgabeparameter als `FC_RP→FC_RP→STATUS`. Der dokumentierte Vertrag +verlangt außen eine Referenz, innen den Unique-Statuspointer. Auch die neu aus +Microsofts vollständigem IDL erzeugte WIDL7-Datei zeigt den doppelten Ref: +Es handelt sich präziser um die rekursive Parameterattributbehandlung dieses +Compilers, nicht lediglich um eine andere Schreibweise unseres Narrow-IDL. +[Pointerregeln](https://learn.microsoft.com/en-us/windows/win32/rpc/three-pointer-types), +[Typformat](https://learn.microsoft.com/en-us/windows/win32/rpc/pointers-tfs), +[WIDL7-Compilerquelle](https://raw.githubusercontent.com/wine-mirror/wine/wine-7.0/tools/widl/typegen.c). + +Der enge Kandidat deklariert den inneren Typ ausdrücklich `[unique]` und lässt +das redundante äußere `[ref]` zugunsten des dokumentierten Parameterdefaults +weg. WIDL erzeugt tatsächlich `FC_RP→FC_UP→STATUS`; kein Stubbyte wurde gepatcht. +Ganze Procedureformatfolge/Opnum6 unverändert, alle19Statusfeldoffsets und120B- +Struktur gegen frisch erzeugte Microsoft-Full-IDL-Header per AMD64-Clangassert +geprüft. C/Auth/Statusguard/Clockgate unverändert. MicrosoftMIDL wurde mangels +nativer Werkzeuge ausdrücklich noch nicht ausgeführt. + +Neuer privater Buildfall `f5805176-4a5a-468d-b1b2-088030253c8d`, Qualifikation +`e96baaf6…`:63Compilerinputs vorher/nachher gleich, fünf Clocktests tatsächlich +bestanden, beide WindowsGNU-Alltargets-Clippyprofile, Release- und Unitlink +bestanden. Ein zunächst0Tests selektierender Aufruf und ein falscher lokaler +Harnesslink bleiben als unvollständige Vorgängerlogs erhalten. Neues Artefakt +`91889303e1650d8612b946512f069a3421eb686c7bed315426c48bd62b18a1fd`, +Original-Linkdatei erhalten. Nur `native/w32time.idl` geändert, SHA `37da4acb…`. + +**Native Prüfung dieses Kandidaten noch nicht ausgeführt.** Der tatsächliche +Auslöser der bisherigen1783-Ausnahme ist damit weiter nicht bewiesen. Kein +Clockvertrauen aus Codegenerierung oder lokalem Build, keine Rückstufung vom +unqualifizierten ALPC-Pfad auf eine nur vermeintlich authentisierte Pipe. diff --git a/docs/windows-eventlog-contract-acceptance.md b/docs/windows-eventlog-contract-acceptance.md new file mode 100644 index 0000000..7403819 --- /dev/null +++ b/docs/windows-eventlog-contract-acceptance.md @@ -0,0 +1,65 @@ +# Native M3-Eventlog-Vertragsprüfung + +`deploy/windows_eventlog_acceptance.py` ist ein neuer, separater Bootstraprunner. +Der unveränderte Profilrunner und dessen16/7- beziehungsweise16/6-Belege bleiben +readbackfähig. Neuer Namespace: `windows-eventlog-contracts`. + +Der neue Pack enthält ausschließlich Domain-Lib17 und Core-Contracts7 auf Linux, +danach Domain-Lib17 und Core-Contracts6 auf Windows. Der Unix-OpenSSL-Paritätstest +entfällt unter Windows. Server-/Windows-M3-Quellen werden zusätzlich exakt an den +geprüften Stand gebunden, einschließlich Schemars-Metadatenfix und Gateway- +Ergebnisprüfung. Diese Adapterquellen werden durch die beiden Vertrags-EXEs +**nicht** als native Eventlogprovider qualifiziert. Es gibt weder Liveabfrage +noch RMM-Job, Capabilityaktivierung, Dienst-/Produktdatenbank-/PKIänderung. + +Tatsächlich auf Cloud-Linux bestanden:24Prüfungen ohne Skips, Fall +`6f9bdfb7-3902-462e-a08f-ec612e36c635`. Gemeinsamer Build +`afb44f9b-8040-4efa-ac2c-fd6c3a43f8a6`, Manifest-SHA256 +`ed1371e9b058f8e7f874b071fcfce1bc409769546d5e63235295adadb5ec1de7`, +Linuxproof-SHA256 +`d37ce1644dd169c932176e3e9bd33cee22b9fa4698e9faa0f5aea0f2de09f7d4`. +Anschließend bestanden auf der Windows-VM alle23Prüfungen ohne Skips, Fall +`ccb258a7-5930-4e02-b955-d522f98f0c36`, Windowsproof-SHA256 +`ee1ba206637f2465e7a066b6e23ca2f8bf9b57c0ae08ddc80fe23e0199b027e1`. +Dieser Beleg bindet denselben Build und den ursprünglichen Linuxproof. +Der lokale Build mit +98identischen Vorher-/Nachher-Inputs, vier erhaltenen Original-Linkdateien, +beiden Ziel-Clippyprüfungen und24lokalen Linux-Vertragstests ist bestanden. + +Der lokale Offlinebuild erhält Original-Linkdateien neben gestrippten +Lieferkopien, prüft Clippy für beide Vertragsziele und führt die tatsächlichen +Linux-Lieferbytes mit exakter Liste und ungefiltert aus. Windowslisten bleiben +bis zur tatsächlichen Windowsausgabe erwartet. Vor/nach müssen sämtliche +gebundenen Cargo-/Toolchain-/Domain-/Core-/Protocol-/Proto-/Server-/Windows- und +Runner-/Bootstrapquellen gleich sein. Ein weiterer Backend-/PG-Testlauf ist +nicht Bestandteil dieses Runners. + +**Aufrufschema; beide obigen nativen Fälle tatsächlich ausgeführt:** + +```sh +python3 deploy/windows_eventlog_acceptance.py build-local +python3 deploy/windows_eventlog_acceptance.py linux --manifest devstate/windows-eventlog-contracts/build-UUID/manifest.json --access-root /ABSOLUTER/PFAD/ZUM/HAUPTREPO --execute-native-tests +python3 deploy/windows_eventlog_acceptance.py windows --manifest devstate/windows-eventlog-contracts/build-UUID/manifest.json --access-root /ABSOLUTER/PFAD/ZUM/HAUPTREPO --execute-native-tests +``` + +Artefakte und lokale Belege bleiben im isolierten Eventlogworktree. Ausschließlich +`--access-root` liest bei ausdrücklicher nativer Ausführung streng geparste +bestehende SSH-Konfigurationen für Cloud-Linux116.203.20.85 beziehungsweise den +bekannten gepinnten Windows-Reversezugang. Keine Credentialkopie oder neue +Hostkeyannahme. Windows bindet den SHA256 des zuvor tatsächlich verifizierten +Linuxbelegs desselben Manifests. + +Jeder native Fall hat eigene Create-once-Intents und Startbelege. Linux nutzt +privaten Rootscratch, geleerte Umgebung und eigene Netzwerknamespace. Beide +Plattformen prüfen vor dem ersten Teststart die vollständigen Testnamen beider +Programme. Windows läuft als Bootstrapadministrator, kein LocalService-Nachweis. +Ausgaben und Transportfehler bleiben privat; stdout enthält nur Status und +Nachweishashes. + +Nach Antwortverlust ausschließlich `readback-linux` beziehungsweise +`readback-windows` mit ursprünglichem Manifest, `--case UUID` und `--access-root`. +Readback lädt nichts hoch und startet nichts neu. Er bindet unveränderliche +Originalintents, Manifest sowie Runner-/Remotescripthashes; spätere +Produktquellenänderungen erzwingen keinen neuen Lauf. Nur neue Starts benötigen +den aktuellen exakt qualifizierten Quellstand. Unvollständige Fälle bleiben +unvollständig; kein automatischer zweiter Versuch. diff --git a/docs/windows-eventlog-source.md b/docs/windows-eventlog-source.md new file mode 100644 index 0000000..1f6acd3 --- /dev/null +++ b/docs/windows-eventlog-source.md @@ -0,0 +1,55 @@ +# Windows-M3: begrenztes Ereignisprotokoll + +Stand2026-11-14: Quelle implementiert und unabhängig gegenseitig geprüft, +noch nicht auf dem Windowsgast installiert oder als RMM-Aktion ausgeführt. +Der isolierte Branch enthält auch das bereits separat nativ qualifizierte +optionale PowerShell-Prozessprofil. Keine Capability wurde veröffentlicht. + +`query_logs` bindet ausschließlich Quelle +`windows.eventlog.application.rustdiscovery-mvp-test`, Kanal `Application` +und Provider `RustDiscovery-MVP-Test`. Erforderlich sind Windows-/Sync- und +eigene Fixturecapability, `logs:read` und `agent_service`. Kein SYSTEM-Logpfad, +kein freies XPath, kein angeforderter Liveabruf als passives Inventar. +UTC-Zeitfenster höchstens24Stunden, höchstens65536Ausgabebytes und feste +innere5Sekunden-Grenze; äußerer Jobrahmen10–120Sekunden. Alle Werte stehen +aus hashgeprüften gespeicherten Planbytes vor der WebUI-Genehmigung fest. + +Die synchrone native Eventlog-API läuft im begrenzten eigenen Prozess innerhalb +des vorhandenen atomaren JobObjects. Der Child prüft tatsächlichen LocalService- +Token samt Agent-Service-SID, echten SCM-/Kernel-Elternprozess, geschützte +Konfiguration, signierten Originalrequest, aktuelle qualifizierte Uhr und Boot, +den bereits dauerhaft gebuchten Verbrauch/Start samt tatsächlichem SQLite- +Dateihandle sowie einen einmaligen Startmarker. Der Aufruf über den festen +CLI-Modus allein verleiht keine Berechtigung. Keine Schlüssel-/Requestbytes +auf der Kommandozeile. Uhr-/Jobgates bleiben geschlossen. + +Worker und Server prüfen konkrete Herkunft, Zeitfenster, Budget, Vollständigkeit +und Fehler. Bekannte Ereignis-/Byte-/Einzelereignisgrenzen bleiben ausdrücklich +partiell; Zeitüberschreitung oder Berechtigungsfehler werden kein vollständiger +Erfolg. Eine im Review gefundene Lücke im serverseitigen Successguard wurde +geschlossen: `win32_error` ausdrücklichnull, kein Neustartbedarf und generisches +Kürzungsflag identisch zur konkreten Evidenz. + +Lokal tatsächlich bestanden:238Rusttests, ein isolierter PostgreSQL-Fall und +38Webtests samt Typecheck/Build/Clippy. Der PG-Fall prüfte unter anderem fehlendes +`logs:read`, verlorene Capability und `pending_approval` mit0Jobs/Outbox/Grants. +Nach dem Successguard-Fix wurden dessen bestehende Mutationsmatrix und +Server-Clippy gezielt erneut ausgeführt. Drei portable Provider-/Ergebnistests +liefen tatsächlich im Linux-Buildcontainer; zwei Windows-Workerprüfungen sind +bisher nur crosskompiliert. Keine dieser Ebenen ersetzt den Nativepfad. + +Private Quellenbelege: `devstate/windows-eventlog-backend/qualification.json` +(`facab5cc…`), `qualification-success-flags.json` (`f5972644…`) und +`devstate/windows-eventlog/windows-source-qualification.json` (`feb16e79…`). +Die gesonderte Vertragsabnahme ist zuerst auf Cloud-Linux24, danach Windows23 +ohne Skips bestanden. Manifest `ed1371e9…`; Linuxfall `6f9bdfb7…`, Windowsfall +`ccb258a7…`. [Native Runnerdokumentation](windows-eventlog-contract-acceptance.md) +hält genaue Bindungen und Grenzen fest. Kein Live-Eventlog-/Workerrollenbeweis. + +Weiter offen: reale LocalService-Eventlog-API, eigene Fixtureereignisse, +Filter-/Zeit-/Ausgabe-/Fehlergrenzen und vollständiger WebUI-genehmigter Job. +Keine Registrierung oder Erhebung auf dem Ziel wurde durch diese Quellarbeiten +ausgeführt. Der Schemars-Attributfix entfernt lediglich die irreführende +Nullable-Angabe des optionalen Prozessprofils; strikte Deserialisierung und +fehlendes Feld bleiben unverändert. Die vorherigen Nativebelege bleiben an +ihre ursprünglichen Quellen gebunden. diff --git a/docs/windows-helper-pipe-drain.md b/docs/windows-helper-pipe-drain.md new file mode 100644 index 0000000..d1c2eaa --- /dev/null +++ b/docs/windows-helper-pipe-drain.md @@ -0,0 +1,112 @@ +# Windows Helper: begrenzte Transportbestätigung + +Dieser Schnitt ist im isolierten Zweig `codex/windows-pipe-drain` implementiert. +Eine native Ausführung des neuen Framings und ein Komponentenupgrade sind noch +nicht nachgewiesen. Die bestehenden Nachweise des Agenten `89004ad3…` und Helpers +`e6aacbdd…` bleiben historische Belege ihres ursprünglichen Framings. + +## Anlass und Beleggrenze + +Beim tatsächlichen Agenten wurde `client_read_reply / HELPER_PIPE_IO / 233` +beobachtet; die Helperdiagnose war nicht vorhanden. Der bisherige Dienst rief +auch nach erfolgreichem Schreiben der Antwort sofort `DisconnectNamedPipe` auf. +Microsoft dokumentiert, dass dabei ungelesene Pipebytes verworfen werden. Das +ist ein konkreter möglicher Fehlerpfad, aber kein Nachweis, dass genau dieser +Pfad die beobachtete Störung verursacht hat. +[DisconnectNamedPipe](https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-disconnectnamedpipe) + +Eine fehlende Diagnose beweist weder einen erfolgreichen Peercheck noch eine +bestimmte Ablehnung: strukturierte `Denied`-Antworten erzeugen keinen privaten +Pipefehlerbeleg, Connect-Leerlauf kann ohne Beleg enden, und das begrenzte +Diagnoseschreiben selbst kann fehlschlagen. Diese Pfade werden hier nicht +umgedeutet oder gelockert. + +## Framing v2 und Reihenfolge + +Agent und Helper verwenden ausschließlich `\\.\pipe\RustDiscovery-System-v2`. +Jeder JSON-Frame beginnt mit acht ASCIIbytes `RDPIPC02`, gefolgt von vier Bytes +Länge in Big Endian und den bisherigen, maximal 1 MiB großen JSONbytes. +V1 und V2 haben unterschiedliche Endpunkte; zusätzlich verweigert der V2-Parser +alte oder fremde Framingtags. Es gibt keinen stillen Fallback. Beide EXEs müssen +als gemeinsam qualifiziertes, gegenseitig gepinntes Paar bereitgestellt werden. +Die JSONschemas von Call/Reply, `helper.system.v1`, Systempolicy-Schema 1 und +RMM-Jobbytes ändern sich nicht. + +Nach dem vollständigen Lesen der Antwort prüft der Agent weiterhin den tatsächlichen +Helperprozess, Schema, Challenge, Enrollmentbindung, Policyhash, Helperbinaryhash +und die Operation/Antwort-Zuordnung. Erst danach schreibt er genau 56 Bytes: +`RDPACK02` (8), ursprüngliche UUID-Challenge in UUID-Bytefolge (16), SHA-256 der +**exakt empfangenen JSON-Antwortbytes** (32). Es findet keine Re-Serialisierung +für diese Prüfsumme statt. Auch eine vollständig geprüfte `Denied`-Antwort wird +so transportseitig bestätigt, ohne sie in einen Erfolg umzuwandeln. + +Der Helper prüft denselben bereits authentifizierten Peer vor dem Schreiben und +nach dem Lesen der Bestätigung. Schreiben, Empfang und Prüfung der Bestätigung +teilen dieselbe bisherige Antwortdeadline von fünf Sekunden; der Client behält +seine bisherige Gesamtdeadline. Erst danach trennt die Dienstschleife die Pipe. +Fehlende, falsche oder verspätete Bestätigung schließt den bestehenden Syncgate +und trennt die Verbindung über den bisherigen Fehlerpfad; statische Diagnosen +unterscheiden `server_drain_receipt` und `client_drain_receipt`. Es gibt keinen +weiteren Aufruf, Retry oder erneute Ausführung. + +Diese Bestätigung bestätigt nur den Empfang der Antwortbytes. Sie ist kein +JobResult-, Journal-, SignedSync-, Grant-, Freigabe- oder Wirkungsbeleg und löscht +keinen dauerhaften Zustand. Ein bereits ausgeführter Auftrag wird durch einen +fehlenden Transport-ACK nicht zurückgenommen oder wiederholt. + +`FlushFileBuffers` wird nicht verwendet: dieser synchrone Pipeaufruf wartet laut +Microsoft bis zum Lesen aller gepufferten Daten und bietet hier keine passende +begrenzte Warteoperation. +[FlushFileBuffers](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers) + +## Prüfung und verbleibender nativer Nachweis + +Die bestehenden IPC-Codecfälle prüfen zusätzlich fremde/alte Frameversionen, +begrenzte Längen sowie manipulierte ACK-Tags, Challenge und Prüfsumme. Eine andere +JSON-Bytefolge mit gleichem JSON-Inhalt darf den ursprünglichen Frame nicht +bestätigen. Die bestehenden Unitfallzahlen bleiben dadurch unverändert. +Lokale Qualifikation wird separat in einem geschützten `devstate/`-Beleg mit +Quellen- und Artefakthashes erfasst. Cross-Clippy und Linken sind kein nativer +Pipebeleg. Vor einem Zielupgrade sind unabhängiger Quellreview, die nativen +Windows-Units des gleichen Manifests und ein eigener erhaltender Paarupgrade mit +explizitem Backup sowie anschließendem tatsächlichen Agent/Helper-Austausch +notwendig. Alle Produktgates bleiben geschlossen. + +Lokale Ausführung: Default- und `system-helper`-Clippy für alle Windows-GNU-Targets +mit `-D warnings`, Release-Build beider EXEs und Linken beider Unitprogramme waren +erfolgreich. 61 gebundene Quelldateien blieben vor/nach dem Lauf gleich. Die +bestehenden 23 Library- und 7 Binary-Unitfälle sind weiterhin die **erwartete** +native Liste; hier wurde kein Windows-Testprogramm ausgeführt. Geschützter +Beleg: `devstate/windows-pipe-drain/qualification-1e0f2b22-1e74-41af-b8ed-0a356be054ba/qualification.json`. + +## Separater Build-/Unitrunner + +`deploy/windows/system_pipe_acceptance.py` erstellt einen neuen Pack unter +`devstate/windows-pipe-drain-build/`. Er prüft den unveränderten lokalen +Qualifikationsbeleg samt ursprünglichen Links und verwendet die bereits +bestandenen Clippy-/Release-/Unitlinkschritte wieder. Nur die fehlenden beiden +Release-Stateprobes werden ergänzt. Alle sechs Auslieferungsdateien erhalten +eine unveränderte `.linked`-Kopie; ausschließlich die Unit-Auslieferungskopien +werden von Debuginformationen befreit. Neue Quellen einschließlich Runner, +Compilerimage, Cargo-/Protoinputs und tatsächlich verwendeter Bootstrapmodule +sind vor/nach dem Build gebunden. + +`--access-root` muss der ursprüngliche Hauptcheckout sein. Nur dort werden die +vorhandenen geschützten Zugangskonfigurationen für den bereits gepinnten +Bootstraptransport und die tatsächlichen Linux-80-/Windows-58-Corebelege gelesen. +Es werden keine Zugangsdaten oder Main-`devstate`-Bäume kopiert oder verlinkt. +Neue Build-, Upload- und Unitfallbelege gehören zum isolierten Pipeworktree. + +Der Native-Unitfall verwendet den neuen Namespace `native-pipe-v2`. **Beide** +vollständigen nativen Listen müssen exakt den erwarteten 23 Library- und 7 +Binary-Namen entsprechen, bevor irgendein Unitfall ausgeführt wird. Intent, +Listen, Start und einzelne Testergebnisse werden create-only gespeichert. +Nach einem Antwortverlust darf ausschließlich derselbe Fall gelesen werden; +auch ein fehlender oder unvollständiger Fall löst keinen erneuten Start aus. +Die Units laufen als Bootstrapadministrator, nicht als Produkt-LocalService, +und beweisen keinen tatsächlichen Pipeaustausch. Zentral gespeicherte +Gerätezähler und geschlossene Fault-/Policyflags werden vor/nach verglichen. + +Der bestehende rohe `helper_peer_controls::raw`-Probe schreibt weiterhin einen +V1-Header. Sein Ablehnungsresultat ist deshalb **kein** V2-Peernegativnachweis; +Runner und Manifest behaupten ausdrücklich keine solche Qualifikation. diff --git a/docs/windows-msi-contracts.md b/docs/windows-msi-contracts.md new file mode 100644 index 0000000..8db42ef --- /dev/null +++ b/docs/windows-msi-contracts.md @@ -0,0 +1,83 @@ +# Gemeinsame MSI-Verträge: Linux-first-Unitpack + +Der neue Runner `deploy/windows_msi_contract_acceptance.py` gehört zum isolierten +Worktree `rust-discovery-windows-msi-integration`. Historische M3-, Profil- und +NoStart-Runner sowie ihre ursprünglichen Manifeste bleiben unverändert. + +Dieser Pack prüft gemeinsame Daten-/Autorisierungs-/Ledger-/Syncverträge und die +bestehenden selbstisolierten Unixhelper-Dateiguards. Er installiert kein MSI, +startet keinen RMM-Dienst, verändert keine Produktjournale oder Identitäten und +erzeugt keine Jobs oder Genehmigungen. Insbesondere ist er kein tatsächlicher +SYSTEM-, Clock-, Cache-, Download-, Installer- oder WebUI-Wirkungsnachweis. + +## Exakte Listen und Quellen + +Die zunächst tatsächlich lokal aus den Release-Testprogrammen ermittelten +Linuxlisten enthalten Domain20, Corecontracts8, Helpersync26, Agentsync26 und +Unixhelper21: insgesamt101 Fälle. Der zusätzliche MSI-Corevertrag heißt +`historical_msi_nonstart_keeps_full_signature_binding_and_never_becomes_root_authority`. +Die Windowsliste ist bis zur tatsächlichen nativen Ausgabe ausdrücklich +**erwartet**:20+7+26+26=79. Nur der Unix-OpenSSL-Vergleich und die gesamte +Unixhelper-Dateiguardsuite entfallen dort. Es gibt keine Mindestzahlen oder +Überspringen/Filtern von Fällen zur Erfüllung dieser Zahlen. + +Das Manifest bindet68 Quellen einschließlich vollständiger Domain-/Core-/ +Unixhelper-/Linuxadapterquellen, Cargo-/Protoinputs, eng benötigter Fixtures, +des neuen Runners und seiner Bootstraptransportquellen vor/nach dem Build. +Die13 separat geprüften Domain-/Backend-/SQL-Pins sind exakt an den lokalen +Qualifikationsbeleg `b7bced0a341ffb88af8b584cbe8355ff1021add541004347f609f6136a684b74` +gebunden. Diese Backendpins sind ausschließlich Quellenbindung: der Unitpack +führt weder PostgreSQL noch einen Server aus. Webquellen sind kein Compilerinput +und gehören zum unabhängigen UI-Nachweis ihres Owners. + +## Tatsächlich lokal ausgeführt + +Linux- und Windows-GNU-Clippy der ausgewählten Pakete mit allen Targets und +`-D warnings`, Release-Linken der fünf Linux- und vier Windows-Testprogramme, +Prüfung beider Dateiformate und der unveränderten Original-Links waren erfolgreich. +Alle fünf tatsächlichen Linux-Lieferdateien wurden mit vollständiger Liste und +101 erfolgreichen, ungefilterten Tests im isolierten Offlinecontainer geprüft. +Alle Linux-Dateien referenzieren maximal GLIBC2.34. Nur Auslieferungskopien wurden +von Debuginformationen befreit; `.linked`-Originale bleiben separat erhalten. + +Python-Syntax und PowerShell-AST sind geprüft. Enge lokale Driverguards verweigern +fehlende/doppelte/fremde Listen und übersprungene/gefilterte Resultate. Ein +Mocklauf ohne Zielaufruf verlor die Antwort nach dem alleinigen Ausführungsaufruf; +der nachfolgende gleichnamige Readback lud keine Artefakte erneut hoch und +startete keinen Test erneut. Beide Plattformen verlangen sämtliche tatsächlichen +Listen vor irgendeinem Unitfall. Teilbelege bleiben create-only erhalten. + +Geschütztes lokales Buildmanifest: +`devstate/windows-msi-contracts/build-484a3761-6c84-426f-968d-25a638e9c6f7/manifest.json`, +SHA256 `2d96a07be72f3d71a8097510eb5f647ced6577086a45417c599b772018c29a53`. +Die reine lokale Listenerhebung davor sowie Compiler-/Fehlerausgaben bleiben +privat erhalten. Es wurden durch diesen Arbeitsschritt keine VM-/SSH-Aufrufe, +Upload-, Server-, Dienst-, Registry- oder Installeraktionen ausgeführt. + +## Noch geplante native Abnahme + +Nach unabhängiger Prüfung führt Root zuerst dieselben Lieferdateien auf der +tatsächlichen Linux-VM aus. Erst deren erfolgreicher, unveränderlich an dasselbe +Manifest gebundener Beleg erlaubt die Windows-Unitphase. Native Ausführung ist +ein ausdrücklicher CLI-Schritt mit dem ursprünglichen Main-`--access-root` nur +für den vorhandenen gepinnten SSH-Transport. Zugangsdaten und Main-`devstate` +werden weder kopiert noch verlinkt; alle neuen lokalen Belege bleiben im Worktree. + +Beide Zielseiten erhalten eigene create-only Bootstrapfälle unter +`windows-msi-contracts`. Vor Tests müssen alle fünf beziehungsweise vier +nativen Listen exakt stimmen. Antwortverlust erlaubt ausschließlich denselben +Fall zu lesen, auch bei fehlenden oder unvollständigen Ergebnissen; kein Retry +oder automatisches Resume. Unter Linux laufen die Units zusätzlich in einem +neuen Netzwerknamespace mit begrenztem Prozess-/Ausgabebudget; ihre temporären +Dateien gehören zum eigenen Bootstrapfall. Windows läuft ehrlich als +Bootstrapadministrator, nicht als Produkt-LocalService oder SYSTEM-Executor. + +Auf der tatsächlichen Cloud-Linux-VM anschließend alle101Prüfungen ohne Skips +bestanden: Fall `d015f861-e459-4b82-8ce5-460c3e4c570c`, gemeinsames Manifest +`2d96a07be72f3d71a8097510eb5f647ced6577086a45417c599b772018c29a53`, +Linuxproof-SHA256 +`09fa24e32cd3f3563e80e91265c6cc7c612321b0fdd1febe7aef3ab6de53cf2a`. +Windows79 weiterhin **geplant, nicht nativ ausgeführt**. Der Linux-Unit-PASS +ist keine Installation-/Upgrade-/Noop-Abnahme: jede tatsächliche +Paketwirkung benötigt weiterhin ihren eigenen unveränderlichen Antrag und die +Genehmigung in der WebUI. diff --git a/docs/windows-msi-integration.md b/docs/windows-msi-integration.md new file mode 100644 index 0000000..ad1aa8f --- /dev/null +++ b/docs/windows-msi-integration.md @@ -0,0 +1,90 @@ +# Windows-M4: Domain und Backend + +Stand2026-11-14, Spezifikation1.19. Implementierter und unabhängig geprüfter +Quellenschnitt im Branch `codex/windows-msi-integration`; **kein Clouddeployment +und keine native Paketabnahme**. Die folgenden Teile ergänzen die getrennten +[MSI-Providerquellen](windows-msi11-source.md). + +`ensure_package` erhält das optionale, bei Vorhandensein strikt objektförmige +`windows_msi_manifest`. Intern Box, auf dem Draht unverändertes flaches +Rezeptobjekt; fehlend ausgelassen. Explizites Null, unbekannte Felder und +Objektformen der String-Enums werden verweigert. APT und MSI schließen sich aus. +Nur `msi`/`rmm-windows-msi-fixture`/`x64`/`machine`/SYSTEM mit exakt gebundener +Rezept-ID/-Version und gewünschter Produktversion. Alte Linux-Goldenbytes bleiben +erhalten. MSI-Jobrahmen10–120Sekunden und1024–65536Ausgabebytes. + +Eigener unveränderlicher Windows-MSI-Katalog mit Migration0014, FORCE-Tenant-RLS, +zusammengesetztem Tenant-/Artifact-ID-/SHA-/Längen-FK und bestehenden interaktiven +Katalogrechten. Import/List sind vom APT-Katalog getrennt. Pro MSI-Job/Versuch +wird genau eine Artefaktreferenz automatisch aus dem Rezept abgeleitet; freie +`artifact_ids` sind hier verboten. Fehlende, zusätzliche oder doppelte Artefakte +und abweichende gesamte Rezeptmengen werden bei Planung, Revalidation, +Zustellung, Permit und Geräte-mTLS-Download verweigert. Linux-Artefaktpfade +behalten ihre bisherigen Verträge. + +SYSTEM-Policyversion2 verlangt die eigene explizite Allowlist von höchstens +zwei vollständigen MSI-Rezepthashes, bestehende Geräte-/Principal-/Helperpins, +`system:execute` und getrennte Paketcaps. Schema1 bleibt SCM-only und verweigert +MSI. Die historische Core-Nichtstartprüfung kennt nur den zusätzlichen exakt +validierten MSI-Actiontyp; keine Missing-Row-/Signatur-/Lineageausnahme, +Installer- oder Cachewirkung durch Abandon. + +Tatsächlich lokal bestanden:118betroffene Domain-/Core-/Servertests, danach +gezielter Test der verschärften vollständigen Rezeptmenge; isolierter realer +PostgreSQL-Fall mit Migration/RLS/Tenant-FK/Immutabilität/Planreferenzmutationen +und `pending_approval` mit0Jobs/Outbox/Permits. Fehlendes `system:execute` +verweigert. Fünf bestehende Serverfälle bleiben ausdrücklich ignoriert. +Linux-Clippy einschließlich Agent/privilegiertem Helper und WindowsGNU- +System-helper-Alltargets sowie Format-/Diffprüfung bestanden. + +Der erste PG-Lauf belegte einen unnötigen `FOR SHARE`-Lock, der zusätzliche +UPDATE-Rechte erfordert hätte. Ausschließlich der Lock wurde entfernt; +Unveränderlichkeit/SELECT-/INSERT-Rechte bleiben erhalten. Fehlversuchslog +privat unverändert. Der erfolgreiche PG-Fall nutzte ausdrücklich opake +Metadaten-Testbytes, kein tatsächlich geprüftes oder installiertes MSI. +Lokaler Beleg `devstate/windows-msi-integration/local-qualification.json`, +SHA256 `b7bced0a341ffb88af8b584cbe8355ff1021add541004347f609f6136a684b74`, +bindet13Quelldateien. Unabhängiger Quellenreview ohne weiteren konkreten Blocker. + +Weiter offen: Windows-Nativevertragsnachweise, tatsächliche WebUI-Abnahme, +Windowsdownload und geschützter SYSTEM-Cache, Helper-/Runtimeverdrahtung und +jeweils echte WebUI-genehmigte Installation/Upgrade/Noops. Keine Capability, +Helperpolicy oder Clock-/Jobkonfiguration auf der VM wurde geöffnet. Die +vorherige UI verweigerte SYSTEM-Nicht-SCM-Pläne bis zum folgenden separaten +Schnitt. Quellen/Units sind keine Paketfreigabe. + +## Getrennter WebUI-Quellenschnitt + +Feste SYSTEM-MSI-Auswahl und eigener kleiner Katalogbereich implementiert; +APT bleibt getrennt. Anzeige enthält Product-/Upgrade-/gegebenenfalls1.1- +PackageCode, Version, Artifact/Rezept/Nutzdateihashes, Vorgänger, feste Argumente +und No-Reboot-/Budgetvertrag. Sie stammt aus kopierter typisierter Projection +hashgeprüfter gespeicherter Planbytes. Alle Preview-Aliase und spätere Änderungen +werden vor Anzeige sowie erneut vor Submit/Genehmigung geprüft. Keine neue +Genehmigungsabkürzung oder Policy-/Capabilityveröffentlichung. + +Rootreview fand einen echten Codecfehler: die erste UI-Fassung erwartete +`apt_manifest:null`, Rust lässt `None` tatsächlich aus. Die ursprünglichen +TS-Fixtures spiegelten diesen Fehler. Eng korrigiert: gespeicherte MSI-Actions +verlangen das fehlende Feld; nur das Formular akzeptiert fehlend/null, kein +APT-Objekt. Zwei echte, vom unveränderten Rust-Domaincodec erzeugte synthetische +PlanPayload-Goldens1.0/1.1 werden nun im bestehenden Webtest verifiziert und +gerendert. Keine Signer, realen Geräte oder Nativewirkungen in diesen Fixtures. + +Abschließend42Webtests, Typecheck und Build bestanden; fünf Webdateien und +sämtliche Webinputs vor/nach gebunden, Rust13 unverändert. Neuer privater Beleg +`devstate/windows-msi-ui/wire-fix-955acab2-35af-4507-bb2b-d7cd1fa49347/qualification.json`, +SHA256 `f84d4962ab9a6ef64ef0327fcf7c524efb0bd4064d2416f174a9dfffb524a8a1`. +Der ursprüngliche `fe02…`-Beleg bleibt historisch und ist ausdrücklich wegen +des Codecfehlers abgelöst. Kein Clouddeployment, Browser-Nativlauf, Installer- +oder tatsächlicher WebUI-Freigabenachweis durch diese Quelltests. + +## Tatsächlicher Linux-first-Vertragsnachweis + +Auf der tatsächlichen Cloud-Linux-Test-VM bestanden anschließend alle101 +ungefilterten Prüfungen ohne Skips: Domain20, Corecontracts8, HelperSync26, +AgentSync26 und Unixhelper21. Fall `d015f861-e459-4b82-8ce5-460c3e4c570c`, +Manifest `2d96a07b…`, Proof `09fa24e3…`. Nur eigene isolierte Unitprogramme, +kein RMM-Job, Dienst-/Agentdeployment, Produktjournal-/Schlüsselzugriff oder +Cloudserver-/MSI-/WebUI-Nativnachweis. Windows79 sind weiterhin erwartet und +noch nicht nativ ausgeführt. [Genauer Runnervertrag](windows-msi-contracts.md). diff --git a/docs/windows-msi-runtime-units.md b/docs/windows-msi-runtime-units.md new file mode 100644 index 0000000..1257af4 --- /dev/null +++ b/docs/windows-msi-runtime-units.md @@ -0,0 +1,84 @@ +# Getrennter MSI-Runtime-Unitpack + +`deploy/windows_msi_runtime_acceptance.py` ergänzt ausschließlich die Units der +aktuellen Windows-Runtime. Er verändert keine historischen Runner oder +Manifeste. Der vorhandene Common-Pack bleibt mit seinen tatsächlichen +Linux101-/Windows79-Belegen separat gebunden; dessen gemeinsame Core-/Domain-/ +Proto-/Cargoquellen sind zur Runtime identisch. Er wird nicht als Runtime-Test +umetikettiert. + +## Tatsächliche lokale Qualifikation + +Der eingefrorene Runtime-Quellenbeleg +`17f3eb2258773a22be26afd9681d271f69136cfb52dbda31aac2a0ef577b3816` +bindet82 Compilerinputs und die unabhängig geprüften acht Runtimeänderungen. +Seine bestandenen Windows-GNU-Clippy-Läufe sowie seine unveränderten Windows- +Library-/Binary-Testdateien und Original-Links werden direkt wiederverwendet. +Es findet keine erneute Windows-Kompilierung oder Strip-Operation statt. + +Zusätzlich gebaut und ausgeführt wurden nur die fehlende Linux-Release- +Library-Lieferdatei sowie deren Clippyprüfung. Die vollständige tatsächliche +Liste enthält10 Fälle: vier Journal-, drei Eventlog-Vertrags- und drei MSI- +Klassifikations-/Nachbedingungsfälle. Alle10 Lieferbyte-Tests bestanden ohne +Skip, Filter oder Fehler im isolierten lokalen Offlinecontainer. Die Datei +benötigt maximal GLIBC2.34; der unveränderte Original-Link ist archiviert. + +Für Windows sind35 Library- und7 Binaryfälle **erwartet**, insgesamt42. +Die Namen wurden aus dem vorhandenen nativen23er-Profil und den additiven +kompilierten Testfällen vollständig aufgelöst und in den qualifizierten +Windowsbytes gefunden. Das ist kein ausgeführtes Windows-`--list` und kein +nativer42er-PASS. Die echte vollständige Liste muss vor dem ersten Windows- +Unitfall exakt übereinstimmen. Neue Testfälle wurden dafür nicht geschrieben. + +Das neue Manifest bindet85 Inputs: die82 unveränderten Compilerinputs, den +neuen Runner und seine zwei Parser-/Bootstrapquellen. Python-Syntax, +PowerShell-AST und gezielte lokale Driverguards sind geprüft. Ein reiner +Mocklauf mit verlorener Antwort erlaubte nachfolgend nur den gleichen +Readbackfall; es erfolgte kein erneuter Upload oder Ausführungsaufruf. + +Geschütztes Buildmanifest: +`devstate/windows-msi-runtime-units/build-9458b2f4-3e1b-4931-ae68-f9306993ce12/manifest.json`, +SHA256 `620255bcc57de32044cf23331511937565ac3415514f6ce4d953dad7c5286250`. + +## Getrennt geplante native Schritte + +Nach Root-Review wird zuerst ausschließlich die neue Linux-Library auf der +Cloud-Test-VM geprüft. Erst deren unveränderlicher gleiches-Manifest-PASS erlaubt +den neuen Windows-Unitfall. Beide Windowslisten müssen vor irgendeinem +Testfall stimmen. Die Vorgängerbelege Linux101/Windows79 bleiben dabei erhalten. +Alle neuen Bootstrapfälle, Listen und Resultate sind create-only; Antwortverlust +oder fehlende Teilresultate erlauben ausschließlich gleichnamigen Readback, +keinen Retry oder automatisches Resume. Prozess- und Ausgabe-Nachwartezeiten +sind begrenzt. Linux verwendet einen eigenen Netzwerknamespace und temporäre +Dateien; Windows läuft ehrlich als Bootstrapadministrator. + +Native Ausführung liest den expliziten ursprünglichen Main-`--access-root` +ausschließlich für den vorhandenen gepinnten SSH-Transport. Keine Credential- +oder Main-`devstate`-Kopien/Symlinks. Neue Belege gehören zum isolierten +Runtimeworktree. Produktdienste, Journale, Identitäten, Schlüssel, Capabilities, +Clock- und SYSTEM-Policies werden nicht verändert. + +Die unten belegte Linux10-Phase ist inzwischen nativ bestanden; die +Windows42-Phase bleibt **geplant, nicht ausgeführt**. Der Pack +belegt keine LocalService-/SYSTEM-Ausführung, keinen Cache-/PERSIST-Datei- +Parallelzugriff, keinen MSI-Installations-/Upgrade-/Noop-Job und keinen +vertrauenswürdigen Windowsclockadapter. Diese Abnahmen bleiben getrennt und +unterliegen vor jeder tatsächlichen RMM-Wirkung weiterhin der WebUI-Grenze. + + +## Tatsächlich auf der Cloud-Linux-VM bestanden + +Root führte nach unabhängigem Driverreview dieselbe Lieferdatei auf der +permanenten x86-64-Linux-VM aus. Alle10Tests ohne Skips/Filter bestanden, +Fall `050c33bd-026e-4f89-83e5-d84b1d468827`, ManifestSHA256 +`620255bcc57de32044cf23331511937565ac3415514f6ce4d953dad7c5286250`, +LinuxproofSHA256 +`679ac094e60f6a5f18c2e461d55ef0a447517287c3eb80f2d9a1b9a5a5fe31dd`. +Die exakte vollständige native Liste wurde vor irgendeinem Test geprüft; +eigenes Bootstrap-Scratchverzeichnis, leere Prozessumgebung und neuer +Netzwerknamespace. Keine RMM-Dienste, Produktdaten, PKI oder Paketstände geändert. + +Windows35+7 bleiben **erwartet, noch nicht nativ ausgeführt**. Der befristete +Entwicklungszugang war abgelaufen; Serverhälfte separat erneuert, konkrete +Konsolenrecovery durch den Benutzer noch offen. Kein Ersatz des Windowsbelegs +und keine Paket-/SYSTEM-/Clock-/Jobcapability aus dem Linux-PASS ableiten. diff --git a/docs/windows-msi-runtime.md b/docs/windows-msi-runtime.md new file mode 100644 index 0000000..4b6607c --- /dev/null +++ b/docs/windows-msi-runtime.md @@ -0,0 +1,89 @@ +# Windows MSI runtime: implementierter, noch nicht nativ abgenommener Schnitt + +Stand 2026-11-14, isolierter Zweig `codex/windows-msi-runtime`, Basis `66c1855`. +Dies ist ein Quellen-/Lokalnachweis. Es wurde damit kein Windows-Installer, +RMM-Job, Dienstwechsel oder Bootstrap am Ziel ausgeführt. Die nativen +Domain-/Backend-Nachweise des vorhergehenden M4-Schritts ersetzen die folgenden +noch offenen Runtime-Nachweise nicht. + +## Implementierte Grenze + +Der Network-Agent lädt ausschließlich das im genehmigten Originaljob gebundene +MSI-Artefakt über den bestehenden mTLS-Client und den festen Agent-Endpunkt. +Es gibt keine Installer-URL, Weiterleitung, Bearer-Abkürzung oder abgeschaltete +TLS-Prüfung. Länge und SHA-256 sind auf das Manifest und höchstens 256 KiB +begrenzt. Das Staging liegt unter dem festen Agentzustandsverzeichnis. + +Der SYSTEM-Helper nimmt weiterhin nur die originale signierte Request an, +keinen Callerpfad und keine Installerbytes. Settings-Schema 1 bleibt SCM-only; +Schema 2 kann höchstens zwei genaue Recipe-SHA-256 freigeben. Fehlende neue +Settings-Felder werden beim Serialisieren ausgelassen; vorhandenes `null` ist +kein erlaubter Recipe-Allowlistwert. Die lokale Allowlist begrenzt ausschließlich +und ersetzt weder Plan, Startgrant, Clock noch signierten Sync. + +Vor einer SYSTEM-Cachekopie persistiert der Helper den atomaren Originalstart +mit Verbrauch. Die Kopie verwendet den manifestabgeleiteten SHA-Pfad, prüft +Nicht-Reparse, Elternhandles, File-ID, Länge, Hash und die SYS/Admin-Cachegrenze. +Neue Cacheeinträge werden ohne Ersetzen bestehender Dateien veröffentlicht. +Cache/Staging müssen vorher eingerichtet sein; der Runtimecode repariert weder +ACLs noch Verzeichnisse. Der Provider hält eigene Archiv-/Prozesshandles. + +Ein originales monotones Laufzeitbudget umfasst SYSTEM-Cacheaufbereitung, +MSI-Metadaten, unabhängige Bestandsprüfung, Noopentscheidung und Installerlauf. +Die festen Argumente und MSI-Tabellenprüfungen bleiben erhalten. Erfolg benötigt +unabhängige Product-/Family-/PackageCode-/Payload-Nachbedingungen; `3010` bleibt +als Rebootbedarf sichtbar, `1641` unklar, `1618` ein fehlgeschlagener eigener +Start. Noop hat keinen Installerexitcode. Weder automatischer Reboot noch +allgemeines `msiexec`-Kill oder automatischer Wiederholungsstart sind enthalten. + +Die vorhandene Intentzeile besitzt zusätzlich eine dauerhafte, ausschließlich +verweigernde Abbruchmarkierung. Sie ist nur von 0 nach 1 änderbar; Originalbytes, +Grant und `may_have_dispatched` werden dabei nicht zurückgesetzt. Eine eng +geprüfte additive Migration akzeptiert nur die bisherige genaue Tabellen- und +Triggerdefinition. Der Helper liest die vollständige Originalbindung über eine +jeweils frische gewöhnliche Read-only-Verbindung vor Noop/Start und während der +Beobachtung. Fehlende, fehlerhafte oder gesperrte Beobachtung verweigert. Dies ist +keine zusätzliche Ausführungsautorität. Zwischen letzter Prüfung und OS-Start +bleibt das übliche kleine Rennen; es gibt kein behauptetes Rückgängigmachen. + +Nach unklarem Timeout oder Beobachtungsfehler hält der Helper den eigenen Child, +Archivhandles und Originalgrant-/Requestbindung in einem ungeklärten Slot. +Weitere MSI-Starts werden bis zur Beobachtung desselben Prozessendes verweigert. +Ein späterer Exit ändert das bereits persistierte `unknown` nicht. Nach einem +Helperabsturz bleibt die ursprüngliche Ledger-Sperre bestehen. Intent, Abandon, +RawMirror und signierter Core-Sync behalten ihre strengen Originalbindungen; +keine Missing-row-Regel wurde gelockert. + +## Tatsächlich lokale Prüfung + +Der private Fall `qualified-ee6681c7-8d9d-4af9-8947-5325e1ea2e0f` unter +`devstate/windows-msi-runtime/` bindet 82 Compiler-/Manifestquellen vor und nach +allen qualifizierten Prüfungen identisch. Qualification-SHA-256: +`17f3eb2258773a22be26afd9681d271f69136cfb52dbda31aac2a0ef577b3816`. + +- Linux-Container: 10 Bibliothekstests ausgeführt, 10 bestanden, 0 ignoriert. +- Windows-GNU: Default- und SYSTEM-Feature-Alltargets-Clippy mit `-D warnings` + bestanden. +- Windows-GNU: Lib-/Agent-Bin-Unittests gelinkt, nicht ausgeführt. +- Windows-GNU: beide Releasebinaries gelinkt; AMD64-PE geprüft. Originale und + mit `--strip-all` bearbeitete Auslieferungsdateien sind getrennt erhalten. +- `git diff --check` bestanden. Keine Domain-/Core-/Server-/Web- oder + Root-Cargo-/Lockänderung. + +Release-Auslieferung Agent: +`7d9b1221ccb3587437d0866296472330947a16445c4e40069981fc4197242d83`. +Release-Auslieferung Helper: +`96589d53d366a0ccbd60c301fd3d1f03cd4d9f93d85bdcb3ce9b7f4647afc273`. +Die Zwischenbelege vor der ergänzten laufenden Abbruchbeobachtung bleiben +historisch erhalten und sind nicht die finale Qualification. + +## Noch offen + +Die Windows-Unitbinaries, Cache-/Staging-ACLs und gehaltenen Handles, tatsächliche +PERSIST-Migration mit konkurrierender Deny-Lesung, Installer 1.0, MajorUpgrade +1.1, beide Noops und die Abbruch-/Unklarheitsfälle müssen auf der Windows-VM +nachgewiesen werden. Installerjobs benötigen jeweils die reale WebUI-Grenze. +Capabilityliste bleibt passiv, Clock-Gate unqualifiziert, Job-/Effektdefaults +bleiben geschlossen. Diese Implementierung behauptet weder native MSI-Abnahme +noch Windows-MVP-Abschluss. EXE, WinGet, allgemeine Installerargumente oder eine +neue Rebootstrecke gehören nicht zu diesem Schnitt. diff --git a/docs/windows-msi11-source.md b/docs/windows-msi11-source.md new file mode 100644 index 0000000..b680ffd --- /dev/null +++ b/docs/windows-msi11-source.md @@ -0,0 +1,44 @@ +# Eigene MSI-Fixture1.1: Quellenstand + +Stand2026-11-14: drei unexportierte Quellen im isolierten Branch +`codex/windows-msi-provider` erweitert und unabhängig geprüft. Keine Änderung +an produktiven Cargo-/Action-/Helper-/Serverpfaden und keine Capability oder +Paketfreigabe. MSI1.1 wurde noch nicht gebaut, registriert oder installiert. +Der tatsächliche alte MSI1.0-Artefaktbeleg bleibt unverändert erhalten. + +Neues festes Rezept `rmm-windows-msi-fixture-upgrade-1.1`, ProductCode +`{D42BD387-079B-4BBF-A134-0DA7B7A97011}`, gleicher UpgradeCode wie1.0. +Das vollständige Rezept bindet zusätzlich den konkreten neuen PackageCode und +genau den bekannten1.0-Vorgänger samt Version/33-Byte-Nutzdateihash. Alte1.0- +Rezeptbytes bleiben durch am Ende angefügte, bei None ausgelassene Felder gleich. +Kein Fresh1.1, Repair, Downgrade oder beliebiger Vorgänger. + +Provider und Builder verlangen feste Upgrade-Tabelle, LaunchCondition und +Ausführungssequenz, keine CustomActions. Vorheriger Machine-Produktzustand, +Familie und Nutzdatei müssen passen. Nachher1.1 exakt installiert und1.0 entfernt; +ein eigener Noop verlangt denselben konkreten1.1-PackageCode. Ein anderes neu +gebautes1.1-MSI erhält keine implizite Neuinstallationserlaubnis. + +Zwei Quellenfehler wurden korrigiert: spät beobachteter Installerexit oder +Nachzustand wird nicht rückwirkend erfolgreich; zusätzlich beginnt das eine +Verifikations-/Installationsbudget vor allen synchronen Prüfungen und umfasst +auch Noop samt frischem Fence und erneuter Abbruchprüfung. Timeout bleibt +Unknown, kein globales Installerkillen oder Retry. `Run::Noop` kann dadurch +auch eine Unknown-Observation enthalten; spätere Aufrufer müssen deren State +auswerten und dürfen die Enumvariante nicht pauschal als Erfolg behandeln. + +Tatsächlich lokal bestanden: fünf gezielte Harness-Tests ohne Skips, +Linux-/WindowsGNU-Harness-Clippy mit Warnungen als Fehler und PowerShell-AST. +Der Harness bindet explizit diesen Worktree und exportiert keine Produktmodule. +Originaler Quellenbeleg `devstate/windows-package-msi11/local-source-qualification.json` +(`0b562dc2…`) bleibt historisch; enger Folgebeleg +`noop-deadline-qualification.json` (`23ed1175…`) bindet den korrigierten Stand. +Finale Packagequelle `617b2a73…`, Domain `070a8dfa…`, Builder `6365ac16…`. +Im Builder wurde anschließend nur ein überflüssiges Leerzeichen entfernt: +PowerShell-Parse erfolgreich und vollständige Tokenliste vorher/nachher +identisch; vorheriger Builderquellenbeleg `2d63eaba…` bleibt historisch. + +Offen sind Linux-first-Vertragsintegration, Tenantkatalog, geschützter +Artefakttransport/Cache, typisierte SYSTEM-Paketerlaubnis und tatsächliche +WebUI-genehmigte Installation/Upgrade/Noops auf der Windows-VM. Lokale Tests +und Crossbuilds sind keine native Installerabnahme. diff --git a/docs/windows-powershell-policy.md b/docs/windows-powershell-policy.md index 9c0b8e0..e869b61 100644 --- a/docs/windows-powershell-policy.md +++ b/docs/windows-powershell-policy.md @@ -1,10 +1,11 @@ # Windows PowerShell5.1: feste prozessbezogene RemoteSigned-Option -Stand2026-11-14, Spezifikation1.18. **Userentscheidung dokumentiert, -Implementierungsentwurf noch nicht umgesetzt und nicht nativ abgenommen.** -Diese Arbeit liest Produktquellen und ändert ausschließlich Dokumentation. -Laufende Rust-/Artefaktbindungen bleiben unverändert. Keine VM-/Server-/Gitaktion -oder Capability-/Clock-/Jobkonfigurationsänderung gehört zu diesem Schritt. +Stand2026-11-14, Spezifikation1.18. **Userentscheidung als Sourcekandidat im +isolierten Profilbranch umgesetzt und nativ Linux-first vertraglich geprüft.** +Die23 Domain-/Coreverträge bestanden auf der Cloud-Linux-VM; Windows22 und die +tatsächliche genehmigte PowerShellwirkung bleiben offen. Keine Capability-, +Clock- oder Jobkonfiguration aktiviert, kein Deployment. Hauptcheckout und +laufende Windowsartefakte bleiben unverändert. [Tatsächlicher Nachweis](windows-profile-acceptance.md). ## Autorisierter Umfang diff --git a/docs/windows-profile-acceptance.md b/docs/windows-profile-acceptance.md new file mode 100644 index 0000000..fcc4d64 --- /dev/null +++ b/docs/windows-profile-acceptance.md @@ -0,0 +1,65 @@ +# Native Prüfung des optionalen Windows-Prozessprofils + +Stand2026-11-14: implementierter und unabhängig geprüfter Profilvertrag im +isolierten Branch `codex/windows-powershell-profile`. Domain-/Server-/Original- +Grantprüfungen, isolierter PostgreSQL-Fall, beide Windows-Cross-Clippy-Profile, +35Webtests, Typecheck und Webbuild lokal bestanden. Zwei UI-Aliasbefunde und +die zunächst akzeptierte Null-/Objekt-Profilform sind eng korrigiert. + +Anschließend bestanden auf der tatsächlichen Cloud-Linux-VM alle23 Vertrags- +prüfungen ohne Skips, Fall `f463acc9-ab77-4b70-bcfb-a016f40c9410`. +Gemeinsamer Build `005b26fd-8269-40dd-8a35-798fabb024a5`, Manifest-SHA256 +`26247a3a9c187e8ce315250ab3bb3a8b5c1fe681028b15143fb9f193e4af9c80`, +Linuxproof-SHA256 `7ecabd9a3ca8fac20aaa7a185b6e128735ea77f143988f058829ae642ad32d25`. +Danach bestanden alle22 Windows-Vertragstests ohne Skips, Fall +`9abd3e0d-d40f-4d6f-adc8-8fdbbefb98e8`, Windowsproof-SHA256 +`47cee8c9c449a45699641d53c0fca48b89a53a90dd22cfd03a2ab7675c0a348d`. +Der Windowsbeleg bindet denselben Build und den vorangegangenen Linuxproof. +Kein Deployment oder tatsächlicher PowerShell-Job; neue Profilcapability nicht +veröffentlicht. Der Hauptcheckout und seine zuvor qualifizierten Artefakte +bleiben für die ausstehende Komponentenwartung unverändert. + +`deploy/windows_profile_acceptance.py` ist ein eigener Bootstrap-Nachweisrunner, +kein RMM-Executor. Er verwendet ausschließlich zwei feste Unitprogramme: +Domain-Lib16 und Core-Contracts7 auf der Cloud-Linux-VM, danach Domain-Lib16 und +Core-Contracts6 auf der Windows-VM. Nur der Unix-OpenSSL-Paritätstest entfällt +unter Windows. Es werden keine Dienste, Produktjournale, PKI, Policies, +Capabilities oder RMM-Jobs verändert. SQLite-Fixturen entstehen ausschließlich +im eigenen temporären Testverzeichnis. Die Windows-Tests laufen als +Bootstrapadministrator; sie beweisen keinen LocalService- oder SYSTEM-Job. + +Der lokale Build erhält Original-Linkdateien neben den gestrippten Lieferkopien, +prüft beide Clippy-Ziele und führt die tatsächlich lieferbaren Linuxbytes lokal +mit `--list` und ungefiltert aus. Alle Workspace-Cargomanifeste, Lock/Toolchain, +Domain/Core/Protocol/Proto sowie Runner und Bootstraptransport werden vorher und +nachher gehasht. Windows-Testnamen sind bis zur nativen Ausgabe nur erwartet. +Der lokale Linuxlauf ersetzt keinen Linux-VM-Nachweis. + +**Aufrufschema; obige Fälle tatsächlich ausgeführt:** + +```sh +python3 deploy/windows_profile_acceptance.py build-local +python3 deploy/windows_profile_acceptance.py linux --manifest devstate/windows-profile/build-UUID/manifest.json --access-root /ABSOLUTER/PFAD/ZUM/HAUPTREPO --execute-native-tests +python3 deploy/windows_profile_acceptance.py windows --manifest devstate/windows-profile/build-UUID/manifest.json --access-root /ABSOLUTER/PFAD/ZUM/HAUPTREPO --execute-native-tests +``` + +`--access-root` dient ausschließlich dem streng geparsten bestehenden +SSH-Zugang: Cloud-Linux `116.203.20.85` bzw. der unveränderten gepinnten +Windows-Reverse-SSH-Kette über Cloud-Loopback22240. Keine Credentialkopien, +Symlinks oder Hostkeyänderung. Alle Artefakte und lokalen Belege bleiben im +isolierten Profilworktree. Linux läuft mit geleerter Umgebung und eigener +Netzwerknamespace. Beide nativen Phasen prüfen vor dem ersten Teststart sämtliche +Testnamen beider Programme. Windows bindet den bereits verifizierten tatsächlichen +Linux-Beleg desselben Manifests über dessen SHA256. + +Jeder Fall hat eigene Create-once-Intents und Startbelege. Eine verlorene Antwort +erlaubt ausschließlich beispielsweise `readback-linux --manifest … --case UUID +--access-root …` bzw. `readback-windows`. Dieser Pfad lädt nichts hoch und startet +keinen Prozess erneut. Er kann `remote_case_absent`, `prepared_without_start` oder +`started_without_result` als unvollständigen Stand berichten. Ein neuer Fall ist +keine automatische Fehlerwiederholung. Originale Readbacks bleiben an das feste +Manifest, Intents und Runner-/Remotescripthashes gebunden; spätere Änderungen an +Produktquellen erzwingen keine neue native Ausführung und verhindern ihren +Readback nicht. Neue Starts benötigen weiterhin die exakt qualifizierten +aktuellen Quellen. Vollständige Ausgaben/Transportfehler bleiben privat, +Standardausgabe enthält nur Status, Fallkennung und Nachweishashes. diff --git a/migrations/0014_windows_msi_catalog.sql b/migrations/0014_windows_msi_catalog.sql new file mode 100644 index 0000000..910ffe3 --- /dev/null +++ b/migrations/0014_windows_msi_catalog.sql @@ -0,0 +1,27 @@ +-- Additive immutable tenant catalog; existing APT recipes/artifact semantics stay. +ALTER TABLE artifacts ADD CONSTRAINT artifacts_tenant_identity_digest + UNIQUE (tenant_id,artifact_id,sha256,size_bytes); +CREATE TABLE windows_msi_recipes ( + tenant_id uuid NOT NULL REFERENCES tenants, + catalog_id uuid NOT NULL, + recipe_id text NOT NULL CHECK(recipe_id IN ('rmm-windows-msi-fixture-install-1.0','rmm-windows-msi-fixture-upgrade-1.1')), + recipe_version text NOT NULL CHECK(recipe_version='1'), + recipe_sha256 text NOT NULL CHECK(recipe_sha256 ~ '^[0-9a-f]{64}$'), + artifact_id uuid NOT NULL, + artifact_sha256 text NOT NULL CHECK(artifact_sha256 ~ '^[0-9a-f]{64}$'), + artifact_bytes bigint NOT NULL CHECK(artifact_bytes BETWEEN 1 AND 262144), + payload bytea NOT NULL CHECK(octet_length(payload) BETWEEN 1 AND 65536), + created_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY(tenant_id,catalog_id), + UNIQUE(tenant_id,recipe_id,recipe_version), UNIQUE(tenant_id,recipe_sha256), + FOREIGN KEY(tenant_id,artifact_id,artifact_sha256,artifact_bytes) + REFERENCES artifacts(tenant_id,artifact_id,sha256,size_bytes) +); +ALTER TABLE windows_msi_recipes ENABLE ROW LEVEL SECURITY; +ALTER TABLE windows_msi_recipes FORCE ROW LEVEL SECURITY; +CREATE POLICY tenant_scope ON windows_msi_recipes + USING (tenant_id::text=current_setting('app.tenant_id',true)) + WITH CHECK (tenant_id::text=current_setting('app.tenant_id',true)); +GRANT SELECT, INSERT ON windows_msi_recipes TO rmm_app; +CREATE TRIGGER immutable_content BEFORE UPDATE OR DELETE ON windows_msi_recipes + FOR EACH ROW EXECUTE FUNCTION immutable_plan(); diff --git a/tests/backend_root_logs.rs b/tests/backend_root_logs.rs index cdd11c7..d19be31 100644 --- a/tests/backend_root_logs.rs +++ b/tests/backend_root_logs.rs @@ -33,10 +33,12 @@ fn fixture() -> (RootScope, JobPayload, Vec) { enrollment_generation: 1, assignment_version: 1, principals: vec!["selected".into()], + helper_binary_sha256: None, helper_policy_sha256: "ab".repeat(32), acceptance_evidence_id: "reviewed-helper-record".into(), allowed_services: vec!["mutation-only.service".into()], allow_root_scripts: false, + allowed_script_profiles: None, allow_reboot: false, apt: None, root_logs: Some(RootLogPolicy { diff --git a/tests/browser/mvp-actions.spec.ts b/tests/browser/mvp-actions.spec.ts index 496db44..947ed75 100644 --- a/tests/browser/mvp-actions.spec.ts +++ b/tests/browser/mvp-actions.spec.ts @@ -61,8 +61,8 @@ test('simulated human prepares root request through UI only, without approving', await page.getByLabel('Begründung', { exact: true }).fill(text(request.reason)); await page.getByRole('button', { name: 'Plan erstellen · noch keine Ausführung' }).click(); const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) }); - await expect(detail.locator('pre')).toBeVisible(); - const plan = object(JSON.parse(await detail.locator('pre').innerText())); + await expect(detail.locator('pre[aria-label^="Alle festen Zielpreviews"]')).toBeVisible(); + const plan = object(JSON.parse(await detail.locator('pre[aria-label^="Alle festen Zielpreviews"]').innerText())); expect(createHash('sha256').update(bytes(plan.plan_payload_bytes)).digest('hex')).toBe(plan.plan_hash); await detail.getByRole('button', { name: 'Antrag absenden · pending_approval' }).click(); const op = page.locator('section').filter({ has: page.getByRole('heading', { name: /^Antrag \/ Ausführung / }) }); @@ -102,8 +102,8 @@ test('one opted-in immutable dev/test action: inspect all fixed bytes → UI cli await expect(op.locator('pre').first()).toContainText('"execution_state": null'); await op.getByRole('button', { name: 'Unveränderlichen Plan zur Prüfung öffnen' }).click(); const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) }); - await expect(detail.locator('pre')).toBeVisible(); - const plan = object(JSON.parse(await detail.locator('pre').innerText())); + await expect(detail.locator('pre[aria-label^="Alle festen Zielpreviews"]')).toBeVisible(); + const plan = object(JSON.parse(await detail.locator('pre[aria-label^="Alle festen Zielpreviews"]').innerText())); expect(plan.plan_hash).toBe(expected.plan_hash); expect(bytes(plan.plan_payload_bytes)).toEqual(bytes(expected.plan_payload_bytes)); expect(createHash('sha256').update(bytes(plan.plan_payload_bytes)).digest('hex')).toBe(expected.plan_hash); diff --git a/tests/browser/vm-jobs.spec.ts b/tests/browser/vm-jobs.spec.ts index 92f91cd..5afb014 100644 --- a/tests/browser/vm-jobs.spec.ts +++ b/tests/browser/vm-jobs.spec.ts @@ -15,10 +15,10 @@ test('preserved real VM: pending fixed Hello World → UI click → confirmed re await expect(op.locator('pre').first()).toContainText('"execution_state": null'); await op.getByRole('button', { name: 'Unveränderlichen Plan zur Prüfung öffnen' }).click(); const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) }); - await expect(detail.locator('pre')).toBeVisible(); + await expect(detail.locator('pre[aria-label^="Alle festen Zielpreviews"]')).toBeVisible(); // Inspect the complete rendered immutable preview, not a separately fetched // or caller-supplied payload. Refuse all root/package/reboot/extra-target jobs. - const plan = JSON.parse(await detail.locator('pre').innerText()); + const plan = JSON.parse(await detail.locator('pre[aria-label^="Alle festen Zielpreviews"]').innerText()); expect(plan.resolved_targets).toHaveLength(1); expect(plan.resolved_targets[0].asset_id).toBe(asset); expect(plan.jobs).toHaveLength(1); diff --git a/tests/script_console/codec-golden.json b/tests/script_console/codec-golden.json new file mode 100644 index 0000000..c48944a --- /dev/null +++ b/tests/script_console/codec-golden.json @@ -0,0 +1,5571 @@ +[ + { + "job_payload_bytes": [ + 123, + 34, + 115, + 99, + 104, + 101, + 109, + 97, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 116, + 101, + 110, + 97, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 34, + 44, + 34, + 97, + 115, + 115, + 101, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 50, + 34, + 44, + 34, + 97, + 103, + 101, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 51, + 34, + 44, + 34, + 101, + 110, + 114, + 111, + 108, + 108, + 109, + 101, + 110, + 116, + 95, + 103, + 101, + 110, + 101, + 114, + 97, + 116, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 97, + 115, + 115, + 105, + 103, + 110, + 109, + 101, + 110, + 116, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 106, + 111, + 98, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 52, + 34, + 44, + 34, + 97, + 116, + 116, + 101, + 109, + 112, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 53, + 34, + 44, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 123, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 34, + 114, + 117, + 110, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 34, + 44, + 34, + 105, + 110, + 116, + 101, + 114, + 112, + 114, + 101, + 116, + 101, + 114, + 34, + 58, + 34, + 112, + 111, + 115, + 105, + 120, + 95, + 115, + 104, + 34, + 44, + 34, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 117, + 116, + 102, + 56, + 34, + 58, + 34, + 112, + 114, + 105, + 110, + 116, + 102, + 32, + 39, + 104, + 101, + 108, + 108, + 111, + 92, + 92, + 110, + 39, + 92, + 110, + 34, + 44, + 34, + 97, + 114, + 103, + 117, + 109, + 101, + 110, + 116, + 115, + 34, + 58, + 91, + 93, + 44, + 34, + 101, + 120, + 112, + 101, + 99, + 116, + 101, + 100, + 95, + 115, + 116, + 100, + 111, + 117, + 116, + 34, + 58, + 110, + 117, + 108, + 108, + 125, + 44, + 34, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 95, + 105, + 100, + 101, + 110, + 116, + 105, + 116, + 121, + 34, + 58, + 34, + 97, + 103, + 101, + 110, + 116, + 95, + 115, + 101, + 114, + 118, + 105, + 99, + 101, + 34, + 44, + 34, + 110, + 111, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 50, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 115, + 116, + 97, + 114, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 51, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 109, + 97, + 120, + 95, + 114, + 117, + 110, + 116, + 105, + 109, + 101, + 95, + 115, + 101, + 99, + 111, + 110, + 100, + 115, + 34, + 58, + 51, + 48, + 44, + 34, + 109, + 97, + 120, + 95, + 111, + 117, + 116, + 112, + 117, + 116, + 95, + 98, + 121, + 116, + 101, + 115, + 34, + 58, + 52, + 48, + 57, + 54, + 125 + ], + "job_payload_hash": "e88c97761b5efa1fc6d928e82099236bb66462dec79569aa618443cad3061f2c", + "name": "legacy_linux_agent", + "plan_hash": "0ca05398ad8b353980fc48822251c36c5e684360ac1ad7ab3490de6c4416ce4b", + "plan_payload_bytes": [ + 123, + 34, + 115, + 99, + 104, + 101, + 109, + 97, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 112, + 108, + 97, + 110, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 54, + 34, + 44, + 34, + 116, + 101, + 110, + 97, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 34, + 44, + 34, + 112, + 114, + 105, + 110, + 99, + 105, + 112, + 97, + 108, + 34, + 58, + 34, + 115, + 105, + 109, + 117, + 108, + 97, + 116, + 101, + 100, + 45, + 100, + 101, + 118, + 45, + 115, + 99, + 114, + 105, + 112, + 116, + 45, + 99, + 111, + 110, + 115, + 111, + 108, + 101, + 34, + 44, + 34, + 114, + 101, + 97, + 115, + 111, + 110, + 34, + 58, + 34, + 67, + 111, + 100, + 101, + 99, + 32, + 102, + 105, + 120, + 116, + 117, + 114, + 101, + 58, + 32, + 108, + 101, + 103, + 97, + 99, + 121, + 95, + 108, + 105, + 110, + 117, + 120, + 95, + 97, + 103, + 101, + 110, + 116, + 59, + 32, + 110, + 111, + 32, + 110, + 97, + 116, + 105, + 118, + 101, + 32, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 34, + 44, + 34, + 106, + 111, + 98, + 115, + 34, + 58, + 91, + 123, + 34, + 112, + 97, + 121, + 108, + 111, + 97, + 100, + 34, + 58, + 123, + 34, + 115, + 99, + 104, + 101, + 109, + 97, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 116, + 101, + 110, + 97, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 34, + 44, + 34, + 97, + 115, + 115, + 101, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 50, + 34, + 44, + 34, + 97, + 103, + 101, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 51, + 34, + 44, + 34, + 101, + 110, + 114, + 111, + 108, + 108, + 109, + 101, + 110, + 116, + 95, + 103, + 101, + 110, + 101, + 114, + 97, + 116, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 97, + 115, + 115, + 105, + 103, + 110, + 109, + 101, + 110, + 116, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 106, + 111, + 98, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 52, + 34, + 44, + 34, + 97, + 116, + 116, + 101, + 109, + 112, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 53, + 34, + 44, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 123, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 34, + 114, + 117, + 110, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 34, + 44, + 34, + 105, + 110, + 116, + 101, + 114, + 112, + 114, + 101, + 116, + 101, + 114, + 34, + 58, + 34, + 112, + 111, + 115, + 105, + 120, + 95, + 115, + 104, + 34, + 44, + 34, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 117, + 116, + 102, + 56, + 34, + 58, + 34, + 112, + 114, + 105, + 110, + 116, + 102, + 32, + 39, + 104, + 101, + 108, + 108, + 111, + 92, + 92, + 110, + 39, + 92, + 110, + 34, + 44, + 34, + 97, + 114, + 103, + 117, + 109, + 101, + 110, + 116, + 115, + 34, + 58, + 91, + 93, + 44, + 34, + 101, + 120, + 112, + 101, + 99, + 116, + 101, + 100, + 95, + 115, + 116, + 100, + 111, + 117, + 116, + 34, + 58, + 110, + 117, + 108, + 108, + 125, + 44, + 34, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 95, + 105, + 100, + 101, + 110, + 116, + 105, + 116, + 121, + 34, + 58, + 34, + 97, + 103, + 101, + 110, + 116, + 95, + 115, + 101, + 114, + 118, + 105, + 99, + 101, + 34, + 44, + 34, + 110, + 111, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 50, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 115, + 116, + 97, + 114, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 51, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 109, + 97, + 120, + 95, + 114, + 117, + 110, + 116, + 105, + 109, + 101, + 95, + 115, + 101, + 99, + 111, + 110, + 100, + 115, + 34, + 58, + 51, + 48, + 44, + 34, + 109, + 97, + 120, + 95, + 111, + 117, + 116, + 112, + 117, + 116, + 95, + 98, + 121, + 116, + 101, + 115, + 34, + 58, + 52, + 48, + 57, + 54, + 125, + 44, + 34, + 106, + 111, + 98, + 95, + 112, + 97, + 121, + 108, + 111, + 97, + 100, + 95, + 104, + 97, + 115, + 104, + 34, + 58, + 34, + 101, + 56, + 56, + 99, + 57, + 55, + 55, + 54, + 49, + 98, + 53, + 101, + 102, + 97, + 49, + 102, + 99, + 54, + 100, + 57, + 50, + 56, + 101, + 56, + 50, + 48, + 57, + 57, + 50, + 51, + 54, + 98, + 98, + 54, + 54, + 52, + 54, + 50, + 100, + 101, + 99, + 55, + 57, + 53, + 54, + 57, + 97, + 97, + 54, + 49, + 56, + 52, + 52, + 51, + 99, + 97, + 100, + 51, + 48, + 54, + 49, + 102, + 50, + 99, + 34, + 44, + 34, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 115, + 104, + 97, + 50, + 53, + 54, + 34, + 58, + 34, + 97, + 98, + 98, + 49, + 56, + 53, + 51, + 99, + 48, + 101, + 102, + 50, + 97, + 97, + 101, + 51, + 102, + 102, + 97, + 53, + 49, + 48, + 97, + 53, + 54, + 52, + 97, + 50, + 98, + 57, + 49, + 48, + 54, + 52, + 102, + 100, + 53, + 100, + 55, + 53, + 55, + 102, + 52, + 51, + 55, + 54, + 55, + 55, + 99, + 98, + 49, + 49, + 100, + 53, + 99, + 99, + 53, + 100, + 56, + 57, + 54, + 56, + 99, + 100, + 34, + 125, + 93, + 44, + 34, + 109, + 97, + 120, + 95, + 112, + 97, + 114, + 97, + 108, + 108, + 101, + 108, + 34, + 58, + 49, + 44, + 34, + 104, + 97, + 108, + 116, + 95, + 97, + 102, + 116, + 101, + 114, + 95, + 102, + 97, + 105, + 108, + 117, + 114, + 101, + 115, + 34, + 58, + 49, + 125 + ], + "script_sha256": "abb1853c0ef2aae3ffa510a564a2b91064fd5d757f437677cb11d5cc5d8968cd" + }, + { + "job_payload_bytes": [ + 123, + 34, + 115, + 99, + 104, + 101, + 109, + 97, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 116, + 101, + 110, + 97, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 34, + 44, + 34, + 97, + 115, + 115, + 101, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 50, + 34, + 44, + 34, + 97, + 103, + 101, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 51, + 34, + 44, + 34, + 101, + 110, + 114, + 111, + 108, + 108, + 109, + 101, + 110, + 116, + 95, + 103, + 101, + 110, + 101, + 114, + 97, + 116, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 97, + 115, + 115, + 105, + 103, + 110, + 109, + 101, + 110, + 116, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 106, + 111, + 98, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 101, + 34, + 44, + 34, + 97, + 116, + 116, + 101, + 109, + 112, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 102, + 34, + 44, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 123, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 34, + 114, + 117, + 110, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 34, + 44, + 34, + 105, + 110, + 116, + 101, + 114, + 112, + 114, + 101, + 116, + 101, + 114, + 34, + 58, + 34, + 112, + 111, + 115, + 105, + 120, + 95, + 115, + 104, + 34, + 44, + 34, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 117, + 116, + 102, + 56, + 34, + 58, + 34, + 105, + 100, + 92, + 110, + 112, + 114, + 105, + 110, + 116, + 102, + 32, + 39, + 114, + 111, + 111, + 116, + 32, + 115, + 116, + 100, + 111, + 117, + 116, + 92, + 92, + 110, + 39, + 92, + 110, + 112, + 114, + 105, + 110, + 116, + 102, + 32, + 39, + 114, + 111, + 111, + 116, + 32, + 115, + 116, + 100, + 101, + 114, + 114, + 92, + 92, + 110, + 39, + 32, + 62, + 38, + 50, + 92, + 110, + 34, + 44, + 34, + 97, + 114, + 103, + 117, + 109, + 101, + 110, + 116, + 115, + 34, + 58, + 91, + 93, + 44, + 34, + 101, + 120, + 112, + 101, + 99, + 116, + 101, + 100, + 95, + 115, + 116, + 100, + 111, + 117, + 116, + 34, + 58, + 110, + 117, + 108, + 108, + 44, + 34, + 112, + 114, + 105, + 118, + 105, + 108, + 101, + 103, + 101, + 100, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 112, + 114, + 111, + 102, + 105, + 108, + 101, + 34, + 58, + 34, + 102, + 117, + 108, + 108, + 95, + 109, + 97, + 105, + 110, + 116, + 101, + 110, + 97, + 110, + 99, + 101, + 95, + 118, + 49, + 34, + 125, + 44, + 34, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 95, + 105, + 100, + 101, + 110, + 116, + 105, + 116, + 121, + 34, + 58, + 34, + 114, + 111, + 111, + 116, + 34, + 44, + 34, + 110, + 111, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 50, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 115, + 116, + 97, + 114, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 51, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 109, + 97, + 120, + 95, + 114, + 117, + 110, + 116, + 105, + 109, + 101, + 95, + 115, + 101, + 99, + 111, + 110, + 100, + 115, + 34, + 58, + 51, + 48, + 44, + 34, + 109, + 97, + 120, + 95, + 111, + 117, + 116, + 112, + 117, + 116, + 95, + 98, + 121, + 116, + 101, + 115, + 34, + 58, + 52, + 48, + 57, + 54, + 125 + ], + "job_payload_hash": "c5e6aefacc68b610ec226b4f27294b251c8151887c8ad36ffb408cea840547c3", + "name": "linux_root_full", + "plan_hash": "e48e4426b525814a2ef9255b47d109b34ceebe2d56a5ba1bd3f9588cce34b5ef", + "plan_payload_bytes": [ + 123, + 34, + 115, + 99, + 104, + 101, + 109, + 97, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 112, + 108, + 97, + 110, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 48, + 34, + 44, + 34, + 116, + 101, + 110, + 97, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 34, + 44, + 34, + 112, + 114, + 105, + 110, + 99, + 105, + 112, + 97, + 108, + 34, + 58, + 34, + 115, + 105, + 109, + 117, + 108, + 97, + 116, + 101, + 100, + 45, + 100, + 101, + 118, + 45, + 115, + 99, + 114, + 105, + 112, + 116, + 45, + 99, + 111, + 110, + 115, + 111, + 108, + 101, + 34, + 44, + 34, + 114, + 101, + 97, + 115, + 111, + 110, + 34, + 58, + 34, + 67, + 111, + 100, + 101, + 99, + 32, + 102, + 105, + 120, + 116, + 117, + 114, + 101, + 58, + 32, + 108, + 105, + 110, + 117, + 120, + 95, + 114, + 111, + 111, + 116, + 95, + 102, + 117, + 108, + 108, + 59, + 32, + 110, + 111, + 32, + 110, + 97, + 116, + 105, + 118, + 101, + 32, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 34, + 44, + 34, + 106, + 111, + 98, + 115, + 34, + 58, + 91, + 123, + 34, + 112, + 97, + 121, + 108, + 111, + 97, + 100, + 34, + 58, + 123, + 34, + 115, + 99, + 104, + 101, + 109, + 97, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 116, + 101, + 110, + 97, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 34, + 44, + 34, + 97, + 115, + 115, + 101, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 50, + 34, + 44, + 34, + 97, + 103, + 101, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 51, + 34, + 44, + 34, + 101, + 110, + 114, + 111, + 108, + 108, + 109, + 101, + 110, + 116, + 95, + 103, + 101, + 110, + 101, + 114, + 97, + 116, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 97, + 115, + 115, + 105, + 103, + 110, + 109, + 101, + 110, + 116, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 106, + 111, + 98, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 101, + 34, + 44, + 34, + 97, + 116, + 116, + 101, + 109, + 112, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 102, + 34, + 44, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 123, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 34, + 114, + 117, + 110, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 34, + 44, + 34, + 105, + 110, + 116, + 101, + 114, + 112, + 114, + 101, + 116, + 101, + 114, + 34, + 58, + 34, + 112, + 111, + 115, + 105, + 120, + 95, + 115, + 104, + 34, + 44, + 34, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 117, + 116, + 102, + 56, + 34, + 58, + 34, + 105, + 100, + 92, + 110, + 112, + 114, + 105, + 110, + 116, + 102, + 32, + 39, + 114, + 111, + 111, + 116, + 32, + 115, + 116, + 100, + 111, + 117, + 116, + 92, + 92, + 110, + 39, + 92, + 110, + 112, + 114, + 105, + 110, + 116, + 102, + 32, + 39, + 114, + 111, + 111, + 116, + 32, + 115, + 116, + 100, + 101, + 114, + 114, + 92, + 92, + 110, + 39, + 32, + 62, + 38, + 50, + 92, + 110, + 34, + 44, + 34, + 97, + 114, + 103, + 117, + 109, + 101, + 110, + 116, + 115, + 34, + 58, + 91, + 93, + 44, + 34, + 101, + 120, + 112, + 101, + 99, + 116, + 101, + 100, + 95, + 115, + 116, + 100, + 111, + 117, + 116, + 34, + 58, + 110, + 117, + 108, + 108, + 44, + 34, + 112, + 114, + 105, + 118, + 105, + 108, + 101, + 103, + 101, + 100, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 112, + 114, + 111, + 102, + 105, + 108, + 101, + 34, + 58, + 34, + 102, + 117, + 108, + 108, + 95, + 109, + 97, + 105, + 110, + 116, + 101, + 110, + 97, + 110, + 99, + 101, + 95, + 118, + 49, + 34, + 125, + 44, + 34, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 95, + 105, + 100, + 101, + 110, + 116, + 105, + 116, + 121, + 34, + 58, + 34, + 114, + 111, + 111, + 116, + 34, + 44, + 34, + 110, + 111, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 50, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 115, + 116, + 97, + 114, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 51, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 109, + 97, + 120, + 95, + 114, + 117, + 110, + 116, + 105, + 109, + 101, + 95, + 115, + 101, + 99, + 111, + 110, + 100, + 115, + 34, + 58, + 51, + 48, + 44, + 34, + 109, + 97, + 120, + 95, + 111, + 117, + 116, + 112, + 117, + 116, + 95, + 98, + 121, + 116, + 101, + 115, + 34, + 58, + 52, + 48, + 57, + 54, + 125, + 44, + 34, + 106, + 111, + 98, + 95, + 112, + 97, + 121, + 108, + 111, + 97, + 100, + 95, + 104, + 97, + 115, + 104, + 34, + 58, + 34, + 99, + 53, + 101, + 54, + 97, + 101, + 102, + 97, + 99, + 99, + 54, + 56, + 98, + 54, + 49, + 48, + 101, + 99, + 50, + 50, + 54, + 98, + 52, + 102, + 50, + 55, + 50, + 57, + 52, + 98, + 50, + 53, + 49, + 99, + 56, + 49, + 53, + 49, + 56, + 56, + 55, + 99, + 56, + 97, + 100, + 51, + 54, + 102, + 102, + 98, + 52, + 48, + 56, + 99, + 101, + 97, + 56, + 52, + 48, + 53, + 52, + 55, + 99, + 51, + 34, + 44, + 34, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 115, + 104, + 97, + 50, + 53, + 54, + 34, + 58, + 34, + 52, + 49, + 52, + 53, + 100, + 57, + 54, + 97, + 56, + 56, + 51, + 51, + 53, + 55, + 57, + 50, + 51, + 99, + 53, + 48, + 51, + 99, + 49, + 97, + 56, + 100, + 56, + 97, + 102, + 49, + 52, + 102, + 57, + 51, + 100, + 48, + 98, + 57, + 49, + 52, + 57, + 54, + 54, + 101, + 98, + 97, + 50, + 48, + 51, + 54, + 100, + 53, + 102, + 49, + 48, + 55, + 55, + 102, + 54, + 54, + 54, + 98, + 50, + 50, + 34, + 125, + 93, + 44, + 34, + 109, + 97, + 120, + 95, + 112, + 97, + 114, + 97, + 108, + 108, + 101, + 108, + 34, + 58, + 49, + 44, + 34, + 104, + 97, + 108, + 116, + 95, + 97, + 102, + 116, + 101, + 114, + 95, + 102, + 97, + 105, + 108, + 117, + 114, + 101, + 115, + 34, + 58, + 49, + 125 + ], + "script_sha256": "4145d96a883357923c503c1a8d8af14f93d0b914966eba2036d5f1077f666b22" + }, + { + "job_payload_bytes": [ + 123, + 34, + 115, + 99, + 104, + 101, + 109, + 97, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 116, + 101, + 110, + 97, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 34, + 44, + 34, + 97, + 115, + 115, + 101, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 50, + 34, + 44, + 34, + 97, + 103, + 101, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 51, + 34, + 44, + 34, + 101, + 110, + 114, + 111, + 108, + 108, + 109, + 101, + 110, + 116, + 95, + 103, + 101, + 110, + 101, + 114, + 97, + 116, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 97, + 115, + 115, + 105, + 103, + 110, + 109, + 101, + 110, + 116, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 106, + 111, + 98, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 56, + 34, + 44, + 34, + 97, + 116, + 116, + 101, + 109, + 112, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 57, + 34, + 44, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 123, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 34, + 114, + 117, + 110, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 34, + 44, + 34, + 105, + 110, + 116, + 101, + 114, + 112, + 114, + 101, + 116, + 101, + 114, + 34, + 58, + 34, + 112, + 111, + 119, + 101, + 114, + 115, + 104, + 101, + 108, + 108, + 34, + 44, + 34, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 117, + 116, + 102, + 56, + 34, + 58, + 34, + 87, + 114, + 105, + 116, + 101, + 45, + 79, + 117, + 116, + 112, + 117, + 116, + 32, + 39, + 83, + 89, + 83, + 84, + 69, + 77, + 32, + 115, + 116, + 100, + 111, + 117, + 116, + 39, + 92, + 114, + 92, + 110, + 91, + 67, + 111, + 110, + 115, + 111, + 108, + 101, + 93, + 58, + 58, + 69, + 114, + 114, + 111, + 114, + 46, + 87, + 114, + 105, + 116, + 101, + 76, + 105, + 110, + 101, + 40, + 39, + 83, + 89, + 83, + 84, + 69, + 77, + 32, + 115, + 116, + 100, + 101, + 114, + 114, + 39, + 41, + 92, + 114, + 92, + 110, + 34, + 44, + 34, + 97, + 114, + 103, + 117, + 109, + 101, + 110, + 116, + 115, + 34, + 58, + 91, + 93, + 44, + 34, + 101, + 120, + 112, + 101, + 99, + 116, + 101, + 100, + 95, + 115, + 116, + 100, + 111, + 117, + 116, + 34, + 58, + 110, + 117, + 108, + 108, + 44, + 34, + 119, + 105, + 110, + 100, + 111, + 119, + 115, + 95, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 95, + 112, + 114, + 111, + 102, + 105, + 108, + 101, + 34, + 58, + 34, + 112, + 111, + 119, + 101, + 114, + 115, + 104, + 101, + 108, + 108, + 95, + 53, + 49, + 95, + 114, + 101, + 109, + 111, + 116, + 101, + 95, + 115, + 105, + 103, + 110, + 101, + 100, + 34, + 44, + 34, + 112, + 114, + 105, + 118, + 105, + 108, + 101, + 103, + 101, + 100, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 112, + 114, + 111, + 102, + 105, + 108, + 101, + 34, + 58, + 34, + 102, + 117, + 108, + 108, + 95, + 109, + 97, + 105, + 110, + 116, + 101, + 110, + 97, + 110, + 99, + 101, + 95, + 118, + 49, + 34, + 125, + 44, + 34, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 95, + 105, + 100, + 101, + 110, + 116, + 105, + 116, + 121, + 34, + 58, + 34, + 115, + 121, + 115, + 116, + 101, + 109, + 34, + 44, + 34, + 110, + 111, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 50, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 115, + 116, + 97, + 114, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 51, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 109, + 97, + 120, + 95, + 114, + 117, + 110, + 116, + 105, + 109, + 101, + 95, + 115, + 101, + 99, + 111, + 110, + 100, + 115, + 34, + 58, + 51, + 48, + 44, + 34, + 109, + 97, + 120, + 95, + 111, + 117, + 116, + 112, + 117, + 116, + 95, + 98, + 121, + 116, + 101, + 115, + 34, + 58, + 52, + 48, + 57, + 54, + 125 + ], + "job_payload_hash": "e38450ee1867adcd2fff64e643efdb5bb8e9bb6a3a65a71e7c8383daaaef3119", + "name": "windows_system_full", + "plan_hash": "0942efafd091d12e4f5a2798e52d9b294f7276536cd31d43a0b7ddada2ffc8f4", + "plan_payload_bytes": [ + 123, + 34, + 115, + 99, + 104, + 101, + 109, + 97, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 112, + 108, + 97, + 110, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 97, + 34, + 44, + 34, + 116, + 101, + 110, + 97, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 34, + 44, + 34, + 112, + 114, + 105, + 110, + 99, + 105, + 112, + 97, + 108, + 34, + 58, + 34, + 115, + 105, + 109, + 117, + 108, + 97, + 116, + 101, + 100, + 45, + 100, + 101, + 118, + 45, + 115, + 99, + 114, + 105, + 112, + 116, + 45, + 99, + 111, + 110, + 115, + 111, + 108, + 101, + 34, + 44, + 34, + 114, + 101, + 97, + 115, + 111, + 110, + 34, + 58, + 34, + 67, + 111, + 100, + 101, + 99, + 32, + 102, + 105, + 120, + 116, + 117, + 114, + 101, + 58, + 32, + 119, + 105, + 110, + 100, + 111, + 119, + 115, + 95, + 115, + 121, + 115, + 116, + 101, + 109, + 95, + 102, + 117, + 108, + 108, + 59, + 32, + 110, + 111, + 32, + 110, + 97, + 116, + 105, + 118, + 101, + 32, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 34, + 44, + 34, + 106, + 111, + 98, + 115, + 34, + 58, + 91, + 123, + 34, + 112, + 97, + 121, + 108, + 111, + 97, + 100, + 34, + 58, + 123, + 34, + 115, + 99, + 104, + 101, + 109, + 97, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 116, + 101, + 110, + 97, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 34, + 44, + 34, + 97, + 115, + 115, + 101, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 50, + 34, + 44, + 34, + 97, + 103, + 101, + 110, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 51, + 34, + 44, + 34, + 101, + 110, + 114, + 111, + 108, + 108, + 109, + 101, + 110, + 116, + 95, + 103, + 101, + 110, + 101, + 114, + 97, + 116, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 97, + 115, + 115, + 105, + 103, + 110, + 109, + 101, + 110, + 116, + 95, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 49, + 44, + 34, + 106, + 111, + 98, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 56, + 34, + 44, + 34, + 97, + 116, + 116, + 101, + 109, + 112, + 116, + 95, + 105, + 100, + 34, + 58, + 34, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 45, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 49, + 57, + 34, + 44, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 123, + 34, + 97, + 99, + 116, + 105, + 111, + 110, + 34, + 58, + 34, + 114, + 117, + 110, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 34, + 44, + 34, + 105, + 110, + 116, + 101, + 114, + 112, + 114, + 101, + 116, + 101, + 114, + 34, + 58, + 34, + 112, + 111, + 119, + 101, + 114, + 115, + 104, + 101, + 108, + 108, + 34, + 44, + 34, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 117, + 116, + 102, + 56, + 34, + 58, + 34, + 87, + 114, + 105, + 116, + 101, + 45, + 79, + 117, + 116, + 112, + 117, + 116, + 32, + 39, + 83, + 89, + 83, + 84, + 69, + 77, + 32, + 115, + 116, + 100, + 111, + 117, + 116, + 39, + 92, + 114, + 92, + 110, + 91, + 67, + 111, + 110, + 115, + 111, + 108, + 101, + 93, + 58, + 58, + 69, + 114, + 114, + 111, + 114, + 46, + 87, + 114, + 105, + 116, + 101, + 76, + 105, + 110, + 101, + 40, + 39, + 83, + 89, + 83, + 84, + 69, + 77, + 32, + 115, + 116, + 100, + 101, + 114, + 114, + 39, + 41, + 92, + 114, + 92, + 110, + 34, + 44, + 34, + 97, + 114, + 103, + 117, + 109, + 101, + 110, + 116, + 115, + 34, + 58, + 91, + 93, + 44, + 34, + 101, + 120, + 112, + 101, + 99, + 116, + 101, + 100, + 95, + 115, + 116, + 100, + 111, + 117, + 116, + 34, + 58, + 110, + 117, + 108, + 108, + 44, + 34, + 119, + 105, + 110, + 100, + 111, + 119, + 115, + 95, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 95, + 112, + 114, + 111, + 102, + 105, + 108, + 101, + 34, + 58, + 34, + 112, + 111, + 119, + 101, + 114, + 115, + 104, + 101, + 108, + 108, + 95, + 53, + 49, + 95, + 114, + 101, + 109, + 111, + 116, + 101, + 95, + 115, + 105, + 103, + 110, + 101, + 100, + 34, + 44, + 34, + 112, + 114, + 105, + 118, + 105, + 108, + 101, + 103, + 101, + 100, + 95, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 112, + 114, + 111, + 102, + 105, + 108, + 101, + 34, + 58, + 34, + 102, + 117, + 108, + 108, + 95, + 109, + 97, + 105, + 110, + 116, + 101, + 110, + 97, + 110, + 99, + 101, + 95, + 118, + 49, + 34, + 125, + 44, + 34, + 101, + 120, + 101, + 99, + 117, + 116, + 105, + 111, + 110, + 95, + 105, + 100, + 101, + 110, + 116, + 105, + 116, + 121, + 34, + 58, + 34, + 115, + 121, + 115, + 116, + 101, + 109, + 34, + 44, + 34, + 110, + 111, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 50, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 115, + 116, + 97, + 114, + 116, + 95, + 98, + 101, + 102, + 111, + 114, + 101, + 34, + 58, + 34, + 50, + 48, + 50, + 54, + 45, + 49, + 49, + 45, + 49, + 52, + 84, + 49, + 51, + 58, + 48, + 48, + 58, + 48, + 48, + 90, + 34, + 44, + 34, + 109, + 97, + 120, + 95, + 114, + 117, + 110, + 116, + 105, + 109, + 101, + 95, + 115, + 101, + 99, + 111, + 110, + 100, + 115, + 34, + 58, + 51, + 48, + 44, + 34, + 109, + 97, + 120, + 95, + 111, + 117, + 116, + 112, + 117, + 116, + 95, + 98, + 121, + 116, + 101, + 115, + 34, + 58, + 52, + 48, + 57, + 54, + 125, + 44, + 34, + 106, + 111, + 98, + 95, + 112, + 97, + 121, + 108, + 111, + 97, + 100, + 95, + 104, + 97, + 115, + 104, + 34, + 58, + 34, + 101, + 51, + 56, + 52, + 53, + 48, + 101, + 101, + 49, + 56, + 54, + 55, + 97, + 100, + 99, + 100, + 50, + 102, + 102, + 102, + 54, + 52, + 101, + 54, + 52, + 51, + 101, + 102, + 100, + 98, + 53, + 98, + 98, + 56, + 101, + 57, + 98, + 98, + 54, + 97, + 51, + 97, + 54, + 53, + 97, + 55, + 49, + 101, + 55, + 99, + 56, + 51, + 56, + 51, + 100, + 97, + 97, + 97, + 101, + 102, + 51, + 49, + 49, + 57, + 34, + 44, + 34, + 115, + 99, + 114, + 105, + 112, + 116, + 95, + 115, + 104, + 97, + 50, + 53, + 54, + 34, + 58, + 34, + 54, + 57, + 97, + 57, + 55, + 49, + 97, + 53, + 54, + 98, + 57, + 100, + 56, + 50, + 56, + 51, + 100, + 101, + 51, + 50, + 50, + 102, + 50, + 50, + 100, + 98, + 48, + 55, + 101, + 48, + 55, + 57, + 48, + 52, + 50, + 49, + 53, + 97, + 49, + 98, + 55, + 54, + 102, + 49, + 100, + 98, + 102, + 98, + 97, + 98, + 50, + 52, + 101, + 55, + 56, + 51, + 98, + 57, + 55, + 99, + 50, + 100, + 48, + 56, + 34, + 125, + 93, + 44, + 34, + 109, + 97, + 120, + 95, + 112, + 97, + 114, + 97, + 108, + 108, + 101, + 108, + 34, + 58, + 49, + 44, + 34, + 104, + 97, + 108, + 116, + 95, + 97, + 102, + 116, + 101, + 114, + 95, + 102, + 97, + 105, + 108, + 117, + 114, + 101, + 115, + 34, + 58, + 49, + 125 + ], + "script_sha256": "69a971a56b9d8283de322f22db07e07904215a1b76f1dbfbab24e783b97c2d08" + } +] diff --git a/web/README.md b/web/README.md index 102570f..1a75a1e 100644 --- a/web/README.md +++ b/web/README.md @@ -1,6 +1,6 @@ # Linux-MVP WebUI -React/TypeScript, no runtime demo data, no desktop/terminal/hypervisor integration. +React/TypeScript, no runtime demo data, no desktop/PTY/hypervisor integration. Build assets: **`web/dist/`**; serve `index.html` and `/assets/*` from the TLS backend on the same origin. Do not proxy authentication to a different origin. All API URLs are relative; cookies use `credentials: same-origin`, mutations use `X-CSRF-Token`. @@ -10,8 +10,64 @@ No token or password persistence, raw HTML rendering, automatic approval or retr From `web/`: `npm ci`, `npm run typecheck`, `npm test`, `npm run build`. `npm run dev` binds loopback, but is not the authenticated TLS acceptance setup. -The authoritative runtime is the backend serving `web/dist` at -`https://rmm-dev.internal:18443`. No certificate-verification bypass is supported. +The current server is `discovery.ease-technologies.de`; management access uses the +configured SSH tunnel and verified TLS. Earlier local `rmm-dev.internal:18443` +examples below are historical. The notebook is development only, without an RMM +server or enrolled agent. No certificate-verification bypass is supported. + +## Script console (specification 1.20, source implementation) + +`run_script` remains a durable plan/request, not a PTY or interactive terminal. +The planner accepts complete fixed POSIX/Bash or Windows PowerShell5.1 scripts. +The optional action-wide `parameters.privileged_script_profile` is exactly +`full_maintenance_v1`; missing preserves the existing bounded mode, while present +null/unknown values fail. Full maintenance requires root on Linux or SYSTEM on +Windows, plus `scripts:execute` and the separate root/system scope. SYSTEM scripts +require the explicit profile. The UI selection grants no permission: central/local +opt-in, target capabilities, independent helper checks and actual WebUI approval +remain necessary. Full authority includes filesystem, network and services, and +can alter the agent/PKI or cause a reboot. It has no sandbox/no-reboot promise. +The profile budgets are Linux 1–300s, Windows 1–120s, at most 32768 script bytes +and 65536 output bytes. RemoteSigned, when explicitly bound, remains limited to +the approved Windows job process. + +Before submit/approval, `src/script-console.ts` projects every script from the +SHA256-verified stored plan bytes. It checks full payload structure, fixed +target/job/attempt IDs, script SHA256 and the job SHA256 reconstructed in Rust +declaration order. Every supplied payload/target alias must agree. It snapshots +the checked views, rejects later mutation and rechecks immediately before each +submit/approval action. The displayed script, arguments, identity, profiles, +budgets, deadlines and hashes come from that projection, never the draft editor. +Control characters, CR, BOM and bidi controls are made visible in the text view; +the full original JSON and stored bytes remain available. Existing pending-only +submission and interactivity/CSRF checks remain in force. + +`src/console-output.tsx` displays the actual stored job status and final stdout/ +stderr. Status polling is every four seconds; output arrives with the durable +result. There is no incremental output stream. Null result, empty output, +unknown exit code, unknown outcome, requested cancellation and truncation are +distinct. UTF-8 is a display attempt, not a claimed source encoding; invalid +UTF-8 is marked and original bytes can be downloaded as inert `.bin` files. +Terminal escapes and bidi controls are visible, never interpreted. Agent output +is React text and cannot create HTML, approval controls or follow-up jobs. + +MCP already dispatches plan/submit/read-results to the same server application +functions. Shared PlanRequest/Action schemas expose the profile; MCP has no +approval tool and its bearer context cannot become an interactive approval +session. No independent MCP execution or approval path was added. + +Local typechecking and renderer tests are source evidence only. Native Linux/ +Windows runs, deployment and actual UI/MCP acceptance remain separate gates. +Actually executed on 2026-11-14 for this source slice: `npm ci --ignore-scripts +--no-audit --no-fund`, `npm run typecheck`, `npm test` (49 passed across seven +files), `npm run build` (TypeScript and Vite passed). Five script-console tests +read `../tests/script_console/codec-golden.json` directly: the same three actual +Rust codec families cover legacy Linux agent, Linux root/full and Windows +SYSTEM/full with RemoteSigned. Checks cover full displayed script/target/attempt/ +profile/budgets/hashes, all aliases, mutation during/after verification and +independent scopes. Two output tests cover hostile HTML/terminal/bidi bytes, +unknown/cancellation/truncation, invalid encodings and absent/invalid results. +No browser, server, VM job, approval or deployment was executed in these checks. ## JSON adapter contract @@ -70,9 +126,10 @@ All business endpoints are under `/api/v1/tenants/{tenant}`: asset_id can also come from the exact payload. Every target requires a nonempty exact payload and payload hash; preview coverage must equal the fixed target set, duplicate asset IDs and contradictory payload asset IDs are rejected. - `job_id`/`attempt_id` are **not prerequisites** for planning, submission or approval: - if present they are optional fixed plan references, not proof of executable jobs - or persisted attempts. Actual jobs/attempts are created only after approval. + Script plans require the reserved `job_id`/`attempt_id` from the exact stored + payload, as well as matching script/job hashes. These are fixed definitions, + not proof of executable jobs or persisted attempts. Actual execution rows are + created only after approval. Exact payload metadata and script/package hashes must be included. UI does not recompute the plan hash from JSON. Missing fixed targets/previews/payload/hash or excluded targets block submission and approval. All fields are shown, not just diff --git a/web/src/api.test.ts b/web/src/api.test.ts index f87eded..2ccda1d 100644 --- a/web/src/api.test.ts +++ b/web/src/api.test.ts @@ -65,6 +65,24 @@ describe('JSON contract boundary', () => { expect(() => validateInput({ ...input, parameters: { ...input.parameters, execution_identity: 'root' } })).toThrow('agent_service'); validateInput({ ...input, parameters: { ...input.parameters, variants: [{ ...windows, script_utf8: "\ufeffWrite-Output 'é'\r\n" }] } }); }); + it('bounds the fixed Event Log source and exact 24h interval without rewriting Linux queries', () => { + const base: PlanInput = { tenant_id: 't', action: 'query_logs', targets: { asset_ids: ['a'] }, parameters: { execution_identity: 'agent_service', unit: 'windows.eventlog.application.rustdiscovery-mvp-test', since: '2026-11-14T00:00:00Z', until: '2026-11-15T00:00:00Z', max_lines: 1000, timeout_seconds: 30, max_output_bytes: 4096 }, schedule: { not_before: new Date().toISOString(), start_before: new Date(Date.now()+60000).toISOString(), offline_policy: 'wait_until_deadline' }, rollout: { max_parallel: 1, halt_after_failures: 1 }, reason: 'fixed fixture logs' }; + const before = JSON.stringify(base); validateInput(base); expect(JSON.stringify(base)).toBe(before); + for (const change of [{ execution_identity: 'root' }, { execution_identity: 'system' }, { unit: 'windows.eventlog.security.other' }, { timeout_seconds: 9 }, { timeout_seconds: 121 }, { max_output_bytes: 1023 }, { max_output_bytes: 65537 }, { max_lines: 1001 }, { until: '2026-11-15T00:00:00.000000001Z' }]) expect(() => validateInput({ ...base, parameters: { ...base.parameters, ...change } })).toThrow(); + const linux = { ...base, parameters: { ...base.parameters, unit: 'rmm-test.service', execution_identity: 'root', timeout_seconds: 1, max_output_bytes: 512 } }; const original=JSON.stringify(linux); validateInput(linux); expect(JSON.stringify(linux)).toBe(original); + expect(() => validateInput({ ...linux, parameters: { ...linux.parameters, until: '2026-11-15T00:59:00Z' } })).toThrow('24Stunden'); + }); + it('accepts only the fixed Windows process profile without changing inherited variants', () => { + const windows = { os_family: 'windows', interpreter: 'powershell', encoding: 'utf-8', script_utf8: "Write-Output 'fixed'\r\n" }; + const base: PlanInput = { tenant_id: 't', action: 'run_script', targets: { asset_ids: ['a'] }, parameters: { variants: [windows], execution_identity: 'agent_service', timeout_seconds: 30, max_output_bytes: 4096 }, schedule: { not_before: new Date().toISOString(), start_before: new Date(Date.now() + 60000).toISOString(), offline_policy: 'wait_until_deadline' }, rollout: { max_parallel: 1, halt_after_failures: 1 }, reason: 'fixed profile fixture' }; + const original = JSON.stringify(base); validateInput(base); expect(JSON.stringify(base)).toBe(original); + const profiled = { ...windows, windows_execution_profile: 'powershell_51_remote_signed' }; + validateInput({ ...base, parameters: { ...base.parameters, variants: [profiled] } }); + expect(() => validateInput({ ...base, parameters: { ...base.parameters, windows_execution_profile: 'powershell_51_remote_signed' } })).toThrow('Skriptvariante'); + for (const windows_execution_profile of ['Bypass', 'Unrestricted', 'RemoteSigned', 'inherited', null, {}]) expect(() => validateInput({ ...base, parameters: { ...base.parameters, variants: [{ ...windows, windows_execution_profile }] } })).toThrow('Windows-Ausführungsprofil'); + for (const variant of [{ ...profiled, os_family: 'linux' }, { ...profiled, interpreter: 'bash' }, { ...profiled, interpreter: 'pwsh' }]) expect(() => validateInput({ ...base, parameters: { ...base.parameters, variants: [variant] } })).toThrow(); + for (const execution_identity of ['root', 'system']) expect(() => validateInput({ ...base, parameters: { ...base.parameters, execution_identity, variants: [profiled] } })).toThrow('agent_service'); + }); it('allows SYSTEM only for exact bounded fixture SCM parameters without changing Linux restart', () => { const input: PlanInput = { tenant_id: 't', action: 'set_service_state', targets: { asset_ids: ['asset'] }, parameters: { execution_identity: 'system', unit: 'RmmDiscoveryFixtureService', state: 'running', timeout_seconds: 30, max_output_bytes: 4096 }, schedule: { not_before: new Date().toISOString(), start_before: new Date(Date.now() + 60000).toISOString(), offline_policy: 'wait_until_deadline' }, rollout: { max_parallel: 1, halt_after_failures: 1 }, reason: 'fixed SCM plan' }; const before = JSON.stringify(input); validateInput(input); expect(JSON.stringify(input)).toBe(before); diff --git a/web/src/api.ts b/web/src/api.ts index 276712e..64e2d6e 100644 --- a/web/src/api.ts +++ b/web/src/api.ts @@ -59,7 +59,108 @@ export const pretty = (v: unknown) => JSON.stringify(v, null, 2); export const actions = ['run_script', 'refresh_inventory', 'query_logs', 'set_service_state', 'ensure_package', 'apply_updates', 'reboot', 'apt_native_acceptance'] as const; export type Action = typeof actions[number]; +export const windowsPowerShellRemoteSignedProfile = 'powershell_51_remote_signed' as const; +export const fullMaintenanceScriptProfile = 'full_maintenance_v1' as const; +export function privilegedScriptProfile(value: RecordValue, identity: unknown): 'bounded' | typeof fullMaintenanceScriptProfile { + if (!Object.hasOwn(value, 'privileged_script_profile')) return 'bounded'; + if (value.privileged_script_profile !== fullMaintenanceScriptProfile || !['root', 'system'].includes(String(identity))) throw new Error('Vollwartungsprofil ausschließlich full_maintenance_v1 mit root/system; kein null oder geerbtes Privileg'); + return fullMaintenanceScriptProfile; +} +export function validateScriptIdentityProfile(value: RecordValue, identity: unknown, interpreter: unknown): void { + const profile = privilegedScriptProfile(value, identity); + if (interpreter === 'powershell') { + if (identity !== 'agent_service' && !(identity === 'system' && profile === fullMaintenanceScriptProfile)) throw new Error('PowerShell nur agent_service oder ausdrücklich SYSTEM/full_maintenance_v1'); + } else if (!['posix_sh', 'bash'].includes(String(interpreter)) || !['agent_service', 'root'].includes(String(identity))) throw new Error('Linux-Skript benötigt agent_service/root und festen Shellinterpreter'); +} +/** A missing field is inherited policy; a present field is never silently coerced. */ +export function windowsScriptProfile(value: RecordValue): 'inherited' | typeof windowsPowerShellRemoteSignedProfile { + if (!Object.prototype.hasOwnProperty.call(value, 'windows_execution_profile')) return 'inherited'; + if (value.windows_execution_profile !== windowsPowerShellRemoteSignedProfile || value.interpreter !== 'powershell' + || (value.os_family !== undefined && value.os_family !== 'windows')) throw new Error('Windows-Ausführungsprofil nur powershell_51_remote_signed für Windows PowerShell5.1'); + return windowsPowerShellRemoteSignedProfile; +} +export const windowsEventLogFixtureSource = 'windows.eventlog.application.rustdiscovery-mvp-test'; +export const windowsEventLogFixtureChannel = 'Application'; +export const windowsEventLogFixtureProvider = 'RustDiscovery-MVP-Test'; +/** Preserve sub-millisecond bounds; Date.parse alone would admit 24h + 1ns. */ +export function logWindow(since: unknown, until: unknown): void { + const instant = (value: unknown): bigint => { + if (typeof value !== 'string') throw new Error('Exaktes UTC-Log-Zeitfenster erforderlich'); + const match = /^(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)(?:\.(\d{1,9}))?(Z|[+-]\d\d:\d\d)$/.exec(value); + if (!match) throw new Error('Exaktes UTC-Log-Zeitfenster erforderlich'); + const seconds = Date.parse(match[1]! + match[3]!); + if (!Number.isFinite(seconds)) throw new Error('Exaktes UTC-Log-Zeitfenster erforderlich'); + return BigInt(seconds) * 1000000n + BigInt((match[2] ?? '').padEnd(9, '0')); + }; + const start = instant(since), end = instant(until); + if (end <= start || end - start > 86400000000000n) throw new Error('Exaktes Log-Zeitfenster höchstens24Stunden erforderlich'); +} +export function validateWindowsLogLimits(identity: unknown, runtime: unknown, output: unknown, since: unknown): void { + if (identity !== 'agent_service' || typeof runtime !== 'number' || !Number.isSafeInteger(runtime) || runtime < 10 || runtime > 120 + || typeof output !== 'number' || !Number.isSafeInteger(output) || output < 1024 || output > 65536 + || typeof since !== 'string' || Date.parse(since) < 0) throw new Error('Windows-Fixture-Eventlog nur agent_service,10–120s und1024–65536Bytes'); +} export interface PlanInput { tenant_id: string; action: Action; targets: { asset_ids: string[] }; parameters: RecordValue; artifact_ids?: string[]; schedule: { not_before: string; start_before: string; offline_policy: 'wait_until_deadline' }; rollout: { max_parallel: number; halt_after_failures: number }; reason: string } +export const windowsMsi = { + package: 'rmm-windows-msi-fixture', installRecipe: 'rmm-windows-msi-fixture-install-1.0', upgradeRecipe: 'rmm-windows-msi-fixture-upgrade-1.1', + product: '{3F070B4B-9C08-4D65-BD41-13D81B884AEC}', upgradedProduct: '{D42BD387-079B-4BBF-A134-0DA7B7A97011}', family: '{C9BF1FB8-E31D-4BD7-BA0D-AB46539BBBAA}', + payload: 'C:\\Program Files\\RustDiscoveryFixtures\\msi-payload.txt', payload10: '0c90c4fc7233017cc88c0e925095d6662ac57abdb303c7527ef59d02b454fca0', payload11: 'ba0c83deff9eed014009bcd45369b2a356ae909ebf0c1434dc836b0e6d759c49', +} as const; +export interface MsiPostcondition { product_code: string; product_version: string; payload_path: string; payload_sha256: string; payload_bytes: number } +export interface WindowsMsiRecipe { + schema_version: 1; recipe_id: string; recipe_version: '1'; platform: 'windows'; upgrade_code: string; architecture: 'x64'; context: 'machine'; command_profile: 'fixture_quiet_no_reboot_v1'; + artifact: { artifact_id: string; sha256: string; bytes: number }; postcondition: MsiPostcondition; no_reboot: true; package_code?: string; predecessor?: MsiPostcondition; +} +export interface WindowsMsiManifest { recipe: WindowsMsiRecipe; recipe_sha256: string } +function exactMsiFields(value: RecordValue, fields: string[]): void { + if (Object.keys(value).some(key => !fields.includes(key)) || fields.some(key => !Object.prototype.hasOwnProperty.call(value, key))) throw new Error('MSI-Schemafelder fehlen oder sind unbekannt'); +} +export function msiDigest(value: unknown): string { if (typeof value !== 'string' || !/^[a-f0-9]{64}$/.test(value)) throw new Error('Exakter MSI-SHA256 erforderlich'); return value; } +export function msiUuid(value: unknown): string { if (typeof value !== 'string' || !/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(value) || value === '00000000-0000-0000-0000-000000000000') throw new Error('Exakte MSI-UUID erforderlich'); return value; } +function msiInteger(value: unknown, min: number, max: number): number { if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < min || value > max) throw new Error('MSI-Limit außerhalb des festen Budgets'); return value; } +/** Reconstruct every nested object in Rust declaration order before hashing. + * No unknown properties, null options, enum objects or caller-supplied argv. + */ +export function windowsMsiManifest(value: unknown): WindowsMsiManifest { + const m = record(value, 'windows_msi_manifest'); exactMsiFields(m, ['recipe', 'recipe_sha256']); + const r = record(m.recipe, 'MSI-Rezept'), upgrade = r.recipe_id === windowsMsi.upgradeRecipe; + exactMsiFields(r, ['schema_version', 'recipe_id', 'recipe_version', 'platform', 'upgrade_code', 'architecture', 'context', 'command_profile', 'artifact', 'postcondition', 'no_reboot', ...(upgrade ? ['package_code', 'predecessor'] : [])]); + if (r.schema_version !== 1 || (!upgrade && r.recipe_id !== windowsMsi.installRecipe) || r.recipe_version !== '1' || r.platform !== 'windows' || r.upgrade_code !== windowsMsi.family || r.architecture !== 'x64' || r.context !== 'machine' || r.command_profile !== 'fixture_quiet_no_reboot_v1' || r.no_reboot !== true) throw new Error('Nur feste Windows-Machine/x64-MSI-Rezepte mit No-Reboot-Vertrag'); + const a = record(r.artifact); exactMsiFields(a, ['artifact_id', 'sha256', 'bytes']); + const post = (value: unknown): MsiPostcondition => { const p = record(value); exactMsiFields(p, ['product_code', 'product_version', 'payload_path', 'payload_sha256', 'payload_bytes']); + if (p.payload_path !== windowsMsi.payload) throw new Error('Exakter MSI-Nutzdateipfad erforderlich'); + return { product_code: text(p.product_code, 'ProductCode'), product_version: text(p.product_version, 'Produktversion'), payload_path: p.payload_path, payload_sha256: msiDigest(p.payload_sha256), payload_bytes: msiInteger(p.payload_bytes, 1, 65536) }; + }; + const p = post(r.postcondition); + if (p.product_code !== (upgrade ? windowsMsi.upgradedProduct : windowsMsi.product) || p.product_version !== (upgrade ? '1.1.0' : '1.0.0')) throw new Error('MSI-Produkt/Version widerspricht festem Rezept'); + const recipe: WindowsMsiRecipe = { schema_version: 1, recipe_id: r.recipe_id as string, recipe_version: '1', platform: 'windows', upgrade_code: windowsMsi.family, architecture: 'x64', context: 'machine', command_profile: 'fixture_quiet_no_reboot_v1', + artifact: { artifact_id: msiUuid(a.artifact_id), sha256: msiDigest(a.sha256), bytes: msiInteger(a.bytes, 1, 262144) }, postcondition: p, no_reboot: true }; + if (upgrade) { + const code = text(r.package_code, 'PackageCode'); + if (!/^\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}$/.test(code) || code === '{00000000-0000-0000-0000-000000000000}' || [windowsMsi.product, windowsMsi.upgradedProduct, windowsMsi.family].some(x => x === code)) throw new Error('Exakter eigener MSI-PackageCode erforderlich'); + const old = post(r.predecessor); + if (p.payload_sha256 !== windowsMsi.payload11 || p.payload_bytes !== 33 || old.product_code !== windowsMsi.product || old.product_version !== '1.0.0' || old.payload_sha256 !== windowsMsi.payload10 || old.payload_bytes !== 33) throw new Error('MSI1.1 benötigt exakt den bekannten1.0-Vorgänger und die feste1.1-Nutzdatei'); + recipe.package_code = code; recipe.predecessor = old; + } + return { recipe, recipe_sha256: msiDigest(m.recipe_sha256) }; +} +export function validateWindowsMsiLimits(runtime: unknown, output: unknown): void { msiInteger(runtime, 10, 120); msiInteger(output, 1024, 65536); } +/** Stored Action bytes omit apt_manifest=None; WebParameters may accept null. */ +export function windowsMsiAction(value: RecordValue): RecordValue { + exactMsiFields(value, ['action', 'provider', 'package', 'version', 'architecture', 'recipe_version', 'installation_context', 'recipe_id', 'windows_msi_manifest']); + const m = windowsMsiManifest(value.windows_msi_manifest); + if (value.action !== 'ensure_package' || value.provider !== 'msi' || value.package !== windowsMsi.package || value.version !== m.recipe.postcondition.product_version || value.architecture !== 'x64' || value.recipe_version !== m.recipe.recipe_version || value.installation_context !== 'machine' || value.recipe_id !== m.recipe.recipe_id) throw new Error('Äußere MSI-Felder widersprechen Manifest'); + return { action: 'ensure_package', provider: 'msi', package: windowsMsi.package, version: value.version, architecture: 'x64', recipe_version: m.recipe.recipe_version, installation_context: 'machine', recipe_id: m.recipe.recipe_id, windows_msi_manifest: m }; +} +export function validateWindowsMsiInput(input: PlanInput): WindowsMsiManifest { + const p = input.parameters; + exactMsiFields(p, ['execution_identity', 'timeout_seconds', 'max_output_bytes', 'provider', 'package', 'version', 'architecture', 'recipe_version', 'installation_context', 'recipe_id', 'windows_msi_manifest', ...(Object.prototype.hasOwnProperty.call(p, 'apt_manifest') ? ['apt_manifest'] : [])]); + if (input.action !== 'ensure_package' || p.execution_identity !== 'system' || (input.artifact_ids?.length ?? 0) !== 0) throw new Error('MSI ausschließlich als SYSTEM-ensure_package mit automatisch genau einem Artefakt'); + validateWindowsMsiLimits(p.timeout_seconds, p.max_output_bytes); + if (p.apt_manifest !== undefined && p.apt_manifest !== null) throw new Error('APT-Manifest bei MSI nicht zulässig'); + windowsMsiAction({ action: input.action, provider: p.provider, package: p.package, version: p.version, architecture: p.architecture, recipe_version: p.recipe_version, installation_context: p.installation_context, recipe_id: p.recipe_id, windows_msi_manifest: p.windows_msi_manifest }); + return windowsMsiManifest(p.windows_msi_manifest); +} /** Decode explicit escaped CRLF before planning; never alter approved output bytes. */ export function expectedStdout(value: string, format: string): string | null { if (value === '') return null; @@ -68,32 +169,49 @@ export function expectedStdout(value: string, format: string): string | null { return output; } export function validateInput(input: PlanInput): void { + if (Object.hasOwn(input.parameters, 'privileged_script_profile') && input.action !== 'run_script') throw new Error('Vollwartungsprofil ausschließlich für run_script'); + if (Object.prototype.hasOwnProperty.call(input.parameters, 'windows_execution_profile')) throw new Error('Windows-Ausführungsprofil muss an der festen Windows-Skriptvariante stehen'); if (!input.targets.asset_ids.length || new Set(input.targets.asset_ids).size !== input.targets.asset_ids.length) throw new Error('Feste eindeutige Ziele erforderlich'); if (!input.reason.trim()) throw new Error('Begründung erforderlich'); const start = Date.parse(input.schedule.not_before), end = Date.parse(input.schedule.start_before); if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start || end <= Date.now()) throw new Error('Ungültige oder abgelaufene Frist'); for (const n of [input.rollout.max_parallel, input.rollout.halt_after_failures, input.parameters.timeout_seconds, input.parameters.max_output_bytes]) if (typeof n !== 'number' || !Number.isSafeInteger(n) || n < 1) throw new Error('Limits müssen positive Ganzzahlen sein'); if (input.action === 'run_script') { + const profile = privilegedScriptProfile(input.parameters, input.parameters.execution_identity); const variants = input.parameters.variants; if (!Array.isArray(variants) || !variants.length || variants.length > 2 || !variants.every(v => { const variant = record(v), script = typeof variant.script_utf8 === 'string' ? variant.script_utf8 : ''; + if (Object.hasOwn(variant, 'privileged_script_profile')) throw new Error('Privilegiertes Skriptprofil muss aktionsweit in parameters stehen'); + windowsScriptProfile(variant); + validateScriptIdentityProfile(input.parameters, input.parameters.execution_identity, variant.interpreter); const bytes = new TextEncoder().encode(script).length; const platform = variant.os_family === 'linux' && ['posix_sh', 'bash'].includes(String(variant.interpreter)) || variant.os_family === 'windows' && variant.interpreter === 'powershell'; return bytes > 0 && bytes <= 32768 && !script.includes('\0') && variant.encoding === 'utf-8' && platform && (variant.os_family !== 'windows' || /^[\x00-\x7f]*$/.test(script) || script.startsWith('\ufeff')); }) || new Set(variants.map(v => record(v).os_family)).size !== variants.length) throw new Error('Je Plattform genau eine passende UTF-8 Variante (max.32KiB); PowerShell5.1: ASCII oder explizites UTF-8-BOM vor Hashbildung'); - if (variants.some(v => record(v).os_family === 'windows') && input.parameters.execution_identity !== 'agent_service') throw new Error('Windows PowerShell benötigt agent_service; kein SYSTEM-/Root-Fallback'); + if (profile === fullMaintenanceScriptProfile && (Number(input.parameters.timeout_seconds) > (variants.some(v => record(v).os_family === 'windows') ? 120 : 300) || Number(input.parameters.max_output_bytes) > 65536)) throw new Error('Vollwartung: Linux höchstens300s / Windows höchstens120s und65536Ausgabebytes'); } if (!['agent_service', 'root', 'system'].includes(String(input.parameters.execution_identity))) throw new Error('Explizite Ausführungsidentität erforderlich'); if (input.parameters.execution_identity === 'system') { - if (input.action !== 'set_service_state') throw new Error('SYSTEM ausschließlich für typisierte eigene Windows-Fixturedienste; keine SYSTEM-Shell'); - validateSystemService(input.parameters.unit, input.parameters.state, input.parameters.timeout_seconds, input.parameters.max_output_bytes); - if (Object.keys(input.parameters).some(k => !['execution_identity', 'unit', 'state', 'timeout_seconds', 'max_output_bytes'].includes(k))) throw new Error('SYSTEM-Dienstauftrag enthält fremde Parameter'); + if (input.action === 'run_script') { + if (privilegedScriptProfile(input.parameters, 'system') !== fullMaintenanceScriptProfile) throw new Error('SYSTEM-Skript benötigt ausdrücklich full_maintenance_v1'); + } else if (input.action === 'ensure_package') validateWindowsMsiInput(input); + else { + if (input.action !== 'set_service_state') throw new Error('SYSTEM ausschließlich für typisierte eigene Windows-Fixturedienste oder feste MSI-Rezepte; keine SYSTEM-Shell'); + validateSystemService(input.parameters.unit, input.parameters.state, input.parameters.timeout_seconds, input.parameters.max_output_bytes); + if (Object.keys(input.parameters).some(k => !['execution_identity', 'unit', 'state', 'timeout_seconds', 'max_output_bytes'].includes(k))) throw new Error('SYSTEM-Dienstauftrag enthält fremde Parameter'); + } } + if (input.parameters.provider === 'msi' || Object.prototype.hasOwnProperty.call(input.parameters, 'windows_msi_manifest')) validateWindowsMsiInput(input); const required = (key: string) => text(input.parameters[key], `parameters.${key}`); if (input.action === 'refresh_inventory') required('scope'); - if (input.action === 'query_logs') { required('unit'); const since = Date.parse(required('since')), until = Date.parse(required('until')); if (!Number.isFinite(since) || !Number.isFinite(until) || until <= since) throw new Error('Exaktes Log-Zeitfenster erforderlich'); if (typeof input.parameters.max_lines !== 'number' || !Number.isSafeInteger(input.parameters.max_lines) || input.parameters.max_lines < 1 || input.parameters.max_lines > 1000) throw new Error('Loglimit muss 1–1000 sein'); } + if (input.action === 'query_logs') { + const unit = required('unit'); logWindow(required('since'), required('until')); + if (unit.startsWith('windows.eventlog.') && unit !== windowsEventLogFixtureSource) throw new Error('Nur die feste Windows-Fixture-Eventlogquelle ist konfiguriert'); + if (unit === windowsEventLogFixtureSource) validateWindowsLogLimits(input.parameters.execution_identity, input.parameters.timeout_seconds, input.parameters.max_output_bytes, input.parameters.since); + if (typeof input.parameters.max_lines !== 'number' || !Number.isSafeInteger(input.parameters.max_lines) || input.parameters.max_lines < 1 || input.parameters.max_lines > 1000) throw new Error('Loglimit muss 1–1000 sein'); + } if (input.action === 'set_service_state') { required('unit'); if (!['running', 'stopped', 'restarted'].includes(required('state'))) throw new Error('Ungültiger Dienstzustand'); } if (input.action === 'ensure_package') { for (const key of ['provider', 'recipe_version', 'package', 'version', 'architecture', 'installation_context']) required(key); } if (input.action === 'apply_updates') { required('provider'); if (!Array.isArray(input.parameters.packages) || !input.parameters.packages.length || !input.parameters.packages.every(v => typeof v === 'string' && v.length > 0)) throw new Error('Feste Updatepakete erforderlich'); if (input.parameters.reboot_policy !== 'never') throw new Error('Kein impliziter Reboot; eigenen festen Plan erstellen'); } diff --git a/web/src/console-output.test.tsx b/web/src/console-output.test.tsx new file mode 100644 index 0000000..0479e28 --- /dev/null +++ b/web/src/console-output.test.tsx @@ -0,0 +1,27 @@ +import { expect, it, vi } from 'vitest'; +import { renderToStaticMarkup } from 'react-dom/server'; +import { ConsoleOutput, outputBytes, outputText } from './console-output'; + +it('renders hostile output as inert text, reports real status and preserves exact raw bytes', () => { + const fetch = vi.fn(); vi.stubGlobal('fetch', fetch); + try { + const hostile = '\r\n\x1b[2J\u202eapprove', raw = Array.from(new TextEncoder().encode(hostile)); + const job = { state: 'unknown', attempt_id: 'fixed-attempt', revision: 4, cancel_requested: true, result: { stdout: raw, stderr: [], exit_code: null, error_code: 'TIMEOUT', output_truncated: true, reboot_required: false } }; + const before = JSON.stringify(job), html = renderToStaticMarkup(); + for (const expected of ['unknown', 'fixed-attempt', 'TIMEOUT', 'unknown / nicht gemeldet', 'nur ein Ausschnitt', 'keinen Live-Ausgabestream', 'Abbruch angefordert', 'neuen genehmigten Plan', '<script>', '\\u000d', '\\u001b', '\\u202e', JSON.stringify(raw)]) expect(html).toContain(expected); + expect(html).not.toMatch(/<(script|img|iframe|form|input)\b/i); + expect(html).not.toContain('onclick='); expect(fetch).not.toHaveBeenCalled(); expect(JSON.stringify(job)).toBe(before); + } finally { vi.unstubAllGlobals(); } +}); + +it('distinguishes no result, empty bytes, invalid encoding, invalid bytes and missing truncation evidence', () => { + const pending = renderToStaticMarkup(); + expect(pending).toContain('Noch kein gespeichertes Ergebnis'); expect(pending).not.toContain('(leere Ausgabe)'); + const partial = renderToStaticMarkup(); + expect(partial).toContain('kein gültiges UTF-8'); expect(partial).toContain('Vollständigkeit ist nicht bestätigt'); expect(partial).toContain('(leere Ausgabe)'); + const bad = renderToStaticMarkup(); + expect(bad.match(/Keine verlässliche Textansicht/g)).toHaveLength(2); + for (const raw of [undefined, 'hello', [1.5], [-1], [256]]) expect(() => outputBytes(raw)).toThrow(); + const bytes = outputBytes([239, 187, 191, 65, 13, 10]); + expect(outputText(bytes).text).toBe('\\ufeffA\\u000d\n'); expect(Array.from(bytes)).toEqual([239, 187, 191, 65, 13, 10]); +}); diff --git a/web/src/console-output.tsx b/web/src/console-output.tsx new file mode 100644 index 0000000..510073f --- /dev/null +++ b/web/src/console-output.tsx @@ -0,0 +1,54 @@ +import { record, RecordValue, stringField } from './api'; + +/** The wire format carries bytes, not an encoding declaration. Never guess a + * Windows code page or interpret output as HTML, terminal escapes or actions. */ +export function outputBytes(value: unknown): Uint8Array { + if (!Array.isArray(value) || value.length > 262144 || value.some(byte => !Number.isInteger(byte) || byte < 0 || byte > 255)) throw new Error('Ungültige oder zu große Ausgabebytes'); + return new Uint8Array(value); +} +export function outputText(raw: Uint8Array): { text: string; encoding: string } { + let decoded: string, encoding: string; + try { decoded = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(raw); encoding = 'UTF-8 lesbar; Quellencoding nicht separat belegt'; } + catch { decoded = new TextDecoder('utf-8', { ignoreBOM: true }).decode(raw); encoding = 'Encoding unbekannt / kein gültiges UTF-8: Ersatzzeichen nur in der Textansicht'; } + // Make terminal/control and bidi characters visible. In particular CR cannot + // visually overwrite an earlier line; CRLF remains recognizable as such. + return { text: decoded.replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069\ufeff]/g, + char => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`), encoding }; +} +function download(raw: Uint8Array, name: 'stdout' | 'stderr') { + const url = URL.createObjectURL(new Blob([raw], { type: 'application/octet-stream' })); + const link = document.createElement('a'); + link.href = url; link.download = `${name}.bin`; link.click(); + setTimeout(() => URL.revokeObjectURL(url), 1000); +} +function OutputChannel({ value, name }: { value: unknown; name: 'stdout' | 'stderr' }) { + try { + const raw = outputBytes(value), view = outputText(raw); + return

{name} · {raw.length} gespeicherte Bytes

{view.encoding}. Steuerzeichen werden sichtbar als \\uXXXX dargestellt; Rohbytes bleiben erhalten.

+
{raw.length ? view.text : '(leere Ausgabe)'}
+ +
{name} Originalbytes (dezimal)
{JSON.stringify(Array.from(raw))}
+
; + } catch (error) { return

{name}: {error instanceof Error ? error.message : 'Ausgabe nicht lesbar'}. Keine verlässliche Textansicht.

; } +} +export function ConsoleOutput({ job }: { job: RecordValue }) { + const state = stringField(job, 'state'); + return

Konsolenergebnis

+

Zustand: {state} · Versuch: {stringField(job, 'attempt_id')} · Revision: {typeof job.revision === 'number' ? job.revision : 'unknown'}

+

Die Ansicht fragt den gespeicherten Zustand alle 4 Sekunden ab. stdout/stderr erscheinen nach Eingang des Ergebnisses; es gibt keinen Live-Ausgabestream.

+ {state === 'unknown' &&

Wirkung oder Ergebnis unklar. Keine automatische Wiederholung; ein neuer Versuch benötigt Abgleich und einen neuen genehmigten Plan.

} + {job.cancel_requested === true &&

Abbruch angefordert; dies bestätigt weder Prozessende noch Rollback.

} + {job.pending_reason != null &&

Wartegrund: {String(job.pending_reason)}

} + {job.result == null ?

Noch kein gespeichertes Ergebnis. Fehlende Ausgabe ist kein Erfolgsnachweis.

: } +
; +} +function Result({ value }: { value: unknown }) { + try { + const result = record(value, 'Jobergebnis'); + return <>

Exitcode: {typeof result.exit_code === 'number' && Number.isInteger(result.exit_code) ? result.exit_code : 'unknown / nicht gemeldet'} · Fehlercode: {typeof result.error_code === 'string' && result.error_code ? result.error_code : 'nicht gemeldet'}

+

Ausgabe begrenzt: {typeof result.output_truncated === 'boolean' ? String(result.output_truncated) : 'unknown'}. {result.output_truncated === true ? 'Die gespeicherten Bytes sind nur ein Ausschnitt.' : result.output_truncated !== false ? 'Vollständigkeit ist nicht bestätigt.' : 'Keine Trunkierung gemeldet.'}

+

Neustartbedarf: {typeof result.reboot_required === 'boolean' ? String(result.reboot_required) : 'unknown'}. Ein Exitcode 0 bestätigt Prozessabschluss; fachliche Nachbedingungen stehen im vollständigen Ergebnis.

+

Ausgabe ist nicht vertrauenswürdiger Text vom Zielgerät. Sie erteilt keine Genehmigung und wird nicht als Anweisung ausgeführt.

+ ; + } catch { return

Ergebnisformat unbekannt; vollständige Originaldaten stehen darunter.

; } +} diff --git a/web/src/json-view.test.tsx b/web/src/json-view.test.tsx index 8c0ec18..c7c5eeb 100644 --- a/web/src/json-view.test.tsx +++ b/web/src/json-view.test.tsx @@ -1,9 +1,39 @@ import { expect, it, vi } from 'vitest'; import { renderToStaticMarkup } from 'react-dom/server'; +import { sha256, verifyPlanIntegrity } from './safety'; // Import the actual renderer without mounting the application's network effects. vi.mock('react-dom/client', () => ({ createRoot: () => ({ render: vi.fn() }) })); +it('shows fixed Event Log source, half-open UTC window and both runtime bounds before approval', async () => { + const fetch=vi.fn();vi.stubGlobal('fetch',fetch);vi.stubGlobal('document',{getElementById:()=>({})}); + try{ + const {QueryLogsPlanNotice}=await import('./main'); + const payload={jobs:[{payload:{asset_id:'win-fixture',execution_identity:'agent_service',max_runtime_seconds:30,max_output_bytes:4096,action:{action:'query_logs',unit:'windows.eventlog.application.rustdiscovery-mvp-test',since:'2026-11-14T00:00:00Z',until:'2026-11-14T01:00:00Z',max_lines:1000}}}]}; + const raw=new TextEncoder().encode(JSON.stringify(payload)),plan={plan_payload_bytes:Array.from(raw),plan_hash:await sha256(raw),plan_payload:payload}; + expect(renderToStaticMarkup()).toContain('role="alert"');await verifyPlanIntegrity(plan); + const html=renderToStaticMarkup();for(const expected of ['Application','RustDiscovery-MVP-Test','1000','4096','30s','5000ms','kooperativ','2026-11-14T01:00:00Z)','Keine Bookmark'])expect(html).toContain(expected); + expect(html).not.toMatch(/<(button|input|form)\b/);expect(fetch).not.toHaveBeenCalled(); + }finally{vi.unstubAllGlobals();} +}); + +it('shows the verified PowerShell process profile before approval without issuing requests', async () => { + const fetch = vi.fn(); vi.stubGlobal('fetch', fetch); vi.stubGlobal('document', { getElementById: () => ({}) }); + try { + const { PowerShellPlanNotice } = await import('./main'); + const payload = { jobs: [{ payload: { asset_id: 'fixed-windows', execution_identity: 'agent_service', action: { action: 'run_script', interpreter: 'powershell', script_utf8: 'Write-Output 1\r\n', arguments: [], expected_stdout: '1\r\n', windows_execution_profile: 'powershell_51_remote_signed' } } }] }; + const raw = new TextEncoder().encode(JSON.stringify(payload)), plan = { plan_payload_bytes: Array.from(raw), plan_hash: await sha256(raw), plan_payload: payload }; + expect(renderToStaticMarkup()).toContain('role="alert"'); + await verifyPlanIntegrity(plan); + const html = renderToStaticMarkup(); + for (const expected of ['fixed-windows', 'Windows PowerShell5.1', 'RemoteSigned', 'nur Jobprozess', 'angeforderte Option']) expect(html).toContain(expected); + expect(html).not.toMatch(/<(button|input|form)\b/); + plan.plan_payload_bytes[0] = 0; + expect(renderToStaticMarkup()).toContain('role="alert"'); + expect(fetch).not.toHaveBeenCalled(); + } finally { vi.unstubAllGlobals(); } +}); + it('renders hostile log content only as text, without executable nodes, approval controls or requests', async () => { const fetch = vi.fn(); vi.stubGlobal('fetch', fetch); @@ -30,13 +60,17 @@ it('shows fixed SYSTEM identity and service state from stored plan bytes without const fetch = vi.fn(); vi.stubGlobal('fetch', fetch); vi.stubGlobal('document', { getElementById: () => ({}) }); try { const { SystemPlanNotice } = await import('./main'); - const payload = { jobs: [{ payload: { execution_identity: 'system', asset_id: 'fixed-asset', action: { action: 'set_service_state', unit: 'RmmDiscoveryFixtureService', state: 'stopped' } } }] }; - const plan = { plan_payload_bytes: Array.from(new TextEncoder().encode(JSON.stringify(payload))), jobs: [{ payload: { execution_identity: 'root', action: { unit: 'untrusted-view' } } }] }; + const payload = { jobs: [{ payload: { execution_identity: 'system', asset_id: 'fixed-asset', max_runtime_seconds: 30, max_output_bytes: 4096, action: { action: 'set_service_state', unit: 'RmmDiscoveryFixtureService', state: 'stopped' } } }] }; + const raw = new TextEncoder().encode(JSON.stringify(payload)); + const plan = { plan_payload_bytes: Array.from(raw), plan_hash: await sha256(raw), jobs: [{ payload: { execution_identity: 'root', action: { unit: 'untrusted-view' } } }] }; + expect(renderToStaticMarkup()).toContain('role="alert"'); + await verifyPlanIntegrity(plan); const html = renderToStaticMarkup(); expect(html).toContain('SYSTEM'); expect(html).toContain('system:execute'); expect(html).toContain('Identität: system'); for (const expected of ['fixed-asset', 'RmmDiscoveryFixtureService', 'Zielzustand: stopped', 'Keine SYSTEM-Shell']) expect(html).toContain(expected); expect(html).not.toContain('untrusted-view'); expect(html).not.toMatch(/<(button|input|form|pre)\b/); - expect(renderToStaticMarkup()).toBe(''); + const emptyRaw = new TextEncoder().encode('{"jobs":[]}'), empty = { plan_payload_bytes: Array.from(emptyRaw), plan_hash: await sha256(emptyRaw) }; await verifyPlanIntegrity(empty); + expect(renderToStaticMarkup()).toBe(''); for (const plan_payload_bytes of [[255], [123], 'invalid']) { const invalid = renderToStaticMarkup(); expect(invalid).toContain('role="alert"'); expect(invalid).toContain('nicht lesbar'); diff --git a/web/src/main.tsx b/web/src/main.tsx index e160aa5..d6a328a 100644 --- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -1,20 +1,66 @@ import React, { useEffect, useState } from 'react'; import { createRoot } from 'react-dom/client'; -import { actions, Action, ApiError, authConfig, approvalBlockers, expectedStdout, objectId, page, Page, PlanInput, pretty, record, RecordValue, request, session, Session, stringField, tenant, tenantPath, validateInput } from './api'; +import { actions, Action, ApiError, authConfig, approvalBlockers, expectedStdout, objectId, page, Page, PlanInput, pretty, record, RecordValue, request, session, Session, stringField, tenant, tenantPath, validateInput, windowsPowerShellRemoteSignedProfile, fullMaintenanceScriptProfile } from './api'; import './style.css'; import { AdminControl, Catalog, TenantControls } from './management'; -import { allowed, availableActions, parseStrictJson, rootWarning, systemWarning, systemPlanJobs, validateSensitiveInput, verifyPlanIntegrity } from './safety'; +import { allowed, availableActions, parseStrictJson, powerShellApprovalBlockers, powerShellPlanJobs, rootWarning, systemWarning, systemPlanJobs, validateSensitiveInput, verifyPlanIntegrity, queryLogsApprovalBlockers, queryLogsPlanJobs, windowsMsiApprovalBlockers, windowsMsiPlanJobs, verifyWindowsMsiManifest, fullMaintenanceWarning } from './safety'; import { Login } from './login'; +import { ConsoleOutput, outputText } from './console-output'; +import { scriptApprovalBlockers, scriptPlanJobs, verifyScriptPlan } from './script-console'; const errorText = (error: unknown) => error instanceof Error ? error.message : 'Unbekannter Fehler'; -export function JsonView({ value, label = 'Vollständige Daten' }: { value: unknown; label?: string }) { return
{label}
{pretty(value)}
; } +export function JsonView({ value, label = 'Vollständige Daten' }: { value: unknown; label?: string }) { return
{label}
{pretty(value)}
; } +export function ScriptPlanNotice({ plan }: { plan: RecordValue }) { + try { + const jobs = scriptPlanJobs(plan); if (!jobs.length) return null; + return

Vollständige feste Skriptaufträge ({jobs.length})

Diese Inhalte stammen aus den hashgeprüften gespeicherten Planbytes. Jeder Zielauftrag, Versuch und Skripthash ist fest gebunden.

+ {jobs.map(job =>

Asset {job.assetId}

Agent: {job.agentId} · Job: {job.jobId} · Versuch: {job.attemptId}

+

Interpreter: {job.interpreter} · Identität: {job.identity} · Privilegiertes Profil: {job.privilegedProfile === fullMaintenanceScriptProfile ? fullMaintenanceScriptProfile : 'bestehendes begrenztes Profil'} · Windowsprozess: {job.interpreter !== 'powershell' ? 'nicht zutreffend' : job.windowsProfile === 'powershell_51_remote_signed' ? 'Windows PowerShell5.1 RemoteSigned, nur Jobprozess' : 'geerbte Windowsrichtlinie / kein Windows-Prozessoverride'}

+

Frühester Start: {job.notBefore} · Startfrist: {job.startBefore} · Laufzeit: {job.runtimeSeconds}s · Ausgabe: höchstens {job.outputBytes} Bytes

+

Skript: {job.scriptBytes} UTF-8-Bytes · SHA256: {job.scriptHash}
Jobpayload-SHA256: {job.jobHash}

+ {job.privilegedProfile === fullMaintenanceScriptProfile ?

{fullMaintenanceWarning}

: job.identity === 'root' &&

{rootWarning}

} +

Vollständiger Skripttext; Steuerzeichen, BOM und CR werden sichtbar als \uXXXX dargestellt. LF bleibt Zeilenumbruch. Kein Kürzen oder Normalisieren der gespeicherten Bytes.

+
{outputText(new TextEncoder().encode(job.script)).text}
+

Feste Argumente (JSON): {JSON.stringify(job.arguments)}

stdout-Nachbedingung (JSON): {JSON.stringify(job.expectedStdout)}{job.expectedStdout === null && ' · keine zusätzliche fachliche stdout-Prüfung gebunden'}

+
)} +
; + } catch { return ; } +} export function SystemPlanNotice({ plan }: { plan: RecordValue }) { let jobs: RecordValue[]; - try { jobs = systemPlanJobs(plan); } + try { jobs = systemPlanJobs(plan).filter(p => record(p.action).action === 'set_service_state'); } catch { return ; } if (!jobs.length) return null; return ; } +export function WindowsMsiPlanNotice({ plan }: { plan: RecordValue }) { + try { + const jobs = windowsMsiPlanJobs(plan); if (!jobs.length) return null; + return ; + } catch { return ; } +} +export function PowerShellPlanNotice({ plan }: { plan: RecordValue }) { + try { + const jobs = powerShellPlanJobs(plan); + if (!jobs.length) return null; + return ; + } catch { return ; } +} +export function QueryLogsPlanNotice({ plan }: { plan: RecordValue }) { + try { + const jobs = queryLogsPlanJobs(plan); + if (!jobs.length) return null; + return ; + } catch { return ; } +} function ErrorBox({ error }: { error: string | null }) { return error ?
{error}
: null; } function useResource(load: (signal: AbortSignal) => Promise, deps: React.DependencyList, poll = false) { const [data, setData] = useState(null), [error, setError] = useState(null), [revision, setRevision] = useState(0); @@ -49,40 +95,69 @@ function Planner({ tenantId, auth, selected, showPlan }: { tenantId: string; aut const [identity, setIdentity] = useState('agent_service'); return

Neuen unveränderlichen Plan vorbereiten

Nur fest ausgewählte Asset-IDs. Keine Ausführung durch Planung oder Antrag; jede Aktion einschließlich Live-Inventar benötigt Genehmigung.

{ e.preventDefault(); setBusy(true); setError(null); const f = new FormData(e.currentTarget); try { - const common = { execution_identity: String(f.get('identity')), timeout_seconds: Number(f.get('timeout')), max_output_bytes: Number(f.get('output')) }; - const params = action === 'run_script' ? { ...common, variants: scriptPlatform === 'mixed' ? parseStrictJson(mixedVariants) : [{ os_family: scriptPlatform, interpreter: String(f.get('interpreter')), encoding: 'utf-8', script_utf8: String(f.get('script')) }], arguments: [], expected_stdout: expectedStdout(String(f.get('expected')), String(f.get('expected_format'))) } : { ...record(parseStrictJson(parameters)), ...common }; + const common = { execution_identity: String(f.get('identity')), timeout_seconds: Number(f.get('timeout')), max_output_bytes: Number(f.get('output')), ...(action === 'run_script' && f.get('privileged_script_profile') === fullMaintenanceScriptProfile ? { privileged_script_profile: fullMaintenanceScriptProfile } : {}) }; + const params = action === 'run_script' ? { ...common, variants: scriptPlatform === 'mixed' ? parseStrictJson(mixedVariants) : [{ os_family: scriptPlatform, interpreter: String(f.get('interpreter')), encoding: 'utf-8', script_utf8: String(f.get('script')), ...(scriptPlatform === 'windows' ? { windows_execution_profile: windowsPowerShellRemoteSignedProfile } : {}) }], arguments: [], expected_stdout: expectedStdout(String(f.get('expected')), String(f.get('expected_format'))) } : { ...record(parseStrictJson(parameters)), ...common }; const input: PlanInput = { tenant_id: tenantId, action, targets: { asset_ids: [...selected] }, parameters: params, schedule: { not_before: new Date(String(f.get('not_before'))).toISOString(), start_before: new Date(String(f.get('expires'))).toISOString(), offline_policy: 'wait_until_deadline' }, rollout: { max_parallel: Number(f.get('parallel')), halt_after_failures: Number(f.get('failures')) }, reason: String(f.get('reason')) }; const ids = String(f.get('artifact_ids') ?? '').split(/[\s,]+/).filter(Boolean); if (ids.length) { if (ids.length > 16 || new Set(ids).size !== ids.length || ids.some(id => !/^[0-9a-f-]{36}$/i.test(id))) throw new Error('Maximal 16 eindeutige feste Artefakt-UUIDs'); input.artifact_ids = ids; } validateInput(input); await validateSensitiveInput(input, auth); const plan = record(await request(tenantPath(tenantId, '/operation-plans'), { body: input, csrf: auth.csrf_token })); showPlan(objectId(plan, 'plan')); } catch (e) { setError(errorText(e)); } finally { setBusy(false); } }}> + {action === 'query_logs' &&

Windows-MVP nur Quelle windows.eventlog.application.rustdiscovery-mvp-test, Kanal Application, Provider RustDiscovery-MVP-Test, agent_service. Festes UTC-Fenster höchstens24h,1–1000Events,1024–65536Bytes,äußere Joblaufzeit10–120s; inneres kooperatives Abfragebudget höchstens5000ms. Keine freie Kanal-/XPathauswahl und keine Bookmarkfortschreibung. Linux behält seine feste Journalunit.

} {action === 'apt_native_acceptance' &&

NUR SIMULIERTE DEV/TEST-NATIVABNAHME. In Vorbereitung; Backend/Helper/Scope müssen diese eigene Aktion unterstützen. Nur rmm-apt-fixture-install-1.0 oder rmm-apt-fixture-update-1.1, Rezeptversion 1, vollständiges Schema-2-Manifest. null/Platzhalter sind nicht konfiguriert, keine Produktionsaktion, keine automatische Ausführung.

} - {action === 'run_script' ? <>{scriptPlatform === 'mixed' ?