Skip to content

"invalid tar header" / "unexpected EOF" scanning docker-save archives with mixed layer compression (uncompressed layer 0 + gzip later layers) #11748

Not planned

Description

@junjae79

Description

trivy image --input <file> fails with archive/tar: invalid tar header (or unexpected EOF for small layers) when a docker-save archive contains layers with mixed compression: layer 0 is an uncompressed tar blob while a later layer blob is gzip — even though every layer's mediaType is declared correctly and every blob is individually valid.

Reversing the order (gzip layer first, uncompressed layer later) scans fine. This asymmetry suggests the tarball reading path decides compression once (from the first layer) and applies it to all layers, instead of detecting per layer. When the first layer is uncompressed, subsequent gzip blobs are parsed as raw tar and fail at the first header block (large blob → invalid tar header; blob smaller than ~1.5 tar blocks → unexpected EOF).

This is not a synthetic-only situation. Docker Engine with the containerd image store (default for fresh installs since v28/v29) exports each layer blob in whatever form it is stored:

  • base images imported via docker load (common in air-gapped environments) keep uncompressed layer representations;
  • locally built layers (classic builder and BuildKit alike) are committed gzip.

docker save of such an image produces exactly the failing mix (first layers uncompressed, later layers gzip). Both --input file.tar and the daemon path (--image-src docker, which streams the same export) fail. GNU tar, docker load, and gzip -t all handle the same archive without complaint.

Probably related: #10338 (identical error signature on a pulled base image; closed without root-cause resolution).

Desired Behavior

Layer compression is detected per layer (e.g. magic-byte peek per blob, or honoring each layer's declared mediaType), so mixed-compression docker-save archives scan successfully.

Actual Behavior

FATAL  Fatal error  run error: image scan error: scan error: scan failed: failed analysis:
  analyze error: pipeline error: failed to analyze layer
  (sha256:<diff_id of the gzip layer>): walk error: failed to extract the archive:
  archive/tar: invalid tar header

For a gzip layer blob smaller than ~1.5 tar blocks the error is unexpected EOF instead.

Reproduction Steps

No docker needed — the following python3 script synthesizes two docker-save-style archives that differ only in layer order:

import gzip, hashlib, io, json, os, tarfile

def sha(b): return hashlib.sha256(b).hexdigest()

def mktar(name, size):
    buf = io.BytesIO()
    with tarfile.open(fileobj=buf, mode="w", format=tarfile.GNU_FORMAT) as t:
        d = os.urandom(size)
        ti = tarfile.TarInfo(name); ti.size = len(d); ti.mode = 0o644
        t.addfile(ti, io.BytesIO(d))
    return buf.getvalue()

def gz(b):
    o = io.BytesIO()
    with gzip.GzipFile(fileobj=o, mode="wb", mtime=0) as g: g.write(b)
    return o.getvalue()

tarA = mktar("etc/base.txt", 4096)        # uncompressed layer
tarB = mktar("app/app.bin", 1024 * 512)   # gzip layer

def build(out, layers):  # layers: [(blob, diff_id, mediaType)]
    cfg = json.dumps({"architecture": "amd64", "os": "linux", "config": {},
        "rootfs": {"type": "layers", "diff_ids": ["sha256:" + d for _, d, _ in layers]},
        "history": [{"created": "2026-01-01T00:00:00Z", "created_by": "x"} for _ in layers]},
        indent=3).encode()
    cfg_dig = sha(cfg)
    mani = json.dumps({"schemaVersion": 2,
        "mediaType": "application/vnd.oci.image.manifest.v1+json",
        "config": {"mediaType": "application/vnd.oci.image.config.v1+json",
                   "digest": "sha256:" + cfg_dig, "size": len(cfg)},
        "layers": [{"mediaType": mt, "digest": "sha256:" + sha(b), "size": len(b)}
                   for b, _, mt in layers]}, indent=3).encode()
    mani_dig = sha(mani)
    index = json.dumps({"schemaVersion": 2,
        "mediaType": "application/vnd.oci.image.index.v1+json",
        "manifests": [{"mediaType": "application/vnd.oci.image.manifest.v1+json",
                       "digest": "sha256:" + mani_dig, "size": len(mani),
                       "annotations": {"org.opencontainers.image.ref.name": "repro:1"}}]},
        indent=3).encode()
    legacy = json.dumps([{"Config": "blobs/sha256/" + cfg_dig, "RepoTags": ["repro:1"],
                          "Layers": ["blobs/sha256/" + sha(b) for b, _, _ in layers]}]).encode()
    with tarfile.open(out, "w", format=tarfile.GNU_FORMAT) as t:
        def add(n, b):
            ti = tarfile.TarInfo(n); ti.size = len(b); ti.mode = 0o644
            t.addfile(ti, io.BytesIO(b))
        add("oci-layout", b'{"imageLayoutVersion":"1.0.0"}')
        add("index.json", index); add("manifest.json", legacy)
        add("blobs/sha256/" + cfg_dig, cfg); add("blobs/sha256/" + mani_dig, mani)
        for b, _, _ in layers: add("blobs/sha256/" + sha(b), b)
    print("built", out)

T = "application/vnd.oci.image.layer.v1.tar"
G = "application/vnd.oci.image.layer.v1.tar+gzip"
# FAILS: uncompressed first, gzip second
build("mix_fail.tar", [(tarA, sha(tarA), T), (gz(tarB), sha(tarB), G)])
# PASSES: same layers, gzip first
build("mix_pass.tar", [(gz(tarB), sha(tarB), G), (tarA, sha(tarA), T)])
$ python3 make_repro.py
$ trivy image --input mix_fail.tar --scanners secret
FATAL ... failed to analyze layer (sha256:...): walk error: failed to extract the archive: archive/tar: invalid tar header
$ trivy image --input mix_pass.tar --scanners secret
<scans clean>

Notes from further bisecting (all of these pass, so they are ruled out): a gzip blob whose mediaType declares uncompressed v1.tar, trailing garbage after the gzip stream, concatenated gzip members, missing tar end-of-archive blocks, manifest size field mismatches, application/vnd.docker.image.rootfs.diff.tar.gzip inside an OCI manifest. Only the "uncompressed first layer + gzip later layer" combination fails.

Real-world chain (verified on Docker Engine 29.3.1 and 29.9.3, containerd image store, driver: overlayfs): base image brought in with docker load (uncompressed blobs) + image built FROM it (classic builder or BuildKit commit gzip blobs) → docker save emits uncompressed-first mixed archive → trivy image fails on both --input and --image-src docker, while docker load, GNU tar and gzip -t all accept the archive.

Target

Container Image

Scanner

Vulnerability (also reproducible with Secret scanner alone, so no DB is needed)

Output Format

Table

Mode

Standalone

Operating System

Linux (reproduced in container; also on Windows host)

Version

Version: 0.76.2

Activity

  1. github-actions commented on Nov 26, 2026

    @github-actions
    Contributor
  2. junjae79 commented on Nov 26, 2026

    @junjae79
    Author

    Refiled as a Bugs discussion per the contribution policy: #11749

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions