forked from falcosecurity/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppm_flag_helpers.h
1952 lines (1725 loc) · 39.4 KB
/
ppm_flag_helpers.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 PPM_FLAG_HELPERS_H_
#define PPM_FLAG_HELPERS_H_
/* The ASSERT is defined in other files that we don't
* want to inlcude with the modern probe. `ppm.h`
*/
#ifdef __USE_VMLINUX__
#define ASSERT(expr)
#endif
#if !defined(UDIG) && !defined(__USE_VMLINUX__)
#include <linux/mman.h>
#include <linux/futex.h>
#include <linux/ptrace.h>
#include <linux/capability.h>
#include <linux/eventpoll.h>
#include "ppm.h"
#ifdef __NR_io_uring_register
#include <uapi/linux/io_uring.h>
#endif
#endif // ifndef UDIG
#define PPM_MS_MGC_MSK 0xffff0000
#define PPM_MS_MGC_VAL 0xC0ED0000
/* Check if the res is different from `PPM_SUCCCES` */
#define CHECK_RES(x) \
if(unlikely(x != PPM_SUCCESS)) \
{ \
return x; \
} \
static __always_inline uint32_t open_flags_to_scap(uint32_t flags)
{
uint32_t res = 0;
switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
case O_WRONLY:
res |= PPM_O_WRONLY;
break;
case O_RDWR:
res |= PPM_O_RDWR;
break;
default:
res |= PPM_O_RDONLY;
break;
}
if (flags & O_CREAT)
res |= PPM_O_CREAT;
#ifdef O_TMPFILE
if (flags & O_TMPFILE)
res |= PPM_O_TMPFILE;
#endif
if (flags & O_APPEND)
res |= PPM_O_APPEND;
#ifdef O_DSYNC
if (flags & O_DSYNC)
res |= PPM_O_DSYNC;
#endif
if (flags & O_EXCL)
res |= PPM_O_EXCL;
#ifdef O_NONBLOCK
if (flags & O_NONBLOCK)
res |= PPM_O_NONBLOCK;
#endif
#ifdef O_SYNC
if (flags & O_SYNC)
res |= PPM_O_SYNC;
#endif
if (flags & O_TRUNC)
res |= PPM_O_TRUNC;
#ifdef O_DIRECT
if (flags & O_DIRECT)
res |= PPM_O_DIRECT;
#endif
#ifdef O_DIRECTORY
if (flags & O_DIRECTORY)
res |= PPM_O_DIRECTORY;
#endif
#ifdef O_LARGEFILE
if (flags & O_LARGEFILE)
res |= PPM_O_LARGEFILE;
#endif
#ifdef O_CLOEXEC
if (flags & O_CLOEXEC)
res |= PPM_O_CLOEXEC;
#endif
return res;
}
static __always_inline u32 open_modes_to_scap(unsigned long flags,
unsigned long modes)
{
#ifdef UDIG
unsigned long flags_mask = O_CREAT | O_TMPFILE;
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
unsigned long flags_mask = O_CREAT | O_TMPFILE;
#else
unsigned long flags_mask = O_CREAT;
#endif
#endif /* UDIG */
u32 res = 0;
if ((flags & flags_mask) == 0)
return res;
if (modes & S_IRUSR)
res |= PPM_S_IRUSR;
if (modes & S_IWUSR)
res |= PPM_S_IWUSR;
if (modes & S_IXUSR)
res |= PPM_S_IXUSR;
/*
* PPM_S_IRWXU == S_IRUSR | S_IWUSR | S_IXUSR
*/
if (modes & S_IRGRP)
res |= PPM_S_IRGRP;
if (modes & S_IWGRP)
res |= PPM_S_IWGRP;
if (modes & S_IXGRP)
res |= PPM_S_IXGRP;
/*
* PPM_S_IRWXG == S_IRGRP | S_IWGRP | S_IXGRP
*/
if (modes & S_IROTH)
res |= PPM_S_IROTH;
if (modes & S_IWOTH)
res |= PPM_S_IWOTH;
if (modes & S_IXOTH)
res |= PPM_S_IXOTH;
/*
* PPM_S_IRWXO == S_IROTH | S_IWOTH | S_IXOTH
*/
if (modes & S_ISUID)
res |= PPM_S_ISUID;
if (modes & S_ISGID)
res |= PPM_S_ISGID;
if (modes & S_ISVTX)
res |= PPM_S_ISVTX;
return res;
}
static __always_inline u32 openat2_resolve_to_scap(unsigned long flags)
{
u32 res = 0;
#ifdef RESOLVE_NO_XDEV
if (flags & RESOLVE_NO_XDEV)
res |= PPM_RESOLVE_NO_XDEV;
#endif
#ifdef RESOLVE_NO_MAGICLINKS
if (flags & RESOLVE_NO_MAGICLINKS)
res |= PPM_RESOLVE_NO_MAGICLINKS;
#endif
#ifdef RESOLVE_NO_SYMLINKS
if (flags & RESOLVE_NO_SYMLINKS)
res |= PPM_RESOLVE_NO_SYMLINKS;
#endif
#ifdef RESOLVE_BENEATH
if (flags & RESOLVE_BENEATH)
res |= PPM_RESOLVE_BENEATH;
#endif
#ifdef RESOLVE_IN_ROOT
if (flags & RESOLVE_IN_ROOT)
res |= PPM_RESOLVE_IN_ROOT;
#endif
#ifdef RESOLVE_CACHED
if (flags & RESOLVE_CACHED)
res |= PPM_RESOLVE_CACHED;
#endif
return res;
}
static __always_inline u32 io_uring_setup_flags_to_scap(unsigned long flags){
u32 res = 0;
#ifdef IORING_SETUP_IOPOLL
if (flags & IORING_SETUP_IOPOLL)
res |= PPM_IORING_SETUP_IOPOLL;
#endif
#ifdef IORING_SETUP_SQPOLL
if (flags & IORING_SETUP_SQPOLL)
res |= PPM_IORING_SETUP_SQPOLL;
#endif
#ifdef IORING_SQ_NEED_WAKEUP
if (flags & IORING_SQ_NEED_WAKEUP)
res |= PPM_IORING_SQ_NEED_WAKEUP;
#endif
#ifdef IORING_SETUP_SQ_AFF
if (flags & IORING_SETUP_SQ_AFF)
res |= PPM_IORING_SETUP_SQ_AFF;
#endif
#ifdef IORING_SETUP_CQSIZE
if (flags & IORING_SETUP_CQSIZE)
res |= PPM_IORING_SETUP_CQSIZE;
#endif
#ifdef IORING_SETUP_CLAMP
if (flags & IORING_SETUP_CLAMP)
res |= PPM_IORING_SETUP_CLAMP;
#endif
#ifdef IORING_SETUP_ATTACH_WQ
if (flags & IORING_SETUP_ATTACH_WQ)
res |= PPM_IORING_SETUP_ATTACH_WQ;
#endif
#ifdef IORING_SETUP_R_DISABLED
if (flags & IORING_SETUP_R_DISABLED)
res |= PPM_IORING_SETUP_R_DISABLED;
#endif
return res;
}
static __always_inline u32 io_uring_setup_feats_to_scap(unsigned long flags){
u32 res = 0;
#ifdef IORING_FEAT_SINGLE_MMAP
if (flags & IORING_FEAT_SINGLE_MMAP)
res |= PPM_IORING_FEAT_SINGLE_MMAP;
#endif
#ifdef IORING_FEAT_NODROP
if (flags & IORING_FEAT_NODROP)
res |= PPM_IORING_FEAT_NODROP;
#endif
#ifdef IORING_FEAT_SUBMIT_STABLE
if (flags & IORING_FEAT_SUBMIT_STABLE)
res |= PPM_IORING_FEAT_SUBMIT_STABLE;
#endif
#ifdef IORING_FEAT_RW_CUR_POS
if (flags & IORING_FEAT_RW_CUR_POS)
res |= PPM_IORING_FEAT_RW_CUR_POS;
#endif
#ifdef IORING_FEAT_CUR_PERSONALITY
if (flags & IORING_FEAT_CUR_PERSONALITY)
res |= PPM_IORING_FEAT_CUR_PERSONALITY;
#endif
#ifdef IORING_FEAT_FAST_POLL
if (flags & IORING_FEAT_FAST_POLL)
res |= PPM_IORING_FEAT_FAST_POLL;
#endif
#ifdef IORING_FEAT_POLL_32BITS
if (flags & IORING_FEAT_POLL_32BITS)
res |= PPM_IORING_FEAT_POLL_32BITS;
#endif
#ifdef IORING_FEAT_SQPOLL_NONFIXED
if (flags & IORING_FEAT_SQPOLL_NONFIXED)
res |= PPM_IORING_FEAT_SQPOLL_NONFIXED;
#endif
#ifdef IORING_FEAT_ENTER_EXT_ARG
if (flags & IORING_FEAT_ENTER_EXT_ARG)
res |= PPM_IORING_FEAT_ENTER_EXT_ARG;
#endif
#ifdef IORING_FEAT_NATIVE_WORKERS
if (flags & IORING_FEAT_NATIVE_WORKERS)
res |= PPM_IORING_FEAT_NATIVE_WORKERS;
#endif
#ifdef IORING_FEAT_RSRC_TAGS
if (flags & IORING_FEAT_RSRC_TAGS)
res |= PPM_IORING_FEAT_RSRC_TAGS;
#endif
return res;
}
static __always_inline u32 io_uring_enter_flags_to_scap(unsigned long flags)
{
u32 res = 0;
#ifdef IORING_ENTER_GETEVENTS
if (flags & IORING_ENTER_GETEVENTS)
res |= PPM_IORING_ENTER_GETEVENTS;
#endif
#ifdef IORING_ENTER_SQ_WAKEUP
if (flags & IORING_ENTER_SQ_WAKEUP)
res |= PPM_IORING_ENTER_SQ_WAKEUP;
#endif
#ifdef IORING_ENTER_SQ_WAIT
if (flags & IORING_ENTER_SQ_WAIT)
res |= PPM_IORING_ENTER_SQ_WAIT;
#endif
#ifdef IORING_ENTER_EXT_ARG
if (flags & IORING_ENTER_EXT_ARG)
res |= PPM_IORING_ENTER_EXT_ARG;
#endif
return res;
}
static __always_inline u32 io_uring_register_opcodes_to_scap(unsigned long flags)
{
/*
* io_uring_register opcodes are defined via enum in io_uring.h.
* It is userspace API (thus stable) and arch independent.
* Therefore we map them 1:1; if any unmapped flag arrives,
* we will just print its value to userspace without mapping it to a string flag.
* We then need to append new flags to both flags_table and ppm_events_public PPM_ flags.
*/
return flags;
}
static __always_inline u32 clone_flags_to_scap(unsigned long flags)
{
u32 res = 0;
if (flags & CLONE_FILES)
res |= PPM_CL_CLONE_FILES;
if (flags & CLONE_FS)
res |= PPM_CL_CLONE_FS;
#ifdef CLONE_IO
if (flags & CLONE_IO)
res |= PPM_CL_CLONE_IO;
#endif
#ifdef CLONE_NEWIPC
if (flags & CLONE_NEWIPC)
res |= PPM_CL_CLONE_NEWIPC;
#endif
#ifdef CLONE_NEWNET
if (flags & CLONE_NEWNET)
res |= PPM_CL_CLONE_NEWNET;
#endif
#ifdef CLONE_NEWNS
if (flags & CLONE_NEWNS)
res |= PPM_CL_CLONE_NEWNS;
#endif
#ifdef CLONE_NEWPID
if (flags & CLONE_NEWPID)
res |= PPM_CL_CLONE_NEWPID;
#endif
#ifdef CLONE_NEWUTS
if (flags & CLONE_NEWUTS)
res |= PPM_CL_CLONE_NEWUTS;
#endif
if (flags & CLONE_PARENT_SETTID)
res |= PPM_CL_CLONE_PARENT_SETTID;
if (flags & CLONE_PARENT)
res |= PPM_CL_CLONE_PARENT;
if (flags & CLONE_PTRACE)
res |= PPM_CL_CLONE_PTRACE;
if (flags & CLONE_SIGHAND)
res |= PPM_CL_CLONE_SIGHAND;
if (flags & CLONE_SYSVSEM)
res |= PPM_CL_CLONE_SYSVSEM;
if (flags & CLONE_THREAD)
res |= PPM_CL_CLONE_THREAD;
if (flags & CLONE_UNTRACED)
res |= PPM_CL_CLONE_UNTRACED;
if (flags & CLONE_VM)
res |= PPM_CL_CLONE_VM;
#ifdef CLONE_NEWUSER
if (flags & CLONE_NEWUSER)
res |= PPM_CL_CLONE_NEWUSER;
#endif
if (flags & CLONE_CHILD_CLEARTID)
res |= PPM_CL_CLONE_CHILD_CLEARTID;
if (flags & CLONE_CHILD_SETTID)
res |= PPM_CL_CLONE_CHILD_SETTID;
if (flags & CLONE_SETTLS)
res |= PPM_CL_CLONE_SETTLS;
#ifdef CLONE_STOPPED
if (flags & CLONE_STOPPED)
res |= PPM_CL_CLONE_STOPPED;
#endif
if (flags & CLONE_VFORK)
res |= PPM_CL_CLONE_VFORK;
#ifdef CLONE_NEWCGROUP
if (flags & CLONE_NEWCGROUP)
res |= PPM_CL_CLONE_NEWCGROUP;
#endif
return res;
}
static __always_inline u8 socket_family_to_scap(u8 family)
{
if (family == AF_INET)
return PPM_AF_INET;
else if (family == AF_INET6)
return PPM_AF_INET6;
else if (family == AF_UNIX)
return PPM_AF_UNIX;
#ifdef AF_NETLINK
else if (family == AF_NETLINK)
return PPM_AF_NETLINK;
#endif
#ifdef AF_PACKET
else if (family == AF_PACKET)
return PPM_AF_PACKET;
#endif
#ifdef AF_UNSPEC
else if (family == AF_UNSPEC)
return PPM_AF_UNSPEC;
#endif
#ifdef AF_AX25
else if (family == AF_AX25)
return PPM_AF_AX25;
#endif
#ifdef AF_IPX
else if (family == AF_IPX)
return PPM_AF_IPX;
#endif
#ifdef AF_APPLETALK
else if (family == AF_APPLETALK)
return PPM_AF_APPLETALK;
#endif
#ifdef AF_NETROM
else if (family == AF_NETROM)
return PPM_AF_NETROM;
#endif
#ifdef AF_BRIDGE
else if (family == AF_BRIDGE)
return PPM_AF_BRIDGE;
#endif
#ifdef AF_ATMPVC
else if (family == AF_ATMPVC)
return PPM_AF_ATMPVC;
#endif
#ifdef AF_X25
else if (family == AF_X25)
return PPM_AF_X25;
#endif
#ifdef AF_ROSE
else if (family == AF_ROSE)
return PPM_AF_ROSE;
#endif
#ifdef AF_DECnet
else if (family == AF_DECnet)
return PPM_AF_DECnet;
#endif
#ifdef AF_NETBEUI
else if (family == AF_NETBEUI)
return PPM_AF_NETBEUI;
#endif
#ifdef AF_SECURITY
else if (family == AF_SECURITY)
return PPM_AF_SECURITY;
#endif
#ifdef AF_KEY
else if (family == AF_KEY)
return PPM_AF_KEY;
#endif
#ifdef AF_ROUTE
else if (family == AF_ROUTE)
return PPM_AF_ROUTE;
#endif
#ifdef AF_ASH
else if (family == AF_ASH)
return PPM_AF_ASH;
#endif
#ifdef AF_ECONET
else if (family == AF_ECONET)
return PPM_AF_ECONET;
#endif
#ifdef AF_ATMSVC
else if (family == AF_ATMSVC)
return PPM_AF_ATMSVC;
#endif
#ifdef AF_RDS
else if (family == AF_RDS)
return PPM_AF_RDS;
#endif
#ifdef AF_SNA
else if (family == AF_SNA)
return PPM_AF_SNA;
#endif
#ifdef AF_IRDA
else if (family == AF_IRDA)
return PPM_AF_IRDA;
#endif
#ifdef AF_PPPOX
else if (family == AF_PPPOX)
return PPM_AF_PPPOX;
#endif
#ifdef AF_WANPIPE
else if (family == AF_WANPIPE)
return PPM_AF_WANPIPE;
#endif
#ifdef AF_LLC
else if (family == AF_LLC)
return PPM_AF_LLC;
#endif
#ifdef AF_CAN
else if (family == AF_CAN)
return PPM_AF_CAN;
#endif
#ifdef AF_TIPC
else if (family == AF_TIPC)
return PPM_AF_TIPC;
#endif
#ifdef AF_BLUETOOTH
else if (family == AF_BLUETOOTH)
return PPM_AF_BLUETOOTH;
#endif
#ifdef AF_IUCV
else if (family == AF_IUCV)
return PPM_AF_IUCV;
#endif
#ifdef AF_RXRPC
else if (family == AF_RXRPC)
return PPM_AF_RXRPC;
#endif
#ifdef AF_ISDN
else if (family == AF_ISDN)
return PPM_AF_ISDN;
#endif
#ifdef AF_PHONET
else if (family == AF_PHONET)
return PPM_AF_PHONET;
#endif
#ifdef AF_IEEE802154
else if (family == AF_IEEE802154)
return PPM_AF_IEEE802154;
#endif
#ifdef AF_CAIF
else if (family == AF_CAIF)
return PPM_AF_CAIF;
#endif
#ifdef AF_ALG
else if (family == AF_ALG)
return PPM_AF_ALG;
#endif
#ifdef AF_NFC
else if (family == AF_NFC)
return PPM_AF_NFC;
#endif
else {
ASSERT(false);
return PPM_AF_UNSPEC;
}
}
static __always_inline u32 prot_flags_to_scap(int prot)
{
u32 res = 0;
if (prot & PROT_READ)
res |= PPM_PROT_READ;
if (prot & PROT_WRITE)
res |= PPM_PROT_WRITE;
if (prot & PROT_EXEC)
res |= PPM_PROT_EXEC;
#ifdef PROT_SEM
if (prot & PROT_SEM)
res |= PPM_PROT_SEM;
#endif
if (prot & PROT_GROWSDOWN)
res |= PPM_PROT_GROWSDOWN;
if (prot & PROT_GROWSUP)
res |= PPM_PROT_GROWSUP;
#ifdef PROT_SAO
if (prot & PROT_SAO)
res |= PPM_PROT_SAO;
#endif
return res;
}
static __always_inline u32 mmap_flags_to_scap(int flags)
{
u32 res = 0;
if (flags & MAP_SHARED)
res |= PPM_MAP_SHARED;
if (flags & MAP_PRIVATE)
res |= PPM_MAP_PRIVATE;
if (flags & MAP_FIXED)
res |= PPM_MAP_FIXED;
if (flags & MAP_ANONYMOUS)
res |= PPM_MAP_ANONYMOUS;
#ifdef MAP_32BIT
if (flags & MAP_32BIT)
res |= PPM_MAP_32BIT;
#endif
#ifdef MAP_RENAME
if (flags & MAP_RENAME)
res |= PPM_MAP_RENAME;
#endif
if (flags & MAP_NORESERVE)
res |= PPM_MAP_NORESERVE;
if (flags & MAP_POPULATE)
res |= PPM_MAP_POPULATE;
if (flags & MAP_NONBLOCK)
res |= PPM_MAP_NONBLOCK;
if (flags & MAP_GROWSDOWN)
res |= PPM_MAP_GROWSDOWN;
if (flags & MAP_DENYWRITE)
res |= PPM_MAP_DENYWRITE;
if (flags & MAP_EXECUTABLE)
res |= PPM_MAP_EXECUTABLE;
#ifdef MAP_INHERIT
if (flags & MAP_INHERIT)
res |= PPM_MAP_INHERIT;
#endif
if (flags & MAP_FILE)
res |= PPM_MAP_FILE;
if (flags & MAP_LOCKED)
res |= PPM_MAP_LOCKED;
return res;
}
static __always_inline u8 fcntl_cmd_to_scap(unsigned long cmd)
{
switch (cmd) {
case F_DUPFD:
return PPM_FCNTL_F_DUPFD;
case F_GETFD:
return PPM_FCNTL_F_GETFD;
case F_SETFD:
return PPM_FCNTL_F_SETFD;
case F_GETFL:
return PPM_FCNTL_F_GETFL;
case F_SETFL:
return PPM_FCNTL_F_SETFL;
case F_GETLK:
return PPM_FCNTL_F_GETLK;
case F_SETLK:
return PPM_FCNTL_F_SETLK;
case F_SETLKW:
return PPM_FCNTL_F_SETLKW;
case F_SETOWN:
return PPM_FCNTL_F_SETOWN;
case F_GETOWN:
return PPM_FCNTL_F_GETOWN;
case F_SETSIG:
return PPM_FCNTL_F_SETSIG;
case F_GETSIG:
return PPM_FCNTL_F_GETSIG;
#ifndef UDIG
#ifndef CONFIG_64BIT
case F_GETLK64:
return PPM_FCNTL_F_GETLK64;
case F_SETLK64:
return PPM_FCNTL_F_SETLK64;
case F_SETLKW64:
return PPM_FCNTL_F_SETLKW64;
#endif
#endif /* UDIG */
#ifdef F_SETOWN_EX
case F_SETOWN_EX:
return PPM_FCNTL_F_SETOWN_EX;
#endif
#ifdef F_GETOWN_EX
case F_GETOWN_EX:
return PPM_FCNTL_F_GETOWN_EX;
#endif
case F_SETLEASE:
return PPM_FCNTL_F_SETLEASE;
case F_GETLEASE:
return PPM_FCNTL_F_GETLEASE;
case F_CANCELLK:
return PPM_FCNTL_F_CANCELLK;
#ifdef F_DUPFD_CLOEXEC
case F_DUPFD_CLOEXEC:
return PPM_FCNTL_F_DUPFD_CLOEXEC;
#endif
case F_NOTIFY:
return PPM_FCNTL_F_NOTIFY;
#ifdef F_SETPIPE_SZ
case F_SETPIPE_SZ:
return PPM_FCNTL_F_SETPIPE_SZ;
#endif
#ifdef F_GETPIPE_SZ
case F_GETPIPE_SZ:
return PPM_FCNTL_F_GETPIPE_SZ;
#endif
#ifdef F_OFD_GETLK
case F_OFD_GETLK:
return PPM_FCNTL_F_OFD_GETLK;
#endif
#ifdef F_OFD_SETLK
case F_OFD_SETLK:
return PPM_FCNTL_F_OFD_SETLK;
#endif
#ifdef F_OFD_SETLKW
case F_OFD_SETLKW:
return PPM_FCNTL_F_OFD_SETLKW;
#endif
default:
ASSERT(false);
return PPM_FCNTL_UNKNOWN;
}
}
static __always_inline u8 sockopt_level_to_scap(int level)
{
switch (level) {
case SOL_SOCKET:
return PPM_SOCKOPT_LEVEL_SOL_SOCKET;
#ifdef SOL_TCP
case SOL_TCP:
return PPM_SOCKOPT_LEVEL_SOL_TCP;
#endif
default:
/* no ASSERT as there are legitimate other levels we don't just support yet */
return PPM_SOCKOPT_LEVEL_UNKNOWN;
}
}
static __always_inline u8 sockopt_optname_to_scap(int level, int optname)
{
if (level != SOL_SOCKET)
{
/* no ASSERT as there are legitimate other levels we don't just support yet */
return PPM_SOCKOPT_LEVEL_UNKNOWN;
}
switch (optname) {
#ifdef SO_DEBUG
case SO_DEBUG:
return PPM_SOCKOPT_SO_DEBUG;
#endif
#ifdef SO_REUSEADDR
case SO_REUSEADDR:
return PPM_SOCKOPT_SO_REUSEADDR;
#endif
#ifdef SO_TYPE
case SO_TYPE:
return PPM_SOCKOPT_SO_TYPE;
#endif
#ifdef SO_ERROR
case SO_ERROR:
return PPM_SOCKOPT_SO_ERROR;
#endif
#ifdef SO_DONTROUTE
case SO_DONTROUTE:
return PPM_SOCKOPT_SO_DONTROUTE;
#endif
#ifdef SO_BROADCAST
case SO_BROADCAST:
return PPM_SOCKOPT_SO_BROADCAST;
#endif
#ifdef SO_SNDBUF
case SO_SNDBUF:
return PPM_SOCKOPT_SO_SNDBUF;
#endif
#ifdef SO_RCVBUF
case SO_RCVBUF:
return PPM_SOCKOPT_SO_RCVBUF;
#endif
#ifdef SO_SNDBUFFORCE
case SO_SNDBUFFORCE:
return PPM_SOCKOPT_SO_SNDBUFFORCE;
#endif
#ifdef SO_RCVBUFFORCE
case SO_RCVBUFFORCE:
return PPM_SOCKOPT_SO_RCVBUFFORCE;
#endif
#ifdef SO_KEEPALIVE
case SO_KEEPALIVE:
return PPM_SOCKOPT_SO_KEEPALIVE;
#endif
#ifdef SO_OOBINLINE
case SO_OOBINLINE:
return PPM_SOCKOPT_SO_OOBINLINE;
#endif
#ifdef SO_NO_CHECK
case SO_NO_CHECK:
return PPM_SOCKOPT_SO_NO_CHECK;
#endif
#ifdef SO_PRIORITY
case SO_PRIORITY:
return PPM_SOCKOPT_SO_PRIORITY;
#endif
#ifdef SO_LINGER
case SO_LINGER:
return PPM_SOCKOPT_SO_LINGER;
#endif
#ifdef SO_BSDCOMPAT
case SO_BSDCOMPAT:
return PPM_SOCKOPT_SO_BSDCOMPAT;
#endif
#ifdef SO_REUSEPORT
case SO_REUSEPORT:
return PPM_SOCKOPT_SO_REUSEPORT;
#endif
#ifdef SO_PASSCRED
case SO_PASSCRED:
return PPM_SOCKOPT_SO_PASSCRED;
#endif
#ifdef SO_PEERCRED
case SO_PEERCRED:
return PPM_SOCKOPT_SO_PEERCRED;
#endif
#ifdef SO_RCVLOWAT
case SO_RCVLOWAT:
return PPM_SOCKOPT_SO_RCVLOWAT;
#endif
#ifdef SO_SNDLOWAT
case SO_SNDLOWAT:
return PPM_SOCKOPT_SO_SNDLOWAT;
#endif
/* We use this workaround to avoid 2 switch cases with the same value.
* An `elif` approach is not enough if `SO_RCVTIMEO` is not defined.
* In this case we have only `SO_RCVTIMEO_OLD` and `SO_RCVTIMEO_NEW` so
* we couldn't be able to detect the right flag value, for example:
* `SO_RCVTIMEO_OLD` is defined so we compile only this branch, but
* actual value of `SO_RCVTIMEO` is `SO_RCVTIMEO_NEW`.
* https://github.com/torvalds/linux/commit/a9beb86ae6e55bd92f38453c8623de60b8e5a308
*/
#ifdef SO_RCVTIMEO
case SO_RCVTIMEO:
return PPM_SOCKOPT_SO_RCVTIMEO;
#endif
#if (defined(SO_RCVTIMEO_OLD) && !defined(SO_RCVTIMEO)) || (defined(SO_RCVTIMEO_OLD) && (SO_RCVTIMEO_OLD != SO_RCVTIMEO))
case SO_RCVTIMEO_OLD:
return PPM_SOCKOPT_SO_RCVTIMEO;
#endif
#if (defined(SO_RCVTIMEO_NEW) && !defined(SO_RCVTIMEO)) || (defined(SO_RCVTIMEO_NEW) && (SO_RCVTIMEO_NEW != SO_RCVTIMEO))
case SO_RCVTIMEO_NEW:
return PPM_SOCKOPT_SO_RCVTIMEO;
#endif
/* Look at `SO_RCVTIMEO` */
#ifdef SO_SNDTIMEO
case SO_SNDTIMEO:
return PPM_SOCKOPT_SO_SNDTIMEO;
#endif
#if (defined(SO_SNDTIMEO_OLD) && !defined(SO_SNDTIMEO)) || (defined(SO_SNDTIMEO_OLD) && (SO_SNDTIMEO_OLD != SO_SNDTIMEO))
case SO_SNDTIMEO_OLD:
return PPM_SOCKOPT_SO_SNDTIMEO;
#endif
#if (defined(SO_SNDTIMEO_NEW) && !defined(SO_SNDTIMEO)) || (defined(SO_SNDTIMEO_NEW) && (SO_SNDTIMEO_NEW != SO_SNDTIMEO))
case SO_SNDTIMEO_NEW:
return PPM_SOCKOPT_SO_SNDTIMEO;
#endif
#ifdef SO_SECURITY_AUTHENTICATION
case SO_SECURITY_AUTHENTICATION:
return PPM_SOCKOPT_SO_SECURITY_AUTHENTICATION;
#endif
#ifdef SO_SECURITY_ENCRYPTION_TRANSPORT
case SO_SECURITY_ENCRYPTION_TRANSPORT:
return PPM_SOCKOPT_SO_SECURITY_ENCRYPTION_TRANSPORT;
#endif
#ifdef SO_SECURITY_ENCRYPTION_NETWORK
case SO_SECURITY_ENCRYPTION_NETWORK:
return PPM_SOCKOPT_SO_SECURITY_ENCRYPTION_NETWORK;
#endif
#ifdef SO_BINDTODEVICE
case SO_BINDTODEVICE:
return PPM_SOCKOPT_SO_BINDTODEVICE;
#endif
#ifdef SO_ATTACH_FILTER
case SO_ATTACH_FILTER:
return PPM_SOCKOPT_SO_ATTACH_FILTER;
#endif
#ifdef SO_DETACH_FILTER
case SO_DETACH_FILTER:
return PPM_SOCKOPT_SO_DETACH_FILTER;
#endif
#ifdef SO_PEERNAME
case SO_PEERNAME:
return PPM_SOCKOPT_SO_PEERNAME;
#endif
#ifdef SO_TIMESTAMP
case SO_TIMESTAMP:
return PPM_SOCKOPT_SO_TIMESTAMP;
#endif
#ifdef SO_ACCEPTCONN
case SO_ACCEPTCONN:
return PPM_SOCKOPT_SO_ACCEPTCONN;
#endif
#ifdef SO_PEERSEC
case SO_PEERSEC:
return PPM_SOCKOPT_SO_PEERSEC;
#endif
#ifdef SO_PASSSEC
case SO_PASSSEC:
return PPM_SOCKOPT_SO_PASSSEC;
#endif
#ifdef SO_TIMESTAMPNS
case SO_TIMESTAMPNS:
return PPM_SOCKOPT_SO_TIMESTAMPNS;
#endif
#ifdef SO_MARK
case SO_MARK:
return PPM_SOCKOPT_SO_MARK;
#endif
#ifdef SO_TIMESTAMPING
case SO_TIMESTAMPING:
return PPM_SOCKOPT_SO_TIMESTAMPING;
#endif
#ifdef SO_PROTOCOL
case SO_PROTOCOL:
return PPM_SOCKOPT_SO_PROTOCOL;
#endif
#ifdef SO_DOMAIN
case SO_DOMAIN:
return PPM_SOCKOPT_SO_DOMAIN;
#endif
#ifdef SO_RXQ_OVFL
case SO_RXQ_OVFL:
return PPM_SOCKOPT_SO_RXQ_OVFL;
#endif
#ifdef SO_WIFI_STATUS
case SO_WIFI_STATUS:
return PPM_SOCKOPT_SO_WIFI_STATUS;
#endif
#ifdef SO_PEEK_OFF
case SO_PEEK_OFF:
return PPM_SOCKOPT_SO_PEEK_OFF;
#endif