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
//! Process hardening: making gitaway's memory harder for other programs to
//! read while it holds a secret.
//!
//! gitaway keeps the device's private key (and, away from home, the proxy's
//! token) in its own memory. [`harden_process`] runs first thing in
//! [`crate::cli::main`], before any secret is loaded.
//!
//! On Linux it marks the process not dumpable (`PR_SET_DUMPABLE` = 0):
//!
//! - no core dump is written if gitaway crashes, so the key never lands in a
//! file on disk;
//! - other processes running as the same user can no longer attach a
//! debugger (`ptrace`) or read `/proc/<pid>/mem` and `/proc/<pid>/environ`.
//! Only root can.
//!
//! The setting applies to gitaway only. A program gitaway starts (git, ssh)
//! is dumpable again, because the kernel resets the flag when a process
//! runs a new program.
//!
//! On other systems it does nothing yet.
//!
//! # Extending
//!
//! Another platform's equivalent (such as macOS's `PT_DENY_ATTACH`) is a new
//! `#[cfg]` branch in [`harden_process`]. Keep each step one the process can
//! apply to itself without privileges.
use io;
/// Applies every hardening step this platform supports. Call it once, at
/// start-up, before loading a secret.
///
/// # Errors
///
/// If the system refuses a step, which a sandbox that blocks `prctl` could
/// do. gitaway warns and carries on: this is a layer on top of the key
/// store's own protection, and refusing would also stop plain git commands.