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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//! Test utilities for archive creation and validation.
//!
//! This module provides reusable helpers for creating in-memory test archives,
//! reducing code duplication across format-specific tests.
//!
//! # Panics
//!
//! All functions in this module may panic on I/O errors since they are
//! designed for test use only where panics are acceptable.

use std::io::Cursor;
use std::io::Write;

/// Creates a FIFO at `path` with the host `mkfifo`.
#[cfg(unix)]
pub(crate) fn make_fifo(path: &std::path::Path) {
    let status = std::process::Command::new("mkfifo")
        .arg(path)
        .status()
        .unwrap();
    assert!(status.success(), "mkfifo failed");
}

/// The 8.3 spelling of `path`, or `None` when the volume has no short names
/// (they can be disabled per volume).
#[cfg(windows)]
pub(crate) fn short_spelling(path: &std::path::Path) -> Option<std::path::PathBuf> {
    use std::os::windows::process::CommandExt;

    let output = std::process::Command::new("cmd")
        .raw_arg(format!(
            "/C for %I in (\"{}\") do @echo %~sI",
            path.display()
        ))
        .output()
        .ok()?;
    let short = std::path::PathBuf::from(String::from_utf8(output.stdout).ok()?.trim());
    (short.exists() && short != path).then_some(short)
}

/// The host's relative path limit, written out per host and independent of the
/// production constant so a wrong value there is caught.
#[cfg(target_os = "linux")]
pub(crate) const HOST_PATH_LIMIT: usize = 4095;
#[cfg(target_os = "windows")]
pub(crate) const HOST_PATH_LIMIT: usize = 32767;
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
pub(crate) const HOST_PATH_LIMIT: usize = 1023;

/// The most bytes a symlink target may take on the host, written out per host
/// like [`HOST_PATH_LIMIT`]: on Windows 4091 UTF-16 units of at most 3 bytes.
#[cfg(target_os = "windows")]
pub(crate) const HOST_SYMLINK_TARGET_BYTES: usize = 4091 * 3;
#[cfg(not(target_os = "windows"))]
pub(crate) const HOST_SYMLINK_TARGET_BYTES: usize = HOST_PATH_LIMIT;

/// Flips the low bit of the first byte of `marker` found in `bytes`, so stored
/// data fails its checksum.
pub(crate) fn flip_first(bytes: &mut [u8], marker: &[u8]) {
    let at = bytes
        .windows(marker.len())
        .position(|w| w == marker)
        .unwrap();
    bytes[at] ^= 0x01;
}

/// A reader that breaks the `Read` contract by reporting one byte more than
/// the buffer it was given.
pub(crate) struct OverReporting;

impl std::io::Read for OverReporting {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        Ok(buf.len() + 1)
    }
}

/// Appends to the file when its entry starts, which is after the walk and
/// before the source is opened.
pub(crate) struct GrowOnStart(pub(crate) std::path::PathBuf);

impl crate::ProgressCallback for GrowOnStart {
    fn on_entry_start(&mut self, _path: &std::path::Path, _position: crate::EntryPosition) {
        std::fs::OpenOptions::new()
            .append(true)
            .open(&self.0)
            .unwrap()
            .write_all(&[0u8; 4096])
            .unwrap();
    }

    fn on_bytes_written(&mut self, _bytes: u64) {}

    fn on_entry_complete(&mut self, _path: &std::path::Path) {}

    fn on_complete(&mut self) {}
}

/// A relative path of exactly `len` bytes made of 100-byte components.
#[must_use]
pub(crate) fn path_of_len(len: usize) -> String {
    let mut parts = Vec::new();
    let mut left = len;
    while left > 0 {
        let take = left.min(100);
        parts.push("a".repeat(take));
        left = left.saturating_sub(take + 1);
    }
    parts.join("/")
}

/// Creates an in-memory TAR archive from a list of `(path, content)` entries.
///
/// All files are created with mode `0o644`.
#[must_use]
pub(crate) fn create_test_tar(entries: Vec<(&str, &[u8])>) -> Vec<u8> {
    let mut ar = tar::Builder::new(Vec::new());
    for (path, data) in entries {
        let mut header = tar::Header::new_gnu();
        header.set_size(data.len() as u64);
        header.set_mode(0o644);
        header.set_cksum();
        ar.append_data(&mut header, path, data).unwrap();
    }
    ar.into_inner().unwrap()
}

