-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathsocket.c
More file actions
4223 lines (3712 loc) · 125 KB
/
Copy pathsocket.c
File metadata and controls
4223 lines (3712 loc) · 125 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
/*
* Copyright (c) 2022 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include <ctype.h>
#include <arpa/inet.h>
#include <sched.h>
#include <sys/prctl.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/stat.h>
#include <limits.h>
#include <bcc/perf_reader.h>
#include <linux/version.h>
#include "clib.h"
#include "config.h"
#include "symbol.h"
#include "proc.h"
#include "tracer.h"
#include "probe.h"
#include "table.h"
#include "utils.h"
#include "socket.h"
#include "log.h"
#include "go_tracer.h"
#include "ssl_tracer.h"
#include "unwind_tracer.h"
#include "load.h"
#include "btf_core.h"
#include "config.h"
#include "perf_reader.h"
#include "common_utils.h"
#include "extended/extended.h"
#include "trace_utils.h"
#include "socket_trace_bpf_common.c"
#include "socket_trace_bpf_3_10_0.c"
#include "socket_trace_bpf_5_2_plus.c"
#include "socket_trace_bpf_kylin.c"
#include "socket_trace_bpf_kfunc.c"
#include "socket_trace_bpf_rt.c"
#include "socket_trace_bpf_kprobe.c"
static enum linux_kernel_type g_k_type;
static bool use_kfunc_bin; // Whether to use fentry/fexit binary eBPF bytecode
static struct list_head events_list; // Use for extra register events
static pthread_t proc_events_pthread; // Process exec/exit thread
static bool kprobe_feature_disable; // Whether to disable the kprobe feature?
static bool unix_socket_feature_enable; // Whether to enable the kprobe feature?
static bool virtual_file_collect_enable; // Whether to enable virtual file collect?
/*
* Control whether to disable the tracing feature.
* 'true' disables the tracing feature, and 'false' enables it.
* The default is 'false'.
*/
static bool g_disable_syscall_tracing;
/*
* tracer_hooks_detach() and tracer_hooks_attach() will become terrible
* when the number of probes is very large. Because we have to spend a
* long time waiting for it to complete, this is not a good way, we hope
* that calling socket_tracer_stop() or socket_tracer_start() will not
* block the execution of subsequent tasks.
*
* In order to solve this problem, we use a global variable(`probes_act`)
* to hold the latest attach/detach behavior, it be executed later by
* another thread, so that the current thread will not be blocked.
*/
static volatile uint64_t probes_act;
extern u64 thread_index_max;
extern __thread uword thread_index; // for symbol pid caches hash
extern int sys_cpus_count;
extern bool *cpu_online;
extern uint32_t attach_failed_count;
static int infer_socktrace_fd;
static uint32_t conf_max_socket_entries;
static uint32_t conf_max_trace_entries;
/*
* The datadump related Settings
*/
static bool datadump_use_remote;
static debug_callback_t datadump_cb;
static bool datadump_enable;
static int datadump_pid; // If the value is 0, process-ID/thread-ID filtering is not performed.
static uint32_t datadump_start_time;
/*
* The sequence number of the socket data is used to label the
* dumped data for ordering purposes.
*/
static uint64_t datadump_seq;
static uint32_t datadump_timeout;
static char datadump_comm[16]; // If null, process or thread name filtering is not performed.
static uint8_t datadump_proto;
static uint16_t datadump_port;
static char datadump_ipaddr[ADDRSTRLEN];
static char datadump_file_path[DATADUMP_FILE_PATH_SIZE];
static FILE *datadump_file;
static pthread_mutex_t datadump_mutex;
/*
* The maximum amount of data passed to the agent by eBPF programe.
* Set by set_data_limit_max()
*/
static uint32_t socket_data_limit_max;
static uint32_t ai_agent_data_limit_max;
static uint32_t go_tracing_timeout = GO_TRACING_TIMEOUT_DEFAULT;
// 0: disable 1: during request 2: all
static uint32_t io_event_collect_mode = 1;
static uint64_t io_event_minimal_duration = 1000000;
static uint64_t hooked_socket_syscalls_bitmap = HOOKED_SOCKET_SYSCALL_ALL;
/*
* The maximum threshold for socket map reclamation, with map
* reclamation occurring if this value is exceeded.
*/
static uint32_t conf_socket_map_max_reclaim;
struct bpf_tracer *g_tracer;
bpf_offset_param_t g_kern_offsets;
/*
* The table for L7 protocol filtering ports.
*/
ports_bitmap_t *ports_bitmap[PROTO_NUM];
extern uint32_t k_version;
extern int major, minor;
extern char linux_release[128];
extern uint64_t sys_boot_time_ns;
extern uint64_t prev_sys_boot_time_ns;
extern uint64_t adapt_kern_uid;
extern int bpf_raw_tracepoint_open(const char *name, int prog_fd);
static bool bpf_stats_map_collect(struct bpf_tracer *tracer,
struct trace_stats *stats_total);
static bool is_adapt_success(struct bpf_tracer *t);
static int update_offsets_table(struct bpf_tracer *t,
bpf_offset_param_t * offset);
static void datadump_process(void *data, int64_t boot_time);
static bool bpf_stats_map_update(struct bpf_tracer *tracer,
int socket_num, int trace_num,
int conflict_count,
int max_delay,
int total_time, int event_count);
static void save_kern_offsets(struct bpf_tracer *t);
static void display_kern_offsets(bpf_offset_param_t *offset);
static bool fentry_try_attach(const char *fn)
{
int prog_fd, attach_fd;
char kfunc_name[PROBE_NAME_SZ];
snprintf(kfunc_name, sizeof(kfunc_name), "kfunc__%s", fn);
struct bpf_insn insns[] = {
BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0), /* r0 = 0 */
BPF_EXIT_INSN(),
};
int stderr_fd = suspend_stderr();
if (stderr_fd < 0) {
ebpf_warning("Failed to suspend stderr.\n");
return false;
}
prog_fd = df_prog_load
(BPF_PROG_TYPE_TRACING, kfunc_name, insns, sizeof(insns));
if (prog_fd < 0) {
resume_stderr(stderr_fd);
return false;
}
attach_fd = bpf_raw_tracepoint_open(NULL, prog_fd);
if (attach_fd >= 0)
close(attach_fd);
close(prog_fd);
resume_stderr(stderr_fd);
return attach_fd >= 0;
}
static bool fentry_can_attach(const char *name)
{
const char *vmlinux_path = "/sys/kernel/btf/vmlinux";
if (access(vmlinux_path, R_OK))
return false;
/*
* There is a known bug in the fentry/fexit mechanism, detailed and fixed in
* the following commit:
* https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=e21d2b92354b3cd25dd774ebb0f0e52ff04a7861
*
* This bug poses a risk of system crash when attaching or detaching hooks on
* certain interfaces. To ensure the current kernel has been patched, we check
* for the presence of the function "__bpf_tramp_image_put_rcu", which was
* introduced as part of the bug fix. The existence of this function indicates
* that the kernel includes the necessary fix.
*/
if (kallsyms_lookup_name("__bpf_tramp_image_put_rcu") <= 0) {
ebpf_info("The current kernel is missing the fix for the fentry/fe"
"xit-related bug, will not use fentry/fexit bytecode.\n");
return false;
}
return fentry_try_attach(name);
}
static inline void
kfunc_set_sym_for_entry_and_exit(struct tracer_probes_conf *tps, const char *fn)
{
kfunc_set_symbol(tps, fn, false);
kfunc_set_symbol(tps, fn, true);
}
static inline bool hooked_socket_syscall_enabled(uint64_t flag)
{
return (hooked_socket_syscalls_bitmap & flag) != 0;
}
static inline void
config_tracepoint_enter_and_exit(struct tracer_probes_conf *tps,
const char *enter_tp,
const char *exit_tp)
{
tps_set_symbol(tps, enter_tp);
tps_set_symbol(tps, exit_tp);
}
static inline void
config_mixed_probe_and_tracepoint_exit(struct tracer_probes_conf *tps,
const char *enter_probe,
const char *exit_tp)
{
probes_set_enter_symbol(tps, enter_probe);
tps_set_symbol(tps, exit_tp);
}
static inline void config_probes_for_proc_event(struct tracer_probes_conf *tps)
{
if (access(SYSCALL_FORK_TP_PATH, F_OK)) {
/*
* Different CPU architectures have variations in system calls.
* It is necessary to confirm whether a specific system call exists.
* You can check https://arm64.syscall.sh/ for reference.
*/
if (kallsyms_lookup_name("sys_fork"))
probes_set_exit_symbol(tps, "sys_fork");
else if (kallsyms_lookup_name("__arm64_sys_fork"))
probes_set_exit_symbol(tps, "__arm64_sys_fork");
else if (kallsyms_lookup_name("__x64_sys_fork"))
probes_set_exit_symbol(tps, "__x64_sys_fork");
} else {
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_fork");
}
if (access(SYSCALL_CLONE_TP_PATH, F_OK)) {
if (kallsyms_lookup_name("sys_clone"))
probes_set_exit_symbol(tps, "sys_clone");
else if (kallsyms_lookup_name("__arm64_sys_clone"))
probes_set_exit_symbol(tps, "__arm64_sys_clone");
else if (kallsyms_lookup_name("__x64_sys_clone"))
probes_set_exit_symbol(tps, "__x64_sys_clone");
} else {
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_clone");
}
if (access(FTRACE_SCHED_PROC_PATH, F_OK)) {
#if defined(__x86_64__)
probes_set_exit_symbol(tps, "__x64_sys_execveat");
probes_set_exit_symbol(tps, "__x64_sys_execve");
#else
probes_set_exit_symbol(tps, "__arm64_sys_execveat");
probes_set_exit_symbol(tps, "__arm64_sys_execve");
#endif
probes_set_enter_symbol(tps, "do_exit");
} else {
tps_set_symbol(tps, "tracepoint/sched/sched_process_exec");
tps_set_symbol(tps, "tracepoint/sched/sched_process_exit");
}
}
#ifdef EXTENDED_AI_AGENT_FILE_IO
static inline void config_probes_for_ai_agent(struct tracer_probes_conf *tps)
{
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_openat");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_unlinkat");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_unlink");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_fchmodat");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_chmod");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_fchownat");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_chown");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_setuid");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_setgid");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_setreuid");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_setregid");
/* fork propagation is AI-agent-specific; exec/exit are already covered by
* config_probes_for_proc_event(). */
tps_set_symbol(tps, "tracepoint/sched/sched_process_fork");
}
#else
static inline void config_probes_for_ai_agent(struct tracer_probes_conf *tps)
{
(void)tps;
}
#endif
static void config_probes_for_kfunc(struct tracer_probes_conf *tps)
{
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_WRITE))
kfunc_set_sym_for_entry_and_exit(tps, "ksys_write");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_READ))
kfunc_set_sym_for_entry_and_exit(tps, "ksys_read");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_SENDTO))
kfunc_set_sym_for_entry_and_exit(tps, "__sys_sendto");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_SENDMSG))
kfunc_set_sym_for_entry_and_exit(tps, "__sys_sendmsg");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_SENDMMSG))
kfunc_set_sym_for_entry_and_exit(tps, "__sys_sendmmsg");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_RECVMSG))
kfunc_set_sym_for_entry_and_exit(tps, "__sys_recvmsg");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_WRITEV))
kfunc_set_sym_for_entry_and_exit(tps, "do_writev");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_READV))
kfunc_set_sym_for_entry_and_exit(tps, "do_readv");
#if defined(__x86_64__)
kfunc_set_symbol(tps, "__x64_sys_close", false);
#else
kfunc_set_symbol(tps, "__arm64_sys_close", false);
#endif
kfunc_set_symbol(tps, "__sys_socket", true);
kfunc_set_symbol(tps, "__sys_accept4", true);
kfunc_set_symbol(tps, "__sys_connect", false);
config_probes_for_proc_event(tps);
config_probes_for_ai_agent(tps);
/*
* On certain kernels, such as 5.15.0-127-generic and 5.10.134-18.al8.x86_64,
* `recvmmsg()/recvfrom()` probes of type `kprobe`/`kfunc` may not work properly. To address
* this, we use the more stable `tracepoint`-based probe instead.
*/
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_RECVFROM))
config_tracepoint_enter_and_exit(tps,
"tracepoint/syscalls/sys_enter_recvfrom",
"tracepoint/syscalls/sys_exit_recvfrom");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_RECVMMSG))
config_tracepoint_enter_and_exit(tps,
"tracepoint/syscalls/sys_enter_recvmmsg",
"tracepoint/syscalls/sys_exit_recvmmsg");
// Periodic trigger for timeout checks on cached data
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_getppid");
// If file I/O event collection is not enabled, hook installation will be skipped
if (io_event_collect_mode == IO_EVENT_COLLECT_DISABLE)
return;
// file R/W probes
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_pread64");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_preadv");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_pwrite64");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_pwritev");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_pread64");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_preadv");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_pwrite64");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_pwritev");
if (!access(SYSCALL_PRWV2_TP_PATH, F_OK)) {
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_preadv2");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_preadv2");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_pwritev2");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_pwritev2");
}
}
static void config_probes_for_kprobe_and_tracepoint(struct tracer_probes_conf
*tps)
{
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_SENDMSG))
config_mixed_probe_and_tracepoint_exit(tps, "__sys_sendmsg",
"tracepoint/syscalls/sys_exit_sendmsg");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_SENDMMSG))
config_mixed_probe_and_tracepoint_exit(tps, "__sys_sendmmsg",
"tracepoint/syscalls/sys_exit_sendmmsg");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_RECVMSG))
config_mixed_probe_and_tracepoint_exit(tps, "__sys_recvmsg",
"tracepoint/syscalls/sys_exit_recvmsg");
if (k_version == KERNEL_VERSION(3, 10, 0)) {
/*
* The Linux 3.10 kernel interface for Redhat7 and
* Centos7 is sys_writev() and sys_readv()
*/
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_WRITEV))
config_mixed_probe_and_tracepoint_exit(tps, "sys_writev",
"tracepoint/syscalls/sys_exit_writev");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_READV))
config_mixed_probe_and_tracepoint_exit(tps, "sys_readv",
"tracepoint/syscalls/sys_exit_readv");
} else {
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_WRITEV))
config_mixed_probe_and_tracepoint_exit(tps, "do_writev",
"tracepoint/syscalls/sys_exit_writev");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_READV))
config_mixed_probe_and_tracepoint_exit(tps, "do_readv",
"tracepoint/syscalls/sys_exit_readv");
}
config_probes_for_proc_event(tps);
/* tracepoints */
/*
* 由于在Linux 4.17+ sys_write, sys_read, sys_sendto, sys_recvfrom
* 接口会发生变化为了避免对内核的依赖采用tracepoints方式
*/
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_WRITE))
config_tracepoint_enter_and_exit(tps,
"tracepoint/syscalls/sys_enter_write",
"tracepoint/syscalls/sys_exit_write");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_READ))
config_tracepoint_enter_and_exit(tps,
"tracepoint/syscalls/sys_enter_read",
"tracepoint/syscalls/sys_exit_read");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_SENDTO))
config_tracepoint_enter_and_exit(tps,
"tracepoint/syscalls/sys_enter_sendto",
"tracepoint/syscalls/sys_exit_sendto");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_RECVFROM))
config_tracepoint_enter_and_exit(tps,
"tracepoint/syscalls/sys_enter_recvfrom",
"tracepoint/syscalls/sys_exit_recvfrom");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_RECVMMSG))
config_tracepoint_enter_and_exit(tps,
"tracepoint/syscalls/sys_enter_recvmmsg",
"tracepoint/syscalls/sys_exit_recvmmsg");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_connect");
// exit tracepoints
/*
* `tracepoint/syscalls/sys_exit_socket` This is currently added only to
* implement the NGINX tracing feature. If the tracing feature is disabled,
* the syscall socket() interface will not be hooked.
*/
if (!g_disable_syscall_tracing)
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_socket");
else
ebpf_info("Due to the tracing feature being disabled, the"
" syscall socket() will not be attached.\n");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_accept");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_accept4");
// clear trace connection & fetch close info
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_close");
// Periodic trigger for timeout checks on cached data
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_getppid");
// If file I/O event collection is not enabled, hook installation will be skipped
if (io_event_collect_mode == IO_EVENT_COLLECT_DISABLE)
return;
// file R/W probes
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_pread64");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_preadv");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_pwrite64");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_pwritev");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_pread64");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_preadv");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_pwrite64");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_pwritev");
if (!access(SYSCALL_PRWV2_TP_PATH, F_OK)) {
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_preadv2");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_preadv2");
tps_set_symbol(tps, "tracepoint/syscalls/sys_enter_pwritev2");
tps_set_symbol(tps, "tracepoint/syscalls/sys_exit_pwritev2");
}
config_probes_for_ai_agent(tps);
}
static inline void __config_kprobe(struct tracer_probes_conf *tps,
const char *name_1,
const char *name_2,
const char *syscall_name)
{
/*
* In Linux 4.17+, use sys_write, sys_read, sys_sendto, sys_recvfrom;
* otherwise, use ksys_write, ksys_read, __sys_sendto, __sys_recvfrom
*
*/
if (kallsyms_lookup_name(name_1))
probes_set_symbol(tps, name_1);
else if (kallsyms_lookup_name(name_2))
probes_set_symbol(tps, name_2);
else
ebpf_warning("Missing system call '%s()'\n",
syscall_name);
}
static void config_probes_for_kprobe(struct tracer_probes_conf *tps)
{
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_WRITE))
__config_kprobe(tps, "ksys_write", "sys_write", "write");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_READ))
__config_kprobe(tps, "ksys_read", "sys_read", "read");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_SENDTO))
__config_kprobe(tps, "__sys_sendto", "sys_sendto", "sendto");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_RECVFROM))
__config_kprobe(tps, "__sys_recvfrom", "sys_recvfrom", "recvfrom");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_SENDMSG))
probes_set_symbol(tps, "__sys_sendmsg");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_SENDMMSG))
probes_set_symbol(tps, "__sys_sendmmsg");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_RECVMSG))
probes_set_symbol(tps, "__sys_recvmsg");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_RECVMMSG))
probes_set_symbol(tps, "__sys_recvmmsg");
if (io_event_collect_mode != IO_EVENT_COLLECT_DISABLE) {
probes_set_symbol(tps, "ksys_pread64");
probes_set_symbol(tps, "do_preadv");
probes_set_symbol(tps, "ksys_pwrite64");
probes_set_symbol(tps, "do_pwritev");
}
if (k_version == KERNEL_VERSION(3, 10, 0)) {
/*
* The Linux 3.10 kernel interface for Redhat7 and
* Centos7 is sys_writev() and sys_readv()
*/
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_WRITEV))
probes_set_symbol(tps, "sys_writev");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_READV))
probes_set_symbol(tps, "sys_readv");
} else {
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_WRITEV))
probes_set_symbol(tps, "do_writev");
if (hooked_socket_syscall_enabled(HOOKED_SOCKET_SYSCALL_READV))
probes_set_symbol(tps, "do_readv");
}
config_probes_for_proc_event(tps);
#if defined(__x86_64__)
probes_set_enter_symbol(tps, "__x64_sys_getppid");
#else
if (kallsyms_lookup_name("__arm64_sys_getppid"))
probes_set_enter_symbol(tps, "__arm64_sys_getppid");
else
probes_set_enter_symbol(tps, "sys_getppid");
#endif
probes_set_exit_symbol(tps, "__sys_accept4");
probes_set_enter_symbol(tps, "__close_fd");
probes_set_exit_symbol(tps, "__sys_socket");
probes_set_enter_symbol(tps, "__sys_connect");
config_probes_for_ai_agent(tps);
}
static void socket_tracer_set_probes(struct tracer_probes_conf *tps)
{
if (g_k_type == K_TYPE_KFUNC)
config_probes_for_kfunc(tps);
else if (g_k_type == K_TYPE_KPROBE)
config_probes_for_kprobe(tps);
else
config_probes_for_kprobe_and_tracepoint(tps);
}
/* ==========================================================
* 内核结构成员偏移推断,模拟一个TCP通信tick内核,使其完成推断
* ==========================================================
*/
static int kernel_offset_infer_server(void)
{
int cli_fd;
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
memset(&client_addr, 0, sizeof(struct sockaddr_in));
int client_count = 0, cpu_online_count = 0, i;
for (i = 0; i < sys_cpus_count; i++) {
if (cpu_online[i])
cpu_online_count++;
}
next_cpu_client:
cli_fd =
accept(infer_socktrace_fd, (struct sockaddr *)&client_addr,
&addr_len);
if (cli_fd < 0) {
ebpf_warning("[eBPF Kernel Adapt] Fail to accept client"
"request - %s\n", strerror(errno));
return ETR_IO;
}
char buffer[16];
int len;
for (;;) {
len = recv(cli_fd, buffer, sizeof(buffer), 0);
if (len < 0) {
continue;
}
if (len == 0) {
client_count++;
close(cli_fd);
break;
}
buffer[len] = '\0';
if (strcmp(buffer, "hello") == 0) {
snprintf(buffer, sizeof(buffer), "OK");
send(cli_fd, buffer, 2, 0);
}
}
if (client_count < cpu_online_count)
goto next_cpu_client;
close(infer_socktrace_fd);
ebpf_info("[eBPF Kernel Adapt] kernel_offset_infer_server close."
"client_count:%d\n", client_count);
return ETR_OK;
}
static int kernel_offset_infer_client(void)
{
int cli_fd;
struct sockaddr_in server_addr;
char buf[16];
int len;
if ((cli_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
ebpf_warning
("[eBPF Kernel Adapt] Fail client create socket - %s\n",
strerror(errno));
return ETR_IO;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(OFFSET_INFER_SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(OFFSET_INFER_SERVER_ADDR);
if (connect
(cli_fd, (struct sockaddr *)&server_addr,
sizeof(server_addr)) < 0) {
ebpf_warning("[eBPF Kernel Adapt] Fail to connect"
" - %s\n", strerror(errno));
return ETR_IO;
}
for (;;) {
snprintf(buf, sizeof(buf), "hello");
len = send(cli_fd, buf, strlen(buf), 0);
if (len != strlen(buf))
continue;
rcv_loop:
len = recv(cli_fd, buf, sizeof(buf), 0);
if (len > 0) {
/*
* Another send action occurs here to avoid
* failure to infer the value of 'write_seq'.
* Troubleshoot the invalid kernel adaptation
* mechanism in EulerOS 2.9 and EulerOS 2.10
* (Linux 4.18).
*/
buf[len] = '\0';
send(cli_fd, buf, len, 0);
break;
} else if (len == 0) {
break;
} else
goto rcv_loop;
}
close(cli_fd);
return ETR_OK;
}
static int kernel_offset_infer_init(void)
{
struct sockaddr_in srv_addr;
memset(&srv_addr, 0, sizeof(srv_addr));
infer_socktrace_fd = socket(AF_INET, SOCK_STREAM, 0);
if (infer_socktrace_fd < 0) {
ebpf_warning
("[eBPF Kernel Adapt] Fail to create server socket - %s\n",
strerror(errno));
return ETR_IO;
}
srv_addr.sin_family = AF_INET;
srv_addr.sin_port = htons(OFFSET_INFER_SERVER_PORT);
srv_addr.sin_addr.s_addr = inet_addr(OFFSET_INFER_SERVER_ADDR);
if (-1 ==
bind(infer_socktrace_fd, (struct sockaddr *)&srv_addr,
sizeof(srv_addr))) {
ebpf_warning("[eBPF Kernel Adapt] Fail to bind server socket"
" %s:%d - %s\n"
"Please check the following two situations:\n"
"(1) Whether the %s address has been "
"configured on the loopback device.\n"
"(2) Check if port %d has already been occupied "
"by other services.\n\n"
"%s:%d is used to trigger a temporary service"
" for Linux kernel adaptation. It is important to ensure"
" that %s is configured on the lo device and that"
" port %d is not occupied by other services.\n\n"
"If port %d is occupied by other services, you can either"
" change the port number of the other service or wait until"
" the deepflow-agent is started before starting this service."
"This is because the deepflow-agent only temporarily uses "
"port %d during its startup phase and will close the service"
"once it is started.\n\n",
OFFSET_INFER_SERVER_ADDR,
OFFSET_INFER_SERVER_PORT,
strerror(errno),
OFFSET_INFER_SERVER_ADDR,
OFFSET_INFER_SERVER_PORT,
OFFSET_INFER_SERVER_ADDR,
OFFSET_INFER_SERVER_PORT,
OFFSET_INFER_SERVER_ADDR,
OFFSET_INFER_SERVER_PORT,
OFFSET_INFER_SERVER_PORT,
OFFSET_INFER_SERVER_PORT);
close(infer_socktrace_fd);
return ETR_IO;
}
if (-1 == listen(infer_socktrace_fd, 1)) {
ebpf_warning
("[eBPF Kernel Adapt] Server socket listen failed - %s\n",
strerror(errno));
close(infer_socktrace_fd);
return ETR_IO;
}
return ETR_OK;
}
static int socktrace_sockopt_set(sockoptid_t opt, const void *conf, size_t size)
{
return 0;
}
static bool bpf_offset_map_collect(struct bpf_tracer *tracer,
struct bpf_offset_param_array *array)
{
int nr_cpus = get_num_possible_cpus();
bpf_offset_param_t values[nr_cpus];
if (!bpf_table_get_value(tracer, MAP_MEMBERS_OFFSET_NAME, 0, values))
return false;
bpf_offset_param_t *out_val = (bpf_offset_param_t *) (array + 1);
int i;
for (i = 0; i < array->count; i++)
out_val[i] = values[i];
return true;
}
static int socktrace_sockopt_get(sockoptid_t opt, const void *conf, size_t size,
void **out, size_t * outsize)
{
struct bpf_tracer *t = find_bpf_tracer(SK_TRACER_NAME);
if (t == NULL)
return -1;
*outsize = sizeof(struct bpf_socktrace_params) +
sizeof(bpf_offset_param_t) * sys_cpus_count;
*out = calloc(1, *outsize);
if (*out == NULL) {
ebpf_warning("calloc, error:%s\n", strerror(errno));
return -1;
}
struct bpf_socktrace_params *params = *out;
struct bpf_offset_param_array *array = ¶ms->offset_array;
array->count = sys_cpus_count;
// Fetch socket Information from eBPF map
struct ebpf_map *map =
ebpf_obj__get_map_by_name(t->obj, MAP_SOCKET_INFO_NAME);
if (map == NULL) {
ebpf_warning("[%s] map(name:%s) is NULL.\n", __func__,
MAP_SOCKET_INFO_NAME);
}
int map_fd = map->fd;
struct socktrace_msg *msg = (struct socktrace_msg *)conf;
if (size != sizeof(*msg)) {
ebpf_warning("The parameter 'socktrace_msg' is passed"
"incorrectly with a size mismatch. The passed"
" size is %d, while the actual structure length"
" is %d.\n", size, sizeof(*msg));
return -1;
}
uint64_t conn_key = (uint64_t)msg->pid << 32 | msg->fd;
struct socket_info_s info;
if (bpf_lookup_elem(map_fd, &conn_key, &info) == 0) {
params->socket_id = info.uid;
params->seq = info.seq;
params->l7_proto = info.l7_proto;
params->data_source = info.data_source;
params->direction = info.direction;
params->pre_direction = info.pre_direction;
params->is_tls = info.is_tls;
params->peer_fd = info.peer_fd;
params->prev_data_len = info.prev_data_len;
params->allow_reassembly = info.allow_reassembly;
params->finish_reasm = info.finish_reasm;
params->force_reasm = info.force_reasm;
params->no_trace = info.no_trace;
params->reasm_bytes = info.reasm_bytes;
params->update_time = info.update_time;
}
params->kern_socket_map_max = conf_max_socket_entries;
params->kern_trace_map_max = conf_max_trace_entries;
params->tracer_state = t->state;
pthread_mutex_lock(&datadump_mutex);
params->datadump_enable = datadump_enable;
params->datadump_pid = datadump_pid;
params->datadump_proto = datadump_proto;
params->datadump_port = datadump_port;
params->proc_exec_event_count = get_proc_exec_event_count();
params->proc_exit_event_count = get_proc_exit_event_count();
safe_buf_copy(params->datadump_file_path,
sizeof(params->datadump_file_path),
(void *)datadump_file_path, sizeof(datadump_file_path));
memcpy(params->datadump_comm, datadump_comm, sizeof(datadump_comm));
memcpy(params->datadump_ipaddr, datadump_ipaddr, sizeof(datadump_ipaddr));
pthread_mutex_unlock(&datadump_mutex);
struct trace_stats stats_total;
if (bpf_stats_map_collect(t, &stats_total)) {
params->kern_socket_map_used = stats_total.socket_map_count;
params->kern_trace_map_used = stats_total.trace_map_count;
}
if (!bpf_offset_map_collect(t, array)) {
free(*out);
return -1;
}
return 0;
}
static struct tracer_sockopts socktrace_sockopts = {
.version = SOCKOPT_VERSION,
.set_opt_min = SOCKOPT_SET_SOCKTRACE_ADD,
.set_opt_max = SOCKOPT_SET_SOCKTRACE_FLUSH,
.set = socktrace_sockopt_set,
.get_opt_min = SOCKOPT_GET_SOCKTRACE_SHOW,
.get_opt_max = SOCKOPT_GET_SOCKTRACE_SHOW,
.get = socktrace_sockopt_get,
};
static int datadump_sockopt_set(sockoptid_t opt, const void *conf, size_t size)
{
struct datadump_msg *msg = (struct datadump_msg *)conf;
pthread_mutex_lock(&datadump_mutex);
if (msg->is_params) {
datadump_pid = msg->pid;
datadump_proto = msg->proto;
datadump_port = msg->port;
safe_buf_copy(datadump_comm, sizeof(datadump_comm),
(void *)msg->comm, sizeof(msg->comm));
safe_buf_copy(datadump_ipaddr, sizeof(datadump_ipaddr),
(void *)msg->ipaddr, sizeof(msg->ipaddr));
ebpf_info("Set datadump pid %d comm %s proto %d ip %s port %d\n",
datadump_pid, datadump_comm, datadump_proto,
datadump_ipaddr, datadump_port);
} else {
if (!datadump_enable && msg->enable) {
// create a new output file
if (datadump_file == stdout && !msg->only_stdout) {
char *file = gen_file_name_by_datetime();
if (file != NULL) {
snprintf(datadump_file_path,
sizeof(datadump_file_path),
"%s/datadump-%s.log",
DATADUMP_FILE_PATH_PREFIX,
file);
free(file);
datadump_file =
fopen(datadump_file_path, "a+");
if (datadump_file == NULL) {
memcpy(datadump_file_path,
"stdout", 7);
datadump_file = stdout;
}
ebpf_info("create datadump file %s\n",
datadump_file_path);
}
}
}
if (msg->enable) {
datadump_seq = 0;
datadump_start_time = get_sys_uptime();
datadump_timeout = msg->timeout;
}
if (datadump_enable && !msg->enable) {
datadump_seq = 0;
datadump_timeout = 0;
datadump_pid = 0;
datadump_port = 0;
datadump_comm[0] = '\0';
datadump_ipaddr[0] = '\0';
datadump_proto = 0;
fprintf(datadump_file,
"\n\nDump data is finished, use time: %us.\n\n",
get_sys_uptime() - datadump_start_time);
if (datadump_file != stdout) {
fclose(datadump_file);
ebpf_info("close datadump file %s\n",
datadump_file_path);
}
memcpy(datadump_file_path, "stdout", 7);
datadump_file = stdout;
datadump_start_time = 0;
}
datadump_enable = msg->enable;
ebpf_info("datadump %s\n",
datadump_enable ? "enable" : "disable");
}
pthread_mutex_unlock(&datadump_mutex);
return 0;
}
static int datadump_sockopt_get(sockoptid_t opt, const void *conf, size_t size,
void **out, size_t * outsize)
{
return 0;
}
/*
* Configure and enable datadump
*
* @pid
* Specifying a process ID or thread ID. If set to '0', it indicates
* all processes or threads.
* @comm
* Specifying a process name or thread name. If set to an empty string(""),
* it indicates all processes or threads.
* @proto
* Specifying the L7 protocol number. If set to '0', it indicates all
* L7 protocols.
* @ipaddr
* Specifying ip address.
* @port
* Specifying port.
* @timeout
* Specifying the timeout duration. If the elapsed time exceeds this
* duration, datadump will stop. The unit is in seconds.
* @callback
* Callback interface, used to transfer data to the remote controller.
*
* @return 0 on success, and a negative value on failure.
*/
int datadump_set_config(int pid, const char *comm, int proto, const char *ipaddr,
int port, int timeout, debug_callback_t cb)
{
if (pid < 0 || proto < 0 || proto >= PROTO_NUM || comm == NULL
|| timeout <= 0) {
ebpf_warning("Invalid parameter\n");
return -1;
}
pthread_mutex_lock(&datadump_mutex);
if (datadump_enable) {
ebpf_warning("datadump is already running in the process.\n");
goto finish;