Colin Cross | 1f80552 | 2021-05-14 11:10:59 -0700 | [diff] [blame] | 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. |
| 4 | |
Dan Willemsen | bc60c3c | 2021-12-15 01:09:00 -0800 | [diff] [blame] | 5 | //go:build linux && cgo |
Colin Cross | 1f80552 | 2021-05-14 11:10:59 -0700 | [diff] [blame] | 6 | // +build linux,cgo |
| 7 | |
| 8 | package cgotest |
| 9 | |
| 10 | import ( |
| 11 | "fmt" |
Dan Willemsen | cc753b7 | 2021-08-31 13:25:42 -0700 | [diff] [blame] | 12 | "os" |
| 13 | "sort" |
Colin Cross | 1f80552 | 2021-05-14 11:10:59 -0700 | [diff] [blame] | 14 | "strings" |
| 15 | "syscall" |
| 16 | "testing" |
| 17 | ) |
| 18 | |
| 19 | // #include <stdio.h> |
| 20 | // #include <stdlib.h> |
| 21 | // #include <pthread.h> |
| 22 | // #include <unistd.h> |
| 23 | // #include <sys/types.h> |
| 24 | // |
| 25 | // pthread_t *t = NULL; |
| 26 | // pthread_mutex_t mu; |
| 27 | // int nts = 0; |
| 28 | // int all_done = 0; |
| 29 | // |
| 30 | // static void *aFn(void *vargp) { |
| 31 | // int done = 0; |
| 32 | // while (!done) { |
| 33 | // usleep(100); |
| 34 | // pthread_mutex_lock(&mu); |
| 35 | // done = all_done; |
| 36 | // pthread_mutex_unlock(&mu); |
| 37 | // } |
| 38 | // return NULL; |
| 39 | // } |
| 40 | // |
| 41 | // void trial(int argc) { |
| 42 | // int i; |
| 43 | // nts = 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); |
| 48 | // } |
| 49 | // } |
| 50 | // |
| 51 | // void cleanup(void) { |
| 52 | // int i; |
| 53 | // pthread_mutex_lock(&mu); |
| 54 | // all_done = 1; |
| 55 | // pthread_mutex_unlock(&mu); |
| 56 | // for (i = 0; i < nts; i++) { |
| 57 | // pthread_join(t[i], NULL); |
| 58 | // } |
| 59 | // pthread_mutex_destroy(&mu); |
| 60 | // free(t); |
| 61 | // } |
| 62 | import "C" |
| 63 | |
| 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() |
Dan Willemsen | cc753b7 | 2021-08-31 13:25:42 -0700 | [diff] [blame] | 69 | fs, err := os.ReadDir(fmt.Sprintf("/proc/%d/task", pid)) |
Colin Cross | 1f80552 | 2021-05-14 11:10:59 -0700 | [diff] [blame] | 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()) |
Dan Willemsen | cc753b7 | 2021-08-31 13:25:42 -0700 | [diff] [blame] | 77 | d, err := os.ReadFile(tf) |
Colin Cross | 1f80552 | 2021-05-14 11:10:59 -0700 | [diff] [blame] | 78 | if err != nil { |
| 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. |
| 86 | continue |
| 87 | } |
| 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 { |
| 103 | break |
| 104 | } |
| 105 | // Fall through in the unlikely case |
| 106 | // that filter at some point is |
| 107 | // "Pid:\t". |
| 108 | } |
| 109 | if strings.HasPrefix(line, filter) { |
Dan Willemsen | cc753b7 | 2021-08-31 13:25:42 -0700 | [diff] [blame] | 110 | if line == expected { |
| 111 | foundAThread = true |
| 112 | break |
Colin Cross | 1f80552 | 2021-05-14 11:10:59 -0700 | [diff] [blame] | 113 | } |
Dan Willemsen | cc753b7 | 2021-08-31 13:25:42 -0700 | [diff] [blame] | 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:], " ") |
| 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) |
Colin Cross | 1f80552 | 2021-05-14 11:10:59 -0700 | [diff] [blame] | 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 | // 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") |
| 147 | } |
| 148 | |
| 149 | // Launch some threads in C. |
| 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 | } |