diff --git a/libpod/container_internal.go b/libpod/container_internal.go index 48c5c970ca..90f9234aa3 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -2514,7 +2514,7 @@ func (c *Container) extractSecretToCtrStorage(secr *ContainerSecret) error { if err := os.Chmod(secretFile, os.FileMode(secr.Mode)); err != nil { return err } - if err := label.Relabel(secretFile, c.config.MountLabel, false); err != nil { + if err := c.relabel(secretFile, c.config.MountLabel, false); err != nil { return err } return nil diff --git a/libpod/container_internal_common.go b/libpod/container_internal_common.go index 81b0fbf337..fc5978942c 100644 --- a/libpod/container_internal_common.go +++ b/libpod/container_internal_common.go @@ -869,7 +869,7 @@ func (c *Container) mountNotifySocket(g generate.Generator) error { return fmt.Errorf("unable to create notify %q dir: %w", notifyDir, err) } } - if err := label.Relabel(notifyDir, c.MountLabel(), true); err != nil { + if err := c.relabel(notifyDir, c.MountLabel(), true); err != nil { return fmt.Errorf("relabel failed %q: %w", notifyDir, err) } logrus.Debugf("Add bindmount notify %q dir", notifyDir) @@ -2288,7 +2288,7 @@ func (c *Container) bindMountRootFile(source, dest string) error { if err := os.Chown(source, c.RootUID(), c.RootGID()); err != nil { return err } - if err := label.Relabel(source, c.MountLabel(), false); err != nil { + if err := c.relabel(source, c.MountLabel(), false); err != nil { return err } @@ -2824,7 +2824,7 @@ func (c *Container) createSecretMountDir(runPath string) error { if err := umask.MkdirAllIgnoreUmask(src, os.FileMode(0o755)); err != nil { return err } - if err := label.Relabel(src, c.config.MountLabel, false); err != nil { + if err := c.relabel(src, c.config.MountLabel, false); err != nil { return err } if err := os.Chown(src, c.RootUID(), c.RootGID()); err != nil { @@ -2927,7 +2927,12 @@ func (c *Container) relabel(src, mountLabel string, shared bool) error { return nil } } - return label.Relabel(src, mountLabel, shared) + err := label.Relabel(src, mountLabel, shared) + if errors.Is(err, unix.ENOTSUP) { + logrus.Debugf("Labeling not supported on %q", src) + return nil + } + return err } func (c *Container) ChangeHostPathOwnership(src string, recurse bool, uid, gid int) error { diff --git a/libpod/networking_linux.go b/libpod/networking_linux.go index 601d9a49bf..592f55cb24 100644 --- a/libpod/networking_linux.go +++ b/libpod/networking_linux.go @@ -440,7 +440,10 @@ func (r *Runtime) GetRootlessNetNs(new bool) (*RootlessNetNS, error) { // this is important, otherwise the iptables command will fail err = label.Relabel(runDir, "system_u:object_r:iptables_var_run_t:s0", false) if err != nil { - return nil, fmt.Errorf("could not create relabel rootless-netns run directory: %w", err) + if !errors.Is(err, unix.ENOTSUP) { + return nil, fmt.Errorf("could not create relabel rootless-netns run directory: %w", err) + } + logrus.Debugf("Labeling not supported on %q", runDir) } // create systemd run directory err = os.MkdirAll(filepath.Join(runDir, "systemd"), 0700) diff --git a/libpod/util.go b/libpod/util.go index ed7c1260f6..106925b258 100644 --- a/libpod/util.go +++ b/libpod/util.go @@ -6,6 +6,7 @@ package libpod import ( "bufio" "encoding/binary" + "errors" "fmt" "io" "net/http" @@ -23,6 +24,7 @@ import ( spec "github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/selinux/go-selinux/label" "github.com/sirupsen/logrus" + "golang.org/x/sys/unix" ) // FuncTimer helps measure the execution time of a function @@ -273,6 +275,10 @@ func writeStringToPath(path, contents, mountLabel string, uid, gid int) error { } // Relabel runDirResolv for the container if err := label.Relabel(path, mountLabel, false); err != nil { + if errors.Is(err, unix.ENOTSUP) { + logrus.Debugf("Labeling not supported on %q", path) + return nil + } return err } diff --git a/libpod/util_linux.go b/libpod/util_linux.go index ac5fdeeb1b..0c11fba018 100644 --- a/libpod/util_linux.go +++ b/libpod/util_linux.go @@ -4,6 +4,7 @@ package libpod import ( + "errors" "fmt" "os" "path/filepath" @@ -146,7 +147,7 @@ func LabelVolumePath(path, mountLabel string) error { } if err := lvpRelabel(path, mountLabel, true); err != nil { - if err == syscall.ENOTSUP { + if errors.Is(err, unix.ENOTSUP) { logrus.Debugf("Labeling not supported on %q", path) } else { return fmt.Errorf("setting selinux label for %s to %q as shared: %w", path, mountLabel, err) diff --git a/test/system/410-selinux.bats b/test/system/410-selinux.bats index a8b0fbc604..c7d71643b4 100644 --- a/test/system/410-selinux.bats +++ b/test/system/410-selinux.bats @@ -355,4 +355,31 @@ EOF is "$output" "$user:system_r:container_t:$level" "Confined with role override label Correctly" } +@test "podman selinux: check unsupported relabel" { + skip_if_no_selinux + skip_if_rootless + + LABEL="system_u:object_r:tmp_t:s0" + RELABEL="system_u:object_r:container_file_t:s0" + tmpdir=$PODMAN_TMPDIR/vol + mkdir -p $tmpdir + + mount --type tmpfs -o "context=\"$LABEL\"" tmpfs $tmpdir + + run ls -dZ ${tmpdir} + is "$output" "${LABEL} ${tmpdir}" "No Relabel Correctly" + run_podman run --rm -v $tmpdir:/test:z --privileged $IMAGE true + run ls -dZ $tmpdir + is "$output" "${LABEL} $tmpdir" "Ignored shared relabel Correctly" + + run_podman run --rm -v $tmpdir:/test:Z --privileged $IMAGE true + run ls -dZ $tmpdir + is "$output" "${LABEL} $tmpdir" "Ignored private relabel Correctly"} + umount $tmpdir + + run_podman run --rm -v $tmpdir:/test:z --privileged $IMAGE true + run ls -dZ $tmpdir + is "$output" "${RELABEL} $tmpdir" "Ignored private relabel Correctly"} +} + # vim: filetype=sh