| 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 |
//go:build linux |
| 6 |
|
| 7 |
package syscall_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"flag" |
| 11 |
"fmt" |
| 12 |
"internal/testenv" |
| 13 |
"io" |
| 14 |
"os" |
| 15 |
"os/exec" |
| 16 |
"os/user" |
| 17 |
"path/filepath" |
| 18 |
"runtime" |
| 19 |
"strconv" |
| 20 |
"strings" |
| 21 |
"syscall" |
| 22 |
"testing" |
| 23 |
"unsafe" |
| 24 |
) |
| 25 |
|
| 26 |
func isDocker() bool { |
| 27 |
_, err := os.Stat("/.dockerenv") |
| 28 |
return err == nil |
| 29 |
} |
| 30 |
|
| 31 |
func isLXC() bool { |
| 32 |
return os.Getenv("container") == "lxc" |
| 33 |
} |
| 34 |
|
| 35 |
func skipInContainer(t *testing.T) { |
| 36 |
// TODO: the callers of this func are using this func to skip |
| 37 |
// tests when running as some sort of "fake root" that's uid 0 |
| 38 |
// but lacks certain Linux capabilities. Most of the Go builds |
| 39 |
// run in privileged containers, though, where root is much |
| 40 |
// closer (if not identical) to the real root. We should test |
| 41 |
// for what we need exactly (which capabilities are active?), |
| 42 |
// instead of just assuming "docker == bad". Then we'd get more test |
| 43 |
// coverage on a bunch of builders too. |
| 44 |
if isDocker() { |
| 45 |
t.Skip("skip this test in Docker container") |
| 46 |
} |
| 47 |
if isLXC() { |
| 48 |
t.Skip("skip this test in LXC container") |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func skipNoUserNamespaces(t *testing.T) { |
| 53 |
if _, err := os.Stat("/proc/self/ns/user"); err != nil { |
| 54 |
if os.IsNotExist(err) { |
| 55 |
t.Skip("kernel doesn't support user namespaces") |
| 56 |
} |
| 57 |
if os.IsPermission(err) { |
| 58 |
t.Skip("unable to test user namespaces due to permissions") |
| 59 |
} |
| 60 |
t.Fatalf("Failed to stat /proc/self/ns/user: %v", err) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func skipUnprivilegedUserClone(t *testing.T) { |
| 65 |
// Skip the test if the sysctl that prevents unprivileged user |
| 66 |
// from creating user namespaces is enabled. |
| 67 |
data, errRead := os.ReadFile("/proc/sys/kernel/unprivileged_userns_clone") |
| 68 |
if errRead != nil || len(data) < 1 || data[0] == '0' { |
| 69 |
t.Skip("kernel prohibits user namespace in unprivileged process") |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Check if we are in a chroot by checking if the inode of / is |
| 74 |
// different from 2 (there is no better test available to non-root on |
| 75 |
// linux). |
| 76 |
func isChrooted(t *testing.T) bool { |
| 77 |
root, err := os.Stat("/") |
| 78 |
if err != nil { |
| 79 |
t.Fatalf("cannot stat /: %v", err) |
| 80 |
} |
| 81 |
return root.Sys().(*syscall.Stat_t).Ino != 2 |
| 82 |
} |
| 83 |
|
| 84 |
func checkUserNS(t *testing.T) { |
| 85 |
skipInContainer(t) |
| 86 |
skipNoUserNamespaces(t) |
| 87 |
if isChrooted(t) { |
| 88 |
// create_user_ns in the kernel (see |
| 89 |
// https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/kernel/user_namespace.c) |
| 90 |
// forbids the creation of user namespaces when chrooted. |
| 91 |
t.Skip("cannot create user namespaces when chrooted") |
| 92 |
} |
| 93 |
// On some systems, there is a sysctl setting. |
| 94 |
if os.Getuid() != 0 { |
| 95 |
skipUnprivilegedUserClone(t) |
| 96 |
} |
| 97 |
// On Centos 7 make sure they set the kernel parameter user_namespace=1 |
| 98 |
// See issue 16283 and 20796. |
| 99 |
if _, err := os.Stat("/sys/module/user_namespace/parameters/enable"); err == nil { |
| 100 |
buf, _ := os.ReadFile("/sys/module/user_namespace/parameters/enabled") |
| 101 |
if !strings.HasPrefix(string(buf), "Y") { |
| 102 |
t.Skip("kernel doesn't support user namespaces") |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// On Centos 7.5+, user namespaces are disabled if user.max_user_namespaces = 0 |
| 107 |
if _, err := os.Stat("/proc/sys/user/max_user_namespaces"); err == nil { |
| 108 |
buf, errRead := os.ReadFile("/proc/sys/user/max_user_namespaces") |
| 109 |
if errRead == nil && buf[0] == '0' { |
| 110 |
t.Skip("kernel doesn't support user namespaces") |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func whoamiCmd(t *testing.T, uid, gid int, setgroups bool) *exec.Cmd { |
| 116 |
checkUserNS(t) |
| 117 |
cmd := exec.Command("whoami") |
| 118 |
cmd.SysProcAttr = &syscall.SysProcAttr{ |
| 119 |
Cloneflags: syscall.CLONE_NEWUSER, |
| 120 |
UidMappings: []syscall.SysProcIDMap{ |
| 121 |
{ContainerID: 0, HostID: uid, Size: 1}, |
| 122 |
}, |
| 123 |
GidMappings: []syscall.SysProcIDMap{ |
| 124 |
{ContainerID: 0, HostID: gid, Size: 1}, |
| 125 |
}, |
| 126 |
GidMappingsEnableSetgroups: setgroups, |
| 127 |
} |
| 128 |
return cmd |
| 129 |
} |
| 130 |
|
| 131 |
func testNEWUSERRemap(t *testing.T, uid, gid int, setgroups bool) { |
| 132 |
cmd := whoamiCmd(t, uid, gid, setgroups) |
| 133 |
out, err := cmd.CombinedOutput() |
| 134 |
if err != nil { |
| 135 |
t.Fatalf("Cmd failed with err %v, output: %s", err, out) |
| 136 |
} |
| 137 |
sout := strings.TrimSpace(string(out)) |
| 138 |
want := "root" |
| 139 |
if sout != want { |
| 140 |
t.Fatalf("whoami = %q; want %q", out, want) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func TestCloneNEWUSERAndRemapRootDisableSetgroups(t *testing.T) { |
| 145 |
if os.Getuid() != 0 { |
| 146 |
t.Skip("skipping root only test") |
| 147 |
} |
| 148 |
testNEWUSERRemap(t, 0, 0, false) |
| 149 |
} |
| 150 |
|
| 151 |
func TestCloneNEWUSERAndRemapRootEnableSetgroups(t *testing.T) { |
| 152 |
if os.Getuid() != 0 { |
| 153 |
t.Skip("skipping root only test") |
| 154 |
} |
| 155 |
testNEWUSERRemap(t, 0, 0, true) |
| 156 |
} |
| 157 |
|
| 158 |
func TestCloneNEWUSERAndRemapNoRootDisableSetgroups(t *testing.T) { |
| 159 |
if os.Getuid() == 0 { |
| 160 |
t.Skip("skipping unprivileged user only test") |
| 161 |
} |
| 162 |
testNEWUSERRemap(t, os.Getuid(), os.Getgid(), false) |
| 163 |
} |
| 164 |
|
| 165 |
func TestCloneNEWUSERAndRemapNoRootSetgroupsEnableSetgroups(t *testing.T) { |
| 166 |
if os.Getuid() == 0 { |
| 167 |
t.Skip("skipping unprivileged user only test") |
| 168 |
} |
| 169 |
cmd := whoamiCmd(t, os.Getuid(), os.Getgid(), true) |
| 170 |
err := cmd.Run() |
| 171 |
if err == nil { |
| 172 |
t.Skip("probably old kernel without security fix") |
| 173 |
} |
| 174 |
if !os.IsPermission(err) { |
| 175 |
t.Fatalf("Unprivileged gid_map rewriting with GidMappingsEnableSetgroups must fail with permission error; got %v", err) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestEmptyCredGroupsDisableSetgroups(t *testing.T) { |
| 180 |
cmd := whoamiCmd(t, os.Getuid(), os.Getgid(), false) |
| 181 |
cmd.SysProcAttr.Credential = &syscall.Credential{} |
| 182 |
if err := cmd.Run(); err != nil { |
| 183 |
t.Fatal(err) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func TestUnshare(t *testing.T) { |
| 188 |
skipInContainer(t) |
| 189 |
// Make sure we are running as root so we have permissions to use unshare |
| 190 |
// and create a network namespace. |
| 191 |
if os.Getuid() != 0 { |
| 192 |
t.Skip("kernel prohibits unshare in unprivileged process, unless using user namespace") |
| 193 |
} |
| 194 |
|
| 195 |
path := "/proc/net/dev" |
| 196 |
if _, err := os.Stat(path); err != nil { |
| 197 |
if os.IsNotExist(err) { |
| 198 |
t.Skip("kernel doesn't support proc filesystem") |
| 199 |
} |
| 200 |
if os.IsPermission(err) { |
| 201 |
t.Skip("unable to test proc filesystem due to permissions") |
| 202 |
} |
| 203 |
t.Fatal(err) |
| 204 |
} |
| 205 |
if _, err := os.Stat("/proc/self/ns/net"); err != nil { |
| 206 |
if os.IsNotExist(err) { |
| 207 |
t.Skip("kernel doesn't support net namespace") |
| 208 |
} |
| 209 |
t.Fatal(err) |
| 210 |
} |
| 211 |
|
| 212 |
orig, err := os.ReadFile(path) |
| 213 |
if err != nil { |
| 214 |
t.Fatal(err) |
| 215 |
} |
| 216 |
origLines := strings.Split(strings.TrimSpace(string(orig)), "\n") |
| 217 |
|
| 218 |
cmd := exec.Command("cat", path) |
| 219 |
cmd.SysProcAttr = &syscall.SysProcAttr{ |
| 220 |
Unshareflags: syscall.CLONE_NEWNET, |
| 221 |
} |
| 222 |
out, err := cmd.CombinedOutput() |
| 223 |
if err != nil { |
| 224 |
if strings.Contains(err.Error(), "operation not permitted") { |
| 225 |
// Issue 17206: despite all the checks above, |
| 226 |
// this still reportedly fails for some users. |
| 227 |
// (older kernels?). Just skip. |
| 228 |
t.Skip("skipping due to permission error") |
| 229 |
} |
| 230 |
t.Fatalf("Cmd failed with err %v, output: %s", err, out) |
| 231 |
} |
| 232 |
|
| 233 |
// Check there is only the local network interface |
| 234 |
sout := strings.TrimSpace(string(out)) |
| 235 |
if !strings.Contains(sout, "lo:") { |
| 236 |
t.Fatalf("Expected lo network interface to exist, got %s", sout) |
| 237 |
} |
| 238 |
|
| 239 |
lines := strings.Split(sout, "\n") |
| 240 |
if len(lines) >= len(origLines) { |
| 241 |
t.Fatalf("Got %d lines of output, want <%d", len(lines), len(origLines)) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
func TestGroupCleanup(t *testing.T) { |
| 246 |
if os.Getuid() != 0 { |
| 247 |
t.Skip("we need root for credential") |
| 248 |
} |
| 249 |
cmd := exec.Command("id") |
| 250 |
cmd.SysProcAttr = &syscall.SysProcAttr{ |
| 251 |
Credential: &syscall.Credential{ |
| 252 |
Uid: 0, |
| 253 |
Gid: 0, |
| 254 |
}, |
| 255 |
} |
| 256 |
out, err := cmd.CombinedOutput() |
| 257 |
if err != nil { |
| 258 |
t.Fatalf("Cmd failed with err %v, output: %s", err, out) |
| 259 |
} |
| 260 |
strOut := strings.TrimSpace(string(out)) |
| 261 |
expected := "uid=0(root) gid=0(root)" |
| 262 |
// Just check prefix because some distros reportedly output a |
| 263 |
// context parameter; see https://golang.org/issue/16224. |
| 264 |
// Alpine does not output groups; see https://golang.org/issue/19938. |
| 265 |
if !strings.HasPrefix(strOut, expected) { |
| 266 |
t.Errorf("id command output: %q, expected prefix: %q", strOut, expected) |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
func TestGroupCleanupUserNamespace(t *testing.T) { |
| 271 |
if os.Getuid() != 0 { |
| 272 |
t.Skip("we need root for credential") |
| 273 |
} |
| 274 |
checkUserNS(t) |
| 275 |
cmd := exec.Command("id") |
| 276 |
uid, gid := os.Getuid(), os.Getgid() |
| 277 |
cmd.SysProcAttr = &syscall.SysProcAttr{ |
| 278 |
Cloneflags: syscall.CLONE_NEWUSER, |
| 279 |
Credential: &syscall.Credential{ |
| 280 |
Uid: uint32(uid), |
| 281 |
Gid: uint32(gid), |
| 282 |
}, |
| 283 |
UidMappings: []syscall.SysProcIDMap{ |
| 284 |
{ContainerID: 0, HostID: uid, Size: 1}, |
| 285 |
}, |
| 286 |
GidMappings: []syscall.SysProcIDMap{ |
| 287 |
{ContainerID: 0, HostID: gid, Size: 1}, |
| 288 |
}, |
| 289 |
} |
| 290 |
out, err := cmd.CombinedOutput() |
| 291 |
if err != nil { |
| 292 |
t.Fatalf("Cmd failed with err %v, output: %s", err, out) |
| 293 |
} |
| 294 |
strOut := strings.TrimSpace(string(out)) |
| 295 |
|
| 296 |
// Strings we've seen in the wild. |
| 297 |
expected := []string{ |
| 298 |
"uid=0(root) gid=0(root) groups=0(root)", |
| 299 |
"uid=0(root) gid=0(root) groups=0(root),65534(nobody)", |
| 300 |
"uid=0(root) gid=0(root) groups=0(root),65534(nogroup)", |
| 301 |
"uid=0(root) gid=0(root) groups=0(root),65534", |
| 302 |
"uid=0(root) gid=0(root) groups=0(root),65534(nobody),65534(nobody),65534(nobody),65534(nobody),65534(nobody),65534(nobody),65534(nobody),65534(nobody),65534(nobody),65534(nobody)", // Alpine; see https://golang.org/issue/19938 |
| 303 |
"uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023", // CentOS with SELinux context, see https://golang.org/issue/34547 |
| 304 |
"uid=0(root) gid=0(root) groups=0(root),65534(nobody) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023", // Fedora with SElinux context, see https://golang.org/issue/46752 |
| 305 |
} |
| 306 |
for _, e := range expected { |
| 307 |
if strOut == e { |
| 308 |
return |
| 309 |
} |
| 310 |
} |
| 311 |
t.Errorf("id command output: %q, expected one of %q", strOut, expected) |
| 312 |
} |
| 313 |
|
| 314 |
// TestUnshareHelperProcess isn't a real test. It's used as a helper process |
| 315 |
// for TestUnshareMountNameSpace. |
| 316 |
func TestUnshareMountNameSpaceHelper(*testing.T) { |
| 317 |
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { |
| 318 |
return |
| 319 |
} |
| 320 |
defer os.Exit(0) |
| 321 |
if err := syscall.Mount("none", flag.Args()[0], "proc", 0, ""); err != nil { |
| 322 |
fmt.Fprintf(os.Stderr, "unshare: mount %v failed: %v", os.Args, err) |
| 323 |
os.Exit(2) |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
// Test for Issue 38471: unshare fails because systemd has forced / to be shared |
| 328 |
func TestUnshareMountNameSpace(t *testing.T) { |
| 329 |
skipInContainer(t) |
| 330 |
// Make sure we are running as root so we have permissions to use unshare |
| 331 |
// and create a network namespace. |
| 332 |
if os.Getuid() != 0 { |
| 333 |
t.Skip("kernel prohibits unshare in unprivileged process, unless using user namespace") |
| 334 |
} |
| 335 |
|
| 336 |
d, err := os.MkdirTemp("", "unshare") |
| 337 |
if err != nil { |
| 338 |
t.Fatalf("tempdir: %v", err) |
| 339 |
} |
| 340 |
|
| 341 |
cmd := exec.Command(os.Args[0], "-test.run=TestUnshareMountNameSpaceHelper", d) |
| 342 |
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") |
| 343 |
cmd.SysProcAttr = &syscall.SysProcAttr{Unshareflags: syscall.CLONE_NEWNS} |
| 344 |
|
| 345 |
o, err := cmd.CombinedOutput() |
| 346 |
if err != nil { |
| 347 |
if strings.Contains(err.Error(), ": permission denied") { |
| 348 |
t.Skipf("Skipping test (golang.org/issue/19698); unshare failed due to permissions: %s, %v", o, err) |
| 349 |
} |
| 350 |
t.Fatalf("unshare failed: %s, %v", o, err) |
| 351 |
} |
| 352 |
|
| 353 |
// How do we tell if the namespace was really unshared? It turns out |
| 354 |
// to be simple: just try to remove the directory. If it's still mounted |
| 355 |
// on the rm will fail with EBUSY. Then we have some cleanup to do: |
| 356 |
// we must unmount it, then try to remove it again. |
| 357 |
|
| 358 |
if err := os.Remove(d); err != nil { |
| 359 |
t.Errorf("rmdir failed on %v: %v", d, err) |
| 360 |
if err := syscall.Unmount(d, syscall.MNT_FORCE); err != nil { |
| 361 |
t.Errorf("Can't unmount %v: %v", d, err) |
| 362 |
} |
| 363 |
if err := os.Remove(d); err != nil { |
| 364 |
t.Errorf("rmdir after unmount failed on %v: %v", d, err) |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
// Test for Issue 20103: unshare fails when chroot is used |
| 370 |
func TestUnshareMountNameSpaceChroot(t *testing.T) { |
| 371 |
skipInContainer(t) |
| 372 |
// Make sure we are running as root so we have permissions to use unshare |
| 373 |
// and create a network namespace. |
| 374 |
if os.Getuid() != 0 { |
| 375 |
t.Skip("kernel prohibits unshare in unprivileged process, unless using user namespace") |
| 376 |
} |
| 377 |
|
| 378 |
d, err := os.MkdirTemp("", "unshare") |
| 379 |
if err != nil { |
| 380 |
t.Fatalf("tempdir: %v", err) |
| 381 |
} |
| 382 |
|
| 383 |
// Since we are doing a chroot, we need the binary there, |
| 384 |
// and it must be statically linked. |
| 385 |
x := filepath.Join(d, "syscall.test") |
| 386 |
cmd := exec.Command(testenv.GoToolPath(t), "test", "-c", "-o", x, "syscall") |
| 387 |
cmd.Env = append(os.Environ(), "CGO_ENABLED=0") |
| 388 |
if o, err := cmd.CombinedOutput(); err != nil { |
| 389 |
t.Fatalf("Build of syscall in chroot failed, output %v, err %v", o, err) |
| 390 |
} |
| 391 |
|
| 392 |
cmd = exec.Command("/syscall.test", "-test.run=TestUnshareMountNameSpaceHelper", "/") |
| 393 |
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") |
| 394 |
cmd.SysProcAttr = &syscall.SysProcAttr{Chroot: d, Unshareflags: syscall.CLONE_NEWNS} |
| 395 |
|
| 396 |
o, err := cmd.CombinedOutput() |
| 397 |
if err != nil { |
| 398 |
if strings.Contains(err.Error(), ": permission denied") { |
| 399 |
t.Skipf("Skipping test (golang.org/issue/19698); unshare failed due to permissions: %s, %v", o, err) |
| 400 |
} |
| 401 |
t.Fatalf("unshare failed: %s, %v", o, err) |
| 402 |
} |
| 403 |
|
| 404 |
// How do we tell if the namespace was really unshared? It turns out |
| 405 |
// to be simple: just try to remove the executable. If it's still mounted |
| 406 |
// on, the rm will fail. Then we have some cleanup to do: |
| 407 |
// we must force unmount it, then try to remove it again. |
| 408 |
|
| 409 |
if err := os.Remove(x); err != nil { |
| 410 |
t.Errorf("rm failed on %v: %v", x, err) |
| 411 |
if err := syscall.Unmount(d, syscall.MNT_FORCE); err != nil { |
| 412 |
t.Fatalf("Can't unmount %v: %v", d, err) |
| 413 |
} |
| 414 |
if err := os.Remove(x); err != nil { |
| 415 |
t.Fatalf("rm failed on %v: %v", x, err) |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
if err := os.Remove(d); err != nil { |
| 420 |
t.Errorf("rmdir failed on %v: %v", d, err) |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
func TestUnshareUidGidMappingHelper(*testing.T) { |
| 425 |
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { |
| 426 |
return |
| 427 |
} |
| 428 |
defer os.Exit(0) |
| 429 |
if err := syscall.Chroot(os.TempDir()); err != nil { |
| 430 |
fmt.Fprintln(os.Stderr, err) |
| 431 |
os.Exit(2) |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
// Test for Issue 29789: unshare fails when uid/gid mapping is specified |
| 436 |
func TestUnshareUidGidMapping(t *testing.T) { |
| 437 |
if os.Getuid() == 0 { |
| 438 |
t.Skip("test exercises unprivileged user namespace, fails with privileges") |
| 439 |
} |
| 440 |
checkUserNS(t) |
| 441 |
cmd := exec.Command(os.Args[0], "-test.run=TestUnshareUidGidMappingHelper") |
| 442 |
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") |
| 443 |
cmd.SysProcAttr = &syscall.SysProcAttr{ |
| 444 |
Unshareflags: syscall.CLONE_NEWNS | syscall.CLONE_NEWUSER, |
| 445 |
GidMappingsEnableSetgroups: false, |
| 446 |
UidMappings: []syscall.SysProcIDMap{ |
| 447 |
{ |
| 448 |
ContainerID: 0, |
| 449 |
HostID: syscall.Getuid(), |
| 450 |
Size: 1, |
| 451 |
}, |
| 452 |
}, |
| 453 |
GidMappings: []syscall.SysProcIDMap{ |
| 454 |
{ |
| 455 |
ContainerID: 0, |
| 456 |
HostID: syscall.Getgid(), |
| 457 |
Size: 1, |
| 458 |
}, |
| 459 |
}, |
| 460 |
} |
| 461 |
out, err := cmd.CombinedOutput() |
| 462 |
if err != nil { |
| 463 |
t.Fatalf("Cmd failed with err %v, output: %s", err, out) |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
type capHeader struct { |
| 468 |
version uint32 |
| 469 |
pid int32 |
| 470 |
} |
| 471 |
|
| 472 |
type capData struct { |
| 473 |
effective uint32 |
| 474 |
permitted uint32 |
| 475 |
inheritable uint32 |
| 476 |
} |
| 477 |
|
| 478 |
const CAP_SYS_TIME = 25 |
| 479 |
const CAP_SYSLOG = 34 |
| 480 |
|
| 481 |
type caps struct { |
| 482 |
hdr capHeader |
| 483 |
data [2]capData |
| 484 |
} |
| 485 |
|
| 486 |
func getCaps() (caps, error) { |
| 487 |
var c caps |
| 488 |
|
| 489 |
// Get capability version |
| 490 |
if _, _, errno := syscall.Syscall(syscall.SYS_CAPGET, uintptr(unsafe.Pointer(&c.hdr)), uintptr(unsafe.Pointer(nil)), 0); errno != 0 { |
| 491 |
return c, fmt.Errorf("SYS_CAPGET: %v", errno) |
| 492 |
} |
| 493 |
|
| 494 |
// Get current capabilities |
| 495 |
if _, _, errno := syscall.Syscall(syscall.SYS_CAPGET, uintptr(unsafe.Pointer(&c.hdr)), uintptr(unsafe.Pointer(&c.data[0])), 0); errno != 0 { |
| 496 |
return c, fmt.Errorf("SYS_CAPGET: %v", errno) |
| 497 |
} |
| 498 |
|
| 499 |
return c, nil |
| 500 |
} |
| 501 |
|
| 502 |
func mustSupportAmbientCaps(t *testing.T) { |
| 503 |
var uname syscall.Utsname |
| 504 |
if err := syscall.Uname(&uname); err != nil { |
| 505 |
t.Fatalf("Uname: %v", err) |
| 506 |
} |
| 507 |
var buf [65]byte |
| 508 |
for i, b := range uname.Release { |
| 509 |
buf[i] = byte(b) |
| 510 |
} |
| 511 |
ver := string(buf[:]) |
| 512 |
ver, _, _ = strings.Cut(ver, "\x00") |
| 513 |
if strings.HasPrefix(ver, "2.") || |
| 514 |
strings.HasPrefix(ver, "3.") || |
| 515 |
strings.HasPrefix(ver, "4.1.") || |
| 516 |
strings.HasPrefix(ver, "4.2.") { |
| 517 |
t.Skipf("kernel version %q predates required 4.3; skipping test", ver) |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
// TestAmbientCapsHelper isn't a real test. It's used as a helper process for |
| 522 |
// TestAmbientCaps. |
| 523 |
func TestAmbientCapsHelper(*testing.T) { |
| 524 |
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { |
| 525 |
return |
| 526 |
} |
| 527 |
defer os.Exit(0) |
| 528 |
|
| 529 |
caps, err := getCaps() |
| 530 |
if err != nil { |
| 531 |
fmt.Fprintln(os.Stderr, err) |
| 532 |
os.Exit(2) |
| 533 |
} |
| 534 |
if caps.data[0].effective&(1<<uint(CAP_SYS_TIME)) == 0 { |
| 535 |
fmt.Fprintln(os.Stderr, "CAP_SYS_TIME unexpectedly not in the effective capability mask") |
| 536 |
os.Exit(2) |
| 537 |
} |
| 538 |
if caps.data[1].effective&(1<<uint(CAP_SYSLOG&31)) == 0 { |
| 539 |
fmt.Fprintln(os.Stderr, "CAP_SYSLOG unexpectedly not in the effective capability mask") |
| 540 |
os.Exit(2) |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
func TestAmbientCaps(t *testing.T) { |
| 545 |
// Make sure we are running as root so we have permissions to use unshare |
| 546 |
// and create a network namespace. |
| 547 |
if os.Getuid() != 0 { |
| 548 |
t.Skip("kernel prohibits unshare in unprivileged process, unless using user namespace") |
| 549 |
} |
| 550 |
|
| 551 |
testAmbientCaps(t, false) |
| 552 |
} |
| 553 |
|
| 554 |
func TestAmbientCapsUserns(t *testing.T) { |
| 555 |
checkUserNS(t) |
| 556 |
testAmbientCaps(t, true) |
| 557 |
} |
| 558 |
|
| 559 |
func testAmbientCaps(t *testing.T, userns bool) { |
| 560 |
skipInContainer(t) |
| 561 |
mustSupportAmbientCaps(t) |
| 562 |
|
| 563 |
skipUnprivilegedUserClone(t) |
| 564 |
|
| 565 |
// skip on android, due to lack of lookup support |
| 566 |
if runtime.GOOS == "android" { |
| 567 |
t.Skip("skipping test on android; see Issue 27327") |
| 568 |
} |
| 569 |
|
| 570 |
u, err := user.Lookup("nobody") |
| 571 |
if err != nil { |
| 572 |
t.Fatal(err) |
| 573 |
} |
| 574 |
uid, err := strconv.ParseInt(u.Uid, 0, 32) |
| 575 |
if err != nil { |
| 576 |
t.Fatal(err) |
| 577 |
} |
| 578 |
gid, err := strconv.ParseInt(u.Gid, 0, 32) |
| 579 |
if err != nil { |
| 580 |
t.Fatal(err) |
| 581 |
} |
| 582 |
|
| 583 |
// Copy the test binary to a temporary location which is readable by nobody. |
| 584 |
f, err := os.CreateTemp("", "gotest") |
| 585 |
if err != nil { |
| 586 |
t.Fatal(err) |
| 587 |
} |
| 588 |
defer os.Remove(f.Name()) |
| 589 |
defer f.Close() |
| 590 |
e, err := os.Open(os.Args[0]) |
| 591 |
if err != nil { |
| 592 |
t.Fatal(err) |
| 593 |
} |
| 594 |
defer e.Close() |
| 595 |
if _, err := io.Copy(f, e); err != nil { |
| 596 |
t.Fatal(err) |
| 597 |
} |
| 598 |
if err := f.Chmod(0755); err != nil { |
| 599 |
t.Fatal(err) |
| 600 |
} |
| 601 |
if err := f.Close(); err != nil { |
| 602 |
t.Fatal(err) |
| 603 |
} |
| 604 |
|
| 605 |
cmd := exec.Command(f.Name(), "-test.run=TestAmbientCapsHelper") |
| 606 |
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") |
| 607 |
cmd.Stdout = os.Stdout |
| 608 |
cmd.Stderr = os.Stderr |
| 609 |
cmd.SysProcAttr = &syscall.SysProcAttr{ |
| 610 |
Credential: &syscall.Credential{ |
| 611 |
Uid: uint32(uid), |
| 612 |
Gid: uint32(gid), |
| 613 |
}, |
| 614 |
AmbientCaps: []uintptr{CAP_SYS_TIME, CAP_SYSLOG}, |
| 615 |
} |
| 616 |
if userns { |
| 617 |
cmd.SysProcAttr.Cloneflags = syscall.CLONE_NEWUSER |
| 618 |
const nobody = 65534 |
| 619 |
uid := os.Getuid() |
| 620 |
gid := os.Getgid() |
| 621 |
cmd.SysProcAttr.UidMappings = []syscall.SysProcIDMap{{ |
| 622 |
ContainerID: int(nobody), |
| 623 |
HostID: int(uid), |
| 624 |
Size: int(1), |
| 625 |
}} |
| 626 |
cmd.SysProcAttr.GidMappings = []syscall.SysProcIDMap{{ |
| 627 |
ContainerID: int(nobody), |
| 628 |
HostID: int(gid), |
| 629 |
Size: int(1), |
| 630 |
}} |
| 631 |
|
| 632 |
// Set credentials to run as user and group nobody. |
| 633 |
cmd.SysProcAttr.Credential = &syscall.Credential{ |
| 634 |
Uid: nobody, |
| 635 |
Gid: nobody, |
| 636 |
} |
| 637 |
} |
| 638 |
if err := cmd.Run(); err != nil { |
| 639 |
t.Fatal(err.Error()) |
| 640 |
} |
| 641 |
} |