1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! This module defines the `harden_process` function which performs all
//! possible hardening steps available for the platform.

#[cfg(windows)]
use crate::error::AllocErr;
use crate::error::{EmptySystemError, SysErr};
use core::fmt;

#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
use crate::internals::prctl;
#[cfg(unix)]
use crate::internals::rlimit;
#[cfg(feature = "std")]
use thiserror::Error;

/// Error hardening process.
#[derive(Debug, Clone)]
pub struct HardenError<E: SysErr>(ImplHardenError<E>);

impl<E: SysErr> From<ImplHardenError<E>> for HardenError<E> {
    fn from(inner: ImplHardenError<E>) -> Self {
        Self(inner)
    }
}

impl<E: SysErr> HardenError<E> {
    #[cfg(any(target_os = "linux", target_os = "freebsd"))]
    fn from_prctl(e: E) -> Self {
        ImplHardenError::PrCtl(e).into()
    }

    #[cfg(target_os = "macos")]
    fn from_ptrace(e: E) -> Self {
        ImplHardenError::Ptrace(e).into()
    }

    #[cfg(unix)]
    fn from_rlimit(e: E) -> Self {
        ImplHardenError::Rlimit(e).into()
    }

    #[cfg(windows)]
    fn from_winapi(e: E) -> Self {
        ImplHardenError::WinAPI(e).into()
    }
}

impl<E: SysErr> fmt::Display for HardenError<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

#[cfg(feature = "std")]
impl<E: SysErr + std::error::Error + 'static> std::error::Error for HardenError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.0.source()
    }
}

/// Error hardening process. Variants of this enum are system specific, so it
/// is not exposed as part of the public API, instead wrapped in the struct
/// [`HardenError`].
#[derive(Debug, Clone)]
#[cfg_attr(feature = "std", derive(Error))]
enum ImplHardenError<E: SysErr> {
    #[cfg(any(target_os = "linux", target_os = "freebsd"))]
    PrCtl(#[cfg_attr(feature = "std", source)] E),
    #[cfg(target_os = "macos")]
    Ptrace(#[cfg_attr(feature = "std", source)] E),
    #[cfg(unix)]
    Rlimit(#[cfg_attr(feature = "std", source)] E),
    #[cfg(windows)]
    WinAPI(#[cfg_attr(feature = "std", source)] E),
}

impl<E: SysErr> fmt::Display for ImplHardenError<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            #[cfg(any(target_os = "linux", target_os = "freebsd"))]
            Self::PrCtl(_) => write!(f, "process hardening error in process control"),
            #[cfg(target_os = "macos")]
            Self::Ptrace(_) => write!(f, "process hardening error in ptrace"),
            #[cfg(unix)]
            Self::Rlimit(_) => write!(f, "process hardening error in resouce limits"),
            #[cfg(windows)]
            Self::WinAPI(_) => write!(f, "process hardening error in winapi"),
        }
    }
}

/// Harden error which does not include the underlying system error, but does
/// contain which hardening step went wrong.
///
/// Available on no-std targets.
pub type SimplHardenError = HardenError<EmptySystemError>;

/// Disable tracing for this process.
///
/// # Errors
/// Returns an error when the system or libc interface returns an error.
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
fn disable_process_tracing<E: SysErr>() -> Result<(), HardenError<E>> {
    prctl::set_process_nontraceable().map_err(HardenError::from_prctl)
}

/// Disable tracing for this process.
///
/// # Errors
/// Returns an error when the system or libc interface returns an error.
#[cfg(target_os = "macos")]
fn disable_process_tracing<E: SysErr>() -> Result<(), HardenError<E>> {
    prctl::set_process_nontraceable().map_err(HardenError::from_ptrace)
}

/// Disable core dumps for this process.
///
/// # Errors
/// Returns an error when the system or libc interface returns an error.
#[cfg(unix)]
fn disable_core_dumps<E: SysErr>() -> Result<(), HardenError<E>> {
    let rlim = rlimit::Rlimit::new(0, 0);
    rlimit::set_coredump_rlimit(&rlim).map_err(HardenError::from_rlimit)
}

/// Limit user access to process by setting a default restrictive DACL for the
/// process.
#[cfg(windows)]
fn windows_set_dacl<E: SysErr + AllocErr>() -> Result<(), HardenError<E>> {
    use windows::Win32::System::Threading::{
        PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SYNCHRONIZE, PROCESS_TERMINATE,
    };

    use crate::win_acl::TokenUser;

    // First obtain the SID of the current user
    let user = TokenUser::process_user().map_err(HardenError::from_winapi)?;
    let sid = user.sid();

    // Now specify the ACL we want to create
    let acl_spec = crate::win_acl::EmptyAcl;
    let access_mask = PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE | PROCESS_SYNCHRONIZE;
    let acl_spec = crate::win_acl::AddAllowAceAcl::new(acl_spec, access_mask, sid);

    // Create ACL and set as process DACL
    let acl = acl_spec.create().map_err(HardenError::from_winapi)?;
    acl.set_process_dacl_protected()
        .map_err(HardenError::from_winapi)
}

/// Performs all possible hardening steps for the platform.
///
/// # Errors
/// Returns an error when one of the available hardening steps error due to a
/// system or libc interface returning an error. In case of error it is
/// recommended to issue an error and shut down the application without loading
/// secrets into memory.
///
/// The system error can be any error implementing the [`SysErr`] trait. See
/// the [`error`](crate::error) module for more information.
#[cfg(unix)]
pub fn harden_process_other_err<E: SysErr>() -> Result<(), HardenError<E>> {
    #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
    disable_process_tracing()?;
    disable_core_dumps()?;
    Ok(())
}

/// Performs all possible hardening steps for the platform.
///
/// This is not implemented yet for windows.
///
/// # Errors
/// Returns an error when one of the available hardening steps error due to a
/// system or libc interface returning an error. In case of error it is
/// recommended to issue an error and shut down the application without loading
/// secrets into memory.
///
/// The system error can be any error implementing the [`SysErr`] trait. See
/// the [`error`](crate::error) module for more information.
#[cfg(windows)]
pub fn harden_process_other_err<E: SysErr + AllocErr>() -> Result<(), HardenError<E>> {
    windows_set_dacl()
}

/// Performs all possible hardening steps for the platform.
///
/// # Errors
/// Returns an error when one of the available hardening steps error due to a
/// system or libc interface returning an error. In case of error it is
/// recommended to issue an error and shut down the application without loading
/// secrets into memory.
///
/// The error doesn't contain the underlying system error but this function is
/// available on no-std targets.
pub fn harden_process() -> Result<(), SimplHardenError> {
    harden_process_other_err()
}

/// Performs all possible hardening steps for the platform.
///
/// # Errors
/// Returns an error when one of the available hardening steps error due to a
/// system or libc interface returning an error. In case of error it is
/// recommended to issue an error and shut down the application without loading
/// secrets into memory.
///
/// The error contains the underlying system error but this function is
/// available only when the `std` feature is enabled.
#[cfg(feature = "std")]
pub fn harden_process_std_err() -> Result<(), HardenError<crate::error::StdSystemError>> {
    harden_process_other_err()
}

#[cfg(test)]
mod tests {
    use super::harden_process;

    #[test]
    fn test_harden_process() {
        assert!(harden_process().is_ok());
    }

    #[test]
    #[cfg(feature = "std")]
    fn comptest_hardenerror_impl_error() {
        fn take_error<E: std::error::Error>(_e: E) {}

        let _ = harden_process().map_err(|e| take_error(e));
    }
}