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
//! 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.
//! 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.
/// 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.