1
//! Hardening the current process.
2
//!
3
//! Some operating systems give processes the ability to prevent other
4
//! (low-privilege) processes from inspecting their state.
5
//! When possible, we usually want to opt in to that.
6

            
7
use std::sync::{
8
    Arc,
9
    atomic::{AtomicBool, Ordering},
10
};
11

            
12
/// Enable process hardening, to make it harder for low-privilege users to
13
/// extract information from Arti.
14
///
15
/// This function only has effect the first time it is called.  If it returns an
16
/// error, the caller should probably exit the process.
17
///
18
/// # Limitations
19
///
20
/// See notes from the [`secmem_proc`] crate: this is a best-effort defense, and
21
/// only makes these attacks _harder_.  It can interfere with debugging.
22
276
pub fn enable_process_hardening() -> Result<(), Error> {
23
    /// Have we called this method before?
24
    static ENABLED: AtomicBool = AtomicBool::new(false);
25

            
26
276
    if ENABLED.swap(true, Ordering::SeqCst) {
27
        // Already enabled, or already tried to enable.
28

            
29
        // TODO: We should fail if any previous attempt failed!
30
        return Ok(());
31
276
    }
32

            
33
276
    secmem_proc::harden_process().map_err(|e| Error::Failed(Arc::new(e)))?;
34

            
35
276
    Ok(())
36
276
}
37

            
38
/// An error returned from calling enable_process_hardening.
39
#[derive(Clone, Debug, thiserror::Error)]
40
#[non_exhaustive]
41
pub enum Error {
42
    /// `secmem_proc` was unable to turn on process hardening.
43
    #[error("Could not enable process hardening")]
44
    Failed(#[source] Arc<secmem_proc::error::Error>),
45
}