Skip to content

Commit

Permalink
Merge pull request #5792 from openshift-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…5775-to-release-1.23

[release-1.23] server: Canonize selinux label for comparison with filesystem label
  • Loading branch information
openshift-merge-robot committed Apr 8, 2022
2 parents 9ef73d4 + 57fcb08 commit db0a1aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions server/label_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"fmt"

selinux "github.com/opencontainers/selinux/go-selinux"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -11,12 +12,17 @@ import (

func securityLabel(path, secLabel string, shared, maybeRelabel bool) error {
if maybeRelabel {
currentLabel, err := label.FileLabel(path)
if err == nil && currentLabel == secLabel {
logrus.Debugf(
"Skipping relabel for %s, as TrySkipVolumeSELinuxLabel is true and the label of the top level of the volume is already correct",
path)
return nil
canonicalSecLabel, err := selinux.CanonicalizeContext(secLabel)
if err != nil {
logrus.Errorf("Canonicalize label failed %s: %v", secLabel, err)
} else {
currentLabel, err := label.FileLabel(path)
if err == nil && currentLabel == canonicalSecLabel {
logrus.Debugf(
"Skipping relabel for %s, as TrySkipVolumeSELinuxLabel is true and the label of the top level of the volume is already correct",
path)
return nil
}
}
}
if err := label.Relabel(path, secLabel, shared); err != nil && !errors.Is(err, unix.ENOTSUP) {
Expand Down
2 changes: 1 addition & 1 deletion test/selinux.bats
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function teardown() {
create_runtime_with_allowed_annotation "selinux" "io.kubernetes.cri-o.TrySkipVolumeSELinuxLabel"
start_crio

jq ' .linux.security_context.selinux_options = {"level": "s0:c100,c200"}
jq ' .linux.security_context.selinux_options = {"level": "s0:c200,c100"}
| .annotations["io.kubernetes.cri-o.TrySkipVolumeSELinuxLabel"] = "true"' \
"$TESTDATA"/sandbox_config.json > "$TESTDIR"/sandbox.json

Expand Down

0 comments on commit db0a1aa

Please sign in to comment.