-
Notifications
You must be signed in to change notification settings - Fork 726
/
ppm_fillers.c
5085 lines (4347 loc) · 110 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
/*
Copyright (c) 2013-2018 Draios Inc. dba Sysdig.
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
#ifndef UDIG
#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
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 20)
#include "ppm_syscall.h"
#else
#include <asm/syscall.h>
#endif
#else /* UDIG */
#define _GNU_SOURCE
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
#include <limits.h>
#include <unistd.h>
#include <sys/mman.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <time.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <poll.h>
#include <sys/sem.h>
#include <sys/file.h>
#include <sys/quota.h>
#include <sys/ptrace.h>
#include "udig_capture.h"
#include "ppm_ringbuffer.h"
#include "ppm_events_public.h"
#include "ppm_events.h"
#include "ppm.h"
#include "udig_inf.h"
#endif /* UDIG */
#include "ppm_ringbuffer.h"
#include "ppm_events_public.h"
#include "ppm_events.h"
#include "ppm.h"
#include "ppm_flag_helpers.h"
#ifndef UDIG
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
#include <linux/bpf.h>
#endif
#include "kernel_hacks.h"
#endif /* UDIG */
#define merge_64(hi, lo) ((((unsigned long long)(hi)) << 32) + ((lo) & 0xffffffffUL))
#ifndef UDIG
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
}
#endif /* UDIG */
int f_sys_generic(struct event_filler_arguments *args)
{
int res;
long table_index = args->syscall_id - SYSCALL_TABLE_ID0;
const enum ppm_syscall_code *cur_g_syscall_code_routing_table = args->cur_g_syscall_code_routing_table;
#ifdef _HAS_SOCKETCALL
if (unlikely(args->syscall_id == args->socketcall_syscall)) {
/*
* All the socket calls should be implemented
*/
ASSERT(false);
return PPM_FAILURE_BUG;
}
#endif
/*
* name
*/
if (likely(table_index >= 0 &&
table_index < SYSCALL_TABLE_SIZE)) {
enum ppm_syscall_code sc_code = cur_g_syscall_code_routing_table[table_index];
/*
* ID
*/
res = val_to_ring(args, sc_code, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
if (args->event_type == PPME_GENERIC_E) {
/*
* nativeID
*/
res = val_to_ring(args, args->syscall_id, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
}
} else {
ASSERT(false);
res = val_to_ring(args, (unsigned long)"<out of bound>", 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return 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(current, args->regs, 0, 1, &val);
res = val_to_ring(args, val, 0, true, 0);
if (unlikely(res != PPM_SUCCESS))
return 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);
if (unlikely(res != PPM_SUCCESS))
return res;
return add_sentinel(args);
}
static inline uint32_t get_fd_dev(int64_t fd)
{
#ifdef UDIG
return 0;
#else
struct files_struct *files;
struct fdtable *fdt;
struct file *file;
struct inode *inode;
struct super_block *sb;
uint32_t dev = 0;
if (fd < 0)
return dev;
files = current->files;
if (unlikely(!files))
return dev;
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;
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 dev;
#endif /* UDIG */
}
int f_sys_open_x(struct event_filler_arguments *args)
{
unsigned long val;
unsigned long flags;
unsigned long modes;
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);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* name
*/
syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val);
res = val_to_ring(args, val, 0, true, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* Flags
* Note that we convert them into the ppm portable representation before pushing them to the ring
*/
syscall_get_arguments_deprecated(current, args->regs, 1, 1, &flags);
res = val_to_ring(args, open_flags_to_scap(flags), 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* mode
*/
syscall_get_arguments_deprecated(current, args->regs, 2, 1, &modes);
res = val_to_ring(args, open_modes_to_scap(flags, modes), 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* dev
*/
res = val_to_ring(args, get_fd_dev(retval), 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return 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(current, args->regs, 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);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* data
*/
if (retval < 0) {
/*
* The operation failed, return an empty buffer
*/
val = 0;
bufsize = 0;
} else {
syscall_get_arguments_deprecated(current, args->regs, 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);
if (unlikely(res != PPM_SUCCESS))
return 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(current, args->regs, 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);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* data
*/
syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val);
bufsize = val;
/*
* Copy the buffer
*/
syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val);
args->enforce_snaplen = true;
res = val_to_ring(args, val, bufsize, true, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
return add_sentinel(args);
}
#ifndef UDIG
/*
* 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
#endif /* UDIG */
/* 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;
}
#ifndef UDIG
#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 int 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;
int 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;
}
#endif /* UDIG */
int f_proc_startupdate(struct event_filler_arguments *args)
{
#ifdef UDIG
return udig_proc_startupdate(args);
#else /* UDIG */
unsigned long val;
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;
char *spwd = "";
long total_vm = 0;
long total_rss = 0;
long swap = 0;
int available = STR_STORAGE_SIZE;
/*
* 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);
if (unlikely(res != PPM_SUCCESS))
return res;
if (unlikely(retval < 0 &&
args->event_type != PPME_SYSCALL_EXECVE_19_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);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* Args
*/
res = val_to_ring(args, (int64_t)(long)args->str_storage, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return 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;
}
} else {
/*
* The execve call failed. I get exe, args from the
* input args; put one \0-separated exe-args string into
* str_storage
*/
args->str_storage[0] = 0;
syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val);
#ifdef CONFIG_COMPAT
if (unlikely(args->compat))
args_len = compat_accumulate_argv_or_env((compat_uptr_t)val,
args->str_storage, available);
else
#endif
args_len = accumulate_argv_or_env((const char __user * __user *)val,
args->str_storage, available);
if (unlikely(args_len < 0))
args_len = 0;
}
if (args_len == 0)
*args->str_storage = 0;
exe_len = strnlen(args->str_storage, args_len);
if (exe_len < args_len)
++exe_len;
/*
* exe
*/
res = val_to_ring(args, (uint64_t)(long)args->str_storage, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* Args
*/
res = val_to_ring(args, (int64_t)(long)args->str_storage + exe_len, args_len - exe_len, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
}
/*
* tid
*/
res = val_to_ring(args, (int64_t)current->pid, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* pid
*/
res = val_to_ring(args, (int64_t)current->tgid, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* ptid
*/
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
if (current->real_parent)
ptid = current->real_parent->pid;
#else
if (current->parent)
ptid = current->parent->pid;
#endif
else
ptid = 0;
res = val_to_ring(args, (int64_t)ptid, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* cwd, pushed empty to avoid breaking compatibility
* with the older event format
*/
res = val_to_ring(args, (uint64_t)(long)spwd, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* fdlimit
*/
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
res = val_to_ring(args, (int64_t)rlimit(RLIMIT_NOFILE), 0, false, 0);
#else
res = val_to_ring(args, (int64_t)0, 0, false, 0);
#endif
if (res != PPM_SUCCESS)
return res;
/*
* pgft_maj
*/
res = val_to_ring(args, current->maj_flt, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* pgft_min
*/
res = val_to_ring(args, current->min_flt, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
if (mm) {
total_vm = mm->total_vm << (PAGE_SHIFT-10);
total_rss = ppm_get_mm_rss(mm) << (PAGE_SHIFT-10);
swap = ppm_get_mm_swap(mm) << (PAGE_SHIFT-10);
}
/*
* vm_size
*/
res = val_to_ring(args, total_vm, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* vm_rss
*/
res = val_to_ring(args, total_rss, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* vm_swap
*/
res = val_to_ring(args, swap, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* comm
*/
res = val_to_ring(args, (uint64_t)current->comm, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* cgroups
*/
args->str_storage[0] = 0;
#ifdef CONFIG_CGROUPS
rcu_read_lock();
#include <linux/cgroup_subsys.h>
cgroups_error:
rcu_read_unlock();
#endif
res = val_to_ring(args, (int64_t)(long)args->str_storage, STR_STORAGE_SIZE - available, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
if (args->event_type == PPME_SYSCALL_CLONE_20_X ||
args->event_type == PPME_SYSCALL_FORK_20_X ||
args->event_type == PPME_SYSCALL_VFORK_20_X) {
/*
* clone-only parameters
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
uint64_t euid = from_kuid_munged(current_user_ns(), current_euid());
uint64_t egid = from_kgid_munged(current_user_ns(), current_egid());
#elif LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
uint64_t euid = current_euid();
uint64_t egid = current_egid();
#else
uint64_t euid = current->euid;
uint64_t egid = current->egid;
#endif
int64_t in_pidns = 0;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
struct pid_namespace *pidns = task_active_pid_ns(current);
#endif
/*
* flags
*/
if (args->event_type == PPME_SYSCALL_CLONE_20_X) {
#ifdef CONFIG_S390
syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val);
#else
syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val);
#endif
} else
val = 0;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
if(pidns != &init_pid_ns || pid_ns_for_children(current) != pidns)
in_pidns = PPM_CL_CHILD_IN_PIDNS;
#endif
res = val_to_ring(args, (uint64_t)clone_flags_to_scap(val) | in_pidns, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* uid
*/
res = val_to_ring(args, euid, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* gid
*/
res = val_to_ring(args, egid, 0, false, 0);
if (unlikely(res != PPM_SUCCESS))
return res;
/*
* vtid
*/
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
res = val_to_ring(args, task_pid_vnr(current), 0, false, 0);