Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling set_size writes wrong value #305

Open
kpcyrd opened this issue Nov 25, 2022 · 0 comments
Open

Calling set_size writes wrong value #305

kpcyrd opened this issue Nov 25, 2022 · 0 comments

Comments

@kpcyrd
Copy link

kpcyrd commented Nov 25, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant