Skip to content

Commit 3306a4d

Browse files
authored
Merge pull request #377 from umohnani8/tests
Add new test to critest for privileged container
2 parents ac3af29 + d2c7e3d commit 3306a4d

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

pkg/validate/selinux_linux.go

+76
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package validate
1818

1919
import (
20+
"fmt"
2021
"time"
2122

2223
"github.com/kubernetes-sigs/cri-tools/pkg/framework"
@@ -84,6 +85,58 @@ var _ = framework.KubeDescribe("SELinux", func() {
8485
}
8586
_ = createContainerWithSelinux(rc, ic, sandboxID, sandboxConfig, options, false, false)
8687
})
88+
89+
It("selinux mount label should persist when container is privileged", func() {
90+
By("create pod")
91+
privileged := true
92+
podID, podConfig := createPrivilegedPodSandbox(rc, privileged)
93+
94+
By("create container for security context Privileged is true")
95+
containerID := createPrivilegedContainer(rc, ic, podID, podConfig, "container-with-isPrivileged-mount-and-process-label-test-", privileged)
96+
97+
By("start container")
98+
startContainer(rc, containerID)
99+
Eventually(func() runtimeapi.ContainerState {
100+
return getContainerStatus(rc, containerID).State
101+
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_RUNNING))
102+
103+
By("check the Privileged container")
104+
checkMountLabel(rc, containerID)
105+
})
106+
107+
It("check selinux process label for privileged and unprivileged containers", func() {
108+
By("create pod")
109+
privileged := true
110+
podID, podConfig := createPrivilegedPodSandbox(rc, privileged)
111+
112+
By("create container for security context Privileged is true")
113+
containerID := createPrivilegedContainer(rc, ic, podID, podConfig, "container-with-isPrivileged-process-label-test-", privileged)
114+
115+
By("start container")
116+
startContainer(rc, containerID)
117+
Eventually(func() runtimeapi.ContainerState {
118+
return getContainerStatus(rc, containerID).State
119+
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_RUNNING))
120+
121+
By("check the Privileged container")
122+
checkProcessLabel(rc, containerID, privileged)
123+
124+
By("create pod")
125+
privileged = false
126+
podID, podConfig = createPrivilegedPodSandbox(rc, privileged)
127+
128+
By("create container for security context Privileged is true")
129+
containerID = createPrivilegedContainer(rc, ic, podID, podConfig, "container-with-notPrivileged-process-label-test-", privileged)
130+
131+
By("start container")
132+
startContainer(rc, containerID)
133+
Eventually(func() runtimeapi.ContainerState {
134+
return getContainerStatus(rc, containerID).State
135+
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_RUNNING))
136+
137+
By("check the Privileged container")
138+
checkProcessLabel(rc, containerID, privileged)
139+
})
87140
})
88141
}
89142
})
@@ -136,3 +189,26 @@ func checkContainerSelinux(rc internalapi.RuntimeService, containerID string, sh
136189
Expect(status.GetExitCode()).NotTo(Equal(int32(0)))
137190
}
138191
}
192+
193+
func checkMountLabel(rc internalapi.RuntimeService, containerID string) {
194+
// Check that the mount label is set for privileged containers
195+
cmd := []string{"cat", "/proc/1/mountinfo"}
196+
stdout, stderr, err := rc.ExecSync(containerID, cmd, time.Duration(defaultExecSyncTimeout)*time.Second)
197+
msg := fmt.Sprintf("cmd %v, stdout %q, stderr %q", cmd, stdout, stderr)
198+
Expect(err).NotTo(HaveOccurred(), msg)
199+
Expect(string(stdout)).To(ContainSubstring("object_r:container_file_t"))
200+
}
201+
202+
func checkProcessLabel(rc internalapi.RuntimeService, containerID string, privileged bool) {
203+
// Check that the correct process label is set for privileged and unprivileged containers
204+
cmd := []string{"cat", "/proc/self/attr/current"}
205+
stdout, stderr, err := rc.ExecSync(containerID, cmd, time.Duration(defaultExecSyncTimeout)*time.Second)
206+
msg := fmt.Sprintf("cmd %v, stdout %q, stderr %q", cmd, stdout, stderr)
207+
Expect(err).NotTo(HaveOccurred(), msg)
208+
209+
if privileged {
210+
Expect(string(stdout)).To(ContainSubstring("system_r:spc_t"))
211+
} else {
212+
Expect(string(stdout)).To(ContainSubstring("system_r:container_t"))
213+
}
214+
}

0 commit comments

Comments
 (0)