pub fn harden_process() {
#[cfg(target_os = "linux")]
linux::harden();
}
#[cfg(target_os = "linux")]
mod linux {
use rustix::process::{DumpableBehavior, Resource, Rlimit, set_dumpable_behavior, setrlimit};
pub fn harden() {
let _ = setrlimit(
Resource::Core,
Rlimit {
current: Some(0),
maximum: Some(0),
},
);
let _ = set_dumpable_behavior(DumpableBehavior::NotDumpable);
}
}
#[cfg(test)]
mod tests {
#[test]
fn harden_process_is_best_effort_and_repeatable() {
super::harden_process();
super::harden_process();
}
#[cfg(target_os = "linux")]
#[test]
fn harden_disables_core_dumps() {
super::harden_process();
let lim = rustix::process::getrlimit(rustix::process::Resource::Core);
assert_eq!(lim.current, Some(0), "core-dump soft limit must be zero");
}
}