This repository was archived by the owner on May 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtracee.bpf.c
More file actions
2526 lines (2062 loc) · 81.4 KB
/
Copy pathtracee.bpf.c
File metadata and controls
2526 lines (2062 loc) · 81.4 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// +build ignore
// Note: This file is licenced differently from the rest of the project
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) Aqua Security inc.
#include <vmlinux.h>
#include <vmlinux_flavors.h>
#include <vmlinux_missing.h>
#undef container_of
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <maps.h>
#include <types.h>
#include <common/arch.h>
#include <common/arguments.h>
#include <common/binprm.h>
#include <common/buffer.h>
#include <common/cgroups.h>
#include <common/common.h>
#include <common/consts.h>
#include <common/context.h>
#include <common/filesystem.h>
#include <common/filtering.h>
#include <common/logging.h>
#include <common/memory.h>
#include <common/network.h>
#include <common/stats.h>
#include <common/metrics.h>
#include <common/signatures.h>
char LICENSE[] SEC("license") = "GPL";
extern _Bool LINUX_HAS_SYSCALL_WRAPPER __kconfig;
// trace/events/syscalls.h: TP_PROTO(struct pt_regs *regs, long id)
// initial entry for sys_enter syscall logic
SEC("raw_tracepoint/sys_enter")
int tracepoint__raw_syscalls__sys_enter(struct bpf_raw_tracepoint_args *ctx)
{
struct task_struct *task = (struct task_struct *) bpf_get_current_task();
int id = ctx->args[1];
if (is_compat(task)) {
// Translate 32bit syscalls to 64bit syscalls, so we can send to the correct handler
u32 *id_64 = bpf_map_lookup_elem(&sys_32_to_64_map, &id);
if (id_64 == 0)
return 0;
id = *id_64;
}
u64 cgroup_id = 0;
if (global_config.cgroup_v1) {
cgroup_id = get_cgroup_v1_subsys0_id(task);
} else {
cgroup_id = bpf_get_current_cgroup_id();
}
// Skip if cgroup is muted
if (bpf_map_lookup_elem(&ignored_cgroups_map, &cgroup_id) != NULL) {
return 0;
}
// Update containers syscall stats.
if (global_config.track_syscall_stats) {
update_syscall_stats(ctx, cgroup_id, id);
}
// Continue to tail calls.
bpf_tail_call(ctx, &sys_enter_init_tail, id);
return 0;
}
// initial tail call entry from sys_enter.
// purpose is to save the syscall info of relevant syscalls through the task_info map.
// can move to one of:
// 1. sys_enter_submit, general event submit logic from sys_enter
// 2. directly to syscall tail handler in sys_enter_tails
SEC("raw_tracepoint/sys_enter_init")
int sys_enter_init(struct bpf_raw_tracepoint_args *ctx)
{
struct task_struct *task = (struct task_struct *) bpf_get_current_task();
u64 pid_tgid = bpf_get_current_pid_tgid();
u32 tid = pid_tgid;
task_info_t *task_info = bpf_map_lookup_elem(&task_info_map, &tid);
if (unlikely(task_info == NULL)) {
task_info = init_task_info(tid, 0);
if (unlikely(task_info == NULL)) {
return 0;
}
init_task_context(&task_info->context, task);
}
syscall_data_t *sys = &(task_info->syscall_data);
sys->id = ctx->args[1];
if (LINUX_HAS_SYSCALL_WRAPPER) {
struct pt_regs *regs = (struct pt_regs *) ctx->args[0];
if (is_x86_compat(task)) {
#if defined(bpf_target_x86)
sys->args.args[0] = BPF_CORE_READ(regs, bx);
sys->args.args[1] = BPF_CORE_READ(regs, cx);
sys->args.args[2] = BPF_CORE_READ(regs, dx);
sys->args.args[3] = BPF_CORE_READ(regs, si);
sys->args.args[4] = BPF_CORE_READ(regs, di);
sys->args.args[5] = BPF_CORE_READ(regs, bp);
#endif // bpf_target_x86
} else {
sys->args.args[0] = PT_REGS_PARM1_CORE_SYSCALL(regs);
sys->args.args[1] = PT_REGS_PARM2_CORE_SYSCALL(regs);
sys->args.args[2] = PT_REGS_PARM3_CORE_SYSCALL(regs);
sys->args.args[3] = PT_REGS_PARM4_CORE_SYSCALL(regs);
sys->args.args[4] = PT_REGS_PARM5_CORE_SYSCALL(regs);
sys->args.args[5] = PT_REGS_PARM6_CORE_SYSCALL(regs);
}
} else {
bpf_probe_read(sys->args.args, sizeof(6 * sizeof(u64)), (void *) ctx->args);
}
if (is_compat(task)) {
// Translate 32bit syscalls to 64bit syscalls, so we can send to the correct handler
u32 *id_64 = bpf_map_lookup_elem(&sys_32_to_64_map, &sys->id);
if (id_64 == 0)
return 0;
sys->id = *id_64;
}
// exit, exit_group and rt_sigreturn syscalls don't return
if (sys->id != SYSCALL_EXIT && sys->id != SYSCALL_EXIT_GROUP &&
sys->id != SYSCALL_RT_SIGRETURN) {
sys->ts = bpf_ktime_get_ns();
task_info->syscall_traced = true;
}
// if id is irrelevant continue to next tail call
bpf_tail_call(ctx, &sys_enter_submit_tail, sys->id);
// call syscall handler, if exists
bpf_tail_call(ctx, &sys_enter_tails, sys->id);
return 0;
}
// submit tail call part of sys_enter.
// events that are required for submission go through two logics here:
// 1. parsing their FD filepath if requested as an option
// 2. submitting the event if relevant
// may move to the direct syscall handler in sys_enter_tails
SEC("raw_tracepoint/sys_enter_submit")
int sys_enter_submit(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_program_data(&p, ctx))
return 0;
if (!should_trace(&p))
return 0;
syscall_data_t *sys = &p.task_info->syscall_data;
if (sys->id != SYSCALL_RT_SIGRETURN && !p.task_info->syscall_traced) {
save_to_submit_buf(&p.event->args_buf, (void *) &(sys->args.args[0]), sizeof(int), 0);
events_perf_submit(&p, sys->id, 0);
}
// call syscall handler, if exists
bpf_tail_call(ctx, &sys_enter_tails, sys->id);
return 0;
}
// trace/events/syscalls.h: TP_PROTO(struct pt_regs *regs, long ret)
// initial entry for sys_exit syscall logic
SEC("raw_tracepoint/sys_exit")
int tracepoint__raw_syscalls__sys_exit(struct bpf_raw_tracepoint_args *ctx)
{
struct pt_regs *regs = (struct pt_regs *) ctx->args[0];
int id = get_syscall_id_from_regs(regs);
struct task_struct *task = (struct task_struct *) bpf_get_current_task();
if (is_compat(task)) {
// Translate 32bit syscalls to 64bit syscalls, so we can send to the correct handler
u32 *id_64 = bpf_map_lookup_elem(&sys_32_to_64_map, &id);
if (id_64 == 0)
return 0;
id = *id_64;
}
// Skip if cgroup is muted.
u64 cgroup_id = 0;
if (global_config.cgroup_v1) {
cgroup_id = get_cgroup_v1_subsys0_id(task);
} else {
cgroup_id = bpf_get_current_cgroup_id();
}
if (bpf_map_lookup_elem(&ignored_cgroups_map, &cgroup_id) != NULL) {
return 0;
}
bpf_tail_call(ctx, &sys_exit_init_tail, id);
return 0;
}
// initial tail call entry from sys_exit.
// purpose is to "confirm" the syscall data saved by marking it as complete(see
// task_info->syscall_traced) and adding the return value to the syscall_info struct. can move to
// one of:
// 1. sys_exit, general event submit logic from sys_exit
// 2. directly to syscall tail hanler in sys_exit_tails
SEC("raw_tracepoint/sys_exit_init")
int sys_exit_init(struct bpf_raw_tracepoint_args *ctx)
{
struct task_struct *task = (struct task_struct *) bpf_get_current_task();
u64 pid_tgid = bpf_get_current_pid_tgid();
u32 tid = pid_tgid;
task_info_t *task_info = bpf_map_lookup_elem(&task_info_map, &tid);
if (unlikely(task_info == NULL)) {
task_info = init_task_info(tid, 0);
if (unlikely(task_info == NULL))
return 0;
init_task_context(&task_info->context, task);
}
// check if syscall is being traced and mark that it finished
if (!task_info->syscall_traced)
return 0;
task_info->syscall_traced = false;
syscall_data_t *sys = &task_info->syscall_data;
long ret = ctx->args[1];
struct pt_regs *regs = (struct pt_regs *) ctx->args[0];
int id = get_syscall_id_from_regs(regs);
if (is_compat(task)) {
// Translate 32bit syscalls to 64bit syscalls, so we can send to the correct handler
u32 *id_64 = bpf_map_lookup_elem(&sys_32_to_64_map, &id);
if (id_64 == 0)
return 0;
id = *id_64;
}
// Sanity check - we returned from the expected syscall this task was executing
if (sys->id != id)
return 0;
sys->ret = ret;
// move to submit tail call if needed
bpf_tail_call(ctx, &sys_exit_submit_tail, id);
// otherwise move to direct syscall handler
bpf_tail_call(ctx, &sys_exit_tails, id);
return 0;
}
// submit tail call part of sys_exit.
// most syscall events are submitted at this point, and if not,
// they are submitted through direct syscall handlers in sys_exit_tails
SEC("raw_tracepoint/sys_exit_submit")
int sys_exit_submit(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_program_data(&p, ctx))
return 0;
if (!should_trace(&p))
return 0;
syscall_data_t *sys = &p.task_info->syscall_data;
long ret = ctx->args[1];
if (!should_submit(sys->id, p.event))
goto out;
// We can't use saved args after execve syscall, as pointers are invalid.
// To avoid showing execve event both on entry and exit, we only output failed execs.
if ((sys->id == SYSCALL_EXECVE || sys->id == SYSCALL_EXECVEAT) && (ret == 0))
goto out;
save_args_to_submit_buf(p.event, &sys->args);
p.event->context.ts = sys->ts;
events_perf_submit(&p, sys->id, ret);
out:
bpf_tail_call(ctx, &sys_exit_tails, sys->id);
return 0;
}
// here are the direct hook points for sys_enter and sys_exit.
// There are used not for submitting syscall events but the enter and exit events themselves.
// As such they are usually not attached, and will only be used if sys_enter or sys_exit events are
// given as tracing arguments.
// separate hook point for sys_enter event tracing
SEC("raw_tracepoint/trace_sys_enter")
int trace_sys_enter(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_program_data(&p, ctx))
return 0;
if (!should_trace(&p))
return 0;
if (!should_submit(RAW_SYS_ENTER, p.event))
return 0;
// always submit since this won't be attached otherwise
int id = ctx->args[1];
struct task_struct *task = (struct task_struct *) bpf_get_current_task();
if (is_compat(task)) {
// Translate 32bit syscalls to 64bit syscalls, so we can send to the correct handler
u32 *id_64 = bpf_map_lookup_elem(&sys_32_to_64_map, &id);
if (id_64 == 0)
return 0;
id = *id_64;
}
save_to_submit_buf(&p.event->args_buf, (void *) &id, sizeof(int), 0);
events_perf_submit(&p, RAW_SYS_ENTER, 0);
return 0;
}
// separate hook point for sys_exit event tracing
SEC("raw_tracepoint/trace_sys_exit")
int trace_sys_exit(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_program_data(&p, ctx))
return 0;
if (!should_trace(&p))
return 0;
if (!should_submit(RAW_SYS_EXIT, p.event))
return 0;
// always submit since this won't be attached otherwise
struct pt_regs *regs = (struct pt_regs *) ctx->args[0];
int id = get_syscall_id_from_regs(regs);
struct task_struct *task = (struct task_struct *) bpf_get_current_task();
if (is_compat(task)) {
// Translate 32bit syscalls to 64bit syscalls, so we can send to the correct handler
u32 *id_64 = bpf_map_lookup_elem(&sys_32_to_64_map, &id);
if (id_64 == 0)
return 0;
id = *id_64;
}
save_to_submit_buf(&p.event->args_buf, (void *) &id, sizeof(int), 0);
events_perf_submit(&p, RAW_SYS_EXIT, 0);
return 0;
}
SEC("raw_tracepoint/sys_execve")
int syscall__execve(void *ctx)
{
program_data_t p = {};
if (!init_tailcall_program_data(&p, ctx))
return 0;
if (!p.task_info->syscall_traced)
return -1;
syscall_data_t *sys = &p.task_info->syscall_data;
p.event->context.ts = sys->ts;
if (!should_submit(SYSCALL_EXECVE, p.event))
return 0;
reset_event_args(&p);
save_str_to_buf(&p.event->args_buf, (void *) sys->args.args[0] /*filename*/, 0);
save_str_arr_to_buf(&p.event->args_buf, (const char *const *) sys->args.args[1] /*argv*/, 1);
return events_perf_submit(&p, SYSCALL_EXECVE, 0);
}
SEC("raw_tracepoint/sys_execveat")
int syscall__execveat(void *ctx)
{
program_data_t p = {};
if (!init_tailcall_program_data(&p, ctx))
return 0;
if (!p.task_info->syscall_traced)
return -1;
syscall_data_t *sys = &p.task_info->syscall_data;
p.event->context.ts = sys->ts;
if (!should_submit(SYSCALL_EXECVEAT, p.event))
return 0;
reset_event_args(&p);
save_to_submit_buf(&p.event->args_buf, (void *) &sys->args.args[0] /*dirfd*/, sizeof(int), 0);
save_str_to_buf(&p.event->args_buf, (void *) sys->args.args[1] /*pathname*/, 1);
save_str_arr_to_buf(&p.event->args_buf, (const char *const *) sys->args.args[2] /*argv*/, 2);
save_to_submit_buf(&p.event->args_buf, (void *) &sys->args.args[4] /*flags*/, sizeof(int), 4);
return events_perf_submit(&p, SYSCALL_EXECVEAT, 0);
}
statfunc int send_stdio_via_socket_from_socket_dup(program_data_t *p, u64 oldfd, u64 newfd)
{
if (!should_submit(STDIO_VIA_SOCKET, p->event)) {
return 0;
}
if (!check_fd_type(oldfd, S_IFSOCK)) {
return 0;
}
struct file *f = get_struct_file_from_fd(oldfd);
if (f == NULL) {
return -1;
}
// get the address
struct socket *socket_from_file = (struct socket *) BPF_CORE_READ(f, private_data);
if (socket_from_file == NULL) {
return -1;
}
struct sock *sk = get_socket_sock(socket_from_file);
u16 family = get_sock_family(sk);
if ((family != AF_INET) && (family != AF_INET6) && (family != AF_UNIX)) {
return 0;
}
if (!is_stdio_via_socket(newfd, family)) {
return 0;
}
reset_event_args(p);
save_to_submit_buf(&(p->event->args_buf), &newfd, sizeof(u32), 0);
if (family == AF_INET) {
net_conn_v4_t net_details = {};
struct sockaddr_in remote;
get_network_details_from_sock_v4(sk, &net_details, 0);
get_remote_sockaddr_in_from_network_details(&remote, &net_details, family);
save_to_submit_buf(&(p->event->args_buf), &remote, sizeof(struct sockaddr_in), 1);
} else if (family == AF_INET6) {
net_conn_v6_t net_details = {};
struct sockaddr_in6 remote;
get_network_details_from_sock_v6(sk, &net_details, 0);
get_remote_sockaddr_in6_from_network_details(&remote, &net_details, family);
save_to_submit_buf(&(p->event->args_buf), &remote, sizeof(struct sockaddr_in6), 1);
}
return events_perf_submit(p, STDIO_VIA_SOCKET, 0);
}
SEC("raw_tracepoint/sys_dup")
int sys_dup_exit_tail(void *ctx)
{
program_data_t p = {};
if (!init_tailcall_program_data(&p, ctx))
return 0;
if (!should_trace(&p))
return 0;
syscall_data_t *sys = &p.task_info->syscall_data;
if (sys->ret < 0) {
// dup failed
return 0;
}
if (sys->id == SYSCALL_DUP) {
// args.args[0]: oldfd
// retval: newfd
send_stdio_via_socket_from_socket_dup(&p, sys->args.args[0], sys->ret);
} else if (sys->id == SYSCALL_DUP2 || sys->id == SYSCALL_DUP3) {
// args.args[0]: oldfd
// args.args[1]: newfd
// retval: retval
send_stdio_via_socket_from_socket_dup(&p, sys->args.args[0], sys->args.args[1]);
}
return 0;
}
// trace/events/sched.h: TP_PROTO(struct task_struct *parent, struct task_struct *child)
//
// NOTE: sched_process_fork is called by kernel_clone(), which is executed during
// clone() calls as well, not only fork(). This means that sched_process_fork()
// is also able to pick the creation of LWPs through clone().
SEC("raw_tracepoint/sched_process_fork")
int tracepoint__sched__sched_process_fork(struct bpf_raw_tracepoint_args *ctx)
{
long ret = 0;
program_data_t p = {};
if (!init_program_data(&p, ctx))
return 0;
// NOTE: proc_info_map updates before should_trace() as the entries are needed in other places.
struct task_struct *parent = (struct task_struct *) ctx->args[0];
struct task_struct *child = (struct task_struct *) ctx->args[1];
// Information needed before the event:
int parent_pid = get_task_host_tgid(parent);
u64 child_start_time = get_task_start_time(child);
int child_pid = get_task_host_tgid(child);
int child_tid = get_task_host_pid(child);
int child_ns_pid = get_task_ns_tgid(child);
int child_ns_tid = get_task_ns_pid(child);
// Update the task_info map with the new task's info
ret = bpf_map_update_elem(&task_info_map, &child_tid, p.task_info, BPF_ANY);
if (ret < 0)
tracee_log(ctx, BPF_LOG_LVL_DEBUG, BPF_LOG_ID_MAP_UPDATE_ELEM, ret);
task_info_t *task = bpf_map_lookup_elem(&task_info_map, &child_tid);
if (unlikely(task == NULL)) {
// this should never happen - we just updated the map with this key
tracee_log(ctx, BPF_LOG_LVL_WARN, BPF_LOG_ID_MAP_LOOKUP_ELEM, 0);
return 0;
}
task->context.tid = child_ns_tid;
task->context.host_tid = child_tid;
task->context.start_time = child_start_time;
// Update the proc_info_map with the new process's info (from parent)
proc_info_t *c_proc_info = bpf_map_lookup_elem(&proc_info_map, &child_pid);
if (c_proc_info == NULL) {
// It is a new process (not another thread): add it to proc_info_map.
proc_info_t *p_proc_info = bpf_map_lookup_elem(&proc_info_map, &parent_pid);
if (unlikely(p_proc_info == NULL)) {
// parent should exist in proc_info_map (init_program_data sets it)
tracee_log(ctx, BPF_LOG_LVL_WARN, BPF_LOG_ID_MAP_LOOKUP_ELEM, 0);
return 0;
}
// Copy the parent's proc_info to the child's entry.
bpf_map_update_elem(&proc_info_map, &child_pid, p_proc_info, BPF_NOEXIST);
c_proc_info = bpf_map_lookup_elem(&proc_info_map, &child_pid);
if (unlikely(c_proc_info == NULL)) {
tracee_log(ctx, BPF_LOG_LVL_WARN, BPF_LOG_ID_MAP_LOOKUP_ELEM, 0);
return 0;
}
c_proc_info->new_proc = true; // started after tracee (new_pid filter)
}
if (!should_trace(&p))
return 0;
// Submit the event
if (should_submit(SCHED_PROCESS_FORK, p.event)) {
// Parent information.
u64 parent_start_time = get_task_start_time(parent);
int parent_tid = get_task_host_pid(parent);
int parent_ns_pid = get_task_ns_tgid(parent);
int parent_ns_tid = get_task_ns_pid(parent);
// Parent (might be a thread or a process).
save_to_submit_buf(&p.event->args_buf, (void *) &parent_tid, sizeof(int), 0);
save_to_submit_buf(&p.event->args_buf, (void *) &parent_ns_tid, sizeof(int), 1);
save_to_submit_buf(&p.event->args_buf, (void *) &parent_pid, sizeof(int), 2);
save_to_submit_buf(&p.event->args_buf, (void *) &parent_ns_pid, sizeof(int), 3);
save_to_submit_buf(&p.event->args_buf, (void *) &parent_start_time, sizeof(u64), 4);
// Child (might be a lwp or a process, sched_process_fork trace is calle by clone() also).
save_to_submit_buf(&p.event->args_buf, (void *) &child_tid, sizeof(int), 5);
save_to_submit_buf(&p.event->args_buf, (void *) &child_ns_tid, sizeof(int), 6);
save_to_submit_buf(&p.event->args_buf, (void *) &child_pid, sizeof(int), 7);
save_to_submit_buf(&p.event->args_buf, (void *) &child_ns_pid, sizeof(int), 8);
save_to_submit_buf(&p.event->args_buf, (void *) &child_start_time, sizeof(u64), 9);
// Both, the thread group leader and the "up_parent" (the first process, not lwp, found
// as a parent of the child in the hierarchy), are needed by the userland process tree.
// The userland process tree default source of events is the signal events, but there is
// an option to use regular event for maintaining it as well (and it is needed for some
// situatins). These arguments will always be removed by userland event processors.
struct task_struct *leader = get_leader_task(child);
struct task_struct *up_parent = get_leader_task(get_parent_task(leader));
// Up Parent information: Go up in hierarchy until parent is process.
u64 up_parent_start_time = get_task_start_time(up_parent);
int up_parent_pid = get_task_host_tgid(up_parent);
int up_parent_tid = get_task_host_pid(up_parent);
int up_parent_ns_pid = get_task_ns_tgid(up_parent);
int up_parent_ns_tid = get_task_ns_pid(up_parent);
// Leader information.
u64 leader_start_time = get_task_start_time(leader);
int leader_pid = get_task_host_tgid(leader);
int leader_tid = get_task_host_pid(leader);
int leader_ns_pid = get_task_ns_tgid(leader);
int leader_ns_tid = get_task_ns_pid(leader);
// Up Parent: always a process (might be the same as Parent if parent is a process).
save_to_submit_buf(&p.event->args_buf, (void *) &up_parent_tid, sizeof(int), 10);
save_to_submit_buf(&p.event->args_buf, (void *) &up_parent_ns_tid, sizeof(int), 11);
save_to_submit_buf(&p.event->args_buf, (void *) &up_parent_pid, sizeof(int), 12);
save_to_submit_buf(&p.event->args_buf, (void *) &up_parent_ns_pid, sizeof(int), 13);
save_to_submit_buf(&p.event->args_buf, (void *) &up_parent_start_time, sizeof(u64), 14);
// Leader: always a process (might be the same as the Child if child is a process).
save_to_submit_buf(&p.event->args_buf, (void *) &leader_tid, sizeof(int), 15);
save_to_submit_buf(&p.event->args_buf, (void *) &leader_ns_tid, sizeof(int), 16);
save_to_submit_buf(&p.event->args_buf, (void *) &leader_pid, sizeof(int), 17);
save_to_submit_buf(&p.event->args_buf, (void *) &leader_ns_pid, sizeof(int), 18);
save_to_submit_buf(&p.event->args_buf, (void *) &leader_start_time, sizeof(u64), 19);
// Submit
signal_events_perf_submit(&p, SCHED_PROCESS_FORK, 0);
}
return 0;
}
SEC("kprobe/exec_binprm")
int BPF_KPROBE(trace_exec_binprm)
{
args_t args = {};
args.args[0] = PT_REGS_PARM1(ctx);
args.args[1] = PT_REGS_PARM2(ctx);
args.args[2] = PT_REGS_PARM3(ctx);
args.args[3] = PT_REGS_PARM4(ctx);
args.args[4] = PT_REGS_PARM5(ctx);
args.args[5] = PT_REGS_PARM6(ctx);
// requried by kretprobe for this function
save_args(&args, EXEC_BINPRM);
// NOTE: we cannot do the calc based on the value we store in the args map, as the binprm struct
// gets modified during the function execution and before it hits the tracepoint.
u32 pid = bpf_get_current_pid_tgid() >> 32;
struct linux_binprm *bprm = (void *) PT_REGS_PARM1(ctx);
struct file *file = get_file_ptr_from_bprm(bprm);
struct path f_path = (struct path) BPF_CORE_READ(file, f_path);
struct dentry *dentry = f_path.dentry;
struct inode *inode = BPF_CORE_READ(file, f_inode);
struct super_block *sb = BPF_CORE_READ(inode, i_sb);
u16 flags = 0;
if (sb && inode) {
if (get_exe_upper_layer(dentry, sb)) {
flags |= FS_EXE_UPPER_LAYER;
}
if (is_executed_in_tmpfs(sb)) {
flags |= FS_EXE_FROM_TMPFS;
}
if (get_exe_from_memfd(file)) {
flags |= FS_EXE_FROM_MEMFD;
}
}
bpf_map_update_elem(&pid_original_file_flags, &pid, &flags, BPF_ANY);
return 0;
}
// trace/events/sched.h: TP_PROTO(struct task_struct *p, pid_t old_pid, struct linux_binprm *bprm)
SEC("raw_tracepoint/sched_process_exec")
int tracepoint__sched__sched_process_exec(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_program_data(&p, ctx)) {
return 0;
}
struct linux_binprm *bprm = (struct linux_binprm *) ctx->args[2];
if (bprm == NULL) {
return -1;
}
struct file *file = get_file_ptr_from_bprm(bprm);
void *file_path = get_path_str(__builtin_preserve_access_index(&file->f_path));
proc_info_t *proc_info = p.proc_info;
proc_info->new_proc = true; // task has started after tracee started running
// extract the binary name to be used in should_trace
__builtin_memset(proc_info->binary.path, 0, MAX_BIN_PATH_SIZE);
bpf_probe_read_kernel_str(proc_info->binary.path, MAX_BIN_PATH_SIZE, file_path);
proc_info->binary.mnt_id = p.event->context.task.mnt_id;
if (!should_trace(&p)) {
return 0;
}
if (!should_submit(SCHED_PROCESS_EXEC, p.event)) {
return 0;
}
// Note: From v5.9+, there are two interesting fields in bprm that could be added:
// 1. struct file *executable: the executable name passed to an interpreter
// 2. fdpath: generated filename for execveat (after resolving dirfd)
const char *filename = get_binprm_filename(bprm);
dev_t s_dev = get_dev_from_file(file);
struct inode *inode = BPF_CORE_READ(file, f_inode);
unsigned long inode_nr = BPF_CORE_READ(inode, i_ino);
u64 ctime = get_ctime_nanosec_from_file(file);
umode_t inode_mode = get_inode_mode_from_file(file);
save_str_to_buf(&p.event->args_buf, (void *) filename, 0);
save_str_to_buf(&p.event->args_buf, file_path, 1);
save_to_submit_buf(&p.event->args_buf, &s_dev, sizeof(dev_t), 2);
save_to_submit_buf(&p.event->args_buf, &inode_nr, sizeof(unsigned long), 3);
save_to_submit_buf(&p.event->args_buf, &ctime, sizeof(u64), 4);
save_to_submit_buf(&p.event->args_buf, &inode_mode, sizeof(umode_t), 5);
// NOTES:
// - interp is the real interpreter (sh, bash, python, perl, ...)
// - interpreter is the binary interpreter (ld.so), also known as the loader
// - interpreter might be the same as executable (so there is no interpreter)
// Check if there is an interpreter and if it is different from the executable:
bool itp_inode_exists = proc_info->interpreter.id.inode != 0;
bool itp_dev_diff = proc_info->interpreter.id.device != s_dev;
bool itp_inode_diff = proc_info->interpreter.id.inode != inode_nr;
if (itp_inode_exists && (itp_dev_diff || itp_inode_diff)) {
save_str_to_buf(
&p.event->args_buf, &proc_info->interpreter.pathname, 6); // interpreter path
save_to_submit_buf(&p.event->args_buf,
&proc_info->interpreter.id.device,
sizeof(dev_t),
7); // interpreter device number
save_to_submit_buf(&p.event->args_buf,
&proc_info->interpreter.id.inode,
sizeof(u64),
8); // interpreter inode number
save_to_submit_buf(&p.event->args_buf,
&proc_info->interpreter.id.ctime,
sizeof(u64),
9); // interpreter changed time
}
struct path f_path = (struct path) BPF_CORE_READ(file, f_path);
struct dentry *dentry = f_path.dentry;
struct super_block *sb = BPF_CORE_READ(inode, i_sb);
u32 flags = 0;
if (sb && inode) {
if (get_exe_upper_layer(dentry, sb)) {
flags |= FS_EXE_UPPER_LAYER;
}
if (is_executed_in_tmpfs(sb)) {
flags |= FS_EXE_FROM_TMPFS;
}
if (get_exe_from_memfd(file)) {
flags |= FS_EXE_FROM_MEMFD;
}
}
// If there is a dummy element in the map, we know the binary was dropped.
if (bpf_map_lookup_elem(&dropped_binary_inodes, &inode_nr)) {
flags |= FS_EXE_DROPPED_BINARY;
}
pid_t pid = p.event->context.task.host_pid;
u16 *original_flags = bpf_map_lookup_elem(&pid_original_file_flags, &pid);
if (original_flags != NULL) {
u32 upper_flags = *original_flags;
upper_flags = upper_flags << 16;
flags |= upper_flags;
bpf_map_delete_elem(&pid_original_file_flags, &pid);
}
save_to_submit_buf(&p.event->args_buf, &flags, sizeof(flags), 15);
bpf_tail_call(ctx, &prog_array_tp, TAIL_SCHED_PROCESS_EXEC_EVENT_SUBMIT);
return 0;
}
SEC("raw_tracepoint/sched_process_exec_event_submit_tail")
int sched_process_exec_event_submit_tail(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_tailcall_program_data(&p, ctx))
return -1;
struct task_struct *task = (struct task_struct *) ctx->args[0];
struct linux_binprm *bprm = (struct linux_binprm *) ctx->args[2];
if (bprm == NULL)
return -1;
// bprm->mm is null at this point (set by begin_new_exec()), and task->mm is already initialized
struct mm_struct *mm = get_mm_from_task(task);
unsigned long arg_start, arg_end;
arg_start = get_arg_start_from_mm(mm);
arg_end = get_arg_end_from_mm(mm);
int argc = get_argc_from_bprm(bprm);
struct file *stdin_file = get_struct_file_from_fd(0);
unsigned short stdin_type = get_inode_mode_from_file(stdin_file) & S_IFMT;
void *stdin_path = get_path_str(__builtin_preserve_access_index(&stdin_file->f_path));
const char *interp = get_binprm_interp(bprm);
int invoked_from_kernel = 0;
if (get_task_parent_flags(task) & PF_KTHREAD) {
invoked_from_kernel = 1;
}
save_args_str_arr_to_buf(&p.event->args_buf, (void *) arg_start, (void *) arg_end, argc, 10);
save_str_to_buf(&p.event->args_buf, (void *) interp, 11);
save_to_submit_buf(&p.event->args_buf, &stdin_type, sizeof(unsigned short), 12);
save_str_to_buf(&p.event->args_buf, stdin_path, 13);
save_to_submit_buf(&p.event->args_buf, &invoked_from_kernel, sizeof(int), 14);
signal_events_perf_submit(&p, SCHED_PROCESS_EXEC, 0);
return 0;
}
// trace/events/sched.h: TP_PROTO(struct task_struct *p)
SEC("raw_tracepoint/sched_process_exit")
int tracepoint__sched__sched_process_exit(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_program_data(&p, ctx))
return 0;
// evaluate should_trace before removing this pid from the maps
bool traced = !!should_trace(&p);
bpf_map_delete_elem(&task_info_map, &p.event->context.task.host_tid);
bool group_dead = false;
struct task_struct *task = p.task;
struct signal_struct *signal = BPF_CORE_READ(task, signal);
atomic_t live = BPF_CORE_READ(signal, live);
// This check could be true for multiple thread exits if the thread count was 0 when the hooks
// were triggered. This could happen for example if the threads performed exit in different CPUs
// simultaneously.
if (live.counter == 0) {
group_dead = true;
}
bool oom_killed = false;
if (bpf_map_lookup_elem(&oom_info, &p.task_info->context.host_pid)) {
oom_killed = true;
bpf_map_delete_elem(&oom_info, &p.task_info->context.host_pid);
}
if (!traced)
return 0;
long exit_code = get_task_exit_code(p.task);
if (oom_killed) {
if (should_submit(PROCESS_OOM_KILLED, p.event)) {
save_to_submit_buf(&p.event->args_buf, (void *) &exit_code, sizeof(long), 0);
save_to_submit_buf(&p.event->args_buf, (void *) &group_dead, sizeof(bool), 1);
signal_events_perf_submit(&p, PROCESS_OOM_KILLED, 0);
}
return 0;
}
if (should_submit(SCHED_PROCESS_EXIT, p.event)) {
save_to_submit_buf(&p.event->args_buf, (void *) &exit_code, sizeof(long), 0);
save_to_submit_buf(&p.event->args_buf, (void *) &group_dead, sizeof(bool), 1);
signal_events_perf_submit(&p, SCHED_PROCESS_EXIT, 0);
}
return 0;
}
// trace/events/sched.h: TP_PROTO(struct task_struct *p)
SEC("raw_tracepoint/sched_process_free")
int tracepoint__sched__sched_process_free(struct bpf_raw_tracepoint_args *ctx)
{
struct task_struct *task = (struct task_struct *) ctx->args[0];
int pid = get_task_host_pid(task);
int tgid = get_task_host_tgid(task);
if (pid == tgid) {
// we only care about process (and not thread) exit
// if tgid task is freed, we know for sure that the process exited
// so we can safely remove it from the process map
bpf_map_delete_elem(&proc_info_map, &tgid);
}
return 0;
}
// trace/events/sched.h: TP_PROTO(bool preempt, struct task_struct *prev, struct task_struct *next)
SEC("raw_tracepoint/sched_switch")
int tracepoint__sched__sched_switch(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_program_data(&p, ctx))
return 0;
if (!should_trace(&p))
return 0;
if (!should_submit(SCHED_SWITCH, p.event))
return 0;
struct task_struct *prev = (struct task_struct *) ctx->args[1];
struct task_struct *next = (struct task_struct *) ctx->args[2];
int prev_pid = get_task_host_pid(prev);
int next_pid = get_task_host_pid(next);
int cpu = bpf_get_smp_processor_id();
save_to_submit_buf(&p.event->args_buf, (void *) &cpu, sizeof(int), 0);
save_to_submit_buf(&p.event->args_buf, (void *) &prev_pid, sizeof(int), 1);
save_str_to_buf(&p.event->args_buf, prev->comm, 2);
save_to_submit_buf(&p.event->args_buf, (void *) &next_pid, sizeof(int), 3);
save_str_to_buf(&p.event->args_buf, next->comm, 4);
return events_perf_submit(&p, SCHED_SWITCH, 0);
}
statfunc struct trace_kprobe *get_trace_kprobe_from_trace_probe(void *tracep)
{
struct trace_kprobe *tracekp =
(struct trace_kprobe *) container_of(tracep, struct trace_kprobe, tp);
return tracekp;
}
statfunc struct trace_uprobe *get_trace_uprobe_from_trace_probe(void *tracep)
{
struct trace_uprobe *traceup =
(struct trace_uprobe *) container_of(tracep, struct trace_uprobe, tp);
return traceup;
}
// This function returns a pointer to struct trace_probe from struct trace_event_call.
statfunc void *get_trace_probe_from_trace_event_call(struct trace_event_call *call)
{
void *tracep_ptr;
struct trace_probe___v53 *legacy_tracep;
if (bpf_core_field_exists(legacy_tracep->call)) {
tracep_ptr = container_of(call, struct trace_probe___v53, call);
} else {
struct trace_probe_event *tpe = container_of(call, struct trace_probe_event, call);
struct list_head probes = BPF_CORE_READ(tpe, probes);
tracep_ptr = container_of(probes.next, struct trace_probe, list);
}
return tracep_ptr;
}
// trace/events/cgroup.h: TP_PROTO(struct cgroup *cgrp, const char *path)
SEC("raw_tracepoint/cgroup_mkdir")
int tracepoint__cgroup__cgroup_mkdir(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_program_data(&p, ctx))
return 0;
if (!should_trace(&p))
return 0;
if (!should_submit(CGROUP_MKDIR, p.event))
return 0;
struct cgroup *dst_cgrp = (struct cgroup *) ctx->args[0];
char *path = (char *) ctx->args[1];
u32 hierarchy_id = get_cgroup_hierarchy_id(dst_cgrp);
u64 cgroup_id = get_cgroup_id(dst_cgrp);
u32 cgroup_id_lsb = cgroup_id;
save_to_submit_buf(&p.event->args_buf, &cgroup_id, sizeof(u64), 0);
save_str_to_buf(&p.event->args_buf, path, 1);
save_to_submit_buf(&p.event->args_buf, &hierarchy_id, sizeof(u32), 2);
signal_events_perf_submit(&p, CGROUP_MKDIR, 0);
return 0;
}
// trace/events/cgroup.h: TP_PROTO(struct cgroup *cgrp, const char *path)
SEC("raw_tracepoint/cgroup_rmdir")
int tracepoint__cgroup__cgroup_rmdir(struct bpf_raw_tracepoint_args *ctx)
{
program_data_t p = {};
if (!init_program_data(&p, ctx))
return 0;