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());
}
}