fix Arbitrary file write extracting an archive containing symbolic links on common() #7982
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.
elastic-agent/dev-tools/mage/common.go
Line 493 in 2a274c2
fix the issue need to ensure that the target of the symbolic link (
header.Linkname
) is resolved and validated before creating the symlink. Specifically:filepath.EvalSymlinks
to resolve any symbolic links in the target path (header.Linkname
).destinationDir
to prevent directory traversal attacks.The changes will be made in the
untar
function, specifically in the case handlingtar.TypeSymlink
.Extracting symbolic links from a malicious zip archive, without validating that the destination file path is within the destination directory, can cause files outside the destination directory to be overwritten. This can happen if there are previously-extracted symbolic links or directory traversal elements and links (
..
) in archive paths. This problem is related to the ZipSlip vulnerability which is detected by thego/zipslip
query; please see that query's help for more general information about malicious archive file vulnerabilities. This query considers the specific case where symbolic links are extracted from an archive, in which case the extraction code must be aware of existing symbolic links when checking whether it is about to extract a link pointing to a location outside the target extraction directory.Recommendation
Ensure that output paths constructed from zip archive entries are validated. This includes resolving any previously extracted symbolic links, using
path/filepath.EvalSymlinks
, to prevent writing files or links to unexpected locations.POC
links are extracted from an archive using the syntactic
filepath.Rel
function to check whether the link and its target fall within the destination directory. However, the extraction code doesn't resolve previously-extracted links, so a pair of links likesubdir/parent -> ..
followed byescape -> subdir/parent/.. -> subdir/../..
leaves a link pointing to the parent of the archive root. The syntacticRel
is ineffective because it equatessubdir/parent/..
withsubdir/
, but this is not the case whensubdir/parent
is a symbolic link.To fix this vulnerability, resolve pre-existing symbolic links before checking that the link's target is acceptable:
References
Zip Slip Vulnerability
Path Traversal
CWE-22
Checklist
./changelog/fragments
using the changelog tool