diff options
Diffstat (limited to 'libgo/go/syscall/exec_linux_test.go')
-rw-r--r-- | libgo/go/syscall/exec_linux_test.go | 65 |
1 files changed, 39 insertions, 26 deletions
diff --git a/libgo/go/syscall/exec_linux_test.go b/libgo/go/syscall/exec_linux_test.go index 60d2734f66a..6d319411840 100644 --- a/libgo/go/syscall/exec_linux_test.go +++ b/libgo/go/syscall/exec_linux_test.go @@ -10,13 +10,22 @@ import ( "io/ioutil" "os" "os/exec" - "regexp" - "strconv" "strings" "syscall" "testing" ) +// Check if we are in a chroot by checking if the inode of / is +// different from 2 (there is no better test available to non-root on +// linux). +func isChrooted(t *testing.T) bool { + root, err := os.Stat("/") + if err != nil { + t.Fatalf("cannot stat /: %v", err) + } + return root.Sys().(*syscall.Stat_t).Ino != 2 +} + func whoamiCmd(t *testing.T, uid, gid int, setgroups bool) *exec.Cmd { if _, err := os.Stat("/proc/self/ns/user"); err != nil { if os.IsNotExist(err) { @@ -24,6 +33,26 @@ func whoamiCmd(t *testing.T, uid, gid int, setgroups bool) *exec.Cmd { } t.Fatalf("Failed to stat /proc/self/ns/user: %v", err) } + if isChrooted(t) { + // create_user_ns in the kernel (see + // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/kernel/user_namespace.c) + // forbids the creation of user namespaces when chrooted. + t.Skip("cannot create user namespaces when chrooted") + } + // On some systems, there is a sysctl setting. + if os.Getuid() != 0 { + data, errRead := ioutil.ReadFile("/proc/sys/kernel/unprivileged_userns_clone") + if errRead == nil && data[0] == '0' { + t.Skip("kernel prohibits user namespace in unprivileged process") + } + } + // When running under the Go continuous build, skip tests for + // now when under Kubernetes. (where things are root but not quite) + // Both of these are our own environment variables. + // See Issue 12815. + if os.Getenv("GO_BUILDER_NAME") != "" && os.Getenv("IN_KUBERNETES") == "1" { + t.Skip("skipping test on Kubernetes-based builders; see Issue 12815") + } cmd := exec.Command("whoami") cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUSER, @@ -42,14 +71,6 @@ func testNEWUSERRemap(t *testing.T, uid, gid int, setgroups bool) { cmd := whoamiCmd(t, uid, gid, setgroups) out, err := cmd.CombinedOutput() if err != nil { - // On some systems, there is a sysctl setting. - if os.IsPermission(err) && os.Getuid() != 0 { - data, errRead := ioutil.ReadFile("/proc/sys/kernel/unprivileged_userns_clone") - if errRead == nil && data[0] == '0' { - t.Skip("kernel prohibits user namespace in unprivileged process") - } - } - t.Fatalf("Cmd failed with err %v, output: %s", err, out) } sout := strings.TrimSpace(string(out)) @@ -73,22 +94,6 @@ func TestCloneNEWUSERAndRemapRootEnableSetgroups(t *testing.T) { testNEWUSERRemap(t, 0, 0, false) } -// kernelVersion returns the major and minor versions of the Linux -// kernel version. It calls t.Skip if it can't figure it out. -func kernelVersion(t *testing.T) (int, int) { - bytes, err := ioutil.ReadFile("/proc/version") - if err != nil { - t.Skipf("can't get kernel version: %v", err) - } - matches := regexp.MustCompile("([0-9]+).([0-9]+)").FindSubmatch(bytes) - if len(matches) < 3 { - t.Skipf("can't get kernel version from %s", bytes) - } - major, _ := strconv.Atoi(string(matches[1])) - minor, _ := strconv.Atoi(string(matches[2])) - return major, minor -} - func TestCloneNEWUSERAndRemapNoRootDisableSetgroups(t *testing.T) { if os.Getuid() == 0 { t.Skip("skipping unprivileged user only test") @@ -109,3 +114,11 @@ func TestCloneNEWUSERAndRemapNoRootSetgroupsEnableSetgroups(t *testing.T) { t.Fatalf("Unprivileged gid_map rewriting with GidMappingsEnableSetgroups must fail") } } + +func TestEmptyCredGroupsDisableSetgroups(t *testing.T) { + cmd := whoamiCmd(t, os.Getuid(), os.Getgid(), false) + cmd.SysProcAttr.Credential = &syscall.Credential{} + if err := cmd.Run(); err != nil { + t.Fatal(err) + } +} |