alexcrichton / tar-rs

Tar file reading/writing for Rust
https://docs.rs/tar
Apache License 2.0
625 stars 184 forks source link

Calling set_size writes wrong value #305

Open kpcyrd opened 1 year ago

kpcyrd commented 1 year ago

hello! I've used the tar crate in a project to modify an archive and noticed the generated archives are invalid in some cases, /usr/bin/tar complains about a truncated archive and after some inspection I noticed the size that gets written appears to be multiplied by 8.

This doesn't seem to be an interoperability issue, calling .size() immediately after calling .set_size(...) also returns a value that's much bigger than what I intended to set.

I hacked together a minimal test case that searches with cargo-fuzz and found one:

#![no_main]
use libfuzzer_sys::fuzz_target;

use anyhow::Result;
use anyhow::Context;
use tar;

fn run_test(data: &[u8]) -> Result<()> {
    let mut r = &data[..];
    let mut a = tar::Archive::new(&mut r);
    let entry = a.entries()?.next().context("")??;
    let mut h = entry.header().clone();

    h.set_size(1337);
    if h.size().context("failed to get size")? != 1337 {
        panic!("bugged header");
    }

    Ok(())
}

fuzz_target!(|data: &[u8]| {
    run_test(data).ok();
});

tar

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 247, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 197, 250, 0, 0, 0, 0, 0, 0, 255, 9, 9, 181, 225, 154, 128, 0, 0, 16, 0, 0, 0, 0, 0, 181, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 255, 255, 5, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 46, 0, 0, 9, 9, 9, 181, 181, 181, 181, 1, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 0, 0, 9, 181, 181, 181, 181, 55, 55, 55, 55, 55, 55, 55, 0, 0, 0, 181, 0, 0, 0, 0, 0, 181, 181, 64, 13, 245, 181, 181, 181, 181, 181, 181, 0, 245, 181, 181, 181, 181, 181, 181, 0, 0, 0, 0, 0, 41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 181, 181, 181, 181, 0, 0, 0, 0, 0, 0, 0, 0, 181, 230, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 0, 0, 0, 0, 0, 12, 0, 247, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 46, 0, 0, 9, 9, 9, 181, 181, 181, 181, 1, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 181, 181, 181, 181, 13, 181, 180, 181, 181, 181, 181, 181, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 181, 181, 181, 13, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 0, 0, 0, 5, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 247, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 181, 225, 154, 128, 0, 0, 16, 0, 0, 0, 0, 0, 181, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 224, 255, 255, 5, 255, 255, 255, 255, 255, 255, 233, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 247, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 9, 9, 181, 181, 181, 181, 55, 55, 55, 55, 55, 55, 54, 0, 0, 0, 0, 0, 0, 0, 0, 181, 181, 181, 64, 13, 245, 181, 181, 181, 181, 181, 181, 0, 245, 181, 181, 181, 181, 181, 181, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 9, 0]

Screenshot

image

Possibly related to #286, #298