Repository navigation
fix(file): resolve tar hard link targets from the archive root (#1510) - #1572
Conversation
…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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Rohilalala
left a comment
There was a problem hiding this comment.
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 👍
Backport of #1510 to
v2. It is a clean cherry-pick of ae73b33.On
v2,extractTarDirectoryresolves a hard link'sLinknameagainst 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).v2also passes the target toos.Linkwithout 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.mainrefuses a symlink target, and this backport brings that check tov2.Change
extractHardLinkresolvesLinknamefrom the archive root withresolveRelToBase, then runscheckSymlinkEscapeon 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 vetandgofmtall pass.