From da31bd327af904dd4721b4eefa7c505bb3afd214 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 14 Jan 2022 09:51:14 -0500 Subject: [PATCH] unix: lock the thread for TestPrctlRetInt The prctl system calls used in this test modify and examine the properties of the current thread. For the test to work correctly (and to avoid interfering with other tests), the thread must be locked for those calls and should not be reused when the test completes. Fixes golang/go#50607 Change-Id: I2b14e6c0440155f4287d97f0bb72099af291ba0d Reviewed-on: https://go-review.googlesource.com/c/sys/+/378555 Trust: Bryan Mills Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- unix/syscall_linux_test.go | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/unix/syscall_linux_test.go b/unix/syscall_linux_test.go index 3a07cfec..67b2f5de 100644 --- a/unix/syscall_linux_test.go +++ b/unix/syscall_linux_test.go @@ -815,16 +815,35 @@ func TestEpoll(t *testing.T) { } func TestPrctlRetInt(t *testing.T) { - err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) - if err != nil { - t.Skipf("Prctl: %v, skipping test", err) + skipc := make(chan bool, 1) + skip := func() { + skipc <- true + runtime.Goexit() } - v, err := unix.PrctlRetInt(unix.PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) - if err != nil { - t.Fatalf("failed to perform prctl: %v", err) - } - if v != 1 { - t.Fatalf("unexpected return from prctl; got %v, expected %v", v, 1) + + go func() { + // This test uses prctl to modify the calling thread, so run it on its own + // throwaway thread and do not unlock it when the goroutine exits. + runtime.LockOSThread() + defer close(skipc) + + err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) + if err != nil { + t.Logf("Prctl: %v, skipping test", err) + skip() + } + + v, err := unix.PrctlRetInt(unix.PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) + if err != nil { + t.Errorf("failed to perform prctl: %v", err) + } + if v != 1 { + t.Errorf("unexpected return from prctl; got %v, expected %v", v, 1) + } + }() + + if <-skipc { + t.SkipNow() } }