-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test that runs
runsc do
inside a non-gVisor container.
This is used in contexts such as Dangerzone: https://gvisor.dev/blog/2024/09/23/safe-ride-into-the-dangerzone/ Updates issue #10944. PiperOrigin-RevId: 681229280
- Loading branch information
1 parent
ca8d05a
commit cae035d
Showing
6 changed files
with
215 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// Copyright 2024 The gVisor Authors. | ||
// | ||
// 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. | ||
|
||
// Package integration provides end-to-end integration tests for runsc. | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types/mount" | ||
"gvisor.dev/gvisor/pkg/test/dockerutil" | ||
) | ||
|
||
// TestGVisorInDocker runs `runsc` inside a non-gVisor container. | ||
// This is used in contexts such as Dangerzone: | ||
// https://gvisor.dev/blog/2024/09/23/safe-ride-into-the-dangerzone/ | ||
func TestGVisorInDocker(t *testing.T) { | ||
ctx := context.Background() | ||
runscPath, err := dockerutil.RuntimePath() | ||
if err != nil { | ||
t.Fatalf("Cannot locate runtime path: %v", err) | ||
} | ||
for _, test := range []struct { | ||
Name string | ||
User string | ||
WorkDir string | ||
CapAdd []string | ||
Args []string | ||
MountCgroupfs bool | ||
}{ | ||
{ | ||
Name: "Rootful", | ||
User: "root", | ||
CapAdd: []string{ | ||
// Necessary to set up networking (creating veth devices). | ||
"NET_ADMIN", | ||
// Necessary to set up networking, which calls `ip netns add` which | ||
// calls `mount(2)`. | ||
"SYS_ADMIN", | ||
}, | ||
// Mount cgroupfs as writable, otherwise the runtime won't be able to | ||
// set up cgroups. | ||
MountCgroupfs: true, | ||
}, | ||
{ | ||
Name: "Rootful without networking", | ||
User: "root", | ||
CapAdd: []string{ | ||
// "Can't run sandbox process in minimal chroot since we don't have CAP_SYS_ADMIN" | ||
"SYS_ADMIN", | ||
}, | ||
Args: []string{ | ||
"--network=none", | ||
}, | ||
MountCgroupfs: true, | ||
}, | ||
{ | ||
Name: "Rootful with host networking", | ||
User: "root", | ||
CapAdd: []string{ | ||
// Necessary to set up networking (creating veth devices). | ||
"NET_ADMIN", | ||
// Necessary to set up networking, which calls `ip netns add` which | ||
// calls `mount(2)`. | ||
"SYS_ADMIN", | ||
}, | ||
Args: []string{ | ||
"--network=host", | ||
}, | ||
MountCgroupfs: true, | ||
}, | ||
{ | ||
Name: "Rootful without networking and cgroupfs", | ||
User: "root", | ||
CapAdd: []string{ | ||
// "Can't run sandbox process in minimal chroot since we don't have CAP_SYS_ADMIN" | ||
"SYS_ADMIN", | ||
}, | ||
Args: []string{ | ||
"--network=none", | ||
"--ignore-cgroups=true", | ||
}, | ||
}, | ||
{ | ||
Name: "Rootless", | ||
User: "nonroot", | ||
WorkDir: "/home/nonroot", | ||
Args: []string{ | ||
"--rootless=true", | ||
}, | ||
}, | ||
{ | ||
Name: "Rootless without networking", | ||
User: "nonroot", | ||
WorkDir: "/home/nonroot", | ||
Args: []string{ | ||
"--rootless=true", | ||
"--network=none", | ||
}, | ||
}, | ||
} { | ||
t.Run(test.Name, func(t *testing.T) { | ||
d := dockerutil.MakeNativeContainer(ctx, t) | ||
defer d.CleanUp(ctx) | ||
opts := dockerutil.RunOpts{ | ||
Image: "basic/integrationtest", | ||
User: test.User, | ||
WorkDir: test.WorkDir, | ||
SecurityOpts: []string{ | ||
// Disable default seccomp filter which blocks `mount(2)` and others. | ||
"seccomp=unconfined", | ||
|
||
// Disable AppArmor which also blocks mounts. | ||
"apparmor=unconfined", | ||
|
||
// Set correct SELinux label; this allows ptrace. | ||
"label=type:container_engine_t", | ||
}, | ||
CapAdd: test.CapAdd, | ||
Mounts: []mount.Mount{ | ||
// Mount the runtime binary. | ||
{ | ||
Type: mount.TypeBind, | ||
Source: runscPath, | ||
Target: "/runtime", | ||
ReadOnly: true, | ||
}, | ||
}, | ||
} | ||
if test.MountCgroupfs { | ||
opts.Mounts = append(opts.Mounts, mount.Mount{ | ||
Type: mount.TypeBind, | ||
Source: "/sys/fs/cgroup", | ||
Target: "/sys/fs/cgroup", | ||
ReadOnly: false, | ||
}) | ||
} | ||
// Mount an unobstructed view of procfs at /proc2 so that the runtime | ||
// can mount a fresh procfs. | ||
// TODO(gvisor.dev/issue/10944): Remove this once issue is fixed. | ||
opts.Mounts = append(opts.Mounts, mount.Mount{ | ||
Type: mount.TypeBind, | ||
Source: "/proc", | ||
Target: "/proc2", | ||
ReadOnly: false, | ||
BindOptions: &mount.BindOptions{ | ||
NonRecursive: true, | ||
}, | ||
}) | ||
wantMessage := "It became a jumble of words, a litany, almost a kind of glossolalia." | ||
args := []string{ | ||
"/runtime", | ||
"--debug=true", | ||
"--debug-log=/dev/stderr", | ||
} | ||
args = append(args, test.Args...) | ||
args = append(args, "do", "/bin/echo", wantMessage) | ||
t.Logf("Running: %v", args) | ||
if got, err := d.Run(ctx, opts, args...); err != nil { | ||
t.Fatalf("Error: %v\nLogs:\n%s", err, strings.TrimSpace(got)) | ||
} else if !strings.Contains(got, wantMessage) { | ||
t.Fatalf("Did not observe substring %q in logs:\n%s", wantMessage, strings.TrimSpace(got)) | ||
} | ||
}) | ||
} | ||
} |