Skip to content

Commit 58e5d9f

Browse files
committed
new ssl read
1 parent 5db1ce1 commit 58e5d9f

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

src2/ssl_read.c

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,69 @@
1-
#include <linux/bpf.h>
1+
#include "vmlinux.h"
22
#include <bpf/bpf_helpers.h>
3+
#include <bpf/bpf_tracing.h>
4+
#include <bpf/bpf_core_read.h>
35

4-
#define TLS_MASK 0x100000000ULL
6+
#define MAX_BUF_SIZE 4096
7+
#define TLS_MASK 0x4000000000000000ULL
8+
9+
char LICENSE[] SEC("license") = "Dual MIT/GPL";
10+
11+
struct event {
12+
__u32 pid;
13+
__u64 tid;
14+
int len;
15+
char buf[MAX_BUF_SIZE];
16+
};
17+
18+
struct {
19+
__uint(type, BPF_MAP_TYPE_HASH);
20+
__uint(max_entries, 1024);
21+
__type(key, __u64); // pid_tgid | TLS_MASK
22+
__type(value, void *);
23+
} ssl_read_args SEC(".maps");
524

625
struct {
7-
__uint(type, BPF_MAP_TYPE_RINGBUF);
8-
__uint(max_entries, 1 << 24);
26+
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
927
} events SEC(".maps");
1028

11-
struct ssl_event_t {
12-
__u64 pid_tgid;
13-
__u64 ssl_ptr;
14-
__u64 buffer;
15-
int num;
16-
};
29+
// --- Helper to store buffer pointer at function entry ---
30+
static __always_inline void ssl_uprobe_read_enter_v3(struct pt_regs *ctx, __u64 id, __u32 pid, void *ssl, void *buffer, int num, int dummy) {
31+
if (buffer == NULL)
32+
return;
33+
34+
bpf_map_update_elem(&ssl_read_args, &id, &buffer, BPF_ANY);
35+
}
36+
37+
// --- Helper to process the return from SSL_read ---
38+
static __always_inline void process_exit_of_syscalls_read_recvfrom(struct pt_regs *ctx, __u64 id, __u64 pid, int ret, int is_tls) {
39+
void **bufp = bpf_map_lookup_elem(&ssl_read_args, &id);
40+
if (!bufp)
41+
return;
1742

43+
void *buf = *bufp;
44+
bpf_map_delete_elem(&ssl_read_args, &id);
45+
46+
if (ret <= 0 || ret > MAX_BUF_SIZE)
47+
return;
48+
49+
struct event evt = {};
50+
evt.pid = pid;
51+
evt.tid = id;
52+
evt.len = ret;
53+
54+
// Read plaintext data from buffer
55+
bpf_probe_read_user(&evt.buf, ret, buf);
56+
57+
// Submit to userspace
58+
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &evt, sizeof(evt));
59+
}
60+
61+
// --- Entry probe for SSL_read ---
1862
SEC("uprobe/SSL_read_v3")
19-
void BPF_UPROBE(ssl_read_enter_v3, void* ssl, void* buffer, int num) {
63+
void BPF_UPROBE(ssl_read_enter_v3, void *ssl, void *buffer, int num) {
2064
__u64 pid_tgid = bpf_get_current_pid_tgid();
2165
__u32 pid = pid_tgid >> 32;
2266
__u64 id = pid_tgid | TLS_MASK;
23-
ssl_uprobe_read_enter_v3(ctx, id, pid, ssl, buffer, num, 0);
24-
}
2567

26-
char LICENSE[] SEC("license") = "Dual BSD/GPL";
68+
ssl_uprobe_read_enter_v3(ctx, id, pid, ssl, buffer, num, 0);
69+
}

0 commit comments

Comments
 (0)