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"
|
2015-06-13 14:46:10 -06:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2015-06-03 11:50:39 -06:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
t.Fatalf("Failed to stat /proc/self/ns/user: %v", err)
|
|
|
|
}
|
|
|
|
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 {
|
2015-06-19 14:48:06 -06:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 11:50:39 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-06-13 14:46:10 -06:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|