Skip to content

Add tar builtins#270

Merged
mitchpaulus merged 5 commits into
mainfrom
tar-builtins
Jul 7, 2026
Merged

Add tar builtins#270
mitchpaulus merged 5 commits into
mainfrom
tar-builtins

Conversation

@mitchpaulus

Copy link
Copy Markdown
Owner

No description provided.

mitchpaulus and others added 5 commits July 7, 2026 09:46
Follow-up to the tar builtins: an adversarial sweep of the archive code
surfaced hang and path-escape vectors. Fix them, and mirror the applicable
fixes to the existing zip extractor so the two stay in lockstep. Each case is
covered by a test (Tar_test.go, Tar_unix_test.go, ZipHardening_test.go).

- Never hang: tarPack rejects fifo/device/socket entries before os.Open, and
  packing never follows source symlinks, so a symlink loop or a link to
  /dev/zero cannot block or inflate the archive.

- Write-through a pre-existing symlink in the destination is now refused:
  ensureRealParentWithinBase resolves the deepest existing ancestor via
  EvalSymlinks and rejects when it lands outside the destination root, while
  still allowing symlinks that stay inside it. This closes a path-traversal
  vector present in both the tar and zip extractors.

- Decompression-bomb cap: new optional maxBytes key (int, default 0 =
  unlimited) on the zip/tar extract option dicts bounds the total uncompressed
  bytes written per extraction via a shared streaming byteBudget.

Verified against symlink loops, fifo packing, path traversal, escaping and
pre-existing symlinks, hardlinks, truncated/garbage/empty archives, PAX/GNU
long names, and a 200MB gzip bomb. Docs and CHANGELOG updated.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Reviewed GNU tar's extract.c and its security history for hardening we lack.
Its open_output_file / maybe_recoverable logic never writes through a symlink
at the target name: it creates with O_EXCL by default and, when overwriting,
uses O_NOFOLLOW or unlinks the existing name before recreating.

We previously created regular files with O_CREATE|O_WRONLY|O_TRUNC after a
stat()-based existence check. Our pre-existing-symlink guard only resolved the
parent directory, so with overwrite enabled, extracting an entry whose
destination name was already a symlink to an outside file wrote THROUGH it and
clobbered the target (verified: dest/victim -> ../secret was overwritten).

Route both the tar and zip regular-file writers through createExtractedFile,
which opens O_CREATE|O_WRONLY|O_EXCL (never follows a symlink, even a dangling
one), and for overwrite removes the existing name itself (os.Remove unlinks the
link, not its target) then creates a fresh file atomically. skipExisting and
default-error semantics are preserved. Adds regression tests for tar and zip.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Smaller GNU-tar-derived hardenings for the extraction path:
- Restore permissions via the open file descriptor (outFile.Chmod) instead of
  os.Chmod(path), so a metadata restore can never be redirected through a
  symlink at the destination name.
- Reject an archive entry name or symlink target containing an embedded NUL
  byte before it is used as a path (belt-and-suspenders: Go's archive/tar
  refuses to encode a NUL, and the OS rejects NUL paths, but a hand-crafted
  PAX record could carry one).
- Add regression tests for escaping symlink targets (absolute and ../ forms)
  and a direct test of the NUL guard.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
GNU tar's decade of symlink-race fixes concluded that lexical/at-check-time
containment is inherently TOCTOU-prone, and it now opens every extraction path
via openat2(RESOLVE_BENEATH) so the kernel refuses escapes atomically
(CVE-2025-45582). Go 1.24+ exposes the same mechanism as os.Root.

Route all tar and zip extraction writes (extract-all, single-entry, and
subtree) through an os.Root anchored at the destination. Every Mkdir/OpenFile/
Symlink/Chmod/Remove is resolved relative to the root fd, so the kernel blocks
any path that escapes via ".." or a symlink component, race-free, while still
permitting symlinks that stay within the destination (verified). The prior
lexical normalization + containment check is kept as a cheap first layer, and
regular files are still created with O_EXCL (no write-through at the final
component). Removes the now-superseded ensureRealParentWithinBase EvalSymlinks
guard and the non-root createExtractedFile.

Behavior is unchanged for valid archives; the full test suite and the
adversarial cases (traversal, escaping and pre-existing symlinks, hardlinks,
final-component symlink overwrite, in-dest symlink) all pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mitchpaulus
mitchpaulus merged commit 3fed441 into main Jul 7, 2026
1 check passed
@mitchpaulus
mitchpaulus deleted the tar-builtins branch July 7, 2026 16:08
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

Successfully merging this pull request may close these issues.

1 participant