Source file misc/cgo/test/issue1435.go

     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  
     5  // +build linux,cgo
     6  
     7  package cgotest
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"sort"
    13  	"strings"
    14  	"syscall"
    15  	"testing"
    16  )
    17  
    18  // #include <stdio.h>
    19  // #include <stdlib.h>
    20  // #include <pthread.h>
    21  // #include <unistd.h>
    22  // #include <sys/types.h>
    23  //
    24  // pthread_t *t = NULL;
    25  // pthread_mutex_t mu;
    26  // int nts = 0;
    27  // int all_done = 0;
    28  //
    29  // static void *aFn(void *vargp) {
    30  //   int done = 0;
    31  //   while (!done) {
    32  //     usleep(100);
    33  //     pthread_mutex_lock(&mu);
    34  //     done = all_done;
    35  //     pthread_mutex_unlock(&mu);
    36  //   }
    37  //   return NULL;
    38  // }
    39  //
    40  // void trial(int argc) {
    41  //   int i;
    42  //   nts = argc;
    43  //   t = calloc(nts, sizeof(pthread_t));
    44  //   pthread_mutex_init(&mu, NULL);
    45  //   for (i = 0; i < nts; i++) {
    46  //     pthread_create(&t[i], NULL, aFn, NULL);
    47  //   }
    48  // }
    49  //
    50  // void cleanup(void) {
    51  //   int i;
    52  //   pthread_mutex_lock(&mu);
    53  //   all_done = 1;
    54  //   pthread_mutex_unlock(&mu);
    55  //   for (i = 0; i < nts; i++) {
    56  //     pthread_join(t[i], NULL);
    57  //   }
    58  //   pthread_mutex_destroy(&mu);
    59  //   free(t);
    60  // }
    61  import "C"
    62  
    63  // compareStatus is used to confirm the contents of the thread
    64  // specific status files match expectations.
    65  func compareStatus(filter, expect string) error {
    66  	expected := filter + expect
    67  	pid := syscall.Getpid()
    68  	fs, err := os.ReadDir(fmt.Sprintf("/proc/%d/task", pid))
    69  	if err != nil {
    70  		return fmt.Errorf("unable to find %d tasks: %v", pid, err)
    71  	}
    72  	expectedProc := fmt.Sprintf("Pid:\t%d", pid)
    73  	foundAThread := false
    74  	for _, f := range fs {
    75  		tf := fmt.Sprintf("/proc/%s/status", f.Name())
    76  		d, err := os.ReadFile(tf)
    77  		if err != nil {
    78  			// There are a surprising number of ways this
    79  			// can error out on linux.  We've seen all of
    80  			// the following, so treat any error here as
    81  			// equivalent to the "process is gone":
    82  			//    os.IsNotExist(err),
    83  			//    "... : no such process",
    84  			//    "... : bad file descriptor.
    85  			continue
    86  		}
    87  		lines := strings.Split(string(d), "\n")
    88  		for _, line := range lines {
    89  			// Different kernel vintages pad differently.
    90  			line = strings.TrimSpace(line)
    91  			if strings.HasPrefix(line, "Pid:\t") {
    92  				// On loaded systems, it is possible
    93  				// for a TID to be reused really
    94  				// quickly. As such, we need to
    95  				// validate that the thread status
    96  				// info we just read is a task of the
    97  				// same process PID as we are
    98  				// currently running, and not a
    99  				// recently terminated thread
   100  				// resurfaced in a different process.
   101  				if line != expectedProc {
   102  					break
   103  				}
   104  				// Fall through in the unlikely case
   105  				// that filter at some point is
   106  				// "Pid:\t".
   107  			}
   108  			if strings.HasPrefix(line, filter) {
   109  				if line == expected {
   110  					foundAThread = true
   111  					break
   112  				}
   113  				if filter == "Groups:" && strings.HasPrefix(line, "Groups:\t") {
   114  					// https://github.com/golang/go/issues/46145
   115  					// Containers don't reliably output this line in sorted order so manually sort and compare that.
   116  					a := strings.Split(line[8:], " ")
   117  					sort.Strings(a)
   118  					got := strings.Join(a, " ")
   119  					if got == expected[8:] {
   120  						foundAThread = true
   121  						break
   122  					}
   123  
   124  				}
   125  				return fmt.Errorf("%q got:%q want:%q (bad) [pid=%d file:'%s' %v]\n", tf, line, expected, pid, string(d), expectedProc)
   126  			}
   127  		}
   128  	}
   129  	if !foundAThread {
   130  		return fmt.Errorf("found no thread /proc/<TID>/status files for process %q", expectedProc)
   131  	}
   132  	return nil
   133  }
   134  
   135  // test1435 test 9 glibc implemented setuid/gid syscall functions are
   136  // mapped.  This test is a slightly more expansive test than that of
   137  // src/syscall/syscall_linux_test.go:TestSetuidEtc() insofar as it
   138  // launches concurrent threads from C code via CGo and validates that
   139  // they are subject to the system calls being tested. For the actual
   140  // Go functionality being tested here, the syscall_linux_test version
   141  // is considered authoritative, but non-trivial improvements to that
   142  // should be mirrored here.
   143  func test1435(t *testing.T) {
   144  	if syscall.Getuid() != 0 {
   145  		t.Skip("skipping root only test")
   146  	}
   147  
   148  	// Launch some threads in C.
   149  	const cts = 5
   150  	C.trial(cts)
   151  	defer C.cleanup()
   152  
   153  	vs := []struct {
   154  		call           string
   155  		fn             func() error
   156  		filter, expect string
   157  	}{
   158  		{call: "Setegid(1)", fn: func() error { return syscall.Setegid(1) }, filter: "Gid:", expect: "\t0\t1\t0\t1"},
   159  		{call: "Setegid(0)", fn: func() error { return syscall.Setegid(0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"},
   160  
   161  		{call: "Seteuid(1)", fn: func() error { return syscall.Seteuid(1) }, filter: "Uid:", expect: "\t0\t1\t0\t1"},
   162  		{call: "Setuid(0)", fn: func() error { return syscall.Setuid(0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"},
   163  
   164  		{call: "Setgid(1)", fn: func() error { return syscall.Setgid(1) }, filter: "Gid:", expect: "\t1\t1\t1\t1"},
   165  		{call: "Setgid(0)", fn: func() error { return syscall.Setgid(0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"},
   166  
   167  		{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"},
   168  		{call: "Setgroups(nil)", fn: func() error { return syscall.Setgroups(nil) }, filter: "Groups:", expect: ""},
   169  		{call: "Setgroups([]int{0})", fn: func() error { return syscall.Setgroups([]int{0}) }, filter: "Groups:", expect: "\t0"},
   170  
   171  		{call: "Setregid(101,0)", fn: func() error { return syscall.Setregid(101, 0) }, filter: "Gid:", expect: "\t101\t0\t0\t0"},
   172  		{call: "Setregid(0,102)", fn: func() error { return syscall.Setregid(0, 102) }, filter: "Gid:", expect: "\t0\t102\t102\t102"},
   173  		{call: "Setregid(0,0)", fn: func() error { return syscall.Setregid(0, 0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"},
   174  
   175  		{call: "Setreuid(1,0)", fn: func() error { return syscall.Setreuid(1, 0) }, filter: "Uid:", expect: "\t1\t0\t0\t0"},
   176  		{call: "Setreuid(0,2)", fn: func() error { return syscall.Setreuid(0, 2) }, filter: "Uid:", expect: "\t0\t2\t2\t2"},
   177  		{call: "Setreuid(0,0)", fn: func() error { return syscall.Setreuid(0, 0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"},
   178  
   179  		{call: "Setresgid(101,0,102)", fn: func() error { return syscall.Setresgid(101, 0, 102) }, filter: "Gid:", expect: "\t101\t0\t102\t0"},
   180  		{call: "Setresgid(0,102,101)", fn: func() error { return syscall.Setresgid(0, 102, 101) }, filter: "Gid:", expect: "\t0\t102\t101\t102"},
   181  		{call: "Setresgid(0,0,0)", fn: func() error { return syscall.Setresgid(0, 0, 0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"},
   182  
   183  		{call: "Setresuid(1,0,2)", fn: func() error { return syscall.Setresuid(1, 0, 2) }, filter: "Uid:", expect: "\t1\t0\t2\t0"},
   184  		{call: "Setresuid(0,2,1)", fn: func() error { return syscall.Setresuid(0, 2, 1) }, filter: "Uid:", expect: "\t0\t2\t1\t2"},
   185  		{call: "Setresuid(0,0,0)", fn: func() error { return syscall.Setresuid(0, 0, 0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"},
   186  	}
   187  
   188  	for i, v := range vs {
   189  		if err := v.fn(); err != nil {
   190  			t.Errorf("[%d] %q failed: %v", i, v.call, err)
   191  			continue
   192  		}
   193  		if err := compareStatus(v.filter, v.expect); err != nil {
   194  			t.Errorf("[%d] %q comparison: %v", i, v.call, err)
   195  		}
   196  	}
   197  }
   198  

View as plain text