use std::io;
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use super::parse::{is_at_or_under, parse_v2_relative_path};
const PIDFD_GET_INFO: libc::Ioctl = 0xC040_FF0B_u32 as libc::Ioctl;
const PIDFD_INFO_CGROUPID: u64 = 1 << 2;
#[repr(C)]
#[derive(Default)]
struct PidfdInfo {
mask: u64,
cgroupid: u64,
rest: [u32; 12],
}
pub(crate) fn pidfd_cgroup_id(pidfd: BorrowedFd<'_>) -> io::Result<Option<u64>> {
#[cfg(test)]
if super::fault::pidfd_info_missing() {
return Ok(None);
}
#[cfg(test)]
if super::fault::pidfd_info_fails() {
return Err(io::Error::from_raw_os_error(libc::EIO));
}
#[cfg(test)]
if let Some(id) = super::fault::forced_pidfd_cgroup_id() {
return Ok(Some(id));
}
let mut info = PidfdInfo {
mask: PIDFD_INFO_CGROUPID,
..PidfdInfo::default()
};
let rc = unsafe { libc::ioctl(pidfd.as_raw_fd(), PIDFD_GET_INFO, &mut info as *mut PidfdInfo) };
if rc == 0 {
if info.mask & PIDFD_INFO_CGROUPID == 0 {
return Ok(None);
}
return Ok(Some(info.cgroupid));
}
let e = io::Error::last_os_error();
match e.raw_os_error() {
Some(libc::ENOTTY) | Some(libc::EINVAL) => Ok(None),
_ => Err(e),
}
}
pub(crate) fn proc_names(
leaf_path: &str,
pid: u32,
recorded: impl FnOnce() -> Option<bool>,
other_id: bool,
) -> io::Result<bool> {
#[cfg(test)]
if let Some(errno) = super::fault::proc_hidden_as() {
return Err(io::Error::from_raw_os_error(errno));
}
let proc_dir = match crate::identity::proc_view() {
crate::identity::ProcView::Same(dir) => dir,
crate::identity::ProcView::Diverged => {
return Err(io::Error::other(format!(
"this process's /proc is an outer pid namespace's, so pid {pid}'s cgroup cannot be read"
)));
}
crate::identity::ProcView::Unassessable(why) => {
let kind = why.source.as_ref().map_or(io::ErrorKind::Other, io::Error::kind);
return Err(io::Error::new(
kind,
format!(
"the /proc view could not be established, so pid {pid}'s cgroup cannot be read: {}",
why.reason
),
));
}
};
let text = proc_dir.read_to_string(&format!("{pid}/cgroup"))?;
let path = parse_v2_relative_path(&text)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "no cgroup v2 `0::` line"))?;
names_leaf(path, leaf_path, other_id, recorded)
}
const DELETED: &str = " (deleted)";
pub(crate) fn names_leaf(
path: &str,
leaf_path: &str,
other_id: bool,
recorded: impl FnOnce() -> Option<bool>,
) -> io::Result<bool> {
if is_at_or_under(path, leaf_path) {
return Ok(true);
}
if path.strip_suffix(DELETED) != Some(leaf_path) || other_id {
return Ok(false);
}
recorded().ok_or_else(|| {
io::Error::other(format!(
"{path} is either the removed leaf or a live cgroup of that name, and the task's place was not \
recorded before the leaf's removal"
))
})
}
#[derive(Debug, Clone, Default)]
pub(crate) struct PlacedAtRemoval(Arc<std::sync::Mutex<Option<(u32, bool)>>>);
impl PlacedAtRemoval {
pub(crate) fn record(&self, pid: u32, inside: bool) {
*self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some((pid, inside));
}
pub(crate) fn clear(&self) {
*self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = None;
}
pub(crate) fn of(&self, pid: u32) -> Option<bool> {
match *self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) {
Some((recorded, inside)) if recorded == pid => Some(inside),
_ => None,
}
}
}
#[derive(Debug)]
pub(crate) struct Subtree {
leaf_id: u64,
dir: Option<Arc<OwnedFd>>,
path: Option<String>,
swept: super::Swept,
placed: PlacedAtRemoval,
killed: Arc<AtomicBool>,
}
impl Subtree {
pub(crate) fn new(
leaf_id: u64,
dir: Option<Arc<OwnedFd>>,
path: Option<String>,
swept: super::Swept,
placed: PlacedAtRemoval,
killed: Arc<AtomicBool>,
) -> Subtree {
Subtree {
leaf_id,
dir,
path,
swept,
placed,
killed,
}
}
pub(crate) fn holds(&self, pid: u32, pidfd: Option<BorrowedFd<'_>>) -> io::Result<bool> {
let mut elsewhere = false;
if let Some(pidfd) = pidfd {
if let Some(id) = pidfd_cgroup_id(pidfd)? {
if id == self.leaf_id || self.swept.holds(id) {
return Ok(true);
}
if let Some(under) = self.walk_places(id) {
return Ok(under);
}
elsewhere = true;
}
}
let Some(path) = &self.path else {
return Ok(false);
};
proc_names(path, pid, || self.placed.of(pid), elsewhere).map_err(|e| {
if elsewhere {
io::Error::new(
e.kind(),
format!(
"pid {pid} is in another cgroup than its leaf, maybe one under it, and that cgroup's path \
cannot be read ({e})"
),
)
} else {
e
}
})
}
fn walk_places(&self, id: u64) -> Option<bool> {
let dir = self.dir.as_ref()?;
match super::find_descendant(dir.as_fd(), id) {
super::Walked::Found => Some(true),
super::Walked::Absent => match self.dying() {
Ok(Some(0)) => Some(false),
Ok(dying) => {
log::debug!(
"the cgroups under a leaf were all walked without finding cgroup {id}, but {} are removed \
and not yet freed, so it is not shown to be outside",
dying.map_or("an unknown number".to_owned(), |n| n.to_string())
);
None
}
Err(e) => {
log::debug!(
"the cgroups under a leaf were all walked without finding cgroup {id}, but its cgroup.stat \
cannot be read ({e}), so it is not shown to be outside"
);
None
}
},
super::Walked::Unknown(e) => {
log::debug!("the cgroups under a leaf could not all be walked ({e})");
None
}
}
}
fn dying(&self) -> io::Result<Option<u64>> {
let dir = self
.dir
.as_ref()
.ok_or_else(|| io::Error::other("the subtree holds no leaf directory"))?;
let file = rustix::fs::openat(
dir.as_fd(),
"cgroup.stat",
rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC,
rustix::fs::Mode::empty(),
)?;
let text = io::read_to_string(std::fs::File::from(file))?;
Ok(dying_in(&text))
}
pub(crate) fn reached(&self, pid: u32, pidfd: Option<BorrowedFd<'_>>) -> io::Result<bool> {
if !self.killed.load(Ordering::Relaxed) {
return Ok(false);
}
self.holds(pid, pidfd)
}
}
fn dying_in(text: &str) -> Option<u64> {
text.lines()
.find_map(|line| line.strip_prefix("nr_dying_descendants ")?.trim().parse::<u64>().ok())
}
pub(crate) fn front_placement(leaf_id: impl FnOnce() -> io::Result<u64>) -> Result<(), crate::error::Error> {
use std::os::fd::AsFd;
if let Err(e) = leaf_id() {
return Err(match e.raw_os_error() {
Some(libc::ENOSYS | libc::EOPNOTSUPP | libc::EPERM) => unplaceable(format!(
"this host gives no cgroup's id by name_to_handle_at ({e}), as a kernel without CONFIG_FHANDLE, a \
filesystem without export operations or a seccomp filter refuses it"
)),
_ => crate::error::Error::Io(crate::error::io_context(
"the cgroup id of the elevated front's leaf",
e,
)),
});
}
let own = rustix::process::pidfd_open(rustix::process::getpid(), rustix::process::PidfdFlags::empty())
.map_err(|e| crate::error::Error::Io(crate::error::io_context("pidfd_open of this process", e.into())))?;
match pidfd_cgroup_id(own.as_fd()) {
Ok(Some(_)) => return Ok(()),
Ok(None) => {}
Err(e) => {
return Err(unplaceable(format!(
"PIDFD_GET_INFO failed on this process's own pidfd ({e})"
)))
}
}
untraceable_cgroup_readable().map_err(refusal)
}
fn refusal(why: Unreadable) -> crate::error::Error {
match why {
Unreadable::Hidden(e) => unplaceable(format!(
"this kernel has no PIDFD_GET_INFO (Linux 6.13 or later), and /proc hides the cgroup of a process \
this one may not trace ({e}), as a hidepid /proc does: it would hide the elevated front's too"
)),
Unreadable::View(why) => unplaceable(format!(
"this kernel has no PIDFD_GET_INFO (Linux 6.13 or later), and {why}"
)),
Unreadable::Probe(e) => crate::error::Error::Io(crate::error::io_context(
"probing whether /proc shows the cgroup of a process this one may not trace",
e,
)),
}
}
fn unplaceable(why: String) -> crate::error::Error {
crate::error::Error::Unsupported {
op: "contain() on an elevated command".into(),
platform: "linux",
detail: format!(
"{why}. cosca could not tell whether a cgroup kill reached the elevated front, so the spawn is \
refused before anything is spawned"
),
}
}
#[derive(Debug)]
enum Unreadable {
Hidden(io::Error),
View(String),
Probe(io::Error),
}
struct Probe {
pid: libc::pid_t,
ready_read: std::io::PipeReader,
}
fn fork_probe(parent: libc::pid_t, dumpable: bool) -> io::Result<Probe> {
use std::os::fd::AsRawFd;
let (ready_read, ready_write) = std::io::pipe()?;
let (ready_r, ready_w) = (ready_read.as_raw_fd(), ready_write.as_raw_fd());
let _guard = crate::child::spawn::spawn_lock();
let pid = unsafe { libc::fork() };
if pid == 0 {
unsafe {
libc::close(ready_r);
libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL, 0, 0, 0);
if libc::getppid() != parent {
libc::_exit(0);
}
if !dumpable {
libc::prctl(libc::PR_SET_DUMPABLE, 0, 0, 0, 0);
}
let mut pdeathsig: libc::c_int = 0;
libc::prctl(libc::PR_GET_PDEATHSIG, (&raw mut pdeathsig) as libc::c_ulong, 0, 0, 0);
let report = [libc::prctl(libc::PR_GET_DUMPABLE, 0, 0, 0, 0) as u8, pdeathsig as u8];
libc::write(ready_w, report.as_ptr().cast(), report.len());
loop {
libc::pause();
}
}
}
if pid < 0 {
return Err(io::Error::last_os_error());
}
drop(ready_write);
Ok(Probe { pid, ready_read })
}
fn untraceable_cgroup_readable() -> Result<(), Unreadable> {
let proc_dir = match crate::identity::proc_view() {
crate::identity::ProcView::Same(dir) => dir,
crate::identity::ProcView::Diverged => {
return Err(Unreadable::View(
"this process's /proc is an outer pid namespace's, so no process's cgroup can be read by its pid"
.into(),
))
}
crate::identity::ProcView::Unassessable(why) => {
return Err(Unreadable::View(format!(
"this process's /proc view could not be established ({})",
why.reason
)))
}
};
#[cfg(test)]
let dumpable = super::fault::probe_kept_dumpable();
#[cfg(not(test))]
let dumpable = false;
let Probe { pid, ready_read } =
fork_probe(rustix::process::getpid().as_raw_nonzero().get(), dumpable).map_err(Unreadable::Probe)?;
let mut ready = [0u8; 2];
let readied = loop {
match std::io::Read::read(&mut &ready_read, &mut ready) {
Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
other => break other,
}
};
#[cfg(test)]
let hidden = super::fault::proc_hidden_as();
#[cfg(not(test))]
let hidden: Option<i32> = None;
let read = match readied {
Ok(2) if ready[0] != 0 => Err(Unreadable::Probe(io::Error::other(
"the probe's child could not make itself non-dumpable",
))),
Ok(2) if i32::from(ready[1]) != libc::SIGKILL => Err(Unreadable::Probe(io::Error::other(
"the probe's child could not ask to die with its parent",
))),
Ok(2) => match hidden {
Some(errno) => Err(io::Error::from_raw_os_error(errno)),
None => proc_dir.read_to_string(&format!("{pid}/cgroup")).map(drop),
}
.map_err(|e| match e.raw_os_error() {
Some(libc::ENOENT | libc::EPERM | libc::EACCES) => Unreadable::Hidden(e),
_ => Unreadable::Probe(e),
}),
Ok(_) => Err(Unreadable::Probe(io::Error::other(
"the probe's child died before it was ready",
))),
Err(e) => Err(Unreadable::Probe(e)),
};
unsafe { libc::kill(pid, libc::SIGKILL) };
let mut status = 0;
loop {
if unsafe { libc::waitpid(pid, &mut status, 0) } == pid {
break;
}
let e = io::Error::last_os_error();
if e.raw_os_error() != Some(libc::EINTR) {
return Err(Unreadable::Probe(e));
}
}
read
}
#[cfg(test)]
#[path = "reach_tests.rs"]
mod reach_tests;