pub fn harden_process() -> bool {
set_non_dumpable()
}
#[cfg(target_os = "linux")]
fn set_non_dumpable() -> bool {
let rc = unsafe { libc::prctl(libc::PR_SET_DUMPABLE, 0, 0, 0, 0) };
if rc == 0 {
tracing::info!("PR_SET_DUMPABLE=0 applied: core dumps and ptrace disabled for this process");
true
} else {
tracing::warn!("PR_SET_DUMPABLE=0 failed; core dumps may still be possible");
false
}
}
#[cfg(not(target_os = "linux"))]
fn set_non_dumpable() -> bool {
tracing::debug!("PR_SET_DUMPABLE not available on this platform; skipping non-dumpable hardening");
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn harden_process_does_not_panic_and_reports_outcome() {
let applied = harden_process();
#[cfg(target_os = "linux")]
{
assert!(applied, "PR_SET_DUMPABLE should succeed on Linux");
let status = std::fs::read_to_string("/proc/self/status").unwrap_or_default();
let dumpable_line = status.lines().find(|l| l.starts_with("Dumpable:"));
if let Some(line) = dumpable_line {
assert!(
line.contains('0'),
"/proc/self/status should report non-dumpable, got: {line}"
);
}
}
#[cfg(not(target_os = "linux"))]
{
assert!(!applied, "hardening is a no-op off Linux");
}
}
}