/// Creates an in-memory ZIP archive from a list of `(path, content)` entries.
///
/// Files are stored uncompressed with mode `0o644`.
#[must_use]
pub(crate) fn create_test_zip(entries: Vec<(&str, &[u8])>) -> Vec<u8> {
    use zip::write::SimpleFileOptions;
    use zip::write::ZipWriter;

    let buffer = Vec::new();
    let mut zip = ZipWriter::new(Cursor::new(buffer));

    let options = SimpleFileOptions::default()
        .compression_method(zip::CompressionMethod::Stored)
        .unix_permissions(0o644);

    for (path, data) in entries {
        zip.start_file(path, options).unwrap();
        zip.write_all(data).unwrap();
    }

    zip.finish().unwrap().into_inner()
}

/// Creates a raw in-memory ZIP with a single entry whose name is written
/// verbatim into the local file header and central directory, bypassing any
/// normalization that the `zip` crate applies via `start_file`. Use this to
/// craft entries with absolute paths or traversal sequences that
/// the `zip` crate's own writer refuses to produce.
#[must_use]
pub(crate) fn create_raw_zip_entry(entry_name: &str, content: &[u8]) -> Vec<u8> {
    use crate::testkit::raw_zip::RawEntry;
    use crate::testkit::raw_zip::RawZip;

    let entry = RawEntry {
        crc: 0,
        date: 0,
        ..RawEntry::stored(entry_name, content)
    };
    RawZip::new(vec![entry]).build().bytes
}

/// Builder for creating TAR test archives with files, directories, symlinks,
/// and hardlinks.
pub(crate) struct TarTestBuilder {
    builder: tar::Builder<Vec<u8>>,
}

impl TarTestBuilder {
    /// Creates a new TAR test builder.
    #[must_use]
    pub(crate) fn new() -> Self {
        Self {
            builder: tar::Builder::new(Vec::new()),
        }
    }

    /// Adds a regular file to the archive.
    #[must_use]
    pub(crate) fn add_file(mut self, path: &str, data: &[u8]) -> Self {
        let mut header = tar::Header::new_gnu();
        header.set_size(data.len() as u64);
        header.set_mode(0o644);
        header.set_cksum();
        self.builder.append_data(&mut header, path, data).unwrap();
        self
    }

    /// Adds a directory to the archive.
    #[must_use]
    pub(crate) fn add_directory(mut self, path: &str) -> Self {
        let mut header = tar::Header::new_gnu();
        header.set_size(0);
        header.set_mode(0o755);
        header.set_entry_type(tar::EntryType::Directory);
        header.set_cksum();
        self.builder
            .append_data(&mut header, path, std::io::empty())
            .unwrap();
        self
    }

    /// Builds and returns the TAR archive data.
    #[must_use]
    pub(crate) fn build(self) -> Vec<u8> {
        self.builder.into_inner().unwrap()
    }
}

impl Default for TarTestBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Creates a raw ZIP archive containing a single symlink entry.
///
/// The ZIP spec encodes symlink targets as uncompressed file data, with
/// `S_IFLNK` mode bits stored in the high 16 bits of the central directory's
/// external attributes field. This bypasses the zip crate writer's
/// `unix_permissions()` path, which does not set external attributes reliably.
#[must_use]
pub(crate) fn create_zip_with_symlink(link_path: &str, target: &str) -> Vec<u8> {
    use crate::testkit::raw_zip::RawEntry;
    use crate::testkit::raw_zip::RawZip;
    use crate::testkit::raw_zip::SYMLINK_MODE;

    let entry = RawEntry {
        date: 0,
        external_attributes: SYMLINK_MODE << 16,
        ..RawEntry::stored(link_path, target.as_bytes())
    };
    RawZip::new(vec![entry]).build().bytes
}

