Files
rust-discovery/crates/privileged-helper/src/update_guard.rs
T
2026-11-16 15:25:46 +01:00

1295 lines
49 KiB
Rust

//! Full-service-only update ownership. This is a local OS adapter, never a
//! caller's ready/approved claim. General keeps its original default denial.
use crate::{Binding, reconcile::ExecutorGuard};
use anyhow::{Context, Result, ensure};
use rmm_agent_core::{
helper_reconcile::ExecutorLedgerGuard,
reconcile::SyncClock,
update_handoff::{
self, NativeEffect, NativeEffectStage, NativeService, Participant, UpdateParticipantGuard,
},
};
use rmm_domain::{
agent_update::{ParticipantRole, UpdateDelegation},
jobs::hash,
};
use rmm_updater_handoff::{Event, Evidence, Handoff, Phase};
use rmm_updater_linux::{
Budget,
files::{Directory, Identity, LedgerIdentity},
services::{HeldService, Service, ServicePolicy, Systemd},
};
use rmm_updater_worker::{bundle, clock::NativeClock, original::Original, trust::TrustState};
use rusqlite::Connection;
use std::{
fs::{File, OpenOptions},
os::unix::fs::{MetadataExt, OpenOptionsExt},
path::Path,
time::Duration,
};
use uuid::Uuid;
fn identity(meta: &std::fs::Metadata) -> Identity {
Identity {
device: meta.dev(),
inode: meta.ino(),
uid: meta.uid(),
gid: meta.gid(),
mode: meta.mode(),
links: meta.nlink(),
}
}
fn lock_identity(executor: &ExecutorGuard) -> Result<Identity> {
let held = executor._file.metadata()?;
let named = std::fs::symlink_metadata("/var/lib/rmm-helper/executor.lock")?;
ensure!(
held.is_file()
&& named.is_file()
&& !named.file_type().is_symlink()
&& identity(&held) == identity(&named),
"UPDATE_HELD_LOCK_REPLACED"
);
Ok(identity(&held))
}
fn service_policies(original: &Original, trust: &TrustState, target: bool) -> Result<Systemd> {
let pair = if target {
&original.handoff().target
} else {
&original.handoff().predecessor
};
let mut pins = Vec::new();
for pin in &trust.policy().services {
let service = match pin.unit.as_str() {
"rmm-agent-remote.service" => Service::Agent,
"rmm-helper-cloud.service" => Service::General,
"rmm-full-maintenance-helper.service" => Service::Full,
"rmm-apt-helper.service" => Service::Apt,
_ => anyhow::bail!("UPDATE_UNKNOWN_PROVISIONED_SERVICE"),
};
pins.push(ServicePolicy {
service,
image_sha256: if service == Service::Agent {
pair.agent_sha256.clone()
} else {
pair.helper_sha256.clone()
},
unit_sha256: pin.unit_sha256.clone(),
});
}
Systemd::new(pins)
}
pub struct FullUpdateGuard<'a> {
executor: &'a ExecutorGuard,
binding: &'a Binding,
clock: &'a NativeClock,
original: Original,
trust: TrustState,
witnesses: bundle::Loaded,
budget: Budget,
services: Systemd,
own: HeldService,
updater: Option<HeldService>,
ledger_file: File,
directory: Directory,
sqlite_handle: usize,
participant: Participant,
history_qualified: std::cell::Cell<bool>,
health: Option<VerifiedHealth>,
}
impl<'a> FullUpdateGuard<'a> {
/// Only an already consumed original handoff may be reconstructed here.
/// Admission has a different pre-consumption adapter; this is no new start.
pub fn open(
db: &Connection,
executor: &'a ExecutorGuard,
binding: &'a Binding,
clock: &'a NativeClock,
grant: Uuid,
) -> Result<Self> {
executor.check(db)?;
let journal = update_handoff::read(db, executor, grant)?;
ensure!(
!matches!(journal.phase(), Phase::Unknown | Phase::OutcomePersisted),
"UPDATE_NOT_ACTIVE"
);
let (record,start):(Vec<u8>,Vec<u8>)=db.query_row("SELECT h.record,e.start FROM agent_update_handoffs h JOIN execution_evidence e USING(grant_id) WHERE h.grant_id=?1",[grant.to_string()],|r|Ok((r.get(0)?,r.get(1)?)))?;
let header: Handoff = serde_json::from_slice(&record)?;
// Read-only adjudication must still work after a different boot or
// original deadline. This bounded observation budget is NO effect lease.
let budget = Budget::new(
Uuid::parse_str(&rmm_agent_linux::boot_id()?)?,
Duration::from_secs(10),
)?;
let delegation = update_handoff::read_delegation(db, grant)?;
ensure!(
delegation.original_helper.role == ParticipantRole::LinuxFullHelper,
"UPDATE_FULL_AUTHORIZER_REQUIRED"
);
let original = Original::verify_retained(binding, &start, &record, delegation)?;
let trust = TrustState::load_retained(&original, binding)?;
let witnesses = bundle::read_for_handoff(original.handoff())?;
let target = matches!(
journal.phase(),
Phase::LedgerReturnIntent | Phase::ServicesStarted | Phase::HealthVerified
);
let 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!(
own.proof().process.pid == std::process::id(),
"UPDATE_ONLY_ACTUAL_FULL_PROCESS"
);
let prior = &original.delegation().original_helper;
let participant =
if own.proof().process.pid == prior.pid && own.proof().process.birth == prior.birth {
ensure!(
own.proof().process.image_sha256 == prior.image_sha256
&& hash(&serde_json::to_vec(own.proof())?) == prior.service_instance_sha256,
"UPDATE_ORIGINAL_FULL_CHANGED"
);
Participant::OriginalHelper
} else if 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 {
Participant::Observer
};
let updater = Systemd::observe_updater(
ServicePolicy {
service: Service::Updater,
image_sha256: original.handoff().updater_sha256.clone(),
unit_sha256: trust.policy().updater_service.unit_sha256.clone(),
},
&budget,
)
.ok();
let directory = Directory::open(Path::new("/var/lib/rmm-helper"), 0, true)?;
let ledger_file = OpenOptions::new()
.read(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC | libc::O_NONBLOCK)
.open("/var/lib/rmm-helper/consumption.sqlite")?;
let value = Self {
executor,
binding,
clock,
original,
trust,
witnesses,
budget,
services,
own,
updater,
ledger_file,
directory,
sqlite_handle: unsafe { db.handle() } as usize,
participant,
history_qualified: std::cell::Cell::new(false),
health: None,
};
value.check(db)?;
Ok(value)
}
pub fn qualify_effects(&self, db: &Connection) -> Result<()> {
self.check(db)?;
self.trust.recheck(&self.original, self.clock)?;
self.trust
.qualify_history(db, self.executor, self.binding)?;
self.history_qualified.set(true);
self.check_effect_window()
}
pub fn original(&self) -> &Original {
&self.original
}
pub fn budget(&self) -> &Budget {
&self.budget
}
pub fn trust(&self) -> &TrustState {
&self.trust
}
pub fn services(&self) -> &Systemd {
&self.services
}
pub fn verify_updater_peer(&self, stream: &std::os::unix::net::UnixStream) -> Result<()> {
let peer = self
.updater
.as_ref()
.context("UPDATE_ORIGINAL_UPDATER_NOT_LIVE")?
.authenticate_peer(stream, &self.budget)?;
ensure!(
peer == self.original.delegation().updater,
"UPDATE_ORIGINAL_UPDATER_PEER_REQUIRED"
);
Ok(())
}
pub fn verify_health_peer(&self, stream: &std::os::unix::net::UnixStream) -> Result<()> {
use std::os::fd::AsRawFd;
if self.verify_updater_peer(stream).is_ok() {
return Ok(());
}
self.check_effect_window()?;
ensure!(
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::<libc::ucred>::uninit();
let mut length = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
ensure!(
unsafe {
libc::getsockopt(
stream.as_raw_fd(),
libc::SOL_SOCKET,
libc::SO_PEERCRED,
peer.as_mut_ptr().cast(),
&mut length,
)
} == 0
&& length as usize == std::mem::size_of::<libc::ucred>(),
"UPDATE_HEALTH_AGENT_PEER"
);
let peer = unsafe { peer.assume_init() };
let expected = &agent.proof().process;
ensure!(
peer.pid > 0
&& peer.pid as u32 == expected.pid
&& peer.uid == expected.uid
&& peer.gid == expected.gid,
"UPDATE_HEALTH_EXACT_AGENT_REQUIRED"
);
agent.verify(&self.budget)
}
pub fn qualify_health(
&mut self,
db: &Connection,
signed: &rmm_updater_handoff::SignedHealth,
) -> Result<()> {
self.check_effect_window()?;
ensure!(
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!(
matches!(
journal.phase(),
Phase::ServicesStarted | Phase::RollbackServicesStarted
),
"UPDATE_HEALTH_PHASE"
);
let event = journal.prepare(
u64::try_from(self.clock.now()?.timestamp())?,
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.
self.health = Some(self.health_evidence(db, signed)?);
Ok(())
}
pub fn qualify_stored_health(&mut self, db: &Connection) -> Result<Vec<u8>> {
self.check_effect_window()?;
let journal = update_handoff::read(db, self, self.original.permit().grant_id)?;
ensure!(
matches!(
journal.phase(),
Phase::HealthVerified | Phase::RollbackHealthVerified
),
"UPDATE_ORIGINAL_STORED_HEALTH_REQUIRED"
);
let event_raw:Vec<u8>=db.query_row("SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence DESC LIMIT 1",[self.original.permit().grant_id.to_string()],|r|r.get(0))?;
let event: Event = serde_json::from_slice(&event_raw)?;
ensure!(
hash(&event_raw) == journal.head_sha256(),
"UPDATE_STORED_HEALTH_HEAD_CHANGED"
);
let 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)
}
fn health_evidence(
&self,
db: &Connection,
signed: &rmm_updater_handoff::SignedHealth,
) -> Result<VerifiedHealth> {
use rmm_agent_core::crypto::Ed25519Verifier;
self.check_effect_window()?;
ensure!(
signed.payload.len() <= 4096 && signed.signature.len() == 64,
"UPDATE_HEALTH_SIGNED_SIZE"
);
let mut message = rmm_updater_handoff::HEALTH_DOMAIN.to_vec();
message.extend_from_slice(&signed.payload);
ensure!(
rmm_agent_core::crypto::DalekVerifier.verify(
&self.trust.health_public_key(),
&message,
&signed.signature
)?,
"UPDATE_ORIGINAL_HEALTH_SIGNATURE_REQUIRED"
);
let receipt: rmm_updater_handoff::HealthReceipt = serde_json::from_slice(&signed.payload)?;
let (generation, agent_journal, helper_journal): (u64, String, String) = db.query_row(
"SELECT generation,agent_journal_id,journal_id FROM helper_sync_meta WHERE id=1",
[],
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
)?;
ensure!(
generation == receipt.connection_generation
&& agent_journal == receipt.agent_journal_id.to_string()
&& helper_journal == receipt.helper_journal_id.to_string(),
"UPDATE_HEALTH_CURRENT_LOCAL_CHANNEL_REQUIRED"
);
self.verify_current_services(&receipt.service_processes)?;
let span = rmm_agent_core::update_health_sync::verified_span(
db,
self.executor,
self.binding,
self.original.handoff(),
&receipt,
self.trust.health_public_key(),
)?;
let applied = span.applied().clone();
verify_local_inventory(&self.witnesses, &self.original, &receipt)?;
Ok(VerifiedHealth {
signed_sha256: hash(&serde_json::to_vec(signed)?),
applied,
receipt,
})
}
pub fn started(&self) -> Result<(Vec<rmm_updater_handoff::StartedService>, String)> {
self.check_effect_window()?;
ensure!(
matches!(
self.participant,
Participant::NewHelper | Participant::RestoredHelper
),
"UPDATE_NEW_OR_RESTORED_FULL_REQUIRED"
);
let mut raw = Vec::new();
let mut proofs = Vec::new();
for service in [Service::Agent, Service::General, Service::Full] {
let held = self.services.observe(service, &self.budget)?;
proofs.push(held.started_proof(&self.budget)?);
raw.push(held.proof().clone());
}
Ok((proofs, hash(&serde_json::to_vec(&raw)?)))
}
pub fn service_observation_sha256(&self, service: NativeService) -> Result<String> {
self.check_effect_window()?;
let service = match service {
NativeService::Agent => Service::Agent,
NativeService::General => Service::General,
NativeService::Full => Service::Full,
NativeService::WindowsAgent | NativeService::WindowsHelper => {
anyhow::bail!("UPDATE_WINDOWS_SERVICE_ON_LINUX")
}
};
let held = self.services.observe(service, &self.budget)?;
held.verify(&self.budget)?;
Ok(hash(&serde_json::to_vec(held.proof())?))
}
fn check_effect_window(&self) -> Result<()> {
ensure!(
self.history_qualified.get(),
"UPDATE_ACTUAL_GUARDED_HISTORY_REQUIRED"
);
ensure!(
Uuid::parse_str(&rmm_agent_linux::boot_id()?)? == self.original.handoff().boot_id,
"UPDATE_EFFECT_BOOT_CHANGED"
);
self.original
.check_current(self.binding, self.clock, &self.budget)?;
self.trust.recheck(&self.original, self.clock)?;
Ok(())
}
fn verify_current_services(
&self,
expected: &[rmm_updater_handoff::StartedService],
) -> Result<()> {
ensure!(
expected.len() == 3,
"UPDATE_EXACT_THREE_STARTED_PROOFS_REQUIRED"
);
for service in [Service::Agent, Service::General, Service::Full] {
let actual = self
.services
.observe(service, &self.budget)?
.started_proof(&self.budget)?;
ensure!(
expected.iter().any(|saved| saved == &actual),
"UPDATE_STARTED_PROCESS_CHANGED"
);
}
Ok(())
}
fn verify_owner(&self, phase: Phase, events: &[Event]) -> Result<()> {
if !matches!(
phase,
Phase::ServicesStarted
| Phase::HealthVerified
| Phase::RollbackServicesStarted
| Phase::RollbackHealthVerified
) {
let updater = self
.updater
.as_ref()
.context("UPDATE_ORIGINAL_UPDATER_NOT_LIVE")?;
updater.verify(&self.budget)?;
let proof = updater.proof();
let expected = &self.original.delegation().updater;
ensure!(
proof.process.pid == expected.pid
&& proof.process.birth == expected.birth
&& proof.process.image_sha256 == expected.image_sha256
&& hash(&serde_json::to_vec(proof)?) == expected.service_instance_sha256,
"UPDATE_ORIGINAL_UPDATER_OWNER_CHANGED"
);
}
if matches!(
phase,
Phase::ServicesStarted
| Phase::HealthVerified
| Phase::RollbackServicesStarted
| Phase::RollbackHealthVerified
) {
let started = events
.iter()
.find_map(|event| match &event.evidence {
Evidence::ServicesStarted { services, .. }
if matches!(phase, Phase::ServicesStarted | Phase::HealthVerified) =>
{
Some(services)
}
Evidence::RollbackServicesStarted { services, .. }
if matches!(
phase,
Phase::RollbackServicesStarted | Phase::RollbackHealthVerified
) =>
{
Some(services)
}
_ => None,
})
.context("UPDATE_STARTED_PROOF_MISSING")?;
self.verify_current_services(started)?;
}
Ok(())
}
}
impl ExecutorLedgerGuard for FullUpdateGuard<'_> {
fn check(&self, db: &Connection) -> Result<()> {
self.check_namespace(db, None)
}
fn is_update_owner_authority(&self) -> bool {
true
}
fn check_update_owner(
&self,
handoff: &Handoff,
delegation: &UpdateDelegation,
phase: Phase,
events: &[Event],
) -> Result<()> {
ensure!(
handoff.bytes()? == self.original.handoff().bytes()?
&& delegation == self.original.delegation(),
"UPDATE_OWNER_ORIGINAL_CHANGED"
);
self.verify_owner(phase, events)
}
}
impl UpdateParticipantGuard for FullUpdateGuard<'_> {
fn participant(&self) -> Participant {
self.participant
}
fn verify_update_event(
&self,
handoff: &Handoff,
delegation: &UpdateDelegation,
event: &Event,
) -> Result<()> {
ensure!(
handoff.bytes()? == self.original.handoff().bytes()?
&& delegation == self.original.delegation(),
"UPDATE_EVENT_ORIGINAL_CHANGED"
);
self.check_effect_window()?;
ensure!(
matches!(
self.participant,
Participant::NewHelper | Participant::RestoredHelper
),
"UPDATE_NEW_OR_RESTORED_FULL_REQUIRED"
);
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] {
let current = self.services.observe(service, &self.budget)?;
proofs.push(current.started_proof(&self.budget)?);
held.push(current.proof().clone());
}
ensure!(
*services == proofs
&& *ledger_identity_sha256
== self.original.handoff().ledger_identity_sha256
&& *observation_sha256 == hash(&serde_json::to_vec(&held)?),
"UPDATE_ACTUAL_STARTED_PROOFS_REQUIRED"
);
}
Evidence::HealthVerified { receipt } | 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()
.context("UPDATE_ACTUAL_LOCAL_HEALTH_REQUIRED")?;
ensure!(
hash(&serde_json::to_vec(receipt)?) == health.signed_sha256,
"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"),
}
Ok(())
}
fn verify_native_effect(
&self,
handoff: &Handoff,
delegation: &UpdateDelegation,
effect: &NativeEffect,
stage: NativeEffectStage,
) -> Result<()> {
ensure!(
handoff.bytes()? == self.original.handoff().bytes()?
&& delegation == self.original.delegation(),
"UPDATE_NATIVE_ORIGINAL_CHANGED"
);
self.check_effect_window()?;
self.updater
.as_ref()
.context("UPDATE_ORIGINAL_UPDATER_NOT_LIVE")?
.verify(&self.budget)?;
match (self.participant, effect, stage) {
(
Participant::OriginalHelper,
NativeEffect::StopService { service },
NativeEffectStage::Claim,
) => {
let service = match service {
NativeService::Agent => Service::Agent,
NativeService::General => Service::General,
NativeService::Full => Service::Full,
NativeService::WindowsAgent | NativeService::WindowsHelper => {
anyhow::bail!("UPDATE_WINDOWS_SERVICE_ON_LINUX")
}
};
self.services
.observe(service, &self.budget)?
.verify(&self.budget)?;
}
(
Participant::NewHelper,
NativeEffect::StartService { service },
NativeEffectStage::Observe,
) => {
let service = match service {
NativeService::Agent => Service::Agent,
NativeService::General => Service::General,
NativeService::Full => Service::Full,
NativeService::WindowsAgent | NativeService::WindowsHelper => {
anyhow::bail!("UPDATE_WINDOWS_SERVICE_ON_LINUX")
}
};
self.services
.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<String> {
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 {
signed_sha256: String,
applied: update_handoff::AppliedUpdateSync,
receipt: rmm_updater_handoff::HealthReceipt,
}
fn verify_local_inventory(
witnesses: &bundle::Loaded,
original: &Original,
receipt: &rmm_updater_handoff::HealthReceipt,
) -> Result<()> {
use prost::Message;
use rmm_updater_worker::retained::Retention;
use rusqlite::OpenFlags;
const PATH: &str = "/var/lib/rmm-agent-remote/journal.sqlite";
ensure!(
witnesses
.retained
.specs()
.iter()
.any(|s| s.path == Path::new(PATH)
&& s.retention == Retention::MutableDatabase
&& s.identity.uid == 993),
"UPDATE_RETAINED_AGENT_JOURNAL_REQUIRED"
);
witnesses.retained.verify()?;
let db = Connection::open_with_flags(
PATH,
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
)?;
db.busy_timeout(Duration::ZERO)?;
// This journal belongs to the unprivileged agent. Bound SQLite's own
// materialization before querying; a hostile oversized BLOB/view cannot
// turn a root health check into an unbounded allocation or schema program.
unsafe {
rusqlite::ffi::sqlite3_limit(
db.handle(),
rusqlite::ffi::SQLITE_LIMIT_LENGTH,
(rmm_domain::MAX_MESSAGE + 65536) as i32,
);
}
db.execute_batch("PRAGMA trusted_schema=OFF;PRAGMA query_only=ON;")?;
let tx = db.unchecked_transaction()?;
let kind: String = tx.query_row(
"SELECT type FROM sqlite_master WHERE name='update_health_inventory'",
[],
|r| r.get(0),
)?;
ensure!(kind == "table", "UPDATE_AGENT_RETENTION_MUST_BE_TABLE");
let length: u64 = tx.query_row(
"SELECT length(envelope) FROM update_health_inventory WHERE grant_id=?1 AND message_id=?2",
rusqlite::params![
original.permit().grant_id.to_string(),
receipt.inventory_message_id.to_string()
],
|r| r.get(0),
)?;
ensure!(
length > 0 && length <= rmm_domain::MAX_MESSAGE as u64,
"UPDATE_LOCAL_INVENTORY_RAW_LIMIT"
);
let envelope = rmm_agent_core::update_health_inventory::read(
&tx,
original.permit().grant_id,
receipt.inventory_message_id,
)?;
let marker:(String,String)=tx.query_row("SELECT signed_decision_id,signed_response_sha256 FROM update_health_inventory WHERE grant_id=?1 AND message_id=?2",rusqlite::params![original.permit().grant_id.to_string(),receipt.inventory_message_id.to_string()],|r|Ok((r.get(0)?,r.get(1)?)))?;
ensure!(
marker
== (
receipt.signed_sync_decision_id.to_string(),
receipt.signed_sync_response_sha256.clone()
)
&& envelope.agent_id == original.handoff().attempt.agent_id.to_string()
&& envelope.boot_id == original.handoff().boot_id.to_string()
&& envelope.enrollment_generation == original.handoff().attempt.enrollment_generation
&& envelope.connection_generation == receipt.connection_generation,
"UPDATE_LOCAL_INVENTORY_ORIGINAL_BINDING"
);
let Some(rmm_protocol::Payload::InventorySnapshot(snapshot)) = &envelope.payload else {
anyhow::bail!("UPDATE_LOCAL_BASE_INVENTORY_REQUIRED")
};
ensure!(
snapshot.scope == "linux.base"
&& !snapshot.data.is_empty()
&& snapshot.sha256 == hex::decode(hash(&snapshot.data))?
&& envelope.encode_to_vec().len() <= rmm_domain::MAX_MESSAGE,
"UPDATE_LOCAL_BASE_INVENTORY_HASH"
);
rmm_domain::inventory_document::validate_for_scope(
&snapshot.data,
chrono::Utc::now(),
"linux.base",
)
.map_err(anyhow::Error::msg)?;
tx.commit()?;
witnesses.retained.verify()
}
impl FullUpdateGuard<'_> {
fn check_namespace(
&self,
db: &Connection,
namespace: Option<&rmm_updater_worker::record::RecordNamespaceProof<'_>>,
) -> Result<()> {
self.executor.check(db)?;
ensure!(
unsafe { db.handle() } as usize == self.sqlite_handle,
"UPDATE_ORIGINAL_SQLITE_HANDLE_CHANGED"
);
self.directory.verify()?;
let named = std::fs::symlink_metadata("/var/lib/rmm-helper/consumption.sqlite")?;
let held = self.ledger_file.metadata()?;
let lock = lock_identity(self.executor)?;
ensure!(
held.is_file()
&& named.is_file()
&& !named.file_type().is_symlink()
&& identity(&held) == identity(&named)
&& (LedgerIdentity {
ledger: identity(&held),
lock
})
.sha256()?
== self.original.handoff().ledger_identity_sha256,
"UPDATE_ORIGINAL_LEDGER_IDENTITIES_CHANGED"
);
self.budget.check()?;
if let Some(namespace) = namespace {
self.trust
.recheck_protected_record_namespace(&self.original, namespace)?;
} else {
self.trust.recheck_protected()?;
}
self.witnesses.retained.verify()?;
let start: Vec<u8> = db.query_row(
"SELECT start FROM execution_evidence WHERE grant_id=?1",
[self.original.permit().grant_id.to_string()],
|r| r.get(0),
)?;
ensure!(
start == self.original.start_bytes(),
"UPDATE_ORIGINAL_START_CHANGED"
);
self.own.verify(&self.budget)?;
if let Some(health) = &self.health {
self.verify_current_services(&health.receipt.service_processes)?;
ensure!(
update_handoff::read_applied_sync(
db,
self.executor,
self.original.permit().grant_id,
health.applied.decision_id
)? == health.applied,
"UPDATE_LOCAL_HEALTH_APPLY_CHANGED"
);
verify_local_inventory(&self.witnesses, &self.original, &health.receipt)?;
}
self.directory.verify()
}
fn check_record_effect(
&self,
namespace: &rmm_updater_worker::record::RecordNamespaceProof<'_>,
) -> Result<()> {
ensure!(
self.history_qualified.get() && self.participant == Participant::NewHelper,
"UPDATE_ORIGINAL_NEW_FULL_HISTORY_REQUIRED"
);
ensure!(
Uuid::parse_str(&rmm_agent_linux::boot_id()?)? == self.original.handoff().boot_id,
"UPDATE_RECORD_BOOT_CHANGED"
);
self.original
.check_current(self.binding, self.clock, &self.budget)?;
self.trust
.recheck_record_namespace(&self.original, self.clock, namespace)?;
self.witnesses.files.verify_pair(&self.budget)?;
self.own.verify(&self.budget)?;
ensure!(self.health.is_some(), "UPDATE_RECORD_LOCAL_HEALTH_REQUIRED");
Ok(())
}
}
/// The only admitted deviation from TrustState's active record name. This
/// wrapper is local, nonserializable and borrowed from a live one-shot port.
struct FullRecordGuard<'a, 'base, 'transition> {
base: &'a FullUpdateGuard<'base>,
namespace: &'a rmm_updater_worker::record::RecordNamespaceProof<'transition>,
}
impl ExecutorLedgerGuard for FullRecordGuard<'_, '_, '_> {
fn check(&self, db: &Connection) -> Result<()> {
self.base.check_namespace(db, Some(self.namespace))
}
fn is_update_owner_authority(&self) -> bool {
true
}
fn check_update_owner(
&self,
h: &Handoff,
d: &UpdateDelegation,
p: Phase,
e: &[Event],
) -> Result<()> {
self.base.check_update_owner(h, d, p, e)
}
}
impl UpdateParticipantGuard for FullRecordGuard<'_, '_, '_> {
fn participant(&self) -> Participant {
self.base.participant
}
fn verify_update_event(&self, h: &Handoff, d: &UpdateDelegation, event: &Event) -> Result<()> {
ensure!(
h.bytes()? == self.base.original.handoff().bytes()?
&& d == self.base.original.delegation(),
"UPDATE_RECORD_FINAL_ORIGINAL_BINDING"
);
self.base.check_record_effect(self.namespace)?;
ensure!(
self.namespace.phase() == rmm_updater_worker::record::RecordPhase::Published
&& matches!(event.evidence, Evidence::OutcomePersisted { .. }),
"UPDATE_RECORD_FINAL_PUBLICATION_REQUIRED"
);
Ok(())
}
fn verify_native_effect(
&self,
h: &Handoff,
d: &UpdateDelegation,
effect: &NativeEffect,
stage: NativeEffectStage,
) -> Result<()> {
use rmm_updater_worker::record::RecordPhase;
ensure!(
h.bytes()? == self.base.original.handoff().bytes()?
&& d == self.base.original.delegation(),
"UPDATE_RECORD_NATIVE_ORIGINAL_BINDING"
);
self.base.check_record_effect(self.namespace)?;
let (old, new, expected) = match (effect, stage) {
(
NativeEffect::SaveInstalledRecord {
old_sha256,
new_sha256,
},
NativeEffectStage::Claim,
) => (old_sha256, new_sha256, RecordPhase::Prepared),
(
NativeEffect::SaveInstalledRecord {
old_sha256,
new_sha256,
},
NativeEffectStage::Observe,
) => (old_sha256, new_sha256, RecordPhase::Saved),
(
NativeEffect::PublishInstalledRecord {
old_sha256,
new_sha256,
},
NativeEffectStage::Claim,
) => (old_sha256, new_sha256, RecordPhase::Saved),
(
NativeEffect::PublishInstalledRecord {
old_sha256,
new_sha256,
},
NativeEffectStage::Observe,
) => (old_sha256, new_sha256, RecordPhase::Published),
_ => anyhow::bail!("UPDATE_ONLY_ORIGINAL_RECORD_EFFECTS"),
};
ensure!(
self.namespace.phase() == expected
&& self.namespace.old_sha256() == old
&& self.namespace.new_sha256() == new,
"UPDATE_EXACT_RECORD_EFFECT_NAMESPACE"
);
Ok(())
}
}
struct FullRecordFence<'db, 'base, 'inner> {
db: &'db mut Connection,
base: &'base FullUpdateGuard<'inner>,
pending: Option<update_handoff::NativeEffectClaim>,
}
impl rmm_updater_worker::record::RecordFence for FullRecordFence<'_, '_, '_> {
fn before_effect(
&mut self,
effect: &NativeEffect,
namespace: &rmm_updater_worker::record::RecordNamespaceProof<'_>,
) -> Result<()> {
ensure!(self.pending.is_none(), "UPDATE_RECORD_UNOBSERVED_CLAIM");
let guard = FullRecordGuard {
base: self.base,
namespace,
};
let journal = update_handoff::read(self.db, &guard, self.base.original.permit().grant_id)?;
self.pending = Some(update_handoff::claim_native_effect(
self.db,
&guard,
self.base.original.permit().grant_id,
journal.head_sha256(),
effect,
self.base.clock.now()?,
)?);
Ok(())
}
fn after_effect(
&mut self,
effect: &NativeEffect,
namespace: &rmm_updater_worker::record::RecordNamespaceProof<'_>,
observed: &str,
) -> Result<()> {
ensure!(
namespace.observation_sha256()? == observed,
"UPDATE_RECORD_ACTUAL_OBSERVATION_REQUIRED"
);
let claim = self
.pending
.as_ref()
.context("UPDATE_RECORD_ORIGINAL_CLAIM_REQUIRED")?;
ensure!(claim.effect() == effect, "UPDATE_RECORD_EFFECT_CHANGED");
let guard = FullRecordGuard {
base: self.base,
namespace,
};
update_handoff::observe_native_effect(
self.db,
&guard,
claim,
observed,
self.base.clock.now()?,
)?;
self.pending = None;
Ok(())
}
}
impl FullUpdateGuard<'_> {
/// One local original finalization. Does not set consumed.result or release
/// any start fence. A failed/uncertain publication is never dispatched again.
pub fn publish_record_and_finalize(&mut self, db: &mut Connection) -> Result<Vec<u8>> {
self.qualify_effects(db)?;
let event = self.qualify_stored_health(db)?;
let journal = update_handoff::read(db, self, self.original.permit().grant_id)?;
let built = rmm_updater_worker::record::BuiltRecord::build(
&self.original,
self.trust.installed_raw(),
&journal,
&event,
)?;
let record_sha = built.sha256().to_owned();
let health_sha = self
.health
.as_ref()
.context("UPDATE_ACTUAL_LOCAL_HEALTH_REQUIRED")?
.signed_sha256
.clone();
let mut transition =
rmm_updater_worker::record::RecordTransition::prepare(built, &self.budget)?;
{
let mut fence = FullRecordFence {
db,
base: self,
pending: None,
};
transition.save_previous(&mut fence, &self.budget)?;
transition.publish(&mut fence, &self.budget)?;
ensure!(
fence.pending.is_none(),
"UPDATE_RECORD_PUBLICATION_OBSERVATION_REQUIRED"
);
}
let namespace = transition.namespace_proof();
let guard = FullRecordGuard {
base: self,
namespace: &namespace,
};
let journal = update_handoff::read(db, &guard, self.original.permit().grant_id)?;
let outcome = crate::Outcome {
state: "succeeded".into(),
stdout: vec![],
stderr: vec![],
exit_code: Some(0),
output_truncated: false,
reboot_required: false,
error_code: String::new(),
evidence: serde_json::json!({"postconditions_passed":true,"previous_boot_id":self.original.handoff().boot_id,"agent_update":{"schema_version":1,"handoff_sha256":journal.handoff_sha256(),"manifest_sha256":self.original.handoff().target.manifest_sha256,"predecessor_record_sha256":self.original.handoff().predecessor_record_sha256,"installed_record_sha256":record_sha,"signed_health_sha256":health_sha,"activation_floor":namespace.record().record().activation_floor,"rolled_back":false}}),
};
update_handoff::finalize(
db,
&guard,
self.original.permit(),
journal.head_sha256(),
u64::try_from(self.clock.now()?.timestamp())?,
&outcome,
)?;
let bytes: Vec<u8> = db.query_row(
"SELECT outcome FROM execution_evidence WHERE grant_id=?1",
[self.original.permit().grant_id.to_string()],
|r| r.get(0),
)?;
Ok(bytes)
}
}
impl FullUpdateGuard<'_> {
pub fn finalize_restored(&mut self, db: &mut Connection) -> Result<Vec<u8>> {
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<Option<(Vec<u8>, Vec<u8>)>> {
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<String> = 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::<Vec<_>>();
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<u8>, Vec<u8>) = db.query_row(
"SELECT request,response FROM helper_sync_decisions WHERE decision_id=?1",
[id.to_string()],
|r| Ok((r.get(0)?, r.get(1)?)),
)?;
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)))
}
}