archive: Resolve symlinks one component at a time - #101
Open
vvoland wants to merge 1 commit into
Open
Conversation
Path resolution passed multi-component paths to os.Lstat and os.Readlink, so the kernel followed symlinks in the intermediate components. The walk never read those links, so it did not record a relative escape, and resolution depended on symlinks located outside the extraction destination. Given: dest/a -> x/escape dest/x/escape -> ../.. dest/../absolute -> /target both Unpack and UnpackLayer accepted the entry `a/absolute/file` and wrote `dest/target/file` through the absolute symlink next to dest, because only `a` was read as a link and its target `x/escape` is local. Now the walk pops one component at a time and joins it to the already-resolved prefix, so every os.Lstat and os.Readlink call observes exactly the link that is about to be followed. That prefix contains no symlink, so ".." can pop it lexically, and it stays at the root. The escaping link is recorded again and the entry above is rejected with os.Root's path-escape error. Signed-off-by: Paweł Gronowski <git@grono.dev>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #101 +/- ##
==========================================
- Coverage 65.81% 65.56% -0.25%
==========================================
Files 42 44 +2
Lines 2039 2309 +270
==========================================
+ Hits 1342 1514 +172
- Misses 519 586 +67
- Partials 178 209 +31 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
vvoland
marked this pull request as ready for review
August 3, 2026 13:26
Member
|
We should probably upstream this as well in https://github.com/containerd/continuity |
thaJeztah
reviewed
Aug 3, 2026
| resolved = next | ||
| continue | ||
| } | ||
| if fi.Mode()&os.ModeSymlink == 0 { |
Member
There was a problem hiding this comment.
As a follow-up, we should look at the Windows side of things, with go1.23 (?) no longer exposing all kind of links as symlink.
Some quick vibe-coded code to check (for windows);
func linkTarget(path string, fi os.FileInfo) (string, bool, error) {
mode := fi.Mode()
// Regular symbolic links are still reported as ModeSymlink.
if mode&os.ModeSymlink != 0 {
target, err := os.Readlink(path)
return target, true, err
}
// Since Go 1.23, junctions and other name-surrogate reparse points are
// generally reported as ModeIrregular rather than ModeSymlink.
if mode&os.ModeIrregular == 0 {
return "", false, nil
}
target, err := os.Readlink(path)
if err == nil {
return target, true, nil
}
// An irregular object that Readlink cannot interpret is not necessarily
// safe to traverse. Return the error rather than treating it as an
// ordinary directory or file.
if errors.Is(err, syscall.ERROR_NOT_A_REPARSE_POINT) ||
errors.Is(err, syscall.ERROR_INVALID_REPARSE_DATA) {
return "", false, &fs.PathError{
Op: "readlink",
Path: path,
Err: err,
}
}
return "", false, err
}And call-site;
fi, err := os.Lstat(candidate)
if err != nil {
return "", err
}
target, isLink, err := linkTarget(candidate, fi)
if err != nil {
return "", err
}
if !isLink {
// Continue with ordinary component.
continue
}
// ....
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Path resolution passed multi-component paths to os.Lstat and os.Readlink, so the kernel followed symlinks in the intermediate components. The walk never read those links, so it did not record a relative escape, and resolution depended on symlinks located outside the extraction destination.
Given:
dest/a -> x/escape dest/x/escape -> ../.. dest/../absolute -> /target
both Unpack and UnpackLayer accepted the entry
a/absolute/fileand wrotedest/target/filethrough the absolute symlink next to dest, because onlyawas read as a link and its targetx/escapeis local.Now the walk pops one component at a time and joins it to the already-resolved prefix, so every os.Lstat and os.Readlink call observes exactly the link that is about to be followed. That prefix contains no symlink, so ".." can pop it lexically, and it stays at the root. The escaping link is recorded again and the entry above is rejected with os.Root's path-escape error.