/// Builder for creating ZIP test archives with files, directories, and
/// symlinks.
pub(crate) struct ZipTestBuilder {
    zip: zip::ZipWriter<Cursor<Vec<u8>>>,
}

impl ZipTestBuilder {
    /// Creates a new ZIP test builder.
    #[must_use]
    pub(crate) fn new() -> Self {
        Self {
            zip: zip::ZipWriter::new(Cursor::new(Vec::new())),
        }
    }

    /// Adds a regular file to the archive.
    #[must_use]
    pub(crate) fn add_file(mut self, path: &str, data: &[u8]) -> Self {
        use zip::write::SimpleFileOptions;

        let options = SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored)
            .unix_permissions(0o644);

        self.zip.start_file(path, options).unwrap();
        self.zip.write_all(data).unwrap();
        self
    }

    /// Adds a directory to the archive.
    #[must_use]
    pub(crate) fn add_directory(mut self, path: &str) -> Self {
        use zip::write::SimpleFileOptions;

        let options = SimpleFileOptions::default().unix_permissions(0o755);
        self.zip.add_directory(path, options).unwrap();
        self
    }

    /// Builds and returns the ZIP archive data.
    #[must_use]
    pub(crate) fn build(self) -> Vec<u8> {
        self.zip.finish().unwrap().into_inner()
    }
}

impl Default for ZipTestBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Builds a 512-byte raw ustar TAR header block, bypassing `tar::Header`'s
/// safety checks (`set_path`/`set_link_name` reject NUL bytes and other
/// adversarial values) so fixtures for security regression tests can be
/// constructed byte-for-byte.
fn raw_tar_header(name: &[u8], size: usize, typeflag: u8) -> [u8; 512] {
    let mut h = [0u8; 512];
    let name_len = name.len().min(100);
    h[..name_len].copy_from_slice(&name[..name_len]);
    h[100..108].copy_from_slice(b"0000644\0");
    h[108..116].copy_from_slice(b"0000000\0");
    h[116..124].copy_from_slice(b"0000000\0");
    h[124..136].copy_from_slice(format!("{size:011o}\0").as_bytes());
    h[136..148].copy_from_slice(b"00000000000\0");
    h[156] = typeflag;
    h[257..263].copy_from_slice(b"ustar\0");
    h[263..265].copy_from_slice(b"00");
    h[148..156].copy_from_slice(b"        ");
    let checksum: u32 = h.iter().map(|&b| u32::from(b)).sum();
    h[148..156].copy_from_slice(format!("{checksum:06o}\0 ").as_bytes());
    h
}

/// Pads `data` to a 512-byte TAR block boundary and appends it to `out`.
fn pad_tar_block(out: &mut Vec<u8>, data: &[u8]) {
    out.extend_from_slice(data);
    let rem = data.len() % 512;
    if rem != 0 {
        out.extend(std::iter::repeat_n(0u8, 512 - rem));
    }
}

/// Builds a raw ustar-format TAR archive with a PAX extended header record
/// (`{pax_key}={pax_value}`) followed by one entry of type `typeflag` named
/// `entry_name` with body `data`.
///
/// Writes headers directly rather than through `tar::Header`'s safe setters,
/// so adversarial PAX overrides — e.g. an embedded NUL byte or an empty
/// value in a `path`/`linkpath` record — can be constructed for security
/// regression tests.
#[must_use]
pub(crate) fn tar_with_pax_record(
    pax_key: &str,
    pax_value: &[u8],
    entry_name: &[u8],
    typeflag: u8,
    data: &[u8],
) -> Vec<u8> {
    let record = crate::testkit::pax::record(pax_key.as_bytes(), pax_value);

    let mut tar = Vec::new();
    tar.extend_from_slice(&raw_tar_header(b"PaxHeaders/entry", record.len(), b'x'));
    pad_tar_block(&mut tar, &record);
    tar.extend_from_slice(&raw_tar_header(entry_name, data.len(), typeflag));
    pad_tar_block(&mut tar, data);
    tar.extend(std::iter::repeat_n(0u8, 1024));
    tar
}

