pub fn apply_process_hardening() {
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
{
apply_linux_hardening();
}
#[cfg(not(all(target_os = "linux", not(target_env = "ohos"))))]
{
tracing::debug!("Process hardening skipped: not on Linux");
}
}
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
fn apply_linux_hardening() {
let result = unsafe { libc::prctl(libc::PR_SET_DUMPABLE, 0i64, 0i64, 0i64, 0i64) };
if result != 0 {
let err = std::io::Error::last_os_error();
tracing::warn!(
"PR_SET_DUMPABLE failed ({}); continuing without this hardening",
err
);
} else {
tracing::debug!("PR_SET_DUMPABLE=0 applied");
}
let result = unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1i64, 0i64, 0i64, 0i64) };
if result != 0 {
let err = std::io::Error::last_os_error();
tracing::warn!(
"PR_SET_NO_NEW_PRIVS failed ({}); continuing without this hardening",
err
);
} else {
tracing::debug!("PR_SET_NO_NEW_PRIVS=1 applied");
}
let rlim_core = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
let result = unsafe { libc::setrlimit(libc::RLIMIT_CORE, &raw const rlim_core) };
if result != 0 {
let err = std::io::Error::last_os_error();
tracing::warn!(
"RLIMIT_CORE failed ({}); continuing without this hardening",
err
);
} else {
tracing::debug!("RLIMIT_CORE=0 applied");
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_apply_process_hardening_does_not_panic() {
apply_process_hardening();
}
}