//! Bounded kernel observations only. No command line/environment/key reads and //! no data from unrelated processes is returned. Not admission by itself. use anyhow::{Context, Result, ensure}; use rmm_updater_linux::{Budget, files::Identity}; use std::{ ffi::CString, fs::File, io::Read, os::{ fd::{AsRawFd, FromRawFd}, unix::fs::MetadataExt, }, }; const MAX_PIDS: usize = 16384; const MAX_FDS: usize = 65536; const MAX_MAPS: usize = 8 * 1024 * 1024; const PROC_MAGIC: libc::c_long = 0x9fa0; #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct Namespace { pub device: u64, pub inode: u64, } #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct Stamp { pub pid: u32, pub birth: u64, pub tgid: u32, pub uid: u32, pub gid: u32, pub image: Option<(u64, u64, i64, i64, i64, i64)>, pub cgroup: Vec, pub user_ns: Namespace, pub pid_ns: Namespace, pub cgroup_ns: Namespace, } pub(crate) struct Process { pidfd: File, directory: File, pub stamp: Stamp, } #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct Hits { pub stamp: Stamp, pub descriptors: Vec, pub mappings: Vec, pub fd_count: usize, pub task_cgroups: Vec, pub map_count: usize, } pub(crate) struct Proc { directory: File, user_ns: Namespace, pid_ns: Namespace, cgroup_ns: Namespace, } impl Proc { pub fn context(&self) -> Result { let meta = self.directory.metadata()?; Ok(crate::census::HostContext { proc_device: meta.dev(), proc_inode: meta.ino(), user_namespace_device: self.user_ns.device, user_namespace_inode: self.user_ns.inode, pid_namespace_device: self.pid_ns.device, pid_namespace_inode: self.pid_ns.inode, cgroup_namespace_device: self.cgroup_ns.device, cgroup_namespace_inode: self.cgroup_ns.inode, }) } pub fn open() -> Result { ensure!( unsafe { libc::geteuid() } == 0, "UPDATE_CENSUS_HOST_ROOT_REQUIRED" ); let path = CString::new("/proc")?; let fd = unsafe { libc::open( path.as_ptr(), libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC, ) }; ensure!(fd >= 0, "UPDATE_CENSUS_PROC_OPEN"); let directory = unsafe { File::from_raw_fd(fd) }; let mut info = std::mem::MaybeUninit::::zeroed(); ensure!( unsafe { libc::fstatfs(directory.as_raw_fd(), info.as_mut_ptr()) } == 0 && unsafe { info.assume_init() }.f_type == PROC_MAGIC, "UPDATE_CENSUS_PROC_FILESYSTEM" ); ensure!( directory.metadata()?.uid() == 0 && directory.metadata()?.mode() & 0o022 == 0, "UPDATE_CENSUS_PROC_OWNER" ); let group = open_dir(&directory, &std::process::id().to_string())?; let tasks = open_dir(&group, "task")?; let tid = unsafe { libc::syscall(libc::SYS_gettid) }; ensure!(tid > 0, "UPDATE_CENSUS_CURRENT_TASK"); let own = open_dir(&tasks, &tid.to_string())?; let own_stamp = stamp(&own, u32::try_from(tid)?)?; ensure!( own_stamp.tgid == std::process::id() && own_stamp.uid == 0, "UPDATE_CENSUS_CURRENT_TASK_NOT_HOST_ROOT" ); let init = open_dir(&directory, "1")?; let user_ns = namespace(&own, "user")?; let pid_ns = namespace(&own, "pid")?; let cgroup_ns = namespace(&own, "cgroup")?; ensure!( namespace(&init, "user")? == user_ns && namespace(&init, "pid")? == pid_ns, "UPDATE_CENSUS_NAMESPACE_NOT_HOST_VIEW" ); // A hidden/subset proc mount cannot prove a complete PID enumeration. // Paths are examined only in memory; no mountinfo content is exported. let raw = read(&own, "mountinfo", 256 * 1024)?; let text = std::str::from_utf8(&raw)?; let mut count = 0; for row in text.lines() { let (left, right) = row.split_once(" - ").context("UPDATE_CENSUS_MOUNTINFO")?; let left = left.split_ascii_whitespace().collect::>(); let right = right.split_ascii_whitespace().collect::>(); ensure!( left.len() >= 6 && right.len() >= 3, "UPDATE_CENSUS_MOUNTINFO" ); if left[4] == "/proc" { count += 1; ensure!( left[3] == "/" && right[0] == "proc" && left[5] .split(',') .chain(right[2].split(',')) .all(|v| matches!( v, "rw" | "ro" | "nosuid" | "nodev" | "noexec" | "relatime" | "strictatime" | "noatime" | "nodiratime" | "hidepid=0" | "hidepid=off" )), "UPDATE_CENSUS_HIDDEN_OR_UNKNOWN_PROC_OPTIONS" ); } } ensure!(count == 1, "UPDATE_CENSUS_PROC_MOUNT_AMBIGUOUS"); Ok(Self { directory, user_ns, pid_ns, cgroup_ns, }) } pub fn pids(&self, budget: &Budget) -> Result> { let mut pids = Vec::new(); for entry in std::fs::read_dir(format!( "/proc/thread-self/fd/{}", self.directory.as_raw_fd() ))? { budget.check()?; let name = entry?.file_name(); let Some(name) = name.to_str() else { anyhow::bail!("UPDATE_CENSUS_PROC_ENTRY") }; if name.bytes().all(|v| v.is_ascii_digit()) { let pid: u32 = name.parse()?; ensure!(pid > 0 && pids.len() < MAX_PIDS, "UPDATE_CENSUS_PID_LIMIT"); pids.push(pid); } } pids.sort_unstable(); ensure!( !pids.is_empty() && !pids.windows(2).any(|w| w[0] == w[1]), "UPDATE_CENSUS_PID_SET" ); Ok(pids) } pub fn process(&self, pid: u32) -> Result { ensure!(pid > 0, "UPDATE_CENSUS_PID"); let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, pid, 0) }; ensure!(fd >= 0, "UPDATE_CENSUS_PIDFD_UNAVAILABLE"); let pidfd = unsafe { File::from_raw_fd(fd as i32) }; let directory = open_dir(&self.directory, &pid.to_string())?; let stamp = stamp(&directory, pid)?; let result = Process { pidfd, directory, stamp, }; result.verify()?; Ok(result) } pub fn verify_known_namespace(&self, stamp: &Stamp) -> Result<()> { ensure!( stamp.user_ns == self.user_ns && stamp.pid_ns == self.pid_ns && stamp.cgroup_ns == self.cgroup_ns, "UPDATE_CENSUS_READER_NAMESPACE_CHANGED" ); Ok(()) } } impl Process { pub fn verify(&self) -> Result<()> { let mut poll = libc::pollfd { fd: self.pidfd.as_raw_fd(), events: libc::POLLIN, revents: 0, }; ensure!( unsafe { libc::poll(&mut poll, 1, 0) } == 0 && poll.revents == 0 && stamp(&self.directory, self.stamp.pid)? == self.stamp, "UPDATE_CENSUS_PROCESS_CHANGED" ); Ok(()) } pub fn inspect(&self, targets: &[Identity], budget: &Budget) -> Result { self.verify()?; let task_dir = open_dir(&self.directory, "task")?; let tids = task_ids(&task_dir, budget)?; let mut held_tasks = Vec::new(); let mut descriptors = Vec::new(); let mut fd_count = 0; let mut task_cgroups = Vec::new(); for tid in &tids { self.verify()?; let directory = open_dir(&task_dir, &tid.to_string())?; let before = stamp(&directory, *tid)?; ensure!( before.tgid == self.stamp.pid, "UPDATE_CENSUS_TASK_GROUP_CHANGED" ); let (hits, count) = inspect_fds(&directory, targets, budget)?; fd_count += count; ensure!(fd_count <= MAX_FDS, "UPDATE_CENSUS_TOTAL_FD_LIMIT"); if !hits.is_empty() { ensure!( before.uid == self.stamp.uid && before.gid == self.stamp.gid && before.user_ns == self.stamp.user_ns && before.pid_ns == self.stamp.pid_ns && before.cgroup_ns == self.stamp.cgroup_ns, "UPDATE_CENSUS_READER_TASK_CONTEXT_CHANGED" ); } descriptors.extend(hits); task_cgroups.extend(before.cgroup.iter().cloned()); ensure!( stamp(&directory, *tid)? == before, "UPDATE_CENSUS_TASK_CHANGED" ); // Retain each actual task directory until the complete probe ends; // a same-TID replacement cannot substitute a new task directory. held_tasks.push((directory, before)); } let raw = read(&self.directory, "maps", MAX_MAPS)?; let text = std::str::from_utf8(&raw)?; let mut mappings = Vec::new(); let mut map_count = 0; for row in text.lines() { budget.check()?; map_count += 1; ensure!(map_count <= 65536, "UPDATE_CENSUS_MAP_LIMIT"); let mut fields = row.split_ascii_whitespace(); for _ in 0..3 { fields.next().context("UPDATE_CENSUS_MAP_FIELDS")?; } let (major, minor) = fields .next() .context("UPDATE_CENSUS_MAP_DEVICE")? .split_once(':') .context("UPDATE_CENSUS_MAP_DEVICE")?; let device = libc::makedev( u32::from_str_radix(major, 16)?, u32::from_str_radix(minor, 16)?, ); let inode: u64 = fields.next().context("UPDATE_CENSUS_MAP_INODE")?.parse()?; if inode != 0 { for (index, target) in targets.iter().enumerate() { if device == target.device && inode == target.inode { mappings.push(index); } } } } descriptors.sort_unstable(); descriptors.dedup(); mappings.sort_unstable(); mappings.dedup(); ensure!( task_ids(&task_dir, budget)? == tids, "UPDATE_CENSUS_TASK_SET_CHANGED" ); for (directory, before) in held_tasks { ensure!( stamp(&directory, before.pid)? == before, "UPDATE_CENSUS_TASK_CHANGED" ); } task_cgroups.sort(); task_cgroups.dedup(); self.verify()?; Ok(Hits { stamp: self.stamp.clone(), descriptors, mappings, fd_count, task_cgroups, map_count, }) } } fn inspect_fds( directory: &File, targets: &[Identity], budget: &Budget, ) -> Result<(Vec, usize)> { let fd_dir = open_dir(directory, "fd")?; let mut descriptors = Vec::new(); let mut fd_count = 0; // Keep read_dir's descriptor alive while inspecting every magic link; // this also handles enumeration of our own descriptor without a gap. for entry in std::fs::read_dir(format!("/proc/thread-self/fd/{}", fd_dir.as_raw_fd()))? { budget.check()?; fd_count += 1; ensure!(fd_count <= MAX_FDS, "UPDATE_CENSUS_FD_LIMIT"); let entry = entry?; let name = entry.file_name(); let name = name.to_str().context("UPDATE_CENSUS_FD_NAME")?; ensure!( !name.is_empty() && name.bytes().all(|v| v.is_ascii_digit()), "UPDATE_CENSUS_FD_NAME" ); let name = CString::new(name)?; let mut info = std::mem::MaybeUninit::::zeroed(); ensure!( unsafe { libc::fstatat(fd_dir.as_raw_fd(), name.as_ptr(), info.as_mut_ptr(), 0) } == 0, "UPDATE_CENSUS_INCOMPLETE_FD" ); let info = unsafe { info.assume_init() }; for (index, target) in targets.iter().enumerate() { if info.st_dev == target.device && info.st_ino == target.inode { descriptors.push(index); } } } Ok((descriptors, fd_count)) } fn task_ids(directory: &File, budget: &Budget) -> Result> { let mut ids = Vec::new(); for entry in std::fs::read_dir(format!("/proc/thread-self/fd/{}", directory.as_raw_fd()))? { budget.check()?; let name = entry?.file_name(); let text = name.to_str().context("UPDATE_CENSUS_TASK_NAME")?; ensure!( !text.is_empty() && text.bytes().all(|v| v.is_ascii_digit()) && ids.len() < 4096, "UPDATE_CENSUS_TASK_LIMIT" ); ids.push(text.parse()?); } ids.sort_unstable(); ensure!( !ids.is_empty() && !ids.windows(2).any(|w| w[0] == w[1]), "UPDATE_CENSUS_TASK_SET" ); Ok(ids) } fn open_dir(parent: &File, name: &str) -> Result { let name = CString::new(name)?; let fd = unsafe { libc::openat( parent.as_raw_fd(), name.as_ptr(), libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC, ) }; ensure!(fd >= 0, "UPDATE_CENSUS_DIRECTORY_UNAVAILABLE"); Ok(unsafe { File::from_raw_fd(fd) }) } fn read(parent: &File, name: &str, max: usize) -> Result> { let name = CString::new(name)?; let fd = unsafe { libc::openat( parent.as_raw_fd(), name.as_ptr(), libc::O_RDONLY | libc::O_NOFOLLOW | libc::O_CLOEXEC, ) }; ensure!(fd >= 0, "UPDATE_CENSUS_PROC_DATA_UNAVAILABLE"); let mut raw = Vec::new(); unsafe { File::from_raw_fd(fd) } .take((max + 1) as u64) .read_to_end(&mut raw)?; ensure!(raw.len() <= max, "UPDATE_CENSUS_PROC_DATA_LIMIT"); Ok(raw) } fn namespace(parent: &File, kind: &str) -> Result { let name = CString::new(format!("ns/{kind}"))?; let mut info = std::mem::MaybeUninit::::zeroed(); ensure!( unsafe { libc::fstatat(parent.as_raw_fd(), name.as_ptr(), info.as_mut_ptr(), 0) } == 0, "UPDATE_CENSUS_NAMESPACE_UNAVAILABLE" ); let info = unsafe { info.assume_init() }; Ok(Namespace { device: info.st_dev, inode: info.st_ino, }) } fn stamp(parent: &File, pid: u32) -> Result { let raw = read(parent, "stat", 65536)?; let raw = std::str::from_utf8(&raw)?; let (_, fields) = raw.rsplit_once(") ").context("UPDATE_CENSUS_STAT")?; let fields = fields.split_ascii_whitespace().collect::>(); ensure!(fields.len() >= 20, "UPDATE_CENSUS_STAT"); let birth = fields[19].parse()?; let flags: u64 = fields[6].parse()?; let raw = read(parent, "status", 65536)?; let text = std::str::from_utf8(&raw)?; let id = |label: &str| -> Result { let mut rows = text.lines().filter_map(|r| r.strip_prefix(label)); let row = rows.next().context("UPDATE_CENSUS_CREDENTIALS")?; ensure!(rows.next().is_none(), "UPDATE_CENSUS_CREDENTIALS"); let fields = row .split_ascii_whitespace() .map(str::parse) .collect::, _>>()?; ensure!( fields.len() == 4 && fields.iter().all(|v| *v == fields[0]), "UPDATE_CENSUS_CHANGING_CREDENTIALS" ); Ok(fields[0]) }; let uid = id("Uid:")?; let gid = id("Gid:")?; let mut tgids = text.lines().filter_map(|r| r.strip_prefix("Tgid:")); let tgid = tgids .next() .context("UPDATE_CENSUS_TASK_GROUP")? .trim() .parse()?; ensure!(tgids.next().is_none(), "UPDATE_CENSUS_TASK_GROUP"); let name = CString::new("exe")?; let fd = unsafe { libc::openat( parent.as_raw_fd(), name.as_ptr(), libc::O_PATH | libc::O_CLOEXEC, ) }; let image = if fd >= 0 { let file = unsafe { File::from_raw_fd(fd) }; let meta = file.metadata()?; ensure!(meta.is_file(), "UPDATE_CENSUS_PROCESS_IMAGE"); Some(( meta.dev(), meta.ino(), meta.mtime(), meta.mtime_nsec(), meta.ctime(), meta.ctime_nsec(), )) } else { ensure!( std::io::Error::last_os_error().raw_os_error() == Some(libc::ENOENT) && flags & 0x0020_0000 != 0, "UPDATE_CENSUS_PROCESS_IMAGE_UNAVAILABLE" ); None }; let raw = read(parent, "cgroup", 65536)?; let text = std::str::from_utf8(&raw)?; let mut cgroup = Vec::new(); for line in text.lines() { let mut fields = line.splitn(3, ':'); fields.next().context("UPDATE_CENSUS_CGROUP")?; fields.next().context("UPDATE_CENSUS_CGROUP")?; let path = fields.next().context("UPDATE_CENSUS_CGROUP")?; // Kernel cgroup paths are compared only as bounded opaque strings; // they are never used as filesystem paths. Host namespace authority // comes separately from the protected bootstrap registry. ensure!( path.starts_with('/') && path.len() <= 4096, "UPDATE_CENSUS_CGROUP" ); cgroup.push(path.to_owned()); } ensure!( !cgroup.is_empty() && cgroup.len() <= 64, "UPDATE_CENSUS_CGROUP" ); Ok(Stamp { pid, birth, tgid, uid, gid, image, cgroup, user_ns: namespace(parent, "user")?, pid_ns: namespace(parent, "pid")?, cgroup_ns: namespace(parent, "cgroup")?, }) } #[cfg(test)] mod tests { use super::*; use std::{ fs::{self, OpenOptions}, os::unix::fs::OpenOptionsExt, time::Duration, }; #[test] fn private_thread_fd_table_cannot_hide_a_reader_from_the_group_probe() { assert_eq!(unsafe { libc::geteuid() }, 0); let path = std::path::Path::new("/run") .join(format!("rmm-census-thread-{}", uuid::Uuid::new_v4())); let file = OpenOptions::new() .read(true) .write(true) .create_new(true) .mode(0o600) .open(&path) .unwrap(); struct Cleanup(std::path::PathBuf); impl Drop for Cleanup { fn drop(&mut self) { fs::remove_file(&self.0).unwrap(); } } let _cleanup = Cleanup(path.clone()); let meta = file.metadata().unwrap(); let target = Identity { device: meta.dev(), inode: meta.ino(), uid: meta.uid(), gid: meta.gid(), mode: meta.mode(), links: meta.nlink(), }; let (ready, wait_ready) = std::sync::mpsc::channel(); let (release, wait_release) = std::sync::mpsc::channel(); let thread = std::thread::spawn(move || { // Only this owned fixture thread separates its descriptor table. // close_range(UNSHARE) is the same files-table operation without // Docker's unrelated unshare syscall gate. UINT_MAX cannot name // an open Linux fd, so no descriptor is closed. No namespace, // mount, machine policy, seccomp profile or service is changed. if unsafe { libc::syscall( libc::SYS_close_range, u32::MAX, u32::MAX, libc::CLOSE_RANGE_UNSHARE, ) } != 0 { ready .send(Err(std::io::Error::last_os_error().raw_os_error())) .unwrap(); return; } let private = File::open(path).unwrap(); ready.send(Ok(())).unwrap(); let _ = wait_release.recv(); drop(private); }); struct Thread { release: Option>, thread: Option>, } impl Drop for Thread { fn drop(&mut self) { if let Some(tx) = self.release.take() { let _ = tx.send(()); } if let Some(thread) = self.thread.take() { thread.join().unwrap(); } } } let thread = Thread { release: Some(release), thread: Some(thread), }; assert_eq!( wait_ready.recv().unwrap(), Ok(()), "fixture requires close_range private-files-table support" ); drop(file); let budget = Budget::new( fs::read_to_string("/proc/sys/kernel/random/boot_id") .unwrap() .trim() .parse() .unwrap(), Duration::from_secs(5), ) .unwrap(); let proc = Proc::open().unwrap(); let process = proc.process(std::process::id()).unwrap(); // Reproduces the incomplete TGID-only algorithm: no matching FD. assert!( inspect_fds(&process.directory, std::slice::from_ref(&target), &budget) .unwrap() .0 .is_empty() ); // The real complete probe must still find the reader's task table. let hits = process.inspect(&[target], &budget).unwrap(); assert_eq!(hits.descriptors, [0]); assert!(hits.mappings.is_empty()); drop(thread); } #[test] fn actual_proc_probe_detects_existing_descriptors_and_mapped_inode_without_exporting_paths() { // An isolated network-none container process, not a host-wide scan, // service/namespace qualification or admission-positive fixture. assert_eq!(unsafe { libc::geteuid() }, 0); let path = std::path::Path::new("/run").join(format!("rmm-census-probe-{}", uuid::Uuid::new_v4())); let file = OpenOptions::new() .read(true) .write(true) .create_new(true) .mode(0o600) .open(&path) .unwrap(); file.set_len(4096).unwrap(); struct Cleanup(std::path::PathBuf); impl Drop for Cleanup { fn drop(&mut self) { fs::remove_file(&self.0).unwrap(); } } let _cleanup = Cleanup(path); let meta = file.metadata().unwrap(); let target = Identity { device: meta.dev(), inode: meta.ino(), uid: meta.uid(), gid: meta.gid(), mode: meta.mode(), links: meta.nlink(), }; let budget = Budget::new( fs::read_to_string("/proc/sys/kernel/random/boot_id") .unwrap() .trim() .parse() .unwrap(), Duration::from_secs(5), ) .unwrap(); let proc = Proc::open().unwrap(); let process = proc.process(std::process::id()).unwrap(); let before = process .inspect(std::slice::from_ref(&target), &budget) .unwrap(); assert_eq!(before.descriptors, [0]); assert!(before.mappings.is_empty()); let pointer = unsafe { libc::mmap( std::ptr::null_mut(), 4096, libc::PROT_READ, libc::MAP_PRIVATE, file.as_raw_fd(), 0, ) }; assert_ne!(pointer, libc::MAP_FAILED); struct Mapping(*mut libc::c_void); impl Drop for Mapping { fn drop(&mut self) { assert_eq!(unsafe { libc::munmap(self.0, 4096) }, 0); } } let _mapping = Mapping(pointer); drop(file); let mapped = process.inspect(&[target], &budget).unwrap(); assert!(mapped.descriptors.is_empty()); assert_eq!(mapped.mappings, [0]); assert_eq!(before.stamp, mapped.stamp); } }