diff --git a/crates/agent-core/src/reconcile.rs b/crates/agent-core/src/reconcile.rs index c4a9f2a..4b1d8cd 100644 --- a/crates/agent-core/src/reconcile.rs +++ b/crates/agent-core/src/reconcile.rs @@ -38,6 +38,13 @@ pub trait SyncClock { upper: own_wall, }) } + /// Full trusted UTC interval for crypto validity. Existing point-clock + /// providers retain their contract; interval providers override atomically. + fn bounds(&self) -> Result<(DateTime,DateTime)> { + let reading=self.reading()?; + ensure!(reading.own_wall<=reading.upper,"CLOCK_INTERVAL_ORDER"); + Ok((reading.own_wall,reading.upper)) + } } #[derive(Clone, Debug, PartialEq, Eq)] pub struct Channel { diff --git a/crates/agent-core/src/update_handoff.rs b/crates/agent-core/src/update_handoff.rs index 3fb34e6..886ba8b 100644 --- a/crates/agent-core/src/update_handoff.rs +++ b/crates/agent-core/src/update_handoff.rs @@ -38,6 +38,7 @@ pub enum Participant { OriginalHelper, StableUpdater, NewHelper, + RestoredHelper, } pub trait UpdateParticipantGuard: ExecutorLedgerGuard { fn participant(&self) -> Participant; @@ -50,6 +51,18 @@ pub trait UpdateParticipantGuard: ExecutorLedgerGuard { ) -> Result<()> { anyhow::bail!("UPDATE_NATIVE_EFFECT_NOT_QUALIFIED") } + /// A retained native START/conditional STOP witness must be independently + /// bound to the original once-only source/held handles. No caller hash or + /// current service-name/PID lookup is an attestation of a past operation. + fn native_observation_sha256( + &self, + _handoff: &Handoff, + _delegation: &UpdateDelegation, + _effect: &NativeEffect, + _stage: NativeEffectStage, + ) -> Result { + anyhow::bail!("UPDATE_RETAINED_NATIVE_OBSERVATION_NOT_QUALIFIED") + } /// Recheck fixed service/PID/birth/image, original boot, SAME file IDs, /// current clock/deadline and evidence-specific protected observations. /// Health additionally compares receipt contents with the actually applied @@ -91,7 +104,11 @@ pub fn initialize(db: &Connection, guard: &impl ExecutorLedgerGuard) -> Result<( 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));", + witness BLOB NOT NULL,witness_sha256 TEXT NOT NULL,PRIMARY KEY(grant_id,decision_id)); + CREATE TABLE IF NOT EXISTS agent_update_native_nonstarts( + 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));", )?; Ok(()) } @@ -300,7 +317,7 @@ pub(crate) fn read_locked(db: &Connection, grant: Uuid) -> Result { let (effect_key, raw, digest) = row?; let claim: NativeEffectClaim = serde_json::from_slice(&raw)?; ensure!( - native_count <= 17 + native_count <= if handoff.schema_version == 2 { 32 } else { 17 } && raw.len() <= 2048 && hash(&raw) == digest && serde_json::to_vec(&claim)? == raw @@ -313,6 +330,10 @@ pub(crate) fn read_locked(db: &Connection, grant: Uuid) -> Result { } let invalid:u64=db.query_row("SELECT count(*) FROM agent_update_native_observations o LEFT JOIN agent_update_native_claims c ON c.grant_id=o.grant_id AND c.effect_key=o.effect_key WHERE o.grant_id=?1 AND (c.claim_sha256 IS NULL OR c.claim_sha256<>o.claim_sha256)",[grant.to_string()],|r|r.get(0))?; ensure!(invalid == 0, "UPDATE_NATIVE_OBSERVATION_BINDING"); + if handoff.schema_version == 2 { + let invalid:u64=db.query_row("SELECT count(*) FROM agent_update_native_nonstarts n LEFT JOIN agent_update_native_claims c ON c.grant_id=n.grant_id AND c.effect_key=n.effect_key LEFT JOIN agent_update_native_observations o ON o.grant_id=n.grant_id AND o.effect_key=n.effect_key WHERE n.grant_id=?1 AND (c.claim_sha256 IS NULL OR c.claim_sha256<>n.claim_sha256 OR o.claim_sha256 IS NOT NULL OR length(n.observation_sha256)<>64)",[grant.to_string()],|r|r.get(0))?; + ensure!(invalid == 0, "UPDATE_NATIVE_NONSTART_BINDING"); + } let mut journal = Journal::open( &record, key.try_into() @@ -337,7 +358,9 @@ pub(crate) fn read_locked(db: &Connection, grant: Uuid) -> Result { ); journal.replay(&event)?; let parsed: Event = serde_json::from_slice(&event)?; - if let Evidence::OutcomePersisted { outcome_sha256 } = parsed.evidence { + if let Evidence::OutcomePersisted { outcome_sha256 } + | Evidence::RollbackOutcomePersisted { outcome_sha256 } = parsed.evidence + { stored_outcome_hash = Some(outcome_sha256); } } @@ -440,7 +463,11 @@ pub fn append( ensure!( !matches!( parsed.evidence, - Evidence::OutcomePersisted { .. } | Evidence::Unknown { .. } + Evidence::OutcomePersisted { .. } + | Evidence::RollbackOutcomePersisted { .. } + | Evidence::RollbackIntent { .. } + | Evidence::RollbackLedgerReturnIntent + | Evidence::Unknown { .. } ), "UPDATE_TERMINAL_REQUIRES_OUTCOME_CAS" ); @@ -481,6 +508,16 @@ fn verify_writer( |r| r.get(0), )?; let handoff: Handoff = serde_json::from_slice(&raw)?; + let native_services = + if handoff.pin_transition == rmm_updater_handoff::PinTransition::WindowsCodePairClockV1 { + vec![NativeService::WindowsAgent, NativeService::WindowsHelper] + } else { + vec![ + NativeService::Agent, + NativeService::General, + NativeService::Full, + ] + }; let expected = match event.evidence { Evidence::AllStopped { .. } | Evidence::UpdaterOwnsLedger { .. } @@ -491,6 +528,25 @@ fn verify_writer( Evidence::ServicesStarted { .. } | Evidence::HealthVerified { .. } | Evidence::OutcomePersisted { .. } => Participant::NewHelper, + Evidence::RollbackStopped { .. } + | Evidence::RestoreIntent { .. } + | Evidence::CandidatePreserved { .. } + | Evidence::PredecessorRestored { .. } + | Evidence::PredecessorVerified { .. } + | Evidence::RollbackLedgerReturnIntent => Participant::StableUpdater, + Evidence::RollbackServicesStarted { .. } + | Evidence::RollbackHealthVerified { .. } + | Evidence::RollbackOutcomePersisted { .. } => Participant::RestoredHelper, + Evidence::RollbackIntent { .. } => { + ensure!( + matches!( + guard.participant(), + Participant::StableUpdater | Participant::NewHelper + ), + "UPDATE_ROLLBACK_INTENT_WRITER" + ); + guard.participant() + } Evidence::Unknown { .. } => guard.participant(), Evidence::StopIntent => anyhow::bail!("UPDATE_STOP_ALREADY_ATOMICALLY_STORED"), }; @@ -502,11 +558,7 @@ fn verify_writer( }; match &event.evidence { Evidence::AllStopped { .. } => { - for service in [ - NativeService::Agent, - NativeService::General, - NativeService::Full, - ] { + for service in native_services { if handoff.stop_set.contains(&native_service(service)) { require_observed(NativeEffect::StopService { service })?; } @@ -518,19 +570,64 @@ fn verify_writer( rmm_updater_handoff::FileRole::HelperBinary => NativeFileRole::HelperBinary, rmm_updater_handoff::FileRole::LinuxFullConfig => NativeFileRole::FullConfig, rmm_updater_handoff::FileRole::LinuxFullPin => NativeFileRole::FullPin, + rmm_updater_handoff::FileRole::WindowsSettings + if handoff.pin_transition + == rmm_updater_handoff::PinTransition::WindowsCodePairClockV1 => + { + NativeFileRole::WindowsSettings + } + rmm_updater_handoff::FileRole::WindowsPin + if handoff.pin_transition + == rmm_updater_handoff::PinTransition::WindowsCodePairClockV1 => + { + NativeFileRole::WindowsPin + } + rmm_updater_handoff::FileRole::WindowsTimeProfile + if handoff.pin_transition + == rmm_updater_handoff::PinTransition::WindowsCodePairClockV1 => + { + NativeFileRole::WindowsTimeProfile + } _ => anyhow::bail!("UPDATE_NATIVE_PLATFORM_UNQUALIFIED"), }; require_observed(NativeEffect::PublishReplacement { role })?; } Evidence::ServicesStarted { .. } => { - for service in [ - NativeService::General, - NativeService::Full, - NativeService::Agent, - ] { + for service in native_services { require_observed(NativeEffect::StartService { service })?; } } + Evidence::RollbackStopped { .. } => { + for service in native_services { + let key = native_key(&NativeEffect::RollbackStopService { service })?; + let observed:u64=db.query_row("SELECT count(*) FROM agent_update_native_observations WHERE grant_id=?1 AND effect_key=?2",params![grant.to_string(),key],|r|r.get(0))?; + let nonstart:u64=db.query_row("SELECT count(*) FROM agent_update_native_nonstarts WHERE grant_id=?1 AND effect_key=?2",params![grant.to_string(),key],|r|r.get(0))?; + let conditional = if service == NativeService::General { + db.query_row("SELECT count(*) FROM agent_update_native_observations WHERE grant_id=?1 AND effect_key=?2",params![grant.to_string(),native_key(&NativeEffect::ConditionalRollbackStopGeneral)?],|r|r.get::<_,u64>(0))? + } else { + 0 + }; + ensure!( + observed + nonstart + conditional == 1, + "UPDATE_ROLLBACK_STOP_NOT_PROVED" + ); + } + } + Evidence::CandidatePreserved { role, .. } => { + require_observed(NativeEffect::SaveRejectedCandidate { + role: to_native_file(*role), + })? + } + Evidence::PredecessorRestored { role, .. } => { + require_observed(NativeEffect::RestorePredecessor { + role: to_native_file(*role), + })? + } + Evidence::RollbackServicesStarted { .. } => { + for service in native_services { + require_observed(NativeEffect::RollbackStartService { service })?; + } + } _ => {} } guard.verify_update_event(&handoff, &read_delegation(db, grant)?, event) @@ -597,6 +694,54 @@ pub fn finalize( tx.commit()?; Ok(()) } +/// A recovered predecessor is a FAILED candidate update, never success for the +/// requested target. Existing consumption/outcome/PKI/floor are not reset. +pub fn finalize_rollback( + db: &mut Connection, + guard: &impl UpdateParticipantGuard, + permit: &ExecutionPermitPayload, + expected_head: &str, + observed_at: u64, + outcome: &Outcome, +) -> Result<()> { + ensure!( + guard.participant() == Participant::RestoredHelper + && outcome.state == "failed" + && outcome.error_code == "AGENT_UPDATE_RETURNED_TO_PREDECESSOR" + && !outcome.reboot_required, + "UPDATE_ROLLBACK_FINAL_OUTCOME" + ); + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + let mut journal = read(&tx, guard, permit.grant_id)?; + ensure!( + journal.phase() == Phase::RollbackHealthVerified && journal.head_sha256() == expected_head, + "UPDATE_ROLLBACK_FINAL_HEAD" + ); + let records:u64=tx.query_row("SELECT count(*) FROM agent_update_native_claims WHERE grant_id=?1 AND effect_key IN ('save_installed_record','publish_installed_record')", + [permit.grant_id.to_string()],|r|r.get(0))?; + ensure!(records == 0, "UPDATE_ROLLBACK_RECORD_ALREADY_CHANGED"); + let bytes = ledger::persist_outcome_in(&tx, permit, outcome)?; + let event = journal.prepare( + observed_at, + Evidence::RollbackOutcomePersisted { + outcome_sha256: hash(&bytes), + }, + )?; + let parsed: Event = serde_json::from_slice(&event)?; + verify_writer(&tx, guard, permit.grant_id, &parsed)?; + journal.replay(&event)?; + append_in( + &tx, + permit.grant_id, + expected_head, + &event, + &parsed, + &journal, + )?; + guard.check(&tx)?; + tx.commit()?; + Ok(()) +} /// Persist uncertainty once under the same original consumption. No release, /// new start, result rewrite or transition back out of Unknown is provided. @@ -666,6 +811,15 @@ fn phase_name(phase: Phase) -> &'static str { Phase::LedgerReturnIntent => "ledger_return_intent", Phase::ServicesStarted => "services_started", Phase::HealthVerified => "health_verified", + Phase::RollbackIntent => "rollback_intent", + Phase::RollbackStopped => "rollback_stopped", + Phase::RestoreIntent => "restore_intent", + Phase::CandidatePreserved => "candidate_preserved", + Phase::PredecessorRestored => "predecessor_restored", + Phase::PredecessorVerified => "predecessor_verified", + Phase::RollbackLedgerReturnIntent => "rollback_ledger_return_intent", + Phase::RollbackServicesStarted => "rollback_services_started", + Phase::RollbackHealthVerified => "rollback_health_verified", Phase::OutcomePersisted => "outcome_persisted", Phase::Unknown => "unknown", } @@ -699,6 +853,17 @@ pub enum NativeEffect { old_sha256: String, new_sha256: String, }, + RollbackStopService { + service: NativeService, + }, + SaveRejectedCandidate { + role: NativeFileRole, + }, + RollbackStartService { + service: NativeService, + }, + RollbackReturnLedger, + ConditionalRollbackStopGeneral, } #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "snake_case")] @@ -725,6 +890,9 @@ pub enum NativeEffectStage { Claim, Observe, ReserveFutureStart, + ObserveNonstart, + ReserveConditionalStop, + ObserveRetainedStart, } #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(deny_unknown_fields)] @@ -755,6 +923,12 @@ impl NativeEffectClaim { pub struct PreparedReturnClaims { pub return_ledger: NativeEffectClaim, pub starts: [NativeEffectClaim; N], + conditional_rollback_stop_general: Option, +} +impl PreparedReturnClaims { + pub fn conditional_rollback_stop_general(&self) -> Option<&NativeEffectClaim> { + self.conditional_rollback_stop_general.as_ref() + } } fn native_context( db: &Connection, @@ -791,6 +965,18 @@ fn native_role(role: NativeFileRole) -> rmm_updater_handoff::FileRole { NativeFileRole::WindowsTimeProfile => F::WindowsTimeProfile, } } +fn to_native_file(role: rmm_updater_handoff::FileRole) -> NativeFileRole { + use rmm_updater_handoff::FileRole as F; + match role { + F::AgentBinary => NativeFileRole::AgentBinary, + F::HelperBinary => NativeFileRole::HelperBinary, + F::LinuxFullConfig => NativeFileRole::FullConfig, + F::LinuxFullPin => NativeFileRole::FullPin, + F::WindowsSettings => NativeFileRole::WindowsSettings, + F::WindowsPin => NativeFileRole::WindowsPin, + F::WindowsTimeProfile => NativeFileRole::WindowsTimeProfile, + } +} fn native_service(service: NativeService) -> rmm_updater_handoff::Service { use rmm_updater_handoff::Service as S; match service { @@ -820,7 +1006,10 @@ fn insert_native_claim( [h.attempt.grant_id.to_string()], |r| r.get(0), )?; - ensure!(count < 17, "UPDATE_NATIVE_EFFECT_LIMIT"); + ensure!( + count < if h.schema_version == 2 { 32 } else { 17 }, + "UPDATE_NATIVE_EFFECT_LIMIT" + ); let claim = NativeEffectClaim { grant_id: h.attempt.grant_id, owner_nonce: h.owner_nonce, @@ -890,6 +1079,12 @@ pub fn claim_native_effect( && old_sha256 != new_sha256, "UPDATE_NATIVE_INSTALLED_RECORD_BINDING" ); + if h.schema_version == 2 + && h.pin_transition == rmm_updater_handoff::PinTransition::LinuxGeneralFullV1 + { + let denied:u64=tx.query_row("SELECT count(*) FROM agent_update_native_nonstarts WHERE grant_id=?1 AND effect_key=?2",params![grant.to_string(),native_key(&NativeEffect::ConditionalRollbackStopGeneral)?],|r|r.get(0))?; + ensure!(denied == 1, "UPDATE_CONDITIONAL_STOP_NOT_CLOSED"); + } if matches!(effect, NativeEffect::PublishInstalledRecord { .. }) { let save = native_key(&NativeEffect::SaveInstalledRecord { old_sha256: old_sha256.clone(), @@ -900,11 +1095,60 @@ pub fn claim_native_effect( require_matching_record_save(&tx, grant, old_sha256, new_sha256)?; } } + NativeEffect::RollbackStopService { service } => { + ensure!( + h.schema_version == 2 + && j.phase() == Phase::RollbackIntent + && matches!( + guard.participant(), + Participant::NewHelper | Participant::StableUpdater + ) + && h.stop_set.contains(&native_service(*service)), + "UPDATE_ROLLBACK_STOP_ROLE" + ); + } + NativeEffect::SaveRejectedCandidate { role } => { + ensure!( + h.schema_version == 2 + && guard.participant() == Participant::StableUpdater + && j.phase() == Phase::RestoreIntent, + "UPDATE_ROLLBACK_SAVE_ROLE" + ); + let last:Vec=tx.query_row("SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence DESC LIMIT 1",[grant.to_string()],|r|r.get(0))?; + ensure!( + serde_json::from_slice::(&last)?.evidence + == Evidence::RestoreIntent { + role: native_role(*role) + }, + "UPDATE_ROLLBACK_SAVE_FILE" + ); + } + NativeEffect::RestorePredecessor { role } => { + ensure!( + h.schema_version == 2 + && guard.participant() == Participant::StableUpdater + && j.phase() == Phase::CandidatePreserved, + "UPDATE_ROLLBACK_RESTORE_ROLE" + ); + let last:Vec=tx.query_row("SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence DESC LIMIT 1",[grant.to_string()],|r|r.get(0))?; + ensure!( + matches!(serde_json::from_slice::(&last)?.evidence, + Evidence::CandidatePreserved{role:r,..} if r==native_role(*role)), + "UPDATE_ROLLBACK_RESTORE_FILE" + ); + let save = native_key(&NativeEffect::SaveRejectedCandidate { role: *role })?; + let observed:u64=tx.query_row("SELECT count(*) FROM agent_update_native_observations WHERE grant_id=?1 AND effect_key=?2",params![grant.to_string(),save],|r|r.get(0))?; + ensure!(observed == 1, "UPDATE_ROLLBACK_CANDIDATE_NOT_PRESERVED"); + } // Rollback needs its own qualified event transition; never infer it // from an uncertain replacement or silently restart the predecessor. - NativeEffect::RestorePredecessor { .. } - | NativeEffect::StartService { .. } - | NativeEffect::ReturnLedger => anyhow::bail!("UPDATE_NATIVE_SPECIAL_TRANSITION_REQUIRED"), + NativeEffect::StartService { .. } + | NativeEffect::ReturnLedger + | NativeEffect::RollbackStartService { .. } + | NativeEffect::RollbackReturnLedger + | NativeEffect::ConditionalRollbackStopGeneral => { + anyhow::bail!("UPDATE_NATIVE_SPECIAL_TRANSITION_REQUIRED") + } } guard.verify_native_effect(&h, &d, effect, NativeEffectStage::Claim)?; let result = insert_native_claim(&tx, &h, expected_head, effect.clone(), now)?; @@ -912,6 +1156,281 @@ pub fn claim_native_effect( tx.commit()?; Ok(result) } +/// Known startup/health failure only, still the original consumed attempt. +/// Nonstarts are separately attested under the actual guard; no failed start +/// is mislabeled a successful service observation and no old claim is erased. +pub fn begin_rollback( + db: &mut Connection, + guard: &impl UpdateParticipantGuard, + grant: Uuid, + expected_head: &str, + cause: &rmm_updater_handoff::RollbackCause, + failure_observation: &str, + now: DateTime, +) -> Result<()> { + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + begin_rollback_in( + &tx, + guard, + grant, + expected_head, + cause, + failure_observation, + now, + )?; + guard.check(&tx)?; + tx.commit()?; + Ok(()) +} +fn begin_rollback_in( + tx: &Connection, + guard: &impl UpdateParticipantGuard, + grant: Uuid, + expected_head: &str, + cause: &rmm_updater_handoff::RollbackCause, + failure_observation: &str, + now: DateTime, +) -> Result<()> { + ensure!( + rmm_domain::agent_update::digest(failure_observation), + "UPDATE_ROLLBACK_FAILURE_OBSERVATION" + ); + let (handoff, delegation, mut journal) = native_context(tx, guard, grant, now)?; + ensure!( + handoff.schema_version == 2 + && journal.head_sha256() == expected_head + && matches!( + guard.participant(), + Participant::NewHelper | Participant::StableUpdater + ), + "UPDATE_ROLLBACK_ORIGINAL_ROLE" + ); + let raw = journal.prepare( + u64::try_from(now.timestamp())?, + Evidence::RollbackIntent { + cause: cause.clone(), + failure_observation_sha256: failure_observation.into(), + }, + )?; + let event: Event = serde_json::from_slice(&raw)?; + verify_writer(tx, guard, grant, &event)?; + let mut query=tx.prepare("SELECT c.effect_key,c.claim,c.claim_sha256 FROM agent_update_native_claims c LEFT JOIN agent_update_native_observations o ON o.grant_id=c.grant_id AND o.effect_key=c.effect_key LEFT JOIN agent_update_native_nonstarts n ON n.grant_id=c.grant_id AND n.effect_key=c.effect_key WHERE c.grant_id=?1 AND o.claim_sha256 IS NULL AND n.claim_sha256 IS NULL")?; + let rows = query + .query_map([grant.to_string()], |r| { + Ok(( + r.get::<_, String>(0)?, + r.get::<_, Vec>(1)?, + r.get::<_, String>(2)?, + )) + })? + .collect::>>()?; + drop(query); + let mut actual_failed = false; + for (effect_key, raw_claim, claim_sha) in rows { + let claim: NativeEffectClaim = serde_json::from_slice(&raw_claim)?; + match &claim.effect { + NativeEffect::ReturnLedger => {} + NativeEffect::ConditionalRollbackStopGeneral => { + guard.verify_native_effect( + &handoff, + &delegation, + &claim.effect, + NativeEffectStage::ObserveNonstart, + )?; + let observation = guard.native_observation_sha256( + &handoff, + &delegation, + &claim.effect, + NativeEffectStage::ObserveNonstart, + )?; + ensure!( + rmm_domain::agent_update::digest(&observation), + "UPDATE_ROLLBACK_NONSTART_OBSERVATION" + ); + tx.execute("INSERT INTO agent_update_native_nonstarts(grant_id,effect_key,claim_sha256,observation_sha256,observed_at) VALUES(?1,?2,?3,?4,?5)",params![grant.to_string(),effect_key,claim_sha,observation,now.to_rfc3339()])?; + } + NativeEffect::StartService { service } => { + ensure!( + matches!( + cause, + rmm_updater_handoff::RollbackCause::StartupFailed { .. } + ) && native_key(&claim.effect)? == effect_key + && hash(&raw_claim) == claim_sha, + "UPDATE_ROLLBACK_UNOBSERVED_START" + ); + guard.verify_native_effect( + &handoff, + &delegation, + &claim.effect, + NativeEffectStage::ObserveNonstart, + )?; + let observation = guard.native_observation_sha256( + &handoff, + &delegation, + &claim.effect, + NativeEffectStage::ObserveNonstart, + )?; + ensure!( + rmm_domain::agent_update::digest(&observation), + "UPDATE_ROLLBACK_NONSTART_OBSERVATION" + ); + if matches!(cause,rmm_updater_handoff::RollbackCause::StartupFailed{service:s} if *s==native_service(*service)) + { + actual_failed = true; + } + tx.execute("INSERT INTO agent_update_native_nonstarts(grant_id,effect_key,claim_sha256,observation_sha256,observed_at) VALUES(?1,?2,?3,?4,?5)",params![grant.to_string(),effect_key,claim_sha,observation,now.to_rfc3339()])?; + } + _ => anyhow::bail!("UPDATE_ROLLBACK_UNCERTAIN_NATIVE_EFFECT"), + } + } + ensure!( + matches!(cause, rmm_updater_handoff::RollbackCause::HealthRejected) || actual_failed, + "UPDATE_ROLLBACK_KNOWN_FAILURE_REQUIRED" + ); + journal.replay(&raw)?; + append_in(tx, grant, expected_head, &raw, &event, &journal)?; + // A service whose original once-only start is independently known not to + // have happened needs NO STOP effect. Close an exact deny-only subordinate + // claim, not a fabricated successful ControlService/systemctl observation. + let mut query=tx.prepare("SELECT claim FROM agent_update_native_claims c JOIN agent_update_native_nonstarts n ON n.grant_id=c.grant_id AND n.effect_key=c.effect_key WHERE c.grant_id=?1")?; + let nonstarts = query + .query_map([grant.to_string()], |r| r.get::<_, Vec>(0))? + .collect::>>()?; + drop(query); + for raw_claim in nonstarts { + let claim: NativeEffectClaim = serde_json::from_slice(&raw_claim)?; + if let NativeEffect::StartService { service } = claim.effect { + let effect = NativeEffect::RollbackStopService { service }; + guard.verify_native_effect( + &handoff, + &delegation, + &effect, + NativeEffectStage::ObserveNonstart, + )?; + let observation = guard.native_observation_sha256( + &handoff, + &delegation, + &effect, + NativeEffectStage::ObserveNonstart, + )?; + ensure!( + rmm_domain::agent_update::digest(&observation), + "UPDATE_ROLLBACK_STOP_NONSTART_HASH" + ); + let tombstone = + insert_native_claim(tx, &handoff, journal.head_sha256(), effect.clone(), now)?; + tx.execute("INSERT INTO agent_update_native_nonstarts(grant_id,effect_key,claim_sha256,observation_sha256,observed_at) VALUES(?1,?2,?3,?4,?5)",params![grant.to_string(),native_key(&effect)?,hash(&serde_json::to_vec(&tombstone)?),observation,now.to_rfc3339()])?; + } + } + Ok(()) +} +pub struct ReservedGeneralStop<'a> { + pub grant_id: Uuid, + pub expected_head: &'a str, + pub general_start_observation_sha256: &'a str, + pub general_stop_observation_sha256: &'a str, + pub failure_observation_sha256: &'a str, + pub now: DateTime, +} +/// Early Full startup failure: the concrete started General process was +/// stopped using the conditional intent reserved BEFORE the original return. +/// Only the same still-live updater may record retained original START + STOP +/// observations after all readers stopped and the original lock was acquired. +pub fn begin_rollback_after_reserved_stop( + db: &mut Connection, + guard: &impl UpdateParticipantGuard, + input: ReservedGeneralStop<'_>, +) -> Result<()> { + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + let (h, d, journal) = native_context(&tx, guard, input.grant_id, input.now)?; + ensure!( + h.schema_version == 2 + && h.pin_transition == rmm_updater_handoff::PinTransition::LinuxGeneralFullV1 + && guard.participant() == Participant::StableUpdater + && journal.phase() == Phase::LedgerReturnIntent + && journal.head_sha256() == input.expected_head, + "UPDATE_CONDITIONAL_ROLLBACK_CONTEXT" + ); + for (effect, stage, observation) in [ + ( + NativeEffect::StartService { + service: NativeService::General, + }, + NativeEffectStage::ObserveRetainedStart, + input.general_start_observation_sha256, + ), + ( + NativeEffect::ConditionalRollbackStopGeneral, + NativeEffectStage::Observe, + input.general_stop_observation_sha256, + ), + ] { + ensure!( + rmm_domain::agent_update::digest(observation), + "UPDATE_CONDITIONAL_OBSERVATION_HASH" + ); + let claim = read_native_claim(&tx, guard, input.grant_id, &effect)?; + ensure!( + claim.head_sha256 == journal.head_sha256() + && claim.owner_nonce == h.owner_nonce + && input.now >= claim.claimed_at, + "UPDATE_CONDITIONAL_ORIGINAL_CLAIM" + ); + guard.verify_native_effect(&h, &d, &effect, stage)?; + ensure!( + guard.native_observation_sha256(&h, &d, &effect, stage)? == observation, + "UPDATE_CONDITIONAL_RETAINED_OBSERVATION_CHANGED" + ); + tx.execute("INSERT INTO agent_update_native_observations(grant_id,effect_key,claim_sha256,observation_sha256,observed_at) VALUES(?1,?2,?3,?4,?5)",params![input.grant_id.to_string(),native_key(&effect)?,hash(&serde_json::to_vec(&claim)?),observation,input.now.to_rfc3339()])?; + } + begin_rollback_in( + &tx, + guard, + input.grant_id, + input.expected_head, + &rmm_updater_handoff::RollbackCause::StartupFailed { + service: rmm_updater_handoff::Service::LinuxFullHelper, + }, + input.failure_observation_sha256, + input.now, + )?; + guard.check(&tx)?; + tx.commit()?; + Ok(()) +} +/// Deny-only close of a conditional reserve after actual new Full startup. +/// It is not a STOP observation and never dispatches/resumes the reservation. +pub fn deny_conditional_stop( + db: &mut Connection, + guard: &impl UpdateParticipantGuard, + grant: Uuid, + observation: &str, + now: DateTime, +) -> Result<()> { + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + let (h, d, journal) = native_context(&tx, guard, grant, now)?; + ensure!( + h.schema_version == 2 + && guard.participant() == Participant::NewHelper + && matches!( + journal.phase(), + Phase::ServicesStarted | Phase::HealthVerified + ), + "UPDATE_CONDITIONAL_DENY_ROLE" + ); + let effect = NativeEffect::ConditionalRollbackStopGeneral; + let claim = read_native_claim(&tx, guard, grant, &effect)?; + guard.verify_native_effect(&h, &d, &effect, NativeEffectStage::ObserveNonstart)?; + ensure!( + guard.native_observation_sha256(&h, &d, &effect, NativeEffectStage::ObserveNonstart)? + == observation, + "UPDATE_CONDITIONAL_DENY_OBSERVATION" + ); + tx.execute("INSERT INTO agent_update_native_nonstarts(grant_id,effect_key,claim_sha256,observation_sha256,observed_at) VALUES(?1,?2,?3,?4,?5)",params![grant.to_string(),native_key(&effect)?,hash(&serde_json::to_vec(&claim)?),observation,now.to_rfc3339()])?; + guard.check(&tx)?; + tx.commit()?; + Ok(()) +} pub fn observe_native_effect( db: &mut Connection, guard: &impl UpdateParticipantGuard, @@ -966,8 +1485,43 @@ pub fn observe_native_effect( ) } NativeEffect::ReturnLedger | NativeEffect::RestorePredecessor { .. } => { + if matches!(claim.effect, NativeEffect::RestorePredecessor { .. }) { + ensure!( + h.schema_version == 2 + && guard.participant() == Participant::StableUpdater + && j.phase() == Phase::CandidatePreserved + && j.head_sha256() == claim.head_sha256, + "UPDATE_ROLLBACK_FILE_OBSERVER" + ); + } else { + anyhow::bail!("UPDATE_NATIVE_OBSERVATION_NOT_SUPPORTED"); + } + } + NativeEffect::RollbackStopService { .. } => ensure!( + h.schema_version == 2 + && guard.participant() == Participant::StableUpdater + && j.phase() == Phase::RollbackIntent, + "UPDATE_ROLLBACK_STOP_OBSERVER" + ), + NativeEffect::SaveRejectedCandidate { .. } => ensure!( + h.schema_version == 2 + && guard.participant() == Participant::StableUpdater + && j.phase() == Phase::RestoreIntent + && j.head_sha256() == claim.head_sha256, + "UPDATE_ROLLBACK_FILE_OBSERVER" + ), + NativeEffect::RollbackStartService { .. } => ensure!( + h.schema_version == 2 + && guard.participant() == Participant::RestoredHelper + && j.phase() == Phase::RollbackLedgerReturnIntent, + "UPDATE_ROLLBACK_START_OBSERVER" + ), + NativeEffect::RollbackReturnLedger => { anyhow::bail!("UPDATE_NATIVE_OBSERVATION_NOT_SUPPORTED") } + NativeEffect::ConditionalRollbackStopGeneral => { + anyhow::bail!("UPDATE_NATIVE_SPECIAL_TRANSITION_REQUIRED") + } } guard.verify_native_effect(&h, &d, &claim.effect, NativeEffectStage::Observe)?; tx.execute("INSERT INTO agent_update_native_observations(grant_id,effect_key,claim_sha256,observation_sha256,observed_at) VALUES(?1,?2,?3,?4,?5)",params![claim.grant_id.to_string(),native_key(&claim.effect)?,hash(&raw),observation_sha256,now.to_rfc3339()])?; @@ -1079,6 +1633,16 @@ fn prepare_return_for( NativeEffectStage::ReserveFutureStart, )?; } + let conditional = h.schema_version == 2 + && h.pin_transition == rmm_updater_handoff::PinTransition::LinuxGeneralFullV1; + if conditional { + guard.verify_native_effect( + &h, + &d, + &NativeEffect::ConditionalRollbackStopGeneral, + NativeEffectStage::ReserveConditionalStop, + )?; + } j.replay(return_event)?; append_in(&tx, grant, expected_head, return_event, &parsed, &j)?; let return_ledger = @@ -1093,13 +1657,148 @@ fn prepare_return_for( now, )?); } + let conditional_rollback_stop_general = if conditional { + Some(insert_native_claim( + &tx, + &h, + j.head_sha256(), + NativeEffect::ConditionalRollbackStopGeneral, + now, + )?) + } else { + None + }; + guard.check(&tx)?; + tx.commit()?; + Ok(PreparedReturnClaims { + return_ledger, + conditional_rollback_stop_general, + starts: starts + .try_into() + .map_err(|_| anyhow::anyhow!("UPDATE_NATIVE_START_COUNT"))?, + }) +} +pub fn prepare_rollback_return_and_starts( + db: &mut Connection, + guard: &impl UpdateParticipantGuard, + grant: Uuid, + expected_head: &str, + return_event: &[u8], + now: DateTime, +) -> Result { + prepare_rollback_return_for( + db, + guard, + grant, + expected_head, + return_event, + now, + [ + NativeService::General, + NativeService::Full, + NativeService::Agent, + ], + ) +} +pub fn prepare_windows_rollback_return_and_starts( + db: &mut Connection, + guard: &impl UpdateParticipantGuard, + grant: Uuid, + expected_head: &str, + return_event: &[u8], + now: DateTime, +) -> Result> { + prepare_rollback_return_for( + db, + guard, + grant, + expected_head, + return_event, + now, + [NativeService::WindowsHelper, NativeService::WindowsAgent], + ) +} +fn prepare_rollback_return_for( + db: &mut Connection, + guard: &impl UpdateParticipantGuard, + grant: Uuid, + expected_head: &str, + return_event: &[u8], + now: DateTime, + sequence: [NativeService; N], +) -> Result> { + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + let (h, d, mut journal) = native_context(&tx, guard, grant, now)?; + ensure!( + h.schema_version == 2 + && guard.participant() == Participant::StableUpdater + && journal.phase() == Phase::PredecessorVerified + && journal.head_sha256() == expected_head, + "UPDATE_ROLLBACK_RETURN_ROLE" + ); + ensure!( + (sequence.as_slice() + == [ + NativeService::General, + NativeService::Full, + NativeService::Agent + ] + && h.pin_transition == rmm_updater_handoff::PinTransition::LinuxGeneralFullV1) + || (sequence.as_slice() == [NativeService::WindowsHelper, NativeService::WindowsAgent] + && h.pin_transition == rmm_updater_handoff::PinTransition::WindowsCodePairClockV1), + "UPDATE_ROLLBACK_RETURN_SERVICE_SET" + ); + let missing:u64=tx.query_row("SELECT count(*) FROM agent_update_native_claims c LEFT JOIN agent_update_native_observations o ON o.grant_id=c.grant_id AND o.effect_key=c.effect_key LEFT JOIN agent_update_native_nonstarts n ON n.grant_id=c.grant_id AND n.effect_key=c.effect_key WHERE c.grant_id=?1 AND o.claim_sha256 IS NULL AND n.claim_sha256 IS NULL AND c.effect_key<>?2", + params![grant.to_string(),native_key(&NativeEffect::ReturnLedger)?],|r|r.get(0))?; + ensure!(missing == 0, "UPDATE_ROLLBACK_UNOBSERVED_EFFECT"); + let event: Event = serde_json::from_slice(return_event)?; + ensure!( + event.evidence == Evidence::RollbackLedgerReturnIntent + && event.observed_at == u64::try_from(now.timestamp())?, + "UPDATE_ROLLBACK_RETURN_EVENT" + ); + verify_writer(&tx, guard, grant, &event)?; + guard.verify_native_effect( + &h, + &d, + &NativeEffect::RollbackReturnLedger, + NativeEffectStage::Claim, + )?; + for service in sequence { + guard.verify_native_effect( + &h, + &d, + &NativeEffect::RollbackStartService { service }, + NativeEffectStage::ReserveFutureStart, + )?; + } + journal.replay(return_event)?; + append_in(&tx, grant, expected_head, return_event, &event, &journal)?; + let return_ledger = insert_native_claim( + &tx, + &h, + journal.head_sha256(), + NativeEffect::RollbackReturnLedger, + now, + )?; + let mut starts = Vec::new(); + for service in sequence { + starts.push(insert_native_claim( + &tx, + &h, + journal.head_sha256(), + NativeEffect::RollbackStartService { service }, + now, + )?); + } guard.check(&tx)?; tx.commit()?; Ok(PreparedReturnClaims { return_ledger, starts: starts .try_into() - .map_err(|_| anyhow::anyhow!("UPDATE_NATIVE_START_COUNT"))?, + .map_err(|_| anyhow::anyhow!("UPDATE_ROLLBACK_START_COUNT"))?, + conditional_rollback_stop_general: None, }) } @@ -1158,6 +1857,14 @@ pub struct NativeControlReply { /// Only ReadOriginal; exact retained execution_evidence.start bytes, /// bounded to 512KiB before canonical Base64url encoding, never rewritten. pub original_start_base64url: Option, + /// ReadOriginal only: exact applied helper_sync_decisions.request protobuf + /// and response JSON SignedResponse bytes. Both absent until that actual + /// ORIGINAL inflight control acknowledgement has been applied. Evidence + /// only: never a caller ready bit, replacement request or new authority. + #[serde(default,skip_serializing_if="Option::is_none")] + pub pre_stop_sync_request_base64url:Option, + #[serde(default,skip_serializing_if="Option::is_none")] + pub pre_stop_sync_response_base64url:Option, } fn require_matching_record_save( diff --git a/crates/agent-core/src/update_health_sync.rs b/crates/agent-core/src/update_health_sync.rs index ba8f314..e2024a5 100644 --- a/crates/agent-core/src/update_health_sync.rs +++ b/crates/agent-core/src/update_health_sync.rs @@ -251,7 +251,10 @@ fn services_prefix( let (observed, journal) = verify_inflight_proof(&proof, key).map_err(anyhow::Error::msg)?; ensure!( observed.bytes()? == header.bytes()? - && journal.phase() == Phase::ServicesStarted + && matches!( + journal.phase(), + Phase::ServicesStarted | Phase::RollbackServicesStarted + ) && local.grant_id == header.attempt.grant_id.to_string() && local.consumption == "consumed" && local.state == "running" diff --git a/crates/agent/src/agent_update_apply.rs b/crates/agent/src/agent_update_apply.rs new file mode 100644 index 0000000..2e8d7ef --- /dev/null +++ b/crates/agent/src/agent_update_apply.rs @@ -0,0 +1,392 @@ +//! Approved artifact prefetch before a grant; Full independently admits the handoff. +use anyhow::{Context, Result, ensure}; +use rmm_domain::{ + agent_update::{ + APPLY_CAPABILITY, ArtifactDescriptor, ArtifactRole, CHECK_CAPABILITY, Platform, + }, + jobs::{Action, JobPayload}, +}; +use rmm_privileged_helper::{Binding, Request, control, update_admission::Accepted}; +use sha2::{Digest, Sha256}; +use std::{ + fs::{self, File, OpenOptions}, + io::{Read, Write}, + os::unix::fs::{MetadataExt, OpenOptionsExt}, + path::{Path, PathBuf}, + time::{Duration, Instant}, +}; +const MAXIMUM: u64 = 256 * 1024 * 1024; +const CHUNK: u64 = 512 * 1024; +pub struct Prefetch(pub tokio::task::JoinHandle>); +impl Drop for Prefetch { + fn drop(&mut self) { + self.0.abort(); + } +} +fn directory(path: &Path, create: bool) -> Result<()> { + if create && !path.try_exists()? { + std::fs::DirBuilder::new().mode(0o700).create(path)?; + } + let m = fs::symlink_metadata(path)?; + ensure!( + m.is_dir() && !m.file_type().is_symlink() && m.uid() == 993 && m.mode() & 0o777 == 0o700, + "UPDATE_PREFETCH_DIRECTORY" + ); + Ok(()) +} +use std::os::unix::fs::DirBuilderExt; +fn parent(job: &JobPayload) -> Result { + ensure!( + unsafe { libc::geteuid() } == 993, + "UPDATE_PREFETCH_AGENT_UID" + ); + let base = Path::new("/var/lib/rmm-agent-remote"); + directory(base, false)?; + let base = base.join("update-downloads"); + directory(&base, true)?; + let base = base.join(job.job_id.to_string()); + directory(&base, true)?; + let base = base.join(job.attempt_id.to_string()); + directory(&base, true)?; + Ok(base) +} +fn label(a: &ArtifactDescriptor) -> &'static str { + match a.role { + ArtifactRole::Agent => "agent.bin", + ArtifactRole::Helper => "helper.bin", + } +} +fn checked(path: &Path, a: &ArtifactDescriptor) -> Result<()> { + let mut f = OpenOptions::new() + .read(true) + .custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC) + .open(path)?; + let m = f.metadata()?; + ensure!( + m.is_file() + && m.uid() == 993 + && m.mode() & 0o777 == 0o600 + && m.nlink() == 1 + && m.len() == a.bytes, + "UPDATE_PREFETCH_FILE" + ); + let mut h = Sha256::new(); + let mut n = 0; + let mut buf = [0; 65536]; + loop { + let size = f.read(&mut buf)?; + if size == 0 { + break; + } + n += size as u64; + ensure!(n <= a.bytes, "UPDATE_PREFETCH_LENGTH"); + h.update(&buf[..size]); + } + ensure!( + n == a.bytes && format!("{:x}", h.finalize()) == a.sha256, + "UPDATE_PREFETCH_DIGEST" + ); + Ok(()) +} +pub async fn prefetch(client: reqwest::Client, base: String, job: JobPayload) -> Result<()> { + let Action::UpdateAgent { update } = &job.action else { + anyhow::bail!("UPDATE_ACTION_REQUIRED") + }; + let manifest = update.manifest().map_err(anyhow::Error::msg)?; + ensure!( + manifest.validate() && manifest.platform == Platform::LinuxX64, + "UPDATE_PREFETCH_MANIFEST" + ); + let pjob = job.clone(); + let folder = tokio::task::spawn_blocking(move || parent(&pjob)).await??; + let deadline = Instant::now() + Duration::from_secs(600); + for artifact in manifest.artifacts { + ensure!( + artifact.bytes > 0 && artifact.bytes <= MAXIMUM, + "UPDATE_PREFETCH_LIMIT" + ); + let path = folder.join(label(&artifact)); + let existing = path.clone(); + let desc = artifact.clone(); + if tokio::task::spawn_blocking(move || -> Result { + if existing.try_exists()? { + checked(&existing, &desc)?; + Ok(true) + } else { + Ok(false) + } + }) + .await?? + { + continue; + } + let stage = folder.join(format!("{}.partial", label(&artifact))); + let stage_path = stage.clone(); + let mut file = tokio::task::spawn_blocking(move || { + OpenOptions::new() + .write(true) + .create_new(true) + .mode(0o600) + .custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC) + .open(stage_path) + }) + .await??; + let mut offset = 0; + let mut hash = Sha256::new(); + while offset < artifact.bytes { + ensure!( + Instant::now() < deadline && chrono::Utc::now() < job.start_before, + "UPDATE_PREFETCH_EXPIRED" + ); + let length = CHUNK.min(artifact.bytes - offset); + let url = format!( + "{base}/agent/v1/update-artifacts/{}?job_id={}&attempt_id={}&offset={offset}&length={length}", + artifact.artifact_id, job.job_id, job.attempt_id + ); + let mut response = client.get(&url).send().await?; + let partial = offset != 0 || length != artifact.bytes; + ensure!( + response.url().as_str() == url + && response.status() + == if partial { + reqwest::StatusCode::PARTIAL_CONTENT + } else { + reqwest::StatusCode::OK + }, + "UPDATE_PREFETCH_HTTP" + ); + let h = response.headers(); + ensure!( + h.get("x-artifact-sha256").and_then(|v| v.to_str().ok()) + == Some(artifact.sha256.as_str()) + && h.get("content-type").and_then(|v| v.to_str().ok()) + == Some("application/octet-stream") + && !h.contains_key("content-encoding") + && response.content_length() == Some(length), + "UPDATE_PREFETCH_HEADERS" + ); + if partial { + ensure!( + h.get("content-range").and_then(|v| v.to_str().ok()) + == Some( + format!( + "bytes {}-{}/{}", + offset, + offset + length - 1, + artifact.bytes + ) + .as_str() + ), + "UPDATE_PREFETCH_RANGE" + ); + } else { + ensure!(!h.contains_key("content-range"), "UPDATE_PREFETCH_RANGE"); + } + let mut bytes = Vec::with_capacity(length as usize); + while let Some(chunk) = response.chunk().await? { + ensure!( + bytes.len() + chunk.len() <= length as usize, + "UPDATE_PREFETCH_BODY_LIMIT" + ); + bytes.extend_from_slice(&chunk); + } + ensure!( + bytes.len() == length as usize && Instant::now() < deadline, + "UPDATE_PREFETCH_BODY_LENGTH" + ); + hash.update(&bytes); + file = tokio::task::spawn_blocking(move || -> Result { + file.write_all(&bytes)?; + Ok(file) + }) + .await??; + offset += length; + } + ensure!( + format!("{:x}", hash.finalize()) == artifact.sha256, + "UPDATE_PREFETCH_DIGEST" + ); + let dir = folder.clone(); + tokio::task::spawn_blocking(move || -> Result<()> { + file.sync_all()?; + drop(file); + fs::hard_link(&stage, &path)?; + fs::remove_file(stage)?; + File::open(dir)?.sync_all()?; + checked(&path, &artifact) + }) + .await??; + } + Ok(()) +} +/// No Outcome is invented on acceptance; loss after write stays ambiguous. +pub fn handoff(binding: &Binding, request: &Request, grant: uuid::Uuid) -> Result { + let socket = std::env::var_os("RMM_FULL_MAINTENANCE_HELPER_SOCKET") + .context("UPDATE_FULL_SOCKET_REQUIRED")?; + let pin = + std::env::var_os("RMM_FULL_MAINTENANCE_POLICY_FILE").context("UPDATE_FULL_PIN_REQUIRED")?; + let pin = control::GeneralPin::read(Path::new(&pin), binding)?; + ensure!( + pin.profile == "full_maintenance_v1", + "UPDATE_FULL_PROFILE_REQUIRED" + ); + control::general_probe(Path::new(&socket), binding, &pin)?; + let ack: Accepted = + control::exchange(Path::new(&socket), request, Duration::from_secs(50), 4096)?; + ack.matches(request, grant)?; + Ok(ack) +} + +/// Opaque public metadata authenticated by the current protected Full peer, +/// with complete guarded history verified there. No caller JSON constructor. +#[derive(Clone)] +pub struct GuardedRecord { + raw: Vec, + observation: rmm_domain::agent_update::AgentUpdateObservation, +} +impl GuardedRecord { + pub fn bytes(&self, policy_raw: &[u8], keys_raw: &[u8]) -> Result<&[u8]> { + use rmm_domain::agent_update::ObservationQuality; + let o = &self.observation; + let age = chrono::Utc::now() - o.observed_at; + let p = o + .predecessor + .as_ref() + .context("UPDATE_CATALOG_PREDECESSOR")?; + let u = o.updater.as_ref().context("UPDATE_CATALOG_UPDATER")?; + ensure!( + o.validate() + && o.quality == ObservationQuality::Valid + && o.platform == Platform::LinuxX64 + && age >= chrono::Duration::zero() + && age <= chrono::Duration::seconds(90) + && rmm_domain::jobs::hash(policy_raw) == u.policy_sha256 + && rmm_domain::jobs::hash(keys_raw) == u.keyring_sha256 + && rmm_domain::jobs::hash(&self.raw) == p.installed_record_sha256, + "UPDATE_CATALOG_GUARDED_RECORD" + ); + Ok(&self.raw) + } +} +/// Passive cached Full observations only; never a live MCP collection. +type Observation = rmm_domain::agent_update::AgentUpdateObservation; +struct Observing { + receiver: tokio::sync::oneshot::Receiver)>>, + task: tokio::task::JoinHandle<()>, +} +impl Drop for Observing { + fn drop(&mut self) { + self.task.abort(); + } +} +#[derive(Default)] +pub struct Observer { + current: Option, + record: Option, + pending: Option, +} +impl Observer { + pub fn cached(&self) -> Option<&Observation> { + let value = self.current.as_ref()?; + let age = chrono::Utc::now() - value.observed_at; + (value.validate() + && age >= chrono::Duration::zero() + && age <= chrono::Duration::seconds(90)) + .then_some(value) + } + pub fn poll( + &mut self, + binding: Option<&Binding>, + candidate: Option<(rmm_domain::agent_update::ReleaseEnvelope, u64)>, + ) -> Observation { + if let Some(pending) = &mut self.pending { + match pending.receiver.try_recv() { + Ok(Ok((value, raw))) if value.validate() => { + self.record = Some(GuardedRecord { + raw, + observation: value.clone(), + }); + self.current = Some(value); + self.pending = None; + } + Ok(_) | Err(tokio::sync::oneshot::error::TryRecvError::Closed) => { + self.current = None; + self.record = None; + self.pending = None; + } + Err(tokio::sync::oneshot::error::TryRecvError::Empty) => {} + } + } + if self.pending.is_none() + && let Some(binding) = binding.cloned() + { + let (sender, receiver) = tokio::sync::oneshot::channel(); + let task = tokio::task::spawn_blocking(move || { + let (envelope, generation) = + candidate.map_or((None, None), |(e, g)| (Some(e), Some(g))); + let value = rmm_privileged_helper::update_admission::observe_record( + Path::new("/run/rmm-helper-maintenance/control.sock"), + &binding, + envelope, + generation, + ); + let _ = sender.send(value); + }); + self.pending = Some(Observing { receiver, task }); + } + self.cached() + .cloned() + .unwrap_or_else(|| Observation::not_configured(Platform::LinuxX64, chrono::Utc::now())) + } + pub fn catalog_record(&self) -> Option { + self.cached()?; + self.record.clone() + } + pub fn capabilities(&self) -> Vec { + let Some(value) = self + .cached() + .filter(|v| v.quality == rmm_domain::agent_update::ObservationQuality::Valid) + else { + return vec![]; + }; + let (Some(updater), Some(predecessor)) = (&value.updater, &value.predecessor) else { + return vec![]; + }; + vec![ + APPLY_CAPABILITY.into(), + CHECK_CAPABILITY.into(), + "update_agent".into(), + "root:update_agent".into(), + "helper.root.v1".into(), + format!("updater.binary.sha256:{}", updater.binary_sha256), + format!("updater.policy.sha256:{}", updater.policy_sha256), + format!("helper.binary.sha256:{}", predecessor.helper_sha256), + ] + } +} +pub fn supported(binding: &Binding, job: &JobPayload) -> bool { + (|| -> Result { + ensure!( + unsafe { libc::geteuid() } == 993 && job.execution_identity == "root", + "UPDATE_AGENT_PRINCIPAL" + ); + let Action::UpdateAgent { update } = &job.action else { + return Ok(false); + }; + let observation = rmm_privileged_helper::update_admission::observe( + Path::new("/run/rmm-helper-maintenance/control.sock"), + binding, + Some(update.release_envelope.clone()), + Some(update.updater.keyring_generation), + )?; + Ok(observation.validate() + && observation.quality == rmm_domain::agent_update::ObservationQuality::Valid + && observation.predecessor.as_ref() == Some(&update.predecessor) + && observation.updater.as_ref() == Some(&update.updater) + && observation.candidate_previews.iter().any(|c| { + c.manifest_sha256 == update.manifest_sha256 + && c.peer_pin_transition == update.peer_pin_transition + })) + })() + .unwrap_or(false) +} diff --git a/crates/agent/src/agent_update_catalog.rs b/crates/agent/src/agent_update_catalog.rs index 4d2bea6..0b8829d 100644 --- a/crates/agent/src/agent_update_catalog.rs +++ b/crates/agent/src/agent_update_catalog.rs @@ -62,7 +62,7 @@ pub fn observation(report: Report, collected_at: DateTime) -> serde_json::V "error_code":error,"data":report}) } struct Pending { - receiver: oneshot::Receiver, + receiver: oneshot::Receiver<(Report, Option)>, task: tokio::task::JoinHandle<()>, } impl Drop for Pending { @@ -73,10 +73,21 @@ impl Drop for Pending { #[derive(Default)] pub struct Checker { report: Option, + release: Option, started: Option, pending: Option, } impl Checker { + /// Exact authenticated envelope, for the independent read-only Full + /// verifier. This cache never confers admission or a capability itself. + pub fn candidate_envelope(&self, now: DateTime) -> Option<(ReleaseEnvelope, u64)> { + let report = self.cached(now)?; + let candidate = report.candidate?; + let envelope = self.release.as_ref()?; + let decoded = envelope.decode().ok()?; + (hash(&decoded.payload_bytes) == candidate.manifest_sha256) + .then(|| (envelope.clone(), candidate.keyring_generation)) + } pub fn cached(&self, now: DateTime) -> Option { self.report .as_ref() @@ -90,17 +101,20 @@ impl Checker { binding: Option<&Binding>, clock: TrustedClock, address: SocketAddr, + guarded: Option, client: impl FnOnce() -> Result, ) -> Report { if let Some(pending) = &mut self.pending { match pending.receiver.try_recv() { - Ok(report) => { + Ok((report, release)) => { self.report = Some(report); + self.release = release; self.pending.take(); } Err(oneshot::error::TryRecvError::Empty) => {} Err(oneshot::error::TryRecvError::Closed) => { self.report = Some(empty(Status::Unavailable)); + self.release = None; self.pending.take(); } } @@ -123,6 +137,7 @@ impl Checker { Path::new(POLICY), Path::new(KEYRING), Path::new(RECORD), + guarded.as_ref(), ) }) .transpose(); @@ -148,7 +163,7 @@ impl Checker { let report = result .ok() .and_then(Result::ok) - .unwrap_or_else(|| empty(Status::Unavailable)); + .unwrap_or_else(|| (empty(Status::Unavailable), None)); let _ = sender.send(report); }); self.pending = Some(Pending { receiver, task }); @@ -254,7 +269,13 @@ struct Trust { generation: u64, } impl Trust { - fn load(binding: &Binding, policy: &Path, keys: &Path, record: &Path) -> Result> { + fn load( + binding: &Binding, + policy: &Path, + keys: &Path, + record: &Path, + guarded: Option<&crate::agent_update_apply::GuardedRecord>, + ) -> Result> { let result = (|| -> Result { let policy = Protected::read(policy, 65536)?; let (local, _) = @@ -264,12 +285,22 @@ impl Trust { "UPDATE_CATALOG_FIXED_ANCHOR_PATHS" ); let keys = Protected::read(keys, 16384)?; - let record = Protected::read(record, 32768)?; - let result = Self::verify(binding, &policy.raw, &keys.raw, &record.raw)?; - Ok(Self { - files: vec![policy, keys, record], - ..result - }) + if let Some(guarded) = guarded { + let raw = guarded.bytes(&policy.raw, &keys.raw)?; + let result = + Self::verify_guarded(binding, &policy.raw, &keys.raw, raw, Some(guarded))?; + Ok(Self { + files: vec![policy, keys], + ..result + }) + } else { + let record = Protected::read(record, 32768)?; + let result = Self::verify(binding, &policy.raw, &keys.raw, &record.raw)?; + Ok(Self { + files: vec![policy, keys, record], + ..result + }) + } })(); match result { Ok(value) => Ok(Some(value)), @@ -288,6 +319,15 @@ impl Trust { policy_raw: &[u8], keys_raw: &[u8], record_raw: &[u8], + ) -> Result { + Self::verify_guarded(binding, policy_raw, keys_raw, record_raw, None) + } + fn verify_guarded( + binding: &Binding, + policy_raw: &[u8], + keys_raw: &[u8], + record_raw: &[u8], + guarded: Option<&crate::agent_update_apply::GuardedRecord>, ) -> Result { let (policy, _) = LocalUpdatePolicy::from_bytes(policy_raw).map_err(anyhow::Error::msg)?; ensure!( @@ -322,14 +362,23 @@ impl Trust { .initial_baseline .decode() .map_err(anyhow::Error::msg)?; - // Only the independently signed bootstrap can currently authenticate - // the floor here. Later Updated records require the separate guarded - // Full history observer; do not guess a floor from parsed root JSON. ensure!( - baseline.payload_bytes == record_raw - && hash(record_raw) == policy.initial_record_sha256, - "UPDATE_CATALOG_GUARDED_HISTORY_REQUIRED" + hash(&baseline.payload_bytes) == policy.initial_record_sha256, + "UPDATE_CATALOG_BASELINE_BYTES" ); + if let Some(guarded) = guarded { + ensure!( + guarded.bytes(policy_raw, keys_raw)? == record_raw, + "UPDATE_CATALOG_GUARDED_HISTORY_REQUIRED" + ); + } else { + ensure!( + baseline.payload_bytes == record_raw, + "UPDATE_CATALOG_GUARDED_HISTORY_REQUIRED" + ); + } + let installed = + InstalledReleaseRecord::from_bytes(record_raw).map_err(anyhow::Error::msg)?; let signer = keys .keys .iter() @@ -347,8 +396,7 @@ impl Trust { Verifier::new_without_digest(&public)?.verify_oneshot(&baseline.signature, &signed)?, "UPDATE_CATALOG_BASELINE_SIGNATURE" ); - let decoded = baseline - .record + let decoded = installed .release_envelope .decode() .map_err(anyhow::Error::msg)?; @@ -358,7 +406,7 @@ impl Trust { protocol: decoded.manifest.protocol_major, state: decoded.manifest.state_schema, generation: keys.generation, - record: baseline.record, + record: installed, }; result.release(&result.record.release_envelope, result.record.activated_at)?; Ok(result) @@ -458,14 +506,17 @@ async fn request( address: SocketAddr, trust: &Trust, clock: &TrustedClock, -) -> Result { +) -> Result<(Report, Option)> { ensure!(clock.trusted(), "UPDATE_CATALOG_CLOCK_UNTRUSTED"); let bytes = fetch(client, address).await?; ensure!(clock.trusted(), "UPDATE_CATALOG_CLOCK_UNTRUSTED"); for file in &trust.files { file.recheck()?; } - trust.response(&bytes, Utc::now()) + let report = trust.response(&bytes, Utc::now())?; + let response: CatalogResponse = serde_json::from_slice(&bytes)?; + let release = response.release.map(|r| r.release_envelope); + Ok((report, release)) } async fn fetch(client: &reqwest::Client, address: SocketAddr) -> Result> { @@ -750,6 +801,7 @@ mod tests { None, TrustedClock::new(), "127.0.0.1:1".parse().unwrap(), + None, || panic!("missing anchor must not load device TLS keys"), ); assert_eq!(report.status, Status::NotConfigured); @@ -757,6 +809,7 @@ mod tests { None, TrustedClock::new(), "127.0.0.1:1".parse().unwrap(), + None, || panic!("cadence must not create client"), ); assert_eq!(report.checked_at, again.checked_at); diff --git a/crates/agent/src/agent_update_health.rs b/crates/agent/src/agent_update_health.rs index 45f50fe..2d26f6f 100644 --- a/crates/agent/src/agent_update_health.rs +++ b/crates/agent/src/agent_update_health.rs @@ -222,7 +222,8 @@ pub fn context( 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 { + let rollback = journal.phase() == Phase::RollbackServicesStarted; + if journal.phase() != Phase::ServicesStarted && !rollback { return Ok(None); } ensure!( @@ -241,7 +242,8 @@ pub fn context( .context("UPDATE_HEALTH_SERVICES_STARTED")?, )?; ensure!( - matches!(event.evidence, Evidence::ServicesStarted { .. }), + matches!(event.evidence, Evidence::ServicesStarted { .. }) && !rollback + || matches!(event.evidence, Evidence::RollbackServicesStarted { .. }) && rollback, "UPDATE_HEALTH_SERVICES_STARTED" ); let (last, generation, fault, pending): (Option, u64, Option, Option) = @@ -424,8 +426,14 @@ pub async fn request( .map_err(anyhow::Error::msg)?; journal.prepare( u64::try_from(now.timestamp())?, - Evidence::HealthVerified { - receipt: signed.clone(), + if journal.phase() == Phase::RollbackServicesStarted { + Evidence::RollbackHealthVerified { + receipt: signed.clone(), + } + } else { + Evidence::HealthVerified { + receipt: signed.clone(), + } }, )?; ensure!(Instant::now() < context.deadline, "UPDATE_HEALTH_EXPIRED"); diff --git a/crates/agent/src/jobs.rs b/crates/agent/src/jobs.rs index d0c5784..d45bad6 100644 --- a/crates/agent/src/jobs.rs +++ b/crates/agent/src/jobs.rs @@ -24,6 +24,9 @@ pub fn supported(job: &JobPayload) -> bool { ) } pub fn supported_with_binding(job: &JobPayload, binding: Option<&Binding>) -> bool { + if matches!(job.action, Action::UpdateAgent { .. }) { + return binding.is_some_and(|b| crate::agent_update_apply::supported(b, job)); + } if rmm_privileged_helper::apt::is_apt_action(job) { // SAFETY: geteuid has no preconditions. return unsafe { libc::geteuid() } != 0 diff --git a/crates/agent/src/main.rs b/crates/agent/src/main.rs index 8927de3..b601efc 100644 --- a/crates/agent/src/main.rs +++ b/crates/agent/src/main.rs @@ -1,3 +1,4 @@ +mod agent_update_apply; mod agent_update_catalog; mod agent_update_health; mod apt; @@ -62,6 +63,7 @@ struct Agent { certificates: certificate_rotation::RotationStore, rotation_policy: certificate_rotation::RotationPolicy, catalog: agent_update_catalog::Checker, + update_observer: agent_update_apply::Observer, } #[derive(Default)] struct EffectFence { @@ -428,6 +430,13 @@ impl Agent { || binding.verify(&request, Utc::now()).is_err() { jobs::no_start("failed", "START_WINDOW_OR_CLOCK_INVALID_BEFORE_EFFECT") + } else if matches!(job.action, rmm_domain::jobs::Action::UpdateAgent { .. }) { + match agent_update_apply::handoff(&binding, &request, grant.grant_id) { + // Full now owns this original. The stable updater may + // stop us. Reconciliation alone supplies its final result. + Ok(_) => return, + Err(_) => rmm_privileged_helper::Outcome::unknown("UPDATE_HANDOFF_UNCONFIRMED"), + } } else if job.execution_identity == "root" { apt::invoke_with_control(&binding, &job, &request, || { ensure!( @@ -483,6 +492,9 @@ impl Agent { } capabilities.extend(apt::capabilities(binding)); capabilities.extend(helper_sync::general_capabilities(binding)); + if self.clock.trusted() { + capabilities.extend(self.update_observer.capabilities()); + } } capabilities.sort(); capabilities.dedup(); @@ -530,18 +542,15 @@ impl Agent { if !storage::passive_collection_ready(&self.db)? { return Ok(()); } - // Until the separately qualified protected updater/helper observer is - // provisioned, report its real absence; never infer apply readiness from - // this schema support or the agent's own code/build version. - let observation = rmm_domain::agent_update::AgentUpdateObservation::not_configured( - rmm_domain::agent_update::Platform::LinuxX64, - Utc::now(), - ); let address = self.address; let ca = &self.ca; let certificates = &self.certificates; - self.catalog - .poll(self.binding.as_ref(), self.clock.clone(), address, || { + self.catalog.poll( + self.binding.as_ref(), + self.clock.clone(), + address, + self.update_observer.catalog_record(), + || { let identity = certificates.current(Utc::now().timestamp())?; https_channel::mtls_client( address, @@ -549,7 +558,12 @@ impl Agent { &identity.certificate_pem, &identity.private_key_pem, ) - }); + }, + ); + let observation = self.update_observer.poll( + self.binding.as_ref(), + self.catalog.candidate_envelope(Utc::now()), + ); let data = serde_json::to_vec(&serde_json::json!({"agent_update":observation}))?; let envelope = self.envelope( generation, @@ -660,6 +674,8 @@ impl Agent { let mut first_heartbeat = true; let mut health_task: Option = None; let mut health_inventory_prefix: Option = None; + let mut update_prefetch: Option<(JobOffer, agent_update_apply::Prefetch)> = None; + let mut prefetched = std::collections::HashSet::new(); loop { tokio::select! { Some(result) = self.inventory_rx.recv() => { @@ -742,6 +758,13 @@ impl Agent { } _ = tick.tick() => { self.replay(&ws, generation).await?; + if update_prefetch.as_ref().is_some_and(|(_,task)|task.0.is_finished()) { + let (offer, mut task)=update_prefetch.take().context("UPDATE_PREFETCH_REQUIRED")?; + match (&mut task.0).await { + Ok(Ok(())) => {prefetched.insert((offer.job_id.clone(),offer.attempt_id.clone()));}, + _ => {self.record(generation,&offer,"",jobs::no_start("failed","UPDATE_ARTIFACT_PREFETCH_FAILED"))?;needs_sync=true;} + } + } 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); @@ -749,7 +772,7 @@ impl Agent { if let Some(proof)=&update { let decoded=proof.decode().map_err(anyhow::Error::msg)?; let event:Option=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 event.is_some_and(|event|matches!(event.evidence,rmm_updater_handoff::Evidence::ServicesStarted{..}|rmm_updater_handoff::Evidence::RollbackServicesStarted{..})) { 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. @@ -760,6 +783,13 @@ impl Agent { } } } + // Planning pins must be refreshed before the 90-second stored-data + // planning limit. One inflight blocking collector bounds pressure; + // heartbeats never perform this metadata work. + if update_at.elapsed() >= Duration::from_secs(60) { + self.queue_update_observation(generation)?; + update_at = Instant::now(); + } 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||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)) { @@ -773,6 +803,15 @@ impl Agent { } else if !self.ready(generation,job.execution_identity=="root") { needs_sync=true; } else if Utc::now() >= job.not_before { + if matches!(job.action,rmm_domain::jobs::Action::UpdateAgent{..}) && !prefetched.contains(&(offer.job_id.clone(),offer.attempt_id.clone())) { + if update_prefetch.is_none() { + 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 base=format!("https://{}:{}",https_channel::server_name()?,self.address.port()); + update_prefetch=Some((offer.clone(),agent_update_apply::Prefetch(tokio::spawn(agent_update_apply::prefetch(client,base,job))))); + } + continue; + } // Reserve before send. A lost response must never cause // a second permit request for this attempt. jobs::reserve_request(&self.db, &offer)?; @@ -808,13 +847,6 @@ impl Agent { heartbeat_at = Instant::now(); heartbeat_after = Duration::from_millis(rand::random_range(27_000..33_000)); } - // Planning pins must be refreshed before the 90-second stored-data - // planning limit. One inflight blocking collector bounds pressure; - // heartbeats never perform this metadata work. - if update_at.elapsed() >= Duration::from_secs(60) { - self.queue_update_observation(generation)?; - update_at = Instant::now(); - } if inventory_at.elapsed() >= Duration::from_secs(60) { self.queue_inventory(); inventory_at = Instant::now(); @@ -1017,6 +1049,7 @@ async fn main() -> Result<()> { certificates, rotation_policy, catalog: agent_update_catalog::Checker::default(), + update_observer: agent_update_apply::Observer::default(), }; agent.recover_interrupted()?; let mut backoff = 1_u64; diff --git a/crates/agent/src/runtime_tests.rs b/crates/agent/src/runtime_tests.rs index 2c1a0ab..7334ade 100644 --- a/crates/agent/src/runtime_tests.rs +++ b/crates/agent/src/runtime_tests.rs @@ -151,6 +151,7 @@ fn new_process_persists_interruption_before_helper_io_without_unlock_or_repeat() certificates, rotation_policy: certificate_rotation::RotationPolicy::default(), catalog: agent_update_catalog::Checker::default(), + update_observer: agent_update_apply::Observer::default(), }; // A forged local consumption/start cannot become a purported grant result. agent @@ -539,6 +540,7 @@ async fn run(mode: wire_channel::Mode, trusted: bool, forged: bool, rounds: u64) certificates, rotation_policy: certificate_rotation::RotationPolicy::default(), catalog: agent_update_catalog::Checker::default(), + update_observer: agent_update_apply::Observer::default(), }; for _ in 0..rounds { assert!( diff --git a/crates/privileged-helper/src/lib.rs b/crates/privileged-helper/src/lib.rs index 172dd2a..af42b7a 100644 --- a/crates/privileged-helper/src/lib.rs +++ b/crates/privileged-helper/src/lib.rs @@ -9,8 +9,10 @@ pub mod readiness; pub mod reconcile; #[cfg(test)] mod tests; +pub mod update_admission; pub mod update_control; pub mod update_guard; +pub mod update_observer; use anyhow::{Context, Result, ensure}; use chrono::{DateTime, Utc}; use openssl::pkey::{Id, PKey}; diff --git a/crates/privileged-helper/src/main.rs b/crates/privileged-helper/src/main.rs index ea09630..0d36917 100644 --- a/crates/privileged-helper/src/main.rs +++ b/crates/privileged-helper/src/main.rs @@ -15,7 +15,7 @@ use std::{ net::{UnixListener, UnixStream}, }, }, - path::PathBuf, + path::{Path, PathBuf}, time::Duration, }; @@ -133,6 +133,7 @@ impl Config { #[derive(Deserialize)] #[serde(untagged)] enum Incoming { + UpdateObserve(rmm_privileged_helper::update_observer::Request), NativeUpdate(rmm_agent_core::update_handoff::NativeControlRequest), Control(control::Request), Probe(rmm_privileged_helper::readiness::Request), @@ -170,6 +171,17 @@ fn policy(config: &Config, job: &rmm_domain::jobs::JobPayload) -> Result<()> { job.execution_identity == "root", "HELPER_REQUIRES_EXPLICIT_ROOT_IDENTITY" ); + if matches!(job.action, Action::UpdateAgent { .. }) { + ensure!( + config.profile == HelperProfile::FullMaintenanceV1 + && config.agent_uid == 993 + && config.socket + == std::path::Path::new("/run/rmm-helper-maintenance/control.sock") + && std::path::Path::new("/etc/rmm-agent-updater/policy.json").try_exists()?, + "UPDATE_REQUIRES_SEPARATELY_PROVISIONED_FULL" + ); + return Ok(()); + } if rmm_privileged_helper::apt::is_apt_action(job) { ensure!( config.profile == HelperProfile::AptOnly, @@ -386,6 +398,44 @@ fn handle( ensure!(size <= 512 * 1024, "JOB_REQUEST_SIZE"); request } + Incoming::UpdateObserve(request) => { + peer_allowed(stream, config.agent_uid)?; + ensure!( + size <= 65536 + && config.profile == HelperProfile::FullMaintenanceV1 + && config.agent_uid == 993 + && config.socket == Path::new("/run/rmm-helper-maintenance/control.sock"), + "UPDATE_OBSERVER_ONLY_FULL" + ); + let guard = reconcile::ExecutorGuard::acquire(&config.state, 0)?; + let key_path = config + .signing_key_file + .as_deref() + .unwrap_or(Path::new("/etc/rmm-agent-updater/job-signing-key.json")); + let policy = config.general_policy()?.sha256()?; + let response = rmm_privileged_helper::update_observer::collect( + db, + rmm_privileged_helper::update_observer::Local { + executor: &guard, + binding: &config.binding, + clock: update_clock.ok_or_else(|| anyhow::anyhow!("UPDATE_NOT_CONFIGURED"))?, + sync, + sync_clock, + key_path, + policy_sha256: &policy, + helper_sha256: binary_sha256 + .as_deref() + .ok_or_else(|| anyhow::anyhow!("UPDATE_FULL_BINARY_PIN_REQUIRED"))?, + }, + &request, + )?; + peer_allowed(stream, config.agent_uid)?; + let bytes = serde_json::to_vec(&response)?; + ensure!(bytes.len() <= 192 * 1024, "UPDATE_OBSERVER_REPLY_LIMIT"); + stream.write_all(&u32::try_from(bytes.len())?.to_be_bytes())?; + stream.write_all(&bytes)?; + return Ok(()); + } Incoming::Control(request) => { peer_allowed(stream, config.agent_uid)?; let (challenge, identity) = request.binding(); @@ -534,6 +584,65 @@ fn handle( // Both profiles use these exact same inodes, locked per effect rather than // daemon lifetime so the dedicated and general helper can coexist. let resource = reconcile::ExecutorGuard::acquire(&config.state, 0)?; + if matches!(job.action, Action::UpdateAgent { .. }) { + let consumed: bool = db.query_row("SELECT EXISTS(SELECT 1 FROM consumed WHERE grant_id=?1 OR (agent=?2 AND generation=?3 AND job=?4 AND attempt=?5))", + rusqlite::params![permit.grant_id.to_string(),permit.agent_id.to_string(),permit.enrollment_generation,permit.job_id.to_string(),permit.attempt_id.to_string()],|r|r.get(0))?; + ensure!(!consumed, "UPDATE_ALREADY_CONSUMED_NO_REDISPATCH"); + let key_path = config + .signing_key_file + .clone() + .unwrap_or_else(|| PathBuf::from("/etc/rmm-agent-updater/job-signing-key.json")); + private_path(&key_path, 0, false)?; + ensure!( + fs::metadata(&key_path)?.len() <= 4096, + "UPDATE_JOB_KEY_RECORD_LIMIT" + ); + let key = rmm_privileged_helper::SigningKey::read(&key_path, 0)?; + ensure!( + key.public_key_hex == config.binding.public_key_hex, + "UPDATE_LOCAL_JOB_KEY_CHANGED" + ); + let key_hash = rmm_domain::jobs::hash(&fs::read(&key_path)?); + let prepared = rmm_privileged_helper::update_admission::Prepared::new( + db, + rmm_privileged_helper::update_admission::Local { + executor: &resource, + binding: &config.binding, + clock: update_clock.ok_or_else(|| anyhow::anyhow!("UPDATE_NOT_CONFIGURED"))?, + job_key_path: &key_path, + job_key_raw_sha256: &key_hash, + }, + &request, + )?; + peer_allowed(stream, config.agent_uid)?; + let mut probe = 0u8; + // Preparation may copy sizeable exact artifacts. Recheck the actual + // caller at the original consumption boundary without consuming bytes. + // SAFETY: live socket and a writable byte; MSG_PEEK leaves input intact. + let available = unsafe { + libc::recv( + stream.as_raw_fd(), + (&mut probe as *mut u8).cast(), + 1, + libc::MSG_PEEK | libc::MSG_DONTWAIT, + ) + }; + ensure!( + available < 0 + && std::io::Error::last_os_error().kind() == std::io::ErrorKind::WouldBlock, + "UPDATE_CALLER_CANCELLED_OR_DISCONNECTED" + ); + let accepted = prepared.consume(db, sync, sync_clock)?; + let bytes = serde_json::to_vec(&accepted)?; + ensure!(bytes.len() <= 4096, "UPDATE_HANDOFF_ACK_LIMIT"); + stream.write_all(&u32::try_from(bytes.len())?.to_be_bytes())?; + stream.write_all(&bytes)?; + stream.flush()?; + // Full resumes its listener immediately after this once-only Activate + // reply. The worker then reads this same original ledger via Full before + // it may stop Agent/General/Full. This is never a final job Outcome. + return prepared.dispatch_after_reply(); + } let previous: Option>> = db .query_row( "SELECT COALESCE(result,(SELECT outcome FROM execution_evidence WHERE execution_evidence.grant_id=consumed.grant_id)) FROM consumed WHERE grant_id=?1", diff --git a/crates/privileged-helper/src/update_admission.rs b/crates/privileged-helper/src/update_admission.rs new file mode 100644 index 0000000..82b74dd --- /dev/null +++ b/crates/privileged-helper/src/update_admission.rs @@ -0,0 +1,767 @@ +//! PRE-consumption Full admission. Protected local provisioning and exact +//! approved artifact bytes are verified before the ordinary HelperSync gate +//! atomically writes the original consumption/start/handoff/delegation. +use crate::{ + Binding, Request, + reconcile::{self, ExecutorGuard}, +}; +use anyhow::{Context, Result, ensure}; +use chrono::{DateTime, Utc}; +use rmm_agent_core::{ + helper_reconcile::ExecutorLedgerGuard, + reconcile::SyncClock, + update_handoff::{PreparedHandoff, UpdateAdmissionGuard}, +}; +use rmm_domain::{ + agent_update::{ + ParticipantIdentity, ParticipantRole, PeerPinTransition, ResolvedAgentUpdate, + UpdateDelegation, + }, + jobs::{Action, JobPayload, hash}, +}; +use rmm_updater_handoff::{ + AttemptBinding, CodePair, FileRole, FileTransition, Handoff, PinTransition, +}; +use rmm_updater_linux::{ + Budget, + files::{Directory, HeldFile, Identity, LedgerIdentity, Role, StageDescriptor, Staged}, + ipc, + services::{HeldService, Service, ServicePolicy, Systemd}, +}; +use rmm_updater_worker::{ + bundle, + clock::NativeClock, + files::FileWitnesses, + full::FullFiles, + reader_registry::QualifiedRegistry, + retained::{RetainedFileSpec, RetainedSet, Retention}, + trust::TrustState, +}; +use rusqlite::Connection; +use serde::{Deserialize, Serialize}; +use std::{ + cell::RefCell, + ffi::OsStr, + fs::{File, OpenOptions}, + os::unix::{ + fs::{MetadataExt, OpenOptionsExt}, + net::UnixStream, + }, + path::{Path, PathBuf}, +}; +use uuid::Uuid; + +/// Nonterminal acknowledgement only. Agent must keep the original job running +/// without storing an Outcome; the worker may now stop the Agent and Full. +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Accepted { + pub schema_version: u32, + pub operation: String, + pub grant_id: Uuid, + pub owner_nonce: Uuid, + pub handoff_sha256: String, + pub original_request_sha256: String, + pub ready: bool, + pub terminal: bool, +} +impl Accepted { + pub fn matches(&self, request: &Request, grant: Uuid) -> Result<()> { + ensure!( + self.schema_version == 1 + && self.operation == "agent_update_handoff_accepted_v1" + && self.grant_id == grant + && !self.owner_nonce.is_nil() + && rmm_domain::agent_update::digest(&self.handoff_sha256) + && self.original_request_sha256 == hash(&serde_json::to_vec(request)?) + && !self.ready + && !self.terminal, + "UPDATE_TYPED_HANDOFF_ACK" + ); + Ok(()) + } +} +struct Protected { + directory: Directory, + file: HeldFile, + path: PathBuf, + maximum: u64, +} +impl Protected { + fn read(path: &Path, maximum: u64) -> Result<(Self, Vec)> { + let directory = + Directory::open(path.parent().context("UPDATE_ADMISSION_PARENT")?, 0, false)?; + let mut file = directory.inspect( + path.file_name().context("UPDATE_ADMISSION_NAME")?, + 0, + maximum, + )?; + let bytes = file.bytes(maximum)?; + Ok(( + Self { + directory, + file, + path: path.into(), + maximum, + }, + bytes, + )) + } + fn verify(&self) -> Result<()> { + self.directory.file( + self.path.file_name().context("UPDATE_ADMISSION_NAME")?, + self.file.proof(), + self.maximum, + )?; + Ok(()) + } +} +fn identity(m: &std::fs::Metadata) -> Identity { + Identity { + device: m.dev(), + inode: m.ino(), + uid: m.uid(), + gid: m.gid(), + mode: m.mode(), + links: m.nlink(), + } +} +fn participant(service: &HeldService) -> Result { + let p = service.proof(); + ensure!(p.process.uid == 0, "UPDATE_ADMISSION_ROOT_PARTICIPANT"); + if p.service == Service::Updater { + ensure!(p.process.gid == 0, "UPDATE_STABLE_UPDATER_GROUP"); + } + let role = match p.service { + Service::Full => ParticipantRole::LinuxFullHelper, + Service::Updater => ParticipantRole::LinuxUpdater, + _ => anyhow::bail!("UPDATE_ADMISSION_PARTICIPANT_ROLE"), + }; + Ok(ParticipantIdentity { + role, + pid: p.process.pid, + birth: p.process.birth, + image_sha256: p.process.image_sha256.clone(), + security_principal: "uid:0".into(), + service_instance_sha256: hash(&serde_json::to_vec(p)?), + }) +} +pub struct Local<'a> { + pub executor: &'a ExecutorGuard, + pub binding: &'a Binding, + pub clock: &'a NativeClock, + pub job_key_path: &'a Path, + pub job_key_raw_sha256: &'a str, +} +pub struct Prepared<'a> { + local: Local<'a>, + request: Request, + update: ResolvedAgentUpdate, + header: Handoff, + delegation: UpdateDelegation, + trust: TrustState, + registry: QualifiedRegistry, + files: FileWitnesses, + retained: RetainedSet, + ledger: LedgerIdentity, + ledger_file: File, + ledger_directory: Directory, + sqlite_handle: usize, + full: HeldService, + updater: HeldService, + full_files: RefCell, + _stages: [Staged; 4], + protected: Vec, + budget: Budget, +} +impl<'a> Prepared<'a> { + pub fn new(db: &Connection, local: Local<'a>, request: &Request) -> Result { + local.executor.check(db)?; + let (job, permit) = local.binding.verify(request, local.clock.now()?)?; + let Action::UpdateAgent { update } = &job.action else { + anyhow::bail!("UPDATE_ACTION_REQUIRED") + }; + ensure!( + job.execution_identity == "root" && update.validate() && job.max_runtime_seconds == 600, + "UPDATE_ADMISSION_ACTION" + ); + let trust = TrustState::load_prepared(update, local.binding, local.clock)?; + trust.qualify_history(db, local.executor, local.binding)?; + let boot: Uuid = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")? + .trim() + .parse()?; + // This is preparation time within the existing start window, not a new + // 600s execution lease. The immutable 600s handoff is stamped last. + let budget = Budget::new(boot, (permit.start_before - local.clock.now()?).to_std()?)?; + let registry = QualifiedRegistry::load(trust.policy())?; + registry.verify_admission(trust.policy(), &budget)?; + let policies = trust + .policy() + .services + .iter() + .zip([ + Service::Agent, + Service::General, + Service::Full, + Service::Apt, + ]) + .map(|(pin, service)| ServicePolicy { + service, + unit_sha256: pin.unit_sha256.clone(), + image_sha256: if service == Service::Agent { + update.predecessor.agent_sha256.clone() + } else { + update.predecessor.helper_sha256.clone() + }, + }) + .collect(); + let services = Systemd::new(policies)?; + let full = services.observe(Service::Full, &budget)?; + ensure!( + full.proof().process.pid == std::process::id(), + "UPDATE_ACTUAL_FULL_PROCESS_REQUIRED" + ); + let updater = Systemd::observe_updater( + ServicePolicy { + service: Service::Updater, + image_sha256: update.updater.binary_sha256.clone(), + unit_sha256: trust.policy().updater_service.unit_sha256.clone(), + }, + &budget, + )?; + let description = ipc::exchange( + UnixStream::connect("/run/rmm-agent-updater/control.sock")?, + &updater, + &budget, + &ipc::Request::Describe {}, + )?; + ensure!( + matches!(description.state, ipc::State::Described) + && description.updater == participant(&updater)? + && description.head_sha256.is_none(), + "UPDATE_ADMISSION_LIVE_UPDATER" + ); + let nonce = Uuid::new_v4(); + let manifest = update.manifest().map_err(anyhow::Error::msg)?; + let agent = stage_download( + &job, + Role::Agent, + &manifest.artifacts[0].sha256, + manifest.artifacts[0].bytes, + nonce, + &budget, + )?; + let helper = stage_download( + &job, + Role::Helper, + &manifest.artifacts[1].sha256, + manifest.artifacts[1].bytes, + nonce, + &budget, + )?; + let mut full_files = FullFiles::open( + update, + local.job_key_path, + local.job_key_raw_sha256, + &budget, + )?; + let [full_config, full_pin] = full_files.stage(nonce, &budget)?; + let stages = [agent, helper, full_config, full_pin]; + let files = FileWitnesses::capture(&stages, nonce, &budget)?; + let (retained_config, raw) = Protected::read( + Path::new("/etc/rmm-agent-updater/retained-files.json"), + 65536, + )?; + let mut specs: Vec = serde_json::from_slice(&raw)?; + for (path, retention) in [ + ( + "/var/lib/rmm-agent-remote/journal.sqlite", + Retention::MutableDatabase, + ), + ( + "/var/lib/rmm-agent-remote/consumption.sqlite", + Retention::MutableDatabase, + ), + ( + "/var/lib/rmm-agent-remote/agent.lock", + Retention::ImmutableMetadata, + ), + ( + "/var/lib/rmm-helper/consumption.sqlite", + Retention::MutableDatabase, + ), + ( + "/var/lib/rmm-helper/executor.lock", + Retention::ImmutableMetadata, + ), + ( + "/var/lib/rmm-agent-remote/identity.json", + Retention::ImmutableMetadata, + ), + ( + "/var/lib/rmm-agent-remote/device.key", + Retention::ImmutableMetadata, + ), + ( + "/var/lib/rmm-agent-remote/device.pem", + Retention::ImmutableMetadata, + ), + ] { + ensure!( + specs + .iter() + .any(|s| s.path == Path::new(path) && s.retention == retention), + "UPDATE_MANDATORY_RETAINED_STATE" + ); + } + ensure!( + specs.iter().any( + |s| s.path == local.job_key_path && s.retention == Retention::ImmutableMetadata + ), + "UPDATE_RETAINED_JOB_KEY_REQUIRED" + ); + retain_active_tls(&mut specs, local.binding)?; + let retained = RetainedSet::capture(specs)?; + let ledger_directory = Directory::open(Path::new("/var/lib/rmm-helper"), 0, true)?; + let ledger_file = OpenOptions::new() + .read(true) + .custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC) + .open("/var/lib/rmm-helper/consumption.sqlite")?; + let ledger = LedgerIdentity { + ledger: identity(&ledger_file.metadata()?), + lock: identity(&local.executor._file.metadata()?), + }; + ensure!( + ledger.ledger + == identity(&std::fs::symlink_metadata( + "/var/lib/rmm-helper/consumption.sqlite" + )?) + && ledger.lock + == identity(&std::fs::symlink_metadata( + "/var/lib/rmm-helper/executor.lock" + )?), + "UPDATE_ADMISSION_LEDGER_IDENTITIES" + ); + let (general, raw) = + Protected::read(Path::new("/etc/rmm-remote/general-helper-proof.json"), 8192)?; + let PeerPinTransition::LinuxGeneralFullV1 { + general_pin_sha256, + full: transition, + } = &update.peer_pin_transition + else { + anyhow::bail!("UPDATE_FULL_TRANSITION_REQUIRED") + }; + ensure!( + hash(&raw) == *general_pin_sha256, + "UPDATE_GENERAL_PIN_CHANGED" + ); + let (helper_journal,checkpoint,decision,agent_journal,generation):(String,u64,String,String,u64)=db.query_row( + "SELECT journal_id,checkpoint,last_decision,agent_journal_id,generation FROM helper_sync_meta WHERE id=1 AND fault IS NULL",[], + |r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?)))?; + let mut stop_set = vec![ + rmm_updater_handoff::Service::LinuxAgent, + rmm_updater_handoff::Service::LinuxGeneralHelper, + rmm_updater_handoff::Service::LinuxFullHelper, + ]; + if trust + .policy() + .services + .iter() + .any(|pin| pin.unit == Service::Apt.unit()) + { + stop_set.push(rmm_updater_handoff::Service::LinuxAptHelper); + } + let now = local.clock.now()?; + let consumed_at = u64::try_from(now.timestamp())?; + let start = rmm_agent_core::ledger::start_witness_bytes(&permit, request, boot, now)?; + let header = Handoff { + schema_version: 2, + attempt: AttemptBinding { + tenant_id: permit.tenant_id, + asset_id: permit.asset_id, + agent_id: permit.agent_id, + enrollment_generation: permit.enrollment_generation, + assignment_version: permit.assignment_version, + plan_id: permit + .agent_update_plan_id + .context("UPDATE_PLAN_ID_REQUIRED")?, + plan_sha256: permit.plan_hash.clone(), + job_id: permit.job_id, + attempt_id: permit.attempt_id, + job_payload_sha256: permit.job_payload_hash.clone(), + grant_id: permit.grant_id, + authorization_id: permit.authorization_id, + original_request_sha256: hash(&serde_json::to_vec(request)?), + original_start_witness_sha256: hash(&start), + }, + agent_journal_id: agent_journal.parse()?, + helper_journal_id: helper_journal.parse()?, + helper_checkpoint: checkpoint, + previous_decision_id: decision.parse()?, + boot_id: boot, + connection_generation: generation, + owner_nonce: nonce, + updater_sha256: update.updater.binary_sha256.clone(), + updater_policy_sha256: update.updater.policy_sha256.clone(), + predecessor_record_sha256: update.predecessor.installed_record_sha256.clone(), + retained_state_sha256: retained.sha256().into(), + ledger_identity_sha256: ledger.sha256()?, + staged_files_sha256: files.sha256().into(), + predecessor: CodePair { + manifest_sha256: update.predecessor.manifest_sha256.clone(), + agent_sha256: update.predecessor.agent_sha256.clone(), + helper_sha256: update.predecessor.helper_sha256.clone(), + pin_sha256: transition.old_pin_sha256.clone(), + }, + target: CodePair { + manifest_sha256: update.manifest_sha256.clone(), + agent_sha256: manifest.artifacts[0].sha256.clone(), + helper_sha256: manifest.artifacts[1].sha256.clone(), + pin_sha256: transition.new_pin_sha256.clone(), + }, + pin_transition: PinTransition::LinuxGeneralFullV1, + pin_transition_sha256: hash(&serde_json::to_vec(&update.peer_pin_transition)?), + files: vec![ + FileTransition { + role: FileRole::AgentBinary, + old_sha256: update.predecessor.agent_sha256.clone(), + new_sha256: manifest.artifacts[0].sha256.clone(), + }, + FileTransition { + role: FileRole::HelperBinary, + old_sha256: update.predecessor.helper_sha256.clone(), + new_sha256: manifest.artifacts[1].sha256.clone(), + }, + FileTransition { + role: FileRole::LinuxFullConfig, + old_sha256: transition.old_config_sha256.clone(), + new_sha256: transition.new_config_sha256.clone(), + }, + FileTransition { + role: FileRole::LinuxFullPin, + old_sha256: transition.old_pin_sha256.clone(), + new_sha256: transition.new_pin_sha256.clone(), + }, + ], + stop_set, + consumed_at, + finish_before: consumed_at + .checked_add(600) + .context("UPDATE_FINISH_OVERFLOW")?, + }; + let delegation = UpdateDelegation { + schema_version: 1, + boot_id: boot, + owner_nonce: nonce, + handoff_sha256: hash(&header.bytes()?), + original_helper: participant(&full)?, + updater: participant(&updater)?, + prepared_at: now, + }; + bundle::write_before_handoff( + &header, + &files, + &retained, + &ledger, + bundle::Participants { + delegation: &delegation, + original_full: &full, + updater: &updater, + budget: &budget, + }, + )?; + let prepared = Self { + local, + request: request.clone(), + update: update.as_ref().clone(), + header, + delegation, + trust, + registry, + files, + retained, + ledger, + ledger_file, + ledger_directory, + sqlite_handle: unsafe { db.handle() } as usize, + full, + updater, + full_files: RefCell::new(full_files), + _stages: stages, + protected: vec![retained_config, general], + budget, + }; + prepared.check(db)?; + Ok(prepared) + } + pub fn consume( + &self, + db: &mut Connection, + sync: &mut reconcile::HelperSync, + clock: &reconcile::NativeClock, + ) -> Result { + let prepared = PreparedHandoff { + request: &self.request, + handoff: &self.header, + delegation: &self.delegation, + recorded_at: self.delegation.prepared_at, + }; + sync.begin_update_handoff( + db, + self, + self.header.connection_generation, + &prepared, + clock, + &reconcile::NativeBoot, + )?; + Ok(Accepted { + schema_version: 1, + operation: "agent_update_handoff_accepted_v1".into(), + grant_id: self.header.attempt.grant_id, + owner_nonce: self.header.owner_nonce, + handoff_sha256: hash(&self.header.bytes()?), + original_request_sha256: self.header.attempt.original_request_sha256.clone(), + ready: false, + terminal: false, + }) + } + /// Exactly once, AFTER the caller flushed Accepted and before returning to + /// Full's listener. A transport error is not retried and creates no Outcome. + pub fn dispatch_after_reply(self) -> Result<()> { + let response = ipc::exchange( + UnixStream::connect("/run/rmm-agent-updater/control.sock")?, + &self.updater, + &self.budget, + &ipc::Request::Activate { + grant_id: self.header.attempt.grant_id, + owner_nonce: self.header.owner_nonce, + handoff_sha256: hash(&self.header.bytes()?), + }, + )?; + ensure!( + matches!(response.state, ipc::State::Inflight), + "UPDATE_ACTIVATION_ACK_REQUIRED" + ); + Ok(()) + } +} +impl ExecutorLedgerGuard for Prepared<'_> { + fn check(&self, db: &Connection) -> Result<()> { + self.local.executor.check(db)?; + ensure!( + unsafe { db.handle() } as usize == self.sqlite_handle, + "UPDATE_PREPARED_SAME_CONNECTION" + ); + self.ledger_directory.verify()?; + ensure!( + identity(&self.ledger_file.metadata()?) == self.ledger.ledger + && identity(&std::fs::symlink_metadata( + "/var/lib/rmm-helper/consumption.sqlite" + )?) == self.ledger.ledger + && identity(&self.local.executor._file.metadata()?) == self.ledger.lock + && identity(&std::fs::symlink_metadata( + "/var/lib/rmm-helper/executor.lock" + )?) == self.ledger.lock, + "UPDATE_PREPARED_SAME_LEDGER_AND_LOCK" + ); + self.budget.check() + } +} +impl UpdateAdmissionGuard for Prepared<'_> { + fn health_public_key(&self) -> Result<[u8; 32]> { + Ok(self.trust.health_public_key()) + } + fn verify_prepared_update( + &self, + request: &Request, + handoff: &Handoff, + delegation: &UpdateDelegation, + now: DateTime, + ) -> Result<()> { + ensure!( + serde_json::to_vec(request)? == serde_json::to_vec(&self.request)? + && handoff.bytes()? == self.header.bytes()? + && serde_json::to_vec(delegation)? == serde_json::to_vec(&self.delegation)? + && now == self.delegation.prepared_at, + "UPDATE_PREPARED_ORIGINAL_BYTES_CHANGED" + ); + self.budget.check()?; + self.local + .binding + .verify(request, self.local.clock.now()?)?; + self.trust + .recheck_prepared(&self.update, self.local.clock)?; + self.registry.verify_protected()?; + self.full.verify(&self.budget)?; + self.updater.verify(&self.budget)?; + ensure!( + participant(&self.full)? == delegation.original_helper + && participant(&self.updater)? == delegation.updater, + "UPDATE_PREPARED_PARTICIPANTS_CHANGED" + ); + self.full_files.borrow_mut().verify_original(&self.budget)?; + self.files.reopen_stages(&self.header, &self.budget)?; + self.retained.verify()?; + for protected in &self.protected { + protected.verify()?; + } + Ok(()) + } +} +/// Metadata only for the currently selected TLS pair. Never read or export key +/// bytes, and never require a bootstrap rewrite after normal TLS rotation. +fn retain_active_tls(specs: &mut Vec, binding: &Binding) -> Result<()> { + let base = Path::new("/var/lib/rmm-agent-remote/certificates"); + let dir = Directory::open_agent_state(base)?; + let mut selector = dir.inspect(OsStr::new("current.json"), 993, 4096)?; + let raw = selector.bytes(4096)?; + let value: serde_json::Value = serde_json::from_slice(&raw)?; + ensure!( + value["schema_version"] == 1 + && value["agent_id"] == binding.agent_id.to_string() + && value["enrollment_generation"] == binding.enrollment_generation, + "UPDATE_TLS_SELECTOR_IDENTITY" + ); + let version: Uuid = value["version"] + .as_str() + .context("UPDATE_TLS_SELECTOR_VERSION")? + .parse()?; + for path in [ + base.join("current.json"), + base.join("versions") + .join(version.to_string()) + .join("device.key"), + base.join("versions") + .join(version.to_string()) + .join("device.pem"), + ] { + let parent = path.parent().context("UPDATE_TLS_PARENT")?; + Directory::open_agent_state(parent)?.verify()?; + let m = std::fs::symlink_metadata(&path)?; + ensure!( + m.is_file() + && !m.file_type().is_symlink() + && m.uid() == 993 + && m.mode() & 0o777 == 0o600 + && m.nlink() == 1, + "UPDATE_TLS_RETAINED_METADATA" + ); + let spec = RetainedFileSpec { + path, + parent_uid: 993, + identity: identity(&m), + retention: Retention::ImmutableMetadata, + }; + if let Some(existing) = specs.iter().find(|s| s.path == spec.path) { + ensure!(existing == &spec, "UPDATE_TLS_RETAINED_SELECTOR_CHANGED"); + } else { + specs.push(spec); + } + } + dir.file(OsStr::new("current.json"), selector.proof(), 4096)?; + Ok(()) +} +fn stage_download( + job: &JobPayload, + role: Role, + expected: &str, + length: u64, + nonce: Uuid, + budget: &Budget, +) -> Result { + let label = match role { + Role::Agent => "agent.bin", + Role::Helper => "helper.bin", + _ => anyhow::bail!("UPDATE_DOWNLOAD_ROLE"), + }; + let parent = Path::new("/var/lib/rmm-agent-remote/update-downloads") + .join(job.job_id.to_string()) + .join(job.attempt_id.to_string()); + let source_directory = Directory::open_agent_state(&parent)?; + let mut source = source_directory.inspect(OsStr::new(label), 993, 256 * 1024 * 1024)?; + ensure!( + source.proof().bytes == length + && source.proof().sha256 == expected + && source.proof().identity.links == 1 + && source.proof().identity.mode & 0o777 == 0o600, + "UPDATE_PREFETCH_BYTES_REQUIRED" + ); + let target = Path::new(role.target()); + let directory = Directory::open(target.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?; + let original = directory.inspect( + target.file_name().context("UPDATE_TARGET_NAME")?, + 0, + 256 * 1024 * 1024, + )?; + let Action::UpdateAgent { update } = &job.action else { + anyhow::bail!("UPDATE_ACTION_REQUIRED") + }; + ensure!( + original.proof().sha256.as_str() + == match role { + Role::Agent => update.predecessor.agent_sha256.as_str(), + Role::Helper => update.predecessor.helper_sha256.as_str(), + _ => unreachable!(), + }, + "UPDATE_PREDECESSOR_IMAGE_CHANGED" + ); + let staged = Staged::write( + &directory, + StageDescriptor { + role, + nonce, + original: original.proof(), + expected_sha: expected, + length, + }, + source.reader()?, + budget, + )?; + source_directory.file(OsStr::new(label), source.proof(), 256 * 1024 * 1024)?; + Ok(staged) +} + +// Passive helper RPC client; it never creates a handoff. +pub use crate::update_observer::{observe, observe_record}; + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn typed_handoff_ack_is_not_an_outcome_or_a_new_start_lease() { + let request = Request { + job_bytes: b"bound original job".to_vec(), + permit_bytes: b"bound original permit".to_vec(), + signature: vec![0; 64], + plan_hash: "a".repeat(64), + authorization_id: Uuid::new_v4(), + }; + let grant = Uuid::new_v4(); + let accepted = Accepted { + schema_version: 1, + operation: "agent_update_handoff_accepted_v1".into(), + grant_id: grant, + owner_nonce: Uuid::new_v4(), + handoff_sha256: "b".repeat(64), + original_request_sha256: hash(&serde_json::to_vec(&request).unwrap()), + ready: false, + terminal: false, + }; + let raw = serde_json::to_vec(&accepted).unwrap(); + serde_json::from_slice::(&raw) + .unwrap() + .matches(&request, grant) + .unwrap(); + assert!(serde_json::from_slice::(&raw).is_err()); + let mut next = request.clone(); + next.job_bytes.push(0); + assert!(accepted.matches(&next, grant).is_err()); + assert!(accepted.matches(&request, Uuid::new_v4()).is_err()); + let mut false_result = accepted.clone(); + false_result.terminal = true; + assert!(false_result.matches(&request, grant).is_err()); + false_result.terminal = false; + false_result.ready = true; + assert!(false_result.matches(&request, grant).is_err()); + } +} diff --git a/crates/privileged-helper/src/update_control.rs b/crates/privileged-helper/src/update_control.rs index 32b41ed..c22902c 100644 --- a/crates/privileged-helper/src/update_control.rs +++ b/crates/privileged-helper/src/update_control.rs @@ -7,7 +7,7 @@ use rmm_agent_core::{ reconcile::SyncClock, update_handoff::{ self as core, NativeControlOperation, NativeControlReply, NativeControlRequest, - NativeEffect, NativeService, + NativeEffect, NativeEffectStage, NativeService, UpdateParticipantGuard, }, }; use rmm_updater_handoff::{Evidence, Phase}; @@ -21,7 +21,7 @@ pub fn active_grant(db: &Connection) -> Result> { 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 mut query=db.prepare("SELECT grant_id FROM agent_update_handoffs WHERE phase NOT IN ('unknown','outcome_persisted','rollback_outcome_persisted') ORDER BY grant_id LIMIT 2")?; let rows = query .query_map([], |r| r.get::<_, String>(0))? .collect::>>()?; @@ -90,7 +90,10 @@ pub fn handle( } => { let journal = core::read(db, &guard, request.grant_id)?; ensure!( - journal.phase() == Phase::LedgerReturnIntent, + matches!( + journal.phase(), + Phase::LedgerReturnIntent | Phase::RollbackLedgerReturnIntent + ), "UPDATE_START_ALREADY_OBSERVED_OR_PHASE" ); let (actual, actual_hash) = guard.started()?; @@ -103,7 +106,11 @@ pub fn handle( NativeService::Full, NativeService::Agent, ] { - let effect = NativeEffect::StartService { service }; + let effect = if guard.restored() { + NativeEffect::RollbackStartService { service } + } else { + 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 @@ -119,28 +126,60 @@ pub fn handle( } 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, + if guard.restored() { + Evidence::RollbackServicesStarted { + services: actual, + ledger_identity_sha256: header.ledger_identity_sha256.clone(), + observation_sha256: actual_hash, + } + } else { + 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)?; + if header.schema_version == 2 && !guard.restored() { + let observation = guard.native_observation_sha256( + &header, + guard.original().delegation(), + &NativeEffect::ConditionalRollbackStopGeneral, + NativeEffectStage::ObserveNonstart, + )?; + core::deny_conditional_stop( + db, + &guard, + request.grant_id, + &observation, + clock.now()?, + )?; + } } 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(), + if guard.restored() { + Evidence::RollbackHealthVerified { + receipt: receipt.clone(), + } + } else { + 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)?)); + original_outcome_base64url = Some(URL_SAFE_NO_PAD.encode(if guard.restored() { + guard.finalize_restored(db)? + } else { + guard.publish_record_and_finalize(db)? + })); } } let proof = if original_outcome_base64url.is_some() { @@ -151,6 +190,18 @@ pub fn handle( .context("UPDATE_ACTIVE_PROOF_REQUIRED")?, ) }; + let pre_stop = if matches!(request.method, NativeControlOperation::ReadOriginal) { + guard.pre_stop_signed_sync(db)? + } else { + None + }; + let (pre_stop_sync_request_base64url, pre_stop_sync_response_base64url) = match pre_stop { + Some((request, response)) => ( + Some(URL_SAFE_NO_PAD.encode(request)), + Some(URL_SAFE_NO_PAD.encode(response)), + ), + None => (None, None), + }; Ok(NativeControlReply { schema_version: 1, challenge: request.challenge, @@ -167,5 +218,7 @@ pub fn handle( claim, original_start_base64url, original_outcome_base64url, + pre_stop_sync_request_base64url, + pre_stop_sync_response_base64url, }) } diff --git a/crates/privileged-helper/src/update_guard.rs b/crates/privileged-helper/src/update_guard.rs index fbd8bff..5cdf254 100644 --- a/crates/privileged-helper/src/update_guard.rs +++ b/crates/privileged-helper/src/update_guard.rs @@ -133,6 +133,12 @@ impl<'a> FullUpdateGuard<'a> { journal.phase(), Phase::LedgerReturnIntent | Phase::ServicesStarted | Phase::HealthVerified ); + let restored = matches!( + journal.phase(), + Phase::RollbackLedgerReturnIntent + | Phase::RollbackServicesStarted + | Phase::RollbackHealthVerified + ); let services = service_policies(&original, &trust, target)?; let own = services.observe(Service::Full, &budget)?; ensure!( @@ -148,6 +154,8 @@ impl<'a> FullUpdateGuard<'a> { "UPDATE_ORIGINAL_FULL_CHANGED" ); Participant::OriginalHelper + } else if restored && header.boot_id == Uuid::parse_str(&rmm_agent_linux::boot_id()?)? { + Participant::RestoredHelper } else if target && header.boot_id == Uuid::parse_str(&rmm_agent_linux::boot_id()?)? { Participant::NewHelper } else { @@ -227,8 +235,11 @@ impl<'a> FullUpdateGuard<'a> { } self.check_effect_window()?; ensure!( - self.participant == Participant::NewHelper, - "UPDATE_NEW_FULL_REQUIRED" + matches!( + self.participant, + Participant::NewHelper | Participant::RestoredHelper + ), + "UPDATE_NEW_OR_RESTORED_FULL_REQUIRED" ); let agent = self.services.observe(Service::Agent, &self.budget)?; let mut peer = std::mem::MaybeUninit::::uninit(); @@ -264,18 +275,30 @@ impl<'a> FullUpdateGuard<'a> { ) -> Result<()> { self.check_effect_window()?; ensure!( - self.participant == Participant::NewHelper, - "UPDATE_NEW_FULL_REQUIRED" + matches!( + self.participant, + Participant::NewHelper | Participant::RestoredHelper + ), + "UPDATE_NEW_OR_RESTORED_FULL_REQUIRED" ); let mut journal = update_handoff::read(db, self, self.original.permit().grant_id)?; ensure!( - journal.phase() == Phase::ServicesStarted, + matches!( + journal.phase(), + Phase::ServicesStarted | Phase::RollbackServicesStarted + ), "UPDATE_HEALTH_PHASE" ); let event = journal.prepare( u64::try_from(self.clock.now()?.timestamp())?, - Evidence::HealthVerified { - receipt: signed.clone(), + if self.participant == Participant::RestoredHelper { + Evidence::RollbackHealthVerified { + receipt: signed.clone(), + } + } else { + Evidence::HealthVerified { + receipt: signed.clone(), + } }, )?; journal.replay(&event)?; // ORIGINAL pinned health signature and all kernel fields. @@ -286,7 +309,10 @@ impl<'a> FullUpdateGuard<'a> { self.check_effect_window()?; let journal = update_handoff::read(db, self, self.original.permit().grant_id)?; ensure!( - journal.phase() == Phase::HealthVerified, + matches!( + journal.phase(), + Phase::HealthVerified | Phase::RollbackHealthVerified + ), "UPDATE_ORIGINAL_STORED_HEALTH_REQUIRED" ); let event_raw:Vec=db.query_row("SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence DESC LIMIT 1",[self.original.permit().grant_id.to_string()],|r|r.get(0))?; @@ -295,8 +321,16 @@ impl<'a> FullUpdateGuard<'a> { hash(&event_raw) == journal.head_sha256(), "UPDATE_STORED_HEALTH_HEAD_CHANGED" ); - let Evidence::HealthVerified { receipt } = &event.evidence else { - anyhow::bail!("UPDATE_STORED_HEALTH_EVENT_REQUIRED") + let receipt = match &event.evidence { + Evidence::HealthVerified { receipt } if self.participant == Participant::NewHelper => { + receipt + } + Evidence::RollbackHealthVerified { receipt } + if self.participant == Participant::RestoredHelper => + { + receipt + } + _ => anyhow::bail!("UPDATE_STORED_HEALTH_EVENT_REQUIRED"), }; self.health = Some(self.health_evidence(db, receipt)?); Ok(event_raw) @@ -354,8 +388,11 @@ impl<'a> FullUpdateGuard<'a> { pub fn started(&self) -> Result<(Vec, String)> { self.check_effect_window()?; ensure!( - self.participant == Participant::NewHelper, - "UPDATE_NEW_FULL_REQUIRED" + matches!( + self.participant, + Participant::NewHelper | Participant::RestoredHelper + ), + "UPDATE_NEW_OR_RESTORED_FULL_REQUIRED" ); let mut raw = Vec::new(); let mut proofs = Vec::new(); @@ -415,7 +452,13 @@ impl<'a> FullUpdateGuard<'a> { Ok(()) } fn verify_owner(&self, phase: Phase, events: &[Event]) -> Result<()> { - if !matches!(phase, Phase::ServicesStarted | Phase::HealthVerified) { + if !matches!( + phase, + Phase::ServicesStarted + | Phase::HealthVerified + | Phase::RollbackServicesStarted + | Phase::RollbackHealthVerified + ) { let updater = self .updater .as_ref() @@ -431,15 +474,30 @@ impl<'a> FullUpdateGuard<'a> { "UPDATE_ORIGINAL_UPDATER_OWNER_CHANGED" ); } - if matches!(phase, Phase::ServicesStarted | Phase::HealthVerified) { + if matches!( + phase, + Phase::ServicesStarted + | Phase::HealthVerified + | Phase::RollbackServicesStarted + | Phase::RollbackHealthVerified + ) { let started = events .iter() - .find_map(|event| { - if let Evidence::ServicesStarted { services, .. } = &event.evidence { + .find_map(|event| match &event.evidence { + Evidence::ServicesStarted { services, .. } + if matches!(phase, Phase::ServicesStarted | Phase::HealthVerified) => + { Some(services) - } else { - None } + Evidence::RollbackServicesStarted { services, .. } + if matches!( + phase, + Phase::RollbackServicesStarted | Phase::RollbackHealthVerified + ) => + { + Some(services) + } + _ => None, }) .context("UPDATE_STARTED_PROOF_MISSING")?; self.verify_current_services(started)?; @@ -486,16 +544,39 @@ impl UpdateParticipantGuard for FullUpdateGuard<'_> { ); self.check_effect_window()?; ensure!( - self.participant == Participant::NewHelper, - "UPDATE_NEW_FULL_REQUIRED" + matches!( + self.participant, + Participant::NewHelper | Participant::RestoredHelper + ), + "UPDATE_NEW_OR_RESTORED_FULL_REQUIRED" ); - self.witnesses.files.verify_pair(&self.budget)?; + if self.participant == Participant::RestoredHelper { + self.witnesses.files.verify_predecessor_pair(&self.budget)?; + } else { + self.witnesses.files.verify_pair(&self.budget)?; + } match &event.evidence { Evidence::ServicesStarted { services, ledger_identity_sha256, observation_sha256, + } + | Evidence::RollbackServicesStarted { + services, + ledger_identity_sha256, + observation_sha256, } => { + ensure!( + matches!( + (&event.evidence, self.participant), + (Evidence::ServicesStarted { .. }, Participant::NewHelper) + | ( + Evidence::RollbackServicesStarted { .. }, + Participant::RestoredHelper + ) + ), + "UPDATE_STARTED_ROLE" + ); let mut held = Vec::new(); let mut proofs = Vec::new(); for service in [Service::Agent, Service::General, Service::Full] { @@ -511,7 +592,18 @@ impl UpdateParticipantGuard for FullUpdateGuard<'_> { "UPDATE_ACTUAL_STARTED_PROOFS_REQUIRED" ); } - Evidence::HealthVerified { receipt } => { + Evidence::HealthVerified { receipt } | Evidence::RollbackHealthVerified { receipt } => { + ensure!( + matches!( + (&event.evidence, self.participant), + (Evidence::HealthVerified { .. }, Participant::NewHelper) + | ( + Evidence::RollbackHealthVerified { .. }, + Participant::RestoredHelper + ) + ), + "UPDATE_HEALTH_ROLE" + ); let health = self .health .as_ref() @@ -521,6 +613,14 @@ impl UpdateParticipantGuard for FullUpdateGuard<'_> { "UPDATE_LOCAL_HEALTH_RECEIPT_CHANGED" ); } + Evidence::RollbackOutcomePersisted { .. } + if self.participant == Participant::RestoredHelper => + { + ensure!( + self.health.is_some(), + "UPDATE_ROLLBACK_LOCAL_HEALTH_REQUIRED" + ); + } // InstalledRecord/Outcome have a separate original-record CAS. _ => anyhow::bail!("UPDATE_FULL_EVENT_NOT_YET_QUALIFIED"), } @@ -578,10 +678,63 @@ impl UpdateParticipantGuard for FullUpdateGuard<'_> { .observe(service, &self.budget)? .verify(&self.budget)?; } + ( + Participant::NewHelper, + NativeEffect::ConditionalRollbackStopGeneral, + NativeEffectStage::ObserveNonstart, + ) => { + // The retained new Full proves the early-Full-failure branch + // was never entered. Closing this reservation records NO stop. + ensure!( + handoff.schema_version == 2, + "UPDATE_CONDITIONAL_STOP_PROFILE" + ); + for service in [Service::General, Service::Full] { + self.services + .observe(service, &self.budget)? + .verify(&self.budget)?; + } + } + ( + Participant::RestoredHelper, + NativeEffect::RollbackStartService { service }, + NativeEffectStage::Observe, + ) => { + let service = match service { + NativeService::Agent => Service::Agent, + NativeService::General => Service::General, + NativeService::Full => Service::Full, + _ => anyhow::bail!("UPDATE_WINDOWS_SERVICE_ON_LINUX"), + }; + self.witnesses.files.verify_predecessor_pair(&self.budget)?; + self.services + .observe(service, &self.budget)? + .verify(&self.budget)?; + } _ => anyhow::bail!("UPDATE_FULL_NATIVE_EFFECT_NOT_QUALIFIED"), } Ok(()) } + fn native_observation_sha256( + &self, + handoff: &Handoff, + delegation: &UpdateDelegation, + effect: &NativeEffect, + stage: NativeEffectStage, + ) -> Result { + self.verify_native_effect(handoff, delegation, effect, stage)?; + ensure!( + *effect == NativeEffect::ConditionalRollbackStopGeneral + && stage == NativeEffectStage::ObserveNonstart, + "UPDATE_FULL_NATIVE_HASH_NOT_QUALIFIED" + ); + Ok(hash(&serde_json::to_vec(&( + "conditional_general_stop_unused_v1", + handoff.owner_nonce, + handoff.attempt.grant_id, + self.services.observe(Service::Full, &self.budget)?.proof(), + ))?)) + } } struct VerifiedHealth { @@ -985,3 +1138,157 @@ impl FullUpdateGuard<'_> { Ok(bytes) } } + +impl FullUpdateGuard<'_> { + pub fn finalize_restored(&mut self, db: &mut Connection) -> Result> { + ensure!( + self.participant == Participant::RestoredHelper, + "UPDATE_RESTORED_FULL_REQUIRED" + ); + self.qualify_effects(db)?; + self.qualify_stored_health(db)?; + self.witnesses.files.verify_predecessor_pair(&self.budget)?; + // TrustState still pins the SAME original record and activation floor. + self.trust.recheck(&self.original, self.clock)?; + let journal = update_handoff::read(db, self, self.original.permit().grant_id)?; + let health = self + .health + .as_ref() + .context("UPDATE_ROLLBACK_HEALTH_REQUIRED")?; + let outcome = crate::Outcome { + state: "failed".into(), + stdout: vec![], + stderr: vec![], + exit_code: None, + output_truncated: false, + reboot_required: false, + error_code: "AGENT_UPDATE_RETURNED_TO_PREDECESSOR".into(), + evidence: serde_json::json!({"postconditions_passed":false,"previous_boot_id":self.original.handoff().boot_id, + "agent_update":{"schema_version":2,"handoff_sha256":journal.handoff_sha256(), + "manifest_sha256":self.original.handoff().target.manifest_sha256, + "predecessor_record_sha256":self.original.handoff().predecessor_record_sha256, + "installed_record_sha256":self.original.handoff().predecessor_record_sha256, + "signed_health_sha256":health.signed_sha256,"rolled_back":true}}), + }; + update_handoff::finalize_rollback( + db, + self, + self.original.permit(), + journal.head_sha256(), + u64::try_from(self.clock.now()?.timestamp())?, + &outcome, + )?; + Ok(db.query_row( + "SELECT outcome FROM execution_evidence WHERE grant_id=?1", + [self.original.permit().grant_id.to_string()], + |r| r.get(0), + )?) + } + pub fn restored(&self) -> bool { + self.participant == Participant::RestoredHelper + } + /// Exact ORIGINAL bytes from a signed inflight decision already applied + /// by this actual Full process under the same existing guarded ledger. + /// Missing evidence keeps the worker waiting; it never becomes readiness. + pub fn pre_stop_signed_sync(&self, db: &Connection) -> Result, Vec)>> { + use prost::Message; + use rusqlite::OptionalExtension; + self.check(db)?; + if self.participant != Participant::OriginalHelper { + return Ok(None); + } + let journal = update_handoff::read(db, self, self.original.permit().grant_id)?; + if journal.phase() != Phase::StopIntent { + return Ok(None); + } + let id: Option = db + .query_row( + "SELECT last_decision FROM helper_sync_meta WHERE id=1 AND fault IS NULL", + [], + |r| r.get(0), + ) + .optional()? + .flatten(); + let Some(id) = id else { return Ok(None) }; + let id: Uuid = id.parse()?; + let applied:bool=db.query_row("SELECT EXISTS(SELECT 1 FROM agent_update_sync_applied WHERE grant_id=?1 AND decision_id=?2)", + rusqlite::params![self.original.permit().grant_id.to_string(),id.to_string()],|r|r.get(0))?; + if !applied { + return Ok(None); + } + self.qualify_effects(db)?; + let round = rmm_agent_core::update_health_sync::read_round( + db, + self, + self.binding, + self.original.permit().grant_id, + id, + )?; + let decision = round.decision(); + let h = self.original.handoff(); + let now = self.clock.now()?; + let proof = update_handoff::current_proof(db, self, self.original.permit().grant_id)? + .context("UPDATE_PRESTOP_ORIGINAL_PROOF_REQUIRED")?; + let expected = + rmm_domain::agent_update::inflight_decision(&proof, self.trust.health_public_key()) + .map_err(anyhow::Error::msg)?; + let attempts = decision + .attempts + .iter() + .filter(|a| a.job_id == h.attempt.job_id && a.attempt_id == h.attempt.attempt_id) + .collect::>(); + ensure!(attempts.len() == 1, "UPDATE_PRESTOP_ONE_ORIGINAL_ACK"); + let ack = attempts[0]; + ensure!( + decision.valid_profile(now) + && !decision.ready + && !decision.helper_ready + && decision.boot_id == h.boot_id + && decision.connection_generation == h.connection_generation + && decision.journal_id == h.agent_journal_id + && decision.helper_journal_id == Some(h.helper_journal_id) + && ack.update_inflight.as_ref() == Some(&expected) + && ack.grant_id == Some(h.attempt.grant_id) + && ack.authorization_id == Some(h.attempt.authorization_id) + && ack.plan_hash.as_deref() == Some(h.attempt.plan_sha256.as_str()) + && ack.job_payload_hash == h.attempt.job_payload_sha256 + && ack.original_boot_id == Some(h.boot_id) + && ack.action == "update_agent" + && ack.execution_identity == "root" + && !ack.unlock + && ack.outcome_state == "running" + && ack.outcome_bytes.is_empty() + && ack.outcome_sha256.is_none() + && round.applied().applied_at >= decision.issued_at + && round.applied().applied_at < decision.valid_until, + "UPDATE_PRESTOP_APPLIED_ORIGINAL_ACK" + ); + let (raw, response): (Vec, Vec) = db.query_row( + "SELECT request,response FROM helper_sync_decisions WHERE decision_id=?1", + [id.to_string()], + |r| Ok((r.get(0)?, r.get(1)?)), + )?; + ensure!( + raw.len() <= 256 * 1024 && response.len() <= 256 * 1024, + "UPDATE_PRESTOP_WIRE_LIMIT" + ); + let request = rmm_protocol::SyncRequest::decode(raw.as_slice())?; + let local = request + .attempts + .iter() + .find(|a| { + a.job_id == h.attempt.job_id.to_string() + && a.attempt_id == h.attempt.attempt_id.to_string() + }) + .context("UPDATE_PRESTOP_REQUEST_ATTEMPT")?; + ensure!( + request.journal_complete + && local.state == "running" + && local.consumption == "consumed" + && local.update_handoff_bytes == proof.bytes().map_err(anyhow::Error::msg)?, + "UPDATE_PRESTOP_REQUEST_ORIGINAL_PREFIX" + ); + self.check_effect_window()?; + Ok(Some((raw, response))) + } +} diff --git a/crates/privileged-helper/src/update_observer.rs b/crates/privileged-helper/src/update_observer.rs new file mode 100644 index 0000000..1624789 --- /dev/null +++ b/crates/privileged-helper/src/update_observer.rs @@ -0,0 +1,386 @@ +//! Passive Full-only updater observation. This path cannot stage, consume, +//! start/stop a service, append a handoff, repair state or grant readiness. +use crate::{ + Binding, control, + readiness::Identity, + reconcile::{self, ExecutorGuard}, +}; +use anyhow::{Context, Result, ensure}; +use rmm_agent_core::{helper_reconcile::HelperSync, reconcile::SyncClock}; +use rmm_domain::{ + agent_update::{ + AgentUpdateObservation, CandidatePreview, ObservationQuality, PeerPinTransition, Platform, + ReleaseEnvelope, + }, + jobs::hash, +}; +use rmm_updater_linux::{ + Budget, + files::Directory, + ipc, + services::{Service, ServicePolicy, Systemd}, +}; +use rmm_updater_worker::{ + clock::NativeClock, full::FullFiles, reader_registry::QualifiedRegistry, trust::TrustState, +}; +use rusqlite::Connection; +use serde::{Deserialize, Serialize}; +use std::{ + ffi::OsStr, + os::unix::{fs::MetadataExt, net::UnixStream}, + path::Path, + time::Duration, +}; +use uuid::Uuid; +const SOCKET: &str = "/run/rmm-helper-maintenance/control.sock"; +#[derive(Clone, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Request { + pub operation: String, + pub schema_version: u32, + pub challenge: Uuid, + pub identity: Identity, + pub candidate: Option, + pub keyring_generation: Option, +} +#[derive(Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Response { + pub schema_version: u32, + pub challenge: Uuid, + pub identity: Identity, + pub profile: String, + pub policy_sha256: String, + pub helper_binary_sha256: String, + pub ledger_device: u64, + pub ledger_inode: u64, + pub lock_device: u64, + pub lock_inode: u64, + pub observation: AgentUpdateObservation, + pub installed_record: Vec, + pub installed_record_sha256: String, +} +pub fn observe( + socket: &Path, + binding: &Binding, + candidate: Option, + keyring_generation: Option, +) -> Result { + observe_record(socket, binding, candidate, keyring_generation) + .map(|(observation, _)| observation) +} +/// Exact public release/evidence metadata, returned only from the same +/// authenticated Full response as the independently measured observation. +/// The private Root record directory and file permissions remain unchanged. +pub fn observe_record( + socket: &Path, + binding: &Binding, + candidate: Option, + keyring_generation: Option, +) -> Result<(AgentUpdateObservation, Vec)> { + ensure!( + socket == Path::new(SOCKET) && candidate.is_some() == keyring_generation.is_some(), + "UPDATE_OBSERVER_INPUT" + ); + let pin = control::GeneralPin::read( + Path::new("/etc/rmm-remote/full-maintenance-helper-proof.json"), + binding, + )?; + ensure!( + pin.profile == "full_maintenance_v1", + "UPDATE_OBSERVER_FULL_PIN" + ); + let request = Request { + operation: "update_observe_v1".into(), + schema_version: 1, + challenge: Uuid::new_v4(), + identity: Identity::from_binding(binding)?, + candidate, + keyring_generation, + }; + let response: Response = + control::exchange(socket, &request, Duration::from_secs(15), 192 * 1024)?; + ensure!( + response.schema_version == 1 + && response.challenge == request.challenge + && response.identity == request.identity + && response.profile == pin.profile + && response.policy_sha256 == pin.policy_sha256 + && Some(&response.helper_binary_sha256) == pin.helper_binary_sha256.as_ref() + && response.ledger_inode != 0 + && response.lock_inode != 0 + && response.observation.validate() + && response.observation.platform == Platform::LinuxX64, + "UPDATE_OBSERVER_RESPONSE_BINDING" + ); + let record = rmm_domain::agent_update_provisioning::InstalledReleaseRecord::from_bytes( + &response.installed_record, + ) + .map_err(anyhow::Error::msg)?; + let predecessor = response + .observation + .predecessor + .as_ref() + .context("UPDATE_OBSERVER_RECORD_PREDECESSOR")?; + let manifest = record + .release_envelope + .decode() + .map_err(anyhow::Error::msg)? + .manifest; + ensure!( + response.installed_record_sha256 == hash(&response.installed_record) + && response.installed_record_sha256 == predecessor.installed_record_sha256 + && record.platform == response.observation.platform + && record.manifest_sha256 == predecessor.manifest_sha256 + && manifest.release_id == predecessor.release_id + && manifest.release_sequence == predecessor.release_sequence + && manifest.artifacts[0].sha256 == predecessor.agent_sha256 + && manifest.artifacts[1].sha256 == predecessor.helper_sha256 + && Some(record.activation_floor) == response.observation.activation_floor + && record.rolled_back == response.observation.rolled_back, + "UPDATE_OBSERVER_RECORD_BINDING" + ); + Ok((response.observation, response.installed_record)) +} +pub struct Local<'a> { + pub executor: &'a ExecutorGuard, + pub binding: &'a Binding, + pub clock: &'a NativeClock, + pub sync: &'a HelperSync, + pub sync_clock: &'a reconcile::NativeClock, + pub key_path: &'a Path, + pub policy_sha256: &'a str, + pub helper_sha256: &'a str, +} +pub fn collect(db: &Connection, local: Local<'_>, request: &Request) -> Result { + ensure!( + request.operation == "update_observe_v1" + && request.schema_version == 1 + && !request.challenge.is_nil() + && request.identity == Identity::from_binding(local.binding)? + && request.candidate.is_some() == request.keyring_generation.is_some(), + "UPDATE_OBSERVER_REQUEST_BINDING" + ); + local.executor.check(db)?; + ensure!( + crate::update_control::active_grant(db)?.is_none(), + "UPDATE_OBSERVER_ACTIVE_HANDOFF" + ); + let generation = local + .sync + .active_generation() + .context("UPDATE_OBSERVER_CURRENT_SYNC_REQUIRED")?; + ensure!( + local.sync.can_start( + db, + local.executor, + generation, + local.sync_clock, + &reconcile::NativeBoot + ), + "UPDATE_OBSERVER_CONFIRMED_SYNC_REQUIRED" + ); + let decision: String = db.query_row( + "SELECT last_decision FROM helper_sync_meta WHERE id=1 AND fault IS NULL AND generation=?1", + [generation], + |r| r.get(0), + )?; + let decision = Uuid::parse_str(&decision)?; + let trust = TrustState::load_observed(local.binding, local.clock)?; + trust.qualify_history(db, local.executor, local.binding)?; + let boot = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")? + .trim() + .parse()?; + let budget = Budget::new(boot, Duration::from_secs(15))?; + let registry = QualifiedRegistry::load(trust.policy())?; + registry.verify_admission(trust.policy(), &budget)?; + let policies = trust + .policy() + .services + .iter() + .zip([ + Service::Agent, + Service::General, + Service::Full, + Service::Apt, + ]) + .map(|(pin, service)| ServicePolicy { + service, + unit_sha256: pin.unit_sha256.clone(), + image_sha256: if service == Service::Agent { + trust.predecessor().agent_sha256.clone() + } else { + trust.predecessor().helper_sha256.clone() + }, + }) + .collect(); + let services = Systemd::new(policies)?; + let mut held = Vec::new(); + for service in [Service::Agent, Service::General, Service::Full] { + held.push(services.observe(service, &budget)?); + } + ensure!( + held[2].proof().process.pid == std::process::id() + && held[2].proof().process.image_sha256 == local.helper_sha256, + "UPDATE_OBSERVER_ACTUAL_FULL" + ); + let updater = Systemd::observe_updater( + ServicePolicy { + service: Service::Updater, + image_sha256: trust.updater_binding().binary_sha256.clone(), + unit_sha256: trust.policy().updater_service.unit_sha256.clone(), + }, + &budget, + )?; + let description = ipc::exchange( + UnixStream::connect("/run/rmm-agent-updater/control.sock")?, + &updater, + &budget, + &ipc::Request::Describe {}, + )?; + let updater_proof = updater.proof(); + ensure!( + matches!(description.state, ipc::State::Described) + && !description.ready + && description.head_sha256.is_none() + && description.updater.pid == updater_proof.process.pid + && description.updater.birth == updater_proof.process.birth + && description.updater.image_sha256 == updater_proof.process.image_sha256 + && description.updater.service_instance_sha256 + == hash(&serde_json::to_vec(updater_proof)?), + "UPDATE_OBSERVER_LIVE_UPDATER" + ); + let key_directory = Directory::open( + local + .key_path + .parent() + .context("UPDATE_OBSERVER_KEY_PARENT")?, + 0, + false, + )?; + let mut key = key_directory.inspect( + local + .key_path + .file_name() + .context("UPDATE_OBSERVER_KEY_NAME")?, + 0, + 4096, + )?; + let record: crate::SigningKey = serde_json::from_slice(&key.bytes(4096)?)?; + record.validate()?; + ensure!( + record.public_key_hex == local.binding.public_key_hex, + "UPDATE_OBSERVER_JOB_KEY_BINDING" + ); + let general = Directory::open(Path::new("/etc/rmm-remote"), 0, false)?; + let general_pin = general.inspect(OsStr::new("general-helper-proof.json"), 0, 8192)?; + let mut previews = Vec::new(); + if let Some(candidate) = &request.candidate { + ensure!( + request.keyring_generation == Some(trust.updater_binding().keyring_generation), + "UPDATE_OBSERVER_KEYRING_GENERATION" + ); + trust.verify_candidate(candidate, local.clock)?; + let manifest = candidate.decode().map_err(anyhow::Error::msg)?.manifest; + if manifest.release_id != trust.predecessor().release_id { + let full = FullFiles::preview( + &manifest.artifacts[1].sha256, + local.key_path, + &key.proof().sha256, + &budget, + )?; + let transition = PeerPinTransition::LinuxGeneralFullV1 { + general_pin_sha256: general_pin.proof().sha256.clone(), + full, + }; + // Validate the exact renderer result against the actual verified + // future manifest/predecessor, using the ordinary immutable codec. + let resolved = rmm_domain::agent_update::ResolvedAgentUpdate { + schema_version: 1, + profile: rmm_domain::agent_update::UpdateProfile::FixedCodePairV1, + release_envelope: candidate.clone(), + release_envelope_sha256: candidate.sha256().map_err(anyhow::Error::msg)?, + manifest_sha256: hash( + &candidate + .decode() + .map_err(anyhow::Error::msg)? + .payload_bytes, + ), + predecessor: trust.predecessor().clone(), + updater: trust.updater_binding().clone(), + peer_pin_transition: transition.clone(), + recovery: rmm_domain::agent_update::Recovery::RestoreExactPredecessorOnce, + health_profile: rmm_domain::agent_update::HealthProfile::MtlsInventorySyncV1, + health_timeout_seconds: 180, + no_reboot: true, + }; + ensure!(resolved.validate(), "UPDATE_OBSERVER_RENDERED_CANDIDATE"); + previews.push(CandidatePreview { + release_id: manifest.release_id, + manifest_sha256: resolved.manifest_sha256, + peer_pin_transition: transition, + }); + } + } + for service in &held { + service.verify(&budget)?; + } + updater.verify(&budget)?; + trust.recheck_observed()?; + registry.verify_admission(trust.policy(), &budget)?; + local.executor.check(db)?; + ensure!( + local.sync.can_start( + db, + local.executor, + generation, + local.sync_clock, + &reconcile::NativeBoot + ), + "UPDATE_OBSERVER_SYNC_EXPIRED" + ); + let observed_at = local.clock.now()?; + let observed_at = observed_at + - chrono::Duration::nanoseconds(i64::from( + observed_at.timestamp_subsec_nanos() % 1_000_000, + )); + let observation = AgentUpdateObservation { + schema_version: 1, + platform: Platform::LinuxX64, + observed_at, + quality: ObservationQuality::Valid, + predecessor: Some(trust.predecessor().clone()), + updater: Some(trust.updater_binding().clone()), + activation_floor: Some(trust.activation_floor()), + rolled_back: false, + helper_observation_sha256: Some(hash(&serde_json::to_vec(&( + held.iter().map(|h| h.proof()).collect::>(), + updater.proof(), + decision, + generation, + key.proof(), + general_pin.proof(), + &previews, + ))?)), + signed_sync_decision_id: Some(decision), + candidate_previews: previews, + }; + ensure!(observation.validate(), "UPDATE_OBSERVER_OBSERVATION_SHAPE"); + let ledger = std::fs::metadata("/var/lib/rmm-helper/consumption.sqlite")?; + let lock = std::fs::metadata("/var/lib/rmm-helper/executor.lock")?; + Ok(Response { + schema_version: 1, + challenge: request.challenge, + identity: request.identity.clone(), + profile: "full_maintenance_v1".into(), + policy_sha256: local.policy_sha256.into(), + helper_binary_sha256: local.helper_sha256.into(), + ledger_device: ledger.dev(), + ledger_inode: ledger.ino(), + lock_device: lock.dev(), + lock_inode: lock.ino(), + observation, + installed_record: trust.installed_raw().to_vec(), + installed_record_sha256: hash(trust.installed_raw()), + }) +} diff --git a/crates/server/src/agent_updates.rs b/crates/server/src/agent_updates.rs index f526c54..877494a 100644 --- a/crates/server/src/agent_updates.rs +++ b/crates/server/src/agent_updates.rs @@ -134,19 +134,61 @@ fn verifier_platform(platform: Platform) -> rmm_updater::Platform { Platform::WindowsX64 => rmm_updater::Platform::WindowsX64, } } +/// Network input can choose only one of two statically named protected +/// configuration slots. It never supplies a path or introduces update trust. +fn keyring_path_with( + platform: Platform, + lookup: impl Fn(&str) -> Result, Error>, +) -> Result { + let name = match platform { + Platform::LinuxX64 => "RMM_AGENT_UPDATE_LINUX_KEYRING_FILE", + Platform::WindowsX64 => "RMM_AGENT_UPDATE_WINDOWS_KEYRING_FILE", + }; + if let Some(path) = lookup(name)? { + if path.is_empty() || !FsPath::new(&path).is_absolute() { + return Err(fail("AGENT_UPDATE_NOT_CONFIGURED")); + } + return Ok(path.into()); + } + // Exact legacy compatibility only when the platform-specific slot is + // absent. An invalid explicit slot must never silently choose other trust. + lookup("RMM_AGENT_UPDATE_KEYRING_FILE")? + .map(Into::into) + .ok_or_else(|| fail("AGENT_UPDATE_NOT_CONFIGURED")) +} pub fn verify_envelope( envelope: &ReleaseEnvelope, binding: Option<&rmm_domain::agent_update::UpdaterBinding>, +) -> Result { + let platform = envelope.decode().map_err(|_| invalid())?.manifest.platform; + let path = keyring_path_with(platform, |name| match std::env::var(name) { + Ok(value) => Ok(Some(value)), + Err(std::env::VarError::NotPresent) => Ok(None), + Err(std::env::VarError::NotUnicode(_)) => Err(fail("AGENT_UPDATE_NOT_CONFIGURED")), + })?; + let raw = protected_file(&path, 16384)?; + verify_envelope_with_keyring( + envelope, + binding, + &raw, + chrono::Utc::now() + .timestamp() + .try_into() + .map_err(|_| invalid())?, + ) +} +fn verify_envelope_with_keyring( + envelope: &ReleaseEnvelope, + binding: Option<&rmm_domain::agent_update::UpdaterBinding>, + raw: &[u8], + now_utc_seconds: u64, ) -> Result { let decoded = envelope.decode().map_err(|_| invalid())?; - let path = std::env::var("RMM_AGENT_UPDATE_KEYRING_FILE") - .map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?; - let raw = protected_file(FsPath::new(&path), 16384)?; let config: rmm_domain::agent_update::UpdateKeyringFile = - serde_json::from_slice(&raw).map_err(|_| fail("AGENT_UPDATE_TRUST_INVALID"))?; + serde_json::from_slice(raw).map_err(|_| fail("AGENT_UPDATE_TRUST_INVALID"))?; if !config.validate() || binding.is_some_and(|b| { - b.keyring_generation != config.generation || b.keyring_sha256 != hash(&raw) + b.keyring_generation != config.generation || b.keyring_sha256 != hash(raw) }) { return Err(fail("AGENT_UPDATE_TRUST_INVALID")); @@ -174,10 +216,7 @@ pub fn verify_envelope( .map_err(|_| fail("AGENT_UPDATE_TRUST_INVALID"))?; let context = rmm_updater::TrustedContext { platform: verifier_platform(decoded.manifest.platform), - now_utc_seconds: chrono::Utc::now() - .timestamp() - .try_into() - .map_err(|_| invalid())?, + now_utc_seconds, keyring_generation: config.generation, protocol_major: 1, state_schema: 1, @@ -826,6 +865,164 @@ pub async fn validate_current_target( mod tests { use super::*; #[test] + fn protected_keyring_slots_are_platform_specific_with_exact_legacy_fallback() { + use std::collections::BTreeMap; + let paths = BTreeMap::from([ + ("RMM_AGENT_UPDATE_LINUX_KEYRING_FILE", "/trust/linux.json"), + ( + "RMM_AGENT_UPDATE_WINDOWS_KEYRING_FILE", + "/trust/windows.json", + ), + ("RMM_AGENT_UPDATE_KEYRING_FILE", "/trust/legacy.json"), + ]); + let choose = |platform, map: &BTreeMap<&str, &str>| { + keyring_path_with(platform, |name| Ok(map.get(name).map(|v| v.to_string()))) + }; + assert_eq!( + choose(Platform::LinuxX64, &paths).unwrap(), + FsPath::new("/trust/linux.json") + ); + assert_eq!( + choose(Platform::WindowsX64, &paths).unwrap(), + FsPath::new("/trust/windows.json") + ); + let legacy = BTreeMap::from([("RMM_AGENT_UPDATE_KEYRING_FILE", "legacy-relative.json")]); + for platform in [Platform::LinuxX64, Platform::WindowsX64] { + assert_eq!( + choose(platform, &legacy).unwrap(), + FsPath::new("legacy-relative.json") + ); + } + let only_linux = + BTreeMap::from([("RMM_AGENT_UPDATE_LINUX_KEYRING_FILE", "/trust/linux.json")]); + assert!(choose(Platform::WindowsX64, &only_linux).is_err()); + for invalid in ["", "relative.json"] { + let wrong = BTreeMap::from([ + ("RMM_AGENT_UPDATE_LINUX_KEYRING_FILE", invalid), + ("RMM_AGENT_UPDATE_KEYRING_FILE", "/trust/legacy.json"), + ]); + assert!(choose(Platform::LinuxX64, &wrong).is_err()); + } + assert!( + keyring_path_with(Platform::WindowsX64, |_| Err(fail( + "AGENT_UPDATE_NOT_CONFIGURED" + ))) + .is_err() + ); + } + #[test] + fn selected_platform_keyring_still_enforces_real_signatures_revocation_generation_and_raw_hash() + { + use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; + use openssl::{pkey::PKey, sign::Signer}; + use rmm_domain::agent_update::{ + ArtifactRole, UpdateKeyringEntry, UpdateKeyringFile, UpdaterBinding, + }; + let make = |platform| { + let key = PKey::generate_ed25519().unwrap(); + let public = key.raw_public_key().unwrap(); + let manifest = ReleaseManifest { + schema_version: 1, + release_id: Uuid::new_v4(), + release_sequence: 2, + version: "0.1.1".into(), + platform, + published_at: 90, + expires_at: 200, + protocol_major: 1, + state_schema: 1, + minimum_updater_version: 1, + artifacts: [ + ArtifactDescriptor { + role: ArtifactRole::Agent, + artifact_id: Uuid::new_v4(), + sha256: hash(b"agent"), + bytes: 10, + }, + ArtifactDescriptor { + role: ArtifactRole::Helper, + artifact_id: Uuid::new_v4(), + sha256: hash(b"helper"), + bytes: 20, + }, + ], + }; + let raw = encode(&manifest).unwrap(); + let mut message = rmm_updater::RELEASE_SIGNATURE_DOMAIN.to_vec(); + message.extend_from_slice(&raw); + let signature = Signer::new_without_digest(&key) + .unwrap() + .sign_oneshot_to_vec(&message) + .unwrap(); + let envelope = ReleaseEnvelope { + schema_version: 1, + key_id: hash(&public), + payload_bytes: URL_SAFE_NO_PAD.encode(&raw), + signature: URL_SAFE_NO_PAD.encode(signature), + }; + let keys = UpdateKeyringFile { + schema_version: 1, + generation: 7, + keys: vec![UpdateKeyringEntry { + public_key_hex: hex::encode(public), + platform, + first_sequence: 1, + last_sequence: 4, + revoked: false, + }], + }; + (envelope, keys) + }; + let (linux, linux_keys) = make(Platform::LinuxX64); + let (windows, windows_keys) = make(Platform::WindowsX64); + let linux_raw = encode(&linux_keys).unwrap(); + let windows_raw = encode(&windows_keys).unwrap(); + assert_eq!( + verify_envelope_with_keyring(&linux, None, &linux_raw, 100) + .unwrap() + .platform, + Platform::LinuxX64 + ); + assert_eq!( + verify_envelope_with_keyring(&windows, None, &windows_raw, 100) + .unwrap() + .platform, + Platform::WindowsX64 + ); + assert!(verify_envelope_with_keyring(&windows, None, &linux_raw, 100).is_err()); + assert!(verify_envelope_with_keyring(&linux, None, &windows_raw, 100).is_err()); + let fixture: Value = serde_json::from_str(include_str!( + "../../../tests/agent_update/codec-golden.json" + )) + .unwrap(); + let mut binding: UpdaterBinding = serde_json::from_value( + fixture["linux_full"]["payload"]["jobs"][0]["payload"]["action"]["update"]["updater"] + .clone(), + ) + .unwrap(); + binding.keyring_generation = 7; + binding.keyring_sha256 = hash(&linux_raw); + assert!(verify_envelope_with_keyring(&linux, Some(&binding), &linux_raw, 100).is_ok()); + binding.keyring_generation = 8; + assert!(verify_envelope_with_keyring(&linux, Some(&binding), &linux_raw, 100).is_err()); + binding.keyring_generation = 7; + binding.keyring_sha256 = hash(b"different raw bytes"); + assert!(verify_envelope_with_keyring(&linux, Some(&binding), &linux_raw, 100).is_err()); + let mut forged = linux.clone(); + forged.signature = URL_SAFE_NO_PAD.encode([0u8; 64]); + assert!(verify_envelope_with_keyring(&forged, None, &linux_raw, 100).is_err()); + let mut revoked = linux_keys; + revoked.keys[0].revoked = true; + assert!( + verify_envelope_with_keyring(&linux, None, &encode(&revoked).unwrap(), 100).is_err() + ); + let mut changed = linux.decode().unwrap().manifest; + changed.platform = Platform::WindowsX64; + let mut forged = linux; + forged.payload_bytes = URL_SAFE_NO_PAD.encode(encode(&changed).unwrap()); + assert!(verify_envelope_with_keyring(&forged, None, &linux_raw, 100).is_err()); + } + #[test] fn actual_codec_pair_plan_requires_exact_per_attempt_artifact_authority() { let fixtures: Value = serde_json::from_str(include_str!( "../../../tests/agent_update/codec-golden.json" diff --git a/crates/updater-linux/src/files.rs b/crates/updater-linux/src/files.rs index 530cab6..b557797 100644 --- a/crates/updater-linux/src/files.rs +++ b/crates/updater-linux/src/files.rs @@ -113,6 +113,7 @@ pub struct Directory { fd: File, path: PathBuf, identity: Identity, + agent_state: bool, } impl Directory { /// All ancestors are held while opening their child with O_NOFOLLOW. A @@ -143,13 +144,46 @@ impl Directory { identity: Identity::of(&m), fd, path: path.to_path_buf(), + agent_state: false, + }) + } + /// Read/stage-input traversal only, rooted at the fixed private agent state. + /// Install roots continue using the stricter root-ancestor constructor. + /// Every descendant is opened relative to a held fd, without symlinks, and + /// must remain private UID993 state; no network-selected ancestor is trusted. + pub fn open_agent_state(path: &Path) -> Result { + let base = Path::new("/var/lib/rmm-agent-remote"); + let suffix = path.strip_prefix(base).context("UPDATE_AGENT_STATE_ROOT")?; + let root = Self::open(base, 993, true)?; + let mut fd = root.fd.try_clone()?; + for component in suffix.components() { + let Component::Normal(name) = component else { + anyhow::bail!("UPDATE_AGENT_STATE_COMPONENT") + }; + fd = openat(&fd, name, libc::O_RDONLY | libc::O_DIRECTORY, 0)?; + let m = fd.metadata()?; + ensure!( + m.is_dir() && m.uid() == 993 && m.mode() & 0o077 == 0, + "UPDATE_AGENT_STATE_PRIVATE_DIRECTORY" + ); + } + root.verify()?; + Ok(Self { + identity: Identity::of(&fd.metadata()?), + fd, + path: path.into(), + agent_state: true, }) } pub fn identity(&self) -> &Identity { &self.identity } pub fn verify(&self) -> Result<()> { - let current = Self::open(&self.path, self.identity.uid, false)?; + let current = if self.agent_state { + Self::open_agent_state(&self.path)? + } else { + Self::open(&self.path, self.identity.uid, false)? + }; ensure!( current.identity == self.identity && Identity::of(&self.fd.metadata()?) == self.identity, @@ -198,7 +232,7 @@ impl Directory { ); Ok(()) } - fn absent(&self, basename: &OsStr) -> Result<()> { + pub fn absent(&self, basename: &OsStr) -> Result<()> { let value = name(basename)?; let mut m = std::mem::MaybeUninit::::uninit(); // SAFETY: live dirfd, valid basename, properly sized writable stat. @@ -251,6 +285,13 @@ impl HeldFile { pub fn proof(&self) -> &FileProof { &self.proof } + /// A held read-only, hash-verified fd for bounded streaming into a stage. + /// The destination independently verifies the final length/hash/EOF again. + pub fn reader(&mut self) -> Result<&mut File> { + self.verify(self.proof.bytes)?; + self.fd.seek(SeekFrom::Start(0))?; + Ok(&mut self.fd) + } pub fn verify(&mut self, max: u64) -> Result<()> { let m = self.fd.metadata()?; ensure!( @@ -544,6 +585,9 @@ pub struct Installed { new: FileProof, } impl Installed { + pub fn role(&self) -> Role { + self.role + } pub fn old(&self) -> &FileProof { &self.old } @@ -564,10 +608,14 @@ impl Installed { dir.file(OsStr::new(&self.backup), &self.old, 256 * 1024 * 1024)?; let displaced = self.backup.replace(".previous", ".rejected"); dir.absent(OsStr::new(&displaced))?; - let effect = Effect::RestorePredecessor { role: self.role }; - fence.before_effect(&effect)?; + let save = Effect::SaveRejectedCandidate { role: self.role }; + fence.before_effect(&save)?; guard.verify_for_effect(budget)?; dir.rename_new(OsStr::new(&self.target), OsStr::new(&displaced))?; + dir.file(OsStr::new(&displaced), &self.new, 256 * 1024 * 1024)?; + fence.after_effect(&save, &digest(&serde_json::to_vec(&self.new)?))?; + let effect = Effect::RestorePredecessor { role: self.role }; + fence.before_effect(&effect)?; // No existing target can be overwritten; both code generations remain. guard.verify_for_effect(budget)?; dir.rename_new(OsStr::new(&self.backup), OsStr::new(&self.target))?; diff --git a/crates/updater-linux/src/lib.rs b/crates/updater-linux/src/lib.rs index b4128ff..2873c9e 100644 --- a/crates/updater-linux/src/lib.rs +++ b/crates/updater-linux/src/lib.rs @@ -64,6 +64,7 @@ pub enum Effect { PublishReplacement { role: files::Role }, StartService { service: services::Service }, RestorePredecessor { role: files::Role }, + SaveRejectedCandidate { role: files::Role }, ReturnLedger, } diff --git a/crates/updater-linux/src/services.rs b/crates/updater-linux/src/services.rs index e1474e5..b8b8278 100644 --- a/crates/updater-linux/src/services.rs +++ b/crates/updater-linux/src/services.rs @@ -150,22 +150,117 @@ impl Systemd { ], budget, )?; - let text = std::str::from_utf8(&output)?; - let mut fields = BTreeMap::new(); - for row in text.lines() { - let (k, v) = row + parse_service_status(&output) + } + fn failure_status(service: Service, budget: &Budget) -> Result> { + let bytes = command( + &[ + "show", + service.unit(), + "-p", + "SubState", + "-p", + "ControlPID", + "-p", + "Result", + "-p", + "ExecMainCode", + "-p", + "ExecMainStatus", + "-p", + "Job", + "-p", + "ControlGroup", + ], + budget, + )?; + let mut status = BTreeMap::new(); + for line in std::str::from_utf8(&bytes)?.lines() { + let (k, v) = line .split_once('=') - .ok_or_else(|| anyhow::anyhow!("UPDATE_SYSTEMD_RESPONSE"))?; + .ok_or_else(|| anyhow::anyhow!("UPDATE_FAILED_STATUS"))?; ensure!( - fields.insert(k.into(), v.into()).is_none(), - "UPDATE_SYSTEMD_DUPLICATE" + status.insert(k.to_owned(), v.to_owned()).is_none(), + "UPDATE_FAILED_STATUS_DUPLICATE" ); } + ensure!(status.len() == 7, "UPDATE_FAILED_STATUS_FIELDS"); + Ok(status) + } + fn failed_start(&self, service: Service, budget: &Budget) -> Result { + let pin = self + .policies + .iter() + .find(|p| p.service == service) + .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_NOT_PINNED"))?; + let first = Self::status(service, budget)?; + let extra = Self::failure_status(service, budget)?; ensure!( - fields.len() == 8 && fields.get("LoadState").is_some_and(|v| v == "loaded"), - "UPDATE_SYSTEMD_LOADED" + first["ActiveState"] == "failed" + && first["MainPID"] == "0" + && extra["SubState"] == "failed" + && extra["ControlPID"] == "0" + && extra["Job"].is_empty() + && extra["ControlGroup"].is_empty() + && matches!( + extra["Result"].as_str(), + "exit-code" + | "signal" + | "core-dump" + | "timeout" + | "protocol" + | "resources" + | "start-limit-hit" + ) + && first["InvocationID"].len() == 32 + && first["InvocationID"].bytes().any(|b| b != b'0') + && first["InvocationID"].bytes().all(|b| b.is_ascii_hexdigit()) + && unit_composition(&first)? == pin.unit_sha256, + "UPDATE_START_FAILURE_NOT_CLOSED" ); - Ok(fields) + ensure!( + Self::status(service, budget)? == first + && Self::failure_status(service, budget)? == extra, + "UPDATE_START_FAILURE_CHANGED" + ); + let mut status = first; + status.extend(extra); + Ok(FailedStartProof { + service, + unit_sha256: pin.unit_sha256.clone(), + status, + }) + } + pub fn verify_failed_start(&self, expected: &FailedStartProof, budget: &Budget) -> Result<()> { + ensure!( + self.failed_start(expected.service, budget)? == *expected, + "UPDATE_FAILED_START_CHANGED" + ); + Ok(()) + } + pub fn verify_inactive(&self, service: Service, budget: &Budget) -> Result<()> { + let pin = self + .policies + .iter() + .find(|p| p.service == service) + .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_NOT_PINNED"))?; + let status = Self::status(service, budget)?; + let extra = Self::failure_status(service, budget)?; + ensure!( + status["ActiveState"] == "inactive" + && status["MainPID"] == "0" + && extra["ControlPID"] == "0" + && extra["Job"].is_empty() + && extra["ControlGroup"].is_empty() + && unit_composition(&status)? == pin.unit_sha256, + "UPDATE_SERVICE_NOT_CLOSED_INACTIVE" + ); + ensure!( + Self::status(service, budget)? == status + && Self::failure_status(service, budget)? == extra, + "UPDATE_INACTIVE_SERVICE_CHANGED" + ); + Ok(()) } pub fn observe(&self, service: Service, budget: &Budget) -> Result { let pin = self @@ -265,6 +360,9 @@ impl Systemd { let result = StoppedServices { systemd: self, original, + failed: None, + subsequently_exited: Vec::new(), + early_full_failure: None, }; result.verify_still_stopped(budget)?; Ok(result) @@ -356,17 +454,170 @@ impl HeldService { pub struct StoppedServices { systemd: Systemd, original: Vec, + failed: Option, + subsequently_exited: Vec, + early_full_failure: Option, +} +/// Concrete systemd failure of our acknowledged once-only start. A timeout, +/// lost command reply, activating/auto-restart or residual control process is +/// never represented by this value. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct FailedStartProof { + pub service: Service, + pub unit_sha256: String, + pub status: BTreeMap, +} +pub enum Startup { + Complete { + started: Vec, + stopped: StoppedServices, + }, + KnownFailed(KnownStartupFailure), +} +/// Retained only by the live worker that actually started and stopped the new +/// General under the original conditional reservation. Not deserializable. +pub struct EarlyFullFailure { + general_start: ServiceProof, + general_stop_sha256: String, + failure: FailedStartProof, +} +impl EarlyFullFailure { + pub fn general_start(&self) -> &ServiceProof { + &self.general_start + } + pub fn general_stop_sha256(&self) -> &str { + &self.general_stop_sha256 + } + pub fn failure(&self) -> &FailedStartProof { + &self.failure + } + pub fn failure_sha256(&self) -> Result { + Ok(digest(&serde_json::to_vec(&self.failure)?)) + } +} +pub struct KnownStartupFailure { + stopped: StoppedServices, + target: Systemd, + started: Vec, + failure: FailedStartProof, +} +impl KnownStartupFailure { + pub fn failure(&self) -> &FailedStartProof { + &self.failure + } + pub fn started(&self) -> &[HeldService] { + &self.started + } + pub fn verify(&self, budget: &Budget) -> Result<()> { + budget.check()?; + self.target.verify_failed_start(&self.failure, budget)?; + for held in &self.started { + held.verify(budget)?; + } + for service in [Service::General, Service::Full, Service::Agent] { + if service == self.failure.service + || self.started.iter().any(|s| s.proof.service == service) + { + continue; + } + self.target.verify_inactive(service, budget)?; + } + ensure!( + self.stopped + .original + .iter() + .all(|s| s.process.exited().unwrap_or(false)), + "UPDATE_OLD_PROCESS_STILL_LIVE" + ); + Ok(()) + } + /// An early Full failure leaves only the newly started General reader. + /// The embedding fence must hold its original first-return conditional + /// reservation. No SQLite may be opened until this exact held PID exits. + pub fn stop_reserved_general( + mut self, + budget: &Budget, + fence: &mut impl DurableEffectFence, + ) -> Result<(StoppedServices, ServiceProof, String)> { + self.verify(budget)?; + ensure!( + self.failure.service == Service::Full + && self.started.len() == 1 + && self.started[0].proof.service == Service::General, + "UPDATE_CONDITIONAL_STOP_ONLY_EARLY_FULL_FAILURE" + ); + let held = &self.started[0]; + let proof = held.proof.clone(); + let effect = Effect::StopService { + service: Service::General, + }; + fence.before_effect(&effect)?; + command(&["--no-block", "stop", Service::General.unit()], budget)?; + loop { + budget.check()?; + self.target.verify_failed_start(&self.failure, budget)?; + self.target.verify_inactive(Service::Agent, budget)?; + let state = Systemd::status(Service::General, budget)?; + ensure!( + state["MainPID"] == "0" || state["MainPID"] == proof.process.pid.to_string(), + "UPDATE_CONDITIONAL_STOP_PID_CHANGED" + ); + if state["ActiveState"] == "inactive" + && state["MainPID"] == "0" + && held.process.exited()? + { + break; + } + thread::sleep(Duration::from_millis(20)); + } + let observation = digest(&serde_json::to_vec(&( + "reserved_general_stop_v1", + &proof, + &self.failure, + ))?); + fence.after_effect(&effect, &observation)?; + self.stopped.subsequently_exited.append(&mut self.started); + self.stopped.failed = Some(self.failure.clone()); + self.stopped.early_full_failure = Some(EarlyFullFailure { + general_start: proof.clone(), + general_stop_sha256: observation.clone(), + failure: self.failure, + }); + self.stopped.verify_still_stopped(budget)?; + Ok((self.stopped, proof, observation)) + } } impl StoppedServices { + pub fn early_full_failure(&self, budget: &Budget) -> Result<&EarlyFullFailure> { + self.verify_still_stopped(budget)?; + let proof = self + .early_full_failure + .as_ref() + .ok_or_else(|| anyhow::anyhow!("UPDATE_NO_RETAINED_EARLY_FAILURE"))?; + ensure!( + self.subsequently_exited.len() == 1 + && self.subsequently_exited[0].proof == proof.general_start + && self.subsequently_exited[0].process.exited()?, + "UPDATE_RETAINED_GENERAL_STOP_CHANGED" + ); + Ok(proof) + } pub fn verify_still_stopped(&self, budget: &Budget) -> Result<()> { for pin in &self.systemd.policies { let s = Systemd::status(pin.service, budget)?; - ensure!( - s["ActiveState"] == "inactive" - && s["MainPID"] == "0" - && unit_composition(&s)? == pin.unit_sha256, - "UPDATE_READER_NOT_STOPPED" - ); + if let Some(failed) = self.failed.as_ref().filter(|f| f.service == pin.service) { + // Unit composition is still the provisioned original unit; + // only code/config files were part of the approved transition. + self.systemd.verify_failed_start(failed, budget)?; + } else { + ensure!( + s["ActiveState"] == "inactive" + && s["MainPID"] == "0" + && unit_composition(&s)? == pin.unit_sha256, + "UPDATE_READER_NOT_STOPPED" + ); + } if pin.service == Service::Apt { ensure!( s["UnitFileState"] == "disabled", @@ -377,6 +628,7 @@ impl StoppedServices { ensure!( self.original .iter() + .chain(self.subsequently_exited.iter()) .all(|s| s.process.exited().unwrap_or(false)), "UPDATE_OLD_PROCESS_STILL_LIVE" ); @@ -393,6 +645,17 @@ impl StoppedServices { budget: &Budget, fence: &mut impl DurableEffectFence, ) -> Result> { + match self.start_once_observed(new, budget, fence)? { + Startup::Complete { started, .. } => Ok(started), + Startup::KnownFailed(_) => anyhow::bail!("UPDATE_START_KNOWN_FAILED"), + } + } + pub fn start_once_observed( + self, + new: Systemd, + budget: &Budget, + fence: &mut impl DurableEffectFence, + ) -> Result { self.verify_still_stopped(budget)?; ensure!( self.systemd @@ -424,6 +687,17 @@ impl StoppedServices { started.push(held); break; } + if state["ActiveState"] == "failed" { + let failure = new.failed_start(service, budget)?; + let result = KnownStartupFailure { + stopped: self, + target: new, + started, + failure, + }; + result.verify(budget)?; + return Ok(Startup::KnownFailed(result)); + } ensure!( state["ActiveState"] == "activating" || state["ActiveState"] == "active", "UPDATE_START_UNPROVEN" @@ -433,10 +707,40 @@ impl StoppedServices { } // Return in the canonical Handoff stop-set order, not actual start order. started.sort_by_key(|s| s.proof.service); - Ok(started) + Ok(Startup::Complete { + started, + stopped: self, + }) } } +fn parse_service_status(raw: &[u8]) -> Result> { + const FIELDS: [&str; 7] = [ + "LoadState", + "ActiveState", + "MainPID", + "InvocationID", + "UnitFileState", + "FragmentPath", + "DropInPaths", + ]; + let mut fields = BTreeMap::new(); + for row in std::str::from_utf8(raw)?.lines() { + let (k, v) = row + .split_once('=') + .ok_or_else(|| anyhow::anyhow!("UPDATE_SYSTEMD_RESPONSE"))?; + ensure!( + FIELDS.contains(&k) && fields.insert(k.into(), v.into()).is_none(), + "UPDATE_SYSTEMD_PROPERTIES" + ); + } + ensure!( + fields.len() == FIELDS.len() && fields.get("LoadState").is_some_and(|s| s == "loaded"), + "UPDATE_SYSTEMD_LOADED" + ); + Ok(fields) +} + fn unit_composition(fields: &BTreeMap) -> Result { use crate::files::Directory; use std::{ffi::OsStr, path::Path}; @@ -550,3 +854,20 @@ fn command(args: &[&str], budget: &Budget) -> Result> { thread::sleep(Duration::from_millis(5)); } } + +#[cfg(test)] +mod status_tests { + use super::*; + #[test] + fn accepts_actual_seven_property_shape_and_denies_missing_or_extra_properties() { + let status=b"LoadState=loaded\nActiveState=active\nMainPID=123\nInvocationID=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nUnitFileState=enabled\nFragmentPath=/etc/systemd/system/rmm-agent-remote.service\nDropInPaths=\n"; + assert_eq!(parse_service_status(status).unwrap().len(), 7); + let mut extra = status.to_vec(); + extra.extend_from_slice(b"Unexpected=1\n"); + assert!(parse_service_status(&extra).is_err()); + assert!(parse_service_status(b"LoadState=loaded\nActiveState=active\n").is_err()); + let mut duplicate = status.to_vec(); + duplicate.extend_from_slice(b"MainPID=321\n"); + assert!(parse_service_status(&duplicate).is_err()); + } +} diff --git a/crates/updater-worker/examples/linux_updater_bootstrap.rs b/crates/updater-worker/examples/linux_updater_bootstrap.rs new file mode 100644 index 0000000..fe74537 --- /dev/null +++ b/crates/updater-worker/examples/linux_updater_bootstrap.rs @@ -0,0 +1,355 @@ +//! Administrative public metadata only. No files, keys, services or jobs written. +use anyhow::{Result, ensure}; +use ed25519_dalek::VerifyingKey; +use rmm_domain::{ + agent_update::{ReleaseEnvelope, ReleaseManifest, UpdateKeyringFile}, + agent_update_provisioning::{HealthKeyPin, InstalledReleaseRecord, LocalUpdatePolicy}, + jobs::hash, +}; +use serde::Deserialize; +use std::io::Read; +use uuid::Uuid; +#[derive(Deserialize)] +#[serde(tag = "operation", rename_all = "snake_case", deny_unknown_fields)] +enum Input { + RenderFull { + config: Vec, + pin: Vec, + job_public_key_hex: String, + new_helper_sha256: String, + }, + Registry { + qualification_sha256: String, + }, + QualifyPair { + minimum_generation: u64, + expected_agent_sha256: String, + expected_helper_sha256: String, + unit_pins: Vec, + }, + ValidatePublicBundle { + record: Vec, + policy: Vec, + keyring: Vec, + health_pin: Vec, + }, + CanonicalRelease { + manifest: ReleaseManifest, + }, + CanonicalRecord { + record: InstalledReleaseRecord, + }, + CanonicalPolicy { + policy: Box, + }, + ValidateRelease { + envelope: ReleaseEnvelope, + }, +} +/// Independent, read-only administrator sample after the coordinated pair +/// bootstrap. Fresh normal server signatures are checked at the CURRENT native +/// clock; neither old historical rows nor bootstrap metadata create a lease. +fn qualify_pair( + minimum_generation: u64, + agent_sha256: &str, + helper_sha256: &str, + pins: &[rmm_domain::agent_update_provisioning::LocalServicePin], +) -> Result { + use prost::Message; + use rmm_agent_core::{ + auth::Binding, + auth::HelperIdentity, + crypto::DalekVerifier, + helper_reconcile::{SignedResponse, verify_checkpoint_repair_history_with}, + reconcile::{Channel, SyncClock, verify_response_with}, + }; + use rmm_protocol::{SyncRequest, SyncResponse}; + use rmm_updater_linux::{ + Budget, + services::{Service, ServicePolicy, Systemd}, + }; + use rusqlite::{Connection, OpenFlags}; + use std::os::{fd::AsRawFd, unix::fs::MetadataExt}; + ensure!( + unsafe { libc::geteuid() } == 0 + && minimum_generation > 0 + && [agent_sha256, helper_sha256] + .iter() + .all(|v| rmm_domain::agent_update::digest(v)), + "BOOTSTRAP_QUALIFICATION_SCOPE" + ); + let boot: Uuid = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")? + .trim() + .parse()?; + let budget = Budget::new(boot, std::time::Duration::from_secs(30))?; + let clock = rmm_updater_worker::clock::NativeClock::new()?; + let mut policies = Vec::new(); + for (index, pin) in pins.iter().enumerate() { + let service = match index { + 0 => Service::Agent, + 1 => Service::General, + 2 => Service::Full, + 3 => Service::Apt, + _ => anyhow::bail!("BOOTSTRAP_UNIT_PIN_SET"), + }; + ensure!(pin.unit == service.unit(), "BOOTSTRAP_UNIT_PIN_SET"); + policies.push(ServicePolicy { + service, + image_sha256: if index == 0 { + agent_sha256 + } else { + helper_sha256 + } + .into(), + unit_sha256: pin.unit_sha256.clone(), + }); + } + let system = Systemd::new(policies)?; + let held = [ + system.observe(Service::Agent, &budget)?, + system.observe(Service::General, &budget)?, + system.observe(Service::Full, &budget)?, + ]; + if pins.len() == 4 { + system.verify_inactive(Service::Apt, &budget)?; + } + let binding_path = std::path::Path::new("/var/lib/rmm-agent-remote/binding.json"); + let metadata = std::fs::symlink_metadata(binding_path)?; + ensure!( + metadata.is_file() + && !metadata.file_type().is_symlink() + && metadata.uid() == 993 + && metadata.nlink() == 1 + && metadata.mode() & 0o077 == 0, + "BOOTSTRAP_AGENT_PUBLIC_BINDING" + ); + let binding_raw = std::fs::read(binding_path)?; + ensure!(binding_raw.len() <= 8192, "BOOTSTRAP_BINDING_LIMIT"); + let mut binding: Binding = serde_json::from_slice(&binding_raw)?; + #[derive(Deserialize)] + #[serde(deny_unknown_fields)] + struct PublicKeyRecord { key_id:String, algorithm:String, schema_version:u32, public_key_hex:String } + let mut effective = None; + for (path,owner) in [("/var/lib/rmm-agent-remote/job-signing-key.json",993),("/etc/rmm-remote/helper-signing-key.json",0)] { + let path=std::path::Path::new(path);let info=std::fs::symlink_metadata(path)?; + ensure!(info.is_file()&&!info.file_type().is_symlink()&&info.uid()==owner&&info.nlink()==1&&info.mode()&0o022==0&&info.len()<=4096,"BOOTSTRAP_PROTECTED_PUBLIC_KEY_RECORD"); + let raw=std::fs::read(path)?;let key:PublicKeyRecord=serde_json::from_slice(&raw)?;let public:[u8;32]=hex::decode(&key.public_key_hex)?.try_into().map_err(|_|anyhow::anyhow!("BOOTSTRAP_PUBLIC_KEY_SIZE"))?; + ensure!(key.schema_version==1&&key.algorithm=="Ed25519"&&key.key_id==hash(&public)&&!VerifyingKey::from_bytes(&public)?.is_weak(),"BOOTSTRAP_PUBLIC_KEY_RECORD"); + if let Some(old)=&effective{ensure!(old==&key.public_key_hex,"BOOTSTRAP_INDEPENDENT_KEY_PINS_DIFFER");}else{effective=Some(key.public_key_hex);} + ensure!(std::fs::read(path)?==raw,"BOOTSTRAP_PUBLIC_KEY_RECORD_CHANGED"); + } + let effective=effective.ok_or_else(||anyhow::anyhow!("BOOTSTRAP_PUBLIC_KEY_REQUIRED"))?; + ensure!(binding.public_key_hex.is_empty()||binding.public_key_hex==effective,"BOOTSTRAP_BINDING_PUBLIC_KEY");binding.public_key_hex=effective; + let lock = std::fs::File::open("/var/lib/rmm-helper/executor.lock")?; + ensure!( + unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0, + "BOOTSTRAP_HELPER_BUSY" + ); + let open = |path: &str| -> Result { + ensure!( + !std::path::Path::new(&format!("{path}-wal")).exists() + && !std::path::Path::new(&format!("{path}-shm")).exists(), + "BOOTSTRAP_DELETE_JOURNAL_REQUIRED" + ); + let db = Connection::open_with_flags( + path, + OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX, + )?; + db.busy_timeout(std::time::Duration::from_millis(250))?; + db.execute_batch("PRAGMA query_only=ON;")?; + ensure!( + db.query_row("PRAGMA journal_mode", [], |r| r.get::<_, String>(0))? == "delete" + && db.query_row("PRAGMA integrity_check", [], |r| r.get::<_, String>(0))? == "ok", + "BOOTSTRAP_JOURNAL_INTEGRITY" + ); + Ok(db) + }; + let agent = open("/var/lib/rmm-agent-remote/journal.sqlite")?; + let consumption = open("/var/lib/rmm-agent-remote/consumption.sqlite")?; + let helper = open("/var/lib/rmm-helper/consumption.sqlite")?; + ensure!(agent.query_row("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')",[],|r|r.get::<_,u64>(0))?==0,"BOOTSTRAP_OPEN_AGENT_JOB"); + for db in [&consumption, &helper] { + ensure!( + db.query_row( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get::<_, u64>(0) + )? == 0, + "BOOTSTRAP_UNRESOLVED_CONSUMPTION" + ); + } + let (generation,decision):(u64,String)=agent.query_row("SELECT generation,last_decision FROM reconcile_meta WHERE id=1 AND fault IS NULL AND pending_request IS NULL",[],|r|Ok((r.get(0)?,r.get(1)?)))?; + ensure!( + generation > minimum_generation, + "BOOTSTRAP_NEW_GENERATION_REQUIRED" + ); + let (h_generation, h_decision): (u64, String) = helper.query_row( + "SELECT generation,last_decision FROM helper_sync_meta WHERE id=1 AND fault IS NULL", + [], + |r| Ok((r.get(0)?, r.get(1)?)), + )?; + ensure!( + generation == h_generation && decision == h_decision, + "BOOTSTRAP_PAIR_NORMAL_SYNC_NOT_ALIGNED" + ); + let (hrequest, hresponse): (Vec, Vec) = helper.query_row( + "SELECT request,response FROM helper_sync_decisions WHERE decision_id=?1", + [&decision], + |r| Ok((r.get(0)?, r.get(1)?)), + )?; + let signed: SignedResponse = serde_json::from_slice(&hresponse)?; + let request = SyncRequest::decode(hrequest.as_slice())?; + ensure!( + request.encode_to_vec() == hrequest, + "BOOTSTRAP_SYNC_REQUEST_CANONICAL" + ); + let response = SyncResponse { + decision_bytes: signed.decision_bytes.clone(), + signature: signed.signature.clone(), + }; + let applied = verify_response_with( + &DalekVerifier, + &binding, + &request, + &Channel { + connection_generation: generation, + boot_id: boot, + }, + &response, + clock.now()?, + )?; + ensure!( + applied.decision_id.to_string() == decision + && applied.ready + && applied.helper_ready + && applied + .attempts + .iter() + .all(|a| a.update_inflight.is_none() && a.outcome_state != "unknown"), + "BOOTSTRAP_FRESH_NORMAL_SYNC_REQUIRED" + ); + let stored: Vec = agent.query_row( + "SELECT response FROM reconcile_requests WHERE id=?1 AND generation=?2", + rusqlite::params![request.request_id, generation], + |r| r.get(0), + )?; + ensure!( + SyncResponse::decode(stored.as_slice())? == response, + "BOOTSTRAP_BOTH_NORMAL_ORIGINAL_RESPONSES" + ); + verify_checkpoint_repair_history_with( + &helper, + &binding, + None, + HelperIdentity::Root, + &DalekVerifier, + )?; + for service in &held { + service.verify(&budget)?; + } + ensure!( + std::fs::read(binding_path)? == binding_raw, + "BOOTSTRAP_BINDING_CHANGED" + ); + clock.now()?; + budget.check()?; + Ok( + serde_json::json!({"schema_version":1,"profile":"linux_compatible_baseline_qualification_v1","boot_id":boot,"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, + "services":held.iter().map(|v|v.proof()).collect::>(),"normal_signed_sync_observed":true,"generation":generation,"decision_id":decision,"request_sha256":hash(&hrequest),"decision_sha256":hash(&signed.decision_bytes),"signature_sha256":hash(&signed.signature),"observed_at":clock.now()?,"ready":false,"helper_ready":false,"unlock":false,"job_authority":false,"apt_native_runtime_qualified":false}), + ) +} + +fn run() -> Result<()> { + ensure!(std::env::args().count() == 1, "BOOTSTRAP_NO_ARGUMENTS"); + let mut raw = Vec::new(); + std::io::stdin() + .take(512 * 1024 + 1) + .read_to_end(&mut raw)?; + ensure!(raw.len() <= 512 * 1024, "BOOTSTRAP_INPUT_LIMIT"); + let output = match serde_json::from_slice::(&raw)? { + Input::RenderFull { + config, + pin, + job_public_key_hex, + new_helper_sha256, + } => { + let key: [u8; 32] = hex::decode(job_public_key_hex)? + .try_into() + .map_err(|_| anyhow::anyhow!("BOOTSTRAP_PUBLIC_KEY_SIZE"))?; + let key = VerifyingKey::from_bytes(&key)?; + let value = + rmm_updater_handoff::render_linux_full(&config, &pin, &new_helper_sha256, &key)?; + serde_json::json!({"proof":value.proof(),"config":value.new_config_bytes(),"pin":value.new_pin_bytes()}) + } + Input::QualifyPair { + minimum_generation, + expected_agent_sha256, + expected_helper_sha256, + unit_pins, + } => qualify_pair( + minimum_generation, + &expected_agent_sha256, + &expected_helper_sha256, + &unit_pins, + )?, + Input::Registry { + qualification_sha256, + } => { + let boot: Uuid = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")? + .trim() + .parse()?; + let budget = rmm_updater_linux::Budget::new(boot, std::time::Duration::from_secs(30))?; + serde_json::to_value(rmm_updater_worker::reader_registry::bootstrap_metadata( + &qualification_sha256, + &budget, + )?)? + } + Input::CanonicalRelease { manifest } => { + ensure!(manifest.validate(), "BOOTSTRAP_RELEASE_SHAPE"); + let bytes = serde_json::to_vec(&manifest)?; + serde_json::json!({"bytes":bytes,"sha256":hash(&bytes)}) + } + Input::CanonicalRecord { record } => { + let bytes = record.bytes().map_err(anyhow::Error::msg)?; + serde_json::json!({"bytes":bytes,"sha256":hash(&bytes)}) + } + Input::CanonicalPolicy { policy } => { + ensure!(policy.validate(), "BOOTSTRAP_POLICY_SHAPE"); + let bytes = serde_json::to_vec(&policy)?; + serde_json::json!({"bytes":bytes,"sha256":hash(&bytes)}) + } + Input::ValidateRelease { envelope } => { + let decoded = envelope.decode().map_err(anyhow::Error::msg)?; + serde_json::json!({"manifest":decoded.manifest,"manifest_sha256":hash(&decoded.payload_bytes),"shape_only_not_signature_verified":true}) + } + Input::ValidatePublicBundle { + record, + policy, + keyring, + health_pin, + } => { + let installed = + InstalledReleaseRecord::from_bytes(&record).map_err(anyhow::Error::msg)?; + let (local, policy_sha256) = + LocalUpdatePolicy::from_bytes(&policy).map_err(anyhow::Error::msg)?; + let keys: UpdateKeyringFile = serde_json::from_slice(&keyring)?; + let health: HealthKeyPin = serde_json::from_slice(&health_pin)?; + ensure!( + keys.validate() + && health.validate() + && local.initial_record_sha256 == hash(&record) + && local.initial_activation_floor == installed.activation_floor, + "BOOTSTRAP_PUBLIC_BUNDLE_SHAPE" + ); + serde_json::json!({"record_sha256":hash(&record),"policy_sha256":policy_sha256,"keyring_sha256":hash(&keyring),"health_pin_sha256":hash(&health_pin),"shape_only_not_signature_verified":true}) + } + }; + println!("{}", serde_json::to_string(&output)?); + Ok(()) +} +fn main() { + if run().is_err() { + eprintln!("LINUX_BOOTSTRAP_METADATA_FAILED"); + std::process::exit(1) + } +} diff --git a/crates/updater-worker/src/bridge.rs b/crates/updater-worker/src/bridge.rs index d0c8546..bce67f1 100644 --- a/crates/updater-worker/src/bridge.rs +++ b/crates/updater-worker/src/bridge.rs @@ -40,8 +40,10 @@ const MAX_REPLY: usize = 1536 * 1024; pub enum Generation { Original, Replacement, + Restored, } pub struct BridgeContext<'a> { + pub binding: &'a rmm_agent_core::auth::Binding, pub handoff: &'a Handoff, pub delegation: &'a UpdateDelegation, pub services: &'a Systemd, @@ -62,6 +64,7 @@ pub struct Reply { pub original_start: Option>, pub claim: Option, pub original_outcome: Option>, + pub pre_stop_sync: Option<(Vec, Vec)>, } impl<'a> FullBridge<'a> { /// Prefix is either the exact initial atomic StopIntent or a complete @@ -137,18 +140,20 @@ impl<'a> FullBridge<'a> { "UPDATE_BRIDGE_CLAIM_PHASE" ), NativeControlOperation::ObserveStarts { .. } => ensure!( - context.generation == Generation::Replacement - && journal - .as_ref() - .is_some_and(|j| j.phase() == Phase::LedgerReturnIntent), + journal.as_ref().is_some_and(|j| matches!( + (context.generation, j.phase()), + (Generation::Replacement, Phase::LedgerReturnIntent) + | (Generation::Restored, Phase::RollbackLedgerReturnIntent) + )), "UPDATE_BRIDGE_START_PHASE" ), NativeControlOperation::Health { receipt } => { ensure!( - context.generation == Generation::Replacement - && journal - .as_ref() - .is_some_and(|j| j.phase() == Phase::ServicesStarted), + journal.as_ref().is_some_and(|j| matches!( + (context.generation, j.phase()), + (Generation::Replacement, Phase::ServicesStarted) + | (Generation::Restored, Phase::RollbackServicesStarted) + )), "UPDATE_BRIDGE_HEALTH_PHASE" ); journal @@ -156,16 +161,23 @@ impl<'a> FullBridge<'a> { .context("UPDATE_BRIDGE_ORIGINAL_READ_REQUIRED")? .prepare( u64::try_from(context.clock.now()?.timestamp())?, - rmm_updater_handoff::Evidence::HealthVerified { - receipt: receipt.clone(), + if context.generation == Generation::Restored { + rmm_updater_handoff::Evidence::RollbackHealthVerified { + receipt: receipt.clone(), + } + } else { + rmm_updater_handoff::Evidence::HealthVerified { + receipt: receipt.clone(), + } }, )?; } NativeControlOperation::Finalize => ensure!( - context.generation == Generation::Replacement - && journal - .as_ref() - .is_some_and(|j| j.phase() == Phase::HealthVerified), + journal.as_ref().is_some_and(|j| matches!( + (context.generation, j.phase()), + (Generation::Replacement, Phase::HealthVerified) + | (Generation::Restored, Phase::RollbackHealthVerified) + )), "UPDATE_BRIDGE_FINAL_PHASE" ), } @@ -227,6 +239,11 @@ impl<'a> FullBridge<'a> { && peer.pid != context.delegation.original_helper.pid, "UPDATE_BRIDGE_REPLACEMENT_HELPER_CHANGED" ), + Generation::Restored => ensure!( + peer.image_sha256 == context.handoff.predecessor.helper_sha256 + && peer.pid != context.delegation.original_helper.pid, + "UPDATE_BRIDGE_RESTORED_HELPER_CHANGED" + ), } let request = NativeControlRequest { operation: "agent_update_control_v1".into(), @@ -276,6 +293,21 @@ impl<'a> FullBridge<'a> { && reply.handoff_sha256 == request.handoff_sha256, "UPDATE_BRIDGE_REPLY_BINDING" ); + let pre_stop_sync = match ( + &reply.pre_stop_sync_request_base64url, + &reply.pre_stop_sync_response_base64url, + ) { + (None, None) => None, + (Some(raw_request), Some(response)) + if matches!(request.method, NativeControlOperation::ReadOriginal) => + { + Some(( + decode(raw_request, 256 * 1024)?, + decode(response, 256 * 1024)?, + )) + } + _ => anyhow::bail!("UPDATE_BRIDGE_PRESTOP_FIELDS_DENIED"), + }; let original_start = reply .original_start_base64url .as_deref() @@ -297,8 +329,13 @@ impl<'a> FullBridge<'a> { let outcome: ReconciledOutcome = serde_json::from_slice(raw)?; ensure!( canonical_outcome_bytes(&outcome)? == *raw - && outcome.state == "succeeded" - && !outcome.reboot_required, + && if context.generation == Generation::Restored { + outcome.state == "failed" + && outcome.error_code == "AGENT_UPDATE_RETURNED_TO_PREDECESSOR" + && !outcome.reboot_required + } else { + outcome.state == "succeeded" && !outcome.reboot_required + }, "UPDATE_BRIDGE_NONCANONICAL_OR_UNSUCCESSFUL_OUTCOME" ); } else { @@ -322,6 +359,58 @@ impl<'a> FullBridge<'a> { && proof.events[..prior.events.len()] == prior.events), "UPDATE_BRIDGE_PROOF_ROLLBACK_OR_CHANGED" ); + if let Some((raw, response)) = &pre_stop_sync { + use prost::Message; + let req = rmm_protocol::SyncRequest::decode(raw.as_slice())?; + let signed: rmm_agent_core::helper_reconcile::SignedResponse = + serde_json::from_slice(response)?; + let response = rmm_protocol::SyncResponse { + decision_bytes: signed.decision_bytes, + signature: signed.signature, + }; + let decision = rmm_agent_core::reconcile::verify_response( + context.binding, + &req, + &rmm_agent_core::reconcile::Channel { + boot_id: context.handoff.boot_id, + connection_generation: context.handoff.connection_generation, + }, + &response, + context.clock.now()?, + )?; + let expected = + rmm_domain::agent_update::inflight_decision(proof, context.health_public_key) + .map_err(anyhow::Error::msg)?; + let matching = decision + .attempts + .iter() + .filter(|a| { + a.job_id == context.handoff.attempt.job_id + && a.attempt_id == context.handoff.attempt.attempt_id + }) + .collect::>(); + ensure!(matching.len() == 1, "UPDATE_BRIDGE_PRESTOP_ACK_ATTEMPT"); + let ack = matching[0]; + ensure!( + context.generation == Generation::Original + && journal.phase() == Phase::StopIntent + && req.encode_to_vec() == *raw + && !decision.ready + && !decision.helper_ready + && ack.update_inflight.as_ref() == Some(&expected) + && !ack.unlock + && ack.outcome_state == "running" + && ack.outcome_bytes.is_empty() + && ack.outcome_sha256.is_none() + && ack.grant_id == Some(context.handoff.attempt.grant_id) + && ack.authorization_id == Some(context.handoff.attempt.authorization_id) + && ack.plan_hash.as_deref() + == Some(context.handoff.attempt.plan_sha256.as_str()) + && ack.job_payload_hash == context.handoff.attempt.job_payload_sha256 + && ack.original_boot_id == Some(context.handoff.boot_id), + "UPDATE_BRIDGE_SIGNED_PRESTOP_ACK" + ); + } match request.method { NativeControlOperation::ReadOriginal => ensure!( reply.claim.is_none() && original_start.is_some(), @@ -348,16 +437,20 @@ impl<'a> FullBridge<'a> { ); } NativeControlOperation::ObserveStarts { .. } => ensure!( - context.generation == Generation::Replacement - && journal.phase() == Phase::ServicesStarted - && reply.claim.is_none() + matches!( + (context.generation, journal.phase()), + (Generation::Replacement, Phase::ServicesStarted) + | (Generation::Restored, Phase::RollbackServicesStarted) + ) && 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() + matches!( + (context.generation, journal.phase()), + (Generation::Replacement, Phase::HealthVerified) + | (Generation::Restored, Phase::RollbackHealthVerified) + ) && reply.claim.is_none() && original_start.is_none(), "UPDATE_BRIDGE_HEALTH_REPLY" ), @@ -377,6 +470,7 @@ impl<'a> FullBridge<'a> { claim: reply.claim, original_start, original_outcome, + pre_stop_sync, }) } pub fn head_sha256(&self) -> Result<&str> { @@ -422,8 +516,21 @@ impl<'a> FullBridge<'a> { .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), + match last.evidence { + rmm_updater_handoff::Evidence::ServicesStarted { + services: actual, + observation_sha256: hash, + .. + } if self.context.generation == Generation::Replacement => + actual == services && hash == observation_sha256, + rmm_updater_handoff::Evidence::RollbackServicesStarted { + services: actual, + observation_sha256: hash, + .. + } if self.context.generation == Generation::Restored => + actual == services && hash == observation_sha256, + _ => false, + }, "UPDATE_BRIDGE_STARTED_RESULT_CHANGED" ); Ok(reply) diff --git a/crates/updater-worker/src/engine.rs b/crates/updater-worker/src/engine.rs index 90767ee..ace486d 100644 --- a/crates/updater-worker/src/engine.rs +++ b/crates/updater-worker/src/engine.rs @@ -6,7 +6,7 @@ use crate::{ bridge::{BridgeContext, FullBridge, Generation}, bundle, census, clock::NativeClock, - fence::LedgerFence, + fence::{LedgerFence, RollbackFence}, ledger::{OwnedInputs, OwnedLedger}, original::Original, participants::{StableParticipant, verify_original_helper}, @@ -31,7 +31,7 @@ use rmm_updater_linux::{ Budget, DurableEffectFence, Effect, files::{Directory, HeldFile, LedgerGuard, Role}, ipc::{self, Request, Response, State}, - services::{Service, ServicePolicy, ServiceProof, Systemd}, + services::{Service, ServicePolicy, ServiceProof, Startup, Systemd}, }; use std::{ffi::OsStr, path::Path, time::Duration}; use uuid::Uuid; @@ -97,6 +97,12 @@ struct Queued { budget: Budget, } impl Runtime { + pub fn failed(&self) -> bool { + self.failed + } + pub fn terminal(&self) -> bool { + self.terminal + } pub fn new(binding: Binding) -> Result { let clock = NativeClock::new()?; let budget = observation_budget()?; @@ -155,7 +161,7 @@ impl Runtime { let services = Systemd::new(self.old_policies.clone())?; let budget = observation_budget()?; if let Err(error) = listener.serve_one(&services, &budget, self) { - if self.queued.take().is_some() { + if self.queued.take().is_some() || self.accepted { self.failed = true; } return Err(error); @@ -226,6 +232,7 @@ impl Runtime { .verify_delegation(&header.delegation, &budget)?; let reply = { let mut bridge = FullBridge::original(BridgeContext { + binding: &self.binding, handoff: &header.handoff, delegation: &header.delegation, services: &services, @@ -235,7 +242,19 @@ impl Runtime { health_public_key: self.health_key, generation: Generation::Original, })?; - bridge.call(NativeControlOperation::ReadOriginal)? + loop { + let reply = bridge.call(NativeControlOperation::ReadOriginal)?; + if reply.pre_stop_sync.is_some() { + break reply; + } + // Passive read only, under the ORIGINAL boot/deadline/owner. + // Do not stop a reader before the server has retained and the + // actual Full has applied this exact signed inflight context. + budget.check()?; + self.updater + .verify_delegation(&header.delegation, &budget)?; + std::thread::sleep(Duration::from_millis(50)); + } }; let proof = reply.proof.context("UPDATE_ENGINE_ORIGINAL_PROOF")?; let original = Original::verify( @@ -281,6 +300,7 @@ impl Runtime { census::scan_admission(&targets, &known, self.registry.host_context(), &budget)?; let mut bridge = FullBridge::new( BridgeContext { + binding: &self.binding, handoff: original.handoff(), delegation: original.delegation(), services: &services, @@ -381,9 +401,10 @@ impl Runtime { ledger_identity_sha256: witnesses.ledger.sha256()?, })?; // Do not hold a second connection before all readers really stopped. - let swap = (|| -> Result<()> { + let swap = (|| -> Result> { let stages = witnesses.files.reopen_stages(original.handoff(), &budget)?; let mut fence = LedgerFence::new(&mut ledger)?; + let mut installed = Vec::new(); for (stage, role) in stages .into_iter() @@ -396,23 +417,27 @@ impl Runtime { 0, false, )?; - stage.install(&directory, &os, &budget, &mut fence)?; + installed.push(stage.install(&directory, &os, &budget, &mut fence)?); } fence.finish()?; ledger.append_observed(Evidence::PairVerified { pair: original.handoff().target.clone(), retained_state_sha256: original.handoff().retained_state_sha256.clone(), - }) + })?; + Ok(installed) })(); - if let Err(error) = swap { - // Exactly one best-effort ORIGINAL Unknown CAS while we still own - // the real guard. Failure is preserved; no restoration or retry. - let _ = ledger.mark_unknown( - u64::try_from(self.clock.now()?.timestamp())?, - rmm_updater_handoff::UnknownReason::ReplaceUncertain, - ); - return Err(error); - } + let installed = match swap { + Ok(installed) => installed, + Err(error) => { + // Exactly one best-effort ORIGINAL Unknown CAS while we still own + // the real guard. Failure is preserved; no restoration or retry. + let _ = ledger.mark_unknown( + u64::try_from(self.clock.now()?.timestamp())?, + rmm_updater_handoff::UnknownReason::ReplaceUncertain, + ); + return Err(error); + } + }; let prepared = ledger.prepare_return_and_close()?; // The original DB is actually closed. End the continuous census/lock // borrow before the same concrete guard can be consumed by Return. @@ -432,6 +457,7 @@ impl Runtime { let observer = Systemd::new(target_policies)?; let mut bridge = FullBridge::new( BridgeContext { + binding: &self.binding, handoff: original.handoff(), delegation: original.delegation(), services: &observer, @@ -447,8 +473,139 @@ impl Runtime { agent_startup.verify()?; os.release(&budget, &mut fence)?; agent_startup.return_after_root_release(&budget)?; - let started = stopped.start_once(target, &budget, &mut fence)?; - fence.finish()?; + let started = match stopped.start_once_observed(target, &budget, &mut fence)? { + Startup::Complete { started, .. } => { + fence.finish()?; + started + } + Startup::KnownFailed(failed) => { + let mut conditional = fence.early_full_failure(&failed)?; + let expected_head = conditional.proof().head_sha256.clone(); + let (stopped, _, _) = failed.stop_reserved_general(&budget, &mut conditional)?; + conditional.finish()?; + drop(bridge); + let agent_startup = + BoundAgentLock::bind(&witnesses.retained)?.acquire(&stopped, &budget)?; + let os = LedgerGuard::acquire(&stopped, &witnesses.ledger, &budget)?; + self.registry + .verify_known_failure_stopped(&self.policy, &stopped, &budget)?; + let readers = census::scan_stopped( + &targets, + &admission, + &os, + &agent_startup, + &stopped, + self.updater.held_service(), + &budget, + )?; + let readers = readers.bind( + census::ClosureInputs { + targets: &targets, + root: &os, + agent: &agent_startup, + stopped: &stopped, + updater: self.updater.held_service(), + registry: &self.registry, + }, + &budget, + )?; + let mut ledger = OwnedLedger::open(OwnedInputs { + os: &os, + readers: &readers, + original: &original, + binding: &self.binding, + clock: &self.clock, + budget: &budget, + updater: &self.updater, + retained: &witnesses.retained, + files: &witnesses.files, + stopped: &stopped, + trust: &trust, + })?; + ledger.begin_rollback_after_early_full_failure(&expected_head)?; + ledger.append_rollback_stopped()?; + { + let mut rollback = RollbackFence::new(&mut ledger)?; + for installed in installed.into_iter().rev() { + let directory = Directory::open( + Path::new(installed.role().target()) + .parent() + .context("UPDATE_ROLLBACK_PARENT")?, + 0, + false, + )?; + installed.restore(&directory, &os, &budget, &mut rollback)?; + } + rollback.finish()?; + } + ledger.append_observed(Evidence::PredecessorVerified { + pair: original.handoff().predecessor.clone(), + retained_state_sha256: original.handoff().retained_state_sha256.clone(), + predecessor_record_sha256: original.handoff().predecessor_record_sha256.clone(), + })?; + let prepared = ledger.prepare_rollback_return_and_close()?; + drop(readers); + let reservation = prepared.bind(returned::Inputs { + original: &original, + binding: &self.binding, + clock: &self.clock, + budget: &budget, + updater: &self.updater, + retained: &witnesses.retained, + files: &witnesses.files, + trust: &trust, + })?; + let predecessor = Systemd::new(self.old_policies.clone())?; + let observer = Systemd::new(self.old_policies.clone())?; + let mut restored = FullBridge::new( + BridgeContext { + binding: &self.binding, + handoff: original.handoff(), + delegation: original.delegation(), + services: &observer, + updater: &self.updater, + clock: &self.clock, + budget: &budget, + health_public_key: self.health_key, + generation: Generation::Restored, + }, + reservation.proof().clone(), + )?; + let mut fence = reservation.start_fence(&observer, &mut restored); + agent_startup.verify()?; + os.release(&budget, &mut fence)?; + agent_startup.return_after_root_release(&budget)?; + let started = stopped.start_once(predecessor, &budget, &mut fence)?; + fence.finish()?; + restored.observe_starts(&started)?; + loop { + original.check_current(&self.binding, &self.clock, &budget)?; + self.updater + .verify_delegation(original.delegation(), &budget)?; + witnesses.retained.verify()?; + for service in &started { + service.verify(&budget)?; + } + let reply = restored.call(NativeControlOperation::ReadOriginal)?; + let proof = reply.proof.context("UPDATE_ROLLBACK_HEALTH_PROOF")?; + self.head = Some(proof.head_sha256.clone()); + let (_, journal) = + rmm_domain::agent_update::verify_inflight_proof(&proof, self.health_key) + .map_err(anyhow::Error::msg)?; + match journal.phase() { + Phase::RollbackServicesStarted => { + std::thread::sleep(Duration::from_millis(100)) + } + Phase::RollbackHealthVerified => { + restored.call(NativeControlOperation::Finalize)?; + self.head = None; + return Ok(()); + } + _ => anyhow::bail!("UPDATE_ROLLBACK_HEALTH_PREFIX_CHANGED"), + } + } + } + }; bridge.observe_starts(&started)?; loop { original.check_current(&self.binding, &self.clock, &budget)?; diff --git a/crates/updater-worker/src/fence.rs b/crates/updater-worker/src/fence.rs index c7fb737..91e5d11 100644 --- a/crates/updater-worker/src/fence.rs +++ b/crates/updater-worker/src/fence.rs @@ -118,3 +118,107 @@ impl DurableEffectFence for LedgerFence<'_, '_, '_> { self.observe(effect, observation_sha256) } } + +/// Reverse only a completely verified forward pair after the original +/// guarded journal entered known-failure RollbackStopped. A partial rename or +/// lost reply poisons this fence; it never resumes/restores an Unknown. +pub struct RollbackFence<'w, 'a, 's> { + ledger: &'w mut OwnedLedger<'a, 's>, + pending: Option<(Effect, NativeEffectClaim)>, + preserved: Option, + poisoned: bool, +} +impl<'w, 'a, 's> RollbackFence<'w, 'a, 's> { + pub fn new(ledger: &'w mut OwnedLedger<'a, 's>) -> Result { + ledger.qualify_for_effects()?; + ensure!( + ledger.journal()?.phase() == Phase::RollbackStopped, + "UPDATE_ROLLBACK_NO_RESUME" + ); + Ok(Self { + ledger, + pending: None, + preserved: None, + poisoned: false, + }) + } + pub fn finish(self) -> Result<()> { + ensure!( + !self.poisoned + && self.pending.is_none() + && self.preserved.is_none() + && self.ledger.journal()?.phase() == Phase::PredecessorRestored, + "UPDATE_ROLLBACK_FENCE_INCOMPLETE" + ); + self.ledger.verify_predecessor_pair() + } +} +impl DurableEffectFence for RollbackFence<'_, '_, '_> { + fn before_effect(&mut self, effect: &Effect) -> Result<()> { + ensure!( + !self.poisoned && self.pending.is_none(), + "UPDATE_ROLLBACK_NO_RETRY" + ); + self.poisoned = true; + let native = match *effect { + Effect::SaveRejectedCandidate { role } => { + ensure!(self.preserved.is_none(), "UPDATE_ROLLBACK_SAVE_ORDER"); + self.ledger.append_observed(Evidence::RestoreIntent { + role: role.handoff_role(), + })?; + NativeEffect::SaveRejectedCandidate { + role: native_role(role), + } + } + Effect::RestorePredecessor { role } => { + ensure!( + self.preserved == Some(role), + "UPDATE_ROLLBACK_RESTORE_WITHOUT_SAVE" + ); + NativeEffect::RestorePredecessor { + role: native_role(role), + } + } + _ => anyhow::bail!("UPDATE_ROLLBACK_EFFECT_DENIED"), + }; + self.pending = Some((effect.clone(), self.ledger.claim_native(&native)?)); + Ok(()) + } + fn after_effect(&mut self, effect: &Effect, observation: &str) -> Result<()> { + let (expected, claim) = self + .pending + .take() + .ok_or_else(|| anyhow::anyhow!("UPDATE_ROLLBACK_NOT_CLAIMED"))?; + ensure!( + self.poisoned && *effect == expected, + "UPDATE_ROLLBACK_CALLBACK_CHANGED" + ); + let (role, preserved) = match *effect { + Effect::SaveRejectedCandidate { role } => (role, true), + Effect::RestorePredecessor { role } => (role, false), + _ => anyhow::bail!("UPDATE_ROLLBACK_EFFECT_DENIED"), + }; + ensure!( + self.ledger + .native_observation_sha256(role.handoff_role(), preserved)? + == observation, + "UPDATE_ROLLBACK_OBSERVATION_CHANGED" + ); + self.ledger.observe_native(&claim, observation)?; + if preserved { + self.ledger.append_observed(Evidence::CandidatePreserved { + role: role.handoff_role(), + observation_sha256: observation.into(), + })?; + self.preserved = Some(role); + } else { + self.ledger.append_observed(Evidence::PredecessorRestored { + role: role.handoff_role(), + observed_sha256: self.ledger.predecessor_sha256(role.handoff_role())?, + })?; + self.preserved = None; + } + self.poisoned = false; + Ok(()) + } +} diff --git a/crates/updater-worker/src/files.rs b/crates/updater-worker/src/files.rs index 51b7f8f..737e779 100644 --- a/crates/updater-worker/src/files.rs +++ b/crates/updater-worker/src/files.rs @@ -283,6 +283,72 @@ impl FileWitnesses { } Ok(()) } + pub fn verify_candidate_saved(&self, which: FileRole, budget: &Budget) -> Result<()> { + budget.check()?; + let file = self.file(which)?; + let path = Path::new(file.role.target()); + let directory = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?; + directory.file( + OsStr::new(&format!( + ".rmm-update-{}-{}.rejected", + self.nonce, + label(file.role) + )), + &file.staged, + 256 * 1024 * 1024, + )?; + directory.file( + OsStr::new(&format!( + ".rmm-update-{}-{}.previous", + self.nonce, + label(file.role) + )), + &file.original, + 256 * 1024 * 1024, + )?; + directory.absent(path.file_name().context("UPDATE_TARGET_NAME")?)?; + directory.absent(OsStr::new(&format!( + ".rmm-update-{}-{}.stage", + self.nonce, + label(file.role) + )))?; + Ok(()) + } + pub fn verify_restored(&self, which: FileRole, budget: &Budget) -> Result<()> { + budget.check()?; + let file = self.file(which)?; + let path = Path::new(file.role.target()); + let directory = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?; + directory.file( + path.file_name().context("UPDATE_TARGET_NAME")?, + &file.original, + 256 * 1024 * 1024, + )?; + directory.file( + OsStr::new(&format!( + ".rmm-update-{}-{}.rejected", + self.nonce, + label(file.role) + )), + &file.staged, + 256 * 1024 * 1024, + )?; + for suffix in ["previous", "stage"] { + directory.absent(OsStr::new(&format!( + ".rmm-update-{}-{}.{}", + self.nonce, + label(file.role), + suffix + )))?; + } + Ok(()) + } + pub fn verify_predecessor_pair(&self, budget: &Budget) -> Result<()> { + for file in &self.files { + self.verify_restored(file.role.handoff_role(), budget)?; + } + Ok(()) + } fn file(&self, which: FileRole) -> Result<&Proof> { let role = role(which)?; self.files diff --git a/crates/updater-worker/src/full.rs b/crates/updater-worker/src/full.rs index 28e6b85..c7f5bf1 100644 --- a/crates/updater-worker/src/full.rs +++ b/crates/updater-worker/src/full.rs @@ -36,6 +36,81 @@ pub struct FullFiles { rendered: LinuxFullTransition, } impl FullFiles { + /// Pure read/render of the fixed protected Full transition. No staging, + /// download, grant, ledger intent, service control or filesystem mutation. + pub fn preview( + new_helper_sha256: &str, + job_key_path: &Path, + job_key_raw_sha256: &str, + budget: &Budget, + ) -> Result { + budget.check()?; + let key_directory = Directory::open( + job_key_path.parent().context("UPDATE_FULL_KEY_PARENT")?, + 0, + false, + )?; + let mut key_file = key_directory.inspect( + job_key_path.file_name().context("UPDATE_FULL_KEY_NAME")?, + 0, + 4096, + )?; + ensure!( + key_file.proof().sha256 == job_key_raw_sha256, + "UPDATE_FULL_KEY_RAW_HASH" + ); + let key_record: SigningKeyRecord = serde_json::from_slice(&key_file.bytes(4096)?)?; + let raw = hex::decode(&key_record.public_key_hex)?; + ensure!( + key_record.schema_version == 1 + && key_record.algorithm == "Ed25519" + && key_record.key_id == hash(&raw), + "UPDATE_FULL_KEY_IDENTITY" + ); + let key = VerifyingKey::from_bytes( + &raw.try_into() + .map_err(|_| anyhow::anyhow!("UPDATE_FULL_KEY_SIZE"))?, + )?; + ensure!(!key.is_weak(), "UPDATE_FULL_WEAK_KEY"); + let directory = Directory::open(Path::new("/etc/rmm-remote"), 0, false)?; + let mut config = directory.inspect(OsStr::new("helper-full-maintenance.json"), 0, 16384)?; + let mut pin = + directory.inspect(OsStr::new("full-maintenance-helper-proof.json"), 0, 8192)?; + let config_bytes = config.bytes(16384)?; + let value: serde_json::Value = serde_json::from_slice(&config_bytes)?; + if let Some(path) = value.get("signing_key_file").and_then(|v| v.as_str()) { + ensure!( + Path::new(path) == job_key_path, + "UPDATE_FULL_KEY_PATH_CHANGED" + ); + } else { + ensure!( + value["binding"]["public_key_hex"].as_str() == Some(&hex::encode(key.as_bytes())), + "UPDATE_FULL_INLINE_KEY_CHANGED" + ); + } + let rendered = + render_linux_full(&config_bytes, &pin.bytes(8192)?, new_helper_sha256, &key)?; + directory.file( + OsStr::new("helper-full-maintenance.json"), + config.proof(), + 16384, + )?; + directory.file( + OsStr::new("full-maintenance-helper-proof.json"), + pin.proof(), + 8192, + )?; + key_directory.file( + job_key_path.file_name().context("UPDATE_FULL_KEY_NAME")?, + key_file.proof(), + 4096, + )?; + budget.check()?; + Ok(serde_json::from_slice(&serde_json::to_vec( + rendered.proof(), + )?)?) + } /// `job_key_path` and `job_key_raw_sha256` come from independently protected /// LOCAL policy, not an update job, IPC request or manifest. The exact key /// identity is additionally bound to the approved Full transition proof. diff --git a/crates/updater-worker/src/history.rs b/crates/updater-worker/src/history.rs index 2470dd1..437a74a 100644 --- a/crates/updater-worker/src/history.rs +++ b/crates/updater-worker/src/history.rs @@ -1,757 +1,28 @@ -//! Read-only current-version history verification under the actual retained -//! executor guard. Every input file must already have been independently opened -//! and authenticated by the local adapter. No request supplies trusted keys. -//! -//! This is admission-oriented verification: current release-key revocations -//! apply. It must NOT gate an Unknown-only historical adjudication. A changed -//! job-signing key needs an independently retained historical Binding; keys are -//! never learned from a job. There is no clock, dispatch, repair or resume here. -use anyhow::{Context, Result, ensure}; -use chrono::{DateTime, Utc}; -use ed25519_dalek::{Signature, VerifyingKey}; -use rmm_agent_core::{ - auth::{Binding, Outcome, Request}, - helper_reconcile::ExecutorLedgerGuard, - ledger, - update_handoff::{self, NativeEffect, NativeEffectClaim}, -}; -use rmm_domain::{ - agent_update::{self, Platform, ReleaseEnvelope, UpdateKeyringFile}, - agent_update_provisioning::{ - ActivationEvidence, BASELINE_SIGNATURE_DOMAIN, InstalledReleaseRecord, MAX_RECORD_BYTES, - SignedBaselineRecord, - }, - jobs::{Action, hash}, -}; -use rmm_updater::{ - ActiveRelease, Platform as ReleasePlatform, SignedRelease, TrustedContext, TrustedUpdateKey, - UpdateKeyring, verify_release, -}; -use rmm_updater_handoff::{Event, Evidence, Handoff, HealthReceipt, Phase, SignedHealth}; -use rusqlite::{Connection, OptionalExtension, params}; -use std::collections::{BTreeMap, BTreeSet}; -use uuid::Uuid; - -const MAX_RECORDS: usize = 64; -const MAX_TOTAL_BYTES: usize = 2 * 1024 * 1024; - -/// `predecessor_records` is an unordered exact set of intermediate records. -/// The signed initial record may optionally appear once. Missing, duplicate, -/// unused/forked or completed-but-omitted records are denied. V1 rollback and -/// historical job-key rotation without its trusted binding are unsupported. -pub fn verify_history( - db: &Connection, - guard: &impl ExecutorLedgerGuard, - binding: &Binding, - protected_keyring: &UpdateKeyringFile, - baseline: &SignedBaselineRecord, - predecessor_records: &[Vec], - active_raw: &[u8], -) -> Result { - guard.check(db)?; - ensure!(protected_keyring.validate(), "UPDATE_HISTORY_KEYRING"); - let initial = baseline.decode().map_err(anyhow::Error::msg)?; - ensure!( - initial.record.platform == Platform::LinuxX64, - "UPDATE_HISTORY_PLATFORM" - ); - let keyring = current_keyring(protected_keyring)?; - let initial_release = initial - .record - .release_envelope - .decode() - .map_err(anyhow::Error::msg)?; - let entry = protected_keyring - .keys - .iter() - .find(|key| hex::decode(&key.public_key_hex).is_ok_and(|raw| hash(&raw) == baseline.key_id)) - .context("UPDATE_HISTORY_BASELINE_KEY_MISSING")?; - ensure!( - !entry.revoked - && entry.platform == initial.record.platform - && (entry.first_sequence..=entry.last_sequence) - .contains(&initial_release.manifest.release_sequence), - "UPDATE_HISTORY_BASELINE_KEY_REVOKED_OR_RANGE" - ); - let public: [u8; 32] = hex::decode(&entry.public_key_hex)? - .try_into() - .map_err(|_| anyhow::anyhow!("UPDATE_HISTORY_KEY_SIZE"))?; - let verifier = VerifyingKey::from_bytes(&public)?; - ensure!(!verifier.is_weak(), "UPDATE_HISTORY_WEAK_BASELINE_KEY"); - let mut signed = BASELINE_SIGNATURE_DOMAIN.to_vec(); - signed.extend_from_slice(&initial.payload_bytes); - verifier.verify_strict(&signed, &Signature::from_bytes(&initial.signature))?; - verify_record_release( - &keyring, - protected_keyring.generation, - &initial.record, - initial_release.manifest.protocol_major, - initial_release.manifest.state_schema, - )?; - - ensure!( - predecessor_records.len() < MAX_RECORDS - && !active_raw.is_empty() - && active_raw.len() <= MAX_RECORD_BYTES, - "UPDATE_HISTORY_RECORD_LIMIT" - ); - let mut total = active_raw - .len() - .checked_add(initial.payload_bytes.len()) - .context("UPDATE_HISTORY_SIZE")?; - let active = InstalledReleaseRecord::from_bytes(active_raw).map_err(anyhow::Error::msg)?; - let active_hash = hash(active_raw); - let baseline_hash = hash(&initial.payload_bytes); - let mut previous = BTreeMap::new(); - for raw in predecessor_records { - total = total - .checked_add(raw.len()) - .context("UPDATE_HISTORY_SIZE")?; - ensure!( - !raw.is_empty() && raw.len() <= MAX_RECORD_BYTES && total <= MAX_TOTAL_BYTES, - "UPDATE_HISTORY_SIZE" - ); - let digest = hash(raw); - ensure!( - digest != active_hash && previous.insert(digest, raw.as_slice()).is_none(), - "UPDATE_HISTORY_DUPLICATE" - ); - } - ensure!(total <= MAX_TOTAL_BYTES, "UPDATE_HISTORY_SIZE"); - let mut chain = vec![(active_hash, active_raw, active.clone())]; - while chain.last().context("UPDATE_HISTORY_EMPTY")?.0 != baseline_hash { - ensure!(chain.len() < MAX_RECORDS, "UPDATE_HISTORY_DEPTH"); - let current = &chain.last().context("UPDATE_HISTORY_EMPTY")?.2; - let digest = current - .previous_record_sha256 - .as_ref() - .context("UPDATE_HISTORY_PARENT_MISSING")?; - let raw = if *digest == baseline_hash { - if let Some(copy) = previous.remove(digest) { - ensure!( - copy == initial.payload_bytes, - "UPDATE_HISTORY_BASELINE_CHANGED" - ); - } - initial.payload_bytes.as_slice() - } else { - previous - .remove(digest) - .context("UPDATE_HISTORY_PARENT_MISSING")? - }; - ensure!( - !chain.iter().any(|(seen, _, _)| seen == digest), - "UPDATE_HISTORY_CYCLE" - ); - let record = InstalledReleaseRecord::from_bytes(raw).map_err(anyhow::Error::msg)?; - chain.push((digest.clone(), raw, record)); - } - ensure!( - previous.is_empty() - && chain.last().context("UPDATE_HISTORY_EMPTY")?.1 == initial.payload_bytes, - "UPDATE_HISTORY_UNUSED_OR_WRONG_ANCHOR" - ); - let mut grants = BTreeSet::new(); - for pair in chain.windows(2) { - let (digest, raw, record) = &pair[0]; - let (parent_digest, _, parent) = &pair[1]; - ensure!( - !record.rolled_back - && record.platform == parent.platform - && record.activation_floor > parent.activation_floor - && record.activated_at >= parent.activated_at, - "UPDATE_HISTORY_FLOOR_OR_ROLLBACK" - ); - verify_record_release( - &keyring, - protected_keyring.generation, - record, - initial_release.manifest.protocol_major, - initial_release.manifest.state_schema, - )?; - let grant = verify_update(db, guard, binding, raw, record, parent_digest, parent)?; - ensure!(grants.insert(grant), "UPDATE_HISTORY_REUSED_GRANT"); - ensure!(*digest == hash(raw), "UPDATE_HISTORY_RECORD_CHANGED"); - } - // Detect a record-file-only rollback/fork: every completed publication in - // this same device ledger must be present in the verified chain. - ensure!( - completed_update_grants(db, binding)? == grants, - "UPDATE_HISTORY_OMITTED_CONSUMPTION" - ); - 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 { - let mut query = db.prepare("SELECT h.grant_id,c.claim FROM agent_update_handoffs h LEFT JOIN agent_update_native_claims c ON c.grant_id=h.grant_id AND c.effect_key='publish_installed_record' WHERE h.phase='outcome_persisted' ORDER BY h.grant_id LIMIT 65")?; - let mut rows = query.query([])?; - let mut seen = BTreeSet::new(); - while let Some(row) = rows.next()? { - let grant: String = row.get(0)?; - let raw: Option> = row.get(1)?; - let raw = raw.context("UPDATE_HISTORY_PUBLICATION_MISSING")?; - ensure!( - raw.len() <= 2048 && seen.len() < MAX_RECORDS, - "UPDATE_HISTORY_PUBLICATION_LIMIT" - ); - let claim: NativeEffectClaim = serde_json::from_slice(&raw)?; - let grant = Uuid::parse_str(&grant)?; - ensure!( - claim.grant_id() == grant && grants.contains(&grant) && seen.insert(grant), - "UPDATE_HISTORY_OMITTED_PUBLICATION" - ); - } - ensure!(seen == grants, "UPDATE_HISTORY_MISSING_PUBLICATION"); - } else { - ensure!(grants.is_empty(), "UPDATE_HISTORY_MISSING_LEDGER"); - } - guard.check(db)?; - Ok(active) -} - -fn current_keyring(file: &UpdateKeyringFile) -> Result { - let mut keys = Vec::new(); - for key in &file.keys { - let public: [u8; 32] = hex::decode(&key.public_key_hex)? - .try_into() - .map_err(|_| anyhow::anyhow!("UPDATE_HISTORY_KEY_SIZE"))?; - keys.push(TrustedUpdateKey::new( - public, - match key.platform { - Platform::LinuxX64 => ReleasePlatform::LinuxX64, - Platform::WindowsX64 => ReleasePlatform::WindowsX64, - }, - key.first_sequence, - key.last_sequence, - key.revoked, - )?); - } - UpdateKeyring::new(file.generation, keys) -} - -/// The authoritative completed set starts at ORIGINAL consumptions, not at an -/// optional successor proof row. Stream a bounded local history, including old -/// non-update jobs, without exposing any request/script bytes outside here. -fn completed_update_grants(db: &Connection, binding: &Binding) -> Result> { - let mut query=db.prepare("SELECT c.grant_id,e.start,e.outcome,c.result FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) WHERE c.result IS NOT NULL OR e.outcome IS NOT NULL ORDER BY c.grant_id LIMIT 4097")?; - let mut rows = query.query([])?; - let mut count = 0; - let mut bytes = 0usize; - let mut grants = BTreeSet::new(); - while let Some(row) = rows.next()? { - count += 1; - ensure!(count <= 4096, "UPDATE_HISTORY_CONSUMPTION_LIMIT"); - let grant: String = row.get(0)?; - let start: Option> = row.get(1)?; - let outcome: Option> = row.get(2)?; - let finished: Option> = row.get(3)?; - for raw in [&start, &outcome, &finished].into_iter().flatten() { - bytes = bytes - .checked_add(raw.len()) - .context("UPDATE_HISTORY_CONSUMPTION_SIZE")?; - ensure!( - raw.len() <= 512 * 1024 && bytes <= 16 * 1024 * 1024, - "UPDATE_HISTORY_CONSUMPTION_SIZE" - ); - } - let raw = outcome - .as_ref() - .or(finished.as_ref()) - .context("UPDATE_HISTORY_COMPLETION_MISSING")?; - if let (Some(a), Some(b)) = (&outcome, &finished) { - let a: rmm_domain::reconciliation::ReconciledOutcome = serde_json::from_slice(a)?; - let b: rmm_domain::reconciliation::ReconciledOutcome = serde_json::from_slice(b)?; - ensure!( - rmm_domain::reconciliation::canonical_outcome_bytes(&a)? - == rmm_domain::reconciliation::canonical_outcome_bytes(&b)?, - "UPDATE_HISTORY_COMPLETION_CHANGED" - ); - } - let value: Outcome = serde_json::from_slice(raw)?; - if value.state != "succeeded" { - continue; - } - let start = start.context("UPDATE_HISTORY_START_MISSING")?; - let start: serde_json::Value = serde_json::from_slice(&start)?; - let request: Request = serde_json::from_value(start["request"].clone())?; - let reserved_at: DateTime = serde_json::from_value(start["reserved_at"].clone())?; - let (job, permit) = binding.verify(&request, reserved_at)?; - ensure!( - permit.grant_id.to_string() == grant, - "UPDATE_HISTORY_CONSUMPTION_CHANGED" - ); - if matches!(job.action, Action::UpdateAgent { .. }) { - ensure!( - grants.len() < MAX_RECORDS && grants.insert(permit.grant_id), - "UPDATE_HISTORY_COMPLETION_LIMIT" - ); - } - } - Ok(grants) -} - -fn verify_record_release( - keys: &UpdateKeyring, - generation: u64, - record: &InstalledReleaseRecord, - baseline_protocol_major: u32, - baseline_state_schema: u32, -) -> Result<()> { - ensure!( - !record.rolled_back, - "UPDATE_HISTORY_V1_ROLLBACK_UNSUPPORTED" - ); - let decoded = record - .release_envelope - .decode() - .map_err(anyhow::Error::msg)?; - let platform = match record.platform { - Platform::LinuxX64 => ReleasePlatform::LinuxX64, - Platform::WindowsX64 => ReleasePlatform::WindowsX64, - }; - let envelope: &ReleaseEnvelope = &record.release_envelope; - verify_release( - SignedRelease { - schema_version: envelope.schema_version, - key_id: &envelope.key_id, - payload_bytes: &decoded.payload_bytes, - signature: &decoded.signature, - }, - keys, - &TrustedContext { - platform, - now_utc_seconds: record.activated_at, - keyring_generation: generation, - // Anchor compatibility to the separately signed, qualified - // bootstrap. A signed successor cannot attest its own decoder - // compatibility by supplying both sides of this comparison. - protocol_major: baseline_protocol_major, - state_schema: baseline_state_schema, - updater_version: 1, - minimum_release_sequence: record.activation_floor, - active_release: Some(ActiveRelease { - sequence: decoded.manifest.release_sequence, - manifest_sha256: record.manifest_sha256.clone(), - }), - }, - )?; - Ok(()) -} - -fn verify_update( - db: &Connection, - guard: &impl ExecutorLedgerGuard, - binding: &Binding, - raw_record: &[u8], - record: &InstalledReleaseRecord, - parent_hash: &str, - parent: &InstalledReleaseRecord, -) -> Result { - let ActivationEvidence::Updated { - handoff_sha256, - signed_health_sha256, - } = &record.activation_evidence - else { - anyhow::bail!("UPDATE_HISTORY_BOOTSTRAP_FORK"); - }; - let mut query = db.prepare("SELECT h.grant_id,h.record,e.start,e.outcome,c.result FROM agent_update_handoffs h JOIN execution_evidence e USING(grant_id) JOIN consumed c USING(grant_id) WHERE h.record_sha256=?1 LIMIT 2")?; - let values = query - .query_map([handoff_sha256], |r| { - Ok(( - r.get::<_, String>(0)?, - r.get::<_, Vec>(1)?, - r.get::<_, Vec>(2)?, - r.get::<_, Option>>(3)?, - r.get::<_, Option>>(4)?, - )) - })? - .collect::>>()?; - ensure!( - values.len() == 1, - "UPDATE_HISTORY_ORIGINAL_MISSING_OR_DUPLICATE" - ); - let (grant, raw_handoff, start, outcome, finished) = &values[0]; - ensure!( - raw_handoff.len() <= rmm_updater_handoff::MAX_RECORD_BYTES && start.len() <= 512 * 1024, - "UPDATE_HISTORY_ORIGINAL_LIMIT" - ); - let grant = Uuid::parse_str(grant)?; - let header: Handoff = serde_json::from_slice(raw_handoff)?; - ensure!( - header.bytes()? == *raw_handoff - && hash(raw_handoff) == *handoff_sha256 - && header.attempt.grant_id == grant - && header.predecessor_record_sha256 == parent_hash, - "UPDATE_HISTORY_ORIGINAL_BINDING" - ); - let start_value: serde_json::Value = serde_json::from_slice(start)?; - let reserved_at: DateTime = serde_json::from_value(start_value["reserved_at"].clone())?; - let request: Request = serde_json::from_value(start_value["request"].clone())?; - // Historical identity/signature/window verification only. Never a live - // execution verifier or a replacement start grant. - let (job, permit) = binding.verify(&request, reserved_at)?; - let Action::UpdateAgent { update } = &job.action else { - anyhow::bail!("UPDATE_HISTORY_JOB_ACTION"); - }; - let previous = parent - .release_envelope - .decode() - .map_err(anyhow::Error::msg)?; - ensure!( - job.execution_identity == "root" - && update.release_envelope == record.release_envelope - && agent_update::handoff_matches_job(&header, &job) - && header.attempt.grant_id == permit.grant_id - && header.attempt.job_payload_sha256 == permit.job_payload_hash - && header.attempt.plan_id - == permit.agent_update_plan_id.context("UPDATE_HISTORY_PLAN")? - && header.attempt.authorization_id == permit.authorization_id - && header.attempt.plan_sha256 == permit.plan_hash - && header.attempt.original_start_witness_sha256 == hash(start) - && header.attempt.original_request_sha256 == hash(&serde_json::to_vec(&request)?) - && header.consumed_at == u64::try_from(reserved_at.timestamp())? - && header.finish_before - == header - .consumed_at - .checked_add(600) - .context("UPDATE_HISTORY_DEADLINE")? - && ledger::start_witness_bytes(&permit, &request, header.boot_id, reserved_at)? - == *start - && update.predecessor.installed_record_sha256 == parent_hash - && update.predecessor.manifest_sha256 == parent.manifest_sha256 - && update.predecessor.release_id == previous.manifest.release_id - && update.predecessor.release_sequence == previous.manifest.release_sequence - && header.predecessor.manifest_sha256 == parent.manifest_sha256 - && header.predecessor.agent_sha256 == previous.manifest.artifacts[0].sha256 - && header.predecessor.helper_sha256 == previous.manifest.artifacts[1].sha256 - && header.target.manifest_sha256 == record.manifest_sha256, - "UPDATE_HISTORY_SIGNED_ORIGINAL_CHANGED" - ); - let (count,size):(u64,u64)=db.query_row("SELECT count(*),COALESCE(sum(length(event)),0) FROM (SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence LIMIT 17)", [grant.to_string()], |r|Ok((r.get(0)?,r.get(1)?)))?; - ensure!( - count <= 16 && size <= 16 * 16384, - "UPDATE_HISTORY_EVENT_LIMIT" - ); - let journal = update_handoff::read(db, guard, grant)?; - ensure!( - journal.phase() == Phase::OutcomePersisted, - "UPDATE_HISTORY_INCOMPLETE_OR_UNKNOWN" - ); - let (helper, agent): (String, Option) = db.query_row( - "SELECT journal_id,agent_journal_id FROM helper_sync_meta WHERE id=1", - [], - |r| Ok((r.get(0)?, r.get(1)?)), - )?; - ensure!( - helper == header.helper_journal_id.to_string() - && agent.as_deref() == Some(header.agent_journal_id.to_string().as_str()), - "UPDATE_HISTORY_LEDGER_LINEAGE" - ); - let mut events = db.prepare( - "SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence", - )?; - let events = events - .query_map([grant.to_string()], |r| r.get::<_, Vec>(0))? - .collect::>>()?; - complete_native_history(db, &header, &events, parent_hash, &hash(raw_record))?; - let mut health: Option<(SignedHealth, u64)> = None; - let mut final_at = None; - for raw in &events { - let event: Event = serde_json::from_slice(raw)?; - match event.evidence { - Evidence::HealthVerified { receipt } => { - ensure!(health.is_none(), "UPDATE_HISTORY_DOUBLE_HEALTH"); - health = Some((receipt, event.observed_at)); - } - Evidence::OutcomePersisted { outcome_sha256 } => { - let original = outcome.as_ref().context("UPDATE_HISTORY_OUTCOME_MISSING")?; - ensure!( - original.len() <= 512 * 1024 && hash(original) == outcome_sha256, - "UPDATE_HISTORY_OUTCOME_CHANGED" - ); - let value: Outcome = serde_json::from_slice(original)?; - ensure!( - value.state == "succeeded" - && !value.reboot_required - && finished.as_ref().is_none_or(|v| v == original), - "UPDATE_HISTORY_OUTCOME_NOT_SUCCESS" - ); - final_at = Some(event.observed_at); - } - _ => {} - } - } - let (health, verified_at) = health.context("UPDATE_HISTORY_HEALTH_MISSING")?; - let receipt: HealthReceipt = serde_json::from_slice(&health.payload)?; - ensure!( - hash(&serde_json::to_vec(&health)?) == *signed_health_sha256 - && record.activated_at == receipt.issued_at - && final_at.context("UPDATE_HISTORY_FINAL_MISSING")? >= verified_at, - "UPDATE_HISTORY_HEALTH_OR_ACTIVATION_CHANGED" - ); - // Journal replay already independently verified the health signature with - // the public key bound by this ORIGINAL signed job and checked its deadline. - let record_hash = hash(raw_record); - let saved = publication( - db, - grant, - "save_installed_record", - parent_hash, - &record_hash, - )?; - let published = publication( - db, - grant, - "publish_installed_record", - parent_hash, - &record_hash, - )?; - ensure!( - saved.0.timestamp() >= i64::try_from(verified_at)? - && published.0 >= saved.1 - && published.1.timestamp() - <= i64::try_from(final_at.context("UPDATE_HISTORY_FINAL_MISSING")?)?, - "UPDATE_HISTORY_PUBLICATION_ORDER" - ); - guard.check(db)?; - Ok(grant) -} - -fn complete_native_history( - db: &Connection, - header: &Handoff, - events: &[Vec], - old: &str, - new: &str, -) -> Result<()> { - use rmm_agent_core::update_handoff::{NativeFileRole as F, NativeService as S}; - let mut expected = BTreeSet::new(); - for service in [S::Agent, S::General, S::Full] { - expected.insert(serde_json::to_string(&NativeEffect::StopService { - service, - })?); - expected.insert(serde_json::to_string(&NativeEffect::StartService { - service, - })?); - } - for role in [F::AgentBinary, F::HelperBinary, F::FullConfig, F::FullPin] { - expected.insert(serde_json::to_string(&NativeEffect::SavePredecessor { - role, - })?); - expected.insert(serde_json::to_string(&NativeEffect::PublishReplacement { - role, - })?); - } - expected.insert(serde_json::to_string(&NativeEffect::ReturnLedger)?); - expected.insert("save_installed_record".into()); - expected.insert("publish_installed_record".into()); - let parsed = events - .iter() - .map(|raw| Ok((hash(raw), serde_json::from_slice::(raw)?))) - .collect::>>()?; - let time = |predicate: &dyn Fn(&Evidence) -> bool| -> Result { - let entries = parsed - .values() - .filter(|event| predicate(&event.evidence)) - .collect::>(); - ensure!(entries.len() == 1, "UPDATE_HISTORY_NATIVE_PHASE_MISSING"); - Ok(entries[0].observed_at) - }; - let grant = header.attempt.grant_id.to_string(); - let mut query=db.prepare("SELECT c.effect_key,c.claim,c.claim_sha256,o.claim_sha256,o.observation_sha256,o.observed_at FROM agent_update_native_claims c LEFT JOIN agent_update_native_observations o USING(grant_id,effect_key) WHERE c.grant_id=?1 ORDER BY c.effect_key LIMIT 18")?; - let mut rows = query.query([grant])?; - let mut seen = BTreeSet::new(); - let mut return_group: Option<(String, DateTime)> = None; - while let Some(row) = rows.next()? { - let key: String = row.get(0)?; - let raw: Vec = row.get(1)?; - let digest: String = row.get(2)?; - ensure!( - seen.len() < 17 - && raw.len() <= 2048 - && hash(&raw) == digest - && expected.contains(&key) - && seen.insert(key.clone()), - "UPDATE_HISTORY_NATIVE_SET" - ); - let claim: NativeEffectClaim = serde_json::from_slice(&raw)?; - let expected_key = match claim.effect() { - NativeEffect::SaveInstalledRecord { - old_sha256, - new_sha256, - } => { - ensure!( - old_sha256 == old && new_sha256 == new, - "UPDATE_HISTORY_RECORD_PAIR" - ); - "save_installed_record".into() - } - NativeEffect::PublishInstalledRecord { - old_sha256, - new_sha256, - } => { - ensure!( - old_sha256 == old && new_sha256 == new, - "UPDATE_HISTORY_RECORD_PAIR" - ); - "publish_installed_record".into() - } - effect => serde_json::to_string(effect)?, - }; - let head = parsed - .get(claim.head_sha256()) - .context("UPDATE_HISTORY_NATIVE_HEAD_MISSING")?; - let expected_head = match claim.effect() { - NativeEffect::StopService { .. } => matches!(head.evidence, Evidence::StopIntent), - NativeEffect::StartService { .. } | NativeEffect::ReturnLedger => { - matches!(head.evidence, Evidence::LedgerReturnIntent) - } - NativeEffect::SaveInstalledRecord { .. } - | NativeEffect::PublishInstalledRecord { .. } => { - matches!(head.evidence, Evidence::HealthVerified { .. }) - } - NativeEffect::SavePredecessor { role } | NativeEffect::PublishReplacement { role } => { - let expected = match role { - F::AgentBinary => rmm_updater_handoff::FileRole::AgentBinary, - F::HelperBinary => rmm_updater_handoff::FileRole::HelperBinary, - F::FullConfig => rmm_updater_handoff::FileRole::LinuxFullConfig, - F::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin, - F::WindowsSettings => rmm_updater_handoff::FileRole::WindowsSettings, - F::WindowsPin => rmm_updater_handoff::FileRole::WindowsPin, - F::WindowsTimeProfile => rmm_updater_handoff::FileRole::WindowsTimeProfile, - }; - matches!(head.evidence,Evidence::ReplaceIntent{role} if role==expected) - } - _ => false, - }; - ensure!( - key == expected_key - && expected_head - && serde_json::to_vec(&claim)? == raw - && claim.grant_id() == header.attempt.grant_id - && claim.owner_nonce() == header.owner_nonce - && u64::try_from(claim.claimed_at().timestamp())? >= head.observed_at - && u64::try_from(claim.claimed_at().timestamp())? >= header.consumed_at - && u64::try_from(claim.claimed_at().timestamp())? < header.finish_before, - "UPDATE_HISTORY_NATIVE_CLAIM_CHANGED" - ); - if matches!( - claim.effect(), - NativeEffect::ReturnLedger | NativeEffect::StartService { .. } - ) { - let group = (claim.head_sha256().to_owned(), claim.claimed_at()); - ensure!( - u64::try_from(group.1.timestamp())? == head.observed_at - && return_group.as_ref().is_none_or(|prior| prior == &group), - "UPDATE_HISTORY_RETURN_STARTS_NOT_ATOMIC" - ); - return_group = Some(group); - } - let observed: Option = row.get(3)?; - let observation: Option = row.get(4)?; - let when: Option = row.get(5)?; - if matches!(claim.effect(), NativeEffect::ReturnLedger) { - ensure!( - observed.is_none() && observation.is_none() && when.is_none(), - "UPDATE_HISTORY_FALSE_RETURN_OBSERVATION" - ); - } else { - ensure!( - observed.as_ref() == Some(&digest) - && observation - .as_ref() - .is_some_and(|v| agent_update::digest(v)), - "UPDATE_HISTORY_NATIVE_OBSERVATION" - ); - let when: DateTime = when.context("UPDATE_HISTORY_NATIVE_OBSERVATION")?.parse()?; - ensure!( - when >= claim.claimed_at() - && u64::try_from(when.timestamp())? < header.finish_before, - "UPDATE_HISTORY_NATIVE_OBSERVATION_TIME" - ); - let end = match claim.effect() { - NativeEffect::StopService { .. } => { - time(&|e| matches!(e, Evidence::AllStopped { .. }))? - } - NativeEffect::StartService { .. } => { - time(&|e| matches!(e, Evidence::ServicesStarted { .. }))? - } - NativeEffect::SaveInstalledRecord { .. } - | NativeEffect::PublishInstalledRecord { .. } => { - time(&|e| matches!(e, Evidence::OutcomePersisted { .. }))? - } - NativeEffect::SavePredecessor { role } - | NativeEffect::PublishReplacement { role } => { - let expected = match role { - F::AgentBinary => rmm_updater_handoff::FileRole::AgentBinary, - F::HelperBinary => rmm_updater_handoff::FileRole::HelperBinary, - F::FullConfig => rmm_updater_handoff::FileRole::LinuxFullConfig, - F::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin, - F::WindowsSettings => rmm_updater_handoff::FileRole::WindowsSettings, - F::WindowsPin => rmm_updater_handoff::FileRole::WindowsPin, - F::WindowsTimeProfile => rmm_updater_handoff::FileRole::WindowsTimeProfile, - }; - time(&|e| matches!(e,Evidence::Replaced{role,..} if *role==expected))? - } - _ => anyhow::bail!("UPDATE_HISTORY_NATIVE_PHASE"), - }; - ensure!( - u64::try_from(when.timestamp())? <= end, - "UPDATE_HISTORY_NATIVE_LATE_OBSERVATION" - ); - if let NativeEffect::PublishReplacement { role } = claim.effect() { - let save = serde_json::to_string(&NativeEffect::SavePredecessor { role: *role })?; - let saved:String=db.query_row("SELECT observed_at FROM agent_update_native_observations WHERE grant_id=?1 AND effect_key=?2",params![header.attempt.grant_id.to_string(),save],|r|r.get(0))?; - let saved: DateTime = saved.parse()?; - ensure!( - saved <= claim.claimed_at(), - "UPDATE_HISTORY_NATIVE_PUBLISH_BEFORE_SAVE" - ); - } - } - } - ensure!(seen == expected, "UPDATE_HISTORY_NATIVE_HISTORY_MISSING"); - Ok(()) -} - -fn publication( - db: &Connection, - grant: Uuid, - effect: &str, - old: &str, - new: &str, -) -> Result<(DateTime, DateTime)> { - let value: Option<(Vec,String,String,String)> = db.query_row( - "SELECT c.claim,c.claim_sha256,o.claim_sha256,o.observed_at FROM agent_update_native_claims c JOIN agent_update_native_observations o USING(grant_id,effect_key) WHERE c.grant_id=?1 AND c.effect_key=?2", - params![grant.to_string(),effect], |r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?))).optional()?; - let (raw, digest, observed, at) = value.context("UPDATE_HISTORY_PUBLICATION_MISSING")?; - ensure!( - raw.len() <= 2048 && hash(&raw) == digest && digest == observed, - "UPDATE_HISTORY_PUBLICATION_CHANGED" - ); - let claim: NativeEffectClaim = serde_json::from_slice(&raw)?; - let expected = if effect == "save_installed_record" { - NativeEffect::SaveInstalledRecord { - old_sha256: old.into(), - new_sha256: new.into(), - } - } else { - NativeEffect::PublishInstalledRecord { - old_sha256: old.into(), - new_sha256: new.into(), - } - }; - let observed: DateTime = at.parse()?; - ensure!( - serde_json::to_vec(&claim)? == raw - && claim.grant_id() == grant - && claim.effect() == &expected - && observed >= claim.claimed_at(), - "UPDATE_HISTORY_PUBLICATION_PAIR" - ); - Ok((claim.claimed_at(), observed)) -} +//! Shared admission history; native Linux fixtures remain here. +pub use rmm_update_history::verify_history; #[cfg(test)] mod tests { + use anyhow::{Result, ensure}; + use chrono::{DateTime, Utc}; + use rmm_agent_core::{ + auth::{Binding, Outcome, Request}, + helper_reconcile::ExecutorLedgerGuard, + ledger, + update_handoff::{self, NativeEffect, NativeEffectClaim}, + }; + use rmm_domain::{ + agent_update::{self, Platform, ReleaseEnvelope, UpdateKeyringFile}, + agent_update_provisioning::{ + ActivationEvidence, BASELINE_SIGNATURE_DOMAIN, InstalledReleaseRecord, + SignedBaselineRecord, + }, + jobs::{Action, hash}, + }; + use rmm_updater_handoff::{Event, Evidence, Handoff, HealthReceipt, SignedHealth}; + use rusqlite::{Connection, params}; + use uuid::Uuid; + // Historical in-memory bytes only: no service, filesystem, real operation, // current authority or simulated WebUI approval is created by this fixture. use super::*; diff --git a/crates/updater-worker/src/ledger.rs b/crates/updater-worker/src/ledger.rs index db770f0..5823fe8 100644 --- a/crates/updater-worker/src/ledger.rs +++ b/crates/updater-worker/src/ledger.rs @@ -212,6 +212,12 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> { NativeEffect::PublishReplacement { role } => { self.native_observation_sha256(file_role(*role), true)? } + NativeEffect::SaveRejectedCandidate { role } => { + self.native_observation_sha256(file_role(*role), true)? + } + NativeEffect::RestorePredecessor { role } => { + self.native_observation_sha256(file_role(*role), false)? + } _ => anyhow::bail!("UPDATE_OWNED_OBSERVATION_NOT_ALLOWED"), }; ensure!(expected == observation, "UPDATE_NATIVE_OBSERVATION_CHANGED"); @@ -243,6 +249,19 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> { pub fn verify_pair(&self) -> Result<()> { self.guard.files.verify_pair(self.guard.budget) } + pub fn verify_predecessor_pair(&self) -> Result<()> { + self.guard.files.verify_predecessor_pair(self.guard.budget) + } + pub fn predecessor_sha256(&self, role: rmm_updater_handoff::FileRole) -> Result { + self.guard + .original + .handoff() + .files + .iter() + .find(|f| f.role == role) + .map(|f| f.old_sha256.clone()) + .ok_or_else(|| anyhow::anyhow!("UPDATE_OLD_FILE_NOT_BOUND")) + } pub fn inflight_proof(&self) -> Result { let journal = self.journal()?; let mut statement = self.db.prepare( @@ -291,6 +310,71 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> { old_services, }) } + pub fn prepare_rollback_return_and_close(mut self) -> Result { + let journal = self.journal()?; + ensure!( + journal.phase() == Phase::PredecessorVerified, + "UPDATE_ROLLBACK_RETURN_REQUIRES_PREDECESSOR" + ); + let now = self.guard.clock.now()?; + let event = journal.prepare( + u64::try_from(now.timestamp())?, + Evidence::RollbackLedgerReturnIntent, + )?; + let claims = update_handoff::prepare_rollback_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, + }) + } + pub fn begin_rollback_after_early_full_failure(&mut self, expected_head: &str) -> Result<()> { + let source = self.guard.stopped.early_full_failure(self.guard.budget)?; + let start = hash(&serde_json::to_vec(source.general_start())?); + let failure = source.failure_sha256()?; + update_handoff::begin_rollback_after_reserved_stop( + &mut self.db, + &self.guard, + update_handoff::ReservedGeneralStop { + grant_id: self.guard.original.permit().grant_id, + expected_head, + general_start_observation_sha256: &start, + general_stop_observation_sha256: source.general_stop_sha256(), + failure_observation_sha256: &failure, + now: self.guard.clock.now()?, + }, + ) + } + pub fn append_rollback_stopped(&mut self) -> Result<()> { + let source = self.guard.stopped.early_full_failure(self.guard.budget)?; + let observation = hash(&serde_json::to_vec(&( + source.general_start(), + source.general_stop_sha256(), + source.failure(), + ))?); + let owner = self.guard.updater.identity(); + self.append_observed(Evidence::RollbackStopped { + process: rmm_updater_handoff::Process { + pid: owner.pid, + birth: owner.birth, + image_sha256: owner.image_sha256.clone(), + }, + ledger_identity_sha256: self.guard.os.identity().sha256()?, + observation_sha256: observation, + }) + } /// 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<()> { @@ -443,6 +527,81 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> { ); } Evidence::LedgerReturnIntent => self.files.verify_pair(self.budget)?, + Evidence::RollbackIntent { + cause: + rmm_updater_handoff::RollbackCause::StartupFailed { + service: rmm_updater_handoff::Service::LinuxFullHelper, + }, + failure_observation_sha256, + } => { + let source = self.stopped.early_full_failure(self.budget)?; + ensure!( + *failure_observation_sha256 == source.failure_sha256()?, + "UPDATE_ROLLBACK_FAILURE_CHANGED" + ); + self.files.verify_pair(self.budget)?; + } + Evidence::RollbackStopped { + process, + ledger_identity_sha256, + observation_sha256, + } => { + let source = self.stopped.early_full_failure(self.budget)?; + let owner = self.updater.identity(); + ensure!( + process.pid == owner.pid + && process.birth == owner.birth + && process.image_sha256 == owner.image_sha256 + && *ledger_identity_sha256 == self.os.identity().sha256()? + && *observation_sha256 + == hash(&serde_json::to_vec(&( + source.general_start(), + source.general_stop_sha256(), + source.failure() + ))?), + "UPDATE_ROLLBACK_STOPPED_WITNESS" + ); + } + Evidence::RestoreIntent { role } => self.files.verify_replaced(*role, self.budget)?, + Evidence::CandidatePreserved { + role, + observation_sha256, + } => { + self.files.verify_candidate_saved(*role, self.budget)?; + ensure!( + *observation_sha256 == self.files.native_observation_sha256(*role, true)?, + "UPDATE_REJECTED_CANDIDATE_HASH" + ); + } + Evidence::PredecessorRestored { + role, + observed_sha256, + } => { + self.files.verify_restored(*role, self.budget)?; + ensure!( + handoff + .files + .iter() + .any(|f| f.role == *role && f.old_sha256 == *observed_sha256), + "UPDATE_RESTORED_PREDECESSOR_HASH" + ); + } + Evidence::PredecessorVerified { + pair, + retained_state_sha256, + predecessor_record_sha256, + } => { + self.files.verify_predecessor_pair(self.budget)?; + ensure!( + *pair == handoff.predecessor + && *retained_state_sha256 == handoff.retained_state_sha256 + && *predecessor_record_sha256 == handoff.predecessor_record_sha256, + "UPDATE_PREDECESSOR_BINDING" + ); + } + Evidence::RollbackLedgerReturnIntent => { + self.files.verify_predecessor_pair(self.budget)? + } _ => anyhow::bail!("UPDATE_EVENT_WRITER_NOT_ALLOWED"), } Ok(()) @@ -492,8 +651,123 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> { | (NativeEffect::StartService { .. }, NativeEffectStage::ReserveFutureStart) => { self.files.verify_pair(self.budget)? } + ( + NativeEffect::ConditionalRollbackStopGeneral, + NativeEffectStage::ReserveConditionalStop, + ) => { + ensure!( + handoff.schema_version == 2 + && delegation.original_helper.role + == rmm_domain::agent_update::ParticipantRole::LinuxFullHelper, + "UPDATE_CONDITIONAL_STOP_PROFILE" + ); + self.stopped.verify_still_stopped(self.budget)?; + self.files.verify_pair(self.budget)?; + } + ( + NativeEffect::StartService { + service: update_handoff::NativeService::General, + }, + NativeEffectStage::ObserveRetainedStart, + ) + | (NativeEffect::ConditionalRollbackStopGeneral, NativeEffectStage::Observe) + | ( + NativeEffect::StartService { + service: + update_handoff::NativeService::Full | update_handoff::NativeService::Agent, + }, + NativeEffectStage::ObserveNonstart, + ) => { + self.stopped.early_full_failure(self.budget)?; + self.files.verify_pair(self.budget)?; + } + ( + NativeEffect::RollbackStopService { + service: + update_handoff::NativeService::Full | update_handoff::NativeService::Agent, + }, + NativeEffectStage::ObserveNonstart, + ) => { + self.stopped.early_full_failure(self.budget)?; + self.files.verify_pair(self.budget)?; + } + (NativeEffect::SaveRejectedCandidate { role: r }, NativeEffectStage::Claim) => { + self.files.verify_replaced(role(r), self.budget)? + } + (NativeEffect::SaveRejectedCandidate { role: r }, NativeEffectStage::Observe) + | (NativeEffect::RestorePredecessor { role: r }, NativeEffectStage::Claim) => { + self.files.verify_candidate_saved(role(r), self.budget)? + } + (NativeEffect::RestorePredecessor { role: r }, NativeEffectStage::Observe) => { + self.files.verify_restored(role(r), self.budget)? + } + (NativeEffect::RollbackReturnLedger, NativeEffectStage::Claim) + | (NativeEffect::RollbackStartService { .. }, NativeEffectStage::ReserveFutureStart) => { + self.files.verify_predecessor_pair(self.budget)? + } _ => anyhow::bail!("UPDATE_NATIVE_OWNED_EFFECT_DENIED"), } Ok(()) } + fn native_observation_sha256( + &self, + h: &Handoff, + d: &UpdateDelegation, + effect: &NativeEffect, + stage: NativeEffectStage, + ) -> Result { + self.verify_native_effect(h, d, effect, stage)?; + let source = self.stopped.early_full_failure(self.budget)?; + match (effect, stage) { + ( + NativeEffect::StartService { + service: update_handoff::NativeService::General, + }, + NativeEffectStage::ObserveRetainedStart, + ) => Ok(hash(&serde_json::to_vec(source.general_start())?)), + (NativeEffect::ConditionalRollbackStopGeneral, NativeEffectStage::Observe) => { + Ok(source.general_stop_sha256().into()) + } + ( + NativeEffect::StartService { + service: update_handoff::NativeService::Full, + }, + NativeEffectStage::ObserveNonstart, + ) => source.failure_sha256(), + ( + NativeEffect::RollbackStopService { + service: update_handoff::NativeService::Full, + }, + NativeEffectStage::ObserveNonstart, + ) => Ok(hash(&serde_json::to_vec(&( + "failed_full_stop_not_needed_v1", + h.owner_nonce, + h.attempt.grant_id, + source.failure_sha256()?, + ))?)), + ( + NativeEffect::RollbackStopService { + service: update_handoff::NativeService::Agent, + }, + NativeEffectStage::ObserveNonstart, + ) => Ok(hash(&serde_json::to_vec(&( + "unstarted_agent_stop_not_needed_v1", + h.owner_nonce, + h.attempt.grant_id, + source.failure_sha256()?, + ))?)), + ( + NativeEffect::StartService { + service: update_handoff::NativeService::Agent, + }, + NativeEffectStage::ObserveNonstart, + ) => Ok(hash(&serde_json::to_vec(&( + "agent_start_not_dispatched_v1", + h.owner_nonce, + h.attempt.grant_id, + source.failure_sha256()?, + ))?)), + _ => anyhow::bail!("UPDATE_OWNED_NATIVE_HASH_NOT_QUALIFIED"), + } + } } diff --git a/crates/updater-worker/src/main.rs b/crates/updater-worker/src/main.rs new file mode 100644 index 0000000..6ac9bdc --- /dev/null +++ b/crates/updater-worker/src/main.rs @@ -0,0 +1,106 @@ +//! Stable local service entrypoint. No network endpoint, command arguments, +//! approval API or startup replay of a previously admitted update. +#[cfg(target_os = "linux")] +mod linux { + use anyhow::{Context, Result, ensure}; + use rmm_agent_core::auth::Binding; + use rmm_updater_linux::{files::Directory, ipc::Listener}; + use rmm_updater_worker::engine::Runtime; + use std::{ + ffi::OsStr, + path::Path, + sync::atomic::{AtomicBool, Ordering}, + }; + + static STOP: AtomicBool = AtomicBool::new(false); + extern "C" fn stop(_: libc::c_int) { + STOP.store(true, Ordering::SeqCst); + } + fn binding() -> Result { + let directory = Directory::open(Path::new("/etc/rmm-agent-updater"), 0, false)?; + let name = OsStr::new("binding.json"); + let mut file = directory.inspect(name, 0, 32768)?; + ensure!( + file.proof().identity.mode & 0o777 == 0o600 && file.proof().identity.links == 1, + "UPDATE_DAEMON_PRIVATE_BINDING" + ); + let bytes = file.bytes(32768)?; + let binding: Binding = serde_json::from_slice(&bytes)?; + let public: [u8; 32] = hex::decode(&binding.public_key_hex)? + .try_into() + .map_err(|_| anyhow::anyhow!("UPDATE_DAEMON_SIGNING_PIN"))?; + ensure!( + !binding.tenant_id.is_nil() + && !binding.asset_id.is_nil() + && !binding.agent_id.is_nil() + && binding.enrollment_generation > 0 + && binding.assignment_version > 0 + && !ed25519_dalek::VerifyingKey::from_bytes(&public)?.is_weak(), + "UPDATE_DAEMON_BINDING" + ); + directory.file(name, file.proof(), 32768)?; + Ok(binding) + } + pub fn run() -> Result<()> { + let args: Vec<_> = std::env::args_os().skip(1).collect(); + if args == [OsStr::new("--help")] { + println!( + "rmm-agent-updater --serve\nFixed protected local binding/policy and authenticated Fullhelper IPC only. No automatic activation or startup replay. A failed/ambiguous pass stops closed; operator recovery must inspect the original handoff." + ); + return Ok(()); + } + ensure!(args == [OsStr::new("--serve")], "UPDATE_DAEMON_ARGUMENTS"); + // SAFETY: geteuid/getegid have no pointer arguments or preconditions. + ensure!( + unsafe { libc::geteuid() } == 0 && unsafe { libc::getegid() } == 0, + "UPDATE_DAEMON_ROOT_SERVICE_REQUIRED" + ); + // SAFETY: handlers only set a lock-free atomic and hold no borrowed state. + unsafe { + let mut action: libc::sigaction = std::mem::zeroed(); + action.sa_sigaction = stop as *const () as usize; + libc::sigemptyset(&mut action.sa_mask); + ensure!( + libc::sigaction(libc::SIGTERM, &action, std::ptr::null_mut()) == 0 + && libc::sigaction(libc::SIGINT, &action, std::ptr::null_mut()) == 0, + "UPDATE_DAEMON_SIGNALS" + ); + } + let binding = binding()?; + let mut runtime = Runtime::new(binding.clone())?; + let listener = Listener::bind()?; + while !STOP.load(Ordering::SeqCst) { + if let Err(error) = runtime.serve_one(&listener) { + // A short idle poll is normal. Other failed handshakes do not + // become an activation; after possible dispatch the runtime's + // fuse closes permanently and this process must stop. + if runtime.failed() { + anyhow::bail!("UPDATE_DAEMON_ORIGINAL_PASS_FAILED"); + } + if error.to_string() != "UPDATE_IPC_NO_REQUEST" { + eprintln!("UPDATE_DAEMON_REQUEST_REJECTED"); + } + } + if runtime.terminal() { + // A successful ORIGINAL finalization permits the service to + // observe its new installed record for a later, separately + // approved attempt. No old handoff is resumed by construction. + runtime = Runtime::new(binding.clone()).context("UPDATE_DAEMON_NEW_BASELINE")?; + } + } + Ok(()) + } +} + +#[cfg(target_os = "linux")] +fn main() { + if linux::run().is_err() { + eprintln!("UPDATE_DAEMON_CLOSED"); + std::process::exit(1); + } +} +#[cfg(not(target_os = "linux"))] +fn main() { + eprintln!("Native Linux updater service only"); + std::process::exit(1); +} diff --git a/crates/updater-worker/src/reader_registry.rs b/crates/updater-worker/src/reader_registry.rs index 2d8d71c..85251c5 100644 --- a/crates/updater-worker/src/reader_registry.rs +++ b/crates/updater-worker/src/reader_registry.rs @@ -231,16 +231,28 @@ impl QualifiedRegistry { Ok(()) } pub fn verify_admission(&self, policy: &LocalUpdatePolicy, budget: &Budget) -> Result<()> { - self.verify_phase(policy, budget, false) + self.verify_phase(policy, budget, false, None) } pub fn verify_stopped(&self, policy: &LocalUpdatePolicy, budget: &Budget) -> Result<()> { - self.verify_phase(policy, budget, true) + self.verify_phase(policy, budget, true, None) + } + pub fn verify_known_failure_stopped( + &self, + policy: &LocalUpdatePolicy, + stopped: &rmm_updater_linux::services::StoppedServices, + budget: &Budget, + ) -> Result<()> { + let source = stopped.early_full_failure(budget)?; + self.verify_phase(policy, budget, true, Some(source.failure()))?; + stopped.early_full_failure(budget)?; + Ok(()) } fn verify_phase( &self, policy: &LocalUpdatePolicy, budget: &Budget, stopped: bool, + failed: Option<&rmm_updater_linux::services::FailedStartProof>, ) -> Result<()> { budget.check()?; self.verify_protected()?; @@ -269,7 +281,15 @@ impl QualifiedRegistry { for (index, unit) in self.registry.units.iter().enumerate() { let active = index < 4 && (!stopped || index == 3); let status = observe(unit, budget)?; - status.verify(unit, active, index < 4)?; + if let Some(failure) = failed.filter(|f| f.service.unit() == unit.unit) { + ensure!( + stopped && index < 3 && !active, + "UPDATE_FAILED_READER_SCOPE" + ); + status.verify_known_failed(unit, failure)?; + } else { + status.verify(unit, active, index < 4)?; + } first.insert(unit.unit.clone(), status); } for unit in &self.registry.rollback_timers { @@ -586,6 +606,41 @@ impl Status { && self.connections.is_none_or(|n| n == 0) && self.fields["Job"].is_empty() } + fn verify_known_failed( + &self, + unit: &Unit, + failure: &rmm_updater_linux::services::FailedStartProof, + ) -> Result<()> { + let f = &self.fields; + ensure!( + !unit.composition.is_empty() + && failure.service.unit() == unit.unit + && f["ActiveState"] == "failed" + && f["SubState"] == "failed" + && self.main_pid == Some(0) + && self.control_pid == Some(0) + && f["Job"].is_empty() + && f["LoadState"] == "loaded" + && f["UnitFileState"] == unit.unit_file_state + && relations(&f["Triggers"])? == unit.triggers + && relations(&f["TriggeredBy"])? == unit.triggered_by, + "UPDATE_FAILED_READER_NOT_CLOSED" + ); + let mut paths = vec![f["FragmentPath"].as_str()]; + paths.extend(f["DropInPaths"].split_ascii_whitespace()); + ensure!( + paths + .into_iter() + .eq(unit.composition.iter().map(|(p, _)| p.as_str())), + "UPDATE_FAILED_READER_UNIT_CHANGED" + ); + for (key, value) in &failure.status { + if let Some(actual) = f.get(key) { + ensure!(actual == value, "UPDATE_FAILED_READER_STATUS_CHANGED"); + } + } + Ok(()) + } fn verify(&self, unit: &Unit, active: bool, original_reader: bool) -> Result<()> { let fields = &self.fields; ensure!( @@ -796,6 +851,145 @@ fn command(args: &[&str], budget: &Budget) -> Result> { } } +/// Public bootstrap metadata only. This does not publish policy, qualify a +/// reader, start the updater or grant any update/job authority. The operator +/// must bind the qualification receipt to actual freshly verified code/sync. +#[derive(Serialize)] +pub struct BootstrapMetadata { + pub registry_bytes: Vec, + pub reader_registry_sha256: String, + pub services: Vec, + pub updater_service: rmm_domain::agent_update_provisioning::LocalServicePin, +} +/// Read the actual fixed 21-unit closure AFTER compatible pair startup and +/// installation of the still-stopped stable updater unit. Never invent file IDs +/// or infer a registry from source unit text; retain each measured file until +/// the second identical actual observation. No filesystem/SCM mutations. +pub fn bootstrap_metadata( + qualification_sha256: &str, + budget: &Budget, +) -> Result { + ensure!( + digest(qualification_sha256) && unsafe { libc::geteuid() } == 0, + "UPDATE_BOOTSTRAP_METADATA_SCOPE" + ); + let host_context = current_host_context()?; + let first_rollback = rollback_names(budget)?; + let names = fixed_units() + .into_iter() + .chain(first_rollback.iter().cloned()) + .collect::>(); + let mut retained = Vec::new(); + let mut units = Vec::new(); + let mut first = Vec::new(); + for (index, name) in names.iter().enumerate() { + let mut unit = Unit { + unit: name.clone(), + unit_file_state: String::new(), + composition: Vec::new(), + triggers: Vec::new(), + triggered_by: Vec::new(), + }; + let status = observe(&unit, budget)?; + let fields = &status.fields; + unit.unit_file_state = fields["UnitFileState"].clone(); + unit.triggers = relations(&fields["Triggers"])?; + unit.triggered_by = relations(&fields["TriggeredBy"])?; + ensure!( + unit.triggers + .iter() + .chain(&unit.triggered_by) + .all(|n| names.contains(n)), + "UPDATE_BOOTSTRAP_UNKNOWN_ACTIVATION_RELATION" + ); + if fields["LoadState"] == "loaded" { + let mut paths = vec![fields["FragmentPath"].as_str()]; + paths.extend(fields["DropInPaths"].split_ascii_whitespace()); + ensure!( + !paths[0].is_empty() && paths.len() <= 17, + "UPDATE_BOOTSTRAP_UNIT_PATHS" + ); + for (position, path) in paths.into_iter().enumerate() { + let leaf = Path::new(path).file_name().and_then(|v| v.to_str()); + ensure!( + unit_path(path) + && if position == 0 { + leaf == Some(name.as_str()) + } else { + leaf.is_some_and(|v| v.ends_with(".conf")) + }, + "UPDATE_BOOTSTRAP_UNIT_PATH" + ); + let held = Protected::open(Path::new(path))?; + unit.composition.push((path.into(), held.proof.clone())); + retained.push(held); + } + } + // The prospective updater is deliberately inactive until ALL protected + // trust/baseline/retained material has been independently provisioned. + status.verify(&unit, index < 3, index < 4)?; + if index >= 4 && !unit.composition.is_empty() { + ensure!( + unit.unit_file_state == "disabled", + "UPDATE_BOOTSTRAP_CLOSED_UNIT_AUTOSTART" + ); + } + ensure!(retained.len() <= MAX_FILES, "UPDATE_BOOTSTRAP_FILE_LIMIT"); + first.push(status); + units.push(unit); + } + ensure!( + units.len() == 21 + first_rollback.len() && !units[3].composition.is_empty(), + "UPDATE_BOOTSTRAP_UPDATER_UNIT_REQUIRED" + ); + for (unit, status) in units.iter().zip(&first) { + ensure!( + observe(unit, budget)? == *status, + "UPDATE_BOOTSTRAP_UNIT_CHANGED" + ); + } + for held in &retained { + held.verify()?; + } + ensure!( + rollback_names(budget)? == first_rollback && current_host_context()? == host_context, + "UPDATE_BOOTSTRAP_HOST_OR_TIMER_CHANGED" + ); + let pin = |unit: &Unit| -> Result { + Ok(rmm_domain::agent_update_provisioning::LocalServicePin { + unit: unit.unit.clone(), + unit_sha256: hash(&serde_json::to_vec(&unit.composition)?), + }) + }; + let mut services = units[..3].iter().map(pin).collect::>>()?; + if !units[4].composition.is_empty() { + services.push(pin(&units[4])?); + } + let updater_service = pin(&units[3])?; + let rollback_timers = units.split_off(21); + let registry = Registry { + schema_version: 1, + profile: "linux_shared_readers_v1".into(), + bootstrap_receipt_sha256: qualification_sha256.into(), + reader_contract_sha256: reader_contract_sha256(), + host_context, + units, + rollback_timers, + }; + let registry_bytes = serde_json::to_vec(®istry)?; + ensure!( + registry_bytes.len() <= MAX_BYTES as usize, + "UPDATE_BOOTSTRAP_REGISTRY_LIMIT" + ); + budget.check()?; + Ok(BootstrapMetadata { + reader_registry_sha256: hash(®istry_bytes), + registry_bytes, + services, + updater_service, + }) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/updater-worker/src/record.rs b/crates/updater-worker/src/record.rs index f93dc5d..26cdae9 100644 --- a/crates/updater-worker/src/record.rs +++ b/crates/updater-worker/src/record.rs @@ -8,7 +8,7 @@ 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}, + agent_update_provisioning::{InstalledReleaseRecord, MAX_RECORD_BYTES}, jobs::hash, }; use rmm_updater_handoff::{ @@ -131,41 +131,15 @@ impl BuiltRecord { 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 record = rmm_domain::agent_update_provisioning::assemble_updated_record( + update, + prior_raw, + handoff_sha256, + signed, + receipt, + ) + .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"); @@ -667,6 +641,7 @@ mod tests { // 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 rmm_domain::agent_update_provisioning::ActivationEvidence; use std::{fs, os::unix::fs::DirBuilderExt, path::PathBuf, time::Duration}; struct Fixture(PathBuf); diff --git a/crates/updater-worker/src/retained.rs b/crates/updater-worker/src/retained.rs index 101df49..b0aaa1b 100644 --- a/crates/updater-worker/src/retained.rs +++ b/crates/updater-worker/src/retained.rs @@ -87,7 +87,12 @@ impl RetainedSet { "UPDATE_RETAINED_CODE_PATH" ); let parent = spec.path.parent().context("UPDATE_RETAINED_PARENT")?; - let directory = Directory::open(parent, spec.parent_uid, false)?; + let directory = if parent.starts_with("/var/lib/rmm-agent-remote") { + ensure!(spec.parent_uid == 993, "UPDATE_RETAINED_AGENT_OWNER"); + Directory::open_agent_state(parent)? + } else { + Directory::open(parent, spec.parent_uid, false)? + }; let file = std::fs::OpenOptions::new() .read(true) .custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC) diff --git a/crates/updater-worker/src/returned.rs b/crates/updater-worker/src/returned.rs index 8f43957..102c766 100644 --- a/crates/updater-worker/src/returned.rs +++ b/crates/updater-worker/src/returned.rs @@ -13,7 +13,7 @@ use rmm_domain::{agent_update::UpdateInflightProof, jobs::hash}; use rmm_updater_linux::{ Budget, DurableEffectFence, Effect, files::{Directory, Identity, LedgerIdentity}, - services::{Service, ServiceProof, Systemd}, + services::{FailedStartProof, KnownStartupFailure, Service, ServiceProof, Systemd}, }; use std::{ fs::OpenOptions, @@ -103,7 +103,13 @@ impl<'a> Reservation<'a> { .updater .verify_delegation(self.context.original.delegation(), self.context.budget)?; self.context.retained.verify()?; - self.context.files.verify_pair(self.context.budget)?; + if self.rollback()? { + self.context + .files + .verify_predecessor_pair(self.context.budget)?; + } else { + self.context.files.verify_pair(self.context.budget)?; + } self.context .trust .recheck(self.context.original, self.context.clock)?; @@ -121,22 +127,31 @@ impl<'a> Reservation<'a> { .map_err(anyhow::Error::msg)?; ensure!( observed.bytes()? == h.bytes()? - && journal.phase() == rmm_updater_handoff::Phase::LedgerReturnIntent - && self.claims.return_ledger.effect() == &NativeEffect::ReturnLedger, + && matches!( + journal.phase(), + rmm_updater_handoff::Phase::LedgerReturnIntent + | rmm_updater_handoff::Phase::RollbackLedgerReturnIntent + ) + && self.claims.return_ledger.effect() + == &if self.rollback()? { + NativeEffect::RollbackReturnLedger + } else { + 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" - ); + let service = [ + NativeService::General, + NativeService::Full, + NativeService::Agent, + ][index]; + let expected = if self.rollback()? { + NativeEffect::RollbackStartService { service } + } else { + NativeEffect::StartService { service } + }; + ensure!(claim.effect() == &expected, "UPDATE_RETURN_START_ORDER"); } for claim in std::iter::once(&self.claims.return_ledger).chain(self.claims.starts.iter()) { ensure!( @@ -151,6 +166,14 @@ impl<'a> Reservation<'a> { verify_identity_files(&self.context.ledger)?; Ok(()) } + fn rollback(&self) -> Result { + let (_, journal) = rmm_domain::agent_update::verify_inflight_proof( + &self.proof, + self.context.trust.health_public_key(), + ) + .map_err(anyhow::Error::msg)?; + Ok(journal.phase() == rmm_updater_handoff::Phase::RollbackLedgerReturnIntent) + } /// 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>( @@ -181,7 +204,7 @@ pub struct StartFence<'a, 'b> { pending: Option, poisoned: bool, } -impl StartFence<'_, '_> { +impl<'a, 'b> StartFence<'a, 'b> { pub fn finish(self) -> Result<()> { ensure!( !self.poisoned && self.return_checked && self.next == 3 && self.pending.is_none(), @@ -189,6 +212,121 @@ impl StartFence<'_, '_> { ); Ok(()) } + pub fn early_full_failure( + self, + failed: &KnownStartupFailure, + ) -> Result> { + failed.verify(self.reservation.context.budget)?; + ensure!( + self.return_checked + && self.next == 1 + && self.poisoned + && self.pending == Some(Service::Full) + && failed.failure().service == Service::Full + && failed.started().len() == 1 + && failed.started()[0].proof().service == Service::General, + "UPDATE_CONDITIONAL_STOP_START_LINEAGE" + ); + let claim = self + .reservation + .claims + .conditional_rollback_stop_general() + .ok_or_else(|| anyhow::anyhow!("UPDATE_CONDITIONAL_STOP_NOT_RESERVED"))?; + ensure!( + claim.effect() == &NativeEffect::ConditionalRollbackStopGeneral + && claim.grant_id() == self.reservation.context.original.permit().grant_id + && claim.owner_nonce() == self.reservation.context.original.handoff().owner_nonce + && claim.head_sha256() == self.reservation.proof.head_sha256, + "UPDATE_CONDITIONAL_STOP_RESERVATION_CHANGED" + ); + Ok(ConditionalStopFence { + reservation: self.reservation, + new: self.new, + failure: failed.failure().clone(), + general: failed.started()[0].proof().clone(), + pending: false, + finished: false, + }) + } +} +/// The one precommitted exceptional stop. It cannot be constructed from JSON, +/// reused for another service, or used after losing the original live attempt. +pub struct ConditionalStopFence<'a, 'b> { + reservation: Reservation<'a>, + new: &'b Systemd, + failure: FailedStartProof, + general: ServiceProof, + pending: bool, + finished: bool, +} +impl ConditionalStopFence<'_, '_> { + pub fn proof(&self) -> &UpdateInflightProof { + self.reservation.proof() + } + pub fn finish(self) -> Result<()> { + ensure!( + self.pending && self.finished, + "UPDATE_CONDITIONAL_STOP_INCOMPLETE" + ); + Ok(()) + } +} +impl DurableEffectFence for ConditionalStopFence<'_, '_> { + fn before_effect(&mut self, effect: &Effect) -> Result<()> { + ensure!( + !self.pending + && !self.finished + && *effect + == Effect::StopService { + service: Service::General + }, + "UPDATE_CONDITIONAL_STOP_NO_RETRY" + ); + self.pending = true; + self.reservation.check()?; + verify_lock_released(&self.reservation.context.ledger)?; + self.new + .verify_failed_start(&self.failure, self.reservation.context.budget)?; + self.new + .verify_inactive(Service::Agent, self.reservation.context.budget)?; + let held = self + .new + .observe(Service::General, self.reservation.context.budget)?; + ensure!( + *held.proof() == self.general, + "UPDATE_CONDITIONAL_STOP_GENERAL_CHANGED" + ); + Ok(()) + } + fn after_effect(&mut self, effect: &Effect, observation: &str) -> Result<()> { + ensure!( + self.pending + && !self.finished + && *effect + == Effect::StopService { + service: Service::General + }, + "UPDATE_CONDITIONAL_STOP_CALLBACK_CHANGED" + ); + self.reservation.check()?; + self.new + .verify_failed_start(&self.failure, self.reservation.context.budget)?; + self.new + .verify_inactive(Service::Agent, self.reservation.context.budget)?; + self.new + .verify_inactive(Service::General, self.reservation.context.budget)?; + ensure!( + observation + == hash(&serde_json::to_vec(&( + "reserved_general_stop_v1", + &self.general, + &self.failure + ))?), + "UPDATE_CONDITIONAL_STOP_OBSERVATION_CHANGED" + ); + self.finished = true; + Ok(()) + } } impl DurableEffectFence for StartFence<'_, '_> { fn before_effect(&mut self, effect: &Effect) -> Result<()> { diff --git a/crates/updater-worker/src/trust.rs b/crates/updater-worker/src/trust.rs index cc15a57..75c90cf 100644 --- a/crates/updater-worker/src/trust.rs +++ b/crates/updater-worker/src/trust.rs @@ -73,19 +73,98 @@ impl TrustState { /// 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 { - let update = original.update(); + Self::load_inputs( + Some(original.update()), + binding, + original.delegation().prepared_at, + ) + } + /// Explicit PRE-consumption verification. No Original/start/consumption is + /// fabricated to reuse a retained-evidence API. Caller still needs fresh + /// independent HelperSync admission and atomic durable handoff afterwards. + pub fn load_prepared( + update: &rmm_domain::agent_update::ResolvedAgentUpdate, + binding: &Binding, + clock: &NativeClock, + ) -> Result { + ensure!( + update.validate() && update.execution_identity() == Some("root"), + "UPDATE_PREPARED_ACTION" + ); + let state = Self::load_inputs(Some(update), binding, clock.now()?)?; + state.recheck_prepared(update, clock)?; + Ok(state) + } + pub fn load_observed(binding: &Binding, clock: &NativeClock) -> Result { + let state = Self::load_inputs(None, binding, clock.now()?)?; + state.recheck_observed()?; + Ok(state) + } + pub fn recheck_observed(&self) -> Result<()> { + self.recheck_protected()?; + ensure!( + !self.baseline_signer_revoked, + "UPDATE_BASELINE_SIGNER_REVOKED" + ); + verify_installed_release( + &self.keyring, + &self.installed, + self.updater_binding.keyring_generation, + ) + } + pub fn updater_binding(&self) -> &rmm_domain::agent_update::UpdaterBinding { + &self.updater_binding + } + pub fn predecessor(&self) -> &rmm_domain::agent_update::Predecessor { + &self.predecessor + } + pub fn activation_floor(&self) -> u64 { + self.installed.activation_floor + } + pub fn verify_candidate( + &self, + envelope: &rmm_domain::agent_update::ReleaseEnvelope, + clock: &NativeClock, + ) -> Result<()> { + self.recheck_observed()?; + let active = self + .installed + .release_envelope + .decode() + .map_err(anyhow::Error::msg)?; + verify( + &self.keyring, + envelope, + &TrustedContext { + platform: ReleasePlatform::LinuxX64, + now_utc_seconds: u64::try_from(clock.now()?.timestamp())?, + keyring_generation: self.updater_binding.keyring_generation, + protocol_major: active.manifest.protocol_major, + state_schema: active.manifest.state_schema, + updater_version: 1, + minimum_release_sequence: self.installed.activation_floor, + active_release: Some(ActiveRelease { + sequence: active.manifest.release_sequence, + manifest_sha256: self.installed.manifest_sha256.clone(), + }), + }, + ) + } + fn load_inputs( + update: Option<&rmm_domain::agent_update::ResolvedAgentUpdate>, + binding: &Binding, + prepared_at: chrono::DateTime, + ) -> Result { let mut protected = Vec::new(); let (file, raw) = Protected::read(Path::new("/etc/rmm-agent-updater/policy.json"), 65536)?; let (policy, policy_hash) = LocalUpdatePolicy::from_bytes(&raw).map_err(anyhow::Error::msg)?; ensure!( - policy_hash == update.updater.policy_sha256 - && policy.tenant_id == binding.tenant_id + policy.tenant_id == binding.tenant_id && policy.asset_id == binding.asset_id && policy.agent_id == binding.agent_id && policy.enrollment_generation == binding.enrollment_generation - && policy.assignment_version == binding.assignment_version - && policy.updater_binary_sha256 == update.updater.binary_sha256, + && policy.assignment_version == binding.assignment_version, "UPDATE_PROTECTED_POLICY_CHANGED" ); protected.push(file); @@ -97,12 +176,8 @@ impl TrustState { protected.push(file); let (file, raw) = Protected::read(Path::new(&policy.keyring_path), 16384)?; let keys: UpdateKeyringFile = serde_json::from_slice(&raw)?; - ensure!( - keys.validate() - && keys.generation == update.updater.keyring_generation - && hash(&raw) == update.updater.keyring_sha256, - "UPDATE_PROTECTED_KEYRING_CHANGED" - ); + ensure!(keys.validate(), "UPDATE_PROTECTED_KEYRING_CHANGED"); + let keyring_sha256 = hash(&raw); protected.push(file); let job_key = hex::decode(&binding.public_key_hex)?; let mut trusted = Vec::new(); @@ -171,8 +246,6 @@ impl TrustState { let health_public_key = health.public_key().map_err(anyhow::Error::msg)?; ensure!( !VerifyingKey::from_bytes(&health_public_key)?.is_weak() - && hash(&raw) == update.updater.health_key_pin_sha256 - && hex::encode(health_public_key) == update.updater.health_public_key_hex && health_public_key.as_slice() != job_key && keys .keys @@ -180,17 +253,14 @@ impl TrustState { .all(|key| key.public_key_hex != hex::encode(health_public_key)), "UPDATE_HEALTH_KEY_NOT_SEPARATE_OR_CHANGED" ); + let health_key_pin_sha256 = hash(&raw); protected.push(file); let (file, raw) = Protected::read(Path::new(&policy.installed_record_path), 32768)?; - ensure!( - hash(&raw) == update.predecessor.installed_record_sha256, - "UPDATE_INSTALLED_RECORD_CHANGED" - ); let installed = InstalledReleaseRecord::from_bytes(&raw).map_err(anyhow::Error::msg)?; ensure!( !installed.rolled_back && installed.activation_floor >= policy.initial_activation_floor - && installed.activated_at <= original.handoff().consumed_at, + && installed.activated_at <= u64::try_from(prepared_at.timestamp())?, "UPDATE_INSTALLED_FLOOR_OR_TIME" ); if matches!( @@ -208,37 +278,53 @@ impl TrustState { .release_envelope .decode() .map_err(anyhow::Error::msg)?; - ensure!( - active.manifest.release_id == update.predecessor.release_id - && active.manifest.release_sequence == update.predecessor.release_sequence - && installed.manifest_sha256 == update.predecessor.manifest_sha256 - && active.manifest.artifacts[0].sha256 == update.predecessor.agent_sha256 - && active.manifest.artifacts[1].sha256 == update.predecessor.helper_sha256, - "UPDATE_INSTALLED_PREDECESSOR_BINDING" - ); + let predecessor = rmm_domain::agent_update::Predecessor { + release_id: active.manifest.release_id, + release_sequence: active.manifest.release_sequence, + manifest_sha256: installed.manifest_sha256.clone(), + installed_record_sha256: hash(&raw), + agent_sha256: active.manifest.artifacts[0].sha256.clone(), + helper_sha256: active.manifest.artifacts[1].sha256.clone(), + }; + let updater_binding = rmm_domain::agent_update::UpdaterBinding { + binary_sha256: policy.updater_binary_sha256.clone(), + policy_sha256: policy_hash, + keyring_generation: keys.generation, + keyring_sha256, + health_public_key_hex: hex::encode(health_public_key), + health_key_pin_sha256, + }; + if let Some(update) = update { + ensure!( + update.predecessor == predecessor && update.updater == updater_binding, + "UPDATE_PROTECTED_INPUT_BINDING" + ); + } verify_installed_release(&historical, &installed, keys.generation)?; - verify( - &historical, - &update.release_envelope, - &TrustedContext { - platform: ReleasePlatform::LinuxX64, - 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, - updater_version: 1, - minimum_release_sequence: installed.activation_floor, - active_release: Some(ActiveRelease { - sequence: active.manifest.release_sequence, - manifest_sha256: installed.manifest_sha256.clone(), - }), - }, - )?; + if let Some(update) = update { + verify( + &historical, + &update.release_envelope, + &TrustedContext { + platform: ReleasePlatform::LinuxX64, + now_utc_seconds: u64::try_from(prepared_at.timestamp())?, + keyring_generation: keys.generation, + protocol_major: active.manifest.protocol_major, + state_schema: active.manifest.state_schema, + updater_version: 1, + minimum_release_sequence: installed.activation_floor, + active_release: Some(ActiveRelease { + sequence: active.manifest.release_sequence, + manifest_sha256: installed.manifest_sha256.clone(), + }), + }, + )?; + } protected.push(file); let baseline_signer_revoked = baseline_entry.revoked; Ok(Self { - updater_binding: update.updater.clone(), - predecessor: update.predecessor.clone(), + updater_binding, + predecessor, protected, policy, installed, @@ -255,6 +341,25 @@ impl TrustState { self.recheck_crypto(original, clock) } fn recheck_crypto(&self, original: &Original, clock: &NativeClock) -> Result<()> { + self.recheck_crypto_inputs(original.update(), clock) + } + pub fn recheck_prepared( + &self, + update: &rmm_domain::agent_update::ResolvedAgentUpdate, + clock: &NativeClock, + ) -> Result<()> { + ensure!( + update.updater == self.updater_binding && update.predecessor == self.predecessor, + "UPDATE_PREPARED_CONTEXT_CHANGED" + ); + self.recheck_protected()?; + self.recheck_crypto_inputs(update, clock) + } + fn recheck_crypto_inputs( + &self, + update: &rmm_domain::agent_update::ResolvedAgentUpdate, + clock: &NativeClock, + ) -> Result<()> { ensure!( !self.baseline_signer_revoked, "UPDATE_BASELINE_SIGNER_REVOKED" @@ -262,7 +367,7 @@ impl TrustState { verify_installed_release( &self.keyring, &self.installed, - original.update().updater.keyring_generation, + update.updater.keyring_generation, )?; let active = self .installed @@ -271,11 +376,11 @@ impl TrustState { .map_err(anyhow::Error::msg)?; verify( &self.keyring, - &original.update().release_envelope, + &update.release_envelope, &TrustedContext { platform: ReleasePlatform::LinuxX64, now_utc_seconds: u64::try_from(clock.now()?.timestamp())?, - keyring_generation: original.update().updater.keyring_generation, + keyring_generation: update.updater.keyring_generation, protocol_major: active.manifest.protocol_major, state_schema: active.manifest.state_schema, updater_version: 1, diff --git a/deploy/linux/rmm-agent-updater.service b/deploy/linux/rmm-agent-updater.service new file mode 100644 index 0000000..97c7ef6 --- /dev/null +++ b/deploy/linux/rmm-agent-updater.service @@ -0,0 +1,27 @@ +[Unit] +Description=RMM stable updater for individually approved original code-pair jobs +After=time-sync.target + +[Service] +Type=simple +User=root +Group=root +UMask=0077 +RuntimeDirectory=rmm-agent-updater +RuntimeDirectoryMode=0700 +ExecStart=/usr/local/libexec/rmm-agent-updater --serve +# An ambiguous handoff must be inspected, never automatically resumed by a +# replacement updater process. A successful pass reloads its new record in the +# same still-authenticated process for the next independently approved job. +Restart=no +NoNewPrivileges=yes +PrivateTmp=yes +PrivateNetwork=yes +ProtectHome=yes +ProtectSystem=strict +ReadWritePaths=/usr/local/libexec /etc/rmm-remote /var/lib/rmm-agent-updater /var/lib/rmm-helper /var/lib/rmm-agent-remote +RestrictAddressFamilies=AF_UNIX +KillMode=control-group + +[Install] +WantedBy=multi-user.target diff --git a/deploy/linux_updater_bootstrap.py b/deploy/linux_updater_bootstrap.py new file mode 100644 index 0000000..3d16d6b --- /dev/null +++ b/deploy/linux_updater_bootstrap.py @@ -0,0 +1,225 @@ +#!/usr/bin/env python3 +"""Read-only prerequisite snapshot for preserving Linux updater bootstrap. + +No apply/start/stop/reenroll/package/policy operation is implemented here. +Credentials use the existing strict parser and independently pinned SSH host. +Private keys are stat'ed only; no private key or environment bytes are read. +""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import uuid +import remote_client_bootstrap as transport +ROOT=Path(__file__).resolve().parents[1] + +def inspect_vm(payload): + import contextlib + import hashlib + import json + import os + from pathlib import Path + import sqlite3 + import stat + import subprocess + import time + import uuid + require(payload=={'operation':'linux_updater_bootstrap_inspect_v1'} and os.geteuid()==0 + and os.uname().machine=='x86_64','fixed read-only Linux target') + def digest(raw):return hashlib.sha256(raw).hexdigest() + def encode(value):return json.dumps(value,separators=(',',':'),ensure_ascii=False).encode() + def identity(s):return dict(device=s.st_dev,inode=s.st_ino,uid=s.st_uid,gid=s.st_gid,mode=s.st_mode,links=s.st_nlink) + def metadata(path): + path=Path(path) + try:s=path.lstat() + except FileNotFoundError:return None + require(not stat.S_ISLNK(s.st_mode),'no metadata symlink') + return dict(identity=identity(s),bytes=s.st_size) + def public(path,limit=65536): + path=Path(path);s=path.lstat() + require(stat.S_ISREG(s.st_mode) and s.st_uid==0 and s.st_nlink==1 + and not s.st_mode&0o022 and s.st_size<=limit,'bounded protected public input') + with path.open('rb') as stream:raw=stream.read(limit+1) + after=path.lstat() + require(len(raw)==s.st_size and identity(after)==identity(s) and after.st_size==s.st_size + and after.st_mtime_ns==s.st_mtime_ns and after.st_ctime_ns==s.st_ctime_ns,'stable public input') + return raw + def proof(path): + raw=public(path,256*1024*1024);s=Path(path).lstat() + return dict(identity=identity(s),bytes=len(raw),sha256=digest(raw)) + def run(args): + result=subprocess.run(args,stdin=subprocess.DEVNULL,stdout=subprocess.PIPE,stderr=subprocess.PIPE, + timeout=10,env={'PATH':'/usr/bin:/bin','LC_ALL':'C'}) + require(result.returncode in (0,1) and len(result.stdout)<=65536,'bounded read-only systemd response') + return result.stdout.decode() + services=('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-full-maintenance-helper.service', + 'rmm-agent-updater.service','rmm-apt-helper.service','rmm-agent.service','rmm-helper-dev-test.service') + names=list(services) + for name in services: + names.extend((name[:-8]+'.socket',name[:-8]+'.timer')) + timers=run(['systemctl','list-units','--all','--no-legend','--plain','--no-pager','rmm-general-startup-rollback-*.timer']) + rollback_names=[] + for line in timers.splitlines(): + name=line.split()[0] + require(name.startswith('rmm-general-startup-rollback-') and name.endswith('.timer'),'fixed rollback timer') + uuid.UUID(name[len('rmm-general-startup-rollback-'):-len('.timer')]) + rollback_names.append(name) + names.extend(sorted(rollback_names)) + properties=['Id','LoadState','ActiveState','SubState','MainPID','ControlPID','InvocationID','UnitFileState', + 'FragmentPath','DropInPaths','Triggers','TriggeredBy','Job','ControlGroup'] + units={} + for name in names: + output=run(['systemctl','show','--all','--property='+','.join(properties),'--',name]) + fields={} + for line in output.splitlines(): + key,value=line.split('=',1);require(key in properties and key not in fields,'exact service properties') + fields[key]=value + require(fields.get('Id')==name,'exact queried unit') + composition=[] + if fields.get('LoadState')=='loaded': + paths=[fields['FragmentPath']]+fields.get('DropInPaths','').split() + for path in paths:composition.append((path,proof(path))) + units[name]=dict(status=fields,composition=composition,unit_sha256=digest(encode(composition))) + binaries={} + for path in ('/usr/local/libexec/rmm-agent-remote','/usr/local/libexec/rmm-privileged-helper','/usr/local/libexec/rmm-agent-updater'): + binaries[path]=proof(path) if Path(path).exists() else None + configs={} + for name in ('helper-general.json','helper-full-maintenance.json'): + path=Path('/etc/rmm-remote')/name + if not path.exists():configs[name]=None;continue + raw=public(path);value=json.loads(raw) + configs[name]=dict(sha256=digest(raw),fields={key:value.get(key) for key in ( + 'profile','agent_uid','socket_gid','socket','state','signing_key_file','allow_reboot','allow_root_scripts', + 'allowed_script_profiles','helper_binary_sha256')},binding={key:value['binding'][key] for key in ( + 'tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')}) + public_pins={} + for name in ('general-helper-proof.json','full-maintenance-helper-proof.json','helper-signing-key.json'): + path=Path('/etc/rmm-remote')/name + public_pins[name]=proof(path) if path.exists() else None + state_paths=[ + '/var/lib/rmm-agent-remote','/var/lib/rmm-agent-remote/journal.sqlite', + '/var/lib/rmm-agent-remote/consumption.sqlite','/var/lib/rmm-agent-remote/agent.lock', + '/var/lib/rmm-agent-remote/binding.json','/var/lib/rmm-agent-remote/identity.json', + '/var/lib/rmm-agent-remote/job-signing-key.json','/var/lib/rmm-agent-remote/device.key', + '/var/lib/rmm-agent-remote/device.pem','/var/lib/rmm-agent-remote/certificates', + '/var/lib/rmm-helper','/var/lib/rmm-helper/consumption.sqlite','/var/lib/rmm-helper/executor.lock', + '/etc/rmm-agent-updater','/var/lib/rmm-agent-updater','/etc/rmm-remote/agent.env'] + selector=Path('/var/lib/rmm-agent-remote/certificates/current.json') + if selector.exists(): + # Public selector only; active private key is metadata-only below. + s=selector.lstat();require(stat.S_ISREG(s.st_mode) and s.st_size<=4096,'public TLS selector') + current=json.loads(selector.read_bytes());version=str(uuid.UUID(current['version'])) + state_paths.extend((str(selector),str(selector.parent/'versions'/version), + str(selector.parent/'versions'/version/'device.key'),str(selector.parent/'versions'/version/'device.pem'))) + state={path:metadata(path) for path in state_paths} + databases={} + for path in ('/var/lib/rmm-agent-remote/journal.sqlite','/var/lib/rmm-agent-remote/consumption.sqlite','/var/lib/rmm-helper/consumption.sqlite'): + require(not Path(path+'-wal').exists() and not Path(path+'-shm').exists(),'existing DELETE journals required') + with contextlib.closing(sqlite3.connect('file:'+path+'?mode=ro',uri=True,timeout=2)) as db: + db.execute('PRAGMA query_only=ON') + require(db.execute('PRAGMA journal_mode').fetchone()[0]=='delete','read-only DELETE journal') + tables={row[0] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")} + counts={name:db.execute('SELECT count(*) FROM '+name).fetchone()[0] for name in ( + 'consumed','execution_evidence','helper_sync_decisions','job_attempts','reconcile_decisions') if name in tables} + unresolved={} + if 'consumed' in tables:unresolved['consumed']=db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0] + if 'job_attempts' in tables:unresolved['job_attempts']=db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0] + faults={name:db.execute('SELECT count(*) FROM '+name+' WHERE fault IS NOT NULL').fetchone()[0] + for name in ('helper_sync_meta','reconcile_meta') if name in tables} + # Hash original evidence bytes inside the target; no stdout/script + # body or reconciliation request/signature is exported. + evidence=[] + if 'execution_evidence' in tables: + columns={r[1] for r in db.execute('PRAGMA table_info(execution_evidence)')} + for grant,start,outcome in db.execute('SELECT grant_id,start,outcome FROM execution_evidence'): + evidence.append(dict(grant_id=grant,start_sha256=digest(start),outcome_sha256=None if outcome is None else digest(outcome))) + databases[path]=dict(counts=counts,unresolved=unresolved,faults=faults, + evidence_sha256=digest(encode(sorted(evidence,key=lambda r:r['grant_id']))), + integrity=db.execute('PRAGMA integrity_check').fetchone()[0]) + require(not Path(path+'-wal').exists() and not Path(path+'-shm').exists(),'no SQLite sidecars introduced') + host={} + for label,path in (('proc','/proc'),('user_namespace','/proc/1/ns/user'),('pid_namespace','/proc/1/ns/pid'),('cgroup_namespace','/proc/1/ns/cgroup')): + s=Path(path).stat();host[label+'_device']=s.st_dev;host[label+'_inode']=s.st_ino + return dict(schema_version=1,operation=payload['operation'],read_only=True,utc_seconds=int(time.time()), + boot_id=Path('/proc/sys/kernel/random/boot_id').read_text().strip(),units=units,binaries=binaries, + configs=configs,public_pins=public_pins,state=state,databases=databases,host_context=host, + rollback_timers=rollback_names,updater_provisioned=Path('/etc/rmm-agent-updater/policy.json').exists()) + +def prepare(snapshot_path,agent,helper,updater,qualification): + import stat + def require(ok,code): + if not ok:raise RuntimeError(code) + def input_file(path,limit): + path=Path(path);meta=path.lstat() + require(stat.S_ISREG(meta.st_mode) and not path.is_symlink() and meta.st_nlink==1 + and meta.st_uid==os.getuid() and not meta.st_mode&0o022 and 0= Duration::from_secs(60) { + self.queue_update_observation(generation)?; + update_at = Instant::now(); + } +''' + needle=' if !busy && capability_at.elapsed()>=Duration::from_secs(60) {capability_at=Instant::now();if self.capabilities()!=advertised{return Ok(());}}' + assert old_main.count(block)==1 and old_main.count(needle)==1 + moved='\n'.join(' '+line if line else line for line in block.splitlines())+'\n' + assert old_main.replace(block,'').replace(needle,moved+needle).encode()==main,'exact existing observer poll move before capability check required' + q.write(case/'entered.json',q.encode({'base_manifest_sha256':BASE_MANIFEST,'module_sha256':q.sha(fixed),'main_sha256':q.sha(main),'no_target_mutations':True,'original_case_unchanged':True})) + cache=case/'cache';cache.mkdir(mode=0o700) + # Copy, never hardlink: compiler output cannot change the original cache. + shutil.copytree(BASE/'cache/release',cache/'release') + artifacts={};input_hashes={} + for version in ('source0','source1'): + expected=json.loads((BASE/(version+'-inputs.json')).read_text());names=sorted(expected) + assert q.capture(BASE/version,names)==expected + target=case/version;shutil.copytree(BASE/version,target) + module=target/MODULE;module.chmod(0o600);assert module.read_bytes()==old_module;module.write_bytes(fixed);module.chmod(0o444) + entry=target/MAIN;entry.chmod(0o600);assert entry.read_bytes()==old_main.encode();entry.write_bytes(main);entry.chmod(0o444) + actual=q.capture(target,names);assert {n for n in names if actual[n]!=expected[n]}=={MODULE,MAIN} + q.readonly(target);q.write(case/(version+'-inputs.json'),q.encode(actual));input_hashes[version]=q.sha((case/(version+'-inputs.json')).read_bytes()) + q.run(case,target,cache,version+'-clippy',['cargo','clippy','--locked','--offline','--release','--jobs','2','-p','rmm-agent','--all-targets','--','-D','warnings']) + q.run(case,target,cache,version+'-agent-release',['cargo','build','--locked','--offline','--release','--jobs','2','-p','rmm-agent','--bin','rmm-agent']) + output=case/'artifacts'/version;output.mkdir(mode=0o700,parents=True) + agent=q.copy_artifact(cache/'release/rmm-agent',output/'rmm-agent');assert agent['sha256']!=old['artifacts'][version]['agent']['sha256'] + artifacts[version]={**old['artifacts'][version],'agent':agent} + assert q.capture(target,names)==actual + assert artifacts['source0']['agent']['sha256']!=artifacts['source1']['agent']['sha256'] + for label,item in [('helper0',old['artifacts']['source0']['helper']),('helper1',old['artifacts']['source1']['helper']),('updater',old['stable_updater'])]: + assert q.sha(Path(item['path']).read_bytes())==item['sha256'],label + for unit in old['units'].values():assert q.sha(Path(unit['path']).read_bytes())==unit['sha256'] + manifest={'schema_version':1,'profile':'linux_compatible_updater_capability_derivative_v2','base_manifest_sha256':BASE_MANIFEST,'base_manifest':str(BASE/'manifest.json'),'changed_modules':{MODULE:q.sha(fixed),MAIN:q.sha(main)},'poll_harvest_before_capability_check':True,'sampler_frequency_unchanged':True,'source0_version':'0.1.0','source1_version':'0.1.1','source_input_hashes':input_hashes,'builder':q.BUILDER,'artifacts':artifacts,'stable_updater':old['stable_updater'],'units':old['units'],'old_sources_artifacts_units_unchanged':True,'only_agents_rebuilt':True,'source0_and_source1_clippy_passed':True,'native_vm_tested':False,'actual_qualified_capability_still_requires_native_baseline':True,'bootstrap_installed':False,'apply_executed':False} + q.write(case/'manifest.json',q.encode(manifest));q.write(case/'completed.json',q.encode({'manifest_sha256':q.sha((case/'manifest.json').read_bytes())})) + print(json.dumps({'case':str(case),'manifest_sha256':q.sha((case/'manifest.json').read_bytes()),'artifacts':artifacts})) +if __name__=='__main__':main() diff --git a/deploy/linux_updater_compatible_apply.py b/deploy/linux_updater_compatible_apply.py new file mode 100644 index 0000000..7c80f3a --- /dev/null +++ b/deploy/linux_updater_compatible_apply.py @@ -0,0 +1,375 @@ +#!/usr/bin/env python3 +"""Guarded compatible Linux bootstrap, explicitly coordinated separate phases. + +No job approval, package operation, reenrollment or private-key transfer. This +controller is prepared source; invoking mutations requires Root coordination. +""" +import argparse +import base64 +import hashlib +import inspect +import io +import json +import os +from pathlib import Path +import shlex +import subprocess +import tarfile +import uuid +import linux_updater_bootstrap as prerequisite +import remote_client_bootstrap as transport +ROOT=Path(__file__).resolve().parents[1] +BASE=ROOT/'devstate/linux-updater-capability-build/9923a97f-4c8c-4e0a-bbad-4f1d88f0d43a/manifest.json' +BASE_SHA='a41ad700b4b7527bf911bf00f2745914188760c631af0c3255364c05f1ef4fbc' +BOOT='45aba743-be1d-4ae5-ba00-139fb9363bfe' + +def sha(raw):return hashlib.sha256(raw).hexdigest() +def once(path,raw): + path.parent.mkdir(mode=0o700,parents=True,exist_ok=True) + with path.open('xb') as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno()) + path.chmod(0o600) +def native_rpc(function,payload,config='linux-vm.conf',timeout=80): + # Same strict credential parser and already-confirmed host as existing tooling. + cfg=transport.access.load_config(ROOT/'credentials'/config) + code='import json,os,subprocess\nfrom pathlib import Path\n'+inspect.getsource(transport.require)+'\n'+inspect.getsource(function)+'\ntry:\n print(json.dumps('+function.__name__+'('+repr(payload)+')))\nexcept Exception as error:\n import traceback\n print(json.dumps({"error_class":type(error).__name__,"frames":[{"function":f.name,"line":f.lineno}for f in traceback.extract_tb(error.__traceback__)]}))\n' + ssh=['ssh','-F','/dev/null','-T','-p',cfg.get('port','22'),'-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ClearAllForwardings=yes','-o','ConnectTimeout=7','-i',str(Path(cfg['ssh_key_path']).expanduser()),cfg['username']+'@'+cfg['address'],'python3 -I -'] + result=subprocess.run(ssh,input=code.encode(),capture_output=True,timeout=timeout) + if result.returncode:raise RuntimeError('Bootstrap transport failed; no private diagnostics printed') + value=json.loads(result.stdout) + if 'error_class'in value:raise RuntimeError('Bootstrap incomplete; retained native intent needs inspection: '+json.dumps(value)) + return value + +def public_inputs(payload): + import stat,json,hashlib,os + from pathlib import Path + require(payload=={'operation':'linux_compatible_public_inputs_v1'}and os.geteuid()==0,'fixed public inputs') + result={} + for name in ('helper-general.json','general-helper-proof.json','helper-full-maintenance.json','full-maintenance-helper-proof.json','helper-signing-key.json'): + path=Path('/etc/rmm-remote')/name;meta=path.lstat();require(stat.S_ISREG(meta.st_mode)and meta.st_uid==0 and meta.st_nlink==1 and not meta.st_mode&0o022 and meta.st_size<=16384,'protected public input') + raw=path.read_bytes();require(path.lstat().st_ino==meta.st_ino and path.lstat().st_mtime_ns==meta.st_mtime_ns,'public input changed');result[name]={'bytes':list(raw),'sha256':hashlib.sha256(raw).hexdigest()} + general=json.loads(bytes(result['helper-general.json']['bytes']));pin=json.loads(bytes(result['general-helper-proof.json']['bytes'])) + require(general['profile']=='general'and general.get('helper_binary_sha256')is None and pin['schema_version']==1 and pin['profile']=='general'and pin.get('helper_binary_sha256')is None,'exact non-binary General identity/policy profile') + return result + +def central_closed(payload): + import json,subprocess,datetime,uuid,hashlib + from pathlib import Path + agent=str(uuid.UUID(payload['agent_id']));tenant=str(uuid.UUID(payload['tenant_id'])) + def run(a):return subprocess.check_output(a,text=True,stderr=subprocess.DEVNULL,timeout=15) + flags={} + for role in ('api','gateway'): + container=json.loads(run(['docker','inspect','rust-discovery-server-'+role+'-1']))[0];env=dict(row.split('=',1)for row in container['Config']['Env']);flag=env.get('RMM_DEV_ROOT_ENABLED','false') + require(flag in ('true','false'),'known Root gate') + expired=False + if flag=='true': + path=env.get('RMM_DEV_ROOT_POLICY_FILE');require(path=='/root-policy/policy.json','fixed public Root policy') + mount=next(m for m in container['Mounts']if m['Destination']=='/root-policy');require(mount['RW']is False,'readonly protected policy') + doc=json.loads((Path(mount['Source'])/'policy.json').read_text());expiry=datetime.datetime.fromisoformat(doc['expires_at'].replace('Z','+00:00'));require(expiry.tzinfo is not None,'absolute policy expiry');expired=expiry<=datetime.datetime.now(datetime.timezone.utc) + require(flag=='false'or expired,'central Root scope must be closed before compatible bootstrap') + require('RMM_AGENT_UPDATE_POLICY_FILE'not in env,'initial updater scope must remain absent') + flags[role]={'root_enabled':flag=='true','root_policy_expired':expired,'container_id':container['Id']} + query="BEGIN READ ONLY; SELECT count(*) FROM jobs WHERE tenant_id='"+tenant+"' AND agent_id='"+agent+"' AND state NOT IN ('succeeded','failed','cancelled','expired'); COMMIT;" + rows=run(['docker','exec','rust-discovery-server-postgres-1','psql','-XqAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm','-c',query]).splitlines();require(rows==['0'],'no open or unknown Linux job') + return {'operation':'linux_compatible_closed_scope_v1','tenant_id':tenant,'agent_id':agent,'observed_at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'roles':flags,'open_jobs':0,'job_authority':False} + +def passive_check(payload): + import datetime,json,subprocess,uuid + tenant=str(uuid.UUID(payload['tenant_id']));agent=str(uuid.UUID(payload['agent_id'])) + query="BEGIN READ ONLY; SELECT json_build_object('connection_generation',i.connection_generation,'capabilities',i.capabilities,'last_contact_at',i.last_contact_at,'observed_at',o.observed_at,'received_at',o.received_at,'data',o.data,'payload_sha256',o.payload_sha256) FROM agent_installations i JOIN LATERAL (SELECT * FROM observations WHERE tenant_id=i.tenant_id AND agent_id=i.agent_id AND source_id='agent.update' ORDER BY sequence DESC LIMIT 1)o ON true WHERE i.tenant_id='"+tenant+"' AND i.agent_id='"+agent+"' AND i.revoked_at IS NULL; COMMIT;" + output=subprocess.check_output(['docker','exec','rust-discovery-server-postgres-1','psql','-XqAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm','-c',query],stderr=subprocess.DEVNULL,text=True,timeout=15).splitlines();require(len(output)==1,'one actual current installation/passive observation') + observed=json.loads(output[0]);sample=observed['data']['agent_update'];now=datetime.datetime.now(datetime.timezone.utc) + for text in (observed['last_contact_at'],observed['received_at'],sample['observed_at']): + age=(now-datetime.datetime.fromisoformat(text.replace('Z','+00:00'))).total_seconds();require(0<=age<90,'fresh actual persisted passive evidence') + require(sample['quality']=='valid'and sample['platform']=='x86_64-unknown-linux-gnu'and sample['activation_floor']==1 and not sample['rolled_back']and sample['helper_observation_sha256']and sample['signed_sync_decision_id'],'actual Full verified passive baseline') + require(sample['predecessor']['installed_record_sha256']==payload['record_sha256']and sample['predecessor']['agent_sha256']==payload['agent_sha256']and sample['predecessor']['helper_sha256']==payload['helper_sha256'],'actual current predecessor') + require(sample['updater']['binary_sha256']==payload['updater_sha256']and sample['updater']['policy_sha256']==payload['policy_sha256']and sample['updater']['keyring_sha256']==payload['keyring_sha256']and sample['updater']['health_key_pin_sha256']==payload['health_pin_sha256'],'actual protected updater trust bindings') + required=['agent.update.apply/1','agent.update.check/1','update_agent','root:update_agent','helper.root.v1','rmm.sync.v1','helper.binary.sha256:'+payload['helper_sha256'],'helper.policy.sha256:'+payload['helper_policy_sha256'],'updater.binary.sha256:'+payload['updater_sha256'],'updater.policy.sha256:'+payload['policy_sha256']] + require(all(cap in observed['capabilities']for cap in required),'actual current qualified runtime capabilities, no SQL override') + return {'operation':'linux_compatible_passive_qualification_v1','tenant_id':tenant,'agent_id':agent,'sample':sample,'connection_generation':observed['connection_generation'],'capabilities':observed['capabilities'],'observation_payload_sha256':observed['payload_sha256'],'actual_passive_path':True,'database_written':False,'job_authority':False} + +def stage(payload): + import base64,hashlib,json,os,stat,uuid + from pathlib import Path + require(os.geteuid()==0 and os.uname().machine=='x86_64','root x86 Linux only') + ident=str(uuid.UUID(payload['case_id']));base=Path('/var/lib/rmm-updater-bootstrap') + if not base.exists():base.mkdir(mode=0o700) + meta=base.lstat();require(stat.S_ISDIR(meta.st_mode)and meta.st_uid==0 and stat.S_IMODE(meta.st_mode)==0o700,'protected bootstrap root') + folder=base/ident;folder.mkdir(mode=0o700) + files=payload['files'];require(set(files)=={'agent.bin','helper.bin','updater.bin','sdk.bin','full-config.json','full-pin.json','updater.service'},'closed bootstrap artifact set') + for name,entry in files.items(): + raw=base64.b64decode(entry['base64'],validate=True);require(len(raw)==entry['bytes']and hashlib.sha256(raw).hexdigest()==entry['sha256'],'exact public artifact') + if name.endswith('.bin'):require(raw[:6]==b'\x7fELF\x02\x01'and raw[18:20]==b'\x3e\x00','Linux x86-64 ELF') + fd=os.open(folder/name,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o500 if name.endswith('.bin') else 0o600) + with os.fdopen(fd,'wb')as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno()) + manifest={k:v for k,v in payload.items()if k!='files'};manifest['files']={n:{k:v for k,v in e.items()if k!='base64'}for n,e in files.items()} + with(folder/'manifest.json').open('x')as stream:json.dump(manifest,stream,sort_keys=True);stream.flush();os.fsync(stream.fileno()) + return {'case_id':ident,'folder':str(folder),'staged':True,'services_changed':False,'job_authority':False} + +def pair(payload): + import base64,contextlib,fcntl,hashlib,json,os,select,sqlite3,stat,subprocess,time,uuid,datetime + from pathlib import Path + require(os.geteuid()==0 and os.uname().machine=='x86_64','root x86 Linux only') + ident=str(uuid.UUID(payload['case_id']));folder=Path('/var/lib/rmm-updater-bootstrap')/ident;manifest=json.loads((folder/'manifest.json').read_text()) + require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==manifest['boot_id'],'original boot') + def sha(raw):return hashlib.sha256(raw).hexdigest() + def run(a,timeout=20):return subprocess.check_output(a,text=True,stderr=subprocess.DEVNULL,timeout=timeout).strip() + def syncdir(path): + fd=os.open(path,os.O_RDONLY|os.O_DIRECTORY) + try:os.fsync(fd) + finally:os.close(fd) + def record(name,value): + with(folder/name).open('x')as stream:json.dump(value,stream,sort_keys=True);stream.flush();os.fsync(stream.fileno()) + syncdir(folder) + def public(path,maximum): + path=Path(path);meta=path.lstat();require(stat.S_ISREG(meta.st_mode)and meta.st_uid==0 and meta.st_nlink==1 and not meta.st_mode&0o022 and meta.st_size<=maximum,'protected original public file');return path.read_bytes(),meta + def identity(path): + m=Path(path).lstat();require(not stat.S_ISLNK(m.st_mode),'no substituted path');return [m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_nlink] + def status(unit): + fields={k:v for k,v in(line.split('=',1)for line in run(['systemctl','show','--all','--property=Id,ActiveState,SubState,MainPID,ControlPID,InvocationID,Job,ControlGroup,UnitFileState','--',unit]).splitlines())};require(fields['Id']==unit,'exact service');return fields + units=['rmm-agent-remote.service','rmm-helper-cloud.service','rmm-full-maintenance-helper.service'] + def databases(): + result={} + for path in ('/var/lib/rmm-agent-remote/journal.sqlite','/var/lib/rmm-agent-remote/consumption.sqlite','/var/lib/rmm-helper/consumption.sqlite'): + require(not Path(path+'-wal').exists()and not Path(path+'-shm').exists(),'existing DELETE journals') + with contextlib.closing(sqlite3.connect('file:'+path+'?mode=ro',uri=True,timeout=.5))as db: + db.execute('PRAGMA query_only=ON');require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','original journal integrity') + tables={r[0]for r in db.execute("SELECT name FROM sqlite_master WHERE type='table'")};evidence=[] + if 'consumed'in tables: + require(db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0,'no unresolved original effect') + for row in db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY c.grant_id'): + evidence.append([v if isinstance(v,(str,int))or v is None else sha(v)for v in row]) + if 'job_attempts'in tables:require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'no open original agent job') + for name in ('helper_sync_meta','reconcile_meta'): + if name in tables:require(db.execute('SELECT count(*) FROM '+name+' WHERE fault IS NOT NULL').fetchone()[0]==0,'no original journal fault') + generation=db.execute('SELECT generation FROM reconcile_meta WHERE id=1').fetchone()[0]if 'reconcile_meta'in tables else None + result[path]={'file_id':identity(path),'evidence_sha256':sha(json.dumps(evidence,separators=(',',':')).encode()),'consumptions':len(evidence),'generation':generation} + return result + def retained_metadata(): + result={} + fixed=['/var/lib/rmm-agent-remote/binding.json','/var/lib/rmm-agent-remote/identity.json','/var/lib/rmm-agent-remote/job-signing-key.json','/var/lib/rmm-agent-remote/device.key','/var/lib/rmm-agent-remote/device.pem','/etc/rmm-remote/agent.env','/etc/rmm-remote/helper-general.json','/etc/rmm-remote/general-helper-proof.json','/etc/rmm-remote/helper-signing-key.json'] + cert=Path('/var/lib/rmm-agent-remote/certificates') + if cert.exists():fixed.extend(str(p)for p in cert.rglob('*')if p.is_file()) + for path in fixed: + m=Path(path).lstat();require(stat.S_ISREG(m.st_mode)and m.st_nlink==1,'retained original metadata');result[path]=identity(path)+[m.st_size,m.st_mtime_ns,m.st_ctime_ns] + return result + for name,entry in manifest['files'].items():require(sha((folder/name).read_bytes())==entry['sha256'],'staged artifact changed') + for unit in ('rmm-apt-helper.service','rmm-agent.service','rmm-helper-dev-test.service'): + s=status(unit);require(s['ActiveState']=='inactive'and s['MainPID']in('','0')and s['ControlPID']in('','0')and s['Job']==''and s['UnitFileState']in('','disabled'),'APT and legacy remain closed') + if payload['action']=='install': + require(not(folder/'pair-install-intent.json').exists(),'existing intent means readback, never repeat') + fence=payload['central_fence'];require(fence['operation']=='linux_compatible_closed_scope_v1'and fence['open_jobs']==0 and not fence['job_authority']and fence['tenant_id']==manifest['tenant_id']and fence['agent_id']==manifest['agent_id'],'closed central scope') + at=datetime.datetime.fromisoformat(fence['observed_at']);require(0<=(datetime.datetime.now(datetime.timezone.utc)-at).total_seconds()<30,'fresh closed-scope readback') + before={u:status(u)for u in units};pids=[] + for u,s in before.items(): + require(s['ActiveState']=='active'and int(s['MainPID'])>1 and s['ControlPID']=='0'and s['Job']=='','exact active original pair') + pid=int(s['MainPID']);fd=os.pidfd_open(pid);pids.append(fd) + original=databases();retained=retained_metadata() + for source,target,oldsha in [('agent.bin','/usr/local/libexec/rmm-agent-remote',manifest['old_agent_sha256']),('helper.bin','/usr/local/libexec/rmm-privileged-helper',manifest['old_helper_sha256']),('full-config.json','/etc/rmm-remote/helper-full-maintenance.json',manifest['old_config_sha256']),('full-pin.json','/etc/rmm-remote/full-maintenance-helper-proof.json',manifest['old_pin_sha256'])]: + raw,_=public(target,256*1024*1024);require(sha(raw)==oldsha,'original target CAS') + record('pair-install-intent.json',{'before':before,'databases':original,'retained':retained,'central_fence':fence,'files':manifest['files']}) + run(['systemctl','stop','--',units[0],units[2],units[1]],30) + for fd in pids: + poll=select.poll();poll.register(fd,select.POLLIN);require(poll.poll(1000),'original process not exited');os.close(fd) + for u in units: + s=status(u);require(s['ActiveState']=='inactive'and s['MainPID']=='0'and s['ControlPID']=='0'and not s['Job']and not s['ControlGroup'],'all original processes stopped') + with contextlib.ExitStack()as held: + for path in ('/var/lib/rmm-agent-remote/agent.lock','/var/lib/rmm-helper/executor.lock'): + f=held.enter_context(open(path,'rb'));fcntl.flock(f,fcntl.LOCK_EX|fcntl.LOCK_NB) + closed=databases() + require(all(closed[p]['file_id']==v['file_id']and closed[p]['evidence_sha256']==v['evidence_sha256']for p,v in original.items()),'original effect history changed during stop') + for source,target,oldsha in [('agent.bin','/usr/local/libexec/rmm-agent-remote',manifest['old_agent_sha256']),('helper.bin','/usr/local/libexec/rmm-privileged-helper',manifest['old_helper_sha256']),('full-config.json','/etc/rmm-remote/helper-full-maintenance.json',manifest['old_config_sha256']),('full-pin.json','/etc/rmm-remote/full-maintenance-helper-proof.json',manifest['old_pin_sha256'])]: + target=Path(target);old,meta=public(target,256*1024*1024);require(sha(old)==oldsha,'stopped target CAS') + previous=target.with_name(target.name+'.bootstrap-'+ident+'.previous');candidate=target.with_name(target.name+'.bootstrap-'+ident+'.candidate');require(not previous.exists()and not candidate.exists(),'create-once bootstrap names') + fd=os.open(candidate,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,stat.S_IMODE(meta.st_mode)) + with os.fdopen(fd,'wb')as stream:os.fchown(stream.fileno(),meta.st_uid,meta.st_gid);stream.write((folder/source).read_bytes());stream.flush();os.fsync(stream.fileno()) + record(source+'-save-intent.json',{'target':str(target),'previous':str(previous),'old_sha256':oldsha});os.rename(target,previous);syncdir(target.parent) + record(source+'-publish-intent.json',{'candidate':str(candidate),'target':str(target),'sha256':manifest['files'][source]['sha256']});os.rename(candidate,target);syncdir(target.parent) + syncdir(target.parent) + require(sha(target.read_bytes())==manifest['files'][source]['sha256']and sha(previous.read_bytes())==oldsha,'exact predecessor and replacement retained') + require(retained_metadata()==retained,'original key identity General policy or PKI changed') + record('pair-installed.json',{'databases':closed,'retained':retained,'old_generation':original['/var/lib/rmm-agent-remote/journal.sqlite']['generation'],'services_stopped':True,'job_authority':False}) + return {'case_id':ident,'compatible_files_installed':True,'services_stopped':True,'next':'separate reviewed unit setup and start phase','job_authority':False} + elif payload['action']=='units': + installed=json.loads((folder/'pair-installed.json').read_text());require(not(folder/'unit-install-intent.json').exists(),'existing unit intent means readback only') + require(retained_metadata()==installed['retained'],'original metadata changed') + for u in units: + row=status(u);require(row['ActiveState']=='inactive'and row['MainPID']=='0'and row['ControlPID']=='0'and not row['Job'],'pair remains stopped before unit setup') + paths={'/usr/local/libexec/rmm-agent-updater':('updater.bin',0o755),'/etc/systemd/system/rmm-agent-updater.service':('updater.service',0o644)} + require(all(not Path(path).exists()for path in paths),'stable updater is create-only') + require(not Path('/etc/rmm-agent-updater').exists()and not Path('/var/lib/rmm-agent-updater').exists(),'initial trust namespace only') + override=Path('/etc/systemd/system/rmm-full-maintenance-helper.service.d/99-rmm-update-closed-start.conf');require(not override.exists(),'create-only closed restart override') + record('unit-install-intent.json',{'paths':list(paths),'override':str(override),'restart':'no','services_started':False}) + for path,(name,mode)in paths.items(): + fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode) + with os.fdopen(fd,'wb')as stream:stream.write((folder/name).read_bytes());stream.flush();os.fsync(stream.fileno()) + if not override.parent.exists():override.parent.mkdir(mode=0o755) + require(override.parent.lstat().st_uid==0 and not override.parent.is_symlink()and not override.parent.lstat().st_mode&0o022,'protected Full override parent') + with override.open('xb')as stream:stream.write(b'[Service]\nRestart=no\n');stream.flush();os.fsync(stream.fileno()) + override.chmod(0o644) + for path,mode in [('/etc/rmm-agent-updater',0o755),('/var/lib/rmm-agent-updater',0o700),('/var/lib/rmm-agent-updater/records',0o700),('/var/lib/rmm-agent-updater/handoffs',0o700)]:Path(path).mkdir(mode=mode) + binding=json.loads(Path('/var/lib/rmm-agent-remote/binding.json').read_text());key=json.loads(Path('/etc/rmm-remote/helper-signing-key.json').read_text()) + require(binding.get('public_key_hex')in('',key['public_key_hex']),'independent effective public key pin');binding['public_key_hex']=key['public_key_hex'] + fd=os.open('/etc/rmm-agent-updater/binding.json',os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600) + with os.fdopen(fd,'w')as stream:json.dump(binding,stream,separators=(',',':'));stream.flush();os.fsync(stream.fileno()) + run(['systemctl','daemon-reload']) + require(run(['systemctl','show','--value','--property=Restart','--',units[2]])=='no','actual closed Full restart contract') + updater=status('rmm-agent-updater.service');require(updater['ActiveState']=='inactive'and updater['MainPID']=='0'and updater['ControlPID']=='0'and not updater['Job']and updater['UnitFileState']=='disabled','updater remains stopped and disabled') + record('units-installed.json',{'updater':updater,'restart_override_sha256':sha(override.read_bytes()),'namespace_provisioned':True,'updater_started':False}) + return {'case_id':ident,'updater_unit_installed_not_started':True,'full_restart_no_bound':True,'job_authority':False} + elif payload['action']=='start': + installed=json.loads((folder/'pair-installed.json').read_text());require((folder/'units-installed.json').is_file(),'separate reviewed unit setup required');require(not(folder/'pair-start-intent.json').exists(),'existing start means readback only') + require(retained_metadata()==installed['retained'],'original metadata changed') + for u in units: + s=status(u);require(s['ActiveState']=='inactive'and s['MainPID']=='0'and s['ControlPID']=='0'and not s['Job'],'pair remains stopped') + record('pair-start-intent.json',{'units':[units[1],units[2],units[0]],'only_once':True}) + for u in (units[1],units[2],units[0]): + record(u+'-start-intent.json',{'unit':u});run(['systemctl','start','--',u],20);s=status(u);require(s['ActiveState']=='active'and int(s['MainPID'])>1,'single compatible start') + record('pair-started.json',{'status':{u:status(u)for u in units},'job_authority':False}) + return {'case_id':ident,'compatible_services_started_once':True,'job_authority':False,'normal_signed_sync_qualification_still_required':True} + elif payload['action']=='qualify': + installed=json.loads((folder/'pair-installed.json').read_text());require((folder/'pair-started.json').is_file(),'compatible starts required') + def pin(unit): + raw=run(['systemctl','show','--all','--property=FragmentPath,DropInPaths','--',unit]);fields=dict(line.split('=',1)for line in raw.splitlines());paths=[fields['FragmentPath']]+fields['DropInPaths'].split();require(bool(paths[0]),'actual unit composition') + composition=[] + for path in paths: + data,m=public(path,65536);composition.append([path,{'identity':dict(zip(('device','inode','uid','gid','mode','links'),identity(path))),'bytes':len(data),'sha256':sha(data)}]) + return {'unit':unit,'unit_sha256':sha(json.dumps(composition,separators=(',',':'),ensure_ascii=False).encode())} + pins=[pin(u)for u in units] + if run(['systemctl','show','--value','--property=LoadState','--','rmm-apt-helper.service'])=='loaded':pins.append(pin('rmm-apt-helper.service')) + request={'operation':'qualify_pair','minimum_generation':installed['old_generation'],'expected_agent_sha256':manifest['files']['agent.bin']['sha256'],'expected_helper_sha256':manifest['files']['helper.bin']['sha256'],'unit_pins':pins} + raw=subprocess.check_output([str(folder/'sdk.bin')],input=json.dumps(request).encode(),stderr=subprocess.DEVNULL,timeout=35);value=json.loads(raw) + require(value['profile']=='linux_compatible_baseline_qualification_v1'and value['normal_signed_sync_observed']and value['ready']is False and value['helper_ready']is False and value['unlock']is False and value['job_authority']is False,'actual fresh normal qualification') + after=databases();require(all(after[p]['file_id']==v['file_id']and after[p]['evidence_sha256']==v['evidence_sha256']for p,v in installed['databases'].items()),'all original consumption and evidence retained') + require(retained_metadata()==installed['retained'],'original General identity key or PKI changed') + with(folder/'qualification.json').open('xb')as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno()) + record('pair-qualified.json',{'qualification_sha256':sha(raw),'unit_pins':pins,'databases':after,'job_authority':False}) + return {'case_id':ident,'qualification_sha256':sha(raw),'qualification_base64url':base64.urlsafe_b64encode(raw).rstrip(b'=').decode(),'sample':value,'services_restarted':False,'job_authority':False} + elif payload['action']=='registry': + receipt=json.loads((folder/'pair-qualified.json').read_text());raw=(folder/'qualification.json').read_bytes();require(sha(raw)==receipt['qualification_sha256'],'actual qualification bytes unchanged') + require(not(folder/'registry.json').exists(),'create-only actual registry') + request={'operation':'registry','qualification_sha256':receipt['qualification_sha256']} + output=subprocess.check_output([str(folder/'sdk.bin')],input=json.dumps(request).encode(),stderr=subprocess.DEVNULL,timeout=35);value=json.loads(output) + registry=bytes(value['registry_bytes']);require(sha(registry)==value['reader_registry_sha256'],'actual typed registry hash') + with(folder/'registry.json').open('xb')as stream:stream.write(registry);stream.flush();os.fsync(stream.fileno()) + record('registry-prepared.json',value) + return {'case_id':ident,'registry':value,'services_changed':False,'job_authority':False} + elif payload['action']=='trust': + import tempfile + trust=payload['trust'];require(trust['operation']=='linux_compatible_trust_install_v1'and trust['case_id']==ident and trust['job_authority']is False,'fixed public trust package') + receipt=json.loads((folder/'pair-qualified.json').read_text());require(trust['qualification_sha256']==receipt['qualification_sha256'],'original native qualification') + require(not(folder/'trust-install-intent.json').exists(),'existing trust intent means readback only') + expected={'policy.json','keyring.json','health-key.json','reader-registry.json','installed-record.json'};require(set(trust['files'])==expected,'closed trust document set') + raw={} + for name,entry in trust['files'].items(): + raw[name]=base64.b64decode(entry['base64'],validate=True);require(len(raw[name])==entry['bytes']and len(raw[name])<=65536 and sha(raw[name])==entry['sha256'],'exact bounded public trust bytes') + require(raw['reader-registry.json']==(folder/'registry.json').read_bytes(),'actual measured reader registry') + request={'operation':'validate_public_bundle','record':list(raw['installed-record.json']),'policy':list(raw['policy.json']),'keyring':list(raw['keyring.json']),'health_pin':list(raw['health-key.json'])} + checked=json.loads(subprocess.check_output([str(folder/'sdk.bin')],input=json.dumps(request).encode(),stderr=subprocess.DEVNULL,timeout=20));require(checked==trust['shape_checks']and checked['shape_only_not_signature_verified']is True,'independent native typed shape check') + policy=json.loads(raw['policy.json']);keys=json.loads(raw['keyring.json']);health=json.loads(raw['health-key.json']);baseline=policy['initial_baseline'];record_doc=json.loads(raw['installed-record.json']);envelope=record_doc['release_envelope'] + require(keys['generation']==1 and len(keys['keys'])==1 and keys['keys'][0]['platform']=='x86_64-unknown-linux-gnu'and keys['keys'][0]['first_sequence']==1 and keys['keys'][0]['last_sequence']==2 and not keys['keys'][0]['revoked'],'initial two-release trust scope') + public_key=bytes.fromhex(keys['keys'][0]['public_key_hex']);job_key=json.loads(Path('/etc/rmm-remote/helper-signing-key.json').read_text())['public_key_hex'];require(len(public_key)==32 and public_key.hex()!=job_key and public_key.hex()!=health['public_key_hex']and health['public_key_hex']!=job_key,'separate updater release health and Job keys') + require(sha(public_key)==baseline['key_id']==envelope['key_id'],'exact independently provisioned release key identity') + def decode(value): + decoded=base64.urlsafe_b64decode(value+'='*((-len(value))%4));require(base64.urlsafe_b64encode(decoded).rstrip(b'=').decode()==value,'canonical signed public bytes');return decoded + with tempfile.TemporaryDirectory(prefix='rmm-bootstrap-verify-')as tmp: + temp=Path(tmp);(temp/'public.der').write_bytes(bytes.fromhex('302a300506032b6570032100')+public_key) + for role,document,prefix in [('baseline',baseline,b'rmm.agent-update-baseline.v1\0'),('release',envelope,b'rmm.agent-release.v1\0')]: + message=decode(document['payload_bytes']);signature=decode(document['signature']);require(len(signature)==64,'original Ed25519 signature') + (temp/'message').write_bytes(prefix+message);(temp/'signature').write_bytes(signature) + subprocess.run(['openssl','pkeyutl','-verify','-rawin','-pubin','-keyform','DER','-inkey',str(temp/'public.der'),'-in',str(temp/'message'),'-sigfile',str(temp/'signature')],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,timeout=10) + if role=='baseline':require(message==raw['installed-record.json'],'exact initial signed record') + else: + release=json.loads(message);require(sha(message)==record_doc['manifest_sha256']and release['release_sequence']==1 and release['version']=='0.1.0'and release['platform']=='x86_64-unknown-linux-gnu','exact initial signed manifest') + require([(a['role'],a['sha256'],a['bytes'])for a in release['artifacts']]==[(role,manifest['files'][name]['sha256'],manifest['files'][name]['bytes'])for role,name in [('agent','agent.bin'),('helper','helper.bin')]],'signed release binds exact installed compatible code pair') + require(record_doc['activation_evidence']=={'kind':'bootstrap','qualification_receipt_sha256':receipt['qualification_sha256']}and policy['updater_binary_sha256']==manifest['files']['updater.bin']['sha256'],'actual qualification and stable updater') + require(all(policy[k]==manifest[k]for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version'))and policy['services']==json.loads((folder/'registry-prepared.json').read_text())['services'],'exact local identity and actual unit pins') + retained=retained_metadata();specs=[] + mutable={'/var/lib/rmm-agent-remote/journal.sqlite','/var/lib/rmm-agent-remote/consumption.sqlite','/var/lib/rmm-helper/consumption.sqlite'} + paths=sorted(set(retained)|mutable|{'/var/lib/rmm-agent-remote/agent.lock','/var/lib/rmm-helper/executor.lock'}) + for path in paths: + m=Path(path).lstat();require(stat.S_ISREG(m.st_mode)and not Path(path).is_symlink(),'retained state original regular file');parent=Path(path).parent.lstat();owner=993 if path.startswith('/var/lib/rmm-agent-remote/')else 0;require(parent.st_uid==owner and not parent.st_mode&0o022,'retained state protected parent') + specs.append({'path':path,'parent_uid':owner,'identity':dict(zip(('device','inode','uid','gid','mode','links'),identity(path))),'retention':'mutable_database'if path in mutable else'immutable_metadata'}) + require(01 and updater['ControlPID']=='0'and not updater['Job'],'actual stable updater started') + fd=os.pidfd_open(pid) + try: + start=Path('/proc/'+str(pid)+'/stat').read_text().rsplit(') ',1)[1].split()[19];image=Path('/proc/'+str(pid)+'/exe').read_bytes();require(sha(image)==manifest['files']['updater.bin']['sha256'],'actual original stable updater image') + require(status('rmm-agent-updater.service')==updater,'same updater process held through observation') + finally:os.close(fd) + record('updater-started.json',{'status':updater,'process_start_ticks':start,'image_sha256':sha(image),'job_authority':False}) + return {'case_id':ident,'stable_updater_started_once':True,'pid':pid,'image_sha256':sha(image),'actual_agent_passive_observation_still_required':True,'job_authority':False} + elif payload['action']=='readback': + names=[p.name for p in folder.glob('*')if p.name.endswith('.json')and p.name!='manifest.json'];public={} + for name in ('qualification.json','registry-prepared.json','pair-qualified.json','trust-installed.json','updater-started.json'): + path=folder/name + if path.exists(): + raw=path.read_bytes();require(len(raw)<=192*1024,'bounded public readback');public[name]={'sha256':sha(raw),'base64url':base64.urlsafe_b64encode(raw).rstrip(b'=').decode()} + return {'case_id':ident,'records':names,'public_evidence':public,'status':{u:status(u)for u in units+['rmm-agent-updater.service']},'databases':databases(),'job_authority':False} + else:raise RuntimeError('unknown fixed pair action') + +def prepare(sdk_manifest): + require=lambda ok,msg:None if ok else(_ for _ in()).throw(RuntimeError(msg)) + require(sha(BASE.read_bytes())==BASE_SHA,'exact capability derivative V2 manifest') + build=json.loads(BASE.read_text());sdk=json.loads(sdk_manifest.read_text());require(sdk['profile']=='linux_updater_bootstrap_metadata_sdk_v1'and sdk['release_built']and sdk['clippy_all_targets_passed']and not sdk['target_mutated'],'sealed metadata SDK') + inputs=native_rpc(public_inputs,{'operation':'linux_compatible_public_inputs_v1'}) + snapshot=transport.rpc('linux-vm.conf',prerequisite.inspect_vm,{'operation':'linux_updater_bootstrap_inspect_v1'}) + require(snapshot['boot_id']==BOOT and not snapshot['updater_provisioned'],'original initial target') + full=json.loads(bytes(inputs['helper-full-maintenance.json']['bytes']));key=json.loads(bytes(inputs['helper-signing-key.json']['bytes'])) + require(full['signing_key_file']=='/etc/rmm-remote/helper-signing-key.json','independent fixed Full keyfile') + binary=Path(sdk['binary']['path']);require(sha(binary.read_bytes())==sdk['binary']['sha256'],'sealed SDK bytes') + request={'operation':'render_full','config':inputs['helper-full-maintenance.json']['bytes'],'pin':inputs['full-maintenance-helper-proof.json']['bytes'],'job_public_key_hex':key['public_key_hex'],'new_helper_sha256':build['artifacts']['source0']['helper']['sha256']} + command=['docker','run','--rm','-i','--network','none','--read-only','--tmpfs','/tmp:rw,size=64m','-v',str(binary)+':/sdk:ro','--entrypoint','/sdk','sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6'] + rendered=subprocess.run(command,input=json.dumps(request).encode(),capture_output=True,check=True,timeout=30);transition=json.loads(rendered.stdout) + ident=str(uuid.uuid4());folder=ROOT/'devstate/linux-updater-compatible'/ident;folder.mkdir(mode=0o700,parents=True) + files={} + for name,item in [('agent.bin',build['artifacts']['source0']['agent']),('helper.bin',build['artifacts']['source0']['helper']),('updater.bin',build['stable_updater']),('sdk.bin',sdk['binary'])]: + raw=Path(item['path']).read_bytes();require(sha(raw)==item['sha256']and len(raw)==item['bytes'],'sealed exact code artifact');files[name]=raw + files['full-config.json']=bytes(transition['config']);files['full-pin.json']=bytes(transition['pin']) + unit=ROOT/'devstate/linux-updater-qualification/21d20391-6102-4996-8a7a-efbd3e78bd7d/source0/deploy/linux/rmm-agent-updater.service';files['updater.service']=unit.read_bytes() + proof=transition['proof'];require(proof['new_helper_sha256']==build['artifacts']['source0']['helper']['sha256']and proof['old_config_sha256']==inputs['helper-full-maintenance.json']['sha256']and proof['old_pin_sha256']==inputs['full-maintenance-helper-proof.json']['sha256'],'typed exact Full transition') + for name,raw in files.items():once(folder/name,raw) + binding=full['binding'];payload={'schema_version':1,'profile':'linux_compatible_bootstrap_package_v1','case_id':ident,'boot_id':BOOT,'tenant_id':binding['tenant_id'],'agent_id':binding['agent_id'],'asset_id':binding['asset_id'],'enrollment_generation':binding['enrollment_generation'],'assignment_version':binding['assignment_version'],'build_manifest_sha256':BASE_SHA,'sdk_manifest_sha256':sha(sdk_manifest.read_bytes()),'old_agent_sha256':snapshot['binaries']['/usr/local/libexec/rmm-agent-remote']['sha256'],'old_helper_sha256':snapshot['binaries']['/usr/local/libexec/rmm-privileged-helper']['sha256'],'old_config_sha256':proof['old_config_sha256'],'old_pin_sha256':proof['old_pin_sha256'],'full_transition':proof,'files':{n:{'sha256':sha(raw),'bytes':len(raw),'base64':base64.b64encode(raw).decode()}for n,raw in files.items()},'job_authority':False} + once(folder/'package.json',json.dumps(payload,sort_keys=True,separators=(',',':')).encode());once(folder/'readonly-before.json',json.dumps(snapshot,sort_keys=True,indent=2).encode());once(folder/'public-inputs.json',json.dumps(inputs,sort_keys=True,indent=2).encode()) + summary={k:v for k,v in payload.items()if k!='files'};summary['files']={n:{k:v for k,v in e.items()if k!='base64'}for n,e in payload['files'].items()};once(folder/'manifest.json',json.dumps(summary,sort_keys=True,indent=2).encode()) + print(json.dumps({'package':str(folder/'package.json'),'manifest':str(folder/'manifest.json'),'case_id':ident,'local_only_preparation':True,'target_staged':False,'services_changed':False,'full_policy_sha256':proof['new_policy_sha256']})) + +def main(): + parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('prepare','public-inputs','central-check','stage','install','units','start','qualify','registry','trust','updater-start','passive-check','readback'));parser.add_argument('--package',type=Path);parser.add_argument('--sdk-manifest',type=Path);parser.add_argument('--trust-package',type=Path);args=parser.parse_args() + if args.action=='prepare': + if args.sdk_manifest is None:parser.error('--sdk-manifest required for preparation') + prepare(args.sdk_manifest);return + if args.package is None:parser.error('--package required for fixed native phases') + p=json.loads(args.package.read_text());require=lambda ok,msg:None if ok else(_ for _ in()).throw(RuntimeError(msg)) + require(p['schema_version']==1 and p['profile']=='linux_compatible_bootstrap_package_v1'and p['boot_id']==BOOT,'reviewed exact package') + if args.action=='public-inputs':value=native_rpc(public_inputs,{'operation':'linux_compatible_public_inputs_v1'}) + elif args.action=='central-check':value=native_rpc(central_closed,{'agent_id':p['agent_id'],'tenant_id':p['tenant_id']},config='server.conf') + elif args.action=='stage':value=native_rpc(stage,p,timeout=120) + elif args.action=='passive-check': + if args.trust_package is None:parser.error('--trust-package required for exact passive comparison') + trust=json.loads(args.trust_package.read_text());files=trust['files'];value=native_rpc(passive_check,{'tenant_id':p['tenant_id'],'agent_id':p['agent_id'],'record_sha256':files['installed-record.json']['sha256'],'policy_sha256':files['policy.json']['sha256'],'keyring_sha256':files['keyring.json']['sha256'],'health_pin_sha256':files['health-key.json']['sha256'],'agent_sha256':p['files']['agent.bin']['sha256'],'helper_sha256':p['files']['helper.bin']['sha256'],'updater_sha256':p['files']['updater.bin']['sha256'],'helper_policy_sha256':p['full_transition']['new_policy_sha256']},config='server.conf') + else: + payload={'case_id':p['case_id'],'action':args.action} + if args.action=='install':payload['central_fence']=native_rpc(central_closed,{'agent_id':p['agent_id'],'tenant_id':p['tenant_id']},config='server.conf') + if args.action=='trust': + if args.trust_package is None:parser.error('--trust-package required for protected trust installation') + payload['trust']=json.loads(args.trust_package.read_text()) + value=native_rpc(pair,payload,timeout=120) + print(json.dumps(value)) +if __name__=='__main__':main() diff --git a/deploy/linux_updater_public_materials.py b/deploy/linux_updater_public_materials.py new file mode 100644 index 0000000..065bdfe --- /dev/null +++ b/deploy/linux_updater_public_materials.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +"""Prepare public updater metadata using the sealed Rust codecs; never signs. + +Every input is public evidence. A locally prepared document is neither a native +qualification nor trusted until the existing native verifier accepts it. +""" +import argparse +import base64 +import datetime +import hashlib +import json +from pathlib import Path +import subprocess +import uuid +import linux_updater_compatible_apply as bootstrap + +SDK = bootstrap.ROOT / 'devstate/linux-updater-bootstrap-sdk/d309c949-3a70-4448-88da-49c710093221/sdk.bin' +SDK_SHA = '6fcc543d538fca30ceb499364880e4dc3689700d50597f18bd11bfe3559a0021' + +def require(condition): + if not condition: raise RuntimeError('Public updater preparation contract failed') +def digest(raw): return hashlib.sha256(raw).hexdigest() +def encode(value): return json.dumps(value, separators=(',', ':'), ensure_ascii=False).encode() +def b64(raw): return base64.urlsafe_b64encode(raw).rstrip(b'=').decode() +def unb64(value): + raw = base64.urlsafe_b64decode(value + '=' * (-len(value) % 4)) + require(b64(raw) == value) + return raw +def typed(value): + require(digest(SDK.read_bytes()) == SDK_SHA) + result = subprocess.run(['docker', 'run', '--rm', '-i', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=64m', '-v', str(SDK)+':/sdk:ro', '--entrypoint', '/sdk', 'sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6'], input=encode(value), capture_output=True, check=True, timeout=30) + return json.loads(result.stdout) +def prepare(action, data): + signer = data['signer'] + require(signer['platform'] == 'linux' and signer['profile'] == 'agent_updater_separate_signers_v1') + common = {'case_id': signer['case_id'], 'platform': 'linux', 'job_authority': False} + if action == 'release-input': + build = json.loads(bootstrap.BASE.read_text()) + require(digest(bootstrap.BASE.read_bytes()) == bootstrap.BASE_SHA) + seq = data['sequence']; require(seq in (1, 2)) + published = int(datetime.datetime.now(datetime.timezone.utc).timestamp()) + manifest = {'schema_version': 1, 'release_id': str(uuid.uuid4()), 'release_sequence': seq, 'version': '0.1.0' if seq == 1 else '0.1.1', 'platform': 'x86_64-unknown-linux-gnu', 'published_at': published, 'expires_at': published + 604800, 'protocol_major': 1, 'state_schema': 1, 'minimum_updater_version': 1, 'artifacts': []} + for role in ('agent', 'helper'): + item = build['artifacts']['source'+str(seq-1)][role] + manifest['artifacts'].append({'role': role, 'artifact_id': str(uuid.uuid4()), 'sha256': item['sha256'], 'bytes': item['bytes']}) + result = typed({'operation': 'canonical_release', 'manifest': manifest}) + raw = bytes(result['bytes']) + return {**common, 'canonical_payload_base64url': b64(raw), 'payload_sha256': digest(raw)} + qualification = unb64(data['qualification_base64url']); q = json.loads(qualification) + require(q['profile'] == 'linux_compatible_baseline_qualification_v1' and q['normal_signed_sync_observed']) + require(all(q[n] is False for n in ('ready', 'helper_ready', 'unlock', 'job_authority'))) + if action == 'record-input': + release = data['signed_release']; manifest = json.loads(unb64(release['envelope']['payload_bytes'])) + require(manifest['release_sequence'] == 1 and manifest['version'] == '0.1.0') + record = {'schema_version': 1, 'profile': 'decoder_compatible_handoff_v1', 'platform': manifest['platform'], 'release_envelope': release['envelope'], 'manifest_sha256': release['payload_sha256'], 'activation_floor': 1, 'activated_at': int(datetime.datetime.fromisoformat(q['observed_at'].replace('Z', '+00:00')).timestamp()), 'previous_record_sha256': None, 'activation_evidence': {'kind': 'bootstrap', 'qualification_receipt_sha256': digest(qualification)}, 'rolled_back': False} + raw = bytes(typed({'operation': 'canonical_record', 'record': record})['bytes']) + return {**common, 'canonical_payload_base64url': b64(raw), 'payload_sha256': digest(raw), 'qualification_base64url': b64(qualification)} + require(action == 'trust-package') + package = data['bootstrap_package']; registry = data['registry']; baseline = data['signed_baseline'] + record_raw = unb64(baseline['envelope']['payload_bytes']); record = json.loads(record_raw) + require(record['activation_evidence'] == {'kind': 'bootstrap', 'qualification_receipt_sha256': digest(qualification)}) + registry_raw = bytes(registry['registry_bytes']); require(digest(registry_raw) == registry['reader_registry_sha256']) + policy = {'schema_version': 1, 'profile': 'linux_general_full_v1', **{n: package[n] for n in ('tenant_id', 'asset_id', 'agent_id', 'enrollment_generation', 'assignment_version')}, 'ledger_path': '/var/lib/rmm-helper/consumption.sqlite', 'lock_path': '/var/lib/rmm-helper/executor.lock', 'installed_record_path': '/var/lib/rmm-agent-updater/installed-record.json', 'keyring_path': '/etc/rmm-agent-updater/keyring.json', 'health_pin_path': '/etc/rmm-agent-updater/health-key.json', 'registry_path': '/etc/rmm-agent-updater/reader-registry.json', 'reader_registry_sha256': registry['reader_registry_sha256'], 'services': registry['services'], 'updater_service': registry['updater_service'], 'updater_binary_sha256': package['files']['updater.bin']['sha256'], 'initial_baseline': baseline['envelope'], 'initial_record_sha256': digest(record_raw), 'initial_manifest_sha256': record['manifest_sha256'], 'initial_activation_floor': 1} + policy_raw = bytes(typed({'operation': 'canonical_policy', 'policy': policy})['bytes']) + keyring_raw = encode({'schema_version': 1, 'generation': 1, 'keys': [{'public_key_hex': signer['release_public_key_hex'], 'platform': 'x86_64-unknown-linux-gnu', 'first_sequence': 1, 'last_sequence': 2, 'revoked': False}]}) + require(keyring_raw == unb64(signer['keyring_base64url']) and digest(keyring_raw) == signer['keyring_sha256']) + health_raw = encode(signer['health_pin']) + shape = typed({'operation': 'validate_public_bundle', 'record': list(record_raw), 'policy': list(policy_raw), 'keyring': list(keyring_raw), 'health_pin': list(health_raw)}) + require(shape['shape_only_not_signature_verified'] is True) + files = {'policy.json': policy_raw, 'keyring.json': keyring_raw, 'health-key.json': health_raw, 'reader-registry.json': registry_raw, 'installed-record.json': record_raw} + return {'operation': 'linux_compatible_trust_install_v1', 'case_id': package['case_id'], 'signer_case_id': signer['case_id'], 'qualification_sha256': digest(qualification), 'files': {n: {'base64': base64.b64encode(raw).decode(), 'sha256': digest(raw), 'bytes': len(raw)} for n, raw in files.items()}, 'shape_checks': shape, 'signature_verified': False, 'job_authority': False} + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('action', choices=('release-input', 'record-input', 'trust-package')) + parser.add_argument('--input', required=True, type=Path) + parser.add_argument('--output', required=True, type=Path) + args = parser.parse_args() + result = prepare(args.action, json.loads(args.input.read_text())) + bootstrap.once(args.output, encode(result)) + print(json.dumps({'output': str(args.output), 'sha256': digest(args.output.read_bytes()), 'local_only': True, 'target_changed': False})) +if __name__ == '__main__': main() diff --git a/deploy/linux_updater_qualification.py b/deploy/linux_updater_qualification.py new file mode 100644 index 0000000..ca96e23 --- /dev/null +++ b/deploy/linux_updater_qualification.py @@ -0,0 +1,211 @@ +#!/usr/bin/env python3 +"""Freeze and build the Linux compatible baseline and version-only successor. + +Offline read-only Docker source mounts, independent local cache, no credentials, +VM/server command, job, daemon, signing-key generation or deployment. Windows +sources are retained as provenance only, never qualified by this Linux run. +""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import stat +import subprocess +import tarfile +import time +import uuid +ROOT=Path(__file__).resolve().parents[1] +STATE=ROOT/'devstate/linux-updater-qualification' +BUILDER='sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6' +PACKAGES=['rmm-agent','rmm-agent-core','rmm-domain','rmm-privileged-helper','rmm-updater-linux','rmm-updater-worker','rmm-updater-handoff','rmm-update-history'] +UNIT_TARGETS=[('agent','rmm-agent',['--bin','rmm-agent']),('helper','rmm-privileged-helper',['--lib']), + ('core','rmm-agent-core',['--lib']),('helper-reconcile','rmm-agent-core',['--test','helper_reconcile']), + ('domain','rmm-domain',['--lib']),('updater-linux','rmm-updater-linux',['--lib']), + ('worker','rmm-updater-worker',['--lib']),('handoff','rmm-updater-handoff',['--test','handoff_contract'])] +SAFE_FILTERS={ + 'updater-linux':['services::status_tests::accepts_actual_seven_property_shape_and_denies_missing_or_extra_properties'], + 'helper':['update_admission::tests::typed_handoff_ack_is_not_an_outcome_or_a_new_start_lease'], + 'core':['update_health_sync::tests::same_floor_does_not_admit_a_one_nanosecond_early_or_late_apply'], + 'helper-reconcile':['update_handoff_consumption_start_and_stop_intent_are_one_transaction', + 'update_inflight_negative_control_keeps_every_start_closed_and_owner_loss_is_terminal', + 'native_stop_intent_is_original_head_bound_create_once_and_transactional', + 'restarted_update_can_only_schedule_control_from_its_verified_original_not_unknown'], +} + +def require(ok,code): + if not ok:raise RuntimeError(code) + +def sha(raw):return hashlib.sha256(raw).hexdigest() +def encode(value):return json.dumps(value,sort_keys=True,indent=2).encode()+b'\n' +def write(path,raw): + path=Path(path);path.parent.mkdir(mode=0o700,parents=True,exist_ok=True) + fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600) + with os.fdopen(fd,'wb') as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno()) + +def public_files(): + names=subprocess.run(['git','ls-files','--cached','--others','--exclude-standard','-z'],cwd=ROOT,capture_output=True,check=True).stdout.split(b'\0') + prefixes=('crates/','proto/','migrations/','deploy/','tools/','tests/') + fixed={'Cargo.toml','Cargo.lock','rust-toolchain.toml','Rust_RMM_Implementierungsspezifikation.md','AGENTS.md'} + result=[] + for raw in names: + if not raw:continue + name=os.fsdecode(raw);path=Path(name) + if name not in fixed and not name.startswith(prefixes):continue + require(not path.is_absolute() and '..' not in path.parts,'source relative path') + if '__pycache__' in path.parts or path.suffix in ('.pyc','.pyo') or path.name=='.env':continue + full=ROOT/path + require(not full.is_symlink() and full.is_file(),'public regular source only') + meta=full.stat();require(meta.st_size<=16*1024*1024,'bounded compiler/public fixture') + result.append(name) + require({'Cargo.toml','Cargo.lock','crates/agent/src/main.rs','crates/privileged-helper/src/main.rs','crates/updater-worker/src/main.rs'}<=set(result),'complete Linux entrypoints') + return sorted(set(result)) + +def capture(root,names): + result={} + for name in names: + file=root/name;before=file.stat();raw=file.read_bytes();after=file.stat() + require((before.st_dev,before.st_ino,before.st_size,before.st_mtime_ns,before.st_ctime_ns)== + (after.st_dev,after.st_ino,after.st_size,after.st_mtime_ns,after.st_ctime_ns),'source changed while reading') + result[name]=dict(bytes=len(raw),sha256=sha(raw)) + return result + +def readonly(root): + for path in root.rglob('*'): + path.chmod(0o555 if path.is_dir() else 0o444) + root.chmod(0o555) + +def freeze(folder): + names=public_files();before=capture(ROOT,names);source0=folder/'source0' + source0.mkdir(mode=0o700) + for name in names:write(source0/name,(ROOT/name).read_bytes()) + require(capture(source0,names)==before and public_files()==names and capture(ROOT,names)==before,'whole source input snapshot drift; preserve failed case, do not build') + require(re.search(r'(?ms)^\[workspace\.package\]\s*version = "0\.1\.0"$',(source0/'Cargo.toml').read_text()),'exact baseline version0.1.0') + readonly(source0);write(folder/'source0-inputs.json',encode(before)) + source1=folder/'source1';shutil.copytree(source0,source1) + cargo=source1/'Cargo.toml';lock=source1/'Cargo.lock';cargo.chmod(0o600);lock.chmod(0o600) + original=cargo.read_text();require(original.count('version = "0.1.0"')==1,'one workspace version field') + cargo.write_text(original.replace('version = "0.1.0"','version = "0.1.1"',1)) + match=re.search(r'(?ms)^members\s*=\s*(\[.*?\])',original) + require(match is not None,'literal workspace member list') + members=json.loads(match[1]);internal=[] + require(all(isinstance(member,str) and member.startswith('crates/') and '..' not in Path(member).parts for member in members),'closed workspace member paths') + for member in members: + package=(source1/member/'Cargo.toml').read_text() + if re.search(r'(?m)^version\.workspace\s*=\s*true$',package): + name=re.search(r'(?m)^name\s*=\s*"([a-z0-9-]+)"$',package) + require(name is not None,'literal inherited package name');internal.append(name[1]) + text=lock.read_text() + for name in internal: + pattern='name = "'+name+'"\nversion = "0.1.0"' + require(text.count(pattern)==1,'one inherited internal lock package') + text=text.replace(pattern,'name = "'+name+'"\nversion = "0.1.1"',1) + lock.write_text(text) + # External crate packages and the excluded independent oidc-auth version + # remain exact; only inherited workspace package versions change. + inverse=text + for name in internal:inverse=inverse.replace('name = "'+name+'"\nversion = "0.1.1"','name = "'+name+'"\nversion = "0.1.0"',1) + require(inverse==(source0/'Cargo.lock').read_text(),'no change outside exact inherited internal version lines') + cargo.chmod(0o444);lock.chmod(0o444);readonly(source1) + after=capture(source1,names) + require({name for name in names if before[name]!=after[name]}=={'Cargo.toml','Cargo.lock'},'successor only differs in version/lock metadata') + write(folder/'source1-inputs.json',encode(after)) + return names,before,after,internal + +def run(folder,source,cache,label,args): + logfile=folder/'logs'/(label+'.log');logfile.parent.mkdir(mode=0o700,exist_ok=True) + command=['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=512m', + '--tmpfs','/run:rw,size=128m','--tmpfs','/var/lib:rw,size=128m', + '-v',str(source)+':/src:ro','-v',str(cache)+':/target','-e','CARGO_TARGET_DIR=/target', + '-w','/src','--entrypoint','/bin/sh',BUILDER,'-c','mkdir -p /tmp/opencode\nexec '+__import__('shlex').join(args)] + write(folder/'logs'/(label+'-command.json'),encode(command)) + started=time.time();print(json.dumps(dict(stage=label,started=True)),flush=True) + with logfile.open('xb') as stream: + result=subprocess.run(command,stdout=stream,stderr=subprocess.STDOUT,timeout=1800) + logfile.chmod(0o600) + result_record=dict(command=command,exit_code=result.returncode,elapsed_seconds=round(time.time()-started,3),log_sha256=sha(logfile.read_bytes())) + write(folder/'logs'/(label+'-result.json'),encode(result_record)) + require(result.returncode==0,'build/check failed: '+label+'; original log preserved') + return logfile.read_bytes() + +def copy_artifact(path,target): + raw=path.read_bytes();require(raw[:6]==b'\x7fELF\x02\x01' and raw[18:20]==b'\x3e\x00','native Linux x86-64 ELF') + write(target,raw);target.chmod(0o500) + return dict(path=str(target),sha256=sha(raw),bytes=len(raw),platform='x86_64-unknown-linux-gnu') + +def cargo_packages(): + return [part for package in PACKAGES for part in ('-p',package)] + +def build(): + os.umask(0o077);ident=str(uuid.uuid4());folder=STATE/ident;folder.mkdir(mode=0o700,parents=True) + write(folder/'entered.json',encode(dict(schema_version=1,case_id=ident,root=str(ROOT),builder=BUILDER, + git_head=subprocess.run(['git','rev-parse','HEAD'],cwd=ROOT,capture_output=True,check=True,text=True).stdout.strip(), + no_target_mutations=True,windows_not_qualified=True))) + names,input0,input1,internal=freeze(folder) + cache=folder/'cache';cache.mkdir(mode=0o700) + results={};units={} + for version,inputs in (('source0',input0),('source1',input1)): + source=folder/version + run(folder,source,cache,version+'-metadata',['cargo','metadata','--locked','--offline','--no-deps','--format-version=1']) + run(folder,source,cache,version+'-clippy',['cargo','clippy','--locked','--offline',*cargo_packages(),'--all-targets','--','-D','warnings']) + packages=['-p','rmm-agent','-p','rmm-privileged-helper']+(['-p','rmm-updater-worker'] if version=='source0' else []) + run(folder,source,cache,version+'-release',['cargo','build','--locked','--offline','--release',*packages,'--bins']) + output=folder/'artifacts'/version;output.mkdir(mode=0o700,parents=True) + results[version]={} + for label,binary in [('agent','rmm-agent'),('helper','rmm-privileged-helper')]+([('updater','rmm-agent-updater')] if version=='source0' else []): + results[version][label]=copy_artifact(cache/'release'/binary,output/binary) + require(capture(source,names)==inputs,'frozen source changed during build') + if version=='source1':continue + for label,package,selector in UNIT_TARGETS: + raw=run(folder,source,cache,'units-'+label,['cargo','test','--locked','--offline','--no-run','--message-format=json','-p',package,*selector]) + found=[] + for line in raw.splitlines(): + try:record=json.loads(line) + except ValueError:continue + if record.get('reason')=='compiler-artifact' and record.get('profile',{}).get('test') and record.get('executable'): + found.append(record['executable']) + require(len(found)==1,'one linked unit executable '+label) + relative=Path(found[0]).relative_to('/target') + artifact=copy_artifact(cache/relative,output/(label+'-tests')) + listing=run(folder,source,cache,'list-'+label,[str(Path('/target')/relative),'--list','--format','terse']).decode() + tests=[line[:-6] for line in listing.splitlines() if line.endswith(': test')] + require(tests and len(set(tests))==len(tests),'complete unique native test names') + allowed=SAFE_FILTERS.get(label,[]) + if label=='handoff':allowed=tests + require(set(allowed)<=set(tests),'exact safe filter exists') + artifact.update(all_tests=tests,allowed_native_tests=allowed, + other_tests_not_authorized_by_this_pack=True, + safety='Pure handoff/codec or own temporary portable SQLite fixture only; no product service/database/keys.' if allowed else 'Linked only; no VM execution authorized.') + units[label]=artifact + for index,name in enumerate(allowed): + run(folder,source,cache,'focused-'+label+'-'+str(index),[str(Path('/target')/relative),'--exact',name,'--nocapture']) + require(capture(source,names)==inputs,'frozen source changed during unit qualification') + require(results['source0']['agent']['sha256']!=results['source1']['agent']['sha256'] + and results['source0']['helper']['sha256']!=results['source1']['helper']['sha256'],'real new codepair bytes required') + manifest=dict(schema_version=1,case_id=ident,profile='linux_compatible_updater_buildpack_v1', + builder=BUILDER,network='none',source_mount='read_only',cache=str(cache), + source0_version='0.1.0',source1_version='0.1.1',changed_source1_files=['Cargo.toml','Cargo.lock'], + inherited_packages=internal,external_dependencies_unchanged=True, + source0_inputs_sha256=sha((folder/'source0-inputs.json').read_bytes()), + source1_inputs_sha256=sha((folder/'source1-inputs.json').read_bytes()), + source_files_count=len(names),artifacts=results,units=units, + stable_updater=results['source0']['updater'],checks=dict(linux_source0_clippy=True,linux_source1_clippy=True, + release_links=True,exact_test_lists=True,focused_local_contracts=True,frozen_inputs_after_build=True), + windows_copied_only_not_qualified=True,native_vm_tested=False, + bootstrap_installed=False,apply_executed=False,keys_generated=False,approvals_performed=False) + write(folder/'manifest.json',encode(manifest)) + for version in ('source0','source1'): + archive=folder/(version+'.tar.gz') + with tarfile.open(archive,'w:gz') as output: + for name in names:output.add(folder/version/name,arcname=name,recursive=False) + archive.chmod(0o400) + write(folder/'archive-hashes.json',encode({version:sha((folder/(version+'.tar.gz')).read_bytes()) for version in ('source0','source1')})) + write(folder/'completed.json',encode(dict(manifest_sha256=sha((folder/'manifest.json').read_bytes()),native_vm_tested=False))) + print(json.dumps(dict(completed=True,manifest=str(folder/'manifest.json'),manifest_sha256=sha((folder/'manifest.json').read_bytes()),artifacts=results)),flush=True) + +def main(): + parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=['build']) + parser.parse_args();build() +if __name__=='__main__':main() diff --git a/deploy/updater_server_signing_bootstrap.py b/deploy/updater_server_signing_bootstrap.py new file mode 100644 index 0000000..92647f8 --- /dev/null +++ b/deploy/updater_server_signing_bootstrap.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +"""Explicit server-only release/baseline signing bootstrap; never installs policy. + +Private release and Health keys remain on the server. No job/time key reuse, +service restart, catalog mutation, grant or approval is performed by this tool. +Prepared source: mutation phases require coordination by the owning Root task. +""" +import argparse +import base64 +import json +from pathlib import Path +import linux_updater_compatible_apply as bootstrap + +def server(payload): + import base64,datetime,hashlib,json,os,stat,subprocess,uuid + from pathlib import Path + require(os.geteuid()==0 and os.uname().machine=='aarch64','fixed Root ARM64 server') + root=Path('/opt/rust-discovery/devstate');platform=payload.get('platform','linux');require(platform in('linux','windows'),'closed release key platform');release_dir=root/('agent-update-release-'+platform+'-v1');health_dir=root/'agent-update-health-v1' + release_key=release_dir/'release.pem';health_key=health_dir/'health.pem';trust_dir=root/('agent-update-public-'+platform+'-v1') + ident=str(uuid.UUID(payload['case_id']));case=root/('updater-signing-'+ident) + def digest(raw):return hashlib.sha256(raw).hexdigest() + def encode(value):return json.dumps(value,separators=(',',':'),ensure_ascii=False).encode() + def run(args):return subprocess.check_output(args,stderr=subprocess.DEVNULL,timeout=10) + def syncdir(path): + fd=os.open(path,os.O_RDONLY|os.O_DIRECTORY) + try:os.fsync(fd) + finally:os.close(fd) + def metadata(path): + m=path.lstat();require(stat.S_ISREG(m.st_mode)and m.st_nlink==1 and stat.S_IMODE(m.st_mode)==0o600,'single protected key file');return [m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_nlink,m.st_size,m.st_mtime_ns,m.st_ctime_ns] + def public(key): + raw=run(['openssl','pkey','-in',str(key),'-pubout','-outform','DER']);require(len(raw)==44 and raw[:12]==bytes.fromhex('302a300506032b6570032100'),'Ed25519 public DER');return raw[12:] + def store(name,value): + path=case/name + with path.open('xb')as stream:stream.write(encode(value));stream.flush();os.fsync(stream.fileno()) + syncdir(case) + def b64(raw):return base64.urlsafe_b64encode(raw).rstrip(b'=').decode() + def decode(text): + raw=base64.urlsafe_b64decode(text+'='*((-len(text))%4));require(b64(raw)==text,'canonical Base64url');return raw + if payload['action']=='public-separation': + pin=Path('/etc/rmm-time-source/time-public-key.json');before=metadata(pin);require(before[2]==0 and before[6]<=8192,'original Root public time pin');raw=pin.read_bytes();require(metadata(pin)==before,'public time pin unchanged') + time_key=json.loads(raw)['public_key_hex'];job_key=payload['job_public_key_hex'];require(all(len(v)==64 and set(v)<=set('0123456789abcdef')for v in(time_key,job_key))and time_key!=job_key,'independent actual Job/time public keys') + return {'operation':'updater_signer_public_separation_v1','forbidden_public_key_sha256':sorted([digest(bytes.fromhex(v))for v in(time_key,job_key)]),'time_public_pin_sha256':digest(raw),'private_key_read':False,'services_changed':False,'job_authority':False} + if payload['action']=='provision': + forbidden=payload['forbidden_public_key_sha256'];require(len(forbidden)>=2 and len(forbidden)<=16 and len(set(forbidden))==len(forbidden)and all(len(v)==64 and set(v)<=set('0123456789abcdef')for v in forbidden),'independent Job/time public-key SHA256 values required, never role aliases') + require(not release_dir.exists()and not trust_dir.exists()and not case.exists(),'create-only independent release signer namespace') + owner=(root/'jobs/ed25519.pem').lstat().st_uid;require(owner==10001,'actual existing signer owner contract') + case.mkdir(mode=0o700);store('provision-intent.json',{'release_dir':str(release_dir),'health_dir':str(health_dir),'forbidden_public_key_sha256':forbidden,'job_authority':False}) + health_origin=health_dir/'public-origin.json' + reuse_health=health_dir.exists() + if reuse_health: + directory=health_dir.lstat();origin_meta=health_origin.lstat();require(stat.S_ISDIR(directory.st_mode)and directory.st_uid==owner and stat.S_IMODE(directory.st_mode)==0o700 and stat.S_ISREG(origin_meta.st_mode)and origin_meta.st_uid==0 and origin_meta.st_nlink==1 and stat.S_IMODE(origin_meta.st_mode)==0o600 and origin_meta.st_size<=4096,'original protected shared Health provenance') + origin=json.loads(health_origin.read_text());require(origin['profile']=='shared_agent_update_health_v1'and metadata(health_key)==origin['private_metadata']and public(health_key).hex()==origin['public_key_hex'],'exact original shared Health key') + for directory,key,uid in [(release_dir,release_key,0)]+([]if reuse_health else[(health_dir,health_key,owner)]): + directory.mkdir(mode=0o700);fd=os.open(key,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600);os.close(fd) + run(['openssl','genpkey','-algorithm','Ed25519','-out',str(key)]);os.chmod(key,0o600);os.chown(key,uid,uid);os.chown(directory,uid,uid) + fd=os.open(key,os.O_RDONLY|os.O_NOFOLLOW) + try:os.fsync(fd) + finally:os.close(fd) + syncdir(directory);syncdir(root) + release=public(release_key);health=public(health_key);require(release!=health and digest(release)not in forbidden and digest(health)not in forbidden,'independent key domains') + if not reuse_health: + with health_origin.open('x')as stream:json.dump({'profile':'shared_agent_update_health_v1','public_key_hex':health.hex(),'private_metadata':metadata(health_key)},stream,sort_keys=True);stream.flush();os.fsync(stream.fileno()) + health_origin.chmod(0o600) + syncdir(health_dir) + other=root/('agent-update-release-'+('windows'if platform=='linux'else'linux')+'-v1/release-public.json') + if other.exists(): + other_meta=other.lstat();require(stat.S_ISREG(other_meta.st_mode)and other_meta.st_uid==0 and stat.S_IMODE(other_meta.st_mode)==0o600 and other_meta.st_nlink==1 and other_meta.st_size<=4096,'other platform original public provenance');require(json.loads(other.read_text())['public_key_hex']!=release.hex(),'distinct platform release keys') + with(release_dir/'release-public.json').open('x')as stream:json.dump({'platform':platform,'public_key_hex':release.hex(),'key_id':digest(release)},stream,sort_keys=True);stream.flush();os.fsync(stream.fileno()) + os.chmod(release_dir/'release-public.json',0o600) + syncdir(release_dir) + keyring=encode({'schema_version':1,'generation':1,'keys':[{'public_key_hex':release.hex(),'platform':'x86_64-unknown-linux-gnu'if platform=='linux'else'x86_64-pc-windows-msvc','first_sequence':1,'last_sequence':2,'revoked':False}]}) + trust_dir.mkdir(mode=0o700);os.chown(trust_dir,owner,owner) + fd=os.open(trust_dir/'keyring.json',os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600) + with os.fdopen(fd,'wb')as stream:os.fchown(stream.fileno(),owner,owner);stream.write(keyring);stream.flush();os.fsync(stream.fileno()) + syncdir(trust_dir);syncdir(root) + result={'schema_version':1,'profile':'agent_updater_separate_signers_v1','platform':platform,'shared_health_key_reused':reuse_health,'case_id':ident,'release_key_id':digest(release),'release_public_key_hex':release.hex(),'keyring_base64url':b64(keyring),'keyring_sha256':digest(keyring),'public_keyring_host_path':str(trust_dir/'keyring.json'),'public_keyring_metadata':metadata(trust_dir/'keyring.json'),'keyring_recipient_roles':['api','gateway'],'health_pin':{'schema_version':1,'key_id':digest(health),'public_key_hex':health.hex()},'release_private_metadata':metadata(release_key),'health_private_metadata':metadata(health_key),'health_recipient_role':'gateway','health_recipient_uid':owner,'gateway_mount_pending':True,'services_changed':False,'job_authority':False} + store('provision-result.json',result);return result + require(case.is_dir(),'existing explicit signer case') + provision=json.loads((case/'provision-result.json').read_text());require(provision['case_id']==ident and provision['platform']==platform and public(release_key).hex()==provision['release_public_key_hex']and public(health_key).hex()==provision['health_pin']['public_key_hex'],'original independent keys') + require(metadata(release_key)==provision['release_private_metadata']and metadata(health_key)==provision['health_private_metadata'],'protected private key metadata unchanged') + require(metadata(trust_dir/'keyring.json')==provision['public_keyring_metadata']and digest((trust_dir/'keyring.json').read_bytes())==provision['keyring_sha256'],'original protected public keyring') + if payload['action']=='readback':return provision + require(payload['action']in('sign-release','sign-baseline'),'fixed signing purpose only') + require(platform=='linux','Windows signing awaits a separately qualified actual MSVC source pair; never relabel GNU') + raw=decode(payload['canonical_payload_base64url']);require(len(raw)<=32768 and digest(raw)==payload['payload_sha256'],'exact typed public payload') + doc=json.loads(raw) + artifacts={1:[('7fef8d0f1e2bcf5cd6c373e567a0c17bfb08d8c780cd4d1925b6c1355e572547',16275360),('8fdc6ded02278eb18e64003d0bc7bf45d452cd48ded0450e6e900ca66e44d449',13325104)],2:[('4d212066bef226af3fa679ffe95f69c43a9668cbef7e665d53328f4e41aa3799',16273896),('89b28db3afe870c667297dff97ad7a042c550ccce1f712d69108e8de672ad450',13322944)]} + if payload['action']=='sign-release': + sequence=doc['release_sequence'];require(sequence in(1,2)and doc['schema_version']==1 and doc['version']==('0.1.0'if sequence==1 else'0.1.1')and doc['platform']=='x86_64-unknown-linux-gnu'and doc['protocol_major']==1 and doc['state_schema']==1 and doc['minimum_updater_version']==1,'exact compatible release profile') + require(str(uuid.UUID(doc['release_id']))==doc['release_id']and doc['release_id']!='00000000-0000-0000-0000-000000000000','release identity') + require(abs(int(datetime.datetime.now(datetime.timezone.utc).timestamp())-doc['published_at'])<300 and doc['expires_at']-doc['published_at']==604800,'bounded initial release dates') + require(len(doc['artifacts'])==2 and [a['role']for a in doc['artifacts']]==['agent','helper']and [(a['sha256'],a['bytes'])for a in doc['artifacts']]==artifacts[sequence],'exact frozen source artifact hashes') + ids=[str(uuid.UUID(a['artifact_id']))for a in doc['artifacts']];require(len(set(ids))==2 and all(v!='00000000-0000-0000-0000-000000000000'for v in ids),'distinct artifact identities') + prefix=b'rmm.agent-release.v1\0';name='release-'+str(sequence) + else: + require(doc['schema_version']==1 and doc['profile']=='decoder_compatible_handoff_v1'and doc['platform']=='x86_64-unknown-linux-gnu'and doc['activation_floor']==1 and doc['previous_record_sha256']is None and doc['rolled_back']is False,'initial compatible baseline only') + prior=json.loads((case/'release-1-result.json').read_text());require(doc['release_envelope']==prior['envelope']and doc['manifest_sha256']==prior['payload_sha256'],'exact original signed baseline release') + qualification=decode(payload['qualification_base64url']);q=json.loads(qualification) + require(q['profile']=='linux_compatible_baseline_qualification_v1'and q['normal_signed_sync_observed']and q['ready']is False and q['helper_ready']is False and q['unlock']is False and q['job_authority']is False,'independent actual compatible qualification, never a lease') + require([s['service']for s in q['services']]==['agent','general','full']and [s['process']['image_sha256']for s in q['services']]==[artifacts[1][0][0],artifacts[1][1][0],artifacts[1][1][0]],'actual compatible initial code pair') + require(doc['activation_evidence']=={'kind':'bootstrap','qualification_receipt_sha256':digest(qualification)},'exact public qualification hash') + observed=datetime.datetime.fromisoformat(q['observed_at'].replace('Z','+00:00'));require(observed.tzinfo is not None and int(observed.timestamp())==doc['activated_at'],'actual baseline activation time') + prefix=b'rmm.agent-update-baseline.v1\0';name='baseline' + require(not(case/(name+'-intent.json')).exists(),'existing signing intent means readback only') + store(name+'-intent.json',{'payload_sha256':digest(raw),'purpose':payload['action'],'job_authority':False}) + message=case/(name+'.message');signature=case/(name+'.signature') + with message.open('xb')as stream:stream.write(prefix+raw);stream.flush();os.fsync(stream.fileno()) + run(['openssl','pkeyutl','-sign','-rawin','-inkey',str(release_key),'-in',str(message),'-out',str(signature)]) + signed=signature.read_bytes();require(len(signed)==64,'Ed25519 signature') + public_der=case/(name+'.public.der') + with public_der.open('xb')as stream:stream.write(bytes.fromhex('302a300506032b6570032100')+bytes.fromhex(provision['release_public_key_hex']));stream.flush();os.fsync(stream.fileno()) + run(['openssl','pkeyutl','-verify','-rawin','-pubin','-keyform','DER','-inkey',str(public_der),'-in',str(message),'-sigfile',str(signature)]) + result={'schema_version':1,'key_id':provision['release_key_id'],'payload_sha256':digest(raw),'envelope':{'schema_version':1,'key_id':provision['release_key_id'],'payload_bytes':b64(raw),'signature':b64(signed)},'job_authority':False} + store(name+'-result.json',result);return result + +def main(): + parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('public-separation','provision','readback','sign-release','sign-baseline'));parser.add_argument('--input',type=Path,required=True);args=parser.parse_args() + p=json.loads(args.input.read_text());p['action']=args.action + print(json.dumps(bootstrap.native_rpc(server,p,config='server.conf'))) +if __name__=='__main__':main() diff --git a/docs/agent-updater-runtime-source.md b/docs/agent-updater-runtime-source.md index 622b54d..0304129 100644 --- a/docs/agent-updater-runtime-source.md +++ b/docs/agent-updater-runtime-source.md @@ -160,3 +160,93 @@ Ein eigener Runner ist ausschließlich für zwei eigene native Prozessfixtures vorbereitet; er ist noch nicht ausgeführt. Details, frühere fehlgeschlagene Fixtureprüfung und verbleibende MVP-Gates stehen im [Readercensusnachweis](agent-updater-reader-census-results.md). + +## Quellenfortschreibung 2026-11-16: Full-Admission und stabiler Linux-Daemon + +Die zuvor fehlende Quellverbindung enthält jetzt einen getrennten festen +`rmm-agent-updater --serve`-Einstieg und eine Systemd-Vorlage. Der Dienst erhält +keine Jobfreigabe-API. Die vorhandene Full-Helperstrecke prüft die vollständig +vorab geladenen Agent-/Helperdateien unabhängig vom Netzwerkagenten und legt +vier exakt gebundene Root-Stages an. Erst danach speichert die bestehende +HelperSync-Transaktion unter derselben Original-Executorsperre Consumption, +Startzeuge, Handoff, Delegation und StopIntent atomar. Die Antwort +`agent_update_handoff_accepted_v1` ist ausdrücklich kein Outcome und enthält +`ready=false` und `terminal=false`. Der Helper antwortet zuerst dem Agenten; +erst anschließend aktiviert er einmal den tatsächlich gebundenen Updater. +Die Cloud-VM wurde dadurch noch nicht aktualisiert. + +Die Quellstrecke erhält auch den aktuellen TLS-Selektor und die tatsächlich +aktive Zertifikatsversion anhand von Dateimetadaten. Sie liest dabei keine +privaten Schlüsselbytes. Ein fester, descriptor-relativ geöffneter +Agent-Cachepfad bleibt auf UID 993 und den bestehenden privaten Agent-Statebaum +beschränkt; die Root-Installationsverzeichnisse behalten ihre eigene strenge +Root-Pfadauflösung. + +Der zusätzliche Full-Aufruf `update_observe_v1` ist ausschließlich lesend. +Er prüft die tatsächliche HelperSync-Lease, Baseline/Release-Trust, Historie, +Floor, feste Dienstgenerationen, Readerregistry und den laufenden Updater. +Ein optionaler Kandidat wird als ursprünglicher ReleaseEnvelope mit eigener +Keyringgeneration entgegengenommen und unabhängig geprüft. Erst danach rendert +Full die genaue Kandidaten-Konfiguration und den Pin für die spätere +WebUI-Anzeige. Katalogmetadaten allein liefern keinen CandidatePreview. Der +Aufruf lädt keine Binärdateien, legt keine Stage an und schreibt keine +Start-/Handoff-/Ready-Zeile. + +Der V2-Rückweg ist im Linux-Worker für den konkret unterscheidbaren Fall eines +nach einmal bestätigt dispatchtem Start gescheiterten neuen Full-Helfers +verbunden. Ein vorher im ersten LedgerReturnIntent reservierter bedingter Stop +beendet ausschließlich die tatsächlich gehaltene neue General-Generation. +Agent/Full ohne erfolgreichen Start erhalten getrennte negative Belege und +keine erfundenen erfolgreichen Stops. Erst nach beendetem Reader, beiden +ursprünglichen Sperren und erneutem vollständigen FD-/maps-Census öffnet der +Updater wieder das Originalledger. Die vier Dateien werden in Gegenrichtung +über getrennte create-once Intents zurückgeführt; Kandidat und Vorgänger bleiben +erhalten. Ein tatsächlich neu gestarteter Vorgänger-Full muss die ursprüngliche +signierte Healthstrecke erneut bestehen. Sein abschließendes Updateergebnis ist +`failed / AGENT_UPDATE_RETURNED_TO_PREDECESSOR`; der ursprüngliche aktive Record +und Floor werden nicht neu publiziert. Verlorene Antworten, unklare Renames, +automatischer Restart und nicht nachgewiesene Dienstzustände bleiben gesperrt. +Andere Startup-/Healthfehlerpfade sind durch diese Quellenfortschreibung noch +nicht als real ausgeführte Wiederherstellung qualifiziert. + +Bei der Verbindung wurde ein konkreter Dienstbeobachtungsfehler korrigiert: +der Systemd-Aufruf fragt sieben Eigenschaften ab, der bisherige Parser verlangte +acht. Der Parser verlangt jetzt genau die tatsächlich angefragten sieben Namen +und verweigert fehlende, zusätzliche und doppelte Eigenschaften. + +Lokal tatsächlich ausgeführt: Helperbibliothek 42/42 bestanden, einschließlich +des neuen typisierten Handoff-Acknowledgement-Gegenfalls. Der erste breite Lauf +hatte sechs APT-Fixturefehler wegen des fehlenden privaten Verzeichnisses +`/tmp/opencode`; mit diesem Fixtureverzeichnis bestanden alle 42. Ein erster +Updater-Linux-Lauf bestand 5/7 und scheiterte bei zwei Dateifixtures am absichtlich +schreibgeschützten Container-`/run`. Diese Fehlversuche sind kein nativer +Zielnachweis. Der weitere Quellencheck und die getrennte reale Linux-first- +Abnahme werden separat ergänzt. Keine Applycapability und kein erfolgreicher +Cloud-Apply werden aus diesen lokalen Ergebnissen abgeleitet. + +Mit privaten Container-Tmpfs für `/tmp`, `/run` und `/var/lib` bestand anschließend +der vollständige betroffene Lauf: Helper 42, Updater-Linux 7, Worker 12, insgesamt +61 bestanden, 0 Fehler, 0 ignoriert. Zuvor scheiterte zusätzlich ein Workerfixture am +schreibgeschützten Container-`/var/lib`; auch diese Ursache wurde durch die +korrekte eigene Fixtureumgebung beseitigt. Der abschließende all-targets-Clippy- +Lauf für Helper und Worker mit `-D warnings` und `git diff --check` bestand. +Diese Tests enthalten ausdrücklich Containerfixtures mit festen privaten +Statepfaden; die vollständigen Testbinaries dürfen deshalb nicht gegen die +bestehenden installierten Statepfade der Cloud-VM gestartet werden. Der reine +Sieben-Eigenschaften-Parsertest benötigt keine Systemd-/Dateieffekte. + +Die zusätzliche ursprüngliche Updater-Zeitgeneration und der tatsächlich +angewandte Pre-Stop-Syncbeleg sind separat in +[Originalzeitgeneration](updater-original-time-generation.md) beschrieben. +Auch diese Quellverbindung ist noch keine reale Applyabnahme. + +Am 16. November wurde die öffentliche deterministische Recordzusammensetzung +in `rmm-domain::agent_update_provisioning::assemble_updated_record` gemeinsam +genutzt. Sie liefert ausschließlich bereits öffentliche Recordmetadaten; +die privaten nativen BuiltRecord-Konstruktoren behalten ihre unabhängigen +Original-/Healthsignatur- und Guardprüfungen. Der Linux-Namespacevertrag +bleibt unverändert. Tatsächliche isolierte Nachprüfung: Agent und Worker +mit allen Targets/Clippy `-D warnings` bestanden; beide bestehenden +Record-Namespaceprüfungen (Originalinode/Archive sowie Ablehnung fremder +Dateiidentitäten) bestanden, 2/2, keine ignoriert. Dafür wurde ausschließlich +der private Container-`/run`-Scratch verwendet, keine VM-Datei verändert. diff --git a/docs/linux-updater-compatible-bootstrap-plan.md b/docs/linux-updater-compatible-bootstrap-plan.md new file mode 100644 index 0000000..21f6f2b --- /dev/null +++ b/docs/linux-updater-compatible-bootstrap-plan.md @@ -0,0 +1,237 @@ +# Linux-Updater: erhaltender Bootstrap und erste reale Abnahme + +Stand 16. November 2026. **Plan und tatsächliche lesende Vorprüfung; noch keine +Installation, kein Dienstwechsel und kein Apply.** Der koordinierende +Rootprozess führt Zieländerungen und WebUI-Abnahmen durch. Die neue Datei +`deploy/linux_updater_bootstrap.py` besitzt ausschließlich `inspect` und lokale +Paketvorbereitung `prepare`; sie implementiert keinen versteckten Applymodus. + +## Tatsächlich gelesen + +`python3 deploy/linux_updater_bootstrap.py inspect` las die bestehende +Cloud-Linux-VM über den vorhandenen streng geparsten SSH-Zugang mit bestätigtem +Hostschlüssel. Der private Originalbeleg liegt in +`devstate/linux-updater-bootstrap/eb9ad00e-0229-4ebf-8ba4-09861bb246c5/read-only.json`. +Boot: `45aba743-be1d-4ae5-ba00-139fb9363bfe`. + +| Gegenstand | Tatsächlicher Zustand | +|---|---| +| Agent | aktiv/enabled, SHA256 `0a7ad6eefaefda79ee80b6a41b2df904e9e7f50ae6ebeed01b98c25b6ce5c06f` | +| General/Full | beide aktiv, gemeinsame Binary `79442727c3ba31cda02a8bc0e67481f14db9f39fc07520a880a90b64f10f1eaf`; General enabled, Full disabled | +| Updater | Unit, Binary, Konfigurations- und Stateverzeichnis fehlen | +| APT/Legacy | inaktiv/disabled, keine beobachteten General-Rollbacktimer | +| Agentjournal | 30 Aufträge, kein offener Auftrag, kein Fault, Integrität ok | +| Agentverbrauch | 27 Verbräuche und 27 Startzeugen, kein offener Verbrauch | +| Rootverbrauch | 19 Verbräuche und 19 Startzeugen, kein offener Verbrauch/Fault | + +Die Root-Evidenzaggregate blieben lesend gebunden an +`def53ce39f60ebe6815352df2ef1980d5170569519bbca0a9e132d3eecb470d1`, +die Agent-Evidenz an +`61e54815aecb8b5625ff7a8e5b09a5a509ba39d23f3682ad5c969bcb93ff7836`. +Dies sind Hashes der auf dem Ziel gehashten Originalstart-/Outcomebytes mit +Grantbezug, kein Export von Jobausgaben. Die Vorprüfung exportiert ausschließlich +öffentliche Konfigurationsfelder, feste Unit-/Binärhashes und Dateimetadaten. +Private Schlüssel und Agentumgebungsdatei wurden ausschließlich `stat`-geprüft. +Die vorhandenen Datenbanken wurden read-only im tatsächlich vorhandenen +DELETE-Journalmodus geöffnet; es entstanden keine WAL-/SHM-Sidecars. + +Der erste lesende Versuch stoppte beim Stabilitätsvergleich einer öffentlichen +Datei, weil der Vergleich auch die durch Lesen veränderliche atime einschloss. +Die Korrektur vergleicht Identität, Größe, mtime und ctime; der nächste vollständige +Read bestand. Der TLS-Selektor wird anhand seines tatsächlichen Felds `version` +behandelt. Kein fehlgeschlagener Read wurde als Bootstraperfolg ausgegeben. + +## Verbindliche Reihenfolge + +Die bisherigen Images sind kein geeigneter Rückfall für den neuen Updater: +ihnen fehlen Decoder-/Owner-/Healthteile. Deshalb zunächst **kompatible neue +Baseline**, erst anschließend ein separat genehmigtes Update auf andere Bytes. +„Release 0“ bezeichnet unten die Baselinegeneration mit positiver Sequenz 1; +Sequenz 0 ist im Releasecodec ungültig. + +1. Den gemeinsamen Quellsatz einfrieren. Agent, Helper und stabilen Updater für + Linux x86-64 bauen, genaue Quellen-/Listen-/Hashbelege aufbewahren und die + dafür vorgesehenen nicht destruktiven Vertragsprüfungen zuerst auf der + tatsächlichen Cloud-Linux-VM ausführen. Containerfixtures, die feste vorhandene + Agent-Statepfade voraussetzen, dort ausdrücklich nicht starten. +2. Einen frischen Ziel-CAS und den zentral geschlossenen Devtestscope prüfen. + Agent, General und Full in einem begrenzten administrativen Dienstwechsel + stoppen; APT/Legacy bleiben geschlossen. Beide ursprünglichen Sperren halten. + DB-Dateien, Identität, PKI und sämtliche Originalzeilen/Outcomebytes erhalten. + Alte Binaries create-only als historische Bootstrapbelege aufbewahren. +3. Die kompatiblen neuen Agent-/Helperbytes an den bisherigen festen Zielpfaden + bereitstellen und den Full-Konfigurations-/Policy-/Binärpin auf genau diese + Helperbytes binden. Generalpolicy und Generalpin bleiben erhalten. Keine + APT-Rezept-/Proofanpassung und kein Paketjob. Eine geänderte gemeinsame + Helperbinary ist **kein** neuer APT-Laufzeitqualifikationsnachweis. +4. General, Full und Agent genau einmal starten, tatsächliche neue PID/Birth/ + Image-/Unitzeugen lesen, normale signierte Agent-/Helpersynchronisierung und + passives Inventar nachweisen. Erst daraus den öffentlichen tatsächlichen + Baselinequalifikationsbeleg bilden; er enthält keinen Hash einer noch zu + erzeugenden Baseline/Registry/Policy, sodass keine Hashzirkularität entsteht. +5. Auf dem Server getrennte Release-/Baselinesignier- und Healthschlüssel + erzeugen. Keine privaten Schlüssel verlassen den Server. Der Releasekey + bleibt Root vorbehalten; der separate Healthkey wird ausschließlich dem + vorhandenen unprivilegierten Gateway-Dienst als geschützte PEM-Datei zugänglich + gemacht. Der tatsächliche Handler `/agent/v1/update-health` läuft im Gateway. + Job- und Zeitsignierer werden nicht wiederverwendet. +6. Release-0-Manifest exakt für die kompatibel installierten Agent-/Helperbytes + signieren, daraus den kanonischen Initialrecord mit Sequenz/Floor 1, + tatsächlicher Aktivierungszeit, Bootstrapqualifikationshash und + `rolled_back=false` bilden und separat signieren. Identität und bestehende + Journale werden nicht durch diese Recorddatei ersetzt. +7. Die neue feste Updaterunit installieren, reale Unit-Inodes/Kompositionen + nachlesen und die vollständige 21-Unit-Registry plus tatsächliche etwaige + Rollbacktimer erzeugen. Geschützte Registry enthält den Baselinebeleg und + den unveränderten Readervertrag; dazu die tatsächlichen PID-/User-/Cgroup- + Namespace-/Procidentitäten. Erst dann Registryhash und signierte Baseline + in der lokalen Policy binden. Keine Unit-Pins aus einem Quelltext statt der + tatsächlich installierten Dateien übernehmen. +8. Den Updater einmal als eigenes Root-Systemd-Programm starten; sein eigener + PID/Birth/Image/Unitbeleg muss passen. Danach den Full-Aufruf + `update_observe_v1` als tatsächlicher UID-993-Agent prüfen. Nur daraus + gültige Apply-/Checkcapabilities und CandidatePreview veröffentlichen. +9. Release 1 als tatsächlich neue Codepaarbytes bauen, beispielsweise aus + demselben eingefrorenen Runtimequellsatz mit sauberer Version 0.1.1 und + eigener nachvollziehbarer Buildprovenienz. Der bereits gebundene stabile + Updater bleibt unverändert. Full rendert den neuen Konfigurations-/Pinwechsel + aus dem unabhängig geprüften Kandidaten. Veröffentlichung im bestehenden + Tenantkatalog erzeugt noch keinen ausführbaren Job. +10. MCP muss einen persistenten `pending_approval`-Plan für genau diese Bytes + erstellen. Der tatsächliche sichtbar simulierte Devtest-WebUIprincipal + genehmigt interaktiv. Vorher keine Jobwirkung oder Startgrants. Danach + genau einen ursprünglichen Verbrauch, Staging, Stop/Lock/Census/Swap/Start, + neuen unabhängigen signierten Sync-/Baseinventar-Healthbeleg und endgültigen + Originalrecord-/Floor-/Outcomestand prüfen. Nach diesem Abnahmeschritt + Dokumentation und Code nach Gitea pushen. + +## Pfade und Berechtigungen + +| Pfad | Geplante Bindung | +|---|---| +| `/usr/local/libexec/rmm-agent-updater` | qualifiziertes stabiles ELF, Root:Root, 0755; nie Teil des Job-Codepaars | +| `/etc/systemd/system/rmm-agent-updater.service` | feste geprüfte Rootunit, 0644, kein automatischer Wiederanlauf eines unsicheren Handoffs | +| `/etc/rmm-agent-updater` | Root:Root, 0755; keine Schreibrechte für Agent/Gruppe/Andere | +| `binding.json` dort | vorhandenes öffentliches Binding, Root:Root 0600 | +| `policy.json`, `keyring.json`, `health-key.json` dort | öffentliche geschützte Trustmetadaten, Root:Root 0644 | +| `reader-registry.json`, `retained-files.json` dort | feste lokal provisionierte tatsächliche Belege; Root besitzt die Schreibautorität | +| `/var/lib/rmm-agent-updater` | ursprünglicher privater Record-FS-Vertrag: Root:Root 0700 | +| `installed-record.json`, Recordarchive/Handoffs | private Rootdateien/-verzeichnisse, 0600/0700; keine öffentliche Projektion | +| Agentcache | fester vorhandener UID-993-Statebaum; neue Unterverzeichnisse 0700, Prefetchdateien 0600 | + +Der Agent erhält die **öffentlichen Recordbytes** über dieselbe authentisierte +Full-Antwort wie den aktuellen gemessenen Zustand. Hash, Binding und exakte +Predecessor-/Floorwerte werden geprüft. Der bestehende private Recordport wird +hierfür nicht auf öffentliche Dateirechte umgestellt. Full schreibt dabei nichts. + +Die Retainedliste bindet mindestens die drei bestehenden DB-Dateien, zwei +ursprünglichen Locks, öffentliche Binding-/Identitäts-/Jobkeydateien, die +Original-PKI-Dateien sowie den tatsächlichen TLS-Selektor und die aktuell aktive +TLS-Version. Für Schlüssel ausschließlich Dateimetadaten, nie Schlüsselbytes. +Unit-/Binär-/Trustdateien erhalten ihre getrennten eigenen Hash-/FD-Pins. + +## Bekannter kontrollierter Fehlerfall + +Der vorbereitete Linuxrückweg qualifiziert zunächst einen tatsächlich gescheiterten +neuen Fullstart nach bereits erfolgreichem Generalstart. Ein allgemeines +`exit 1`-Helperfixture genügt dafür nicht: dieselbe Binary bedient beide Rollen +und würde bereits General scheitern lassen. Ein separater privater, signierter +Fixturequellsatz muss General normal bedienen und ausschließlich den neuen +Fullstart vor Arbeitsaufnahme eindeutig beenden; kein Produktions-Failureflag +oder versteckter ausführbarer Sonderkanal wird dafür eingeführt. + +Die derzeitige Fullunit hat `Restart=on-failure` mit 5 Sekunden. Ein solcher +weiter geplanter automatischer Wiederanlauf ist kein bekannter geschlossener +Fehler. Für diesen Test muss bereits **vor Baseline/Registry** ein ausdrücklich +geprüfter `Restart=no`-Full-Override zum qualifizierten kompatiblen Setup gehören, +oder ein tatsächlich terminaler Startlimitfall unabhängig bewiesen sein. +Eine nachträgliche ungepinnte Unitänderung während des Updatejobs ist verboten. +Die tatsächlichen negativen Start-/Stopbelege, Rückführung beider Codeversionen, +Originalfristen sowie frisch signierte Vorgänger-Health müssen anschließend +nativ bestehen. Unklare Rename-/Socket-/Startantworten bleiben Unknown und +werden weder wiederholt noch als bekannte Rückführung ausgegeben. + +## Stand der Ausführung + +Tatsächlich abgeschlossen sind der lesende Zielbeleg, lokale +Python-Syntaxprüfung der Vorbereitung und das unten abgegrenzte lokale Buildpack. +Quell- und Containerprüfungen der Runtime +stehen separat in [Runtimequellen](agent-updater-runtime-source.md). Bootstrap, +Schlüsselprovisionierung, neuer Dienststart, passiver qualifizierter Fullbeleg, +WebUI-genehmigter Apply und native Rückführung sind **geplant, nicht ausgeführt**. + +## Eingefrorenes Linux-Buildpack, tatsächlich lokal gebaut + +`deploy/linux_updater_qualification.py build` erstellte den unveränderten Fall +`devstate/linux-updater-qualification/21d20391-6102-4996-8a7a-efbd3e78bd7d`. +615 öffentliche Eingabedateien wurden mit vorherigem/nachherigem Hashvergleich +eingefroren; Zugangsdaten, Devstate und Schlüssel gehören nicht zum Paket. +Source0 verwendet Workspaceversion 0.1.0. Source1 unterscheidet sich ausschließlich +in `Cargo.toml` und den erbenden internen Paketversionen in `Cargo.lock` (0.1.1). +Externe Abhängigkeiten bleiben exakt gleich. Windowsquellen wurden ausschließlich +als Provenienz mitkopiert; dieser Linuxlauf qualifiziert sie nicht. + +Beide Quellsätze bestanden `cargo clippy --locked --offline` mit `--all-targets +-- -D warnings` für Agent, Agentcore, Domain, Helper, Linuxupdater, Worker, +Handoff und Updatehistory. Source0 linkte Agent, Helper und stabilen Updater als +Release; Source1 Agent und Helper. Beide neuen Paarbytes unterscheiden sich +tatsächlich von Source0. Der stabile Updater wird ausschließlich aus Source0 +verwendet. Alle Befehle liefen ohne Netzwerk in Docker mit schreibgeschütztem +Sourcebaum, eigenem Targetcache und gepinntem Builder +`sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6`. + +| Artefakt unter `artifacts/` im Fall | SHA-256 | +|---|---| +| `source0/rmm-agent` | `61a073f44be95a9e2a9edf816104fa198452f9512bc099b59b4931000fc98b1e` | +| `source0/rmm-privileged-helper` | `8fdc6ded02278eb18e64003d0bc7bf45d452cd48ded0450e6e900ca66e44d449` | +| `source0/rmm-agent-updater` | `dec6336728eac8af1e0d84d4921c7c9ecbcc45bc4619d357d346b716336be90a` | +| `source1/rmm-agent` | `c27319f312294f96583a2ecb14c9b1ed0b54e205c70dcd156ae511a1dc254e63` | +| `source1/rmm-privileged-helper` | `89b28db3afe870c667297dff97ad7a042c550ccce1f712d69108e8de672ad450` | + +Manifesthash: `9ed5f153fe76665150aaab298c4b21d74d7199b2312a28fa23aa9e30b13ddd08`. +Source0-Archiv: `9b9e16c754f233c8675057aa44f89f86c2574ced0ba9d85d60157402363008de`; +Source1-Archiv: `b2bdf4962a1beacf27903263952f4db025dbadfd7705fecc0b02dea3b32dde61`. +Die vollständigen Einzeldateimanifeste und exakten Docker-/Cargoargumente samt +Exitcodes und Loghashes liegen im Fall. Acht Unit-/Integrationsexecutables +wurden gebaut und ihre tatsächlichen Testnamen aufgezeichnet. Nur die im +Manifest explizit aufgeführten 18 Filter sind für den nächsten VM-Schritt +vorgesehen: sieben zielgerichtete Parser-/Ack-/Sync-/SQLiteverträge und elf reine +Handoffverträge. Diese 18 liefen im isolierten lokalen Container erfolgreich. +Andere gelinkte Tests sind durch dieses Paket nicht zur Zielausführung vorgesehen. +Noch keines dieser Programme wurde durch diesen Buildschritt auf der Cloud-VM +ausgeführt; kein Produktdienst oder Zieljournal wurde geändert. + +Diese Aussage beschreibt ausschließlich den damaligen Buildschritt. Der spätere +koordinierte native Lauf aller 18 vorgesehenen Filter auf der Cloud-Linux-VM +bestand; dessen Originalbelege und Abgrenzung stehen in +[Compatible-Linux-Vertragsprüfungen](updater-compatible-linux-contracts.md). +Er ersetzt weiterhin keinen tatsächlichen Updaterjob. + +Der erste erhaltene Fall `7c030441-81e0-4235-9565-0b413b0cedae` scheiterte +an `items_after_test_module` im Linuxunit-Testtarget. Das Parser-Testmodul wurde +ohne Verhaltensänderung ans Dateiende verschoben; der erfolgreiche Fall entstand +anschließend aus einem neuen Snapshot. Der fehlgeschlagene Originalbeleg bleibt +erhalten. + +## ARM64-Serverbuild, geplant und nicht ausgeführt + +Der Source0-Quellsatz enthält auch den aktuellen Server-/Zeitbrokerquellcode. +Für die später separat koordinierte Serverbereitstellung muss dieser exakte +Archivhash auf dem ARM64-Server geprüft und jede reguläre Archivdatei gegen +`source0-inputs.json` geprüft werden. Der vorhandene ARM64-Rustbuilder wird erst +mit `docker image inspect` auf Architektur/OS/Image-ID geprüft und danach über +seine tatsächliche Image-ID angesprochen. In einem neuen eigenen Targetcache +und ohne Netzwerk sind dort geplant: + +```text +cargo clippy --locked --offline -p rmm-server -p rmm-time-broker-linux --all-targets -- -D warnings +cargo build --locked --offline --release -p rmm-server -p rmm-time-broker-linux --bins +``` + +Der Container erhält den verifizierten Quellsatz ausschließlich lesend sowie +private temporäre Verzeichnisse. Artefakte müssen anschließend als tatsächliche +AArch64-ELFs, mit Hash/Buildlog/Image-ID und unverändertem Eingabemanifest +nachgewiesen werden. Keine Dockerfile-APT-Schritte, Netzwerkdownloads oder +Abhängigkeitsänderungen sind Teil dieses Plans. Serverdienste, bestehende +Ingress-/Broker-/Zeitpins und private Signierschlüssel werden durch einen +Build nicht geändert. Der lokale x86-64-Lauf ist kein ARM64-Buildnachweis. diff --git a/docs/linux-updater-compatible-controllers.md b/docs/linux-updater-compatible-controllers.md new file mode 100644 index 0000000..23f6d3b --- /dev/null +++ b/docs/linux-updater-compatible-controllers.md @@ -0,0 +1,174 @@ +# Kompatibler Linux-Updater: konkrete administrative Controller + +Stand 16. November 2026. Die nachstehenden Quellen sind vorbereitet und lokal +geprüft. **Kein erhaltender Dienstwechsel, kein Signierschlüssel, kein neuer +Updaterdienst und kein Updatejob wurden durch diese Vorbereitung auf einem Ziel +ausgeführt.** Die Zielphasen koordiniert der Rootprozess; RMM-Aufträge brauchen +weiterhin den tatsächlichen Weg MCP → `pending_approval` → WebUI → Originalgrant. + +## Genau verwendbare Artefakte + +Das unveränderte ursprüngliche Buildpack `21d20391-6102-4996-8a7a-efbd3e78bd7d` +bleibt erhalten. Der neue Agent muss zusätzlich die beiden bereits bestehenden +Domaincapabilities `agent.update.apply/1` und `agent.update.check/1` nach einem +gültigen geschützten Fullobserver melden. Außerdem muss der bestehende +60-Sekunden-Tick das fertige Observerergebnis vor seinem Capabilityvergleich +abholen. Andernfalls wurde die alte Probe erst nach dem Vergleich geerntet und +konnte beim nächsten Vergleich bereits älter als die erlaubten 90 Sekunden sein. +Der Tickaufruf wurde verschoben, ohne Samplerate oder Gültigkeitsgrenze zu ändern. + +Beide Änderungen sind in der eigenständigen Derivation +`devstate/linux-updater-capability-build/9923a97f-4c8c-4e0a-bbad-4f1d88f0d43a` +eingefroren. Manifest-SHA256: +`a41ad700b4b7527bf911bf00f2745914188760c631af0c3255364c05f1ef4fbc`. +Source0/Source1 verwenden unverändert 0.1.0/0.1.1. + +| Artefakt | SHA256 | +|---|---| +| Source0 Agent | `7fef8d0f1e2bcf5cd6c373e567a0c17bfb08d8c780cd4d1925b6c1355e572547` | +| Source1 Agent | `4d212066bef226af3fa679ffe95f69c43a9668cbef7e665d53328f4e41aa3799` | +| Source0 Helper, unverändert aus 21D | `8fdc6ded02278eb18e64003d0bc7bf45d452cd48ded0450e6e900ca66e44d449` | +| Source1 Helper, unverändert aus 21D | `89b28db3afe870c667297dff97ad7a042c550ccce1f712d69108e8de672ad450` | +| Stabiler Updater, unverändert aus 21D | `dec6336728eac8af1e0d84d4921c7c9ecbcc45bc4619d357d346b716336be90a` | + +Die Derivation verwendet einen eigenen kopierten Buildcache. Beide Agenten +bestanden all-targets Clippy mit `-D warnings` und den Releasebuild im gepinnten +Offlinebuilder. Alle 18 zuvor nativ geprüften Contractexecutables, Helper und +Updater wurden nicht neu gebaut und ihre Originalhashes erneut verglichen. +Der frühere reine Capabilitymarker-Fall `f89fbb79-…` bleibt historisch erhalten; +er enthält die Tickkorrektur noch nicht und ist hier nicht die Baseline. + +Der separate öffentliche Metadaten-SDK liegt im eingefrorenen Fall +`devstate/linux-updater-bootstrap-sdk/d309c949-3a70-4448-88da-49c710093221`. +Manifest-SHA256 `cf9464ff21b48b28903369692e3bb63148b2c488297ec709e8fd9c9881448528`, +Binary-SHA256 `6fcc543d538fca30ceb499364880e4dc3689700d50597f18bd11bfe3559a0021`. +Sein Worker-all-targets-Clippy und Releasebuild bestanden. Er schreibt keine +Zieldateien und startet keine Dienste. Er rendert Full über die bestehenden +Rusttypen, erzeugt kanonische öffentliche Manifest-/Record-/Policybytes und +misst bei `qualify_pair`/`registry` ausschließlich die wirklichen Zielzeugen. +Die Bundlecodecoperation kennzeichnet ausdrücklich nur die Formprüfung. + +## Vorbereitete erhaltende Phasen + +`deploy/linux_updater_compatible_apply.py` trennt folgende explizite Phasen; +keine Fehlerantwort startet einen bereits begonnenen Schritt erneut: + +1. `prepare`: Nur lesender Original-CAS auf der VM und lokale Paketvorbereitung. + Diese Phase wurde tatsächlich ausgeführt. Der lokale Fall + `devstate/linux-updater-compatible/ac6cf439-e0a6-4323-82e1-c55df96b2441` + enthält `package.json`, den lesenden Vorbeleg und Manifest-SHA256 + `d3917236c03b93273b4f43f1df3b6773cc04873018262567d3d31510c993c082`. + Es wurde noch kein Paket auf der VM bereitgestellt. +2. `stage`: Ausschließlich create-only sieben feste geprüfte öffentliche + Artefakte in einem neuen Root-700-Fallverzeichnis bereitstellen. +3. `install`: Frischen geschlossenen zentralen Scope und null offene Jobs prüfen; + echte Original-PIDs halten, Agent/General/Full stoppen, beide Originalflocks + halten und alle ursprünglichen DB-Inodes/Start-/Outcomeaggregate bewahren. + Vier feste Dateien mit create-only Vorgänger- und Kandidatennamen ersetzen. + Alle drei Dienste bleiben danach gestoppt. Bei bestehendem Intent nur lesen. +4. `units`: Den stabilen Updater und die feste Unit create-only installieren, + Full-Override `Restart=no` vor der Registry bereitstellen, daemon-reload und + tatsächlichen inaktiven Updater/Restartzustand prüfen. Keine Dienststarts. +5. `start`: General, Full, Agent jeweils genau einmal starten. Das ist zunächst + nur ein Dienststart, noch kein Baselinequalifikationsnachweis. +6. `qualify`: Der SDK prüft echte PID/Birth/Image-/Unitzeugen und dieselbe frisch + signierte normale Agent-/Helpersynchronisierung mit aktuellem NativeClock, + unabhängig gepinnten öffentlichen Jobkeys und erhaltener History. Kein + vergangener Clockwert und kein administrativer Ready-Ersatz. Die ausgegebene + Qualifikation enthält `ready/helper_ready/unlock/job_authority=false`. +7. `registry`: Die wirkliche feste 21-Unit-Schließung, etwaige Rollbacktimer, + Host-/Namespaceidentitäten und tatsächliche Dateipins zweimal messen. Ein + geladener APTdienst verlangt seinen vierten lokalen Servicepin, bleibt aber + inaktiv/disabled und erhält keine neue APT-Laufzeitqualifikation. +8. `trust`: Kanonische Form nativ mit dem SDK prüfen, Release- und separate + Baselinesignatur über genau diese Bytes zusätzlich mit OpenSSL prüfen, + aktuelle Artefakt-/Identitäts-/Registrybindungen vergleichen und die festen + Trustdateien create-only anlegen. Private Originalkeys werden nur per + Metadaten in die Retainedliste aufgenommen, niemals gelesen oder exportiert. +9. `updater-start`: Nach vollständiger Trustinstallation den stabilen Dienst + genau einmal starten und seinen echten gehaltenen PID/Imagezeugen prüfen. +10. `passive-check`: Nur serverseitig lesen. Die wirkliche normale Agentmeldung + muss weniger als 90 Sekunden alt sein, gültige Fullobserver-/Syncbezüge, + den exakten Vorgänger/Updater/Policy/Keyring/Healthpin und die benötigten + Runtimecapabilities enthalten. Es gibt keinen SQL-Schreibersatz für eine + fehlende Capability. `readback` holt nach einem verlorenen Transport nur + feste öffentliche Belege ab. + +Der vorhandene Generalpin Schema 1 enthält ausdrücklich keinen Binarypin; der +Produktcode verlangt hier `helper_binary_sha256=None`. Seine exakten Bytes und +Policy bleiben erhalten. Das erlaubt keine Umdeutung des alten Pins zu einem +Native-Abnahmenachweis für die neuen Helperbytes. Dafür ist Schritt 6 getrennt +notwendig. Der alte APTproof bleibt unverändert und weiterhin geschlossen. + +## Servertrust und Signieren + +`deploy/updater_server_signing_bootstrap.py` ist ein separat aufzurufender +Server-Rootbootstrap. Der reine Read `public-separation` wurde tatsächlich +ausgeführt: Er las ausschließlich den bestehenden öffentlichen Timepin und +verglich ihn mit dem unabhängig geschützten öffentlichen Jobkey. Timepin-Rawhash +`c31d31d2d61f881d04f55f704abbbf98f4822650c016f39e39d4d2318bc3f6e2`; +die beiden tatsächlichen Publickey-SHA256-Werte sind +`1d9b8f83191a99d69b435dc69e8c9b52650680e8e02025f3e338e5083924def8` und +`4338aabe7e7ba77428e969750a7a5323d7b7349aa52ef7e1a5f24c90be12ca7a`. +Der Read erzeugte keine Schlüssel und änderte keinen Dienst. Der öffentliche +Beleg und der noch nicht ausgeführte `signer-provision-input.json` liegen im +lokalen kompatiblen Bootstrapfall `ac6cf439-…`. + +`provision` erzeugt für Linux und Windows jeweils einen +eigenen Release-/Baselineschlüssel. Der globale Healthschlüssel wird nur beim +ersten Aufruf erzeugt und später ausschließlich nach unveränderter öffentlicher +Provenienz und Schlüsselmetadaten wiederverwendet. Der Aufruf verlangt echte +Job-/Zeit-Publickey-SHA256-Werte als Ausschlussliste; Rollenalias-IDs genügen +nicht. Kein privater Schlüssel verlässt den Server oder erscheint im Output. + +Releasekeys bleiben Root in 0700/0600. Der tatsächliche Server-Trustreader +verlangt den bestehenden Jobkey-Dateiowner 10001 auch für Keyring/Healthdatei, +Datei 0600/ein Link und geschützten Elternpfad. Deshalb erhält jede Plattform +ein eigenes öffentliches Verzeichnis 10001/0700 mit `keyring.json` 0600. +Dieses wird später read-only in API **und** Gateway gemountet; der separate +Healthkey wird read-only ausschließlich in das Gateway gemountet. Der Container +behält seine vorhandene UID und schreibgeschützte Root-FS. Private Releasekeys +werden überhaupt nicht in Anwendungscontainer gemountet. + +Neue statische Serverkonfiguration: + +| Einstellung | Zweck | +|---|---| +| `RMM_AGENT_UPDATE_LINUX_KEYRING_FILE` | ausschließlich geschützter Linux-Keyring | +| `RMM_AGENT_UPDATE_WINDOWS_KEYRING_FILE` | ausschließlich geschützter Windows-Keyring | +| `RMM_AGENT_UPDATE_KEYRING_FILE` | unveränderter Legacyfallback, nur wenn der Plattformslot fehlt | +| `RMM_AGENT_UPDATE_HEALTH_KEY_FILE` | separater Gateway-Healthsignierer | + +Ein vom Netz empfangener Plattformenum wählt ausschließlich einen dieser zwei +fest konfigurierten Pfade. Ein leerer/relativer expliziter Plattformslot wird +verweigert. Danach gelten weiterhin sämtliche Signatur-, Plattform-, Revocation-, +Generation- und Original-Rawhashprüfungen. Ein gemeinsamer Mehrplattformkeyring +würde die bestehenden streng plattformgebundenen Clients nicht erfüllen; +deshalb wird deren Trustvertrag nicht gelockert. Die beiden gezielten +Serververträge bestanden mit echten lokalen Ed25519-Signaturen für beide +Plattformen und negativen Kreuzkeyring-/Signatur-/Revocation-/Generation-/ +Rawhashfällen; Server-all-targets-Clippy bestand ebenfalls. Diese Quellen +brauchen noch einen neuen, getrennt koordinierten ARM64-Imagebuild/Deploy. + +`deploy/linux_updater_public_materials.py` verwendet den sealed SDK für +`release-input`, `record-input`, `trust-package`. Es signiert nichts. Der Server +signiert ausschließlich die zwei fest qualifizierten Linux-Codepaare und die +Initialbaseline mit dem tatsächlichen Qualifikationshash. Ein Ausgabe-JSON ist +keine lokale Vertrauensquelle. Die kanonischen Manifestcodec-Rundläufe für beide +wirklichen Linux-Artefaktpaare und Python-Syntaxprüfung aller drei Controller +bestanden lokal; dabei wurden weder Schlüssel noch Signaturen erzeugt. + +Windows-GNU-Artefakte werden ausdrücklich nicht als kanonische Windows-MSVC- +Releases signiert. Die tatsächliche MSVC-Qualifikation ist ein eigener offener +Schritt. Das ändert weder das Linux-first-Gate noch einen Plattformvertrag. + +## Danach noch tatsächlich abzunehmen + +Die beschriebenen Zielphasen sind weiterhin auszuführen. Neue Servermounts, +plattformbezogener Keyring, Gateway-Healthkey, enger Update-Denyscope und +Tenantkatalog müssen anschließend konkret und mit erhaltenen Composepins +bereitgestellt werden. Erst dann folgen der tatsächlich über MCP beantragte +und in der WebUI genehmigte Linux-Updatejob, dessen Originalverbrauch, +Codepaarwechsel, signierte Health und endgültiger Record/Floor sowie der +bekannte geschlossene Fullstart-Fehlerfall mit erhaltender Rückführung. Lokale +Codec-/Build-/Syntaxbelege ersetzen diese nativen Abnahmen nicht.