#[cfg(target_os = "linux")]
#[allow(unsafe_code)]
pub fn harden_process_dumpable() -> Result<(), String> {
let result = unsafe { libc::prctl(libc::PR_SET_DUMPABLE, 0, 0, 0, 0) };
if result == 0 {
Ok(())
} else {
Err(format!(
"prctl(PR_SET_DUMPABLE, 0) failed: {}",
std::io::Error::last_os_error()
))
}
}
#[cfg(not(target_os = "linux"))]
pub fn harden_process_dumpable() -> Result<(), String> {
Ok(())
}
#[cfg(all(test, target_os = "linux"))]
mod tests {
use super::*;
#[test]
#[allow(unsafe_code)]
fn harden_process_dumpable_clears_dumpable_flag() {
harden_process_dumpable().expect("prctl(PR_SET_DUMPABLE) should succeed");
let flag = unsafe { libc::prctl(libc::PR_GET_DUMPABLE, 0, 0, 0, 0) };
assert_eq!(flag, 0, "process should report non-dumpable after hardening");
}
}
#[cfg(all(test, not(target_os = "linux")))]
mod non_linux_tests {
use super::*;
#[test]
fn harden_process_dumpable_is_noop_ok() {
assert!(harden_process_dumpable().is_ok());
}
}