use std::collections::HashMap;
use std::ffi::OsString;
use std::os::fd::OwnedFd;
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use rustix::event::{PollFd, PollFlags};
use rustix::fs::{mknodat, open, FileType, Mode, OFlags, CWD};
use crate::elevation::shim::hooks::{Gate, Inject};
use crate::elevation::shim::link::fake_shim::{my_euid, Rig};
use crate::elevation::shim::protocol::ShimArgs;
pub(super) struct Spec {
program: OsString,
args: Vec<OsString>,
gates: Vec<Gate>,
injects: Vec<Inject>,
child_gate: bool,
child_fault: bool,
loop_failure: bool,
exhaust_fds: bool,
cosca_pid: Option<u32>,
cosca_euid: Option<u32>,
search_path: Option<OsString>,
ignore: Vec<libc::c_int>,
cwd: Option<PathBuf>,
flag: Option<OsString>,
dir: Option<PathBuf>,
stdin_held: bool,
without_proc: bool,
shipped: bool,
raw_seams: Vec<(String, OsString)>,
exe: Option<PathBuf>,
}
impl Spec {
pub(super) fn new(program: impl Into<OsString>, args: &[&str]) -> Spec {
Spec {
program: program.into(),
args: args.iter().map(OsString::from).collect(),
gates: vec![],
injects: vec![],
child_gate: false,
child_fault: false,
loop_failure: false,
exhaust_fds: false,
cosca_pid: None,
cosca_euid: None,
search_path: None,
ignore: vec![],
cwd: None,
flag: None,
dir: None,
stdin_held: false,
without_proc: false,
shipped: false,
raw_seams: vec![],
exe: None,
}
}
pub(super) fn sh(script: &str) -> Spec {
Spec::new("/bin/sh", &["-c", script])
}
pub(super) fn gate(mut self, gate: Gate) -> Spec {
self.gates.push(gate);
self
}
pub(super) fn inject(mut self, what: Inject) -> Spec {
self.injects.push(what);
self
}
pub(super) fn child_gate(mut self) -> Spec {
self.child_gate = true;
self
}
pub(super) fn child_fault(mut self) -> Spec {
self.child_fault = true;
self
}
pub(super) fn loop_failure(mut self) -> Spec {
self.loop_failure = true;
self
}
pub(super) fn exhaust_fds(mut self) -> Spec {
self.exhaust_fds = true;
self
}
pub(super) fn cosca_pid(mut self, pid: u32) -> Spec {
self.cosca_pid = Some(pid);
self
}
pub(super) fn cosca_euid(mut self, euid: u32) -> Spec {
self.cosca_euid = Some(euid);
self
}
pub(super) fn search_path(mut self, path: impl Into<OsString>) -> Spec {
self.search_path = Some(path.into());
self
}
pub(super) fn ignoring(mut self, signal: libc::c_int) -> Spec {
self.ignore.push(signal);
self
}
pub(super) fn stdin_held(mut self) -> Spec {
self.stdin_held = true;
self
}
pub(super) fn shipped(mut self) -> Spec {
self.shipped = true;
self
}
pub(super) fn raw_seam(mut self, name: &str, value: impl Into<OsString>) -> Spec {
self.raw_seams.push((name.to_owned(), value.into()));
self
}
pub(super) fn exe(mut self, exe: &Path) -> Spec {
self.exe = Some(exe.to_owned());
self
}
pub(super) fn flag(mut self, flag: &str) -> Spec {
self.flag = Some(flag.into());
self
}
pub(super) fn dir(mut self, dir: &Path) -> Spec {
self.dir = Some(dir.to_owned());
self
}
pub(super) fn without_proc(mut self) -> Spec {
self.without_proc = true;
self
}
pub(super) fn cwd(mut self, dir: &Path) -> Spec {
self.cwd = Some(dir.to_owned());
self
}
}
pub(super) struct ShimRig {
pub(super) link: Rig,
work: std::sync::Arc<tempfile::TempDir>,
}
impl ShimRig {
pub(super) fn new() -> ShimRig {
ShimRig {
link: Rig::new(),
work: std::sync::Arc::new(tempfile::tempdir().expect("a work directory")),
}
}
fn fifo(&self, name: &str) -> PathBuf {
let path = self.work.path().join(name);
mknodat(CWD, &path, FileType::Fifo, Mode::from_raw_mode(0o600), 0).expect("a FIFO");
path
}
pub(super) fn spawn(&self, spec: Spec) -> Run {
let work_log = self.fifo("log");
let log_fd = open(
&work_log,
OFlags::RDONLY | OFlags::NONBLOCK | OFlags::CLOEXEC,
Mode::empty(),
)
.expect("the log's read end");
let args = ShimArgs {
dir: spec.dir.clone().unwrap_or_else(|| self.link.link.dir().to_owned()),
cosca_pid: spec.cosca_pid.unwrap_or_else(std::process::id),
cosca_identity: None,
cosca_euid: spec.cosca_euid.unwrap_or_else(my_euid),
search_path: spec.search_path.clone(),
program: spec.program.clone(),
args: spec.args.clone(),
};
let exe = spec
.exe
.clone()
.unwrap_or_else(|| std::env::current_exe().expect("the test binary's path"));
let mut argv = args.to_argv(exe.as_os_str(), false);
if let Some(flag) = &spec.flag {
argv[1] = flag.clone();
}
let mut command = Command::new(&exe);
command
.arg0(&argv[0])
.args(&argv[1..])
.env_clear()
.env("PATH", "/usr/local/bin:/usr/bin:/bin")
.env("COSCA_SEAM_LOG", &work_log)
.stdin(if spec.stdin_held { Stdio::piped() } else { Stdio::null() })
.stdout(Stdio::piped())
.stderr(Stdio::piped());
let mut gates = HashMap::new();
for gate in &spec.gates {
let fifo = self.fifo(&format!("gate-{}", gate.name()));
command.env(
format!("COSCA_SEAM_GATE_{}", gate.name().to_uppercase().replace('-', "_")),
&fifo,
);
gates.insert(*gate, hold_open(&fifo));
}
if !spec.injects.is_empty() {
let names: Vec<&str> = spec.injects.iter().map(|i| i.name()).collect();
command.env("COSCA_SEAM_INJECT", names.join(","));
}
let child_gate = spec.child_gate.then(|| self.fifo("child-gate"));
if let Some(path) = &child_gate {
command.env("COSCA_SEAM_CHILD_GATE", path);
}
let child_gate = child_gate.map(|path| hold_open(&path));
if spec.child_fault {
command.env("COSCA_SEAM_CHILD_FAULT", "1");
}
let loop_failure = spec.loop_failure.then(|| self.fifo("loop-failure"));
if let Some(path) = &loop_failure {
command.env("COSCA_SEAM_LOOP_FAILURE", path);
}
if spec.exhaust_fds {
command.env("COSCA_SEAM_EXHAUST_FDS", "1");
}
if spec.shipped {
command.env("COSCA_TESTCFG_SHIM_SHIPPED", "1");
}
for (name, value) in &spec.raw_seams {
command.env(name, value);
}
if let Some(cwd) = &spec.cwd {
command.current_dir(cwd);
}
let ignore = spec.ignore.clone();
let without_proc = spec.without_proc;
unsafe {
command.pre_exec(move || {
let no_core = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
libc::setrlimit(libc::RLIMIT_CORE, &no_core);
for signal in &ignore {
libc::signal(*signal, libc::SIG_IGN);
}
if without_proc
&& (libc::unshare(libc::CLONE_NEWNS) != 0
|| libc::mount(
std::ptr::null(),
c"/".as_ptr(),
std::ptr::null(),
libc::MS_REC | libc::MS_PRIVATE,
std::ptr::null(),
) != 0
|| libc::umount2(c"/proc".as_ptr(), libc::MNT_DETACH) != 0)
{
return Err(std::io::Error::last_os_error());
}
Ok(())
});
}
let mut child = crate::test_spawn::spawn(&mut command).expect("the shim starts");
let stdin = child.stdin.take();
let pidfd = rustix::process::pidfd_open(
rustix::process::Pid::from_raw(child.id() as i32).expect("a pid"),
rustix::process::PidfdFlags::empty(),
)
.expect("a pidfd on the shim");
Run {
child: Some(child),
stdin,
pidfd,
log: LogTail {
fifo: log_fd,
pending: vec![],
lines: vec![],
eof: false,
},
gates,
child_gate,
_work: self.work.clone(),
loop_failure,
}
}
}
impl ShimRig {
pub(super) fn run_to_end(&self, spec: Spec) -> Finished {
self.spawn(spec).finish()
}
}
struct LogTail {
fifo: OwnedFd,
pending: Vec<u8>,
lines: Vec<String>,
eof: bool,
}
impl LogTail {
fn drain(&mut self) {
let mut buf = [0u8; 4096];
loop {
let mut fds = [PollFd::new(&self.fifo, PollFlags::IN)];
if !crate::wait::any_ready_now(&mut fds).expect("poll the log") {
return;
}
match rustix::io::read(&self.fifo, &mut buf) {
Ok(0) => {
self.eof = true;
return;
}
Ok(n) => {
self.pending.extend_from_slice(&buf[..n]);
while let Some(at) = self.pending.iter().position(|&b| b == b'\n') {
let line: Vec<u8> = self.pending.drain(..=at).collect();
self.lines
.push(String::from_utf8_lossy(&line[..line.len() - 1]).into_owned());
}
}
Err(rustix::io::Errno::INTR | rustix::io::Errno::AGAIN) => {}
Err(e) => panic!("read of the log: {e}"),
}
}
}
}
pub(super) struct Run {
child: Option<Child>,
stdin: Option<std::process::ChildStdin>,
pidfd: rustix::fd::OwnedFd,
log: LogTail,
gates: HashMap<Gate, OwnedFd>,
child_gate: Option<OwnedFd>,
_work: std::sync::Arc<tempfile::TempDir>,
loop_failure: Option<PathBuf>,
}
#[derive(Debug)]
pub(super) enum Gone {
LogEnded,
ShimExited,
}
impl Run {
pub(super) fn pid(&self) -> u32 {
self.child.as_ref().expect("not yet waited").id()
}
pub(super) fn wait_count(&mut self, needle: &str, n: usize) {
let count = |lines: &[String]| lines.iter().filter(|l| l.contains(needle)).count();
loop {
self.log.drain();
if count(&self.log.lines) >= n {
return;
}
let found = self.wait_line_after(self.log.lines.len(), |l| l.contains(needle));
let ended = if found { String::new() } else { self.how_it_ended() };
assert!(
found,
"the shim's log ended before {n} lines with {needle:?}: {:#?}\n{ended}",
self.log.lines
);
}
}
fn wait_line_after(&mut self, from: usize, wanted: impl Fn(&str) -> bool) -> bool {
loop {
self.log.drain();
if self.log.lines.iter().skip(from).any(|l| wanted(l)) {
return true;
}
if self.log.eof {
return false;
}
let known = self.log.lines.len();
let mut fds = [
PollFd::new(&self.log.fifo, PollFlags::IN),
PollFd::new(&self.pidfd, PollFlags::IN),
];
crate::wait::poll_ready(&mut fds).expect("poll the log and the shim");
if !fds[1].revents().is_empty() && fds[0].revents().is_empty() {
self.log.drain();
if self.log.lines.len() == known {
return false;
}
}
}
}
pub(super) fn program_pid(&mut self) -> i32 {
let line = self.wait_for("forked child pid=");
let digits: String = line["forked child pid=".len()..]
.chars()
.take_while(char::is_ascii_digit)
.collect();
digits.parse().expect("a pid")
}
pub(super) fn program_pidfd(&mut self) -> rustix::fd::OwnedFd {
let pid = self.program_pid();
rustix::process::pidfd_open(
rustix::process::Pid::from_raw(pid).expect("a pid"),
rustix::process::PidfdFlags::empty(),
)
.expect("the program is unreaped")
}
pub(super) fn holds_the_release_pipe(&mut self) -> bool {
let (shim, child) = (self.pid(), self.program_pid() as u32);
let program_pidfd = self.program_pidfd();
let exited = |pidfd: &OwnedFd| {
let mut fds = [PollFd::new(pidfd, PollFlags::IN)];
crate::wait::any_ready_now(&mut fds).expect("poll the program's pidfd")
};
let pipefs = {
let (r, _w) = std::io::pipe().expect("a pipe");
rustix::fs::fstat(&r).expect("fstat").st_dev
};
let ends = |pid: u32, pidfd: &OwnedFd| -> Vec<(u64, bool)> {
let table = std::fs::read_dir(format!("/proc/{pid}/fd"))
.unwrap_or_else(|e| panic!("process {pid} exited during the scan: {e}"));
let mut ends = vec![];
for entry in table {
let entry = entry.unwrap_or_else(|e| panic!("process {pid} exited during the scan: {e}"));
let name = entry.file_name().to_string_lossy().into_owned();
let fd: i32 = name.parse().expect("an fd number");
let copy = match rustix::process::pidfd_getfd(pidfd, fd, rustix::process::PidfdGetfdFlags::empty()) {
Ok(copy) => copy,
Err(rustix::io::Errno::BADF) => continue,
Err(e) => panic!("pidfd_getfd of fd {fd} of process {pid}: {e}"),
};
let stat = rustix::fs::fstat(©).expect("fstat of a copy");
if FileType::from_raw_mode(stat.st_mode) != FileType::Fifo || stat.st_dev != pipefs {
continue;
}
let flags = rustix::fs::fcntl_getfl(©).expect("fcntl F_GETFL of a copy");
ends.push((stat.st_ino, flags & OFlags::ACCMODE == OFlags::WRONLY));
}
ends
};
let (shim_ends, child_ends) = (ends(shim, &self.pidfd), ends(child, &program_pidfd));
assert!(
!exited(&self.pidfd),
"the shim exited during the scan: {}",
self.how_it_ended()
);
assert!(!exited(&program_pidfd), "the program exited during the scan");
child_ends
.iter()
.filter(|(pipe, writable)| !writable && !shim_ends.iter().any(|(p, w)| p == pipe && !w))
.any(|(pipe, _)| shim_ends.iter().any(|(p, w)| p == pipe && *w))
}
pub(super) fn signal_mask(&self, field: &str) -> u64 {
let status = std::fs::read_to_string(format!("/proc/{}/status", self.pid())).expect("the shim's status");
let value = status
.lines()
.find_map(|l| l.strip_prefix(field).and_then(|r| r.strip_prefix(':')))
.expect("the field");
u64::from_str_radix(value.trim(), 16).expect("a hex mask")
}
fn how_it_ended(&mut self) -> String {
let mut fds = [PollFd::new(&self.pidfd, PollFlags::IN)];
if !crate::wait::any_ready_now(&mut fds).expect("poll the shim's pidfd") {
return "the shim is still running".into();
}
let Some(child) = self.child.as_mut() else {
return "the shim was collected".into();
};
let status = child
.wait()
.map_or_else(|e| format!("wait failed: {e}"), |s| format!("{s:?}"));
let mut stderr = String::new();
if let Some(pipe) = child.stderr.as_mut() {
if let Ok(flags) = rustix::fs::fcntl_getfl(&*pipe) {
_ = rustix::fs::fcntl_setfl(&*pipe, flags | OFlags::NONBLOCK);
}
_ = std::io::Read::read_to_string(pipe, &mut stderr);
}
format!("the shim ended: {status}; its stderr: {stderr:?}")
}
pub(super) fn take_stdout(&mut self) -> std::process::ChildStdout {
self.child
.as_mut()
.expect("not yet waited")
.stdout
.take()
.expect("not yet taken")
}
pub(super) fn take_stderr(&mut self) -> std::process::ChildStderr {
self.child
.as_mut()
.expect("not yet waited")
.stderr
.take()
.expect("not yet taken")
}
pub(super) fn lines(&mut self) -> &[String] {
self.log.drain();
&self.log.lines
}
pub(super) fn wait_line(&mut self, wanted: impl Fn(&str) -> bool) -> Result<String, Gone> {
loop {
self.log.drain();
if let Some(line) = self.log.lines.iter().find(|l| wanted(l)) {
return Ok(line.clone());
}
if self.log.eof {
return Err(Gone::LogEnded);
}
let known = self.log.lines.len();
let mut fds = [
PollFd::new(&self.log.fifo, PollFlags::IN),
PollFd::new(&self.pidfd, PollFlags::IN),
];
crate::wait::poll_ready(&mut fds).expect("poll the log and the shim");
let (log_ready, shim_exited) = (!fds[0].revents().is_empty(), !fds[1].revents().is_empty());
if shim_exited && !log_ready {
self.log.drain();
if self.log.lines.len() == known {
return Err(Gone::ShimExited);
}
}
}
}
pub(super) fn wait_for(&mut self, needle: &str) -> String {
match self.wait_line(|l| l.contains(needle)) {
Ok(line) => line,
Err(gone) => {
let ended = self.how_it_ended();
panic!(
"the shim's log ended ({gone:?}) without {needle:?}; it has {:#?}\n{ended}",
self.log.lines
)
}
}
}
pub(super) fn wait_for_in_log(&mut self, needle: &str) -> String {
loop {
self.log.drain();
if let Some(line) = self.log.lines.iter().find(|l| l.contains(needle)) {
return line.clone();
}
let ended = if self.log.eof {
self.how_it_ended()
} else {
String::new()
};
assert!(
!self.log.eof,
"the log ended without {needle:?}: {:#?}\n{ended}",
self.log.lines
);
let mut fds = [PollFd::new(&self.log.fifo, PollFlags::IN)];
crate::wait::poll_ready(&mut fds).expect("poll the log");
}
}
pub(super) fn release(&mut self, gate: Gate) {
self.wait_for(&format!("gate: waiting at {}", gate.name()));
let holder = self.gates.get(&gate).expect("the gate was requested");
release_gate(holder);
}
pub(super) fn release_child_if_waiting(&mut self) {
release_gate(self.child_gate.as_ref().expect("the child gate was requested"));
}
pub(super) fn release_child(&mut self) {
self.wait_for_in_log("child: waiting at gate");
release_gate(self.child_gate.as_ref().expect("the child gate was requested"));
}
pub(super) fn fail_loop(&mut self) {
let fifo = self.loop_failure.clone().expect("the loop failure was requested");
open_for_release(&fifo);
}
pub(super) fn wait_exit(&mut self) -> std::process::ExitStatus {
self.child
.as_mut()
.expect("not yet waited")
.wait()
.expect("the shim's exit")
}
pub(super) fn finish(mut self) -> Finished {
let child = self.child.take().expect("not yet waited");
let output = child.wait_with_output().expect("the shim's exit");
self.log.drain();
Finished {
code: output.status.code(),
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
lines: std::mem::take(&mut self.log.lines),
}
}
pub(super) fn close_stdin(&mut self) {
self.stdin = None;
}
pub(super) fn signal(&self, signal: rustix::process::Signal) {
rustix::process::pidfd_send_signal(&self.pidfd, signal).expect("the shim is alive");
}
}
impl Drop for Run {
fn drop(&mut self) {
if let Some(mut child) = self.child.take() {
_ = child.kill();
_ = child.wait();
}
}
}
pub(super) struct Finished {
pub(super) code: Option<i32>,
pub(super) stderr: String,
pub(super) lines: Vec<String>,
}
impl Finished {
pub(super) fn logged(&self, needle: &str) -> bool {
self.lines.iter().any(|l| l.contains(needle))
}
}
fn hold_open(fifo: &Path) -> OwnedFd {
open(fifo, OFlags::RDWR | OFlags::CLOEXEC, Mode::empty()).expect("the gate's FIFO")
}
fn release_gate(holder: &OwnedFd) {
rustix::io::write(holder, b"g").expect("the gate's FIFO has room");
}
fn open_for_release(fifo: &Path) {
let w = open(fifo, OFlags::WRONLY | OFlags::CLOEXEC, Mode::empty()).expect("the gate's write end");
drop(w);
}
pub(super) struct Owner {
child: Child,
stdin: Option<std::process::ChildStdin>,
out: std::io::BufReader<std::process::ChildStdout>,
pub(super) dir: PathBuf,
pub(super) pid: u32,
_work: tempfile::TempDir,
}
impl Owner {
pub(super) fn start(mode: &str) -> Owner {
Owner::start_with(mode, &[])
}
pub(super) fn start_with(mode: &str, wrapper: &[&str]) -> Owner {
use std::io::BufRead;
let work = tempfile::tempdir().expect("a work directory");
let exe = std::env::current_exe().expect("the test binary's path");
let mut command = match wrapper.split_first() {
Some((first, rest)) => {
let mut c = Command::new(first);
c.args(rest).arg(exe);
c
}
None => Command::new(exe),
};
command
.args(crate::elevation::shim::owner_helper::argv(mode, work.path()))
.env_clear()
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit());
let mut child = crate::test_spawn::spawn(&mut command).expect("the owner starts");
let stdin = child.stdin.take();
let mut out = std::io::BufReader::new(child.stdout.take().expect("piped"));
let (mut dir, mut pid) = (None, None);
loop {
let mut line = String::new();
let n = out.read_line(&mut line).expect("the owner's output");
assert!(n > 0, "the owner ended before it was ready (dir {dir:?}, pid {pid:?})");
let line = line.trim_end();
if let Some(d) = line.strip_prefix("dir=") {
dir = Some(PathBuf::from(d));
} else if let Some(p) = line.strip_prefix("pid=") {
pid = Some(p.parse().expect("a pid"));
} else if line == "ready" {
break;
}
}
Owner {
child,
stdin,
out,
dir: dir.expect("the owner said its directory"),
pid: pid.expect("the owner said its pid"),
_work: work,
}
}
pub(super) fn fork_copy(&mut self) {
use std::io::{BufRead, Write};
let stdin = self.stdin.as_mut().expect("the owner's stdin is open");
writeln!(stdin, "fork").expect("the owner is alive");
loop {
let mut line = String::new();
let n = self.out.read_line(&mut line).expect("the owner's output");
assert!(n > 0, "the owner ended before it forked");
if line.trim_end() == "forked" {
return;
}
}
}
pub(super) fn wait_answered(&mut self) {
use std::io::BufRead;
loop {
let mut line = String::new();
let n = self.out.read_line(&mut line).expect("the owner's output");
assert!(n > 0, "the owner ended before it answered a shim");
if line.trim_end() == "event Answered(Allow)" {
return;
}
}
}
pub(super) fn fork_reader(&mut self) {
use std::io::{BufRead, Write};
let stdin = self.stdin.as_mut().expect("the owner's stdin is open");
writeln!(stdin, "{}", crate::elevation::shim::owner_helper::READ_FRAMES).expect("the owner is alive");
loop {
let mut line = String::new();
let n = self.out.read_line(&mut line).expect("the owner's output");
assert!(n > 0, "the owner ended before it forked");
if line.trim_end() == "forked" {
return;
}
}
}
pub(super) fn frames(&mut self) -> Vec<u8> {
use std::io::BufRead;
loop {
let mut line = String::new();
let n = self.out.read_line(&mut line).expect("the owner's output");
assert!(n > 0, "the reader ended without saying what it read");
if let Some(hex) = line.trim_end().strip_prefix("frames ") {
return (0..hex.len())
.step_by(2)
.map(|i| u8::from_str_radix(&hex[i..i + 2], 16).expect("hex"))
.collect();
}
}
}
pub(super) fn send_and_exit(&mut self, bytes: &str) {
use std::io::Write;
let stdin = self.stdin.as_mut().expect("the owner's stdin is open");
writeln!(stdin, "{}{bytes}", crate::elevation::shim::owner_helper::SEND_EXIT).expect("the owner is alive");
let status = self.child.wait().expect("the owner's exit");
assert!(status.success(), "the owner failed: {status:?}");
}
pub(super) fn kill(&mut self) {
self.child.kill().expect("the owner is alive");
self.child.wait().expect("the owner's exit");
}
pub(super) fn close_stdin(&mut self) {
self.stdin = None;
}
}
impl Drop for Owner {
fn drop(&mut self) {
self.stdin = None;
_ = self.child.kill();
_ = self.child.wait();
}
}