github.com/brownsys/tracing-framework-go@v0.0.0-20161210174012-0542a62412fe/go/darwin_amd64/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 if _, err := os.Stat("/proc/self/ns/net"); err != nil { 159 if os.IsNotExist(err) { 160 t.Skip("kernel doesn't support net namespace") 161 } 162 t.Fatal(err) 163 } 164 165 cmd := exec.Command("cat", path) 166 cmd.SysProcAttr = &syscall.SysProcAttr{ 167 Unshareflags: syscall.CLONE_NEWNET, 168 } 169 out, err := cmd.CombinedOutput() 170 if err != nil { 171 t.Fatalf("Cmd failed with err %v, output: %s", err, out) 172 } 173 174 // Check there is only the local network interface 175 sout := strings.TrimSpace(string(out)) 176 if !strings.Contains(sout, "lo:") { 177 t.Fatalf("Expected lo network interface to exist, got %s", sout) 178 } 179 180 lines := strings.Split(sout, "\n") 181 if len(lines) != 3 { 182 t.Fatalf("Expected 3 lines of output, got %d", len(lines)) 183 } 184 } 185 186 func TestGroupCleanup(t *testing.T) { 187 if os.Getuid() != 0 { 188 t.Skip("we need root for credential") 189 } 190 cmd := exec.Command("id") 191 cmd.SysProcAttr = &syscall.SysProcAttr{ 192 Credential: &syscall.Credential{ 193 Uid: 0, 194 Gid: 0, 195 }, 196 } 197 out, err := cmd.CombinedOutput() 198 if err != nil { 199 t.Fatalf("Cmd failed with err %v, output: %s", err, out) 200 } 201 strOut := strings.TrimSpace(string(out)) 202 expected := "uid=0(root) gid=0(root) groups=0(root)" 203 // Just check prefix because some distros reportedly output a 204 // context parameter; see https://golang.org/issue/16224. 205 if !strings.HasPrefix(strOut, expected) { 206 t.Errorf("id command output: %q, expected prefix: %q", strOut, expected) 207 } 208 } 209 210 func TestGroupCleanupUserNamespace(t *testing.T) { 211 if os.Getuid() != 0 { 212 t.Skip("we need root for credential") 213 } 214 checkUserNS(t) 215 cmd := exec.Command("id") 216 uid, gid := os.Getuid(), os.Getgid() 217 cmd.SysProcAttr = &syscall.SysProcAttr{ 218 Cloneflags: syscall.CLONE_NEWUSER, 219 Credential: &syscall.Credential{ 220 Uid: uint32(uid), 221 Gid: uint32(gid), 222 }, 223 UidMappings: []syscall.SysProcIDMap{ 224 {ContainerID: 0, HostID: uid, Size: 1}, 225 }, 226 GidMappings: []syscall.SysProcIDMap{ 227 {ContainerID: 0, HostID: gid, Size: 1}, 228 }, 229 } 230 out, err := cmd.CombinedOutput() 231 if err != nil { 232 t.Fatalf("Cmd failed with err %v, output: %s", err, out) 233 } 234 strOut := strings.TrimSpace(string(out)) 235 236 // Strings we've seen in the wild. 237 expected := []string{ 238 "uid=0(root) gid=0(root) groups=0(root)", 239 "uid=0(root) gid=0(root) groups=0(root),65534(nobody)", 240 "uid=0(root) gid=0(root) groups=0(root),65534(nogroup)", 241 "uid=0(root) gid=0(root) groups=0(root),65534", 242 } 243 for _, e := range expected { 244 if strOut == e { 245 return 246 } 247 } 248 t.Errorf("id command output: %q, expected one of %q", strOut, expected) 249 }