82 lines
3.0 KiB
Rust
82 lines
3.0 KiB
Rust
//! Concrete native service/process binding. A denied /proc read is a denial;
|
|
//! UID0, process names and installed file hashes are never image fallbacks.
|
|
use anyhow::{Result, ensure};
|
|
use rmm_domain::{
|
|
agent_update::{ParticipantIdentity, ParticipantRole, UpdateDelegation},
|
|
jobs::hash,
|
|
};
|
|
use rmm_updater_linux::{
|
|
Budget,
|
|
services::{HeldService, Service, ServicePolicy, Systemd},
|
|
};
|
|
|
|
pub struct StableParticipant {
|
|
held: HeldService,
|
|
identity: ParticipantIdentity,
|
|
}
|
|
impl StableParticipant {
|
|
pub fn observe(policy: ServicePolicy, budget: &Budget) -> Result<Self> {
|
|
let held = Systemd::observe_updater(policy, budget)?;
|
|
let proof = held.proof();
|
|
ensure!(
|
|
proof.process.pid == std::process::id()
|
|
&& proof.process.uid == 0
|
|
&& proof.process.gid == 0,
|
|
"UPDATE_WORKER_MUST_BE_EXACT_STABLE_SERVICE"
|
|
);
|
|
let identity = ParticipantIdentity {
|
|
role: ParticipantRole::LinuxUpdater,
|
|
pid: proof.process.pid,
|
|
birth: proof.process.birth,
|
|
image_sha256: proof.process.image_sha256.clone(),
|
|
security_principal: "uid:0".into(),
|
|
service_instance_sha256: hash(&serde_json::to_vec(proof)?),
|
|
};
|
|
ensure!(identity.validate(), "UPDATE_WORKER_IDENTITY");
|
|
Ok(Self { held, identity })
|
|
}
|
|
pub fn verify_delegation(&self, delegation: &UpdateDelegation, budget: &Budget) -> Result<()> {
|
|
self.held.verify(budget)?;
|
|
ensure!(
|
|
delegation.validate() && delegation.updater == self.identity,
|
|
"UPDATE_WORKER_DELEGATION_CHANGED"
|
|
);
|
|
Ok(())
|
|
}
|
|
pub fn held_service(&self) -> &HeldService {
|
|
&self.held
|
|
}
|
|
pub fn identity(&self) -> &ParticipantIdentity {
|
|
&self.identity
|
|
}
|
|
}
|
|
|
|
/// Before stopping the independently checking original helper, compare its
|
|
/// actual concrete process instance with the immutable atomic delegation.
|
|
pub fn verify_original_helper(
|
|
services: &Systemd,
|
|
delegation: &UpdateDelegation,
|
|
budget: &Budget,
|
|
) -> Result<HeldService> {
|
|
ensure!(delegation.validate(), "UPDATE_ORIGINAL_DELEGATION");
|
|
let service = match delegation.original_helper.role {
|
|
ParticipantRole::LinuxGeneralHelper => Service::General,
|
|
ParticipantRole::LinuxFullHelper => Service::Full,
|
|
_ => anyhow::bail!("UPDATE_ORIGINAL_LINUX_HELPER_REQUIRED"),
|
|
};
|
|
let held = services.observe(service, budget)?;
|
|
let proof = held.proof();
|
|
let expected = &delegation.original_helper;
|
|
ensure!(
|
|
proof.process.pid == expected.pid
|
|
&& proof.process.birth == expected.birth
|
|
&& proof.process.image_sha256 == expected.image_sha256
|
|
&& proof.process.uid == 0
|
|
&& expected.security_principal == "uid:0"
|
|
&& hash(&serde_json::to_vec(proof)?) == expected.service_instance_sha256,
|
|
"UPDATE_ORIGINAL_HELPER_INSTANCE_CHANGED"
|
|
);
|
|
held.verify(budget)?;
|
|
Ok(held)
|
|
}
|