Implement guarded Linux updater runtime and compatible baseline tooling
Rust development slice / linux (push) Failing after 8s

This commit is contained in:
2026-11-16 15:25:46 +01:00
parent 6497c79a91
commit 96a2b6e5ae
44 changed files with 6986 additions and 1024 deletions
@@ -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<u8>,
pin: Vec<u8>,
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<rmm_domain::agent_update_provisioning::LocalServicePin>,
},
ValidatePublicBundle {
record: Vec<u8>,
policy: Vec<u8>,
keyring: Vec<u8>,
health_pin: Vec<u8>,
},
CanonicalRelease {
manifest: ReleaseManifest,
},
CanonicalRecord {
record: InstalledReleaseRecord,
},
CanonicalPolicy {
policy: Box<LocalUpdatePolicy>,
},
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<serde_json::Value> {
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<Connection> {
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<u8>, Vec<u8>) = 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<u8> = 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::<Vec<_>>(),"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::<Input>(&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)
}
}
+131 -24
View File
@@ -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<Vec<u8>>,
pub claim: Option<NativeEffectClaim>,
pub original_outcome: Option<Vec<u8>>,
pub pre_stop_sync: Option<(Vec<u8>, Vec<u8>)>,
}
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::<Vec<_>>();
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)
+175 -18
View File
@@ -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<Self> {
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<Vec<rmm_updater_linux::files::Installed>> {
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)?;
+104
View File
@@ -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<Role>,
poisoned: bool,
}
impl<'w, 'a, 's> RollbackFence<'w, 'a, 's> {
pub fn new(ledger: &'w mut OwnedLedger<'a, 's>) -> Result<Self> {
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(())
}
}
+66
View File
@@ -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
+75
View File
@@ -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<rmm_domain::agent_update::LinuxFullTransition> {
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.
+22 -751
View File
@@ -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<u8>],
active_raw: &[u8],
) -> Result<InstalledReleaseRecord> {
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<Vec<u8>> = 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<UpdateKeyring> {
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<BTreeSet<Uuid>> {
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<Vec<u8>> = row.get(1)?;
let outcome: Option<Vec<u8>> = row.get(2)?;
let finished: Option<Vec<u8>> = 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<Utc> = 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<Uuid> {
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<u8>>(1)?,
r.get::<_, Vec<u8>>(2)?,
r.get::<_, Option<Vec<u8>>>(3)?,
r.get::<_, Option<Vec<u8>>>(4)?,
))
})?
.collect::<rusqlite::Result<Vec<_>>>()?;
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<Utc> = 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<String>) = 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<u8>>(0))?
.collect::<rusqlite::Result<Vec<_>>>()?;
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<u8>],
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::<Event>(raw)?)))
.collect::<Result<BTreeMap<_, _>>>()?;
let time = |predicate: &dyn Fn(&Evidence) -> bool| -> Result<u64> {
let entries = parsed
.values()
.filter(|event| predicate(&event.evidence))
.collect::<Vec<_>>();
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<Utc>)> = None;
while let Some(row) = rows.next()? {
let key: String = row.get(0)?;
let raw: Vec<u8> = 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<String> = row.get(3)?;
let observation: Option<String> = row.get(4)?;
let when: Option<String> = 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<Utc> = 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<Utc> = 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<Utc>, DateTime<Utc>)> {
let value: Option<(Vec<u8>,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<Utc> = 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::*;
+274
View File
@@ -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<String> {
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<rmm_domain::agent_update::UpdateInflightProof> {
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<PreparedReservation> {
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<String> {
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"),
}
}
}
+106
View File
@@ -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<Binding> {
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);
}
+197 -3
View File
@@ -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<Vec<u8>> {
}
}
/// 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<u8>,
pub reader_registry_sha256: String,
pub services: Vec<rmm_domain::agent_update_provisioning::LocalServicePin>,
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<BootstrapMetadata> {
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::<Vec<_>>();
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<rmm_domain::agent_update_provisioning::LocalServicePin> {
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::<Result<Vec<_>>>()?;
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(&registry)?;
ensure!(
registry_bytes.len() <= MAX_BYTES as usize,
"UPDATE_BOOTSTRAP_REGISTRY_LIMIT"
);
budget.check()?;
Ok(BootstrapMetadata {
reader_registry_sha256: hash(&registry_bytes),
registry_bytes,
services,
updater_service,
})
}
#[cfg(test)]
mod tests {
use super::*;
+10 -35
View File
@@ -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);
+6 -1
View File
@@ -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)
+154 -16
View File
@@ -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<bool> {
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<Service>,
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<ConditionalStopFence<'a, 'b>> {
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<()> {
+153 -48
View File
@@ -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<Self> {
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<Self> {
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<Self> {
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<chrono::Utc>,
) -> Result<Self> {
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,