DEV Community

Cover image for I fed one tarball to 8 tar readers and got 5 different folders
Ian
Ian

Posted on

I fed one tarball to 8 tar readers and got 5 different folders

The file is 4,096 bytes. Eight blocks of 512.

I extracted it with the tar that ships on my Mac, and a two-byte file came out 512 bytes long. Its first eight characters spell smuggled.

The archive comes from Harmen Stoppels, who wrote up The Hardest Tarball? last week and collects cases like it in a test suite called tar-minefield. I ran his case through every tar reader I had on hand. None of them crashed. They just disagreed.

What's inside the 4 KB

The case is F35 in the suite, and its page on the project site lays out every block. In order:

  1. A regular file a holding a\n.
  2. A pax extended header with one record: size=512.
  3. A hardlink h pointing at a. Its own header says size 0, but the pax record before it says 512.
  4. A header for an empty file named smuggled.
  5. The two zero blocks that end the archive.

So the whole fight is over block 4. If a hardlink can carry data, those 512 bytes belong to h. If it can't, they're the header of a third file. POSIX says data for a hardlink "may be included" (Stoppels quotes that line in his post), and stops there. It doesn't say what the data should do.

His post builds a cousin of this archive with a single GNU tar command. I don't have GNU tar on this Mac, so I used the suite's own file and checked it against the hash on the case page: 9cda45a9...07760, a match.

Eight readers, one Mac

Each reader got its own empty folder. For Python I used tarfile with filter="data", the mode the project's matrix lists for pip. For Go I wrote a 39-line extractor on top of the standard archive/tar reader, so the framing is Go's and the file writing is mine. node-tar and tar-fs went into a throwaway folder through npm. The rest were already installed.

Here's what landed on disk:

reader                      a       h          smuggled
macOS tar (bsdtar 3.5.3)    512 B   512 B      -
Python tarfile 3.15.2       2 B     2 B        0 B
Perl Archive::Tar 2.38      2 B     2 B        0 B
Go archive/tar 1.23.5       2 B     2 B        0 B
node-tar 7.5.22             2 B     2 B        -
tar-fs 3.1.3                2 B     2 B        -
PHP PharData 8.5.12         2 B     0 B (*)    0 B
RubyGems 4.0.3              2 B     -          0 B
Enter fullscreen mode Exit fullscreen mode

(*) PHP made h a separate empty file, not a link.

Five different trees. The macOS one is the strangest, so I looked inside:

$ xxd x-bsdtar/a | head -1
00000000: 736d 7567 676c 6564 0000 0000 0000 0000  smuggled........
$ cmp <(dd if=F35.tar bs=512 skip=5 count=1 2>/dev/null) x-bsdtar/a && echo same
same
Enter fullscreen mode Exit fullscreen mode

bsdtar took the pax size at its word, read the next 512 bytes as the hardlink's content, and wrote them through the link. Since h and a are one file, the original a\n is gone. In its place: the header block of a file that, for bsdtar, never existed.

One detail surprised me more than the table. Python and Go both report h with a size of 512. Then they read the very next block as a header anyway. They see the claim and set it aside.

