Wire preserved update control, health evidence and record publication
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
Generated
+9
@@ -2176,9 +2176,11 @@ name = "rmm-agent"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.22.1",
|
||||
"chrono",
|
||||
"fs2",
|
||||
"futures-util",
|
||||
"hex",
|
||||
"libc",
|
||||
"openssl",
|
||||
"prost",
|
||||
@@ -2189,6 +2191,7 @@ dependencies = [
|
||||
"rmm-domain",
|
||||
"rmm-privileged-helper",
|
||||
"rmm-protocol",
|
||||
"rmm-updater-handoff",
|
||||
"rusqlite",
|
||||
"rustls",
|
||||
"rustls-pemfile",
|
||||
@@ -2312,14 +2315,20 @@ name = "rmm-privileged-helper"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.22.1",
|
||||
"chrono",
|
||||
"fs2",
|
||||
"hex",
|
||||
"libc",
|
||||
"openssl",
|
||||
"prost",
|
||||
"rmm-agent-core",
|
||||
"rmm-agent-linux",
|
||||
"rmm-domain",
|
||||
"rmm-protocol",
|
||||
"rmm-updater-handoff",
|
||||
"rmm-updater-linux",
|
||||
"rmm-updater-worker",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
||||
@@ -791,6 +791,48 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
||||
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)?;
|
||||
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])?;
|
||||
@@ -1291,11 +1333,22 @@ fn ledger_fingerprint(db: &Connection) -> Result<String> {
|
||||
Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?))
|
||||
})?
|
||||
.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(&(
|
||||
rows,
|
||||
updates,
|
||||
claims,
|
||||
observations,
|
||||
applied,
|
||||
))?));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -394,3 +394,27 @@ pub fn permit_request(offer: &JobOffer, permit: &ExecutionPermit) -> Result<Requ
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -9,3 +9,4 @@ pub mod jobs;
|
||||
pub mod ledger;
|
||||
pub mod reconcile;
|
||||
pub mod update_handoff;
|
||||
pub mod update_health_inventory;
|
||||
|
||||
@@ -306,7 +306,13 @@ impl<V: Ed25519Verifier> ClientSync<V> {
|
||||
merge_helper(&mut attempts, h)?;
|
||||
}
|
||||
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");
|
||||
let next: i64 = tx.query_row(
|
||||
"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",
|
||||
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",
|
||||
params![decision.decision_id.to_string(),decision.helper_journal_id.map(|id|id.to_string()),decision.helper_checkpoint,fault,seq])?;
|
||||
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_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));")?;
|
||||
crate::update_health_inventory::initialize(db)?;
|
||||
db.execute(
|
||||
"INSERT OR IGNORE INTO reconcile_meta(id,journal_id) VALUES(1,?1)",
|
||||
[Uuid::new_v4().to_string()],
|
||||
|
||||
@@ -33,6 +33,8 @@ pub trait UpdateAdmissionGuard: ExecutorLedgerGuard {
|
||||
/// Role comes from an authenticated concrete local participant, not IPC data.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Participant {
|
||||
/// Authenticated Full reader; never a native effect participant.
|
||||
Observer,
|
||||
OriginalHelper,
|
||||
StableUpdater,
|
||||
NewHelper,
|
||||
@@ -64,6 +66,11 @@ pub trait UpdateParticipantGuard: ExecutorLedgerGuard {
|
||||
/// executor guard. This does not alter old schema objects, rows or file IDs.
|
||||
pub fn initialize(db: &Connection, guard: &impl ExecutorLedgerGuard) -> Result<()> {
|
||||
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(
|
||||
"CREATE TABLE IF NOT EXISTS agent_update_handoffs(
|
||||
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(
|
||||
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),
|
||||
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(())
|
||||
}
|
||||
@@ -1060,10 +1070,14 @@ pub enum NativeControlOperation {
|
||||
expected_head_sha256: String,
|
||||
service: NativeService,
|
||||
},
|
||||
ObserveStarts,
|
||||
ObserveStarts {
|
||||
services: Vec<rmm_updater_handoff::StartedService>,
|
||||
observation_sha256: String,
|
||||
},
|
||||
Health {
|
||||
receipt: rmm_updater_handoff::SignedHealth,
|
||||
},
|
||||
Finalize,
|
||||
}
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
@@ -1079,7 +1093,8 @@ pub struct NativeControlReply {
|
||||
pub owner_nonce: Uuid,
|
||||
pub handoff_sha256: String,
|
||||
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>,
|
||||
/// Only ReadOriginal; exact retained execution_evidence.start bytes,
|
||||
/// 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(¤t.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, ¤t.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);
|
||||
}
|
||||
}
|
||||
@@ -1777,6 +1777,23 @@ 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)
|
||||
.unwrap();
|
||||
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();
|
||||
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_eq!(
|
||||
f.db.query_row::<u32, _, _>(
|
||||
@@ -1974,3 +1991,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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -28,3 +28,7 @@ rmm-protocol = { path = "../protocol" }
|
||||
rmm-agent-linux = { path = "../agent-linux" }
|
||||
rmm-privileged-helper = { path = "../privileged-helper" }
|
||||
libc = "0.2.177"
|
||||
|
||||
rmm-updater-handoff = { path = "../updater-handoff" }
|
||||
base64 = "=0.22.1"
|
||||
hex.workspace = true
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -299,3 +299,83 @@ pub fn general_supported(binding: &Binding, job: &rmm_domain::jobs::JobPayload)
|
||||
})()
|
||||
.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(())
|
||||
}
|
||||
}
|
||||
|
||||
+100
-6
@@ -1,3 +1,4 @@
|
||||
mod agent_update_health;
|
||||
mod apt;
|
||||
mod bootstrap;
|
||||
mod certificate_rotation;
|
||||
@@ -100,6 +101,12 @@ impl EffectFence {
|
||||
&& (!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 = (
|
||||
JobOffer,
|
||||
rmm_domain::jobs::ExecutionPermitPayload,
|
||||
@@ -217,6 +224,51 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
}
|
||||
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(
|
||||
&mut self,
|
||||
generation: u64,
|
||||
@@ -245,14 +297,25 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
if grant.is_empty() {
|
||||
jobs::interrupted_request(&self.db, &offer)?;
|
||||
} 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(
|
||||
0,
|
||||
&offer,
|
||||
&grant,
|
||||
jobs::recover_interrupted_outcome(&self.ledger, &binding, &offer, &grant)?
|
||||
.unwrap_or_else(|| {
|
||||
rmm_privileged_helper::Outcome::unknown("AGENT_PROCESS_INTERRUPTED")
|
||||
}),
|
||||
saved.unwrap_or_else(|| {
|
||||
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 capability_at = Instant::now();
|
||||
let mut first_heartbeat = true;
|
||||
let mut health_task: Option<PendingUpdateHealth> = None;
|
||||
let mut health_inventory_prefix: Option<String> = None;
|
||||
loop {
|
||||
tokio::select! {
|
||||
Some(result) = self.inventory_rx.recv() => {
|
||||
@@ -574,8 +639,19 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
if let Ok((data,observed_at)) = result {
|
||||
let inventory = self.inventory_envelope(generation,data,observed_at)?;
|
||||
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"); }
|
||||
}
|
||||
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() => {
|
||||
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");
|
||||
@@ -639,9 +715,26 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
_ = tick.tick() => {
|
||||
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 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 self.prepared.is_some() && !busy && 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_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||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 !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
|
||||
.certificates
|
||||
.due(self.rotation_policy, Utc::now().timestamp())?
|
||||
&& self.update_proof()?.is_none()
|
||||
{
|
||||
self.close_gate();
|
||||
drop(ws);
|
||||
|
||||
@@ -18,3 +18,10 @@ fs2 = "0.4.3"
|
||||
rusqlite = { version = "0.32.1", features = ["bundled"] }
|
||||
rmm-domain = { path = "../domain" }
|
||||
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" }
|
||||
|
||||
@@ -9,6 +9,8 @@ pub mod readiness;
|
||||
pub mod reconcile;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub mod update_control;
|
||||
pub mod update_guard;
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
use openssl::pkey::{Id, PKey};
|
||||
|
||||
@@ -133,11 +133,12 @@ impl Config {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum Incoming {
|
||||
NativeUpdate(rmm_agent_core::update_handoff::NativeControlRequest),
|
||||
Control(control::Request),
|
||||
Probe(rmm_privileged_helper::readiness::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 size = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
|
||||
// SAFETY: getsockopt receives correctly aligned writable storage and length.
|
||||
@@ -158,10 +159,10 @@ fn peer_allowed(stream: &UnixStream, uid: u32) -> Result<()> {
|
||||
"PEER_SIZE"
|
||||
);
|
||||
// SAFETY: successful SO_PEERCRED initialized the complete ucred.
|
||||
ensure!(
|
||||
unsafe { credentials.assume_init() }.uid == uid && uid != 0,
|
||||
"PEER_DENIED"
|
||||
);
|
||||
Ok(unsafe { credentials.assume_init() }.uid)
|
||||
}
|
||||
fn peer_allowed(stream: &UnixStream, uid: u32) -> Result<()> {
|
||||
ensure!(uid != 0 && peer_uid(stream)? == uid, "PEER_DENIED");
|
||||
Ok(())
|
||||
}
|
||||
fn policy(config: &Config, job: &rmm_domain::jobs::JobPayload) -> Result<()> {
|
||||
@@ -320,17 +321,29 @@ fn execute(job: &rmm_domain::jobs::JobPayload) -> Result<Outcome> {
|
||||
.into(),
|
||||
})
|
||||
}
|
||||
struct RuntimeClocks {
|
||||
action: rmm_privileged_helper::TrustedClock,
|
||||
sync: reconcile::NativeClock,
|
||||
update: Option<rmm_updater_worker::clock::NativeClock>,
|
||||
}
|
||||
fn handle(
|
||||
config: &Config,
|
||||
db: &mut rusqlite::Connection,
|
||||
stream: &mut UnixStream,
|
||||
clock: &rmm_privileged_helper::TrustedClock,
|
||||
clocks: &RuntimeClocks,
|
||||
sync: &mut reconcile::HelperSync,
|
||||
sync_clock: &reconcile::NativeClock,
|
||||
mut image: Option<&mut image::RunningImage>,
|
||||
) -> Result<()> {
|
||||
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_write_timeout(Some(Duration::from_secs(5)))?;
|
||||
let mut size = [0; 4];
|
||||
@@ -345,11 +358,36 @@ fn handle(
|
||||
"RUNNING_HELPER_BINARY_CHANGED"
|
||||
);
|
||||
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) => {
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
ensure!(size <= 512 * 1024, "JOB_REQUEST_SIZE");
|
||||
request
|
||||
}
|
||||
Incoming::Control(request) => {
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
let (challenge, identity) = request.binding();
|
||||
ensure!(
|
||||
!challenge.is_nil()
|
||||
@@ -360,6 +398,21 @@ fn handle(
|
||||
"CONTROL_IDENTITY"
|
||||
);
|
||||
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![]);
|
||||
match &request {
|
||||
control::Request::Snapshot { generation, .. } => {
|
||||
@@ -369,27 +422,36 @@ fn handle(
|
||||
&guard,
|
||||
reconcile::Clock::now(sync_clock)?,
|
||||
)?;
|
||||
snapshot = Some(sync.snapshot(
|
||||
db,
|
||||
&guard,
|
||||
*generation,
|
||||
sync_clock,
|
||||
&reconcile::NativeBoot,
|
||||
)?)
|
||||
snapshot = Some(if let Some(owner) = &update_guard {
|
||||
sync.snapshot(db, owner, *generation, sync_clock, &reconcile::NativeBoot)?
|
||||
} else {
|
||||
sync.snapshot(db, &guard, *generation, sync_clock, &reconcile::NativeBoot)?
|
||||
})
|
||||
}
|
||||
control::Request::Apply {
|
||||
response,
|
||||
request_bytes,
|
||||
..
|
||||
} => {
|
||||
let applied = sync.apply(
|
||||
db,
|
||||
&guard,
|
||||
response,
|
||||
request_bytes,
|
||||
sync_clock,
|
||||
&reconcile::NativeBoot,
|
||||
)?;
|
||||
let applied = if let Some(owner) = &update_guard {
|
||||
sync.apply(
|
||||
db,
|
||||
owner,
|
||||
response,
|
||||
request_bytes,
|
||||
sync_clock,
|
||||
&reconcile::NativeBoot,
|
||||
)?
|
||||
} else {
|
||||
sync.apply(
|
||||
db,
|
||||
&guard,
|
||||
response,
|
||||
request_bytes,
|
||||
sync_clock,
|
||||
&reconcile::NativeBoot,
|
||||
)?
|
||||
};
|
||||
snapshot = Some(applied.snapshot);
|
||||
unlocked = applied.unlocked_grants;
|
||||
}
|
||||
@@ -446,6 +508,7 @@ fn handle(
|
||||
return Ok(());
|
||||
}
|
||||
Incoming::Probe(request) => {
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
ensure!(
|
||||
config.profile == HelperProfile::AptOnly,
|
||||
"PROBE_REQUIRES_APT_HELPER"
|
||||
@@ -722,9 +785,23 @@ fn run() -> Result<()> {
|
||||
let _initialization_lock = lock_state(&config.state, 0)?;
|
||||
open_ledger(&config.state, 0)?
|
||||
};
|
||||
let clock = rmm_privileged_helper::TrustedClock::new();
|
||||
let sync_clock = reconcile::NativeClock::default();
|
||||
let mut sync = reconcile::HelperSync::new(config.binding.clone(), &sync_clock)?;
|
||||
let update_clock = if config.profile == HelperProfile::FullMaintenanceV1
|
||||
&& std::path::Path::new("/etc/rmm-agent-updater/policy.json").try_exists()?
|
||||
{
|
||||
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
|
||||
.socket
|
||||
.parent()
|
||||
@@ -751,9 +828,8 @@ fn run() -> Result<()> {
|
||||
&config,
|
||||
&mut db,
|
||||
&mut stream,
|
||||
&clock,
|
||||
&clocks,
|
||||
&mut sync,
|
||||
&sync_clock,
|
||||
running_image.as_mut(),
|
||||
) {
|
||||
let code = safe_error_code(&error);
|
||||
|
||||
@@ -27,7 +27,7 @@ impl BootSource for NativeBoot {
|
||||
/// 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.
|
||||
pub struct ExecutorGuard {
|
||||
_file: File,
|
||||
pub(crate) _file: File,
|
||||
directory: PathBuf,
|
||||
_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,
|
||||
})
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -14,11 +14,9 @@ uuid.workspace = true
|
||||
libc = "0.2.177"
|
||||
rusqlite = { version = "0.32.1", features = ["bundled"] }
|
||||
ed25519-dalek = "=2.2.0"
|
||||
base64 = "=0.22.1"
|
||||
rmm-domain = { path = "../domain" }
|
||||
rmm-agent-core = { path = "../agent-core" }
|
||||
rmm-updater = { path = "../updater" }
|
||||
rmm-updater-handoff = { path = "../updater-handoff" }
|
||||
rmm-updater-linux = { path = "../updater-linux" }
|
||||
|
||||
[dev-dependencies]
|
||||
base64 = "=0.22.1"
|
||||
|
||||
@@ -0,0 +1,518 @@
|
||||
//! 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: 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,
|
||||
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()?;
|
||||
let (_, journal) = verify_inflight_proof(&self.prefix, context.health_public_key)
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
match &method {
|
||||
NativeControlOperation::ReadOriginal => {}
|
||||
NativeControlOperation::ClaimStop {
|
||||
expected_head_sha256,
|
||||
..
|
||||
} => ensure!(
|
||||
context.generation == Generation::Original
|
||||
&& journal.phase() == Phase::StopIntent
|
||||
&& *expected_head_sha256 == self.prefix.head_sha256,
|
||||
"UPDATE_BRIDGE_CLAIM_PHASE"
|
||||
),
|
||||
NativeControlOperation::ObserveStarts { .. } => ensure!(
|
||||
context.generation == Generation::Replacement
|
||||
&& journal.phase() == Phase::LedgerReturnIntent,
|
||||
"UPDATE_BRIDGE_START_PHASE"
|
||||
),
|
||||
NativeControlOperation::Health { receipt } => {
|
||||
ensure!(
|
||||
context.generation == Generation::Replacement
|
||||
&& journal.phase() == Phase::ServicesStarted,
|
||||
"UPDATE_BRIDGE_HEALTH_PHASE"
|
||||
);
|
||||
journal.prepare(
|
||||
u64::try_from(context.clock.now()?.timestamp())?,
|
||||
rmm_updater_handoff::Evidence::HealthVerified {
|
||||
receipt: receipt.clone(),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
NativeControlOperation::Finalize => ensure!(
|
||||
context.generation == Generation::Replacement
|
||||
&& journal.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
|
||||
&& proof.events.len() >= self.prefix.events.len()
|
||||
&& proof.events[..self.prefix.events.len()] == self.prefix.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 = 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) -> &str {
|
||||
&self.prefix.head_sha256
|
||||
}
|
||||
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)
|
||||
}
|
||||
@@ -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(())
|
||||
}
|
||||
@@ -22,6 +22,7 @@ pub struct LedgerFence<'w, 'a, 's> {
|
||||
}
|
||||
impl<'w, 'a, 's> LedgerFence<'w, 'a, 's> {
|
||||
pub fn new(ledger: &'w mut OwnedLedger<'a, 's>) -> Result<Self> {
|
||||
ledger.qualify_for_effects()?;
|
||||
ensure!(
|
||||
ledger.journal()?.phase() == Phase::UpdaterOwnsLedger,
|
||||
"UPDATE_NO_FENCE_RESUME"
|
||||
|
||||
@@ -5,9 +5,9 @@ use rmm_domain::jobs::hash;
|
||||
use rmm_updater_handoff::{FileRole, Handoff};
|
||||
use rmm_updater_linux::{
|
||||
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 uuid::Uuid;
|
||||
|
||||
@@ -29,12 +29,20 @@ fn role(role: FileRole) -> Result<Role> {
|
||||
_ => anyhow::bail!("UPDATE_LINUX_FILE_ROLE"),
|
||||
}
|
||||
}
|
||||
#[derive(Serialize)]
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Proof {
|
||||
role: Role,
|
||||
original: FileProof,
|
||||
staged: FileProof,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Record {
|
||||
schema_version: u32,
|
||||
owner_nonce: Uuid,
|
||||
files: Vec<Proof>,
|
||||
}
|
||||
pub struct FileWitnesses {
|
||||
files: Vec<Proof>,
|
||||
nonce: Uuid,
|
||||
@@ -63,6 +71,7 @@ impl FileWitnesses {
|
||||
)?;
|
||||
ensure!(
|
||||
stage.proof().identity.links == 1
|
||||
&& stage.proof().identity.device == original.proof().identity.device
|
||||
&& stage.proof().identity.uid == original.proof().identity.uid
|
||||
&& stage.proof().identity.gid == original.proof().identity.gid
|
||||
&& stage.proof().identity.mode == original.proof().identity.mode,
|
||||
@@ -74,7 +83,11 @@ impl FileWitnesses {
|
||||
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 {
|
||||
files,
|
||||
nonce,
|
||||
@@ -84,6 +97,65 @@ impl FileWitnesses {
|
||||
pub fn sha256(&self) -> &str {
|
||||
&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<()> {
|
||||
ensure!(
|
||||
handoff.files.len() == 4
|
||||
@@ -101,6 +173,30 @@ impl FileWitnesses {
|
||||
}
|
||||
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<()> {
|
||||
let file = self.file(which)?;
|
||||
budget.check()?;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,7 +3,7 @@
|
||||
//! consumption, schema reset, outcome healing or readiness transition.
|
||||
use crate::{
|
||||
clock::NativeClock, files::FileWitnesses, original::Original, participants::StableParticipant,
|
||||
retained::RetainedSet, trust::TrustState,
|
||||
retained::RetainedSet, returned::PreparedReservation, trust::TrustState,
|
||||
};
|
||||
use anyhow::{Result, ensure};
|
||||
use rmm_agent_core::{
|
||||
@@ -22,8 +22,22 @@ use rmm_updater_linux::services::StoppedServices;
|
||||
use rmm_updater_linux::{Budget, files::LedgerGuard};
|
||||
use rusqlite::{Connection, OpenFlags};
|
||||
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";
|
||||
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> {
|
||||
os: &'a LedgerGuard<'stopped>,
|
||||
@@ -56,7 +70,7 @@ pub struct OwnedLedger<'a, 'stopped> {
|
||||
}
|
||||
impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
|
||||
pub fn open(input: OwnedInputs<'a, 'stopped>) -> Result<Self> {
|
||||
input.os.verify_for_effect(input.budget)?;
|
||||
input.os.verify_for_effect(&observation_budget()?)?;
|
||||
ensure!(
|
||||
input.os.identity().sha256()? == input.original.handoff().ledger_identity_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.
|
||||
update_handoff::read(&db, &guard, input.original.permit().grant_id)?;
|
||||
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)?;
|
||||
Ok(Self { db, guard })
|
||||
}
|
||||
pub fn journal(&self) -> Result<Journal> {
|
||||
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<()> {
|
||||
let journal = self.journal()?;
|
||||
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<()> {
|
||||
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(
|
||||
&mut self.db,
|
||||
&self.guard,
|
||||
@@ -153,17 +231,66 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
|
||||
pub fn verify_pair(&self) -> Result<()> {
|
||||
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
|
||||
/// open/failed-close connection never produces a released-ledger witness.
|
||||
pub fn close(self) -> Result<()> {
|
||||
self.guard.check(&self.db)?;
|
||||
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<'_, '_> {
|
||||
fn check(&self, db: &Connection) -> Result<()> {
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
let observation = observation_budget()?;
|
||||
self.os.verify_for_effect(&observation)?;
|
||||
ensure!(
|
||||
unsafe { db.handle() } as usize == self.sqlite_handle,
|
||||
"UPDATE_SQLITE_CONNECTION_CHANGED"
|
||||
@@ -174,11 +301,16 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(path == LEDGER, "UPDATE_SQLITE_PATH_CHANGED");
|
||||
self.original
|
||||
.check_current(self.binding, self.clock, self.budget)?;
|
||||
self.trust.recheck(self.original, self.clock)?;
|
||||
Original::verify_retained(
|
||||
self.binding,
|
||||
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
|
||||
.verify_delegation(self.original.delegation(), self.budget)?;
|
||||
.verify_delegation(self.original.delegation(), &observation)?;
|
||||
self.retained.verify()?;
|
||||
let grant = self.original.permit().grant_id.to_string();
|
||||
let start: Vec<u8> = db.query_row(
|
||||
@@ -196,7 +328,7 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(unresolved == 0, "UPDATE_OTHER_UNRESOLVED_ATTEMPT");
|
||||
self.os.verify_for_effect(self.budget)
|
||||
self.os.verify_for_effect(&observation)
|
||||
}
|
||||
fn check_update_owner(
|
||||
&self,
|
||||
@@ -217,6 +349,9 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|
||||
&& delegation == self.original.delegation(),
|
||||
"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.updater.verify_delegation(delegation, self.budget)
|
||||
}
|
||||
@@ -236,8 +371,17 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
|
||||
&& delegation == self.original.delegation(),
|
||||
"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
|
||||
.check_current(self.binding, self.clock, self.budget)?;
|
||||
self.trust.recheck(self.original, self.clock)?;
|
||||
self.updater.verify_delegation(delegation, self.budget)?;
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
self.retained.verify()?;
|
||||
@@ -304,6 +448,7 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
|
||||
);
|
||||
self.original
|
||||
.check_current(self.binding, self.clock, self.budget)?;
|
||||
self.trust.recheck(self.original, self.clock)?;
|
||||
self.updater.verify_delegation(delegation, self.budget)?;
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
self.retained.verify()?;
|
||||
@@ -314,6 +459,9 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
|
||||
NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin,
|
||||
};
|
||||
match (effect, stage) {
|
||||
(NativeEffect::StopService { .. }, NativeEffectStage::Observe) => {
|
||||
self.stopped.verify_still_stopped(self.budget)?
|
||||
}
|
||||
(NativeEffect::SavePredecessor { role: r }, NativeEffectStage::Claim) => {
|
||||
self.files.verify_original_and_stage(role(r), self.budget)?
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
//! Concrete, original-attempt-bound Linux worker. No approval or retry path.
|
||||
#![cfg(target_os = "linux")]
|
||||
|
||||
pub mod bridge;
|
||||
pub mod bundle;
|
||||
pub mod clock;
|
||||
pub mod fence;
|
||||
pub mod files;
|
||||
pub mod full;
|
||||
pub mod history;
|
||||
pub mod ledger;
|
||||
pub mod original;
|
||||
pub mod participants;
|
||||
pub mod record;
|
||||
pub mod retained;
|
||||
pub mod returned;
|
||||
pub mod trust;
|
||||
|
||||
@@ -32,6 +32,18 @@ impl Original {
|
||||
delegation: UpdateDelegation,
|
||||
clock: &impl SyncClock,
|
||||
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> {
|
||||
ensure!(
|
||||
!start_bytes.is_empty()
|
||||
@@ -40,7 +52,6 @@ impl Original {
|
||||
&& record.len() <= rmm_updater_handoff::MAX_RECORD_BYTES,
|
||||
"UPDATE_ORIGINAL_LIMIT"
|
||||
);
|
||||
budget.check()?;
|
||||
let handoff: Handoff = serde_json::from_slice(record)?;
|
||||
handoff.validate()?;
|
||||
ensure!(
|
||||
@@ -72,12 +83,8 @@ impl Original {
|
||||
&request,
|
||||
handoff.boot_id,
|
||||
delegation.prepared_at
|
||||
)? == start_bytes
|
||||
&& std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
|
||||
.trim()
|
||||
.parse::<uuid::Uuid>()?
|
||||
== handoff.boot_id,
|
||||
"UPDATE_ORIGINAL_START_OR_BOOT_CHANGED"
|
||||
)? == start_bytes,
|
||||
"UPDATE_ORIGINAL_START_CHANGED"
|
||||
);
|
||||
ensure!(
|
||||
job.execution_identity == "root"
|
||||
@@ -144,7 +151,6 @@ impl Original {
|
||||
delegation,
|
||||
start_bytes: start_bytes.to_vec(),
|
||||
};
|
||||
result.check_current(binding, clock, budget)?;
|
||||
Ok(result)
|
||||
}
|
||||
pub fn check_current(
|
||||
@@ -154,6 +160,13 @@ impl Original {
|
||||
budget: &Budget,
|
||||
) -> Result<()> {
|
||||
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()?;
|
||||
ensure!(
|
||||
now >= self.delegation.prepared_at
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,8 @@ pub struct RetainedFileSpec {
|
||||
pub identity: Identity,
|
||||
pub retention: Retention,
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Stamp {
|
||||
size: u64,
|
||||
mtime: i64,
|
||||
@@ -120,6 +121,59 @@ impl RetainedSet {
|
||||
pub fn sha256(&self) -> &str {
|
||||
&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<()> {
|
||||
for file in &self.files {
|
||||
file.verify()?;
|
||||
@@ -161,3 +215,91 @@ impl HeldState {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -53,14 +53,26 @@ impl Protected {
|
||||
}
|
||||
}
|
||||
pub struct TrustState {
|
||||
updater_binding: rmm_domain::agent_update::UpdaterBinding,
|
||||
predecessor: rmm_domain::agent_update::Predecessor,
|
||||
protected: Vec<Protected>,
|
||||
policy: LocalUpdatePolicy,
|
||||
installed: InstalledReleaseRecord,
|
||||
installed_raw: Vec<u8>,
|
||||
keyring: UpdateKeyring,
|
||||
keyring_file: UpdateKeyringFile,
|
||||
baseline_signer_revoked: bool,
|
||||
health_public_key: [u8; 32],
|
||||
}
|
||||
impl TrustState {
|
||||
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 mut protected = Vec::new();
|
||||
let (file, raw) = Protected::read(Path::new("/etc/rmm-agent-updater/policy.json"), 65536)?;
|
||||
@@ -88,6 +100,7 @@ impl TrustState {
|
||||
protected.push(file);
|
||||
let job_key = hex::decode(&binding.public_key_hex)?;
|
||||
let mut trusted = Vec::new();
|
||||
let mut historical_keys = Vec::new();
|
||||
let mut baseline_key = None;
|
||||
for entry in &keys.keys {
|
||||
ensure!(entry.platform == Platform::LinuxX64, "UPDATE_KEY_PLATFORM");
|
||||
@@ -108,23 +121,30 @@ impl TrustState {
|
||||
entry.last_sequence,
|
||||
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 historical = UpdateKeyring::new(keys.generation, historical_keys)?;
|
||||
let baseline = policy
|
||||
.initial_baseline
|
||||
.decode()
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
let (baseline_entry, public) = baseline_key.context("UPDATE_BASELINE_KEY_NOT_TRUSTED")?;
|
||||
ensure!(
|
||||
!baseline_entry.revoked
|
||||
&& baseline
|
||||
.record
|
||||
.release_envelope
|
||||
.decode()
|
||||
.map_err(anyhow::Error::msg)?
|
||||
.manifest
|
||||
.release_sequence
|
||||
>= baseline_entry.first_sequence
|
||||
baseline
|
||||
.record
|
||||
.release_envelope
|
||||
.decode()
|
||||
.map_err(anyhow::Error::msg)?
|
||||
.manifest
|
||||
.release_sequence
|
||||
>= baseline_entry.first_sequence
|
||||
&& baseline
|
||||
.record
|
||||
.release_envelope
|
||||
@@ -161,18 +181,23 @@ impl TrustState {
|
||||
"UPDATE_INSTALLED_RECORD_CHANGED"
|
||||
);
|
||||
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!(
|
||||
matches!(
|
||||
installed.activation_evidence,
|
||||
ActivationEvidence::Bootstrap { .. }
|
||||
) && raw == baseline.payload_bytes
|
||||
&& hash(&raw) == policy.initial_record_sha256
|
||||
&& installed.activation_floor == policy.initial_activation_floor,
|
||||
"UPDATE_INSTALLED_CHAIN_NOT_QUALIFIED"
|
||||
!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,
|
||||
ActivationEvidence::Bootstrap { .. }
|
||||
) {
|
||||
ensure!(
|
||||
raw == baseline.payload_bytes
|
||||
&& hash(&raw) == policy.initial_record_sha256
|
||||
&& installed.activation_floor == policy.initial_activation_floor,
|
||||
"UPDATE_INSTALLED_BASELINE_CHANGED"
|
||||
);
|
||||
}
|
||||
let active = installed
|
||||
.release_envelope
|
||||
.decode()
|
||||
@@ -185,13 +210,13 @@ impl TrustState {
|
||||
&& active.manifest.artifacts[1].sha256 == update.predecessor.helper_sha256,
|
||||
"UPDATE_INSTALLED_PREDECESSOR_BINDING"
|
||||
);
|
||||
verify_installed_release(&keyring, &installed, keys.generation)?;
|
||||
verify_installed_release(&historical, &installed, keys.generation)?;
|
||||
verify(
|
||||
&keyring,
|
||||
&historical,
|
||||
&update.release_envelope,
|
||||
&TrustedContext {
|
||||
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,
|
||||
protocol_major: active.manifest.protocol_major,
|
||||
state_schema: active.manifest.state_schema,
|
||||
@@ -204,18 +229,35 @@ impl TrustState {
|
||||
},
|
||||
)?;
|
||||
protected.push(file);
|
||||
let baseline_signer_revoked = baseline_entry.revoked;
|
||||
Ok(Self {
|
||||
updater_binding: update.updater.clone(),
|
||||
predecessor: update.predecessor.clone(),
|
||||
protected,
|
||||
policy,
|
||||
installed,
|
||||
installed_raw: raw,
|
||||
keyring,
|
||||
keyring_file: keys,
|
||||
baseline_signer_revoked,
|
||||
health_public_key,
|
||||
})
|
||||
}
|
||||
pub fn recheck(&self, original: &Original, clock: &NativeClock) -> Result<()> {
|
||||
for file in &self.protected {
|
||||
file.verify()?;
|
||||
}
|
||||
self.recheck_original_binding(original)?;
|
||||
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
|
||||
.installed
|
||||
.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 {
|
||||
&self.policy
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
# 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).
|
||||
@@ -60,3 +60,73 @@ WebUI-genehmigter Linux-Apply sowie dessen unabhängiger Health-/Erhaltungsnachw
|
||||
Restore/Rollback bleibt deny, weil der unveränderte Vorwärtskernel noch keinen
|
||||
qualifizierten Rückweg besitzt. Windowsapply ist separat offen. Kein Zielhost
|
||||
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.
|
||||
|
||||
@@ -40,9 +40,13 @@ General-/Full-/Updater-Dienstkontexte sind noch nativ zu qualifizieren.
|
||||
und InstalledRecord-Dateien. Originale rohe Hashes bleiben maßgeblich. Die
|
||||
initiale Baseline benötigt ihre gesonderte Ed25519-Domänensignatur; aktuelle
|
||||
Schlüsselwiderrufe/-bereiche und Releaseverifikation werden unabhängig geprüft.
|
||||
Health-/Release-/Jobkeys werden getrennt geprüft. Die erste Aktivierung bleibt
|
||||
an den separat signierten Bootstraprecord gebunden. Ein `Updated`-Vorgänger
|
||||
wird bis zur vollständigen Record-/Health-Historyprüfung ausdrücklich verweigert.
|
||||
Health-/Release-/Jobkeys werden getrennt geprüft. Historische read-only Lader
|
||||
sind von aktueller Candidate-/Clock-/Boot-/Revocationprüfung getrennt und
|
||||
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
|
||||
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
|
||||
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
|
||||
|
||||
Vollständige Engine/Daemonhülle und originale aktivierungsgebundene Stageaufnahme,
|
||||
Full-Control-Bridge vor Stop, drei Stopbeobachtungen vor AllStopped, atomare
|
||||
Rückgabe-/Startreservation, tatsächliche Guardabgabe vor einmaligen Starts,
|
||||
neuer Full-Observer und tatsächlicher Health-/Outcomeanschluss sind noch offen.
|
||||
Stopfence/Drive-Schleife außerhalb des alten synchronen Helpercalls,
|
||||
drei Stopbeobachtungen vor AllStopped und die vollständige Runtimeverdrahtung
|
||||
von Guardabgabe/Starts/Full-Observer/Health/Outcome sind noch offen.
|
||||
Floor-/InstalledRecordpublikation braucht zusätzlich die geschlossenen
|
||||
originalen nativen Intents nach Health und erhaltende Record-CAS. Folgeupdates
|
||||
brauchen vollständige geschützte Record-/Health-Historyverifikation.
|
||||
originalen nativen Intents nach Health und erhaltende Record-CAS. Die History-
|
||||
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.
|
||||
`RestorePredecessor` bleibt deshalb auch bei vorab gebundener Recoveryoption
|
||||
@@ -105,7 +126,35 @@ derselben privaten Quellkopie. Kein Frozenverifier wurde dafür verändert.
|
||||
Privater Nachweis:
|
||||
`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 VM/SSH-/Job-/WebUI-Aktion und kein nativer Worker-/Applynachweis.
|
||||
Der native Linux-OS5-Nachweis qualifiziert weiterhin dessen eigene eingefrorene
|
||||
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).
|
||||
|
||||
Reference in New Issue
Block a user