559 lines
17 KiB
Rust
559 lines
17 KiB
Rust
//! Complete current FD/maps observations plus exact known process generations.
|
|
//! This is one half of admission, never a replacement for the independently
|
|
//! qualified closed bootstrap/service registry or actual singleton/flock guard.
|
|
use crate::{
|
|
agent_startup::{BoundAgentLock, HeldAgentLock},
|
|
census_process::{Proc, Stamp},
|
|
reader_registry::QualifiedRegistry,
|
|
retained::{RetainedSet, Retention},
|
|
};
|
|
use anyhow::{Context, Result, ensure};
|
|
use rmm_domain::jobs::hash;
|
|
use rmm_updater_linux::{
|
|
Budget,
|
|
files::{Directory, Identity, LedgerGuard, LedgerIdentity},
|
|
services::{HeldService, Service, ServiceProof, StoppedServices},
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{
|
|
fs::{File, OpenOptions},
|
|
os::unix::fs::{MetadataExt, OpenOptionsExt},
|
|
path::Path,
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
const PATHS: [&str; 5] = [
|
|
"/var/lib/rmm-agent-remote/journal.sqlite",
|
|
"/var/lib/rmm-agent-remote/consumption.sqlite",
|
|
"/var/lib/rmm-helper/consumption.sqlite",
|
|
"/var/lib/rmm-helper/executor.lock",
|
|
"/var/lib/rmm-agent-remote/agent.lock",
|
|
];
|
|
/// Raw metadata only. Authority requires equality with the separately protected
|
|
/// compatible-bootstrap registry; own-namespace == PID1 alone is insufficient.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct HostContext {
|
|
pub proc_device: u64,
|
|
pub proc_inode: u64,
|
|
pub user_namespace_device: u64,
|
|
pub user_namespace_inode: u64,
|
|
pub pid_namespace_device: u64,
|
|
pub pid_namespace_inode: u64,
|
|
pub cgroup_namespace_device: u64,
|
|
pub cgroup_namespace_inode: u64,
|
|
}
|
|
pub fn current_host_context() -> Result<HostContext> {
|
|
Proc::open()?.context()
|
|
}
|
|
struct Target {
|
|
directory: Directory,
|
|
file: File,
|
|
identity: Identity,
|
|
}
|
|
/// Always the fixed three existing databases and two distinct existing locks.
|
|
pub struct Targets {
|
|
files: Vec<Target>,
|
|
sha256: String,
|
|
}
|
|
impl Targets {
|
|
pub fn bind(
|
|
retained: &RetainedSet,
|
|
ledger: &LedgerIdentity,
|
|
agent: &BoundAgentLock,
|
|
) -> Result<Self> {
|
|
retained.verify()?;
|
|
let specs = retained.specs();
|
|
let mut files = Vec::new();
|
|
for (index, path) in PATHS.iter().enumerate() {
|
|
let expected = match index {
|
|
3 => ledger.lock.clone(),
|
|
4 => agent.identity().clone(),
|
|
_ => {
|
|
let spec = specs
|
|
.iter()
|
|
.find(|s| s.path == Path::new(path))
|
|
.context("UPDATE_CENSUS_DATABASE_WITNESS_REQUIRED")?;
|
|
ensure!(
|
|
spec.retention == Retention::MutableDatabase,
|
|
"UPDATE_CENSUS_DATABASE_WITNESS_REQUIRED"
|
|
);
|
|
if index == 2 {
|
|
ensure!(
|
|
spec.identity == ledger.ledger,
|
|
"UPDATE_CENSUS_ROOT_LEDGER_CHANGED"
|
|
);
|
|
}
|
|
spec.identity.clone()
|
|
}
|
|
};
|
|
ensure!(
|
|
expected.uid == if index < 2 || index == 4 { 993 } else { 0 }
|
|
&& expected.mode & 0o022 == 0,
|
|
"UPDATE_CENSUS_TARGET_PERMISSIONS"
|
|
);
|
|
let directory = Directory::open(
|
|
Path::new(path)
|
|
.parent()
|
|
.context("UPDATE_CENSUS_TARGET_PARENT")?,
|
|
expected.uid,
|
|
true,
|
|
)?;
|
|
// O_PATH binds identity without reading any database/key contents.
|
|
let file = OpenOptions::new()
|
|
.read(true)
|
|
.custom_flags(libc::O_PATH | libc::O_NOFOLLOW | libc::O_CLOEXEC)
|
|
.open(path)?;
|
|
files.push(Target {
|
|
directory,
|
|
file,
|
|
identity: expected,
|
|
});
|
|
}
|
|
let sha256 = hash(&serde_json::to_vec(
|
|
&files.iter().map(|t| &t.identity).collect::<Vec<_>>(),
|
|
)?);
|
|
let result = Self { files, sha256 };
|
|
result.verify()?;
|
|
Ok(result)
|
|
}
|
|
fn verify(&self) -> Result<()> {
|
|
ensure!(self.files.len() == 5, "UPDATE_CENSUS_TARGET_SET");
|
|
for (target, path) in self.files.iter().zip(PATHS) {
|
|
target.directory.verify()?;
|
|
let held = target.file.metadata()?;
|
|
let named = std::fs::symlink_metadata(path)?;
|
|
ensure!(
|
|
held.is_file()
|
|
&& named.is_file()
|
|
&& !named.file_type().is_symlink()
|
|
&& identity(&held) == target.identity
|
|
&& identity(&named) == target.identity,
|
|
"UPDATE_CENSUS_TARGET_REPLACED"
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
struct Known {
|
|
service: ServiceProof,
|
|
stamp: Stamp,
|
|
}
|
|
/// Private observation, no Deserialize/Clone/caller completeness boolean.
|
|
pub struct Admission {
|
|
host: HostContext,
|
|
targets_sha256: String,
|
|
known: Vec<Known>,
|
|
finished: Instant,
|
|
summary: Summary,
|
|
}
|
|
#[derive(Serialize)]
|
|
struct Summary {
|
|
schema_version: u32,
|
|
phase: &'static str,
|
|
targets_sha256: String,
|
|
host_context_sha256: String,
|
|
known_generations_sha256: String,
|
|
pid_count: usize,
|
|
fd_count: usize,
|
|
map_count: usize,
|
|
project_matches: Vec<Match>,
|
|
}
|
|
#[derive(Serialize)]
|
|
struct Match {
|
|
pid: u32,
|
|
birth: u64,
|
|
target_indices: Vec<usize>,
|
|
}
|
|
pub struct Stopped {
|
|
host: HostContext,
|
|
targets_sha256: String,
|
|
updater: ServiceProof,
|
|
finished: Instant,
|
|
summary: Summary,
|
|
}
|
|
pub struct ClosureInputs<'a, 'stopped> {
|
|
pub targets: &'a Targets,
|
|
pub root: &'a LedgerGuard<'stopped>,
|
|
pub agent: &'a HeldAgentLock,
|
|
pub stopped: &'a StoppedServices,
|
|
pub updater: &'a HeldService,
|
|
pub registry: &'a QualifiedRegistry,
|
|
}
|
|
/// The observed absence is retained only under continuously borrowed original
|
|
/// singleton/flock guards. No freshness timestamp is renewed and no future
|
|
/// noncooperating privileged process absence is claimed.
|
|
pub struct ClosedReaders<'a, 'stopped> {
|
|
proof: Stopped,
|
|
input: ClosureInputs<'a, 'stopped>,
|
|
}
|
|
impl ClosedReaders<'_, '_> {
|
|
pub fn verify(&self, budget: &Budget) -> Result<()> {
|
|
self.input.registry.verify_protected()?;
|
|
self.proof.verify_held(
|
|
self.input.targets,
|
|
self.input.root,
|
|
self.input.agent,
|
|
self.input.stopped,
|
|
self.input.updater,
|
|
budget,
|
|
)
|
|
}
|
|
}
|
|
impl Admission {
|
|
pub fn sha256(&self) -> Result<String> {
|
|
Ok(hash(&serde_json::to_vec(&self.summary)?))
|
|
}
|
|
/// Must be called again by the exact live original authorizer immediately
|
|
/// before consumption; a passive persisted hash cannot stand in for it.
|
|
pub fn verify_fresh(
|
|
&self,
|
|
targets: &Targets,
|
|
known: &[&HeldService],
|
|
budget: &Budget,
|
|
) -> Result<()> {
|
|
budget.check()?;
|
|
ensure!(
|
|
self.finished.elapsed() < Duration::from_secs(1)
|
|
&& self.targets_sha256 == targets.sha256,
|
|
"UPDATE_CENSUS_ADMISSION_STALE"
|
|
);
|
|
targets.verify()?;
|
|
ensure!(
|
|
known.len() == self.known.len(),
|
|
"UPDATE_CENSUS_READER_SET_CHANGED"
|
|
);
|
|
let proc = Proc::open()?;
|
|
ensure!(
|
|
proc.context()? == self.host,
|
|
"UPDATE_CENSUS_HOST_CONTEXT_CHANGED"
|
|
);
|
|
for (held, old) in known.iter().zip(&self.known) {
|
|
held.verify(budget)?;
|
|
ensure!(
|
|
*held.proof() == old.service && proc.process(old.stamp.pid)?.stamp == old.stamp,
|
|
"UPDATE_CENSUS_READER_GENERATION_CHANGED"
|
|
);
|
|
}
|
|
targets.verify()?;
|
|
budget.check()?;
|
|
ensure!(
|
|
self.finished.elapsed() < Duration::from_secs(1),
|
|
"UPDATE_CENSUS_ADMISSION_STALE"
|
|
);
|
|
Ok(())
|
|
}
|
|
}
|
|
impl Stopped {
|
|
pub fn bind<'a, 'stopped>(
|
|
self,
|
|
input: ClosureInputs<'a, 'stopped>,
|
|
budget: &Budget,
|
|
) -> Result<ClosedReaders<'a, 'stopped>> {
|
|
input.registry.verify_protected()?;
|
|
ensure!(
|
|
*input.registry.host_context() == self.host,
|
|
"UPDATE_CENSUS_REGISTRY_HOST_MISMATCH"
|
|
);
|
|
self.verify_fresh(
|
|
input.targets,
|
|
input.root,
|
|
input.agent,
|
|
input.stopped,
|
|
input.updater,
|
|
budget,
|
|
)?;
|
|
Ok(ClosedReaders { proof: self, input })
|
|
}
|
|
pub fn sha256(&self) -> Result<String> {
|
|
Ok(hash(&serde_json::to_vec(&self.summary)?))
|
|
}
|
|
pub fn verify_fresh(
|
|
&self,
|
|
targets: &Targets,
|
|
root: &LedgerGuard<'_>,
|
|
agent: &HeldAgentLock,
|
|
stopped: &StoppedServices,
|
|
updater: &HeldService,
|
|
budget: &Budget,
|
|
) -> Result<()> {
|
|
budget.check()?;
|
|
ensure!(
|
|
self.finished.elapsed() < Duration::from_secs(1)
|
|
&& self.targets_sha256 == targets.sha256,
|
|
"UPDATE_CENSUS_STOPPED_STALE"
|
|
);
|
|
self.verify_held(targets, root, agent, stopped, updater, budget)?;
|
|
budget.check()?;
|
|
ensure!(
|
|
self.finished.elapsed() < Duration::from_secs(1),
|
|
"UPDATE_CENSUS_STOPPED_STALE"
|
|
);
|
|
Ok(())
|
|
}
|
|
fn verify_held(
|
|
&self,
|
|
targets: &Targets,
|
|
root: &LedgerGuard<'_>,
|
|
agent: &HeldAgentLock,
|
|
stopped: &StoppedServices,
|
|
updater: &HeldService,
|
|
budget: &Budget,
|
|
) -> Result<()> {
|
|
targets.verify()?;
|
|
root.verify_for_effect(budget)?;
|
|
agent.verify()?;
|
|
stopped.verify_still_stopped(budget)?;
|
|
updater.verify(budget)?;
|
|
ensure!(
|
|
self.targets_sha256 == targets.sha256
|
|
&& targets.files[2].identity == root.identity().ledger
|
|
&& targets.files[3].identity == root.identity().lock
|
|
&& targets.files[4].identity == *agent.identity()
|
|
&& *updater.proof() == self.updater
|
|
&& current_host_context()? == self.host,
|
|
"UPDATE_CENSUS_HELD_CLOSURE_CHANGED"
|
|
);
|
|
targets.verify()?;
|
|
budget.check()
|
|
}
|
|
}
|
|
pub fn scan_admission(
|
|
targets: &Targets,
|
|
known: &[&HeldService],
|
|
qualified_host: &HostContext,
|
|
budget: &Budget,
|
|
) -> Result<Admission> {
|
|
ensure!(
|
|
known.iter().map(|s| s.proof().service).eq([
|
|
Service::Agent,
|
|
Service::General,
|
|
Service::Full,
|
|
Service::Updater
|
|
]),
|
|
"UPDATE_CENSUS_KNOWN_READERS"
|
|
);
|
|
targets.verify()?;
|
|
let proc = Proc::open()?;
|
|
ensure!(
|
|
proc.context()? == *qualified_host,
|
|
"UPDATE_CENSUS_HOST_CONTEXT_UNQUALIFIED"
|
|
);
|
|
let mut saved = Vec::new();
|
|
for held in known {
|
|
held.verify(budget)?;
|
|
let process = proc.process(held.proof().process.pid)?;
|
|
proc.verify_known_namespace(&process.stamp)?;
|
|
bind_process(&process.stamp, held.proof())?;
|
|
saved.push(Known {
|
|
service: held.proof().clone(),
|
|
stamp: process.stamp,
|
|
});
|
|
}
|
|
ensure!(
|
|
saved
|
|
.iter()
|
|
.filter(|s| s.stamp.pid == std::process::id()
|
|
&& matches!(s.service.service, Service::Full | Service::Updater))
|
|
.count()
|
|
== 1,
|
|
"UPDATE_CENSUS_AUTHORIZER_PROCESS_REQUIRED"
|
|
);
|
|
let summary = scan(&proc, targets, &saved, &[], "admission", budget)?;
|
|
let finished = Instant::now();
|
|
for (held, old) in known.iter().zip(&saved) {
|
|
held.verify(budget)?;
|
|
ensure!(
|
|
proc.process(old.stamp.pid)?.stamp == old.stamp,
|
|
"UPDATE_CENSUS_READER_CHANGED"
|
|
);
|
|
}
|
|
targets.verify()?;
|
|
ensure!(
|
|
finished.elapsed() < Duration::from_secs(1),
|
|
"UPDATE_CENSUS_ADMISSION_STALE"
|
|
);
|
|
Ok(Admission {
|
|
host: proc.context()?,
|
|
targets_sha256: targets.sha256.clone(),
|
|
known: saved,
|
|
finished,
|
|
summary,
|
|
})
|
|
}
|
|
pub fn scan_stopped(
|
|
targets: &Targets,
|
|
admission: &Admission,
|
|
root: &LedgerGuard<'_>,
|
|
agent: &HeldAgentLock,
|
|
stopped: &StoppedServices,
|
|
updater: &HeldService,
|
|
budget: &Budget,
|
|
) -> Result<Stopped> {
|
|
root.verify_for_effect(budget)?;
|
|
agent.verify()?;
|
|
stopped.verify_still_stopped(budget)?;
|
|
updater.verify(budget)?;
|
|
targets.verify()?;
|
|
ensure!(
|
|
admission.targets_sha256 == targets.sha256
|
|
&& targets.files[2].identity == root.identity().ledger
|
|
&& targets.files[3].identity == root.identity().lock
|
|
&& targets.files[4].identity == *agent.identity()
|
|
&& updater.proof().service == Service::Updater
|
|
&& updater.proof().process.pid == std::process::id(),
|
|
"UPDATE_CENSUS_STOPPED_LOCKS_OR_OWNER"
|
|
);
|
|
let old = stopped.proofs();
|
|
ensure!(
|
|
old.len() == 3
|
|
&& old
|
|
.iter()
|
|
.zip(&admission.known)
|
|
.all(|(s, k)| *s == k.service),
|
|
"UPDATE_CENSUS_ORIGINAL_STOP_SET_CHANGED"
|
|
);
|
|
let original_updater = admission
|
|
.known
|
|
.last()
|
|
.context("UPDATE_CENSUS_UPDATER_MISSING")?;
|
|
ensure!(
|
|
*updater.proof() == original_updater.service,
|
|
"UPDATE_CENSUS_ORIGINAL_UPDATER_CHANGED"
|
|
);
|
|
let forbidden = admission.known[..3]
|
|
.iter()
|
|
.flat_map(|k| k.stamp.cgroup.iter().cloned())
|
|
.collect::<Vec<_>>();
|
|
let proc = Proc::open()?;
|
|
ensure!(
|
|
proc.context()? == admission.host,
|
|
"UPDATE_CENSUS_HOST_CONTEXT_CHANGED"
|
|
);
|
|
proc.verify_known_namespace(&original_updater.stamp)?;
|
|
let summary = scan(
|
|
&proc,
|
|
targets,
|
|
std::slice::from_ref(original_updater),
|
|
&forbidden,
|
|
"stopped",
|
|
budget,
|
|
)?;
|
|
let finished = Instant::now();
|
|
root.verify_for_effect(budget)?;
|
|
agent.verify()?;
|
|
stopped.verify_still_stopped(budget)?;
|
|
updater.verify(budget)?;
|
|
targets.verify()?;
|
|
ensure!(
|
|
finished.elapsed() < Duration::from_secs(1),
|
|
"UPDATE_CENSUS_STOPPED_STALE"
|
|
);
|
|
Ok(Stopped {
|
|
host: admission.host.clone(),
|
|
targets_sha256: targets.sha256.clone(),
|
|
updater: updater.proof().clone(),
|
|
finished,
|
|
summary,
|
|
})
|
|
}
|
|
fn scan(
|
|
proc: &Proc,
|
|
targets: &Targets,
|
|
allowed: &[Known],
|
|
forbidden_cgroups: &[String],
|
|
phase: &'static str,
|
|
budget: &Budget,
|
|
) -> Result<Summary> {
|
|
let began = Instant::now();
|
|
let pids = proc.pids(budget)?;
|
|
let ids = targets
|
|
.files
|
|
.iter()
|
|
.map(|t| t.identity.clone())
|
|
.collect::<Vec<_>>();
|
|
let mut summary = Summary {
|
|
schema_version: 1,
|
|
phase,
|
|
targets_sha256: targets.sha256.clone(),
|
|
host_context_sha256: hash(&serde_json::to_vec(&proc.context()?)?),
|
|
known_generations_sha256: hash(&serde_json::to_vec(
|
|
&allowed.iter().map(|k| &k.service).collect::<Vec<_>>(),
|
|
)?),
|
|
pid_count: pids.len(),
|
|
fd_count: 0,
|
|
map_count: 0,
|
|
project_matches: Vec::new(),
|
|
};
|
|
for pid in &pids {
|
|
budget.check()?;
|
|
ensure!(
|
|
began.elapsed() < Duration::from_secs(5),
|
|
"UPDATE_CENSUS_TOTAL_DEADLINE"
|
|
);
|
|
let process = proc.process(*pid)?;
|
|
let hits = process.inspect(&ids, budget)?;
|
|
ensure!(
|
|
!hits.task_cgroups.iter().any(|c| forbidden_cgroups
|
|
.iter()
|
|
.any(|old| c == old || c.starts_with(&(old.to_owned() + "/")))),
|
|
"UPDATE_CENSUS_STOPPED_CGROUP_HAS_PROCESS"
|
|
);
|
|
summary.fd_count += hits.fd_count;
|
|
summary.map_count += hits.map_count;
|
|
let known = allowed.iter().find(|k| k.stamp.pid == *pid);
|
|
if let Some(known) = known {
|
|
ensure!(
|
|
known.stamp == hits.stamp,
|
|
"UPDATE_CENSUS_KNOWN_PROCESS_CHANGED"
|
|
);
|
|
}
|
|
if !hits.descriptors.is_empty() || !hits.mappings.is_empty() {
|
|
ensure!(known.is_some(), "UPDATE_CENSUS_UNKNOWN_READER");
|
|
proc.verify_known_namespace(&hits.stamp)?;
|
|
let mut indices = hits.descriptors;
|
|
indices.extend(hits.mappings);
|
|
indices.sort_unstable();
|
|
indices.dedup();
|
|
summary.project_matches.push(Match {
|
|
pid: *pid,
|
|
birth: hits.stamp.birth,
|
|
target_indices: indices,
|
|
});
|
|
}
|
|
}
|
|
ensure!(
|
|
proc.pids(budget)? == pids && began.elapsed() < Duration::from_secs(5),
|
|
"UPDATE_CENSUS_INCOMPLETE_OR_CHANGED_PID_SET"
|
|
);
|
|
Ok(summary)
|
|
}
|
|
fn bind_process(stamp: &Stamp, service: &ServiceProof) -> Result<()> {
|
|
ensure!(
|
|
stamp.pid == service.process.pid
|
|
&& stamp.birth == service.process.birth
|
|
&& stamp.uid == service.process.uid
|
|
&& stamp.gid == service.process.gid
|
|
&& stamp.image.is_some(),
|
|
"UPDATE_CENSUS_PINNED_PROCESS_CHANGED"
|
|
);
|
|
ensure!(
|
|
stamp
|
|
.cgroup
|
|
.iter()
|
|
.any(|p| p.rsplit('/').next() == Some(service.service.unit())),
|
|
"UPDATE_CENSUS_SERVICE_CGROUP_UNBOUND"
|
|
);
|
|
Ok(())
|
|
}
|
|
fn identity(meta: &std::fs::Metadata) -> Identity {
|
|
Identity {
|
|
device: meta.dev(),
|
|
inode: meta.ino(),
|
|
uid: meta.uid(),
|
|
gid: meta.gid(),
|
|
mode: meta.mode(),
|
|
links: meta.nlink(),
|
|
}
|
|
}
|