//! Closed systemd service set; status is corroborated with held kernel process //! identity. No shell, enable/reboot, global kill or repeated stop/start. use crate::{ Budget, DurableEffectFence, Effect, digest, process::{HeldProcess, ProcessProof}, }; use anyhow::{Result, ensure}; use serde::{Deserialize, Serialize}; use std::{ collections::BTreeMap, fs::File, io::Read, os::fd::{AsRawFd, FromRawFd}, os::unix::net::UnixStream, process::{Child, Command, Stdio}, thread, time::{Duration, Instant}, }; #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum Service { Agent, General, Full, Apt, Updater, } impl Service { fn handoff_service(self) -> Result { Ok(match self { Self::Agent => rmm_updater_handoff::Service::LinuxAgent, Self::General => rmm_updater_handoff::Service::LinuxGeneralHelper, Self::Full => rmm_updater_handoff::Service::LinuxFullHelper, Self::Apt => rmm_updater_handoff::Service::LinuxAptHelper, Self::Updater => anyhow::bail!("UPDATE_UPDATER_NOT_IN_TARGET_SET"), }) } pub fn unit(self) -> &'static str { match self { Self::Agent => "rmm-agent-remote.service", Self::General => "rmm-helper-cloud.service", Self::Full => "rmm-full-maintenance-helper.service", Self::Apt => "rmm-apt-helper.service", Self::Updater => "rmm-agent-updater.service", } } fn uid(self) -> u32 { if self == Self::Agent { 993 } else { 0 } } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ServiceProof { pub service: Service, pub invocation_id: String, pub enabled: String, pub process: ProcessProof, pub unit_sha256: String, } pub struct HeldService { proof: ServiceProof, process: HeldProcess, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ServicePolicy { pub service: Service, pub image_sha256: String, pub unit_sha256: String, } pub struct Systemd { policies: Vec, } impl Systemd { pub fn observe_updater(pin: ServicePolicy, budget: &Budget) -> Result { ensure!( pin.service == Service::Updater && crate::valid_digest(&pin.image_sha256) && crate::valid_digest(&pin.unit_sha256), "UPDATE_UPDATER_PINS" ); Self { policies: vec![pin], } .observe(Service::Updater, budget) } /// Full is an ordinary required participant in the current Linux topology; /// APT may additionally be installed but is never in the restart set. pub fn new(policies: Vec) -> Result { let services: Vec<_> = policies.iter().map(|p| p.service).collect(); ensure!( services == [Service::Agent, Service::General, Service::Full] || services == [ Service::Agent, Service::General, Service::Full, Service::Apt ], "UPDATE_CLOSED_SERVICE_SET" ); ensure!( policies.iter().all( |p| crate::valid_digest(&p.image_sha256) && crate::valid_digest(&p.unit_sha256) ), "UPDATE_SERVICE_PINS" ); ensure!( policies[1].image_sha256 == policies[2].image_sha256, "UPDATE_HELPER_PAIR_MISMATCH" ); Ok(Self { policies }) } pub fn authenticate_helper_peer( &self, socket: &UnixStream, budget: &Budget, ) -> Result { for service in [Service::General, Service::Full] { let held = self.observe(service, budget)?; if let Ok(peer) = held.authenticate_peer(socket, budget) { return Ok(peer); } } anyhow::bail!("UPDATE_HELPER_PEER_NOT_BOUND") } fn status(service: Service, budget: &Budget) -> Result> { budget.check()?; let output = command( &[ "show", service.unit(), "-p", "LoadState", "-p", "ActiveState", "-p", "MainPID", "-p", "InvocationID", "-p", "UnitFileState", "-p", "FragmentPath", "-p", "DropInPaths", ], budget, )?; let text = std::str::from_utf8(&output)?; let mut fields = BTreeMap::new(); for row in text.lines() { let (k, v) = row .split_once('=') .ok_or_else(|| anyhow::anyhow!("UPDATE_SYSTEMD_RESPONSE"))?; ensure!( fields.insert(k.into(), v.into()).is_none(), "UPDATE_SYSTEMD_DUPLICATE" ); } ensure!( fields.len() == 8 && fields.get("LoadState").is_some_and(|v| v == "loaded"), "UPDATE_SYSTEMD_LOADED" ); Ok(fields) } pub fn observe(&self, service: Service, budget: &Budget) -> Result { let pin = self .policies .iter() .find(|p| p.service == service) .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_NOT_PINNED"))?; let first = Self::status(service, budget)?; ensure!( first.get("ActiveState").is_some_and(|s| s == "active"), "UPDATE_SERVICE_INACTIVE" ); let pid = first .get("MainPID") .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_PID"))? .parse()?; let invocation_id = first .get("InvocationID") .ok_or_else(|| anyhow::anyhow!("UPDATE_SERVICE_INSTANCE"))? .to_owned(); ensure!( invocation_id.len() == 32 && invocation_id.bytes().all(|b| b.is_ascii_hexdigit()), "UPDATE_SERVICE_INSTANCE" ); // Unit composition must be independently provisioned. No unrecognized // drop-in is silently ignored or inherited as an update permission. let unit_sha = unit_composition(&first)?; ensure!(unit_sha == pin.unit_sha256, "UPDATE_UNIT_CHANGED"); let process = HeldProcess::open( pid, service.uid(), if service == Service::Updater { 0 } else { 984 }, &pin.image_sha256, )?; ensure!( Self::status(service, budget)? == first, "UPDATE_SERVICE_CHANGED_DURING_OBSERVATION" ); process.verify()?; Ok(HeldService { proof: ServiceProof { service, invocation_id, enabled: first["UnitFileState"].clone(), process: process.proof().clone(), unit_sha256: unit_sha, }, process, }) } pub fn stop_all( self, budget: &Budget, fence: &mut impl DurableEffectFence, ) -> Result { let mut original = Vec::new(); for pin in &self.policies { if pin.service == Service::Apt { let s = Self::status(Service::Apt, budget)?; ensure!( s["ActiveState"] == "inactive" && s["MainPID"] == "0" && s["UnitFileState"] == "disabled" && unit_composition(&s)? == pin.unit_sha256, "UPDATE_APT_MUST_REMAIN_CLOSED" ); } else { original.push(self.observe(pin.service, budget)?); } } for held in &original { held.process.verify()?; let effect = Effect::StopService { service: held.proof.service, }; fence.before_effect(&effect)?; budget.check()?; command(&["--no-block", "stop", held.proof.service.unit()], budget)?; loop { budget.check()?; let state = Self::status(held.proof.service, budget)?; if state["ActiveState"] == "inactive" && state["MainPID"] == "0" && held.process.exited()? { break; } ensure!( state["MainPID"] == "0" || state["MainPID"] == held.proof.process.pid.to_string(), "UPDATE_STOP_PID_CHANGED" ); thread::sleep(Duration::from_millis(20)); } fence.after_effect(&effect, &digest(&serde_json::to_vec(&held.proof)?))?; } let result = StoppedServices { systemd: self, original, }; result.verify_still_stopped(budget)?; Ok(result) } } impl HeldService { pub fn started_proof(&self, budget: &Budget) -> Result { self.verify(budget)?; ensure!( self.proof.service != Service::Apt, "UPDATE_APT_NOT_IN_START_SET" ); Ok(rmm_updater_handoff::StartedService { service: self.proof.service.handoff_service()?, process: rmm_updater_handoff::Process { pid: self.proof.process.pid, birth: self.proof.process.birth, image_sha256: self.proof.process.image_sha256.clone(), }, }) } pub fn proof(&self) -> &ServiceProof { &self.proof } pub fn verify(&self, budget: &Budget) -> Result<()> { self.process.verify()?; let current = Systemd::status(self.proof.service, budget)?; ensure!( current["ActiveState"] == "active" && current["MainPID"] == self.proof.process.pid.to_string() && current["InvocationID"] == self.proof.invocation_id && current["UnitFileState"] == self.proof.enabled && unit_composition(¤t)? == self.proof.unit_sha256, "UPDATE_SERVICE_BINDING_CHANGED" ); self.process.verify() } /// Authenticate the actual local connection independently of request JSON. /// Root alone is insufficient; the fixed service's held pidfd must match. pub fn authenticate_peer( &self, socket: &UnixStream, budget: &Budget, ) -> Result { use rmm_domain::agent_update::{ParticipantIdentity, ParticipantRole}; self.verify(budget)?; let mut peer = std::mem::MaybeUninit::::uninit(); let mut len = std::mem::size_of::() as libc::socklen_t; // SAFETY: live Unix socket, correctly sized writable ucred and length. let result = unsafe { libc::getsockopt( socket.as_raw_fd(), libc::SOL_SOCKET, libc::SO_PEERCRED, peer.as_mut_ptr().cast(), &mut len, ) }; ensure!( result == 0 && len as usize == std::mem::size_of::(), "UPDATE_PEER_CREDENTIALS" ); let peer = unsafe { peer.assume_init() }; ensure!( peer.pid > 0 && peer.pid as u32 == self.proof.process.pid && peer.uid == 0 && peer.gid == self.proof.process.gid, "UPDATE_PEER_SERVICE_BINDING" ); let role = match self.proof.service { Service::Updater => ParticipantRole::LinuxUpdater, Service::General => ParticipantRole::LinuxGeneralHelper, Service::Full => ParticipantRole::LinuxFullHelper, _ => anyhow::bail!("UPDATE_PARTICIPANT_ROLE"), }; self.verify(budget)?; Ok(ParticipantIdentity { role, pid: self.proof.process.pid, birth: self.proof.process.birth, image_sha256: self.proof.process.image_sha256.clone(), security_principal: "uid:0".into(), service_instance_sha256: digest(&serde_json::to_vec(&self.proof)?), }) } } pub struct StoppedServices { systemd: Systemd, original: Vec, } impl StoppedServices { pub fn verify_still_stopped(&self, budget: &Budget) -> Result<()> { for pin in &self.systemd.policies { let s = Systemd::status(pin.service, budget)?; ensure!( s["ActiveState"] == "inactive" && s["MainPID"] == "0" && unit_composition(&s)? == pin.unit_sha256, "UPDATE_READER_NOT_STOPPED" ); if pin.service == Service::Apt { ensure!( s["UnitFileState"] == "disabled", "UPDATE_APT_MUST_REMAIN_CLOSED" ); } } ensure!( self.original .iter() .all(|s| s.process.exited().unwrap_or(false)), "UPDATE_OLD_PROCESS_STILL_LIVE" ); Ok(()) } pub fn proofs(&self) -> Vec { self.original.iter().map(|s| s.proof.clone()).collect() } /// The embedding fence MUST verify the exact PairVerified and durable /// LedgerReturnIntent, released same guard and new target hashes first. pub fn start_once( self, new: Systemd, budget: &Budget, fence: &mut impl DurableEffectFence, ) -> Result> { self.verify_still_stopped(budget)?; ensure!( self.systemd .policies .iter() .map(|p| p.service) .eq(new.policies.iter().map(|p| p.service)), "UPDATE_START_SET_CHANGED" ); let mut started = Vec::new(); for service in [Service::General, Service::Full, Service::Agent] { let effect = Effect::StartService { service }; fence.before_effect(&effect)?; budget.check()?; command(&["--no-block", "start", service.unit()], budget)?; loop { budget.check()?; let state = Systemd::status(service, budget)?; if state["ActiveState"] == "active" && state["MainPID"] != "0" { let held = new.observe(service, budget)?; ensure!( !self .original .iter() .any(|old| old.proof.process.pid == held.proof.process.pid), "UPDATE_PROCESS_REUSE" ); fence.after_effect(&effect, &digest(&serde_json::to_vec(&held.proof)?))?; started.push(held); break; } ensure!( state["ActiveState"] == "activating" || state["ActiveState"] == "active", "UPDATE_START_UNPROVEN" ); thread::sleep(Duration::from_millis(20)); } } // Return in the canonical Handoff stop-set order, not actual start order. started.sort_by_key(|s| s.proof.service); Ok(started) } } fn unit_composition(fields: &BTreeMap) -> Result { use crate::files::Directory; use std::{ffi::OsStr, path::Path}; let fragment = fields .get("FragmentPath") .ok_or_else(|| anyhow::anyhow!("UPDATE_UNIT_FRAGMENT"))?; let mut paths = vec![fragment.as_str()]; paths.extend( fields .get("DropInPaths") .ok_or_else(|| anyhow::anyhow!("UPDATE_UNIT_DROPINS"))? .split_ascii_whitespace(), ); let mut proof = Vec::new(); for path in paths { let path = Path::new(path); ensure!(path.is_absolute(), "UPDATE_UNIT_PATH"); let directory = Directory::open( path.parent() .ok_or_else(|| anyhow::anyhow!("UPDATE_UNIT_PARENT"))?, 0, false, )?; let file = directory.inspect(path.file_name().unwrap_or(OsStr::new("")), 0, 65536)?; proof.push((path.to_string_lossy().into_owned(), file.proof().clone())); } Ok(digest(&serde_json::to_vec(&proof)?)) } struct OwnedCommand { child: Child, pidfd: File, } impl Drop for OwnedCommand { fn drop(&mut self) { // Signal this exact retained kernel process, including on every early // error. An exited child cannot turn the descriptor into another PID. unsafe { libc::syscall( libc::SYS_pidfd_send_signal, self.pidfd.as_raw_fd(), libc::SIGKILL, 0usize, 0, ) }; let _ = self.child.wait(); } } fn drain(stream: &mut dyn Read, buffer: &mut Vec) -> Result<()> { let mut bytes = [0; 1024]; loop { match stream.read(&mut bytes) { Ok(0) => break, Ok(n) => { ensure!(buffer.len() + n <= 16384, "UPDATE_COMMAND_OUTPUT_LIMIT"); buffer.extend_from_slice(&bytes[..n]); } Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break, Err(e) => return Err(e.into()), } } Ok(()) } fn command(args: &[&str], budget: &Budget) -> Result> { budget.check()?; let mut child = Command::new("/usr/bin/systemctl") .args(["--system", "--no-ask-password", "--no-pager"]) .args(args) .env_clear() .env("PATH", "/usr/bin:/bin") .env("LC_ALL", "C") .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn()?; // A live Child has not been reaped, so this pidfd cannot target a reused PID. let raw = unsafe { libc::syscall(libc::SYS_pidfd_open, child.id(), 0) }; if raw < 0 { let _ = child.kill(); let _ = child.wait(); anyhow::bail!("UPDATE_COMMAND_PIDFD_UNAVAILABLE"); } let pidfd = unsafe { File::from_raw_fd(raw as i32) }; let mut owned = OwnedCommand { child, pidfd }; let deadline = Instant::now() + Duration::from_secs(10); let mut stdout = owned.child.stdout.take().unwrap(); let mut stderr = owned.child.stderr.take().unwrap(); for fd in [stdout.as_raw_fd(), stderr.as_raw_fd()] { ensure!( unsafe { libc::fcntl(fd, libc::F_SETFL, libc::O_NONBLOCK) } >= 0, "UPDATE_COMMAND_NONBLOCK" ); } let mut out = Vec::new(); let mut error = Vec::new(); loop { drain(&mut stdout, &mut out)?; drain(&mut stderr, &mut error)?; if let Some(status) = owned.child.try_wait()? { drain(&mut stdout, &mut out)?; drain(&mut stderr, &mut error)?; ensure!(status.success(), "UPDATE_SYSTEMD_COMMAND_FAILED"); return Ok(out); } if budget.check().is_err() || Instant::now() >= deadline { // Kill only our held systemctl client. A request may already have // reached systemd: the embedding worker must record Unknown. anyhow::bail!("UPDATE_SYSTEMD_REPLY_UNKNOWN"); } thread::sleep(Duration::from_millis(5)); } }