pub(crate) mod setuid;
#[cfg(test)]
#[path = "test_privilege/setuid_helper_tests.rs"]
mod setuid_helper_tests;
#[cfg(test)]
#[path = "test_privilege/setuid_lane_check_tests.rs"]
mod setuid_lane_check_tests;
#[cfg(test)]
#[path = "test_privilege/setuid_protocol_tests.rs"]
mod setuid_protocol_tests;
pub(crate) fn setuid_blocker_command() -> crate::Command {
let mut cmd = crate::test_child::command(crate::test_child::Console::Hidden);
cmd.executable(setuid::setuid_helper())
.args(["cosca_testbin", "setuid-stdin-block", "root"]);
cmd
}
#[cfg(target_os = "linux")]
pub(crate) fn setuid_blocker_for_a_failing_spawn() -> (crate::Command, crate::PipeWriter, impl FnOnce() + 'static) {
use std::io::Read as _;
let (stdin, writer) = crate::test_child::held_writer_stdin();
let (mut ready, ready_end) = std::io::pipe().expect("pipe");
let mut cmd = setuid_blocker_command();
cmd.stdin(stdin).expect("set stdin");
cmd.stdout(crate::Stdio::from_file(std::fs::File::from(
std::os::fd::OwnedFd::from(ready_end),
)))
.expect("set stdout");
cmd.stderr(crate::Stdio::null()).expect("null stderr");
let until_root = move || {
let mut byte = [0u8; 1];
ready.read_exact(&mut byte).expect("the helper reports ready");
assert_eq!(byte, *b"+");
};
(cmd, writer, until_root)
}
pub(crate) fn spawn_setuid_blocker() -> (crate::Child, crate::PipeWriter) {
use std::io::Read as _;
let mut cmd = setuid_blocker_command();
cmd.stdin(crate::Stdio::pipe()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe()).expect("stdout pipe");
cmd.stderr(crate::Stdio::null()).expect("stderr null");
let mut child = cmd.spawn().expect("spawn the setuid helper");
let stdin = child.stdin().expect("piped stdin");
let mut ready = [0u8; 1];
child
.stdout()
.expect("piped stdout")
.read_exact(&mut ready)
.expect("the helper reports ready");
assert_eq!(ready, *b"+");
(child, stdin)
}
#[cfg(unix)]
pub(crate) fn root_over_setuid_blockers(
count: usize,
configure: impl FnOnce(&mut crate::Command),
) -> (crate::Child, crate::PipeWriter) {
use std::io::Read as _;
let job = r#""$0" setuid-stdin-block root <&3 3<&- &"#;
let script = format!("exec 3<&0; {} wait", vec![job; count].join(" "));
let mut cmd = crate::test_child::command(crate::test_child::Console::Hidden);
cmd.args(["sh", "-c", &script]).arg(setuid::setuid_helper());
cmd.stdin(crate::Stdio::pipe()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe()).expect("stdout pipe");
cmd.stderr(crate::Stdio::null()).expect("stderr null");
configure(&mut cmd);
let mut child = cmd.spawn().expect("spawn the root");
let stdin = child.stdin().expect("piped stdin");
let mut ready = vec![0u8; count];
child
.stdout()
.expect("piped stdout")
.read_exact(&mut ready)
.expect("every helper reports ready");
assert!(ready.iter().all(|byte| *byte == b'+'), "{ready:?}");
(child, stdin)
}
pub(crate) fn assert_holds_dac_bypass() {
assert!(
holds_dac_bypass(),
"the root tests need a DAC bypass (CAP_DAC_OVERRIDE or CAP_DAC_READ_SEARCH effective on Linux, uid 0 elsewhere): \
run the suite as root or with that capability in a sandbox, or set COSCA_TEST_ROOT=0 to opt out"
);
}
fn holds_dac_bypass() -> bool {
#[cfg(target_os = "linux")]
{
rustix::thread::capabilities(None).is_ok_and(|sets| bypasses_dac(sets.effective))
}
#[cfg(not(target_os = "linux"))]
{
unsafe { libc::geteuid() == 0 }
}
}
#[cfg(target_os = "linux")]
fn bypasses_dac(effective: rustix::thread::CapabilitySet) -> bool {
use rustix::thread::CapabilitySet;
effective.intersects(CapabilitySet::DAC_OVERRIDE | CapabilitySet::DAC_READ_SEARCH)
}
pub(crate) fn drop_dac_bypass_before_exec(cmd: &mut std::process::Command) {
use std::os::unix::process::CommandExt as _;
unsafe {
cmd.pre_exec(drop_dac_bypass);
}
}
#[cfg(target_os = "linux")]
fn drop_dac_bypass() -> std::io::Result<()> {
use rustix::thread::CapabilitySet;
let dac = CapabilitySet::DAC_OVERRIDE | CapabilitySet::DAC_READ_SEARCH;
let mut sets = rustix::thread::capabilities(None)?;
sets.effective.remove(dac);
sets.permitted.remove(dac);
sets.inheritable.remove(dac);
rustix::thread::set_capabilities(None, sets)?;
rustix::thread::set_no_new_privs(true)?;
let sets = rustix::thread::capabilities(None)?;
debug_assert!(
!sets.effective.intersects(dac) && !sets.permitted.intersects(dac) && !sets.inheritable.intersects(dac),
"a DAC capability survived the drop"
);
debug_assert!(rustix::thread::no_new_privs()?, "no_new_privs did not take");
Ok(())
}
#[cfg(not(target_os = "linux"))]
pub(crate) const UNPRIVILEGED: libc::uid_t = 65534;
#[cfg(not(target_os = "linux"))]
fn drop_dac_bypass() -> std::io::Result<()> {
unsafe {
if libc::geteuid() != 0 {
return Ok(());
}
if libc::setgroups(0, std::ptr::null()) != 0
|| libc::setgid(UNPRIVILEGED) != 0
|| libc::setuid(UNPRIVILEGED) != 0
{
return Err(std::io::Error::last_os_error());
}
}
Ok(())
}
#[cfg(test)]
#[path = "test_privilege_tests.rs"]
mod test_privilege_tests;