/// Builds a raw ustar-format TAR archive with a PAX `path` record embedding
/// `pax_path` (which may contain a NUL byte) as the effective path of a
/// regular-file entry with body `data`.
#[must_use]
pub(crate) fn tar_with_pax_nul_path(pax_path: &[u8], data: &[u8]) -> Vec<u8> {
    tar_with_pax_record("path", pax_path, b"ustar_name.txt", b'0', data)
}

/// Builds a raw ustar-format TAR archive with a PAX `linkpath` record
/// embedding `pax_linkpath` (which may be empty or contain a NUL byte) as
/// the effective link target of a symlink/hardlink entry. `typeflag` is
/// `b'2'` for a symlink entry, `b'1'` for a hardlink entry.
#[must_use]
pub(crate) fn tar_with_pax_linkpath(pax_linkpath: &[u8], typeflag: u8) -> Vec<u8> {
    tar_with_pax_record("linkpath", pax_linkpath, b"link_entry", typeflag, b"")
}

/// Builds a raw ustar-format TAR archive with a single regular-file entry
/// whose `name` field is `name_bytes`, which may not be valid UTF-8.
///
/// Writes the header directly rather than through `tar::Header::set_path`
/// (which requires a `Path`), so arbitrary non-UTF8 byte sequences can be
/// used as the entry name for extractability regression tests.
#[must_use]
pub(crate) fn tar_with_nonutf8_name(name_bytes: &[u8], data: &[u8]) -> Vec<u8> {
    let mut tar = Vec::new();
    tar.extend_from_slice(&raw_tar_header(name_bytes, data.len(), b'0'));
    pad_tar_block(&mut tar, data);
    tar.extend(std::iter::repeat_n(0u8, 1024));
    tar
}

