exarch-core 0.12.0

Memory-safe archive extraction library with security validation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! 7z archive format extraction.
//!
//! Provides secure extraction of 7z archives with security validation.
//!
//! # Security Features
//!
//! - AES-256 content and headers decrypted only with a password; the key
//!   derivation of one operation is bounded by `max_kdf_work`, and raw-key mode
//!   is refused as weak
//! - Solid archives rejected by default (configurable)
//! - Path traversal prevention
//! - Decompression bomb detection
//! - Memory exhaustion protection for solid blocks
//! - Windows symlink detection (via reparse point attributes)
//! - Unix symlink targets validated through the symlink policy
//! - Unix permission bits of regular files restored through the same sanitizer
//!   as TAR and ZIP: setuid, setgid and sticky never apply and the world-write
//!   policy holds; an entry without a Unix mode keeps the default
//!
//! # Supported Compression Methods
//!
//! - LZMA / LZMA2
//! - BZIP2
//! - `PPMd`
//! - Copy (stored)
//!
//! # Symlinks, Hardlinks and Special Entries
//!
//! 7z has no link entry type, so links are detected from attributes:
//!
//! - **Windows symlinks**: Detected via `FILE_ATTRIBUTE_REPARSE_POINT` and
//!   rejected: their target is not stored in a readable form
//! - **Unix symlinks**: Detected through the Unix mode p7zip keeps in the high
//!   16 bits of the attributes (`0x8000` flag plus `S_IFLNK`). The target is
//!   the entry's data. `extract` reads and validates it through the symlink
//!   policy, so the link is created only under `allow_symlinks`. `list` leaves
//!   it unread unless asked (`StoredTargets::Read`), and `verify` reads it only
//!   under an integrity check
//! - **FIFOs, sockets, device nodes**, an undefined Unix type, or a recorded
//!   kind (Unix mode or the Windows directory bit) that contradicts the entry's
//!   own kind are refused by `list`, `verify` and `extract`
//! - **Hardlinks**: Cannot be detected, extracted as separate files (data
//!   duplication)
//!
//! **Cost**: Link targets live in the compressed data. By default `list`
//! decodes nothing for them, `verify` decodes them only inside its integrity
//! pass (no extra decode), and `extract` decodes each block once, reading a
//! target as it creates the link. Reading targets in `list` decodes the blocks
//! that hold links, bounded by the solid policy, the admission checks and
//! the solid block limit.
//!
//! **Security Impact**: `extract` validates a symlink target against the disk
//! as it creates the link, so a refusal for the target's content (escape,
//! corrupt data, not UTF-8) is a `PartialExtraction` that leaves the entries
//! written before it inside the destination; `atomic` makes the extraction
//! all-or-nothing. A link whose target is empty or too large is refused from
//! the file table before any write. `verify` without an integrity check cannot
//! validate targets: it reports one Medium issue counting the unread links.
//!
//! # Solid Archives
//!
//! 7z supports "solid" compression where multiple files are compressed together
//! as a single block. While this provides better compression ratios, it has
//! security implications:
//!
//! - **Wasted work**: Extracting a single file requires decoding the entire
//!   solid block up to that file
//! - **Denial of service**: Malicious archives can declare very large solid
//!   blocks that exhaust CPU time and disk space
//!
//! **Default Policy**: Solid archives are **rejected** by default.
//! Use `SecurityConfig::with_allow_solid_archives(true)` to enable extraction
//! with the declared decoded size of every block bounded by
//! `SolidLimits::max_block_memory`. The limit bounds decoded volume per block,
//! not resident memory.
//!
//! # Decoder Memory and Block Layout
//!
//! `extract` and `verify` share one admission step, `sevenz_refusals`. Before
//! any byte is decoded, every block's coder memory (LZMA/LZMA2 dictionary,
//! `PPMd` memory) is checked against `max_decoder_memory`, and blocks with an
//! unknown coder, AES content without a usable password or over the
//! key-derivation budget, or whose files do not add up
//! to the decoded size are refused. A header whose sub-stream count differs
//! from its number of streamed files (a directory that owns a sub-stream, an
//! orphan block) is refused when the archive is opened, by `list` as well.
//!
//! # Parallel LZMA2 Decoding
//!
//! A block that holds a single LZMA2 stream is split at its dictionary resets
//! into independent runs (7-Zip writes one per 4x dictionary when it
//! compresses on several threads). `extract` and `verify` decode those runs
//! ahead on worker threads, within the lookahead memory
//! [`ParallelDecode`](crate::ParallelDecode) allows (128 MiB by default), while
//! the consumer reads them back in order. A run that does not fit the budget, a
//! stream the planner cannot prove well formed, and every other block layout
//! decode sequentially, with the same bytes and error kinds (a corrupt stream
//! fails the same file either way). The decoder window stays bounded by
//! `max_decoder_memory`; the lookahead adds at most its configured budget on
//! top. sevenz-rust2's own multi-threaded reader is not used because it buffers
//! whole runs with no output bound.
//!
//! The archive header is read under a byte limit, the lower of
//! `max_decoder_memory` and 256 bytes per `max_file_count` entry plus 64 KiB
//! (with an unbounded `max_file_count`, `max_decoder_memory` alone).
//! The limit holds the raw header size, the decoded size of an encoded header
//! and that header coder's declared dictionary or model, and, because the
//! entry table sevenz-rust2 builds grows with the decoded header, it bounds
//! that table too. A refusal of the size derived from `max_file_count` is
//! `QuotaExceeded { HeaderSize }`, any other `QuotaExceeded { DecoderMemory }`.
//! The number of entries the header declares sizes the entry table, so it is
//! read before the table is built (a plain header, or an encoded header with
//! one copy, LZMA or LZMA2 coder) and held to `max_file_count` plus a small
//! allowance, as `QuotaExceeded { HeaderEntryCount }`. A header that is present
//! but whose count cannot be read (another coder, several coders, an
//! unfollowable layout, a missing start header on a file longer than an empty
//! archive) is refused as `InvalidArchive` before it is parsed. The count is
//! read in a separate pass from the parse, the same window the header size
//! probe already has; a source that changes between the two reads is outside
//! the model. Not bounded: the buffer that receives the decoded header (about
//! twice the limit at most). Unlike a block, whose window is charged as
//! `min(dictionary, 2 * unpack size)`, the header coder is charged its declared
//! dictionary, so a very small `max_decoder_memory` refuses most 7-Zip
//! archives at open (see [`crate::SecurityConfig::max_decoder_memory`]).
//!
//! # Examples
//!
//! Allow solid archives with a block limit:
//!
//! ```no_run
//! use exarch_core::SecurityConfig;
//! use exarch_core::SolidArchives;
//! use exarch_core::SolidLimits;
//! use exarch_core::non_zero;
//!
//! let limits = SolidLimits::DEFAULT.with_max_block_memory(non_zero!(512 * 1024 * 1024));
//! let config = SecurityConfig::default().with_solid_archives(SolidArchives::Allow(limits));
//! // ... extract with config
//! ```

