Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1837 : keep file capabilities on archival #1838

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions integration/dockerfiles/Dockerfile_test_issue_1837
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM registry.access.redhat.com/ubi8/ubi:8.2 AS BASE
# Install ping
RUN yum --disableplugin=subscription-manager install -y iputils

FROM BASE
RUN set -e && [ ! -z "$(getcap /bin/ping)" ] || exit 1
4 changes: 4 additions & 0 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ func ExtractFile(dest string, hdr *tar.Header, tr io.Reader) error {
return err
}

if err = writeSecurityXattrToToFile(path, hdr); err != nil {
return err
}

if err = setFileTimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
return err
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/tar_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/system"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -76,6 +77,10 @@ func (t *Tar) AddFileToTar(p string) error {
if err != nil {
return err
}
err = readSecurityXattrToTarHeader(p, hdr)
if err != nil {
return err
}

if p == config.RootDir {
// allow entry for / to preserve permission changes etc. (currently ignored anyway by Docker runtime)
Expand Down Expand Up @@ -116,6 +121,41 @@ func (t *Tar) AddFileToTar(p string) error {
return nil
}

const (
securityCapabilityXattr = "security.capability"
)

// writeSecurityXattrToTarHeader writes security.capability
// xattrs from a a tar header to filesystem
func writeSecurityXattrToToFile(path string, hdr *tar.Header) error {
if hdr.Xattrs == nil {
return nil
}
if capability, ok := hdr.Xattrs[securityCapabilityXattr]; ok {
err := system.Lsetxattr(path, securityCapabilityXattr, []byte(capability), 0)
if err != nil && !errors.Is(err, syscall.EOPNOTSUPP) && err != system.ErrNotSupportedPlatform {
return errors.Wrapf(err, "failed to write %q attribute to %q", securityCapabilityXattr, path)
}
}
return nil
}

// readSecurityXattrToTarHeader reads security.capability
// xattrs from filesystem to a tar header
func readSecurityXattrToTarHeader(path string, hdr *tar.Header) error {
if hdr.Xattrs == nil {
hdr.Xattrs = make(map[string]string)
}
capability, err := system.Lgetxattr(path, securityCapabilityXattr)
if err != nil && !errors.Is(err, syscall.EOPNOTSUPP) && err != system.ErrNotSupportedPlatform {
return errors.Wrapf(err, "failed to read %q attribute from %q", securityCapabilityXattr, path)
}
if capability != nil {
hdr.Xattrs[securityCapabilityXattr] = string(capability)
}
return nil
}

func (t *Tar) Whiteout(p string) error {
dir := filepath.Dir(p)
name := ".wh." + filepath.Base(p)
Expand Down