Skip to content

Reject tar hard link targets that escape the output directory - #5437

Open
shaggyinsomniac wants to merge 1 commit into
google:masterfrom
shaggyinsomniac:fix/tar-hardlink-escape
Open

Reject tar hard link targets that escape the output directory#5437
shaggyinsomniac wants to merge 1 commit into
google:masterfrom
shaggyinsomniac:fix/tar-hardlink-escape

Conversation

@shaggyinsomniac

Copy link
Copy Markdown

Fixes #5436.

What

  • TarArchiveReader.extract now resolves a hard link member's target the same way tarfile will (absolute linknames re-rooted at the output directory, relative ones joined against the member's directory) and applies the existing _is_attempting_path_traversal containment check to it.
  • Adds two regression tests: an escaping relative linkname is rejected with the victim file untouched, and legitimate in-archive hard links (canonical member-rooted linknames, built from real on-disk hard links) still extract.

Why

The member-name guard could not catch this class: os.path.realpath does not resolve hard links, so an alias created by os.link pointing outside the output directory still appears contained. Details and a standalone stdlib PoC in #5436.

Testing

pytest src/clusterfuzz/_internal/tests/core/system/archive_test.py — both new tests pass; no other tests changed behavior (one unrelated pre-existing env-sensitive zip-chmod mock test fails identically with and without this change).

TarArchiveReader.extract validated member names only. A hard link member
whose relative linkname contains '..' makes tarfile's os.link() alias an
existing file outside the output directory; a later write through the
alias overwrites it. The realpath-based check cannot detect this because
it does not resolve hard links. Mirror tarfile's link-target resolution
and apply the same containment check to it.
@shaggyinsomniac
shaggyinsomniac requested a review from a team as a code owner August 26, 2026 14:44
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.

system/archive.py: tar hard link linknames with '..' can overwrite files outside the extraction directory

1 participant