diff --git a/crates/agent-windows/src/helper_runtime.rs b/crates/agent-windows/src/helper_runtime.rs index 4a1dfe7..cc976e4 100644 --- a/crates/agent-windows/src/helper_runtime.rs +++ b/crates/agent-windows/src/helper_runtime.rs @@ -785,8 +785,38 @@ pub(crate) fn msi_test_job() -> JobPayload { } } fn write_current_probe(state: &Path, value: &serde_json::Value) -> Result<()> { + write_report(state, PROBE_FILE, ".helper-probe.pending", value) +} + +/// A historical observation by the agent. The helper lane calls this only after +/// the authenticated peer has acknowledged its own signature verification. +pub fn record_verified_time( + state: &Path, + request: &rmm_time_attestation::Request, + response_sha256: &str, + observed_at: chrono::DateTime, +) -> Result<()> { + use sha2::{Digest, Sha256}; + let (filename, pending) = match request.context.role { + rmm_time_attestation::Role::Agent => ("agent-time-sample.json", ".agent-time.pending"), + rmm_time_attestation::Role::SystemHelper => { + ("helper-time-sample.json", ".helper-time.pending") + } + }; + let value = json!({ + "schema_version": 1, "agent_pid": std::process::id(), "observed_at": observed_at, + "role": request.context.role, "windows_boot_id": request.context.windows_boot_id, + "connection_generation": request.context.connection_generation, + "verifier_incarnation": request.context.verifier_incarnation, + "request_sha256": format!("{:x}", Sha256::digest(serde_json::to_vec(request)?)), + "response_sha256": response_sha256 + }); + write_report(state, filename, pending, &value) +} + +fn write_report(state: &Path, name: &str, pending: &str, value: &serde_json::Value) -> Result<()> { native::private_path(state, true)?; - let path = state.join(PROBE_FILE); + let path = state.join(name); if path.try_exists()? { native::private_path(&path, false)?; } @@ -794,7 +824,7 @@ fn write_current_probe(state: &Path, value: &serde_json::Value) -> Result<()> { ensure!(bytes.len() <= 8192, "HELPER_PROBE_REPORT_LIMIT"); // One fixed create-only staging file bounds even interrupted-report state. // A leftover from a killed process is refused; it is never overwritten. - let temporary = state.join(".helper-probe.pending"); + let temporary = state.join(pending); let mut file = std::fs::OpenOptions::new() .write(true) .create_new(true) diff --git a/crates/agent-windows/src/runtime.rs b/crates/agent-windows/src/runtime.rs index 0070eae..20f59b2 100644 --- a/crates/agent-windows/src/runtime.rs +++ b/crates/agent-windows/src/runtime.rs @@ -177,6 +177,7 @@ struct TimeRefresh { clock: rmm_agent_windows::signed_clock::RuntimeClock, helper: Option, tasks: Vec>, + samples: Vec>>, } #[cfg(feature = "system-helper")] impl Drop for TimeRefresh { @@ -189,6 +190,30 @@ impl Drop for TimeRefresh { } } #[cfg(feature = "system-helper")] +struct VerifiedTimeSample { + request: rmm_time_attestation::Request, + response_hash: String, + observed_at: chrono::DateTime, +} +#[cfg(feature = "system-helper")] +impl VerifiedTimeSample { + fn new(request: rmm_time_attestation::Request, response_hash: String) -> Self { + Self { + request, + response_hash, + observed_at: Utc::now(), + } + } + fn persist(&self, state: &Path) -> Result<()> { + rmm_agent_windows::helper_runtime::record_verified_time( + state, + &self.request, + &self.response_hash, + self.observed_at, + ) + } +} +#[cfg(feature = "system-helper")] async fn time_response( client: &reqwest::Client, address: std::net::SocketAddr, @@ -484,6 +509,17 @@ impl Agent { let own_stop = stop.clone(); let own_http = client.clone(); let address = self.config.connect_address; + // Only the connection owner publishes observations. Dropping it also + // discards unfinished samples, so an old worker cannot overwrite a newer report. + let sample_slot = + || (mode == SamplingMode::Passive).then(|| Arc::new(std::sync::OnceLock::new())); + let own_receipt = sample_slot(); + let helper_receipt = helper.as_ref().and_then(|_| sample_slot()); + let samples = own_receipt + .iter() + .chain(helper_receipt.iter()) + .cloned() + .collect(); tasks.push(tokio::spawn(async move { while !own_stop.load(Ordering::SeqCst) { let result = async { @@ -493,7 +529,12 @@ impl Agent { let request = own_clock.begin(generation)?; let bytes = time_response(&own_http, address, &request).await?; ensure!(!own_stop.load(Ordering::SeqCst), "TIME_CONNECTION_CLOSED"); - own_clock.complete(generation, &bytes) + own_clock.complete(generation, &bytes)?; + if let Some(slot) = own_receipt.as_ref().filter(|s| s.get().is_none()) { + let hash = format!("{:x}", Sha256::digest(&bytes)); + let _ = slot.set(VerifiedTimeSample::new(request, hash)); + } + Ok::<(), anyhow::Error>(()) } .await; if result.is_err() { @@ -514,7 +555,14 @@ impl Agent { !helper_stop.load(Ordering::SeqCst), "TIME_CONNECTION_CLOSED" ); - time_helper.time_complete(generation, bytes).await + let pending = helper_receipt.as_ref().filter(|s| s.get().is_none()); + let report = + pending.map(|slot| (slot, format!("{:x}", Sha256::digest(&bytes)))); + time_helper.time_complete(generation, bytes).await?; + if let Some((slot, hash)) = report { + let _ = slot.set(VerifiedTimeSample::new(request, hash)); + } + Ok::<(), anyhow::Error>(()) } .await; if result.is_err() { @@ -531,6 +579,7 @@ impl Agent { clock, helper, tasks, + samples, }); Ok(()) } @@ -1694,6 +1743,14 @@ impl Agent { )?; channel.poll(poll).await?; } + #[cfg(feature = "system-helper")] + if let Some(refresh) = &mut self.time_refresh { + let state = &self.config.state_directory; + refresh.samples.retain(|slot| { + slot.get() + .is_none_or(|sample| sample.persist(state).is_err()) + }); + } heartbeat = Instant::now(); heartbeat_after = Duration::from_millis(rand::random_range(27000..33000)); } diff --git a/docs/windows-trusted-os-clock-source.md b/docs/windows-trusted-os-clock-source.md index 215bc0c..ad3fd7f 100644 --- a/docs/windows-trusted-os-clock-source.md +++ b/docs/windows-trusted-os-clock-source.md @@ -136,3 +136,15 @@ fehlgeschlagen oder übersprungen, 42 weitere gefiltert. Quittung Der native Testprozess prüfte die Logik mit Testdaten. Der signierte Zeitvergleich in den beiden installierten Dienstprozessen ist eine eigene offene Abnahme. + +Für den Nachweis aus den laufenden Diensten hält der Agent je passiver Verbindung +den ersten vollständig geprüften Zeitvergleich pro Rolle fest. Im Agent-State +entstehen `agent-time-sample.json` und `helper-time-sample.json`. Die +SYSTEM-Beobachtung setzt die authentifizierte positive Antwort des Helpers auf +`TimeComplete` voraus. Beide Belege enthalten Rolle, Agent-PID, Windows-Boot, +Verbindungsgeneration, Verifier-Inkarnation, Beobachtungszeit und die SHA-256-Werte +der Anfrage und der empfangenen Antwort. Der Agent veröffentlicht sie beim +Heartbeat aus dem Zustand der jeweiligen Verbindung. Diese historischen Daten +werden bei der Jobfreigabe und der Prüfung der aktuellen Zeit nie eingelesen. +Der erweiterte Code bestand den Windows-Alltargets-Clippycheck. Die tatsächlichen +Belegdateien der installierten Rollen stehen noch aus.