From cfbf1f7bfc0e956560af23ef212497eb617d516b Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Sun, 15 Nov 2026 01:07:19 +0100 Subject: [PATCH] Verify health against exact retained original sync timestamps --- crates/agent-core/src/lib.rs | 1 + crates/agent-core/src/update_health_sync.rs | 323 +++++++++++++++++++ crates/agent-core/tests/helper_reconcile.rs | 11 + crates/privileged-helper/src/update_guard.rs | 35 +- docs/agent-updater-health-client.md | 28 ++ 5 files changed, 369 insertions(+), 29 deletions(-) create mode 100644 crates/agent-core/src/update_health_sync.rs diff --git a/crates/agent-core/src/lib.rs b/crates/agent-core/src/lib.rs index cb6a414..c7f6fb4 100644 --- a/crates/agent-core/src/lib.rs +++ b/crates/agent-core/src/lib.rs @@ -10,3 +10,4 @@ pub mod ledger; pub mod reconcile; pub mod update_handoff; pub mod update_health_inventory; +pub mod update_health_sync; diff --git a/crates/agent-core/src/update_health_sync.rs b/crates/agent-core/src/update_health_sync.rs new file mode 100644 index 0000000..ba8f314 --- /dev/null +++ b/crates/agent-core/src/update_health_sync.rs @@ -0,0 +1,323 @@ +//! Read-only original HelperSync evidence. Exact UTC bodies, not quantized +//! receipt scalars, bound the real local application time. Creates no lease. +use crate::{ + auth::Binding, + helper_reconcile::{ExecutorLedgerGuard, SignedResponse}, + reconcile::{Channel, verify_response}, + update_handoff::{self, AppliedUpdateSync}, +}; +use anyhow::{Context, Result, ensure}; +use chrono::{DateTime, Utc}; +use prost::Message; +use rmm_domain::{ + agent_update::{UpdateInflightProof, inflight_decision, verify_inflight_proof}, + jobs::hash, + reconciliation::ReconcileDecisionPayload, +}; +use rmm_protocol::{SyncRequest, SyncResponse}; +use rmm_updater_handoff::{Handoff, HealthReceipt, Phase}; +use rusqlite::Connection; +use uuid::Uuid; + +/// No deserializer or external construction. Raw request/response hashes and +/// actual local application come from the SAME guarded existing helper ledger. +pub struct Round { + applied: AppliedUpdateSync, + request: SyncRequest, + decision: ReconcileDecisionPayload, +} +impl Round { + pub fn applied(&self) -> &AppliedUpdateSync { + &self.applied + } + pub fn decision(&self) -> &ReconcileDecisionPayload { + &self.decision + } +} +pub fn read_round( + db: &Connection, + guard: &impl ExecutorLedgerGuard, + binding: &Binding, + grant: Uuid, + id: Uuid, +) -> Result { + guard.check(db)?; + let (request_len, response_len): (usize, usize) = db.query_row( + "SELECT length(request),length(response) FROM helper_sync_decisions WHERE decision_id=?1", + [id.to_string()], + |r| Ok((r.get(0)?, r.get(1)?)), + )?; + ensure!( + request_len > 0 + && request_len <= rmm_domain::MAX_MESSAGE + && response_len > 0 + && response_len <= 4 * rmm_domain::MAX_MESSAGE + 1024, + "UPDATE_HEALTH_STORED_SYNC_SIZE" + ); + let applied = update_handoff::read_applied_sync(db, guard, grant, id)?; + let (raw, signed): (Vec, Vec) = db.query_row( + "SELECT request,response FROM helper_sync_decisions WHERE decision_id=?1", + [id.to_string()], + |r| Ok((r.get(0)?, r.get(1)?)), + )?; + let request = SyncRequest::decode(raw.as_slice())?; + let signed: SignedResponse = serde_json::from_slice(&signed)?; + let response = SyncResponse { + decision_bytes: signed.decision_bytes, + signature: signed.signature, + }; + ensure!( + request.encode_to_vec() == raw + && hash(&raw) == applied.request_sha256 + && hash(&response.encode_to_vec()) == applied.response_protobuf_sha256, + "UPDATE_HEALTH_STORED_SYNC_BYTES" + ); + let unverified: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?; + // Historical verification only. Current clock/owner/boot/policy remain the + // calling Fullguard's independent prerequisites, never renewed by this read. + let decision = verify_response( + binding, + &request, + &Channel { + boot_id: unverified.boot_id, + connection_generation: unverified.connection_generation, + }, + &response, + unverified.issued_at, + )?; + ensure!( + decision.decision_id == id && serde_json::to_vec(&decision)? == response.decision_bytes, + "UPDATE_HEALTH_ORIGINAL_SYNC_CODEC" + ); + guard.check(db)?; + Ok(Round { + applied, + request, + decision, + }) +} +pub struct VerifiedSpan { + applied: AppliedUpdateSync, + lower: DateTime, + upper: DateTime, +} +impl VerifiedSpan { + pub fn applied(&self) -> &AppliedUpdateSync { + &self.applied + } + pub fn lower(&self) -> DateTime { + self.lower + } + pub fn upper(&self) -> DateTime { + self.upper + } +} +pub fn verified_span( + db: &Connection, + guard: &impl ExecutorLedgerGuard, + binding: &Binding, + header: &Handoff, + receipt: &HealthReceipt, + health_key: [u8; 32], +) -> Result { + guard.check(db)?; + let applied = read_round( + db, + guard, + binding, + header.attempt.grant_id, + receipt.signed_sync_decision_id, + )?; + ensure!( + applied.applied.request_sha256 == receipt.signed_sync_request_sha256 + && applied.applied.response_protobuf_sha256 == receipt.signed_sync_response_sha256 + && receipt.inventory_ack_sha256 == applied.applied.response_protobuf_sha256 + && applied + .decision + .acknowledged_message_ids + .contains(&receipt.inventory_message_id), + "UPDATE_HEALTH_ORIGINAL_APPLIED_OR_ACK" + ); + let prefix = services_prefix(&applied, header, receipt, health_key)?; + let original_prefix = UpdateInflightProof::from_bytes(&prefix).map_err(anyhow::Error::msg)?; + let current = update_handoff::current_proof(db, guard, header.attempt.grant_id)? + .context("UPDATE_HEALTH_ORIGINAL_CURRENT_PREFIX_MISSING")?; + ensure!( + original_prefix.delegation == current.delegation + && original_prefix.events.len() <= current.events.len() + && current.events[..original_prefix.events.len()] == original_prefix.events, + "UPDATE_HEALTH_ORIGINAL_PREFIX_NOT_RETAINED" + ); + let invalid:usize=db.query_row("SELECT count(*) FROM agent_update_sync_applied WHERE grant_id=?1 AND length(decision_id)<>36",[header.attempt.grant_id.to_string()],|r|r.get(0))?; + ensure!(invalid == 0, "UPDATE_HEALTH_ORIGINAL_SYNC_ID_SIZE"); + let mut query=db.prepare("SELECT decision_id FROM agent_update_sync_applied WHERE grant_id=?1 ORDER BY decision_id LIMIT 1025")?; + let ids = query + .query_map([header.attempt.grant_id.to_string()], |r| { + r.get::<_, String>(0) + })? + .collect::>>()?; + ensure!(ids.len() <= 1024, "UPDATE_HEALTH_ORIGINAL_SYNC_LIMIT"); + let mut confirming = None; + for id in ids { + let id: Uuid = id.parse()?; + if id == receipt.signed_sync_decision_id { + continue; + } + let (len,): (usize,) = db.query_row( + "SELECT length(request) FROM helper_sync_decisions WHERE decision_id=?1", + [id.to_string()], + |r| Ok((r.get(0)?,)), + )?; + ensure!( + len > 0 && len <= rmm_domain::MAX_MESSAGE, + "UPDATE_HEALTH_CONFIRMING_SIZE" + ); + let raw: Vec = db.query_row( + "SELECT request FROM helper_sync_decisions WHERE decision_id=?1", + [id.to_string()], + |r| r.get(0), + )?; + let request = SyncRequest::decode(raw.as_slice())?; + if request.previous_decision_id != receipt.signed_sync_decision_id.to_string() + || request.helper_previous_decision_id != receipt.signed_sync_decision_id.to_string() + { + continue; + } + // Hash/signature/application checks are mandatory AFTER this cheap + // candidate selection too. Caller request fields never prove an apply. + let round = read_round(db, guard, binding, header.attempt.grant_id, id)?; + ensure!( + services_prefix(&round, header, receipt, health_key)? == prefix && confirming.is_none(), + "UPDATE_HEALTH_CONFIRMING_PREFIX_OR_FORK" + ); + confirming = Some(round); + } + let confirming = confirming.context("UPDATE_HEALTH_ACTUAL_CONFIRMING_ROUND_MISSING")?; + require_exact_span( + applied.decision.issued_at, + applied.applied.applied_at, + confirming.decision.issued_at, + receipt.signed_sync_applied_at, + )?; + ensure!( + applied.applied.applied_at < applied.decision.valid_until, + "UPDATE_HEALTH_ACTUAL_APPLY_EXPIRED" + ); + guard.check(db)?; + Ok(VerifiedSpan { + applied: applied.applied, + lower: applied.decision.issued_at, + upper: confirming.decision.issued_at, + }) +} +fn services_prefix( + round: &Round, + header: &Handoff, + receipt: &HealthReceipt, + key: [u8; 32], +) -> Result> { + let request = &round.request; + let decision = &round.decision; + ensure!( + !decision.ready + && !decision.helper_ready + && request.journal_complete + && decision.connection_generation == receipt.connection_generation + && decision.boot_id == header.boot_id + && request.journal_id == header.agent_journal_id.to_string() + && request.helper_journal_id == header.helper_journal_id.to_string(), + "UPDATE_HEALTH_CONFIRMING_CHANNEL" + ); + let mut local = request.attempts.iter().filter(|a| { + a.job_id == header.attempt.job_id.to_string() + && a.attempt_id == header.attempt.attempt_id.to_string() + }); + let local = local + .next() + .context("UPDATE_HEALTH_ORIGINAL_ATTEMPT_MISSING")? + .clone(); + ensure!( + request + .attempts + .iter() + .filter(|a| a.job_id == header.attempt.job_id.to_string() + && a.attempt_id == header.attempt.attempt_id.to_string()) + .count() + == 1, + "UPDATE_HEALTH_ORIGINAL_ATTEMPT_DUPLICATE" + ); + let proof = + UpdateInflightProof::from_bytes(&local.update_handoff_bytes).map_err(anyhow::Error::msg)?; + let (observed, journal) = verify_inflight_proof(&proof, key).map_err(anyhow::Error::msg)?; + ensure!( + observed.bytes()? == header.bytes()? + && journal.phase() == Phase::ServicesStarted + && local.grant_id == header.attempt.grant_id.to_string() + && local.consumption == "consumed" + && local.state == "running" + && local.job_payload_hash == header.attempt.job_payload_sha256 + && local.original_boot_id == header.boot_id.to_string() + && local.outcome_bytes.is_empty() + && local.outcome_sha256.is_empty() + && local.result.is_none() + && local.result_envelope.is_none(), + "UPDATE_HEALTH_ORIGINAL_INFLIGHT" + ); + let signed = decision + .attempts + .iter() + .filter(|a| a.job_id == header.attempt.job_id && a.attempt_id == header.attempt.attempt_id) + .collect::>(); + ensure!(signed.len() == 1, "UPDATE_HEALTH_SIGNED_ATTEMPT"); + let signed = signed[0]; + ensure!( + signed.update_inflight.as_ref() + == Some(&inflight_decision(&proof, key).map_err(anyhow::Error::msg)?) + && signed.grant_id == Some(header.attempt.grant_id) + && signed.authorization_id == Some(header.attempt.authorization_id) + && signed.plan_hash.as_deref() == Some(header.attempt.plan_sha256.as_str()) + && signed.job_payload_hash == header.attempt.job_payload_sha256 + && signed.action == "update_agent" + && signed.execution_identity == "root" + && signed.outcome_state == "running" + && !signed.unlock + && signed.outcome_bytes.is_empty() + && signed.outcome_sha256.is_none(), + "UPDATE_HEALTH_SIGNED_ORIGINAL_INFLIGHT" + ); + Ok(local.update_handoff_bytes) +} +fn require_exact_span( + lower: DateTime, + actual: DateTime, + upper: DateTime, + wire_floor: u64, +) -> Result<()> { + ensure!( + lower <= actual && actual <= upper && wire_floor == u64::try_from(upper.timestamp())?, + "UPDATE_HEALTH_EXACT_ACTUAL_APPLY_SPAN" + ); + Ok(()) +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn same_floor_does_not_admit_a_one_nanosecond_early_or_late_apply() { + let base = DateTime::from_timestamp(1000, 0).unwrap(); + let lower = base + chrono::Duration::nanoseconds(100_000_001); + let upper = base + chrono::Duration::nanoseconds(200_000_001); + assert_eq!(lower.timestamp(), upper.timestamp()); + require_exact_span(lower, lower, upper, 1000).unwrap(); + require_exact_span(lower, upper, upper, 1000).unwrap(); + assert!( + require_exact_span(lower, lower - chrono::Duration::nanoseconds(1), upper, 1000) + .is_err() + ); + assert!( + require_exact_span(lower, upper + chrono::Duration::nanoseconds(1), upper, 1000) + .is_err() + ); + assert!(require_exact_span(lower, lower, upper, 1001).is_err()); + } +} diff --git a/crates/agent-core/tests/helper_reconcile.rs b/crates/agent-core/tests/helper_reconcile.rs index df507a4..86a3b8a 100644 --- a/crates/agent-core/tests/helper_reconcile.rs +++ b/crates/agent-core/tests/helper_reconcile.rs @@ -1787,6 +1787,17 @@ fn update_inflight_negative_control_keeps_every_start_closed_and_owner_loss_is_t decision.decision_id, ) .unwrap(); + let retained_round = rmm_agent_core::update_health_sync::read_round( + &f.db, + &guard, + &f.binding, + handoff.attempt.grant_id, + decision.decision_id, + ) + .unwrap(); + assert_eq!(retained_round.applied(), &local_apply); + assert_eq!(retained_round.decision().issued_at, decision.issued_at); + assert_eq!(retained_round.decision().valid_until, decision.valid_until); assert_eq!(local_apply.applied_at, f.clock.now); assert_eq!(local_apply.request_sha256, hash(&raw)); let proto = rmm_protocol::SyncResponse { diff --git a/crates/privileged-helper/src/update_guard.rs b/crates/privileged-helper/src/update_guard.rs index 9836e67..6eb0fc3 100644 --- a/crates/privileged-helper/src/update_guard.rs +++ b/crates/privileged-helper/src/update_guard.rs @@ -335,38 +335,15 @@ impl<'a> FullUpdateGuard<'a> { "UPDATE_HEALTH_CURRENT_LOCAL_CHANNEL_REQUIRED" ); self.verify_current_services(&receipt.service_processes)?; - let applied = update_handoff::read_applied_sync( + let span = rmm_agent_core::update_health_sync::verified_span( db, self.executor, - self.original.permit().grant_id, - receipt.signed_sync_decision_id, + self.binding, + self.original.handoff(), + &receipt, + self.trust.health_public_key(), )?; - ensure!( - applied.request_sha256 == receipt.signed_sync_request_sha256 - && applied.response_protobuf_sha256 == receipt.signed_sync_response_sha256 - && receipt.inventory_ack_sha256 == applied.response_protobuf_sha256, - "UPDATE_LOCAL_APPLIED_RAW_BINDING" - ); - let response_raw: Vec = db.query_row( - "SELECT response FROM helper_sync_decisions WHERE decision_id=?1", - [receipt.signed_sync_decision_id.to_string()], - |r| r.get(0), - )?; - let response: rmm_agent_core::helper_reconcile::SignedResponse = - serde_json::from_slice(&response_raw)?; - let decision: rmm_domain::reconciliation::ReconcileDecisionPayload = - serde_json::from_slice(&response.decision_bytes)?; - let applied_seconds = u64::try_from(applied.applied_at.timestamp())?; - ensure!( - applied_seconds >= u64::try_from(decision.issued_at.timestamp())? - && applied_seconds <= receipt.signed_sync_applied_at - && !decision.ready - && !decision.helper_ready - && decision - .acknowledged_message_ids - .contains(&receipt.inventory_message_id), - "UPDATE_ACTUAL_LOCAL_APPLY_OR_INVENTORY_ACK" - ); + let applied = span.applied().clone(); verify_local_inventory(&self.witnesses, &self.original, &receipt)?; Ok(VerifiedHealth { signed_sha256: hash(&serde_json::to_vec(signed)?), diff --git a/docs/agent-updater-health-client.md b/docs/agent-updater-health-client.md index 1fcac49..fd21da9 100644 --- a/docs/agent-updater-health-client.md +++ b/docs/agent-updater-health-client.md @@ -87,3 +87,31 @@ gegeben; dieser prüft eigene Control-/Inventar-/Prozesszeugen unabhängig. Der zuerst qualifizierte Deliveryschnitt oben bleibt historisch. Änderungen und tatsächliche lokale Folgeprüfungen sowie offene native Gates stehen in [agent-updater-runtime-source.md](agent-updater-runtime-source.md). + +## Exakte lokale Applyspanne statt Sekundenquantisierung + +Nachtrag2026-11-15: Der unveränderte eingefrorene Healthreceipt enthält +`signed_sync_applied_at` weiterhin als `u64` und unverändert als +`floor(confirming.issued_at)`. Dieser Scalar erteilt keine Zeitfreigabe. +Der neue lesende Coregetter `update_health_sync::read_round` prüft bounded +die vorhandenen Originalrequest-Protobufs, SignedResponsebytes und tatsächlich +im selben HelperSync-Commit gespeicherten Appliedwitnesses. Die bestehende +Signatur-/Kontextprüfung läuft unabhängig erneut; es wird weder ein ACKboolean +noch eine Callerzeit akzeptiert und kein Journal/Ready/Lease neu geschrieben. + +`verified_span` findet genau ein tatsächlich angewandtes bestätigendes D2 mit +beiden vorherigen Decision-IDs gleich D1, derselben Originalgeräte-/Boot-/ +Verbindungsbindung und identischem ServicesStarted-Präfix. Dieser Präfix muss +auch unverändert im aktuellen Originalhandoff enthalten sein. Der Fullguard +prüft jetzt `D1.issued_at <= actual_local_applied_at <= D2.issued_at` mit +den exakten ursprünglichen DateTimes. Erst danach folgt die bestehende lokale +Inventar-/Dienst-/Originalhealthprüfung. Der u64-Wert wird ausschließlich auf +Gleichheit mit dem ursprünglichen D2-Sekundenfloor geprüft. + +Der enge tatsächliche Grenzfall akzeptiert beide exakten Endpunkte und verweigert +eine um1ns zu frühe oder zu späte Anwendung trotz identischem Sekundenfloor. +Der vorhandene vollständige negative HelperSyncfall prüft zusätzlich den echten +Original-Roundgetter nach Signatur-/Applypersistierung. Lokaler Quell-/Testbeleg: +`devstate/updater-health-exact-span-6879f7cc-f284-4f50-b3e6-de321f3af6a2/`. +Kein nativer Health-/Applynachweis und keine Änderung an Frozenreceipt-/ +Kernelbytes oder an der WebUI-Grenze.