-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtelemetryLoader.c
1619 lines (1426 loc) · 52.1 KB
/
telemetryLoader.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
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
/*
SysinternalsEBPF
Copyright (c) Microsoft Corporation
All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//====================================================================
//
// telemetryLoader.c
//
// eBPF telemetry loader and controller.
//
//====================================================================
#include <unistd.h>
#include <asm/unistd.h>
#include <stdlib.h>
#include <libbpf.h>
#include <sys/resource.h>
#include <bpf.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <sys/utsname.h>
#include <types.h>
#include <signal.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <net/ethernet.h>
#include <linux/limits.h>
#include "libsysinternalsEBPF.h"
#include "discoverOffsets.h"
#include "unameOffsets.h"
#include "searchOffsets.h"
#include "sysinternalsEBPF.h"
#include "syscalls.h"
typedef void (EventCallback_u32)(void *ctx, int cpu, void *data, __u32 size);
typedef void (EventLostCallback_u64)(void *ctx, int cpu, __u64 lostCnt);
bool linkTPprogs(const ebpfTelemetryObject *obj, const bool *activeSyscalls);
bool linkRTPprogs(const ebpfTelemetryObject *obj, const bool *activeSyscalls);
bool linkOtherTPprogs(const ebpfTelemetryObject *obj, const bool *activeSyscalls);
extern const syscallNames syscallNumToName[SYSCALL_MAX+1];
static log_callback logCallback = NULL;
static struct bpf_object *bpfObj = NULL;
struct perf_buffer_opts pbOpts = {};
struct perf_buffer *pb = NULL;
typedef struct {
unsigned int numProgs;
struct bpf_program **prog;
unsigned int numLinks;
struct bpf_link **link;
} ebpfTPprogs;
const ebpfTelemetryObject *object = NULL;
ebpfConfig config;
int configMapFd = 0;
int perfErrorsMapFd = 0;
unsigned int numSysEnter;
unsigned int numSysExit;
unsigned int numRawSysEnter;
unsigned int numRawSysExit;
unsigned int numOtherTp;
static ebpfTPprogs *bpfSysEnter;
static ebpfTPprogs *bpfSysExit;
static struct bpf_program **bpfRawSysEnter;
static struct bpf_program **bpfRawSysExit;
static struct bpf_link **bpfRawSysEnterLink;
static struct bpf_link **bpfRawSysExitLink;
static struct bpf_program **bpfOtherTp;
static struct bpf_link **bpfOtherTpLink;
bool rawTracepoints = false;
enum direction {forwards, backwards};
struct bpf_object *rawBpfObj = NULL;
int rawSock = -1;
double g_bootSecSinceEpoch = 0;
bool running = true;
bool signalInterrupt = false;
bool telemetryCancelled = false;
eBPFerrorString eBPFerrorStrings[] =
{
{ E_EBPF_SUCCESS, "Operation completed successfully"},
{ E_EBPF_CATASTROPHIC, "Catastrophic failure"},
{ E_EBPF_NOTSUPPORTED, "Operation not supported"},
{ E_EBPF_NOPROG, "eBPF object could not be located"},
{ E_EBPF_NOMAP, "eBPF map could not be located"},
{ E_EBPF_NOATTACH, "eBPF program could not be attached"},
{ E_EBPF_NORB, "perf ring buffer could not be started"},
{ E_EBPF_NOFILEPATH, "eBPF program could not be opened"},
{ E_EBPF_INVALIDPARAMS, "Invalid parameters"},
{ E_EBPF_NOTP, "Syscall tracepoint programs could not be located"},
{ E_EBPF_NORTP, "Syscall raw tracepoint programs could not be located"},
{ E_EBPF_NOOTHTP, "Non-syscall tracepoint programs could not be located"},
{ E_EBPF_NOLOAD, "eBPF object could not be loaded"},
{ E_EBPF_CONFIGFAIL, "Configuration could not be loaded"},
{ E_EBPF_MAPUPDATEFAIL, "Map could not be updated"},
{ E_EBPF_NORAWSOCK, "Raw socket program could not be attached"},
{ E_DISC_CATASTROPHIC, "Discovery - catastrophic failure"},
{ E_DISC_NOTSUPPORTED, "Discovery - operation not supported"},
{ E_DISC_NOPROG, "Discovery - eBPF programs could not be loaded"},
{ E_DISC_NOMAP, "Discovery - map could not be located"},
{ E_DISC_NOATTACH, "Discovery - eBPF program could not be attached"},
{ E_DISC_NORB, "Discovery - perf ring buffer could not be started"},
{ E_DISC_NOPDEATH, "Discovery - cannot set PDEATH signal"},
{ E_DISC_NOTASK, "Discovery - could not obtain task struct"},
{ E_DISC_GET_COMM, "Discovery - could not read COMM from /proc"},
{ E_DISC_PID_OFFSET, "Discovery - could not find PID offset"},
{ E_DISC_START_TIME_OFFSET, "Discovery - could not find start_time offset"},
{ E_DISC_COMM_OFFSET, "Discovery - could not find COMM offset"},
{ E_DISC_CREDS_OFFSET, "Discovery - could not find creds offsets"},
{ E_DISC_PWD_PATH_OFFSET, "Discovery - could not find path offset"},
{ E_DISC_DENTRY_NAME_OFFSET, "Discovery - could not find dentry name offset"},
{ E_DISC_DENTRY_PARENT_OFFSET,"Discovery - could not find dentry parent offset"},
{ E_DISC_DENTRY_INODE_OFFSET, "Discovery - could not find dentry inode offset"},
{ E_DISC_MOUNT_OFFSET, "Discovery - could not find mount offset"},
{ E_DISC_FD_OFFSET, "Discovery - could not find the file descriptor offsets"},
{ E_DISC_TTY_OFFSET, "Discovery - could not find the TTY offsets"},
{ E_DISC_MM_OFFSET, "Discovery - could not find the MM offsets"},
{ E_DISC_EXE_PATH_OFFSET, "Discovery - could not find the exe offsets"},
{ E_DISC_SKBUFF_OFFSET, "Discovery - could not find the skbuff offsets"},
};
//--------------------------------------------------------------------
//
// logMessage
//
// Helper function that calls the log callback function (if set)
//
//--------------------------------------------------------------------
void logMessage(const char* format, ...)
{
if(logCallback != NULL)
{
va_list args;
va_start(args, format);
logCallback(format, args);
va_end(args);
}
}
//--------------------------------------------------------------------
//
// setLogCallback
//
// Sets the logging callback that will be invoked for all log messages.
//
//--------------------------------------------------------------------
void setLogCallback(log_callback callback)
{
logCallback = callback;
}
//--------------------------------------------------------------------
//
// eBPFstrerror
//
// Convert an eBPF error number to a printable error string.
//
//--------------------------------------------------------------------
const char *eBPFstrerror(int error)
{
unsigned int i;
unsigned int numErrors = sizeof(eBPFerrorStrings) / sizeof(*eBPFerrorStrings);
for (i=0; i<numErrors; i++) {
if (eBPFerrorStrings[i].error == error)
return eBPFerrorStrings[i].str;
}
return "";
}
//--------------------------------------------------------------------
//
// telemetryCloseAll
//
// Shut down all eBPF telemetry and free memory.
//
//--------------------------------------------------------------------
void telemetryCloseAll()
{
unsigned int i, j;
if (!rawTracepoints) {
for (i=0; i<numSysEnter; i++) {
ebpfTPprogs *s = &bpfSysEnter[i];
for (j=0; j<s->numLinks; j++) {
if (s->link[j] != NULL) {
bpf_link__destroy(s->link[j]);
}
}
if (s->prog)
free(s->prog);
if (s->link)
free(s->link);
}
if (bpfSysEnter) {
free(bpfSysEnter);
bpfSysEnter = NULL;
}
for (i=0; i<numSysExit; i++) {
ebpfTPprogs *s = &bpfSysExit[i];
for (j=0; j<s->numLinks; j++) {
if (s->link[j] != NULL) {
bpf_link__destroy(s->link[j]);
}
}
if (s->prog)
free(s->prog);
if (s->link)
free(s->link);
}
if (bpfSysExit) {
free(bpfSysExit);
bpfSysExit = NULL;
}
} else {
for (i=0; i<numRawSysEnter; i++) {
if (bpfRawSysEnterLink[i] != NULL) {
bpf_link__destroy(bpfRawSysEnterLink[i]);
}
}
if (bpfRawSysEnterLink) {
free(bpfRawSysEnterLink);
bpfRawSysEnterLink = NULL;
}
if (bpfRawSysEnter) {
free(bpfRawSysEnter);
bpfRawSysEnter = NULL;
}
for (i=0; i<numRawSysExit; i++) {
if (bpfRawSysExitLink[i] != NULL) {
bpf_link__destroy(bpfRawSysExitLink[i]);
}
}
if (bpfRawSysExitLink) {
free(bpfRawSysExitLink);
bpfRawSysExitLink = NULL;
}
if (bpfRawSysExit) {
free(bpfRawSysExit);
bpfRawSysExit = NULL;
}
}
if (numOtherTp > 0) {
for (i=0; i<numOtherTp; i++) {
if (bpfOtherTp[i] != NULL) {
bpf_link__destroy(bpfOtherTpLink[i]);
}
}
if (bpfOtherTp) {
free(bpfOtherTp);
bpfOtherTp = NULL;
}
if (bpfOtherTpLink) {
free(bpfOtherTpLink);
bpfOtherTpLink = NULL;
}
}
if (bpfObj) {
bpf_object__close(bpfObj);
bpfObj = NULL;
}
if (pb) {
perf_buffer__free(pb);
pb = NULL;
}
if (rawSock != -1) {
close(rawSock);
bpf_object__close(rawBpfObj);
rawBpfObj = NULL;
rawSock = -1;
}
}
//--------------------------------------------------------------------
//
// telemetrySignalInterrupt
//
// Inform the control loop that the signal just received was expected,
// to avoid the control loop assuming the error exit from the poll was
// a catastrophic error.
//
//--------------------------------------------------------------------
void telemetrySignalInterrupt(int code)
{
signalInterrupt = true;
}
//--------------------------------------------------------------------
//
// telemetryCancel
//
// Inform the control loop that the telemetry has been cancelled.
//
//--------------------------------------------------------------------
void telemetryCancel()
{
telemetryCancelled = true;
}
//--------------------------------------------------------------------
//
// telemetryUpdateSyscalls
//
// Stop running eBPF programs and relink with new activeSyscalls array
// values. Used to update when the configuration changes.
//
//--------------------------------------------------------------------
void telemetryUpdateSyscalls(bool *activeSyscalls)
{
if (activeSyscalls == NULL) {
logMessage("ebpfTelemetryUpdateSyscalls invalid params\n");
return;
}
unsigned int i, j;
unsigned int configEntry = 0;
if (!rawTracepoints) {
// destroy old links
for (i=0; i<numSysEnter; i++) {
ebpfTPprogs *p = &bpfSysEnter[i];
for (j=0; j<p->numLinks; j++) {
if (p->link[j] != NULL) {
bpf_link__destroy(p->link[j]);
p->link[j] = NULL;
}
}
}
for (i=0; i<numSysExit; i++) {
ebpfTPprogs *p = &bpfSysExit[i];
for (j=0; j<p->numLinks; j++) {
if (p->link[j] != NULL) {
bpf_link__destroy(p->link[j]);
p->link[j] = NULL;
}
}
}
// link to new syscall config
linkTPprogs(object, activeSyscalls);
} else {
// destroy old links
for (i=0; i<numRawSysEnter; i++) {
if (bpfRawSysEnterLink[i] != NULL) {
bpf_link__destroy(bpfRawSysEnterLink[i]);
}
}
for (i=0; i<numRawSysExit; i++) {
if (bpfRawSysExitLink[i] != NULL) {
bpf_link__destroy(bpfRawSysExitLink[i]);
}
}
// link to new syscall config
linkRTPprogs(object, activeSyscalls);
}
// destroy old links
if (numOtherTp > 0) {
for (i=0; i<numOtherTp; i++) {
if (bpfOtherTpLink[i] != NULL) {
bpf_link__destroy(bpfOtherTpLink[i]);
}
}
}
// link to new syscall config
linkOtherTPprogs(object, activeSyscalls);
// update syscall config in EBPF
memcpy(config.active, activeSyscalls, sizeof(config.active));
bpf_map_update_elem(configMapFd, &configEntry, &config, BPF_ANY);
}
//--------------------------------------------------------------------
//
// connectRawSock
//
// Connect the raw socket eBPF program to a new raw socket.
// (Enables skb/consume_skb tracepoint to see outbound packets.)
//
//--------------------------------------------------------------------
bool connectRawSock(const ebpfTelemetryConfig *ebpfConfig)
{
char filepath[] = SYSINTERNALS_EBPF_INSTALL_DIR "/" EBPF_RAW_SOCK_OBJ;
struct stat filepathStat;
int rawProgFd = -1;
struct bpf_program *rawBpfProg = NULL;
// check path
if (stat(filepath, &filepathStat) != 0 || !S_ISREG(filepathStat.st_mode)) {
logMessage("ERROR: cannot access EBPF kernel object: %s\n", filepath);
return false;
}
struct bpf_object_open_opts openopts = {};
openopts.sz = sizeof(struct bpf_object_open_opts);
if(ebpfConfig->btfFile)
{
openopts.btf_custom_path = ebpfConfig->btfFile;
}
rawBpfObj = bpf_object__open_file(filepath, &openopts);
if (libbpf_get_error(bpfObj)) {
logMessage("ERROR: failed to open prog: %s '%s'\n", filepath, strerror(errno));
return false;
}
rawBpfProg = bpf_object__find_program_by_name(rawBpfObj, "rawEBPFprog");
if (rawBpfProg == NULL) {
logMessage("ERROR: failed to locate program: %s '%s'\n", filepath, strerror(errno));
return false;
}
bpf_program__set_type(rawBpfProg, BPF_PROG_TYPE_SOCKET_FILTER);
if (bpf_object__load(rawBpfObj)) {
logMessage("ERROR: failed to load prog: %s '%s'\n", filepath, strerror(errno));
return false;
}
rawProgFd = bpf_program__fd(rawBpfProg);
if (rawProgFd < 0) {
logMessage("ERROR: failed to find prog: %s '%s'\n", filepath, strerror(errno));
return false;
}
rawSock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL));
if (rawSock < 0) {
logMessage("ERROR: cannot open raw socket: '%s'\n", strerror(errno));
return false;
}
if (setsockopt(rawSock, SOL_SOCKET, SO_ATTACH_BPF, &rawProgFd, sizeof(rawProgFd)) < 0) {
logMessage("ERROR: setsockopt failed on raw socket: '%s'\n", strerror(errno));
return false;
}
return true;
}
//--------------------------------------------------------------------
//
// insertConfigOffsets
//
// Extract offsets from comma-separated string and insert into array.
//
//--------------------------------------------------------------------
bool insertConfigOffsets(unsigned int *item, char *value)
{
if (item == NULL || value == NULL) {
logMessage("insertConfigOffsets invalid params\n");
return false;
}
const char *offset = NULL;
unsigned int i;
char *innerStrtok = NULL;
offset = strtok_r(value, " ,", &innerStrtok);
if (!offset) {
item[0] = -1;
return false;
}
i = 0;
while (offset && i < (NUM_REDIRECTS - 1)) {
item[i] = atoi(offset);
offset = strtok_r(NULL, " ,", &innerStrtok);
i++;
}
item[i] = DEREF_END;
return true;
}
//--------------------------------------------------------------------
//
// populateConfigOffsets
//
// Obtain kernel struct offsets from the config file, the offsets
// database, or via discovery, and store in the config..
//
//--------------------------------------------------------------------
bool populateConfigOffsets(ebpfConfig *c, const char *argv[],
time_t procStartTime)
{
if (c == NULL || argv == NULL) {
logMessage("populateConfigOffsets invalid params\n");
return false;
}
FILE *config;
char *line = NULL;
size_t len = 0;
ssize_t readLen;
char *param = NULL;
char *value = NULL;
char *whitespace = NULL;
unsigned int *item = NULL;
char *outerStrtok = NULL;
if(!fileExists(BTF_KERNEL_FILE))
{
config = fopen(CONFIG_FILE, "r");
if (!config) {
if (searchOffsets(&c->offsets)) {
logMessage("Discovery process: from database\n");
return true;
}
logMessage("Discovery process: auto discovery\n");
return (discoverOffsets(&c->offsets, argv, procStartTime) == E_EBPF_SUCCESS);
}
logMessage("Discovery process: getOffsets\n");
while ((readLen = getline(&line, &len, config)) >= 0) {
if (readLen > 0 && line[0] == '#')
continue;
whitespace = line;
while (*whitespace == ' ')
whitespace++;
param = strtok_r(whitespace, " =", &outerStrtok);
if (!param)
continue;
value = strtok_r(NULL, "\n", &outerStrtok);
if (!value)
continue;
whitespace = value;
while (*whitespace == ' ' || *whitespace == '=')
whitespace++;
value = whitespace;
item = findConfigItem(&c->offsets, param);
if (item)
insertConfigOffsets(item, value);
}
free(line);
fclose(config);
}
else
{
logMessage("Discovery process: BTF-CORE\n");
}
return true;
}
//--------------------------------------------------------------------
//
// combineKernelVersion
//
// Converts the kernel version to a uint32_t that can be easily
// compared.
//
//--------------------------------------------------------------------
uint32_t combineKernelVersion(uint32_t major, uint32_t minor)
{
return (major << 16) + minor;
}
//--------------------------------------------------------------------
//
// getKernelVersion
//
// Get the kernel version from the uname and return as a uint32_t.
//
//--------------------------------------------------------------------
uint32_t getKernelVersion()
{
unsigned int major = 0, minor = 0;
struct utsname unameStruct = {{ 0 }};
if (uname(&unameStruct)){
logMessage("Couldn't find uname, '%s'\n", strerror(errno));
return 0;
}
if (sscanf(unameStruct.release, "%u.%u", &major, &minor) == 2){
logMessage("Found Kernel version: %u.%u\n", major, minor);
} else {
logMessage("Couldn't find version\n");
return 0;
}
return combineKernelVersion(major, minor);
}
//--------------------------------------------------------------------
//
// getObjectAndPath
//
// Search the telemetry config for a suitable kernel object.
//
//--------------------------------------------------------------------
const ebpfTelemetryObject *getObjectAndPath(char *filepath,
unsigned int size, const ebpfTelemetryConfig *ebpfConfig)
{
if (filepath == NULL || ebpfConfig == NULL) {
logMessage("getObjectAndPath invalid params\n");
return NULL;
}
const char *filename = NULL;
struct stat filepathStat;
const ebpfTelemetryObject *ebpfObj = NULL;
unsigned int i = 0;
// combine kernel major and minor for easy comparisons
uint32_t combinedKernelVersion = getKernelVersion();
// find first matching EBPF ELF object
for (i=0; i<ebpfConfig->numEBPFobjects; i++) {
ebpfObj = &ebpfConfig->objects[i];
uint32_t minVersion = combineKernelVersion(ebpfObj->minKernel.major, ebpfObj->minKernel.minor);
uint32_t ltVersion = combineKernelVersion(ebpfObj->lessthanKernel.major, ebpfObj->lessthanKernel.minor);
if (combinedKernelVersion >= minVersion &&
(ltVersion == 0 || combinedKernelVersion < ltVersion)) {
filename = ebpfObj->filename;
break;
}
}
if (filename == NULL) {
logMessage("Kernel version not supported\n");
return NULL;
}
// discover path
for (i=0; i<ebpfConfig->numDefaultPaths; i++) {
snprintf(filepath, size, "%s/%s", ebpfConfig->defaultPaths[i], filename);
if (stat(filepath, &filepathStat) == 0 && S_ISREG(filepathStat.st_mode)) {
return ebpfObj;
}
}
logMessage("Cannot locate EBPF kernel object: %s\n", filename);
return NULL;
}
//--------------------------------------------------------------------
//
// locateTPprogs
//
// Build arrays of enter and exit syscall tracepoint programs, with
// space for links.
//
//--------------------------------------------------------------------
bool locateTPprogs(const ebpfTelemetryObject *obj)
{
if (obj == NULL) {
logMessage("locateTPprogs invalid params\n");
return false;
}
unsigned int i;
bpfSysEnter = (ebpfTPprogs *)calloc(sizeof(ebpfTPprogs), numSysEnter);
bpfSysExit = (ebpfTPprogs *)calloc(sizeof(ebpfTPprogs), numSysExit);
if (bpfSysEnter == NULL || bpfSysExit == NULL) {
if (bpfSysEnter) {
free (bpfSysEnter);
bpfSysEnter = NULL;
}
if (bpfSysExit) {
free (bpfSysExit);
bpfSysExit = NULL;
}
logMessage("Cannot calloc\n");
return false;
}
// sys_enter tracepoints
for (i=0; i<numSysEnter; i++) {
const ebpfSyscallTPprog *p = &obj->syscallTPenterProgs[i];
ebpfTPprogs *s = &bpfSysEnter[i];
if (p->syscall == EBPF_GENERIC_SYSCALL) {
// attach this to all active syscall enter tracepoints
s->numProgs = NUM_ARGS + 1;
s->numLinks = SYSCALL_MAX + 1;
} else {
// attach this to specified enter tracepoint
s->numProgs = 1;
s->numLinks = 1;
}
s->prog = (struct bpf_program **)calloc(sizeof(struct bpf_program *), s->numProgs);
s->link = (struct bpf_link **)calloc(sizeof(struct bpf_link *), s->numLinks);
if (s->prog == NULL || s->link == NULL) {
if (s->prog) {
free( s->prog);
s->prog = NULL;
}
if (s->link) {
free( s->link);
s->link = NULL;
}
logMessage("Cannot calloc\n");
return false;
}
if (p->syscall == EBPF_GENERIC_SYSCALL) {
// attach this to all active syscall enter tracepoints
char *programName = strdup(p->program);
unsigned int programNameLen = strlen(programName);
unsigned int n = 0;
for (n=0; n<7; n++) {
programName[programNameLen - 1] = '0' + n;
if ((s->prog[n] = bpf_object__find_program_by_name(bpfObj, programName)) == NULL) {
logMessage("ERROR: failed to find program: '%s' '%s'\n", programName, strerror(errno));
free(programName);
return false;
}
bpf_program__set_type(s->prog[n], BPF_PROG_TYPE_TRACEPOINT);
}
free(programName);
} else {
// attach this to specified enter tracepoint
if ((s->prog[0] = bpf_object__find_program_by_name(bpfObj, p->program)) == NULL) {
logMessage("ERROR: failed to find program: '%s' '%s'\n", p->program, strerror(errno));
return false;
}
bpf_program__set_type(s->prog[0], BPF_PROG_TYPE_TRACEPOINT);
}
}
// sys_exit tracepoints
for (i=0; i<numSysExit; i++) {
const ebpfSyscallTPprog *p = &obj->syscallTPexitProgs[i];
ebpfTPprogs *s = &bpfSysExit[i];
s->numProgs = 1; // all exit programs have same number of arguments
if (p->syscall == EBPF_GENERIC_SYSCALL) {
// attach this to all active syscall exit tracepoints
s->numLinks = SYSCALL_MAX + 1;
} else {
// attach this to specified exit tracepoint
s->numLinks = 1;
}
s->prog = (struct bpf_program **)calloc(sizeof(struct bpf_program *), s->numProgs);
s->link = (struct bpf_link **)calloc(sizeof(struct bpf_link *), s->numLinks);
if (s->prog == NULL || s->link == NULL) {
if (s->prog) {
free( s->prog);
s->prog = NULL;
}
if (s->link) {
free( s->link);
s->link = NULL;
}
logMessage("Cannot calloc\n");
return false;
}
if ((s->prog[0] = bpf_object__find_program_by_name(bpfObj, p->program)) == NULL) {
logMessage("ERROR: failed to find program: '%s' '%s'\n", p->program, strerror(errno));
return false;
}
bpf_program__set_type(s->prog[0], BPF_PROG_TYPE_TRACEPOINT);
}
return true;
}
//--------------------------------------------------------------------
//
// locateRTPprogs
//
// Build arrays for enter and exit raw syscall tracepoint programs and
// links.
//
//--------------------------------------------------------------------
bool locateRTPprogs(const ebpfTelemetryObject *obj)
{
if (obj == NULL) {
logMessage("locateRTPprogs invalid params\n");
return false;
}
unsigned int i;
bpfRawSysEnter = (struct bpf_program **)calloc(sizeof(struct bpf_program *), numRawSysEnter);
bpfRawSysEnterLink = (struct bpf_link **)calloc(sizeof(struct bpf_link *), numRawSysEnter);
bpfRawSysExit = (struct bpf_program **)calloc(sizeof(struct bpf_program *), numRawSysExit);
bpfRawSysExitLink = (struct bpf_link **)calloc(sizeof(struct bpf_link *), numRawSysExit);
if (bpfRawSysEnter == NULL || bpfRawSysEnterLink == NULL ||
bpfRawSysExit == NULL || bpfRawSysExitLink == NULL) {
if (bpfRawSysEnter) {
free( bpfRawSysEnter );
bpfRawSysEnter = NULL;
}
if (bpfRawSysEnterLink) {
free( bpfRawSysEnterLink );
bpfRawSysEnterLink = NULL;
}
if (bpfRawSysExit) {
free( bpfRawSysExit );
bpfRawSysExit = NULL;
}
if (bpfRawSysExitLink) {
free( bpfRawSysExitLink );
bpfRawSysExitLink = NULL;
}
logMessage("Cannot calloc\n");
return false;
}
// raw sys_enter tracepoint
for (i=0; i<numRawSysEnter; i++) {
const ebpfSyscallRTPprog *p = &obj->syscallRTPenterProgs[i];
if ((bpfRawSysEnter[i] = bpf_object__find_program_by_name(bpfObj, p->program)) == NULL) {
logMessage("ERROR: failed to find program: '%s' '%s'\n", p->program, strerror(errno));
return false;
}
bpf_program__set_type(bpfRawSysEnter[i], BPF_PROG_TYPE_RAW_TRACEPOINT);
}
for (i=0; i<numRawSysExit; i++) {
const ebpfSyscallRTPprog *p = &obj->syscallRTPexitProgs[i];
if ((bpfRawSysExit[i] = bpf_object__find_program_by_name(bpfObj, p->program)) == NULL) {
logMessage("ERROR: failed to find program: '%s' '%s'\n", p->program, strerror(errno));
return false;
}
bpf_program__set_type(bpfRawSysExit[i], BPF_PROG_TYPE_RAW_TRACEPOINT);
}
return true;
}
//--------------------------------------------------------------------
//
// locateOtherTPprogs
//
// Build arrays for non-syscall tracepoint programs and links.
//
//--------------------------------------------------------------------
bool locateOtherTPprogs(const ebpfTelemetryObject *obj)
{
if (obj == NULL) {
logMessage("locateOtherTPprogs invalid params\n");
return false;
}
unsigned int i;
bpfOtherTp = (struct bpf_program **)calloc(sizeof(struct bpf_program *), numOtherTp);
bpfOtherTpLink = (struct bpf_link **)calloc(sizeof(struct bpf_link *), numOtherTp);
if (bpfOtherTp == NULL || bpfOtherTpLink == NULL) {
if (bpfOtherTp) {
free( bpfOtherTp );
bpfOtherTp = NULL;
}
if (bpfOtherTpLink) {
free( bpfOtherTpLink );
bpfOtherTpLink = NULL;
}
logMessage("Cannot calloc\n");
return false;
}
for (i=0; i<numOtherTp; i++) {
const ebpfTracepointProg *p = &obj->otherTPprogs[i];
if ((bpfOtherTp[i] = bpf_object__find_program_by_name(bpfObj, p->program)) == NULL) {
logMessage("ERROR: failed to find program: '%s' '%s'\n", p->program, strerror(errno));
return false;
}
bpf_program__set_type(bpfOtherTp[i], BPF_PROG_TYPE_TRACEPOINT);
}
return true;
}
//--------------------------------------------------------------------
//
// populateConfig
//
// Initialise the eBPF config, including locating suitable kernel
// offsets.
//
//--------------------------------------------------------------------
bool populateConfig(ebpfConfig *config,
const ebpfTelemetryObject *obj, const char *argv[],
time_t procStartTime)
{
if (config == NULL || obj == NULL || argv == NULL) {
logMessage("populateConfig invalid params\n");
return false;
}
config->userlandPid = getpid();
config->bootNsSinceEpoch = g_bootSecSinceEpoch * (1000 * 1000 * 1000);
memcpy(config->active, obj->activeSyscalls, sizeof(config->active));
if (!populateConfigOffsets(config, argv, procStartTime)) {
logMessage("Could not automatically discover kernel offsets.\n");
logMessage("Build and run the get_offsets module to generate the offsets config file:\n");
logMessage("/opt/sysinternalsEBPF/sysinternalsEBPF_offsets.conf\n\n");
return false;
}
return true;
}
//--------------------------------------------------------------------
//
// linkTPprogs
//
// Link the required tracepoint programs based on the activeSyscalls
// config.
//
//--------------------------------------------------------------------
bool linkTPprogs(const ebpfTelemetryObject *obj,
const bool *activeSyscalls)
{
if (obj == NULL || activeSyscalls == NULL) {
logMessage("linkTPprogs invalid params\n");
return false;
}
unsigned int i;
char tp[SYSCALL_NAME_LEN * 2];
unsigned int syscall = 0;
for (i=0; i<numSysEnter; i++) {
const ebpfSyscallTPprog *p = &obj->syscallTPenterProgs[i];
ebpfTPprogs *s = &bpfSysEnter[i];
memset(s->link, 0, sizeof(struct bpf_link *) * s->numLinks);
if (p->syscall == EBPF_GENERIC_SYSCALL) {
// attach this to all active syscall enter tracepoints
for (syscall=0; syscall<=SYSCALL_MAX; syscall++) {
if (activeSyscalls[syscall]) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"
snprintf(tp, SYSCALL_NAME_LEN * 2, "sys_enter_%s", syscallNumToName[syscall].name);
#pragma GCC diagnostic pop
unsigned int numArgs = syscallNumToName[syscall].numArgs;
s->link[syscall] = bpf_program__attach_tracepoint(s->prog[numArgs], "syscalls", tp);
if (libbpf_get_error(s->link[syscall]))
return false;
}
}
} else if (activeSyscalls[p->syscall]) {
snprintf(tp, SYSCALL_NAME_LEN * 2, "sys_enter_%s", syscallNumToName[p->syscall].name);
s->link[0] = bpf_program__attach_tracepoint(s->prog[0], "syscalls", tp);
if (libbpf_get_error(s->link[0]))
return false;
}
}