exarch-core 0.10.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
//! Grammar for the bytes `tar` consumed between two entries.
//!
//! The `tar` crate merges `x`, `L` and `K` headers into the entry that follows
//! them and decides on its own where each header and body ends. Readers
//! disagree on how to read a checksum, a size or a PAX length that is not
//! plain, so the raw bytes are verified here against a closed grammar and the
//! result is cross-checked with the positions the `tar` crate reports. The
//! `tar` crate stays the only authority on entry boundaries: this check can
//! only add refusals.
//!
//! The window is a run of 512-byte blocks starting at the next block boundary:
//! zero or more extension headers (`x`, `L`, `K`, each at most once, with a
//! ustar or GNU magic), each followed by its body padded to a block, then the
//! header of the yielded entry exactly at the end of the window.

use std::ops::Range;

use super::CHECKSUM_FIELD;
use super::PrecedingEntry;
use super::SIZE_FIELD;
use super::check_header_block;
use super::check_record_framing;
use super::is_plain_base256;
use super::is_plain_octal;
use crate::Result;
use crate::TarAmbiguity;
use crate::TarExtensionKind;
use crate::TarHeaderField;
use crate::formats::tar_metadata_limit::MetadataWindow;

const BLOCK: usize = 512;
const TYPEFLAG_OFFSET: usize = 156;
const MAGIC_FIELD: Range<usize> = 257..265;
const USTAR_MAGIC: &[u8; 8] = b"ustar\x0000";
const GNU_MAGIC: &[u8; 8] = b"ustar  \0";
const BASE256_VALUE: Range<usize> = 4..12;
const CHECKSUM_SPACES: u64 = 8 * b' ' as u64;

fn extension_kind_of(block: &[u8; BLOCK]) -> Option<TarExtensionKind> {
    let magic = &block[MAGIC_FIELD];
    if magic != USTAR_MAGIC && magic != GNU_MAGIC {
        return None;
    }
    match block[TYPEFLAG_OFFSET] {
        b'x' => Some(TarExtensionKind::Pax),
        b'L' => Some(TarExtensionKind::LongName),
        b'K' => Some(TarExtensionKind::LongLink),
        _ => None,
    }
}

fn block_at(bytes: &[u8], offset: usize) -> Option<&[u8; BLOCK]> {
    bytes
        .get(offset..offset.checked_add(BLOCK)?)?
        .try_into()
        .ok()
}

fn octal_value(field: &[u8]) -> Option<u64> {
    field
        .iter()
        .skip_while(|&&b| b == b' ')
        .take_while(|b| (b'0'..=b'7').contains(b))
        .try_fold(0u64, |acc, &b| {
            acc.checked_mul(8)?
                .checked_add(u64::from(b.checked_sub(b'0')?))
        })
}

fn declared_size(block: &[u8; BLOCK]) -> Result<u64> {
    let field = &block[SIZE_FIELD];
    let value = if is_plain_base256(field) {
        field
            .get(BASE256_VALUE)
            .and_then(|value| value.as_array::<8>())
            .copied()
            .map(u64::from_be_bytes)
    } else if is_plain_octal(field) {
        octal_value(field)
    } else {
        None
    };
    value.ok_or_else(|| {
        TarAmbiguity::NonCanonicalNumber {
            field: TarHeaderField::Size,
        }
        .into()
    })
}

fn verify_checksum(block: &[u8; BLOCK]) -> Result<()> {
    let field = &block[CHECKSUM_FIELD];
    let declared = is_plain_octal(field)
        .then(|| octal_value(field))
        .flatten()
        .ok_or(TarAmbiguity::NonCanonicalNumber {
            field: TarHeaderField::Checksum,
        })?;
    let byte_sum = |bytes: &[u8]| bytes.iter().map(|&b| u32::from(b)).sum::<u32>();
    let (Some(head), Some(tail)) = (
        block.get(..CHECKSUM_FIELD.start),
        block.get(CHECKSUM_FIELD.end..),
    ) else {
        return Err(TarAmbiguity::ChecksumMismatch.into());
    };
    let actual = u64::from(byte_sum(head))
        .saturating_add(u64::from(byte_sum(tail)))
        .saturating_add(CHECKSUM_SPACES);
    (declared == actual).ok_or(TarAmbiguity::ChecksumMismatch.into())
}

/// Offset of the first block boundary at or after the window start, which is
/// where the `tar` crate looks for the next header.
fn first_header_offset(window_start: u64) -> Result<usize> {
    let block = BLOCK as u64;
    window_start
        .checked_next_multiple_of(block)
        .and_then(|boundary| boundary.checked_sub(window_start))
        .and_then(|padding| usize::try_from(padding).ok())
        .ok_or_else(|| TarAmbiguity::WindowMismatch.into())
}

