Created
January 21, 2026 11:51
-
-
Save eqvinox/71d951f2f47a06c1b3d1c532e4605d5d to your computer and use it in GitHub Desktop.
reproducer script for GNU tar generating invalid tar files in multi-volume mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -x | |
| TAR="${1:-tar}" # allow pointing at a different tar version for convenience | |
| cat > info.sh <<EOF | |
| #!/bin/sh | |
| printf '%s.%d\n' "\$TAR_ARCHIVE" "\$TAR_VOLUME" >&\$TAR_FD | |
| EOF | |
| chmod +x info.sh | |
| echo brokenfile > brokenfile | |
| echo nextfile > nextfile | |
| printf "\nif the previous file ends on a volume boundary, everything is fine:\n\n" | |
| dd if=/dev/zero bs=$(( 4096 - 512 - 512 )) count=1 of=prevfile status=none | |
| $TAR -F "$(pwd)/info.sh" --record-size=4k -L 4k -cf normal.tar prevfile brokenfile nextfile | |
| # normal.tar contains: <type 0 for prevfile, size 3072> <prevfile> <type 0 for brokenfile, size 11> [EOF] | |
| # normal.tar.2 contains: <type M for brokenfile, size 11> <brokenfile> <type 0 for nextfile, size 10> <nextfile> [EOF] | |
| # tar works fine with the volume itself (without -M) | |
| $TAR -tvf normal.tar.2 | |
| # M--------- 0/0 11 1970-01-01 01:00 brokenfile--Continued at byte 0-- | |
| # -rw-rw-r-- equinox/equinox 9 2026-01-21 12:36 nextfile | |
| printf "\nif the previous file ends one block before a volume boundary, things break:\n\n" | |
| dd if=/dev/zero bs=$(( 4096 - 512 )) count=1 of=prevfile status=none | |
| $TAR -F "$(pwd)/info.sh" --record-size=4k -L 4k -cf broken.tar prevfile brokenfile nextfile | |
| # broken.tar contains: <type 0 for prevfile, size 3584> <prevfile> [EOF] | |
| # broken.tar.2 contains: <type M for brokenfile, size 11> <type 0 for brokenfile, size 11> <brokenfile> <type 0 for nextfile, size 10> <nextfile> [EOF] | |
| # tar chokes because it's "off by one header" now | |
| $TAR -tvf broken.tar.2 | |
| # M--------- 0/0 11 1970-01-01 01:00 brokenfile--Continued at byte 0-- | |
| # tar: Skipping to next header | |
| # -rw-rw-r-- equinox/equinox 9 2026-01-21 12:36 nextfile | |
| # tar: Exiting with failure status due to previous errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment