Skip to content

fix(file): resolve tar hard link targets from the archive root (#1510) - #1572

Merged
TerryHowe merged 1 commit into
oras-project:v2from
TerryHowe:fix/v2-tar-hardlink-root
Oct 14, 2026
Merged

TerryHowe merged 1 commit into
oras-project:v2from
TerryHowe:fix/v2-tar-hardlink-root

Conversation

@TerryHowe

Copy link
Copy Markdown
Member

Backport of #1510 to v2. It is a clean cherry-pick of ae73b33.

On v2, extractTarDirectory resolves a hard link's Linkname against the link's own directory. A hard link whose target is in another directory therefore fails to extract with "no such file or directory" (#1499).

v2 also passes the target to os.Link without checking whether it is a symlink. On platforms where link(2) follows a symlink target, such as macOS, a symlink already in the extraction directory could be used to hard link a file from outside it. main refuses a symlink target, and this backport brings that check to v2.

Change

extractHardLink resolves Linkname from the archive root with resolveRelToBase, then runs checkSymlinkEscape on the target. It refuses a target that is missing or is itself a symlink.

Tests

These are the unit tests and the store test from #1510. go test -race ./content/file/..., go vet and gofmt all pass.

…project#1510)

A tar hard link's Linkname is a path from the archive root, the same
convention as the entry Name. extractTarDirectory resolved it against the
directory of the link, so a hard link whose target sits in another
directory failed to extract with "no such file or directory". The join
came from the fix for GHSA-fxhp-mv3v-67qp, which was right to stop
passing the raw relative path to os.Link but picked the symlink base.

Resolve the target with resolveRelToBase against the extraction root, the
same way entry names are resolved. The containment checks stay: the
target must be inside the root, the symlink-escape check still runs on
it, and a target that is itself a symlink is refused because link(2) may
follow it on some platforms.

Signed-off-by: Gaurav Jadhav <[email protected]>
Co-authored-by: Terry Howe <[email protected]>
(cherry picked from commit ae73b33)
Signed-off-by: Terry Howe <[email protected]>
@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.21%. Comparing base (1788a6d) to head (9d11d2c).

Files with missing lines Patch % Lines
content/file/utils.go 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##               v2    #1572      +/-   ##
==========================================
+ Coverage   89.09%   89.21%   +0.12%     
==========================================
  Files          64       64              
  Lines        4950     4959       +9     
==========================================
+ Hits         4410     4424      +14     
+ Misses        531      526       -5     
  Partials        9        9              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rohilalala Rohilalala left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a look at this as a backport. It's a clean cherry-pick of ae73b33 (only the test imports differ), and the helpers it depends on (resolveRelToBase, checkSymlinkEscape, realPathForWrite) are identical on v2 and main, they just live in file.go here. Tests and vet pass with -race.

I also threw a few nasty archives at extractTarDirectory on macOS: ../ escapes, absolute targets outside the root, a link through a symlinked dir pointing outside, and links to an in-tree symlink, a directory, a later entry and itself. All refused. Cross-directory links and ./-prefixed names extract fine.

Small side note, not for this PR: none of those refusals match errors.Is(err, ErrPathTraversalDisallowed). The "is outside of" errors come from resolveRelToBase and the symlink one is a plain error, so callers can't tell a traversal attempt from a normal I/O error. Same on main, so maybe a follow-up if you think it's worth it.

LGTM 👍

@shizhMSFT shizhMSFT left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TerryHowe
TerryHowe merged commit 03dcea5 into oras-project:v2 Oct 14, 2026
9 checks passed
@TerryHowe
TerryHowe deleted the fix/v2-tar-hardlink-root branch October 14, 2026 14:04
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.

4 participants