Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce syscalls #56

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,38 @@ jobs:
golint:
name: Golint
runs-on: ubuntu-latest
container:
image: ubuntu:24.04
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2

- uses: actions/setup-go@v2
with:
go-version: '^1.20'
go-version: '^1.21'

- name: Install dependencies
run: |
sudo apt update
sudo apt install -y \
apt update
apt install -y \
sudo \
curl \
build-essential \
xz-utils \
libpcap-dev \
clang \
llvm \
libbpf-dev \
linux-headers-$(uname -r)
libelf-dev
./install-capstone.sh

- name: Generate eBPF object files and Go bindings
run: make bpf

- name: Go lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v6
with:
version: latest
args: --timeout=10m
go-version: '^1.21'
10 changes: 5 additions & 5 deletions bpf/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static __always_inline int add_address_to_chunk(struct pt_regs* ctx, struct tls_
return 1;
}

static __always_inline void send_chunk_part(struct pt_regs* ctx, __u8* buffer, __u64 id,
static __always_inline void send_chunk_part(struct pt_regs* ctx, uintptr_t buffer, __u64 id,
struct tls_chunk* chunk, int start, int end) {
size_t recorded = MIN(end - start, sizeof(chunk->data));

Expand All @@ -44,10 +44,10 @@ static __always_inline void send_chunk_part(struct pt_regs* ctx, __u8* buffer, _
//
long err = 0;
if (chunk->recorded == sizeof(chunk->data)) {
err = bpf_probe_read(chunk->data, sizeof(chunk->data), buffer + start);
err = bpf_probe_read(chunk->data, sizeof(chunk->data), (void*)(buffer + start));
} else {
recorded &= (sizeof(chunk->data) - 1); // Buffer must be N^2
err = bpf_probe_read(chunk->data, recorded, buffer + start);
err = bpf_probe_read(chunk->data, recorded, (void*)(buffer + start));
}

if (err != 0) {
Expand All @@ -58,7 +58,7 @@ static __always_inline void send_chunk_part(struct pt_regs* ctx, __u8* buffer, _
bpf_perf_event_output(ctx, &chunks_buffer, BPF_F_CURRENT_CPU, chunk, sizeof(struct tls_chunk));
}

static __always_inline void send_chunk(struct pt_regs* ctx, __u8* buffer, __u64 id, struct tls_chunk* chunk) {
static __always_inline void send_chunk(struct pt_regs* ctx, uintptr_t buffer, __u64 id, struct tls_chunk* chunk) {
// ebpf loops must be bounded at compile time, we can't use (i < chunk->len / CHUNK_SIZE)
//
// https://lwn.net/Articles/794934/
Expand Down Expand Up @@ -115,7 +115,7 @@ static __always_inline struct ssl_info new_ssl_info() {
return info;
}

static __always_inline struct ssl_info lookup_ssl_info(struct pt_regs* ctx, struct bpf_map_def* map_fd, __u64 pid_tgid) {
static __always_inline struct ssl_info lookup_ssl_info(struct pt_regs* ctx, void* map_fd, __u64 pid_tgid) {
struct ssl_info* infoPtr = bpf_map_lookup_elem(map_fd, &pid_tgid);
struct ssl_info info = new_ssl_info();

Expand Down
6 changes: 3 additions & 3 deletions bpf/fd_to_address_tracepoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Copyright (C) Kubeshark
#define IPV4_ADDR_LEN (16)

struct accept_info {
__u32* addrlen;
uintptr_t addrlen;
};

BPF_HASH(accept_syscall_context, __u64, struct accept_info);
Expand All @@ -24,7 +24,7 @@ struct sys_enter_accept4_ctx {

__u64 fd;
__u64* sockaddr;
__u32* addrlen;
uintptr_t addrlen;
};

SEC("tracepoint/syscalls/sys_enter_accept4")
Expand Down Expand Up @@ -84,7 +84,7 @@ void sys_exit_accept4(struct sys_exit_accept4_ctx* ctx) {
}

__u32 addrlen;
bpf_probe_read(&addrlen, sizeof(__u32), info.addrlen);
bpf_probe_read(&addrlen, sizeof(__u32), (void*)info.addrlen);

if (addrlen != IPV4_ADDR_LEN) {
// Currently only ipv4 is supported linux-src/include/linux/inet.h
Expand Down
4 changes: 2 additions & 2 deletions bpf/fd_tracepoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct sys_exit_read_write_ctx {
__u64 ret;
};

static __always_inline void fd_tracepoints_handle_openssl(struct sys_enter_read_write_ctx* ctx, __u64 id, struct ssl_info* infoPtr, struct bpf_map_def* map_fd, __u64 origin_code) {
static __always_inline void fd_tracepoints_handle_openssl(struct sys_enter_read_write_ctx* ctx, __u64 id, struct ssl_info* infoPtr, void* map_fd, __u64 origin_code) {
struct ssl_info info;
long err = bpf_probe_read(&info, sizeof(struct ssl_info), infoPtr);

Expand All @@ -46,7 +46,7 @@ static __always_inline void fd_tracepoints_handle_openssl(struct sys_enter_read_
}
}

static __always_inline void fd_tracepoints_handle_go(struct sys_enter_read_write_ctx* ctx, __u64 id, struct bpf_map_def* map_fd, __u64 origin_code) {
static __always_inline void fd_tracepoints_handle_go(struct sys_enter_read_write_ctx* ctx, __u64 id, void* map_fd, __u64 origin_code) {
__u32 fd = ctx->fd;

long err = bpf_map_update_elem(map_fd, &id, &fd, BPF_ANY);
Expand Down
8 changes: 4 additions & 4 deletions bpf/go_uprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static __always_inline int go_crypto_tls_get_fd_from_tcp_conn(struct pt_regs* ct
return 0;
}

static __always_inline void go_crypto_tls_uprobe(struct pt_regs* ctx, struct bpf_map_def* go_context, enum ABI abi) {
static __always_inline void go_crypto_tls_uprobe(struct pt_regs* ctx, void* go_context, enum ABI abi) {
__u64 pid_tgid = tracer_get_current_pid_tgid();
__u64 pid = pid_tgid >> 32;
if (!should_target(pid)) {
Expand Down Expand Up @@ -210,10 +210,10 @@ static __always_inline void go_crypto_tls_uprobe(struct pt_regs* ctx, struct bpf
return;
}
// We basically add 00 suffix to the hex address.
info.buffer = (void*)((long)info.buffer << 8);
info.buffer = ((long)info.buffer << 8);
} else {
#endif
info.buffer = (void*)GO_ABI_INTERNAL_PT_REGS_R4(ctx);
info.buffer = GO_ABI_INTERNAL_PT_REGS_R4(ctx);
#if defined(bpf_target_x86)
}
#endif
Expand Down Expand Up @@ -250,7 +250,7 @@ static __always_inline void go_crypto_tls_uprobe(struct pt_regs* ctx, struct bpf
return;
}

static __always_inline void go_crypto_tls_ex_uprobe(struct pt_regs* ctx, struct bpf_map_def* go_context, struct bpf_map_def* go_user_kernel_context, __u32 flags, enum ABI abi) {
static __always_inline void go_crypto_tls_ex_uprobe(struct pt_regs* ctx, void* go_context, void* go_user_kernel_context, __u32 flags, enum ABI abi) {
__u64 pid_tgid = tracer_get_current_pid_tgid();
__u64 pid = pid_tgid >> 32;
if (!should_target(pid)) {
Expand Down
6 changes: 3 additions & 3 deletions bpf/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Copyright (C) Kubeshark
const __s32 invalid_fd = -1;

static int add_address_to_chunk(struct pt_regs* ctx, struct tls_chunk* chunk, __u64 id, __u32 fd, struct ssl_info* info);
static void send_chunk_part(struct pt_regs* ctx, __u8* buffer, __u64 id, struct tls_chunk* chunk, int start, int end);
static void send_chunk(struct pt_regs* ctx, __u8* buffer, __u64 id, struct tls_chunk* chunk);
static void send_chunk_part(struct pt_regs* ctx, uintptr_t buffer, __u64 id, struct tls_chunk* chunk, int start, int end);
static void send_chunk(struct pt_regs* ctx, uintptr_t buffer, __u64 id, struct tls_chunk* chunk);
static void output_ssl_chunk(struct pt_regs* ctx, struct ssl_info* info, int count_bytes, __u64 id, __u32 flags, __u64 cgroup_id);
static struct ssl_info new_ssl_info();
static struct ssl_info lookup_ssl_info(struct pt_regs* ctx, struct bpf_map_def* map_fd, __u64 pid_tgid);
static struct ssl_info lookup_ssl_info(struct pt_regs* ctx, void* map_fd, __u64 pid_tgid);

#endif /* __COMMON__ */
16 changes: 8 additions & 8 deletions bpf/include/maps.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct tls_chunk {
};

struct ssl_info {
void* buffer;
uintptr_t buffer;
__u32 buffer_len;
__u32 fd;
__u64 created_at_nano;
Expand All @@ -56,7 +56,7 @@ struct ssl_info {
// for ssl_write and ssl_read must be zero
// for ssl_write_ex and ssl_read_ex save the *written/*readbytes pointer.
//
size_t* count_ptr;
uintptr_t count_ptr;
};

typedef __u8 conn_flags;
Expand Down Expand Up @@ -131,12 +131,12 @@ struct pkt_data {
};

#define BPF_MAP(_name, _type, _key_type, _value_type, _max_entries) \
struct bpf_map_def SEC("maps") _name = { \
.type = _type, \
.key_size = sizeof(_key_type), \
.value_size = sizeof(_value_type), \
.max_entries = _max_entries, \
};
struct { \
__uint(type, _type); \
__type(key, _key_type); \
__type(value, _value_type); \
__uint(max_entries, _max_entries); \
} _name SEC(".maps");

#define BPF_HASH(_name, _key_type, _value_type) \
BPF_MAP(_name, BPF_MAP_TYPE_HASH, _key_type, _value_type, MAX_ENTRIES_HASH)
Expand Down
2 changes: 1 addition & 1 deletion bpf/include/pids.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Copyright (C) Kubeshark
#ifndef __PIDS__
#define __PIDS__

int _pid_in_map(struct bpf_map_def* pmap, __u32 pid) {
int _pid_in_map(void* pmap, __u32 pid) {
__u32* shouldTarget = bpf_map_lookup_elem(pmap, &pid);

if (shouldTarget != NULL && *shouldTarget == 1) {
Expand Down
14 changes: 7 additions & 7 deletions bpf/openssl_uprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Copyright (C) Kubeshark
static __always_inline int get_count_bytes(struct pt_regs* ctx, struct ssl_info* info, __u64 id) {
int returnValue = PT_REGS_RC(ctx);

if (info->count_ptr == NULL) {
if (info->count_ptr == 0) {
// ssl_read and ssl_write return the number of bytes written/read
//
return returnValue;
Expand All @@ -39,7 +39,7 @@ static __always_inline int get_count_bytes(struct pt_regs* ctx, struct ssl_info*
return countBytes;
}

static __always_inline void ssl_uprobe(struct pt_regs* ctx, void* ssl, void* buffer, int num, struct bpf_map_def* map_fd, size_t* count_ptr) {
static __always_inline void ssl_uprobe(struct pt_regs* ctx, void* ssl, uintptr_t buffer, int num, void* map_fd, uintptr_t count_ptr) {
long err;

__u64 id = tracer_get_current_pid_tgid();
Expand All @@ -60,7 +60,7 @@ static __always_inline void ssl_uprobe(struct pt_regs* ctx, void* ssl, void* buf
}
}

static __always_inline void ssl_uretprobe(struct pt_regs* ctx, struct bpf_map_def* map_fd, __u32 flags) {
static __always_inline void ssl_uretprobe(struct pt_regs* ctx, void* map_fd, __u32 flags) {
__u64 id = tracer_get_current_pid_tgid();

if (!should_target(id >> 32)) {
Expand Down Expand Up @@ -108,7 +108,7 @@ static __always_inline void ssl_uretprobe(struct pt_regs* ctx, struct bpf_map_de
}

SEC("uprobe/ssl_write")
void BPF_KPROBE(ssl_write, void* ssl, void* buffer, int num) {
void BPF_KPROBE(ssl_write, void* ssl, uintptr_t buffer, int num) {
ssl_uprobe(ctx, ssl, buffer, num, &openssl_write_context, 0);
}

Expand All @@ -118,7 +118,7 @@ void BPF_KPROBE(ssl_ret_write) {
}

SEC("uprobe/ssl_read")
void BPF_KPROBE(ssl_read, void* ssl, void* buffer, int num) {
void BPF_KPROBE(ssl_read, void* ssl, uintptr_t buffer, int num) {
ssl_uprobe(ctx, ssl, buffer, num, &openssl_read_context, 0);
}

Expand All @@ -128,7 +128,7 @@ void BPF_KPROBE(ssl_ret_read) {
}

SEC("uprobe/ssl_write_ex")
void BPF_KPROBE(ssl_write_ex, void* ssl, void* buffer, size_t num, size_t* written) {
void BPF_KPROBE(ssl_write_ex, void* ssl, uintptr_t buffer, size_t num, uintptr_t written) {
ssl_uprobe(ctx, ssl, buffer, num, &openssl_write_context, written);
}

Expand All @@ -138,7 +138,7 @@ void BPF_KPROBE(ssl_ret_write_ex) {
}

SEC("uprobe/ssl_read_ex")
void BPF_KPROBE(ssl_read_ex, void* ssl, void* buffer, size_t num, size_t* readbytes) {
void BPF_KPROBE(ssl_read_ex, void* ssl, uintptr_t buffer, size_t num, uintptr_t readbytes) {
ssl_uprobe(ctx, ssl, buffer, num, &openssl_read_context, readbytes);
}

Expand Down
8 changes: 4 additions & 4 deletions bpf/packet_sniffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static __always_inline int parse_packet(struct __sk_buff* skb, int is_tc, __u32*

if (is_tc) {
struct ethhdr* eth = (struct ethhdr*)cursor;
if (eth + 1 > data_end)
if (eth + 1 > (struct ethhdr*)data_end)
return 1;

cursor += sizeof(struct ethhdr);
Expand All @@ -195,7 +195,7 @@ static __always_inline int parse_packet(struct __sk_buff* skb, int is_tc, __u32*
if (skb->protocol == bpf_htons(ETH_P_IP)) {

struct iphdr* ip = (struct iphdr*)cursor;
if (ip + 1 > data_end)
if (ip + 1 > (struct iphdr*)data_end)
return 2;

if (src_ip4) {
Expand All @@ -221,7 +221,7 @@ static __always_inline int parse_packet(struct __sk_buff* skb, int is_tc, __u32*
if (ip_proto == IPPROTO_TCP)
{
struct tcphdr* tcp = (struct tcphdr*)cursor;
if (tcp + 1 > data_end)
if (tcp + 1 > (struct tcphdr*)data_end)
return 5;
if (src_port) {
*src_port = tcp->source;
Expand All @@ -240,7 +240,7 @@ static __always_inline int parse_packet(struct __sk_buff* skb, int is_tc, __u32*
if (ip_proto == IPPROTO_UDP)
{
struct udphdr* udp = (struct udphdr*)cursor;
if (udp + 1 > data_end)
if (udp + 1 > (struct udphdr*)data_end)
return 5;
if (src_port) {
*src_port = udp->source;
Expand Down
4 changes: 2 additions & 2 deletions bpf/tcp_kprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static __always_inline int tcp_kprobes_get_address_pair_from_ctx(struct pt_regs*
return 0;
}

static __always_inline void tcp_kprobes_forward_go(struct pt_regs* ctx, __u64 id, __u32 fd, struct address_info address_info, struct bpf_map_def* map_fd_go_user_kernel) {
static __always_inline void tcp_kprobes_forward_go(struct pt_regs* ctx, __u64 id, __u32 fd, struct address_info address_info, void* map_fd_go_user_kernel) {
__u32 pid = id >> 32;
__u64 key = (__u64)pid << 32 | fd;

Expand All @@ -76,7 +76,7 @@ static void __always_inline tcp_kprobes_forward_openssl(struct ssl_info* info_pt
info_ptr->address_info.sport = address_info.sport;
}

static __always_inline void tcp_kprobe(struct pt_regs* ctx, struct bpf_map_def* map_fd_openssl, struct bpf_map_def* map_fd_go_kernel, struct bpf_map_def* map_fd_go_user_kernel) {
static __always_inline void tcp_kprobe(struct pt_regs* ctx, void* map_fd_openssl, void* map_fd_go_kernel, void* map_fd_go_user_kernel) {
long err;

__u64 id = tracer_get_current_pid_tgid();
Expand Down
Loading
Loading