-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathbasic.h
2704 lines (2386 loc) · 67.2 KB
/
basic.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
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright Authors of Cilium */
#ifndef __BASIC_H__
#define __BASIC_H__
#include "operations.h"
#include "bpf_task.h"
#include "bpf_cred.h"
#include "skb.h"
#include "sock.h"
#include "net_device.h"
#include "../bpf_process_event.h"
#include "bpfattr.h"
#include "perfevent.h"
#include "bpfmap.h"
#include "capabilities.h"
#include "module.h"
#include "../argfilter_maps.h"
#include "../addr_lpm_maps.h"
#include "../string_maps.h"
#include "common.h"
#include "process/data_event.h"
#include "process/bpf_enforcer.h"
/* Type IDs form API with user space generickprobe.go */
enum {
invalid_ty = -2,
nop_ty = -1,
int_type = 1,
char_buf = 2,
char_iovec = 3,
size_type = 4,
skb_type = 5,
string_type = 6,
sock_type = 7,
cred_type = 8,
s64_ty = 10,
u64_ty = 11,
s32_ty = 12,
u32_ty = 13,
filename_ty = 14,
path_ty = 15,
file_ty = 16,
fd_ty = 17,
/* const_buf_type is a type for buffers with static size that is passed
* in the meta argument
*/
const_buf_type = 18,
bpf_attr_type = 19,
perf_event_type = 20,
bpf_map_type = 21,
user_namespace_type = 22,
capability_type = 23,
kiocb_type = 24,
iov_iter_type = 25,
load_module_type = 26,
kernel_module_type = 27,
syscall64_type = 28,
s16_ty = 29,
u16_ty = 30,
s8_ty = 31,
u8_ty = 32,
kernel_cap_ty = 33,
cap_inh_ty = 34,
cap_prm_ty = 35,
cap_eff_ty = 36,
linux_binprm_type = 37,
data_loc_type = 38,
net_dev_ty = 39,
nop_s64_ty = -10,
nop_u64_ty = -11,
nop_u32_ty = -12,
nop_s32_ty = -13,
};
enum {
char_buf_enomem = -1,
char_buf_pagefault = -2,
char_buf_toolarge = -3,
char_buf_saved_for_retprobe = -4,
};
enum {
ACTION_POST = 0,
ACTION_FOLLOWFD = 1,
/* Actual SIGKILL value, but we dont want to pull headers in */
ACTION_SIGKILL = 2,
ACTION_UNFOLLOWFD = 3,
ACTION_OVERRIDE = 4,
ACTION_COPYFD = 5,
ACTION_GETURL = 6,
ACTION_DNSLOOKUP = 7,
ACTION_NOPOST = 8,
ACTION_SIGNAL = 9,
ACTION_TRACKSOCK = 10,
ACTION_UNTRACKSOCK = 11,
ACTION_NOTIFY_KILLER = 12,
};
enum {
FGS_SIGKILL = 9,
};
enum {
TAIL_CALL_PROCESS = 1,
TAIL_CALL_FILTER = 2,
TAIL_CALL_ARGS = 3,
TAIL_CALL_ACTIONS = 4,
TAIL_CALL_SEND = 5,
};
struct selector_action {
__u32 actionlen;
__u32 act[];
};
struct selector_arg_filter {
__u32 index;
__u32 op;
__u32 vallen;
__u32 type;
__u8 value;
} __attribute__((packed));
struct selector_arg_filters {
__u32 arglen;
__u32 argoff[5];
} __attribute__((packed));
#define IS_32BIT 0x80000000
struct event_config {
__u32 func_id;
__s32 arg0;
__s32 arg1;
__s32 arg2;
__s32 arg3;
__s32 arg4;
__u32 arg0m;
__u32 arg1m;
__u32 arg2m;
__u32 arg3m;
__u32 arg4m;
__u32 t_arg0_ctx_off;
__u32 t_arg1_ctx_off;
__u32 t_arg2_ctx_off;
__u32 t_arg3_ctx_off;
__u32 t_arg4_ctx_off;
__u32 syscall;
__s32 argreturncopy;
__s32 argreturn;
/* arg return action specifies to act on the return value; currently
* supported actions include: TrackSock and UntrackSock.
*/
__u32 argreturnaction;
/* policy id identifies the policy of this generic hook and is used to
* apply policies only on certain processes. A value of 0 indicates
* that the hook always applies and no check will be performed.
*/
__u32 policy_id;
__u32 flags;
} __attribute__((packed));
#define MAX_ARGS_SIZE 80
#define MAX_ARGS_ENTRIES 8
#define MAX_MATCH_VALUES 4
/* String parsing consumes instructions so this adds an additional
* knob to tune how many instructions we should spend parsing
* strings.
*/
#define MAX_MATCH_STRING_VALUES 2
/* Number of values allowed in matchArgs while using an "fd" or "file" arg.
*/
#ifdef __LARGE_BPF_PROG
#define MAX_MATCH_FILE_VALUES 8
#else
#define MAX_MATCH_FILE_VALUES 2
#endif
/* Number of allowed actions for selector.
*/
#define MAX_ACTIONS 3
/* Constants bounding printers if these change or buffer size changes then
* we will need to resize. TBD would be to size these at compile time using
* buffer size information.
*/
#ifdef __LARGE_BPF_PROG
#ifdef __LARGE_MAP_KEYS
#define MAX_STRING (STRING_MAPS_SIZE_10 - 2)
#else
#define MAX_STRING (STRING_MAPS_SIZE_7 - 2)
#endif
#else
#define MAX_STRING (STRING_MAPS_SIZE_5 - 1)
#endif
struct msg_linux_binprm {
char path[MAX_STRING];
} __attribute__((packed));
#ifdef __MULTI_KPROBE
static inline __attribute__((always_inline)) __u32 get_index(void *ctx)
{
return (__u32)get_attach_cookie(ctx);
}
#else
#define get_index(ctx) 0
#endif
// We do one tail-call per selector, we can have up to 5 selectors.
#define MAX_SELECTORS 5
#define MAX_SELECTORS_MASK 7
static inline __attribute__((always_inline)) long
filter_32ty_map(struct selector_arg_filter *filter, char *args);
static inline __attribute__((always_inline)) bool ty_is_nop(int ty)
{
switch (ty) {
case nop_ty:
case nop_s64_ty:
case nop_u64_ty:
case nop_s32_ty:
case nop_u32_ty:
return true;
default:
return false;
}
}
static inline __attribute__((always_inline)) int return_error(int *s, int err)
{
*s = err;
return sizeof(int);
}
static inline __attribute__((always_inline)) char *
args_off(struct msg_generic_kprobe *e, unsigned long off)
{
asm volatile("%[off] &= 0x3fff;\n" ::[off] "+r"(off)
:);
return e->args + off;
}
/* Error writer for use when pointer *s is lost to stack and can not
* be recoved with known bounds. We had to push this via asm to stop
* clang from omitting some checks and applying code motion on us.
*/
static inline __attribute__((always_inline)) int
return_stack_error(char *args, int orig, int err)
{
asm volatile("%[orig] &= 0xfff;\n"
"r1 = *(u64 *)%[args];\n"
"r1 += %[orig];\n"
"*(u32 *)(r1 + 0) = %[err];\n" ::[orig] "r+"(orig),
[args] "m+"(args), [err] "r+"(err)
: "r1");
return sizeof(int);
}
static inline __attribute__((always_inline)) int
parse_iovec_array(long off, unsigned long arg, int i, unsigned long max,
struct msg_generic_kprobe *e)
{
struct iovec
iov; // limit is 1024 using a hack now. For 5.4 kernel we should loop over 1024
char index = sizeof(struct iovec) * i;
__u64 size;
int err;
err = probe_read(&iov, sizeof(iov), (struct iovec *)(arg + index));
if (err < 0)
return char_buf_pagefault;
size = iov.iov_len;
if (max && size > max)
size = max;
if (size > 4094)
return char_buf_toolarge;
asm volatile("%[size] &= 0xfff;\n" ::[size] "+r"(size)
:);
err = probe_read(args_off(e, off), size, (char *)iov.iov_base);
if (err < 0)
return char_buf_pagefault;
return size;
}
// for loop can not be unrolled which is needed for 4.19 kernels :(
#define PARSE_IOVEC_ENTRY \
{ \
int c; \
/* embedding this in the loop counter breaks verifier */ \
if (i >= cnt) \
goto char_iovec_done; \
c = parse_iovec_array(off, arg, i, max, e); \
if (c < 0) { \
char *args = args_off(e, off_orig); \
return return_stack_error(args, 0, c); \
} \
size += c; \
if (max) { \
max -= c; \
if (!max) \
goto char_iovec_done; \
} \
c &= 0x7fff; \
off += c; \
i++; \
}
// We parse a max iovec entries and any more can be detected in db
#define PARSE_IOVEC_ENTRIES \
{ \
PARSE_IOVEC_ENTRY \
PARSE_IOVEC_ENTRY \
PARSE_IOVEC_ENTRY \
PARSE_IOVEC_ENTRY \
PARSE_IOVEC_ENTRY \
PARSE_IOVEC_ENTRY \
PARSE_IOVEC_ENTRY \
}
#ifdef __LARGE_BPF_PROG
#define MAX_STRING_FILTER 128
#else
#define MAX_STRING_FILTER 32
#endif
/* Unfortunately, clang really wanted to optimize this and was fairly
* difficult to convince it otherwise. Clang tries to join the bounding
* operations and group the memory accesses sometimes using a couple
* registers and shuffling values through them. All this confuses the
* verifiers especially on <5.x series. So we get the following ASM
* blob which I find easier to read than C code that would work here.
*/
#define ASM_RCMP \
{ \
t = s1; \
asm volatile("%[n] &= 0x7f;\n" \
"r0 = %[t];\n" \
"r0 += %[n];\n" \
"%[c] = *(u8*)(r0 + 0);\n" \
: [c] "=r"(c1) \
: [n] "+r"(n1), [t] "+r:"(t) \
: "r0"); \
t = s2; \
asm volatile("%[n] &= 0x7f;\n" \
"r0 = %[t];\n" \
"r0 += %[n];\n" \
"%[c] = *(u8*)(r0 + 0);\n" \
: [c] "=r"(c2) \
: [n] "+r"(n2), [t] "+r"(t) \
: "c2", "r0"); \
if (c1 != c2) \
goto failed; \
if (n1 < 1 || n2 < 1) \
goto accept; \
n1--; \
n2--; \
}
#define ASM_RCMP5 \
{ \
ASM_RCMP \
ASM_RCMP \
ASM_RCMP \
ASM_RCMP \
ASM_RCMP \
}
#define ASM_RCMP20 \
{ \
ASM_RCMP5 \
ASM_RCMP5 \
ASM_RCMP5 \
ASM_RCMP5 \
}
#define ASM_RCMP50 \
{ \
ASM_RCMP20 \
ASM_RCMP20 \
ASM_RCMP5 \
ASM_RCMP5 \
}
#define ASM_RCMP100 \
{ \
ASM_RCMP50 \
ASM_RCMP50 \
}
/* reverse compare bytes. n1 is index of last byte in s1. Ditto n2 of s2. */
static inline __attribute__((always_inline)) int rcmpbytes(char *s1, char *s2,
u64 n1, u64 n2)
{
char c1 = 0, c2 = 0, *t;
#ifdef __LARGE_BPF_PROG
ASM_RCMP50
#else
ASM_RCMP20
ASM_RCMP20
#endif
accept:
return 0;
failed:
return -1;
}
/* compare bytes. n is number of bytes to compare. */
static inline __attribute__((always_inline)) int cmpbytes(char *s1, char *s2,
size_t n)
{
int i;
#pragma unroll
for (i = 0; i < MAX_STRING_FILTER; i++) {
if (i >= n)
return 0;
if (s1[i] != s2[i])
return -1;
}
return 0;
}
static inline __attribute__((always_inline)) long
copy_path(char *args, const struct path *arg)
{
int *s = (int *)args;
int size = 0, flags = 0;
char *buffer;
void *curr = &args[4];
buffer = d_path_local(arg, &size, &flags);
if (!buffer)
return 0;
asm volatile("%[size] &= 0xff;\n" ::[size] "+r"(size)
:);
probe_read(curr, size, buffer);
*s = size;
size += 4;
/*
* the format of the path is:
* -------------------------------
* | 4 bytes | N bytes | 4 bytes |
* | pathlen | path | flags |
* -------------------------------
* Next we set up the flags.
*/
asm volatile goto(
"r1 = *(u64 *)%[pid];\n"
"r7 = *(u32 *)%[offset];\n"
"if r7 s< 0 goto %l[a];\n"
"if r7 s> 1188 goto %l[a];\n"
"r1 += r7;\n"
"r2 = *(u32 *)%[flags];\n"
"*(u32 *)(r1 + 0) = r2;\n"
:
: [pid] "m"(args), [flags] "m"(flags), [offset] "+m"(size)
: "r0", "r1", "r2", "r7", "memory"
: a);
a:
size += sizeof(u32); // for the flags
return size;
}
static inline __attribute__((always_inline)) long
copy_strings(char *args, char *arg, int max_size)
{
int *s = (int *)args;
long size;
// probe_read_str() always nul-terminates the string.
// So add one to the length to allow for it. This should
// result in us honouring our max_size correctly.
size = probe_read_str(&args[4], max_size + 1, arg);
if (size <= 1)
return invalid_ty;
// Remove the nul character from end.
size--;
*s = size;
// Initial 4 bytes hold string length
return size + 4;
}
static inline __attribute__((always_inline)) long copy_skb(char *args,
unsigned long arg)
{
struct sk_buff *skb = (struct sk_buff *)arg;
struct skb_type *skb_event = (struct skb_type *)args;
/* struct values */
probe_read(&skb_event->hash, sizeof(__u32), _(&skb->hash));
probe_read(&skb_event->len, sizeof(__u32), _(&skb->len));
probe_read(&skb_event->priority, sizeof(__u32), _(&skb->priority));
probe_read(&skb_event->mark, sizeof(__u32), _(&skb->mark));
/* socket data */
set_event_from_skb(skb_event, skb);
return sizeof(struct skb_type);
}
static inline __attribute__((always_inline)) long copy_sock(char *args,
unsigned long arg)
{
struct sock *sk = (struct sock *)arg;
struct sk_type *sk_event = (struct sk_type *)args;
set_event_from_sock(sk_event, sk);
return sizeof(struct sk_type);
}
static inline __attribute__((always_inline)) long
copy_user_ns(char *args, unsigned long arg)
{
struct user_namespace *ns = (struct user_namespace *)arg;
struct msg_user_namespace *u_ns_info =
(struct msg_user_namespace *)args;
probe_read(&u_ns_info->level, sizeof(__s32), _(&ns->level));
probe_read(&u_ns_info->uid, sizeof(__u32), _(&ns->owner));
probe_read(&u_ns_info->gid, sizeof(__u32), _(&ns->group));
probe_read(&u_ns_info->ns_inum, sizeof(__u32), _(&ns->ns.inum));
return sizeof(struct msg_user_namespace);
}
static inline __attribute__((always_inline)) long copy_cred(char *args,
unsigned long arg)
{
struct user_namespace *ns;
struct cred *cred = (struct cred *)arg;
struct msg_cred *info = (struct msg_cred *)args;
struct msg_capabilities *caps = &info->caps;
struct msg_user_namespace *user_ns_info = &info->user_ns;
probe_read(&info->uid, sizeof(__u32), _(&cred->uid));
probe_read(&info->gid, sizeof(__u32), _(&cred->gid));
probe_read(&info->euid, sizeof(__u32), _(&cred->euid));
probe_read(&info->egid, sizeof(__u32), _(&cred->egid));
probe_read(&info->suid, sizeof(__u32), _(&cred->suid));
probe_read(&info->sgid, sizeof(__u32), _(&cred->sgid));
probe_read(&info->fsuid, sizeof(__u32), _(&cred->fsuid));
probe_read(&info->fsgid, sizeof(__u32), _(&cred->fsgid));
info->pad = 0;
probe_read(&info->securebits, sizeof(__u32), _(&cred->securebits));
__get_caps(caps, cred);
probe_read(&ns, sizeof(ns), _(&cred->user_ns));
copy_user_ns((char *)user_ns_info, (unsigned long)ns);
return sizeof(struct msg_cred);
}
static inline __attribute__((always_inline)) long
copy_capability(char *args, unsigned long arg)
{
int cap = (int)arg;
struct capability_info_type *info = (struct capability_info_type *)args;
info->pad = 0;
info->cap = cap;
return sizeof(struct capability_info_type);
}
static inline __attribute__((always_inline)) long
copy_load_module(char *args, unsigned long arg)
{
int ok;
const char *name;
const struct load_info *mod = (struct load_info *)arg;
struct tg_kernel_module *info = (struct tg_kernel_module *)args;
memset(info, 0, sizeof(struct tg_kernel_module));
if (BPF_CORE_READ_INTO(&name, mod, name) != 0)
return 0;
if (probe_read_str(&info->name, TG_MODULE_NAME_LEN - 1, name) < 0)
return 0;
BPF_CORE_READ_INTO(&info->taints, mod, mod, taints);
if (BPF_CORE_READ_INTO(&ok, mod, sig_ok) == 0)
info->sig_ok = !!ok;
return sizeof(struct tg_kernel_module);
}
static inline __attribute__((always_inline)) long
copy_kernel_module(char *args, unsigned long arg)
{
const struct module *mod = (struct module *)arg;
struct tg_kernel_module *info = (struct tg_kernel_module *)args;
memset(info, 0, sizeof(struct tg_kernel_module));
if (probe_read_str(&info->name, TG_MODULE_NAME_LEN - 1, mod->name) < 0)
return 0;
BPF_CORE_READ_INTO(&info->taints, mod, taints);
/*
* Todo: allow to check if module is signed here too.
* the module->sig_ok is available only under CONFIG_MODULE_SIG option, so
* let's not fail here, and users can check the load_info->sig_ok instead.
*/
return sizeof(struct tg_kernel_module);
}
#define ARGM_INDEX_MASK 0xf
#define ARGM_RETURN_COPY BIT(4)
#define ARGM_MAX_DATA BIT(5)
static inline __attribute__((always_inline)) bool
hasReturnCopy(unsigned long argm)
{
return (argm & ARGM_RETURN_COPY) != 0;
}
static inline __attribute__((always_inline)) bool
has_max_data(unsigned long argm)
{
return (argm & ARGM_MAX_DATA) != 0;
}
static inline __attribute__((always_inline)) unsigned long
get_arg_meta(int meta, struct msg_generic_kprobe *e)
{
switch (meta & ARGM_INDEX_MASK) {
case 1:
return e->a0;
case 2:
return e->a1;
case 3:
return e->a2;
case 4:
return e->a3;
case 5:
return e->a4;
}
return 0;
}
static inline __attribute__((always_inline)) long
__copy_char_buf(void *ctx, long off, unsigned long arg, unsigned long bytes,
bool max_data, struct msg_generic_kprobe *e,
struct bpf_map_def *data_heap)
{
int *s = (int *)args_off(e, off);
size_t rd_bytes, extra = 8;
int err;
#ifdef __LARGE_BPF_PROG
if (max_data && data_heap) {
/* The max_data flag is enabled, the first int value indicates
* if we use (1) data events or not (0).
*/
if (bytes >= 0x1000) {
s[0] = 1;
return data_event_bytes(ctx,
(struct data_event_desc *)&s[1],
arg, bytes, data_heap) +
4;
}
s[0] = 0;
s = (int *)args_off(e, off + 4);
extra += 4;
}
#endif // __LARGE_BPF_PROG
/* Bound bytes <4095 to ensure bytes does not read past end of buffer */
rd_bytes = bytes < 0x1000 ? bytes : 0xfff;
asm volatile("%[rd_bytes] &= 0xfff;\n" ::[rd_bytes] "+r"(rd_bytes)
:);
err = probe_read(&s[2], rd_bytes, (char *)arg);
if (err < 0)
return return_error(s, char_buf_pagefault);
s[0] = (int)bytes;
s[1] = (int)rd_bytes;
return rd_bytes + extra;
}
static inline __attribute__((always_inline)) long
copy_char_buf(void *ctx, long off, unsigned long arg, int argm,
struct msg_generic_kprobe *e,
struct bpf_map_def *data_heap)
{
int *s = (int *)args_off(e, off);
unsigned long meta;
size_t bytes = 0;
if (hasReturnCopy(argm)) {
u64 retid = retprobe_map_get_key(ctx);
retprobe_map_set(e->func_id, retid, e->common.ktime, arg);
return return_error(s, char_buf_saved_for_retprobe);
}
meta = get_arg_meta(argm, e);
probe_read(&bytes, sizeof(bytes), &meta);
return __copy_char_buf(ctx, off, arg, bytes, has_max_data(argm), e, data_heap);
}
static inline __attribute__((always_inline)) u16
string_padded_len(u16 len)
{
u16 padded_len = len;
if (len < STRING_MAPS_SIZE_5) {
if (len % STRING_MAPS_KEY_INC_SIZE != 0)
padded_len = ((len / STRING_MAPS_KEY_INC_SIZE) + 1) * STRING_MAPS_KEY_INC_SIZE;
return padded_len;
}
if (len <= STRING_MAPS_SIZE_6 - 2)
return STRING_MAPS_SIZE_6 - 2;
#ifdef __LARGE_BPF_PROG
#ifdef __LARGE_MAP_KEYS
if (len <= STRING_MAPS_SIZE_7 - 2)
return STRING_MAPS_SIZE_7 - 2;
if (len <= STRING_MAPS_SIZE_8 - 2)
return STRING_MAPS_SIZE_8 - 2;
if (len <= STRING_MAPS_SIZE_9 - 2)
return STRING_MAPS_SIZE_9 - 2;
return STRING_MAPS_SIZE_10 - 2;
#else
return STRING_MAPS_SIZE_7 - 2;
#endif
#else
return STRING_MAPS_SIZE_5 - 1;
#endif
}
static inline __attribute__((always_inline)) int
string_map_index(u16 padded_len)
{
if (padded_len < STRING_MAPS_SIZE_5)
return (padded_len / STRING_MAPS_KEY_INC_SIZE) - 1;
#ifdef __LARGE_BPF_PROG
#ifdef __LARGE_MAP_KEYS
switch (padded_len) {
case STRING_MAPS_SIZE_6 - 2:
return 6;
case STRING_MAPS_SIZE_7 - 2:
return 7;
case STRING_MAPS_SIZE_8 - 2:
return 8;
case STRING_MAPS_SIZE_9 - 2:
return 9;
}
return 10;
#else
if (padded_len == STRING_MAPS_SIZE_6 - 2)
return 6;
return 7;
#endif
#else
return 5;
#endif
}
static inline __attribute__((always_inline)) void *
get_string_map(int index, __u32 map_idx)
{
switch (index) {
case 0:
return map_lookup_elem(&string_maps_0, &map_idx);
case 1:
return map_lookup_elem(&string_maps_1, &map_idx);
case 2:
return map_lookup_elem(&string_maps_2, &map_idx);
case 3:
return map_lookup_elem(&string_maps_3, &map_idx);
case 4:
return map_lookup_elem(&string_maps_4, &map_idx);
case 5:
return map_lookup_elem(&string_maps_5, &map_idx);
#ifdef __LARGE_BPF_PROG
case 6:
return map_lookup_elem(&string_maps_6, &map_idx);
case 7:
return map_lookup_elem(&string_maps_7, &map_idx);
#ifdef __LARGE_MAP_KEYS
case 8:
return map_lookup_elem(&string_maps_8, &map_idx);
case 9:
return map_lookup_elem(&string_maps_9, &map_idx);
case 10:
return map_lookup_elem(&string_maps_10, &map_idx);
#endif
#endif
}
return 0;
}
static inline __attribute__((always_inline)) long
filter_char_buf_equal(struct selector_arg_filter *filter, char *arg_str, uint orig_len)
{
__u32 *map_ids = (__u32 *)&filter->value;
char *heap, *zero_heap;
void *string_map;
__u16 padded_len;
__u32 map_idx;
int zero = 0;
__u16 len;
int index;
#ifdef __LARGE_BPF_PROG
#ifdef __LARGE_MAP_KEYS
if (orig_len > STRING_MAPS_SIZE_10 - 2 || !orig_len)
return 0;
#else
if (orig_len > STRING_MAPS_SIZE_7 - 2 || !orig_len)
return 0;
#endif
#else
if (orig_len > STRING_MAPS_SIZE_5 - 1 || !orig_len)
return 0;
#endif
len = (__u16)orig_len;
// Calculate padded string length
padded_len = string_padded_len(len);
// Check if we have entries for this padded length.
// Do this before we copy data for efficiency.
index = string_map_index(padded_len);
map_idx = map_ids[index & 0xf];
if (map_idx == 0xffffffff)
return 0;
heap = (char *)map_lookup_elem(&string_maps_heap, &zero);
zero_heap = (char *)map_lookup_elem(&string_maps_ro_zero, &zero);
if (!heap || !zero_heap)
return 0;
// Copy string to heap, preceded by length -
// u8 for first 6 maps; u16 for latter maps
#ifdef __LARGE_BPF_PROG
if (index <= 5)
heap[0] = len;
else
*(u16 *)heap = len;
#else
heap[0] = len;
#endif
asm volatile("%[len] &= %1;\n"
: [len] "+r"(len)
: "i"(STRING_MAPS_HEAP_MASK));
#ifdef __LARGE_BPF_PROG
if (index <= 5)
probe_read(&heap[1], len, arg_str);
else
probe_read(&heap[2], len, arg_str);
#else
probe_read(&heap[1], len, arg_str);
#endif
// Pad string to multiple of key increment size
if (padded_len > len) {
asm volatile("%[len] &= %1;\n"
: [len] "+r"(len)
: "i"(STRING_MAPS_HEAP_MASK));
#ifdef __LARGE_BPF_PROG
if (index <= 5)
probe_read(heap + len + 1, (padded_len - len) & STRING_MAPS_COPY_MASK, zero_heap);
else
probe_read(heap + len + 2, (padded_len - len) & STRING_MAPS_COPY_MASK, zero_heap);
#else
probe_read(heap + len + 1, (padded_len - len) & STRING_MAPS_COPY_MASK, zero_heap);
#endif
}
// Get map for this string length
string_map = get_string_map(index, map_idx);
if (!string_map)
return 0;
__u8 *pass = map_lookup_elem(string_map, heap);
return !!pass;
}
static inline __attribute__((always_inline)) long
filter_char_buf_prefix(struct selector_arg_filter *filter, char *arg_str, uint arg_len)
{
void *addrmap;
__u32 map_idx = *(__u32 *)&filter->value;
struct string_prefix_lpm_trie *arg;
int zero = 0;
addrmap = map_lookup_elem(&string_prefix_maps, &map_idx);
if (!addrmap || !arg_len)
return 0;
// If the string to check is longer than the prefix map allows, then only check the longest
// substring that the map allows.
if (arg_len >= STRING_PREFIX_MAX_LENGTH)
arg_len = STRING_PREFIX_MAX_LENGTH - 1;
arg = (struct string_prefix_lpm_trie *)map_lookup_elem(&string_prefix_maps_heap, &zero);
if (!arg)
return 0;
arg->prefixlen = arg_len * 8; // prefix is in bits
// Force the verifier to recheck the arg_len after register spilling on 4.19.
asm volatile("%[arg_len] &= %[mask] ;\n"
: [arg_len] "+r"(arg_len)
: [mask] "i"(STRING_PREFIX_MAX_LENGTH - 1));
probe_read(arg->data, arg_len & (STRING_PREFIX_MAX_LENGTH - 1), arg_str);
__u8 *pass = map_lookup_elem(addrmap, arg);
return !!pass;
}
// Define a mask for the maximum path length on Linux.
#define PATH_MASK (4096 - 1)
static inline __attribute__((always_inline)) void
copy_reverse(__u8 *dest, uint len, __u8 *src, uint offset)
{
uint i;
len &= STRING_POSTFIX_MAX_MASK;
#ifndef __LARGE_BPF_PROG
#pragma unroll
#endif
// Maximum we can go to is one less than the absolute maximum.
// This is to allow the masking and indexing to work correctly.
// (Appreciate this is a bit ugly.)
// If len == STRING_POSTFIX_MAX_LENGTH, and this is 128, then
// it will have been masked to 0 above, leading to src indices
// of -1, -2, -3... masked with STRING_POSTFIX_MAX_MASK (127).
// These will equal 127, 126, 125... which will therefore
// reverse copy the string as if it was 127 chars long.
// Alternative (prettier) fixes resulted in a confused verifier
// unfortunately.
for (i = 0; i < (STRING_POSTFIX_MAX_MATCH_LENGTH - 1); i++) {
dest[i & STRING_POSTFIX_MAX_MASK] = src[(len + offset - 1 - i) & PATH_MASK];
if (len + offset == (i + 1))
return;
}
}
static inline __attribute__((always_inline)) long
filter_char_buf_postfix(struct selector_arg_filter *filter, char *arg_str, uint arg_len)
{
void *addrmap;
__u32 map_idx = *(__u32 *)&filter->value;
struct string_postfix_lpm_trie *arg;
uint orig_len = arg_len;
int zero = 0;
addrmap = map_lookup_elem(&string_postfix_maps, &map_idx);
if (!addrmap || !arg_len)
return 0;
if (arg_len >= STRING_POSTFIX_MAX_MATCH_LENGTH)
arg_len = STRING_POSTFIX_MAX_MATCH_LENGTH - 1;
arg = (struct string_postfix_lpm_trie *)map_lookup_elem(&string_postfix_maps_heap, &zero);
if (!arg)
return 0;
arg->prefixlen = arg_len * 8; // prefix is in bits
copy_reverse(arg->data, arg_len, (__u8 *)arg_str, orig_len - arg_len);
__u8 *pass = map_lookup_elem(addrmap, arg);
return !!pass;
}
static inline __attribute__((always_inline)) bool is_not_operator(__u32 op)