github.com/zxy12/golang151_with_comment@v0.0.0-20190507085033-721809559d3c/syscall/exec_linux_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build linux
     6  
     7  package syscall_test
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"os/exec"
    13  	"regexp"
    14  	"strconv"
    15  	"strings"
    16  	"syscall"
    17  	"testing"
    18  )
    19  
    20  func whoamiCmd(t *testing.T, uid, gid int, setgroups bool) *exec.Cmd {
    21  	if _, err := os.Stat("/proc/self/ns/user"); err != nil {
    22  		if os.IsNotExist(err) {
    23  			t.Skip("kernel doesn't support user namespaces")
    24  		}
    25  		t.Fatalf("Failed to stat /proc/self/ns/user: %v", err)
    26  	}
    27  	cmd := exec.Command("whoami")
    28  	cmd.SysProcAttr = &syscall.SysProcAttr{
    29  		Cloneflags: syscall.CLONE_NEWUSER,
    30  		UidMappings: []syscall.SysProcIDMap{
    31  			{ContainerID: 0, HostID: uid, Size: 1},
    32  		},
    33  		GidMappings: []syscall.SysProcIDMap{
    34  			{ContainerID: 0, HostID: gid, Size: 1},
    35  		},
    36  		GidMappingsEnableSetgroups: setgroups,
    37  	}
    38  	return cmd
    39  }
    40  
    41  func testNEWUSERRemap(t *testing.T, uid, gid int, setgroups bool) {
    42  	cmd := whoamiCmd(t, uid, gid, setgroups)
    43  	out, err := cmd.CombinedOutput()
    44  	if err != nil {
    45  		// On some systems, there is a sysctl setting.
    46  		if os.IsPermission(err) && os.Getuid() != 0 {
    47  			data, errRead := ioutil.ReadFile("/proc/sys/kernel/unprivileged_userns_clone")
    48  			if errRead == nil && data[0] == '0' {
    49  				t.Skip("kernel prohibits user namespace in unprivileged process")
    50  			}
    51  		}
    52  
    53  		t.Fatalf("Cmd failed with err %v, output: %s", err, out)
    54  	}
    55  	sout := strings.TrimSpace(string(out))
    56  	want := "root"
    57  	if sout != want {
    58  		t.Fatalf("whoami = %q; want %q", out, want)
    59  	}
    60  }
    61  
    62  func TestCloneNEWUSERAndRemapRootDisableSetgroups(t *testing.T) {
    63  	if os.Getuid() != 0 {
    64  		t.Skip("skipping root only test")
    65  	}
    66  	testNEWUSERRemap(t, 0, 0, false)
    67  }
    68  
    69  func TestCloneNEWUSERAndRemapRootEnableSetgroups(t *testing.T) {
    70  	if os.Getuid() != 0 {
    71  		t.Skip("skipping root only test")
    72  	}
    73  	testNEWUSERRemap(t, 0, 0, false)
    74  }
    75  
    76  // kernelVersion returns the major and minor versions of the Linux
    77  // kernel version.  It calls t.Skip if it can't figure it out.
    78  func kernelVersion(t *testing.T) (int, int) {
    79  	bytes, err := ioutil.ReadFile("/proc/version")
    80  	if err != nil {
    81  		t.Skipf("can't get kernel version: %v", err)
    82  	}
    83  	matches := regexp.MustCompile("([0-9]+).([0-9]+)").FindSubmatch(bytes)
    84  	if len(matches) < 3 {
    85  		t.Skipf("can't get kernel version from %s", bytes)
    86  	}
    87  	major, _ := strconv.Atoi(string(matches[1]))
    88  	minor, _ := strconv.Atoi(string(matches[2]))
    89  	return major, minor
    90  }
    91  
    92  func TestCloneNEWUSERAndRemapNoRootDisableSetgroups(t *testing.T) {
    93  	if os.Getuid() == 0 {
    94  		t.Skip("skipping unprivileged user only test")
    95  	}
    96  	testNEWUSERRemap(t, os.Getuid(), os.Getgid(), false)
    97  }
    98  
    99  func TestCloneNEWUSERAndRemapNoRootSetgroupsEnableSetgroups(t *testing.T) {
   100  	if os.Getuid() == 0 {
   101  		t.Skip("skipping unprivileged user only test")
   102  	}
   103  	cmd := whoamiCmd(t, os.Getuid(), os.Getgid(), true)
   104  	err := cmd.Run()
   105  	if err == nil {
   106  		t.Skip("probably old kernel without security fix")
   107  	}
   108  	if !os.IsPermission(err) {
   109  		t.Fatalf("Unprivileged gid_map rewriting with GidMappingsEnableSetgroups must fail")
   110  	}
   111  }