1
0

searcher_linux_shared.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //go:build linux
  2. package process
  3. import (
  4. "bytes"
  5. "encoding/binary"
  6. "fmt"
  7. "net"
  8. "net/netip"
  9. "os"
  10. "path"
  11. "strings"
  12. "syscall"
  13. "unicode"
  14. "unsafe"
  15. "github.com/sagernet/sing/common/buf"
  16. E "github.com/sagernet/sing/common/exceptions"
  17. N "github.com/sagernet/sing/common/network"
  18. )
  19. // from https://github.com/vishvananda/netlink/blob/bca67dfc8220b44ef582c9da4e9172bf1c9ec973/nl/nl_linux.go#L52-L62
  20. var nativeEndian = func() binary.ByteOrder {
  21. var x uint32 = 0x01020304
  22. if *(*byte)(unsafe.Pointer(&x)) == 0x01 {
  23. return binary.BigEndian
  24. }
  25. return binary.LittleEndian
  26. }()
  27. const (
  28. sizeOfSocketDiagRequest = syscall.SizeofNlMsghdr + 8 + 48
  29. socketDiagByFamily = 20
  30. pathProc = "/proc"
  31. )
  32. func resolveSocketByNetlink(network string, source netip.AddrPort, destination netip.AddrPort) (inode, uid uint32, err error) {
  33. var family uint8
  34. var protocol uint8
  35. switch network {
  36. case N.NetworkTCP:
  37. protocol = syscall.IPPROTO_TCP
  38. case N.NetworkUDP:
  39. protocol = syscall.IPPROTO_UDP
  40. default:
  41. return 0, 0, os.ErrInvalid
  42. }
  43. if source.Addr().Is4() {
  44. family = syscall.AF_INET
  45. } else {
  46. family = syscall.AF_INET6
  47. }
  48. req := packSocketDiagRequest(family, protocol, source)
  49. socket, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_DGRAM, syscall.NETLINK_INET_DIAG)
  50. if err != nil {
  51. return 0, 0, E.Cause(err, "dial netlink")
  52. }
  53. defer syscall.Close(socket)
  54. syscall.SetsockoptTimeval(socket, syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, &syscall.Timeval{Usec: 100})
  55. syscall.SetsockoptTimeval(socket, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &syscall.Timeval{Usec: 100})
  56. err = syscall.Connect(socket, &syscall.SockaddrNetlink{
  57. Family: syscall.AF_NETLINK,
  58. Pad: 0,
  59. Pid: 0,
  60. Groups: 0,
  61. })
  62. if err != nil {
  63. return
  64. }
  65. _, err = syscall.Write(socket, req)
  66. if err != nil {
  67. return 0, 0, E.Cause(err, "write netlink request")
  68. }
  69. buffer := buf.New()
  70. defer buffer.Release()
  71. n, err := syscall.Read(socket, buffer.FreeBytes())
  72. if err != nil {
  73. return 0, 0, E.Cause(err, "read netlink response")
  74. }
  75. buffer.Truncate(n)
  76. messages, err := syscall.ParseNetlinkMessage(buffer.Bytes())
  77. if err != nil {
  78. return 0, 0, E.Cause(err, "parse netlink message")
  79. } else if len(messages) == 0 {
  80. return 0, 0, E.New("unexcepted netlink response")
  81. }
  82. message := messages[0]
  83. if message.Header.Type&syscall.NLMSG_ERROR != 0 {
  84. return 0, 0, E.New("netlink message: NLMSG_ERROR")
  85. }
  86. inode, uid = unpackSocketDiagResponse(&messages[0])
  87. return
  88. }
  89. func packSocketDiagRequest(family, protocol byte, source netip.AddrPort) []byte {
  90. s := make([]byte, 16)
  91. copy(s, source.Addr().AsSlice())
  92. buf := make([]byte, sizeOfSocketDiagRequest)
  93. nativeEndian.PutUint32(buf[0:4], sizeOfSocketDiagRequest)
  94. nativeEndian.PutUint16(buf[4:6], socketDiagByFamily)
  95. nativeEndian.PutUint16(buf[6:8], syscall.NLM_F_REQUEST|syscall.NLM_F_DUMP)
  96. nativeEndian.PutUint32(buf[8:12], 0)
  97. nativeEndian.PutUint32(buf[12:16], 0)
  98. buf[16] = family
  99. buf[17] = protocol
  100. buf[18] = 0
  101. buf[19] = 0
  102. nativeEndian.PutUint32(buf[20:24], 0xFFFFFFFF)
  103. binary.BigEndian.PutUint16(buf[24:26], source.Port())
  104. binary.BigEndian.PutUint16(buf[26:28], 0)
  105. copy(buf[28:44], s)
  106. copy(buf[44:60], net.IPv6zero)
  107. nativeEndian.PutUint32(buf[60:64], 0)
  108. nativeEndian.PutUint64(buf[64:72], 0xFFFFFFFFFFFFFFFF)
  109. return buf
  110. }
  111. func unpackSocketDiagResponse(msg *syscall.NetlinkMessage) (inode, uid uint32) {
  112. if len(msg.Data) < 72 {
  113. return 0, 0
  114. }
  115. data := msg.Data
  116. uid = nativeEndian.Uint32(data[64:68])
  117. inode = nativeEndian.Uint32(data[68:72])
  118. return
  119. }
  120. func resolveProcessNameByProcSearch(inode, uid uint32) (string, error) {
  121. files, err := os.ReadDir(pathProc)
  122. if err != nil {
  123. return "", err
  124. }
  125. buffer := make([]byte, syscall.PathMax)
  126. socket := []byte(fmt.Sprintf("socket:[%d]", inode))
  127. for _, f := range files {
  128. if !f.IsDir() || !isPid(f.Name()) {
  129. continue
  130. }
  131. info, err := f.Info()
  132. if err != nil {
  133. return "", err
  134. }
  135. if info.Sys().(*syscall.Stat_t).Uid != uid {
  136. continue
  137. }
  138. processPath := path.Join(pathProc, f.Name())
  139. fdPath := path.Join(processPath, "fd")
  140. fds, err := os.ReadDir(fdPath)
  141. if err != nil {
  142. continue
  143. }
  144. for _, fd := range fds {
  145. n, err := syscall.Readlink(path.Join(fdPath, fd.Name()), buffer)
  146. if err != nil {
  147. continue
  148. }
  149. if bytes.Equal(buffer[:n], socket) {
  150. return os.Readlink(path.Join(processPath, "exe"))
  151. }
  152. }
  153. }
  154. return "", fmt.Errorf("process of uid(%d),inode(%d) not found", uid, inode)
  155. }
  156. func isPid(s string) bool {
  157. return strings.IndexFunc(s, func(r rune) bool {
  158. return !unicode.IsDigit(r)
  159. }) == -1
  160. }