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

Add new test to critest for privileged container #377

Merged
merged 1 commit into from
Sep 17, 2018
Merged
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
76 changes: 76 additions & 0 deletions pkg/validate/selinux_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package validate

import (
"fmt"
"time"

"github.com/kubernetes-sigs/cri-tools/pkg/framework"
Expand Down Expand Up @@ -84,6 +85,58 @@ var _ = framework.KubeDescribe("SELinux", func() {
}
_ = createContainerWithSelinux(rc, ic, sandboxID, sandboxConfig, options, false, false)
})

It("selinux mount label should persist when container is privileged", func() {
By("create pod")
privileged := true
podID, podConfig := createPrivilegedPodSandbox(rc, privileged)

By("create container for security context Privileged is true")
containerID := createPrivilegedContainer(rc, ic, podID, podConfig, "container-with-isPrivileged-mount-and-process-label-test-", privileged)

By("start container")
startContainer(rc, containerID)
Eventually(func() runtimeapi.ContainerState {
return getContainerStatus(rc, containerID).State
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_RUNNING))

By("check the Privileged container")
checkMountLabel(rc, containerID)
})

It("check selinux process label for privileged and unprivileged containers", func() {
By("create pod")
privileged := true
podID, podConfig := createPrivilegedPodSandbox(rc, privileged)

By("create container for security context Privileged is true")
containerID := createPrivilegedContainer(rc, ic, podID, podConfig, "container-with-isPrivileged-process-label-test-", privileged)

By("start container")
startContainer(rc, containerID)
Eventually(func() runtimeapi.ContainerState {
return getContainerStatus(rc, containerID).State
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_RUNNING))

By("check the Privileged container")
checkProcessLabel(rc, containerID, privileged)

By("create pod")
privileged = false
podID, podConfig = createPrivilegedPodSandbox(rc, privileged)

By("create container for security context Privileged is true")
containerID = createPrivilegedContainer(rc, ic, podID, podConfig, "container-with-notPrivileged-process-label-test-", privileged)

By("start container")
startContainer(rc, containerID)
Eventually(func() runtimeapi.ContainerState {
return getContainerStatus(rc, containerID).State
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_RUNNING))

By("check the Privileged container")
checkProcessLabel(rc, containerID, privileged)
})
})
}
})
Expand Down Expand Up @@ -136,3 +189,26 @@ func checkContainerSelinux(rc internalapi.RuntimeService, containerID string, sh
Expect(status.GetExitCode()).NotTo(Equal(int32(0)))
}
}

func checkMountLabel(rc internalapi.RuntimeService, containerID string) {
// Check that the mount label is set for privileged containers
cmd := []string{"cat", "/proc/1/mountinfo"}
stdout, stderr, err := rc.ExecSync(containerID, cmd, time.Duration(defaultExecSyncTimeout)*time.Second)
msg := fmt.Sprintf("cmd %v, stdout %q, stderr %q", cmd, stdout, stderr)
Expect(err).NotTo(HaveOccurred(), msg)
Expect(string(stdout)).To(ContainSubstring("object_r:container_file_t"))
}

func checkProcessLabel(rc internalapi.RuntimeService, containerID string, privileged bool) {
// Check that the correct process label is set for privileged and unprivileged containers
cmd := []string{"cat", "/proc/self/attr/current"}
stdout, stderr, err := rc.ExecSync(containerID, cmd, time.Duration(defaultExecSyncTimeout)*time.Second)
msg := fmt.Sprintf("cmd %v, stdout %q, stderr %q", cmd, stdout, stderr)
Expect(err).NotTo(HaveOccurred(), msg)

if privileged {
Expect(string(stdout)).To(ContainSubstring("system_r:spc_t"))
} else {
Expect(string(stdout)).To(ContainSubstring("system_r:container_t"))
}
}