use super::common;
use super::occupant::OccupantPolicy;
use super::output_root::OutputRoot;
use super::traits::ArchiveFormat;
use super::traits::sealed;
use crate::ArchiveError;
use crate::EntryCompression;
use crate::ExtractionOptions;
use crate::ExtractionReport;
use crate::ExtractionWarning;
use crate::Malformation;
use crate::ProgressCallback;
use crate::Result;
use crate::SecurityConfig;
use crate::SinkOptions;
use crate::VerificationOptions;
use crate::Violation;
use crate::encryption::Credentials;
use crate::inspection::report::Finding;
use crate::inspection::report::Heuristic;
use crate::inspection::report::IssueSeverity;
use crate::inspection::report::VerificationIssue;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::sync::Arc;

/// Cached entry metadata from initial archive read.
/// Avoids re-parsing archive during extraction.
#[derive(Debug, Clone)]
struct CachedEntry {
    name: String,
    size: u64,
    class: SevenZClass,
    /// Validated compression info; see [`sevenz_entry_compression`].
    compression: EntryCompression,
}

/// Size of the fixed 7z signature header that precedes the packed streams.
const SIGNATURE_HEADER_LEN: usize = 32;
const SIGNATURE_HEADER_SIZE: u64 = SIGNATURE_HEADER_LEN as u64;

