2015-06-03 11:50:39 -06:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package syscall_test
|
|
|
|
|
|
|
|
import (
|
2015-06-13 14:46:10 -06:00
|
|
|
"io/ioutil"
|
2015-06-03 11:50:39 -06:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-11-25 15:47:32 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-06-15 12:35:56 -06:00
|
|
|
func whoamiCmd(t *testing.T, uid, gid int, setgroups bool) *exec.Cmd {
|
2015-06-03 11:50:39 -06:00
|
|
|
if _, err := os.Stat("/proc/self/ns/user"); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
t.Skip("kernel doesn't support user namespaces")
|
|
|
|
}
|
2016-03-07 15:11:48 -07:00
|
|
|
if os.IsPermission(err) {
|
|
|
|
t.Skip("unable to test user namespaces due to permissions")
|
|
|
|
}
|
2015-06-03 11:50:39 -06:00
|
|
|
t.Fatalf("Failed to stat /proc/self/ns/user: %v", err)
|
|
|
|
}
|
2015-11-25 15:47:32 -07:00
|
|
|
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")
|
|
|
|
}
|
2015-08-31 09:41:43 -06:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 16:40:55 -06:00
|
|
|
// 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")
|
|
|
|
}
|
2015-06-03 11:50:39 -06:00
|
|
|
cmd := exec.Command("whoami")
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Cloneflags: syscall.CLONE_NEWUSER,
|
|
|
|
UidMappings: []syscall.SysProcIDMap{
|
|
|
|
{ContainerID: 0, HostID: uid, Size: 1},
|
|
|
|
},
|
|
|
|
GidMappings: []syscall.SysProcIDMap{
|
2015-06-15 12:35:56 -06:00
|
|
|
{ContainerID: 0, HostID: gid, Size: 1},
|
2015-06-03 11:50:39 -06:00
|
|
|
},
|
|
|
|
GidMappingsEnableSetgroups: setgroups,
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2015-06-15 12:35:56 -06:00
|
|
|
func testNEWUSERRemap(t *testing.T, uid, gid int, setgroups bool) {
|
|
|
|
cmd := whoamiCmd(t, uid, gid, setgroups)
|
2015-06-03 11:50:39 -06:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cmd failed with err %v, output: %s", err, out)
|
|
|
|
}
|
|
|
|
sout := strings.TrimSpace(string(out))
|
|
|
|
want := "root"
|
|
|
|
if sout != want {
|
|
|
|
t.Fatalf("whoami = %q; want %q", out, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloneNEWUSERAndRemapRootDisableSetgroups(t *testing.T) {
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("skipping root only test")
|
|
|
|
}
|
2015-06-15 12:35:56 -06:00
|
|
|
testNEWUSERRemap(t, 0, 0, false)
|
2015-06-03 11:50:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloneNEWUSERAndRemapRootEnableSetgroups(t *testing.T) {
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("skipping root only test")
|
|
|
|
}
|
2015-06-15 12:35:56 -06:00
|
|
|
testNEWUSERRemap(t, 0, 0, false)
|
2015-06-03 11:50:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloneNEWUSERAndRemapNoRootDisableSetgroups(t *testing.T) {
|
|
|
|
if os.Getuid() == 0 {
|
|
|
|
t.Skip("skipping unprivileged user only test")
|
|
|
|
}
|
2015-06-15 12:35:56 -06:00
|
|
|
testNEWUSERRemap(t, os.Getuid(), os.Getgid(), false)
|
2015-06-03 11:50:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloneNEWUSERAndRemapNoRootSetgroupsEnableSetgroups(t *testing.T) {
|
|
|
|
if os.Getuid() == 0 {
|
|
|
|
t.Skip("skipping unprivileged user only test")
|
|
|
|
}
|
2015-06-15 12:35:56 -06:00
|
|
|
cmd := whoamiCmd(t, os.Getuid(), os.Getgid(), true)
|
2015-06-03 11:50:39 -06:00
|
|
|
err := cmd.Run()
|
|
|
|
if err == nil {
|
|
|
|
t.Skip("probably old kernel without security fix")
|
|
|
|
}
|
2015-06-19 14:48:06 -06:00
|
|
|
if !os.IsPermission(err) {
|
2015-06-03 11:50:39 -06:00
|
|
|
t.Fatalf("Unprivileged gid_map rewriting with GidMappingsEnableSetgroups must fail")
|
|
|
|
}
|
|
|
|
}
|
2015-08-26 21:45:28 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-05-18 19:47:24 -06:00
|
|
|
|
|
|
|
func TestUnshare(t *testing.T) {
|
|
|
|
// Make sure we are running as root so we have permissions to use unshare
|
|
|
|
// and create a network namespace.
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("kernel prohibits unshare in unprivileged process, unless using user namespace")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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("ip", "a")
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Unshare: syscall.CLONE_NEWNET,
|
|
|
|
}
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cmd failed with err %v, output: %s", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check there is only the local network interface
|
|
|
|
sout := strings.TrimSpace(string(out))
|
|
|
|
if !strings.Contains(sout, "lo") {
|
|
|
|
t.Fatalf("Expected lo network interface to exist, got %s", sout)
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(sout, "\n")
|
|
|
|
if len(lines) != 2 {
|
|
|
|
t.Fatalf("Expected 2 lines of output, got %d", len(lines))
|
|
|
|
}
|
|
|
|
}
|