Skip to content

Commit c387890

Browse files
chrishenziek8s-infra-cherrypick-robot
authored andcommitted
Add integration test for privileged container cgroup mounts
Verifies that running a privileged container does not alter host cgroup mount options (specifically nsdelegate and memory_recursiveprot). Creates a privileged sandbox and container, starts it, and compares the host's /sys/fs/cgroup mount options before and after execution to guarantee safety. Signed-off-by: Chris Henzie <[email protected]>
1 parent 047a335 commit c387890

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package integration
18+
19+
import (
20+
"os"
21+
"strings"
22+
"testing"
23+
24+
"github.com/containerd/cgroups/v3"
25+
"github.com/containerd/containerd/v2/core/mount"
26+
"github.com/containerd/containerd/v2/integration/images"
27+
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
29+
)
30+
31+
func TestPrivilegedContainerCgroupMountOptions(t *testing.T) {
32+
if f := os.Getenv("RUNC_FLAVOR"); f == "crun" {
33+
t.Skip("Skipping until crun supports cgroup v2 mount options (https://github.com/containers/crun/pull/2040)")
34+
}
35+
if cgroups.Mode() != cgroups.Unified {
36+
t.Skip("Requires cgroup v2")
37+
}
38+
39+
hostMountBefore, err := mount.Lookup("/sys/fs/cgroup")
40+
require.NoError(t, err)
41+
42+
if !strings.Contains(hostMountBefore.VFSOptions, "nsdelegate") && !strings.Contains(hostMountBefore.VFSOptions, "memory_recursiveprot") {
43+
t.Skip("requires host cgroup mount to have nsdelegate or memory_recursiveprot")
44+
}
45+
46+
testImage := images.Get(images.BusyBox)
47+
EnsureImageExists(t, testImage)
48+
49+
t.Log("Create a sandbox with privileged=true")
50+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "privileged-cgroup-mount-test", WithPodSecurityContext(true))
51+
52+
t.Log("Create a container with privileged=true")
53+
cnConfig := ContainerConfig("container", testImage, WithCommand("sh", "-c", "sleep 1d"), WithSecurityContext(true))
54+
cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig)
55+
require.NoError(t, err)
56+
t.Cleanup(func() {
57+
if err := runtimeService.RemoveContainer(cn); err != nil {
58+
t.Logf("failed to remove container %s: %v", cn, err)
59+
}
60+
})
61+
62+
t.Log("Start the container")
63+
require.NoError(t, runtimeService.StartContainer(cn))
64+
t.Cleanup(func() {
65+
if err := runtimeService.StopContainer(cn, 10); err != nil {
66+
t.Logf("failed to stop container %s: %v", cn, err)
67+
}
68+
})
69+
70+
hostMountAfter, err := mount.Lookup("/sys/fs/cgroup")
71+
require.NoError(t, err)
72+
73+
if strings.Contains(hostMountBefore.VFSOptions, "nsdelegate") {
74+
assert.Contains(t, hostMountAfter.VFSOptions, "nsdelegate", "nsdelegate should be preserved on the host cgroup mount")
75+
}
76+
if strings.Contains(hostMountBefore.VFSOptions, "memory_recursiveprot") {
77+
assert.Contains(t, hostMountAfter.VFSOptions, "memory_recursiveprot", "memory_recursiveprot should be preserved on the host cgroup mount")
78+
}
79+
}

0 commit comments

Comments
 (0)