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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Writing 7z file entries to disk: direct writes and the temp-file-and-rename
//! path for destinations that already hold something.
use LinkStorage;
use link_storage;
use crateProgressCallback;
use crateResult;
use crateSecurityConfig;
use crateCopyBuffer;
use cratecopy_cancellable;
use cratecommon;
use crateReplacePermit;
use crateLeaf;
use cratePendingMode;
use crateTempFileGuard;
use crateTempLeaf;
use crateVettedDir;
use crateReadFaults;
use crateMarkRun;
use crateFsView;
use crateQuotaPermit;
use crateSafePath;
use crateSafeSymlink;
use ErrorKind;
use Read;
use Write;
use Path;
/// What a 7z file write consumes besides the entry's bytes and location: the
/// scratch buffer, the quota permit, the mark and the progress callback.
///
/// The permit is held by value and the struct is not `Clone`, so one value
/// authorizes exactly one write.
pub
/// Writes `reader`'s bytes directly to `leaf` in `parent` (no temp file, no
/// rename), consuming the [`QuotaPermit`] that authorized the write.
///
/// Used for the common case where nothing currently occupies `leaf`
/// (issue #492): creates `leaf` itself via [`VettedDir::create_new`], giving
/// the same `O_EXCL`+`O_NOFOLLOW` guarantee against a symlink race as
/// [`write_file_with_permit`]'s temp file and as TAR/ZIP's
/// [`common::extract_file_with_permit`] — just without the two extra syscalls
/// (create+rename of a temp file) that only matter when something needs to be
/// atomically replaced. Callers must have already confirmed `leaf` has no
/// pre-existing occupant (see [`DestOccupancy`](super::host)); when it does,
/// use [`write_file_with_permit`] instead so a decode failure mid-stream
/// cannot leave a truncated file where the original stood.
///
/// `parent` is the pinned directory handle the caller obtained from the
/// directory walker; the create, the cleanup on failure and every later step
/// are relative to it, so a parent component swapped after the walk cannot
/// redirect the write (issue #818).
///
/// Taking `permit` by value mirrors the guarantee
/// [`common::copy_file_content_with_permit`] gives TAR's hardlink path, and
/// the guarantee [`common::extract_file_with_permit`] gives TAR/ZIP's
/// normal-file path (issue #445): `QuotaPermit` is neither `Clone` nor
/// `Copy`, so a caller cannot retain it to authorize a second write, and this
/// function cannot be called at all without moving a genuine permit out of
/// the validated entry.
///
/// # Security - Streaming Size Enforcement (GHSA-5j8q-wxg5-hj4r)
///
/// the permit's byte count is `entry.size` — the declared uncompressed size
/// read from 7z's own (attacker-controlled) archive metadata, the same value
/// already used to authorize `permit`. The copy goes through
/// [`copy_bounded`], which enforces it as a hard streaming ceiling and
/// an exact post-copy match rather than trusting it a second time, closing
/// the same gap TAR/ZIP close via [`common::extract_file_with_permit`].
///
/// # Errors
///
/// Returns an error if file creation, the copy, or the underlying I/O fails,
/// or if the actual decompressed byte count exceeds or falls short of
/// the permit's byte count.
pub
/// Writes `reader`'s bytes to `leaf` in `parent` via an atomic
/// temp-file-then-rename, consuming the [`QuotaPermit`] that authorized the
/// write.
///
/// Used only when `leaf` has a pre-existing occupant being overwritten
/// (issue #492): atomicity matters here because a decode failure mid-stream
/// must not leave a truncated file where the original stood. When `leaf` is
/// known to have no pre-existing occupant, [`write_file_direct`] skips the
/// temp file and rename entirely.
///
/// Taking `permit` by value mirrors the guarantee
/// [`common::copy_file_content_with_permit`] gives TAR's hardlink path, and
/// the guarantee [`common::extract_file_with_permit`] gives TAR/ZIP's
/// normal-file path (issue #445): `QuotaPermit` is neither `Clone` nor
/// `Copy`, so a caller cannot retain it to authorize a second write, and this
/// function cannot be called at all without moving a genuine permit out of
/// the validated entry.
///
/// # Security - Symlink-at-Destination Rejection (issue #471)
///
/// The temp file name is predictable from the process PID and a per-process
/// monotonic counter, so a pre-planted symlink (dangling or not) at a
/// predicted temp name used to be silently followed by `File::create`,
/// writing archive content outside the extraction root. The temp file is
/// now created via [`VettedDir::create_new`] (`O_EXCL | O_NOFOLLOW`), which
/// refuses any pre-existing name instead of following it. On collision
/// (`ErrorKind::AlreadyExists`), a fresh counter value is drawn and creation
/// is retried up to [`common::MAX_TEMP_FILE_CREATE_ATTEMPTS`] times. The temp
/// file, the rename and the cleanup are all relative to `parent` (issue #818).
///
/// # Security - Streaming Size Enforcement (GHSA-5j8q-wxg5-hj4r)
///
/// Same enforcement as [`write_file_direct`]: the copy runs through
/// [`copy_bounded`] with the permit's byte count (`entry.size`) as a hard
/// streaming ceiling and exact post-copy match, not trusted metadata.
///
/// # Errors
///
/// Returns an error if a unique temp file cannot be created within
/// [`common::MAX_TEMP_FILE_CREATE_ATTEMPTS`] attempts, if the copy or rename
/// fails, or if the actual decompressed byte count exceeds or falls short of
/// the permit's byte count.
pub
/// Implementation behind [`write_file_with_permit`], parameterized over the
/// candidate temp-name generator.
///
/// Tests inject a private, non-shared generator here instead of calling
/// [`write_file_with_permit`] directly, so that predicting which names a test
/// run will attempt never depends on the process-global temp counter — a
/// global shared with every other test in the same binary that also happens to
/// write a 7z file, and therefore not something a single test can safely
/// predict under `cargo test`'s default multi-threaded execution (only
/// nextest's one-test-per-process model would make that safe).
///
/// # Security - Streaming Size Enforcement (GHSA-5j8q-wxg5-hj4r)
///
/// Same enforcement as [`write_file_direct`]: the copy runs through
/// [`copy_bounded`] with the permit's byte count as a hard streaming
/// ceiling and exact post-copy match, not trusted metadata.
///
/// # Errors
///
/// Returns an error if a unique temp file cannot be created within
/// [`common::MAX_TEMP_FILE_CREATE_ATTEMPTS`] attempts, if the copy or rename
/// fails, or if the actual decompressed byte count exceeds or falls short of
/// the permit's byte count.
pub
/// lstat's `dest_path`, returning `Ok(None)` if nothing occupies it.
///
/// Uses `symlink_metadata` rather than `exists()`/`is_dir()`: those follow
/// symlinks and report a dangling symlink as absent, which would let a
/// dangling symlink slip past both duplicate detection and the
/// pre-existing-symlink rejection below (issues #468, #477, #478). Used by the
/// host pre-pass, which only decides whether to skip or refuse an entry before
/// anything is written.
///
/// # TOCTOU
///
/// This check is advisory, not atomic: the write itself re-probes the name
/// relative to its pinned parent directory ([`VettedDir::probe`]) and both of
/// 7z's write paths enforce the outcome again (issues #492, #818):
///
/// - When nothing occupied the name ([`write_file_direct`]): the fast path
/// creates it with `O_EXCL | O_NOFOLLOW`, so anything planted in the race
/// window makes that create fail with `AlreadyExists` instead of being
/// followed.
/// - When something did occupy it ([`write_file_with_permit`]): this branch
/// never opens the name itself, and `renameat` replaces whatever occupies it
/// without following a symlink planted there in the interim, so a race cannot
/// redirect the write through a symlink target.
pub
/// Refuses, from the file table alone, a link whose target is empty or larger
/// than a target may be, with the error reading it during extraction would
/// give.
///
/// Any other target is read and validated once, during extraction, so
/// extraction decodes each block a single time.
pub