Merge branch 'main' into codex/signed-clock
Rust development slice / linux (push) Failing after 6s

This commit is contained in:
2026-11-15 09:01:22 +01:00
37 changed files with 8431 additions and 97 deletions
Generated
+9
View File
@@ -2176,9 +2176,11 @@ name = "rmm-agent"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64 0.22.1",
"chrono", "chrono",
"fs2", "fs2",
"futures-util", "futures-util",
"hex",
"libc", "libc",
"openssl", "openssl",
"prost", "prost",
@@ -2189,6 +2191,7 @@ dependencies = [
"rmm-domain", "rmm-domain",
"rmm-privileged-helper", "rmm-privileged-helper",
"rmm-protocol", "rmm-protocol",
"rmm-updater-handoff",
"rusqlite", "rusqlite",
"rustls", "rustls",
"rustls-pemfile", "rustls-pemfile",
@@ -2313,14 +2316,20 @@ name = "rmm-privileged-helper"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64 0.22.1",
"chrono", "chrono",
"fs2", "fs2",
"hex", "hex",
"libc", "libc",
"openssl", "openssl",
"prost",
"rmm-agent-core", "rmm-agent-core",
"rmm-agent-linux", "rmm-agent-linux",
"rmm-domain", "rmm-domain",
"rmm-protocol",
"rmm-updater-handoff",
"rmm-updater-linux",
"rmm-updater-worker",
"rusqlite", "rusqlite",
"serde", "serde",
"serde_json", "serde_json",
+57
View File
@@ -204,6 +204,10 @@ impl<V: Ed25519Verifier> HelperSync<V> {
self.lease = None; self.lease = None;
self.capture = None; self.capture = None;
} }
/// Read-only bound of the existing confirmed lease; creates no readiness.
pub fn validity_deadline(&self) -> Option<DateTime<Utc>> {
self.lease.as_ref().map(|lease| lease.until)
}
pub fn active_generation(&self) -> Option<u64> { pub fn active_generation(&self) -> Option<u64> {
self.lease.as_ref().map(|lease| lease.generation) self.lease.as_ref().map(|lease| lease.generation)
} }
@@ -791,6 +795,48 @@ impl<V: Ed25519Verifier> HelperSync<V> {
boot: current_boot, boot: current_boot,
}), }),
)?; )?;
if decision
.attempts
.iter()
.any(|d| d.update_inflight.is_some())
{
use prost::Message;
let applied_at = clock.now()?;
ensure!(
applied_at >= now && applied_at < decision.valid_until,
"UPDATE_SYNC_EXPIRED_BEFORE_APPLY"
);
let response_raw = rmm_protocol::SyncResponse {
decision_bytes: response.decision_bytes.clone(),
signature: response.signature.clone(),
}
.encode_to_vec();
for decision_attempt in decision
.attempts
.iter()
.filter(|d| d.update_inflight.is_some())
{
let row = after
.iter()
.find(|r| {
r.job_id == decision_attempt.job_id
&& r.attempt_id == decision_attempt.attempt_id
&& !r.update_handoff_bytes.is_empty()
})
.context("UPDATE_APPLIED_ORIGINAL_ROW_REQUIRED")?;
crate::update_handoff::record_applied_sync(
&tx,
&crate::update_handoff::AppliedUpdateSync {
schema_version: 1,
grant_id: row.grant_id,
decision_id: decision.decision_id,
request_sha256: hash(raw_request),
response_protobuf_sha256: hash(&response_raw),
applied_at,
},
)?;
}
}
let fingerprint = ledger_fingerprint(&tx)?; let fingerprint = ledger_fingerprint(&tx)?;
tx.execute("INSERT INTO helper_sync_decisions(decision_id,checkpoint,response,request,fingerprint) VALUES(?1,?2,?3,?4,?5)",params![decision.decision_id.to_string(),decision.helper_checkpoint,serde_json::to_vec(response)?,raw_request,fingerprint])?; tx.execute("INSERT INTO helper_sync_decisions(decision_id,checkpoint,response,request,fingerprint) VALUES(?1,?2,?3,?4,?5)",params![decision.decision_id.to_string(),decision.helper_checkpoint,serde_json::to_vec(response)?,raw_request,fingerprint])?;
tx.execute("UPDATE helper_sync_meta SET last_decision=?1,fault=COALESCE(fault,?2),agent_journal_id=?3,agent_checkpoint=?4 WHERE id=1",params![decision.decision_id.to_string(),fault,decision.journal_id.to_string(),decision.checkpoint])?; tx.execute("UPDATE helper_sync_meta SET last_decision=?1,fault=COALESCE(fault,?2),agent_journal_id=?3,agent_checkpoint=?4 WHERE id=1",params![decision.decision_id.to_string(),fault,decision.journal_id.to_string(),decision.checkpoint])?;
@@ -1291,11 +1337,22 @@ fn ledger_fingerprint(db: &Connection) -> Result<String> {
Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?)) Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?))
})? })?
.collect::<rusqlite::Result<Vec<NativeObservationRow>>>()?; .collect::<rusqlite::Result<Vec<NativeObservationRow>>>()?;
let mut applied=db.prepare("SELECT grant_id,decision_id,witness_sha256 FROM agent_update_sync_applied ORDER BY grant_id,decision_id")?;
let applied = applied
.query_map([], |r| {
Ok((
r.get::<_, String>(0)?,
r.get::<_, String>(1)?,
r.get::<_, String>(2)?,
))
})?
.collect::<rusqlite::Result<Vec<_>>>()?;
return Ok(hash(&serde_json::to_vec(&( return Ok(hash(&serde_json::to_vec(&(
rows, rows,
updates, updates,
claims, claims,
observations, observations,
applied,
))?)); ))?));
} }
} }
+24
View File
@@ -394,3 +394,27 @@ pub fn permit_request(offer: &JobOffer, permit: &ExecutionPermit) -> Result<Requ
authorization_id: Uuid::parse_str(&offer.authorization_id)?, authorization_id: Uuid::parse_str(&offer.authorization_id)?,
}) })
} }
/// Read-only control scheduling after an updater intentionally restarted the
/// agent. This is NOT a handoff proof, approval, start grant or effect lease.
/// A fresh independently authenticated helper snapshot still decides whether
/// the exact original is inflight or must become Unknown; existing Unknown is
/// never returned as a candidate and no local outcome is changed here.
pub fn needs_update_control(
db: &Connection,
ledger: &Connection,
binding: &Binding,
) -> Result<bool> {
let mut candidates = 0;
for (offer, grant) in interrupted(db, ledger, binding)? {
let job = binding.job(&offer.payload_bytes)?;
if !matches!(job.action, rmm_domain::jobs::Action::UpdateAgent { .. }) || grant.is_empty() {
continue;
}
if recover_interrupted_outcome(ledger, binding, &offer, &grant)?.is_none() {
candidates += 1;
}
}
ensure!(candidates <= 1, "UPDATE_MULTIPLE_PENDING_CONTROLS");
Ok(candidates == 1)
}
+2
View File
@@ -9,3 +9,5 @@ pub mod jobs;
pub mod ledger; pub mod ledger;
pub mod reconcile; pub mod reconcile;
pub mod update_handoff; pub mod update_handoff;
pub mod update_health_inventory;
pub mod update_health_sync;
+21 -1
View File
@@ -306,7 +306,13 @@ impl<V: Ed25519Verifier> ClientSync<V> {
merge_helper(&mut attempts, h)?; merge_helper(&mut attempts, h)?;
} }
ensure!(attempts.len() <= 256, "SYNC_ATTEMPT_CAPACITY_NO_COMPACTION"); ensure!(attempts.len() <= 256, "SYNC_ATTEMPT_CAPACITY_NO_COMPACTION");
let ids = pending_ids(&tx)?; let mut ids = pending_ids(&tx)?;
if let Some(update) = inflight.first() {
let grant = Uuid::parse_str(&update.grant_id)?;
ids.extend(crate::update_health_inventory::pending_ids(&tx, grant)?);
ids.sort();
ids.dedup();
}
ensure!(ids.len() <= 256, "SYNC_RECEIPT_CAPACITY"); ensure!(ids.len() <= 256, "SYNC_RECEIPT_CAPACITY");
let next: i64 = tx.query_row( let next: i64 = tx.query_row(
"UPDATE counter SET seq=seq+1 WHERE id=1 RETURNING seq", "UPDATE counter SET seq=seq+1 WHERE id=1 RETURNING seq",
@@ -562,6 +568,19 @@ impl<V: Ed25519Verifier> ClientSync<V> {
"UPDATE reconcile_requests SET response=?2 WHERE id=?1 AND response IS NULL", "UPDATE reconcile_requests SET response=?2 WHERE id=?1 AND response IS NULL",
params![decision.request_id.to_string(), response.encode_to_vec()], params![decision.request_id.to_string(), response.encode_to_vec()],
)?; )?;
for local in prepared
.request
.attempts
.iter()
.filter(|a| !a.update_handoff_bytes.is_empty())
{
crate::update_health_inventory::acknowledge_signed(
&tx,
Uuid::parse_str(&local.grant_id)?,
&decision,
response,
)?;
}
tx.execute("UPDATE reconcile_meta SET last_decision=?1,pending_request=NULL,helper_id=?2,helper_checkpoint=?3,fault=COALESCE(fault,?4),high_sequence=?5 WHERE id=1", tx.execute("UPDATE reconcile_meta SET last_decision=?1,pending_request=NULL,helper_id=?2,helper_checkpoint=?3,fault=COALESCE(fault,?4),high_sequence=?5 WHERE id=1",
params![decision.decision_id.to_string(),decision.helper_journal_id.map(|id|id.to_string()),decision.helper_checkpoint,fault,seq])?; params![decision.decision_id.to_string(),decision.helper_journal_id.map(|id|id.to_string()),decision.helper_checkpoint,fault,seq])?;
tx.commit()?; tx.commit()?;
@@ -692,6 +711,7 @@ pub fn initialize(db: &Connection) -> Result<()> {
CREATE TABLE IF NOT EXISTS reconcile_results(id TEXT PRIMARY KEY,job TEXT NOT NULL,attempt TEXT NOT NULL,revision INTEGER NOT NULL,envelope BLOB NOT NULL); CREATE TABLE IF NOT EXISTS reconcile_results(id TEXT PRIMARY KEY,job TEXT NOT NULL,attempt TEXT NOT NULL,revision INTEGER NOT NULL,envelope BLOB NOT NULL);
CREATE TABLE IF NOT EXISTS reconcile_receipts(id TEXT PRIMARY KEY,decision_id TEXT NOT NULL); CREATE TABLE IF NOT EXISTS reconcile_receipts(id TEXT PRIMARY KEY,decision_id TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS reconcile_outcomes(job TEXT NOT NULL,attempt TEXT NOT NULL,outcome BLOB NOT NULL,decision_id TEXT NOT NULL,PRIMARY KEY(job,attempt));")?; CREATE TABLE IF NOT EXISTS reconcile_outcomes(job TEXT NOT NULL,attempt TEXT NOT NULL,outcome BLOB NOT NULL,decision_id TEXT NOT NULL,PRIMARY KEY(job,attempt));")?;
crate::update_health_inventory::initialize(db)?;
db.execute( db.execute(
"INSERT OR IGNORE INTO reconcile_meta(id,journal_id) VALUES(1,?1)", "INSERT OR IGNORE INTO reconcile_meta(id,journal_id) VALUES(1,?1)",
[Uuid::new_v4().to_string()], [Uuid::new_v4().to_string()],
+112 -3
View File
@@ -33,6 +33,8 @@ pub trait UpdateAdmissionGuard: ExecutorLedgerGuard {
/// Role comes from an authenticated concrete local participant, not IPC data. /// Role comes from an authenticated concrete local participant, not IPC data.
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Participant { pub enum Participant {
/// Authenticated Full reader; never a native effect participant.
Observer,
OriginalHelper, OriginalHelper,
StableUpdater, StableUpdater,
NewHelper, NewHelper,
@@ -64,6 +66,11 @@ pub trait UpdateParticipantGuard: ExecutorLedgerGuard {
/// executor guard. This does not alter old schema objects, rows or file IDs. /// executor guard. This does not alter old schema objects, rows or file IDs.
pub fn initialize(db: &Connection, guard: &impl ExecutorLedgerGuard) -> Result<()> { pub fn initialize(db: &Connection, guard: &impl ExecutorLedgerGuard) -> Result<()> {
guard.check(db)?; guard.check(db)?;
db.execute_batch("PRAGMA foreign_keys=ON;")?;
ensure!(
db.query_row("PRAGMA foreign_keys", [], |r| r.get::<_, bool>(0))?,
"UPDATE_LEDGER_FOREIGN_KEYS_REQUIRED"
);
db.execute_batch( db.execute_batch(
"CREATE TABLE IF NOT EXISTS agent_update_handoffs( "CREATE TABLE IF NOT EXISTS agent_update_handoffs(
grant_id TEXT PRIMARY KEY REFERENCES consumed(grant_id), record BLOB NOT NULL, grant_id TEXT PRIMARY KEY REFERENCES consumed(grant_id), record BLOB NOT NULL,
@@ -81,7 +88,10 @@ pub fn initialize(db: &Connection, guard: &impl ExecutorLedgerGuard) -> Result<(
CREATE TABLE IF NOT EXISTS agent_update_native_observations( CREATE TABLE IF NOT EXISTS agent_update_native_observations(
grant_id TEXT NOT NULL,effect_key TEXT NOT NULL,claim_sha256 TEXT NOT NULL, grant_id TEXT NOT NULL,effect_key TEXT NOT NULL,claim_sha256 TEXT NOT NULL,
observation_sha256 TEXT NOT NULL,observed_at TEXT NOT NULL,PRIMARY KEY(grant_id,effect_key), observation_sha256 TEXT NOT NULL,observed_at TEXT NOT NULL,PRIMARY KEY(grant_id,effect_key),
FOREIGN KEY(grant_id,effect_key) REFERENCES agent_update_native_claims(grant_id,effect_key));", FOREIGN KEY(grant_id,effect_key) REFERENCES agent_update_native_claims(grant_id,effect_key));
CREATE TABLE IF NOT EXISTS agent_update_sync_applied(
grant_id TEXT NOT NULL REFERENCES agent_update_handoffs(grant_id),decision_id TEXT NOT NULL,
witness BLOB NOT NULL,witness_sha256 TEXT NOT NULL,PRIMARY KEY(grant_id,decision_id));",
)?; )?;
Ok(()) Ok(())
} }
@@ -1060,10 +1070,14 @@ pub enum NativeControlOperation {
expected_head_sha256: String, expected_head_sha256: String,
service: NativeService, service: NativeService,
}, },
ObserveStarts, ObserveStarts {
services: Vec<rmm_updater_handoff::StartedService>,
observation_sha256: String,
},
Health { Health {
receipt: rmm_updater_handoff::SignedHealth, receipt: rmm_updater_handoff::SignedHealth,
}, },
Finalize,
} }
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
@@ -1079,7 +1093,8 @@ pub struct NativeControlReply {
pub owner_nonce: Uuid, pub owner_nonce: Uuid,
pub handoff_sha256: String, pub handoff_sha256: String,
pub ready: bool, pub ready: bool,
pub proof: rmm_domain::agent_update::UpdateInflightProof, pub proof: Option<rmm_domain::agent_update::UpdateInflightProof>,
pub original_outcome_base64url: Option<String>,
pub claim: Option<NativeEffectClaim>, pub claim: Option<NativeEffectClaim>,
/// Only ReadOriginal; exact retained execution_evidence.start bytes, /// Only ReadOriginal; exact retained execution_evidence.start bytes,
/// bounded to 512KiB before canonical Base64url encoding, never rewritten. /// bounded to 512KiB before canonical Base64url encoding, never rewritten.
@@ -1159,3 +1174,97 @@ mod native_record_tests {
); );
} }
} }
/// Read the exact immutable native reservation, never a dispatch token.
pub fn read_native_claim(
db: &Connection,
guard: &impl ExecutorLedgerGuard,
grant: Uuid,
effect: &NativeEffect,
) -> Result<NativeEffectClaim> {
read(db, guard, grant)?;
let raw: Vec<u8> = db.query_row(
"SELECT claim FROM agent_update_native_claims WHERE grant_id=?1 AND effect_key=?2",
params![grant.to_string(), native_key(effect)?],
|r| r.get(0),
)?;
let claim: NativeEffectClaim = serde_json::from_slice(&raw)?;
ensure!(
claim.grant_id == grant && claim.effect == *effect,
"UPDATE_NATIVE_ORIGINAL_CLAIM_REQUIRED"
);
Ok(claim)
}
/// No new capture/checkpoint or readiness is generated by this read.
pub fn current_proof(
db: &Connection,
guard: &impl ExecutorLedgerGuard,
grant: Uuid,
) -> Result<Option<rmm_domain::agent_update::UpdateInflightProof>> {
read(db, guard, grant)?;
proof_locked(db, grant)
}
/// Actual local control application, recorded by HelperSync in the SAME
/// transaction as its verified original decision. Never supplied by IPC.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AppliedUpdateSync {
pub schema_version: u32,
pub grant_id: Uuid,
pub decision_id: Uuid,
pub request_sha256: String,
pub response_protobuf_sha256: String,
pub applied_at: DateTime<Utc>,
}
pub(crate) fn record_applied_sync(db: &Connection, witness: &AppliedUpdateSync) -> Result<()> {
ensure!(
witness.schema_version == 1
&& !witness.grant_id.is_nil()
&& !witness.decision_id.is_nil()
&& rmm_domain::agent_update::digest(&witness.request_sha256)
&& rmm_domain::agent_update::digest(&witness.response_protobuf_sha256),
"UPDATE_APPLIED_SYNC_SHAPE"
);
let raw = serde_json::to_vec(witness)?;
db.execute("INSERT INTO agent_update_sync_applied(grant_id,decision_id,witness,witness_sha256) VALUES(?1,?2,?3,?4)",params![witness.grant_id.to_string(),witness.decision_id.to_string(),raw,hash(&raw)])?;
Ok(())
}
pub fn read_applied_sync(
db: &Connection,
guard: &impl ExecutorLedgerGuard,
grant: Uuid,
decision: Uuid,
) -> Result<AppliedUpdateSync> {
use prost::Message;
guard.check(db)?;
let (raw,digest):(Vec<u8>,String)=db.query_row("SELECT witness,witness_sha256 FROM agent_update_sync_applied WHERE grant_id=?1 AND decision_id=?2",params![grant.to_string(),decision.to_string()],|r|Ok((r.get(0)?,r.get(1)?)))?;
let witness: AppliedUpdateSync = serde_json::from_slice(&raw)?;
ensure!(
raw.len() <= 2048
&& hash(&raw) == digest
&& serde_json::to_vec(&witness)? == raw
&& witness.schema_version == 1
&& witness.grant_id == grant
&& witness.decision_id == decision,
"UPDATE_APPLIED_SYNC_CHANGED"
);
let (saved_response, request): (Vec<u8>, Vec<u8>) = db.query_row(
"SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1",
[decision.to_string()],
|r| Ok((r.get(0)?, r.get(1)?)),
)?;
let response: crate::helper_reconcile::SignedResponse =
serde_json::from_slice(&saved_response)?;
let protobuf = rmm_protocol::SyncResponse {
decision_bytes: response.decision_bytes,
signature: response.signature,
}
.encode_to_vec();
ensure!(
hash(&protobuf) == witness.response_protobuf_sha256
&& hash(&request) == witness.request_sha256,
"UPDATE_APPLIED_SYNC_ORIGINAL_BYTES_CHANGED"
);
guard.check(db)?;
Ok(witness)
}
@@ -0,0 +1,464 @@
//! Retain at most two ORIGINAL base-inventory envelopes for one update grant.
//! Caller holds the existing agent journal lock and provisions its bounded DB.
//! The health key must come from the independently verified ORIGINAL update job;
//! neither a network response nor a caller-selected key establishes that trust.
use anyhow::{Context, Result, ensure};
use chrono::Utc;
use prost::Message;
use rmm_domain::{
agent_update::UpdateInflightProof, jobs::hash, reconciliation::ReconcileDecisionPayload,
};
use rmm_protocol::{Envelope, Payload, SyncResponse};
use rmm_updater_handoff::{Event, Evidence, Phase, Service};
use rusqlite::{Connection, OptionalExtension, params};
use uuid::Uuid;
pub fn initialize(db: &Connection) -> Result<()> {
db.execute_batch(
"CREATE TABLE IF NOT EXISTS update_health_inventory(
grant_id TEXT NOT NULL,message_id TEXT NOT NULL UNIQUE,
handoff_sha256 TEXT NOT NULL,prefix_sha256 TEXT NOT NULL,
envelope BLOB NOT NULL,envelope_sha256 TEXT NOT NULL,
signed_decision_id TEXT,signed_response_sha256 TEXT,
PRIMARY KEY(grant_id,message_id),
CHECK((signed_decision_id IS NULL)=(signed_response_sha256 IS NULL)));",
)?;
Ok(())
}
struct CurrentUpdate {
proof: UpdateInflightProof,
key: [u8; 32],
generation: u64,
valid_until: chrono::DateTime<Utc>,
}
/// Read-only scheduling of control for an ORIGINAL locally applied update.
/// Historical signature validation here opens no current start/effect lease;
/// it lets a fresh helper capture adjudicate a now-lost owner or expired job.
pub fn current_proof(
db: &Connection,
binding: &crate::auth::Binding,
) -> Result<Option<UpdateInflightProof>> {
Ok(current_update(db, binding)?.map(|current| current.proof))
}
fn current_update(
db: &Connection,
binding: &crate::auth::Binding,
) -> Result<Option<CurrentUpdate>> {
let (last, generation, journal, fault): (Option<String>, u64, String, Option<String>) = db
.query_row(
"SELECT last_decision,generation,journal_id,fault FROM reconcile_meta WHERE id=1",
[],
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)),
)?;
let Some(last) = last else { return Ok(None) };
if fault.is_some() {
return Ok(None);
}
let mut q=db.prepare("SELECT request,response FROM reconcile_requests WHERE response IS NOT NULL ORDER BY rowid DESC LIMIT 64")?;
let mut rows = q.query([])?;
while let Some(row) = rows.next()? {
let response_raw: Vec<u8> = row.get(1)?;
ensure!(
response_raw.len() <= rmm_domain::MAX_MESSAGE,
"UPDATE_HEALTH_CONTROL_SIZE"
);
let response = SyncResponse::decode(response_raw.as_slice())?;
let d: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?;
if d.decision_id.to_string() != last {
continue;
}
// prepare() fences a reconnect to its new generation BEFORE its
// response is applied. The previous applied decision is then expected
// old context, never an error or current update authority.
if d.connection_generation < generation {
return Ok(None);
}
ensure!(
d.connection_generation == generation,
"UPDATE_HEALTH_CONTROL_GENERATION_ROLLBACK"
);
let now = Utc::now();
if d.issued_at > now {
return Ok(None);
}
let raw: Vec<u8> = row.get(0)?;
ensure!(
raw.len() <= rmm_domain::MAX_MESSAGE,
"UPDATE_HEALTH_CONTROL_SIZE"
);
let request = rmm_protocol::SyncRequest::decode(raw.as_slice())?;
ensure!(
request.encode_to_vec() == raw
&& response.encode_to_vec() == response_raw
&& serde_json::to_vec(&d)? == response.decision_bytes,
"UPDATE_HEALTH_CONTROL_CODEC"
);
let channel = crate::reconcile::Channel {
connection_generation: generation,
boot_id: d.boot_id,
};
let d =
crate::reconcile::verify_response(binding, &request, &channel, &response, d.issued_at)?;
let updates = d
.attempts
.iter()
.filter(|a| a.update_inflight.is_some())
.collect::<Vec<_>>();
if updates.is_empty() {
return Ok(None);
}
ensure!(
updates.len() == 1
&& !d.ready
&& !d.helper_ready
&& d.journal_id.to_string() == journal,
"UPDATE_HEALTH_INFLIGHT_REQUIRED"
);
let signed = updates[0];
let local = request
.attempts
.iter()
.filter(|a| {
a.job_id == signed.job_id.to_string()
&& a.attempt_id == signed.attempt_id.to_string()
})
.collect::<Vec<_>>();
ensure!(local.len() == 1, "UPDATE_HEALTH_LOCAL_ATTEMPT");
let local = local[0];
let (offer_raw, state, grant): (Vec<u8>, String, String) = db.query_row(
"SELECT offer,state,grant_id FROM job_attempts WHERE job=?1 AND attempt=?2",
params![signed.job_id.to_string(), signed.attempt_id.to_string()],
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
)?;
let offer = rmm_protocol::JobOffer::decode(offer_raw.as_slice())?;
let job = binding.job(&offer.payload_bytes)?;
let key =
rmm_domain::agent_update::job_health_public_key(&job).map_err(anyhow::Error::msg)?;
let proof = UpdateInflightProof::from_bytes(&local.update_handoff_bytes)
.map_err(anyhow::Error::msg)?;
let (header, _phase) = rmm_domain::agent_update::verify_inflight_proof(&proof, key)
.map_err(anyhow::Error::msg)?;
let expected =
rmm_domain::agent_update::inflight_decision(&proof, key).map_err(anyhow::Error::msg)?;
ensure!(
state == "running"
&& local.state == "running"
&& local.consumption == "consumed"
&& grant == header.attempt.grant_id.to_string()
&& local.grant_id == grant
&& signed.grant_id == Some(header.attempt.grant_id)
&& signed.update_inflight.as_ref() == Some(&expected)
&& !signed.unlock
&& signed.outcome_state == "running"
&& signed.job_payload_hash == hash(&offer.payload_bytes)
&& offer.job_payload_hash == signed.job_payload_hash
&& local.job_payload_hash == signed.job_payload_hash
&& 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()
&& signed.outcome_bytes.is_empty()
&& signed.outcome_sha256.is_none()
&& signed.authorization_id == Some(header.attempt.authorization_id)
&& signed.plan_hash.as_deref() == Some(header.attempt.plan_sha256.as_str())
&& offer.plan_hash == header.attempt.plan_sha256
&& offer.authorization_id == header.attempt.authorization_id.to_string()
&& rmm_domain::agent_update::handoff_matches_job(&header, &job),
"UPDATE_HEALTH_ORIGINAL_BINDING"
);
return Ok(Some(CurrentUpdate {
proof,
key,
generation,
valid_until: d.valid_until,
}));
}
Ok(None)
}
/// Retain only a genuinely collected, fresh base snapshot against an already
/// applied ServicesStarted control. Old control may schedule read-only Sync,
/// but may never make stale inventory into a current health observation.
pub fn retain_current(
db: &Connection,
envelope: &Envelope,
binding: &crate::auth::Binding,
) -> Result<bool> {
if !matches!(envelope.payload,Some(Payload::InventorySnapshot(ref s)) if matches!(s.scope.as_str(),"linux.base"|"windows.base"))
{
return Ok(false);
}
let Some(current) = current_update(db, binding)? else {
return Ok(false);
};
if Utc::now() >= current.valid_until {
return Ok(false);
}
let (_, journal) = rmm_domain::agent_update::verify_inflight_proof(&current.proof, current.key)
.map_err(anyhow::Error::msg)?;
if journal.phase() != Phase::ServicesStarted {
return Ok(false);
}
ensure!(
envelope.connection_generation == current.generation
&& envelope.agent_id == binding.agent_id.to_string()
&& envelope.enrollment_generation == binding.enrollment_generation,
"UPDATE_HEALTH_CURRENT_CHANNEL"
);
retain(db, envelope, &current.proof, current.key)?;
Ok(true)
}
/// Queue integration calls this only for an actually collected new snapshot.
/// Verification independently excludes pre-start/base-relabelled update data.
pub fn retain(
db: &Connection,
envelope: &Envelope,
proof: &UpdateInflightProof,
health_key: [u8; 32],
) -> Result<()> {
let (header, journal) = rmm_domain::agent_update::verify_inflight_proof(proof, health_key)
.map_err(anyhow::Error::msg)?;
ensure!(
journal.phase() == Phase::ServicesStarted,
"UPDATE_HEALTH_NOT_STARTED"
);
let decoded = proof.decode().map_err(anyhow::Error::msg)?;
let last: Event =
serde_json::from_slice(decoded.events.last().context("UPDATE_HEALTH_START_EVENT")?)?;
ensure!(
matches!(last.evidence, Evidence::ServicesStarted { .. }),
"UPDATE_HEALTH_START_EVENT"
);
let scope = if header.stop_set.contains(&Service::WindowsAgent) {
"windows.base"
} else {
"linux.base"
};
let Payload::InventorySnapshot(snapshot) = envelope
.payload
.as_ref()
.context("UPDATE_HEALTH_INVENTORY")?
else {
anyhow::bail!("UPDATE_HEALTH_BASE_REQUIRED")
};
let now = Utc::now();
let observed = chrono::DateTime::from_timestamp_millis(envelope.observed_at)
.context("UPDATE_HEALTH_TIME")?;
let journal_id: String = db.query_row(
"SELECT journal_id FROM reconcile_meta WHERE id=1",
[],
|r| r.get(0),
)?;
ensure!(
journal_id == header.agent_journal_id.to_string()
&& envelope.protocol_major == 1
&& envelope.agent_id == header.attempt.agent_id.to_string()
&& envelope.enrollment_generation == header.attempt.enrollment_generation
&& envelope.boot_id == header.boot_id.to_string()
&& envelope.connection_generation > header.connection_generation
&& envelope.sequence > 0
&& observed.timestamp() >= i64::try_from(last.observed_at)?
&& observed <= now
&& now.timestamp() < i64::try_from(header.finish_before)?
&& now.timestamp()
< i64::try_from(
last.observed_at
.checked_add(180)
.context("UPDATE_HEALTH_TIME")?
)?
&& snapshot.scope == scope
&& snapshot.schema_version == 1
&& snapshot.chunk_count == 1
&& snapshot.chunk_index == 0
&& snapshot.completeness == "partial"
&& snapshot.sha256 == hex::decode(hash(&snapshot.data))?,
"UPDATE_HEALTH_INVENTORY_BINDING"
);
rmm_domain::inventory_document::validate_for_scope(&snapshot.data, now, scope)
.map_err(anyhow::Error::msg)?;
let id = Uuid::parse_str(&envelope.message_id)?;
ensure!(
!id.is_nil() && id.to_string() == envelope.message_id,
"UPDATE_HEALTH_MESSAGE_ID"
);
let raw = envelope.encode_to_vec();
ensure!(
raw.len() <= rmm_domain::MAX_MESSAGE,
"UPDATE_HEALTH_INVENTORY_SIZE"
);
store(
db,
header.attempt.grant_id,
id,
journal.handoff_sha256(),
journal.head_sha256(),
&raw,
)
}
fn store(
db: &Connection,
grant: Uuid,
id: Uuid,
handoff: &str,
prefix: &str,
raw: &[u8],
) -> Result<()> {
let existing:Option<(String,String,Vec<u8>,String)>=db.query_row("SELECT handoff_sha256,prefix_sha256,envelope,envelope_sha256 FROM update_health_inventory WHERE grant_id=?1 AND message_id=?2",params![grant.to_string(),id.to_string()],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?))).optional()?;
if let Some((h, p, b, digest)) = existing {
ensure!(
h == handoff && p == prefix && b == raw && digest == hash(raw),
"UPDATE_HEALTH_RETAIN_CONFLICT"
);
return Ok(());
}
let inserted=db.execute("INSERT INTO update_health_inventory(grant_id,message_id,handoff_sha256,prefix_sha256,envelope,envelope_sha256) SELECT ?1,?2,?3,?4,?5,?6 WHERE (SELECT count(*) FROM update_health_inventory WHERE grant_id=?1)<2",params![grant.to_string(),id.to_string(),handoff,prefix,raw,hash(raw)])?;
ensure!(inserted == 1, "UPDATE_HEALTH_SAMPLE_LIMIT");
Ok(())
}
/// Only the exact current typed original inflight may request these IDs.
pub fn pending_ids(db: &Connection, grant: Uuid) -> Result<Vec<String>> {
let mut q=db.prepare("SELECT message_id FROM update_health_inventory WHERE grant_id=?1 AND signed_decision_id IS NULL ORDER BY rowid LIMIT 2")?;
Ok(q.query_map([grant.to_string()], |r| r.get(0))?
.collect::<rusqlite::Result<Vec<_>>>()?)
}
/// INTERNAL normal SignedSync apply hook, after independent signature/binding
/// verification and durable response/receipt insertion in the same transaction.
/// A plain transport ACK cannot call this path or supply a boolean assertion.
pub(crate) fn acknowledge_signed(
db: &Connection,
grant: Uuid,
decision: &ReconcileDecisionPayload,
response: &SyncResponse,
) -> Result<()> {
ensure!(
serde_json::to_vec(decision)? == response.decision_bytes,
"UPDATE_HEALTH_SIGNED_BYTES"
);
let saved: Option<Vec<u8>> = db
.query_row(
"SELECT response FROM reconcile_requests WHERE id=?1",
[decision.request_id.to_string()],
|r| r.get(0),
)
.optional()?
.flatten();
ensure!(
saved.as_deref() == Some(response.encode_to_vec().as_slice()),
"UPDATE_HEALTH_APPLY_NOT_RETAINED"
);
for id in &decision.acknowledged_message_ids {
let receipt: Option<String> = db
.query_row(
"SELECT decision_id FROM reconcile_receipts WHERE id=?1",
[id.to_string()],
|r| r.get(0),
)
.optional()?;
ensure!(
receipt.as_deref() == Some(decision.decision_id.to_string().as_str()),
"UPDATE_HEALTH_SIGNED_RECEIPT_MISSING"
);
db.execute("UPDATE update_health_inventory SET signed_decision_id=?3,signed_response_sha256=?4 WHERE grant_id=?1 AND message_id=?2 AND signed_decision_id IS NULL",params![grant.to_string(),id.to_string(),decision.decision_id.to_string(),hash(&response.encode_to_vec())])?;
}
Ok(())
}
/// Original raw protobuf bytes remain retained after BOTH kinds of ACK.
pub fn read(db: &Connection, grant: Uuid, message: Uuid) -> Result<Envelope> {
let (raw,digest):(Vec<u8>,String)=db.query_row("SELECT envelope,envelope_sha256 FROM update_health_inventory WHERE grant_id=?1 AND message_id=?2",params![grant.to_string(),message.to_string()],|r|Ok((r.get(0)?,r.get(1)?)))?;
ensure!(
raw.len() <= rmm_domain::MAX_MESSAGE && hash(&raw) == digest,
"UPDATE_HEALTH_RAW_CHANGED"
);
let envelope = Envelope::decode(raw.as_slice())?;
ensure!(
envelope.encode_to_vec() == raw && envelope.message_id == message.to_string(),
"UPDATE_HEALTH_RAW_CHANGED"
);
Ok(envelope)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ordinary_ack_preserves_original_raw_and_signed_sync_pending_id() {
let mut db = Connection::open_in_memory().unwrap();
crate::reconcile::initialize(&db).unwrap();
initialize(&db).unwrap();
db.execute_batch("CREATE TABLE job_events(id TEXT PRIMARY KEY,payload BLOB NOT NULL);")
.unwrap();
let grant = Uuid::new_v4();
let id = Uuid::new_v4();
let envelope = Envelope {
message_id: id.to_string(),
..Default::default()
};
let raw = envelope.encode_to_vec();
store(&db, grant, id, "handoff", "prefix", &raw).unwrap();
crate::jobs::event(&db, &envelope).unwrap();
crate::jobs::ack(&mut db, &id.to_string()).unwrap();
assert!(crate::jobs::events(&db).unwrap().is_empty());
assert_eq!(pending_ids(&db, grant).unwrap(), vec![id.to_string()]);
assert_eq!(read(&db, grant, id).unwrap().encode_to_vec(), raw);
assert!(read(&db, Uuid::new_v4(), id).is_err());
// Internal hook contract only: these synthetic bytes are not a
// signature qualification. The normal verifier must run before the hook.
let decision: ReconcileDecisionPayload = serde_json::from_value(serde_json::json!({
"domain":"rmm.reconciliation-decision","schema_version":1,
"decision_id":Uuid::new_v4(),"request_id":Uuid::new_v4(),"request_hash":"11".repeat(32),
"tenant_id":grant,"asset_id":grant,"agent_id":grant,"enrollment_generation":1,
"assignment_version":1,"connection_generation":2,"boot_id":grant,"journal_id":grant,
"checkpoint":3,"helper_journal_id":grant,"helper_checkpoint":3,
"issued_at":Utc::now(),"valid_until":Utc::now()+chrono::Duration::seconds(60),
"ready":false,"helper_ready":false,"reason":"codec-only","attempts":[],
"acknowledged_message_ids":[id],"key_id":"22".repeat(32)
}))
.unwrap();
let response = SyncResponse {
decision_bytes: serde_json::to_vec(&decision).unwrap(),
signature: vec![1; 64],
};
assert!(acknowledge_signed(&db, grant, &decision, &response).is_err());
db.execute("INSERT INTO reconcile_requests(id,request,envelope,generation,response) VALUES(?1,?2,?2,2,?3)",params![decision.request_id.to_string(),Vec::<u8>::new(),response.encode_to_vec()]).unwrap();
assert!(acknowledge_signed(&db, grant, &decision, &response).is_err());
db.execute(
"INSERT INTO reconcile_receipts(id,decision_id) VALUES(?1,?2)",
params![id.to_string(), decision.decision_id.to_string()],
)
.unwrap();
acknowledge_signed(&db, grant, &decision, &response).unwrap();
assert!(pending_ids(&db, grant).unwrap().is_empty());
assert_eq!(read(&db, grant, id).unwrap().encode_to_vec(), raw);
}
#[test]
fn retain_is_create_once_bounded_to_two_samples_and_detects_tampering() {
let db = Connection::open_in_memory().unwrap();
initialize(&db).unwrap();
let grant = Uuid::new_v4();
let id = Uuid::new_v4();
let raw = Envelope {
message_id: id.to_string(),
..Default::default()
}
.encode_to_vec();
store(&db, grant, id, "handoff", "prefix", &raw).unwrap();
store(&db, grant, id, "handoff", "prefix", &raw).unwrap();
assert!(store(&db, grant, id, "different", "prefix", &raw).is_err());
store(&db, grant, Uuid::new_v4(), "handoff", "prefix", &[1]).unwrap();
assert!(store(&db, grant, Uuid::new_v4(), "handoff", "prefix", &[2]).is_err());
db.execute(
"UPDATE update_health_inventory SET envelope=?2 WHERE message_id=?1",
params![id.to_string(), vec![8_u8]],
)
.unwrap();
assert!(read(&db, grant, id).is_err());
assert_eq!(pending_ids(&db, grant).unwrap().len(), 2);
}
}
+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());
}
}
@@ -984,11 +984,13 @@ fn clean_ready_shared_lock_and_reopen_start_closed() {
let mut f = Fixture::new(); let mut f = Fixture::new();
let mut sync = f.sync(); let mut sync = f.sync();
assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot));
assert_eq!(sync.validity_deadline(), None);
let snapshot = f.snapshot(&mut sync, 1); let snapshot = f.snapshot(&mut sync, 1);
let raw = f.request(&snapshot); let raw = f.request(&snapshot);
let d = f.decision(&snapshot, &raw, 1, true); let d = f.decision(&snapshot, &raw, 1, true);
let applied = f.apply(&mut sync, &d, &raw).unwrap(); let applied = f.apply(&mut sync, &d, &raw).unwrap();
assert!(applied.ready); assert!(applied.ready);
assert_eq!(sync.validity_deadline(), Some(d.valid_until));
assert_eq!(sync.active_generation(), Some(1)); assert_eq!(sync.active_generation(), Some(1));
assert_eq!(applied.snapshot.previous_decision_id, Some(d.decision_id)); assert_eq!(applied.snapshot.previous_decision_id, Some(d.decision_id));
assert!(applied.unlocked_grants.is_empty()); assert!(applied.unlocked_grants.is_empty());
@@ -1004,6 +1006,7 @@ fn clean_ready_shared_lock_and_reopen_start_closed() {
.is_err() .is_err()
); );
sync.close(); sync.close();
assert_eq!(sync.validity_deadline(), None);
assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot));
} }
#[test] #[test]
@@ -1777,6 +1780,34 @@ fn update_inflight_negative_control_keeps_every_start_closed_and_owner_loss_is_t
.apply(&mut f.db, &guard, &signed, &raw, &f.clock, &f.boot) .apply(&mut f.db, &guard, &signed, &raw, &f.clock, &f.boot)
.unwrap(); .unwrap();
assert!(!applied.ready && applied.unlocked_grants.is_empty()); assert!(!applied.ready && applied.unlocked_grants.is_empty());
let local_apply = rmm_agent_core::update_handoff::read_applied_sync(
&f.db,
&guard,
handoff.attempt.grant_id,
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 {
decision_bytes: signed.decision_bytes.clone(),
signature: signed.signature.clone(),
};
assert_eq!(
local_apply.response_protobuf_sha256,
hash(&prost::Message::encode_to_vec(&proto))
);
assert!(!fresh.can_start(&f.db, &guard, 8, &f.clock, &f.boot)); assert!(!fresh.can_start(&f.db, &guard, 8, &f.clock, &f.boot));
assert_eq!( assert_eq!(
f.db.query_row::<u32, _, _>( f.db.query_row::<u32, _, _>(
@@ -1974,3 +2005,63 @@ fn native_stop_intent_is_original_head_bound_create_once_and_transactional() {
); );
} }
} }
#[test]
fn restarted_update_can_only_schedule_control_from_its_verified_original_not_unknown() {
use rmm_agent_core::{jobs, ledger};
let (f, _, request, _, _) = update_case();
let app = Connection::open_in_memory().unwrap();
app.execute_batch("CREATE TABLE job_attempts(job TEXT,attempt TEXT,offer BLOB,state TEXT,revision INTEGER DEFAULT 0,grant_id TEXT DEFAULT '',cancel_requested INTEGER DEFAULT 0,PRIMARY KEY(job,attempt));").unwrap();
let mut own = Connection::open_in_memory().unwrap();
ledger::initialize_ledger(&own).unwrap();
let payload: rmm_domain::jobs::JobPayload = serde_json::from_slice(&request.job_bytes).unwrap();
let permit: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes).unwrap();
let offer = rmm_protocol::JobOffer {
job_id: payload.job_id.to_string(),
attempt_id: payload.attempt_id.to_string(),
authorization_id: request.authorization_id.to_string(),
plan_hash: request.plan_hash.clone(),
job_payload_hash: hash(&request.job_bytes),
payload_bytes: request.job_bytes.clone(),
};
jobs::accept(&app, &f.binding, &offer, |job| {
matches!(job.action, Action::UpdateAgent { .. })
})
.unwrap();
assert!(!jobs::needs_update_control(&app, &own, &f.binding).unwrap());
ledger::begin_execution(&mut own, &permit, &request, f.boot.0, f.clock.now).unwrap();
jobs::transition(&app, &offer, "running", &permit.grant_id.to_string()).unwrap();
let original: Vec<u8> = own
.query_row("SELECT start FROM execution_evidence", [], |r| r.get(0))
.unwrap();
assert!(jobs::needs_update_control(&app, &own, &f.binding).unwrap());
assert_eq!(
app.query_row::<String, _, _>("SELECT state FROM job_attempts", [], |r| r.get(0))
.unwrap(),
"running"
);
assert_eq!(
own.query_row::<u32, _, _>(
"SELECT count(*) FROM execution_evidence WHERE outcome IS NOT NULL",
[],
|r| r.get(0)
)
.unwrap(),
0
);
own.execute("UPDATE consumed SET agent=?1", [Uuid::new_v4().to_string()])
.unwrap();
assert!(jobs::needs_update_control(&app, &own, &f.binding).is_err());
own.execute(
"UPDATE consumed SET agent=?1",
[f.binding.agent_id.to_string()],
)
.unwrap();
jobs::transition(&app, &offer, "unknown", &permit.grant_id.to_string()).unwrap();
assert!(!jobs::needs_update_control(&app, &own, &f.binding).unwrap());
assert_eq!(
own.query_row::<Vec<u8>, _, _>("SELECT start FROM execution_evidence", [], |r| r.get(0))
.unwrap(),
original
);
}
+4
View File
@@ -28,3 +28,7 @@ rmm-protocol = { path = "../protocol" }
rmm-agent-linux = { path = "../agent-linux" } rmm-agent-linux = { path = "../agent-linux" }
rmm-privileged-helper = { path = "../privileged-helper" } rmm-privileged-helper = { path = "../privileged-helper" }
libc = "0.2.177" libc = "0.2.177"
rmm-updater-handoff = { path = "../updater-handoff" }
base64 = "=0.22.1"
hex.workspace = true
+465
View File
@@ -0,0 +1,465 @@
//! Evidence-only client: ORIGINAL retained inventory and two real signed rounds.
//! No collection, job, grant, readiness, helper effect or caller ACK is produced.
use anyhow::{Context, Result, ensure};
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use chrono::{DateTime, Utc};
use prost::Message;
use rmm_agent_core::{
auth::Binding,
reconcile::{Channel, verify_response},
update_health_inventory,
};
use rmm_domain::{
agent_update::{self as update, UpdateInflightProof},
jobs::{JobPayload, hash},
reconciliation::ReconcileDecisionPayload,
};
use rmm_protocol::{Envelope, JobOffer, Payload, SyncRequest, SyncResponse};
use rmm_updater_handoff::{Event, Evidence, Handoff, HealthReceipt, Phase, SignedHealth};
use rusqlite::{Connection, OptionalExtension, params};
use serde::Serialize;
use std::{
net::SocketAddr,
time::{Duration, Instant},
};
use uuid::Uuid;
#[derive(Serialize)]
struct HealthRequest {
proof: UpdateInflightProof,
applied_sync_request_base64url: String,
confirming_sync_request_base64url: String,
inventory_envelope_base64url: String,
}
/// Opaque; constructed only by examining retained, verified local evidence.
pub struct HealthContext {
input: HealthRequest,
header: Handoff,
health_key: [u8; 32],
inventory_id: Uuid,
applied: ReconcileDecisionPayload,
applied_response_sha256: String,
confirming: ReconcileDecisionPayload,
observed_now: DateTime<Utc>,
prepared_at: Instant,
deadline: Instant,
}
impl HealthContext {
pub fn handoff(&self) -> &Handoff {
&self.header
}
pub fn health_public_key(&self) -> [u8; 32] {
self.health_key
}
}
struct StoredSync {
request: SyncRequest,
raw: Vec<u8>,
response: SyncResponse,
decision: ReconcileDecisionPayload,
}
fn stored_sync(
db: &Connection,
id: Uuid,
binding: &Binding,
channel: &Channel,
now: DateTime<Utc>,
) -> Result<Option<StoredSync>> {
// At most 64 recent control rounds, incrementally read; no unbounded journal scan.
let mut q=db.prepare("SELECT request,envelope,response FROM reconcile_requests WHERE response IS NOT NULL ORDER BY rowid DESC LIMIT 64")?;
let mut rows = q.query([])?;
while let Some(row) = rows.next()? {
let raw: Vec<u8> = row.get(0)?;
let envelope_raw: Vec<u8> = row.get(1)?;
let response_raw: Vec<u8> = row.get(2)?;
ensure!(
raw.len() <= rmm_domain::MAX_MESSAGE
&& envelope_raw.len() <= rmm_domain::MAX_MESSAGE
&& response_raw.len() <= rmm_domain::MAX_MESSAGE,
"UPDATE_HEALTH_CONTROL_SIZE"
);
let response = SyncResponse::decode(response_raw.as_slice())?;
let decision: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?;
if decision.decision_id != id {
continue;
}
let request = SyncRequest::decode(raw.as_slice())?;
let envelope = Envelope::decode(envelope_raw.as_slice())?;
ensure!(
request.encode_to_vec() == raw
&& response.encode_to_vec() == response_raw
&& envelope.encode_to_vec() == envelope_raw
&& rmm_protocol::valid_envelope(
&envelope,
&binding.agent_id.to_string(),
channel.connection_generation
)
&& envelope.agent_id == binding.agent_id.to_string()
&& envelope.enrollment_generation == binding.enrollment_generation
&& envelope.boot_id == channel.boot_id.to_string()
&& envelope.connection_generation == channel.connection_generation
&& matches!(envelope.payload,Some(Payload::SyncRequest(ref r)) if r==&request),
"UPDATE_HEALTH_CONTROL_BYTES"
);
let decision = verify_response(binding, &request, channel, &response, now)?;
ensure!(
serde_json::to_vec(&decision)? == response.decision_bytes,
"UPDATE_HEALTH_CONTROL_CODEC"
);
return Ok(Some(StoredSync {
request,
raw,
response,
decision,
}));
}
Ok(None)
}
fn exact_prefix(
saved: &StoredSync,
proof: &UpdateInflightProof,
header: &Handoff,
key: [u8; 32],
) -> Result<()> {
let request = &saved.request;
let d = &saved.decision;
let a = request
.attempts
.iter()
.filter(|a| {
a.job_id == header.attempt.job_id.to_string()
&& a.attempt_id == header.attempt.attempt_id.to_string()
})
.collect::<Vec<_>>();
let signed = d
.attempts
.iter()
.filter(|a| a.job_id == header.attempt.job_id && a.attempt_id == header.attempt.attempt_id)
.collect::<Vec<_>>();
ensure!(
a.len() == 1
&& signed.len() == 1
&& !d.ready
&& !d.helper_ready
&& request.journal_complete,
"UPDATE_HEALTH_INFLIGHT_REQUIRED"
);
let a = a[0];
let signed = signed[0];
let expected = update::inflight_decision(proof, key).map_err(anyhow::Error::msg)?;
ensure!(
a.update_handoff_bytes == proof.bytes().map_err(anyhow::Error::msg)?
&& a.grant_id == header.attempt.grant_id.to_string()
&& a.consumption == "consumed"
&& a.state == "running"
&& a.job_payload_hash == header.attempt.job_payload_sha256
&& a.original_boot_id == header.boot_id.to_string()
&& a.outcome_bytes.is_empty()
&& a.outcome_sha256.is_empty()
&& a.result.is_none()
&& a.result_envelope.is_none()
&& signed.update_inflight.as_ref() == Some(&expected)
&& 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.outcome_state == "running"
&& !signed.unlock
&& signed.outcome_bytes.is_empty()
&& signed.outcome_sha256.is_none()
&& request
.attempts
.iter()
.all(|other| (other.job_id == header.attempt.job_id.to_string()
&& other.attempt_id == header.attempt.attempt_id.to_string())
|| other.update_handoff_bytes.is_empty())
&& d.attempts
.iter()
.all(|other| (other.job_id == header.attempt.job_id
&& other.attempt_id == header.attempt.attempt_id)
|| (other.update_inflight.is_none()
&& matches!(
other.outcome_state.as_str(),
"accepted" | "succeeded" | "failed" | "cancelled" | "expired"
)))
&& d.journal_id == header.agent_journal_id
&& d.helper_journal_id == Some(header.helper_journal_id),
"UPDATE_HEALTH_PREFIX_MISMATCH"
);
Ok(())
}
/// `proof` comes from the independently authenticated helper snapshot. Binding
/// and key are locally provisioned; no supplied receipt/boolean proves apply.
/// `now` must be the existing qualified SyncClock, never a refreshed old sample.
pub fn context(
db: &Connection,
binding: &Binding,
proof: &UpdateInflightProof,
now: DateTime<Utc>,
) -> Result<Option<HealthContext>> {
let prepared_at = Instant::now();
let decoded = proof.decode().map_err(anyhow::Error::msg)?;
let unverified: Handoff = serde_json::from_slice(&decoded.handoff_record)?;
let offer: Option<(Vec<u8>, String, String)> = db
.query_row(
"SELECT offer,state,grant_id FROM job_attempts WHERE job=?1 AND attempt=?2",
params![
unverified.attempt.job_id.to_string(),
unverified.attempt.attempt_id.to_string()
],
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
)
.optional()?;
let Some((raw, state, grant)) = offer else {
return Ok(None);
};
let offer = JobOffer::decode(raw.as_slice())?;
let job: JobPayload = binding.job(&offer.payload_bytes)?;
let key = update::job_health_public_key(&job).map_err(anyhow::Error::msg)?;
let (header, journal) =
update::verify_inflight_proof(proof, key).map_err(anyhow::Error::msg)?;
if journal.phase() != Phase::ServicesStarted {
return Ok(None);
}
ensure!(
state == "running"
&& grant == header.attempt.grant_id.to_string()
&& update::handoff_matches_job(&header, &job)
&& offer.job_payload_hash == hash(&offer.payload_bytes)
&& offer.plan_hash == header.attempt.plan_sha256
&& offer.authorization_id == header.attempt.authorization_id.to_string(),
"UPDATE_HEALTH_ORIGINAL_JOB"
);
let event: Event = serde_json::from_slice(
decoded
.events
.last()
.context("UPDATE_HEALTH_SERVICES_STARTED")?,
)?;
ensure!(
matches!(event.evidence, Evidence::ServicesStarted { .. }),
"UPDATE_HEALTH_SERVICES_STARTED"
);
let (last, generation, fault, pending): (Option<String>, u64, Option<String>, Option<String>) =
db.query_row(
"SELECT last_decision,generation,fault,pending_request FROM reconcile_meta WHERE id=1",
[],
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)),
)?;
if fault.is_some() || last.is_none() || pending.is_some() {
return Ok(None);
}
ensure!(
generation > header.connection_generation,
"UPDATE_HEALTH_FRESH_CONNECTION"
);
let channel = Channel {
connection_generation: generation,
boot_id: header.boot_id,
};
let Some(confirming) =
stored_sync(db, Uuid::parse_str(&last.unwrap())?, binding, &channel, now)?
else {
return Ok(None);
};
if !confirming.request.previous_request_id.is_empty()
|| confirming.request.previous_decision_id.is_empty()
{
return Ok(None);
}
let prior = Uuid::parse_str(&confirming.request.previous_decision_id)?;
if confirming.request.helper_previous_decision_id != prior.to_string() {
return Ok(None);
}
let Some(applied) = stored_sync(db, prior, binding, &channel, now)? else {
return Ok(None);
};
ensure!(
confirming.request.checkpoint > applied.decision.checkpoint
&& confirming.request.helper_checkpoint > applied.decision.helper_checkpoint,
"UPDATE_HEALTH_APPLY_LINK"
);
for sync in [&applied, &confirming] {
exact_prefix(sync, proof, &header, key)?;
ensure!(
sync.decision.issued_at.timestamp() >= i64::try_from(event.observed_at)?,
"UPDATE_HEALTH_CONTROL_BEFORE_START"
);
}
let mut inventory = None;
for id in &applied.decision.acknowledged_message_ids {
let marker:Option<(String,String)>=db.query_row("SELECT handoff_sha256,prefix_sha256 FROM update_health_inventory WHERE grant_id=?1 AND message_id=?2 AND signed_decision_id=?3 AND signed_response_sha256=?4",params![header.attempt.grant_id.to_string(),id.to_string(),applied.decision.decision_id.to_string(),hash(&applied.response.encode_to_vec())],|r|Ok((r.get(0)?,r.get(1)?))).optional()?;
if let Some((handoff, prefix)) = marker {
ensure!(
handoff == journal.handoff_sha256() && prefix == journal.head_sha256(),
"UPDATE_HEALTH_RETAINED_PREFIX"
);
let envelope = update_health_inventory::read(db, header.attempt.grant_id, *id)?;
ensure!(
envelope.connection_generation == generation
&& envelope.boot_id == header.boot_id.to_string()
&& envelope.agent_id == binding.agent_id.to_string()
&& envelope.enrollment_generation == binding.enrollment_generation
&& envelope.observed_at
>= i64::try_from(event.observed_at)?
.checked_mul(1000)
.context("UPDATE_HEALTH_TIME")?
&& envelope.observed_at <= now.timestamp_millis(),
"UPDATE_HEALTH_RETAINED_BINDING"
);
let Some(Payload::InventorySnapshot(ref snapshot)) = envelope.payload else {
anyhow::bail!("UPDATE_HEALTH_BASE_REQUIRED")
};
let scope = match job.action {
rmm_domain::jobs::Action::UpdateAgent { ref update } => {
match update.manifest().map_err(anyhow::Error::msg)?.platform {
update::Platform::LinuxX64 => "linux.base",
update::Platform::WindowsX64 => "windows.base",
}
}
_ => anyhow::bail!("UPDATE_HEALTH_ACTION"),
};
ensure!(snapshot.scope == scope, "UPDATE_HEALTH_BASE_REQUIRED");
inventory = Some((*id, envelope));
break;
}
}
let Some((inventory_id, envelope)) = inventory else {
return Ok(None);
};
let until = header
.finish_before
.min(
event
.observed_at
.checked_add(180)
.context("UPDATE_HEALTH_TIME")?,
)
.min(u64::try_from(applied.decision.valid_until.timestamp())?)
.min(u64::try_from(confirming.decision.valid_until.timestamp())?);
let remaining =
(DateTime::from_timestamp(i64::try_from(until)?, 0).context("UPDATE_HEALTH_TIME")? - now)
.to_std()
.context("UPDATE_HEALTH_EXPIRED")?;
ensure!(!remaining.is_zero(), "UPDATE_HEALTH_EXPIRED");
Ok(Some(HealthContext {
input: HealthRequest {
proof: proof.clone(),
applied_sync_request_base64url: URL_SAFE_NO_PAD.encode(&applied.raw),
confirming_sync_request_base64url: URL_SAFE_NO_PAD.encode(&confirming.raw),
inventory_envelope_base64url: URL_SAFE_NO_PAD.encode(envelope.encode_to_vec()),
},
header,
health_key: key,
inventory_id,
applied_response_sha256: hash(&applied.response.encode_to_vec()),
applied: applied.decision,
confirming: confirming.decision,
observed_now: now,
prepared_at,
deadline: prepared_at + remaining,
}))
}
/// Caller supplies the existing https_channel::mtls_client (no redirects,
/// validated chain/SNI and fixed TCP override); never a caller URL or bearer.
pub async fn request(
client: &reqwest::Client,
address: SocketAddr,
context: &HealthContext,
) -> Result<SignedHealth> {
let body = serde_json::to_vec(&context.input)?;
ensure!(body.len() <= 5 * 1024 * 1024, "UPDATE_HEALTH_REQUEST_SIZE");
let url = format!(
"https://{}:{}/agent/v1/update-health",
crate::https_channel::server_name()?,
address.port()
);
let remaining = context
.deadline
.checked_duration_since(Instant::now())
.context("UPDATE_HEALTH_EXPIRED")?;
ensure!(!remaining.is_zero(), "UPDATE_HEALTH_EXPIRED");
let request_budget = remaining.min(Duration::from_secs(35));
let request_deadline = Instant::now() + request_budget;
let signed = tokio::time::timeout(request_budget, async {
let response = client
.post(&url)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.body(body)
.send()
.await?;
ensure!(response.url().as_str() == url, "UPDATE_HEALTH_REDIRECT");
let raw = receipt_bytes(response, request_deadline).await?;
let signed: SignedHealth = serde_json::from_slice(&raw)?;
ensure!(
signed.payload.len() <= 4096 && signed.signature.len() == 64,
"UPDATE_HEALTH_RECEIPT_SIZE"
);
Ok::<_, anyhow::Error>(signed)
})
.await
.context("UPDATE_HEALTH_TIMEOUT")??;
ensure!(Instant::now() < context.deadline, "UPDATE_HEALTH_EXPIRED");
let now = context.observed_now + chrono::Duration::from_std(context.prepared_at.elapsed())?;
let receipt: HealthReceipt = serde_json::from_slice(&signed.payload)?;
ensure!(
receipt.signed_sync_decision_id == context.applied.decision_id
&& receipt.signed_sync_request_sha256
== hash(&URL_SAFE_NO_PAD.decode(&context.input.applied_sync_request_base64url)?)
&& receipt.signed_sync_response_sha256 == context.applied_response_sha256
&& receipt.inventory_ack_sha256 == context.applied_response_sha256
&& receipt.inventory_message_id == context.inventory_id
&& receipt.connection_generation == context.confirming.connection_generation
&& receipt.signed_sync_applied_at
== u64::try_from(context.confirming.issued_at.timestamp())?
&& receipt.valid_until <= context.header.finish_before,
"UPDATE_HEALTH_RECEIPT_BINDING"
);
let (_, journal) = update::verify_inflight_proof(&context.input.proof, context.health_key)
.map_err(anyhow::Error::msg)?;
journal.prepare(
u64::try_from(now.timestamp())?,
Evidence::HealthVerified {
receipt: signed.clone(),
},
)?;
ensure!(Instant::now() < context.deadline, "UPDATE_HEALTH_EXPIRED");
Ok(signed)
}
async fn receipt_bytes(mut response: reqwest::Response, deadline: Instant) -> Result<Vec<u8>> {
ensure!(
response.status() == reqwest::StatusCode::OK,
"UPDATE_HEALTH_HTTP_STATUS"
);
ensure!(
response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
== Some("application/json")
&& !response
.headers()
.contains_key(reqwest::header::CONTENT_ENCODING)
&& response.content_length().is_none_or(|size| size <= 16384),
"UPDATE_HEALTH_HTTP_BODY"
);
let mut raw = Vec::new();
loop {
ensure!(Instant::now() < deadline, "UPDATE_HEALTH_TIMEOUT");
let chunk = response.chunk().await?;
ensure!(Instant::now() < deadline, "UPDATE_HEALTH_TIMEOUT");
let Some(chunk) = chunk else { break };
ensure!(
chunk.len() <= 16384 - raw.len(),
"UPDATE_HEALTH_RECEIPT_SIZE"
);
raw.extend_from_slice(&chunk);
}
Ok(raw)
}
+80
View File
@@ -299,3 +299,83 @@ pub fn general_supported(binding: &Binding, job: &rmm_domain::jobs::JobPayload)
})() })()
.unwrap_or(false) .unwrap_or(false)
} }
impl Helpers {
/// Evidence-only delivery to the actual new Fullhelper. The independent
/// helper reads its own applied control/retained inventory before writing.
pub fn submit_update_health(
&self,
header: &rmm_updater_handoff::Handoff,
key: [u8; 32],
receipt: &rmm_updater_handoff::SignedHealth,
) -> Result<()> {
use rmm_agent_core::update_handoff::{
NativeControlOperation, NativeControlReply, NativeControlRequest,
};
let endpoint = self
.endpoints
.iter()
.find(|e| e.profile == "full_maintenance_v1")
.context("UPDATE_FULL_HELPER_REQUIRED")?;
ensure!(
endpoint.socket == Path::new("/run/rmm-helper-maintenance/control.sock"),
"UPDATE_FIXED_FULL_SOCKET_REQUIRED"
);
let challenge = Uuid::new_v4();
let request = NativeControlRequest {
operation: "agent_update_control_v1".into(),
schema_version: 1,
challenge,
tenant_id: self.binding.tenant_id,
asset_id: self.binding.asset_id,
agent_id: self.binding.agent_id,
enrollment_generation: self.binding.enrollment_generation,
assignment_version: self.binding.assignment_version,
grant_id: header.attempt.grant_id,
owner_nonce: header.owner_nonce,
handoff_sha256: rmm_domain::jobs::hash(&header.bytes()?),
method: NativeControlOperation::Health {
receipt: receipt.clone(),
},
};
let reply: NativeControlReply = control::exchange(
&endpoint.socket,
&request,
std::time::Duration::from_secs(10),
1536 * 1024,
)?;
ensure!(
reply.schema_version == 1
&& reply.challenge == challenge
&& reply.tenant_id == request.tenant_id
&& reply.asset_id == request.asset_id
&& reply.agent_id == request.agent_id
&& reply.enrollment_generation == request.enrollment_generation
&& reply.assignment_version == request.assignment_version
&& reply.grant_id == request.grant_id
&& reply.owner_nonce == request.owner_nonce
&& reply.handoff_sha256 == request.handoff_sha256
&& !reply.ready
&& reply.claim.is_none()
&& reply.original_start_base64url.is_none()
&& reply.original_outcome_base64url.is_none(),
"UPDATE_HEALTH_REPLY_BINDING"
);
let proof = reply.proof.context("UPDATE_HEALTH_REPLY_PROOF_REQUIRED")?;
let (bound, journal) = rmm_domain::agent_update::verify_inflight_proof(&proof, key)
.map_err(anyhow::Error::msg)?;
ensure!(
bound.bytes()? == header.bytes()?
&& journal.phase() == rmm_updater_handoff::Phase::HealthVerified,
"UPDATE_HEALTH_REPLY_ORIGINAL_PHASE"
);
let decoded = proof.decode().map_err(anyhow::Error::msg)?;
let event: rmm_updater_handoff::Event =
serde_json::from_slice(decoded.events.last().context("UPDATE_HEALTH_REPLY_EVENT")?)?;
ensure!(
matches!(event.evidence,rmm_updater_handoff::Evidence::HealthVerified{receipt:ref saved} if saved==receipt),
"UPDATE_HEALTH_REPLY_RECEIPT_CHANGED"
);
Ok(())
}
}
+98 -4
View File
@@ -1,3 +1,4 @@
mod agent_update_health;
mod apt; mod apt;
mod bootstrap; mod bootstrap;
mod certificate_rotation; mod certificate_rotation;
@@ -100,6 +101,12 @@ impl EffectFence {
&& (!root || self.root.load(Ordering::SeqCst)) && (!root || self.root.load(Ordering::SeqCst))
} }
} }
struct PendingUpdateHealth(tokio::task::JoinHandle<Result<()>>);
impl Drop for PendingUpdateHealth {
fn drop(&mut self) {
self.0.abort();
}
}
type Completion = ( type Completion = (
JobOffer, JobOffer,
rmm_domain::jobs::ExecutionPermitPayload, rmm_domain::jobs::ExecutionPermitPayload,
@@ -217,6 +224,51 @@ impl<C: reconcile::SyncClock> Agent<C> {
} }
Ok(()) Ok(())
} }
fn update_proof(&self) -> Result<Option<rmm_domain::agent_update::UpdateInflightProof>> {
self.binding
.as_ref()
.map(|binding| {
rmm_agent_core::update_health_inventory::current_proof(&self.db, binding)
})
.transpose()
.map(Option::flatten)
}
fn start_update_health(
&self,
proof: &rmm_domain::agent_update::UpdateInflightProof,
) -> Result<Option<PendingUpdateHealth>> {
if !self.clock.trusted() {
return Ok(None);
}
let binding = self.binding.as_ref().context("UPDATE_BINDING_REQUIRED")?;
let Some(context) =
agent_update_health::context(&self.db, binding, proof, self.sync_clock.now()?)?
else {
return Ok(None);
};
let header = context.handoff().clone();
let key = context.health_public_key();
let identity = self.certificates.current(Utc::now().timestamp())?;
let client = https_channel::mtls_client(
self.address,
&self.ca,
&identity.certificate_pem,
&identity.private_key_pem,
)?;
let helpers = self
.helpers
.clone()
.context("UPDATE_FULL_HELPER_REQUIRED")?;
let address = self.address;
Ok(Some(PendingUpdateHealth(tokio::spawn(async move {
let receipt = agent_update_health::request(&client, address, &context).await?;
tokio::task::spawn_blocking(move || {
helpers.submit_update_health(&header, key, &receipt)
})
.await??;
Ok(())
}))))
}
fn record( fn record(
&mut self, &mut self,
generation: u64, generation: u64,
@@ -245,12 +297,23 @@ impl<C: reconcile::SyncClock> Agent<C> {
if grant.is_empty() { if grant.is_empty() {
jobs::interrupted_request(&self.db, &offer)?; jobs::interrupted_request(&self.db, &offer)?;
} else { } else {
let saved =
jobs::recover_interrupted_outcome(&self.ledger, &binding, &offer, &grant)?;
if matches!(
binding.job(&offer.payload_bytes)?.action,
rmm_domain::jobs::Action::UpdateAgent { .. }
) && saved.is_none()
{
// This original may have intentionally handed off and
// restarted us. Gates stay closed until a genuine helper
// capture proves inflight; no Unknown is ever undone.
continue;
}
self.record( self.record(
0, 0,
&offer, &offer,
&grant, &grant,
jobs::recover_interrupted_outcome(&self.ledger, &binding, &offer, &grant)? saved.unwrap_or_else(|| {
.unwrap_or_else(|| {
rmm_privileged_helper::Outcome::unknown("AGENT_PROCESS_INTERRUPTED") rmm_privileged_helper::Outcome::unknown("AGENT_PROCESS_INTERRUPTED")
}), }),
)?; )?;
@@ -567,6 +630,8 @@ impl<C: reconcile::SyncClock> Agent<C> {
let mut rotation_at = Instant::now(); let mut rotation_at = Instant::now();
let mut capability_at = Instant::now(); let mut capability_at = Instant::now();
let mut first_heartbeat = true; let mut first_heartbeat = true;
let mut health_task: Option<PendingUpdateHealth> = None;
let mut health_inventory_prefix: Option<String> = None;
loop { loop {
tokio::select! { tokio::select! {
Some(result) = self.inventory_rx.recv() => { Some(result) = self.inventory_rx.recv() => {
@@ -574,8 +639,19 @@ impl<C: reconcile::SyncClock> Agent<C> {
if let Ok((data,observed_at)) = result { if let Ok((data,observed_at)) = result {
let inventory = self.inventory_envelope(generation,data,observed_at)?; let inventory = self.inventory_envelope(generation,data,observed_at)?;
jobs::event(&self.db,&inventory)?; jobs::event(&self.db,&inventory)?;
if let Some(binding)=&self.binding
&& let Err(error)=rmm_agent_core::update_health_inventory::retain_current(&self.db,&inventory,binding) {
report_closed_gate(&error);self.close_gate();needs_sync=true;
}
} else { eprintln!("inventory collection failed; previous observations remain historical"); } } else { eprintln!("inventory collection failed; previous observations remain historical"); }
} }
Some(result)=async {
match health_task.as_mut() {Some(task)=>Some((&mut task.0).await),None=>std::future::pending().await}
} => {
health_task.take();
if let Err(error)=result.map_err(anyhow::Error::from).and_then(std::convert::identity) {report_closed_gate(&error);}
self.close_gate();needs_sync=true;
},
message = ws.receive() => { message = ws.receive() => {
let message = message?; let message = message?;
ensure!(message.protocol_major == 1 && message.protocol_minor==0 && message.enrollment_generation==self.binding.as_ref().map_or(1,|b|b.enrollment_generation) && message.agent_id == self.id && message.connection_generation == generation, "INVALID_SERVER_ENVELOPE"); ensure!(message.protocol_major == 1 && message.protocol_minor==0 && message.enrollment_generation==self.binding.as_ref().map_or(1,|b|b.enrollment_generation) && message.agent_id == self.id && message.connection_generation == generation, "INVALID_SERVER_ENVELOPE");
@@ -639,9 +715,26 @@ impl<C: reconcile::SyncClock> Agent<C> {
_ = tick.tick() => { _ = tick.tick() => {
self.replay(&ws, generation).await?; self.replay(&ws, generation).await?;
let busy:bool=self.db.query_row("SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state IN ('requesting','running','verifying'))",[],|r|r.get(0))?; let busy:bool=self.db.query_row("SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state IN ('requesting','running','verifying'))",[],|r|r.get(0))?;
let update=self.update_proof()?;
let pending_update=self.binding.as_ref().map(|binding|rmm_agent_core::jobs::needs_update_control(&self.db,&self.ledger,binding)).transpose()?.unwrap_or(false);
let own_update=update.is_some() || pending_update;
if let Some(proof)=&update {
let decoded=proof.decode().map_err(anyhow::Error::msg)?;
let event:Option<rmm_updater_handoff::Event>=decoded.events.last().map(|raw|serde_json::from_slice(raw)).transpose()?;
if event.is_some_and(|event|matches!(event.evidence,rmm_updater_handoff::Evidence::ServicesStarted{..})) {
if health_inventory_prefix.as_deref()!=Some(proof.head_sha256.as_str()) {
// Fixed update health includes one genuinely new
// normal base sample, never a fresh timestamp on old data.
self.queue_inventory();health_inventory_prefix=Some(proof.head_sha256.clone());
}
if health_task.is_none() && self.prepared.is_none() {
match self.start_update_health(proof) {Ok(task)=>health_task=task,Err(error)=>{report_closed_gate(&error);self.close_gate();needs_sync=true;}}
}
}
}
if !busy && capability_at.elapsed()>=Duration::from_secs(60) {capability_at=Instant::now();if self.capabilities()!=advertised{return Ok(());}} if !busy && capability_at.elapsed()>=Duration::from_secs(60) {capability_at=Instant::now();if self.capabilities()!=advertised{return Ok(());}}
if self.prepared.is_some() && !busy && sync_at.elapsed()>=Duration::from_secs(20){self.close_gate();needs_sync=true;} if self.prepared.is_some() && (!busy||own_update) && health_task.is_none() && sync_at.elapsed()>=Duration::from_secs(20){self.close_gate();needs_sync=true;}
if self.prepared.is_none() && self.sync.is_some() && !busy && (needs_sync||sync_at.elapsed()>=Duration::from_secs(40)||!self.ready(generation,false)) { if self.prepared.is_none() && self.sync.is_some() && (!busy||own_update) && health_task.is_none() && (needs_sync||sync_at.elapsed()>=Duration::from_secs(40)||!self.ready(generation,false)) {
if let Err(error)=self.start_sync(&ws,generation).await{report_closed_gate(&error);self.close_gate();needs_sync=true;}else{needs_sync=false;}sync_at=Instant::now(); if let Err(error)=self.start_sync(&ws,generation).await{report_closed_gate(&error);self.close_gate();needs_sync=true;}else{needs_sync=false;}sync_at=Instant::now();
} }
if !needs_sync && self.prepared.is_none() && self.ready(generation,false) && jobs::events(&self.db)?.is_empty() && self.binding.is_some() if !needs_sync && self.prepared.is_none() && self.ready(generation,false) && jobs::events(&self.db)?.is_empty() && self.binding.is_some()
@@ -703,6 +796,7 @@ impl<C: reconcile::SyncClock> Agent<C> {
if self if self
.certificates .certificates
.due(self.rotation_policy, Utc::now().timestamp())? .due(self.rotation_policy, Utc::now().timestamp())?
&& self.update_proof()?.is_none()
{ {
self.close_gate(); self.close_gate();
drop(ws); drop(ws);
+7
View File
@@ -18,3 +18,10 @@ fs2 = "0.4.3"
rusqlite = { version = "0.32.1", features = ["bundled"] } rusqlite = { version = "0.32.1", features = ["bundled"] }
rmm-domain = { path = "../domain" } rmm-domain = { path = "../domain" }
rmm-agent-linux = { path = "../agent-linux" } rmm-agent-linux = { path = "../agent-linux" }
rmm-updater-handoff = { path = "../updater-handoff" }
rmm-updater-linux = { path = "../updater-linux" }
rmm-updater-worker = { path = "../updater-worker" }
base64 = "=0.22.1"
prost.workspace = true
rmm-protocol = { path = "../protocol" }
+2
View File
@@ -9,6 +9,8 @@ pub mod readiness;
pub mod reconcile; pub mod reconcile;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub mod update_control;
pub mod update_guard;
use anyhow::{Context, Result, ensure}; use anyhow::{Context, Result, ensure};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use openssl::pkey::{Id, PKey}; use openssl::pkey::{Id, PKey};
+98 -22
View File
@@ -133,11 +133,12 @@ impl Config {
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(untagged)] #[serde(untagged)]
enum Incoming { enum Incoming {
NativeUpdate(rmm_agent_core::update_handoff::NativeControlRequest),
Control(control::Request), Control(control::Request),
Probe(rmm_privileged_helper::readiness::Request), Probe(rmm_privileged_helper::readiness::Request),
Job(Request), Job(Request),
} }
fn peer_allowed(stream: &UnixStream, uid: u32) -> Result<()> { fn peer_uid(stream: &UnixStream) -> Result<u32> {
let mut credentials = std::mem::MaybeUninit::<libc::ucred>::uninit(); let mut credentials = std::mem::MaybeUninit::<libc::ucred>::uninit();
let mut size = std::mem::size_of::<libc::ucred>() as libc::socklen_t; let mut size = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
// SAFETY: getsockopt receives correctly aligned writable storage and length. // SAFETY: getsockopt receives correctly aligned writable storage and length.
@@ -158,10 +159,10 @@ fn peer_allowed(stream: &UnixStream, uid: u32) -> Result<()> {
"PEER_SIZE" "PEER_SIZE"
); );
// SAFETY: successful SO_PEERCRED initialized the complete ucred. // SAFETY: successful SO_PEERCRED initialized the complete ucred.
ensure!( Ok(unsafe { credentials.assume_init() }.uid)
unsafe { credentials.assume_init() }.uid == uid && uid != 0, }
"PEER_DENIED" fn peer_allowed(stream: &UnixStream, uid: u32) -> Result<()> {
); ensure!(uid != 0 && peer_uid(stream)? == uid, "PEER_DENIED");
Ok(()) Ok(())
} }
fn policy(config: &Config, job: &rmm_domain::jobs::JobPayload) -> Result<()> { fn policy(config: &Config, job: &rmm_domain::jobs::JobPayload) -> Result<()> {
@@ -320,17 +321,29 @@ fn execute(job: &rmm_domain::jobs::JobPayload) -> Result<Outcome> {
.into(), .into(),
}) })
} }
struct RuntimeClocks {
action: rmm_privileged_helper::TrustedClock,
sync: reconcile::NativeClock,
update: Option<rmm_updater_worker::clock::NativeClock>,
}
fn handle( fn handle(
config: &Config, config: &Config,
db: &mut rusqlite::Connection, db: &mut rusqlite::Connection,
stream: &mut UnixStream, stream: &mut UnixStream,
clock: &rmm_privileged_helper::TrustedClock, clocks: &RuntimeClocks,
sync: &mut reconcile::HelperSync, sync: &mut reconcile::HelperSync,
sync_clock: &reconcile::NativeClock,
mut image: Option<&mut image::RunningImage>, mut image: Option<&mut image::RunningImage>,
) -> Result<()> { ) -> Result<()> {
use rusqlite::OptionalExtension; use rusqlite::OptionalExtension;
peer_allowed(stream, config.agent_uid)?; let clock = &clocks.action;
let sync_clock = &clocks.sync;
let update_clock = clocks.update.as_ref();
let caller_uid = peer_uid(stream)?;
ensure!(
caller_uid == config.agent_uid
|| (caller_uid == 0 && config.profile == HelperProfile::FullMaintenanceV1),
"PEER_DENIED"
);
stream.set_read_timeout(Some(Duration::from_secs(5)))?; stream.set_read_timeout(Some(Duration::from_secs(5)))?;
stream.set_write_timeout(Some(Duration::from_secs(5)))?; stream.set_write_timeout(Some(Duration::from_secs(5)))?;
let mut size = [0; 4]; let mut size = [0; 4];
@@ -345,11 +358,36 @@ fn handle(
"RUNNING_HELPER_BINARY_CHANGED" "RUNNING_HELPER_BINARY_CHANGED"
); );
let request: Request = match serde_json::from_slice::<Incoming>(&bytes)? { let request: Request = match serde_json::from_slice::<Incoming>(&bytes)? {
Incoming::NativeUpdate(request) => {
ensure!(
config.profile == HelperProfile::FullMaintenanceV1
&& config.socket
== std::path::Path::new("/run/rmm-helper-maintenance/control.sock")
&& size <= 32768,
"UPDATE_FULL_CONTROL_REQUIRED"
);
let resource = reconcile::ExecutorGuard::acquire(&config.state, 0)?;
let response = rmm_privileged_helper::update_control::handle(
db,
&resource,
&config.binding,
update_clock.ok_or_else(|| anyhow::anyhow!("UPDATE_NOT_CONFIGURED"))?,
stream,
&request,
)?;
let raw = serde_json::to_vec(&response)?;
ensure!(raw.len() <= 1536 * 1024, "UPDATE_CONTROL_RESPONSE_LIMIT");
stream.write_all(&u32::try_from(raw.len())?.to_be_bytes())?;
stream.write_all(&raw)?;
return Ok(());
}
Incoming::Job(request) => { Incoming::Job(request) => {
peer_allowed(stream, config.agent_uid)?;
ensure!(size <= 512 * 1024, "JOB_REQUEST_SIZE"); ensure!(size <= 512 * 1024, "JOB_REQUEST_SIZE");
request request
} }
Incoming::Control(request) => { Incoming::Control(request) => {
peer_allowed(stream, config.agent_uid)?;
let (challenge, identity) = request.binding(); let (challenge, identity) = request.binding();
ensure!( ensure!(
!challenge.is_nil() !challenge.is_nil()
@@ -360,6 +398,21 @@ fn handle(
"CONTROL_IDENTITY" "CONTROL_IDENTITY"
); );
let guard = reconcile::ExecutorGuard::acquire(&config.state, 0)?; let guard = reconcile::ExecutorGuard::acquire(&config.state, 0)?;
let update_guard = if config.profile == HelperProfile::FullMaintenanceV1 {
rmm_privileged_helper::update_control::active_grant(db)?
.map(|grant| {
rmm_privileged_helper::update_guard::FullUpdateGuard::open(
db,
&guard,
&config.binding,
update_clock.ok_or_else(|| anyhow::anyhow!("UPDATE_NOT_CONFIGURED"))?,
grant,
)
})
.transpose()?
} else {
None
};
let (mut snapshot, mut unlocked) = (None, vec![]); let (mut snapshot, mut unlocked) = (None, vec![]);
match &request { match &request {
control::Request::Snapshot { generation, .. } => { control::Request::Snapshot { generation, .. } => {
@@ -369,27 +422,36 @@ fn handle(
&guard, &guard,
reconcile::Clock::now(sync_clock)?, reconcile::Clock::now(sync_clock)?,
)?; )?;
snapshot = Some(sync.snapshot( snapshot = Some(if let Some(owner) = &update_guard {
db, sync.snapshot(db, owner, *generation, sync_clock, &reconcile::NativeBoot)?
&guard, } else {
*generation, sync.snapshot(db, &guard, *generation, sync_clock, &reconcile::NativeBoot)?
sync_clock, })
&reconcile::NativeBoot,
)?)
} }
control::Request::Apply { control::Request::Apply {
response, response,
request_bytes, request_bytes,
.. ..
} => { } => {
let applied = sync.apply( let applied = if let Some(owner) = &update_guard {
sync.apply(
db,
owner,
response,
request_bytes,
sync_clock,
&reconcile::NativeBoot,
)?
} else {
sync.apply(
db, db,
&guard, &guard,
response, response,
request_bytes, request_bytes,
sync_clock, sync_clock,
&reconcile::NativeBoot, &reconcile::NativeBoot,
)?; )?
};
snapshot = Some(applied.snapshot); snapshot = Some(applied.snapshot);
unlocked = applied.unlocked_grants; unlocked = applied.unlocked_grants;
} }
@@ -446,6 +508,7 @@ fn handle(
return Ok(()); return Ok(());
} }
Incoming::Probe(request) => { Incoming::Probe(request) => {
peer_allowed(stream, config.agent_uid)?;
ensure!( ensure!(
config.profile == HelperProfile::AptOnly, config.profile == HelperProfile::AptOnly,
"PROBE_REQUIRES_APT_HELPER" "PROBE_REQUIRES_APT_HELPER"
@@ -722,9 +785,23 @@ fn run() -> Result<()> {
let _initialization_lock = lock_state(&config.state, 0)?; let _initialization_lock = lock_state(&config.state, 0)?;
open_ledger(&config.state, 0)? open_ledger(&config.state, 0)?
}; };
let clock = rmm_privileged_helper::TrustedClock::new(); let update_clock = if config.profile == HelperProfile::FullMaintenanceV1
let sync_clock = reconcile::NativeClock::default(); && std::path::Path::new("/etc/rmm-agent-updater/policy.json").try_exists()?
let mut sync = reconcile::HelperSync::new(config.binding.clone(), &sync_clock)?; {
Some(rmm_updater_worker::clock::NativeClock::new()?)
} else {
None
};
if update_clock.is_some() {
let guard = reconcile::ExecutorGuard::acquire(&config.state, 0)?;
rmm_agent_core::update_handoff::initialize(&db, &guard)?;
}
let clocks = RuntimeClocks {
action: rmm_privileged_helper::TrustedClock::new(),
sync: reconcile::NativeClock::default(),
update: update_clock,
};
let mut sync = reconcile::HelperSync::new(config.binding.clone(), &clocks.sync)?;
let parent = config let parent = config
.socket .socket
.parent() .parent()
@@ -751,9 +828,8 @@ fn run() -> Result<()> {
&config, &config,
&mut db, &mut db,
&mut stream, &mut stream,
&clock, &clocks,
&mut sync, &mut sync,
&sync_clock,
running_image.as_mut(), running_image.as_mut(),
) { ) {
let code = safe_error_code(&error); let code = safe_error_code(&error);
+1 -1
View File
@@ -27,7 +27,7 @@ impl BootSource for NativeBoot {
/// Holding this guard is mandatory through snapshot/apply/can_start/consume and /// Holding this guard is mandatory through snapshot/apply/can_start/consume and
/// actual effect. Production uses owner_uid=0; tests use their own private UID. /// actual effect. Production uses owner_uid=0; tests use their own private UID.
pub struct ExecutorGuard { pub struct ExecutorGuard {
_file: File, pub(crate) _file: File,
directory: PathBuf, directory: PathBuf,
_not_sync: std::marker::PhantomData<std::cell::Cell<()>>, _not_sync: std::marker::PhantomData<std::cell::Cell<()>>,
} }
@@ -0,0 +1,171 @@
//! Closed control of the one ORIGINAL Full-authorized update. Caller data never
//! authorizes an event: actual process/file/ledger observations are independent.
use crate::{Binding, reconcile::ExecutorGuard, update_guard::FullUpdateGuard};
use anyhow::{Context, Result, ensure};
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use rmm_agent_core::{
reconcile::SyncClock,
update_handoff::{
self as core, NativeControlOperation, NativeControlReply, NativeControlRequest,
NativeEffect, NativeService,
},
};
use rmm_updater_handoff::{Evidence, Phase};
use rmm_updater_worker::clock::NativeClock;
use rusqlite::Connection;
use std::os::unix::net::UnixStream;
use uuid::Uuid;
pub fn active_grant(db: &Connection) -> Result<Option<Uuid>> {
let exists:bool=db.query_row("SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='agent_update_handoffs')",[],|r|r.get(0))?;
if !exists {
return Ok(None);
}
let mut query=db.prepare("SELECT grant_id FROM agent_update_handoffs WHERE phase NOT IN ('unknown','outcome_persisted') ORDER BY grant_id LIMIT 2")?;
let rows = query
.query_map([], |r| r.get::<_, String>(0))?
.collect::<rusqlite::Result<Vec<_>>>()?;
ensure!(rows.len() <= 1, "UPDATE_MULTIPLE_ORIGINAL_HANDOFFS");
rows.first()
.map(|id| Uuid::parse_str(id).map_err(Into::into))
.transpose()
}
pub fn handle(
db: &mut Connection,
executor: &ExecutorGuard,
binding: &Binding,
clock: &NativeClock,
stream: &UnixStream,
request: &NativeControlRequest,
) -> Result<NativeControlReply> {
ensure!(
request.operation == "agent_update_control_v1"
&& request.schema_version == 1
&& !request.challenge.is_nil()
&& request.tenant_id == binding.tenant_id
&& request.asset_id == binding.asset_id
&& request.agent_id == binding.agent_id
&& request.enrollment_generation == binding.enrollment_generation
&& request.assignment_version == binding.assignment_version,
"UPDATE_CONTROL_BINDING"
);
let mut guard = FullUpdateGuard::open(db, executor, binding, clock, request.grant_id)?;
let header = guard.original().handoff().clone();
ensure!(
header.owner_nonce == request.owner_nonce
&& rmm_domain::jobs::hash(&header.bytes()?) == request.handoff_sha256,
"UPDATE_CONTROL_ORIGINAL_REQUIRED"
);
if !matches!(request.method, NativeControlOperation::ReadOriginal) {
guard.qualify_effects(db)?;
}
if matches!(request.method, NativeControlOperation::Health { .. }) {
guard.verify_health_peer(stream)?;
} else {
guard.verify_updater_peer(stream)?;
}
let mut original_start_base64url = None;
let mut claim = None;
let mut original_outcome_base64url = None;
match &request.method {
NativeControlOperation::ReadOriginal => {
original_start_base64url = Some(URL_SAFE_NO_PAD.encode(guard.original().start_bytes()))
}
NativeControlOperation::ClaimStop {
expected_head_sha256,
service,
} => {
claim = Some(core::claim_native_effect(
db,
&guard,
request.grant_id,
expected_head_sha256,
&NativeEffect::StopService { service: *service },
clock.now()?,
)?);
}
NativeControlOperation::ObserveStarts {
services,
observation_sha256,
} => {
let journal = core::read(db, &guard, request.grant_id)?;
ensure!(
journal.phase() == Phase::LedgerReturnIntent,
"UPDATE_START_ALREADY_OBSERVED_OR_PHASE"
);
let (actual, actual_hash) = guard.started()?;
ensure!(
services == &actual && observation_sha256 == &actual_hash,
"UPDATE_START_WORKER_WITNESS_CHANGED"
);
for service in [
NativeService::General,
NativeService::Full,
NativeService::Agent,
] {
let effect = NativeEffect::StartService { service };
let reserved = core::read_native_claim(db, &guard, request.grant_id, &effect)?;
let already:bool=db.query_row("SELECT EXISTS(SELECT 1 FROM agent_update_native_observations WHERE grant_id=?1 AND claim_sha256=?2)",rusqlite::params![request.grant_id.to_string(),rmm_domain::jobs::hash(&serde_json::to_vec(&reserved)?)],|r|r.get(0))?;
// One control request performs one sequence, never retries an
// uncertain partially committed start-observation transaction.
ensure!(!already, "UPDATE_START_OBSERVATION_ALREADY_COMMITTED");
core::observe_native_effect(
db,
&guard,
&reserved,
&guard.service_observation_sha256(service)?,
clock.now()?,
)?;
}
let event = journal.prepare(
u64::try_from(clock.now()?.timestamp())?,
Evidence::ServicesStarted {
services: actual,
ledger_identity_sha256: header.ledger_identity_sha256.clone(),
observation_sha256: actual_hash,
},
)?;
core::append(db, &guard, request.grant_id, journal.head_sha256(), &event)?;
}
NativeControlOperation::Health { receipt } => {
guard.qualify_health(db, receipt)?;
let journal = core::read(db, &guard, request.grant_id)?;
let event = journal.prepare(
u64::try_from(clock.now()?.timestamp())?,
Evidence::HealthVerified {
receipt: receipt.clone(),
},
)?;
core::append(db, &guard, request.grant_id, journal.head_sha256(), &event)?;
}
NativeControlOperation::Finalize => {
original_outcome_base64url =
Some(URL_SAFE_NO_PAD.encode(guard.publish_record_and_finalize(db)?));
}
}
let proof = if original_outcome_base64url.is_some() {
None
} else {
Some(
core::current_proof(db, &guard, request.grant_id)?
.context("UPDATE_ACTIVE_PROOF_REQUIRED")?,
)
};
Ok(NativeControlReply {
schema_version: 1,
challenge: request.challenge,
tenant_id: binding.tenant_id,
asset_id: binding.asset_id,
agent_id: binding.agent_id,
enrollment_generation: binding.enrollment_generation,
assignment_version: binding.assignment_version,
grant_id: request.grant_id,
owner_nonce: request.owner_nonce,
handoff_sha256: request.handoff_sha256.clone(),
ready: false,
proof,
claim,
original_start_base64url,
original_outcome_base64url,
})
}
@@ -0,0 +1,978 @@
//! Full-service-only update ownership. This is a local OS adapter, never a
//! caller's ready/approved claim. General keeps its original default denial.
use crate::{Binding, reconcile::ExecutorGuard};
use anyhow::{Context, Result, ensure};
use rmm_agent_core::{
helper_reconcile::ExecutorLedgerGuard,
reconcile::SyncClock,
update_handoff::{
self, NativeEffect, NativeEffectStage, NativeService, Participant, UpdateParticipantGuard,
},
};
use rmm_domain::{
agent_update::{ParticipantRole, UpdateDelegation},
jobs::hash,
};
use rmm_updater_handoff::{Event, Evidence, Handoff, Phase};
use rmm_updater_linux::{
Budget,
files::{Directory, Identity, LedgerIdentity},
services::{HeldService, Service, ServicePolicy, Systemd},
};
use rmm_updater_worker::{bundle, clock::NativeClock, original::Original, trust::TrustState};
use rusqlite::Connection;
use std::{
fs::{File, OpenOptions},
os::unix::fs::{MetadataExt, OpenOptionsExt},
path::Path,
time::Duration,
};
use uuid::Uuid;
fn identity(meta: &std::fs::Metadata) -> Identity {
Identity {
device: meta.dev(),
inode: meta.ino(),
uid: meta.uid(),
gid: meta.gid(),
mode: meta.mode(),
links: meta.nlink(),
}
}
fn lock_identity(executor: &ExecutorGuard) -> Result<Identity> {
let held = executor._file.metadata()?;
let named = std::fs::symlink_metadata("/var/lib/rmm-helper/executor.lock")?;
ensure!(
held.is_file()
&& named.is_file()
&& !named.file_type().is_symlink()
&& identity(&held) == identity(&named),
"UPDATE_HELD_LOCK_REPLACED"
);
Ok(identity(&held))
}
fn service_policies(original: &Original, trust: &TrustState, target: bool) -> Result<Systemd> {
let pair = if target {
&original.handoff().target
} else {
&original.handoff().predecessor
};
let mut pins = Vec::new();
for pin in &trust.policy().services {
let service = match pin.unit.as_str() {
"rmm-agent-remote.service" => Service::Agent,
"rmm-helper-cloud.service" => Service::General,
"rmm-full-maintenance-helper.service" => Service::Full,
"rmm-apt-helper.service" => Service::Apt,
_ => anyhow::bail!("UPDATE_UNKNOWN_PROVISIONED_SERVICE"),
};
pins.push(ServicePolicy {
service,
image_sha256: if service == Service::Agent {
pair.agent_sha256.clone()
} else {
pair.helper_sha256.clone()
},
unit_sha256: pin.unit_sha256.clone(),
});
}
Systemd::new(pins)
}
pub struct FullUpdateGuard<'a> {
executor: &'a ExecutorGuard,
binding: &'a Binding,
clock: &'a NativeClock,
original: Original,
trust: TrustState,
witnesses: bundle::Loaded,
budget: Budget,
services: Systemd,
own: HeldService,
updater: Option<HeldService>,
ledger_file: File,
directory: Directory,
sqlite_handle: usize,
participant: Participant,
history_qualified: std::cell::Cell<bool>,
health: Option<VerifiedHealth>,
}
impl<'a> FullUpdateGuard<'a> {
/// Only an already consumed original handoff may be reconstructed here.
/// Admission has a different pre-consumption adapter; this is no new start.
pub fn open(
db: &Connection,
executor: &'a ExecutorGuard,
binding: &'a Binding,
clock: &'a NativeClock,
grant: Uuid,
) -> Result<Self> {
executor.check(db)?;
let journal = update_handoff::read(db, executor, grant)?;
ensure!(
!matches!(journal.phase(), Phase::Unknown | Phase::OutcomePersisted),
"UPDATE_NOT_ACTIVE"
);
let (record,start):(Vec<u8>,Vec<u8>)=db.query_row("SELECT h.record,e.start FROM agent_update_handoffs h JOIN execution_evidence e USING(grant_id) WHERE h.grant_id=?1",[grant.to_string()],|r|Ok((r.get(0)?,r.get(1)?)))?;
let header: Handoff = serde_json::from_slice(&record)?;
// Read-only adjudication must still work after a different boot or
// original deadline. This bounded observation budget is NO effect lease.
let budget = Budget::new(
Uuid::parse_str(&rmm_agent_linux::boot_id()?)?,
Duration::from_secs(10),
)?;
let delegation = update_handoff::read_delegation(db, grant)?;
ensure!(
delegation.original_helper.role == ParticipantRole::LinuxFullHelper,
"UPDATE_FULL_AUTHORIZER_REQUIRED"
);
let original = Original::verify_retained(binding, &start, &record, delegation)?;
let trust = TrustState::load_retained(&original, binding)?;
let witnesses = bundle::read_for_handoff(original.handoff())?;
let target = matches!(
journal.phase(),
Phase::LedgerReturnIntent | Phase::ServicesStarted | Phase::HealthVerified
);
let services = service_policies(&original, &trust, target)?;
let own = services.observe(Service::Full, &budget)?;
ensure!(
own.proof().process.pid == std::process::id(),
"UPDATE_ONLY_ACTUAL_FULL_PROCESS"
);
let prior = &original.delegation().original_helper;
let participant =
if own.proof().process.pid == prior.pid && own.proof().process.birth == prior.birth {
ensure!(
own.proof().process.image_sha256 == prior.image_sha256
&& hash(&serde_json::to_vec(own.proof())?) == prior.service_instance_sha256,
"UPDATE_ORIGINAL_FULL_CHANGED"
);
Participant::OriginalHelper
} else if target && header.boot_id == Uuid::parse_str(&rmm_agent_linux::boot_id()?)? {
Participant::NewHelper
} else {
Participant::Observer
};
let updater = Systemd::observe_updater(
ServicePolicy {
service: Service::Updater,
image_sha256: original.handoff().updater_sha256.clone(),
unit_sha256: trust.policy().updater_service.unit_sha256.clone(),
},
&budget,
)
.ok();
let directory = Directory::open(Path::new("/var/lib/rmm-helper"), 0, true)?;
let ledger_file = OpenOptions::new()
.read(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC | libc::O_NONBLOCK)
.open("/var/lib/rmm-helper/consumption.sqlite")?;
let value = Self {
executor,
binding,
clock,
original,
trust,
witnesses,
budget,
services,
own,
updater,
ledger_file,
directory,
sqlite_handle: unsafe { db.handle() } as usize,
participant,
history_qualified: std::cell::Cell::new(false),
health: None,
};
value.check(db)?;
Ok(value)
}
pub fn qualify_effects(&self, db: &Connection) -> Result<()> {
self.check(db)?;
self.trust.recheck(&self.original, self.clock)?;
self.trust
.qualify_history(db, self.executor, self.binding)?;
self.history_qualified.set(true);
self.check_effect_window()
}
pub fn original(&self) -> &Original {
&self.original
}
pub fn budget(&self) -> &Budget {
&self.budget
}
pub fn trust(&self) -> &TrustState {
&self.trust
}
pub fn services(&self) -> &Systemd {
&self.services
}
pub fn verify_updater_peer(&self, stream: &std::os::unix::net::UnixStream) -> Result<()> {
let peer = self
.updater
.as_ref()
.context("UPDATE_ORIGINAL_UPDATER_NOT_LIVE")?
.authenticate_peer(stream, &self.budget)?;
ensure!(
peer == self.original.delegation().updater,
"UPDATE_ORIGINAL_UPDATER_PEER_REQUIRED"
);
Ok(())
}
pub fn verify_health_peer(&self, stream: &std::os::unix::net::UnixStream) -> Result<()> {
use std::os::fd::AsRawFd;
if self.verify_updater_peer(stream).is_ok() {
return Ok(());
}
self.check_effect_window()?;
ensure!(
self.participant == Participant::NewHelper,
"UPDATE_NEW_FULL_REQUIRED"
);
let agent = self.services.observe(Service::Agent, &self.budget)?;
let mut peer = std::mem::MaybeUninit::<libc::ucred>::uninit();
let mut length = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
ensure!(
unsafe {
libc::getsockopt(
stream.as_raw_fd(),
libc::SOL_SOCKET,
libc::SO_PEERCRED,
peer.as_mut_ptr().cast(),
&mut length,
)
} == 0
&& length as usize == std::mem::size_of::<libc::ucred>(),
"UPDATE_HEALTH_AGENT_PEER"
);
let peer = unsafe { peer.assume_init() };
let expected = &agent.proof().process;
ensure!(
peer.pid > 0
&& peer.pid as u32 == expected.pid
&& peer.uid == expected.uid
&& peer.gid == expected.gid,
"UPDATE_HEALTH_EXACT_AGENT_REQUIRED"
);
agent.verify(&self.budget)
}
pub fn qualify_health(
&mut self,
db: &Connection,
signed: &rmm_updater_handoff::SignedHealth,
) -> Result<()> {
self.check_effect_window()?;
ensure!(
self.participant == Participant::NewHelper,
"UPDATE_NEW_FULL_REQUIRED"
);
let mut journal = update_handoff::read(db, self, self.original.permit().grant_id)?;
ensure!(
journal.phase() == Phase::ServicesStarted,
"UPDATE_HEALTH_PHASE"
);
let event = journal.prepare(
u64::try_from(self.clock.now()?.timestamp())?,
Evidence::HealthVerified {
receipt: signed.clone(),
},
)?;
journal.replay(&event)?; // ORIGINAL pinned health signature and all kernel fields.
self.health = Some(self.health_evidence(db, signed)?);
Ok(())
}
pub fn qualify_stored_health(&mut self, db: &Connection) -> Result<Vec<u8>> {
self.check_effect_window()?;
let journal = update_handoff::read(db, self, self.original.permit().grant_id)?;
ensure!(
journal.phase() == Phase::HealthVerified,
"UPDATE_ORIGINAL_STORED_HEALTH_REQUIRED"
);
let event_raw:Vec<u8>=db.query_row("SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence DESC LIMIT 1",[self.original.permit().grant_id.to_string()],|r|r.get(0))?;
let event: Event = serde_json::from_slice(&event_raw)?;
ensure!(
hash(&event_raw) == journal.head_sha256(),
"UPDATE_STORED_HEALTH_HEAD_CHANGED"
);
let Evidence::HealthVerified { receipt } = &event.evidence else {
anyhow::bail!("UPDATE_STORED_HEALTH_EVENT_REQUIRED")
};
self.health = Some(self.health_evidence(db, receipt)?);
Ok(event_raw)
}
fn health_evidence(
&self,
db: &Connection,
signed: &rmm_updater_handoff::SignedHealth,
) -> Result<VerifiedHealth> {
use rmm_agent_core::crypto::Ed25519Verifier;
self.check_effect_window()?;
ensure!(
signed.payload.len() <= 4096 && signed.signature.len() == 64,
"UPDATE_HEALTH_SIGNED_SIZE"
);
let mut message = rmm_updater_handoff::HEALTH_DOMAIN.to_vec();
message.extend_from_slice(&signed.payload);
ensure!(
rmm_agent_core::crypto::DalekVerifier.verify(
&self.trust.health_public_key(),
&message,
&signed.signature
)?,
"UPDATE_ORIGINAL_HEALTH_SIGNATURE_REQUIRED"
);
let receipt: rmm_updater_handoff::HealthReceipt = serde_json::from_slice(&signed.payload)?;
let (generation, agent_journal, helper_journal): (u64, String, String) = db.query_row(
"SELECT generation,agent_journal_id,journal_id FROM helper_sync_meta WHERE id=1",
[],
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
)?;
ensure!(
generation == receipt.connection_generation
&& agent_journal == receipt.agent_journal_id.to_string()
&& helper_journal == receipt.helper_journal_id.to_string(),
"UPDATE_HEALTH_CURRENT_LOCAL_CHANNEL_REQUIRED"
);
self.verify_current_services(&receipt.service_processes)?;
let span = rmm_agent_core::update_health_sync::verified_span(
db,
self.executor,
self.binding,
self.original.handoff(),
&receipt,
self.trust.health_public_key(),
)?;
let applied = span.applied().clone();
verify_local_inventory(&self.witnesses, &self.original, &receipt)?;
Ok(VerifiedHealth {
signed_sha256: hash(&serde_json::to_vec(signed)?),
applied,
receipt,
})
}
pub fn started(&self) -> Result<(Vec<rmm_updater_handoff::StartedService>, String)> {
self.check_effect_window()?;
ensure!(
self.participant == Participant::NewHelper,
"UPDATE_NEW_FULL_REQUIRED"
);
let mut raw = Vec::new();
let mut proofs = Vec::new();
for service in [Service::Agent, Service::General, Service::Full] {
let held = self.services.observe(service, &self.budget)?;
proofs.push(held.started_proof(&self.budget)?);
raw.push(held.proof().clone());
}
Ok((proofs, hash(&serde_json::to_vec(&raw)?)))
}
pub fn service_observation_sha256(&self, service: NativeService) -> Result<String> {
self.check_effect_window()?;
let service = match service {
NativeService::Agent => Service::Agent,
NativeService::General => Service::General,
NativeService::Full => Service::Full,
};
let held = self.services.observe(service, &self.budget)?;
held.verify(&self.budget)?;
Ok(hash(&serde_json::to_vec(held.proof())?))
}
fn check_effect_window(&self) -> Result<()> {
ensure!(
self.history_qualified.get(),
"UPDATE_ACTUAL_GUARDED_HISTORY_REQUIRED"
);
ensure!(
Uuid::parse_str(&rmm_agent_linux::boot_id()?)? == self.original.handoff().boot_id,
"UPDATE_EFFECT_BOOT_CHANGED"
);
self.original
.check_current(self.binding, self.clock, &self.budget)?;
self.trust.recheck(&self.original, self.clock)?;
Ok(())
}
fn verify_current_services(
&self,
expected: &[rmm_updater_handoff::StartedService],
) -> Result<()> {
ensure!(
expected.len() == 3,
"UPDATE_EXACT_THREE_STARTED_PROOFS_REQUIRED"
);
for service in [Service::Agent, Service::General, Service::Full] {
let actual = self
.services
.observe(service, &self.budget)?
.started_proof(&self.budget)?;
ensure!(
expected.iter().any(|saved| saved == &actual),
"UPDATE_STARTED_PROCESS_CHANGED"
);
}
Ok(())
}
fn verify_owner(&self, phase: Phase, events: &[Event]) -> Result<()> {
if !matches!(phase, Phase::ServicesStarted | Phase::HealthVerified) {
let updater = self
.updater
.as_ref()
.context("UPDATE_ORIGINAL_UPDATER_NOT_LIVE")?;
updater.verify(&self.budget)?;
let proof = updater.proof();
let expected = &self.original.delegation().updater;
ensure!(
proof.process.pid == expected.pid
&& proof.process.birth == expected.birth
&& proof.process.image_sha256 == expected.image_sha256
&& hash(&serde_json::to_vec(proof)?) == expected.service_instance_sha256,
"UPDATE_ORIGINAL_UPDATER_OWNER_CHANGED"
);
}
if matches!(phase, Phase::ServicesStarted | Phase::HealthVerified) {
let started = events
.iter()
.find_map(|event| {
if let Evidence::ServicesStarted { services, .. } = &event.evidence {
Some(services)
} else {
None
}
})
.context("UPDATE_STARTED_PROOF_MISSING")?;
self.verify_current_services(started)?;
}
Ok(())
}
}
impl ExecutorLedgerGuard for FullUpdateGuard<'_> {
fn check(&self, db: &Connection) -> Result<()> {
self.check_namespace(db, None)
}
fn is_update_owner_authority(&self) -> bool {
true
}
fn check_update_owner(
&self,
handoff: &Handoff,
delegation: &UpdateDelegation,
phase: Phase,
events: &[Event],
) -> Result<()> {
ensure!(
handoff.bytes()? == self.original.handoff().bytes()?
&& delegation == self.original.delegation(),
"UPDATE_OWNER_ORIGINAL_CHANGED"
);
self.verify_owner(phase, events)
}
}
impl UpdateParticipantGuard for FullUpdateGuard<'_> {
fn participant(&self) -> Participant {
self.participant
}
fn verify_update_event(
&self,
handoff: &Handoff,
delegation: &UpdateDelegation,
event: &Event,
) -> Result<()> {
ensure!(
handoff.bytes()? == self.original.handoff().bytes()?
&& delegation == self.original.delegation(),
"UPDATE_EVENT_ORIGINAL_CHANGED"
);
self.check_effect_window()?;
ensure!(
self.participant == Participant::NewHelper,
"UPDATE_NEW_FULL_REQUIRED"
);
self.witnesses.files.verify_pair(&self.budget)?;
match &event.evidence {
Evidence::ServicesStarted {
services,
ledger_identity_sha256,
observation_sha256,
} => {
let mut held = Vec::new();
let mut proofs = Vec::new();
for service in [Service::Agent, Service::General, Service::Full] {
let current = self.services.observe(service, &self.budget)?;
proofs.push(current.started_proof(&self.budget)?);
held.push(current.proof().clone());
}
ensure!(
*services == proofs
&& *ledger_identity_sha256
== self.original.handoff().ledger_identity_sha256
&& *observation_sha256 == hash(&serde_json::to_vec(&held)?),
"UPDATE_ACTUAL_STARTED_PROOFS_REQUIRED"
);
}
Evidence::HealthVerified { receipt } => {
let health = self
.health
.as_ref()
.context("UPDATE_ACTUAL_LOCAL_HEALTH_REQUIRED")?;
ensure!(
hash(&serde_json::to_vec(receipt)?) == health.signed_sha256,
"UPDATE_LOCAL_HEALTH_RECEIPT_CHANGED"
);
}
// InstalledRecord/Outcome have a separate original-record CAS.
_ => anyhow::bail!("UPDATE_FULL_EVENT_NOT_YET_QUALIFIED"),
}
Ok(())
}
fn verify_native_effect(
&self,
handoff: &Handoff,
delegation: &UpdateDelegation,
effect: &NativeEffect,
stage: NativeEffectStage,
) -> Result<()> {
ensure!(
handoff.bytes()? == self.original.handoff().bytes()?
&& delegation == self.original.delegation(),
"UPDATE_NATIVE_ORIGINAL_CHANGED"
);
self.check_effect_window()?;
self.updater
.as_ref()
.context("UPDATE_ORIGINAL_UPDATER_NOT_LIVE")?
.verify(&self.budget)?;
match (self.participant, effect, stage) {
(
Participant::OriginalHelper,
NativeEffect::StopService { service },
NativeEffectStage::Claim,
) => {
let service = match service {
NativeService::Agent => Service::Agent,
NativeService::General => Service::General,
NativeService::Full => Service::Full,
};
self.services
.observe(service, &self.budget)?
.verify(&self.budget)?;
}
(
Participant::NewHelper,
NativeEffect::StartService { service },
NativeEffectStage::Observe,
) => {
let service = match service {
NativeService::Agent => Service::Agent,
NativeService::General => Service::General,
NativeService::Full => Service::Full,
};
self.services
.observe(service, &self.budget)?
.verify(&self.budget)?;
}
_ => anyhow::bail!("UPDATE_FULL_NATIVE_EFFECT_NOT_QUALIFIED"),
}
Ok(())
}
}
struct VerifiedHealth {
signed_sha256: String,
applied: update_handoff::AppliedUpdateSync,
receipt: rmm_updater_handoff::HealthReceipt,
}
fn verify_local_inventory(
witnesses: &bundle::Loaded,
original: &Original,
receipt: &rmm_updater_handoff::HealthReceipt,
) -> Result<()> {
use prost::Message;
use rmm_updater_worker::retained::Retention;
use rusqlite::OpenFlags;
const PATH: &str = "/var/lib/rmm-agent-remote/journal.sqlite";
ensure!(
witnesses
.retained
.specs()
.iter()
.any(|s| s.path == Path::new(PATH)
&& s.retention == Retention::MutableDatabase
&& s.identity.uid == 993),
"UPDATE_RETAINED_AGENT_JOURNAL_REQUIRED"
);
witnesses.retained.verify()?;
let db = Connection::open_with_flags(
PATH,
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
)?;
db.busy_timeout(Duration::ZERO)?;
// This journal belongs to the unprivileged agent. Bound SQLite's own
// materialization before querying; a hostile oversized BLOB/view cannot
// turn a root health check into an unbounded allocation or schema program.
unsafe {
rusqlite::ffi::sqlite3_limit(
db.handle(),
rusqlite::ffi::SQLITE_LIMIT_LENGTH,
(rmm_domain::MAX_MESSAGE + 65536) as i32,
);
}
db.execute_batch("PRAGMA trusted_schema=OFF;PRAGMA query_only=ON;")?;
let tx = db.unchecked_transaction()?;
let kind: String = tx.query_row(
"SELECT type FROM sqlite_master WHERE name='update_health_inventory'",
[],
|r| r.get(0),
)?;
ensure!(kind == "table", "UPDATE_AGENT_RETENTION_MUST_BE_TABLE");
let length: u64 = tx.query_row(
"SELECT length(envelope) FROM update_health_inventory WHERE grant_id=?1 AND message_id=?2",
rusqlite::params![
original.permit().grant_id.to_string(),
receipt.inventory_message_id.to_string()
],
|r| r.get(0),
)?;
ensure!(
length > 0 && length <= rmm_domain::MAX_MESSAGE as u64,
"UPDATE_LOCAL_INVENTORY_RAW_LIMIT"
);
let envelope = rmm_agent_core::update_health_inventory::read(
&tx,
original.permit().grant_id,
receipt.inventory_message_id,
)?;
let marker:(String,String)=tx.query_row("SELECT signed_decision_id,signed_response_sha256 FROM update_health_inventory WHERE grant_id=?1 AND message_id=?2",rusqlite::params![original.permit().grant_id.to_string(),receipt.inventory_message_id.to_string()],|r|Ok((r.get(0)?,r.get(1)?)))?;
ensure!(
marker
== (
receipt.signed_sync_decision_id.to_string(),
receipt.signed_sync_response_sha256.clone()
)
&& envelope.agent_id == original.handoff().attempt.agent_id.to_string()
&& envelope.boot_id == original.handoff().boot_id.to_string()
&& envelope.enrollment_generation == original.handoff().attempt.enrollment_generation
&& envelope.connection_generation == receipt.connection_generation,
"UPDATE_LOCAL_INVENTORY_ORIGINAL_BINDING"
);
let Some(rmm_protocol::Payload::InventorySnapshot(snapshot)) = &envelope.payload else {
anyhow::bail!("UPDATE_LOCAL_BASE_INVENTORY_REQUIRED")
};
ensure!(
snapshot.scope == "linux.base"
&& !snapshot.data.is_empty()
&& snapshot.sha256 == hex::decode(hash(&snapshot.data))?
&& envelope.encode_to_vec().len() <= rmm_domain::MAX_MESSAGE,
"UPDATE_LOCAL_BASE_INVENTORY_HASH"
);
rmm_domain::inventory_document::validate_for_scope(
&snapshot.data,
chrono::Utc::now(),
"linux.base",
)
.map_err(anyhow::Error::msg)?;
tx.commit()?;
witnesses.retained.verify()
}
impl FullUpdateGuard<'_> {
fn check_namespace(
&self,
db: &Connection,
namespace: Option<&rmm_updater_worker::record::RecordNamespaceProof<'_>>,
) -> Result<()> {
self.executor.check(db)?;
ensure!(
unsafe { db.handle() } as usize == self.sqlite_handle,
"UPDATE_ORIGINAL_SQLITE_HANDLE_CHANGED"
);
self.directory.verify()?;
let named = std::fs::symlink_metadata("/var/lib/rmm-helper/consumption.sqlite")?;
let held = self.ledger_file.metadata()?;
let lock = lock_identity(self.executor)?;
ensure!(
held.is_file()
&& named.is_file()
&& !named.file_type().is_symlink()
&& identity(&held) == identity(&named)
&& (LedgerIdentity {
ledger: identity(&held),
lock
})
.sha256()?
== self.original.handoff().ledger_identity_sha256,
"UPDATE_ORIGINAL_LEDGER_IDENTITIES_CHANGED"
);
self.budget.check()?;
if let Some(namespace) = namespace {
self.trust
.recheck_protected_record_namespace(&self.original, namespace)?;
} else {
self.trust.recheck_protected()?;
}
self.witnesses.retained.verify()?;
let start: Vec<u8> = db.query_row(
"SELECT start FROM execution_evidence WHERE grant_id=?1",
[self.original.permit().grant_id.to_string()],
|r| r.get(0),
)?;
ensure!(
start == self.original.start_bytes(),
"UPDATE_ORIGINAL_START_CHANGED"
);
self.own.verify(&self.budget)?;
if let Some(health) = &self.health {
self.verify_current_services(&health.receipt.service_processes)?;
ensure!(
update_handoff::read_applied_sync(
db,
self.executor,
self.original.permit().grant_id,
health.applied.decision_id
)? == health.applied,
"UPDATE_LOCAL_HEALTH_APPLY_CHANGED"
);
verify_local_inventory(&self.witnesses, &self.original, &health.receipt)?;
}
self.directory.verify()
}
fn check_record_effect(
&self,
namespace: &rmm_updater_worker::record::RecordNamespaceProof<'_>,
) -> Result<()> {
ensure!(
self.history_qualified.get() && self.participant == Participant::NewHelper,
"UPDATE_ORIGINAL_NEW_FULL_HISTORY_REQUIRED"
);
ensure!(
Uuid::parse_str(&rmm_agent_linux::boot_id()?)? == self.original.handoff().boot_id,
"UPDATE_RECORD_BOOT_CHANGED"
);
self.original
.check_current(self.binding, self.clock, &self.budget)?;
self.trust
.recheck_record_namespace(&self.original, self.clock, namespace)?;
self.witnesses.files.verify_pair(&self.budget)?;
self.own.verify(&self.budget)?;
ensure!(self.health.is_some(), "UPDATE_RECORD_LOCAL_HEALTH_REQUIRED");
Ok(())
}
}
/// The only admitted deviation from TrustState's active record name. This
/// wrapper is local, nonserializable and borrowed from a live one-shot port.
struct FullRecordGuard<'a, 'base, 'transition> {
base: &'a FullUpdateGuard<'base>,
namespace: &'a rmm_updater_worker::record::RecordNamespaceProof<'transition>,
}
impl ExecutorLedgerGuard for FullRecordGuard<'_, '_, '_> {
fn check(&self, db: &Connection) -> Result<()> {
self.base.check_namespace(db, Some(self.namespace))
}
fn is_update_owner_authority(&self) -> bool {
true
}
fn check_update_owner(
&self,
h: &Handoff,
d: &UpdateDelegation,
p: Phase,
e: &[Event],
) -> Result<()> {
self.base.check_update_owner(h, d, p, e)
}
}
impl UpdateParticipantGuard for FullRecordGuard<'_, '_, '_> {
fn participant(&self) -> Participant {
self.base.participant
}
fn verify_update_event(&self, h: &Handoff, d: &UpdateDelegation, event: &Event) -> Result<()> {
ensure!(
h.bytes()? == self.base.original.handoff().bytes()?
&& d == self.base.original.delegation(),
"UPDATE_RECORD_FINAL_ORIGINAL_BINDING"
);
self.base.check_record_effect(self.namespace)?;
ensure!(
self.namespace.phase() == rmm_updater_worker::record::RecordPhase::Published
&& matches!(event.evidence, Evidence::OutcomePersisted { .. }),
"UPDATE_RECORD_FINAL_PUBLICATION_REQUIRED"
);
Ok(())
}
fn verify_native_effect(
&self,
h: &Handoff,
d: &UpdateDelegation,
effect: &NativeEffect,
stage: NativeEffectStage,
) -> Result<()> {
use rmm_updater_worker::record::RecordPhase;
ensure!(
h.bytes()? == self.base.original.handoff().bytes()?
&& d == self.base.original.delegation(),
"UPDATE_RECORD_NATIVE_ORIGINAL_BINDING"
);
self.base.check_record_effect(self.namespace)?;
let (old, new, expected) = match (effect, stage) {
(
NativeEffect::SaveInstalledRecord {
old_sha256,
new_sha256,
},
NativeEffectStage::Claim,
) => (old_sha256, new_sha256, RecordPhase::Prepared),
(
NativeEffect::SaveInstalledRecord {
old_sha256,
new_sha256,
},
NativeEffectStage::Observe,
) => (old_sha256, new_sha256, RecordPhase::Saved),
(
NativeEffect::PublishInstalledRecord {
old_sha256,
new_sha256,
},
NativeEffectStage::Claim,
) => (old_sha256, new_sha256, RecordPhase::Saved),
(
NativeEffect::PublishInstalledRecord {
old_sha256,
new_sha256,
},
NativeEffectStage::Observe,
) => (old_sha256, new_sha256, RecordPhase::Published),
_ => anyhow::bail!("UPDATE_ONLY_ORIGINAL_RECORD_EFFECTS"),
};
ensure!(
self.namespace.phase() == expected
&& self.namespace.old_sha256() == old
&& self.namespace.new_sha256() == new,
"UPDATE_EXACT_RECORD_EFFECT_NAMESPACE"
);
Ok(())
}
}
struct FullRecordFence<'db, 'base, 'inner> {
db: &'db mut Connection,
base: &'base FullUpdateGuard<'inner>,
pending: Option<update_handoff::NativeEffectClaim>,
}
impl rmm_updater_worker::record::RecordFence for FullRecordFence<'_, '_, '_> {
fn before_effect(
&mut self,
effect: &NativeEffect,
namespace: &rmm_updater_worker::record::RecordNamespaceProof<'_>,
) -> Result<()> {
ensure!(self.pending.is_none(), "UPDATE_RECORD_UNOBSERVED_CLAIM");
let guard = FullRecordGuard {
base: self.base,
namespace,
};
let journal = update_handoff::read(self.db, &guard, self.base.original.permit().grant_id)?;
self.pending = Some(update_handoff::claim_native_effect(
self.db,
&guard,
self.base.original.permit().grant_id,
journal.head_sha256(),
effect,
self.base.clock.now()?,
)?);
Ok(())
}
fn after_effect(
&mut self,
effect: &NativeEffect,
namespace: &rmm_updater_worker::record::RecordNamespaceProof<'_>,
observed: &str,
) -> Result<()> {
ensure!(
namespace.observation_sha256()? == observed,
"UPDATE_RECORD_ACTUAL_OBSERVATION_REQUIRED"
);
let claim = self
.pending
.as_ref()
.context("UPDATE_RECORD_ORIGINAL_CLAIM_REQUIRED")?;
ensure!(claim.effect() == effect, "UPDATE_RECORD_EFFECT_CHANGED");
let guard = FullRecordGuard {
base: self.base,
namespace,
};
update_handoff::observe_native_effect(
self.db,
&guard,
claim,
observed,
self.base.clock.now()?,
)?;
self.pending = None;
Ok(())
}
}
impl FullUpdateGuard<'_> {
/// One local original finalization. Does not set consumed.result or release
/// any start fence. A failed/uncertain publication is never dispatched again.
pub fn publish_record_and_finalize(&mut self, db: &mut Connection) -> Result<Vec<u8>> {
self.qualify_effects(db)?;
let event = self.qualify_stored_health(db)?;
let journal = update_handoff::read(db, self, self.original.permit().grant_id)?;
let built = rmm_updater_worker::record::BuiltRecord::build(
&self.original,
self.trust.installed_raw(),
&journal,
&event,
)?;
let record_sha = built.sha256().to_owned();
let health_sha = self
.health
.as_ref()
.context("UPDATE_ACTUAL_LOCAL_HEALTH_REQUIRED")?
.signed_sha256
.clone();
let mut transition =
rmm_updater_worker::record::RecordTransition::prepare(built, &self.budget)?;
{
let mut fence = FullRecordFence {
db,
base: self,
pending: None,
};
transition.save_previous(&mut fence, &self.budget)?;
transition.publish(&mut fence, &self.budget)?;
ensure!(
fence.pending.is_none(),
"UPDATE_RECORD_PUBLICATION_OBSERVATION_REQUIRED"
);
}
let namespace = transition.namespace_proof();
let guard = FullRecordGuard {
base: self,
namespace: &namespace,
};
let journal = update_handoff::read(db, &guard, self.original.permit().grant_id)?;
let outcome = crate::Outcome {
state: "succeeded".into(),
stdout: vec![],
stderr: vec![],
exit_code: Some(0),
output_truncated: false,
reboot_required: false,
error_code: String::new(),
evidence: serde_json::json!({"postconditions_passed":true,"previous_boot_id":self.original.handoff().boot_id,"agent_update":{"schema_version":1,"handoff_sha256":journal.handoff_sha256(),"manifest_sha256":self.original.handoff().target.manifest_sha256,"predecessor_record_sha256":self.original.handoff().predecessor_record_sha256,"installed_record_sha256":record_sha,"signed_health_sha256":health_sha,"activation_floor":namespace.record().record().activation_floor,"rolled_back":false}}),
};
update_handoff::finalize(
db,
&guard,
self.original.permit(),
journal.head_sha256(),
u64::try_from(self.clock.now()?.timestamp())?,
&outcome,
)?;
let bytes: Vec<u8> = db.query_row(
"SELECT outcome FROM execution_evidence WHERE grant_id=?1",
[self.original.permit().grant_id.to_string()],
|r| r.get(0),
)?;
Ok(bytes)
}
}
+1 -3
View File
@@ -14,11 +14,9 @@ uuid.workspace = true
libc = "0.2.177" libc = "0.2.177"
rusqlite = { version = "0.32.1", features = ["bundled"] } rusqlite = { version = "0.32.1", features = ["bundled"] }
ed25519-dalek = "=2.2.0" ed25519-dalek = "=2.2.0"
base64 = "=0.22.1"
rmm-domain = { path = "../domain" } rmm-domain = { path = "../domain" }
rmm-agent-core = { path = "../agent-core" } rmm-agent-core = { path = "../agent-core" }
rmm-updater = { path = "../updater" } rmm-updater = { path = "../updater" }
rmm-updater-handoff = { path = "../updater-handoff" } rmm-updater-handoff = { path = "../updater-handoff" }
rmm-updater-linux = { path = "../updater-linux" } rmm-updater-linux = { path = "../updater-linux" }
[dev-dependencies]
base64 = "=0.22.1"
+210
View File
@@ -0,0 +1,210 @@
//! Existing Agent singleton fence. No create, ACL change or Root-ledger alias.
//! The agent holds this exact lock before opening BOTH of its SQLite files.
use crate::retained::{RetainedSet, Retention};
use anyhow::{Context, Result, ensure};
use rmm_updater_linux::{
Budget,
files::{Directory, Identity},
services::StoppedServices,
};
use std::{
fs::{File, OpenOptions},
os::{
fd::AsRawFd,
unix::fs::{MetadataExt, OpenOptionsExt},
},
path::Path,
};
const PARENT: &str = "/var/lib/rmm-agent-remote";
const PATH: &str = "/var/lib/rmm-agent-remote/agent.lock";
pub struct BoundAgentLock {
directory: Directory,
file: File,
expected: Identity,
}
impl BoundAgentLock {
/// Open/bind before any stop, so a missing preservation witness cannot be
/// discovered only after stopping the Agent. Reading never takes its lock.
pub fn bind(retained: &RetainedSet) -> Result<Self> {
retained.verify()?;
let specs = retained.specs();
let spec = specs
.iter()
.find(|s| s.path == Path::new(PATH))
.context("UPDATE_AGENT_LOCK_WITNESS_REQUIRED")?;
ensure!(
spec.retention == Retention::ImmutableMetadata
&& spec.parent_uid == 993
&& spec.identity.uid == 993
&& spec.identity.gid == 984
&& spec.identity.mode & 0o7777 == 0o600
&& spec.identity.links == 1,
"UPDATE_AGENT_LOCK_PROVISIONING"
);
let directory = Directory::open(Path::new(PARENT), 993, true)?;
let file = OpenOptions::new()
.read(true)
.write(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC)
.open(PATH)?;
let result = Self {
directory,
file,
expected: spec.identity.clone(),
};
result.verify()?;
Ok(result)
}
fn verify(&self) -> Result<()> {
self.directory.verify()?;
let held = self.file.metadata()?;
let named = std::fs::symlink_metadata(PATH)?;
ensure!(
held.is_file()
&& held.len() == 0
&& named.is_file()
&& !named.file_type().is_symlink()
&& identity(&held) == self.expected
&& identity(&named) == self.expected,
"UPDATE_AGENT_LOCK_IDENTITY_CHANGED"
);
Ok(())
}
pub fn identity(&self) -> &Identity {
&self.expected
}
/// Real, fresh stopped-service proof precedes the existing nonblocking
/// singleton acquisition. A running/restarted Agent makes this fail closed.
pub fn acquire(self, stopped: &StoppedServices, budget: &Budget) -> Result<HeldAgentLock> {
stopped.verify_still_stopped(budget)?;
let result = self.acquire_existing()?;
stopped.verify_still_stopped(budget)?;
Ok(result)
}
fn acquire_existing(self) -> Result<HeldAgentLock> {
self.verify()?;
ensure!(
unsafe { libc::flock(self.file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0,
"UPDATE_AGENT_STARTUP_BUSY"
);
let result = HeldAgentLock { bound: self };
result.verify()?;
Ok(result)
}
}
pub struct HeldAgentLock {
bound: BoundAgentLock,
}
impl HeldAgentLock {
pub fn verify(&self) -> Result<()> {
self.bound.verify()
}
pub fn identity(&self) -> &Identity {
self.bound.identity()
}
/// Only the live Engine calls this after SQLite close and actual original
/// Root flock return, before the explicit once-only service start sequence.
pub(crate) fn return_after_root_release(self, budget: &Budget) -> Result<()> {
budget.check()?;
self.verify()?;
ensure!(
unsafe { libc::flock(self.bound.file.as_raw_fd(), libc::LOCK_UN) } == 0,
"UPDATE_AGENT_STARTUP_RETURN_FAILED"
);
Ok(())
}
}
impl Drop for HeldAgentLock {
fn drop(&mut self) {
unsafe {
libc::flock(self.bound.file.as_raw_fd(), libc::LOCK_UN);
}
}
}
fn identity(meta: &std::fs::Metadata) -> Identity {
Identity {
device: meta.dev(),
inode: meta.ino(),
uid: meta.uid(),
gid: meta.gid(),
mode: meta.mode(),
links: meta.nlink(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::retained::RetainedFileSpec;
use std::os::unix::fs::PermissionsExt;
struct Cleanup;
impl Drop for Cleanup {
fn drop(&mut self) {
std::fs::remove_dir_all(PARENT).unwrap();
}
}
#[test]
fn existing_agent_lock_blocks_aliases_and_rejects_samebyte_inode_substitution() {
// Private network-none/root container fixture ONLY; no host path/test.
assert_eq!(unsafe { libc::geteuid() }, 0);
assert!(!Path::new(PARENT).exists());
std::fs::create_dir_all(PARENT).unwrap();
let _cleanup = Cleanup;
std::fs::set_permissions(PARENT, std::fs::Permissions::from_mode(0o700)).unwrap();
let file = OpenOptions::new()
.create_new(true)
.read(true)
.write(true)
.mode(0o600)
.open(PATH)
.unwrap();
for path in [PARENT, PATH] {
let path = std::ffi::CString::new(path).unwrap();
assert_eq!(unsafe { libc::chown(path.as_ptr(), 993, 984) }, 0);
}
let retained = RetainedSet::capture(vec![RetainedFileSpec {
path: Path::new(PATH).to_owned(),
parent_uid: 993,
identity: identity(&file.metadata().unwrap()),
retention: Retention::ImmutableMetadata,
}])
.unwrap();
let bound = BoundAgentLock::bind(&retained).unwrap();
// The existing Agent's separately opened descriptor owns the lock.
assert_eq!(
unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) },
0
);
assert!(bound.acquire_existing().is_err());
assert_eq!(unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_UN) }, 0);
let held = BoundAgentLock::bind(&retained)
.unwrap()
.acquire_existing()
.unwrap();
assert_ne!(
unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) },
0
);
held.verify().unwrap();
std::fs::rename(PATH, Path::new(PARENT).join("agent.lock.previous")).unwrap();
let replacement = OpenOptions::new()
.create_new(true)
.read(true)
.write(true)
.mode(0o600)
.open(PATH)
.unwrap();
let path = std::ffi::CString::new(PATH).unwrap();
assert_eq!(unsafe { libc::chown(path.as_ptr(), 993, 984) }, 0);
assert_eq!(replacement.metadata().unwrap().len(), 0);
assert!(held.verify().is_err());
assert!(BoundAgentLock::bind(&retained).is_err());
drop(held);
assert_eq!(
unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) },
0
);
}
}
+569
View File
@@ -0,0 +1,569 @@
//! Bounded client of the EXISTING fixed Fullhelper control socket. No retry,
//! SQL/event transport, caller paths, process-name or root-UID-only fallback.
use crate::{clock::NativeClock, participants::StableParticipant};
use anyhow::{Context, Result, ensure};
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use rmm_agent_core::{
reconcile::SyncClock,
update_handoff::{
NativeControlOperation, NativeControlReply, NativeControlRequest, NativeEffect,
NativeEffectClaim,
},
};
use rmm_domain::{
agent_update::{ParticipantRole, UpdateDelegation, UpdateInflightProof, verify_inflight_proof},
reconciliation::{ReconciledOutcome, canonical_outcome_bytes},
};
use rmm_updater_handoff::{Handoff, Phase};
use rmm_updater_linux::{
Budget,
files::Directory,
services::{Service, Systemd},
};
use std::{
io::{Read, Write},
os::{
fd::{AsRawFd, FromRawFd},
unix::{
fs::{FileTypeExt, MetadataExt},
net::UnixStream,
},
},
path::Path,
time::{Duration, Instant},
};
use uuid::Uuid;
const SOCKET: &str = "/run/rmm-helper-maintenance/control.sock";
const MAX_REPLY: usize = 1536 * 1024;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Generation {
Original,
Replacement,
}
pub struct BridgeContext<'a> {
pub handoff: &'a Handoff,
pub delegation: &'a UpdateDelegation,
pub services: &'a Systemd,
pub updater: &'a StableParticipant,
pub clock: &'a NativeClock,
pub budget: &'a Budget,
pub health_public_key: [u8; 32],
pub generation: Generation,
}
pub struct FullBridge<'a> {
context: BridgeContext<'a>,
prefix: Option<UpdateInflightProof>,
poisoned: bool,
finalized: bool,
}
pub struct Reply {
pub proof: Option<UpdateInflightProof>,
pub original_start: Option<Vec<u8>>,
pub claim: Option<NativeEffectClaim>,
pub original_outcome: Option<Vec<u8>>,
}
impl<'a> FullBridge<'a> {
/// Prefix is either the exact initial atomic StopIntent or a complete
/// retained prefix read under the original guarded ledger. Never truncate.
pub fn new(context: BridgeContext<'a>, prefix: UpdateInflightProof) -> Result<Self> {
let (handoff, _) = verify_inflight_proof(&prefix, context.health_public_key)
.map_err(anyhow::Error::msg)?;
ensure!(
handoff.bytes()? == context.handoff.bytes()?
&& prefix.delegation == *context.delegation,
"UPDATE_BRIDGE_PREFIX_BINDING"
);
Ok(Self {
context,
prefix: Some(prefix),
poisoned: false,
finalized: false,
})
}
/// First read obtains the actual original prefix from the authenticated
/// Full process/ledger. No event is synthesized from a caller's header.
pub fn original(context: BridgeContext<'a>) -> Result<Self> {
ensure!(
context.generation == Generation::Original,
"UPDATE_BRIDGE_ORIGINAL_ONLY"
);
context.handoff.validate()?;
ensure!(
context.delegation.handoff_sha256 == rmm_domain::jobs::hash(&context.handoff.bytes()?)
&& context.delegation.owner_nonce == context.handoff.owner_nonce,
"UPDATE_BRIDGE_ORIGINAL_HEADER"
);
Ok(Self {
context,
prefix: None,
poisoned: false,
finalized: false,
})
}
pub fn call(&mut self, method: NativeControlOperation) -> Result<Reply> {
let context = &self.context;
context
.updater
.verify_delegation(context.delegation, context.budget)?;
context.budget.check()?;
if self.prefix.is_none() {
ensure!(
matches!(method, NativeControlOperation::ReadOriginal),
"UPDATE_BRIDGE_ORIGINAL_READ_REQUIRED"
);
}
let journal = self
.prefix
.as_ref()
.map(|prefix| {
verify_inflight_proof(prefix, context.health_public_key)
.map(|(_, journal)| journal)
.map_err(anyhow::Error::msg)
})
.transpose()?;
match &method {
NativeControlOperation::ReadOriginal => {}
NativeControlOperation::ClaimStop {
expected_head_sha256,
..
} => ensure!(
context.generation == Generation::Original
&& journal
.as_ref()
.is_some_and(|j| j.phase() == Phase::StopIntent)
&& Some(expected_head_sha256.as_str())
== self.prefix.as_ref().map(|p| p.head_sha256.as_str()),
"UPDATE_BRIDGE_CLAIM_PHASE"
),
NativeControlOperation::ObserveStarts { .. } => ensure!(
context.generation == Generation::Replacement
&& journal
.as_ref()
.is_some_and(|j| j.phase() == Phase::LedgerReturnIntent),
"UPDATE_BRIDGE_START_PHASE"
),
NativeControlOperation::Health { receipt } => {
ensure!(
context.generation == Generation::Replacement
&& journal
.as_ref()
.is_some_and(|j| j.phase() == Phase::ServicesStarted),
"UPDATE_BRIDGE_HEALTH_PHASE"
);
journal
.as_ref()
.context("UPDATE_BRIDGE_ORIGINAL_READ_REQUIRED")?
.prepare(
u64::try_from(context.clock.now()?.timestamp())?,
rmm_updater_handoff::Evidence::HealthVerified {
receipt: receipt.clone(),
},
)?;
}
NativeControlOperation::Finalize => ensure!(
context.generation == Generation::Replacement
&& journal
.as_ref()
.is_some_and(|j| j.phase() == Phase::HealthVerified),
"UPDATE_BRIDGE_FINAL_PHASE"
),
}
let mutating = !matches!(method, NativeControlOperation::ReadOriginal);
ensure!(
!self.finalized && (!mutating || !self.poisoned),
"UPDATE_BRIDGE_NO_RETRY_OR_RESUME"
);
if mutating {
self.poisoned = true;
}
let io_budget = IoBudget {
clock: context.clock,
budget: context.budget,
finish_before: context.handoff.finish_before,
until: Instant::now() + Duration::from_secs(5),
};
io_budget.remaining()?;
ensure!(
std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
.trim()
.parse::<Uuid>()?
== context.handoff.boot_id,
"UPDATE_BRIDGE_BOOT_CHANGED"
);
let directory = Directory::open(Path::new("/run/rmm-helper-maintenance"), 0, false)?;
let meta = std::fs::symlink_metadata(SOCKET)?;
ensure!(
meta.file_type().is_socket()
&& meta.uid() == 0
&& meta.mode() & 0o007 == 0
&& meta.nlink() == 1,
"UPDATE_BRIDGE_SOCKET_PERMISSIONS"
);
let stream = connect(&io_budget)?;
directory.verify()?;
let after = std::fs::symlink_metadata(SOCKET)?;
ensure!(
meta.dev() == after.dev()
&& meta.ino() == after.ino()
&& meta.mode() == after.mode()
&& meta.uid() == after.uid()
&& meta.gid() == after.gid(),
"UPDATE_BRIDGE_SOCKET_CHANGED"
);
let helper = context.services.observe(Service::Full, context.budget)?;
let peer = helper.authenticate_peer(&stream, context.budget)?;
ensure!(
peer.role == ParticipantRole::LinuxFullHelper,
"UPDATE_BRIDGE_FULL_REQUIRED"
);
match context.generation {
Generation::Original => ensure!(
peer == context.delegation.original_helper,
"UPDATE_BRIDGE_ORIGINAL_HELPER_CHANGED"
),
Generation::Replacement => ensure!(
peer.image_sha256 == context.handoff.target.helper_sha256
&& peer.pid != context.delegation.original_helper.pid,
"UPDATE_BRIDGE_REPLACEMENT_HELPER_CHANGED"
),
}
let request = NativeControlRequest {
operation: "agent_update_control_v1".into(),
schema_version: 1,
challenge: Uuid::new_v4(),
tenant_id: context.handoff.attempt.tenant_id,
asset_id: context.handoff.attempt.asset_id,
agent_id: context.handoff.attempt.agent_id,
enrollment_generation: context.handoff.attempt.enrollment_generation,
assignment_version: context.handoff.attempt.assignment_version,
grant_id: context.handoff.attempt.grant_id,
owner_nonce: context.handoff.owner_nonce,
handoff_sha256: context.delegation.handoff_sha256.clone(),
method,
};
let raw = serde_json::to_vec(&request)?;
ensure!(
!raw.is_empty() && raw.len() <= 32768,
"UPDATE_BRIDGE_REQUEST_LIMIT"
);
let mut io = SocketIo {
stream,
budget: io_budget,
};
io.write_all(&(raw.len() as u32).to_be_bytes())?;
io.write_all(&raw)?;
io.flush()?;
let mut size = [0; 4];
io.read_exact(&mut size)?;
let size = u32::from_be_bytes(size) as usize;
ensure!(size > 0 && size <= MAX_REPLY, "UPDATE_BRIDGE_REPLY_LIMIT");
let mut raw = vec![0; size];
io.read_exact(&mut raw)?;
let reply: NativeControlReply = serde_json::from_slice(&raw)?;
helper.verify(context.budget)?;
ensure!(
reply.schema_version == 1
&& !reply.ready
&& reply.challenge == request.challenge
&& reply.tenant_id == request.tenant_id
&& reply.asset_id == request.asset_id
&& reply.agent_id == request.agent_id
&& reply.enrollment_generation == request.enrollment_generation
&& reply.assignment_version == request.assignment_version
&& reply.grant_id == request.grant_id
&& reply.owner_nonce == request.owner_nonce
&& reply.handoff_sha256 == request.handoff_sha256,
"UPDATE_BRIDGE_REPLY_BINDING"
);
let original_start = reply
.original_start_base64url
.as_deref()
.map(|v| decode(v, 512 * 1024))
.transpose()?;
let original_outcome = reply
.original_outcome_base64url
.as_deref()
.map(|v| decode(v, 512 * 1024))
.transpose()?;
if matches!(request.method, NativeControlOperation::Finalize) {
ensure!(
reply.proof.is_none() && reply.claim.is_none() && original_start.is_none(),
"UPDATE_BRIDGE_FINAL_REPLY"
);
let raw = original_outcome
.as_ref()
.context("UPDATE_BRIDGE_ORIGINAL_OUTCOME_MISSING")?;
let outcome: ReconciledOutcome = serde_json::from_slice(raw)?;
ensure!(
canonical_outcome_bytes(&outcome)? == *raw
&& outcome.state == "succeeded"
&& !outcome.reboot_required,
"UPDATE_BRIDGE_NONCANONICAL_OR_UNSUCCESSFUL_OUTCOME"
);
} else {
ensure!(
original_outcome.is_none(),
"UPDATE_BRIDGE_UNEXPECTED_TERMINAL"
);
let proof = reply
.proof
.as_ref()
.context("UPDATE_BRIDGE_PROOF_MISSING")?;
let (handoff, journal) = verify_inflight_proof(proof, context.health_public_key)
.map_err(anyhow::Error::msg)?;
ensure!(
handoff.bytes()? == context.handoff.bytes()?
&& proof.delegation == *context.delegation
&& self
.prefix
.as_ref()
.is_none_or(|prior| proof.events.len() >= prior.events.len()
&& proof.events[..prior.events.len()] == prior.events),
"UPDATE_BRIDGE_PROOF_ROLLBACK_OR_CHANGED"
);
match request.method {
NativeControlOperation::ReadOriginal => ensure!(
reply.claim.is_none() && original_start.is_some(),
"UPDATE_BRIDGE_ORIGINAL_REPLY"
),
NativeControlOperation::ClaimStop {
ref expected_head_sha256,
service,
} => {
let claim = reply
.claim
.as_ref()
.context("UPDATE_BRIDGE_STOP_CLAIM_MISSING")?;
ensure!(
context.generation == Generation::Original
&& journal.phase() == Phase::StopIntent
&& claim.effect() == &NativeEffect::StopService { service }
&& claim.grant_id() == request.grant_id
&& claim.owner_nonce() == request.owner_nonce
&& claim.head_sha256() == expected_head_sha256
&& proof.head_sha256 == *expected_head_sha256
&& original_start.is_none(),
"UPDATE_BRIDGE_STOP_CLAIM_CHANGED"
);
}
NativeControlOperation::ObserveStarts { .. } => ensure!(
context.generation == Generation::Replacement
&& journal.phase() == Phase::ServicesStarted
&& reply.claim.is_none()
&& original_start.is_none(),
"UPDATE_BRIDGE_STARTS_REPLY"
),
NativeControlOperation::Health { .. } => ensure!(
context.generation == Generation::Replacement
&& journal.phase() == Phase::HealthVerified
&& reply.claim.is_none()
&& original_start.is_none(),
"UPDATE_BRIDGE_HEALTH_REPLY"
),
NativeControlOperation::Finalize => unreachable!(),
}
self.prefix = Some(proof.clone());
}
context.budget.check()?;
if matches!(request.method, NativeControlOperation::Finalize) {
self.finalized = true;
}
if mutating {
self.poisoned = false;
}
Ok(Reply {
proof: reply.proof,
claim: reply.claim,
original_start,
original_outcome,
})
}
pub fn head_sha256(&self) -> Result<&str> {
self.prefix
.as_ref()
.map(|p| p.head_sha256.as_str())
.context("UPDATE_BRIDGE_ORIGINAL_READ_REQUIRED")
}
pub fn observe_starts(
&mut self,
held: &[rmm_updater_linux::services::HeldService],
) -> Result<Reply> {
ensure!(
held.len() == 3
&& held.iter().map(|s| s.proof().service).eq([
Service::Agent,
Service::General,
Service::Full
]),
"UPDATE_BRIDGE_STARTED_SET"
);
let services = held
.iter()
.map(|s| s.started_proof(self.context.budget))
.collect::<Result<Vec<_>>>()?;
let proofs = held.iter().map(|s| s.proof().clone()).collect::<Vec<_>>();
let observation_sha256 = rmm_domain::jobs::hash(&serde_json::to_vec(&proofs)?);
let reply = self.call(NativeControlOperation::ObserveStarts {
services: services.clone(),
observation_sha256: observation_sha256.clone(),
})?;
for service in held {
service.verify(self.context.budget)?;
}
let proof = reply
.proof
.as_ref()
.context("UPDATE_BRIDGE_STARTED_PROOF_MISSING")?;
let raw = proof.decode().map_err(anyhow::Error::msg)?;
let last: rmm_updater_handoff::Event = serde_json::from_slice(
raw.events
.last()
.context("UPDATE_BRIDGE_STARTED_EVENT_MISSING")?,
)?;
ensure!(
matches!(last.evidence,rmm_updater_handoff::Evidence::ServicesStarted {services:actual,observation_sha256:hash,..}
if actual==services && hash==observation_sha256),
"UPDATE_BRIDGE_STARTED_RESULT_CHANGED"
);
Ok(reply)
}
}
fn decode(value: &str, max: usize) -> Result<Vec<u8>> {
ensure!(
!value.is_empty() && value.len() <= 4 * max.div_ceil(3),
"UPDATE_BRIDGE_BASE64_LIMIT"
);
let raw = URL_SAFE_NO_PAD.decode(value)?;
ensure!(
!raw.is_empty() && raw.len() <= max && URL_SAFE_NO_PAD.encode(&raw) == value,
"UPDATE_BRIDGE_NONCANONICAL_BASE64"
);
Ok(raw)
}
struct IoBudget<'a> {
clock: &'a NativeClock,
budget: &'a Budget,
finish_before: u64,
until: Instant,
}
impl IoBudget<'_> {
fn remaining(&self) -> Result<Duration> {
self.budget.check()?;
let now = self.clock.now()?;
let finish = chrono::DateTime::from_timestamp(i64::try_from(self.finish_before)?, 0)
.context("UPDATE_BRIDGE_FINISH")?;
let original = (finish - now).to_std()?;
let frame = self.until.saturating_duration_since(Instant::now());
let remaining = original.min(frame);
ensure!(!remaining.is_zero(), "UPDATE_BRIDGE_DEADLINE");
Ok(remaining)
}
}
struct SocketIo<'a> {
stream: UnixStream,
budget: IoBudget<'a>,
}
impl Read for SocketIo<'_> {
fn read(&mut self, bytes: &mut [u8]) -> std::io::Result<usize> {
self.stream.set_read_timeout(Some(
self.budget.remaining().map_err(std::io::Error::other)?,
))?;
self.stream.read(bytes)
}
}
impl Write for SocketIo<'_> {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.stream.set_write_timeout(Some(
self.budget.remaining().map_err(std::io::Error::other)?,
))?;
self.stream.write(bytes)
}
fn flush(&mut self) -> std::io::Result<()> {
self.budget.remaining().map_err(std::io::Error::other)?;
self.stream.flush()
}
}
fn connect(budget: &IoBudget<'_>) -> Result<UnixStream> {
// A blocking Unix connect can wait forever on a full listener backlog.
// Use only the fixed path and an owned nonblocking socket, including during
// connect; never switch to a network endpoint or another helper.
let fd = unsafe {
libc::socket(
libc::AF_UNIX,
libc::SOCK_STREAM | libc::SOCK_CLOEXEC | libc::SOCK_NONBLOCK,
0,
)
};
ensure!(fd >= 0, "UPDATE_BRIDGE_SOCKET_CREATE");
let stream = unsafe { UnixStream::from_raw_fd(fd) };
let mut address: libc::sockaddr_un = unsafe { std::mem::zeroed() };
address.sun_family = libc::AF_UNIX as libc::sa_family_t;
ensure!(
SOCKET.len() < address.sun_path.len(),
"UPDATE_BRIDGE_FIXED_PATH_LIMIT"
);
for (slot, byte) in address.sun_path.iter_mut().zip(SOCKET.bytes()) {
*slot = byte as libc::c_char;
}
let result = unsafe {
libc::connect(
fd,
(&address as *const libc::sockaddr_un).cast(),
std::mem::size_of::<libc::sockaddr_un>() as libc::socklen_t,
)
};
if result < 0 {
let error = std::io::Error::last_os_error();
ensure!(
error.raw_os_error() == Some(libc::EINPROGRESS),
"UPDATE_BRIDGE_CONNECT: {error}"
);
loop {
let remaining = budget.remaining()?;
let mut poll = libc::pollfd {
fd: stream.as_raw_fd(),
events: libc::POLLOUT,
revents: 0,
};
let result = unsafe {
libc::poll(
&mut poll,
1,
i32::try_from(remaining.as_millis().min(200))?.max(1),
)
};
if result < 0 {
let error = std::io::Error::last_os_error();
ensure!(
error.kind() == std::io::ErrorKind::Interrupted,
"UPDATE_BRIDGE_CONNECT_POLL: {error}"
);
continue;
}
if result == 0 {
continue;
}
let mut error: libc::c_int = 0;
let mut length = std::mem::size_of_val(&error) as libc::socklen_t;
ensure!(
unsafe {
libc::getsockopt(
fd,
libc::SOL_SOCKET,
libc::SO_ERROR,
(&mut error as *mut libc::c_int).cast(),
&mut length,
)
} == 0
&& error == 0
&& poll.revents == libc::POLLOUT,
"UPDATE_BRIDGE_CONNECT_FAILED"
);
break;
}
}
budget.remaining()?;
stream.set_nonblocking(false)?;
Ok(stream)
}
+247
View File
@@ -0,0 +1,247 @@
//! One create-only protected handoff witness bundle BEFORE consumption. Its
//! inner raw hashes are bound by the ORIGINAL atomic helper handoff. Reading
//! cannot create a plan, authority, lease, resume or new effect.
use crate::{files::FileWitnesses, retained::RetainedSet};
use anyhow::{Result, ensure};
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use rmm_domain::agent_update::{ParticipantIdentity, ParticipantRole, UpdateDelegation};
use rmm_updater_handoff::Handoff;
use rmm_updater_linux::files::{Directory, LedgerIdentity};
use rmm_updater_linux::{
Budget,
services::{HeldService, Service},
};
use serde::{Deserialize, Serialize};
use std::{
ffi::OsStr,
fs::{self, OpenOptions},
io::Write,
os::unix::fs::{DirBuilderExt, OpenOptionsExt},
path::{Path, PathBuf},
};
use uuid::Uuid;
const ROOT: &str = "/var/lib/rmm-agent-updater/handoffs";
const MAX_BYTES: usize = 256 * 1024;
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Record {
schema_version: u32,
grant_id: Uuid,
owner_nonce: Uuid,
handoff_sha256: String,
handoff_record_base64url: String,
delegation: UpdateDelegation,
files_base64url: String,
retained_base64url: String,
ledger: LedgerIdentity,
}
pub struct Loaded {
pub header: Header,
pub files: FileWitnesses,
pub retained: RetainedSet,
pub ledger: LedgerIdentity,
}
pub struct Header {
pub handoff: Handoff,
pub delegation: UpdateDelegation,
}
pub struct Participants<'a> {
pub delegation: &'a UpdateDelegation,
pub original_full: &'a HeldService,
pub updater: &'a HeldService,
pub budget: &'a Budget,
}
fn path(handoff: &Handoff) -> PathBuf {
Path::new(ROOT).join(handoff.attempt.grant_id.to_string())
}
pub fn write_before_handoff(
handoff: &Handoff,
files: &FileWitnesses,
retained: &RetainedSet,
ledger: &LedgerIdentity,
participants: Participants<'_>,
) -> Result<()> {
handoff.validate()?;
participants.budget.check()?;
participants.original_full.verify(participants.budget)?;
participants.updater.verify(participants.budget)?;
ensure!(
participants.original_full.proof().service == Service::Full
&& participants.updater.proof().service == Service::Updater
&& observed_participant(participants.original_full)?
== participants.delegation.original_helper
&& observed_participant(participants.updater)? == participants.delegation.updater,
"UPDATE_BUNDLE_PARTICIPANTS_CHANGED"
);
validate_delegation(handoff, participants.delegation)?;
files.bind(handoff)?;
retained.verify()?;
ensure!(
retained.sha256() == handoff.retained_state_sha256
&& ledger.sha256()? == handoff.ledger_identity_sha256,
"UPDATE_BUNDLE_BINDING"
);
let parent = Directory::open(Path::new(ROOT), 0, true)?;
let raw = serde_json::to_vec(&Record {
schema_version: 1,
grant_id: handoff.attempt.grant_id,
owner_nonce: handoff.owner_nonce,
handoff_sha256: rmm_domain::jobs::hash(&handoff.bytes()?),
handoff_record_base64url: URL_SAFE_NO_PAD.encode(handoff.bytes()?),
delegation: participants.delegation.clone(),
files_base64url: URL_SAFE_NO_PAD.encode(files.bytes()?),
retained_base64url: URL_SAFE_NO_PAD.encode(retained.bytes()?),
ledger: ledger.clone(),
})?;
ensure!(raw.len() <= MAX_BYTES, "UPDATE_BUNDLE_LIMIT");
// create_dir is exclusive; partial/previous bundles are never reused.
fs::DirBuilder::new().mode(0o700).create(path(handoff))?;
parent.verify()?;
let directory = Directory::open(&path(handoff), 0, true)?;
let mut file = OpenOptions::new()
.create_new(true)
.write(true)
.mode(0o600)
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
.open(path(handoff).join("witnesses.json"))?;
file.write_all(&raw)?;
file.sync_all()?;
let current = directory.inspect(OsStr::new("witnesses.json"), 0, MAX_BYTES as u64)?;
ensure!(
current.proof().sha256 == rmm_domain::jobs::hash(&raw)
&& current.proof().identity.links == 1,
"UPDATE_BUNDLE_WRITE_CHANGED"
);
sync_directory(&path(handoff))?;
sync_directory(Path::new(ROOT))?;
directory.verify()?;
parent.verify()
}
pub fn read_for_handoff(handoff: &Handoff) -> Result<Loaded> {
handoff.validate()?;
let record = read_record(handoff.attempt.grant_id)?;
let header = header(&record)?;
ensure!(
header.handoff.bytes()? == handoff.bytes()?,
"UPDATE_BUNDLE_ORIGINAL_HEADER_CHANGED"
);
let files = FileWitnesses::from_bound_bytes(&decode(&record.files_base64url)?, handoff)?;
let retained = RetainedSet::from_bound_bytes(&decode(&record.retained_base64url)?, handoff)?;
Ok(Loaded {
header,
files,
retained,
ledger: record.ledger,
})
}
pub fn read_header(grant_id: Uuid, owner_nonce: Uuid, handoff_sha256: &str) -> Result<Header> {
ensure!(
!grant_id.is_nil()
&& !owner_nonce.is_nil()
&& rmm_domain::agent_update::digest(handoff_sha256),
"UPDATE_BUNDLE_HEADER_SELECTOR"
);
let record = read_record(grant_id)?;
ensure!(
record.owner_nonce == owner_nonce && record.handoff_sha256 == handoff_sha256,
"UPDATE_BUNDLE_HEADER_BINDING"
);
header(&record)
}
fn read_record(grant_id: Uuid) -> Result<Record> {
ensure!(!grant_id.is_nil(), "UPDATE_BUNDLE_GRANT");
let parent = Directory::open(Path::new(ROOT), 0, true)?;
let directory = Directory::open(&Path::new(ROOT).join(grant_id.to_string()), 0, true)?;
let mut file = directory.inspect(OsStr::new("witnesses.json"), 0, MAX_BYTES as u64)?;
ensure!(
file.proof().identity.mode & 0o777 == 0o600 && file.proof().identity.links == 1,
"UPDATE_BUNDLE_PRIVATE_FILE"
);
let raw = file.bytes(MAX_BYTES as u64)?;
let record: Record = serde_json::from_slice(&raw)?;
ensure!(
serde_json::to_vec(&record)? == raw
&& record.schema_version == 1
&& record.grant_id == grant_id,
"UPDATE_BUNDLE_RECORD_BINDING"
);
directory.file(OsStr::new("witnesses.json"), file.proof(), MAX_BYTES as u64)?;
parent.verify()?;
Ok(record)
}
fn header(record: &Record) -> Result<Header> {
let raw = decode(&record.handoff_record_base64url)?;
let handoff: Handoff = serde_json::from_slice(&raw)?;
ensure!(
handoff.bytes()? == raw
&& rmm_domain::jobs::hash(&raw) == record.handoff_sha256
&& handoff.attempt.grant_id == record.grant_id
&& handoff.owner_nonce == record.owner_nonce
&& record.ledger.sha256()? == handoff.ledger_identity_sha256,
"UPDATE_BUNDLE_HEADER_CHANGED"
);
validate_delegation(&handoff, &record.delegation)?;
ensure!(
rmm_domain::jobs::hash(&decode(&record.files_base64url)?) == handoff.staged_files_sha256
&& rmm_domain::jobs::hash(&decode(&record.retained_base64url)?)
== handoff.retained_state_sha256,
"UPDATE_BUNDLE_INNER_HASH_CHANGED"
);
Ok(Header {
handoff,
delegation: record.delegation.clone(),
})
}
fn validate_delegation(handoff: &Handoff, delegation: &UpdateDelegation) -> Result<()> {
ensure!(
delegation.validate()
&& delegation.handoff_sha256 == rmm_domain::jobs::hash(&handoff.bytes()?)
&& delegation.boot_id == handoff.boot_id
&& delegation.owner_nonce == handoff.owner_nonce
&& delegation.updater.role == ParticipantRole::LinuxUpdater
&& delegation.original_helper.role == ParticipantRole::LinuxFullHelper
&& delegation.updater.image_sha256 == handoff.updater_sha256
&& delegation.original_helper.image_sha256 == handoff.predecessor.helper_sha256
&& u64::try_from(delegation.prepared_at.timestamp())? == handoff.consumed_at,
"UPDATE_BUNDLE_DELEGATION_BINDING"
);
Ok(())
}
fn observed_participant(held: &HeldService) -> Result<ParticipantIdentity> {
let proof = held.proof();
let role = match proof.service {
Service::Updater => ParticipantRole::LinuxUpdater,
Service::Full => ParticipantRole::LinuxFullHelper,
_ => anyhow::bail!("UPDATE_BUNDLE_PARTICIPANT_ROLE"),
};
ensure!(proof.process.uid == 0, "UPDATE_BUNDLE_ROOT_PARTICIPANT");
Ok(ParticipantIdentity {
role,
pid: proof.process.pid,
birth: proof.process.birth,
image_sha256: proof.process.image_sha256.clone(),
security_principal: "uid:0".into(),
service_instance_sha256: rmm_domain::jobs::hash(&serde_json::to_vec(proof)?),
})
}
fn decode(value: &str) -> Result<Vec<u8>> {
ensure!(
!value.is_empty() && value.len() <= 4 * 65536usize.div_ceil(3),
"UPDATE_BUNDLE_INNER_LIMIT"
);
let raw = URL_SAFE_NO_PAD.decode(value)?;
ensure!(
!raw.is_empty() && raw.len() <= 65536 && URL_SAFE_NO_PAD.encode(&raw) == value,
"UPDATE_BUNDLE_INNER_BASE64"
);
Ok(raw)
}
fn sync_directory(path: &Path) -> Result<()> {
OpenOptions::new()
.read(true)
.custom_flags(libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC)
.open(path)?
.sync_all()?;
Ok(())
}
+605
View File
@@ -0,0 +1,605 @@
//! One live original-attempt pass. Activation queues only; the fixed listener
//! must finish its reply before this runtime can stop the calling Fullhelper.
//! No daemon entrypoint, admission/capability registration or restart recovery.
use crate::{
agent_startup::BoundAgentLock,
bridge::{BridgeContext, FullBridge, Generation},
bundle,
clock::NativeClock,
fence::LedgerFence,
ledger::{OwnedInputs, OwnedLedger},
original::Original,
participants::{StableParticipant, verify_original_helper},
retained::RetainedSet,
returned,
trust::TrustState,
};
use anyhow::{Context, Result, ensure};
use rmm_agent_core::{
auth::Binding,
reconcile::SyncClock,
update_handoff::{NativeControlOperation, NativeEffect, NativeEffectClaim, NativeService},
};
use rmm_domain::{
agent_update::{ParticipantIdentity, ParticipantRole, UpdateInflightProof},
agent_update_provisioning::{HealthKeyPin, InstalledReleaseRecord, LocalUpdatePolicy},
jobs::hash,
};
use rmm_updater_handoff::{Evidence, Phase, Process};
use rmm_updater_linux::{
Budget, DurableEffectFence, Effect,
files::{Directory, HeldFile, LedgerGuard, Role},
ipc::{self, Request, Response, State},
services::{Service, ServicePolicy, ServiceProof, Systemd},
};
use std::{ffi::OsStr, path::Path, time::Duration};
use uuid::Uuid;
struct Protected {
directory: Directory,
file: HeldFile,
name: String,
max: u64,
}
impl Protected {
fn read(path: &str, max: u64) -> Result<(Self, Vec<u8>)> {
let path = Path::new(path);
let directory = Directory::open(path.parent().context("UPDATE_ENGINE_PARENT")?, 0, false)?;
let name = path
.file_name()
.context("UPDATE_ENGINE_NAME")?
.to_str()
.context("UPDATE_ENGINE_NAME")?
.to_owned();
let mut file = directory.inspect(OsStr::new(&name), 0, max)?;
let raw = file.bytes(max)?;
Ok((
Self {
directory,
file,
name,
max,
},
raw,
))
}
fn verify(&self) -> Result<()> {
self.directory
.file(OsStr::new(&self.name), self.file.proof(), self.max)?;
Ok(())
}
}
/// Construct only in the separately provisioned stable service, with the
/// independently protected original job Binding. It offers no approval API.
pub struct Runtime {
binding: Binding,
policy: LocalUpdatePolicy,
policy_sha256: String,
installed: InstalledReleaseRecord,
installed_sha256: String,
health_key: [u8; 32],
health_pin_sha256: String,
protected: Vec<Protected>,
old_policies: Vec<ServicePolicy>,
updater: StableParticipant,
clock: NativeClock,
queued: Option<Queued>,
accepted: bool,
failed: bool,
terminal: bool,
head: Option<String>,
}
struct Queued {
agent_startup: BoundAgentLock,
original: Original,
witnesses: bundle::Loaded,
trust: TrustState,
budget: Budget,
proof: UpdateInflightProof,
}
impl Runtime {
pub fn new(binding: Binding) -> Result<Self> {
let clock = NativeClock::new()?;
let budget = observation_budget()?;
let (policy_file, raw) = Protected::read("/etc/rmm-agent-updater/policy.json", 65536)?;
let (policy, policy_sha256) =
LocalUpdatePolicy::from_bytes(&raw).map_err(anyhow::Error::msg)?;
ensure!(
policy.tenant_id == binding.tenant_id
&& policy.asset_id == binding.asset_id
&& policy.agent_id == binding.agent_id
&& policy.enrollment_generation == binding.enrollment_generation
&& policy.assignment_version == binding.assignment_version,
"UPDATE_ENGINE_LOCAL_IDENTITY"
);
let (record_file, raw) = Protected::read(&policy.installed_record_path, 32768)?;
let installed = InstalledReleaseRecord::from_bytes(&raw).map_err(anyhow::Error::msg)?;
let installed_sha256 = hash(&raw);
let old_policies = service_policies(&policy, &installed, None)?;
let (health_file, raw) = Protected::read(&policy.health_pin_path, 4096)?;
let health: HealthKeyPin = serde_json::from_slice(&raw)?;
let health_key = health.public_key().map_err(anyhow::Error::msg)?;
let updater = StableParticipant::observe(
ServicePolicy {
service: Service::Updater,
image_sha256: policy.updater_binary_sha256.clone(),
unit_sha256: policy.updater_service.unit_sha256.clone(),
},
&budget,
)?;
Ok(Self {
binding,
policy,
policy_sha256,
installed,
installed_sha256,
health_key,
health_pin_sha256: hash(&raw),
protected: vec![policy_file, record_file, health_file],
old_policies,
updater,
clock,
queued: None,
accepted: false,
failed: false,
terminal: false,
head: None,
})
}
/// A lost activation reply disables this live runtime before any stop.
/// Drive is deliberately private: it cannot run inside Engine::handle.
pub fn serve_one(&mut self, listener: &ipc::Listener) -> Result<()> {
self.require_reader_registry_and_census()?;
ensure!(!self.failed && !self.terminal, "UPDATE_ENGINE_CLOSED");
let services = Systemd::new(self.old_policies.clone())?;
let budget = observation_budget()?;
if let Err(error) = listener.serve_one(&services, &budget, self) {
if self.queued.take().is_some() {
self.failed = true;
}
return Err(error);
}
if let Some(queued) = self.queued.take() {
// serve_one has written/flushed the bounded reply and dropped its
// accepted stream. Only now may the original Full process stop.
match self.drive(queued) {
Ok(()) => self.terminal = true,
Err(error) => {
self.failed = true;
return Err(error);
}
}
}
Ok(())
}
fn require_reader_registry_and_census(&self) -> Result<()> {
// This is an explicit source-checkpoint barrier, not a caller boolean
// or a known-unit-list substitute. Replace only with the real protected
// bootstrap registry + complete live FD/maps census and both locks.
anyhow::bail!("UPDATE_READER_CENSUS_NOT_CONFIGURED")
}
fn enqueue(
&mut self,
peer: &ParticipantIdentity,
grant: Uuid,
nonce: Uuid,
handoff_sha: &str,
) -> Result<()> {
ensure!(
!self.accepted && self.queued.is_none() && !self.failed && !self.terminal,
"UPDATE_ENGINE_NO_RETRY_OR_RESUME"
);
// Fuse before any fallible original admission. Even an uncertain reply
// cannot dispatch this handoff for a second time in this process.
self.accepted = true;
for file in &self.protected {
file.verify()?;
}
let header = bundle::read_header(grant, nonce, handoff_sha)?;
ensure!(
peer.role == ParticipantRole::LinuxFullHelper
&& *peer == header.delegation.original_helper
&& header.handoff.updater_sha256 == self.policy.updater_binary_sha256
&& header.handoff.updater_policy_sha256 == self.policy_sha256
&& header.handoff.predecessor_record_sha256 == self.installed_sha256,
"UPDATE_ENGINE_ORIGINAL_PARTICIPANTS_OR_POLICY"
);
let finish =
chrono::DateTime::from_timestamp(i64::try_from(header.handoff.finish_before)?, 0)
.context("UPDATE_ENGINE_FINISH")?;
let budget = Budget::new(
header.handoff.boot_id,
(finish - self.clock.now()?).to_std()?,
)?;
let services = Systemd::new(self.old_policies.clone())?;
verify_original_helper(&services, &header.delegation, &budget)?;
self.updater
.verify_delegation(&header.delegation, &budget)?;
let mut bridge = FullBridge::original(BridgeContext {
handoff: &header.handoff,
delegation: &header.delegation,
services: &services,
updater: &self.updater,
clock: &self.clock,
budget: &budget,
health_public_key: self.health_key,
generation: Generation::Original,
})?;
let reply = bridge.call(NativeControlOperation::ReadOriginal)?;
let proof = reply.proof.context("UPDATE_ENGINE_ORIGINAL_PROOF")?;
let original = Original::verify(
&self.binding,
&reply
.original_start
.context("UPDATE_ENGINE_ORIGINAL_START")?,
&header.handoff.bytes()?,
header.delegation,
&self.clock,
&budget,
)?;
let (_, journal) = rmm_domain::agent_update::verify_inflight_proof(&proof, self.health_key)
.map_err(anyhow::Error::msg)?;
ensure!(
journal.phase() == Phase::StopIntent,
"UPDATE_ENGINE_NOT_A_FRESH_HANDOFF"
);
let trust = TrustState::load(&original, &self.binding, &self.clock)?;
ensure!(
trust.health_public_key() == self.health_key
&& original.update().updater.health_key_pin_sha256 == self.health_pin_sha256
&& serde_json::to_vec(trust.policy())? == serde_json::to_vec(&self.policy)?,
"UPDATE_ENGINE_TRUST_CHANGED"
);
let witnesses = bundle::read_for_handoff(original.handoff())?;
witnesses.files.reopen_stages(original.handoff(), &budget)?;
witnesses.retained.verify()?;
let agent_startup = BoundAgentLock::bind(&witnesses.retained)?;
self.head = Some(proof.head_sha256.clone());
self.queued = Some(Queued {
agent_startup,
original,
witnesses,
trust,
budget,
proof,
});
Ok(())
}
fn drive(&mut self, queued: Queued) -> Result<()> {
let Queued {
agent_startup,
original,
witnesses,
trust,
budget,
proof,
} = queued;
original.check_current(&self.binding, &self.clock, &budget)?;
trust.recheck(&original, &self.clock)?;
let services = Systemd::new(self.old_policies.clone())?;
let old = [Service::Agent, Service::General, Service::Full]
.into_iter()
.map(|s| services.observe(s, &budget).map(|h| h.proof().clone()))
.collect::<Result<Vec<_>>>()?;
let mut bridge = FullBridge::new(
BridgeContext {
handoff: original.handoff(),
delegation: original.delegation(),
services: &services,
updater: &self.updater,
clock: &self.clock,
budget: &budget,
health_public_key: self.health_key,
generation: Generation::Original,
},
proof,
)?;
let mut claims = Vec::new();
for service in [
NativeService::Agent,
NativeService::General,
NativeService::Full,
] {
let expected_head_sha256 = bridge.head_sha256()?.to_owned();
claims.push(
bridge
.call(NativeControlOperation::ClaimStop {
expected_head_sha256,
service,
})?
.claim
.context("UPDATE_ENGINE_STOP_CLAIM")?,
);
}
drop(bridge);
let mut stop_fence = StopFence {
original: &original,
binding: &self.binding,
clock: &self.clock,
budget: &budget,
updater: &self.updater,
trust: &trust,
retained: &witnesses.retained,
claims,
old,
next: 0,
pending: false,
poisoned: false,
};
let stopped = services.stop_all(&budget, &mut stop_fence)?;
let claims = stop_fence.finish()?;
let agent_startup = agent_startup.acquire(&stopped, &budget)?;
let os = LedgerGuard::acquire(&stopped, &witnesses.ledger, &budget)?;
let mut ledger = OwnedLedger::open(OwnedInputs {
os: &os,
original: &original,
binding: &self.binding,
clock: &self.clock,
budget: &budget,
updater: &self.updater,
retained: &witnesses.retained,
files: &witnesses.files,
stopped: &stopped,
trust: &trust,
})?;
for (claim, old) in claims.iter().zip(stopped.proofs()) {
ledger.observe_native(claim, &hash(&serde_json::to_vec(&old)?))?;
}
ledger.append_observed(Evidence::AllStopped {
observation_sha256: hash(&serde_json::to_vec(&stopped.proofs())?),
})?;
ledger.append_observed(Evidence::UpdaterOwnsLedger {
process: Process {
pid: self.updater.identity().pid,
birth: self.updater.identity().birth,
image_sha256: self.updater.identity().image_sha256.clone(),
},
ledger_identity_sha256: witnesses.ledger.sha256()?,
})?;
// Do not hold a second connection before all readers really stopped.
let swap = (|| -> Result<()> {
let stages = witnesses.files.reopen_stages(original.handoff(), &budget)?;
let mut fence = LedgerFence::new(&mut ledger)?;
for (stage, role) in
stages
.into_iter()
.zip([Role::Agent, Role::Helper, Role::FullConfig, Role::FullPin])
{
let directory = Directory::open(
Path::new(role.target())
.parent()
.context("UPDATE_ENGINE_TARGET_PARENT")?,
0,
false,
)?;
stage.install(&directory, &os, &budget, &mut fence)?;
}
fence.finish()?;
ledger.append_observed(Evidence::PairVerified {
pair: original.handoff().target.clone(),
retained_state_sha256: original.handoff().retained_state_sha256.clone(),
})
})();
if let Err(error) = swap {
// Exactly one best-effort ORIGINAL Unknown CAS while we still own
// the real guard. Failure is preserved; no restoration or retry.
let _ = ledger.mark_unknown(
u64::try_from(self.clock.now()?.timestamp())?,
rmm_updater_handoff::UnknownReason::ReplaceUncertain,
);
return Err(error);
}
let prepared = ledger.prepare_return_and_close()?;
let reservation = prepared.bind(returned::Inputs {
original: &original,
binding: &self.binding,
clock: &self.clock,
budget: &budget,
updater: &self.updater,
retained: &witnesses.retained,
files: &witnesses.files,
trust: &trust,
})?;
let target_policies = service_policies(&self.policy, &self.installed, Some(&original))?;
let target = Systemd::new(target_policies.clone())?;
let observer = Systemd::new(target_policies)?;
let mut bridge = FullBridge::new(
BridgeContext {
handoff: original.handoff(),
delegation: original.delegation(),
services: &observer,
updater: &self.updater,
clock: &self.clock,
budget: &budget,
health_public_key: self.health_key,
generation: Generation::Replacement,
},
reservation.proof().clone(),
)?;
let mut fence = reservation.start_fence(&observer, &mut bridge);
agent_startup.verify()?;
os.release(&budget, &mut fence)?;
agent_startup.return_after_root_release(&budget)?;
let started = stopped.start_once(target, &budget, &mut fence)?;
fence.finish()?;
bridge.observe_starts(&started)?;
loop {
original.check_current(&self.binding, &self.clock, &budget)?;
self.updater
.verify_delegation(original.delegation(), &budget)?;
witnesses.retained.verify()?;
for service in &started {
service.verify(&budget)?;
}
let reply = bridge.call(NativeControlOperation::ReadOriginal)?;
let proof = reply.proof.context("UPDATE_ENGINE_HEALTH_PROOF")?;
self.head = Some(proof.head_sha256.clone());
let (_, journal) =
rmm_domain::agent_update::verify_inflight_proof(&proof, self.health_key)
.map_err(anyhow::Error::msg)?;
match journal.phase() {
Phase::ServicesStarted => std::thread::sleep(Duration::from_millis(100)),
Phase::HealthVerified => {
bridge.call(NativeControlOperation::Finalize)?;
self.head = None;
return Ok(());
}
_ => anyhow::bail!("UPDATE_ENGINE_HEALTH_PREFIX_CHANGED"),
}
}
}
}
impl ipc::Engine for Runtime {
fn handle(&mut self, peer: &ParticipantIdentity, request: Request) -> Result<Response> {
ensure!(!self.failed && !self.terminal, "UPDATE_ENGINE_CLOSED");
self.updater
.identity()
.validate()
.then_some(())
.context("UPDATE_ENGINE_IDENTITY")?;
let state = match request {
Request::Describe {} => State::Described,
Request::Activate {
grant_id,
owner_nonce,
handoff_sha256,
} => {
self.enqueue(peer, grant_id, owner_nonce, &handoff_sha256)?;
State::Inflight
}
// Health travels through the existing independently validating
// new Fullhelper path. This daemon accepts no duplicate authority.
_ => anyhow::bail!("UPDATE_ENGINE_UNSUPPORTED_CONTROL"),
};
Ok(Response {
schema_version: 1,
state,
ready: false,
updater: self.updater.identity().clone(),
head_sha256: self.head.clone(),
})
}
}
struct StopFence<'a> {
original: &'a Original,
binding: &'a Binding,
clock: &'a NativeClock,
budget: &'a Budget,
updater: &'a StableParticipant,
trust: &'a TrustState,
retained: &'a RetainedSet,
claims: Vec<NativeEffectClaim>,
old: Vec<ServiceProof>,
next: usize,
pending: bool,
poisoned: bool,
}
impl StopFence<'_> {
fn current(&self) -> Result<()> {
self.original
.check_current(self.binding, self.clock, self.budget)?;
self.updater
.verify_delegation(self.original.delegation(), self.budget)?;
self.trust.recheck(self.original, self.clock)?;
self.retained.verify()
}
fn finish(self) -> Result<Vec<NativeEffectClaim>> {
ensure!(
!self.poisoned && !self.pending && self.next == 3,
"UPDATE_ENGINE_STOP_INCOMPLETE"
);
self.current()?;
Ok(self.claims)
}
}
impl DurableEffectFence for StopFence<'_> {
fn before_effect(&mut self, effect: &Effect) -> Result<()> {
ensure!(
!self.poisoned && !self.pending && self.next < 3,
"UPDATE_ENGINE_STOP_NO_RETRY"
);
self.poisoned = true;
self.current()?;
let service = [Service::Agent, Service::General, Service::Full][self.next];
let native = [
NativeService::Agent,
NativeService::General,
NativeService::Full,
][self.next];
let claim = &self.claims[self.next];
ensure!(
*effect == Effect::StopService { service }
&& claim.effect() == &NativeEffect::StopService { service: native }
&& claim.grant_id() == self.original.permit().grant_id
&& claim.owner_nonce() == self.original.handoff().owner_nonce,
"UPDATE_ENGINE_STOP_RESERVATION_CHANGED"
);
self.pending = true;
Ok(())
}
fn after_effect(&mut self, effect: &Effect, observation: &str) -> Result<()> {
ensure!(
self.poisoned
&& self.pending
&& self.next < 3
&& *effect
== Effect::StopService {
service: self.old[self.next].service
}
&& observation == hash(&serde_json::to_vec(&self.old[self.next])?),
"UPDATE_ENGINE_STOP_OBSERVATION_CHANGED"
);
self.current()?;
self.next += 1;
self.pending = false;
self.poisoned = false;
Ok(())
}
}
fn observation_budget() -> Result<Budget> {
Budget::new(
std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
.trim()
.parse()?,
Duration::from_secs(5),
)
}
fn service_policies(
policy: &LocalUpdatePolicy,
installed: &InstalledReleaseRecord,
original: Option<&Original>,
) -> Result<Vec<ServicePolicy>> {
let release = installed
.release_envelope
.decode()
.map_err(anyhow::Error::msg)?;
policy
.services
.iter()
.zip([
Service::Agent,
Service::General,
Service::Full,
Service::Apt,
])
.map(|(pin, service)| {
ensure!(pin.unit == service.unit(), "UPDATE_ENGINE_SERVICE_SET");
let image_sha256 = match (service, original) {
(Service::Agent, Some(o)) => o.handoff().target.agent_sha256.clone(),
(Service::General | Service::Full | Service::Apt, Some(o)) => {
o.handoff().target.helper_sha256.clone()
}
(Service::Agent, None) => release.manifest.artifacts[0].sha256.clone(),
(_, None) => release.manifest.artifacts[1].sha256.clone(),
_ => anyhow::bail!("UPDATE_ENGINE_SERVICE_ROLE"),
};
Ok(ServicePolicy {
service,
image_sha256,
unit_sha256: pin.unit_sha256.clone(),
})
})
.collect()
}
+1
View File
@@ -22,6 +22,7 @@ pub struct LedgerFence<'w, 'a, 's> {
} }
impl<'w, 'a, 's> LedgerFence<'w, 'a, 's> { impl<'w, 'a, 's> LedgerFence<'w, 'a, 's> {
pub fn new(ledger: &'w mut OwnedLedger<'a, 's>) -> Result<Self> { pub fn new(ledger: &'w mut OwnedLedger<'a, 's>) -> Result<Self> {
ledger.qualify_for_effects()?;
ensure!( ensure!(
ledger.journal()?.phase() == Phase::UpdaterOwnsLedger, ledger.journal()?.phase() == Phase::UpdaterOwnsLedger,
"UPDATE_NO_FENCE_RESUME" "UPDATE_NO_FENCE_RESUME"
+100 -4
View File
@@ -5,9 +5,9 @@ use rmm_domain::jobs::hash;
use rmm_updater_handoff::{FileRole, Handoff}; use rmm_updater_handoff::{FileRole, Handoff};
use rmm_updater_linux::{ use rmm_updater_linux::{
Budget, Budget,
files::{Directory, FileProof, Role, Staged}, files::{BoundStage, Directory, FileProof, Role, Staged},
}; };
use serde::Serialize; use serde::{Deserialize, Serialize};
use std::{ffi::OsStr, path::Path}; use std::{ffi::OsStr, path::Path};
use uuid::Uuid; use uuid::Uuid;
@@ -29,12 +29,20 @@ fn role(role: FileRole) -> Result<Role> {
_ => anyhow::bail!("UPDATE_LINUX_FILE_ROLE"), _ => anyhow::bail!("UPDATE_LINUX_FILE_ROLE"),
} }
} }
#[derive(Serialize)] #[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Proof { struct Proof {
role: Role, role: Role,
original: FileProof, original: FileProof,
staged: FileProof, staged: FileProof,
} }
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Record {
schema_version: u32,
owner_nonce: Uuid,
files: Vec<Proof>,
}
pub struct FileWitnesses { pub struct FileWitnesses {
files: Vec<Proof>, files: Vec<Proof>,
nonce: Uuid, nonce: Uuid,
@@ -63,6 +71,7 @@ impl FileWitnesses {
)?; )?;
ensure!( ensure!(
stage.proof().identity.links == 1 stage.proof().identity.links == 1
&& stage.proof().identity.device == original.proof().identity.device
&& stage.proof().identity.uid == original.proof().identity.uid && stage.proof().identity.uid == original.proof().identity.uid
&& stage.proof().identity.gid == original.proof().identity.gid && stage.proof().identity.gid == original.proof().identity.gid
&& stage.proof().identity.mode == original.proof().identity.mode, && stage.proof().identity.mode == original.proof().identity.mode,
@@ -74,7 +83,11 @@ impl FileWitnesses {
staged: stage.proof().clone(), staged: stage.proof().clone(),
}); });
} }
let digest = hash(&serde_json::to_vec(&files)?); let digest = hash(&serde_json::to_vec(&Record {
schema_version: 1,
owner_nonce: nonce,
files: files.clone(),
})?);
Ok(Self { Ok(Self {
files, files,
nonce, nonce,
@@ -84,6 +97,65 @@ impl FileWitnesses {
pub fn sha256(&self) -> &str { pub fn sha256(&self) -> &str {
&self.sha256 &self.sha256
} }
pub fn bytes(&self) -> Result<Vec<u8>> {
let bytes = serde_json::to_vec(&Record {
schema_version: 1,
owner_nonce: self.nonce,
files: self.files.clone(),
})?;
ensure!(
bytes.len() <= 65536 && hash(&bytes) == self.sha256,
"UPDATE_FILE_WITNESS_CHANGED"
);
Ok(bytes)
}
/// Protected bundle bytes remain observations only. Actual phase-specific
/// path/FD/hash verification is separate and mandatory before any effect.
pub fn from_bound_bytes(raw: &[u8], handoff: &Handoff) -> Result<Self> {
ensure!(
!raw.is_empty() && raw.len() <= 65536 && hash(raw) == handoff.staged_files_sha256,
"UPDATE_FILE_WITNESS_HASH"
);
let record: Record = serde_json::from_slice(raw)?;
ensure!(
record.schema_version == 1
&& record.owner_nonce == handoff.owner_nonce
&& record.files.len() == 4
&& serde_json::to_vec(&record)? == raw,
"UPDATE_FILE_WITNESS_CODEC"
);
for (role, file) in ROLES.iter().zip(&record.files) {
ensure!(*role == file.role, "UPDATE_FILE_WITNESS_ROLE_ORDER");
for proof in [&file.original, &file.staged] {
ensure!(
proof.identity.inode > 0
&& proof.identity.uid == 0
&& proof.identity.links > 0
&& proof.identity.mode & libc::S_IFMT == libc::S_IFREG
&& proof.identity.mode & 0o022 == 0
&& proof.bytes > 0
&& proof.bytes <= 256 * 1024 * 1024
&& rmm_domain::agent_update::digest(&proof.sha256),
"UPDATE_FILE_WITNESS_SHAPE"
);
}
ensure!(
file.staged.identity.links == 1
&& file.staged.identity.device == file.original.identity.device
&& file.original.identity.uid == file.staged.identity.uid
&& file.original.identity.gid == file.staged.identity.gid
&& file.original.identity.mode == file.staged.identity.mode,
"UPDATE_FILE_WITNESS_METADATA"
);
}
let result = Self {
files: record.files,
nonce: record.owner_nonce,
sha256: hash(raw),
};
result.bind(handoff)?;
Ok(result)
}
pub fn bind(&self, handoff: &Handoff) -> Result<()> { pub fn bind(&self, handoff: &Handoff) -> Result<()> {
ensure!( ensure!(
handoff.files.len() == 4 handoff.files.len() == 4
@@ -101,6 +173,30 @@ impl FileWitnesses {
} }
Ok(()) Ok(())
} }
/// Reopen only the exact preconsumption protected stages. This is not a
/// dispatch or restart path: the live original owner and each Core claim
/// remain independently mandatory before Stop/Swap/Start.
pub fn reopen_stages(&self, handoff: &Handoff, budget: &Budget) -> Result<[Staged; 4]> {
self.bind(handoff)?;
let mut opened = Vec::new();
for proof in &self.files {
let path = Path::new(proof.role.target());
let dir = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
opened.push(Staged::reopen_bound(
&dir,
BoundStage {
role: proof.role,
nonce: self.nonce,
original: &proof.original,
staged: &proof.staged,
},
budget,
)?);
}
opened
.try_into()
.map_err(|_| anyhow::anyhow!("UPDATE_STAGE_COUNT"))
}
pub fn verify_original_and_stage(&self, which: FileRole, budget: &Budget) -> Result<()> { pub fn verify_original_and_stage(&self, which: FileRole, budget: &Budget) -> Result<()> {
let file = self.file(which)?; let file = self.file(which)?;
budget.check()?; budget.check()?;
File diff suppressed because it is too large Load Diff
+157 -9
View File
@@ -3,7 +3,7 @@
//! consumption, schema reset, outcome healing or readiness transition. //! consumption, schema reset, outcome healing or readiness transition.
use crate::{ use crate::{
clock::NativeClock, files::FileWitnesses, original::Original, participants::StableParticipant, clock::NativeClock, files::FileWitnesses, original::Original, participants::StableParticipant,
retained::RetainedSet, trust::TrustState, retained::RetainedSet, returned::PreparedReservation, trust::TrustState,
}; };
use anyhow::{Result, ensure}; use anyhow::{Result, ensure};
use rmm_agent_core::{ use rmm_agent_core::{
@@ -22,8 +22,22 @@ use rmm_updater_linux::services::StoppedServices;
use rmm_updater_linux::{Budget, files::LedgerGuard}; use rmm_updater_linux::{Budget, files::LedgerGuard};
use rusqlite::{Connection, OpenFlags}; use rusqlite::{Connection, OpenFlags};
use std::time::Duration; use std::time::Duration;
fn observation_budget() -> Result<Budget> {
let boot = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
.trim()
.parse()?;
Budget::new(boot, Duration::from_secs(5))
}
const LEDGER: &str = "/var/lib/rmm-helper/consumption.sqlite"; const LEDGER: &str = "/var/lib/rmm-helper/consumption.sqlite";
fn file_role(role: NativeFileRole) -> rmm_updater_handoff::FileRole {
match role {
NativeFileRole::AgentBinary => rmm_updater_handoff::FileRole::AgentBinary,
NativeFileRole::HelperBinary => rmm_updater_handoff::FileRole::HelperBinary,
NativeFileRole::FullConfig => rmm_updater_handoff::FileRole::LinuxFullConfig,
NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin,
}
}
pub struct OwnedGuard<'a, 'stopped> { pub struct OwnedGuard<'a, 'stopped> {
os: &'a LedgerGuard<'stopped>, os: &'a LedgerGuard<'stopped>,
@@ -56,7 +70,7 @@ pub struct OwnedLedger<'a, 'stopped> {
} }
impl<'a, 'stopped> OwnedLedger<'a, 'stopped> { impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
pub fn open(input: OwnedInputs<'a, 'stopped>) -> Result<Self> { pub fn open(input: OwnedInputs<'a, 'stopped>) -> Result<Self> {
input.os.verify_for_effect(input.budget)?; input.os.verify_for_effect(&observation_budget()?)?;
ensure!( ensure!(
input.os.identity().sha256()? == input.original.handoff().ledger_identity_sha256 input.os.identity().sha256()? == input.original.handoff().ledger_identity_sha256
&& input.retained.sha256() == input.original.handoff().retained_state_sha256, && input.retained.sha256() == input.original.handoff().retained_state_sha256,
@@ -93,12 +107,45 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
// Replay the complete original retained chain and its durable head. // Replay the complete original retained chain and its durable head.
update_handoff::read(&db, &guard, input.original.permit().grant_id)?; update_handoff::read(&db, &guard, input.original.permit().grant_id)?;
db.execute_batch("PRAGMA foreign_keys=ON")?; db.execute_batch("PRAGMA foreign_keys=ON")?;
ensure!(
db.query_row("PRAGMA foreign_keys", [], |r| r.get::<_, bool>(0))?,
"UPDATE_LEDGER_FOREIGN_KEYS_REQUIRED"
);
rmm_agent_core::ledger::bounded_journal_capacity(&db)?; rmm_agent_core::ledger::bounded_journal_capacity(&db)?;
Ok(Self { db, guard }) Ok(Self { db, guard })
} }
pub fn journal(&self) -> Result<Journal> { pub fn journal(&self) -> Result<Journal> {
update_handoff::read(&self.db, &self.guard, self.guard.original.permit().grant_id) update_handoff::read(&self.db, &self.guard, self.guard.original.permit().grant_id)
} }
pub fn qualify_for_effects(&self) -> Result<()> {
self.guard.check(&self.db)?;
self.guard.original.check_current(
self.guard.binding,
self.guard.clock,
self.guard.budget,
)?;
self.guard
.trust
.recheck(self.guard.original, self.guard.clock)?;
self.guard
.trust
.qualify_history(&self.db, &self.guard, self.guard.binding)
}
pub fn mark_unknown(
&mut self,
observed_at: u64,
reason: rmm_updater_handoff::UnknownReason,
) -> Result<()> {
let journal = self.journal()?;
update_handoff::mark_unknown(
&mut self.db,
&self.guard,
self.guard.original.permit(),
journal.head_sha256(),
observed_at,
reason,
)
}
pub fn append_observed(&mut self, evidence: Evidence) -> Result<()> { pub fn append_observed(&mut self, evidence: Evidence) -> Result<()> {
let journal = self.journal()?; let journal = self.journal()?;
let event = journal.prepare( let event = journal.prepare(
@@ -125,6 +172,37 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
) )
} }
pub fn observe_native(&mut self, claim: &NativeEffectClaim, observation: &str) -> Result<()> { pub fn observe_native(&mut self, claim: &NativeEffectClaim, observation: &str) -> Result<()> {
let expected = match claim.effect() {
NativeEffect::StopService { service } => {
let role = match service {
update_handoff::NativeService::Agent => {
rmm_updater_linux::services::Service::Agent
}
update_handoff::NativeService::General => {
rmm_updater_linux::services::Service::General
}
update_handoff::NativeService::Full => {
rmm_updater_linux::services::Service::Full
}
};
let proof = self
.guard
.stopped
.proofs()
.into_iter()
.find(|p| p.service == role)
.ok_or_else(|| anyhow::anyhow!("UPDATE_STOP_WITNESS_MISSING"))?;
hash(&serde_json::to_vec(&proof)?)
}
NativeEffect::SavePredecessor { role } => {
self.native_observation_sha256(file_role(*role), false)?
}
NativeEffect::PublishReplacement { role } => {
self.native_observation_sha256(file_role(*role), true)?
}
_ => anyhow::bail!("UPDATE_OWNED_OBSERVATION_NOT_ALLOWED"),
};
ensure!(expected == observation, "UPDATE_NATIVE_OBSERVATION_CHANGED");
update_handoff::observe_native_effect( update_handoff::observe_native_effect(
&mut self.db, &mut self.db,
&self.guard, &self.guard,
@@ -153,17 +231,66 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
pub fn verify_pair(&self) -> Result<()> { pub fn verify_pair(&self) -> Result<()> {
self.guard.files.verify_pair(self.guard.budget) self.guard.files.verify_pair(self.guard.budget)
} }
pub fn inflight_proof(&self) -> Result<rmm_domain::agent_update::UpdateInflightProof> {
let journal = self.journal()?;
let mut statement = self.db.prepare(
"SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence",
)?;
let events = statement
.query_map([self.guard.original.permit().grant_id.to_string()], |r| {
r.get(0)
})?
.collect::<rusqlite::Result<Vec<Vec<u8>>>>()?;
rmm_domain::agent_update::UpdateInflightProof::from_raw(
&self.guard.original.handoff().bytes()?,
self.guard.original.delegation().clone(),
&events,
journal.head_sha256().into(),
)
.map_err(anyhow::Error::msg)
}
pub fn prepare_return_and_close(mut self) -> Result<PreparedReservation> {
let journal = self.journal()?;
ensure!(
journal.phase() == Phase::PairVerified,
"UPDATE_RETURN_REQUIRES_PAIR_VERIFIED"
);
let now = self.guard.clock.now()?;
let event = journal.prepare(
u64::try_from(now.timestamp())?,
Evidence::LedgerReturnIntent,
)?;
let claims = update_handoff::prepare_return_and_starts(
&mut self.db,
&self.guard,
self.guard.original.permit().grant_id,
journal.head_sha256(),
&event,
now,
)?;
let proof = self.inflight_proof()?;
let ledger = self.guard.os.identity().clone();
let old_services = self.guard.stopped.proofs();
self.close()?;
Ok(PreparedReservation {
claims,
proof,
ledger,
old_services,
})
}
/// Close the actual SQLite connection BEFORE returning the OS flock. An /// Close the actual SQLite connection BEFORE returning the OS flock. An
/// open/failed-close connection never produces a released-ledger witness. /// open/failed-close connection never produces a released-ledger witness.
pub fn close(self) -> Result<()> { pub fn close(self) -> Result<()> {
self.guard.check(&self.db)?; self.guard.check(&self.db)?;
self.db.close().map_err(|(_, e)| e)?; self.db.close().map_err(|(_, e)| e)?;
self.guard.os.verify_for_effect(self.guard.budget) self.guard.os.verify_for_effect(&observation_budget()?)
} }
} }
impl ExecutorLedgerGuard for OwnedGuard<'_, '_> { impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
fn check(&self, db: &Connection) -> Result<()> { fn check(&self, db: &Connection) -> Result<()> {
self.os.verify_for_effect(self.budget)?; let observation = observation_budget()?;
self.os.verify_for_effect(&observation)?;
ensure!( ensure!(
unsafe { db.handle() } as usize == self.sqlite_handle, unsafe { db.handle() } as usize == self.sqlite_handle,
"UPDATE_SQLITE_CONNECTION_CHANGED" "UPDATE_SQLITE_CONNECTION_CHANGED"
@@ -174,11 +301,16 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|r| r.get(0), |r| r.get(0),
)?; )?;
ensure!(path == LEDGER, "UPDATE_SQLITE_PATH_CHANGED"); ensure!(path == LEDGER, "UPDATE_SQLITE_PATH_CHANGED");
self.original Original::verify_retained(
.check_current(self.binding, self.clock, self.budget)?; self.binding,
self.trust.recheck(self.original, self.clock)?; self.original.start_bytes(),
&self.original.handoff().bytes()?,
self.original.delegation().clone(),
)?;
self.trust.recheck_protected()?;
self.trust.recheck_original_binding(self.original)?;
self.updater self.updater
.verify_delegation(self.original.delegation(), self.budget)?; .verify_delegation(self.original.delegation(), &observation)?;
self.retained.verify()?; self.retained.verify()?;
let grant = self.original.permit().grant_id.to_string(); let grant = self.original.permit().grant_id.to_string();
let start: Vec<u8> = db.query_row( let start: Vec<u8> = db.query_row(
@@ -196,7 +328,7 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|r| r.get(0), |r| r.get(0),
)?; )?;
ensure!(unresolved == 0, "UPDATE_OTHER_UNRESOLVED_ATTEMPT"); ensure!(unresolved == 0, "UPDATE_OTHER_UNRESOLVED_ATTEMPT");
self.os.verify_for_effect(self.budget) self.os.verify_for_effect(&observation)
} }
fn check_update_owner( fn check_update_owner(
&self, &self,
@@ -217,6 +349,9 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
&& delegation == self.original.delegation(), && delegation == self.original.delegation(),
"UPDATE_OWNED_WRITER_CONTEXT_CHANGED" "UPDATE_OWNED_WRITER_CONTEXT_CHANGED"
); );
self.original
.check_current(self.binding, self.clock, self.budget)?;
self.trust.recheck(self.original, self.clock)?;
self.os.verify_for_effect(self.budget)?; self.os.verify_for_effect(self.budget)?;
self.updater.verify_delegation(delegation, self.budget) self.updater.verify_delegation(delegation, self.budget)
} }
@@ -236,8 +371,17 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
&& delegation == self.original.delegation(), && delegation == self.original.delegation(),
"UPDATE_EVENT_ORIGINAL_CHANGED" "UPDATE_EVENT_ORIGINAL_CHANGED"
); );
if matches!(event.evidence, Evidence::Unknown { .. }) {
let observation = observation_budget()?;
self.os.verify_for_effect(&observation)?;
self.updater.verify_delegation(delegation, &observation)?;
self.trust.recheck_protected()?;
self.retained.verify()?;
return Ok(());
}
self.original self.original
.check_current(self.binding, self.clock, self.budget)?; .check_current(self.binding, self.clock, self.budget)?;
self.trust.recheck(self.original, self.clock)?;
self.updater.verify_delegation(delegation, self.budget)?; self.updater.verify_delegation(delegation, self.budget)?;
self.os.verify_for_effect(self.budget)?; self.os.verify_for_effect(self.budget)?;
self.retained.verify()?; self.retained.verify()?;
@@ -304,6 +448,7 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
); );
self.original self.original
.check_current(self.binding, self.clock, self.budget)?; .check_current(self.binding, self.clock, self.budget)?;
self.trust.recheck(self.original, self.clock)?;
self.updater.verify_delegation(delegation, self.budget)?; self.updater.verify_delegation(delegation, self.budget)?;
self.os.verify_for_effect(self.budget)?; self.os.verify_for_effect(self.budget)?;
self.retained.verify()?; self.retained.verify()?;
@@ -314,6 +459,9 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin, NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin,
}; };
match (effect, stage) { match (effect, stage) {
(NativeEffect::StopService { .. }, NativeEffectStage::Observe) => {
self.stopped.verify_still_stopped(self.budget)?
}
(NativeEffect::SavePredecessor { role: r }, NativeEffectStage::Claim) => { (NativeEffect::SavePredecessor { role: r }, NativeEffectStage::Claim) => {
self.files.verify_original_and_stage(role(r), self.budget)? self.files.verify_original_and_stage(role(r), self.budget)?
} }
+7
View File
@@ -1,12 +1,19 @@
//! Concrete, original-attempt-bound Linux worker. No approval or retry path. //! Concrete, original-attempt-bound Linux worker. No approval or retry path.
#![cfg(target_os = "linux")] #![cfg(target_os = "linux")]
pub mod agent_startup;
pub mod bridge;
pub mod bundle;
pub mod clock; pub mod clock;
pub mod engine;
pub mod fence; pub mod fence;
pub mod files; pub mod files;
pub mod full; pub mod full;
pub mod history;
pub mod ledger; pub mod ledger;
pub mod original; pub mod original;
pub mod participants; pub mod participants;
pub mod record;
pub mod retained; pub mod retained;
pub mod returned;
pub mod trust; pub mod trust;
+21 -8
View File
@@ -32,6 +32,18 @@ impl Original {
delegation: UpdateDelegation, delegation: UpdateDelegation,
clock: &impl SyncClock, clock: &impl SyncClock,
budget: &Budget, budget: &Budget,
) -> Result<Self> {
let result = Self::verify_retained(binding, start_bytes, record, delegation)?;
result.check_current(binding, clock, budget)?;
Ok(result)
}
/// Historical evidence ONLY. Never supplies current boot/time/owner trust,
/// health authority, a new lease or permission for any native effect.
pub fn verify_retained(
binding: &Binding,
start_bytes: &[u8],
record: &[u8],
delegation: UpdateDelegation,
) -> Result<Self> { ) -> Result<Self> {
ensure!( ensure!(
!start_bytes.is_empty() !start_bytes.is_empty()
@@ -40,7 +52,6 @@ impl Original {
&& record.len() <= rmm_updater_handoff::MAX_RECORD_BYTES, && record.len() <= rmm_updater_handoff::MAX_RECORD_BYTES,
"UPDATE_ORIGINAL_LIMIT" "UPDATE_ORIGINAL_LIMIT"
); );
budget.check()?;
let handoff: Handoff = serde_json::from_slice(record)?; let handoff: Handoff = serde_json::from_slice(record)?;
handoff.validate()?; handoff.validate()?;
ensure!( ensure!(
@@ -72,12 +83,8 @@ impl Original {
&request, &request,
handoff.boot_id, handoff.boot_id,
delegation.prepared_at delegation.prepared_at
)? == start_bytes )? == start_bytes,
&& std::fs::read_to_string("/proc/sys/kernel/random/boot_id")? "UPDATE_ORIGINAL_START_CHANGED"
.trim()
.parse::<uuid::Uuid>()?
== handoff.boot_id,
"UPDATE_ORIGINAL_START_OR_BOOT_CHANGED"
); );
ensure!( ensure!(
job.execution_identity == "root" job.execution_identity == "root"
@@ -144,7 +151,6 @@ impl Original {
delegation, delegation,
start_bytes: start_bytes.to_vec(), start_bytes: start_bytes.to_vec(),
}; };
result.check_current(binding, clock, budget)?;
Ok(result) Ok(result)
} }
pub fn check_current( pub fn check_current(
@@ -154,6 +160,13 @@ impl Original {
budget: &Budget, budget: &Budget,
) -> Result<()> { ) -> Result<()> {
budget.check()?; budget.check()?;
ensure!(
std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
.trim()
.parse::<uuid::Uuid>()?
== self.handoff.boot_id,
"UPDATE_ORIGINAL_BOOT_CHANGED"
);
let now = clock.now()?; let now = clock.now()?;
ensure!( ensure!(
now >= self.delegation.prepared_at now >= self.delegation.prepared_at
+944
View File
@@ -0,0 +1,944 @@
//! Deterministic record bytes and one live, fixed-namespace publication pass.
//! Neither a builder nor a namespace proof grants authority. The caller's real
//! guard must recheck Original/Health/clock/services and every other protected
//! file before committing each native claim and observation in the SAME ledger.
use crate::original::Original;
use anyhow::{Result, ensure};
use ed25519_dalek::{Signature, VerifyingKey};
use rmm_agent_core::update_handoff::NativeEffect;
use rmm_domain::{
agent_update::ResolvedAgentUpdate,
agent_update_provisioning::{ActivationEvidence, InstalledReleaseRecord, MAX_RECORD_BYTES},
jobs::hash,
};
use rmm_updater_handoff::{
Event, Evidence, HEALTH_DOMAIN, HealthReceipt, Journal, Phase, SignedHealth,
};
use rmm_updater_linux::{
Budget,
files::{Directory, FileProof, Identity},
};
use serde::Serialize;
use std::{
ffi::CString,
fs::{File, Metadata, OpenOptions},
io::Write,
os::{
fd::{AsRawFd, FromRawFd},
unix::fs::{FileExt, MetadataExt, OpenOptionsExt},
},
path::Path,
};
use uuid::Uuid;
const ROOT: &str = "/var/lib/rmm-agent-updater";
const CURRENT: &str = "installed-record.json";
/// Private construction binds all future namespace operations to these exact
/// original inputs. Deliberately neither Clone nor Deserialize nor a resume API.
pub struct BuiltRecord {
record: InstalledReleaseRecord,
raw: Vec<u8>,
prior_raw: Vec<u8>,
old_sha256: String,
sha256: String,
grant_id: Uuid,
owner_nonce: Uuid,
}
impl BuiltRecord {
pub fn build(
original: &Original,
prior_raw: &[u8],
journal: &Journal,
health_event_raw: &[u8],
) -> Result<Self> {
let header = original.handoff();
let handoff_sha256 = hash(&header.bytes()?);
ensure!(
journal.phase() == Phase::HealthVerified
&& journal.handoff_sha256() == handoff_sha256
&& !health_event_raw.is_empty()
&& health_event_raw.len() <= rmm_updater_handoff::MAX_EVENT_BYTES
&& hash(health_event_raw) == journal.head_sha256(),
"UPDATE_RECORD_HEALTH_HEAD"
);
let event: Event = serde_json::from_slice(health_event_raw)?;
ensure!(
serde_json::to_vec(&event)? == health_event_raw
&& event.handoff_sha256 == handoff_sha256
&& event.boot_id == header.boot_id,
"UPDATE_RECORD_HEALTH_EVENT"
);
let Evidence::HealthVerified { receipt: signed } = &event.evidence else {
anyhow::bail!("UPDATE_RECORD_HEALTH_REQUIRED")
};
ensure!(
!signed.payload.is_empty()
&& signed.payload.len() <= 4096
&& signed.signature.len() == 64,
"UPDATE_RECORD_HEALTH_LIMIT"
);
let receipt: HealthReceipt = serde_json::from_slice(&signed.payload)?;
ensure!(
serde_json::to_vec(&receipt)? == signed.payload,
"UPDATE_RECORD_HEALTH_NONCANONICAL"
);
// Journal possession is not independent key trust. Authenticate the
// exact receipt again with the ORIGINAL signed job's external key pin.
let raw: [u8; 32] = hex::decode(&original.update().updater.health_public_key_hex)?
.try_into()
.map_err(|_| anyhow::anyhow!("UPDATE_RECORD_HEALTH_KEY"))?;
let public = VerifyingKey::from_bytes(&raw)?;
ensure!(!public.is_weak(), "UPDATE_RECORD_HEALTH_KEY");
let mut message = HEALTH_DOMAIN.to_vec();
message.extend_from_slice(&signed.payload);
public.verify_strict(&message, &Signature::from_slice(&signed.signature)?)?;
ensure!(
receipt.schema_version == 1
&& receipt.handoff_sha256 == handoff_sha256
&& receipt.owner_nonce == header.owner_nonce
&& receipt.boot_id == header.boot_id
&& receipt.helper_journal_id == header.helper_journal_id
&& receipt.agent_journal_id == header.agent_journal_id
&& receipt.connection_generation > header.connection_generation
&& !receipt.ready
&& receipt.issued_at >= header.consumed_at
&& receipt.issued_at <= event.observed_at
&& event.observed_at < receipt.valid_until
&& receipt.valid_until <= header.finish_before,
"UPDATE_RECORD_HEALTH_BINDING"
);
Self::assemble(
original.update(),
prior_raw,
&handoff_sha256,
signed,
&receipt,
header.attempt.grant_id,
header.owner_nonce,
)
}
fn assemble(
update: &ResolvedAgentUpdate,
prior_raw: &[u8],
handoff_sha256: &str,
signed: &SignedHealth,
receipt: &HealthReceipt,
grant_id: Uuid,
owner_nonce: Uuid,
) -> Result<Self> {
ensure!(
update.validate() && !grant_id.is_nil() && !owner_nonce.is_nil(),
"UPDATE_RECORD_INPUT"
);
let prior = InstalledReleaseRecord::from_bytes(prior_raw).map_err(anyhow::Error::msg)?;
let parent = prior
.release_envelope
.decode()
.map_err(anyhow::Error::msg)?;
let target = update.manifest().map_err(anyhow::Error::msg)?;
let old_sha256 = hash(prior_raw);
ensure!(
!prior.rolled_back
&& old_sha256 == update.predecessor.installed_record_sha256
&& prior.manifest_sha256 == update.predecessor.manifest_sha256
&& parent.manifest.release_id == update.predecessor.release_id
&& parent.manifest.release_sequence == update.predecessor.release_sequence
&& parent.manifest.artifacts[0].sha256 == update.predecessor.agent_sha256
&& parent.manifest.artifacts[1].sha256 == update.predecessor.helper_sha256
&& prior.platform == target.platform
&& target.release_sequence > prior.activation_floor
&& receipt.issued_at >= prior.activated_at,
"UPDATE_RECORD_PREDECESSOR_OR_FLOOR"
);
let record = InstalledReleaseRecord {
schema_version: 1,
profile: "decoder_compatible_handoff_v1".into(),
platform: target.platform,
release_envelope: update.release_envelope.clone(),
manifest_sha256: update.manifest_sha256.clone(),
activation_floor: target.release_sequence,
activated_at: receipt.issued_at,
previous_record_sha256: Some(old_sha256.clone()),
activation_evidence: ActivationEvidence::Updated {
handoff_sha256: handoff_sha256.into(),
signed_health_sha256: hash(&serde_json::to_vec(signed)?),
},
rolled_back: false,
};
let raw = record.bytes().map_err(anyhow::Error::msg)?;
let sha256 = hash(&raw);
ensure!(sha256 != old_sha256, "UPDATE_RECORD_SAME_BYTES");
Ok(Self {
record,
raw,
prior_raw: prior_raw.to_vec(),
old_sha256,
sha256,
grant_id,
owner_nonce,
})
}
pub fn raw(&self) -> &[u8] {
&self.raw
}
pub fn record(&self) -> &InstalledReleaseRecord {
&self.record
}
pub fn sha256(&self) -> &str {
&self.sha256
}
pub fn old_sha256(&self) -> &str {
&self.old_sha256
}
pub fn grant_id(&self) -> Uuid {
self.grant_id
}
pub fn owner_nonce(&self) -> Uuid {
self.owner_nonce
}
}
/// The concrete Fullhelper adapter commits an original Core claim before each
/// rename and its original observation afterwards. No permissive implementation.
pub trait RecordFence {
fn before_effect(
&mut self,
effect: &NativeEffect,
namespace: &RecordNamespaceProof<'_>,
) -> Result<()>;
fn after_effect(
&mut self,
effect: &NativeEffect,
namespace: &RecordNamespaceProof<'_>,
observation_sha256: &str,
) -> Result<()>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RecordPhase {
Prepared,
Saved,
Published,
}
struct HeldDirectory {
protected: Directory,
fd: File,
identity: Identity,
}
fn identity(meta: &Metadata) -> Identity {
Identity {
device: meta.dev(),
inode: meta.ino(),
uid: meta.uid(),
gid: meta.gid(),
mode: meta.mode(),
links: meta.nlink(),
}
}
fn component(value: &str) -> Result<CString> {
ensure!(
!value.is_empty() && value != "." && value != ".." && !value.contains('/'),
"UPDATE_RECORD_COMPONENT"
);
Ok(CString::new(value)?)
}
fn openat(dir: &File, name: &str, flags: i32, mode: u32) -> Result<File> {
let name = component(name)?;
// SAFETY: live held dirfd and one validated NUL-terminated component. Each
// successful descriptor is immediately owned by exactly one File.
let fd = unsafe {
libc::openat(
dir.as_raw_fd(),
name.as_ptr(),
flags | libc::O_NOFOLLOW | libc::O_CLOEXEC,
mode,
)
};
ensure!(
fd >= 0,
"UPDATE_RECORD_OPEN: {}",
std::io::Error::last_os_error()
);
Ok(unsafe { File::from_raw_fd(fd) })
}
impl HeldDirectory {
fn open(path: &Path) -> Result<Self> {
let protected = Directory::open(path, 0, true)?;
let fd = OpenOptions::new()
.read(true)
.custom_flags(libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC)
.open(path)?;
let actual = identity(&fd.metadata()?);
ensure!(
&actual == protected.identity() && actual.mode & 0o777 == 0o700,
"UPDATE_RECORD_DIRECTORY_BINDING"
);
let result = Self {
protected,
fd,
identity: actual,
};
result.verify()?;
Ok(result)
}
fn verify(&self) -> Result<()> {
self.protected.verify()?;
ensure!(
identity(&self.fd.metadata()?) == self.identity,
"UPDATE_RECORD_DIRECTORY_CHANGED"
);
Ok(())
}
fn absent(&self, name: &str) -> Result<()> {
let name = component(name)?;
let mut value = std::mem::MaybeUninit::<libc::stat>::uninit();
let result = unsafe {
libc::fstatat(
self.fd.as_raw_fd(),
name.as_ptr(),
value.as_mut_ptr(),
libc::AT_SYMLINK_NOFOLLOW,
)
};
ensure!(
result < 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ENOENT),
"UPDATE_RECORD_DESTINATION_EXISTS"
);
Ok(())
}
fn named(&self, name: &str, held: &RecordFile) -> Result<()> {
self.verify()?;
held.verify()?;
let named = openat(&self.fd, name, libc::O_RDONLY | libc::O_NONBLOCK, 0)?;
ensure!(
file_proof(&named.metadata()?, &held.proof.sha256)? == held.proof,
"UPDATE_RECORD_NAMED_FILE_CHANGED"
);
// A held inode and a same-byte different named inode are distinct.
held.verify()?;
self.verify()
}
fn sync(&self) -> Result<()> {
self.fd.sync_all()?;
Ok(())
}
fn rename_new(&self, from: &str, to: &str) -> Result<()> {
let from = component(from)?;
let to = component(to)?;
let result = unsafe {
libc::renameat2(
self.fd.as_raw_fd(),
from.as_ptr(),
self.fd.as_raw_fd(),
to.as_ptr(),
libc::RENAME_NOREPLACE,
)
};
ensure!(
result == 0,
"UPDATE_RECORD_RENAME: {}",
std::io::Error::last_os_error()
);
self.sync()
}
fn create(&self, name: &str, raw: &[u8]) -> Result<RecordFile> {
self.verify()?;
ensure!(
!raw.is_empty() && raw.len() <= MAX_RECORD_BYTES,
"UPDATE_RECORD_SIZE"
);
let mut file = openat(
&self.fd,
name,
libc::O_RDWR | libc::O_CREAT | libc::O_EXCL,
0o600,
)?;
file.write_all(raw)?;
file.sync_all()?;
self.sync()?;
let held = RecordFile::from_file(file, raw)?;
self.named(name, &held)?;
Ok(held)
}
fn archive(&self, name: &str, raw: &[u8]) -> Result<RecordFile> {
let name_c = component(name)?;
let mut value = std::mem::MaybeUninit::<libc::stat>::uninit();
let result = unsafe {
libc::fstatat(
self.fd.as_raw_fd(),
name_c.as_ptr(),
value.as_mut_ptr(),
libc::AT_SYMLINK_NOFOLLOW,
)
};
if result < 0 {
ensure!(
std::io::Error::last_os_error().raw_os_error() == Some(libc::ENOENT),
"UPDATE_RECORD_ARCHIVE_STAT"
);
return self.create(name, raw);
}
let file = RecordFile::open(self, name, raw)?;
self.named(name, &file)?;
Ok(file)
}
}
fn file_proof(meta: &Metadata, sha256: &str) -> Result<FileProof> {
ensure!(
meta.is_file()
&& meta.uid() == 0
&& meta.mode() & 0o7777 == 0o600
&& meta.nlink() == 1
&& meta.len() > 0
&& meta.len() <= MAX_RECORD_BYTES as u64,
"UPDATE_RECORD_PROTECTED_FILE"
);
Ok(FileProof {
identity: identity(meta),
bytes: meta.len(),
sha256: sha256.into(),
})
}
fn stamp(meta: &Metadata) -> (Identity, u64, i64, i64, i64, i64) {
(
identity(meta),
meta.len(),
meta.mtime(),
meta.mtime_nsec(),
meta.ctime(),
meta.ctime_nsec(),
)
}
struct RecordFile {
fd: File,
proof: FileProof,
}
impl RecordFile {
fn open(dir: &HeldDirectory, name: &str, raw: &[u8]) -> Result<Self> {
ensure!(
!raw.is_empty() && raw.len() <= MAX_RECORD_BYTES,
"UPDATE_RECORD_SIZE"
);
let fd = openat(&dir.fd, name, libc::O_RDONLY | libc::O_NONBLOCK, 0)?;
Self::from_file(fd, raw)
}
fn from_file(fd: File, raw: &[u8]) -> Result<Self> {
ensure!(
!raw.is_empty() && raw.len() <= MAX_RECORD_BYTES,
"UPDATE_RECORD_SIZE"
);
let proof = file_proof(&fd.metadata()?, &hash(raw))?;
ensure!(proof.bytes == raw.len() as u64, "UPDATE_RECORD_LENGTH");
let result = Self { fd, proof };
result.verify()?;
Ok(result)
}
fn verify(&self) -> Result<()> {
let before = self.fd.metadata()?;
ensure!(
file_proof(&before, &self.proof.sha256)? == self.proof,
"UPDATE_RECORD_HELD_FILE_CHANGED"
);
let mut raw = vec![0; self.proof.bytes as usize];
self.fd.read_exact_at(&mut raw, 0)?;
let mut extra = [0; 1];
ensure!(
self.fd.read_at(&mut extra, self.proof.bytes)? == 0
&& hash(&raw) == self.proof.sha256
&& stamp(&before) == stamp(&self.fd.metadata()?),
"UPDATE_RECORD_HELD_BYTES_CHANGED"
);
Ok(())
}
}
pub struct RecordTransition {
built: BuiltRecord,
directory: HeldDirectory,
archives: HeldDirectory,
old: RecordFile,
staged: RecordFile,
archived_old: RecordFile,
archived_new: RecordFile,
stage_name: String,
previous_name: String,
phase: RecordPhase,
poisoned: bool,
}
impl RecordTransition {
/// Call only after independently authorized, still-current original Health.
/// Creates bounded private staging/archive bytes; never changes Current.
pub fn prepare(built: BuiltRecord, budget: &Budget) -> Result<Self> {
Self::prepare_in(Path::new(ROOT), built, budget)
}
fn prepare_in(path: &Path, built: BuiltRecord, budget: &Budget) -> Result<Self> {
budget.check()?;
let directory = HeldDirectory::open(path)?;
let archives = HeldDirectory::open(&path.join("records"))?;
let stage_name = format!(".rmm-record-{}.stage", built.owner_nonce);
let previous_name = format!(".rmm-record-{}.previous", built.owner_nonce);
directory.absent(&stage_name)?;
directory.absent(&previous_name)?;
let old = RecordFile::open(&directory, CURRENT, &built.prior_raw)?;
directory.named(CURRENT, &old)?;
let archived_old =
archives.archive(&format!("{}.json", built.old_sha256), &built.prior_raw)?;
budget.check()?;
let archived_new = archives.archive(&format!("{}.json", built.sha256), &built.raw)?;
budget.check()?;
let staged = directory.create(&stage_name, &built.raw)?;
let result = Self {
built,
directory,
archives,
old,
staged,
archived_old,
archived_new,
stage_name,
previous_name,
phase: RecordPhase::Prepared,
poisoned: false,
};
result.namespace_proof().verify()?;
budget.check()?;
Ok(result)
}
pub fn namespace_proof(&self) -> RecordNamespaceProof<'_> {
RecordNamespaceProof { transition: self }
}
pub fn save_previous(&mut self, fence: &mut impl RecordFence, budget: &Budget) -> Result<()> {
ensure!(
!self.poisoned && self.phase == RecordPhase::Prepared,
"UPDATE_RECORD_NO_SAVE_RESUME"
);
self.poisoned = true;
let effect = NativeEffect::SaveInstalledRecord {
old_sha256: self.built.old_sha256.clone(),
new_sha256: self.built.sha256.clone(),
};
self.namespace_proof().verify()?;
budget.check()?;
fence.before_effect(&effect, &self.namespace_proof())?;
self.namespace_proof().verify()?;
budget.check()?;
self.directory.rename_new(CURRENT, &self.previous_name)?;
self.phase = RecordPhase::Saved;
self.namespace_proof().verify()?;
let observed = self.namespace_proof().observation_sha256()?;
fence.after_effect(&effect, &self.namespace_proof(), &observed)?;
budget.check()?;
self.poisoned = false;
Ok(())
}
pub fn publish(&mut self, fence: &mut impl RecordFence, budget: &Budget) -> Result<()> {
ensure!(
!self.poisoned && self.phase == RecordPhase::Saved,
"UPDATE_RECORD_NO_PUBLISH_RESUME"
);
self.poisoned = true;
let effect = NativeEffect::PublishInstalledRecord {
old_sha256: self.built.old_sha256.clone(),
new_sha256: self.built.sha256.clone(),
};
self.namespace_proof().verify()?;
budget.check()?;
fence.before_effect(&effect, &self.namespace_proof())?;
self.namespace_proof().verify()?;
budget.check()?;
self.directory.rename_new(&self.stage_name, CURRENT)?;
self.phase = RecordPhase::Published;
self.namespace_proof().verify()?;
let observed = self.namespace_proof().observation_sha256()?;
fence.after_effect(&effect, &self.namespace_proof(), &observed)?;
budget.check()?;
self.poisoned = false;
Ok(())
}
}
/// Borrowed held-descriptor proof, not serializable/constructible from IPC and
/// never a flag authorizing a general TrustState skip. verify() checks the whole
/// precise allowed namespace, including both create-only history copies.
pub struct RecordNamespaceProof<'a> {
transition: &'a RecordTransition,
}
impl RecordNamespaceProof<'_> {
pub fn phase(&self) -> RecordPhase {
self.transition.phase
}
pub fn record(&self) -> &BuiltRecord {
&self.transition.built
}
pub fn directory_identity(&self) -> &Identity {
&self.transition.directory.identity
}
pub fn old_file(&self) -> &FileProof {
&self.transition.old.proof
}
pub fn new_file(&self) -> &FileProof {
&self.transition.staged.proof
}
pub fn old_sha256(&self) -> &str {
self.transition.built.old_sha256()
}
pub fn new_sha256(&self) -> &str {
self.transition.built.sha256()
}
pub fn grant_id(&self) -> Uuid {
self.transition.built.grant_id()
}
pub fn owner_nonce(&self) -> Uuid {
self.transition.built.owner_nonce()
}
pub fn verify(&self) -> Result<()> {
let t = self.transition;
t.directory.verify()?;
t.archives.verify()?;
t.old.verify()?;
t.staged.verify()?;
t.archives
.named(&format!("{}.json", t.built.old_sha256), &t.archived_old)?;
t.archives
.named(&format!("{}.json", t.built.sha256), &t.archived_new)?;
ensure!(
t.old.proof.sha256 == t.built.old_sha256
&& t.staged.proof.sha256 == t.built.sha256
&& t.old.proof.identity.inode != t.staged.proof.identity.inode
&& t.old.proof.identity.device == t.staged.proof.identity.device,
"UPDATE_RECORD_PAIR_CHANGED"
);
match t.phase {
RecordPhase::Prepared => {
t.directory.named(CURRENT, &t.old)?;
t.directory.named(&t.stage_name, &t.staged)?;
t.directory.absent(&t.previous_name)?;
}
RecordPhase::Saved => {
t.directory.named(&t.previous_name, &t.old)?;
t.directory.named(&t.stage_name, &t.staged)?;
t.directory.absent(CURRENT)?;
}
RecordPhase::Published => {
t.directory.named(&t.previous_name, &t.old)?;
t.directory.named(CURRENT, &t.staged)?;
t.directory.absent(&t.stage_name)?;
}
}
t.directory.verify()?;
t.archives.verify()
}
pub fn observation_sha256(&self) -> Result<String> {
self.verify()?;
#[derive(Serialize)]
struct Observation<'a> {
grant_id: Uuid,
owner_nonce: Uuid,
phase: RecordPhase,
directory: &'a Identity,
archives: &'a Identity,
old: &'a FileProof,
new: &'a FileProof,
archived_old: &'a FileProof,
archived_new: &'a FileProof,
}
let t = self.transition;
Ok(hash(&serde_json::to_vec(&Observation {
grant_id: self.grant_id(),
owner_nonce: self.owner_nonce(),
phase: t.phase,
directory: &t.directory.identity,
archives: &t.archives.identity,
old: &t.old.proof,
new: &t.staged.proof,
archived_old: &t.archived_old.proof,
archived_new: &t.archived_new.proof,
})?))
}
}
#[cfg(test)]
mod tests {
// Structural record bytes and actual Root-owned scratch IO only. Synthetic
// codec signatures below are NOT accepted by the public authenticated build
// method, never a real release/Health/approval or a simulated target job.
use super::*;
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use std::{fs, os::unix::fs::DirBuilderExt, path::PathBuf, time::Duration};
struct Fixture(PathBuf);
impl Fixture {
fn new(raw: &[u8]) -> Self {
assert_eq!(
unsafe { libc::geteuid() },
0,
"Run only in the isolated Root container scratch fixture, never as a host daemon"
);
let path = PathBuf::from(format!("/run/rmm-record-unit-{}", Uuid::new_v4()));
fs::DirBuilder::new().mode(0o700).create(&path).unwrap();
fs::DirBuilder::new()
.mode(0o700)
.create(path.join("records"))
.unwrap();
let mut file = OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(path.join(CURRENT))
.unwrap();
file.write_all(raw).unwrap();
file.sync_all().unwrap();
Self(path)
}
}
impl Drop for Fixture {
fn drop(&mut self) {
fs::remove_dir_all(&self.0).unwrap();
}
}
fn budget() -> Budget {
let boot = fs::read_to_string("/proc/sys/kernel/random/boot_id")
.unwrap()
.trim()
.parse()
.unwrap();
Budget::new(boot, Duration::from_secs(10)).unwrap()
}
fn fixture_record() -> BuiltRecord {
let golden: serde_json::Value = serde_json::from_str(include_str!(
"../../../tests/agent_update/codec-golden.json"
))
.unwrap();
let mut update: ResolvedAgentUpdate = serde_json::from_value(
golden["linux_full"]["payload"]["jobs"][0]["payload"]["action"]["update"].clone(),
)
.unwrap();
let mut prior_manifest = update.manifest().unwrap();
prior_manifest.release_id = update.predecessor.release_id;
prior_manifest.release_sequence = update.predecessor.release_sequence;
prior_manifest.artifacts[0].sha256 = update.predecessor.agent_sha256.clone();
prior_manifest.artifacts[1].sha256 = update.predecessor.helper_sha256.clone();
let mut prior_envelope = update.release_envelope.clone();
let payload = serde_json::to_vec(&prior_manifest).unwrap();
prior_envelope.payload_bytes = URL_SAFE_NO_PAD.encode(&payload);
let prior = InstalledReleaseRecord {
schema_version: 1,
profile: "decoder_compatible_handoff_v1".into(),
platform: prior_manifest.platform,
release_envelope: prior_envelope,
manifest_sha256: hash(&payload),
activation_floor: prior_manifest.release_sequence,
activated_at: 100,
previous_record_sha256: None,
activation_evidence: ActivationEvidence::Bootstrap {
qualification_receipt_sha256: "12".repeat(32),
},
rolled_back: false,
};
let prior_raw = prior.bytes().unwrap();
update.predecessor.installed_record_sha256 = hash(&prior_raw);
update.predecessor.manifest_sha256 = prior.manifest_sha256;
let receipt = HealthReceipt {
schema_version: 1,
handoff_sha256: "34".repeat(32),
owner_nonce: Uuid::new_v4(),
boot_id: Uuid::new_v4(),
helper_journal_id: Uuid::new_v4(),
agent_journal_id: Uuid::new_v4(),
connection_generation: 2,
service_processes: vec![],
ledger_identity_sha256: "56".repeat(32),
signed_sync_decision_id: Uuid::new_v4(),
signed_sync_request_sha256: "78".repeat(32),
signed_sync_response_sha256: "9a".repeat(32),
signed_sync_applied_at: 120,
ready: false,
inventory_message_id: Uuid::new_v4(),
inventory_received_at: 120,
inventory_ack_sha256: "bc".repeat(32),
issued_at: 123,
valid_until: 150,
};
let health = SignedHealth {
payload: serde_json::to_vec(&receipt).unwrap(),
signature: vec![0; 64],
};
let result = BuiltRecord::assemble(
&update,
&prior_raw,
&receipt.handoff_sha256,
&health,
&receipt,
Uuid::new_v4(),
receipt.owner_nonce,
)
.unwrap();
assert_eq!(result.record.activated_at, 123);
assert_eq!(
result.record.activation_floor,
update.manifest().unwrap().release_sequence
);
assert_eq!(
result.record.previous_record_sha256.as_deref(),
Some(hash(&prior_raw).as_str())
);
let ActivationEvidence::Updated {
signed_health_sha256,
..
} = &result.record.activation_evidence
else {
panic!("fixture")
};
assert_eq!(
*signed_health_sha256,
hash(&serde_json::to_vec(&health).unwrap())
);
result
}
#[derive(Default)]
struct CheckedFence {
calls: Vec<(bool, RecordPhase)>,
}
impl RecordFence for CheckedFence {
fn before_effect(
&mut self,
effect: &NativeEffect,
proof: &RecordNamespaceProof<'_>,
) -> Result<()> {
proof.verify()?;
let phase = match effect {
NativeEffect::SaveInstalledRecord {
old_sha256,
new_sha256,
} => {
ensure!(
old_sha256 == proof.old_sha256() && new_sha256 == proof.new_sha256(),
"fixture pair"
);
RecordPhase::Prepared
}
NativeEffect::PublishInstalledRecord {
old_sha256,
new_sha256,
} => {
ensure!(
old_sha256 == proof.old_sha256() && new_sha256 == proof.new_sha256(),
"fixture pair"
);
RecordPhase::Saved
}
_ => anyhow::bail!("fixture unexpected effect"),
};
ensure!(proof.phase() == phase, "fixture phase");
self.calls.push((true, phase));
Ok(())
}
fn after_effect(
&mut self,
effect: &NativeEffect,
proof: &RecordNamespaceProof<'_>,
observation: &str,
) -> Result<()> {
let phase = match effect {
NativeEffect::SaveInstalledRecord { .. } => RecordPhase::Saved,
NativeEffect::PublishInstalledRecord { .. } => RecordPhase::Published,
_ => anyhow::bail!("fixture unexpected effect"),
};
ensure!(
proof.phase() == phase && observation == proof.observation_sha256()?,
"fixture observation"
);
self.calls.push((false, phase));
Ok(())
}
}
#[test]
fn separate_record_renames_preserve_original_inode_and_both_raw_archives() {
let built = fixture_record();
let fixture = Fixture::new(&built.prior_raw);
let budget = budget();
let old_raw = built.prior_raw.clone();
let new_raw = built.raw.clone();
let old_hash = built.old_sha256.clone();
let new_hash = built.sha256.clone();
let mut transition = RecordTransition::prepare_in(&fixture.0, built, &budget).unwrap();
let original = transition.namespace_proof().old_file().identity.clone();
let mut fence = CheckedFence::default();
transition.save_previous(&mut fence, &budget).unwrap();
assert!(!fixture.0.join(CURRENT).exists());
assert_eq!(
fs::read(fixture.0.join(&transition.previous_name)).unwrap(),
old_raw
);
transition.publish(&mut fence, &budget).unwrap();
assert_eq!(fs::read(fixture.0.join(CURRENT)).unwrap(), new_raw);
assert_eq!(&transition.namespace_proof().old_file().identity, &original);
assert_eq!(
fs::read(fixture.0.join("records").join(format!("{old_hash}.json"))).unwrap(),
old_raw
);
assert_eq!(
fs::read(fixture.0.join("records").join(format!("{new_hash}.json"))).unwrap(),
new_raw
);
assert_eq!(
fence.calls,
vec![
(true, RecordPhase::Prepared),
(false, RecordPhase::Saved),
(true, RecordPhase::Saved),
(false, RecordPhase::Published)
]
);
assert!(transition.publish(&mut fence, &budget).is_err());
}
#[test]
fn same_byte_inode_substitution_and_foreign_current_block_without_resume() {
let built = fixture_record();
let fixture = Fixture::new(&built.prior_raw);
let budget = budget();
let old_raw = built.prior_raw.clone();
let mut transition = RecordTransition::prepare_in(&fixture.0, built, &budget).unwrap();
fs::rename(fixture.0.join(CURRENT), fixture.0.join("retained-original")).unwrap();
let mut file = OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(fixture.0.join(CURRENT))
.unwrap();
file.write_all(&old_raw).unwrap();
file.sync_all().unwrap();
let mut fence = CheckedFence::default();
assert!(transition.save_previous(&mut fence, &budget).is_err());
assert!(fence.calls.is_empty());
assert!(transition.save_previous(&mut fence, &budget).is_err());
assert_eq!(
fs::read(fixture.0.join("retained-original")).unwrap(),
old_raw
);
let built = fixture_record();
let other = Fixture::new(&built.prior_raw);
let mut transition = RecordTransition::prepare_in(&other.0, built, &budget).unwrap();
transition.save_previous(&mut fence, &budget).unwrap();
let mut foreign = OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(other.0.join(CURRENT))
.unwrap();
foreign.write_all(b"foreign public fixture").unwrap();
foreign.sync_all().unwrap();
let calls = fence.calls.len();
assert!(transition.publish(&mut fence, &budget).is_err());
assert_eq!(fence.calls.len(), calls);
assert_eq!(
fs::read(other.0.join(CURRENT)).unwrap(),
b"foreign public fixture"
);
assert!(transition.publish(&mut fence, &budget).is_err());
}
}
+143 -1
View File
@@ -26,7 +26,8 @@ pub struct RetainedFileSpec {
pub identity: Identity, pub identity: Identity,
pub retention: Retention, pub retention: Retention,
} }
#[derive(Clone, Debug, PartialEq, Eq, Serialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Stamp { struct Stamp {
size: u64, size: u64,
mtime: i64, mtime: i64,
@@ -120,6 +121,59 @@ impl RetainedSet {
pub fn sha256(&self) -> &str { pub fn sha256(&self) -> &str {
&self.digest &self.digest
} }
pub fn specs(&self) -> Vec<&RetainedFileSpec> {
self.files.iter().map(|file| &file.spec).collect()
}
/// Exact ORIGINAL spec/stamp bytes. No current timestamp is substituted.
pub fn bytes(&self) -> Result<Vec<u8>> {
let serial: Vec<_> = self
.files
.iter()
.map(|f| {
(
&f.spec,
if f.spec.retention == Retention::ImmutableMetadata {
Some(&f.stamp)
} else {
None
},
)
})
.collect();
let raw = serde_json::to_vec(&serial)?;
ensure!(
raw.len() <= 65536 && hash(&raw) == self.digest,
"UPDATE_RETAINED_WITNESS_CHANGED"
);
Ok(raw)
}
pub fn from_bound_bytes(raw: &[u8], handoff: &rmm_updater_handoff::Handoff) -> Result<Self> {
Self::from_bound_digest(raw, &handoff.retained_state_sha256)
}
fn from_bound_digest(raw: &[u8], original_sha256: &str) -> Result<Self> {
ensure!(
!raw.is_empty() && raw.len() <= 65536 && hash(raw) == original_sha256,
"UPDATE_RETAINED_WITNESS_HASH"
);
let snapshot: Vec<(RetainedFileSpec, Option<Stamp>)> = serde_json::from_slice(raw)?;
ensure!(
!snapshot.is_empty()
&& snapshot.len() <= 64
&& serde_json::to_vec(&snapshot)? == raw
&& snapshot.iter().all(|(spec, stamp)| (spec.retention
== Retention::ImmutableMetadata)
== stamp.is_some()),
"UPDATE_RETAINED_WITNESS_CODEC"
);
// Reopen current files but compare their actual stamps to the ORIGINAL
// canonical bytes. Any changed immutable stamp fails; never refresh it.
let current = Self::capture(snapshot.into_iter().map(|(spec, _)| spec).collect())?;
ensure!(
current.bytes()? == raw && current.digest == original_sha256,
"UPDATE_RETAINED_ORIGINAL_STAMP_CHANGED"
);
Ok(current)
}
pub fn verify(&self) -> Result<()> { pub fn verify(&self) -> Result<()> {
for file in &self.files { for file in &self.files {
file.verify()?; file.verify()?;
@@ -161,3 +215,91 @@ impl HeldState {
self.directory.verify() self.directory.verify()
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use std::{fs, io::Write, os::unix::fs::PermissionsExt};
struct Fixture(PathBuf);
impl Drop for Fixture {
fn drop(&mut self) {
fs::remove_dir_all(&self.0).unwrap();
}
}
#[test]
fn reopen_preserves_original_stamps_and_rejects_replaced_state_without_reading_keys() {
assert_eq!(
unsafe { libc::geteuid() },
0,
"Use isolated root-owned unit container, never a host daemon"
);
let path = PathBuf::from(format!(
"/run/rmm-worker-witness-unit-{}",
uuid::Uuid::new_v4()
));
fs::create_dir(&path).unwrap();
fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap();
let f = Fixture(path);
let mut specs = Vec::new();
for (name, retention) in [
("unit-db", Retention::MutableDatabase),
("public-fixture", Retention::ImmutableMetadata),
] {
let path = f.0.join(name);
let mut file = fs::OpenOptions::new()
.create_new(true)
.write(true)
.mode(0o600)
.open(&path)
.unwrap();
file.write_all(b"public unit fixture").unwrap();
file.sync_all().unwrap();
specs.push(RetainedFileSpec {
path,
parent_uid: 0,
identity: identity(&file.metadata().unwrap()),
retention,
});
}
let captured = RetainedSet::capture(specs).unwrap();
let original = captured.bytes().unwrap();
let sha = captured.sha256().to_owned();
fs::OpenOptions::new()
.append(true)
.open(f.0.join("unit-db"))
.unwrap()
.write_all(b" advanced")
.unwrap();
assert_eq!(
RetainedSet::from_bound_digest(&original, &sha)
.unwrap()
.bytes()
.unwrap(),
original
);
fs::rename(f.0.join("unit-db"), f.0.join("old-db")).unwrap();
let mut replacement = fs::OpenOptions::new()
.create_new(true)
.write(true)
.mode(0o600)
.open(f.0.join("unit-db"))
.unwrap();
replacement
.write_all(b"same name does not preserve identity")
.unwrap();
assert!(RetainedSet::from_bound_digest(&original, &sha).is_err());
drop(replacement);
fs::remove_file(f.0.join("unit-db")).unwrap();
fs::rename(f.0.join("old-db"), f.0.join("unit-db")).unwrap();
assert!(RetainedSet::from_bound_digest(&original, &sha).is_ok());
fs::OpenOptions::new()
.append(true)
.open(f.0.join("public-fixture"))
.unwrap()
.write_all(b" changed")
.unwrap();
assert!(RetainedSet::from_bound_digest(&original, &sha).is_err());
assert!(captured.verify().is_err());
assert_eq!(captured.bytes().unwrap(), original);
}
}
+312
View File
@@ -0,0 +1,312 @@
//! Already committed ReturnIntent and exact once-only start reservations.
//! This path owns NO SQLite connection and never writes beside a running helper.
use crate::{
bridge::FullBridge, clock::NativeClock, files::FileWitnesses, original::Original,
participants::StableParticipant, retained::RetainedSet, trust::TrustState,
};
use anyhow::{Result, ensure};
use rmm_agent_core::{
auth::Binding,
update_handoff::{NativeControlOperation, NativeEffect, NativeService, PreparedReturnClaims},
};
use rmm_domain::{agent_update::UpdateInflightProof, jobs::hash};
use rmm_updater_linux::{
Budget, DurableEffectFence, Effect,
files::{Directory, Identity, LedgerIdentity},
services::{Service, ServiceProof, Systemd},
};
use std::{
fs::OpenOptions,
os::{
fd::AsRawFd,
unix::fs::{MetadataExt, OpenOptionsExt},
},
path::Path,
};
pub(crate) struct Context<'a> {
pub original: &'a Original,
pub binding: &'a Binding,
pub clock: &'a NativeClock,
pub budget: &'a Budget,
pub updater: &'a StableParticipant,
pub retained: &'a RetainedSet,
pub files: &'a FileWitnesses,
pub trust: &'a TrustState,
pub ledger: LedgerIdentity,
pub old_services: Vec<ServiceProof>,
}
pub struct Inputs<'a> {
pub original: &'a Original,
pub binding: &'a Binding,
pub clock: &'a NativeClock,
pub budget: &'a Budget,
pub updater: &'a StableParticipant,
pub retained: &'a RetainedSet,
pub files: &'a FileWitnesses,
pub trust: &'a TrustState,
}
/// Contains no borrow of the old guard: SQLite closes before actual flock
/// release, then the stopped-service value may be consumed by the start path.
pub struct PreparedReservation {
pub(crate) claims: PreparedReturnClaims,
pub(crate) proof: UpdateInflightProof,
pub(crate) ledger: LedgerIdentity,
pub(crate) old_services: Vec<ServiceProof>,
}
impl PreparedReservation {
pub fn bind(self, input: Inputs<'_>) -> Result<Reservation<'_>> {
Reservation::after_closed_connection(
Context {
original: input.original,
binding: input.binding,
clock: input.clock,
budget: input.budget,
updater: input.updater,
retained: input.retained,
files: input.files,
trust: input.trust,
ledger: self.ledger,
old_services: self.old_services,
},
self.claims,
self.proof,
)
}
}
pub struct Reservation<'a> {
context: Context<'a>,
claims: PreparedReturnClaims,
proof: UpdateInflightProof,
}
impl<'a> Reservation<'a> {
pub(crate) fn after_closed_connection(
context: Context<'a>,
claims: PreparedReturnClaims,
proof: UpdateInflightProof,
) -> Result<Self> {
let result = Self {
context,
claims,
proof,
};
result.check()?;
Ok(result)
}
fn check(&self) -> Result<()> {
self.context.original.check_current(
self.context.binding,
self.context.clock,
self.context.budget,
)?;
self.context
.updater
.verify_delegation(self.context.original.delegation(), self.context.budget)?;
self.context.retained.verify()?;
self.context.files.verify_pair(self.context.budget)?;
self.context
.trust
.recheck(self.context.original, self.context.clock)?;
let h = self.context.original.handoff();
self.context.files.bind(h)?;
ensure!(
self.context.retained.sha256() == h.retained_state_sha256
&& self.context.ledger.sha256()? == h.ledger_identity_sha256,
"UPDATE_RETURN_WITNESS_BINDING"
);
let (observed, journal) = rmm_domain::agent_update::verify_inflight_proof(
&self.proof,
self.context.trust.health_public_key(),
)
.map_err(anyhow::Error::msg)?;
ensure!(
observed.bytes()? == h.bytes()?
&& journal.phase() == rmm_updater_handoff::Phase::LedgerReturnIntent
&& self.claims.return_ledger.effect() == &NativeEffect::ReturnLedger,
"UPDATE_RETURN_RESERVATION_BINDING"
);
for (index, claim) in self.claims.starts.iter().enumerate() {
ensure!(
claim.effect()
== &NativeEffect::StartService {
service: [
NativeService::General,
NativeService::Full,
NativeService::Agent
][index]
},
"UPDATE_RETURN_START_ORDER"
);
}
for claim in std::iter::once(&self.claims.return_ledger).chain(self.claims.starts.iter()) {
ensure!(
claim.grant_id() == h.attempt.grant_id
&& claim.owner_nonce() == h.owner_nonce
&& claim.head_sha256() == self.proof.head_sha256
&& u64::try_from(claim.claimed_at().timestamp())? >= h.consumed_at
&& u64::try_from(claim.claimed_at().timestamp())? < h.finish_before,
"UPDATE_RETURN_CLAIM_CHANGED"
);
}
verify_identity_files(&self.context.ledger)?;
Ok(())
}
/// The OS release primitive consumes its real guard, and the borrow on
/// StoppedServices then ends. This fence never closes/unlocks another PID.
pub fn start_fence<'b>(
self,
new: &'b Systemd,
before_agent: &'b mut FullBridge<'a>,
) -> StartFence<'a, 'b> {
StartFence {
reservation: self,
new,
before_agent,
return_checked: false,
next: 0,
pending: None,
poisoned: false,
}
}
pub fn proof(&self) -> &UpdateInflightProof {
&self.proof
}
}
pub struct StartFence<'a, 'b> {
reservation: Reservation<'a>,
new: &'b Systemd,
before_agent: &'b mut FullBridge<'a>,
return_checked: bool,
next: usize,
pending: Option<Service>,
poisoned: bool,
}
impl StartFence<'_, '_> {
pub fn finish(self) -> Result<()> {
ensure!(
!self.poisoned && self.return_checked && self.next == 3 && self.pending.is_none(),
"UPDATE_START_SEQUENCE_INCOMPLETE"
);
Ok(())
}
}
impl DurableEffectFence for StartFence<'_, '_> {
fn before_effect(&mut self, effect: &Effect) -> Result<()> {
ensure!(
!self.poisoned && self.pending.is_none(),
"UPDATE_START_NO_RETRY"
);
self.poisoned = true;
self.reservation.check()?;
match *effect {
Effect::ReturnLedger => {
ensure!(
!self.return_checked && self.next == 0,
"UPDATE_RETURN_ALREADY_USED"
);
self.return_checked = true;
self.poisoned = false;
}
Effect::StartService { service } => {
ensure!(
self.return_checked
&& self.next < 3
&& service == [Service::General, Service::Full, Service::Agent][self.next],
"UPDATE_START_ORDER"
);
if self.next == 0 {
verify_lock_released(&self.reservation.context.ledger)?;
}
if service == Service::Agent {
// The new Full is now alive. A terminal/Unknown original
// or lost owner must deny the remaining Agent start too.
self.before_agent
.call(NativeControlOperation::ReadOriginal)?;
}
self.pending = Some(service);
}
_ => anyhow::bail!("UPDATE_RETURN_FENCE_EFFECT_DENIED"),
}
Ok(())
}
fn after_effect(&mut self, effect: &Effect, observation: &str) -> Result<()> {
let Some(service) = self.pending.take() else {
anyhow::bail!("UPDATE_START_NOT_RESERVED")
};
ensure!(
self.poisoned && *effect == Effect::StartService { service },
"UPDATE_START_CALLBACK_CHANGED"
);
self.reservation.check()?;
let held = self.new.observe(service, self.reservation.context.budget)?;
let prior = self
.reservation
.context
.old_services
.iter()
.find(|s| s.service == service)
.ok_or_else(|| anyhow::anyhow!("UPDATE_START_ORIGINAL_SERVICE_MISSING"))?;
ensure!(
held.proof().enabled == prior.enabled
&& !self
.reservation
.context
.old_services
.iter()
.any(|old| old.process.pid == held.proof().process.pid)
&& hash(&serde_json::to_vec(held.proof())?) == observation,
"UPDATE_START_NATIVE_OBSERVATION_CHANGED"
);
self.next += 1;
self.poisoned = false;
Ok(())
}
}
fn identity(meta: &std::fs::Metadata) -> Identity {
Identity {
device: meta.dev(),
inode: meta.ino(),
uid: meta.uid(),
gid: meta.gid(),
mode: meta.mode(),
links: meta.nlink(),
}
}
fn verify_identity_files(expected: &LedgerIdentity) -> Result<()> {
let dir = Directory::open(Path::new("/var/lib/rmm-helper"), 0, true)?;
for (name, expected) in [
("consumption.sqlite", &expected.ledger),
("executor.lock", &expected.lock),
] {
let file = OpenOptions::new()
.read(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC)
.open(Path::new("/var/lib/rmm-helper").join(name))?;
ensure!(
file.metadata()?.is_file() && identity(&file.metadata()?) == *expected,
"UPDATE_RETURN_FILE_IDENTITY_CHANGED"
);
}
dir.verify()
}
fn verify_lock_released(expected: &LedgerIdentity) -> Result<()> {
verify_identity_files(expected)?;
let lock = OpenOptions::new()
.read(true)
.write(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
.open("/var/lib/rmm-helper/executor.lock")?;
ensure!(
identity(&lock.metadata()?) == expected.lock,
"UPDATE_RETURN_LOCK_CHANGED"
);
ensure!(
unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0,
"UPDATE_OLD_GUARD_NOT_RELEASED"
);
ensure!(
unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_UN) } == 0,
"UPDATE_RETURN_PROBE_UNLOCK_FAILED"
);
verify_identity_files(expected)
}
+178 -13
View File
@@ -53,14 +53,26 @@ impl Protected {
} }
} }
pub struct TrustState { pub struct TrustState {
updater_binding: rmm_domain::agent_update::UpdaterBinding,
predecessor: rmm_domain::agent_update::Predecessor,
protected: Vec<Protected>, protected: Vec<Protected>,
policy: LocalUpdatePolicy, policy: LocalUpdatePolicy,
installed: InstalledReleaseRecord, installed: InstalledReleaseRecord,
installed_raw: Vec<u8>,
keyring: UpdateKeyring, keyring: UpdateKeyring,
keyring_file: UpdateKeyringFile,
baseline_signer_revoked: bool,
health_public_key: [u8; 32], health_public_key: [u8; 32],
} }
impl TrustState { impl TrustState {
pub fn load(original: &Original, binding: &Binding, clock: &NativeClock) -> Result<Self> { pub fn load(original: &Original, binding: &Binding, clock: &NativeClock) -> Result<Self> {
let result = Self::load_retained(original, binding)?;
result.recheck(original, clock)?;
Ok(result)
}
/// EXACT ORIGINAL evidence only. The separate historical keyring below
/// never supplies current admission, Health, Finalize or effect authority.
pub fn load_retained(original: &Original, binding: &Binding) -> Result<Self> {
let update = original.update(); let update = original.update();
let mut protected = Vec::new(); let mut protected = Vec::new();
let (file, raw) = Protected::read(Path::new("/etc/rmm-agent-updater/policy.json"), 65536)?; let (file, raw) = Protected::read(Path::new("/etc/rmm-agent-updater/policy.json"), 65536)?;
@@ -88,6 +100,7 @@ impl TrustState {
protected.push(file); protected.push(file);
let job_key = hex::decode(&binding.public_key_hex)?; let job_key = hex::decode(&binding.public_key_hex)?;
let mut trusted = Vec::new(); let mut trusted = Vec::new();
let mut historical_keys = Vec::new();
let mut baseline_key = None; let mut baseline_key = None;
for entry in &keys.keys { for entry in &keys.keys {
ensure!(entry.platform == Platform::LinuxX64, "UPDATE_KEY_PLATFORM"); ensure!(entry.platform == Platform::LinuxX64, "UPDATE_KEY_PLATFORM");
@@ -108,16 +121,23 @@ impl TrustState {
entry.last_sequence, entry.last_sequence,
entry.revoked, entry.revoked,
)?); )?);
historical_keys.push(TrustedUpdateKey::new(
public,
ReleasePlatform::LinuxX64,
entry.first_sequence,
entry.last_sequence,
false,
)?);
} }
let keyring = UpdateKeyring::new(keys.generation, trusted)?; let keyring = UpdateKeyring::new(keys.generation, trusted)?;
let historical = UpdateKeyring::new(keys.generation, historical_keys)?;
let baseline = policy let baseline = policy
.initial_baseline .initial_baseline
.decode() .decode()
.map_err(anyhow::Error::msg)?; .map_err(anyhow::Error::msg)?;
let (baseline_entry, public) = baseline_key.context("UPDATE_BASELINE_KEY_NOT_TRUSTED")?; let (baseline_entry, public) = baseline_key.context("UPDATE_BASELINE_KEY_NOT_TRUSTED")?;
ensure!( ensure!(
!baseline_entry.revoked baseline
&& baseline
.record .record
.release_envelope .release_envelope
.decode() .decode()
@@ -161,18 +181,23 @@ impl TrustState {
"UPDATE_INSTALLED_RECORD_CHANGED" "UPDATE_INSTALLED_RECORD_CHANGED"
); );
let installed = InstalledReleaseRecord::from_bytes(&raw).map_err(anyhow::Error::msg)?; let installed = InstalledReleaseRecord::from_bytes(&raw).map_err(anyhow::Error::msg)?;
// This first concrete activation is bootstrap-bound. A future Updated
// predecessor must have its complete guarded Health/record chain proved;
// never accept its mutable fields merely because parsing succeeded.
ensure!( ensure!(
matches!( !installed.rolled_back
&& installed.activation_floor >= policy.initial_activation_floor
&& installed.activated_at <= original.handoff().consumed_at,
"UPDATE_INSTALLED_FLOOR_OR_TIME"
);
if matches!(
installed.activation_evidence, installed.activation_evidence,
ActivationEvidence::Bootstrap { .. } ActivationEvidence::Bootstrap { .. }
) && raw == baseline.payload_bytes ) {
ensure!(
raw == baseline.payload_bytes
&& hash(&raw) == policy.initial_record_sha256 && hash(&raw) == policy.initial_record_sha256
&& installed.activation_floor == policy.initial_activation_floor, && installed.activation_floor == policy.initial_activation_floor,
"UPDATE_INSTALLED_CHAIN_NOT_QUALIFIED" "UPDATE_INSTALLED_BASELINE_CHANGED"
); );
}
let active = installed let active = installed
.release_envelope .release_envelope
.decode() .decode()
@@ -185,13 +210,13 @@ impl TrustState {
&& active.manifest.artifacts[1].sha256 == update.predecessor.helper_sha256, && active.manifest.artifacts[1].sha256 == update.predecessor.helper_sha256,
"UPDATE_INSTALLED_PREDECESSOR_BINDING" "UPDATE_INSTALLED_PREDECESSOR_BINDING"
); );
verify_installed_release(&keyring, &installed, keys.generation)?; verify_installed_release(&historical, &installed, keys.generation)?;
verify( verify(
&keyring, &historical,
&update.release_envelope, &update.release_envelope,
&TrustedContext { &TrustedContext {
platform: ReleasePlatform::LinuxX64, platform: ReleasePlatform::LinuxX64,
now_utc_seconds: u64::try_from(clock.now()?.timestamp())?, now_utc_seconds: u64::try_from(original.delegation().prepared_at.timestamp())?,
keyring_generation: keys.generation, keyring_generation: keys.generation,
protocol_major: active.manifest.protocol_major, protocol_major: active.manifest.protocol_major,
state_schema: active.manifest.state_schema, state_schema: active.manifest.state_schema,
@@ -204,18 +229,35 @@ impl TrustState {
}, },
)?; )?;
protected.push(file); protected.push(file);
let baseline_signer_revoked = baseline_entry.revoked;
Ok(Self { Ok(Self {
updater_binding: update.updater.clone(),
predecessor: update.predecessor.clone(),
protected, protected,
policy, policy,
installed, installed,
installed_raw: raw,
keyring, keyring,
keyring_file: keys,
baseline_signer_revoked,
health_public_key, health_public_key,
}) })
} }
pub fn recheck(&self, original: &Original, clock: &NativeClock) -> Result<()> { pub fn recheck(&self, original: &Original, clock: &NativeClock) -> Result<()> {
for file in &self.protected { self.recheck_original_binding(original)?;
file.verify()?; self.recheck_protected()?;
self.recheck_crypto(original, clock)
} }
fn recheck_crypto(&self, original: &Original, clock: &NativeClock) -> Result<()> {
ensure!(
!self.baseline_signer_revoked,
"UPDATE_BASELINE_SIGNER_REVOKED"
);
verify_installed_release(
&self.keyring,
&self.installed,
original.update().updater.keyring_generation,
)?;
let active = self let active = self
.installed .installed
.release_envelope .release_envelope
@@ -239,6 +281,129 @@ impl TrustState {
}, },
) )
} }
pub fn installed_raw(&self) -> &[u8] {
&self.installed_raw
}
/// Exact typed active-record namespace only. Every other protected name
/// remains mandatory; neither caller booleans nor path wildcards can skip it.
pub fn recheck_record_namespace(
&self,
original: &Original,
clock: &NativeClock,
namespace: &crate::record::RecordNamespaceProof<'_>,
) -> Result<()> {
self.recheck_original_binding(original)?;
self.recheck_protected_record_namespace(original, namespace)?;
self.recheck_crypto(original, clock)
}
pub fn recheck_protected_record_namespace(
&self,
original: &Original,
namespace: &crate::record::RecordNamespaceProof<'_>,
) -> Result<()> {
self.recheck_original_binding(original)?;
namespace.verify()?;
ensure!(
namespace.grant_id() == original.permit().grant_id
&& namespace.owner_nonce() == original.handoff().owner_nonce
&& namespace.old_sha256() == original.handoff().predecessor_record_sha256
&& namespace.old_sha256() == hash(&self.installed_raw),
"UPDATE_RECORD_NAMESPACE_ORIGINAL_BINDING"
);
let mut matched = 0;
for protected in &self.protected {
if protected.name == "installed-record.json"
&& protected.dir.identity() == namespace.directory_identity()
{
ensure!(
protected.file.proof() == namespace.old_file(),
"UPDATE_RECORD_ORIGINAL_HELD_IDENTITY_CHANGED"
);
matched += 1;
} else {
protected.verify()?;
}
}
ensure!(matched == 1, "UPDATE_RECORD_EXACT_PROTECTED_NAME_REQUIRED");
namespace.verify()
}
pub fn recheck_protected(&self) -> Result<()> {
for file in &self.protected {
file.verify()?;
}
Ok(())
}
pub fn recheck_original_binding(&self, original: &Original) -> Result<()> {
ensure!(
original.update().updater == self.updater_binding
&& original.update().predecessor == self.predecessor,
"UPDATE_TRUST_ORIGINAL_CONTEXT_CHANGED"
);
Ok(())
}
/// Full admission/Stop claims call this under THEIR actual guard. The
/// updater repeats it after all readers stop and before the first swap.
/// Never gate Unknown-only adjudication on this admission-oriented check.
pub fn qualify_history(
&self,
db: &rusqlite::Connection,
guard: &impl rmm_agent_core::helper_reconcile::ExecutorLedgerGuard,
binding: &Binding,
) -> Result<()> {
self.recheck_protected()?;
let baseline = self
.policy
.initial_baseline
.decode()
.map_err(anyhow::Error::msg)?;
let baseline_hash = hash(&baseline.payload_bytes);
let mut records = Vec::new();
let mut held = Vec::new();
let mut seen = std::collections::BTreeSet::new();
let mut next = self.installed.previous_record_sha256.clone();
let mut total = self.installed_raw.len() + baseline.payload_bytes.len();
while let Some(digest) = next {
ensure!(
rmm_domain::agent_update::digest(&digest)
&& seen.insert(digest.clone())
&& seen.len() < 64,
"UPDATE_RECORD_ARCHIVE_CHAIN"
);
if digest == baseline_hash {
break;
}
let (file, raw) = Protected::read(
&Path::new("/var/lib/rmm-agent-updater/records").join(format!("{digest}.json")),
32768,
)?;
ensure!(hash(&raw) == digest, "UPDATE_RECORD_ARCHIVE_HASH_CHANGED");
total = total
.checked_add(raw.len())
.context("UPDATE_RECORD_ARCHIVE_SIZE")?;
ensure!(total <= 2 * 1024 * 1024, "UPDATE_RECORD_ARCHIVE_SIZE");
let record = InstalledReleaseRecord::from_bytes(&raw).map_err(anyhow::Error::msg)?;
next = record.previous_record_sha256.clone();
records.push(raw);
held.push(file);
}
let record = crate::history::verify_history(
db,
guard,
binding,
&self.keyring_file,
&self.policy.initial_baseline,
&records,
&self.installed_raw,
)?;
ensure!(
record.bytes().map_err(anyhow::Error::msg)? == self.installed_raw,
"UPDATE_HISTORY_ACTIVE_CHANGED"
);
for file in &held {
file.verify()?;
}
self.recheck_protected()
}
pub fn policy(&self) -> &LocalUpdatePolicy { pub fn policy(&self) -> &LocalUpdatePolicy {
&self.policy &self.policy
} }
+117
View File
@@ -0,0 +1,117 @@
# Erhaltener Inventarbeleg und Agent-Healthclient
2026-11-14, Spezifikation1.21. Dieser enge Quellenschnitt ergänzt den bestehenden
Agentjournal- und SignedSyncvertrag. Er installiert oder startet keinen Dienst,
erhebt selbst kein Inventar und erzeugt weder Job, Grant, Bereitschaft noch
Helperwirkung. Registrierung und Aufruf im bestehenden Agent-/Corepfad werden
vom Laufzeitowner getrennt integriert; ein fertiger nativer Healthlauf ist damit
nicht behauptet.
`update_health_inventory` erhält im selben gesperrten, begrenzten Agentjournal
höchstens zwei ursprüngliche Baseinventar-Envelopes je Originalgrant. Die
Retention prüft den vollständigen Inflightpräfix gegen den aus dem unabhängig
verifizierten ursprünglichen Updatejob stammenden Healthkey. Ein beliebiger
Aufrufer-/Netzwerkkey ist keine Provisionierung. Erforderlich sind
`ServicesStarted`, das letzte echte Dienststartereignis, passende Geräte-/Boot-/
Journalbindung und eine neue Verbindungsgeneration. Plattformscope und echte
Erhebungszeit bleiben erhalten; ein `agent.update`-Paket oder ein vor Dienststart
erhobenes Baseinventar ersetzt diesen Nachweis nicht. Kein pauschaler60s-CIMscan
und kein Frischstempeln bereits gespeicherter Basewerte werden eingeführt.
Gewöhnliche Transport-ACKs löschen weiterhin ihren normalen Queueeintrag,
aber keine Retentionbytes. Die ursprüngliche UUID bleibt zusätzlich für den
echten SignedSync ausstehend. Nur dessen interner Applyhook kann sie als signiert
bestätigt markieren: nach normaler unabhängiger Signatur-/Bindungsprüfung und
unter Vergleich der bereits gespeicherten Originalresponse und des normalen
konkreten `reconcile_receipts`-Datensatzes. Selbst danach bleibt das rohe Envelope
erhalten. Keine Caller-ACKliste, `applied=true`-Behauptung, neue Applyzeit oder
Tombstonelöschung ersetzt diese Zeugen. Das vorhandene Journalbudget bleibt
verbindlich; ein dritter Retentionssample oder geänderte ursprüngliche Bytes
werden verweigert.
Der HTTPclient konstruiert seinen nicht öffentlich befüllbaren Kontext nur aus
dem lokalen laufenden Originaljob und zwei bereits gespeicherten tatsächlich
signierten Kontrollrunden. Er prüft beide mit dem normalen Coreverifier. Die
aktuelle bestätigende Runde muss in Agent- und Helper-Vorgängerlink genau die
erste Entscheidung nennen; beide müssen denselben vollständigen
`ServicesStarted`-Präfix bei `ready=false` bestätigen. Ein gerade offener nächster
Request, Fault, fehlender ACK, anderes Boot/Grant/Journal oder veraltete
Kontrollrunde erzeugt keinen Healthrequest. Ein fehlender Zeuge bleibt fehlend.
Das tatsächlich in der ersten Entscheidung bestätigte, separat erhaltene rohe
Baseinventar wird mit den beiden ursprünglichen Requestbytes an die feste Route
`/agent/v1/update-health` geschickt. Der Aufruf benutzt ausschließlich den bereits
normal verifizierenden mTLS-Client aus `https_channel`, dessen geprüften DNSnamen
und TCP-Override; keine freie URL, Bearerweitergabe, Zertifikatsausnahme oder
Redirectfreigabe. Request maximal5MiB, Response maximal16KiB, Healthpayload
maximal4096Bytes und feste64-Byte-Signatur. Timeout und monotone Nachprüfung
begrenzen den Aufruf auf die verbleibende ursprüngliche180s-/600s-Frist und
beide noch gültigen normalen Syncentscheidungen. Es gibt keinen automatischen
Retry.
Vor Rückgabe prüft der Client konkrete Inventar-/Sync-IDs und Originalhashes
und lässt den unveränderten Handoffkernel die separate Healthsignatur,
Dienstprozesse, Owner-/Boot-/Journal-/Codebindung und Fristen prüfen.
`Journal::prepare` ist dabei ausschließlich eine Prüfung; es appendet nichts
und schließt keinen Versuch ab. Tatsächliche lokale Helper-Applyzeit, Dateipaar,
Prozesse, Finalisierung und InstalledRecord/Floor bleiben bei den unabhängigen
lokalen Guards. Der Serverbeleg behauptet keine entfernte OS-Attestierung.
Tatsächlich lokal ausgeführt: registrierter Agent-/Corecheck mit `--locked
--offline`, Core-Clippy für alle Targets mit `-D warnings` und zwei gezielte
Core-Retentiontests bestanden. Der erste benutzt den tatsächlichen normalen
`jobs::ack`: Queueeintrag verschwindet, Originalraw und SignedSync-Pending-ID
bleiben. Der interne SignedSync-Hook verweigert fehlende gespeicherte Response
und fehlenden Receipt; nach beiden bleibt der Rawbeleg erhalten. Diese internen
Hookfixturebytes haben ausdrücklich synthetische Signaturen und qualifizieren
keinen Kryptografiepfad. Der zweite Fall prüft Create-once, zwei Samples,
Fremdgrant und veränderte Rawbytes.
Der erste `--locked`-Versuch lief vor der koordinierten Lockfortschreibung noch
nicht bis zum Compile. Der erste erreichte Agentcompile fand ein falsches
`Option<Payload>`-Pattern; nach Korrektur bestand der Check. Der Client ist
registriert, sein tatsächlicher Mainaufruf wird noch separat integriert;
entsprechende unbenutzte Funktionen bleiben offen ausgewiesene Compilerwarnungen
und werden nicht unterdrückt. Unabhängiger Quellenreview und vollständiger
Agent-Clippy nach tatsächlicher Verdrahtung bleiben offen. Keine native VM-/
Netzwerk-/Applyprüfung oder Zieländerung für diesen Teil erfolgt.
## Spätere Ownerintegration
Der nachfolgende Runtimeowner hat Registrierung und echte Agent-/Corehooks
inzwischen ergänzt. Der Ablauf läuft asynchron neben Heartbeats, behält echte
Basebytes/UUIDs und nimmt zusätzliche IDs nur für den verifizierten Original-
Inflight in normale SignedSync auf. Nach erfolgreichem HTTP wird ausschließlich
der erhaltene SignedHealth an den tatsächlich gebundenen neuen Fullhelper
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.
+81
View File
@@ -60,3 +60,84 @@ WebUI-genehmigter Linux-Apply sowie dessen unabhängiger Health-/Erhaltungsnachw
Restore/Rollback bleibt deny, weil der unveränderte Vorwärtskernel noch keinen Restore/Rollback bleibt deny, weil der unveränderte Vorwärtskernel noch keinen
qualifizierten Rückweg besitzt. Windowsapply ist separat offen. Kein Zielhost qualifizierten Rückweg besitzt. Windowsapply ist separat offen. Kein Zielhost
wurde durch diesen lokalen Checkpoint installiert, gestoppt oder aktualisiert. wurde durch diesen lokalen Checkpoint installiert, gestoppt oder aktualisiert.
## Fortschreibung: zweiter lokaler Quellencheckpoint
Die nächste Teilstrecke verbindet jetzt erhaltene Stage-/Statezeugen, den
gebundenen Full-Controlkanal, echte Dienst-/Prozessprüfungen, lokale
SignedSync-Applyzeugen, Inventarretention und den asynchronen Agent-Healthclient.
Full ist der einzige ursprüngliche Updateautorizer; General kann einen
Originalhandoff nicht aufgrund fehlender Zuständigkeit zu Unknown herabstufen.
Lesender historischer Replay bleibt von aktueller Wirkungsautorität getrennt:
auch nach verlorener Frist/Owner soll Unsicherheit unter demselben erhaltenen
Guard adjudiziert werden können. Sämtliche Forwardeffekte prüfen zusätzlich
aktuelle ursprüngliche Clock-/Boot-/Release-/Revocationbedingungen.
Die feste Fullbridge prüft die tatsächlichen PID-/Birth-/Image-/Unitzeugen.
Dienststartergebnisse müssen exakt den weiterhin gehaltenen einmaligen
Workerresultaten entsprechen. Nach ServicesStarted müssen gerade diese neuen
Agent-/General-/Fullgenerationen weiterleben; Ersatz-PIDs sind kein Erfolg.
Health vergleicht die separat signierten Originalserverbytes mit den lokal in
derselben echten Helper-Sync-Transaktion gespeicherten Request-/Responsehashes
und Applyzeit sowie dem behaltenen Originalbaseinventar und dessen echtem
SignedSync-ACK. Der vorhandene normale Transport-ACK löscht diese Belege nicht.
Der Agent lässt ausschließlich den signatur-/consumed-/start-verifizierten
ursprünglichen Updatekandidaten ohne vorhandenes Outcome beim eigenen Startup
auf tatsächliche unabhängige Helperkontrolle warten. Dies ist kein laufender
Handoffbeweis, keine Ausführung und keine Bereitschaft. Ein bereits vorhandenes
Unknown wird nicht geheilt. Der Kandidat kann nur neue negative Kontrollrunden
planen, damit die normale busy-Sperre seinen ersten Wiederkontakt nicht blockiert.
Ein erwarteter letzter Entscheid der vorherigen Verbindungsgeneration ist beim
Reconnect kein aktueller Healthbeleg. Während eines begrenzten Health-HTTPs
pausieren weitere Kontrollrunden; Heartbeats laufen weiter. Ein tatsächlich neuer
normaler Basesample gehört zur genehmigten Update-Healthstrecke, alte Daten
werden nicht frischgestempelt. Zertifikatsrotation wartet auf das Ende dieses
konkret bekannten Inflightfalls.
Der konkrete Full-Finalizepfad baut Recordbytes deterministisch aus dem
Originalrelease, altem Record und ursprünglicher SignedHealth. Zwei getrennte
native Intents/Nachlese-CAS sichern Save und Publish. Ein temporärer lokaler
Namespaceguard vergleicht exakt die alte gehaltene Datei/Rootdiridentität mit
dem nicht öffentlich konstruierbaren Recordportbeleg; alle übrigen geschützten
Namen bleiben Pflicht. Floor und aktive Recordmetadaten werden zusammen als
ein neuer Record publiziert, alte/neue Rawrecords zusätzlich create-only
aufbewahrt. Erst danach folgt das ursprüngliche Outcome-CAS; kein Resultunlock
oder allgemeiner Ready-/Startfence entsteht daraus.
Tatsächlich lokal im isolierten Offlinecontainer ausgeführt:
- Agent60, Core67, privilegierter Helper68 und Worker5: insgesamt200bestanden,
0Fehler,0ignoriert,0gefiltert im vollständigen betroffenen Release-Testlauf.
- Gemeinsame all-targets Clippyprüfung mit -D warnings und Formatcheck: PASS.
- Die zwei Record-FS-Fälle verwenden ausschließlich neue eigene Rootdateien
unter privatem Container-/run; keine tatsächlichen Projekt- oder Zieldienste.
Belege: `devstate/updater-runtime-checkpoint2-69dc0bb5-2e9f-4639-9212-e878b32eadc6/`.
Ein vorheriger breiter Lauf hing in der HTTPS-Reconnectprobe und wurde als eigener
Container beendet (Exit137, kein PASS). Nach Eingrenzung des Altgenerationsfalls
bestand die gezielte tatsächliche mTLS-HTTPSprobe1/1; anschließend bestand der
vollständige200er-Lauf. Zwei reine Clippy-Strukturfehler (Testmodulposition und
verschachteltes if) wurden ohne Prüfungsunterdrückung korrigiert. Das erste
Clippyfehlerlog bleibt im Belegverzeichnis; Hänger/Abbruch, gezielte1er-Probe und
späterer Stilhinweis sind im ursprünglichen Werkzeugprotokoll erhalten.
Diese lokalen Ergebnisse qualifizieren keinen tatsächlichen Full-/Updater-
Dienstkontext. Der öffentliche Recordbuilder hat hier noch keinen eigenständigen
echt signierten Positivnachweis; die FS-Fälle umgehen seinen authentisierten
Eingang ausdrücklich. Die vollständige Engine/Daemon-/StopFence-/Stagingproducer-
und ursprüngliche Admissionstrecke, qualifizierte passive Kandidatenbeobachtung,
geschlossene Readercensus, unklare Record-I/O-Namespace-Adjudikation und der vor
der ersten Baseline notwendige V2-Rückweg bleiben offen. Applyfähigkeit bleibt
unveröffentlicht; kein Cloud-VM-Apply, kein Produktfertig- oder Produktionsnachweis.
## Nachtrag2026-11-15: gesperrte Worker-Drivefolge
Nach dem gepushten gemeinsamen Checkpoint8e93c9e und dem erhaltenden Mainmerge
07d6118 ist die bisher offene Enginefolge als konkrete Workerquelle verbunden.
Ihr Eintritt bleibt explizit `UPDATE_READER_CENSUS_NOT_CONFIGURED`; weder
Daemonentrypoint noch Job-/Applycapability wird aktiviert. Die genaue
Queue-/Reply-/Stop-/Lock-/Swap-/Start-/Healthfolge und vorhandene separate
Agent-Singletonsperre stehen in [worker](agent-updater-worker.md). Der neu
geprüfte öffentliche Recordbuilder löst die zuvor dort genannte fehlende
positive authentisierte Eingangsprobe. Keine reale Applyabnahme folgt daraus.
+103 -8
View File
@@ -40,9 +40,13 @@ General-/Full-/Updater-Dienstkontexte sind noch nativ zu qualifizieren.
und InstalledRecord-Dateien. Originale rohe Hashes bleiben maßgeblich. Die und InstalledRecord-Dateien. Originale rohe Hashes bleiben maßgeblich. Die
initiale Baseline benötigt ihre gesonderte Ed25519-Domänensignatur; aktuelle initiale Baseline benötigt ihre gesonderte Ed25519-Domänensignatur; aktuelle
Schlüsselwiderrufe/-bereiche und Releaseverifikation werden unabhängig geprüft. Schlüsselwiderrufe/-bereiche und Releaseverifikation werden unabhängig geprüft.
Health-/Release-/Jobkeys werden getrennt geprüft. Die erste Aktivierung bleibt Health-/Release-/Jobkeys werden getrennt geprüft. Historische read-only Lader
an den separat signierten Bootstraprecord gebunden. Ein `Updated`-Vorgänger sind von aktueller Candidate-/Clock-/Boot-/Revocationprüfung getrennt und
wird bis zur vollständigen Record-/Health-Historyprüfung ausdrücklich verweigert. erteilen keine Wirkungserlaubnis. `qualify_history` lädt kanonische Rawrecords
am festen geschützten `records/<sha>.json`-Pfad, begrenzt auf64Records/2MiB,
und prüft die vollständige Originalledger-/Baseline-/Health-/Outcome-Kette.
Der originale Fullguard muss dies vor Verbrauch/Stopclaims tun; der Updater
wiederholt es unter dem tatsächlich übernommenen Guard vor dem ersten Swap.
Keine Floorableitung aus einem bloß geparsten Record, kein Releaseprivatkey Keine Floorableitung aus einem bloß geparsten Record, kein Releaseprivatkey
auf dem Client. Die getrennte TLS-Keyprovisionierung bleibt ein Bootstrapgate. auf dem Client. Die getrennte TLS-Keyprovisionierung bleibt ein Bootstrapgate.
@@ -63,15 +67,32 @@ SavePredecessor und PublishReplacement erhalten getrennte dauerhafte native
Intents und Nachlese. Ein fehlgeschlagener/mehrdeutiger Schritt vergiftet den Intents und Nachlese. Ein fehlgeschlagener/mehrdeutiger Schritt vergiftet den
laufenden Fence; keine Resume-/Retrykonstruktion aus einem Teilintent. laufenden Fence; keine Resume-/Retrykonstruktion aus einem Teilintent.
Hinzugekommen sind eine konkrete streng gebundene Full-Control-Bridge auf dem
bestehenden festen Maintenancesocket und kanonische wiederöffnungsfähige
Witnessbundles. Die Bridge prüft tatsächlichen Fullpeer/PID/Geburt/Image,
Challenge, Originaltupel und monotonen vollständigen Präfix; Request32KiB,
Reply1536KiB sowie gesamte IOfrist sind begrenzt. Ein verlorener mutierender
Reply berechtigt keinen Retry. Header und ursprüngliche Dateistamps bleiben
create-only unter dem Originalgrant geschützt; echte Nachlese bleibt getrennt.
`prepare_return_and_close` persistiert Returnintent plus genau drei Startclaims
atomar und schließt SQLite vor der wirklichen Guardabgabe. Das nichtklonbare
Rückgabeticket enthält keinen Borrow des alten Guards. Der Startfence prüft
freigegebenen Lock, vollständiges Paar und General/Full/Agent genau einmal;
vor Agentstart wird der neue Full erneut lesend zum Originalversuch befragt.
Native Startresultate werden als tatsächliche gehaltene Prozesse vor/nach der
Fullquittung nachgeprüft. Dieser Anschluss ist noch kein gestarteter Enginepfad.
## Offen und standardmäßig verweigert ## Offen und standardmäßig verweigert
Vollständige Engine/Daemonhülle und originale aktivierungsgebundene Stageaufnahme, Vollständige Engine/Daemonhülle und originale aktivierungsgebundene Stageaufnahme,
Full-Control-Bridge vor Stop, drei Stopbeobachtungen vor AllStopped, atomare Stopfence/Drive-Schleife außerhalb des alten synchronen Helpercalls,
Rückgabe-/Startreservation, tatsächliche Guardabgabe vor einmaligen Starts, drei Stopbeobachtungen vor AllStopped und die vollständige Runtimeverdrahtung
neuer Full-Observer und tatsächlicher Health-/Outcomeanschluss sind noch offen. von Guardabgabe/Starts/Full-Observer/Health/Outcome sind noch offen.
Floor-/InstalledRecordpublikation braucht zusätzlich die geschlossenen Floor-/InstalledRecordpublikation braucht zusätzlich die geschlossenen
originalen nativen Intents nach Health und erhaltende Record-CAS. Folgeupdates originalen nativen Intents nach Health und erhaltende Record-CAS. Die History-
brauchen vollständige geschützte Record-/Health-Historyverifikation. Prüfung ist Quell-/Unitfunktion; ihr tatsächlicher Runtime-/Archivanschluss muss
vor Folgeupdates separat vollständig nachgewiesen werden.
Der eingefrorene Vorwärtskernel hat noch keinen qualifizierten Rollbackübergang. Der eingefrorene Vorwärtskernel hat noch keinen qualifizierten Rollbackübergang.
`RestorePredecessor` bleibt deshalb auch bei vorab gebundener Recoveryoption `RestorePredecessor` bleibt deshalb auch bei vorab gebundener Recoveryoption
@@ -105,7 +126,81 @@ derselben privaten Quellkopie. Kein Frozenverifier wurde dafür verändert.
Privater Nachweis: Privater Nachweis:
`devstate/agent-updater-worker/baseline-fix-1fcc8bb2-57fe-4425-b166-f416dc1b4703/`. `devstate/agent-updater-worker/baseline-fix-1fcc8bb2-57fe-4425-b166-f416dc1b4703/`.
Neuer Übergabecheckpoint nach Bridge/Bundle/Returnticket, historischem
Unknown-only-Split und Historyreviewfixes: private eingefrorene Quellkopie
`devstate/agent-updater-worker/handoff-43ade361-9726-4fb4-8e97-cae19c22c6d0/`.
Release **3/3**, keine Skips/Filter, Clippy `-D warnings` und Formatcheck
tatsächlich bestanden. Fälle: signierter Baseline-/Floor-Gegenbeleg, wirklicher
State-Inode-/Originalstamp-Reopen und vollständig signierte History inklusive
konkreter Tamper-/Rollback-/Fork-/Claim-/Completionfehler bei unveränderten
SQLite-Änderungszählern. Receipt-SHA256:
`30425acbffaa05578fd17ee25a2e3f42976fe4237423e874635c49aa0fcb4990`.
Workerquellsatz:
`793efb917979ca59e95cdf0ff01d75b16c75a79d8392bc557f0864728d558cf1`.
Die erste private Kopie fehlte um die `include_str`-Goldenfixture; der daraus
resultierende Compilerfehler bleibt mit seinen Originallogs erhalten. Nach
Ergänzung exakt derselben Repo-Fixture wurden die genannten Checks ausgeführt.
Private Verzeichnisse700/Dateien600/qualifizierte Exe500 sind explizit gesetzt.
Keine Engine gestartet, kein systemd-Aufruf, keine echte Zieldatei geändert, Keine Engine gestartet, kein systemd-Aufruf, keine echte Zieldatei geändert,
keine VM/SSH-/Job-/WebUI-Aktion und kein nativer Worker-/Applynachweis. keine VM/SSH-/Job-/WebUI-Aktion und kein nativer Worker-/Applynachweis.
Der native Linux-OS5-Nachweis qualifiziert weiterhin dessen eigene eingefrorene Der native Linux-OS5-Nachweis qualifiziert weiterhin dessen eigene eingefrorene
Primitiven und ersetzt keine Worker-/Dienstkontextabnahme. Primitiven und ersetzt keine Worker-/Dienstkontextabnahme.
## Anschließender gemeinsamer Runtimeanschluss
Die historische3er-Qualifikation oben bleibt unverändert. Danach wurden die
feste Recordtransition und der konkrete Full-Namespace-/Fenceadapter verbunden;
der gemeinsame lokale200er-Lauf enthält5Workerfälle. Die2zusätzlichen Root-/run-
FS-Fälle beweisen getrennte echte Recordrenames/Originalinodeerhalt und Abweisung
von namensgleichen Ersatzdateien/Currentkollisionen ohne Resume. Sie qualifizieren
keine echte UI-/Job-/Service-/Rootguardwirkung und umgehen ausdrücklich den
öffentlichen authentisierten BuiltRecordeingang. Aktueller Source-/Nachweisstand
und restliche Engine-/Admission-/Rollback-/Nativegates:
[Runtimefortschreibung](agent-updater-runtime-source.md).
## Einmalige Queue-/Drivefolge und vorhandene Agent-Singletonsperre
Am2026-11-15 liegt `worker::engine::Runtime` als noch gesperrter Quellschnitt
vor. Der vorhandene IPC-Activatehandler queuet ausschließlich den originalen
Handoff; die private Drivefolge ist erst nach vollständig geschriebenem Reply
und geschlossener Accepted-Verbindung erreichbar. Eine verlorene Antwort
verwirft die Queue und sperrt dieselbe Runtime. Die erste Fullbridge-Lesung
übernimmt den tatsächlich authentisierten Originalpräfix; vorher sind alle
mutierenden Methoden gesperrt und kein Event wird aus Headerdaten erfunden.
Die Folge verbindet Originalsignatur-/Policy-/Floor-/Dateibeobachtungen, alle
drei Full-Stopclaims vor irgendeinem Stop, die tatsächlichen Stopbeobachtungen
im Originalledger nach Guardübernahme, AllStopped/Owns, vier getrennte
Save-/Publishpaare, Pair, Return-/Startreservation, tatsächlichen DBclose und
flockdrop, General→Full→Agent und neue Full-Start-/Health-/Recordbestätigung.
Es gibt keinen Resume oder Restore. Ein Fehler bei den Fileübergängen versucht
unter dem noch gehaltenen Originalguard genau einen Unknown-CAS und startet
keinen Dienst neu. Native Dienst-/Fehlerfolgen sind damit noch nicht bewiesen.
Zusätzlich wird die vorhandene `/var/lib/rmm-agent-remote/agent.lock` aus dem
OriginalRetainedSet gebunden: unveränderter Inode, UID993/GID984/0600/nlink1,
0Bytes und privater Parent. Nach echtem StoppedServices-Beleg nimmt der Worker
diese vorhandene Sperre exklusiv ohne CREATE. Sie bleibt über Root-DB/Swap/
DBclose/Rootlockreturn gehalten und wird vor der festen Startfolge zurückgegeben.
Der Agent hält schon heute seine ursprüngliche Singletondatei vor beiden
Agent-SQLiteöffnungen; keine ACL, Agentdatei oder Sperrrolle wurde geändert.
**Explizite Sperre dieses Checkpoints:** `Runtime::serve_one` verweigert mit
`UPDATE_READER_CENSUS_NOT_CONFIGURED`. Geschützte kompatible Bootstrapregistry
plus vollständige aktuelle PID-/FD-/maps-Census und Nachstopbeobachtung unter
beiden tatsächlichen Sperren fehlen noch. Eine bekannte Unitliste ersetzt
keinen Census. Keine CLI oder RMM-Admission ruft diese Runtime auf; Applycaps
bleiben geschlossen. Ebenso offen: Staging-/Downloadproducer, passiver gültiger
Updatezustand, V2-Rollbackvertrag vor dem ersten Bootstrap, Unsicherheits-
Namespaces, qualifizierte Dienstkontexte und tatsächlicher Linux-Apply.
Lokaler Nachweis liegt unter
`devstate/updater-engine-source-6a8bb902-4db4-4d93-8843-450ba1dd5622/`.
Der7er-Workerlauf umfasst jetzt auch den **öffentlichen** BuiltRecordbuilder:
Originalsignierter Job und echte vollständig signierte History ergeben exakt
die erwarteten Rawrecordbytes. Ein für einen fremden externen Healthkey gültiger
Journal bleibt unter dem Originaljobkey unzulässig; geänderter Head/prior wird
verweigert. Ein separater echter Container-FS-Fall belegt die Agent-Singleton-
Aliasblockade und Ablehnung eines gleichbyteigen Inodeersatzes. Dies sind lokale
Krypto-/FS-Verträge; sie führen die Queue-/Drive-/Dienstfolge nicht aus.