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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//! Peer-credential checks on an accepted Unix-domain connection.
//!
//! Why: filesystem permissions alone are a documented intention. A `0600`
//! socket in a `0700` directory is unreachable to another uid *while those bits
//! hold*, but nothing in the process re-checks them — an operator who loosens
//! the directory, a backup tool that restores it wide, or a socket reached
//! before this crate's hardening ran would all pass unnoticed. Asking the
//! kernel who is on the other end turns the permission bits into an enforced
//! boundary. ADR-0034 §3 ("The trust boundary") requires it for this reason.
//!
//! What: [`peer_uid`] wraps the platform syscall — `SO_PEERCRED` on Linux,
//! `getpeereid` on macOS and the BSDs — [`peer_uid_verdict`] is the pure
//! comparison behind the refusal, and [`ensure_peer_is_self`] joins them.
//! [`peer_pid`] (#6642) answers the adjacent question — WHICH process is on the
//! other end — for a caller that wants to meter it rather than refuse it.
//! Targets with neither syscall fail closed via
//! [`UdsSecurityError::UnsupportedPlatform`] rather than compiling to an
//! unchecked accept.
//!
//! Test: `peer_uid_of_self_connection_is_self` and
//! `bind_hardened_socket_is_connectable_after_hardening` cover the syscall;
//! `peer_uid_verdict_refuses_a_foreign_uid` and
//! `peer_uid_verdict_accepts_the_same_uid` cover the decision without needing a
//! second uid.
use io;
use AsRawFd;
use UnixStream;
use UdsSecurityError;
/// The uid this process runs as.
///
/// Why: the comparison target for every peer check, and the key
/// [`super::scratch_socket_dir`] uses to give each user its own directory.
/// What: `getuid(2)`. Cannot fail per POSIX.
/// Test: exercised by every peer test; equality with `id -u` is not asserted
/// because the test process has no independent source of truth for it.
/// Decide whether a peer uid is acceptable, as a pure function.
///
/// Why: the refusal branch is the security-critical one and cannot be reached
/// from an unprivileged test — connecting as a second uid needs root or a
/// pre-provisioned account. Taking both uids as arguments makes the decision
/// assertable directly, so only the syscall plumbing remains untested rather
/// than the whole policy (#5099 review finding 5).
///
/// What: returns [`UdsSecurityError::ForeignPeer`] unless `peer == own`. Root
/// is refused like any other foreign uid — root can bypass the filesystem check
/// anyway, so admitting it would buy nothing and widen the accepted set.
///
/// Test: `peer_uid_verdict_accepts_the_same_uid`,
/// `peer_uid_verdict_refuses_a_foreign_uid`,
/// `peer_uid_verdict_refuses_root_when_we_are_not_root`.
/// Read the uid of the process on the other end of `stream`.
///
/// Why: split from [`ensure_peer_is_self`] so a caller that wants to log or
/// meter the peer identity does not have to duplicate the platform `cfg`s.
/// What: `getsockopt(SO_PEERCRED)` on Linux, `getpeereid(3)` elsewhere on unix.
/// The credentials the kernel reports are those captured at `connect` time, so
/// a peer cannot change uid after connecting to defeat the check.
/// Test: `peer_uid_of_self_connection_is_self`.
/// See the Linux variant for the contract; this is the BSD/macOS syscall.
/// Read the pid of the process on the other end of `stream` (#6642).
///
/// Why: trusty-console has to answer "how much CPU is trusty-search using", and
/// a UDS daemon publishes no pid anywhere — no pid file, no field in its health
/// response. The kernel already knows, and the console already dials that exact
/// socket to detect the service, so the connection it makes IS the identifier.
/// This is the one implementation; a second consumer with the same question
/// calls it rather than shelling out to `lsof`.
///
/// Why the return type is `Option` and not `Result`: every caller so far treats
/// "cannot identify the peer" as a metric it simply does not have. There is no
/// remediation to report and nothing to fail on, so the platform gap, the
/// syscall error, and a nonsensical pid all collapse into the one answer the
/// caller acts on. Contrast [`peer_uid`], whose failure MUST refuse a
/// connection and therefore owes the caller a reason.
///
/// What: `getsockopt(SO_PEERCRED)`'s `ucred.pid` on Linux,
/// `getsockopt(SOL_LOCAL, LOCAL_PEERPID)` on macOS/iOS. The other BSDs expose
/// no peer-pid option, so they answer `None`. Like [`peer_uid`], the credentials
/// are those captured at `connect` time.
///
/// The pid identifies the process that was listening when the connection was
/// made. It can be reused after that process exits, so a caller holding one
/// across time must re-verify the process still exists — see
/// [`ProcessCpuSampler::refresh`](crate::sys_metrics::ProcessCpuSampler::refresh),
/// which drops a vanished pid for exactly this reason.
/// Test: `peer_pid_of_self_connection_is_this_process`.
/// See the Linux variant for the contract; this is the Darwin syscall.
/// No peer-pid option on this target — the caller has no measurement.
/// Fail closed on a unix target with neither `SO_PEERCRED` nor `getpeereid`.
/// Refuse a connection whose peer is not this same uid.
///
/// Why: the accept-side half of the `0600` contract. ADR-0034 §3: "the target
/// verifies peer credentials on accept and refuses any connection whose uid is
/// not its own. This is what makes the permission bits an enforced boundary
/// rather than a documented intention."
///
/// What: reads [`peer_uid`] and applies [`peer_uid_verdict`] against
/// [`self_uid`].
///
/// Test: `bind_hardened_socket_is_connectable_after_hardening` proves the
/// accept path for a same-uid peer; `peer_uid_verdict_refuses_a_foreign_uid`
/// proves the refusal decision without needing a second uid.