pub trait ProcessProtection {
fn lock_memory(&self) -> Result<(), ProtectionError>;
fn disable_core_dump(&self) -> Result<(), ProtectionError>;
fn disable_ptrace(&self) -> Result<(), ProtectionError>;
fn apply_all(&self) -> Result<(), ProtectionError> {
self.lock_memory()?;
self.disable_core_dump()?;
self.disable_ptrace()?;
Ok(())
}
}
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum ProtectionError {
#[error("platform unsupported: {0}")]
Unsupported(&'static str),
#[error("syscall failed: {op} (code {code})")]
SyscallFailed {
op: &'static str,
code: i32,
},
#[error("debugger attached: {0}")]
DebuggerAttached(&'static str),
}
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
pub use linux::LinuxProcessProtection as ActiveImpl;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::MacosProcessProtection as ActiveImpl;
#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
pub use windows::WindowsProcessProtection as ActiveImpl;
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
mod fallback;
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
pub use fallback::FallbackProcessProtection as ActiveImpl;
#[cfg(test)]
mod tests {
use super::*;
struct Noop;
impl ProcessProtection for Noop {
fn lock_memory(&self) -> Result<(), ProtectionError> {
Ok(())
}
fn disable_core_dump(&self) -> Result<(), ProtectionError> {
Ok(())
}
fn disable_ptrace(&self) -> Result<(), ProtectionError> {
Ok(())
}
}
#[test]
fn apply_all_composes_three_steps() {
let noop = Noop;
assert!(noop.apply_all().is_ok());
}
}