|
| 1 | +// Copyright 2024 The gVisor Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package integration provides end-to-end integration tests for runsc. |
| 16 | +package integration |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "slices" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/docker/docker/api/types/mount" |
| 26 | + "gvisor.dev/gvisor/pkg/test/dockerutil" |
| 27 | + "gvisor.dev/gvisor/pkg/test/testutil" |
| 28 | +) |
| 29 | + |
| 30 | +// testVariant is a variant of the gVisor in Docker test. |
| 31 | +type testVariant struct { |
| 32 | + Name string |
| 33 | + User string |
| 34 | + WorkDir string |
| 35 | + CapAdd []string |
| 36 | + Args []string |
| 37 | + MountCgroupfs bool |
| 38 | +} |
| 39 | + |
| 40 | +// run runs the test variant. |
| 41 | +func (test testVariant) run(ctx context.Context, logger testutil.Logger, runscPath string) (string, error) { |
| 42 | + d := dockerutil.MakeNativeContainer(ctx, logger) |
| 43 | + defer d.CleanUp(ctx) |
| 44 | + opts := dockerutil.RunOpts{ |
| 45 | + Image: "basic/integrationtest", |
| 46 | + User: test.User, |
| 47 | + WorkDir: test.WorkDir, |
| 48 | + SecurityOpts: []string{ |
| 49 | + // Disable default seccomp filter which blocks `mount(2)` and others. |
| 50 | + "seccomp=unconfined", |
| 51 | + |
| 52 | + // Disable AppArmor which also blocks mounts. |
| 53 | + "apparmor=unconfined", |
| 54 | + |
| 55 | + // Set correct SELinux label; this allows ptrace. |
| 56 | + "label=type:container_engine_t", |
| 57 | + }, |
| 58 | + CapAdd: test.CapAdd, |
| 59 | + Mounts: []mount.Mount{ |
| 60 | + // Mount the runtime binary. |
| 61 | + { |
| 62 | + Type: mount.TypeBind, |
| 63 | + Source: runscPath, |
| 64 | + Target: "/runtime", |
| 65 | + ReadOnly: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + if test.MountCgroupfs { |
| 70 | + opts.Mounts = append(opts.Mounts, mount.Mount{ |
| 71 | + Type: mount.TypeBind, |
| 72 | + Source: "/sys/fs/cgroup", |
| 73 | + Target: "/sys/fs/cgroup", |
| 74 | + ReadOnly: false, |
| 75 | + }) |
| 76 | + } |
| 77 | + // Mount an unobstructed view of procfs at /proc2 so that the runtime |
| 78 | + // can mount a fresh procfs. |
| 79 | + // TODO(gvisor.dev/issue/10944): Remove this once issue is fixed. |
| 80 | + opts.Mounts = append(opts.Mounts, mount.Mount{ |
| 81 | + Type: mount.TypeBind, |
| 82 | + Source: "/proc", |
| 83 | + Target: "/proc2", |
| 84 | + ReadOnly: false, |
| 85 | + BindOptions: &mount.BindOptions{ |
| 86 | + NonRecursive: true, |
| 87 | + }, |
| 88 | + }) |
| 89 | + const wantMessage = "It became a jumble of words, a litany, almost a kind of glossolalia." |
| 90 | + args := []string{ |
| 91 | + "/runtime", |
| 92 | + "--debug=true", |
| 93 | + "--debug-log=/dev/stderr", |
| 94 | + } |
| 95 | + args = append(args, test.Args...) |
| 96 | + args = append(args, "do", "/bin/echo", wantMessage) |
| 97 | + logger.Logf("Running: %v", args) |
| 98 | + got, err := d.Run(ctx, opts, args...) |
| 99 | + got = strings.TrimSpace(got) |
| 100 | + if err != nil { |
| 101 | + return got, err |
| 102 | + } |
| 103 | + if !strings.Contains(got, wantMessage) { |
| 104 | + return got, fmt.Errorf("did not observe substring %q in logs", wantMessage) |
| 105 | + } |
| 106 | + return got, nil |
| 107 | +} |
| 108 | + |
| 109 | +// failureCases returns modified versions of this same test that are expected |
| 110 | +// to fail. Verifying that these variants fail ensures that each test variant |
| 111 | +// runs with the minimal amount of deviations from the default configuration. |
| 112 | +func (test testVariant) failureCases() []testVariant { |
| 113 | + failureCase := func(name string) testVariant { |
| 114 | + copy := test |
| 115 | + copy.Name = name |
| 116 | + return copy |
| 117 | + } |
| 118 | + var failureCases []testVariant |
| 119 | + if test.MountCgroupfs { |
| 120 | + copy := failureCase("without cgroupfs mounted") |
| 121 | + copy.MountCgroupfs = false |
| 122 | + failureCases = append(failureCases, copy) |
| 123 | + } |
| 124 | + for i, capAdd := range test.CapAdd { |
| 125 | + copy := failureCase(fmt.Sprintf("without capability %s", capAdd)) |
| 126 | + copy.CapAdd = append(append([]string(nil), test.CapAdd[:i]...), test.CapAdd[i+1:]...) |
| 127 | + failureCases = append(failureCases, copy) |
| 128 | + } |
| 129 | + for _, tryRemoveArg := range []string{ |
| 130 | + "--rootless=true", |
| 131 | + "--ignore-cgroups=true", |
| 132 | + } { |
| 133 | + if index := slices.Index(test.Args, tryRemoveArg); index != -1 { |
| 134 | + copy := failureCase(fmt.Sprintf("without argument %s", tryRemoveArg)) |
| 135 | + copy.Args = append(append([]string(nil), test.Args[:index]...), test.Args[index+1:]...) |
| 136 | + failureCases = append(failureCases, copy) |
| 137 | + } |
| 138 | + } |
| 139 | + return failureCases |
| 140 | +} |
| 141 | + |
| 142 | +// TestGVisorInDocker runs `runsc` inside a non-gVisor container. |
| 143 | +// This is used in contexts such as Dangerzone: |
| 144 | +// https://gvisor.dev/blog/2024/09/23/safe-ride-into-the-dangerzone/ |
| 145 | +func TestGVisorInDocker(t *testing.T) { |
| 146 | + ctx := context.Background() |
| 147 | + runscPath, err := dockerutil.RuntimePath() |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("Cannot locate runtime path: %v", err) |
| 150 | + } |
| 151 | + for _, test := range []testVariant{ |
| 152 | + { |
| 153 | + Name: "Rootful", |
| 154 | + User: "root", |
| 155 | + CapAdd: []string{ |
| 156 | + // Necessary to set up networking (creating veth devices). |
| 157 | + "NET_ADMIN", |
| 158 | + // Necessary to set up networking, which calls `ip netns add` which |
| 159 | + // calls `mount(2)`. |
| 160 | + "SYS_ADMIN", |
| 161 | + }, |
| 162 | + // Mount cgroupfs as writable, otherwise the runtime won't be able to |
| 163 | + // set up cgroups. |
| 164 | + MountCgroupfs: true, |
| 165 | + }, |
| 166 | + { |
| 167 | + Name: "Rootful without networking", |
| 168 | + User: "root", |
| 169 | + CapAdd: []string{ |
| 170 | + // "Can't run sandbox process in minimal chroot since we don't have CAP_SYS_ADMIN" |
| 171 | + "SYS_ADMIN", |
| 172 | + }, |
| 173 | + Args: []string{ |
| 174 | + "--network=none", |
| 175 | + }, |
| 176 | + MountCgroupfs: true, |
| 177 | + }, |
| 178 | + { |
| 179 | + Name: "Rootful with host networking", |
| 180 | + User: "root", |
| 181 | + CapAdd: []string{ |
| 182 | + // Necessary to set up networking (creating veth devices). |
| 183 | + "NET_ADMIN", |
| 184 | + // Necessary to set up networking, which calls `ip netns add` which |
| 185 | + // calls `mount(2)`. |
| 186 | + "SYS_ADMIN", |
| 187 | + }, |
| 188 | + Args: []string{ |
| 189 | + "--network=host", |
| 190 | + }, |
| 191 | + MountCgroupfs: true, |
| 192 | + }, |
| 193 | + { |
| 194 | + Name: "Rootful without networking and cgroupfs", |
| 195 | + User: "root", |
| 196 | + CapAdd: []string{ |
| 197 | + // "Can't run sandbox process in minimal chroot since we don't have CAP_SYS_ADMIN" |
| 198 | + "SYS_ADMIN", |
| 199 | + }, |
| 200 | + Args: []string{ |
| 201 | + "--network=none", |
| 202 | + "--ignore-cgroups=true", |
| 203 | + }, |
| 204 | + }, |
| 205 | + { |
| 206 | + Name: "Rootless", |
| 207 | + User: "nonroot", |
| 208 | + WorkDir: "/home/nonroot", |
| 209 | + Args: []string{ |
| 210 | + "--rootless=true", |
| 211 | + }, |
| 212 | + }, |
| 213 | + { |
| 214 | + Name: "Rootless without networking", |
| 215 | + User: "nonroot", |
| 216 | + WorkDir: "/home/nonroot", |
| 217 | + Args: []string{ |
| 218 | + "--rootless=true", |
| 219 | + "--network=none", |
| 220 | + }, |
| 221 | + }, |
| 222 | + } { |
| 223 | + t.Run(test.Name, func(t *testing.T) { |
| 224 | + if logs, err := test.run(ctx, t, runscPath); err != nil { |
| 225 | + t.Fatalf("Error: %v; logs:\n%s", err, logs) |
| 226 | + } |
| 227 | + for _, failureCase := range test.failureCases() { |
| 228 | + t.Run(failureCase.Name, func(t *testing.T) { |
| 229 | + if logs, err := failureCase.run(ctx, t, runscPath); err == nil { |
| 230 | + t.Fatalf("Failure case unexpectedly succeeded; logs:\n%s", logs) |
| 231 | + } |
| 232 | + }) |
| 233 | + } |
| 234 | + }) |
| 235 | + } |
| 236 | +} |
0 commit comments