My results match the site's matrix for the readers we share, though my bsdtar (3.5.3), Perl module (2.38) and RubyGems (4.0.3) are older than the versions listed there. Go is the exception: the site tests containerd's extractor, and I used the bare standard library. The site has 24 rows for this case, and four reject it outright: .NET's System.Formats.Tar, Julia's Tar.jl, uv's tar-codec, and live-bootstrap's untar (unless it's run with --non-strict).

The bug lives between two readers

Here's the claim I want to make. Most of these readers aren't broken in a way you could file a clean bug about. Each picks a reading of a loose spec, and most of the readings are defensible. The danger shows up when two of them sit in the same pipeline.

Picture an upload service that scans archives with Python's tarfile, hashes each member, and approves the bundle. Then a build step on a Mac unpacks it with the system tar. The scanner approved an a holding two bytes. The disk has 512 bytes it never hashed. Flip the pair and a node-based tool never sees smuggled, while a Python installer writes it.

The project README names exactly this: a scanner and an extractor built on different implementations can be looking at different files. Its catalogue of ambiguities also files this case's family next to real advisories for tokio-tar, tar-rs and node-tar.

So the question for your own stack isn't "is my tar library correct?" It's "do all the places that read this archive agree?"

Strict is cheaper than it sounds

Stoppels thinks rejecting is the best outcome, even if it bends POSIX. Not everyone in the HN thread agreed. One commenter (molticrystal) said they'd always rather get something out of a damaged archive than nothing. Another (rwmj) suggested two modes: refuse anything odd from untrusted uploads, salvage what you can from your own corrupted backup. A third (stabbles) described tar as a format with two jobs, backup and software distribution, that want opposite threat models.

That split feels right to me. If you're installing software or accepting uploads, reject. And the cost is measured, not guessed. The case page says this shape turned up in none of the 82,586 release tarballs scanned from Spack, nixpkgs, Guix, PyPI, crates.io, RubyGems and live-bootstrap. The README says the project's whole default policy still accepts 99.80% of them.

Check your own pipeline today

The suite ships a checker that takes any extract command. The README shows it like this:

python -I suite/check.py -- tar -xf '{archive}' -C '{dest}'
Enter fullscreen mode Exit fullscreen mode

I didn't run it. It's code from a clone, and for a post I only run my own scripts. I read it instead: it extracts each case into its own temp folder and flags anything written outside it.

What I did run is a stopgap for this one case, in eleven lines:

import sys, tarfile

def check(path):
    with tarfile.open(path) as t:
        for m in t.getmembers():
            if (m.islnk() or m.issym()) and m.size:
                return f"reject: {m.name!r} is a link that claims {m.size} bytes of data"
    return "ok"

for p in sys.argv[1:]:
    print(f"{p}: {check(p)}")
Enter fullscreen mode Exit fullscreen mode
in/F35.tar: reject: 'h' is a link that claims 512 bytes of data
in-own/clean.tar: ok
in-npm/tar-7.5.22.tgz: ok
in-npm/tar-fs-3.1.3.tgz: ok
Enter fullscreen mode Exit fullscreen mode

clean.tar is one I made with macOS tar from a file, a hardlink to it and an empty file. It works because Python reports the 512 even while ignoring it. It's a tripwire for one of the suite's 134 cases, not a policy. For real coverage, run the checker, or put a strict reader at the front of the pipeline.

Rough edges

This is a working draft, and it says so: verdicts carry a "draft" label, and new cases are still landing (an open PR adds 54). The repo is heavy, about 127 MB by GitHub's count. My shallow clone stalled, so I pulled the files I needed through the API. I also couldn't test GNU tar, BusyBox, 7-Zip, Java, .NET, Rust or Julia here; for those, the numbers above are the site's, not mine.

Which tar reader unpacks things in your deploy path, and is it the same one your scanner uses?

Top comments (3)

Collapse
 
aiapikey profile image
于侃 •

The finding worth pinning isn't which of the five is right, it's that all eight fail silently. A reader that resolves the "may" permissively turns 512 unattributed bytes into a file named by whoever wrote the archive - harmless only if every consumer in your pipeline resolves it the same way.

That is the real risk: validate with one library and extract with another, and the disagreement becomes an arbitrary-file-write primitive without a single crash. Stoppels' suite is the right artifact because the matrix is the only spec you actually have.

The control that follows: pin the divergent cases as fixtures, and treat any change in that matrix across a dependency bump as a release blocker. Nothing in a green test suite would have caught this.

Collapse
 
jes_p_d4f61f7aa2ec405a418 profile image
Dev_PJS •

'None of them crashed. They just disagreed' is the sentence that matters. I hit the agent version of this constantly: same brief, same input, two runs — two different results, both reported as done with full confidence. It's why I stopped accepting a run's own summary as verification. The check has to look at the artifact itself, ideally with a different method than the one that produced it, the way you ran eight readers instead of trusting one. A tool that never crashes can still be quietly wrong in five different directions.

Collapse
 
aiapikey profile image
于侃 •

That is the part I would push further: the check has to be able to disagree with the run, and you only know it can if you have watched it fail once. A verifier you have never seen reject anything is an untested verifier, and it will agree with a confident summary forever.

So the practical version is not one check but a pinned pair: hash the artifact for equality, and read the completion flag from metadata for completeness. Two runs that agree on both are comparable; two that agree on neither prove nothing. The agent's own summary is generated by the same process that may have failed, so its confidence carries no information - which is exactly what the eight readers showed. None crashed. Five answers.