-
Notifications
You must be signed in to change notification settings - Fork 3
/
ppm_fillers.c
8240 lines (6916 loc) · 193 KB
/
ppm_fillers.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
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
Copyright (C) 2023 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.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/compat.h>
#include <linux/cdev.h>
#include <asm/unistd.h>
#include <net/sock.h>
#include <net/af_unix.h>
#include <net/compat.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/fdtable.h>
#include <linux/file.h>
#include <linux/fs_struct.h>
#include <linux/pid_namespace.h>
#include <linux/ptrace.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/quota.h>
#include <linux/tty.h>
#include <linux/uaccess.h>
#include <linux/audit.h>
#ifdef CONFIG_CGROUPS
#include <linux/cgroup.h>
#endif
#include <asm/syscall.h>
#include "ppm_ringbuffer.h"
#include "ppm_events_public.h"
#include "ppm_events.h"
#include "ppm.h"
#include "ppm_flag_helpers.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
#include <linux/bpf.h>
#endif
#include "kernel_hacks.h"
#include "systype_compat.h"
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
struct ovl_entry {
struct dentry *__upperdentry;
struct ovl_dir_cache *cache;
union {
struct {
uint64_t version;
const char *redirect;
bool opaque;
bool impure;
bool copying;
};
struct rcu_head rcu;
};
unsigned numlower;
struct path lowerstack[];
};
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
struct ovl_entry {
union {
struct {
unsigned long has_upper;
bool opaque;
};
struct rcu_head rcu;
};
unsigned numlower;
struct path lowerstack[];
};
#else
struct ovl_entry {
union {
struct {
unsigned long flags;
};
struct rcu_head rcu;
};
unsigned numlower;
//struct ovl_path lowerstack[];
};
enum ovl_entry_flag {
OVL_E_UPPER_ALIAS,
OVL_E_OPAQUE,
OVL_E_CONNECTED,
};
#endif
#define merge_64(hi, lo) ((((unsigned long long)(hi)) << 32) + ((lo) & 0xffffffffUL))
static inline struct pid_namespace *pid_ns_for_children(struct task_struct *task)
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
return task->nsproxy->pid_ns;
#else
return task->nsproxy->pid_ns_for_children;
#endif
}
/*
* Detect whether the file being referenced is an anonymous file created using memfd_create()
* and is being executed by referencing its file descriptor (fd). This type of file does not
* exist on disk and resides solely in memory, but it is treated as a legitimate file with an
* inode object and other file attributes.
*
**/
static inline uint32_t get_exe_from_memfd(const struct file *exe_file)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
const char expected_prefix[] = "memfd:";
if(!(exe_file &&
exe_file->f_path.dentry &&
exe_file->f_path.dentry == exe_file->f_path.dentry->d_parent))
{
return 0;
}
if(strncmp(exe_file->f_path.dentry->d_name.name, expected_prefix, sizeof(expected_prefix) - 1) == 0)
{
return PPM_EXE_FROM_MEMFD;
}
#endif
return 0;
}
int f_sys_generic(struct event_filler_arguments *args)
{
int res;
long table_index = args->syscall_id - SYSCALL_TABLE_ID0;
/*
* name
*/
if (likely(table_index >= 0 &&
table_index < SYSCALL_TABLE_SIZE)) {
ppm_sc_code sc_code = g_syscall_table[table_index].ppm_sc;
/*
* ID
*/
res = val_to_ring(args, sc_code, 0, false, 0);
CHECK_RES(res);
if (args->event_type == PPME_GENERIC_E) {
/*
* nativeID
*/
res = val_to_ring(args, args->syscall_id, 0, false, 0);
CHECK_RES(res);
}
} else {
ASSERT(false);
res = val_to_ring(args, (uint64_t)"<out of bound>", 0, false, 0);
CHECK_RES(res);
}
return add_sentinel(args);
}
int f_sys_empty(struct event_filler_arguments *args)
{
return add_sentinel(args);
}
int f_sys_single(struct event_filler_arguments *args)
{
int res;
unsigned long val;
syscall_get_arguments_deprecated(args, 0, 1, &val);
res = val_to_ring(args, val, 0, true, 0);
CHECK_RES(res);
return add_sentinel(args);
}
int f_sys_single_x(struct event_filler_arguments *args)
{
int res;
int64_t retval;
retval = (int64_t)(long)syscall_get_return_value(current, args->regs);
res = val_to_ring(args, retval, 0, false, 0);
CHECK_RES(res);
return add_sentinel(args);
}
int f_sys_fstat_e(struct event_filler_arguments *args)
{
int res = 0;
unsigned long val = 0;
int32_t fd = 0;
/* Parameter 1: fd (type: PT_FD) */
syscall_get_arguments_deprecated(args, 0, 1, &val);
fd = (int32_t)val;
res = val_to_ring(args, (int64_t)fd, 0, false, 0);
CHECK_RES(res);
return add_sentinel(args);
}
static inline void get_fd_dev_ino(int64_t fd, uint32_t* dev, uint64_t* ino)
{
struct files_struct *files;
struct fdtable *fdt;
struct file *file;
struct inode *inode;
struct super_block *sb;
if (fd < 0)
return;
files = current->files;
if (unlikely(!files))
return;
spin_lock(&files->file_lock);
fdt = files_fdtable(files);
if (unlikely(fd > fdt->max_fds))
goto out_unlock;
file = fdt->fd[fd];
if (unlikely(!file))
goto out_unlock;
inode = file_inode(file);
if (unlikely(!inode))
goto out_unlock;
*ino = inode->i_ino;
sb = inode->i_sb;
if (unlikely(!sb))
goto out_unlock;
*dev = new_encode_dev(sb->s_dev);
out_unlock:
spin_unlock(&files->file_lock);
return;
}
static inline void get_fd_fmode_created(int64_t fd, unsigned long* flags)
{
/* FMODE_CREATED flag was introduced in kernel 4.19 and it's not present in earlier versions */
#if LINUX_VERSION_CODE > KERNEL_VERSION(4, 19, 0)
struct files_struct *files;
struct fdtable *fdt;
struct file *file;
if (fd < 0)
return;
files = current->files;
if (unlikely(!files))
return;
spin_lock(&files->file_lock);
fdt = files_fdtable(files);
if (unlikely(fd > fdt->max_fds))
goto out_unlock;
file = fdt->fd[fd];
if (unlikely(!file))
goto out_unlock;
if (file->f_mode & FMODE_CREATED)
*flags |= PPM_O_F_CREATED;
out_unlock:
spin_unlock(&files->file_lock);
#endif
return;
}
int f_sys_open_e(struct event_filler_arguments *args)
{
unsigned long val;
unsigned long flags;
unsigned long modes;
char *name = NULL;
int res;
/*
* name
*/
syscall_get_arguments_deprecated(args, 0, 1, &val);
if(likely(ppm_strncpy_from_user(args->str_storage, (const void __user *)val, PPM_MAX_PATH_SIZE) >= 0))
{
name = args->str_storage;
name[PPM_MAX_PATH_SIZE - 1] = '\0';
}
res = val_to_ring(args, (int64_t)(long)name, 0, false, 0);
CHECK_RES(res);
/*
* Flags
* Note that we convert them into the ppm portable representation before pushing them to the ring
*/
syscall_get_arguments_deprecated(args, 1, 1, &flags);
res = val_to_ring(args, open_flags_to_scap(flags), 0, false, 0);
CHECK_RES(res);
/*
* mode
*/
syscall_get_arguments_deprecated(args, 2, 1, &modes);
res = val_to_ring(args, open_modes_to_scap(flags, modes), 0, false, 0);
CHECK_RES(res);
return add_sentinel(args);
}
int f_sys_open_x(struct event_filler_arguments *args)
{
unsigned long val;
unsigned long flags;
unsigned long scap_flags;
unsigned long modes;
uint32_t dev = 0;
uint64_t ino = 0;
int res;
int64_t retval;
/*
* fd
*/
retval = (int64_t)syscall_get_return_value(current, args->regs);
res = val_to_ring(args, retval, 0, false, 0);
CHECK_RES(res);
/*
* name
*/
syscall_get_arguments_deprecated(args, 0, 1, &val);
res = val_to_ring(args, val, 0, true, 0);
CHECK_RES(res);
/*
* Flags
* Note that we convert them into the ppm portable representation before pushing them to the ring
*/
syscall_get_arguments_deprecated(args, 1, 1, &flags);
scap_flags = open_flags_to_scap(flags);
/* update scap flags if file is created */
get_fd_fmode_created(retval, &scap_flags);
res = val_to_ring(args, scap_flags, 0, false, 0);
CHECK_RES(res);
/*
* mode
*/
syscall_get_arguments_deprecated(args, 2, 1, &modes);
res = val_to_ring(args, open_modes_to_scap(flags, modes), 0, false, 0);
CHECK_RES(res);
get_fd_dev_ino(retval, &dev, &ino);
/*
* dev
*/
res = val_to_ring(args, dev, 0, false, 0);
CHECK_RES(res);
/*
* ino
*/
res = val_to_ring(args, ino, 0, false, 0);
CHECK_RES(res);
return add_sentinel(args);
}
int f_sys_read_e(struct event_filler_arguments *args)
{
unsigned long val = 0;
int res = 0;
int32_t fd = 0;
/* Parameter 1: fd (type: PT_FD) */
syscall_get_arguments_deprecated(args, 0, 1, &val);
fd = (int32_t)val;
res = val_to_ring(args, (int64_t)fd, 0, false, 0);
CHECK_RES(res);
/* Parameter 2: size (type: PT_UINT32) */
syscall_get_arguments_deprecated(args, 2, 1, &val);
res = val_to_ring(args, val, 0, false, 0);
CHECK_RES(res);
return add_sentinel(args);
}
int f_sys_read_x(struct event_filler_arguments *args)
{
unsigned long val;
int res;
int64_t retval;
unsigned long bufsize;
/*
* Retrieve the FD. It will be used for dynamic snaplen calculation.
*/
syscall_get_arguments_deprecated(args, 0, 1, &val);
args->fd = (int)val;
/*
* res
*/
retval = (int64_t)(long)syscall_get_return_value(current, args->regs);
res = val_to_ring(args, retval, 0, false, 0);
CHECK_RES(res);
/*
* data
*/
if (retval < 0) {
/*
* The operation failed, return an empty buffer
*/
val = 0;
bufsize = 0;
} else {
syscall_get_arguments_deprecated(args, 1, 1, &val);
/*
* The return value can be lower than the value provided by the user,
* and we take that into account.
*/
bufsize = retval;
}
/*
* Copy the buffer
*/
args->enforce_snaplen = true;
res = val_to_ring(args, val, bufsize, true, 0);
CHECK_RES(res);
return add_sentinel(args);
}
int f_sys_write_e(struct event_filler_arguments *args)
{
unsigned long val = 0;
int res = 0;
int32_t fd = 0;
/* Parameter 1: fd (type: PT_FD) */
syscall_get_arguments_deprecated(args, 0, 1, &val);
fd = (int32_t)val;
res = val_to_ring(args, (int64_t)fd, 0, false, 0);
CHECK_RES(res);
/* Parameter 2: size (type: PT_UINT32) */
syscall_get_arguments_deprecated(args, 2, 1, &val);
res = val_to_ring(args, val, 0, false, 0);
CHECK_RES(res);
return add_sentinel(args);
}
int f_sys_write_x(struct event_filler_arguments *args)
{
unsigned long val;
int res;
int64_t retval;
unsigned long bufsize;
/*
* Retrieve the FD. It will be used for dynamic snaplen calculation.
*/
syscall_get_arguments_deprecated(args, 0, 1, &val);
args->fd = (int)val;
/* Parameter 1: res (type: PT_ERRNO) */
retval = (int64_t)(long)syscall_get_return_value(current, args->regs);
res = val_to_ring(args, retval, 0, false, 0);
CHECK_RES(res);
/* Parameter 2: data (type: PT_BYTEBUF) */
/* Get the size from userspace paramater */
syscall_get_arguments_deprecated(args, 2, 1, &val);
bufsize = retval > 0 ? retval : val;
syscall_get_arguments_deprecated(args, 1, 1, &val);
args->enforce_snaplen = true;
res = val_to_ring(args, val, bufsize, true, 0);
CHECK_RES(res);
return add_sentinel(args);
}
/*
* get_mm_exe_file is only exported in some kernel versions
*/
struct file *ppm_get_mm_exe_file(struct mm_struct *mm)
{
struct file *exe_file;
/*
* The following if/else preprocessor directive is to cover for that change:
* https://github.com/torvalds/linux/commit/90f31d0ea88880f780574f3d0bb1a227c4c66ca3#diff-e37b5cb4c23f6ab27741c60ec48674eff0268624a228c9a1cddddb9e4ee2922dL709
* That was introduced in linux 4.1, but it's backported in some distro kernels.
* Luckily enough, `get_file_rcu` is a define, so we can check for it and use
* the safer version.
*/
#if defined(get_file_rcu)
rcu_read_lock();
exe_file = rcu_dereference(mm->exe_file);
if (exe_file && !get_file_rcu(exe_file))
exe_file = NULL;
rcu_read_unlock();
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 7, 0)
// Since linux 6.7.0, `get_file_rcu` is no more a define and takes a double pointer parameter.
// See https://github.com/torvalds/linux/commit/0ede61d8589cc2d93aa78230d74ac58b5b8d0244.
rcu_read_lock();
exe_file = get_file_rcu(&mm->exe_file);
rcu_read_unlock();
#else
/* We need mmap_sem to protect against races with removal of
* VM_EXECUTABLE vmas */
down_read(&mm->mmap_sem);
exe_file = mm->exe_file;
if (exe_file)
get_file(exe_file);
up_read(&mm->mmap_sem);
#endif
return exe_file;
}
/*
* get_mm_counter was not inline and exported between 3.0 and 3.4
* https://github.com/torvalds/linux/commit/69c978232aaa99476f9bd002c2a29a84fa3779b5
* Hence the crap in these two functions
*/
unsigned long ppm_get_mm_counter(struct mm_struct *mm, int member)
{
long val = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
val = get_mm_counter(mm, member);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
val = atomic_long_read(&mm->rss_stat.count[member]);
if (val < 0)
val = 0;
#endif
return val;
}
static unsigned long ppm_get_mm_swap(struct mm_struct *mm)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
return ppm_get_mm_counter(mm, MM_SWAPENTS);
#endif
return 0;
}
static unsigned long ppm_get_mm_rss(struct mm_struct *mm)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
return get_mm_rss(mm);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
return ppm_get_mm_counter(mm, MM_FILEPAGES) +
ppm_get_mm_counter(mm, MM_ANONPAGES);
#else
return get_mm_rss(mm);
#endif
return 0;
}
#ifdef CONFIG_CGROUPS
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34)
static int ppm_cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
{
char *start;
struct dentry *dentry = rcu_dereference(cgrp->dentry);
if (!dentry) {
/*
* Inactive subsystems have no dentry for their root
* cgroup
*/
strcpy(buf, "/");
return 0;
}
start = buf + buflen;
*--start = '\0';
for (;;) {
int len = dentry->d_name.len;
start -= len;
if (start < buf)
return -ENAMETOOLONG;
memcpy(start, cgrp->dentry->d_name.name, len);
cgrp = cgrp->parent;
if (!cgrp)
break;
dentry = rcu_dereference(cgrp->dentry);
if (!cgrp->parent)
continue;
if (--start < buf)
return -ENAMETOOLONG;
*start = '/';
}
memmove(buf, start, buf + buflen - start);
return 0;
}
#endif
static int append_cgroup(const char *subsys_name, int subsys_id, char *buf, int *available)
{
int pathlen;
int subsys_len;
char *path;
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) || LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
int res;
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0)
struct cgroup_subsys_state *css = task_css(current, subsys_id);
#else
struct cgroup_subsys_state *css = task_subsys_state(current, subsys_id);
#endif
if (!css) {
ASSERT(false);
return 1;
}
if (!css->cgroup) {
ASSERT(false);
return 1;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
// According to https://github.com/torvalds/linux/commit/4c737b41de7f4eef2a593803bad1b918dd718b10
// cgroup_path now returns an int again
res = cgroup_path(css->cgroup, buf, *available);
if (res < 0) {
ASSERT(false);
path = "NA";
} else {
path = buf;
}
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)
path = cgroup_path(css->cgroup, buf, *available);
if (!path) {
ASSERT(false);
path = "NA";
}
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
res = cgroup_path(css->cgroup, buf, *available);
if (res < 0) {
ASSERT(false);
path = "NA";
} else {
path = buf;
}
#else
res = ppm_cgroup_path(css->cgroup, buf, *available);
if (res < 0) {
ASSERT(false);
path = "NA";
} else {
path = buf;
}
#endif
pathlen = strlen(path);
subsys_len = strlen(subsys_name);
if (subsys_len + 1 + pathlen + 1 > *available)
return 1;
memmove(buf + subsys_len + 1, path, pathlen);
memcpy(buf, subsys_name, subsys_len);
buf += subsys_len;
*buf++ = '=';
buf += pathlen;
*buf++ = 0;
*available -= (subsys_len + 1 + pathlen + 1);
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)
#define SUBSYS(_x) \
if (append_cgroup(#_x, _x ## _cgrp_id, args->str_storage + STR_STORAGE_SIZE - available, &available)) \
goto cgroups_error;
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
#define SUBSYS(_x) \
if (append_cgroup(#_x, _x ## _subsys_id, args->str_storage + STR_STORAGE_SIZE - available, &available)) \
goto cgroups_error;
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
#define IS_SUBSYS_ENABLED(option) IS_ENABLED(option)
#define SUBSYS(_x) \
if (append_cgroup(#_x, _x ## _subsys_id, args->str_storage + STR_STORAGE_SIZE - available, &available)) \
goto cgroups_error;
#else
#define SUBSYS(_x) \
if (append_cgroup(#_x, _x ## _subsys_id, args->str_storage + STR_STORAGE_SIZE - available, &available)) \
goto cgroups_error;
#endif
#endif
/* Takes in a NULL-terminated array of pointers to strings in userspace, and
* concatenates them to a single \0-separated string. Return the length of this
* string, or <0 on error */
int accumulate_argv_or_env(const char __user * __user *argv,
char *str_storage,
int available)
{
int len = 0;
int n_bytes_copied;
if (argv == NULL)
return len;
for (;;) {
const char __user *p;
if (unlikely(ppm_get_user(p, argv)))
return PPM_FAILURE_INVALID_USER_MEMORY;
if (p == NULL)
break;
/* need at least enough space for a \0 */
if (available < 1)
return PPM_FAILURE_BUFFER_FULL;
n_bytes_copied = ppm_strncpy_from_user(&str_storage[len], p,
available);
/* ppm_strncpy_from_user includes the trailing \0 in its return
* count. I want to pretend it was strncpy_from_user() so I
* subtract off the 1 */
n_bytes_copied--;
if (n_bytes_copied < 0)
return PPM_FAILURE_INVALID_USER_MEMORY;
if (n_bytes_copied >= available)
return PPM_FAILURE_BUFFER_FULL;
/* update buffer. I want to keep the trailing \0, so I +1 */
available -= n_bytes_copied+1;
len += n_bytes_copied+1;
argv++;
}
return len;
}
#ifdef CONFIG_COMPAT
/* compat version that deals correctly with 32bits pointers of argv */
static int compat_accumulate_argv_or_env(compat_uptr_t argv,
char *str_storage,
int available)
{
int len = 0;
int n_bytes_copied;
if (compat_ptr(argv) == NULL)
return len;
for (;;) {
compat_uptr_t compat_p;
const char __user *p;
if (unlikely(ppm_get_user(compat_p, compat_ptr(argv))))
return PPM_FAILURE_INVALID_USER_MEMORY;
p = compat_ptr(compat_p);
if (p == NULL)
break;
/* need at least enough space for a \0 */
if (available < 1)
return PPM_FAILURE_BUFFER_FULL;
n_bytes_copied = ppm_strncpy_from_user(&str_storage[len], p,
available);
/* ppm_strncpy_from_user includes the trailing \0 in its return
* count. I want to pretend it was strncpy_from_user() so I
* subtract off the 1 */
n_bytes_copied--;
if (n_bytes_copied < 0) {
return PPM_FAILURE_INVALID_USER_MEMORY;
}
if (n_bytes_copied >= available)
return PPM_FAILURE_BUFFER_FULL;
/* update buffer. I want to keep the trailing \0, so I +1 */
available -= n_bytes_copied+1;
len += n_bytes_copied+1;
argv += sizeof(compat_uptr_t);
}
return len;
}
#endif
static uint32_t ppm_get_tty(void)
{
/* Locking of the signal structures seems too complicated across
* multiple kernel versions to get it right, so simply do protected
* memory accesses, and in the worst case we get some garbage,
* which is not the end of the world. In the vast majority of accesses,
* we'll be just fine.
*/
struct signal_struct *sig;
struct tty_struct *tty;
struct tty_driver *driver;
int major;
int minor_start;
int index;
uint32_t tty_nr = 0;
sig = current->signal;
if (!sig)
return 0;
if (unlikely(copy_from_kernel_nofault(&tty, &sig->tty, sizeof(tty))))
return 0;
if (!tty)
return 0;
if (unlikely(copy_from_kernel_nofault(&index, &tty->index, sizeof(index))))
return 0;
if (unlikely(copy_from_kernel_nofault(&driver, &tty->driver, sizeof(driver))))
return 0;
if (!driver)
return 0;
if (unlikely(copy_from_kernel_nofault(&major, &driver->major, sizeof(major))))
return 0;
if (unlikely(copy_from_kernel_nofault(&minor_start, &driver->minor_start, sizeof(minor_start))))
return 0;
tty_nr = new_encode_dev(MKDEV(major, minor_start) + index);
return tty_nr;
}
bool ppm_is_upper_layer(struct file *exe_file){
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
struct super_block *sb = NULL;
unsigned long sb_magic = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
sb = exe_file->f_path.dentry->d_sb;
#else
sb = exe_file->f_inode->i_sb;
#endif
if(sb)
{
struct ovl_entry *oe = (struct ovl_entry*)(exe_file->f_path.dentry->d_fsdata);
sb_magic = sb->s_magic;
if(sb_magic == PPM_OVERLAYFS_SUPER_MAGIC && oe)
{
unsigned long has_upper = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
if(oe->__upperdentry)
{
return true;
}
#else
struct dentry *upper_dentry = NULL;
unsigned int d_flags = exe_file->f_path.dentry->d_flags;
bool disconnected = (d_flags & DCACHE_DISCONNECTED);
// Pointer arithmetics due to unexported ovl_inode struct
// warning: this works if and only if the dentry pointer
// is placed right after the inode struct
upper_dentry = (struct dentry *)((char *)exe_file->f_path.dentry->d_inode + sizeof(struct inode));
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
has_upper = oe->has_upper;
#elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 5, 0)
has_upper = test_bit(OVL_E_UPPER_ALIAS, &(oe->flags));
#else
has_upper = test_bit(OVL_E_UPPER_ALIAS, (unsigned long*)&oe);
#endif
if(upper_dentry && (has_upper || disconnected))
{
return true;
}
#endif
}
}
#endif
return false;
}
int f_proc_startupdate(struct event_filler_arguments *args)
{
unsigned long val = 0;
int res = 0;
unsigned int exe_len = 0; /* the length of the executable string */
int args_len = 0; /*the combined length of the arguments string + executable string */
struct mm_struct *mm = current->mm;
int64_t retval;
int ptid;
long total_vm = 0;
long total_rss = 0;
long swap = 0;
int available = STR_STORAGE_SIZE;
const struct cred *cred;
uint64_t pidns_init_start_time = 0;
#ifdef __NR_clone3
struct clone_args cl_args;
#endif
/*
* Make sure the operation was successful
*/
retval = (int64_t)syscall_get_return_value(current, args->regs);
res = val_to_ring(args, retval, 0, false, 0);
CHECK_RES(res);
if (unlikely(retval < 0 &&
args->event_type != PPME_SYSCALL_EXECVE_19_X &&
args->event_type != PPME_SYSCALL_EXECVEAT_X)) {
/* The call failed, but this syscall has no exe, args
* anyway, so I report empty ones */
*args->str_storage = 0;
/*
* exe
*/
res = val_to_ring(args, (uint64_t)(long)args->str_storage, 0, false, 0);
CHECK_RES(res);
/*
* Args
*/
res = val_to_ring(args, (int64_t)(long)args->str_storage, 0, false, 0);
CHECK_RES(res);
} else {
if (likely(retval >= 0)) {
/*
* The call succeeded. Get exe, args from the current
* process; put one \0-separated exe-args string into
* str_storage
*/
if (unlikely(!mm)) {
args->str_storage[0] = 0;
pr_info("f_proc_startupdate drop, mm=NULL\n");
return PPM_FAILURE_BUG;
}
if (unlikely(!mm->arg_end)) {
args->str_storage[0] = 0;
pr_info("f_proc_startupdate drop, mm->arg_end=NULL\n");
return PPM_FAILURE_BUG;
}
args_len = mm->arg_end - mm->arg_start;
if (args_len) {
if (args_len > PAGE_SIZE)
args_len = PAGE_SIZE;
if (unlikely(ppm_copy_from_user(args->str_storage, (const void __user *)mm->arg_start, args_len)))
args_len = 0;
else
args->str_storage[args_len - 1] = 0;