exarch-core 0.8.0

Memory-safe archive extraction library with security validation
Documentation
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
//! An [`EntrySink`] that keeps the extracted tree in memory.

use std::collections::BTreeMap;
use std::io;
use std::io::Write;
use std::num::NonZeroU64;
use std::path::Path;
use std::path::PathBuf;

use crate::ArchiveError;
use crate::ArchiveReader;
use crate::ExtractionReport;
use crate::ProgressCallback;
use crate::Result;
use crate::SecurityConfig;
use crate::closed_enum::closed_enum;
use crate::formats::detect::ArchiveType;
use crate::security::fs_view::PathModel;
use crate::security::permissions::SanitizedMode;
use crate::sink::EntrySink;
use crate::sink::SinkFull;
use crate::sink::SinkOptions;
use crate::types::SafePath;

const DEFAULT_LIMIT_BYTES: u64 = 500 * 1024 * 1024;
const INITIAL_CAPACITY_CAP: u64 = 64 * 1024;

/// The most bytes a [`MemorySink`] accepts, counted as its payload (file data,
/// hardlink copies, symlink targets) plus, for every entry, the bytes of its
/// path and [`MemorySink::ENTRY_OVERHEAD`].
///
/// # Examples
///
/// ```
/// use std::num::NonZeroU64;
///
/// use exarch_core::MemoryLimit;
///
/// assert_eq!(MemoryLimit::new(NonZeroU64::MIN).bytes().get(), 1);
/// assert_eq!(MemoryLimit::DEFAULT.bytes().get(), 500 * 1024 * 1024);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemoryLimit(NonZeroU64);

impl MemoryLimit {
    /// 500 MiB, the default `max_total_size`.
    pub const DEFAULT: Self = Self(match NonZeroU64::new(DEFAULT_LIMIT_BYTES) {
        Some(bytes) => bytes,
        None => NonZeroU64::MIN,
    });

    /// A limit of `bytes`.
    #[must_use]
    pub const fn new(bytes: NonZeroU64) -> Self {
        Self(bytes)
    }

    /// The limit in bytes.
    #[must_use]
    pub const fn bytes(self) -> NonZeroU64 {
        self.0
    }

    /// The limit equal to the config's `max_total_size` (a zero quota becomes
    /// one byte), so raising the extraction quota does not meet a second limit.
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::MemoryLimit;
    /// use exarch_core::SecurityConfig;
    ///
    /// let config = SecurityConfig::default().with_max_total_size(4096);
    /// assert_eq!(MemoryLimit::from_config(&config).bytes().get(), 4096);
    /// ```
    #[must_use]
    pub fn from_config<State>(config: &SecurityConfig<State>) -> Self {
        Self(NonZeroU64::new(config.max_total_size).unwrap_or(NonZeroU64::MIN))
    }
}

/// An entry held by a [`MemorySink`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum MemoryEntry {
    /// A regular file.
    File {
        /// The file's bytes.
        data: Vec<u8>,
        /// The permission bits after setuid and setgid were stripped, if the
        /// archive carries any.
        mode: Option<SanitizedMode>,
    },
    /// A directory the archive names explicitly.
    Directory,
    /// A symbolic link, stored as data.
    Symlink {
        /// The target as the archive spells it.
        target: PathBuf,
    },
}

closed_enum! {
    /// What a [`MemoryEntry`] holds.
    ///
    /// The string form is the one every language binding reports.
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::MemoryEntry;
    /// use exarch_core::MemoryEntryKind;
    ///
    /// assert_eq!(MemoryEntry::Directory.kind(), MemoryEntryKind::Directory);
    /// assert_eq!(MemoryEntryKind::Symlink.as_str(), "symlink");
    /// ```
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[non_exhaustive]
    pub enum MemoryEntryKind {
        /// A regular file.
        File => "file",
        /// A directory the archive names explicitly.
        Directory => "directory",
        /// A symbolic link, stored as data.
        Symlink => "symlink",
    }
}

impl MemoryEntry {
    /// What the entry holds.
    #[must_use]
    pub const fn kind(&self) -> MemoryEntryKind {
        match self {
            Self::File { .. } => MemoryEntryKind::File,
            Self::Directory => MemoryEntryKind::Directory,
            Self::Symlink { .. } => MemoryEntryKind::Symlink,
        }
    }

    /// The bytes the entry holds besides its path.
    fn payload_bytes(&self) -> u64 {
        match self {
            Self::File { data, .. } => data.len() as u64,
            Self::Symlink { target } => target.as_os_str().len() as u64,
            Self::Directory => 0,
        }
    }
}

/// The text of an entry path, or [`ArchiveError::InvalidArchive`] when it is
/// not valid UTF-8: a lossy conversion could merge two distinct entries into
/// one name.
///
/// # Errors
///
/// Returns [`ArchiveError::InvalidArchive`] for a path that is not UTF-8.
///
/// # Examples
///
/// ```
/// use std::path::Path;
///
/// use exarch_core::utf8_entry_path;
///
/// assert_eq!(utf8_entry_path(Path::new("a/b.txt")).unwrap(), "a/b.txt");
/// ```
pub fn utf8_entry_path(path: &Path) -> Result<&str> {
    path.to_str().ok_or_else(|| {
        ArchiveError::InvalidArchive(format!("entry path is not valid UTF-8: {}", path.display()))
    })
}

