-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfillers.h
7063 lines (5988 loc) · 156 KB
/
fillers.h
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) 2021 The Falco Authors.
This file is dual licensed under either the MIT or GPL 2. See MIT.txt
or GPL2.txt for full copies of the license.
*/
#ifndef __FILLERS_H
#define __FILLERS_H
#include "../systype_compat.h"
#include "../ppm_flag_helpers.h"
#include "../ppm_version.h"
#include "bpf_helpers.h"
#include <linux/tty.h>
#include <linux/audit.h>
/* Linux kernel 4.15 introduced the new const `UID_GID_MAP_MAX_BASE_EXTENTS` in place of
* the old `UID_GID_MAP_MAX_EXTENTS`, which instead has changed its meaning.
* For more info see https://github.com/torvalds/linux/commit/6397fac4915ab3002dc15aae751455da1a852f25
*/
#ifndef UID_GID_MAP_MAX_BASE_EXTENTS
#define UID_GID_MAP_MAX_BASE_EXTENTS 5
#endif
/*
* Linux 5.6 kernels no longer include the old 32-bit timeval
* structures. But the syscalls (might) still use them.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
#include <linux/time64.h>
struct compat_timespec {
int32_t tv_sec;
int32_t tv_nsec;
};
struct timespec {
int32_t tv_sec;
int32_t tv_nsec;
};
struct timeval {
int32_t tv_sec;
int32_t tv_usec;
};
#else
#define timeval64 timeval
#endif
#define FILLER_RAW(x) \
static __always_inline int __bpf_##x(struct filler_data *data); \
\
__bpf_section(TP_NAME "filler/" #x) \
static __always_inline int bpf_##x(void *ctx) \
#define FILLER(x, is_syscall) \
static __always_inline int __bpf_##x(struct filler_data *data); \
\
__bpf_section(TP_NAME "filler/" #x) \
static __always_inline int bpf_##x(void *ctx) \
{ \
struct filler_data data = {0}; \
int res; \
\
res = init_filler_data(ctx, &data, is_syscall); \
if (res == PPM_SUCCESS) { \
if (!data.state->tail_ctx.len) \
write_evt_hdr(&data); \
res = __bpf_##x(&data); \
} \
\
if (res == PPM_SUCCESS) \
res = push_evt_frame(ctx, &data); \
\
if (data.state) \
data.state->tail_ctx.prev_res = res; \
\
bpf_tail_call(ctx, &tail_map, PPM_FILLER_terminate_filler); \
bpf_printk("Can't tail call terminate filler\n"); \
return 0; \
} \
\
static __always_inline int __bpf_##x(struct filler_data *data) \
FILLER_RAW(terminate_filler)
{
struct scap_bpf_per_cpu_state *state;
state = get_local_state(bpf_get_smp_processor_id());
if (!state)
return 0;
switch (state->tail_ctx.prev_res) {
case PPM_SUCCESS:
break;
case PPM_FAILURE_BUFFER_FULL:
bpf_printk("PPM_FAILURE_BUFFER_FULL event=%d curarg=%d\n",
state->tail_ctx.evt_type,
state->tail_ctx.curarg);
if (state->n_drops_buffer != ULLONG_MAX) {
++state->n_drops_buffer;
}
switch (state->tail_ctx.evt_type) {
// enter
case PPME_SYSCALL_OPEN_E:
case PPME_SYSCALL_CREAT_E:
case PPME_SYSCALL_OPENAT_E:
case PPME_SYSCALL_OPENAT_2_E:
case PPME_SYSCALL_OPENAT2_E:
case PPME_SYSCALL_OPEN_BY_HANDLE_AT_E:
if (state->n_drops_buffer_open_enter != ULLONG_MAX) {
++state->n_drops_buffer_open_enter;
}
break;
case PPME_SYSCALL_DUP_E:
case PPME_SYSCALL_CHMOD_E:
case PPME_SYSCALL_FCHMOD_E:
case PPME_SYSCALL_FCHMODAT_E:
case PPME_SYSCALL_CHOWN_E:
case PPME_SYSCALL_LCHOWN_E:
case PPME_SYSCALL_FCHOWN_E:
case PPME_SYSCALL_FCHOWNAT_E:
case PPME_SYSCALL_LINK_E:
case PPME_SYSCALL_LINK_2_E:
case PPME_SYSCALL_LINKAT_E:
case PPME_SYSCALL_LINKAT_2_E:
case PPME_SYSCALL_MKDIR_E:
case PPME_SYSCALL_MKDIR_2_E:
case PPME_SYSCALL_MKDIRAT_E:
case PPME_SYSCALL_MOUNT_E:
case PPME_SYSCALL_UMOUNT_E:
case PPME_SYSCALL_UMOUNT_1_E:
case PPME_SYSCALL_RENAME_E:
case PPME_SYSCALL_RENAMEAT_E:
case PPME_SYSCALL_RENAMEAT2_E:
case PPME_SYSCALL_RMDIR_E:
case PPME_SYSCALL_RMDIR_2_E:
case PPME_SYSCALL_SYMLINK_E:
case PPME_SYSCALL_SYMLINKAT_E:
case PPME_SYSCALL_UNLINK_E:
case PPME_SYSCALL_UNLINK_2_E:
case PPME_SYSCALL_UNLINKAT_E:
case PPME_SYSCALL_UNLINKAT_2_E:
if (state->n_drops_buffer_dir_file_enter != ULLONG_MAX) {
++state->n_drops_buffer_dir_file_enter;
}
break;
case PPME_SYSCALL_CLONE_11_E:
case PPME_SYSCALL_CLONE_16_E:
case PPME_SYSCALL_CLONE_17_E:
case PPME_SYSCALL_CLONE_20_E:
case PPME_SYSCALL_CLONE3_E:
case PPME_SYSCALL_FORK_E:
case PPME_SYSCALL_FORK_20_E:
case PPME_SYSCALL_VFORK_E:
case PPME_SYSCALL_VFORK_20_E:
if (state->n_drops_buffer_clone_fork_enter != ULLONG_MAX) {
++state->n_drops_buffer_clone_fork_enter;
}
break;
case PPME_SYSCALL_EXECVE_19_E:
case PPME_SYSCALL_EXECVEAT_E:
if (state->n_drops_buffer_execve_enter != ULLONG_MAX) {
++state->n_drops_buffer_execve_enter;
}
break;
case PPME_SOCKET_CONNECT_E:
if (state->n_drops_buffer_connect_enter != ULLONG_MAX) {
++state->n_drops_buffer_connect_enter;
}
break;
case PPME_SYSCALL_BPF_E:
case PPME_SYSCALL_BPF_2_E:
case PPME_SYSCALL_SETPGID_E:
case PPME_SYSCALL_PTRACE_E:
case PPME_SYSCALL_SECCOMP_E:
case PPME_SYSCALL_SETNS_E:
case PPME_SYSCALL_SETRESGID_E:
case PPME_SYSCALL_SETRESUID_E:
case PPME_SYSCALL_SETSID_E:
case PPME_SYSCALL_UNSHARE_E:
case PPME_SYSCALL_CAPSET_E:
if (state->n_drops_buffer_other_interest_enter != ULLONG_MAX) {
++state->n_drops_buffer_other_interest_enter;
}
break;
// exit
case PPME_SYSCALL_OPEN_X:
case PPME_SYSCALL_CREAT_X:
case PPME_SYSCALL_OPENAT_X:
case PPME_SYSCALL_OPENAT_2_X:
case PPME_SYSCALL_OPENAT2_X:
case PPME_SYSCALL_OPEN_BY_HANDLE_AT_X:
if (state->n_drops_buffer_open_exit != ULLONG_MAX) {
++state->n_drops_buffer_open_exit;
}
break;
case PPME_SYSCALL_DUP_X:
case PPME_SYSCALL_CHMOD_X:
case PPME_SYSCALL_FCHMOD_X:
case PPME_SYSCALL_FCHMODAT_X:
case PPME_SYSCALL_CHOWN_X:
case PPME_SYSCALL_LCHOWN_X:
case PPME_SYSCALL_FCHOWN_X:
case PPME_SYSCALL_FCHOWNAT_X:
case PPME_SYSCALL_LINK_X:
case PPME_SYSCALL_LINK_2_X:
case PPME_SYSCALL_LINKAT_X:
case PPME_SYSCALL_LINKAT_2_X:
case PPME_SYSCALL_MKDIR_X:
case PPME_SYSCALL_MKDIR_2_X:
case PPME_SYSCALL_MKDIRAT_X:
case PPME_SYSCALL_MOUNT_X:
case PPME_SYSCALL_UMOUNT_X:
case PPME_SYSCALL_UMOUNT_1_X:
case PPME_SYSCALL_RENAME_X:
case PPME_SYSCALL_RENAMEAT_X:
case PPME_SYSCALL_RENAMEAT2_X:
case PPME_SYSCALL_RMDIR_X:
case PPME_SYSCALL_RMDIR_2_X:
case PPME_SYSCALL_SYMLINK_X:
case PPME_SYSCALL_SYMLINKAT_X:
case PPME_SYSCALL_UNLINK_X:
case PPME_SYSCALL_UNLINK_2_X:
case PPME_SYSCALL_UNLINKAT_X:
case PPME_SYSCALL_UNLINKAT_2_X:
if (state->n_drops_buffer_dir_file_exit != ULLONG_MAX) {
++state->n_drops_buffer_dir_file_exit;
}
break;
case PPME_SYSCALL_CLONE_11_X:
case PPME_SYSCALL_CLONE_16_X:
case PPME_SYSCALL_CLONE_17_X:
case PPME_SYSCALL_CLONE_20_X:
case PPME_SYSCALL_CLONE3_X:
case PPME_SYSCALL_FORK_X:
case PPME_SYSCALL_FORK_20_X:
case PPME_SYSCALL_VFORK_X:
case PPME_SYSCALL_VFORK_20_X:
if (state->n_drops_buffer_clone_fork_exit != ULLONG_MAX) {
++state->n_drops_buffer_clone_fork_exit;
}
break;
case PPME_SYSCALL_EXECVE_19_X:
case PPME_SYSCALL_EXECVEAT_X:
if (state->n_drops_buffer_execve_exit != ULLONG_MAX) {
++state->n_drops_buffer_execve_exit;
}
break;
case PPME_SOCKET_CONNECT_X:
if (state->n_drops_buffer_connect_exit != ULLONG_MAX) {
++state->n_drops_buffer_connect_exit;
}
break;
case PPME_SYSCALL_BPF_X:
case PPME_SYSCALL_BPF_2_X:
case PPME_SYSCALL_SETPGID_X:
case PPME_SYSCALL_PTRACE_X:
case PPME_SYSCALL_SECCOMP_X:
case PPME_SYSCALL_SETNS_X:
case PPME_SYSCALL_SETRESGID_X:
case PPME_SYSCALL_SETRESUID_X:
case PPME_SYSCALL_SETSID_X:
case PPME_SYSCALL_UNSHARE_X:
case PPME_SYSCALL_CAPSET_X:
if (state->n_drops_buffer_other_interest_exit != ULLONG_MAX) {
++state->n_drops_buffer_other_interest_exit;
}
break;
default:
break;
}
break;
case PPM_FAILURE_INVALID_USER_MEMORY:
bpf_printk("PPM_FAILURE_INVALID_USER_MEMORY event=%d curarg=%d\n",
state->tail_ctx.evt_type,
state->tail_ctx.curarg);
if (state->n_drops_pf != ULLONG_MAX) {
++state->n_drops_pf;
}
break;
case PPM_FAILURE_BUG:
bpf_printk("PPM_FAILURE_BUG event=%d curarg=%d\n",
state->tail_ctx.evt_type,
state->tail_ctx.curarg);
if (state->n_drops_bug != ULLONG_MAX) {
++state->n_drops_bug;
}
break;
case PPM_SKIP_EVENT:
break;
case PPM_FAILURE_FRAME_SCRATCH_MAP_FULL:
bpf_printk("PPM_FAILURE_FRAME_SCRATCH_MAP_FULL event=%d curarg=%d\n",
state->tail_ctx.evt_type,
state->tail_ctx.curarg);
if (state->n_drops_scratch_map != ULLONG_MAX) {
++state->n_drops_scratch_map;
}
break;
default:
bpf_printk("Unknown filler res=%d event=%d curarg=%d\n",
state->tail_ctx.prev_res,
state->tail_ctx.evt_type,
state->tail_ctx.curarg);
break;
}
release_local_state(state);
#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS
return 1;
#else
return 0;
#endif // BPF_SUPPORTS_RAW_TRACEPOINTS
}
FILLER(sys_empty, true)
{
return PPM_SUCCESS;
}
FILLER(sys_single, true)
{
unsigned long val;
int res;
val = bpf_syscall_get_argument(data, 0);
res = bpf_val_to_ring(data, val);
return res;
}
FILLER(sys_single_x, true)
{
int res;
long retval;
retval = bpf_syscall_get_retval(data->ctx);
res = bpf_val_to_ring(data, retval);
return res;
}
FILLER(sys_open_e, true)
{
unsigned long flags;
unsigned long val;
unsigned long mode;
int res;
/*
* name
*/
val = bpf_syscall_get_argument(data, 0);
res = bpf_val_to_ring(data, val);
if (res != PPM_SUCCESS)
return res;
/*
* Flags
* Note that we convert them into the ppm portable representation before pushing them to the ring
*/
val = bpf_syscall_get_argument(data, 1);
flags = open_flags_to_scap(val);
res = bpf_val_to_ring(data, flags);
if (res != PPM_SUCCESS)
return res;
/*
* mode
*/
mode = bpf_syscall_get_argument(data, 2);
mode = open_modes_to_scap(val, mode);
res = bpf_val_to_ring(data, mode);
if (res != PPM_SUCCESS)
return res;
return res;
}
FILLER(sys_open_x, true)
{
unsigned int flags;
unsigned int mode;
unsigned long val;
unsigned long dev = 0;
unsigned long ino = 0;
long retval;
int res;
/*
* fd
*/
retval = bpf_syscall_get_retval(data->ctx);
res = bpf_val_to_ring(data, retval);
if (res != PPM_SUCCESS)
return res;
/*
* Name
*/
val = bpf_syscall_get_argument(data, 0);
res = bpf_val_to_ring(data, val);
if (res != PPM_SUCCESS)
return res;
/*
* Flags
*/
val = bpf_syscall_get_argument(data, 1);
flags = open_flags_to_scap(val);
res = bpf_val_to_ring(data, flags);
if (res != PPM_SUCCESS)
return res;
/*
* Mode
*/
mode = bpf_syscall_get_argument(data, 2);
mode = open_modes_to_scap(val, mode);
res = bpf_val_to_ring(data, mode);
if (res != PPM_SUCCESS)
return res;
bpf_get_fd_dev_ino(retval, &dev, &ino);
/*
* Device
*/
res = bpf_val_to_ring(data, dev);
if (res != PPM_SUCCESS)
return res;
/*
* Ino
*/
res = bpf_val_to_ring(data, ino);
return res;
}
FILLER(sys_read_e, true)
{
/* Parameter 1: fd (type: PT_FD) */
s32 fd = (s32)bpf_syscall_get_argument(data, 0);
int res = bpf_push_s64_to_ring(data, (s64)fd);
CHECK_RES(res);
/* Parameter 2: size (type: PT_UINT32) */
size_t size = bpf_syscall_get_argument(data, 2);
return bpf_val_to_ring(data, size);
}
FILLER(sys_read_x, true)
{
unsigned long bufsize;
unsigned long val;
long retval;
int res;
/*
* res
*/
retval = bpf_syscall_get_retval(data->ctx);
res = bpf_val_to_ring(data, retval);
if (res != PPM_SUCCESS)
return res;
if (retval < 0) {
val = 0;
bufsize = 0;
} else {
val = bpf_syscall_get_argument(data, 1);
bufsize = retval;
}
/*
* data
*/
data->fd = bpf_syscall_get_argument(data, 0);
res = __bpf_val_to_ring(data, val, bufsize, PT_BYTEBUF, -1, true, USER);
return res;
}
FILLER(sys_write_e, true)
{
/* Parameter 1: fd (type: PT_FD) */
s32 fd = (s32)bpf_syscall_get_argument(data, 0);
int res = bpf_push_s64_to_ring(data, (s64)fd);
CHECK_RES(res);
/* Parameter 2: size (type: PT_UINT32) */
size_t size = bpf_syscall_get_argument(data, 2);
return bpf_val_to_ring(data, size);
}
FILLER(sys_write_x, true)
{
unsigned long bufsize;
unsigned long val;
long retval;
int res;
/* Parameter 1: res (type: PT_ERRNO) */
retval = bpf_syscall_get_retval(data->ctx);
res = bpf_val_to_ring(data, retval);
if (res != PPM_SUCCESS)
return res;
/* If the syscall fails we are not able to collect reliable params
* so we return empty ones.
*/
if(retval < 0)
{
/* Parameter 2: data (type: PT_BYTEBUF) */
return bpf_push_empty_param(data);
}
/* Parameter 2: data (type: PT_BYTEBUF) */
data->fd = bpf_syscall_get_argument(data, 0);
val = bpf_syscall_get_argument(data, 1);
bufsize = bpf_syscall_get_argument(data, 2);
res = __bpf_val_to_ring(data, val, bufsize, PT_BYTEBUF, -1, true, USER);
return res;
}
#define POLL_MAXFDS 16
static __always_inline int bpf_poll_parse_fds(struct filler_data *data,
bool enter_event)
{
unsigned int read_size;
unsigned int fds_count;
int res = PPM_SUCCESS;
unsigned long nfds;
struct pollfd *fds;
unsigned long val;
unsigned long off;
int j;
nfds = bpf_syscall_get_argument(data, 1);
fds = (struct pollfd *)data->tmp_scratch;
read_size = nfds * sizeof(struct pollfd);
if (read_size > SCRATCH_SIZE_MAX)
{
return PPM_FAILURE_FRAME_SCRATCH_MAP_FULL;
}
val = bpf_syscall_get_argument(data, 0);
/* We don't want to discard the whole event if the pointer is null.
* Setting `nfds = 0` we will just push to userspace the number of fds read,
* in this case `0`.
*/
#ifdef BPF_FORBIDS_ZERO_ACCESS
if (read_size)
if (bpf_probe_read_user(fds,
((read_size - 1) & SCRATCH_SIZE_MAX) + 1,
(void *)val))
#else
if (bpf_probe_read_user(fds, read_size & SCRATCH_SIZE_MAX, (void *)val))
#endif
nfds = 0;
if (data->state->tail_ctx.curoff > SCRATCH_SIZE_HALF)
{
return PPM_FAILURE_FRAME_SCRATCH_MAP_FULL;
}
/* `data->state->tail_ctx.curoff` will always point to the beginning of the space
* in this way we can fill `nfds` after the for loop.
*/
off = data->state->tail_ctx.curoff + sizeof(u16);
fds_count = 0;
#pragma unroll
for (j = 0; j < POLL_MAXFDS; ++j) {
if (off > SCRATCH_SIZE_HALF)
{
return PPM_FAILURE_FRAME_SCRATCH_MAP_FULL;
}
if (j == nfds)
break;
u16 flags;
if (enter_event) {
flags = poll_events_to_scap(fds[j].events);
} else {
flags = poll_events_to_scap(fds[j].revents);
}
*(s64 *)&data->buf[off & SCRATCH_SIZE_HALF] = (s64)fds[j].fd;
off += sizeof(s64);
if (off > SCRATCH_SIZE_HALF)
{
return PPM_FAILURE_FRAME_SCRATCH_MAP_FULL;
}
*(s16 *)&data->buf[off & SCRATCH_SIZE_HALF] = flags;
off += sizeof(s16);
++fds_count;
}
*((u16 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = fds_count;
data->curarg_already_on_frame = true;
return __bpf_val_to_ring(data, 0, off - data->state->tail_ctx.curoff, PT_FDLIST, -1, false, KERNEL);
}
FILLER(sys_poll_e, true)
{
/* Parameter 1: fds (type: PT_FDLIST) */
int res = bpf_poll_parse_fds(data, true);
CHECK_RES(res);
/* Parameter 2: timeout (type: PT_INT64) */
u32 timeout_msecs = (s32)bpf_syscall_get_argument(data, 2);
return bpf_val_to_ring(data, timeout_msecs);
}
FILLER(sys_poll_x, true)
{
/* Parameter 1: ret (type: PT_FD) */
long retval = bpf_syscall_get_retval(data->ctx);
int res = bpf_val_to_ring_type(data, retval, PT_ERRNO);
CHECK_RES(res);
/* Parameter 2: fds (type: PT_FDLIST) */
return bpf_poll_parse_fds(data, false);
}
#define MAX_IOVCNT 32
static __always_inline int bpf_parse_readv_writev_bufs(struct filler_data *data,
const struct iovec __user *iovsrc,
unsigned long iovcnt,
long retval,
int flags)
{
const struct iovec *iov;
int res = PPM_SUCCESS;
unsigned int copylen;
long size = 0;
int j;
copylen = iovcnt * sizeof(struct iovec);
iov = (const struct iovec *)data->tmp_scratch;
if (copylen > SCRATCH_SIZE_MAX)
{
return PPM_FAILURE_FRAME_SCRATCH_MAP_FULL;
}
#ifdef BPF_FORBIDS_ZERO_ACCESS
if (copylen)
if (bpf_probe_read_user((void *)iov,
((copylen - 1) & SCRATCH_SIZE_MAX) + 1,
(void *)iovsrc))
#else
if (bpf_probe_read_user((void *)iov,
copylen & SCRATCH_SIZE_MAX,
(void *)iovsrc))
#endif
return PPM_FAILURE_INVALID_USER_MEMORY;
#pragma unroll
for (j = 0; j < MAX_IOVCNT; ++j) {
if (j == iovcnt)
break;
// BPF seems to require a hard limit to avoid overflows
if (size == LONG_MAX)
break;
size += iov[j].iov_len;
}
if ((flags & PRB_FLAG_IS_WRITE) == 0)
if (size > retval)
size = retval;
if (flags & PRB_FLAG_PUSH_SIZE) {
res = bpf_val_to_ring_type(data, size, PT_UINT32);
if (res != PPM_SUCCESS)
return res;
}
if (flags & PRB_FLAG_PUSH_DATA) {
if (size > 0) {
unsigned long off = _READ(data->state->tail_ctx.curoff);
unsigned long remaining = size;
int j;
#pragma unroll
for (j = 0; j < MAX_IOVCNT; ++j) {
volatile unsigned int to_read;
if (j == iovcnt)
break;
unsigned long off_bounded = off & SCRATCH_SIZE_HALF;
if (off > SCRATCH_SIZE_HALF)
break;
if (iov[j].iov_len <= remaining)
to_read = iov[j].iov_len;
else
to_read = remaining;
if (to_read > SCRATCH_SIZE_HALF)
to_read = SCRATCH_SIZE_HALF;
#ifdef BPF_FORBIDS_ZERO_ACCESS
if (to_read)
if (bpf_probe_read_user(&data->buf[off_bounded],
((to_read - 1) & SCRATCH_SIZE_HALF) + 1,
iov[j].iov_base))
#else
if (bpf_probe_read_user(&data->buf[off_bounded],
to_read & SCRATCH_SIZE_HALF,
iov[j].iov_base))
#endif
return PPM_FAILURE_INVALID_USER_MEMORY;
remaining -= to_read;
off += to_read;
}
} else {
size = 0;
}
data->fd = bpf_syscall_get_argument(data, 0);
data->curarg_already_on_frame = true;
return __bpf_val_to_ring(data, 0, size, PT_BYTEBUF, -1, true, KERNEL);
}
return res;
}
FILLER(sys_readv_preadv_x, true)
{
const struct iovec __user *iov;
unsigned long iovcnt;
long retval;
int res;
/*
* res
*/
retval = bpf_syscall_get_retval(data->ctx);
res = bpf_val_to_ring_type(data, retval, PT_ERRNO);
if (res != PPM_SUCCESS)
return res;
iov = (const struct iovec __user *)bpf_syscall_get_argument(data, 1);
iovcnt = bpf_syscall_get_argument(data, 2);
res = bpf_parse_readv_writev_bufs(data,
iov,
iovcnt,
retval,
PRB_FLAG_PUSH_ALL);
return res;
}
FILLER(sys_writev_e, true)
{
unsigned long iovcnt;
unsigned long val;
int res;
/*
* fd
*/
val = bpf_syscall_get_argument(data, 0);
res = bpf_val_to_ring(data, val);
if (res != PPM_SUCCESS)
return res;
val = bpf_syscall_get_argument(data, 1);
iovcnt = bpf_syscall_get_argument(data, 2);
res = bpf_parse_readv_writev_bufs(data,
(const struct iovec __user *)val,
iovcnt,
0,
PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE);
return res;
}
FILLER(sys_writev_pwritev_x, true)
{
unsigned long iovcnt;
unsigned long val;
long retval;
int res;
/*
* res
*/
retval = bpf_syscall_get_retval(data->ctx);
res = bpf_val_to_ring(data, retval);
if (res != PPM_SUCCESS)
return res;
/*
* data and size
*/
val = bpf_syscall_get_argument(data, 1);
iovcnt = bpf_syscall_get_argument(data, 2);
res = bpf_parse_readv_writev_bufs(data,
(const struct iovec __user *)val,
iovcnt,
0,
PRB_FLAG_PUSH_DATA | PRB_FLAG_IS_WRITE);
return res;
}
static __always_inline int timespec_parse(struct filler_data *data,
unsigned long val)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
struct __kernel_timespec ts = {};
#else
struct timespec ts = {};
#endif
bpf_probe_read_user(&ts, sizeof(ts), (void *)val);
return bpf_val_to_ring_type(data, ((u64)ts.tv_sec) * 1000000000 + ts.tv_nsec, PT_RELTIME);
}
FILLER(sys_nanosleep_e, true)
{
unsigned long val;
int res;
val = bpf_syscall_get_argument(data, 0);
res = timespec_parse(data, val);
return res;
}
FILLER(sys_futex_e, true)
{
unsigned long val;
int res;
/*
* addr
*/
val = bpf_syscall_get_argument(data, 0);
res = bpf_val_to_ring(data, val);
if (res != PPM_SUCCESS)
return res;
/*
* op
*/
val = bpf_syscall_get_argument(data, 1);
res = bpf_val_to_ring(data, futex_op_to_scap(val));
if (res != PPM_SUCCESS)
return res;
/*
* val
*/
val = bpf_syscall_get_argument(data, 2);
res = bpf_val_to_ring(data, val);
return res;
}
static __always_inline unsigned long bpf_get_mm_counter(struct mm_struct *mm,
int member)
{
long val;
// See 6.2 kernel commit: https://github.com/torvalds/linux/commit/f1a7941243c102a44e8847e3b94ff4ff3ec56f25
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
bpf_probe_read_kernel(&val, sizeof(val), &mm->rss_stat.count[member]);
#else
bpf_probe_read_kernel(&val, sizeof(val), &mm->rss_stat[member].count);
#endif
if (val < 0)
val = 0;
return (unsigned long)val;
}
static __always_inline unsigned long bpf_get_mm_rss(struct mm_struct *mm)
{
return bpf_get_mm_counter(mm, MM_FILEPAGES) +
bpf_get_mm_counter(mm, MM_ANONPAGES) +
bpf_get_mm_counter(mm, MM_SHMEMPAGES);
}
static __always_inline unsigned long bpf_get_mm_swap(struct mm_struct *mm)
{
return bpf_get_mm_counter(mm, MM_SWAPENTS);
}
FILLER(sys_brk_munmap_mmap_x, true)
{
struct task_struct *task;
unsigned long total_vm = 0;
struct mm_struct *mm;
long total_rss = 0;
long swap = 0;
long retval;
int res;
task = (struct task_struct *)bpf_get_current_task();
mm = NULL;
bpf_probe_read_kernel(&mm, sizeof(mm), &task->mm);
retval = bpf_syscall_get_retval(data->ctx);
res = bpf_val_to_ring_type(data, retval, PT_UINT64);
if (res != PPM_SUCCESS)
return res;
if (mm) {
total_vm = _READ(mm->total_vm);
total_vm <<= (PAGE_SHIFT - 10);
total_rss = bpf_get_mm_rss(mm) << (PAGE_SHIFT - 10);
swap = bpf_get_mm_swap(mm) << (PAGE_SHIFT - 10);
}
/*
* vm_size
*/
res = bpf_val_to_ring_type(data, total_vm, PT_UINT32);
if (res != PPM_SUCCESS)
return res;
/*
* vm_rss
*/
res = bpf_val_to_ring_type(data, total_rss, PT_UINT32);
if (res != PPM_SUCCESS)
return res;
/*
* vm_swap
*/
res = bpf_val_to_ring_type(data, swap, PT_UINT32);
return res;
}
FILLER(sys_mmap_e, true)
{
unsigned long val;
int res;
/*
* addr
*/
val = bpf_syscall_get_argument(data, 0);
res = bpf_push_u64_to_ring(data, val);
if (res != PPM_SUCCESS)
return res;
/*
* length
*/
val = bpf_syscall_get_argument(data, 1);
res = bpf_push_u64_to_ring(data, val);
if (res != PPM_SUCCESS)
return res;
/*
* prot
*/
val = bpf_syscall_get_argument(data, 2);
res = bpf_push_u32_to_ring(data, prot_flags_to_scap(val));
if (res != PPM_SUCCESS)
return res;
/*
* flags
*/
val = bpf_syscall_get_argument(data, 3);
res = bpf_push_u32_to_ring(data, mmap_flags_to_scap(val));
if (res != PPM_SUCCESS)
return res;
/*
* fd
*/
s32 fd = (s32)bpf_syscall_get_argument(data, 4);
res = bpf_push_s64_to_ring(data, (s64)fd);
if (res != PPM_SUCCESS)
return res;