/// Verifies the bytes `tar` read to reach an entry.
///
/// After a global PAX header the padding up to the first block boundary must
/// be zero, as must the padding after every extension header body, and a GNU
/// long name or link body must be one string ending in a single NUL.
///
/// `header_pos` and `file_pos` are the offsets the `tar` crate reports for the
/// yielded entry's header and data.
///
/// # Errors
///
/// Returns [`crate::ArchiveError::InvalidArchive`] when a header's checksum or
/// size is not canonical, a local PAX body is not canonical, an extension
/// header repeats, or the blocks do not end exactly at the entry the `tar`
/// crate yielded.
pub(super) fn check_window(
    window: &MetadataWindow<'_>,
    preceding: PrecedingEntry,
    header_pos: u64,
    file_pos: u64,
) -> Result<()> {
    let bytes = window.bytes();
    let mut offset = first_header_offset(window.start())?;
    if preceding == PrecedingEntry::GlobalPax {
        check_zero_padding(bytes.get(..offset))?;
    }
    let mut seen: Vec<TarExtensionKind> = Vec::new();
    let mut pax_has_size = false;
    loop {
        let block = block_at(bytes, offset).ok_or(TarAmbiguity::WindowMismatch)?;
        verify_checksum(block)?;
        let size = declared_size(block)?;
        let Some(kind) = extension_kind_of(block) else {
            let has_long = seen.iter().any(|k| *k != TarExtensionKind::Pax);
            if pax_has_size && has_long {
                return Err(TarAmbiguity::StackedSizeRecord.into());
            }
            return check_final_header(window, block, offset, header_pos, file_pos);
        };
        if seen.contains(&kind) {
            return Err(TarAmbiguity::DuplicateExtensionHeader { kind }.into());
        }
        seen.push(kind);

        let body_start = offset
            .checked_add(BLOCK)
            .ok_or(TarAmbiguity::WindowMismatch)?;
        let body_end = usize::try_from(size)
            .ok()
            .and_then(|len| body_start.checked_add(len))
            .ok_or(TarAmbiguity::WindowMismatch)?;
        let body = bytes
            .get(body_start..body_end)
            .ok_or(TarAmbiguity::WindowMismatch)?;
        if kind == TarExtensionKind::Pax {
            check_record_framing(body)?;
            pax_has_size = pax_declares_size(body);
        } else {
            check_long_record(kind, body)?;
        }
        offset = body_start
            .checked_add(body.len().next_multiple_of(BLOCK))
            .ok_or(TarAmbiguity::WindowMismatch)?;
        check_zero_padding(bytes.get(body_end..offset))?;
    }
}

/// Whether a framed PAX body holds a `size` record.
fn pax_declares_size(body: &[u8]) -> bool {
    tar::PaxExtensions::new(body)
        .flatten()
        .any(|record| record.key_bytes() == b"size")
}

/// Checks the padding after a trailing global PAX header, in the window the
/// `tar` crate read before reporting the end of the archive. An archive that
/// ends inside the padding has nothing left to disagree on.
pub(super) fn check_trailing_global_padding(window: &MetadataWindow<'_>) -> Result<()> {
    let bytes = window.bytes();
    let end = first_header_offset(window.start())?.min(bytes.len());
    check_zero_padding(bytes.get(..end))
}

fn check_zero_padding(padding: Option<&[u8]>) -> Result<()> {
    let padding = padding.ok_or(TarAmbiguity::WindowMismatch)?;
    padding
        .iter()
        .all(|&b| b == 0)
        .ok_or_else(|| TarAmbiguity::NonZeroPadding.into())
}

/// Readers cut a GNU long name or link at the first NUL of the padded block,
/// so a non-empty body must end in exactly one NUL. A NUL elsewhere in the
/// text is refused later as a path or target containing null bytes.
fn check_long_record(kind: TarExtensionKind, body: &[u8]) -> Result<()> {
    match body.split_last() {
        None => Ok(()),
        Some((0, text)) if text.last() != Some(&0) => Ok(()),
        _ => Err(TarAmbiguity::MalformedLongRecord { kind }.into()),
    }
}

