@@ -17,6 +17,7 @@ limitations under the License.
17
17
package validate
18
18
19
19
import (
20
+ "fmt"
20
21
"time"
21
22
22
23
"github.com/kubernetes-sigs/cri-tools/pkg/framework"
@@ -84,6 +85,58 @@ var _ = framework.KubeDescribe("SELinux", func() {
84
85
}
85
86
_ = createContainerWithSelinux (rc , ic , sandboxID , sandboxConfig , options , false , false )
86
87
})
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
+ })
87
140
})
88
141
}
89
142
})
@@ -136,3 +189,26 @@ func checkContainerSelinux(rc internalapi.RuntimeService, containerID string, sh
136
189
Expect (status .GetExitCode ()).NotTo (Equal (int32 (0 )))
137
190
}
138
191
}
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