github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/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 "strings" 14 "syscall" 15 "testing" 16 ) 17 18 // Check if we are in a chroot by checking if the inode of / is 19 // different from 2 (there is no better test available to non-root on 20 // linux). 21 func isChrooted(t *testing.T) bool { 22 root, err := os.Stat("/") 23 if err != nil { 24 t.Fatalf("cannot stat /: %v", err) 25 } 26 return root.Sys().(*syscall.Stat_t).Ino != 2 27 } 28 29 func checkUserNS(t *testing.T) { 30 if _, err := os.Stat("/proc/self/ns/user"); err != nil { 31 if os.IsNotExist(err) { 32 t.Skip("kernel doesn't support user namespaces") 33 } 34 if os.IsPermission(err) { 35 t.Skip("unable to test user namespaces due to permissions") 36 } 37 t.Fatalf("Failed to stat /proc/self/ns/user: %v", err) 38 } 39 if isChrooted(t) { 40 // create_user_ns in the kernel (see 41 // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/kernel/user_namespace.c) 42 // forbids the creation of user namespaces when chrooted. 43 t.Skip("cannot create user namespaces when chrooted") 44 } 45 // On some systems, there is a sysctl setting. 46 if 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 // When running under the Go continuous build, skip tests for 53 // now when under Kubernetes. (where things are root but not quite) 54 // Both of these are our own environment variables. 55 // See Issue 12815. 56 if os.Getenv("GO_BUILDER_NAME") != "" && os.Getenv("IN_KUBERNETES") == "1" { 57 t.Skip("skipping test on Kubernetes-based builders; see Issue 12815") 58 } 59 } 60 61 func whoamiCmd(t *testing.T, uid, gid int, setgroups bool) *exec.Cmd { 62 checkUserNS(t) 63 cmd := exec.Command("whoami") 64 cmd.SysProcAttr = &syscall.SysProcAttr{ 65 Cloneflags: syscall.CLONE_NEWUSER, 66 UidMappings: []syscall.SysProcIDMap{ 67 {ContainerID: 0, HostID: uid, Size: 1}, 68 }, 69 GidMappings: []syscall.SysProcIDMap{ 70 {ContainerID: 0, HostID: gid, Size: 1}, 71 }, 72 GidMappingsEnableSetgroups: setgroups, 73 } 74 return cmd 75 } 76 77 func testNEWUSERRemap(t *testing.T, uid, gid int, setgroups bool) { 78 cmd := whoamiCmd(t, uid, gid, setgroups) 79 out, err := cmd.CombinedOutput() 80 if err != nil { 81 t.Fatalf("Cmd failed with err %v, output: %s", err, out) 82 } 83 sout := strings.TrimSpace(string(out)) 84 want := "root" 85 if sout != want { 86 t.Fatalf("whoami = %q; want %q", out, want) 87 } 88 } 89 90 func TestCloneNEWUSERAndRemapRootDisableSetgroups(t *testing.T) { 91 if os.Getuid() != 0 { 92 t.Skip("skipping root only test") 93 } 94 testNEWUSERRemap(t, 0, 0, false) 95 } 96 97 func TestCloneNEWUSERAndRemapRootEnableSetgroups(t *testing.T) { 98 if os.Getuid() != 0 { 99 t.Skip("skipping root only test") 100 } 101 testNEWUSERRemap(t, 0, 0, false) 102 } 103 104 func TestCloneNEWUSERAndRemapNoRootDisableSetgroups(t *testing.T) { 105 if os.Getuid() == 0 { 106 t.Skip("skipping unprivileged user only test") 107 } 108 testNEWUSERRemap(t, os.Getuid(), os.Getgid(), false) 109 } 110 111 func TestCloneNEWUSERAndRemapNoRootSetgroupsEnableSetgroups(t *testing.T) { 112 if os.Getuid() == 0 { 113 t.Skip("skipping unprivileged user only test") 114 } 115 cmd := whoamiCmd(t, os.Getuid(), os.Getgid(), true) 116 err := cmd.Run() 117 if err == nil { 118 t.Skip("probably old kernel without security fix") 119 } 120 if !os.IsPermission(err) { 121 t.Fatalf("Unprivileged gid_map rewriting with GidMappingsEnableSetgroups must fail") 122 } 123 } 124 125 func TestEmptyCredGroupsDisableSetgroups(t *testing.T) { 126 cmd := whoamiCmd(t, os.Getuid(), os.Getgid(), false) 127 cmd.SysProcAttr.Credential = &syscall.Credential{} 128 if err := cmd.Run(); err != nil { 129 t.Fatal(err) 130 } 131 } 132 133 func TestUnshare(t *testing.T) { 134 // Make sure we are running as root so we have permissions to use unshare 135 // and create a network namespace. 136 if os.Getuid() != 0 { 137 t.Skip("kernel prohibits unshare in unprivileged process, unless using user namespace") 138 } 139 140 // When running under the Go continuous build, skip tests for 141 // now when under Kubernetes. (where things are root but not quite) 142 // Both of these are our own environment variables. 143 // See Issue 12815. 144 if os.Getenv("GO_BUILDER_NAME") != "" && os.Getenv("IN_KUBERNETES") == "1" { 145 t.Skip("skipping test on Kubernetes-based builders; see Issue 12815") 146 } 147 148 path := "/proc/net/dev" 149 if _, err := os.Stat(path); err != nil { 150 if os.IsNotExist(err) { 151 t.Skip("kernel doesn't support proc filesystem") 152 } 153 if os.IsPermission(err) { 154 t.Skip("unable to test proc filesystem due to permissions") 155 } 156 t.Fatal(err) 157 } 158 159 cmd := exec.Command("cat", path) 160 cmd.SysProcAttr = &syscall.SysProcAttr{ 161 Unshareflags: syscall.CLONE_NEWNET, 162 } 163 out, err := cmd.CombinedOutput() 164 if err != nil { 165 t.Fatalf("Cmd failed with err %v, output: %s", err, out) 166 } 167 168 // Check there is only the local network interface 169 sout := strings.TrimSpace(string(out)) 170 if !strings.Contains(sout, "lo:") { 171 t.Fatalf("Expected lo network interface to exist, got %s", sout) 172 } 173 174 lines := strings.Split(sout, "\n") 175 if len(lines) != 3 { 176 t.Fatalf("Expected 3 lines of output, got %d", len(lines)) 177 } 178 } 179 180 func TestGroupCleanup(t *testing.T) { 181 if os.Getuid() != 0 { 182 t.Skip("we need root for credential") 183 } 184 cmd := exec.Command("id") 185 cmd.SysProcAttr = &syscall.SysProcAttr{ 186 Credential: &syscall.Credential{ 187 Uid: 0, 188 Gid: 0, 189 }, 190 } 191 out, err := cmd.CombinedOutput() 192 if err != nil { 193 t.Fatalf("Cmd failed with err %v, output: %s", err, out) 194 } 195 strOut := strings.TrimSpace(string(out)) 196 expected := "uid=0(root) gid=0(root) groups=0(root)" 197 if strOut != expected { 198 t.Fatalf("id command output: %s, expected: %s", strOut, expected) 199 } 200 } 201 202 func TestGroupCleanupUserNamespace(t *testing.T) { 203 if os.Getuid() != 0 { 204 t.Skip("we need root for credential") 205 } 206 checkUserNS(t) 207 cmd := exec.Command("id") 208 uid, gid := os.Getuid(), os.Getgid() 209 cmd.SysProcAttr = &syscall.SysProcAttr{ 210 Cloneflags: syscall.CLONE_NEWUSER, 211 Credential: &syscall.Credential{ 212 Uid: uint32(uid), 213 Gid: uint32(gid), 214 }, 215 UidMappings: []syscall.SysProcIDMap{ 216 {ContainerID: 0, HostID: uid, Size: 1}, 217 }, 218 GidMappings: []syscall.SysProcIDMap{ 219 {ContainerID: 0, HostID: gid, Size: 1}, 220 }, 221 } 222 out, err := cmd.CombinedOutput() 223 if err != nil { 224 t.Fatalf("Cmd failed with err %v, output: %s", err, out) 225 } 226 strOut := strings.TrimSpace(string(out)) 227 // there are two possible outs 228 expected1 := "uid=0(root) gid=0(root) groups=0(root)" 229 expected2 := "uid=0(root) gid=0(root) groups=0(root),65534(nobody)" 230 if strOut != expected1 && strOut != expected2 { 231 t.Fatalf("id command output: %s, expected: %s or %s", strOut, expected1, expected2) 232 } 233 }