You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.6 KiB

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