secmem_proc/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(future_incompatible, rust_2018_compatibility, unsafe_op_in_unsafe_fn)]
3#![deny(rust_2018_idioms)]
4#![warn(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
5#![warn(clippy::must_use_candidate)]
6#![allow(clippy::needless_lifetimes)]
7//! `secmem-proc` is a crate designed to harden a process against
8//! *low-privileged* attackers running on the same system trying to obtain
9//! secret memory contents of the current process. More specifically, the crate
10//! disables core dumps, makes a best effort to disable the ability to trace it,
11//! and makes a minimal effort to detect already attached tracers.
12//!
13//! __Note__: all the crate does is *hardening*, i.e. it tries to make attacks
14//! *harder*. It can by no means promise any security! In particular, when an
15//! attacker ptrace attaches to the process before `harden_process` is
16//! executed, it is game over for the process. This crate is no substitute for
17//! properly hardening your OS (configuration)!
18//!
19//! Note that hardening the process also severely limits the ability to debug
20//! it. Therefore you are advised to only harden release builds, not debug
21//! builds.
22//!
23//! # Windows
24//! On Windows, [`harden_process`] sets a severly restricted DACL for the
25//! process. (More precisely, only the `PROCESS_QUERY_LIMITED_INFORMATION`,
26//! `PROCESS_TERMINATE` and `SYNCHRONIZE` permissions are enabled.) This could
27//! be too restrictive for the application to function correctly. When more
28//! permissions are required, the safe API in the [`win_acl`] module can be used
29//! to create and set a custom DACL instead.
30//!
31//! On windows, this crate depends on `std` via a dependency on the [`windows`
32//! crate].
33//!
34//! # Examples
35//! In the below example the main function of some application calls the main
36//! hardening function provided by this crate: `harden_process`. This will
37//! perform all available hardening steps (except unstable ones) on the target
38//! platform. When one of the hardening steps fails or a debugger is detected,
39//! the function returns an error. It is advised to terminate the application on
40//! any error.
41//!
42//! ```
43//! fn main() {
44//! // call `secmem_proc::harden_process` before doing anything else, to harden the process
45//! // against low-privileged attackers trying to obtain secret parts of memory which will
46//! // be handled by the process
47//! if let Err(e) = secmem_proc::harden_process() {
48//! println!("ERROR: could not harden process, exiting");
49//! println!("ERROR: {}", e);
50//! return;
51//! }
52//! // rest of your program
53//! }
54//! ```
55//!
56//! It is also possible to configure what kind of hardening steps are performed.
57//! For this, the API in [`config`] can be used. An example is shown below:
58//!
59//! ```
60//! fn main() {
61//! // harden before doing anything else
62//! let mut config = secmem_proc::Config::DEFAULT;
63//! config.set_anti_tracing(false);
64//! config.set_fs(false);
65//! if let Err(e) = config.harden_process() {
66//! println!("ERROR: could not harden process, exiting");
67//! println!("ERROR: {}", e);
68//! return;
69//! }
70//! // rest of your program
71//! }
72//! ```
73//!
74//! In the last example we use the API in [`win_acl`] to set a custom DACL on
75//! Windows. In the example we grant the `PROCESS_CREATE_THREAD` permissions in
76//! addition to the default ones. Note that in this particular use case the same
77//! could have been achieved using [`Config::set_win_dacl_custom_user_perm`],
78//! which is clearly a lot easier. The below approach is, however, a lot more
79//! flexible.
80//!
81//! ```
82//! #[cfg(windows)]
83//! fn set_windows_dacl() -> secmem_proc::Result {
84//! use windows::Win32::System::Threading::{
85//! PROCESS_CREATE_THREAD, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SYNCHRONIZE,
86//! PROCESS_TERMINATE,
87//! };
88//!
89//! use secmem_proc::win_acl::{AddAllowAceAcl, EmptyAcl, TokenUser};
90//!
91//! // First obtain the SID of the process user
92//! let user = TokenUser::process_user()?;
93//! let sid = user.sid();
94//!
95//! // Now specify the ACL we want to create
96//! // Only things explicitly allowed with `AddAllowAceAcl` will be allowed; noting else
97//! let acl_spec = EmptyAcl;
98//! let access_mask = PROCESS_QUERY_LIMITED_INFORMATION
99//! | PROCESS_TERMINATE
100//! | PROCESS_SYNCHRONIZE
101//! | PROCESS_CREATE_THREAD;
102//! let acl_spec = AddAllowAceAcl::new(acl_spec, access_mask, sid);
103//!
104//! // Create ACL and set as process DACL
105//! let acl = acl_spec.create()?;
106//! acl.set_process_dacl_protected()?;
107//! Ok(())
108//! }
109//!
110//! fn main() {
111//! // harden before doing anything else
112//! let mut config = secmem_proc::Config::DEFAULT;
113//! #[cfg(windows)]
114//! config.set_win_dacl_custom_fn(set_windows_dacl);
115//! config.set_fs(false);
116//! if let Err(e) = config.harden_process() {
117//! println!("ERROR: could not harden process, exiting");
118//! println!("ERROR: {}", e);
119//! return;
120//! }
121//! // rest of your program
122//! }
123//! ```
124//!
125//! # Cargo features
126//! - `std` (default): Enable functionality that requires `std`. Currently
127//! required for anti-tracing on Linux via `/proc/self/status`. This feature
128//! is enabled by default.
129//! - `unstable`: Enable functionality that depends on undocumented or unstable
130//! OS/platform details. This feature only enables support for these; to
131//! actually enable these anti-debugging methods, they have to be specifically
132//! enabled in the [configuration].
133//! - `dev`: This feature enables all features required to run the test-suite,
134//! and should only be enabled for that purpose.
135//!
136//! # Implementation
137//! - Disable ptrace and core dumps for the process on linux using prctl
138//! - Disable ptrace and core dumps for the process on freebsd using procctl
139//! - Disable ptrace on macos using ptrace
140//! - Disable core dumps for the process on posix systems using rlimit
141//! - Set restricted DACL for the process on windows
142//! - When the `std` feature is enabled, detect debuggers on linux by reading
143//! `/proc/self/status` (std, anti-tracing)
144//! - Detect debuggers on windows using `IsDebuggerPresent` and
145//! `CheckRemoteDebuggerPresent` (anti-tracing)
146//! - With unstable enabled, hide the thread from a debugger on windows
147//! (unstable, anti-tracing)
148//! - With unstable enabled, detect debuggers on windows by reading from the
149//! kernel structure `KUSER_SHARED_DATA` (unstable, anti-tracing)
150//!
151//! # Anti-tracing
152//! The hardening methods employed by this crate can be divided into two groups:
153//! * security related process hardening, and
154//! * anti-tracing.
155//!
156//! The difference between the two lies in the thread model. Process hardening
157//! mostly assumes the process is not yet under attack, e.g. it is not yet being
158//! traced. Hardening methods then make changed to the configuration of the
159//! process to limit access other processes have to it, e.g. disable tracing of
160//! the process or disable core dumps. Anti-tracing assumes the process is
161//! already traced/debugged by a malicious process (malware). The goal is then
162//! to detect the tracer/debugger. Anti-tracing methods can always be subverted
163//! by a tracer/debugger, though some are harder to work around than others.
164//! (The `KUSER_SHARED_DATA` unstable anti-tracing method on windows is a
165//! difficult one to work around.) Anti-tracing can be disabled using
166//! [`Config::set_anti_tracing(false)`].
167//!
168//! [`windows` crate]: https://crates.io/crates/windows
169//! [configuration]: config::Config
170//! [`Config::set_anti_tracing(false)`]: Config::set_anti_tracing
171
172#[cfg(windows)]
173extern crate alloc;
174
175mod internals;
176
177pub mod components;
178pub mod config;
179pub mod error;
180pub mod harden;
181pub mod macros;
182
183#[cfg(windows)]
184pub mod win_acl;
185/// This module is only available on windows.
186#[cfg(not(windows))]
187pub mod win_acl {}
188
189pub use config::Config;
190pub use error::Result;
191pub use harden::harden_process;
192
193#[cfg(test)]
194mod tests {
195 /// > Freedom is the freedom to say that two plus two makes four.
196 ///
197 /// Nineteen Eighty-Four, George Orwell
198 #[test]
199 fn freedom() {
200 assert_ne!(2 + 2, 5);
201 assert_eq!(2 + 2, 4);
202 }
203}