Skip to content

Commit

Permalink
Fixes #1837 : keep file capabilities on archival
Browse files Browse the repository at this point in the history
Signed-off-by: JACQUES Francois <[email protected]>
  • Loading branch information
hypnoce committed Dec 20, 2021
1 parent 78808c9 commit 6534a68
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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
22 changes: 22 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,23 @@ func (t *Tar) AddFileToTar(p string) error {
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)
}
xattr := "security.capability"
capability, err := system.Lgetxattr(path, xattr)
if err != nil && !errors.Is(err, syscall.EOPNOTSUPP) && err != system.ErrNotSupportedPlatform {
return errors.Wrapf(err, "failed to read %q attribute from %q", xattr, path)
}
if capability != nil {
hdr.Xattrs[xattr] = string(capability)
}
return nil
}

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

0 comments on commit 6534a68

Please sign in to comment.