imagepull + kernelcat: allow absolute symlink targets
Container (and kernel) layers routinely ship symlinks with absolute
targets — /usr/bin/mawk, /lib/modules/<ver>/build, etc. Those are
interpreted relative to the rootfs at runtime (`/` inside the VM),
not against the host filesystem, so they are rooted inside dest by
construction and need no escape check at write time.
The previous logic resolved absolute Linknames literally (against
the host root), compared to the staging dir, and rejected everything
that didn't happen to live under it. That made `banger image pull
docker.io/library/debian:bookworm` fail on the very first symlink
("etc/alternatives/awk -> /usr/bin/mawk").
Relative targets still get the traversal check — a relative
Linkname with ../s can genuinely escape dest at write time even if
in-VM resolution would be safe — so the defense against malicious
relative chains is intact.
Tests:
- TestFlattenAcceptsAbsoluteSymlink replaces the old overly-strict
test, using the exact etc/alternatives/awk -> /usr/bin/mawk case
that broke debian:bookworm.
- TestFlattenRejectsRelativeSymlinkEscape confirms relative-with-
traversal is still rejected with the same "unsafe symlink"
error.
Same fix applied in internal/kernelcat/fetch.go for consistency;
future kernel bundles with absolute symlinks in the modules tree
would otherwise hit the same wall.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
d5f72dfad9
commit
fdaf7cce0f
3 changed files with 57 additions and 22 deletions
|
|
@ -167,14 +167,15 @@ func extractTar(r io.Reader, target string) error {
|
|||
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
link := hdr.Linkname
|
||||
resolved := link
|
||||
if !filepath.IsAbs(link) {
|
||||
resolved = filepath.Join(filepath.Dir(dst), link)
|
||||
}
|
||||
resolved = filepath.Clean(resolved)
|
||||
if resolved != absTarget && !strings.HasPrefix(resolved, absTarget+string(filepath.Separator)) {
|
||||
return fmt.Errorf("unsafe symlink in tarball: %q -> %q", hdr.Name, hdr.Linkname)
|
||||
// Absolute targets are interpreted at runtime against the
|
||||
// eventual rootfs (`/` inside the VM), so they're rooted
|
||||
// inside absTarget by construction. Only relative targets
|
||||
// need an escape check at write time.
|
||||
if !filepath.IsAbs(hdr.Linkname) {
|
||||
resolved := filepath.Clean(filepath.Join(filepath.Dir(dst), hdr.Linkname))
|
||||
if resolved != absTarget && !strings.HasPrefix(resolved, absTarget+string(filepath.Separator)) {
|
||||
return fmt.Errorf("unsafe symlink in tarball: %q -> %q", hdr.Name, hdr.Linkname)
|
||||
}
|
||||
}
|
||||
if err := os.Symlink(hdr.Linkname, dst); err != nil {
|
||||
return err
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue