Verify health against exact retained original sync timestamps
Rust development slice / linux (push) Failing after 6s

This commit is contained in:
2026-11-15 01:07:19 +01:00
parent a56474805c
commit cfbf1f7bfc
5 changed files with 369 additions and 29 deletions
+1
View File
@@ -10,3 +10,4 @@ pub mod ledger;
pub mod reconcile;
pub mod update_handoff;
pub mod update_health_inventory;
pub mod update_health_sync;
+323
View File
@@ -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<Round> {
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<u8>, Vec<u8>) = 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<Utc>,
upper: DateTime<Utc>,
}
impl VerifiedSpan {
pub fn applied(&self) -> &AppliedUpdateSync {
&self.applied
}
pub fn lower(&self) -> DateTime<Utc> {
self.lower
}
pub fn upper(&self) -> DateTime<Utc> {
self.upper
}
}
pub fn verified_span(
db: &Connection,
guard: &impl ExecutorLedgerGuard,
binding: &Binding,
header: &Handoff,
receipt: &HealthReceipt,
health_key: [u8; 32],
) -> Result<VerifiedSpan> {
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::<rusqlite::Result<Vec<_>>>()?;
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<u8> = 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<Vec<u8>> {
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::<Vec<_>>();
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<Utc>,
actual: DateTime<Utc>,
upper: DateTime<Utc>,
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());
}
}
@@ -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 {
+6 -29
View File
@@ -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<u8> = 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)?),
+28
View File
@@ -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.