archive: cache parent directory fd during tar extraction#26
Open
ctalledo wants to merge 4 commits into
Open
Conversation
0043278 to
c70cd3d
Compare
The Unpack and UnpackLayer functions validated tar entry paths using a pure string check (filepath.Join + filepath.Rel). This does not follow symlinks, so a malicious archive could plant a symlink pointing outside the extraction root and then write files through it, bypassing the check. On Linux this is mitigated by chrootarchive wrapping extraction in a chroot(2) call. On platforms without chroot support (Windows) and for callers that bypass chrootarchive (e.g. BuildKit ADD --unpack), the extraction root is not enforced. Introduce safeResolve (ported from containerd/continuity/fs.RootPath), which walks each path component with os.Lstat and resolves symlinks within the extraction root, bounding absolute targets and relative targets that would escape root back inside it. Apply safeResolve to: - Unpack: main path computation and the deferred directory chtimes loop - UnpackLayer: same - createTarFile TypeLink: hardlink target resolution Add a regression test for the symlink-chain bypass: a within-dest symlink (go_up -> "..") used to redirect a second symlink outside dest (escape -> "../victim"), which the static check missed. Fixes ART-225. Signed-off-by: Cesar Talledo <cesar.talledo@docker.com>
os.Root, used to bound all tar extraction operations within the extraction root at the kernel level using openat(2) semantics, was introduced in Go 1.24. Update go.mod and the CI test matrix to require 1.24 as the oldest supported version. Signed-off-by: Cesar Talledo <cesar.talledo@docker.com>
Replace the path-string-based extraction with os.Root, which bounds all file operations within the extraction root at the kernel level using openat(2) semantics. This is the primary defence against tar path-traversal attacks (ART-226). Key changes: - Unpack and UnpackLayer open an os.Root for dest at entry. - createTarFile takes *os.Root and a root-relative name instead of an absolute path and extractDir. - TypeDir, TypeReg, TypeLink, Lchown, Chmod, and Chtimes all go through root.* methods. - For operations os.Root does not yet support (mknod, xattrs, lchtimes on symlinks), absPath is derived via safeResolve so paths remain bounded within the root. - overlayWhiteoutConverter.ConvertRead makes direct unix.Setxattr and unix.Mknod syscalls and requires an absolute path; pass safeResolve'd absPath rather than the root-relative hdr.Name. - safepath.go is retained: safeResolve is still used for absPath derivation. - testBreakout updated to skip symlinks: correct under the os.Root model, where symlink nodes may have out-of-root targets but traversal through them via root.* is rejected by the kernel. Signed-off-by: Cesar Talledo <cesar.talledo@docker.com>
Each root.*(path) call re-walks every path component via doInRoot, costing 2D syscalls per call at path depth D. A typical file entry triggers ~5 root.* calls, so at depth 4 that is ~40 syscalls in path traversal alone. Add a dirCache that keeps one parent-directory fd open between entries. Consecutive entries in the same directory reuse the cached fd for all *at(2) operations, reducing path-traversal cost to ~5 syscalls per entry regardless of depth. Signed-off-by: Cesar Talledo <cesar.talledo@docker.com>
c70cd3d to
33dca5a
Compare
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.
Summary
Depends on #25.
Each
root.*(path)call re-walks every path component viadoInRoot, costing 2D syscalls per call at path depth D. A typical file entry triggers ~5root.*calls, so at depth 4 that is ~40 syscalls in path traversal alone.This PR adds a
dirCachethat keeps one parent-directory fd open between entries. Consecutive entries in the same directory reuse the cached fd for all*at(2)operations, reducing path-traversal cost to ~5 syscalls per entry regardless of depth.Test plan
go test ./...passes