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
//! Keeping real keys out of reach of other processes of the same user.
//!
//! The gateway holds real keys in memory for its whole lifetime. Its own API
//! has no read channel for them (`route ls` prints the vkey, `export` strips
//! them), but the operating system has one: any process running as the same
//! uid can read `/proc/<pid>/mem`, or attach a debugger. For an agent that is
//! the same user, that is a one-line command.
/// Linux: make this process unreadable to every process without
/// `CAP_SYS_PTRACE`, including the rest of our own uid.
///
/// `PR_SET_DUMPABLE = 0` denies `/proc/<pid>/{mem,environ,maps,fd}` and
/// `PTRACE_ATTACH` to same-uid readers — `kernel.yama.ptrace_scope` is
/// irrelevant here, and 0 on many distributions. Core dumps stop as a side
/// effect, which is what you want for a process holding keys.
///
/// This only works on the process that calls it: `execve(2)` resets the flag
/// to 1, so a launcher cannot do it on someone else's behalf — which is
/// precisely why a wrapper around the agent (`envp run` and friends) cannot
/// provide this protection, and a long-lived gateway can.
///
/// macOS (`task_for_pid`) and Windows (`SeDebugPrivilege`) already refuse
/// same-user memory reads without privilege escalation, so this is a no-op
/// there.