/// Extracts an archive held in memory into a [`MemorySink`].
///
/// The format is detected from the leading bytes unless `format` is given;
/// `limit` defaults to [`MemoryLimit::from_config`]. A failure after entries
/// were delivered reports [`PartialWrites::Discarded`](crate::PartialWrites):
/// nothing is written to disk and the entries are dropped with the error.
///
/// # Errors
///
/// Returns the errors of
/// [`extract_to_sink_with_progress`](crate::extract_to_sink_with_progress), and
/// [`ArchiveError::UnknownFormat`] or a reader-format error when the
/// format cannot be determined.
///
/// # Examples
///
/// ```
/// use std::assert_matches;
///
/// use exarch_core::MemoryEntry;
/// use exarch_core::NoopProgress;
/// use exarch_core::PathModel;
/// use exarch_core::SecurityConfig;
/// use exarch_core::SinkOptions;
/// use exarch_core::extract_bytes_to_memory;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut builder = tar::Builder::new(Vec::new());
/// let mut header = tar::Header::new_gnu();
/// header.set_size(2);
/// header.set_mode(0o644);
/// builder.append_data(&mut header, "a.txt", &b"hi"[..])?;
///
/// let (report, sink) = extract_bytes_to_memory(
///     builder.into_inner()?,
///     None,
///     &SecurityConfig::default(),
///     &SinkOptions::default(),
///     None,
///     PathModel::Host,
///     &mut NoopProgress,
/// )?;
/// assert_eq!(report.files_extracted, 1);
/// let entries = sink.into_entries();
/// assert_matches!(entries.values().next(), Some(MemoryEntry::File { .. }));
/// # Ok(())
/// # }
/// ```
pub fn extract_bytes_to_memory<B: AsRef<[u8]> + Send>(
    bytes: B,
    format: Option<ArchiveType>,
    config: &SecurityConfig,
    options: &SinkOptions,
    limit: Option<MemoryLimit>,
    model: PathModel,
    progress: &mut dyn ProgressCallback,
) -> Result<(ExtractionReport, MemorySink)> {
    let reader = ArchiveReader::from_bytes(bytes, format)?;
    let limit = limit.unwrap_or_else(|| MemoryLimit::from_config(config));
    let mut sink = MemorySink::new(limit).with_path_model(model);
    let report = crate::extract_to_sink_with_progress(reader, &mut sink, config, options, progress)
        .map_err(ArchiveError::discarded)?;
    Ok((report, sink))
}

/// The writer of a file being stored by a [`MemorySink`].
///
/// Nothing is published until `finish_file`; dropping it discards the bytes.
#[derive(Debug)]
pub struct MemoryFile {
    data: Vec<u8>,
    mode: Option<SanitizedMode>,
}

impl Write for MemoryFile {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.data.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

/// An [`EntrySink`] that keeps the extracted tree in memory.
///
/// The [`MemoryLimit`] counts, for every entry (directories included), the
/// bytes of its path plus [`ENTRY_OVERHEAD`](Self::ENTRY_OVERHEAD), and for
/// files, hardlink copies and symlinks their payload. A flood of directory
/// entries with long names therefore fills the sink instead of growing it
/// without bound. The overhead of the model that validates the archive is
/// bounded separately by the node budget and is not counted. While a file is
/// written its buffer may be up to twice the bytes received; it is shrunk when
/// the file is stored.
///
/// # Examples
///
/// ```
/// use std::assert_matches;
/// use std::io;
/// use std::path::Path;
///
/// use exarch_core::ArchiveReader;
/// use exarch_core::SinkOptions;
/// use exarch_core::MemoryEntry;
/// use exarch_core::MemorySink;
/// use exarch_core::SecurityConfig;
/// use exarch_core::extract_to_sink;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut builder = tar::Builder::new(Vec::new());
/// let mut header = tar::Header::new_gnu();
/// header.set_size(5);
/// header.set_mode(0o644);
/// builder.append_data(&mut header, "docs/hello.txt", &b"hello"[..])?;
/// let archive = builder.into_inner()?;
///
/// let mut sink = MemorySink::default();
/// let reader = ArchiveReader::tar(io::Cursor::new(archive), None);
/// extract_to_sink(
///     reader,
///     &mut sink,
///     &SecurityConfig::default(),
///     &SinkOptions::default(),
/// )?;
/// assert_matches!(
///     sink.get(Path::new("docs/hello.txt")),
///     Some(MemoryEntry::File { data, .. }) if data == b"hello"
/// );
/// assert_eq!(
///     sink.stored_bytes(),
///     MemorySink::ENTRY_OVERHEAD + "docs/hello.txt".len() as u64 + 5
/// );
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct MemorySink {
    limit: MemoryLimit,
    path_model: PathModel,
    stored: u64,
    entries: BTreeMap<PathBuf, MemoryEntry>,
}

impl Default for MemorySink {
    fn default() -> Self {
        Self::new(MemoryLimit::DEFAULT)
    }
}

impl MemorySink {
    /// Bytes charged to the limit for every entry on top of its path and
    /// payload, covering the map node and the entry itself.
    pub const ENTRY_OVERHEAD: u64 = 256;

