387 lines
14 KiB
Rust
387 lines
14 KiB
Rust
//! 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::NATIVE_LINUX,
|
|
"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::NATIVE_LINUX,
|
|
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()),
|
|
})
|
|
}
|