Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f01e81418e | ||
|
|
d4cbc40979 | ||
|
|
478ee1fc1a |
@@ -205,6 +205,7 @@ pub struct HelperSync<V = DefaultVerifier> {
|
|||||||
born: DateTime<Utc>,
|
born: DateTime<Utc>,
|
||||||
capture: Option<Capture>,
|
capture: Option<Capture>,
|
||||||
lease: Option<Lease>,
|
lease: Option<Lease>,
|
||||||
|
integrity: Option<crate::ledger::IntegrityMonitor>,
|
||||||
}
|
}
|
||||||
impl HelperSync<DefaultVerifier> {
|
impl HelperSync<DefaultVerifier> {
|
||||||
/// No persisted ready bit can open a newly started helper.
|
/// No persisted ready bit can open a newly started helper.
|
||||||
@@ -222,6 +223,7 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
|||||||
born: clock.now()?,
|
born: clock.now()?,
|
||||||
capture: None,
|
capture: None,
|
||||||
lease: None,
|
lease: None,
|
||||||
|
integrity: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/// Select exactly one protected helper execution identity, closing leases.
|
/// Select exactly one protected helper execution identity, closing leases.
|
||||||
@@ -230,6 +232,12 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
|||||||
self.identity = identity;
|
self.identity = identity;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
/// Use with one protected SQLite connection retained for this instance.
|
||||||
|
/// The native owner must run a full check before any new host effect.
|
||||||
|
pub fn with_periodic_idle_integrity_checks(mut self) -> Self {
|
||||||
|
self.integrity = Some(crate::ledger::IntegrityMonitor::default());
|
||||||
|
self
|
||||||
|
}
|
||||||
pub fn close(&mut self) {
|
pub fn close(&mut self) {
|
||||||
self.lease = None;
|
self.lease = None;
|
||||||
self.capture = None;
|
self.capture = None;
|
||||||
@@ -288,7 +296,8 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
|||||||
ledger_fingerprint: lease.fingerprint.clone(),
|
ledger_fingerprint: lease.fingerprint.clone(),
|
||||||
captured_at: capture.captured_at,
|
captured_at: capture.captured_at,
|
||||||
};
|
};
|
||||||
self.verify_no_start_anchor(db, guard, &anchor, clock, boot)?;
|
let audit_history = self.integrity.is_none();
|
||||||
|
self.verify_no_start_anchor(db, guard, &anchor, clock, boot, audit_history)?;
|
||||||
Ok(anchor)
|
Ok(anchor)
|
||||||
}
|
}
|
||||||
/// Consume exactly the approved update attempt and delegate its ORIGINAL
|
/// Consume exactly the approved update attempt and delegate its ORIGINAL
|
||||||
@@ -389,9 +398,9 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
|||||||
tx.commit()?;
|
tx.commit()?;
|
||||||
return Ok(outcome);
|
return Ok(outcome);
|
||||||
}
|
}
|
||||||
self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?;
|
self.verify_no_start_anchor(&tx, guard, anchor, clock, boot, true)?;
|
||||||
let observation = guard.verify_no_dispatch(request, anchor)?;
|
let observation = guard.verify_no_dispatch(request, anchor)?;
|
||||||
let now = self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?;
|
let now = self.verify_no_start_anchor(&tx, guard, anchor, clock, boot, true)?;
|
||||||
ensure!(
|
ensure!(
|
||||||
!observation.intent_id.is_nil()
|
!observation.intent_id.is_nil()
|
||||||
&& digest(&observation.intent_sha256)
|
&& digest(&observation.intent_sha256)
|
||||||
@@ -421,6 +430,7 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
|||||||
anchor: &NoStartAnchor,
|
anchor: &NoStartAnchor,
|
||||||
clock: &impl Clock,
|
clock: &impl Clock,
|
||||||
boot: &impl BootSource,
|
boot: &impl BootSource,
|
||||||
|
audit_history: bool,
|
||||||
) -> Result<DateTime<Utc>> {
|
) -> Result<DateTime<Utc>> {
|
||||||
guard.check(db)?;
|
guard.check(db)?;
|
||||||
let now = clock.now()?;
|
let now = clock.now()?;
|
||||||
@@ -445,8 +455,10 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
|||||||
&& ledger_fingerprint(db)? == anchor.ledger_fingerprint,
|
&& ledger_fingerprint(db)? == anchor.ledger_fingerprint,
|
||||||
"DENIAL_HELPER_LINEAGE_CHANGED"
|
"DENIAL_HELPER_LINEAGE_CHANGED"
|
||||||
);
|
);
|
||||||
// Exact equality is deliberate: a first implementation refuses even
|
// Idle anchor capture checks the current signed head. The native
|
||||||
// legitimate intervening progress rather than inventing lineage links.
|
// executor audits the full history before execution, and abandonment
|
||||||
|
// always audits it under the same transaction before recording evidence.
|
||||||
|
if audit_history {
|
||||||
verify_checkpoint_repair_history_with(
|
verify_checkpoint_repair_history_with(
|
||||||
db,
|
db,
|
||||||
&self.binding,
|
&self.binding,
|
||||||
@@ -454,6 +466,7 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
|||||||
self.identity,
|
self.identity,
|
||||||
&self.verifier,
|
&self.verifier,
|
||||||
)?;
|
)?;
|
||||||
|
}
|
||||||
let (response, raw): (Vec<u8>, Vec<u8>) = db.query_row(
|
let (response, raw): (Vec<u8>, Vec<u8>) = db.query_row(
|
||||||
"SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1",
|
"SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1",
|
||||||
[anchor.decision_id.to_string()],
|
[anchor.decision_id.to_string()],
|
||||||
@@ -483,8 +496,12 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
|||||||
&& decision.helper_journal_id == Some(anchor.journal_id)
|
&& decision.helper_journal_id == Some(anchor.journal_id)
|
||||||
&& decision.helper_checkpoint == anchor.checkpoint
|
&& decision.helper_checkpoint == anchor.checkpoint
|
||||||
&& decision.connection_generation == anchor.connection_generation
|
&& decision.connection_generation == anchor.connection_generation
|
||||||
|
&& decision.request_hash == anchor.sync_request_sha256
|
||||||
|
&& capture.journal_id == anchor.journal_id
|
||||||
|
&& capture.checkpoint == anchor.checkpoint
|
||||||
&& capture.captured_at == anchor.captured_at
|
&& capture.captured_at == anchor.captured_at
|
||||||
&& capture.boot_id == anchor.boot_id
|
&& capture.boot_id == anchor.boot_id
|
||||||
|
&& capture.captured_at <= decision.issued_at
|
||||||
&& capture.captured_at <= now,
|
&& capture.captured_at <= now,
|
||||||
"DENIAL_CONFIRMED_READY_REQUIRED"
|
"DENIAL_CONFIRMED_READY_REQUIRED"
|
||||||
);
|
);
|
||||||
@@ -523,7 +540,12 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
|||||||
);
|
);
|
||||||
let boot_id = boot.current_boot()?;
|
let boot_id = boot.current_boot()?;
|
||||||
ensure!(!boot_id.is_nil(), "BOOT_ID_REQUIRED");
|
ensure!(!boot_id.is_nil(), "BOOT_ID_REQUIRED");
|
||||||
|
if let Some(integrity) = &mut self.integrity {
|
||||||
|
integrity.check(db, false, "ROOT_LEDGER_INTEGRITY")?;
|
||||||
|
initialize_schema(db, &self.binding)?;
|
||||||
|
} else {
|
||||||
initialize(db, &self.binding)?;
|
initialize(db, &self.binding)?;
|
||||||
|
}
|
||||||
let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||||
let meta = metadata(&tx)?;
|
let meta = metadata(&tx)?;
|
||||||
ensure!(meta.fault.is_none(), "HELPER_MANUAL_RECOVERY_REQUIRED");
|
ensure!(meta.fault.is_none(), "HELPER_MANUAL_RECOVERY_REQUIRED");
|
||||||
@@ -1031,13 +1053,17 @@ struct Meta {
|
|||||||
agent_checkpoint: u64,
|
agent_checkpoint: u64,
|
||||||
}
|
}
|
||||||
fn initialize(db: &Connection, binding: &Binding) -> Result<()> {
|
fn initialize(db: &Connection, binding: &Binding) -> Result<()> {
|
||||||
let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?;
|
crate::ledger::check_integrity(db, "ROOT_LEDGER_INTEGRITY")?;
|
||||||
ensure!(check == "ok", "ROOT_LEDGER_INTEGRITY");
|
initialize_schema(db, binding)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn initialize_schema(db: &Connection, binding: &Binding) -> Result<()> {
|
||||||
db.execute_batch("PRAGMA synchronous=FULL;
|
db.execute_batch("PRAGMA synchronous=FULL;
|
||||||
CREATE TABLE IF NOT EXISTS helper_sync_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,binding_sha256 TEXT NOT NULL,last_decision TEXT,fault TEXT,agent_journal_id TEXT,agent_checkpoint INTEGER NOT NULL DEFAULT 0);
|
CREATE TABLE IF NOT EXISTS helper_sync_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,binding_sha256 TEXT NOT NULL,last_decision TEXT,fault TEXT,agent_journal_id TEXT,agent_checkpoint INTEGER NOT NULL DEFAULT 0);
|
||||||
CREATE TABLE IF NOT EXISTS helper_sync_rows(grant_id TEXT PRIMARY KEY,revision INTEGER NOT NULL,fingerprint TEXT NOT NULL);
|
CREATE TABLE IF NOT EXISTS helper_sync_rows(grant_id TEXT PRIMARY KEY,revision INTEGER NOT NULL,fingerprint TEXT NOT NULL);
|
||||||
CREATE TABLE IF NOT EXISTS helper_sync_captures(checkpoint INTEGER PRIMARY KEY,snapshot BLOB NOT NULL,fingerprint TEXT NOT NULL,captured_at TEXT NOT NULL);
|
CREATE TABLE IF NOT EXISTS helper_sync_captures(checkpoint INTEGER PRIMARY KEY,snapshot BLOB NOT NULL,fingerprint TEXT NOT NULL,captured_at TEXT NOT NULL);
|
||||||
CREATE TABLE IF NOT EXISTS helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL);")?;
|
CREATE TABLE IF NOT EXISTS helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL);
|
||||||
|
CREATE INDEX IF NOT EXISTS helper_sync_decisions_checkpoint ON helper_sync_decisions(checkpoint);")?;
|
||||||
let legacy_captures: bool = db.query_row(
|
let legacy_captures: bool = db.query_row(
|
||||||
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='helper_sync_captures')",
|
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='helper_sync_captures')",
|
||||||
[], |row| row.get(0),
|
[], |row| row.get(0),
|
||||||
@@ -1175,23 +1201,23 @@ pub fn verify_checkpoint_repair_history_with(
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let meta = metadata(db)?;
|
let meta = metadata(db)?;
|
||||||
validate_history(db, binding, &meta, meta.generation, verifier)?;
|
validate_history(db, binding, &meta, meta.generation, verifier)?;
|
||||||
|
let entry_count: u64 = db.query_row(
|
||||||
|
"SELECT count(*) FROM helper_sync_decisions",
|
||||||
|
[],
|
||||||
|
|row| row.get(0),
|
||||||
|
)?;
|
||||||
|
ensure!(entry_count <= 100000, "REPAIR_HISTORY_LIMIT");
|
||||||
let mut statement = db.prepare(
|
let mut statement = db.prepare(
|
||||||
"SELECT response,request,checkpoint FROM helper_sync_decisions ORDER BY checkpoint",
|
"SELECT response,request,checkpoint FROM helper_sync_decisions ORDER BY checkpoint",
|
||||||
)?;
|
)?;
|
||||||
let entries = statement
|
let mut entries = statement.query([])?;
|
||||||
.query_map([], |r| {
|
|
||||||
Ok((
|
|
||||||
r.get::<_, Vec<u8>>(0)?,
|
|
||||||
r.get::<_, Vec<u8>>(1)?,
|
|
||||||
r.get::<_, u64>(2)?,
|
|
||||||
))
|
|
||||||
})?
|
|
||||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
|
||||||
ensure!(entries.len() <= 100000, "REPAIR_HISTORY_LIMIT");
|
|
||||||
let mut previous = None;
|
let mut previous = None;
|
||||||
let mut previous_checkpoint = 0;
|
let mut previous_checkpoint = 0;
|
||||||
let mut previous_agent_checkpoint = 0;
|
let mut previous_agent_checkpoint = 0;
|
||||||
for (signed, raw, checkpoint) in entries {
|
while let Some(entry) = entries.next()? {
|
||||||
|
let signed: Vec<u8> = entry.get(0)?;
|
||||||
|
let raw: Vec<u8> = entry.get(1)?;
|
||||||
|
let checkpoint: u64 = entry.get(2)?;
|
||||||
let response: SignedResponse = serde_json::from_slice(&signed)?;
|
let response: SignedResponse = serde_json::from_slice(&signed)?;
|
||||||
let parsed: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?;
|
let parsed: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?;
|
||||||
let decision = verify(verifier, binding, &response, parsed.issued_at)?;
|
let decision = verify(verifier, binding, &response, parsed.issued_at)?;
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ use rmm_domain::{
|
|||||||
jobs::hash,
|
jobs::hash,
|
||||||
reconciliation::{ReconciledOutcome, canonical_outcome_bytes},
|
reconciliation::{ReconciledOutcome, canonical_outcome_bytes},
|
||||||
};
|
};
|
||||||
use rusqlite::{OptionalExtension, TransactionBehavior, params};
|
use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
/// Finite 1 GiB bound for durable agent/helper history. Unexpected page sizes
|
/// Finite 1 GiB bound for durable agent/helper history. Unexpected page sizes
|
||||||
@@ -29,11 +30,48 @@ pub fn initialize_ledger(db: &rusqlite::Connection) -> Result<()> {
|
|||||||
CREATE TABLE IF NOT EXISTS consumed(grant_id TEXT PRIMARY KEY, agent TEXT NOT NULL, generation INTEGER NOT NULL,
|
CREATE TABLE IF NOT EXISTS consumed(grant_id TEXT PRIMARY KEY, agent TEXT NOT NULL, generation INTEGER NOT NULL,
|
||||||
job TEXT NOT NULL, attempt TEXT NOT NULL, result BLOB, UNIQUE(agent,generation,job,attempt));
|
job TEXT NOT NULL, attempt TEXT NOT NULL, result BLOB, UNIQUE(agent,generation,job,attempt));
|
||||||
CREATE TABLE IF NOT EXISTS execution_evidence(grant_id TEXT PRIMARY KEY, start BLOB NOT NULL, outcome BLOB);")?;
|
CREATE TABLE IF NOT EXISTS execution_evidence(grant_id TEXT PRIMARY KEY, start BLOB NOT NULL, outcome BLOB);")?;
|
||||||
let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?;
|
check_integrity(db, "JOURNAL_INTEGRITY")
|
||||||
ensure!(check == "ok", "JOURNAL_INTEGRITY");
|
}
|
||||||
|
|
||||||
|
pub fn check_integrity(db: &Connection, error_code: &'static str) -> Result<()> {
|
||||||
|
let check: String = db.query_row("PRAGMA integrity_check", [], |row| row.get(0))?;
|
||||||
|
ensure!(check == "ok", "{error_code}");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Full integrity scans for an owner holding one protected, long-lived SQLite
|
||||||
|
/// connection. External commits and schema changes require another scan even
|
||||||
|
/// when the interval has not elapsed. No cached result authorizes an effect.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub(crate) struct IntegrityMonitor {
|
||||||
|
checked: Option<(u64, u64, Instant)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntegrityMonitor {
|
||||||
|
pub(crate) fn check(
|
||||||
|
&mut self,
|
||||||
|
db: &Connection,
|
||||||
|
required: bool,
|
||||||
|
error_code: &'static str,
|
||||||
|
) -> Result<()> {
|
||||||
|
let data: u64 = db.query_row("PRAGMA data_version", [], |row| row.get(0))?;
|
||||||
|
let schema: u64 = db.query_row("PRAGMA schema_version", [], |row| row.get(0))?;
|
||||||
|
if !required
|
||||||
|
&& self.checked.is_some_and(|(previous_data, previous_schema, checked)| {
|
||||||
|
data == previous_data
|
||||||
|
&& schema == previous_schema
|
||||||
|
&& checked.elapsed() < Duration::from_secs(300)
|
||||||
|
})
|
||||||
|
{
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
self.checked = None;
|
||||||
|
check_integrity(db, error_code)?;
|
||||||
|
self.checked = Some((data, schema, Instant::now()));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Before a human WebUI closure replaces the unresolved local outcome, keep the
|
/// Before a human WebUI closure replaces the unresolved local outcome, keep the
|
||||||
/// exact original outcome bytes once. A differing second copy fails closed.
|
/// exact original outcome bytes once. A differing second copy fails closed.
|
||||||
pub(crate) fn retain_closure_original(
|
pub(crate) fn retain_closure_original(
|
||||||
@@ -379,3 +417,34 @@ pub fn start_witness_bytes(
|
|||||||
"reauthentication_required":true,"postconditions_passed":false});
|
"reauthentication_required":true,"postconditions_passed":false});
|
||||||
Ok(serde_json::to_vec(&evidence)?)
|
Ok(serde_json::to_vec(&evidence)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod integrity_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn idle_integrity_reuses_the_check_but_reaudits_external_changes_and_expiry() {
|
||||||
|
let path = std::env::temp_dir().join(format!("rmm-integrity-{}.sqlite", Uuid::new_v4()));
|
||||||
|
{
|
||||||
|
let db = Connection::open(&path).unwrap();
|
||||||
|
db.execute_batch("CREATE TABLE sample(id INTEGER PRIMARY KEY);").unwrap();
|
||||||
|
let mut monitor = IntegrityMonitor::default();
|
||||||
|
monitor.check(&db, false, "JOURNAL_INTEGRITY").unwrap();
|
||||||
|
let first = monitor.checked.unwrap();
|
||||||
|
db.execute("INSERT INTO sample VALUES (1)", []).unwrap();
|
||||||
|
monitor.check(&db, false, "JOURNAL_INTEGRITY").unwrap();
|
||||||
|
assert_eq!(monitor.checked, Some(first));
|
||||||
|
|
||||||
|
let peer = Connection::open(&path).unwrap();
|
||||||
|
peer.execute("INSERT INTO sample VALUES (2)", []).unwrap();
|
||||||
|
monitor.check(&db, false, "JOURNAL_INTEGRITY").unwrap();
|
||||||
|
let changed = monitor.checked.unwrap();
|
||||||
|
assert_ne!(changed.0, first.0);
|
||||||
|
|
||||||
|
monitor.checked = Some((changed.0, changed.1, changed.2 - Duration::from_secs(300)));
|
||||||
|
monitor.check(&db, false, "JOURNAL_INTEGRITY").unwrap();
|
||||||
|
assert!(monitor.checked.unwrap().2 >= changed.2);
|
||||||
|
}
|
||||||
|
std::fs::remove_file(path).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ pub struct ClientSync<V = DefaultVerifier> {
|
|||||||
verifier: V,
|
verifier: V,
|
||||||
helper_identity: HelperIdentity,
|
helper_identity: HelperIdentity,
|
||||||
pending: Option<PreparedSync>,
|
pending: Option<PreparedSync>,
|
||||||
|
integrity: Option<crate::ledger::IntegrityMonitor>,
|
||||||
channel: Option<Channel>,
|
channel: Option<Channel>,
|
||||||
gate: Option<Gate>,
|
gate: Option<Gate>,
|
||||||
}
|
}
|
||||||
@@ -117,6 +118,7 @@ impl<V: Ed25519Verifier> ClientSync<V> {
|
|||||||
verifier,
|
verifier,
|
||||||
helper_identity: HelperIdentity::Root,
|
helper_identity: HelperIdentity::Root,
|
||||||
pending: None,
|
pending: None,
|
||||||
|
integrity: None,
|
||||||
channel: None,
|
channel: None,
|
||||||
gate: None,
|
gate: None,
|
||||||
}
|
}
|
||||||
@@ -127,6 +129,11 @@ impl<V: Ed25519Verifier> ClientSync<V> {
|
|||||||
self.helper_identity = identity;
|
self.helper_identity = identity;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
/// Use with one protected SQLite connection retained for this instance.
|
||||||
|
pub fn with_periodic_idle_integrity_checks(mut self) -> Self {
|
||||||
|
self.integrity = Some(crate::ledger::IntegrityMonitor::default());
|
||||||
|
self
|
||||||
|
}
|
||||||
/// Invoke on every authenticated/fenced ServerHello or transport loss.
|
/// Invoke on every authenticated/fenced ServerHello or transport loss.
|
||||||
pub fn close(&mut self) {
|
pub fn close(&mut self) {
|
||||||
self.gate = None;
|
self.gate = None;
|
||||||
@@ -606,7 +613,17 @@ impl<V: Ed25519Verifier> ClientSync<V> {
|
|||||||
channel.connection_generation > 0 && !channel.boot_id.is_nil(),
|
channel.connection_generation > 0 && !channel.boot_id.is_nil(),
|
||||||
"UNAUTHENTICATED_CHANNEL"
|
"UNAUTHENTICATED_CHANNEL"
|
||||||
);
|
);
|
||||||
|
if let Some(integrity) = &mut self.integrity {
|
||||||
|
let work_pending: bool = db.query_row(
|
||||||
|
"SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown'))",
|
||||||
|
[],
|
||||||
|
|row| row.get(0),
|
||||||
|
)?;
|
||||||
|
integrity.check(db, work_pending, "JOURNAL_INTEGRITY")?;
|
||||||
|
initialize_schema(db)?;
|
||||||
|
} else {
|
||||||
initialize(db)?;
|
initialize(db)?;
|
||||||
|
}
|
||||||
let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||||
let meta = metadata(&tx)?;
|
let meta = metadata(&tx)?;
|
||||||
ensure!(meta.fault.is_none(), "SYNC_MANUAL_RECOVERY_REQUIRED");
|
ensure!(meta.fault.is_none(), "SYNC_MANUAL_RECOVERY_REQUIRED");
|
||||||
@@ -1147,8 +1164,11 @@ impl<V: Ed25519Verifier> ClientSync<V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn initialize(db: &Connection) -> Result<()> {
|
pub fn initialize(db: &Connection) -> Result<()> {
|
||||||
let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?;
|
crate::ledger::check_integrity(db, "JOURNAL_INTEGRITY")?;
|
||||||
ensure!(check == "ok", "JOURNAL_INTEGRITY");
|
initialize_schema(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn initialize_schema(db: &Connection) -> Result<()> {
|
||||||
db.execute_batch("PRAGMA synchronous=FULL;
|
db.execute_batch("PRAGMA synchronous=FULL;
|
||||||
CREATE TABLE IF NOT EXISTS reconcile_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,high_sequence INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,last_decision TEXT,pending_request TEXT,helper_id TEXT,helper_checkpoint INTEGER NOT NULL DEFAULT 0,fault TEXT);
|
CREATE TABLE IF NOT EXISTS reconcile_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,high_sequence INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,last_decision TEXT,pending_request TEXT,helper_id TEXT,helper_checkpoint INTEGER NOT NULL DEFAULT 0,fault TEXT);
|
||||||
CREATE TABLE IF NOT EXISTS reconcile_requests(id TEXT PRIMARY KEY,request BLOB NOT NULL,envelope BLOB NOT NULL,generation INTEGER NOT NULL,response BLOB);
|
CREATE TABLE IF NOT EXISTS reconcile_requests(id TEXT PRIMARY KEY,request BLOB NOT NULL,envelope BLOB NOT NULL,generation INTEGER NOT NULL,response BLOB);
|
||||||
|
|||||||
@@ -466,6 +466,31 @@ struct Fixture {
|
|||||||
identity: HelperIdentity,
|
identity: HelperIdentity,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn idle_anchor_checks_the_head_but_abandonment_rejects_damaged_history() {
|
||||||
|
let (mut f, sync, _, request, mut guard) = denial_case();
|
||||||
|
let mut sync = sync.with_periodic_idle_integrity_checks();
|
||||||
|
f.clock.now += Duration::seconds(1);
|
||||||
|
let capture = f.snapshot(&mut sync, 7);
|
||||||
|
let raw = f.request(&capture);
|
||||||
|
let ready = f.decision(&capture, &raw, 7, true);
|
||||||
|
assert!(f.apply(&mut sync, &ready, &raw).unwrap().ready);
|
||||||
|
guard.intent_recorded_at = f.clock.now;
|
||||||
|
f.db.execute(
|
||||||
|
"UPDATE helper_sync_decisions SET response=X'00' WHERE checkpoint < ?1",
|
||||||
|
[capture.checkpoint],
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let anchor = sync.no_start_anchor(&f.db, &guard, 7, &f.clock, &f.boot).unwrap();
|
||||||
|
guard.anchor_sha256 = hash(&serde_json::to_vec(&anchor).unwrap());
|
||||||
|
let before = denial_ledger_bytes(&f.db);
|
||||||
|
assert!(
|
||||||
|
sync.abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot)
|
||||||
|
.is_err()
|
||||||
|
);
|
||||||
|
assert_eq!(denial_ledger_bytes(&f.db), before);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor() {
|
fn checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor() {
|
||||||
let mut f = Fixture::new();
|
let mut f = Fixture::new();
|
||||||
|
|||||||
@@ -903,9 +903,6 @@ impl SystemHelper {
|
|||||||
RuntimeClock::enrolled_windows_update_helper(settings.binding.clone(), settings.helper.clone())
|
RuntimeClock::enrolled_windows_update_helper(settings.binding.clone(), settings.helper.clone())
|
||||||
} else { RuntimeClock::new(rmm_time_attestation::Role::SystemHelper) }.for_generation(0);
|
} else { RuntimeClock::new(rmm_time_attestation::Role::SystemHelper) }.for_generation(0);
|
||||||
let boot = WindowsBoot::capture()?;
|
let boot = WindowsBoot::capture()?;
|
||||||
let sync = HelperSync::with_verifier(settings.binding.clone(), DalekVerifier, &clock)
|
|
||||||
.ok()
|
|
||||||
.map(|s| s.with_helper_identity(HelperIdentity::System));
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
settings,
|
settings,
|
||||||
pin,
|
pin,
|
||||||
@@ -914,7 +911,8 @@ impl SystemHelper {
|
|||||||
db,
|
db,
|
||||||
clock,
|
clock,
|
||||||
boot,
|
boot,
|
||||||
sync,
|
// The initial connection creates the reconciler using its own qualified time.
|
||||||
|
sync: None,
|
||||||
last_diagnostic: None,
|
last_diagnostic: None,
|
||||||
last_apply_diagnostic: None,
|
last_apply_diagnostic: None,
|
||||||
unsettled_msi: None,
|
unsettled_msi: None,
|
||||||
@@ -1008,14 +1006,17 @@ impl SystemHelper {
|
|||||||
}
|
}
|
||||||
fn sync(&mut self) -> Result<&mut HelperSync<DalekVerifier>> {
|
fn sync(&mut self) -> Result<&mut HelperSync<DalekVerifier>> {
|
||||||
if self.sync.is_none() {
|
if self.sync.is_none() {
|
||||||
self.sync = Some(
|
let sync = HelperSync::with_verifier(
|
||||||
HelperSync::with_verifier(
|
|
||||||
self.settings.binding.clone(),
|
self.settings.binding.clone(),
|
||||||
DalekVerifier,
|
DalekVerifier,
|
||||||
&self.clock,
|
&self.clock,
|
||||||
)?
|
)?
|
||||||
.with_helper_identity(HelperIdentity::System),
|
.with_helper_identity(HelperIdentity::System);
|
||||||
);
|
self.sync = Some(if self.settings.managed_scripts() {
|
||||||
|
sync.with_periodic_idle_integrity_checks()
|
||||||
|
} else {
|
||||||
|
sync
|
||||||
|
});
|
||||||
}
|
}
|
||||||
self.sync.as_mut().context("SYSTEM_SYNC_CLOSED")
|
self.sync.as_mut().context("SYSTEM_SYNC_CLOSED")
|
||||||
}
|
}
|
||||||
@@ -1290,6 +1291,16 @@ impl SystemHelper {
|
|||||||
}
|
}
|
||||||
ensure!(self.settings.enable_effects, "SYSTEM_EFFECTS_DISABLED");
|
ensure!(self.settings.enable_effects, "SYSTEM_EFFECTS_DISABLED");
|
||||||
verified_window(&self.settings.binding, request, &self.clock)?;
|
verified_window(&self.settings.binding, request, &self.clock)?;
|
||||||
|
if self.settings.managed_scripts() {
|
||||||
|
ledger::check_integrity(&self.db, "ROOT_LEDGER_INTEGRITY")?;
|
||||||
|
rmm_agent_core::helper_reconcile::verify_checkpoint_repair_history_with(
|
||||||
|
&self.db,
|
||||||
|
&self.settings.binding,
|
||||||
|
None,
|
||||||
|
HelperIdentity::System,
|
||||||
|
&DalekVerifier,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
self.sync()?;
|
self.sync()?;
|
||||||
ensure!(
|
ensure!(
|
||||||
self.sync.as_ref().unwrap().can_start(
|
self.sync.as_ref().unwrap().can_start(
|
||||||
|
|||||||
@@ -1291,8 +1291,8 @@ impl Agent {
|
|||||||
&& !self.helper_apply_unconfirmed
|
&& !self.helper_apply_unconfirmed
|
||||||
&& !self.prepared
|
&& !self.prepared
|
||||||
&& self.running.is_none()
|
&& self.running.is_none()
|
||||||
&& !self.journal.connection().query_row("SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state IN ('requesting','running','verifying'))",[],|r|r.get::<_,bool>(0))?
|
|
||||||
&& self.helper_probe_at.elapsed() >= Duration::from_secs(30)
|
&& self.helper_probe_at.elapsed() >= Duration::from_secs(30)
|
||||||
|
&& !self.journal.connection().query_row("SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state IN ('requesting','running','verifying'))",[],|r|r.get::<_,bool>(0))?
|
||||||
{
|
{
|
||||||
let agent = self.identity.agent_id.clone();
|
let agent = self.identity.agent_id.clone();
|
||||||
let asset = self.identity.asset_id.clone();
|
let asset = self.identity.asset_id.clone();
|
||||||
@@ -2663,8 +2663,9 @@ impl Agent {
|
|||||||
let mut heartbeat = Instant::now() - Duration::from_secs(60);
|
let mut heartbeat = Instant::now() - Duration::from_secs(60);
|
||||||
let mut previous_cpu = None;
|
let mut previous_cpu = None;
|
||||||
let mut heartbeat_after = Duration::from_secs(30);
|
let mut heartbeat_after = Duration::from_secs(30);
|
||||||
let mut tick = tokio::time::interval(Duration::from_millis(20));
|
let active_tick = Duration::from_millis(20);
|
||||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
let idle_tick = Duration::from_millis(250);
|
||||||
|
let mut next_tick = tokio::time::Instant::now();
|
||||||
let mut readiness_at = Instant::now() - Duration::from_secs(5);
|
let mut readiness_at = Instant::now() - Duration::from_secs(5);
|
||||||
#[cfg(feature = "system-helper")]
|
#[cfg(feature = "system-helper")]
|
||||||
let mut update_control_at = Instant::now() - Duration::from_secs(2);
|
let mut update_control_at = Instant::now() - Duration::from_secs(2);
|
||||||
@@ -2675,9 +2676,19 @@ impl Agent {
|
|||||||
running.cancel.store(true, Ordering::SeqCst);
|
running.cancel.store(true, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let control_pending = self.running.is_some()
|
||||||
|
|| self.prepared
|
||||||
|
|| self.deferred_sync.is_some()
|
||||||
|
|| self.deferred_permit.is_some()
|
||||||
|
|| self.helper_control_pending();
|
||||||
|
#[cfg(feature = "system-helper")]
|
||||||
|
let control_pending = control_pending || self.helper_probe.is_some();
|
||||||
|
let cadence = if control_pending { active_tick } else { idle_tick };
|
||||||
|
next_tick = next_tick.min(tokio::time::Instant::now() + cadence);
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
Some(completion)=self.completed_rx.recv()=>{self.completed(generation,completion)?;needs_sync=true;},
|
Some(completion)=self.completed_rx.recv()=>{self.completed(generation,completion)?;needs_sync=true;},
|
||||||
_=tick.tick()=>{
|
_=tokio::time::sleep_until(next_tick)=>{
|
||||||
|
next_tick = tokio::time::Instant::now() + cadence;
|
||||||
if !stop.load(Ordering::SeqCst) && self.poll_sync_apply(generation,true).is_err(){self.close_gate();needs_sync=true;}
|
if !stop.load(Ordering::SeqCst) && self.poll_sync_apply(generation,true).is_err(){self.close_gate();needs_sync=true;}
|
||||||
#[cfg(feature = "system-helper")]
|
#[cfg(feature = "system-helper")]
|
||||||
if !stop.load(Ordering::SeqCst) && self.poll_helper(&channel,generation).await.is_err() {self.close_gate();needs_sync=self.sync.is_some();}
|
if !stop.load(Ordering::SeqCst) && self.poll_helper(&channel,generation).await.is_err() {self.close_gate();needs_sync=self.sync.is_some();}
|
||||||
@@ -2709,7 +2720,9 @@ impl Agent {
|
|||||||
if self.sync.is_some() && !self.prepared && !self.helper_control_busy_for_sync() && rmm_agent_windows::job_readiness::refresh_due(needs_sync,sync_at.elapsed(),self.sync.as_ref().and_then(|s|s.validity_deadline()),self.clock.reading().ok().map(|r|r.upper)) {
|
if self.sync.is_some() && !self.prepared && !self.helper_control_busy_for_sync() && rmm_agent_windows::job_readiness::refresh_due(needs_sync,sync_at.elapsed(),self.sync.as_ref().and_then(|s|s.validity_deadline()),self.clock.reading().ok().map(|r|r.upper)) {
|
||||||
if self.start_sync(&channel,generation).await.is_err(){self.close_gate();needs_sync=true;}else{needs_sync=false;}sync_at=Instant::now();
|
if self.start_sync(&channel,generation).await.is_err(){self.close_gate();needs_sync=true;}else{needs_sync=false;}sync_at=Instant::now();
|
||||||
}
|
}
|
||||||
if Instant::now() >= permit_after && !needs_sync && !self.prepared && self.ready(generation) && jobs::events(self.journal.connection())?.is_empty() && self.journal.count()?==0 {
|
if Instant::now() >= permit_after && !needs_sync && !self.prepared && self.config.enable_jobs
|
||||||
|
&& self.journal.connection().prepare_cached("SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state='accepted' AND cancel_requested=0)")?.query_row([], |row| row.get::<_, bool>(0))?
|
||||||
|
&& self.ready(generation) && jobs::events(self.journal.connection())?.is_empty() && self.journal.count()?==0 {
|
||||||
// A transiently stale clock skips this tick; it must not end the session.
|
// A transiently stale clock skips this tick; it must not end the session.
|
||||||
if let Ok(now)=self.clock.now() && let Some(offer)=jobs::next_at(self.journal.connection(),now)? {
|
if let Ok(now)=self.clock.now() && let Some(offer)=jobs::next_at(self.journal.connection(),now)? {
|
||||||
let job=self.binding.as_ref().context("JOB_BINDING_REQUIRED")?.job(&offer.payload_bytes)?;
|
let job=self.binding.as_ref().context("JOB_BINDING_REQUIRED")?.job(&offer.payload_bytes)?;
|
||||||
@@ -3078,7 +3091,19 @@ async fn run_service_binding(path: &Path, stop: &'static AtomicBool, release_gua
|
|||||||
ensure!(rmm_agent_windows::echo_probe::protected_scope()? == *scope, "WINDOWS_ECHO_CONFIGURATION_CHANGED");
|
ensure!(rmm_agent_windows::echo_probe::protected_scope()? == *scope, "WINDOWS_ECHO_CONFIGURATION_CHANGED");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let sync=if config.enable_jobs && (!config.windows_echo_probe_only || config.echo_scope.is_some()) {binding.clone().map(|binding|reconcile::ClientSync::new(binding).with_helper_identity(reconcile::HelperIdentity::System))} else {None};
|
let sync = if config.enable_jobs && (!config.windows_echo_probe_only || config.echo_scope.is_some()) {
|
||||||
|
binding.clone().map(|binding| {
|
||||||
|
let sync = reconcile::ClientSync::new(binding)
|
||||||
|
.with_helper_identity(reconcile::HelperIdentity::System);
|
||||||
|
if config.localservice_time_policy.is_some() {
|
||||||
|
sync.with_periodic_idle_integrity_checks()
|
||||||
|
} else {
|
||||||
|
sync
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
let (completed_tx,completed_rx)=tokio::sync::mpsc::channel(1);
|
let (completed_tx,completed_rx)=tokio::sync::mpsc::channel(1);
|
||||||
let mut agent = Agent {
|
let mut agent = Agent {
|
||||||
config,
|
config,
|
||||||
|
|||||||
Reference in New Issue
Block a user