Source file src/syscall/creds_test.go

     1  // Copyright 2012 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  //go:build linux
     6  
     7  package syscall_test
     8  
     9  import (
    10  	"bytes"
    11  	"net"
    12  	"os"
    13  	"syscall"
    14  	"testing"
    15  )
    16  
    17  // TestSCMCredentials tests the sending and receiving of credentials
    18  // (PID, UID, GID) in an ancillary message between two UNIX
    19  // sockets. The SO_PASSCRED socket option is enabled on the sending
    20  // socket for this to work.
    21  func TestSCMCredentials(t *testing.T) {
    22  	socketTypeTests := []struct {
    23  		socketType int
    24  		dataLen    int
    25  	}{
    26  		{
    27  			syscall.SOCK_STREAM,
    28  			1,
    29  		}, {
    30  			syscall.SOCK_DGRAM,
    31  			0,
    32  		},
    33  	}
    34  
    35  	for _, tt := range socketTypeTests {
    36  		fds, err := syscall.Socketpair(syscall.AF_LOCAL, tt.socketType, 0)
    37  		if err != nil {
    38  			t.Fatalf("Socketpair: %v", err)
    39  		}
    40  
    41  		err = syscall.SetsockoptInt(fds[0], syscall.SOL_SOCKET, syscall.SO_PASSCRED, 1)
    42  		if err != nil {
    43  			syscall.Close(fds[0])
    44  			syscall.Close(fds[1])
    45  			t.Fatalf("SetsockoptInt: %v", err)
    46  		}
    47  
    48  		srvFile := os.NewFile(uintptr(fds[0]), "server")
    49  		cliFile := os.NewFile(uintptr(fds[1]), "client")
    50  		defer srvFile.Close()
    51  		defer cliFile.Close()
    52  
    53  		srv, err := net.FileConn(srvFile)
    54  		if err != nil {
    55  			t.Errorf("FileConn: %v", err)
    56  			return
    57  		}
    58  		defer srv.Close()
    59  
    60  		cli, err := net.FileConn(cliFile)
    61  		if err != nil {
    62  			t.Errorf("FileConn: %v", err)
    63  			return
    64  		}
    65  		defer cli.Close()
    66  
    67  		var ucred syscall.Ucred
    68  		if os.Getuid() != 0 {
    69  			ucred.Pid = int32(os.Getpid())
    70  			ucred.Uid = 0
    71  			ucred.Gid = 0
    72  			oob := syscall.UnixCredentials(&ucred)
    73  			_, _, err := cli.(*net.UnixConn).WriteMsgUnix(nil, oob, nil)
    74  			if op, ok := err.(*net.OpError); ok {
    75  				err = op.Err
    76  			}
    77  			if sys, ok := err.(*os.SyscallError); ok {
    78  				err = sys.Err
    79  			}
    80  			if err != syscall.EPERM {
    81  				t.Fatalf("WriteMsgUnix failed with %v, want EPERM", err)
    82  			}
    83  		}
    84  
    85  		ucred.Pid = int32(os.Getpid())
    86  		ucred.Uid = uint32(os.Getuid())
    87  		ucred.Gid = uint32(os.Getgid())
    88  		oob := syscall.UnixCredentials(&ucred)
    89  
    90  		// On SOCK_STREAM, this is internally going to send a dummy byte
    91  		n, oobn, err := cli.(*net.UnixConn).WriteMsgUnix(nil, oob, nil)
    92  		if err != nil {
    93  			t.Fatalf("WriteMsgUnix: %v", err)
    94  		}
    95  		if n != 0 {
    96  			t.Fatalf("WriteMsgUnix n = %d, want 0", n)
    97  		}
    98  		if oobn != len(oob) {
    99  			t.Fatalf("WriteMsgUnix oobn = %d, want %d", oobn, len(oob))
   100  		}
   101  
   102  		oob2 := make([]byte, 10*len(oob))
   103  		n, oobn2, flags, _, err := srv.(*net.UnixConn).ReadMsgUnix(nil, oob2)
   104  		if err != nil {
   105  			t.Fatalf("ReadMsgUnix: %v", err)
   106  		}
   107  		if flags != syscall.MSG_CMSG_CLOEXEC {
   108  			t.Fatalf("ReadMsgUnix flags = %#x, want %#x (MSG_CMSG_CLOEXEC)", flags, syscall.MSG_CMSG_CLOEXEC)
   109  		}
   110  		if n != tt.dataLen {
   111  			t.Fatalf("ReadMsgUnix n = %d, want %d", n, tt.dataLen)
   112  		}
   113  		if oobn2 != oobn {
   114  			// without SO_PASSCRED set on the socket, ReadMsgUnix will
   115  			// return zero oob bytes
   116  			t.Fatalf("ReadMsgUnix oobn = %d, want %d", oobn2, oobn)
   117  		}
   118  		oob2 = oob2[:oobn2]
   119  		if !bytes.Equal(oob, oob2) {
   120  			t.Fatal("ReadMsgUnix oob bytes don't match")
   121  		}
   122  
   123  		scm, err := syscall.ParseSocketControlMessage(oob2)
   124  		if err != nil {
   125  			t.Fatalf("ParseSocketControlMessage: %v", err)
   126  		}
   127  		newUcred, err := syscall.ParseUnixCredentials(&scm[0])
   128  		if err != nil {
   129  			t.Fatalf("ParseUnixCredentials: %v", err)
   130  		}
   131  		if *newUcred != ucred {
   132  			t.Fatalf("ParseUnixCredentials = %+v, want %+v", newUcred, ucred)
   133  		}
   134  	}
   135  }
   136  

View as plain text