| |
| |
| |
|
|
| |
|
|
| package cgotest |
|
|
| import ( |
| "fmt" |
| "internal/testenv" |
| "os" |
| "runtime" |
| "sort" |
| "strings" |
| "syscall" |
| "testing" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import "C" |
|
|
| |
| |
| func compareStatus(filter, expect string) error { |
| expected := filter + expect |
| pid := syscall.Getpid() |
| fs, err := os.ReadDir(fmt.Sprintf("/proc/%d/task", pid)) |
| if err != nil { |
| return fmt.Errorf("unable to find %d tasks: %v", pid, err) |
| } |
| expectedProc := fmt.Sprintf("Pid:\t%d", pid) |
| foundAThread := false |
| for _, f := range fs { |
| tf := fmt.Sprintf("/proc/%s/status", f.Name()) |
| d, err := os.ReadFile(tf) |
| if err != nil { |
| |
| |
| |
| |
| |
| |
| |
| continue |
| } |
| lines := strings.Split(string(d), "\n") |
| for _, line := range lines { |
| |
| line = strings.TrimSpace(line) |
| if strings.HasPrefix(line, "Pid:\t") { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if line != expectedProc { |
| break |
| } |
| |
| |
| |
| } |
| if strings.HasPrefix(line, filter) { |
| if line == expected { |
| foundAThread = true |
| break |
| } |
| if filter == "Groups:" && strings.HasPrefix(line, "Groups:\t") { |
| |
| |
| a := strings.Split(line[8:], " ") |
| sort.Strings(a) |
| got := strings.Join(a, " ") |
| if got == expected[8:] { |
| foundAThread = true |
| break |
| } |
|
|
| } |
| return fmt.Errorf("%q got:%q want:%q (bad) [pid=%d file:'%s' %v]\n", tf, line, expected, pid, string(d), expectedProc) |
| } |
| } |
| } |
| if !foundAThread { |
| return fmt.Errorf("found no thread /proc/<TID>/status files for process %q", expectedProc) |
| } |
| return nil |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func test1435(t *testing.T) { |
| if syscall.Getuid() != 0 { |
| t.Skip("skipping root only test") |
| } |
| if testing.Short() && testenv.Builder() != "" && os.Getenv("USER") == "swarming" { |
| |
| |
| |
| |
| t.Skip("skipping root only test on a non-root builder") |
| } |
| if runtime.GOOS == "linux" { |
| if _, err := os.Stat("/etc/alpine-release"); err == nil { |
| t.Skip("skipping failing test on alpine - go.dev/issue/19938") |
| } |
| } |
|
|
| |
| const cts = 5 |
| C.trial(cts) |
| defer C.cleanup() |
|
|
| vs := []struct { |
| call string |
| fn func() error |
| filter, expect string |
| }{ |
| {call: "Setegid(1)", fn: func() error { return syscall.Setegid(1) }, filter: "Gid:", expect: "\t0\t1\t0\t1"}, |
| {call: "Setegid(0)", fn: func() error { return syscall.Setegid(0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"}, |
|
|
| {call: "Seteuid(1)", fn: func() error { return syscall.Seteuid(1) }, filter: "Uid:", expect: "\t0\t1\t0\t1"}, |
| {call: "Setuid(0)", fn: func() error { return syscall.Setuid(0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"}, |
|
|
| {call: "Setgid(1)", fn: func() error { return syscall.Setgid(1) }, filter: "Gid:", expect: "\t1\t1\t1\t1"}, |
| {call: "Setgid(0)", fn: func() error { return syscall.Setgid(0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"}, |
|
|
| {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"}, |
| {call: "Setgroups(nil)", fn: func() error { return syscall.Setgroups(nil) }, filter: "Groups:", expect: ""}, |
| {call: "Setgroups([]int{0})", fn: func() error { return syscall.Setgroups([]int{0}) }, filter: "Groups:", expect: "\t0"}, |
|
|
| {call: "Setregid(101,0)", fn: func() error { return syscall.Setregid(101, 0) }, filter: "Gid:", expect: "\t101\t0\t0\t0"}, |
| {call: "Setregid(0,102)", fn: func() error { return syscall.Setregid(0, 102) }, filter: "Gid:", expect: "\t0\t102\t102\t102"}, |
| {call: "Setregid(0,0)", fn: func() error { return syscall.Setregid(0, 0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"}, |
|
|
| {call: "Setreuid(1,0)", fn: func() error { return syscall.Setreuid(1, 0) }, filter: "Uid:", expect: "\t1\t0\t0\t0"}, |
| {call: "Setreuid(0,2)", fn: func() error { return syscall.Setreuid(0, 2) }, filter: "Uid:", expect: "\t0\t2\t2\t2"}, |
| {call: "Setreuid(0,0)", fn: func() error { return syscall.Setreuid(0, 0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"}, |
|
|
| {call: "Setresgid(101,0,102)", fn: func() error { return syscall.Setresgid(101, 0, 102) }, filter: "Gid:", expect: "\t101\t0\t102\t0"}, |
| {call: "Setresgid(0,102,101)", fn: func() error { return syscall.Setresgid(0, 102, 101) }, filter: "Gid:", expect: "\t0\t102\t101\t102"}, |
| {call: "Setresgid(0,0,0)", fn: func() error { return syscall.Setresgid(0, 0, 0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"}, |
|
|
| {call: "Setresuid(1,0,2)", fn: func() error { return syscall.Setresuid(1, 0, 2) }, filter: "Uid:", expect: "\t1\t0\t2\t0"}, |
| {call: "Setresuid(0,2,1)", fn: func() error { return syscall.Setresuid(0, 2, 1) }, filter: "Uid:", expect: "\t0\t2\t1\t2"}, |
| {call: "Setresuid(0,0,0)", fn: func() error { return syscall.Setresuid(0, 0, 0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"}, |
| } |
|
|
| for i, v := range vs { |
| if err := v.fn(); err != nil { |
| t.Errorf("[%d] %q failed: %v", i, v.call, err) |
| continue |
| } |
| if err := compareStatus(v.filter, v.expect); err != nil { |
| t.Errorf("[%d] %q comparison: %v", i, v.call, err) |
| } |
| } |
| } |
|
|