Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,6 @@ linters-settings:
- unnamedResult
- unnecessaryBlock
gocyclo:
min-complexity: 122
min-complexity: 127
nakedret:
max-func-lines: 15
17 changes: 14 additions & 3 deletions server/container_create_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"

"github.com/intel/goresctrl/pkg/blockio"
Expand Down Expand Up @@ -282,7 +283,15 @@ func (s *Server) createSandboxContainer(ctx context.Context, ctr ctrIface.Contai
maybeRelabel = true
}

containerVolumes, ociMounts, err := addOCIBindMounts(ctx, ctr, mountLabel, s.config.RuntimeConfig.BindMountPrefix, s.config.AbsentMountSourcesToReject, maybeRelabel)
skipRelabel := false
const superPrivilegedType = "spc_t"
if securityContext.SelinuxOptions.Type == superPrivilegedType || // super privileged container
(ctr.SandboxConfig().Linux.SecurityContext.SelinuxOptions.Type == superPrivilegedType && // super privileged pod
securityContext.SelinuxOptions.Type == "") {
skipRelabel = true
}

containerVolumes, ociMounts, err := addOCIBindMounts(ctx, ctr, mountLabel, s.config.RuntimeConfig.BindMountPrefix, s.config.AbsentMountSourcesToReject, maybeRelabel, skipRelabel)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -834,7 +843,7 @@ func clearReadOnly(m *rspec.Mount) {
m.Options = append(m.Options, "rw")
}

func addOCIBindMounts(ctx context.Context, ctr ctrIface.Container, mountLabel, bindMountPrefix string, absentMountSourcesToReject []string, maybeRelabel bool) ([]oci.ContainerVolume, []rspec.Mount, error) {
func addOCIBindMounts(ctx context.Context, ctr ctrIface.Container, mountLabel, bindMountPrefix string, absentMountSourcesToReject []string, maybeRelabel, skipRelabel bool) ([]oci.ContainerVolume, []rspec.Mount, error) {
volumes := []oci.ContainerVolume{}
ociMounts := []rspec.Mount{}
containerConfig := ctr.Config()
Expand Down Expand Up @@ -944,7 +953,9 @@ func addOCIBindMounts(ctx context.Context, ctr ctrIface.Container, mountLabel, b
}

if m.SelinuxRelabel {
if err := securityLabel(src, mountLabel, false, maybeRelabel); err != nil {
if skipRelabel {
logrus.Debugf("Skipping relabel for %s because of super privileged container (type: spc_t)", src)
} else if err := securityLabel(src, mountLabel, false, maybeRelabel); err != nil {
return nil, nil, err
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/container_create_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAddOCIBindsForDev(t *testing.T) {
t.Error(err)
}

_, binds, err := addOCIBindMounts(context.Background(), ctr, "", "", nil, false)
_, binds, err := addOCIBindMounts(context.Background(), ctr, "", "", nil, false, false)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestAddOCIBindsForSys(t *testing.T) {
t.Error(err)
}

_, binds, err := addOCIBindMounts(context.Background(), ctr, "", "", nil, false)
_, binds, err := addOCIBindMounts(context.Background(), ctr, "", "", nil, false, false)
if err != nil {
t.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/label_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func securityLabel(path, secLabel string, shared, maybeRelabel bool) error {
currentLabel, err := label.FileLabel(path)
if err == nil && currentLabel == secLabel {
logrus.Debugf(
"Skipping relabel for %s, as TrySkipVolumeSELinuxRelabel is true and the label of the top level of the volume is already correct",
"Skipping relabel for %s, as TrySkipVolumeSELinuxLabel is true and the label of the top level of the volume is already correct",
path)
return nil
}
Expand Down
34 changes: 34 additions & 0 deletions test/selinux.bats
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,37 @@ function teardown() {
newlabel=$(ls -Z "$FILE" | grep -o '[a-z,_]*_u:[a-z,_]*_r:[a-z,_]*_t:[c,s,0-9,:,\,]* ')
[[ "$label" == "$newlabel" ]]
}

@test "selinux skips relabeling for super priviliged container" {
if [[ $(getenforce) != "Enforcing" ]]; then
skip "not enforcing"
fi
VOLUME="$TESTDIR"/dir
mkdir -p "$VOLUME"

# shellcheck disable=SC2012
OLDLABEL=$(ls -dZ "$VOLUME" | awk '{ printf $1 }')

start_crio

jq '.linux.security_context.selinux_options = {"type": "spc_t"}' \
"$TESTDATA"/sandbox_config.json > "$TESTDIR"/sandbox.json

jq --arg path "$VOLUME" \
'.mounts = [{
host_path: $path,
container_path: "/tmp/path",
selinux_relabel: true
}]' \
"$TESTDATA"/container_redis.json > "$TESTDIR"/container.json

pod_id=$(crictl runp "$TESTDIR"/sandbox.json)
ctr_id=$(crictl create "$pod_id" "$TESTDIR"/container.json "$TESTDIR"/sandbox.json)

crictl rm "$ctr_id"

# shellcheck disable=SC2012
NEWLABEL=$(ls -dZ "$VOLUME" | awk '{ printf $1 }')

[[ "$OLDLABEL" == "$NEWLABEL" ]]
}