| 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 |
package syscall_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"io/fs" |
| 11 |
"os" |
| 12 |
"os/exec" |
| 13 |
"path/filepath" |
| 14 |
"runtime" |
| 15 |
"sort" |
| 16 |
"strconv" |
| 17 |
"strings" |
| 18 |
"sync" |
| 19 |
"syscall" |
| 20 |
"testing" |
| 21 |
"unsafe" |
| 22 |
) |
| 23 |
|
| 24 |
// chtmpdir changes the working directory to a new temporary directory and |
| 25 |
// provides a cleanup function. Used when PWD is read-only. |
| 26 |
func chtmpdir(t *testing.T) func() { |
| 27 |
oldwd, err := os.Getwd() |
| 28 |
if err != nil { |
| 29 |
t.Fatalf("chtmpdir: %v", err) |
| 30 |
} |
| 31 |
d, err := os.MkdirTemp("", "test") |
| 32 |
if err != nil { |
| 33 |
t.Fatalf("chtmpdir: %v", err) |
| 34 |
} |
| 35 |
if err := os.Chdir(d); err != nil { |
| 36 |
t.Fatalf("chtmpdir: %v", err) |
| 37 |
} |
| 38 |
return func() { |
| 39 |
if err := os.Chdir(oldwd); err != nil { |
| 40 |
t.Fatalf("chtmpdir: %v", err) |
| 41 |
} |
| 42 |
os.RemoveAll(d) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func touch(t *testing.T, name string) { |
| 47 |
f, err := os.Create(name) |
| 48 |
if err != nil { |
| 49 |
t.Fatal(err) |
| 50 |
} |
| 51 |
if err := f.Close(); err != nil { |
| 52 |
t.Fatal(err) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
const ( |
| 57 |
_AT_SYMLINK_NOFOLLOW = 0x100 |
| 58 |
_AT_FDCWD = -0x64 |
| 59 |
_AT_EACCESS = 0x200 |
| 60 |
_F_OK = 0 |
| 61 |
_R_OK = 4 |
| 62 |
) |
| 63 |
|
| 64 |
func TestFaccessat(t *testing.T) { |
| 65 |
defer chtmpdir(t)() |
| 66 |
touch(t, "file1") |
| 67 |
|
| 68 |
err := syscall.Faccessat(_AT_FDCWD, "file1", _R_OK, 0) |
| 69 |
if err != nil { |
| 70 |
t.Errorf("Faccessat: unexpected error: %v", err) |
| 71 |
} |
| 72 |
|
| 73 |
err = syscall.Faccessat(_AT_FDCWD, "file1", _R_OK, 2) |
| 74 |
if err != syscall.EINVAL { |
| 75 |
t.Errorf("Faccessat: unexpected error: %v, want EINVAL", err) |
| 76 |
} |
| 77 |
|
| 78 |
err = syscall.Faccessat(_AT_FDCWD, "file1", _R_OK, _AT_EACCESS) |
| 79 |
if err != nil { |
| 80 |
t.Errorf("Faccessat: unexpected error: %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
err = os.Symlink("file1", "symlink1") |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
|
| 88 |
err = syscall.Faccessat(_AT_FDCWD, "symlink1", _R_OK, _AT_SYMLINK_NOFOLLOW) |
| 89 |
if err != nil { |
| 90 |
t.Errorf("Faccessat SYMLINK_NOFOLLOW: unexpected error %v", err) |
| 91 |
} |
| 92 |
|
| 93 |
// We can't really test _AT_SYMLINK_NOFOLLOW, because there |
| 94 |
// doesn't seem to be any way to change the mode of a symlink. |
| 95 |
// We don't test _AT_EACCESS because such tests are only |
| 96 |
// meaningful if run as root. |
| 97 |
|
| 98 |
err = syscall.Fchmodat(_AT_FDCWD, "file1", 0, 0) |
| 99 |
if err != nil { |
| 100 |
t.Errorf("Fchmodat: unexpected error %v", err) |
| 101 |
} |
| 102 |
|
| 103 |
err = syscall.Faccessat(_AT_FDCWD, "file1", _F_OK, _AT_SYMLINK_NOFOLLOW) |
| 104 |
if err != nil { |
| 105 |
t.Errorf("Faccessat: unexpected error: %v", err) |
| 106 |
} |
| 107 |
|
| 108 |
err = syscall.Faccessat(_AT_FDCWD, "file1", _R_OK, _AT_SYMLINK_NOFOLLOW) |
| 109 |
if err != syscall.EACCES { |
| 110 |
if syscall.Getuid() != 0 { |
| 111 |
t.Errorf("Faccessat: unexpected error: %v, want EACCES", err) |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func TestFchmodat(t *testing.T) { |
| 117 |
defer chtmpdir(t)() |
| 118 |
|
| 119 |
touch(t, "file1") |
| 120 |
os.Symlink("file1", "symlink1") |
| 121 |
|
| 122 |
err := syscall.Fchmodat(_AT_FDCWD, "symlink1", 0444, 0) |
| 123 |
if err != nil { |
| 124 |
t.Fatalf("Fchmodat: unexpected error: %v", err) |
| 125 |
} |
| 126 |
|
| 127 |
fi, err := os.Stat("file1") |
| 128 |
if err != nil { |
| 129 |
t.Fatal(err) |
| 130 |
} |
| 131 |
|
| 132 |
if fi.Mode() != 0444 { |
| 133 |
t.Errorf("Fchmodat: failed to change mode: expected %v, got %v", 0444, fi.Mode()) |
| 134 |
} |
| 135 |
|
| 136 |
err = syscall.Fchmodat(_AT_FDCWD, "symlink1", 0444, _AT_SYMLINK_NOFOLLOW) |
| 137 |
if err != syscall.EOPNOTSUPP { |
| 138 |
t.Fatalf("Fchmodat: unexpected error: %v, expected EOPNOTSUPP", err) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestMain(m *testing.M) { |
| 143 |
if os.Getenv("GO_DEATHSIG_PARENT") == "1" { |
| 144 |
deathSignalParent() |
| 145 |
} else if os.Getenv("GO_DEATHSIG_CHILD") == "1" { |
| 146 |
deathSignalChild() |
| 147 |
} else if os.Getenv("GO_SYSCALL_NOERROR") == "1" { |
| 148 |
syscallNoError() |
| 149 |
} |
| 150 |
|
| 151 |
os.Exit(m.Run()) |
| 152 |
} |
| 153 |
|
| 154 |
func TestParseNetlinkMessage(t *testing.T) { |
| 155 |
for i, b := range [][]byte{ |
| 156 |
{103, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 11, 0, 1, 0, 0, 0, 0, 5, 8, 0, 3, |
| 157 |
0, 8, 0, 6, 0, 0, 0, 0, 1, 63, 0, 10, 0, 69, 16, 0, 59, 39, 82, 64, 0, 64, 6, 21, 89, 127, 0, 0, |
| 158 |
1, 127, 0, 0, 1, 230, 228, 31, 144, 32, 186, 155, 211, 185, 151, 209, 179, 128, 24, 1, 86, |
| 159 |
53, 119, 0, 0, 1, 1, 8, 10, 0, 17, 234, 12, 0, 17, 189, 126, 107, 106, 108, 107, 106, 13, 10, |
| 160 |
}, |
| 161 |
{106, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 11, 0, 1, 0, 0, 0, 0, 3, 8, 0, 3, |
| 162 |
0, 8, 0, 6, 0, 0, 0, 0, 1, 66, 0, 10, 0, 69, 0, 0, 62, 230, 255, 64, 0, 64, 6, 85, 184, 127, 0, 0, |
| 163 |
1, 127, 0, 0, 1, 237, 206, 31, 144, 73, 197, 128, 65, 250, 60, 192, 97, 128, 24, 1, 86, 253, 21, 0, |
| 164 |
0, 1, 1, 8, 10, 0, 51, 106, 89, 0, 51, 102, 198, 108, 104, 106, 108, 107, 104, 108, 107, 104, 10, |
| 165 |
}, |
| 166 |
{102, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 11, 0, 1, 0, 0, 0, 0, 1, 8, 0, 3, 0, |
| 167 |
8, 0, 6, 0, 0, 0, 0, 1, 62, 0, 10, 0, 69, 0, 0, 58, 231, 2, 64, 0, 64, 6, 85, 185, 127, 0, 0, 1, 127, |
| 168 |
0, 0, 1, 237, 206, 31, 144, 73, 197, 128, 86, 250, 60, 192, 97, 128, 24, 1, 86, 104, 64, 0, 0, 1, 1, 8, |
| 169 |
10, 0, 52, 198, 200, 0, 51, 135, 232, 101, 115, 97, 103, 103, 10, |
| 170 |
}, |
| 171 |
} { |
| 172 |
m, err := syscall.ParseNetlinkMessage(b) |
| 173 |
if err != syscall.EINVAL { |
| 174 |
t.Errorf("#%d: got %v; want EINVAL", i, err) |
| 175 |
} |
| 176 |
if m != nil { |
| 177 |
t.Errorf("#%d: got %v; want nil", i, m) |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestSyscallNoError(t *testing.T) { |
| 183 |
// On Linux there are currently no syscalls which don't fail and return |
| 184 |
// a value larger than 0xfffffffffffff001 so we could test RawSyscall |
| 185 |
// vs. RawSyscallNoError on 64bit architectures. |
| 186 |
if unsafe.Sizeof(uintptr(0)) != 4 { |
| 187 |
t.Skip("skipping on non-32bit architecture") |
| 188 |
} |
| 189 |
|
| 190 |
// See https://golang.org/issue/35422 |
| 191 |
// On MIPS, Linux returns whether the syscall had an error in a separate |
| 192 |
// register (R7), not using a negative return value as on other |
| 193 |
// architectures. |
| 194 |
if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" { |
| 195 |
t.Skipf("skipping on %s", runtime.GOARCH) |
| 196 |
} |
| 197 |
|
| 198 |
if os.Getuid() != 0 { |
| 199 |
t.Skip("skipping root only test") |
| 200 |
} |
| 201 |
|
| 202 |
if syscall.Sys_GETEUID == 0 { |
| 203 |
t.Skip("skipping because there is no geteuid system call") |
| 204 |
} |
| 205 |
|
| 206 |
if runtime.GOOS == "android" { |
| 207 |
t.Skip("skipping on rooted android, see issue 27364") |
| 208 |
} |
| 209 |
|
| 210 |
// Copy the test binary to a location that a non-root user can read/execute |
| 211 |
// after we drop privileges |
| 212 |
tempDir, err := os.MkdirTemp("", "TestSyscallNoError") |
| 213 |
if err != nil { |
| 214 |
t.Fatalf("cannot create temporary directory: %v", err) |
| 215 |
} |
| 216 |
defer os.RemoveAll(tempDir) |
| 217 |
os.Chmod(tempDir, 0755) |
| 218 |
|
| 219 |
tmpBinary := filepath.Join(tempDir, filepath.Base(os.Args[0])) |
| 220 |
|
| 221 |
src, err := os.Open(os.Args[0]) |
| 222 |
if err != nil { |
| 223 |
t.Fatalf("cannot open binary %q, %v", os.Args[0], err) |
| 224 |
} |
| 225 |
defer src.Close() |
| 226 |
|
| 227 |
dst, err := os.OpenFile(tmpBinary, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755) |
| 228 |
if err != nil { |
| 229 |
t.Fatalf("cannot create temporary binary %q, %v", tmpBinary, err) |
| 230 |
} |
| 231 |
if _, err := io.Copy(dst, src); err != nil { |
| 232 |
t.Fatalf("failed to copy test binary to %q, %v", tmpBinary, err) |
| 233 |
} |
| 234 |
err = dst.Close() |
| 235 |
if err != nil { |
| 236 |
t.Fatalf("failed to close test binary %q, %v", tmpBinary, err) |
| 237 |
} |
| 238 |
|
| 239 |
uid := uint32(0xfffffffe) |
| 240 |
err = os.Chown(tmpBinary, int(uid), -1) |
| 241 |
if err != nil { |
| 242 |
t.Fatalf("failed to chown test binary %q, %v", tmpBinary, err) |
| 243 |
} |
| 244 |
|
| 245 |
err = os.Chmod(tmpBinary, 0755|fs.ModeSetuid) |
| 246 |
if err != nil { |
| 247 |
t.Fatalf("failed to set setuid bit on test binary %q, %v", tmpBinary, err) |
| 248 |
} |
| 249 |
|
| 250 |
cmd := exec.Command(tmpBinary) |
| 251 |
cmd.Env = append(os.Environ(), "GO_SYSCALL_NOERROR=1") |
| 252 |
|
| 253 |
out, err := cmd.CombinedOutput() |
| 254 |
if err != nil { |
| 255 |
t.Fatalf("failed to start first child process: %v", err) |
| 256 |
} |
| 257 |
|
| 258 |
got := strings.TrimSpace(string(out)) |
| 259 |
want := strconv.FormatUint(uint64(uid)+1, 10) + " / " + |
| 260 |
strconv.FormatUint(uint64(-uid), 10) + " / " + |
| 261 |
strconv.FormatUint(uint64(uid), 10) |
| 262 |
if got != want { |
| 263 |
if filesystemIsNoSUID(tmpBinary) { |
| 264 |
t.Skip("skipping test when temp dir is mounted nosuid") |
| 265 |
} |
| 266 |
// formatted so the values are aligned for easier comparison |
| 267 |
t.Errorf("expected %s,\ngot %s", want, got) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
// filesystemIsNoSUID reports whether the filesystem for the given |
| 272 |
// path is mounted nosuid. |
| 273 |
func filesystemIsNoSUID(path string) bool { |
| 274 |
var st syscall.Statfs_t |
| 275 |
if syscall.Statfs(path, &st) != nil { |
| 276 |
return false |
| 277 |
} |
| 278 |
return st.Flags&syscall.MS_NOSUID != 0 |
| 279 |
} |
| 280 |
|
| 281 |
func syscallNoError() { |
| 282 |
// Test that the return value from SYS_GETEUID32 (which cannot fail) |
| 283 |
// doesn't get treated as an error (see https://golang.org/issue/22924) |
| 284 |
euid1, _, e := syscall.RawSyscall(syscall.Sys_GETEUID, 0, 0, 0) |
| 285 |
euid2, _ := syscall.RawSyscallNoError(syscall.Sys_GETEUID, 0, 0, 0) |
| 286 |
|
| 287 |
fmt.Println(uintptr(euid1), "/", int(e), "/", uintptr(euid2)) |
| 288 |
os.Exit(0) |
| 289 |
} |
| 290 |
|
| 291 |
// reference uapi/linux/prctl.h |
| 292 |
const ( |
| 293 |
PR_GET_KEEPCAPS uintptr = 7 |
| 294 |
PR_SET_KEEPCAPS = 8 |
| 295 |
) |
| 296 |
|
| 297 |
// TestAllThreadsSyscall tests that the go runtime can perform |
| 298 |
// syscalls that execute on all OSThreads - with which to support |
| 299 |
// POSIX semantics for security state changes. |
| 300 |
func TestAllThreadsSyscall(t *testing.T) { |
| 301 |
if _, _, err := syscall.AllThreadsSyscall(syscall.SYS_PRCTL, PR_SET_KEEPCAPS, 0, 0); err == syscall.ENOTSUP { |
| 302 |
t.Skip("AllThreadsSyscall disabled with cgo") |
| 303 |
} |
| 304 |
|
| 305 |
fns := []struct { |
| 306 |
label string |
| 307 |
fn func(uintptr) error |
| 308 |
}{ |
| 309 |
{ |
| 310 |
label: "prctl<3-args>", |
| 311 |
fn: func(v uintptr) error { |
| 312 |
_, _, e := syscall.AllThreadsSyscall(syscall.SYS_PRCTL, PR_SET_KEEPCAPS, v, 0) |
| 313 |
if e != 0 { |
| 314 |
return e |
| 315 |
} |
| 316 |
return nil |
| 317 |
}, |
| 318 |
}, |
| 319 |
{ |
| 320 |
label: "prctl<6-args>", |
| 321 |
fn: func(v uintptr) error { |
| 322 |
_, _, e := syscall.AllThreadsSyscall6(syscall.SYS_PRCTL, PR_SET_KEEPCAPS, v, 0, 0, 0, 0) |
| 323 |
if e != 0 { |
| 324 |
return e |
| 325 |
} |
| 326 |
return nil |
| 327 |
}, |
| 328 |
}, |
| 329 |
} |
| 330 |
|
| 331 |
waiter := func(q <-chan uintptr, r chan<- uintptr, once bool) { |
| 332 |
for x := range q { |
| 333 |
runtime.LockOSThread() |
| 334 |
v, _, e := syscall.Syscall(syscall.SYS_PRCTL, PR_GET_KEEPCAPS, 0, 0) |
| 335 |
if e != 0 { |
| 336 |
t.Errorf("tid=%d prctl(PR_GET_KEEPCAPS) failed: %v", syscall.Gettid(), e) |
| 337 |
} else if x != v { |
| 338 |
t.Errorf("tid=%d prctl(PR_GET_KEEPCAPS) mismatch: got=%d want=%d", syscall.Gettid(), v, x) |
| 339 |
} |
| 340 |
r <- v |
| 341 |
if once { |
| 342 |
break |
| 343 |
} |
| 344 |
runtime.UnlockOSThread() |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
// launches per fns member. |
| 349 |
const launches = 11 |
| 350 |
question := make(chan uintptr) |
| 351 |
response := make(chan uintptr) |
| 352 |
defer close(question) |
| 353 |
|
| 354 |
routines := 0 |
| 355 |
for i, v := range fns { |
| 356 |
for j := 0; j < launches; j++ { |
| 357 |
// Add another goroutine - the closest thing |
| 358 |
// we can do to encourage more OS thread |
| 359 |
// creation - while the test is running. The |
| 360 |
// actual thread creation may or may not be |
| 361 |
// needed, based on the number of available |
| 362 |
// unlocked OS threads at the time waiter |
| 363 |
// calls runtime.LockOSThread(), but the goal |
| 364 |
// of doing this every time through the loop |
| 365 |
// is to race thread creation with v.fn(want) |
| 366 |
// being executed. Via the once boolean we |
| 367 |
// also encourage one in 5 waiters to return |
| 368 |
// locked after participating in only one |
| 369 |
// question response sequence. This allows the |
| 370 |
// test to race thread destruction too. |
| 371 |
once := routines%5 == 4 |
| 372 |
go waiter(question, response, once) |
| 373 |
|
| 374 |
// Keep a count of how many goroutines are |
| 375 |
// going to participate in the |
| 376 |
// question/response test. This will count up |
| 377 |
// towards 2*launches minus the count of |
| 378 |
// routines that have been invoked with |
| 379 |
// once=true. |
| 380 |
routines++ |
| 381 |
|
| 382 |
// Decide what value we want to set the |
| 383 |
// process-shared KEEPCAPS. Note, there is |
| 384 |
// an explicit repeat of 0 when we change the |
| 385 |
// variant of the syscall being used. |
| 386 |
want := uintptr(j & 1) |
| 387 |
|
| 388 |
// Invoke the AllThreadsSyscall* variant. |
| 389 |
if err := v.fn(want); err != nil { |
| 390 |
t.Errorf("[%d,%d] %s(PR_SET_KEEPCAPS, %d, ...): %v", i, j, v.label, j&1, err) |
| 391 |
} |
| 392 |
|
| 393 |
// At this point, we want all launched Go |
| 394 |
// routines to confirm that they see the |
| 395 |
// wanted value for KEEPCAPS. |
| 396 |
for k := 0; k < routines; k++ { |
| 397 |
question <- want |
| 398 |
} |
| 399 |
|
| 400 |
// At this point, we should have a large |
| 401 |
// number of locked OS threads all wanting to |
| 402 |
// reply. |
| 403 |
for k := 0; k < routines; k++ { |
| 404 |
if got := <-response; got != want { |
| 405 |
t.Errorf("[%d,%d,%d] waiter result got=%d, want=%d", i, j, k, got, want) |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
// Provide an explicit opportunity for this Go |
| 410 |
// routine to change Ms. |
| 411 |
runtime.Gosched() |
| 412 |
|
| 413 |
if once { |
| 414 |
// One waiter routine will have exited. |
| 415 |
routines-- |
| 416 |
} |
| 417 |
|
| 418 |
// Whatever M we are now running on, confirm |
| 419 |
// we see the wanted value too. |
| 420 |
if v, _, e := syscall.Syscall(syscall.SYS_PRCTL, PR_GET_KEEPCAPS, 0, 0); e != 0 { |
| 421 |
t.Errorf("[%d,%d] prctl(PR_GET_KEEPCAPS) failed: %v", i, j, e) |
| 422 |
} else if v != want { |
| 423 |
t.Errorf("[%d,%d] prctl(PR_GET_KEEPCAPS) gave wrong value: got=%v, want=1", i, j, v) |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
// compareStatus is used to confirm the contents of the thread |
| 430 |
// specific status files match expectations. |
| 431 |
func compareStatus(filter, expect string) error { |
| 432 |
expected := filter + expect |
| 433 |
pid := syscall.Getpid() |
| 434 |
fs, err := os.ReadDir(fmt.Sprintf("/proc/%d/task", pid)) |
| 435 |
if err != nil { |
| 436 |
return fmt.Errorf("unable to find %d tasks: %v", pid, err) |
| 437 |
} |
| 438 |
expectedProc := fmt.Sprintf("Pid:\t%d", pid) |
| 439 |
foundAThread := false |
| 440 |
for _, f := range fs { |
| 441 |
tf := fmt.Sprintf("/proc/%s/status", f.Name()) |
| 442 |
d, err := os.ReadFile(tf) |
| 443 |
if err != nil { |
| 444 |
// There are a surprising number of ways this |
| 445 |
// can error out on linux. We've seen all of |
| 446 |
// the following, so treat any error here as |
| 447 |
// equivalent to the "process is gone": |
| 448 |
// os.IsNotExist(err), |
| 449 |
// "... : no such process", |
| 450 |
// "... : bad file descriptor. |
| 451 |
continue |
| 452 |
} |
| 453 |
lines := strings.Split(string(d), "\n") |
| 454 |
for _, line := range lines { |
| 455 |
// Different kernel vintages pad differently. |
| 456 |
line = strings.TrimSpace(line) |
| 457 |
if strings.HasPrefix(line, "Pid:\t") { |
| 458 |
// On loaded systems, it is possible |
| 459 |
// for a TID to be reused really |
| 460 |
// quickly. As such, we need to |
| 461 |
// validate that the thread status |
| 462 |
// info we just read is a task of the |
| 463 |
// same process PID as we are |
| 464 |
// currently running, and not a |
| 465 |
// recently terminated thread |
| 466 |
// resurfaced in a different process. |
| 467 |
if line != expectedProc { |
| 468 |
break |
| 469 |
} |
| 470 |
// Fall through in the unlikely case |
| 471 |
// that filter at some point is |
| 472 |
// "Pid:\t". |
| 473 |
} |
| 474 |
if strings.HasPrefix(line, filter) { |
| 475 |
if line == expected { |
| 476 |
foundAThread = true |
| 477 |
break |
| 478 |
} |
| 479 |
if filter == "Groups:" && strings.HasPrefix(line, "Groups:\t") { |
| 480 |
// https://github.com/golang/go/issues/46145 |
| 481 |
// Containers don't reliably output this line in sorted order so manually sort and compare that. |
| 482 |
a := strings.Split(line[8:], " ") |
| 483 |
sort.Strings(a) |
| 484 |
got := strings.Join(a, " ") |
| 485 |
if got == expected[8:] { |
| 486 |
foundAThread = true |
| 487 |
break |
| 488 |
} |
| 489 |
|
| 490 |
} |
| 491 |
return fmt.Errorf("%q got:%q want:%q (bad) [pid=%d file:'%s' %v]\n", tf, line, expected, pid, string(d), expectedProc) |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
if !foundAThread { |
| 496 |
return fmt.Errorf("found no thread /proc/<TID>/status files for process %q", expectedProc) |
| 497 |
} |
| 498 |
return nil |
| 499 |
} |
| 500 |
|
| 501 |
// killAThread locks the goroutine to an OS thread and exits; this |
| 502 |
// causes an OS thread to terminate. |
| 503 |
func killAThread(c <-chan struct{}) { |
| 504 |
runtime.LockOSThread() |
| 505 |
<-c |
| 506 |
return |
| 507 |
} |
| 508 |
|
| 509 |
// TestSetuidEtc performs tests on all of the wrapped system calls |
| 510 |
// that mirror to the 9 glibc syscalls with POSIX semantics. The test |
| 511 |
// here is considered authoritative and should compile and run |
| 512 |
// CGO_ENABLED=0 or 1. Note, there is an extended copy of this same |
| 513 |
// test in ../../misc/cgo/test/issue1435.go which requires |
| 514 |
// CGO_ENABLED=1 and launches pthreads from C that run concurrently |
| 515 |
// with the Go code of the test - and the test validates that these |
| 516 |
// pthreads are also kept in sync with the security state changed with |
| 517 |
// the syscalls. Care should be taken to mirror any enhancements to |
| 518 |
// this test here in that file too. |
| 519 |
func TestSetuidEtc(t *testing.T) { |
| 520 |
if syscall.Getuid() != 0 { |
| 521 |
t.Skip("skipping root only test") |
| 522 |
} |
| 523 |
vs := []struct { |
| 524 |
call string |
| 525 |
fn func() error |
| 526 |
filter, expect string |
| 527 |
}{ |
| 528 |
{call: "Setegid(1)", fn: func() error { return syscall.Setegid(1) }, filter: "Gid:", expect: "\t0\t1\t0\t1"}, |
| 529 |
{call: "Setegid(0)", fn: func() error { return syscall.Setegid(0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"}, |
| 530 |
|
| 531 |
{call: "Seteuid(1)", fn: func() error { return syscall.Seteuid(1) }, filter: "Uid:", expect: "\t0\t1\t0\t1"}, |
| 532 |
{call: "Setuid(0)", fn: func() error { return syscall.Setuid(0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"}, |
| 533 |
|
| 534 |
{call: "Setgid(1)", fn: func() error { return syscall.Setgid(1) }, filter: "Gid:", expect: "\t1\t1\t1\t1"}, |
| 535 |
{call: "Setgid(0)", fn: func() error { return syscall.Setgid(0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"}, |
| 536 |
|
| 537 |
{call: "Setgroups([]int{0,1,2,3})", fn: func() error { return syscall.Setgroups([]int{0, 1, 2, 3}) }, filter: "Groups:", expect: "\t0 1 2 3"}, |
| 538 |
{call: "Setgroups(nil)", fn: func() error { return syscall.Setgroups(nil) }, filter: "Groups:", expect: ""}, |
| 539 |
{call: "Setgroups([]int{0})", fn: func() error { return syscall.Setgroups([]int{0}) }, filter: "Groups:", expect: "\t0"}, |
| 540 |
|
| 541 |
{call: "Setregid(101,0)", fn: func() error { return syscall.Setregid(101, 0) }, filter: "Gid:", expect: "\t101\t0\t0\t0"}, |
| 542 |
{call: "Setregid(0,102)", fn: func() error { return syscall.Setregid(0, 102) }, filter: "Gid:", expect: "\t0\t102\t102\t102"}, |
| 543 |
{call: "Setregid(0,0)", fn: func() error { return syscall.Setregid(0, 0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"}, |
| 544 |
|
| 545 |
{call: "Setreuid(1,0)", fn: func() error { return syscall.Setreuid(1, 0) }, filter: "Uid:", expect: "\t1\t0\t0\t0"}, |
| 546 |
{call: "Setreuid(0,2)", fn: func() error { return syscall.Setreuid(0, 2) }, filter: "Uid:", expect: "\t0\t2\t2\t2"}, |
| 547 |
{call: "Setreuid(0,0)", fn: func() error { return syscall.Setreuid(0, 0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"}, |
| 548 |
|
| 549 |
{call: "Setresgid(101,0,102)", fn: func() error { return syscall.Setresgid(101, 0, 102) }, filter: "Gid:", expect: "\t101\t0\t102\t0"}, |
| 550 |
{call: "Setresgid(0,102,101)", fn: func() error { return syscall.Setresgid(0, 102, 101) }, filter: "Gid:", expect: "\t0\t102\t101\t102"}, |
| 551 |
{call: "Setresgid(0,0,0)", fn: func() error { return syscall.Setresgid(0, 0, 0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"}, |
| 552 |
|
| 553 |
{call: "Setresuid(1,0,2)", fn: func() error { return syscall.Setresuid(1, 0, 2) }, filter: "Uid:", expect: "\t1\t0\t2\t0"}, |
| 554 |
{call: "Setresuid(0,2,1)", fn: func() error { return syscall.Setresuid(0, 2, 1) }, filter: "Uid:", expect: "\t0\t2\t1\t2"}, |
| 555 |
{call: "Setresuid(0,0,0)", fn: func() error { return syscall.Setresuid(0, 0, 0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"}, |
| 556 |
} |
| 557 |
|
| 558 |
for i, v := range vs { |
| 559 |
// Generate some thread churn as we execute the tests. |
| 560 |
c := make(chan struct{}) |
| 561 |
go killAThread(c) |
| 562 |
close(c) |
| 563 |
|
| 564 |
if err := v.fn(); err != nil { |
| 565 |
t.Errorf("[%d] %q failed: %v", i, v.call, err) |
| 566 |
continue |
| 567 |
} |
| 568 |
if err := compareStatus(v.filter, v.expect); err != nil { |
| 569 |
t.Errorf("[%d] %q comparison: %v", i, v.call, err) |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
// TestAllThreadsSyscallError verifies that errors are properly returned when |
| 575 |
// the syscall fails on the original thread. |
| 576 |
func TestAllThreadsSyscallError(t *testing.T) { |
| 577 |
// SYS_CAPGET takes pointers as the first two arguments. Since we pass |
| 578 |
// 0, we expect to get EFAULT back. |
| 579 |
r1, r2, err := syscall.AllThreadsSyscall(syscall.SYS_CAPGET, 0, 0, 0) |
| 580 |
if err == syscall.ENOTSUP { |
| 581 |
t.Skip("AllThreadsSyscall disabled with cgo") |
| 582 |
} |
| 583 |
if err != syscall.EFAULT { |
| 584 |
t.Errorf("AllThreadSyscall(SYS_CAPGET) got %d, %d, %v, want err %v", r1, r2, err, syscall.EFAULT) |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
// TestAllThreadsSyscallBlockedSyscall confirms that AllThreadsSyscall |
| 589 |
// can interrupt threads in long-running system calls. This test will |
| 590 |
// deadlock if this doesn't work correctly. |
| 591 |
func TestAllThreadsSyscallBlockedSyscall(t *testing.T) { |
| 592 |
if _, _, err := syscall.AllThreadsSyscall(syscall.SYS_PRCTL, PR_SET_KEEPCAPS, 0, 0); err == syscall.ENOTSUP { |
| 593 |
t.Skip("AllThreadsSyscall disabled with cgo") |
| 594 |
} |
| 595 |
|
| 596 |
rd, wr, err := os.Pipe() |
| 597 |
if err != nil { |
| 598 |
t.Fatalf("unable to obtain a pipe: %v", err) |
| 599 |
} |
| 600 |
|
| 601 |
// Perform a blocking read on the pipe. |
| 602 |
var wg sync.WaitGroup |
| 603 |
ready := make(chan bool) |
| 604 |
wg.Add(1) |
| 605 |
go func() { |
| 606 |
data := make([]byte, 1) |
| 607 |
|
| 608 |
// To narrow the window we have to wait for this |
| 609 |
// goroutine to block in read, synchronize just before |
| 610 |
// calling read. |
| 611 |
ready <- true |
| 612 |
|
| 613 |
// We use syscall.Read directly to avoid the poller. |
| 614 |
// This will return when the write side is closed. |
| 615 |
n, err := syscall.Read(int(rd.Fd()), data) |
| 616 |
if !(n == 0 && err == nil) { |
| 617 |
t.Errorf("expected read to return 0, got %d, %s", n, err) |
| 618 |
} |
| 619 |
|
| 620 |
// Clean up rd and also ensure rd stays reachable so |
| 621 |
// it doesn't get closed by GC. |
| 622 |
rd.Close() |
| 623 |
wg.Done() |
| 624 |
}() |
| 625 |
<-ready |
| 626 |
|
| 627 |
// Loop here to give the goroutine more time to block in read. |
| 628 |
// Generally this will trigger on the first iteration anyway. |
| 629 |
pid := syscall.Getpid() |
| 630 |
for i := 0; i < 100; i++ { |
| 631 |
if id, _, e := syscall.AllThreadsSyscall(syscall.SYS_GETPID, 0, 0, 0); e != 0 { |
| 632 |
t.Errorf("[%d] getpid failed: %v", i, e) |
| 633 |
} else if int(id) != pid { |
| 634 |
t.Errorf("[%d] getpid got=%d, want=%d", i, id, pid) |
| 635 |
} |
| 636 |
// Provide an explicit opportunity for this goroutine |
| 637 |
// to change Ms. |
| 638 |
runtime.Gosched() |
| 639 |
} |
| 640 |
wr.Close() |
| 641 |
wg.Wait() |
| 642 |
} |