use std::io;
#[derive(Debug, Clone, Copy)]
pub struct Probe {
seccomp_filter: bool,
}
impl Probe {
pub fn system() -> Self {
let seccomp_filter = match std::fs::read_to_string("/proc/self/status") {
Ok(status) => seccomp_filtered(&status),
Err(_) => true,
};
Self { seccomp_filter }
}
}
fn seccomp_filtered(status: &str) -> bool {
status
.lines()
.filter_map(|line| line.strip_prefix("Seccomp:"))
.any(|mode| mode.trim() == "2")
}
pub(super) fn refused_by_sandbox(_: &io::Error, probe: Probe) -> bool {
probe.seccomp_filter
}
#[cfg(test)]
pub(super) fn unconfined() -> Probe {
Probe {
seccomp_filter: false,
}
}
#[cfg(test)]
pub(super) fn run_sandbox() -> (io::Error, Probe) {
(
io::Error::from_raw_os_error(13),
Probe {
seccomp_filter: true,
},
)
}
#[cfg(test)]
mod tests {
use super::seccomp_filtered;
#[test]
fn only_filter_mode_is_a_filter() {
assert!(seccomp_filtered(
"Name:\tsh\nSeccomp:\t2\nSeccomp_filters:\t2\n"
));
assert!(!seccomp_filtered("Name:\tsh\nSeccomp:\t0\n"));
assert!(!seccomp_filtered("Seccomp:\t1\n"));
assert!(!seccomp_filtered("Name:\tsh\n"));
}
}