Source file
misc/cgo/test/issue1435.go
1
2
3
4
5
6
7
8 package cgotest
9
10 import (
11 "fmt"
12 "os"
13 "sort"
14 "strings"
15 "syscall"
16 "testing"
17 )
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 import "C"
63
64
65
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))
70 if err != nil {
71 return fmt.Errorf("unable to find %d tasks: %v", pid, err)
72 }
73 expectedProc := fmt.Sprintf("Pid:\t%d", pid)
74 foundAThread := false
75 for _, f := range fs {
76 tf := fmt.Sprintf("/proc/%s/status", f.Name())
77 d, err := os.ReadFile(tf)
78 if err != nil {
79
80
81
82
83
84
85
86 continue
87 }
88 lines := strings.Split(string(d), "\n")
89 for _, line := range lines {
90
91 line = strings.TrimSpace(line)
92 if strings.HasPrefix(line, "Pid:\t") {
93
94
95
96
97
98
99
100
101
102 if line != expectedProc {
103 break
104 }
105
106
107
108 }
109 if strings.HasPrefix(line, filter) {
110 if line == expected {
111 foundAThread = true
112 break
113 }
114 if filter == "Groups:" && strings.HasPrefix(line, "Groups:\t") {
115
116
117 a := strings.Split(line[8:], " ")
118 sort.Strings(a)
119 got := strings.Join(a, " ")
120 if got == expected[8:] {
121 foundAThread = true
122 break
123 }
124
125 }
126 return fmt.Errorf("%q got:%q want:%q (bad) [pid=%d file:'%s' %v]\n", tf, line, expected, pid, string(d), expectedProc)
127 }
128 }
129 }
130 if !foundAThread {
131 return fmt.Errorf("found no thread /proc/<TID>/status files for process %q", expectedProc)
132 }
133 return nil
134 }
135
136
137
138
139
140
141
142
143
144 func test1435(t *testing.T) {
145 if syscall.Getuid() != 0 {
146 t.Skip("skipping root only test")
147 }
148
149
150 const cts = 5
151 C.trial(cts)
152 defer C.cleanup()
153
154 vs := []struct {
155 call string
156 fn func() error
157 filter, expect string
158 }{
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"},
161
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"},
164
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"},
167
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"},
171
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"},
175
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"},
179
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"},
183
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"},
187 }
188
189 for i, v := range vs {
190 if err := v.fn(); err != nil {
191 t.Errorf("[%d] %q failed: %v", i, v.call, err)
192 continue
193 }
194 if err := compareStatus(v.filter, v.expect); err != nil {
195 t.Errorf("[%d] %q comparison: %v", i, v.call, err)
196 }
197 }
198 }
199
View as plain text