forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_features_test.go
403 lines (327 loc) · 15.8 KB
/
security_features_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2019 Red Hat, Inc.
*
*/
package tests_test
import (
"context"
"fmt"
"strings"
"time"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libmigration"
"kubevirt.io/kubevirt/pkg/virt-operator/resource/generate/components"
"kubevirt.io/kubevirt/tests/libnode"
"kubevirt.io/kubevirt/tests/decorators"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
k8sv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubevirt.io/kubevirt/tests/exec"
"kubevirt.io/kubevirt/tests/framework/checks"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
"kubevirt.io/kubevirt/tests/testsuite"
"kubevirt.io/kubevirt/tests/util"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/console"
cd "kubevirt.io/kubevirt/tests/containerdisk"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libvmifact"
"kubevirt.io/kubevirt/tests/libwait"
)
const (
capSysNice k8sv1.Capability = "SYS_NICE"
capNetBindService k8sv1.Capability = "NET_BIND_SERVICE"
)
var _ = Describe("[Serial][sig-compute]SecurityFeatures", Serial, decorators.SigCompute, func() {
var err error
var virtClient kubecli.KubevirtClient
BeforeEach(func() {
virtClient = kubevirt.Client()
})
Context("Check virt-launcher securityContext", func() {
var kubevirtConfiguration *v1.KubeVirtConfiguration
BeforeEach(func() {
kv := util.GetCurrentKv(virtClient)
kubevirtConfiguration = &kv.Spec.Configuration
})
var container k8sv1.Container
var vmi *v1.VirtualMachineInstance
Context("With selinuxLauncherType as container_t", func() {
BeforeEach(func() {
config := kubevirtConfiguration.DeepCopy()
config.SELinuxLauncherType = "container_t"
tests.UpdateKubeVirtConfigValueAndWait(*config)
vmi = libvmifact.NewCirros()
// VMIs with selinuxLauncherType container_t cannot have network interfaces, since that requires
// the `virt_launcher.process` selinux context
autoattachPodInterface := false
vmi.Spec.Domain.Devices.AutoattachPodInterface = &autoattachPodInterface
vmi.Spec.Domain.Devices.Interfaces = []v1.Interface{}
vmi.Spec.Networks = []v1.Network{}
})
It("[test_id:2953]Ensure virt-launcher pod securityContext type is correctly set", func() {
By("Starting a VirtualMachineInstance")
vmi, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).Create(context.Background(), vmi, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
libwait.WaitForSuccessfulVMIStart(vmi)
By("Check virt-launcher pod SecurityContext values")
vmiPod, err := libpod.GetPodByVirtualMachineInstance(vmi, vmi.Namespace)
Expect(err).NotTo(HaveOccurred())
Expect(vmiPod.Spec.SecurityContext.SELinuxOptions).To(Equal(&k8sv1.SELinuxOptions{Type: "container_t"}))
})
It("[test_id:2895]Make sure the virt-launcher pod is not priviledged", func() {
By("Starting a VirtualMachineInstance")
vmi, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).Create(context.Background(), vmi, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
libwait.WaitForSuccessfulVMIStart(vmi)
By("Check virt-launcher pod SecurityContext values")
vmiPod, err := libpod.GetPodByVirtualMachineInstance(vmi, vmi.Namespace)
Expect(err).NotTo(HaveOccurred())
for _, containerSpec := range vmiPod.Spec.Containers {
if containerSpec.Name == "compute" {
container = containerSpec
break
}
}
Expect(*container.SecurityContext.Privileged).To(BeFalse())
})
It("[test_id:4297]Make sure qemu processes are MCS constrained", func() {
By("Starting a VirtualMachineInstance")
vmi, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).Create(context.Background(), vmi, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
libwait.WaitForSuccessfulVMIStart(vmi)
emulator, err := tests.GetRunningVMIEmulator(vmi)
Expect(err).ToNot(HaveOccurred())
emulator = "[/]" + strings.TrimPrefix(emulator, "/")
pod, err := libpod.GetPodByVirtualMachineInstance(vmi, vmi.Namespace)
Expect(err).NotTo(HaveOccurred())
qemuProcessSelinuxContext, err := exec.ExecuteCommandOnPod(
pod,
"compute",
[]string{"/usr/bin/bash", "-c", fmt.Sprintf("ps -efZ | grep %s | awk '{print $1}'", emulator)},
)
Expect(err).ToNot(HaveOccurred())
By("Checking that qemu process is of the SELinux type container_t")
Expect(strings.Split(qemuProcessSelinuxContext, ":")[2]).To(Equal("container_t"))
By("Checking that qemu process has SELinux category_set")
Expect(strings.Split(qemuProcessSelinuxContext, ":")).To(HaveLen(5))
err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Delete(context.Background(), vmi.Name, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
})
})
Context("With selinuxLauncherType defined as spc_t", func() {
It("[test_id:3787]Should honor custom SELinux type for virt-launcher", func() {
config := kubevirtConfiguration.DeepCopy()
superPrivilegedType := "spc_t"
config.SELinuxLauncherType = superPrivilegedType
tests.UpdateKubeVirtConfigValueAndWait(*config)
vmi = tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskAlpine))
vmi.Namespace = testsuite.NamespacePrivileged
By("Starting a New VMI")
vmi, err = virtClient.VirtualMachineInstance(testsuite.NamespacePrivileged).Create(context.Background(), vmi, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
libwait.WaitForSuccessfulVMIStart(vmi)
By("Ensuring VMI is running by logging in")
libwait.WaitUntilVMIReady(vmi, console.LoginToAlpine)
By("Fetching virt-launcher Pod")
pod, err := libpod.GetPodByVirtualMachineInstance(vmi, testsuite.NamespacePrivileged)
Expect(err).ToNot(HaveOccurred())
By("Verifying SELinux context contains custom type")
Expect(pod.Spec.SecurityContext.SELinuxOptions.Type).To(Equal(superPrivilegedType))
By("Deleting the VMI")
err = virtClient.VirtualMachineInstance(testsuite.NamespacePrivileged).Delete(context.Background(), vmi.Name, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
})
})
Context("With selinuxLauncherType defined as virt_launcher.process", func() {
It("[test_id:4298]qemu process type is virt_launcher.process, when selinuxLauncherType is virt_launcher.process", decorators.CustomSELinux, func() {
config := kubevirtConfiguration.DeepCopy()
launcherType := "virt_launcher.process"
config.SELinuxLauncherType = launcherType
tests.UpdateKubeVirtConfigValueAndWait(*config)
vmi = tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskAlpine))
vmi.Namespace = testsuite.NamespacePrivileged
By("Starting a New VMI")
vmi, err = virtClient.VirtualMachineInstance(testsuite.NamespacePrivileged).Create(context.Background(), vmi, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
libwait.WaitForSuccessfulVMIStart(vmi)
By("Ensuring VMI is running by logging in")
libwait.WaitUntilVMIReady(vmi, console.LoginToAlpine)
By("Fetching virt-launcher Pod")
emulator, err := tests.GetRunningVMIEmulator(vmi)
Expect(err).ToNot(HaveOccurred())
emulator = "[/]" + strings.TrimPrefix(emulator, "/")
pod, err := libpod.GetPodByVirtualMachineInstance(vmi, testsuite.NamespacePrivileged)
Expect(err).ToNot(HaveOccurred())
qemuProcessSelinuxContext, err := exec.ExecuteCommandOnPod(
pod,
"compute",
[]string{"/usr/bin/bash", "-c", fmt.Sprintf("ps -efZ | grep %s | awk '{print $1}'", emulator)},
)
Expect(err).ToNot(HaveOccurred())
By("Checking that qemu process is of the SELinux type virt_launcher.process")
Expect(strings.Split(qemuProcessSelinuxContext, ":")[2]).To(Equal(launcherType))
By("Verifying SELinux context contains custom type in pod")
Expect(pod.Spec.SecurityContext.SELinuxOptions.Type).To(Equal(launcherType))
By("Deleting the VMI")
err = virtClient.VirtualMachineInstance(testsuite.NamespacePrivileged).Delete(context.Background(), vmi.Name, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
})
})
})
Context("Check virt-launcher capabilities", func() {
var container k8sv1.Container
var vmi *v1.VirtualMachineInstance
It("[test_id:4300]has precisely the documented extra capabilities relative to a regular user pod", func() {
vmi = tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskAlpine))
By("Starting a New VMI")
vmi, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).Create(context.Background(), vmi, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
libwait.WaitForSuccessfulVMIStart(vmi)
By("Ensuring VMI is running by logging in")
libwait.WaitUntilVMIReady(vmi, console.LoginToAlpine)
By("Fetching virt-launcher Pod")
pod, err := libpod.GetPodByVirtualMachineInstance(vmi, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
for _, containerSpec := range pod.Spec.Containers {
if containerSpec.Name == "compute" {
container = containerSpec
break
}
}
caps := *container.SecurityContext.Capabilities
if !checks.HasFeature(virtconfig.Root) {
Expect(caps.Add).To(HaveLen(1), fmt.Sprintf("Found capabilities %s, expected NET_BIND_SERVICE", caps.Add))
Expect(caps.Add).To(ContainElement(k8sv1.Capability("NET_BIND_SERVICE")))
} else {
Expect(caps.Add).To(HaveLen(2), fmt.Sprintf("Found capabilities %s, expected NET_BIND_SERVICE and SYS_NICE", caps.Add))
Expect(caps.Add).To(ContainElements(k8sv1.Capability("NET_BIND_SERVICE"), k8sv1.Capability("SYS_NICE")))
}
By("Checking virt-launcher Pod's compute container has precisely the documented extra capabilities")
for _, capa := range caps.Add {
Expect(isLauncherCapabilityValid(capa)).To(BeTrue(), "Expected compute container of virt_launcher to be granted only specific capabilities")
}
if !checks.HasFeature(virtconfig.Root) {
By("Checking virt-launcher Pod's compute container has precisely the documented dropped capabilities")
Expect(caps.Drop).To(HaveLen(1))
Expect(caps.Drop[0]).To(Equal(k8sv1.Capability("ALL")), "Expected compute container of virt_launcher to drop all caps")
}
})
})
Context("Disabling the custom SELinux policy", func() {
var policyRemovedByTest = false
AfterEach(func() {
if policyRemovedByTest {
By("Re-installing custom SELinux policy on all nodes")
err = runOnAllSchedulableNodes(virtClient, []string{"cp", "/var/run/kubevirt/virt_launcher.cil", "/proc/1/root/tmp/"}, "")
// That file may not be deployed on clusters that don't need the policy anymore
if err == nil {
err = runOnAllSchedulableNodes(virtClient, []string{"chroot", "/proc/1/root", "semodule", "-i", "/tmp/virt_launcher.cil"}, "")
Expect(err).ToNot(HaveOccurred())
err = runOnAllSchedulableNodes(virtClient, []string{"rm", "-f", "/proc/1/root/tmp/virt_launcher.cil"}, "")
Expect(err).ToNot(HaveOccurred())
}
}
})
It("Should prevent virt-handler from installing the custom policy", func() {
By("Removing custom SELinux policy from all nodes")
// The policy may or may not be installed on the node, regardless of the feature gate value,
// since the feature gate could have been enabled after deployment. Use error as indication of removal.
err = runOnAllSchedulableNodes(virtClient, []string{"chroot", "/proc/1/root", "semodule", "-r", "virt_launcher"}, "")
policyRemovedByTest = err == nil
By("Disabling the custom policy by adding the corresponding feature gate")
tests.EnableFeatureGate(virtconfig.DisableCustomSELinuxPolicy)
By("Ensuring the custom SELinux policy is absent from all nodes")
Consistently(func() error {
return runOnAllSchedulableNodes(virtClient, []string{"chroot", "/proc/1/root", "semodule", "-l"}, "virt_launcher")
}, 30*time.Second, 10*time.Second).Should(BeNil())
})
})
Context("The VMI SELinux context status", func() {
It("Should get set and stay the the same after a migration", decorators.RequiresTwoSchedulableNodes, func() {
vmi := libvmifact.NewAlpine(libnet.WithMasqueradeNetworking()...)
By("Starting a New VMI")
vmi, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).Create(context.Background(), vmi, metav1.CreateOptions{})
Expect(err).NotTo(HaveOccurred())
libwait.WaitForSuccessfulVMIStart(vmi)
By("Ensuring VMI is running by logging in")
libwait.WaitUntilVMIReady(vmi, console.LoginToAlpine)
By("Ensuring the VMI SELinux context status gets set")
seContext := ""
Eventually(func() string {
vmi, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).Get(context.Background(), vmi.Name, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
seContext = vmi.Status.SelinuxContext
return seContext
}, 30*time.Second, 10*time.Second).ShouldNot(BeEmpty(), "VMI SELinux context status never got set")
By("Ensuring the VMI SELinux context status matches the virt-launcher pod files")
stdout := tests.RunCommandOnVmiPod(vmi, []string{"ls", "-lZd", "/"})
Expect(stdout).To(ContainSubstring(seContext))
By("Migrating the VMI")
migration := libmigration.New(vmi.Name, vmi.Namespace)
libmigration.RunMigrationAndExpectToCompleteWithDefaultTimeout(virtClient, migration)
By("Ensuring the VMI SELinux context status didn't change")
vmi, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).Get(context.Background(), vmi.Name, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(vmi.Status.SelinuxContext).To(Equal(seContext))
By("Fetching virt-launcher Pod")
pod, err := virtClient.CoreV1().Pods(vmi.Namespace).Get(context.Background(), vmi.Status.MigrationState.TargetPod, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
By("Ensuring the right SELinux context is set on the target pod")
Expect(pod.Spec.SecurityContext).NotTo(BeNil())
Expect(pod.Spec.SecurityContext.SELinuxOptions).NotTo(BeNil(), fmt.Sprintf("%#v", pod.Spec.SecurityContext))
ctx := strings.Split(seContext, ":")
Expect(ctx).To(HaveLen(5))
Expect(pod.Spec.SecurityContext.SELinuxOptions.Level).To(Equal(strings.Join(ctx[3:], ":")))
By("Ensuring the target virt-launcher has the same SELinux context as the source")
stdout = tests.RunCommandOnVmiPod(vmi, []string{"ls", "-lZd", "/"})
Expect(stdout).To(ContainSubstring(seContext))
})
})
})
func runOnAllSchedulableNodes(virtClient kubecli.KubevirtClient, command []string, forbiddenString string) error {
nodes := libnode.GetAllSchedulableNodes(virtClient)
for _, node := range nodes.Items {
pod, err := libnode.GetVirtHandlerPod(virtClient, node.Name)
if err != nil {
return err
}
stdout, stderr, err := exec.ExecuteCommandOnPodWithResults(pod, components.VirtHandlerName, command)
if err != nil {
_, _ = GinkgoWriter.Write([]byte(stderr))
return err
}
if forbiddenString != "" {
if strings.Contains(stdout, forbiddenString) {
return fmt.Errorf("found unexpected %s on node %s", forbiddenString, node.Name)
}
}
}
return nil
}
func isLauncherCapabilityValid(capability k8sv1.Capability) bool {
switch capability {
case
capNetBindService, capSysNice:
return true
}
return false
}