mirage / ocaml-tar

Pure OCaml library to read and write tar files
ISC License
54 stars 34 forks source link

Tar.Header.marshal computes incorrect checksum when buffer is larger than Tar.Header.length and is non-zero #144

Closed reynir closed 7 months ago

reynir commented 8 months ago

Now I am struggling to write a reproducing example, but I had Tar.Header.marshal buf hdr which produced a new checksum every time even if the file was the same. Then I rewrote it to Tar.Hheader.marshal (Cstruct.sub buf 0 Tar.Header.length) hdr and the checksum was correct. It was a very long buffer with some random data near the end. It seems the header checksum is computed over the whole buffer, but we should only consider the first 512 bytes (the actual header).

This was observed in tar.2.6.0, but it seems it's an issue with main branch judging from the source code.

reynir commented 8 months ago

Reproducing snippet:

let cs = Cstruct.create 1024 in
Tar.Header.make "test" 5L |> Tar.Header.marshal cs;
(* Blit "Hello, World!" well outside the tar header *)
Cstruct.blit_from_string "Hello, World!" 0 cs 800 13;
Tar.Header.unmarshal cs
reynir commented 8 months ago

This is also an issue in Tar.Header.unmarshal on 2.6.0. In main branch we check the input buffer is Tar.Header.length long and thus does not allow this behavior.