mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-08-19 08:59:16 +00:00
* Run `go generate tls_tapper.go` * Add `golang_uprobes.c` * Add Golang hooks and offsets * Add `golangConnection` struct and implement `pollGolangReadWrite` method * Upgrade `github.com/cilium/ebpf` version to `v0.8.1` * Fix the linter error * Move map related stuff to `maps.h` and run `go generate tls_tapper.go` * Remove unused parameter * Add an environment variable to test Golang locally * Replace `Libssl` occurrences with `Ssllib` for consistency * Fix exe path finding * Temporarily disable OpenSSL * Fix the mixed offsets and dissection preparation * Change the read symbol from `net/http.(*persistConn).Read` to `crypto/tls.(*Conn).Read` * Remove `len` and `cap` fields * Fix the indent * Fix the read data address * Make `golang_dial_writes` key `__u64` and include the PID * Fix the read data address one more time * Temporarily disable the PCAP capture * Add a uprobe for `net/http.(*gzipReader).Read` to read chunked HTTP response body * Cancel `golang_crypto_tls_read_uprobe` if it's a gzip read * Make hash map names more meaningful * Pass the connection address from `write` to `gzip` through a common address between `gzip` and `dial` * Fix the probed line number links * Add `golangReader` struct and implement its `Read` method * Have a single counter pair and request response matcher per Golang connection * Add `MIZU_GLOBAL_GOLANG_PATH` environment variable * `NULL` terminate the bytes with `unix.ByteSliceToString` * Temporarily reject the gzip chunks * Add malformed TODOs * Revert "`NULL` terminate the bytes with `unix.ByteSliceToString`" This reverts commit7ee7ef7e44
. * Bring back `len` and `cap` fields * Set `len` and `cap` in `golang_net_http_gzipreader_read_uprobe` as well * Remove two `TODO`s * Fix the `key_gzip` offsets * Compress if it's gzip chunk (probably wrong!) * Revert "Compress if it's gzip chunk (probably wrong!)" This reverts commit094a7c3da4
. * Remove `golang_net_http_gzipreader_read_uprobe` * Read constant 4KiB * Use constant read length * Get the correct len of bytes (saw the second entry) * Set all buffer sizes to `CHUNK_SIZE` * Remove a `TODO` * Revert "Temporarily disable the PCAP capture" This reverts commita2da15ef2d
. * Update `golang_crypto_tls_read_uprobe` * Set the `reader` field of `tlsStream` to fix a `nil pointer dereference` error * Don't export any fields of `golangConnection` * Close the reader when we drop the connection * Add a tracepoint for `sys_enter_close` to detect socket closes * Rename `socket` struct to `golang_socket` * Call `should_tap` in Golang uprobes * Add `log_error` calls * Revert "Temporarily disable OpenSSL" This reverts commitf54d9a453f
. * Fix linter * Revert "Revert "Temporarily disable OpenSSL"" This reverts commit2433d867af
. * Change `golang_read_writes` map type from `BPF_RINGBUF` to `BPF_PERF_OUTPUT` * Rename `golang_read_write` to `golang_event` * Define an error * Add comments * Revert "Revert "Revert "Temporarily disable OpenSSL""" This reverts commite5a1de9c71
. * Fix `pollGolang` * Revert "Revert "Revert "Revert "Temporarily disable OpenSSL"""" This reverts commit6e1bd5d4f3
. * Fix `panic: send on closed channel` * Revert "Revert "Revert "Revert "Revert "Temporarily disable OpenSSL""""" This reverts commit57d0584655
. * Use `findLibraryByPid` * Revert "Revert "Revert "Revert "Revert "Revert "Temporarily disable OpenSSL"""""" This reverts commit46f3d290b0
. * Revert "Revert "Revert "Revert "Revert "Revert "Revert "Temporarily disable OpenSSL""""""" This reverts commit775c833c06
. * Log tapping Golang * Fix `Poll` * Refactor `golang_net_http_dialconn_uprobe` * Remove an excess error check * Fix `can only use path@version syntax with 'go get' and 'go install' in module-aware mode` error in `tap/tlstapper/bpf-builder/build.sh` * Unify Golang and OpenSSL under a single perf event buffer and `tls_chunk` struct * Generate `tlsTapperChunkType` type (enum) as well * Use kernel page size for the `sys_closes` perf buffer * Fix the linter error * Fix `MIZU_GLOBAL_GOLANG_PID` environment variable's functionality * Rely on tracepoints for file descriptor retrieval in Golang implementation * Remove the unnecessary changes * Move common functions into `common.c` * Declare `lookup_ssl_info` function to reduce duplication * Fix linter * Add comments and TODOs * Remove `MIZU_GLOBAL_GOLANG_PATH` environment variable * Update the object files * Fix indentation * Update object files * Add `go_abi_internal.h` * Fix `lookup_ssl_info` * Convert indentation to spaces * Add header guard comment * Add more comments * Find the `ret` instructions using Capstone Engine and `uprobe` the `return` statements * Implement `get_fd_from_tcp_conn` function * Separate SSL contexts to OpenSSL and Go * Move `get_count_bytes` from `common.c` to `openssl_uprobes.c` * Rename everything contains Golang to Go * Reduce duplication in `go_uprobes.c` * Update the comments * Install Capstone in CI and Docker native builds * Update `devops/install-capstone.sh` * Add Capstone to AArch64 cross-compilation target * Fix some of the issues on ARM64 * Delete the map element in `_ex_urpobe` * Remove an unsued `LOG_` macro * Rename `aquynh` to `capstone-engine` * Add comment * Revert "Fix some of the issues on ARM64" This reverts commit0b3eceddf4
. * Revert "Revert "Fix some of the issues on ARM64"" This reverts commit681534ada1
. * Update object files * Remove unnecessary return * Increase timeout * #run_acceptance_tests * #run_acceptance_tests * Fix the `arm64v8` sourced builds * #run_acceptance_tests
155 lines
5.6 KiB
C
155 lines
5.6 KiB
C
/*
|
|
Note: This file is licenced differently from the rest of the project
|
|
SPDX-License-Identifier: GPL-2.0
|
|
Copyright (C) UP9 Inc.
|
|
|
|
|
|
---
|
|
|
|
README
|
|
|
|
Go does not follow any platform ABI like x86-64 System V ABI.
|
|
Before 1.17, Go followed stack-based Plan9 (Bell Labs) calling convention. (ABI0)
|
|
After 1.17, Go switched to an internal register-based calling convention. (ABIInternal)
|
|
For now, the probes in this file supports only ABIInternal (Go 1.17+)
|
|
|
|
`uretprobe` in Linux kernel uses trampoline pattern to jump to original return
|
|
address of the probed function. A Goroutine's stack size is 2Kb while a C thread is 2MB on Linux.
|
|
If stack size exceeds 2Kb, Go runtime relocates the stack. That causes the
|
|
return address to become incorrect in case of `uretprobe` and probed Go program crashes.
|
|
Therefore `uretprobe` CAN'T BE USED for a Go program.
|
|
|
|
`_ex_uprobe` suffixed probes suppose to be `uretprobe`(s) are actually `uprobe`(s)
|
|
because of the non-standard ABI of Go. Therefore we probe all `ret` mnemonics under the symbol
|
|
by automatically finding them through reading the ELF binary and disassembling the symbols.
|
|
Disassembly related code located in `go_offsets.go` file and it uses Capstone Engine.
|
|
Solution based on: https://github.com/iovisor/bcc/issues/1320#issuecomment-407927542
|
|
*Example* We probe an arbitrary point in a function body (offset +559):
|
|
https://github.com/golang/go/blob/go1.17.6/src/crypto/tls/conn.go#L1299
|
|
|
|
We get the file descriptor using the common $rax register that holds the address
|
|
of `go.itab.*net.TCPConn,net.Conn` and through a series of dereferencing
|
|
using `bpf_probe_read` calls in `go_crypto_tls_get_fd_from_tcp_conn` function.
|
|
|
|
---
|
|
|
|
SOURCES:
|
|
|
|
Tracing Go Functions with eBPF (before 1.17): https://www.grant.pizza/blog/tracing-go-functions-with-ebpf-part-2/
|
|
Challenges of BPF Tracing Go: https://blog.0x74696d.com/posts/challenges-of-bpf-tracing-go/
|
|
x86 calling conventions: https://en.wikipedia.org/wiki/X86_calling_conventions
|
|
Plan 9 from Bell Labs: https://en.wikipedia.org/wiki/Plan_9_from_Bell_Labs
|
|
The issue for calling convention change in Go: https://github.com/golang/go/issues/40724
|
|
Proposal of Register-based Go calling convention: https://go.googlesource.com/proposal/+/master/design/40724-register-calling.md
|
|
Go internal ABI (1.17) specification: https://go.googlesource.com/go/+/refs/heads/dev.regabi/src/cmd/compile/internal-abi.md
|
|
Go internal ABI (current) specification: https://go.googlesource.com/go/+/refs/heads/master/src/cmd/compile/abi-internal.md
|
|
A Quick Guide to Go's Assembler: https://go.googlesource.com/go/+/refs/heads/dev.regabi/doc/asm.html
|
|
Dissecting Go Binaries: https://www.grant.pizza/blog/dissecting-go-binaries/
|
|
Capstone Engine: https://www.capstone-engine.org/
|
|
*/
|
|
|
|
#include "include/headers.h"
|
|
#include "include/util.h"
|
|
#include "include/maps.h"
|
|
#include "include/log.h"
|
|
#include "include/logger_messages.h"
|
|
#include "include/pids.h"
|
|
#include "include/common.h"
|
|
#include "include/go_abi_internal.h"
|
|
#include "include/go_types.h"
|
|
|
|
static __always_inline __u32 go_crypto_tls_get_fd_from_tcp_conn(struct pt_regs *ctx) {
|
|
struct go_interface conn;
|
|
long err = bpf_probe_read(&conn, sizeof(conn), (void*)GO_ABI_INTERNAL_PT_REGS_R1(ctx));
|
|
if (err != 0) {
|
|
return invalid_fd;
|
|
}
|
|
|
|
void* net_fd_ptr;
|
|
err = bpf_probe_read(&net_fd_ptr, sizeof(net_fd_ptr), conn.ptr);
|
|
if (err != 0) {
|
|
return invalid_fd;
|
|
}
|
|
|
|
__u32 fd;
|
|
err = bpf_probe_read(&fd, sizeof(fd), net_fd_ptr + 0x10);
|
|
if (err != 0) {
|
|
return invalid_fd;
|
|
}
|
|
|
|
return fd;
|
|
}
|
|
|
|
static __always_inline void go_crypto_tls_uprobe(struct pt_regs *ctx, struct bpf_map_def* go_context) {
|
|
__u64 pid_tgid = bpf_get_current_pid_tgid();
|
|
__u64 pid = pid_tgid >> 32;
|
|
if (!should_tap(pid)) {
|
|
return;
|
|
}
|
|
|
|
struct ssl_info info = new_ssl_info();
|
|
|
|
info.buffer_len = GO_ABI_INTERNAL_PT_REGS_R2(ctx);
|
|
info.buffer = (void*)GO_ABI_INTERNAL_PT_REGS_R4(ctx);
|
|
info.fd = go_crypto_tls_get_fd_from_tcp_conn(ctx);
|
|
|
|
// GO_ABI_INTERNAL_PT_REGS_GP is Goroutine address
|
|
__u64 pid_fp = pid << 32 | GO_ABI_INTERNAL_PT_REGS_GP(ctx);
|
|
long err = bpf_map_update_elem(go_context, &pid_fp, &info, BPF_ANY);
|
|
|
|
if (err != 0) {
|
|
log_error(ctx, LOG_ERROR_PUTTING_SSL_CONTEXT, pid_tgid, err, 0l);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
static __always_inline void go_crypto_tls_ex_uprobe(struct pt_regs *ctx, struct bpf_map_def* go_context, __u32 flags) {
|
|
__u64 pid_tgid = bpf_get_current_pid_tgid();
|
|
__u64 pid = pid_tgid >> 32;
|
|
if (!should_tap(pid)) {
|
|
return;
|
|
}
|
|
|
|
// GO_ABI_INTERNAL_PT_REGS_GP is Goroutine address
|
|
__u64 pid_fp = pid << 32 | GO_ABI_INTERNAL_PT_REGS_GP(ctx);
|
|
struct ssl_info *info_ptr = bpf_map_lookup_elem(go_context, &pid_fp);
|
|
|
|
if (info_ptr == NULL) {
|
|
return;
|
|
}
|
|
bpf_map_delete_elem(go_context, &pid_fp);
|
|
|
|
struct ssl_info info;
|
|
long err = bpf_probe_read(&info, sizeof(struct ssl_info), info_ptr);
|
|
|
|
if (err != 0) {
|
|
log_error(ctx, LOG_ERROR_READING_SSL_CONTEXT, pid_tgid, err, ORIGIN_SSL_URETPROBE_CODE);
|
|
return;
|
|
}
|
|
|
|
output_ssl_chunk(ctx, &info, info.buffer_len, pid_tgid, flags);
|
|
|
|
return;
|
|
}
|
|
|
|
SEC("uprobe/go_crypto_tls_write")
|
|
void BPF_KPROBE(go_crypto_tls_write) {
|
|
go_crypto_tls_uprobe(ctx, &go_write_context);
|
|
}
|
|
|
|
SEC("uprobe/go_crypto_tls_write_ex")
|
|
void BPF_KPROBE(go_crypto_tls_write_ex) {
|
|
go_crypto_tls_ex_uprobe(ctx, &go_write_context, 0);
|
|
}
|
|
|
|
SEC("uprobe/go_crypto_tls_read")
|
|
void BPF_KPROBE(go_crypto_tls_read) {
|
|
go_crypto_tls_uprobe(ctx, &go_read_context);
|
|
}
|
|
|
|
SEC("uprobe/go_crypto_tls_read_ex")
|
|
void BPF_KPROBE(go_crypto_tls_read_ex) {
|
|
go_crypto_tls_ex_uprobe(ctx, &go_read_context, FLAGS_IS_READ_BIT);
|
|
}
|