#[cfg(target_os = "linux")]
pub fn mlock(ptr: *const u8, len: usize) -> Result<(), String> {
let ret = unsafe { libc::mlock(ptr as *const libc::c_void, len) };
if ret == 0 {
Ok(())
} else {
Err(format!(
"mlock failed (errno {}): secrets may be swappable",
std::io::Error::last_os_error()
))
}
}
#[cfg(not(target_os = "linux"))]
pub fn mlock(_ptr: *const u8, _len: usize) -> Result<(), String> {
Ok(())
}
#[cfg(target_os = "linux")]
pub fn madvise_dontdump(ptr: *const u8, len: usize) -> Result<(), String> {
let ret = unsafe { libc::madvise(ptr as *mut libc::c_void, len, libc::MADV_DONTDUMP) };
if ret == 0 {
Ok(())
} else {
Err("madvise(DONTDUMP) failed: secrets may appear in core dumps".to_string())
}
}
#[cfg(not(target_os = "linux"))]
pub fn madvise_dontdump(_ptr: *const u8, _len: usize) -> Result<(), String> {
Ok(())
}
#[cfg(target_os = "linux")]
pub fn munlock(ptr: *const u8, len: usize) {
unsafe {
libc::munlock(ptr as *const libc::c_void, len);
}
}
#[cfg(not(target_os = "linux"))]
pub fn munlock(_ptr: *const u8, _len: usize) {}