/// 7z archive handler with security validation.
///
/// Supports:
/// - 7z format (LZMA SDK)
/// - Compression methods: LZMA, LZMA2, BZIP2, `PPMd`, Copy
/// - Multi-volume archives (read-only)
/// - AES-256 decryption with a password, bounded key derivation
/// - Solid archive detection (rejected by default)
///
/// # Solid Archives
///
/// Solid compression stores multiple files in a single compressed block.
/// This provides better compression ratios but requires decompressing
/// the entire block to extract a single file, which can cause memory
/// exhaustion attacks.
///
/// **Security Policy**: Solid archives are rejected by default.
/// Use `SecurityConfig::with_allow_solid_archives` to enable with block limits.
#[derive(Debug)]
pub(crate) struct SevenZArchive<R: Read + Seek> {
    source: R,
    entries: Vec<CachedEntry>,
    /// Header parsed when the archive was opened, borrowed by extraction,
    /// `list` and `verify` instead of re-parsing the archive (issues #492,
    /// #742), so a second call on the same instance still has a real parsed
    /// archive to work with.
    header: SevenZHeader,
    /// The policy every operation of the handle enforces.
    config: Arc<SecurityConfig>,
}

impl<R: Read + Seek> SevenZArchive<R> {
    /// Creates a 7z archive reader that enforces `config`.
    ///
    /// # Security Checks
    ///
    /// - Refuses encrypted content without a password
    /// - Validates archive header signature
    /// - Checks for solid compression (rejected by default)
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - The archive header is encrypted and no password is supplied
    /// - Archive header is invalid
    /// - Format is not recognized
    #[cfg(test)]
    pub(crate) fn new(source: R, config: &SecurityConfig) -> Result<Self> {
        Self::open(source, config, &Credentials::default())
    }

    /// Creates a 7z archive reader that enforces `config` and decrypts with
    /// `credentials`.
    ///
    /// # Errors
    ///
    /// Returns error if the archive header is invalid, the format is not
    /// recognized, or `config` refuses the archive, plus the
    /// typed refusals of an encrypted header: no password, a key derivation
    /// over `max_kdf_work`, a wrong password.
    pub(crate) fn open(
        mut source: R,
        config: &SecurityConfig,
        credentials: &Credentials,
    ) -> Result<Self> {
        let header =
            open_sevenz_header(&mut source, HeaderLimit::from_config(config), credentials)?;

        let source_len = source.seek(SeekFrom::End(0)).map_err(ArchiveError::Io)?;
        let compression = sevenz_entry_compression(&header, source_len)?;
        let entries: Vec<CachedEntry> = header
            .files()
            .iter()
            .zip(compression)
            .map(|(e, compression)| CachedEntry {
                name: e.name.clone(),
                size: e.size,
                class: SevenZClass::of(e),
                compression,
            })
            .collect();

        source.rewind().map_err(ArchiveError::Io)?;

        Ok(Self {
            source,
            entries,
            header,
            config: Arc::new(config.clone()),
        })
    }
}

impl<R: Read + Seek> SevenZArchive<R> {
    fn anti_item_count(&self) -> u64 {
        let count = self
            .entries
            .iter()
            .filter(|entry| entry.class == SevenZClass::AntiItem)
            .count();
        u64::try_from(count).unwrap_or(u64::MAX)
    }
}

/// Appends one warning for the anti-items `extract` skipped.
///
/// A no-op when `count` is zero.
fn push_anti_item_warning(report: &mut ExtractionReport, count: u64) {
    if count > 0 {
        report.add_warning(ExtractionWarning::AntiItemsSkipped { count });
    }
}

/// The informational finding `verify` reports for skipped anti-items, or
/// `None` when the archive has none.
fn anti_item_issue(count: u64) -> Option<VerificationIssue> {
    (count > 0).then(|| {
        VerificationIssue::new(
            IssueSeverity::Info,
            Finding::Heuristic(Heuristic::AntiItemsSkipped { count }),
        )
    })
}

impl<R: Read + Seek> sealed::Sealed for SevenZArchive<R> {}

impl<R: Read + Seek> ArchiveFormat for SevenZArchive<R> {
    fn extract<'o>(
        mut self,
        output: impl Into<OutputRoot<'o>>,
        options: &ExtractionOptions,
        progress: &mut dyn ProgressCallback,
    ) -> Result<ExtractionReport> {
        let config = Arc::clone(&self.config);
        let output = output.into().with_record_budget(
            OccupantPolicy::new(options.existing_files, config.repeated_paths).record_budget(),
        );
        self.admit(&config)?.extract_with(
            |config| SevenZHostTarget::create(output, config),
            SinkOptions::existing_files_of(options),
            progress,
        )
    }

    #[cfg(test)]
    fn list(mut self) -> Result<crate::inspection::ArchiveManifest> {
        use crate::inspection::list::ListPass;
        use crate::inspection::list::list_sevenz_parsed;
        let config = Arc::clone(&self.config);
        list_sevenz_parsed(
            &self.header,
            &mut self.source,
            &config,
            &mut ListPass::strict(),
        )
    }

    #[cfg(test)]
    fn verify(
        mut self,
        options: VerificationOptions,
    ) -> Result<crate::inspection::VerificationReport> {
        let config = Arc::clone(&self.config);
        self.verify_with_progress(&config, options, &mut crate::NoopProgress)
    }
}

