1 // Copyright 2019 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.
5 //go:build linux && cgo
20 // #include <stdlib.h>
21 // #include <pthread.h>
22 // #include <unistd.h>
23 // #include <sys/types.h>
25 // pthread_t *t = NULL;
26 // pthread_mutex_t mu;
30 // static void *aFn(void *vargp) {
34 // pthread_mutex_lock(&mu);
36 // pthread_mutex_unlock(&mu);
41 // void trial(int argc) {
44 // t = calloc(nts, sizeof(pthread_t));
45 // pthread_mutex_init(&mu, NULL);
46 // for (i = 0; i < nts; i++) {
47 // pthread_create(&t[i], NULL, aFn, NULL);
51 // void cleanup(void) {
53 // pthread_mutex_lock(&mu);
55 // pthread_mutex_unlock(&mu);
56 // for (i = 0; i < nts; i++) {
57 // pthread_join(t[i], NULL);
59 // pthread_mutex_destroy(&mu);
64 // compareStatus is used to confirm the contents of the thread
65 // specific status files match expectations.
66 func compareStatus(filter
, expect
string) error
{
67 expected
:= filter
+ expect
68 pid
:= syscall
.Getpid()
69 fs
, err
:= os
.ReadDir(fmt
.Sprintf("/proc/%d/task", pid
))
71 return fmt
.Errorf("unable to find %d tasks: %v", pid
, err
)
73 expectedProc
:= fmt
.Sprintf("Pid:\t%d", pid
)
75 for _
, f
:= range fs
{
76 tf
:= fmt
.Sprintf("/proc/%s/status", f
.Name())
77 d
, err
:= os
.ReadFile(tf
)
79 // There are a surprising number of ways this
80 // can error out on linux. We've seen all of
81 // the following, so treat any error here as
82 // equivalent to the "process is gone":
83 // os.IsNotExist(err),
84 // "... : no such process",
85 // "... : bad file descriptor.
88 lines
:= strings
.Split(string(d
), "\n")
89 for _
, line
:= range lines
{
90 // Different kernel vintages pad differently.
91 line
= strings
.TrimSpace(line
)
92 if strings
.HasPrefix(line
, "Pid:\t") {
93 // On loaded systems, it is possible
94 // for a TID to be reused really
95 // quickly. As such, we need to
96 // validate that the thread status
97 // info we just read is a task of the
98 // same process PID as we are
99 // currently running, and not a
100 // recently terminated thread
101 // resurfaced in a different process.
102 if line
!= expectedProc
{
105 // Fall through in the unlikely case
106 // that filter at some point is
109 if strings
.HasPrefix(line
, filter
) {
110 if line
== expected
{
114 if filter
== "Groups:" && strings
.HasPrefix(line
, "Groups:\t") {
115 // https://github.com/golang/go/issues/46145
116 // Containers don't reliably output this line in sorted order so manually sort and compare that.
117 a
:= strings
.Split(line
[8:], " ")
119 got
:= strings
.Join(a
, " ")
120 if got
== expected
[8:] {
126 return fmt
.Errorf("%q got:%q want:%q (bad) [pid=%d file:'%s' %v]\n", tf
, line
, expected
, pid
, string(d
), expectedProc
)
131 return fmt
.Errorf("found no thread /proc/<TID>/status files for process %q", expectedProc
)
136 // test1435 test 9 glibc implemented setuid/gid syscall functions are
137 // mapped. This test is a slightly more expansive test than that of
138 // src/syscall/syscall_linux_test.go:TestSetuidEtc() insofar as it
139 // launches concurrent threads from C code via CGo and validates that
140 // they are subject to the system calls being tested. For the actual
141 // Go functionality being tested here, the syscall_linux_test version
142 // is considered authoritative, but non-trivial improvements to that
143 // should be mirrored here.
144 func test1435(t
*testing
.T
) {
145 if syscall
.Getuid() != 0 {
146 t
.Skip("skipping root only test")
149 // Launch some threads in C.
157 filter
, expect
string
159 {call
: "Setegid(1)", fn
: func() error
{ return syscall
.Setegid(1) }, filter
: "Gid:", expect
: "\t0\t1\t0\t1"},
160 {call
: "Setegid(0)", fn
: func() error
{ return syscall
.Setegid(0) }, filter
: "Gid:", expect
: "\t0\t0\t0\t0"},
162 {call
: "Seteuid(1)", fn
: func() error
{ return syscall
.Seteuid(1) }, filter
: "Uid:", expect
: "\t0\t1\t0\t1"},
163 {call
: "Setuid(0)", fn
: func() error
{ return syscall
.Setuid(0) }, filter
: "Uid:", expect
: "\t0\t0\t0\t0"},
165 {call
: "Setgid(1)", fn
: func() error
{ return syscall
.Setgid(1) }, filter
: "Gid:", expect
: "\t1\t1\t1\t1"},
166 {call
: "Setgid(0)", fn
: func() error
{ return syscall
.Setgid(0) }, filter
: "Gid:", expect
: "\t0\t0\t0\t0"},
168 {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"},
169 {call
: "Setgroups(nil)", fn
: func() error
{ return syscall
.Setgroups(nil) }, filter
: "Groups:", expect
: ""},
170 {call
: "Setgroups([]int{0})", fn
: func() error
{ return syscall
.Setgroups([]int{0}) }, filter
: "Groups:", expect
: "\t0"},
172 {call
: "Setregid(101,0)", fn
: func() error
{ return syscall
.Setregid(101, 0) }, filter
: "Gid:", expect
: "\t101\t0\t0\t0"},
173 {call
: "Setregid(0,102)", fn
: func() error
{ return syscall
.Setregid(0, 102) }, filter
: "Gid:", expect
: "\t0\t102\t102\t102"},
174 {call
: "Setregid(0,0)", fn
: func() error
{ return syscall
.Setregid(0, 0) }, filter
: "Gid:", expect
: "\t0\t0\t0\t0"},
176 {call
: "Setreuid(1,0)", fn
: func() error
{ return syscall
.Setreuid(1, 0) }, filter
: "Uid:", expect
: "\t1\t0\t0\t0"},
177 {call
: "Setreuid(0,2)", fn
: func() error
{ return syscall
.Setreuid(0, 2) }, filter
: "Uid:", expect
: "\t0\t2\t2\t2"},
178 {call
: "Setreuid(0,0)", fn
: func() error
{ return syscall
.Setreuid(0, 0) }, filter
: "Uid:", expect
: "\t0\t0\t0\t0"},
180 {call
: "Setresgid(101,0,102)", fn
: func() error
{ return syscall
.Setresgid(101, 0, 102) }, filter
: "Gid:", expect
: "\t101\t0\t102\t0"},
181 {call
: "Setresgid(0,102,101)", fn
: func() error
{ return syscall
.Setresgid(0, 102, 101) }, filter
: "Gid:", expect
: "\t0\t102\t101\t102"},
182 {call
: "Setresgid(0,0,0)", fn
: func() error
{ return syscall
.Setresgid(0, 0, 0) }, filter
: "Gid:", expect
: "\t0\t0\t0\t0"},
184 {call
: "Setresuid(1,0,2)", fn
: func() error
{ return syscall
.Setresuid(1, 0, 2) }, filter
: "Uid:", expect
: "\t1\t0\t2\t0"},
185 {call
: "Setresuid(0,2,1)", fn
: func() error
{ return syscall
.Setresuid(0, 2, 1) }, filter
: "Uid:", expect
: "\t0\t2\t1\t2"},
186 {call
: "Setresuid(0,0,0)", fn
: func() error
{ return syscall
.Setresuid(0, 0, 0) }, filter
: "Uid:", expect
: "\t0\t0\t0\t0"},
189 for i
, v
:= range vs
{
190 if err
:= v
.fn(); err
!= nil {
191 t
.Errorf("[%d] %q failed: %v", i
, v
.call
, err
)
194 if err
:= compareStatus(v
.filter
, v
.expect
); err
!= nil {
195 t
.Errorf("[%d] %q comparison: %v", i
, v
.call
, err
)