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
//! Socket-directory creation and verification.
//!
//! Why: the containing directory — not the socket's own mode — is what makes a
//! socket unreachable to another uid, because Unix path resolution requires
//! search permission on every component. Getting the directory right is
//! therefore the whole security argument, and it is the part with the sharp
//! edge: `std::fs::metadata` and `set_permissions` both FOLLOW SYMLINKS, so a
//! naive owner+mode check reads the wrong inode when an attacker pre-creates
//! the path as a link (#5099 review finding 1).
//!
//! What: [`prepare_socket_dir`] creates the directory at `0700` atomically, or
//! — when it already exists — refuses a symlink outright and verifies owner and
//! mode on the directory itself. [`classify_existing_dir`] is the pure decision
//! function behind that verification, so every refusal is testable without root.
//!
//! Test: `tests.rs` — `classify_existing_dir_*` for the decisions,
//! `prepare_socket_dir_*` for the filesystem behavior including
//! `prepare_socket_dir_rejects_a_symlink`.
use ;
use io;
use ;
use Path;
use ;
/// Name the file type an `lstat` found, for a diagnostic that says what is
/// actually there rather than only what was expected.
///
/// Test: `classify_existing_dir_rejects_a_regular_file` asserts the rendered
/// message names the type.
pub
/// What [`prepare_socket_dir`] must do about a directory that already exists.
///
/// Why: separating the decision from the syscalls makes the refusal paths —
/// which otherwise need root or a second uid to reach — testable unprivileged.
/// Test: the `classify_existing_dir_*` tests.
pub
/// Decide whether a pre-existing socket directory is usable, as a pure function.
///
/// Why: the three refusal paths (symlink, foreign owner, unnarrowable mode) are
/// the security-critical ones and the hardest to reach from a test — a symlink
/// swap needs a race, a foreign owner needs root. Taking the `lstat` results as
/// plain arguments makes each decision assertable directly (#5099 review
/// finding 5).
///
/// What: rejects a symlink before anything else — this is the fix for the
/// review's finding 1, where `metadata()` on a symlinked directory reported the
/// *target's* uid and mode, so a link pointing at any directory the running
/// user happens to own passed the owner check and `set_permissions` then
/// chmod'd the target. Then rejects a foreign owner, then reports whether the
/// mode needs narrowing.
///
/// The file-type assertion comes second, before ownership. Without it a regular
/// file owned by this uid at the socket-dir path passed both remaining checks,
/// was classified [`DirVerdict::Narrow`], and got chmod'd to `0700` before
/// `bind` failed `ENOTDIR` — mangling the mode of a file this process did not
/// create, and reporting neither the cause nor what it actually found
/// (#5099 review round 2, finding 1).
///
/// `is_symlink`, `is_dir`, `owner`, and `mode` must come from
/// `symlink_metadata` (i.e. `lstat`), never `metadata` — passing followed values
/// reintroduces the bug this function exists to prevent.
///
/// Test: `classify_existing_dir_rejects_a_symlink`,
/// `classify_existing_dir_rejects_a_regular_file`,
/// `classify_existing_dir_rejects_a_foreign_owner`,
/// `classify_existing_dir_narrows_a_wide_dir`,
/// `classify_existing_dir_accepts_an_already_correct_dir`.
pub
/// Create `dir` at [`SOCKET_DIR_MODE`], or verify and repair it if it exists.
///
/// Why: this is what closes the bind-then-chmod window. Binding a Unix socket
/// creates the file, so a `chmod` after `bind` leaves an interval in which the
/// socket exists at the umask-derived mode. A caller cannot shrink that
/// interval to zero, and the obvious alternative — setting the process umask
/// around the bind — is process-global and therefore racy under a
/// multi-threaded tokio runtime, where a sibling thread creating an unrelated
/// file would silently inherit it. Holding the *directory* at `0700` before the
/// socket is created removes the exposure instead of narrowing it: path
/// resolution requires search permission on every component, so no other uid
/// can traverse to the socket at any point, including during the window.
///
/// What: creates `dir` with the mode passed to `mkdir(2)`, which is atomic —
/// unlike `create_dir_all` followed by `set_permissions`, which reproduces the
/// same ordering bug one level up. Ancestors are created with the default mode
/// because only the leaf holds sockets. If `dir` already exists, its `lstat`
/// results go through [`classify_existing_dir`]: a symlink is refused, a
/// non-directory is refused before anything can chmod it, a foreign owner is
/// refused, and a wider mode is narrowed.
///
/// **Residual race, deliberately not chased:** an attacker who can write to the
/// parent could in principle swap the directory for a symlink between the
/// `lstat` and the `chmod`. Under `/tmp`'s sticky bit that is impractical (only
/// the owner may rename or unlink an entry), and closing it properly needs
/// `openat`/`fchmod` on a directory fd. Rejecting symlinks removes the
/// pre-created-link attack, which is the practical one.
///
/// Test: `prepare_socket_dir_creates_at_0700`,
/// `prepare_socket_dir_narrows_a_wide_existing_dir`,
/// `prepare_socket_dir_rejects_a_symlink`,
/// `prepare_socket_dir_rejects_a_regular_file_without_chmodding_it`,
/// `prepare_socket_dir_is_idempotent`.