impl<R: Read + Seek> SevenZArchive<R> {
    /// Verifies the archive, reporting each scanned entry to `progress`.
    pub(crate) fn verify_with_progress(
        &mut self,
        config: &SecurityConfig,
        options: VerificationOptions,
        progress: &mut dyn ProgressCallback,
    ) -> Result<crate::inspection::VerificationReport> {
        use crate::inspection::list::list_sevenz_parsed;
        use crate::inspection::report::ArchiveStructure;
        use crate::inspection::verify::list_for_verify;
        use crate::inspection::verify::verify_manifest_with_structure;

        let mut issues = refusal_issues(&sevenz_refusals(&self.header, config));
        issues.extend(anti_item_issue(self.anti_item_count()));
        let listed = list_for_verify(config, options, progress, |listing| {
            list_sevenz_parsed(&self.header, &mut self.source, config, listing)
        })?;
        let structure = ArchiveStructure::of_manifest(&listed.manifest).with_refusals(&issues);
        Ok(verify_manifest_with_structure(
            &listed.manifest,
            config,
            &structure,
            listed.integrity,
        ))
    }
}

/// Whether a sevenz-rust2 error means the archive needs a password.
///
/// Matches the typed upstream variants so an upstream message change cannot
/// silently reclassify encrypted archives as merely invalid.
pub(crate) fn is_password_error(error: &sevenz_rust2::Error) -> bool {
    matches!(
        error,
        sevenz_rust2::Error::PasswordRequired | sevenz_rust2::Error::MaybeBadPassword(_)
    )
}

/// Converts sevenz-rust2 errors to our `ArchiveError` type.
///
/// I/O failures keep their original `io::Error` (kind and source chain).
impl From<sevenz_rust2::Error> for ArchiveError {
    fn from(err: sevenz_rust2::Error) -> Self {
        match err {
            sevenz_rust2::Error::PasswordRequired => return common::encrypted_archive_error(),
            sevenz_rust2::Error::MaybeBadPassword(_) => {
                return Violation::Encrypted {
                    reason: crate::EncryptionRefusal::WrongPasswordOrCorrupt,
                    name: None,
                }
                .into();
            }
            _ => {}
        }

        match err {
            sevenz_rust2::Error::Io(io_err, _) => Self::Io(io_err),
            sevenz_rust2::Error::FileOpen(io_err, path) => Self::Io(std::io::Error::new(
                io_err.kind(),
                crate::IoContext::new("failed to open a 7z file", format!("{path}: {io_err}")),
            )),
            other => Self::from(Malformation::SevenZLibraryError {
                detail: other.to_string().into(),
            }),
        }
    }
}

#[cfg(test)]
mod link_walk_tests;

#[cfg(test)]
mod parallel_tests;

#[cfg(test)]
mod sequential_crc_tests;

pub(crate) use admit::iter_sevenz_refusals;
pub(crate) use admit::refusal_issues;
pub(crate) use admit::sevenz_entry_compression;
pub(crate) use admit::sevenz_refusals;
pub(crate) use classify::SevenZClass;
pub(crate) use classify::SevenZKind;
pub(crate) use classify::file_permissions;
pub(crate) use header::FileIndex;
pub(crate) use header::HeaderLimit;
pub(crate) use header::SEVENZ_MAGIC;
pub(crate) use header::SevenZHeader;
pub(crate) use header::open_sevenz_header;
#[cfg(test)]
pub(crate) use header::read_sevenz_header;
use host::SevenZHostTarget;
pub(crate) use links::LinkCapture;
pub(crate) use links::LinkOutcome;
pub(crate) use links::LinkTable;
pub(crate) use links::StoredLinkTarget;
pub(crate) use links::deliver_undecoded_links;
pub(crate) use links::walk_sevenz_links;
pub(crate) use verify::is_decode_announced;
pub(crate) use verify::verify_sevenz_blocks;

mod admit;
mod classify;
mod decode;
mod extract;
mod header;
mod header_probe;
mod host;
mod links;
mod lzma2;
mod prefetched;
mod raw_key;
mod sink;
mod verify;
mod write;

#[cfg(test)]
mod tests;