-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathprobes.c
480 lines (385 loc) · 11.8 KB
/
probes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#define bpf_target_x86
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
char LICENSE[] SEC("license") = "Dual BSD/GPL";
struct ebpf_event_t {
bool m_v6;
void * m_handle;
bool m_remove;
uint32_t m_user_id;
uint32_t m_process_id;
uint32_t m_source_address;
__uint128_t m_source_address_v6;
uint16_t m_source_port;
uint32_t m_destination_address;
__uint128_t m_destination_address_v6;
uint16_t m_destination_port;
uint64_t m_timestamp;
uint8_t m_protocol;
} __attribute__((packed));
struct bpf_map_def SEC("maps") g_probe_ipv4_events = {
.type = BPF_MAP_TYPE_RINGBUF,
.max_entries = 4096 * 64
};
struct bpf_map_def SEC("maps") g_ipv4_tcp_connect_map = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(uint64_t),
.value_size = sizeof(struct sock *),
.max_entries = 1000
};
struct bpf_map_def SEC("maps") g_ipv6_tcp_connect_map = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(uint64_t),
.value_size = sizeof(struct sock *),
.max_entries = 1000
};
struct bpf_map_def SEC("maps") g_send_map1 = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(uint64_t),
.value_size = sizeof(struct socket *),
.max_entries = 1000
};
struct bpf_map_def SEC("maps") g_send_map2 = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(uint64_t),
.value_size = sizeof(struct msghdr *),
.max_entries = 1000
};
#define AF_INET 2
SEC("kprobe/tcp_v4_connect") int
kprobe_tcp_v4_connect(const struct pt_regs *const p_context)
{
struct sock *const l_sock = (void *)PT_REGS_PARM1(p_context);
const uint64_t l_id = bpf_get_current_pid_tgid();
bpf_map_update_elem(&g_ipv4_tcp_connect_map, &l_id, &l_sock, 0);
return 0;
}
SEC("kretprobe/tcp_v4_connect") int
kretprobe_tcp_v4_connect(const struct pt_regs *const p_context)
{
const uint64_t l_id = bpf_get_current_pid_tgid();
const uint32_t l_pid = l_id >> 32;
struct sock **const l_sock_ref = bpf_map_lookup_elem(
&g_ipv4_tcp_connect_map,
&l_id
);
if (!l_sock_ref) {
return 0;
}
struct sock *const l_sock = *l_sock_ref;
if (bpf_map_delete_elem(&g_ipv4_tcp_connect_map, &l_id) != 0) {
bpf_printk("bpf_map_delete_elem failed");
return 0;
}
uint16_t l_source_port;
uint16_t l_destination_port;
uint32_t l_source_address;
uint32_t l_destination_address;
bpf_probe_read(
&l_source_port,
sizeof(l_source_port),
&l_sock->__sk_common.skc_num
);
bpf_probe_read(
&l_destination_port,
sizeof(l_destination_port),
&l_sock->__sk_common.skc_dport
);
bpf_probe_read(
&l_source_address,
sizeof(l_source_address),
&l_sock->__sk_common.skc_rcv_saddr
);
bpf_probe_read(
&l_destination_address,
sizeof(l_destination_address),
&l_sock->__sk_common.skc_daddr
);
struct ebpf_event_t *const l_event = bpf_ringbuf_reserve(
&g_probe_ipv4_events,
sizeof(struct ebpf_event_t),
0
);
if (!l_event) {
return 0;
}
l_event->m_v6 = false;
l_event->m_timestamp = bpf_ktime_get_ns();
l_event->m_user_id = bpf_get_current_uid_gid();
l_event->m_process_id = l_pid;
l_event->m_handle = l_sock;
l_event->m_remove = false;
l_event->m_protocol = 6;
l_event->m_source_address = l_source_address;
l_event->m_source_port = l_source_port;
l_event->m_destination_port = l_destination_port;
l_event->m_destination_address = l_destination_address;
bpf_ringbuf_submit(l_event, BPF_RB_FORCE_WAKEUP);
return 0;
}
SEC("kprobe/security_socket_sendmsg") int
kprobe_security_socket_send_msg(const struct pt_regs *const p_context)
{
struct socket *const l_socket = (void *)PT_REGS_PARM1(p_context);
struct msghdr *const l_msg = (void *)PT_REGS_PARM2(p_context);
const uint64_t l_id = bpf_get_current_pid_tgid();
bpf_map_update_elem(&g_send_map1, &l_id, &l_socket, 0);
bpf_map_update_elem(&g_send_map2, &l_id, &l_msg, 0);
return 0;
}
SEC("kprobe/tcp_v6_connect") int
kprobe_tcp_v6_connect(const struct pt_regs *const p_context)
{
struct sock *const l_sock = (void *)PT_REGS_PARM1(p_context);
const uint64_t l_id = bpf_get_current_pid_tgid();
bpf_map_update_elem(&g_ipv6_tcp_connect_map, &l_id, &l_sock, 0);
return 0;
}
SEC("kretprobe/tcp_v6_connect") int
kretprobe_tcp_v6_connect(const struct pt_regs *const p_context)
{
const uint64_t l_id = bpf_get_current_pid_tgid();
const uint32_t l_pid = l_id >> 32;
struct sock **l_sock_ref = bpf_map_lookup_elem(
&g_ipv6_tcp_connect_map,
&l_id
);
if (!l_sock_ref) {
bpf_printk("tcp_v6_connect_return no entry");
return 0;
}
if (bpf_map_delete_elem(&g_ipv6_tcp_connect_map, &l_id) != 0) {
bpf_printk("bpf_map_delete_elem failed");
return 0;
}
struct sock *const l_sock = *l_sock_ref;
struct inet_sock *const l_inet = (struct inet_sock *)l_sock;
uint16_t l_source_port;
uint16_t l_destination_port;
__uint128_t l_source_address;
__uint128_t l_destination_address;
bpf_probe_read(
&l_source_port,
sizeof(l_source_port),
&l_sock->__sk_common.skc_num
);
bpf_probe_read(
&l_destination_port,
sizeof(l_destination_port),
&l_sock->__sk_common.skc_dport
);
bpf_probe_read(
&l_source_address,
sizeof(l_source_address),
&l_sock->__sk_common.skc_v6_rcv_saddr
);
bpf_probe_read(
&l_destination_address,
sizeof(l_destination_address),
&l_sock->__sk_common.skc_v6_daddr
);
struct ebpf_event_t *const l_event = bpf_ringbuf_reserve(
&g_probe_ipv4_events,
sizeof(struct ebpf_event_t),
0
);
if (!l_event) {
return 0;
}
l_event->m_v6 = true;
l_event->m_timestamp = bpf_ktime_get_ns();
l_event->m_user_id = bpf_get_current_uid_gid();
l_event->m_process_id = l_pid;
l_event->m_handle = l_sock;
l_event->m_remove = false;
l_event->m_protocol = 6;
l_event->m_source_address_v6 = l_source_address;
l_event->m_source_port = l_source_port;
l_event->m_destination_port = l_destination_port;
l_event->m_destination_address_v6 = l_destination_address;
bpf_ringbuf_submit(l_event, BPF_RB_FORCE_WAKEUP);
return 0;
}
SEC("kretprobe/security_socket_sendmsg") int
kretprobe_security_socket_send_msg(const struct pt_regs *const p_context_ignore)
{
const uint64_t l_id = bpf_get_current_pid_tgid();
struct socket **const l_sock_ref = bpf_map_lookup_elem(
&g_send_map1,
&l_id
);
struct msghdr **const l_msg_ref = bpf_map_lookup_elem(
&g_send_map2,
&l_id
);
if (!l_sock_ref) {
bpf_printk("bpf_map_lookup_elem l_sock_ref failed");
return 0;
}
if (!l_msg_ref) {
bpf_printk("bpf_map_lookup_elem l_msg_ref failed");
return 0;
}
struct socket *const l_socket = *l_sock_ref;
struct msghdr *const l_msg = *l_msg_ref;
if (bpf_map_delete_elem(&g_send_map1, &l_id) != 0) {
bpf_printk("bpf_map_delete_elem failed");
return 0;
}
if (bpf_map_delete_elem(&g_send_map2, &l_id) != 0) {
bpf_printk("bpf_map_delete_elem failed");
return 0;
}
const struct sock * l_sock = 0;
const struct sockaddr_in *l_usin = 0;
short int l_type = 0;
sa_family_t l_family = 0;
uint16_t l_source_port = 0;
uint16_t l_destination_port = 0;
uint32_t l_source_address = 0;
uint32_t l_destination_address = 0;
uint8_t l_protocol = 0;
if (
bpf_probe_read(
&l_sock,
sizeof(l_sock),
&l_socket->sk
) != 0
) {
bpf_printk("bpf_probe_read failed line %d", __LINE__);
return 0;
}
if (
bpf_probe_read(
&l_family,
sizeof(l_family),
&l_sock->__sk_common.skc_family
) != 0
) {
bpf_printk("bpf_probe_read failed line %d", __LINE__);
return 0;
}
if (l_family != AF_INET) {
return 0;
}
if (
bpf_probe_read(
&l_type,
sizeof(l_type),
&l_socket->type
) != 0
) {
bpf_printk("bpf_probe_read failed line %d", __LINE__);
return 0;
}
if (
bpf_probe_read(
&l_type,
sizeof(l_type),
&l_socket->type
) != 0
) {
bpf_printk("bpf_probe_read failed line %d", __LINE__);
return 0;
}
if (
bpf_probe_read(
&l_protocol,
sizeof(l_protocol),
&l_sock->sk_protocol
) != 0
) {
bpf_printk("bpf_probe_read failed line %d", __LINE__);
return 0;
}
if (
bpf_probe_read(
&l_source_port,
sizeof(l_source_port),
&l_sock->__sk_common.skc_num
) != 0
) {
bpf_printk("bpf_probe_read failed line %d", __LINE__);
return 0;
}
if (
bpf_probe_read(
&l_source_address,
sizeof(l_source_address),
&l_sock->__sk_common.skc_rcv_saddr
) != 0
) {
bpf_printk("bpf_probe_read failed line %d", __LINE__);
return 0;
}
if (
bpf_probe_read(
&l_usin,
sizeof(l_usin),
&l_msg->msg_name
) != 0
) {
bpf_printk("bpf_probe_read failed line %d", __LINE__);
return 0;
}
if (
bpf_probe_read(
&l_destination_port,
sizeof(l_destination_port),
&l_sock->__sk_common.skc_dport
) != 0 || l_destination_port == 0
) {
if (
bpf_probe_read(
&l_destination_port,
sizeof(l_destination_port),
&l_usin->sin_port
) != 0
) {
bpf_printk("bpf_probe_read port failed %d", __LINE__);
return 0;
}
}
if (
bpf_probe_read(
&l_destination_address,
sizeof(l_destination_address),
&l_sock->__sk_common.skc_daddr
) != 0 || l_destination_address == 0
) {
if (
bpf_probe_read(
&l_destination_address,
sizeof(l_destination_address),
&l_usin->sin_addr
) != 0
) {
bpf_printk("bpf_probe_read address failed %d", __LINE__);
return 0;
}
}
struct ebpf_event_t *const l_event = bpf_ringbuf_reserve(
&g_probe_ipv4_events,
sizeof(struct ebpf_event_t),
0
);
if (!l_event) {
bpf_printk("bpf_ringbuf_reserve failed %d", __LINE__);
return 0;
}
l_event->m_v6 = false;
l_event->m_timestamp = bpf_ktime_get_ns();
l_event->m_user_id = bpf_get_current_uid_gid();
l_event->m_process_id = bpf_get_current_pid_tgid() >> 32;
l_event->m_handle = l_socket;
l_event->m_remove = false;
l_event->m_source_address = l_source_address;
l_event->m_source_port = l_source_port;
l_event->m_destination_port = l_destination_port;
l_event->m_destination_address = l_destination_address;
l_event->m_protocol = l_protocol;
bpf_ringbuf_submit(l_event, BPF_RB_FORCE_WAKEUP);
return 0;
}