    /// An empty sink that accepts at most `limit` bytes.
    #[must_use]
    pub const fn new(limit: MemoryLimit) -> Self {
        Self {
            limit,
            path_model: PathModel::Host,
            stored: 0,
            entries: BTreeMap::new(),
        }
    }

    /// Validates against `path_model` instead of the host's rules.
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::EntrySink;
    /// use exarch_core::MemorySink;
    /// use exarch_core::PathModel;
    ///
    /// let sink = MemorySink::default().with_path_model(PathModel::Portable);
    /// assert_eq!(sink.path_model(), PathModel::Portable);
    /// ```
    #[must_use]
    pub const fn with_path_model(mut self, path_model: PathModel) -> Self {
        self.path_model = path_model;
        self
    }

    /// The limit on charged bytes.
    #[must_use]
    pub const fn limit(&self) -> MemoryLimit {
        self.limit
    }

    /// The bytes charged to the limit now: payload, path bytes and
    /// [`ENTRY_OVERHEAD`](Self::ENTRY_OVERHEAD) per entry.
    #[must_use]
    pub const fn stored_bytes(&self) -> u64 {
        self.stored
    }

    /// The entry at `path`, spelled as it was delivered.
    #[must_use]
    pub fn get(&self, path: &Path) -> Option<&MemoryEntry> {
        self.entries.get(path)
    }

    /// Every entry in path order.
    pub fn iter(&self) -> impl Iterator<Item = (&Path, &MemoryEntry)> {
        self.entries
            .iter()
            .map(|(path, entry)| (path.as_path(), entry))
    }

    /// Consumes the sink into its entries.
    #[must_use]
    pub fn into_entries(self) -> BTreeMap<PathBuf, MemoryEntry> {
        self.entries
    }

    fn full(&self) -> io::Error {
        io::Error::new(
            io::ErrorKind::QuotaExceeded,
            SinkFull::new(self.limit.bytes().get()),
        )
    }

    fn charge_of(path: &Path, payload: u64) -> u64 {
        Self::ENTRY_OVERHEAD
            .saturating_add(path.as_os_str().len() as u64)
            .saturating_add(payload)
    }

    fn held_at(&self, path: &Path) -> u64 {
        self.entries
            .get(path)
            .map_or(0, |entry| Self::charge_of(path, entry.payload_bytes()))
    }

    /// The total charge after replacing the entry at `path` by one with
    /// `payload`, or the full error if that passes the limit.
    fn charged_with(&self, path: &Path, payload: u64) -> io::Result<u64> {
        self.stored
            .saturating_sub(self.held_at(path))
            .checked_add(Self::charge_of(path, payload))
            .filter(|total| *total <= self.limit.bytes().get())
            .ok_or_else(|| self.full())
    }

    /// Stores `entry` at `path`, replacing the entry there, unless the charge
    /// would pass the limit.
    fn store(&mut self, path: &SafePath, entry: MemoryEntry) -> io::Result<()> {
        let key = path.as_path();
        self.stored = self.charged_with(key, entry.payload_bytes())?;
        self.entries.insert(key.to_path_buf(), entry);
        Ok(())
    }
}

impl EntrySink for MemorySink {
    type File = MemoryFile;

    fn path_model(&self) -> PathModel {
        self.path_model
    }

    fn create_directory(&mut self, path: &SafePath) -> io::Result<()> {
        self.store(path, MemoryEntry::Directory)
    }

    fn create_file(
        &mut self,
        path: &SafePath,
        mode: Option<SanitizedMode>,
        size: u64,
    ) -> io::Result<MemoryFile> {
        self.charged_with(path.as_path(), size)?;
        self.stored = self.stored.saturating_sub(self.held_at(path.as_path()));
        self.entries.remove(path.as_path());
        let capacity = usize::try_from(size.min(INITIAL_CAPACITY_CAP)).unwrap_or(0);
        Ok(MemoryFile {
            data: Vec::with_capacity(capacity),
            mode,
        })
    }

    fn finish_file(&mut self, path: &SafePath, file: MemoryFile) -> io::Result<()> {
        let MemoryFile { mut data, mode } = file;
        data.shrink_to_fit();
        self.store(path, MemoryEntry::File { data, mode })
    }

    fn create_symlink(&mut self, link: &SafePath, target: &Path) -> io::Result<()> {
        self.store(
            link,
            MemoryEntry::Symlink {
                target: target.to_path_buf(),
            },
        )
    }

    fn create_hardlink(&mut self, link: &SafePath, target: &SafePath) -> io::Result<()> {
        let Some(MemoryEntry::File { data, mode }) = self.entries.get(target.as_path()) else {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                "hardlink target is not a file",
            ));
        };
        self.charged_with(link.as_path(), data.len() as u64)?;
        let copy = MemoryEntry::File {
            data: data.clone(),
            mode: *mode,
        };
        self.store(link, copy)
    }
}