searcher_linux_shared.go 5.0 KB

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