fn check_final_header(
    window: &MetadataWindow<'_>,
    block: &[u8; BLOCK],
    offset: usize,
    header_pos: u64,
    file_pos: u64,
) -> Result<()> {
    let at_end = offset.checked_add(BLOCK) == Some(window.bytes().len());
    let position = window.start().saturating_add(offset as u64);
    if !at_end || position != header_pos || window.end() != file_pos {
        return Err(TarAmbiguity::WindowMismatch.into());
    }
    check_header_block(block, block[MAGIC_FIELD] == *USTAR_MAGIC)
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use std::assert_matches;

    use tar::Header;

    use super::*;
    use crate::ArchiveError;

    fn header_block(typeflag: u8, size: usize) -> Vec<u8> {
        let mut header = Header::new_gnu();
        header.set_size(size as u64);
        header.set_entry_type(tar::EntryType::new(typeflag));
        header.set_cksum();
        header.as_bytes().to_vec()
    }

    fn padded(mut bytes: Vec<u8>) -> Vec<u8> {
        bytes.resize(bytes.len().next_multiple_of(BLOCK), 0);
        bytes
    }

    fn pax_extension(body: &[u8]) -> Vec<u8> {
        padded([header_block(b'x', body.len()), body.to_vec()].concat())
    }

    fn assert_refused(result: Result<()>, expected: &TarAmbiguity) {
        assert_matches!(
            result,
            Err(ArchiveError::TarAmbiguity(found)) if found == *expected
        );
    }

    #[test]
    fn a_window_start_that_overflows_the_next_block_is_refused() {
        assert_refused(
            first_header_offset(u64::MAX).map(drop),
            &TarAmbiguity::WindowMismatch,
        );
    }

    #[test]
    fn a_bare_final_header_is_accepted() {
        let bytes = header_block(b'0', 0);
        let window = MetadataWindow::new(0, &bytes);
        check_window(&window, PrecedingEntry::Other, 0, 512).unwrap();
    }

    #[test]
    fn the_first_header_starts_at_the_next_block_boundary() {
        let mut bytes = vec![0u8; 5];
        bytes.extend(header_block(b'0', 0));
        let window = MetadataWindow::new(507, &bytes);
        check_window(&window, PrecedingEntry::Other, 512, 1024).unwrap();
    }

    #[test]
    fn a_window_ending_past_the_reported_file_position_is_refused() {
        let bytes = header_block(b'0', 0);
        let window = MetadataWindow::new(0, &bytes);
        assert_refused(
            check_window(&window, PrecedingEntry::Other, 0, 1024),
            &TarAmbiguity::WindowMismatch,
        );
    }

    #[test]
    fn a_final_header_at_another_position_than_reported_is_refused() {
        let bytes = header_block(b'0', 0);
        let window = MetadataWindow::new(0, &bytes);
        assert_refused(
            check_window(&window, PrecedingEntry::Other, 512, 512),
            &TarAmbiguity::WindowMismatch,
        );
    }

    #[test]
    fn a_window_that_ends_inside_a_header_is_refused() {
        let bytes = header_block(b'0', 0);
        let window = MetadataWindow::new(0, &bytes[..300]);
        assert_refused(
            check_window(&window, PrecedingEntry::Other, 0, 300),
            &TarAmbiguity::WindowMismatch,
        );
    }

    #[test]
    fn an_extension_header_followed_by_the_entry_is_accepted() {
        let body = b"18 path=long/name\n";
        let bytes = [pax_extension(body), header_block(b'0', 0)].concat();
        let window = MetadataWindow::new(0, &bytes);
        check_window(&window, PrecedingEntry::Other, 1024, 1536).unwrap();
    }

    #[test]
    fn an_extension_body_running_past_the_window_is_refused() {
        let bytes = [header_block(b'x', 4096), vec![0u8; 512]].concat();
        let window = MetadataWindow::new(0, &bytes);
        assert_refused(
            check_window(&window, PrecedingEntry::Other, 512, 1024),
            &TarAmbiguity::WindowMismatch,
        );
    }

    #[test]
    fn a_repeated_extension_header_is_refused() {
        let body = b"18 path=long/name\n";
        let bytes = [
            pax_extension(body),
            pax_extension(body),
            header_block(b'0', 0),
        ]
        .concat();
        let window = MetadataWindow::new(0, &bytes);
        assert_refused(
            check_window(&window, PrecedingEntry::Other, 2048, 2560),
            &TarAmbiguity::DuplicateExtensionHeader {
                kind: TarExtensionKind::Pax,
            },
        );
    }

    #[test]
    fn a_non_canonical_pax_length_is_refused() {
        let body = b"+20 path=long/name\n";
        let bytes = [pax_extension(body), header_block(b'0', 0)].concat();
        let window = MetadataWindow::new(0, &bytes);
        assert_refused(
            check_window(&window, PrecedingEntry::Other, 1024, 1536),
            &TarAmbiguity::MalformedRecord,
        );
    }

    #[test]
    fn a_checksum_that_is_not_the_byte_sum_is_refused() {
        let mut block = header_block(b'0', 0);
        block[0] = b'a';
        let window = MetadataWindow::new(0, &block);
        assert_refused(
            check_window(&window, PrecedingEntry::Other, 0, 512),
            &TarAmbiguity::ChecksumMismatch,
        );
    }

    #[test]
    fn a_vertical_tab_in_an_extension_header_size_is_refused() {
        let body = b"18 path=long/name\n";
        let mut extension = pax_extension(body);
        extension[SIZE_FIELD.start] = 0x0b;
        let sum: u32 = extension[..BLOCK]
            .iter()
            .enumerate()
            .map(|(i, &b)| u32::from(if CHECKSUM_FIELD.contains(&i) { b' ' } else { b }))
            .sum();
        extension[CHECKSUM_FIELD].copy_from_slice(format!("{sum:06o}\0 ").as_bytes());
        let bytes = [extension, header_block(b'0', 0)].concat();
        let window = MetadataWindow::new(0, &bytes);
        assert_refused(
            check_window(&window, PrecedingEntry::Other, 1024, 1536),
            &TarAmbiguity::NonCanonicalNumber {
                field: TarHeaderField::Size,
            },
        );
    }
}