Implement guarded Linux updater runtime and compatible baseline tooling
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -9,8 +9,10 @@ pub mod readiness;
|
||||
pub mod reconcile;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub mod update_admission;
|
||||
pub mod update_control;
|
||||
pub mod update_guard;
|
||||
pub mod update_observer;
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
use openssl::pkey::{Id, PKey};
|
||||
|
||||
@@ -15,7 +15,7 @@ use std::{
|
||||
net::{UnixListener, UnixStream},
|
||||
},
|
||||
},
|
||||
path::PathBuf,
|
||||
path::{Path, PathBuf},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
@@ -133,6 +133,7 @@ impl Config {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum Incoming {
|
||||
UpdateObserve(rmm_privileged_helper::update_observer::Request),
|
||||
NativeUpdate(rmm_agent_core::update_handoff::NativeControlRequest),
|
||||
Control(control::Request),
|
||||
Probe(rmm_privileged_helper::readiness::Request),
|
||||
@@ -170,6 +171,17 @@ fn policy(config: &Config, job: &rmm_domain::jobs::JobPayload) -> Result<()> {
|
||||
job.execution_identity == "root",
|
||||
"HELPER_REQUIRES_EXPLICIT_ROOT_IDENTITY"
|
||||
);
|
||||
if matches!(job.action, Action::UpdateAgent { .. }) {
|
||||
ensure!(
|
||||
config.profile == HelperProfile::FullMaintenanceV1
|
||||
&& config.agent_uid == 993
|
||||
&& config.socket
|
||||
== std::path::Path::new("/run/rmm-helper-maintenance/control.sock")
|
||||
&& std::path::Path::new("/etc/rmm-agent-updater/policy.json").try_exists()?,
|
||||
"UPDATE_REQUIRES_SEPARATELY_PROVISIONED_FULL"
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
if rmm_privileged_helper::apt::is_apt_action(job) {
|
||||
ensure!(
|
||||
config.profile == HelperProfile::AptOnly,
|
||||
@@ -386,6 +398,44 @@ fn handle(
|
||||
ensure!(size <= 512 * 1024, "JOB_REQUEST_SIZE");
|
||||
request
|
||||
}
|
||||
Incoming::UpdateObserve(request) => {
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
ensure!(
|
||||
size <= 65536
|
||||
&& config.profile == HelperProfile::FullMaintenanceV1
|
||||
&& config.agent_uid == 993
|
||||
&& config.socket == Path::new("/run/rmm-helper-maintenance/control.sock"),
|
||||
"UPDATE_OBSERVER_ONLY_FULL"
|
||||
);
|
||||
let guard = reconcile::ExecutorGuard::acquire(&config.state, 0)?;
|
||||
let key_path = config
|
||||
.signing_key_file
|
||||
.as_deref()
|
||||
.unwrap_or(Path::new("/etc/rmm-agent-updater/job-signing-key.json"));
|
||||
let policy = config.general_policy()?.sha256()?;
|
||||
let response = rmm_privileged_helper::update_observer::collect(
|
||||
db,
|
||||
rmm_privileged_helper::update_observer::Local {
|
||||
executor: &guard,
|
||||
binding: &config.binding,
|
||||
clock: update_clock.ok_or_else(|| anyhow::anyhow!("UPDATE_NOT_CONFIGURED"))?,
|
||||
sync,
|
||||
sync_clock,
|
||||
key_path,
|
||||
policy_sha256: &policy,
|
||||
helper_sha256: binary_sha256
|
||||
.as_deref()
|
||||
.ok_or_else(|| anyhow::anyhow!("UPDATE_FULL_BINARY_PIN_REQUIRED"))?,
|
||||
},
|
||||
&request,
|
||||
)?;
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
let bytes = serde_json::to_vec(&response)?;
|
||||
ensure!(bytes.len() <= 192 * 1024, "UPDATE_OBSERVER_REPLY_LIMIT");
|
||||
stream.write_all(&u32::try_from(bytes.len())?.to_be_bytes())?;
|
||||
stream.write_all(&bytes)?;
|
||||
return Ok(());
|
||||
}
|
||||
Incoming::Control(request) => {
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
let (challenge, identity) = request.binding();
|
||||
@@ -534,6 +584,65 @@ fn handle(
|
||||
// Both profiles use these exact same inodes, locked per effect rather than
|
||||
// daemon lifetime so the dedicated and general helper can coexist.
|
||||
let resource = reconcile::ExecutorGuard::acquire(&config.state, 0)?;
|
||||
if matches!(job.action, Action::UpdateAgent { .. }) {
|
||||
let consumed: bool = db.query_row("SELECT EXISTS(SELECT 1 FROM consumed WHERE grant_id=?1 OR (agent=?2 AND generation=?3 AND job=?4 AND attempt=?5))",
|
||||
rusqlite::params![permit.grant_id.to_string(),permit.agent_id.to_string(),permit.enrollment_generation,permit.job_id.to_string(),permit.attempt_id.to_string()],|r|r.get(0))?;
|
||||
ensure!(!consumed, "UPDATE_ALREADY_CONSUMED_NO_REDISPATCH");
|
||||
let key_path = config
|
||||
.signing_key_file
|
||||
.clone()
|
||||
.unwrap_or_else(|| PathBuf::from("/etc/rmm-agent-updater/job-signing-key.json"));
|
||||
private_path(&key_path, 0, false)?;
|
||||
ensure!(
|
||||
fs::metadata(&key_path)?.len() <= 4096,
|
||||
"UPDATE_JOB_KEY_RECORD_LIMIT"
|
||||
);
|
||||
let key = rmm_privileged_helper::SigningKey::read(&key_path, 0)?;
|
||||
ensure!(
|
||||
key.public_key_hex == config.binding.public_key_hex,
|
||||
"UPDATE_LOCAL_JOB_KEY_CHANGED"
|
||||
);
|
||||
let key_hash = rmm_domain::jobs::hash(&fs::read(&key_path)?);
|
||||
let prepared = rmm_privileged_helper::update_admission::Prepared::new(
|
||||
db,
|
||||
rmm_privileged_helper::update_admission::Local {
|
||||
executor: &resource,
|
||||
binding: &config.binding,
|
||||
clock: update_clock.ok_or_else(|| anyhow::anyhow!("UPDATE_NOT_CONFIGURED"))?,
|
||||
job_key_path: &key_path,
|
||||
job_key_raw_sha256: &key_hash,
|
||||
},
|
||||
&request,
|
||||
)?;
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
let mut probe = 0u8;
|
||||
// Preparation may copy sizeable exact artifacts. Recheck the actual
|
||||
// caller at the original consumption boundary without consuming bytes.
|
||||
// SAFETY: live socket and a writable byte; MSG_PEEK leaves input intact.
|
||||
let available = unsafe {
|
||||
libc::recv(
|
||||
stream.as_raw_fd(),
|
||||
(&mut probe as *mut u8).cast(),
|
||||
1,
|
||||
libc::MSG_PEEK | libc::MSG_DONTWAIT,
|
||||
)
|
||||
};
|
||||
ensure!(
|
||||
available < 0
|
||||
&& std::io::Error::last_os_error().kind() == std::io::ErrorKind::WouldBlock,
|
||||
"UPDATE_CALLER_CANCELLED_OR_DISCONNECTED"
|
||||
);
|
||||
let accepted = prepared.consume(db, sync, sync_clock)?;
|
||||
let bytes = serde_json::to_vec(&accepted)?;
|
||||
ensure!(bytes.len() <= 4096, "UPDATE_HANDOFF_ACK_LIMIT");
|
||||
stream.write_all(&u32::try_from(bytes.len())?.to_be_bytes())?;
|
||||
stream.write_all(&bytes)?;
|
||||
stream.flush()?;
|
||||
// Full resumes its listener immediately after this once-only Activate
|
||||
// reply. The worker then reads this same original ledger via Full before
|
||||
// it may stop Agent/General/Full. This is never a final job Outcome.
|
||||
return prepared.dispatch_after_reply();
|
||||
}
|
||||
let previous: Option<Option<Vec<u8>>> = db
|
||||
.query_row(
|
||||
"SELECT COALESCE(result,(SELECT outcome FROM execution_evidence WHERE execution_evidence.grant_id=consumed.grant_id)) FROM consumed WHERE grant_id=?1",
|
||||
|
||||
@@ -0,0 +1,767 @@
|
||||
//! PRE-consumption Full admission. Protected local provisioning and exact
|
||||
//! approved artifact bytes are verified before the ordinary HelperSync gate
|
||||
//! atomically writes the original consumption/start/handoff/delegation.
|
||||
use crate::{
|
||||
Binding, Request,
|
||||
reconcile::{self, ExecutorGuard},
|
||||
};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
use rmm_agent_core::{
|
||||
helper_reconcile::ExecutorLedgerGuard,
|
||||
reconcile::SyncClock,
|
||||
update_handoff::{PreparedHandoff, UpdateAdmissionGuard},
|
||||
};
|
||||
use rmm_domain::{
|
||||
agent_update::{
|
||||
ParticipantIdentity, ParticipantRole, PeerPinTransition, ResolvedAgentUpdate,
|
||||
UpdateDelegation,
|
||||
},
|
||||
jobs::{Action, JobPayload, hash},
|
||||
};
|
||||
use rmm_updater_handoff::{
|
||||
AttemptBinding, CodePair, FileRole, FileTransition, Handoff, PinTransition,
|
||||
};
|
||||
use rmm_updater_linux::{
|
||||
Budget,
|
||||
files::{Directory, HeldFile, Identity, LedgerIdentity, Role, StageDescriptor, Staged},
|
||||
ipc,
|
||||
services::{HeldService, Service, ServicePolicy, Systemd},
|
||||
};
|
||||
use rmm_updater_worker::{
|
||||
bundle,
|
||||
clock::NativeClock,
|
||||
files::FileWitnesses,
|
||||
full::FullFiles,
|
||||
reader_registry::QualifiedRegistry,
|
||||
retained::{RetainedFileSpec, RetainedSet, Retention},
|
||||
trust::TrustState,
|
||||
};
|
||||
use rusqlite::Connection;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
ffi::OsStr,
|
||||
fs::{File, OpenOptions},
|
||||
os::unix::{
|
||||
fs::{MetadataExt, OpenOptionsExt},
|
||||
net::UnixStream,
|
||||
},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Nonterminal acknowledgement only. Agent must keep the original job running
|
||||
/// without storing an Outcome; the worker may now stop the Agent and Full.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Accepted {
|
||||
pub schema_version: u32,
|
||||
pub operation: String,
|
||||
pub grant_id: Uuid,
|
||||
pub owner_nonce: Uuid,
|
||||
pub handoff_sha256: String,
|
||||
pub original_request_sha256: String,
|
||||
pub ready: bool,
|
||||
pub terminal: bool,
|
||||
}
|
||||
impl Accepted {
|
||||
pub fn matches(&self, request: &Request, grant: Uuid) -> Result<()> {
|
||||
ensure!(
|
||||
self.schema_version == 1
|
||||
&& self.operation == "agent_update_handoff_accepted_v1"
|
||||
&& self.grant_id == grant
|
||||
&& !self.owner_nonce.is_nil()
|
||||
&& rmm_domain::agent_update::digest(&self.handoff_sha256)
|
||||
&& self.original_request_sha256 == hash(&serde_json::to_vec(request)?)
|
||||
&& !self.ready
|
||||
&& !self.terminal,
|
||||
"UPDATE_TYPED_HANDOFF_ACK"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
struct Protected {
|
||||
directory: Directory,
|
||||
file: HeldFile,
|
||||
path: PathBuf,
|
||||
maximum: u64,
|
||||
}
|
||||
impl Protected {
|
||||
fn read(path: &Path, maximum: u64) -> Result<(Self, Vec<u8>)> {
|
||||
let directory =
|
||||
Directory::open(path.parent().context("UPDATE_ADMISSION_PARENT")?, 0, false)?;
|
||||
let mut file = directory.inspect(
|
||||
path.file_name().context("UPDATE_ADMISSION_NAME")?,
|
||||
0,
|
||||
maximum,
|
||||
)?;
|
||||
let bytes = file.bytes(maximum)?;
|
||||
Ok((
|
||||
Self {
|
||||
directory,
|
||||
file,
|
||||
path: path.into(),
|
||||
maximum,
|
||||
},
|
||||
bytes,
|
||||
))
|
||||
}
|
||||
fn verify(&self) -> Result<()> {
|
||||
self.directory.file(
|
||||
self.path.file_name().context("UPDATE_ADMISSION_NAME")?,
|
||||
self.file.proof(),
|
||||
self.maximum,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn identity(m: &std::fs::Metadata) -> Identity {
|
||||
Identity {
|
||||
device: m.dev(),
|
||||
inode: m.ino(),
|
||||
uid: m.uid(),
|
||||
gid: m.gid(),
|
||||
mode: m.mode(),
|
||||
links: m.nlink(),
|
||||
}
|
||||
}
|
||||
fn participant(service: &HeldService) -> Result<ParticipantIdentity> {
|
||||
let p = service.proof();
|
||||
ensure!(p.process.uid == 0, "UPDATE_ADMISSION_ROOT_PARTICIPANT");
|
||||
if p.service == Service::Updater {
|
||||
ensure!(p.process.gid == 0, "UPDATE_STABLE_UPDATER_GROUP");
|
||||
}
|
||||
let role = match p.service {
|
||||
Service::Full => ParticipantRole::LinuxFullHelper,
|
||||
Service::Updater => ParticipantRole::LinuxUpdater,
|
||||
_ => anyhow::bail!("UPDATE_ADMISSION_PARTICIPANT_ROLE"),
|
||||
};
|
||||
Ok(ParticipantIdentity {
|
||||
role,
|
||||
pid: p.process.pid,
|
||||
birth: p.process.birth,
|
||||
image_sha256: p.process.image_sha256.clone(),
|
||||
security_principal: "uid:0".into(),
|
||||
service_instance_sha256: hash(&serde_json::to_vec(p)?),
|
||||
})
|
||||
}
|
||||
pub struct Local<'a> {
|
||||
pub executor: &'a ExecutorGuard,
|
||||
pub binding: &'a Binding,
|
||||
pub clock: &'a NativeClock,
|
||||
pub job_key_path: &'a Path,
|
||||
pub job_key_raw_sha256: &'a str,
|
||||
}
|
||||
pub struct Prepared<'a> {
|
||||
local: Local<'a>,
|
||||
request: Request,
|
||||
update: ResolvedAgentUpdate,
|
||||
header: Handoff,
|
||||
delegation: UpdateDelegation,
|
||||
trust: TrustState,
|
||||
registry: QualifiedRegistry,
|
||||
files: FileWitnesses,
|
||||
retained: RetainedSet,
|
||||
ledger: LedgerIdentity,
|
||||
ledger_file: File,
|
||||
ledger_directory: Directory,
|
||||
sqlite_handle: usize,
|
||||
full: HeldService,
|
||||
updater: HeldService,
|
||||
full_files: RefCell<FullFiles>,
|
||||
_stages: [Staged; 4],
|
||||
protected: Vec<Protected>,
|
||||
budget: Budget,
|
||||
}
|
||||
impl<'a> Prepared<'a> {
|
||||
pub fn new(db: &Connection, local: Local<'a>, request: &Request) -> Result<Self> {
|
||||
local.executor.check(db)?;
|
||||
let (job, permit) = local.binding.verify(request, local.clock.now()?)?;
|
||||
let Action::UpdateAgent { update } = &job.action else {
|
||||
anyhow::bail!("UPDATE_ACTION_REQUIRED")
|
||||
};
|
||||
ensure!(
|
||||
job.execution_identity == "root" && update.validate() && job.max_runtime_seconds == 600,
|
||||
"UPDATE_ADMISSION_ACTION"
|
||||
);
|
||||
let trust = TrustState::load_prepared(update, local.binding, local.clock)?;
|
||||
trust.qualify_history(db, local.executor, local.binding)?;
|
||||
let boot: Uuid = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
|
||||
.trim()
|
||||
.parse()?;
|
||||
// This is preparation time within the existing start window, not a new
|
||||
// 600s execution lease. The immutable 600s handoff is stamped last.
|
||||
let budget = Budget::new(boot, (permit.start_before - local.clock.now()?).to_std()?)?;
|
||||
let registry = QualifiedRegistry::load(trust.policy())?;
|
||||
registry.verify_admission(trust.policy(), &budget)?;
|
||||
let policies = trust
|
||||
.policy()
|
||||
.services
|
||||
.iter()
|
||||
.zip([
|
||||
Service::Agent,
|
||||
Service::General,
|
||||
Service::Full,
|
||||
Service::Apt,
|
||||
])
|
||||
.map(|(pin, service)| ServicePolicy {
|
||||
service,
|
||||
unit_sha256: pin.unit_sha256.clone(),
|
||||
image_sha256: if service == Service::Agent {
|
||||
update.predecessor.agent_sha256.clone()
|
||||
} else {
|
||||
update.predecessor.helper_sha256.clone()
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
let services = Systemd::new(policies)?;
|
||||
let full = services.observe(Service::Full, &budget)?;
|
||||
ensure!(
|
||||
full.proof().process.pid == std::process::id(),
|
||||
"UPDATE_ACTUAL_FULL_PROCESS_REQUIRED"
|
||||
);
|
||||
let updater = Systemd::observe_updater(
|
||||
ServicePolicy {
|
||||
service: Service::Updater,
|
||||
image_sha256: update.updater.binary_sha256.clone(),
|
||||
unit_sha256: trust.policy().updater_service.unit_sha256.clone(),
|
||||
},
|
||||
&budget,
|
||||
)?;
|
||||
let description = ipc::exchange(
|
||||
UnixStream::connect("/run/rmm-agent-updater/control.sock")?,
|
||||
&updater,
|
||||
&budget,
|
||||
&ipc::Request::Describe {},
|
||||
)?;
|
||||
ensure!(
|
||||
matches!(description.state, ipc::State::Described)
|
||||
&& description.updater == participant(&updater)?
|
||||
&& description.head_sha256.is_none(),
|
||||
"UPDATE_ADMISSION_LIVE_UPDATER"
|
||||
);
|
||||
let nonce = Uuid::new_v4();
|
||||
let manifest = update.manifest().map_err(anyhow::Error::msg)?;
|
||||
let agent = stage_download(
|
||||
&job,
|
||||
Role::Agent,
|
||||
&manifest.artifacts[0].sha256,
|
||||
manifest.artifacts[0].bytes,
|
||||
nonce,
|
||||
&budget,
|
||||
)?;
|
||||
let helper = stage_download(
|
||||
&job,
|
||||
Role::Helper,
|
||||
&manifest.artifacts[1].sha256,
|
||||
manifest.artifacts[1].bytes,
|
||||
nonce,
|
||||
&budget,
|
||||
)?;
|
||||
let mut full_files = FullFiles::open(
|
||||
update,
|
||||
local.job_key_path,
|
||||
local.job_key_raw_sha256,
|
||||
&budget,
|
||||
)?;
|
||||
let [full_config, full_pin] = full_files.stage(nonce, &budget)?;
|
||||
let stages = [agent, helper, full_config, full_pin];
|
||||
let files = FileWitnesses::capture(&stages, nonce, &budget)?;
|
||||
let (retained_config, raw) = Protected::read(
|
||||
Path::new("/etc/rmm-agent-updater/retained-files.json"),
|
||||
65536,
|
||||
)?;
|
||||
let mut specs: Vec<RetainedFileSpec> = serde_json::from_slice(&raw)?;
|
||||
for (path, retention) in [
|
||||
(
|
||||
"/var/lib/rmm-agent-remote/journal.sqlite",
|
||||
Retention::MutableDatabase,
|
||||
),
|
||||
(
|
||||
"/var/lib/rmm-agent-remote/consumption.sqlite",
|
||||
Retention::MutableDatabase,
|
||||
),
|
||||
(
|
||||
"/var/lib/rmm-agent-remote/agent.lock",
|
||||
Retention::ImmutableMetadata,
|
||||
),
|
||||
(
|
||||
"/var/lib/rmm-helper/consumption.sqlite",
|
||||
Retention::MutableDatabase,
|
||||
),
|
||||
(
|
||||
"/var/lib/rmm-helper/executor.lock",
|
||||
Retention::ImmutableMetadata,
|
||||
),
|
||||
(
|
||||
"/var/lib/rmm-agent-remote/identity.json",
|
||||
Retention::ImmutableMetadata,
|
||||
),
|
||||
(
|
||||
"/var/lib/rmm-agent-remote/device.key",
|
||||
Retention::ImmutableMetadata,
|
||||
),
|
||||
(
|
||||
"/var/lib/rmm-agent-remote/device.pem",
|
||||
Retention::ImmutableMetadata,
|
||||
),
|
||||
] {
|
||||
ensure!(
|
||||
specs
|
||||
.iter()
|
||||
.any(|s| s.path == Path::new(path) && s.retention == retention),
|
||||
"UPDATE_MANDATORY_RETAINED_STATE"
|
||||
);
|
||||
}
|
||||
ensure!(
|
||||
specs.iter().any(
|
||||
|s| s.path == local.job_key_path && s.retention == Retention::ImmutableMetadata
|
||||
),
|
||||
"UPDATE_RETAINED_JOB_KEY_REQUIRED"
|
||||
);
|
||||
retain_active_tls(&mut specs, local.binding)?;
|
||||
let retained = RetainedSet::capture(specs)?;
|
||||
let ledger_directory = Directory::open(Path::new("/var/lib/rmm-helper"), 0, true)?;
|
||||
let ledger_file = OpenOptions::new()
|
||||
.read(true)
|
||||
.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC)
|
||||
.open("/var/lib/rmm-helper/consumption.sqlite")?;
|
||||
let ledger = LedgerIdentity {
|
||||
ledger: identity(&ledger_file.metadata()?),
|
||||
lock: identity(&local.executor._file.metadata()?),
|
||||
};
|
||||
ensure!(
|
||||
ledger.ledger
|
||||
== identity(&std::fs::symlink_metadata(
|
||||
"/var/lib/rmm-helper/consumption.sqlite"
|
||||
)?)
|
||||
&& ledger.lock
|
||||
== identity(&std::fs::symlink_metadata(
|
||||
"/var/lib/rmm-helper/executor.lock"
|
||||
)?),
|
||||
"UPDATE_ADMISSION_LEDGER_IDENTITIES"
|
||||
);
|
||||
let (general, raw) =
|
||||
Protected::read(Path::new("/etc/rmm-remote/general-helper-proof.json"), 8192)?;
|
||||
let PeerPinTransition::LinuxGeneralFullV1 {
|
||||
general_pin_sha256,
|
||||
full: transition,
|
||||
} = &update.peer_pin_transition
|
||||
else {
|
||||
anyhow::bail!("UPDATE_FULL_TRANSITION_REQUIRED")
|
||||
};
|
||||
ensure!(
|
||||
hash(&raw) == *general_pin_sha256,
|
||||
"UPDATE_GENERAL_PIN_CHANGED"
|
||||
);
|
||||
let (helper_journal,checkpoint,decision,agent_journal,generation):(String,u64,String,String,u64)=db.query_row(
|
||||
"SELECT journal_id,checkpoint,last_decision,agent_journal_id,generation FROM helper_sync_meta WHERE id=1 AND fault IS NULL",[],
|
||||
|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?)))?;
|
||||
let mut stop_set = vec![
|
||||
rmm_updater_handoff::Service::LinuxAgent,
|
||||
rmm_updater_handoff::Service::LinuxGeneralHelper,
|
||||
rmm_updater_handoff::Service::LinuxFullHelper,
|
||||
];
|
||||
if trust
|
||||
.policy()
|
||||
.services
|
||||
.iter()
|
||||
.any(|pin| pin.unit == Service::Apt.unit())
|
||||
{
|
||||
stop_set.push(rmm_updater_handoff::Service::LinuxAptHelper);
|
||||
}
|
||||
let now = local.clock.now()?;
|
||||
let consumed_at = u64::try_from(now.timestamp())?;
|
||||
let start = rmm_agent_core::ledger::start_witness_bytes(&permit, request, boot, now)?;
|
||||
let header = Handoff {
|
||||
schema_version: 2,
|
||||
attempt: AttemptBinding {
|
||||
tenant_id: permit.tenant_id,
|
||||
asset_id: permit.asset_id,
|
||||
agent_id: permit.agent_id,
|
||||
enrollment_generation: permit.enrollment_generation,
|
||||
assignment_version: permit.assignment_version,
|
||||
plan_id: permit
|
||||
.agent_update_plan_id
|
||||
.context("UPDATE_PLAN_ID_REQUIRED")?,
|
||||
plan_sha256: permit.plan_hash.clone(),
|
||||
job_id: permit.job_id,
|
||||
attempt_id: permit.attempt_id,
|
||||
job_payload_sha256: permit.job_payload_hash.clone(),
|
||||
grant_id: permit.grant_id,
|
||||
authorization_id: permit.authorization_id,
|
||||
original_request_sha256: hash(&serde_json::to_vec(request)?),
|
||||
original_start_witness_sha256: hash(&start),
|
||||
},
|
||||
agent_journal_id: agent_journal.parse()?,
|
||||
helper_journal_id: helper_journal.parse()?,
|
||||
helper_checkpoint: checkpoint,
|
||||
previous_decision_id: decision.parse()?,
|
||||
boot_id: boot,
|
||||
connection_generation: generation,
|
||||
owner_nonce: nonce,
|
||||
updater_sha256: update.updater.binary_sha256.clone(),
|
||||
updater_policy_sha256: update.updater.policy_sha256.clone(),
|
||||
predecessor_record_sha256: update.predecessor.installed_record_sha256.clone(),
|
||||
retained_state_sha256: retained.sha256().into(),
|
||||
ledger_identity_sha256: ledger.sha256()?,
|
||||
staged_files_sha256: files.sha256().into(),
|
||||
predecessor: CodePair {
|
||||
manifest_sha256: update.predecessor.manifest_sha256.clone(),
|
||||
agent_sha256: update.predecessor.agent_sha256.clone(),
|
||||
helper_sha256: update.predecessor.helper_sha256.clone(),
|
||||
pin_sha256: transition.old_pin_sha256.clone(),
|
||||
},
|
||||
target: CodePair {
|
||||
manifest_sha256: update.manifest_sha256.clone(),
|
||||
agent_sha256: manifest.artifacts[0].sha256.clone(),
|
||||
helper_sha256: manifest.artifacts[1].sha256.clone(),
|
||||
pin_sha256: transition.new_pin_sha256.clone(),
|
||||
},
|
||||
pin_transition: PinTransition::LinuxGeneralFullV1,
|
||||
pin_transition_sha256: hash(&serde_json::to_vec(&update.peer_pin_transition)?),
|
||||
files: vec![
|
||||
FileTransition {
|
||||
role: FileRole::AgentBinary,
|
||||
old_sha256: update.predecessor.agent_sha256.clone(),
|
||||
new_sha256: manifest.artifacts[0].sha256.clone(),
|
||||
},
|
||||
FileTransition {
|
||||
role: FileRole::HelperBinary,
|
||||
old_sha256: update.predecessor.helper_sha256.clone(),
|
||||
new_sha256: manifest.artifacts[1].sha256.clone(),
|
||||
},
|
||||
FileTransition {
|
||||
role: FileRole::LinuxFullConfig,
|
||||
old_sha256: transition.old_config_sha256.clone(),
|
||||
new_sha256: transition.new_config_sha256.clone(),
|
||||
},
|
||||
FileTransition {
|
||||
role: FileRole::LinuxFullPin,
|
||||
old_sha256: transition.old_pin_sha256.clone(),
|
||||
new_sha256: transition.new_pin_sha256.clone(),
|
||||
},
|
||||
],
|
||||
stop_set,
|
||||
consumed_at,
|
||||
finish_before: consumed_at
|
||||
.checked_add(600)
|
||||
.context("UPDATE_FINISH_OVERFLOW")?,
|
||||
};
|
||||
let delegation = UpdateDelegation {
|
||||
schema_version: 1,
|
||||
boot_id: boot,
|
||||
owner_nonce: nonce,
|
||||
handoff_sha256: hash(&header.bytes()?),
|
||||
original_helper: participant(&full)?,
|
||||
updater: participant(&updater)?,
|
||||
prepared_at: now,
|
||||
};
|
||||
bundle::write_before_handoff(
|
||||
&header,
|
||||
&files,
|
||||
&retained,
|
||||
&ledger,
|
||||
bundle::Participants {
|
||||
delegation: &delegation,
|
||||
original_full: &full,
|
||||
updater: &updater,
|
||||
budget: &budget,
|
||||
},
|
||||
)?;
|
||||
let prepared = Self {
|
||||
local,
|
||||
request: request.clone(),
|
||||
update: update.as_ref().clone(),
|
||||
header,
|
||||
delegation,
|
||||
trust,
|
||||
registry,
|
||||
files,
|
||||
retained,
|
||||
ledger,
|
||||
ledger_file,
|
||||
ledger_directory,
|
||||
sqlite_handle: unsafe { db.handle() } as usize,
|
||||
full,
|
||||
updater,
|
||||
full_files: RefCell::new(full_files),
|
||||
_stages: stages,
|
||||
protected: vec![retained_config, general],
|
||||
budget,
|
||||
};
|
||||
prepared.check(db)?;
|
||||
Ok(prepared)
|
||||
}
|
||||
pub fn consume(
|
||||
&self,
|
||||
db: &mut Connection,
|
||||
sync: &mut reconcile::HelperSync,
|
||||
clock: &reconcile::NativeClock,
|
||||
) -> Result<Accepted> {
|
||||
let prepared = PreparedHandoff {
|
||||
request: &self.request,
|
||||
handoff: &self.header,
|
||||
delegation: &self.delegation,
|
||||
recorded_at: self.delegation.prepared_at,
|
||||
};
|
||||
sync.begin_update_handoff(
|
||||
db,
|
||||
self,
|
||||
self.header.connection_generation,
|
||||
&prepared,
|
||||
clock,
|
||||
&reconcile::NativeBoot,
|
||||
)?;
|
||||
Ok(Accepted {
|
||||
schema_version: 1,
|
||||
operation: "agent_update_handoff_accepted_v1".into(),
|
||||
grant_id: self.header.attempt.grant_id,
|
||||
owner_nonce: self.header.owner_nonce,
|
||||
handoff_sha256: hash(&self.header.bytes()?),
|
||||
original_request_sha256: self.header.attempt.original_request_sha256.clone(),
|
||||
ready: false,
|
||||
terminal: false,
|
||||
})
|
||||
}
|
||||
/// Exactly once, AFTER the caller flushed Accepted and before returning to
|
||||
/// Full's listener. A transport error is not retried and creates no Outcome.
|
||||
pub fn dispatch_after_reply(self) -> Result<()> {
|
||||
let response = ipc::exchange(
|
||||
UnixStream::connect("/run/rmm-agent-updater/control.sock")?,
|
||||
&self.updater,
|
||||
&self.budget,
|
||||
&ipc::Request::Activate {
|
||||
grant_id: self.header.attempt.grant_id,
|
||||
owner_nonce: self.header.owner_nonce,
|
||||
handoff_sha256: hash(&self.header.bytes()?),
|
||||
},
|
||||
)?;
|
||||
ensure!(
|
||||
matches!(response.state, ipc::State::Inflight),
|
||||
"UPDATE_ACTIVATION_ACK_REQUIRED"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl ExecutorLedgerGuard for Prepared<'_> {
|
||||
fn check(&self, db: &Connection) -> Result<()> {
|
||||
self.local.executor.check(db)?;
|
||||
ensure!(
|
||||
unsafe { db.handle() } as usize == self.sqlite_handle,
|
||||
"UPDATE_PREPARED_SAME_CONNECTION"
|
||||
);
|
||||
self.ledger_directory.verify()?;
|
||||
ensure!(
|
||||
identity(&self.ledger_file.metadata()?) == self.ledger.ledger
|
||||
&& identity(&std::fs::symlink_metadata(
|
||||
"/var/lib/rmm-helper/consumption.sqlite"
|
||||
)?) == self.ledger.ledger
|
||||
&& identity(&self.local.executor._file.metadata()?) == self.ledger.lock
|
||||
&& identity(&std::fs::symlink_metadata(
|
||||
"/var/lib/rmm-helper/executor.lock"
|
||||
)?) == self.ledger.lock,
|
||||
"UPDATE_PREPARED_SAME_LEDGER_AND_LOCK"
|
||||
);
|
||||
self.budget.check()
|
||||
}
|
||||
}
|
||||
impl UpdateAdmissionGuard for Prepared<'_> {
|
||||
fn health_public_key(&self) -> Result<[u8; 32]> {
|
||||
Ok(self.trust.health_public_key())
|
||||
}
|
||||
fn verify_prepared_update(
|
||||
&self,
|
||||
request: &Request,
|
||||
handoff: &Handoff,
|
||||
delegation: &UpdateDelegation,
|
||||
now: DateTime<Utc>,
|
||||
) -> Result<()> {
|
||||
ensure!(
|
||||
serde_json::to_vec(request)? == serde_json::to_vec(&self.request)?
|
||||
&& handoff.bytes()? == self.header.bytes()?
|
||||
&& serde_json::to_vec(delegation)? == serde_json::to_vec(&self.delegation)?
|
||||
&& now == self.delegation.prepared_at,
|
||||
"UPDATE_PREPARED_ORIGINAL_BYTES_CHANGED"
|
||||
);
|
||||
self.budget.check()?;
|
||||
self.local
|
||||
.binding
|
||||
.verify(request, self.local.clock.now()?)?;
|
||||
self.trust
|
||||
.recheck_prepared(&self.update, self.local.clock)?;
|
||||
self.registry.verify_protected()?;
|
||||
self.full.verify(&self.budget)?;
|
||||
self.updater.verify(&self.budget)?;
|
||||
ensure!(
|
||||
participant(&self.full)? == delegation.original_helper
|
||||
&& participant(&self.updater)? == delegation.updater,
|
||||
"UPDATE_PREPARED_PARTICIPANTS_CHANGED"
|
||||
);
|
||||
self.full_files.borrow_mut().verify_original(&self.budget)?;
|
||||
self.files.reopen_stages(&self.header, &self.budget)?;
|
||||
self.retained.verify()?;
|
||||
for protected in &self.protected {
|
||||
protected.verify()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
/// Metadata only for the currently selected TLS pair. Never read or export key
|
||||
/// bytes, and never require a bootstrap rewrite after normal TLS rotation.
|
||||
fn retain_active_tls(specs: &mut Vec<RetainedFileSpec>, binding: &Binding) -> Result<()> {
|
||||
let base = Path::new("/var/lib/rmm-agent-remote/certificates");
|
||||
let dir = Directory::open_agent_state(base)?;
|
||||
let mut selector = dir.inspect(OsStr::new("current.json"), 993, 4096)?;
|
||||
let raw = selector.bytes(4096)?;
|
||||
let value: serde_json::Value = serde_json::from_slice(&raw)?;
|
||||
ensure!(
|
||||
value["schema_version"] == 1
|
||||
&& value["agent_id"] == binding.agent_id.to_string()
|
||||
&& value["enrollment_generation"] == binding.enrollment_generation,
|
||||
"UPDATE_TLS_SELECTOR_IDENTITY"
|
||||
);
|
||||
let version: Uuid = value["version"]
|
||||
.as_str()
|
||||
.context("UPDATE_TLS_SELECTOR_VERSION")?
|
||||
.parse()?;
|
||||
for path in [
|
||||
base.join("current.json"),
|
||||
base.join("versions")
|
||||
.join(version.to_string())
|
||||
.join("device.key"),
|
||||
base.join("versions")
|
||||
.join(version.to_string())
|
||||
.join("device.pem"),
|
||||
] {
|
||||
let parent = path.parent().context("UPDATE_TLS_PARENT")?;
|
||||
Directory::open_agent_state(parent)?.verify()?;
|
||||
let m = std::fs::symlink_metadata(&path)?;
|
||||
ensure!(
|
||||
m.is_file()
|
||||
&& !m.file_type().is_symlink()
|
||||
&& m.uid() == 993
|
||||
&& m.mode() & 0o777 == 0o600
|
||||
&& m.nlink() == 1,
|
||||
"UPDATE_TLS_RETAINED_METADATA"
|
||||
);
|
||||
let spec = RetainedFileSpec {
|
||||
path,
|
||||
parent_uid: 993,
|
||||
identity: identity(&m),
|
||||
retention: Retention::ImmutableMetadata,
|
||||
};
|
||||
if let Some(existing) = specs.iter().find(|s| s.path == spec.path) {
|
||||
ensure!(existing == &spec, "UPDATE_TLS_RETAINED_SELECTOR_CHANGED");
|
||||
} else {
|
||||
specs.push(spec);
|
||||
}
|
||||
}
|
||||
dir.file(OsStr::new("current.json"), selector.proof(), 4096)?;
|
||||
Ok(())
|
||||
}
|
||||
fn stage_download(
|
||||
job: &JobPayload,
|
||||
role: Role,
|
||||
expected: &str,
|
||||
length: u64,
|
||||
nonce: Uuid,
|
||||
budget: &Budget,
|
||||
) -> Result<Staged> {
|
||||
let label = match role {
|
||||
Role::Agent => "agent.bin",
|
||||
Role::Helper => "helper.bin",
|
||||
_ => anyhow::bail!("UPDATE_DOWNLOAD_ROLE"),
|
||||
};
|
||||
let parent = Path::new("/var/lib/rmm-agent-remote/update-downloads")
|
||||
.join(job.job_id.to_string())
|
||||
.join(job.attempt_id.to_string());
|
||||
let source_directory = Directory::open_agent_state(&parent)?;
|
||||
let mut source = source_directory.inspect(OsStr::new(label), 993, 256 * 1024 * 1024)?;
|
||||
ensure!(
|
||||
source.proof().bytes == length
|
||||
&& source.proof().sha256 == expected
|
||||
&& source.proof().identity.links == 1
|
||||
&& source.proof().identity.mode & 0o777 == 0o600,
|
||||
"UPDATE_PREFETCH_BYTES_REQUIRED"
|
||||
);
|
||||
let target = Path::new(role.target());
|
||||
let directory = Directory::open(target.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
|
||||
let original = directory.inspect(
|
||||
target.file_name().context("UPDATE_TARGET_NAME")?,
|
||||
0,
|
||||
256 * 1024 * 1024,
|
||||
)?;
|
||||
let Action::UpdateAgent { update } = &job.action else {
|
||||
anyhow::bail!("UPDATE_ACTION_REQUIRED")
|
||||
};
|
||||
ensure!(
|
||||
original.proof().sha256.as_str()
|
||||
== match role {
|
||||
Role::Agent => update.predecessor.agent_sha256.as_str(),
|
||||
Role::Helper => update.predecessor.helper_sha256.as_str(),
|
||||
_ => unreachable!(),
|
||||
},
|
||||
"UPDATE_PREDECESSOR_IMAGE_CHANGED"
|
||||
);
|
||||
let staged = Staged::write(
|
||||
&directory,
|
||||
StageDescriptor {
|
||||
role,
|
||||
nonce,
|
||||
original: original.proof(),
|
||||
expected_sha: expected,
|
||||
length,
|
||||
},
|
||||
source.reader()?,
|
||||
budget,
|
||||
)?;
|
||||
source_directory.file(OsStr::new(label), source.proof(), 256 * 1024 * 1024)?;
|
||||
Ok(staged)
|
||||
}
|
||||
|
||||
// Passive helper RPC client; it never creates a handoff.
|
||||
pub use crate::update_observer::{observe, observe_record};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn typed_handoff_ack_is_not_an_outcome_or_a_new_start_lease() {
|
||||
let request = Request {
|
||||
job_bytes: b"bound original job".to_vec(),
|
||||
permit_bytes: b"bound original permit".to_vec(),
|
||||
signature: vec![0; 64],
|
||||
plan_hash: "a".repeat(64),
|
||||
authorization_id: Uuid::new_v4(),
|
||||
};
|
||||
let grant = Uuid::new_v4();
|
||||
let accepted = Accepted {
|
||||
schema_version: 1,
|
||||
operation: "agent_update_handoff_accepted_v1".into(),
|
||||
grant_id: grant,
|
||||
owner_nonce: Uuid::new_v4(),
|
||||
handoff_sha256: "b".repeat(64),
|
||||
original_request_sha256: hash(&serde_json::to_vec(&request).unwrap()),
|
||||
ready: false,
|
||||
terminal: false,
|
||||
};
|
||||
let raw = serde_json::to_vec(&accepted).unwrap();
|
||||
serde_json::from_slice::<Accepted>(&raw)
|
||||
.unwrap()
|
||||
.matches(&request, grant)
|
||||
.unwrap();
|
||||
assert!(serde_json::from_slice::<crate::Outcome>(&raw).is_err());
|
||||
let mut next = request.clone();
|
||||
next.job_bytes.push(0);
|
||||
assert!(accepted.matches(&next, grant).is_err());
|
||||
assert!(accepted.matches(&request, Uuid::new_v4()).is_err());
|
||||
let mut false_result = accepted.clone();
|
||||
false_result.terminal = true;
|
||||
assert!(false_result.matches(&request, grant).is_err());
|
||||
false_result.terminal = false;
|
||||
false_result.ready = true;
|
||||
assert!(false_result.matches(&request, grant).is_err());
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use rmm_agent_core::{
|
||||
reconcile::SyncClock,
|
||||
update_handoff::{
|
||||
self as core, NativeControlOperation, NativeControlReply, NativeControlRequest,
|
||||
NativeEffect, NativeService,
|
||||
NativeEffect, NativeEffectStage, NativeService, UpdateParticipantGuard,
|
||||
},
|
||||
};
|
||||
use rmm_updater_handoff::{Evidence, Phase};
|
||||
@@ -21,7 +21,7 @@ pub fn active_grant(db: &Connection) -> Result<Option<Uuid>> {
|
||||
if !exists {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut query=db.prepare("SELECT grant_id FROM agent_update_handoffs WHERE phase NOT IN ('unknown','outcome_persisted') ORDER BY grant_id LIMIT 2")?;
|
||||
let mut query=db.prepare("SELECT grant_id FROM agent_update_handoffs WHERE phase NOT IN ('unknown','outcome_persisted','rollback_outcome_persisted') ORDER BY grant_id LIMIT 2")?;
|
||||
let rows = query
|
||||
.query_map([], |r| r.get::<_, String>(0))?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
@@ -90,7 +90,10 @@ pub fn handle(
|
||||
} => {
|
||||
let journal = core::read(db, &guard, request.grant_id)?;
|
||||
ensure!(
|
||||
journal.phase() == Phase::LedgerReturnIntent,
|
||||
matches!(
|
||||
journal.phase(),
|
||||
Phase::LedgerReturnIntent | Phase::RollbackLedgerReturnIntent
|
||||
),
|
||||
"UPDATE_START_ALREADY_OBSERVED_OR_PHASE"
|
||||
);
|
||||
let (actual, actual_hash) = guard.started()?;
|
||||
@@ -103,7 +106,11 @@ pub fn handle(
|
||||
NativeService::Full,
|
||||
NativeService::Agent,
|
||||
] {
|
||||
let effect = NativeEffect::StartService { service };
|
||||
let effect = if guard.restored() {
|
||||
NativeEffect::RollbackStartService { service }
|
||||
} else {
|
||||
NativeEffect::StartService { service }
|
||||
};
|
||||
let reserved = core::read_native_claim(db, &guard, request.grant_id, &effect)?;
|
||||
let already:bool=db.query_row("SELECT EXISTS(SELECT 1 FROM agent_update_native_observations WHERE grant_id=?1 AND claim_sha256=?2)",rusqlite::params![request.grant_id.to_string(),rmm_domain::jobs::hash(&serde_json::to_vec(&reserved)?)],|r|r.get(0))?;
|
||||
// One control request performs one sequence, never retries an
|
||||
@@ -119,28 +126,60 @@ pub fn handle(
|
||||
}
|
||||
let event = journal.prepare(
|
||||
u64::try_from(clock.now()?.timestamp())?,
|
||||
Evidence::ServicesStarted {
|
||||
services: actual,
|
||||
ledger_identity_sha256: header.ledger_identity_sha256.clone(),
|
||||
observation_sha256: actual_hash,
|
||||
if guard.restored() {
|
||||
Evidence::RollbackServicesStarted {
|
||||
services: actual,
|
||||
ledger_identity_sha256: header.ledger_identity_sha256.clone(),
|
||||
observation_sha256: actual_hash,
|
||||
}
|
||||
} else {
|
||||
Evidence::ServicesStarted {
|
||||
services: actual,
|
||||
ledger_identity_sha256: header.ledger_identity_sha256.clone(),
|
||||
observation_sha256: actual_hash,
|
||||
}
|
||||
},
|
||||
)?;
|
||||
core::append(db, &guard, request.grant_id, journal.head_sha256(), &event)?;
|
||||
if header.schema_version == 2 && !guard.restored() {
|
||||
let observation = guard.native_observation_sha256(
|
||||
&header,
|
||||
guard.original().delegation(),
|
||||
&NativeEffect::ConditionalRollbackStopGeneral,
|
||||
NativeEffectStage::ObserveNonstart,
|
||||
)?;
|
||||
core::deny_conditional_stop(
|
||||
db,
|
||||
&guard,
|
||||
request.grant_id,
|
||||
&observation,
|
||||
clock.now()?,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
NativeControlOperation::Health { receipt } => {
|
||||
guard.qualify_health(db, receipt)?;
|
||||
let journal = core::read(db, &guard, request.grant_id)?;
|
||||
let event = journal.prepare(
|
||||
u64::try_from(clock.now()?.timestamp())?,
|
||||
Evidence::HealthVerified {
|
||||
receipt: receipt.clone(),
|
||||
if guard.restored() {
|
||||
Evidence::RollbackHealthVerified {
|
||||
receipt: receipt.clone(),
|
||||
}
|
||||
} else {
|
||||
Evidence::HealthVerified {
|
||||
receipt: receipt.clone(),
|
||||
}
|
||||
},
|
||||
)?;
|
||||
core::append(db, &guard, request.grant_id, journal.head_sha256(), &event)?;
|
||||
}
|
||||
NativeControlOperation::Finalize => {
|
||||
original_outcome_base64url =
|
||||
Some(URL_SAFE_NO_PAD.encode(guard.publish_record_and_finalize(db)?));
|
||||
original_outcome_base64url = Some(URL_SAFE_NO_PAD.encode(if guard.restored() {
|
||||
guard.finalize_restored(db)?
|
||||
} else {
|
||||
guard.publish_record_and_finalize(db)?
|
||||
}));
|
||||
}
|
||||
}
|
||||
let proof = if original_outcome_base64url.is_some() {
|
||||
@@ -151,6 +190,18 @@ pub fn handle(
|
||||
.context("UPDATE_ACTIVE_PROOF_REQUIRED")?,
|
||||
)
|
||||
};
|
||||
let pre_stop = if matches!(request.method, NativeControlOperation::ReadOriginal) {
|
||||
guard.pre_stop_signed_sync(db)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let (pre_stop_sync_request_base64url, pre_stop_sync_response_base64url) = match pre_stop {
|
||||
Some((request, response)) => (
|
||||
Some(URL_SAFE_NO_PAD.encode(request)),
|
||||
Some(URL_SAFE_NO_PAD.encode(response)),
|
||||
),
|
||||
None => (None, None),
|
||||
};
|
||||
Ok(NativeControlReply {
|
||||
schema_version: 1,
|
||||
challenge: request.challenge,
|
||||
@@ -167,5 +218,7 @@ pub fn handle(
|
||||
claim,
|
||||
original_start_base64url,
|
||||
original_outcome_base64url,
|
||||
pre_stop_sync_request_base64url,
|
||||
pre_stop_sync_response_base64url,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -133,6 +133,12 @@ impl<'a> FullUpdateGuard<'a> {
|
||||
journal.phase(),
|
||||
Phase::LedgerReturnIntent | Phase::ServicesStarted | Phase::HealthVerified
|
||||
);
|
||||
let restored = matches!(
|
||||
journal.phase(),
|
||||
Phase::RollbackLedgerReturnIntent
|
||||
| Phase::RollbackServicesStarted
|
||||
| Phase::RollbackHealthVerified
|
||||
);
|
||||
let services = service_policies(&original, &trust, target)?;
|
||||
let own = services.observe(Service::Full, &budget)?;
|
||||
ensure!(
|
||||
@@ -148,6 +154,8 @@ impl<'a> FullUpdateGuard<'a> {
|
||||
"UPDATE_ORIGINAL_FULL_CHANGED"
|
||||
);
|
||||
Participant::OriginalHelper
|
||||
} else if restored && header.boot_id == Uuid::parse_str(&rmm_agent_linux::boot_id()?)? {
|
||||
Participant::RestoredHelper
|
||||
} else if target && header.boot_id == Uuid::parse_str(&rmm_agent_linux::boot_id()?)? {
|
||||
Participant::NewHelper
|
||||
} else {
|
||||
@@ -227,8 +235,11 @@ impl<'a> FullUpdateGuard<'a> {
|
||||
}
|
||||
self.check_effect_window()?;
|
||||
ensure!(
|
||||
self.participant == Participant::NewHelper,
|
||||
"UPDATE_NEW_FULL_REQUIRED"
|
||||
matches!(
|
||||
self.participant,
|
||||
Participant::NewHelper | Participant::RestoredHelper
|
||||
),
|
||||
"UPDATE_NEW_OR_RESTORED_FULL_REQUIRED"
|
||||
);
|
||||
let agent = self.services.observe(Service::Agent, &self.budget)?;
|
||||
let mut peer = std::mem::MaybeUninit::<libc::ucred>::uninit();
|
||||
@@ -264,18 +275,30 @@ impl<'a> FullUpdateGuard<'a> {
|
||||
) -> Result<()> {
|
||||
self.check_effect_window()?;
|
||||
ensure!(
|
||||
self.participant == Participant::NewHelper,
|
||||
"UPDATE_NEW_FULL_REQUIRED"
|
||||
matches!(
|
||||
self.participant,
|
||||
Participant::NewHelper | Participant::RestoredHelper
|
||||
),
|
||||
"UPDATE_NEW_OR_RESTORED_FULL_REQUIRED"
|
||||
);
|
||||
let mut journal = update_handoff::read(db, self, self.original.permit().grant_id)?;
|
||||
ensure!(
|
||||
journal.phase() == Phase::ServicesStarted,
|
||||
matches!(
|
||||
journal.phase(),
|
||||
Phase::ServicesStarted | Phase::RollbackServicesStarted
|
||||
),
|
||||
"UPDATE_HEALTH_PHASE"
|
||||
);
|
||||
let event = journal.prepare(
|
||||
u64::try_from(self.clock.now()?.timestamp())?,
|
||||
Evidence::HealthVerified {
|
||||
receipt: signed.clone(),
|
||||
if self.participant == Participant::RestoredHelper {
|
||||
Evidence::RollbackHealthVerified {
|
||||
receipt: signed.clone(),
|
||||
}
|
||||
} else {
|
||||
Evidence::HealthVerified {
|
||||
receipt: signed.clone(),
|
||||
}
|
||||
},
|
||||
)?;
|
||||
journal.replay(&event)?; // ORIGINAL pinned health signature and all kernel fields.
|
||||
@@ -286,7 +309,10 @@ impl<'a> FullUpdateGuard<'a> {
|
||||
self.check_effect_window()?;
|
||||
let journal = update_handoff::read(db, self, self.original.permit().grant_id)?;
|
||||
ensure!(
|
||||
journal.phase() == Phase::HealthVerified,
|
||||
matches!(
|
||||
journal.phase(),
|
||||
Phase::HealthVerified | Phase::RollbackHealthVerified
|
||||
),
|
||||
"UPDATE_ORIGINAL_STORED_HEALTH_REQUIRED"
|
||||
);
|
||||
let event_raw:Vec<u8>=db.query_row("SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence DESC LIMIT 1",[self.original.permit().grant_id.to_string()],|r|r.get(0))?;
|
||||
@@ -295,8 +321,16 @@ impl<'a> FullUpdateGuard<'a> {
|
||||
hash(&event_raw) == journal.head_sha256(),
|
||||
"UPDATE_STORED_HEALTH_HEAD_CHANGED"
|
||||
);
|
||||
let Evidence::HealthVerified { receipt } = &event.evidence else {
|
||||
anyhow::bail!("UPDATE_STORED_HEALTH_EVENT_REQUIRED")
|
||||
let receipt = match &event.evidence {
|
||||
Evidence::HealthVerified { receipt } if self.participant == Participant::NewHelper => {
|
||||
receipt
|
||||
}
|
||||
Evidence::RollbackHealthVerified { receipt }
|
||||
if self.participant == Participant::RestoredHelper =>
|
||||
{
|
||||
receipt
|
||||
}
|
||||
_ => anyhow::bail!("UPDATE_STORED_HEALTH_EVENT_REQUIRED"),
|
||||
};
|
||||
self.health = Some(self.health_evidence(db, receipt)?);
|
||||
Ok(event_raw)
|
||||
@@ -354,8 +388,11 @@ impl<'a> FullUpdateGuard<'a> {
|
||||
pub fn started(&self) -> Result<(Vec<rmm_updater_handoff::StartedService>, String)> {
|
||||
self.check_effect_window()?;
|
||||
ensure!(
|
||||
self.participant == Participant::NewHelper,
|
||||
"UPDATE_NEW_FULL_REQUIRED"
|
||||
matches!(
|
||||
self.participant,
|
||||
Participant::NewHelper | Participant::RestoredHelper
|
||||
),
|
||||
"UPDATE_NEW_OR_RESTORED_FULL_REQUIRED"
|
||||
);
|
||||
let mut raw = Vec::new();
|
||||
let mut proofs = Vec::new();
|
||||
@@ -415,7 +452,13 @@ impl<'a> FullUpdateGuard<'a> {
|
||||
Ok(())
|
||||
}
|
||||
fn verify_owner(&self, phase: Phase, events: &[Event]) -> Result<()> {
|
||||
if !matches!(phase, Phase::ServicesStarted | Phase::HealthVerified) {
|
||||
if !matches!(
|
||||
phase,
|
||||
Phase::ServicesStarted
|
||||
| Phase::HealthVerified
|
||||
| Phase::RollbackServicesStarted
|
||||
| Phase::RollbackHealthVerified
|
||||
) {
|
||||
let updater = self
|
||||
.updater
|
||||
.as_ref()
|
||||
@@ -431,15 +474,30 @@ impl<'a> FullUpdateGuard<'a> {
|
||||
"UPDATE_ORIGINAL_UPDATER_OWNER_CHANGED"
|
||||
);
|
||||
}
|
||||
if matches!(phase, Phase::ServicesStarted | Phase::HealthVerified) {
|
||||
if matches!(
|
||||
phase,
|
||||
Phase::ServicesStarted
|
||||
| Phase::HealthVerified
|
||||
| Phase::RollbackServicesStarted
|
||||
| Phase::RollbackHealthVerified
|
||||
) {
|
||||
let started = events
|
||||
.iter()
|
||||
.find_map(|event| {
|
||||
if let Evidence::ServicesStarted { services, .. } = &event.evidence {
|
||||
.find_map(|event| match &event.evidence {
|
||||
Evidence::ServicesStarted { services, .. }
|
||||
if matches!(phase, Phase::ServicesStarted | Phase::HealthVerified) =>
|
||||
{
|
||||
Some(services)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
Evidence::RollbackServicesStarted { services, .. }
|
||||
if matches!(
|
||||
phase,
|
||||
Phase::RollbackServicesStarted | Phase::RollbackHealthVerified
|
||||
) =>
|
||||
{
|
||||
Some(services)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.context("UPDATE_STARTED_PROOF_MISSING")?;
|
||||
self.verify_current_services(started)?;
|
||||
@@ -486,16 +544,39 @@ impl UpdateParticipantGuard for FullUpdateGuard<'_> {
|
||||
);
|
||||
self.check_effect_window()?;
|
||||
ensure!(
|
||||
self.participant == Participant::NewHelper,
|
||||
"UPDATE_NEW_FULL_REQUIRED"
|
||||
matches!(
|
||||
self.participant,
|
||||
Participant::NewHelper | Participant::RestoredHelper
|
||||
),
|
||||
"UPDATE_NEW_OR_RESTORED_FULL_REQUIRED"
|
||||
);
|
||||
self.witnesses.files.verify_pair(&self.budget)?;
|
||||
if self.participant == Participant::RestoredHelper {
|
||||
self.witnesses.files.verify_predecessor_pair(&self.budget)?;
|
||||
} else {
|
||||
self.witnesses.files.verify_pair(&self.budget)?;
|
||||
}
|
||||
match &event.evidence {
|
||||
Evidence::ServicesStarted {
|
||||
services,
|
||||
ledger_identity_sha256,
|
||||
observation_sha256,
|
||||
}
|
||||
| Evidence::RollbackServicesStarted {
|
||||
services,
|
||||
ledger_identity_sha256,
|
||||
observation_sha256,
|
||||
} => {
|
||||
ensure!(
|
||||
matches!(
|
||||
(&event.evidence, self.participant),
|
||||
(Evidence::ServicesStarted { .. }, Participant::NewHelper)
|
||||
| (
|
||||
Evidence::RollbackServicesStarted { .. },
|
||||
Participant::RestoredHelper
|
||||
)
|
||||
),
|
||||
"UPDATE_STARTED_ROLE"
|
||||
);
|
||||
let mut held = Vec::new();
|
||||
let mut proofs = Vec::new();
|
||||
for service in [Service::Agent, Service::General, Service::Full] {
|
||||
@@ -511,7 +592,18 @@ impl UpdateParticipantGuard for FullUpdateGuard<'_> {
|
||||
"UPDATE_ACTUAL_STARTED_PROOFS_REQUIRED"
|
||||
);
|
||||
}
|
||||
Evidence::HealthVerified { receipt } => {
|
||||
Evidence::HealthVerified { receipt } | Evidence::RollbackHealthVerified { receipt } => {
|
||||
ensure!(
|
||||
matches!(
|
||||
(&event.evidence, self.participant),
|
||||
(Evidence::HealthVerified { .. }, Participant::NewHelper)
|
||||
| (
|
||||
Evidence::RollbackHealthVerified { .. },
|
||||
Participant::RestoredHelper
|
||||
)
|
||||
),
|
||||
"UPDATE_HEALTH_ROLE"
|
||||
);
|
||||
let health = self
|
||||
.health
|
||||
.as_ref()
|
||||
@@ -521,6 +613,14 @@ impl UpdateParticipantGuard for FullUpdateGuard<'_> {
|
||||
"UPDATE_LOCAL_HEALTH_RECEIPT_CHANGED"
|
||||
);
|
||||
}
|
||||
Evidence::RollbackOutcomePersisted { .. }
|
||||
if self.participant == Participant::RestoredHelper =>
|
||||
{
|
||||
ensure!(
|
||||
self.health.is_some(),
|
||||
"UPDATE_ROLLBACK_LOCAL_HEALTH_REQUIRED"
|
||||
);
|
||||
}
|
||||
// InstalledRecord/Outcome have a separate original-record CAS.
|
||||
_ => anyhow::bail!("UPDATE_FULL_EVENT_NOT_YET_QUALIFIED"),
|
||||
}
|
||||
@@ -578,10 +678,63 @@ impl UpdateParticipantGuard for FullUpdateGuard<'_> {
|
||||
.observe(service, &self.budget)?
|
||||
.verify(&self.budget)?;
|
||||
}
|
||||
(
|
||||
Participant::NewHelper,
|
||||
NativeEffect::ConditionalRollbackStopGeneral,
|
||||
NativeEffectStage::ObserveNonstart,
|
||||
) => {
|
||||
// The retained new Full proves the early-Full-failure branch
|
||||
// was never entered. Closing this reservation records NO stop.
|
||||
ensure!(
|
||||
handoff.schema_version == 2,
|
||||
"UPDATE_CONDITIONAL_STOP_PROFILE"
|
||||
);
|
||||
for service in [Service::General, Service::Full] {
|
||||
self.services
|
||||
.observe(service, &self.budget)?
|
||||
.verify(&self.budget)?;
|
||||
}
|
||||
}
|
||||
(
|
||||
Participant::RestoredHelper,
|
||||
NativeEffect::RollbackStartService { service },
|
||||
NativeEffectStage::Observe,
|
||||
) => {
|
||||
let service = match service {
|
||||
NativeService::Agent => Service::Agent,
|
||||
NativeService::General => Service::General,
|
||||
NativeService::Full => Service::Full,
|
||||
_ => anyhow::bail!("UPDATE_WINDOWS_SERVICE_ON_LINUX"),
|
||||
};
|
||||
self.witnesses.files.verify_predecessor_pair(&self.budget)?;
|
||||
self.services
|
||||
.observe(service, &self.budget)?
|
||||
.verify(&self.budget)?;
|
||||
}
|
||||
_ => anyhow::bail!("UPDATE_FULL_NATIVE_EFFECT_NOT_QUALIFIED"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn native_observation_sha256(
|
||||
&self,
|
||||
handoff: &Handoff,
|
||||
delegation: &UpdateDelegation,
|
||||
effect: &NativeEffect,
|
||||
stage: NativeEffectStage,
|
||||
) -> Result<String> {
|
||||
self.verify_native_effect(handoff, delegation, effect, stage)?;
|
||||
ensure!(
|
||||
*effect == NativeEffect::ConditionalRollbackStopGeneral
|
||||
&& stage == NativeEffectStage::ObserveNonstart,
|
||||
"UPDATE_FULL_NATIVE_HASH_NOT_QUALIFIED"
|
||||
);
|
||||
Ok(hash(&serde_json::to_vec(&(
|
||||
"conditional_general_stop_unused_v1",
|
||||
handoff.owner_nonce,
|
||||
handoff.attempt.grant_id,
|
||||
self.services.observe(Service::Full, &self.budget)?.proof(),
|
||||
))?))
|
||||
}
|
||||
}
|
||||
|
||||
struct VerifiedHealth {
|
||||
@@ -985,3 +1138,157 @@ impl FullUpdateGuard<'_> {
|
||||
Ok(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl FullUpdateGuard<'_> {
|
||||
pub fn finalize_restored(&mut self, db: &mut Connection) -> Result<Vec<u8>> {
|
||||
ensure!(
|
||||
self.participant == Participant::RestoredHelper,
|
||||
"UPDATE_RESTORED_FULL_REQUIRED"
|
||||
);
|
||||
self.qualify_effects(db)?;
|
||||
self.qualify_stored_health(db)?;
|
||||
self.witnesses.files.verify_predecessor_pair(&self.budget)?;
|
||||
// TrustState still pins the SAME original record and activation floor.
|
||||
self.trust.recheck(&self.original, self.clock)?;
|
||||
let journal = update_handoff::read(db, self, self.original.permit().grant_id)?;
|
||||
let health = self
|
||||
.health
|
||||
.as_ref()
|
||||
.context("UPDATE_ROLLBACK_HEALTH_REQUIRED")?;
|
||||
let outcome = crate::Outcome {
|
||||
state: "failed".into(),
|
||||
stdout: vec![],
|
||||
stderr: vec![],
|
||||
exit_code: None,
|
||||
output_truncated: false,
|
||||
reboot_required: false,
|
||||
error_code: "AGENT_UPDATE_RETURNED_TO_PREDECESSOR".into(),
|
||||
evidence: serde_json::json!({"postconditions_passed":false,"previous_boot_id":self.original.handoff().boot_id,
|
||||
"agent_update":{"schema_version":2,"handoff_sha256":journal.handoff_sha256(),
|
||||
"manifest_sha256":self.original.handoff().target.manifest_sha256,
|
||||
"predecessor_record_sha256":self.original.handoff().predecessor_record_sha256,
|
||||
"installed_record_sha256":self.original.handoff().predecessor_record_sha256,
|
||||
"signed_health_sha256":health.signed_sha256,"rolled_back":true}}),
|
||||
};
|
||||
update_handoff::finalize_rollback(
|
||||
db,
|
||||
self,
|
||||
self.original.permit(),
|
||||
journal.head_sha256(),
|
||||
u64::try_from(self.clock.now()?.timestamp())?,
|
||||
&outcome,
|
||||
)?;
|
||||
Ok(db.query_row(
|
||||
"SELECT outcome FROM execution_evidence WHERE grant_id=?1",
|
||||
[self.original.permit().grant_id.to_string()],
|
||||
|r| r.get(0),
|
||||
)?)
|
||||
}
|
||||
pub fn restored(&self) -> bool {
|
||||
self.participant == Participant::RestoredHelper
|
||||
}
|
||||
/// Exact ORIGINAL bytes from a signed inflight decision already applied
|
||||
/// by this actual Full process under the same existing guarded ledger.
|
||||
/// Missing evidence keeps the worker waiting; it never becomes readiness.
|
||||
pub fn pre_stop_signed_sync(&self, db: &Connection) -> Result<Option<(Vec<u8>, Vec<u8>)>> {
|
||||
use prost::Message;
|
||||
use rusqlite::OptionalExtension;
|
||||
self.check(db)?;
|
||||
if self.participant != Participant::OriginalHelper {
|
||||
return Ok(None);
|
||||
}
|
||||
let journal = update_handoff::read(db, self, self.original.permit().grant_id)?;
|
||||
if journal.phase() != Phase::StopIntent {
|
||||
return Ok(None);
|
||||
}
|
||||
let id: Option<String> = db
|
||||
.query_row(
|
||||
"SELECT last_decision FROM helper_sync_meta WHERE id=1 AND fault IS NULL",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?
|
||||
.flatten();
|
||||
let Some(id) = id else { return Ok(None) };
|
||||
let id: Uuid = id.parse()?;
|
||||
let applied:bool=db.query_row("SELECT EXISTS(SELECT 1 FROM agent_update_sync_applied WHERE grant_id=?1 AND decision_id=?2)",
|
||||
rusqlite::params![self.original.permit().grant_id.to_string(),id.to_string()],|r|r.get(0))?;
|
||||
if !applied {
|
||||
return Ok(None);
|
||||
}
|
||||
self.qualify_effects(db)?;
|
||||
let round = rmm_agent_core::update_health_sync::read_round(
|
||||
db,
|
||||
self,
|
||||
self.binding,
|
||||
self.original.permit().grant_id,
|
||||
id,
|
||||
)?;
|
||||
let decision = round.decision();
|
||||
let h = self.original.handoff();
|
||||
let now = self.clock.now()?;
|
||||
let proof = update_handoff::current_proof(db, self, self.original.permit().grant_id)?
|
||||
.context("UPDATE_PRESTOP_ORIGINAL_PROOF_REQUIRED")?;
|
||||
let expected =
|
||||
rmm_domain::agent_update::inflight_decision(&proof, self.trust.health_public_key())
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
let attempts = decision
|
||||
.attempts
|
||||
.iter()
|
||||
.filter(|a| a.job_id == h.attempt.job_id && a.attempt_id == h.attempt.attempt_id)
|
||||
.collect::<Vec<_>>();
|
||||
ensure!(attempts.len() == 1, "UPDATE_PRESTOP_ONE_ORIGINAL_ACK");
|
||||
let ack = attempts[0];
|
||||
ensure!(
|
||||
decision.valid_profile(now)
|
||||
&& !decision.ready
|
||||
&& !decision.helper_ready
|
||||
&& decision.boot_id == h.boot_id
|
||||
&& decision.connection_generation == h.connection_generation
|
||||
&& decision.journal_id == h.agent_journal_id
|
||||
&& decision.helper_journal_id == Some(h.helper_journal_id)
|
||||
&& ack.update_inflight.as_ref() == Some(&expected)
|
||||
&& ack.grant_id == Some(h.attempt.grant_id)
|
||||
&& ack.authorization_id == Some(h.attempt.authorization_id)
|
||||
&& ack.plan_hash.as_deref() == Some(h.attempt.plan_sha256.as_str())
|
||||
&& ack.job_payload_hash == h.attempt.job_payload_sha256
|
||||
&& ack.original_boot_id == Some(h.boot_id)
|
||||
&& ack.action == "update_agent"
|
||||
&& ack.execution_identity == "root"
|
||||
&& !ack.unlock
|
||||
&& ack.outcome_state == "running"
|
||||
&& ack.outcome_bytes.is_empty()
|
||||
&& ack.outcome_sha256.is_none()
|
||||
&& round.applied().applied_at >= decision.issued_at
|
||||
&& round.applied().applied_at < decision.valid_until,
|
||||
"UPDATE_PRESTOP_APPLIED_ORIGINAL_ACK"
|
||||
);
|
||||
let (raw, response): (Vec<u8>, Vec<u8>) = db.query_row(
|
||||
"SELECT request,response FROM helper_sync_decisions WHERE decision_id=?1",
|
||||
[id.to_string()],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
ensure!(
|
||||
raw.len() <= 256 * 1024 && response.len() <= 256 * 1024,
|
||||
"UPDATE_PRESTOP_WIRE_LIMIT"
|
||||
);
|
||||
let request = rmm_protocol::SyncRequest::decode(raw.as_slice())?;
|
||||
let local = request
|
||||
.attempts
|
||||
.iter()
|
||||
.find(|a| {
|
||||
a.job_id == h.attempt.job_id.to_string()
|
||||
&& a.attempt_id == h.attempt.attempt_id.to_string()
|
||||
})
|
||||
.context("UPDATE_PRESTOP_REQUEST_ATTEMPT")?;
|
||||
ensure!(
|
||||
request.journal_complete
|
||||
&& local.state == "running"
|
||||
&& local.consumption == "consumed"
|
||||
&& local.update_handoff_bytes == proof.bytes().map_err(anyhow::Error::msg)?,
|
||||
"UPDATE_PRESTOP_REQUEST_ORIGINAL_PREFIX"
|
||||
);
|
||||
self.check_effect_window()?;
|
||||
Ok(Some((raw, response)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,386 @@
|
||||
//! Passive Full-only updater observation. This path cannot stage, consume,
|
||||
//! start/stop a service, append a handoff, repair state or grant readiness.
|
||||
use crate::{
|
||||
Binding, control,
|
||||
readiness::Identity,
|
||||
reconcile::{self, ExecutorGuard},
|
||||
};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use rmm_agent_core::{helper_reconcile::HelperSync, reconcile::SyncClock};
|
||||
use rmm_domain::{
|
||||
agent_update::{
|
||||
AgentUpdateObservation, CandidatePreview, ObservationQuality, PeerPinTransition, Platform,
|
||||
ReleaseEnvelope,
|
||||
},
|
||||
jobs::hash,
|
||||
};
|
||||
use rmm_updater_linux::{
|
||||
Budget,
|
||||
files::Directory,
|
||||
ipc,
|
||||
services::{Service, ServicePolicy, Systemd},
|
||||
};
|
||||
use rmm_updater_worker::{
|
||||
clock::NativeClock, full::FullFiles, reader_registry::QualifiedRegistry, trust::TrustState,
|
||||
};
|
||||
use rusqlite::Connection;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
ffi::OsStr,
|
||||
os::unix::{fs::MetadataExt, net::UnixStream},
|
||||
path::Path,
|
||||
time::Duration,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
const SOCKET: &str = "/run/rmm-helper-maintenance/control.sock";
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Request {
|
||||
pub operation: String,
|
||||
pub schema_version: u32,
|
||||
pub challenge: Uuid,
|
||||
pub identity: Identity,
|
||||
pub candidate: Option<ReleaseEnvelope>,
|
||||
pub keyring_generation: Option<u64>,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Response {
|
||||
pub schema_version: u32,
|
||||
pub challenge: Uuid,
|
||||
pub identity: Identity,
|
||||
pub profile: String,
|
||||
pub policy_sha256: String,
|
||||
pub helper_binary_sha256: String,
|
||||
pub ledger_device: u64,
|
||||
pub ledger_inode: u64,
|
||||
pub lock_device: u64,
|
||||
pub lock_inode: u64,
|
||||
pub observation: AgentUpdateObservation,
|
||||
pub installed_record: Vec<u8>,
|
||||
pub installed_record_sha256: String,
|
||||
}
|
||||
pub fn observe(
|
||||
socket: &Path,
|
||||
binding: &Binding,
|
||||
candidate: Option<ReleaseEnvelope>,
|
||||
keyring_generation: Option<u64>,
|
||||
) -> Result<AgentUpdateObservation> {
|
||||
observe_record(socket, binding, candidate, keyring_generation)
|
||||
.map(|(observation, _)| observation)
|
||||
}
|
||||
/// Exact public release/evidence metadata, returned only from the same
|
||||
/// authenticated Full response as the independently measured observation.
|
||||
/// The private Root record directory and file permissions remain unchanged.
|
||||
pub fn observe_record(
|
||||
socket: &Path,
|
||||
binding: &Binding,
|
||||
candidate: Option<ReleaseEnvelope>,
|
||||
keyring_generation: Option<u64>,
|
||||
) -> Result<(AgentUpdateObservation, Vec<u8>)> {
|
||||
ensure!(
|
||||
socket == Path::new(SOCKET) && candidate.is_some() == keyring_generation.is_some(),
|
||||
"UPDATE_OBSERVER_INPUT"
|
||||
);
|
||||
let pin = control::GeneralPin::read(
|
||||
Path::new("/etc/rmm-remote/full-maintenance-helper-proof.json"),
|
||||
binding,
|
||||
)?;
|
||||
ensure!(
|
||||
pin.profile == "full_maintenance_v1",
|
||||
"UPDATE_OBSERVER_FULL_PIN"
|
||||
);
|
||||
let request = Request {
|
||||
operation: "update_observe_v1".into(),
|
||||
schema_version: 1,
|
||||
challenge: Uuid::new_v4(),
|
||||
identity: Identity::from_binding(binding)?,
|
||||
candidate,
|
||||
keyring_generation,
|
||||
};
|
||||
let response: Response =
|
||||
control::exchange(socket, &request, Duration::from_secs(15), 192 * 1024)?;
|
||||
ensure!(
|
||||
response.schema_version == 1
|
||||
&& response.challenge == request.challenge
|
||||
&& response.identity == request.identity
|
||||
&& response.profile == pin.profile
|
||||
&& response.policy_sha256 == pin.policy_sha256
|
||||
&& Some(&response.helper_binary_sha256) == pin.helper_binary_sha256.as_ref()
|
||||
&& response.ledger_inode != 0
|
||||
&& response.lock_inode != 0
|
||||
&& response.observation.validate()
|
||||
&& response.observation.platform == Platform::LinuxX64,
|
||||
"UPDATE_OBSERVER_RESPONSE_BINDING"
|
||||
);
|
||||
let record = rmm_domain::agent_update_provisioning::InstalledReleaseRecord::from_bytes(
|
||||
&response.installed_record,
|
||||
)
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
let predecessor = response
|
||||
.observation
|
||||
.predecessor
|
||||
.as_ref()
|
||||
.context("UPDATE_OBSERVER_RECORD_PREDECESSOR")?;
|
||||
let manifest = record
|
||||
.release_envelope
|
||||
.decode()
|
||||
.map_err(anyhow::Error::msg)?
|
||||
.manifest;
|
||||
ensure!(
|
||||
response.installed_record_sha256 == hash(&response.installed_record)
|
||||
&& response.installed_record_sha256 == predecessor.installed_record_sha256
|
||||
&& record.platform == response.observation.platform
|
||||
&& record.manifest_sha256 == predecessor.manifest_sha256
|
||||
&& manifest.release_id == predecessor.release_id
|
||||
&& manifest.release_sequence == predecessor.release_sequence
|
||||
&& manifest.artifacts[0].sha256 == predecessor.agent_sha256
|
||||
&& manifest.artifacts[1].sha256 == predecessor.helper_sha256
|
||||
&& Some(record.activation_floor) == response.observation.activation_floor
|
||||
&& record.rolled_back == response.observation.rolled_back,
|
||||
"UPDATE_OBSERVER_RECORD_BINDING"
|
||||
);
|
||||
Ok((response.observation, response.installed_record))
|
||||
}
|
||||
pub struct Local<'a> {
|
||||
pub executor: &'a ExecutorGuard,
|
||||
pub binding: &'a Binding,
|
||||
pub clock: &'a NativeClock,
|
||||
pub sync: &'a HelperSync,
|
||||
pub sync_clock: &'a reconcile::NativeClock,
|
||||
pub key_path: &'a Path,
|
||||
pub policy_sha256: &'a str,
|
||||
pub helper_sha256: &'a str,
|
||||
}
|
||||
pub fn collect(db: &Connection, local: Local<'_>, request: &Request) -> Result<Response> {
|
||||
ensure!(
|
||||
request.operation == "update_observe_v1"
|
||||
&& request.schema_version == 1
|
||||
&& !request.challenge.is_nil()
|
||||
&& request.identity == Identity::from_binding(local.binding)?
|
||||
&& request.candidate.is_some() == request.keyring_generation.is_some(),
|
||||
"UPDATE_OBSERVER_REQUEST_BINDING"
|
||||
);
|
||||
local.executor.check(db)?;
|
||||
ensure!(
|
||||
crate::update_control::active_grant(db)?.is_none(),
|
||||
"UPDATE_OBSERVER_ACTIVE_HANDOFF"
|
||||
);
|
||||
let generation = local
|
||||
.sync
|
||||
.active_generation()
|
||||
.context("UPDATE_OBSERVER_CURRENT_SYNC_REQUIRED")?;
|
||||
ensure!(
|
||||
local.sync.can_start(
|
||||
db,
|
||||
local.executor,
|
||||
generation,
|
||||
local.sync_clock,
|
||||
&reconcile::NativeBoot
|
||||
),
|
||||
"UPDATE_OBSERVER_CONFIRMED_SYNC_REQUIRED"
|
||||
);
|
||||
let decision: String = db.query_row(
|
||||
"SELECT last_decision FROM helper_sync_meta WHERE id=1 AND fault IS NULL AND generation=?1",
|
||||
[generation],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let decision = Uuid::parse_str(&decision)?;
|
||||
let trust = TrustState::load_observed(local.binding, local.clock)?;
|
||||
trust.qualify_history(db, local.executor, local.binding)?;
|
||||
let boot = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
|
||||
.trim()
|
||||
.parse()?;
|
||||
let budget = Budget::new(boot, Duration::from_secs(15))?;
|
||||
let registry = QualifiedRegistry::load(trust.policy())?;
|
||||
registry.verify_admission(trust.policy(), &budget)?;
|
||||
let policies = trust
|
||||
.policy()
|
||||
.services
|
||||
.iter()
|
||||
.zip([
|
||||
Service::Agent,
|
||||
Service::General,
|
||||
Service::Full,
|
||||
Service::Apt,
|
||||
])
|
||||
.map(|(pin, service)| ServicePolicy {
|
||||
service,
|
||||
unit_sha256: pin.unit_sha256.clone(),
|
||||
image_sha256: if service == Service::Agent {
|
||||
trust.predecessor().agent_sha256.clone()
|
||||
} else {
|
||||
trust.predecessor().helper_sha256.clone()
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
let services = Systemd::new(policies)?;
|
||||
let mut held = Vec::new();
|
||||
for service in [Service::Agent, Service::General, Service::Full] {
|
||||
held.push(services.observe(service, &budget)?);
|
||||
}
|
||||
ensure!(
|
||||
held[2].proof().process.pid == std::process::id()
|
||||
&& held[2].proof().process.image_sha256 == local.helper_sha256,
|
||||
"UPDATE_OBSERVER_ACTUAL_FULL"
|
||||
);
|
||||
let updater = Systemd::observe_updater(
|
||||
ServicePolicy {
|
||||
service: Service::Updater,
|
||||
image_sha256: trust.updater_binding().binary_sha256.clone(),
|
||||
unit_sha256: trust.policy().updater_service.unit_sha256.clone(),
|
||||
},
|
||||
&budget,
|
||||
)?;
|
||||
let description = ipc::exchange(
|
||||
UnixStream::connect("/run/rmm-agent-updater/control.sock")?,
|
||||
&updater,
|
||||
&budget,
|
||||
&ipc::Request::Describe {},
|
||||
)?;
|
||||
let updater_proof = updater.proof();
|
||||
ensure!(
|
||||
matches!(description.state, ipc::State::Described)
|
||||
&& !description.ready
|
||||
&& description.head_sha256.is_none()
|
||||
&& description.updater.pid == updater_proof.process.pid
|
||||
&& description.updater.birth == updater_proof.process.birth
|
||||
&& description.updater.image_sha256 == updater_proof.process.image_sha256
|
||||
&& description.updater.service_instance_sha256
|
||||
== hash(&serde_json::to_vec(updater_proof)?),
|
||||
"UPDATE_OBSERVER_LIVE_UPDATER"
|
||||
);
|
||||
let key_directory = Directory::open(
|
||||
local
|
||||
.key_path
|
||||
.parent()
|
||||
.context("UPDATE_OBSERVER_KEY_PARENT")?,
|
||||
0,
|
||||
false,
|
||||
)?;
|
||||
let mut key = key_directory.inspect(
|
||||
local
|
||||
.key_path
|
||||
.file_name()
|
||||
.context("UPDATE_OBSERVER_KEY_NAME")?,
|
||||
0,
|
||||
4096,
|
||||
)?;
|
||||
let record: crate::SigningKey = serde_json::from_slice(&key.bytes(4096)?)?;
|
||||
record.validate()?;
|
||||
ensure!(
|
||||
record.public_key_hex == local.binding.public_key_hex,
|
||||
"UPDATE_OBSERVER_JOB_KEY_BINDING"
|
||||
);
|
||||
let general = Directory::open(Path::new("/etc/rmm-remote"), 0, false)?;
|
||||
let general_pin = general.inspect(OsStr::new("general-helper-proof.json"), 0, 8192)?;
|
||||
let mut previews = Vec::new();
|
||||
if let Some(candidate) = &request.candidate {
|
||||
ensure!(
|
||||
request.keyring_generation == Some(trust.updater_binding().keyring_generation),
|
||||
"UPDATE_OBSERVER_KEYRING_GENERATION"
|
||||
);
|
||||
trust.verify_candidate(candidate, local.clock)?;
|
||||
let manifest = candidate.decode().map_err(anyhow::Error::msg)?.manifest;
|
||||
if manifest.release_id != trust.predecessor().release_id {
|
||||
let full = FullFiles::preview(
|
||||
&manifest.artifacts[1].sha256,
|
||||
local.key_path,
|
||||
&key.proof().sha256,
|
||||
&budget,
|
||||
)?;
|
||||
let transition = PeerPinTransition::LinuxGeneralFullV1 {
|
||||
general_pin_sha256: general_pin.proof().sha256.clone(),
|
||||
full,
|
||||
};
|
||||
// Validate the exact renderer result against the actual verified
|
||||
// future manifest/predecessor, using the ordinary immutable codec.
|
||||
let resolved = rmm_domain::agent_update::ResolvedAgentUpdate {
|
||||
schema_version: 1,
|
||||
profile: rmm_domain::agent_update::UpdateProfile::FixedCodePairV1,
|
||||
release_envelope: candidate.clone(),
|
||||
release_envelope_sha256: candidate.sha256().map_err(anyhow::Error::msg)?,
|
||||
manifest_sha256: hash(
|
||||
&candidate
|
||||
.decode()
|
||||
.map_err(anyhow::Error::msg)?
|
||||
.payload_bytes,
|
||||
),
|
||||
predecessor: trust.predecessor().clone(),
|
||||
updater: trust.updater_binding().clone(),
|
||||
peer_pin_transition: transition.clone(),
|
||||
recovery: rmm_domain::agent_update::Recovery::RestoreExactPredecessorOnce,
|
||||
health_profile: rmm_domain::agent_update::HealthProfile::MtlsInventorySyncV1,
|
||||
health_timeout_seconds: 180,
|
||||
no_reboot: true,
|
||||
};
|
||||
ensure!(resolved.validate(), "UPDATE_OBSERVER_RENDERED_CANDIDATE");
|
||||
previews.push(CandidatePreview {
|
||||
release_id: manifest.release_id,
|
||||
manifest_sha256: resolved.manifest_sha256,
|
||||
peer_pin_transition: transition,
|
||||
});
|
||||
}
|
||||
}
|
||||
for service in &held {
|
||||
service.verify(&budget)?;
|
||||
}
|
||||
updater.verify(&budget)?;
|
||||
trust.recheck_observed()?;
|
||||
registry.verify_admission(trust.policy(), &budget)?;
|
||||
local.executor.check(db)?;
|
||||
ensure!(
|
||||
local.sync.can_start(
|
||||
db,
|
||||
local.executor,
|
||||
generation,
|
||||
local.sync_clock,
|
||||
&reconcile::NativeBoot
|
||||
),
|
||||
"UPDATE_OBSERVER_SYNC_EXPIRED"
|
||||
);
|
||||
let observed_at = local.clock.now()?;
|
||||
let observed_at = observed_at
|
||||
- chrono::Duration::nanoseconds(i64::from(
|
||||
observed_at.timestamp_subsec_nanos() % 1_000_000,
|
||||
));
|
||||
let observation = AgentUpdateObservation {
|
||||
schema_version: 1,
|
||||
platform: Platform::LinuxX64,
|
||||
observed_at,
|
||||
quality: ObservationQuality::Valid,
|
||||
predecessor: Some(trust.predecessor().clone()),
|
||||
updater: Some(trust.updater_binding().clone()),
|
||||
activation_floor: Some(trust.activation_floor()),
|
||||
rolled_back: false,
|
||||
helper_observation_sha256: Some(hash(&serde_json::to_vec(&(
|
||||
held.iter().map(|h| h.proof()).collect::<Vec<_>>(),
|
||||
updater.proof(),
|
||||
decision,
|
||||
generation,
|
||||
key.proof(),
|
||||
general_pin.proof(),
|
||||
&previews,
|
||||
))?)),
|
||||
signed_sync_decision_id: Some(decision),
|
||||
candidate_previews: previews,
|
||||
};
|
||||
ensure!(observation.validate(), "UPDATE_OBSERVER_OBSERVATION_SHAPE");
|
||||
let ledger = std::fs::metadata("/var/lib/rmm-helper/consumption.sqlite")?;
|
||||
let lock = std::fs::metadata("/var/lib/rmm-helper/executor.lock")?;
|
||||
Ok(Response {
|
||||
schema_version: 1,
|
||||
challenge: request.challenge,
|
||||
identity: request.identity.clone(),
|
||||
profile: "full_maintenance_v1".into(),
|
||||
policy_sha256: local.policy_sha256.into(),
|
||||
helper_binary_sha256: local.helper_sha256.into(),
|
||||
ledger_device: ledger.dev(),
|
||||
ledger_inode: ledger.ino(),
|
||||
lock_device: lock.dev(),
|
||||
lock_inode: lock.ino(),
|
||||
observation,
|
||||
installed_record: trust.installed_raw().to_vec(),
|
||||
installed_record_sha256: hash(trust.installed_raw()),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user