/// What a `ZipCrypto` entry is refused as: too weak where decryption is built
/// in, password-required where it is not.
pub(crate) const fn legacy_cipher_refusal() -> crate::EncryptionRefusal {
    if cfg!(feature = "encryption") {
        crate::EncryptionRefusal::WeakEncryptionRefused
    } else {
        crate::EncryptionRefusal::PasswordRequired
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::raw_zip::RawEntry;
    use crate::raw_zip::RawZip;

    #[test]
    fn test_create_test_tar() {
        let tar_data = create_test_tar(vec![("file.txt", b"hello")]);
        assert!(!tar_data.is_empty());
    }

    #[test]
    fn test_create_test_zip() {
        let zip_data = create_test_zip(vec![("file.txt", b"hello")]);
        assert!(!zip_data.is_empty());
    }

    #[test]
    fn test_tar_builder() {
        let tar_data = TarTestBuilder::new()
            .add_file("file.txt", b"content")
            .add_directory("dir/")
            .build();
        assert!(!tar_data.is_empty());
    }

    #[test]
    fn test_zip_builder() {
        let zip_data = ZipTestBuilder::new()
            .add_file("file.txt", b"content")
            .add_directory("dir/")
            .build();
        assert!(!zip_data.is_empty());
    }

    /// ZIP archives whose structure different readers interpret differently.
    fn structural_zip_seeds() -> Vec<(&'static str, RawZip)> {
        let hidden = RawZip::new(vec![RawEntry::stored("evil.sh", b"echo pwned\n")]).build();
        let gap = hidden.bytes[..hidden.directory_start].to_vec();

        let mut hidden_local = RawZip::new(vec![RawEntry::stored("benign.txt", b"hello")]);
        hidden_local.before_directory.clone_from(&gap);
        let mut uncounted = RawZip::new(vec![
            RawEntry::stored("benign.txt", b"hello"),
            RawEntry::stored("hidden.sh", b"echo pwned\n"),
        ]);
        uncounted.end_count = Some(1);
        let mut descriptor_gap = RawZip::new(vec![
            RawEntry::stored("a.txt", b"hello").with_descriptor(true, true),
        ]);
        descriptor_gap.before_directory = gap;

        vec![
            (
                "header-name-mismatch.bin",
                RawZip::new(vec![
                    RawEntry::stored("cron_worker.sh", b"payload")
                        .with_central_name("doc_report.pdf"),
                ]),
            ),
            (
                "duplicate-names.bin",
                RawZip::new(vec![
                    RawEntry::stored("a.txt", b"first"),
                    RawEntry::stored("a.txt", b"EVIL"),
                ]),
            ),
            ("hidden-local-header.bin", hidden_local),
            ("uncounted-central-record.bin", uncounted),
            ("descriptor-gap.bin", descriptor_gap),
        ]
    }

    /// Dumps the adversarial archives built by this module's generators into
    /// `$EXARCH_FUZZ_SEEDS_DIR/<target>/` so `fuzz/seed-corpus.sh` can assemble
    /// them into the fuzz corpus. `test_utils` is `pub(crate)`, so this is the
    /// only place that can reach these generators without widening crate
    /// visibility for a dev-only need. Not run by default; invoked via
    /// `EXARCH_FUZZ_SEEDS_DIR=<dir> cargo test -p exarch-core --lib --
    /// --ignored dump_fuzz_seeds`; the test fails with a message when the
    /// variable is unset.
    #[test]
    #[ignore = "generates fuzz seeds on demand; not part of the default test run"]
    fn dump_fuzz_seeds() {
        let Some(dir) = std::env::var_os("EXARCH_FUZZ_SEEDS_DIR") else {
            panic!("set EXARCH_FUZZ_SEEDS_DIR to the directory that receives the seeds");
        };
        let seeds = std::path::PathBuf::from(dir);

        let write_seed = |target: &str, name: &str, data: &[u8]| {
            let dir = seeds.join(target);
            std::fs::create_dir_all(&dir).unwrap();
            std::fs::write(dir.join(name), data).unwrap();
        };

        write_seed(
            "tar",
            "basic.bin",
            &create_test_tar(vec![("file.txt", b"hello world")]),
        );
        write_seed(
            "tar",
            "builder.bin",
            &TarTestBuilder::new()
                .add_file("file.txt", b"content")
                .add_directory("dir/")
                .build(),
        );
        write_seed(
            "tar",
            "pax-record.bin",
            &tar_with_pax_record(
                "path",
                b"pax/override/path.txt",
                b"ustar_name.txt",
                b'0',
                b"data",
            ),
        );
        write_seed(
            "tar",
            "pax-nul-path.bin",
            &tar_with_pax_nul_path(b"path/with\0nul", b"data"),
        );
        write_seed(
            "tar",
            "pax-linkpath-symlink.bin",
            &tar_with_pax_linkpath(b"../../etc/passwd", b'2'),
        );
        write_seed(
            "tar",
            "pax-linkpath-hardlink.bin",
            &tar_with_pax_linkpath(b"../../etc/passwd", b'1'),
        );
        write_seed(
            "tar",
            "nonutf8-name.bin",
            &tar_with_nonutf8_name(&[0xff, 0xfe, b'/', b'x'], b"data"),
        );

        write_seed(
            "zip",
            "basic.bin",
            &create_test_zip(vec![("file.txt", b"hello world")]),
        );
        write_seed(
            "zip",
            "builder.bin",
            &ZipTestBuilder::new()
                .add_file("file.txt", b"content")
                .add_directory("dir/")
                .build(),
        );
        write_seed(
            "zip",
            "raw-traversal.bin",
            &create_raw_zip_entry("../../etc/passwd", b"data"),
        );
        write_seed(
            "zip",
            "symlink-escape.bin",
            &create_zip_with_symlink("link", "../../etc/passwd"),
        );

        for (name, zip) in structural_zip_seeds() {
            write_seed("zip", name, &zip.build().bytes);
        }
    }
}

#[cfg(test)]
mod pax_record_tests {
    use crate::testkit::pax::record;

    #[test]
    fn the_length_prefix_counts_its_own_digits_at_every_rollover() {
        for value_len in (0..1200).chain(999_980..1_000_020) {
            let built = record(b"k", &vec![b'v'; value_len]);
            let space = built.iter().position(|&b| b == b' ').unwrap();
            let declared: usize = std::str::from_utf8(&built[..space])
                .unwrap()
                .parse()
                .unwrap();
            assert_eq!(declared, built.len(), "value length {value_len}");
        }
    }
}