tls.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package ebpftracer
  2. import (
  3. "bufio"
  4. "bytes"
  5. "debug/buildinfo"
  6. "debug/elf"
  7. "errors"
  8. "fmt"
  9. "github.com/cilium/ebpf/link"
  10. "github.com/coroot/coroot-node-agent/proc"
  11. "golang.org/x/arch/arm64/arm64asm"
  12. "golang.org/x/arch/x86/x86asm"
  13. "golang.org/x/mod/semver"
  14. "k8s.io/klog/v2"
  15. "os"
  16. "regexp"
  17. "strings"
  18. )
  19. const (
  20. minSupportedGoVersion = "v1.17.0"
  21. goTlsWriteSymbol = "crypto/tls.(*Conn).Write"
  22. goTlsReadSymbol = "crypto/tls.(*Conn).Read"
  23. )
  24. var (
  25. opensslVersionRe = regexp.MustCompile(`OpenSSL\s(\d\.\d+\.\d+)`)
  26. )
  27. func (t *Tracer) AttachOpenSslUprobes(pid uint32) []link.Link {
  28. if t.disableL7Tracing {
  29. return nil
  30. }
  31. libPath, version := getSslLibPathAndVersion(pid)
  32. if libPath == "" || version == "" {
  33. return nil
  34. }
  35. log := func(msg string, err error) {
  36. if err != nil {
  37. for _, s := range []string{"no such file or directory", "no such process", "permission denied"} {
  38. if strings.HasSuffix(err.Error(), s) {
  39. return
  40. }
  41. }
  42. klog.ErrorfDepth(1, "pid=%d libssl_version=%s: %s: %s", pid, version, msg, err)
  43. return
  44. }
  45. klog.InfofDepth(1, "pid=%d libssl_version=%s: %s", pid, version, msg)
  46. }
  47. exe, err := link.OpenExecutable(libPath)
  48. if err != nil {
  49. log("failed to open executable", err)
  50. return nil
  51. }
  52. var links []link.Link
  53. writeEnter := "openssl_SSL_write_enter"
  54. readEnter := "openssl_SSL_read_enter"
  55. readExEnter := "openssl_SSL_read_ex_enter"
  56. readExit := "openssl_SSL_read_exit"
  57. switch {
  58. case semver.Compare(version, "v3.0.0") >= 0:
  59. writeEnter = "openssl_SSL_write_enter_v3_0"
  60. readEnter = "openssl_SSL_read_enter_v3_0"
  61. readExEnter = "openssl_SSL_read_ex_enter_v3_0"
  62. case semver.Compare(version, "v1.1.1") >= 0:
  63. writeEnter = "openssl_SSL_write_enter_v1_1_1"
  64. readEnter = "openssl_SSL_read_enter_v1_1_1"
  65. readExEnter = "openssl_SSL_read_ex_enter_v1_1_1"
  66. }
  67. type prog struct {
  68. symbol string
  69. uprobe string
  70. uretprobe string
  71. }
  72. progs := []prog{
  73. {symbol: "SSL_write", uprobe: writeEnter},
  74. {symbol: "SSL_write_ex", uprobe: writeEnter},
  75. {symbol: "SSL_read", uprobe: readEnter},
  76. {symbol: "SSL_read_ex", uprobe: readExEnter},
  77. {symbol: "SSL_read", uretprobe: readExit},
  78. {symbol: "SSL_read_ex", uretprobe: readExit},
  79. }
  80. for _, p := range progs {
  81. if p.uprobe != "" {
  82. l, err := exe.Uprobe(p.symbol, t.uprobes[p.uprobe], nil)
  83. if err != nil {
  84. log("failed to attach uprobe", err)
  85. return nil
  86. }
  87. links = append(links, l)
  88. }
  89. if p.uretprobe != "" {
  90. l, err := exe.Uretprobe(p.symbol, t.uprobes[p.uretprobe], nil)
  91. if err != nil {
  92. log("failed to attach uretprobe", err)
  93. return nil
  94. }
  95. links = append(links, l)
  96. }
  97. }
  98. log("libssl uprobes attached", nil)
  99. return links
  100. }
  101. func (t *Tracer) AttachGoTlsUprobes(pid uint32) []link.Link {
  102. if t.disableL7Tracing {
  103. return nil
  104. }
  105. path := proc.Path(pid, "exe")
  106. var err error
  107. var name, version string
  108. log := func(msg string, err error) {
  109. if err != nil {
  110. for _, s := range []string{"not a Go executable", "no such file or directory", "no such process", "permission denied"} {
  111. if strings.HasSuffix(err.Error(), s) {
  112. return
  113. }
  114. }
  115. klog.ErrorfDepth(1, "pid=%d golang_app=%s golang_version=%s: %s: %s", pid, name, version, msg, err)
  116. return
  117. }
  118. klog.InfofDepth(1, "pid=%d golang_app=%s golang_version=%s: %s", pid, name, version, msg)
  119. }
  120. bi, err := buildinfo.ReadFile(path)
  121. if err != nil {
  122. log("failed to read build info", err)
  123. return nil
  124. }
  125. name, err = os.Readlink(path)
  126. if err != nil {
  127. log("failed to read name", err)
  128. return nil
  129. }
  130. version = strings.Replace(bi.GoVersion, "go", "v", 1)
  131. if semver.Compare(version, minSupportedGoVersion) < 0 {
  132. log(fmt.Sprintf("go_versions below %s are not supported", minSupportedGoVersion), nil)
  133. return nil
  134. }
  135. ef, err := elf.Open(path)
  136. if err != nil {
  137. log("failed to open as elf binary", err)
  138. return nil
  139. }
  140. defer ef.Close()
  141. symbols, err := ef.Symbols()
  142. if err != nil {
  143. if errors.Is(err, elf.ErrNoSymbols) {
  144. log("no symbol section", nil)
  145. return nil
  146. }
  147. log("failed to read symbols", err)
  148. return nil
  149. }
  150. textSection := ef.Section(".text")
  151. if textSection == nil {
  152. log("no text section", nil)
  153. return nil
  154. }
  155. textSectionData, err := textSection.Data()
  156. if err != nil {
  157. log("failed to read text section", err)
  158. return nil
  159. }
  160. textSectionLen := uint64(len(textSectionData) - 1)
  161. exe, err := link.OpenExecutable(path)
  162. if err != nil {
  163. log("failed to open executable", err)
  164. return nil
  165. }
  166. var links []link.Link
  167. for _, s := range symbols {
  168. if elf.ST_TYPE(s.Info) != elf.STT_FUNC || s.Size == 0 {
  169. continue
  170. }
  171. switch s.Name {
  172. case goTlsWriteSymbol, goTlsReadSymbol:
  173. default:
  174. continue
  175. }
  176. address := s.Value
  177. for _, p := range ef.Progs {
  178. if p.Type != elf.PT_LOAD || (p.Flags&elf.PF_X) == 0 {
  179. continue
  180. }
  181. if p.Vaddr <= s.Value && s.Value < (p.Vaddr+p.Memsz) {
  182. address = s.Value - p.Vaddr + p.Off
  183. break
  184. }
  185. }
  186. switch s.Name {
  187. case goTlsWriteSymbol:
  188. l, err := exe.Uprobe(s.Name, t.uprobes["go_crypto_tls_write_enter"], &link.UprobeOptions{Address: address})
  189. if err != nil {
  190. log("failed to attach write_enter uprobe", err)
  191. return nil
  192. }
  193. links = append(links, l)
  194. case goTlsReadSymbol:
  195. l, err := exe.Uprobe(s.Name, t.uprobes["go_crypto_tls_read_enter"], &link.UprobeOptions{Address: address})
  196. if err != nil {
  197. log("failed to attach read_enter uprobe", err)
  198. return nil
  199. }
  200. links = append(links, l)
  201. sStart := s.Value - textSection.Addr
  202. sEnd := sStart + s.Size
  203. if sEnd > textSectionLen {
  204. continue
  205. }
  206. sBytes := textSectionData[sStart:sEnd]
  207. returnOffsets := getReturnOffsets(ef.Machine, sBytes)
  208. if len(returnOffsets) == 0 {
  209. log("failed to attach read_exit uprobe", fmt.Errorf("no return offsets found"))
  210. return nil
  211. }
  212. for _, offset := range returnOffsets {
  213. l, err := exe.Uprobe(s.Name, t.uprobes["go_crypto_tls_read_exit"], &link.UprobeOptions{Address: address, Offset: uint64(offset)})
  214. if err != nil {
  215. log("failed to attach read_exit uprobe", err)
  216. return nil
  217. }
  218. links = append(links, l)
  219. }
  220. }
  221. }
  222. if len(links) == 0 {
  223. return nil
  224. }
  225. log("crypto/tls uprobes attached", nil)
  226. return links
  227. }
  228. func getSslLibPathAndVersion(pid uint32) (string, string) {
  229. f, err := os.Open(proc.Path(pid, "maps"))
  230. if err != nil {
  231. return "", ""
  232. }
  233. defer f.Close()
  234. scanner := bufio.NewScanner(f)
  235. scanner.Split(bufio.ScanLines)
  236. var libsslPath, libcryptoPath string
  237. for scanner.Scan() {
  238. parts := strings.Fields(scanner.Text())
  239. if len(parts) <= 5 {
  240. continue
  241. }
  242. libPath := parts[5]
  243. switch {
  244. case libsslPath == "" && strings.Contains(libPath, "libssl.so"):
  245. fullPath := proc.Path(pid, "root", libPath)
  246. if _, err = os.Stat(fullPath); err == nil {
  247. libsslPath = fullPath
  248. }
  249. case libcryptoPath == "" && strings.Contains(libPath, "libcrypto.so"):
  250. fullPath := proc.Path(pid, "root", libPath)
  251. if _, err = os.Stat(fullPath); err == nil {
  252. libcryptoPath = fullPath
  253. }
  254. default:
  255. continue
  256. }
  257. if libsslPath != "" && libcryptoPath != "" {
  258. break
  259. }
  260. }
  261. if libsslPath == "" || libcryptoPath == "" {
  262. return "", ""
  263. }
  264. ef, err := elf.Open(libcryptoPath)
  265. if err != nil {
  266. return "", ""
  267. }
  268. defer ef.Close()
  269. rodataSection := ef.Section(".rodata")
  270. if rodataSection == nil {
  271. return "", ""
  272. }
  273. rodataSectionData, err := rodataSection.Data()
  274. if err != nil {
  275. return "", ""
  276. }
  277. var version string
  278. for _, b := range bytes.Split(rodataSectionData, []byte("\x00")) {
  279. if len(b) == 0 {
  280. continue
  281. }
  282. s := string(b)
  283. if !strings.HasPrefix(s, "OpenSSL") {
  284. continue
  285. }
  286. if m := opensslVersionRe.FindStringSubmatch(s); len(m) > 1 {
  287. version = m[1]
  288. }
  289. }
  290. return libsslPath, "v" + version
  291. }
  292. func getReturnOffsets(machine elf.Machine, instructions []byte) []int {
  293. var res []int
  294. switch machine {
  295. case elf.EM_X86_64:
  296. for i := 0; i < len(instructions); {
  297. ins, err := x86asm.Decode(instructions[i:], 64)
  298. if err == nil && ins.Op == x86asm.RET {
  299. res = append(res, i)
  300. }
  301. i += ins.Len
  302. }
  303. case elf.EM_AARCH64:
  304. for i := 0; i < len(instructions); {
  305. ins, err := arm64asm.Decode(instructions[i:])
  306. if err == nil && ins.Op == arm64asm.RET {
  307. res = append(res, i)
  308. }
  309. i += 4
  310. }
  311. }
  312. return res
  313. }