-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathexecve_x.cpp
1193 lines (875 loc) · 37.6 KB
/
execve_x.cpp
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
#include "../../event_class/event_class.h"
#include "../../flags/flags_definitions.h"
#include "../../helpers/proc_parsing.h"
#include "sys/mount.h"
#if defined(__NR_execve) && defined(__NR_capget) && defined(__NR_clone3) && defined(__NR_wait4)
#include <linux/sched.h>
TEST(SyscallExit, execveX_failure)
{
auto evt_test = get_syscall_event_test(__NR_execve, EXIT_EVENT);
evt_test->enable_capture();
/*=============================== TRIGGER SYSCALL ===========================*/
/* Get all the info from proc. */
struct proc_info info = {};
pid_t pid = ::getpid();
if(!get_proc_info(pid, &info))
{
FAIL() << "Unable to get all the info from proc" << std::endl;
}
/*
* Get the process capabilities.
*/
/* On kernels >= 5.8 the suggested version should be `_LINUX_CAPABILITY_VERSION_3` */
struct __user_cap_header_struct header = {};
struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3];
cap_user_header_t hdrp = &header;
cap_user_data_t datap = data;
/* Prepare the header. */
header.pid = 0; /* `0` means the pid of the actual process. */
header.version = _LINUX_CAPABILITY_VERSION_3;
assert_syscall_state(SYSCALL_SUCCESS, "capget", syscall(__NR_capget, hdrp, datap), EQUAL, 0);
/*
* Call the `execve`
*/
char pathname[] = "//**null-file-path**//";
std::string too_long_arg (4096, 'x');
const char *newargv[] = {pathname, "", "first_argv", "", too_long_arg.c_str(), "second_argv", NULL};
std::string truncated_too_long_arg (4096 - (strlen(pathname)+1) - (strlen("first_argv")+1) - 2*(strlen("")+1) - 1, 'x');
const char *expected_newargv[] = {pathname, "", "first_argv", "", truncated_too_long_arg.c_str(), NULL};
const char *newenviron[] = {"IN_TEST=yes", "3_ARGUMENT=yes", too_long_arg.c_str(), "2_ARGUMENT=no", NULL};
std::string truncated_too_long_env (4096 - (strlen("IN_TEST=yes")+1) - (strlen("3_ARGUMENT=yes")+1) - 1, 'x');
const char *expected_newenviron[] = {"IN_TEST=yes", "3_ARGUMENT=yes", truncated_too_long_env.c_str(), NULL};
bool expect_truncated = true;
if(evt_test->is_kmod_engine() && getpagesize() > 4096)
{
// for kmod, the size limit is actually PAGE_SIZE;
// see STR_STORAGE_SIZE macro definition in driver/capture_macro.h.
// In case PAGE_SIZE is < 4096, expect NON-truncated args/envs
expect_truncated = false;
}
assert_syscall_state(SYSCALL_FAILURE, "execve", syscall(__NR_execve, pathname, newargv, newenviron));
int64_t errno_value = -errno;
/*=============================== TRIGGER SYSCALL ===========================*/
evt_test->disable_capture();
evt_test->assert_event_presence();
if(HasFatalFailure())
{
return;
}
evt_test->parse_event();
evt_test->assert_header();
/*=============================== ASSERT PARAMETERS ===========================*/
/* Parameter 1: res (type: PT_ERRNO)*/
evt_test->assert_numeric_param(1, (int64_t)errno_value);
/* Parameter 2: exe (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(2, pathname);
/* Parameter 3: args (type: PT_CHARBUFARRAY) */
/* Starting from `1` because the first is `exe`. */
if (expect_truncated)
{
evt_test->assert_charbuf_array_param(3, &expected_newargv[1]);
}
else
{
evt_test->assert_charbuf_array_param(3, &newargv[1]);
}
/* Parameter 4: tid (type: PT_PID) */
evt_test->assert_numeric_param(4, (int64_t)pid);
/* Parameter 5: pid (type: PT_PID) */
/* We are the main thread of the process so it's equal to `tid`. */
evt_test->assert_numeric_param(5, (int64_t)pid);
/* Parameter 6: ptid (type: PT_PID) */
evt_test->assert_numeric_param(6, (int64_t)info.ppid);
/* Parameter 7: cwd (type: PT_CHARBUF) */
/* leave the current working directory empty like in the old probe. */
evt_test->assert_empty_param(7);
/* Parameter 8: fdlimit (type: PT_UINT64) */
evt_test->assert_numeric_param(8, (uint64_t)info.file_rlimit.rlim_cur);
/* Parameter 9: pgft_maj (type: PT_UINT64) */
/* Right now we can't find a precise value to perform the assertion. */
evt_test->assert_numeric_param(9, (uint64_t)0, GREATER_EQUAL);
/* Parameter 10: pgft_min (type: PT_UINT64) */
/* Right now we can't find a precise value to perform the assertion. */
evt_test->assert_numeric_param(10, (uint64_t)0, GREATER_EQUAL);
/* Parameter 11: vm_size (type: PT_UINT32) */
evt_test->assert_numeric_param(11, (uint32_t)0, GREATER_EQUAL);
/* Parameter 12: vm_rss (type: PT_UINT32) */
evt_test->assert_numeric_param(12, (uint32_t)0, GREATER_EQUAL);
/* Parameter 13: vm_swap (type: PT_UINT32) */
evt_test->assert_numeric_param(13, (uint32_t)0, GREATER_EQUAL);
/* Parameter 14: comm (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(14, TEST_EXECUTABLE_NAME);
/* Parameter 15: cgroups (type: PT_CHARBUFARRAY) */
evt_test->assert_cgroup_param(15);
/* Parameter 16: env (type: PT_CHARBUFARRAY) */
if (expect_truncated)
{
evt_test->assert_charbuf_array_param(16, &expected_newenviron[0]);
}
else
{
evt_test->assert_charbuf_array_param(16, &newenviron[0]);
}
/* Parameter 17: tty (type: PT_UINT32) */
evt_test->assert_numeric_param(17, (uint32_t)info.tty);
/* Parameter 18: pgid (type: PT_PID) */
evt_test->assert_numeric_param(18, (int64_t)info.pgid);
/* Parameter 19: loginuid (type: PT_UID) */
evt_test->assert_numeric_param(19, (uint32_t)info.loginuid);
/* PPM_EXE_WRITABLE is set when the user that executed a process can also write to the executable
* file that is used to spawn it or is its owner or otherwise capable.
*/
evt_test->assert_numeric_param(20, (uint32_t)PPM_EXE_WRITABLE);
/* Parameter 21: cap_inheritable (type: PT_UINT64) */
evt_test->assert_numeric_param(21, (uint64_t)capabilities_to_scap(((unsigned long)data[1].inheritable << 32) | data[0].inheritable));
/* Parameter 22: cap_permitted (type: PT_UINT64) */
evt_test->assert_numeric_param(22, (uint64_t)capabilities_to_scap(((unsigned long)data[1].permitted << 32) | data[0].permitted));
/* Parameter 23: cap_effective (type: PT_UINT64) */
evt_test->assert_numeric_param(23, (uint64_t)capabilities_to_scap(((unsigned long)data[1].effective << 32) | data[0].effective));
/* Parameter 24: exe_file ino (type: PT_UINT64) */
evt_test->assert_numeric_param(24, (uint64_t)1, GREATER_EQUAL);
/* Parameter 25: exe_file ctime (last status change time, epoch value in nanoseconds) (type: PT_ABSTIME) */
evt_test->assert_numeric_param(25, (uint64_t)1000000000000000000, GREATER_EQUAL);
/* Parameter 26: exe_file mtime (last modification time, epoch value in nanoseconds) (type: PT_ABSTIME) */
evt_test->assert_numeric_param(26, (uint64_t)1000000000000000000, GREATER_EQUAL);
/* Parameter 27: euid (type: PT_UID) */
evt_test->assert_numeric_param(27, (uint32_t)geteuid(), EQUAL);
/* Parameter 28: trusted_exepath (type: PT_FSPATH) */
/* Here we don't call the execve so the result should be the full path to the drivers test executable */
evt_test->assert_charbuf_param(28, info.exepath);
/*=============================== ASSERT PARAMETERS ===========================*/
evt_test->assert_num_params_pushed(28);
}
TEST(SyscallExit, execveX_failure_args_env_NULL)
{
auto evt_test = get_syscall_event_test(__NR_execve, EXIT_EVENT);
evt_test->enable_capture();
/*=============================== TRIGGER SYSCALL ===========================*/
char pathname[] = "//args_env_NULL//";
assert_syscall_state(SYSCALL_FAILURE, "execve", syscall(__NR_execve, pathname, NULL, NULL));
int64_t errno_value = -errno;
/*=============================== TRIGGER SYSCALL ===========================*/
evt_test->disable_capture();
evt_test->assert_event_presence();
if(HasFatalFailure())
{
return;
}
evt_test->parse_event();
evt_test->assert_header();
/*=============================== ASSERT PARAMETERS ===========================*/
/* Parameter 1: res (type: PT_ERRNO)*/
evt_test->assert_numeric_param(1, (int64_t)errno_value);
/* Parameter 2: exe (type: PT_CHARBUF) */
/* exe is taken from the args and not from the pathname. */
evt_test->assert_charbuf_param(2, "");
/* Parameter 3: args (type: PT_CHARBUFARRAY) */
evt_test->assert_empty_param(3);
/* Parameter 16: env (type: PT_CHARBUFARRAY) */
evt_test->assert_empty_param(16);
/*=============================== ASSERT PARAMETERS ===========================*/
evt_test->assert_num_params_pushed(28);
}
TEST(SyscallExit, execveX_failure_path_NULL_but_not_args)
{
auto evt_test = get_syscall_event_test(__NR_execve, EXIT_EVENT);
evt_test->enable_capture();
/*=============================== TRIGGER SYSCALL ===========================*/
char pathname[] = "//path_NULL_but_not_args//";
const char *newargv[] = {"", NULL};
const char *newenviron[] = {"", NULL};
assert_syscall_state(SYSCALL_FAILURE, "execve", syscall(__NR_execve, pathname, newargv, newenviron));
int64_t errno_value = -errno;
/*=============================== TRIGGER SYSCALL ===========================*/
evt_test->disable_capture();
evt_test->assert_event_presence();
if(HasFatalFailure())
{
return;
}
evt_test->parse_event();
evt_test->assert_header();
/*=============================== ASSERT PARAMETERS ===========================*/
/* Parameter 1: res (type: PT_ERRNO)*/
evt_test->assert_numeric_param(1, (int64_t)errno_value);
/* Parameter 2: exe (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(2, "");
/* Parameter 3: args (type: PT_CHARBUFARRAY) */
evt_test->assert_empty_param(3);
/* Parameter 16: env (type: PT_CHARBUFARRAY) */
evt_test->assert_charbuf_array_param(16, &newenviron[0]);
/*=============================== ASSERT PARAMETERS ===========================*/
evt_test->assert_num_params_pushed(28);
}
TEST(SyscallExit, execveX_success)
{
auto evt_test = get_syscall_event_test(__NR_execve, EXIT_EVENT);
evt_test->enable_capture();
/*=============================== TRIGGER SYSCALL ===========================*/
/* Prepare the execve args */
const char *pathname = "/usr/bin/true";
const char *comm = "true";
std::string too_long_arg (4096, 'x');
const char *newargv[] = {pathname, "", "first_argv", "", too_long_arg.c_str(), "second_argv", NULL};
std::string truncated_too_long_arg (4096 - (strlen(pathname)+1) - (strlen("first_argv")+1) - 2*(strlen("")+1) - 1, 'x');
const char *expected_newargv[] = {pathname, "", "first_argv", "", truncated_too_long_arg.c_str(), NULL};
const char *newenviron[] = {"IN_TEST=yes", "3_ARGUMENT=yes", too_long_arg.c_str(), "2_ARGUMENT=no", NULL};
std::string truncated_too_long_env (4096 - (strlen("IN_TEST=yes")+1) - (strlen("3_ARGUMENT=yes")+1) - 1, 'x');
const char *expected_newenviron[] = {"IN_TEST=yes", "3_ARGUMENT=yes", truncated_too_long_env.c_str(), NULL};
bool expect_truncated = true;
if(evt_test->is_kmod_engine() && getpagesize() > 4096)
{
// for kmod, the size limit is actually PAGE_SIZE;
// see STR_STORAGE_SIZE macro definition in driver/capture_macro.h.
// In case PAGE_SIZE is < 4096, expect NON-truncated args/envs
expect_truncated = false;
}
/* We need to use `SIGCHLD` otherwise the parent won't receive any signal
* when the child terminates.
*/
clone_args cl_args = {};
cl_args.exit_signal = SIGCHLD;
pid_t ret_pid = syscall(__NR_clone3, &cl_args, sizeof(cl_args));
if(ret_pid == 0)
{
syscall(__NR_execve, pathname, newargv, newenviron);
exit(EXIT_FAILURE);
}
assert_syscall_state(SYSCALL_SUCCESS, "clone3", ret_pid, NOT_EQUAL, -1);
/* Catch the child before doing anything else. */
int status = 0;
int options = 0;
assert_syscall_state(SYSCALL_SUCCESS, "wait4", syscall(__NR_wait4, ret_pid, &status, options, NULL), NOT_EQUAL, -1);
if(__WEXITSTATUS(status) == EXIT_FAILURE || __WIFSIGNALED(status) != 0)
{
FAIL() << "The child execve failed." << std::endl;
}
/*=============================== TRIGGER SYSCALL ===========================*/
evt_test->disable_capture();
/* We search for a child event. */
evt_test->assert_event_presence(ret_pid);
if(HasFatalFailure())
{
return;
}
evt_test->parse_event();
evt_test->assert_header();
/*=============================== ASSERT PARAMETERS ===========================*/
/* Please note here we cannot assert all the params, we check only the possible ones. */
/* Parameter 1: res (type: PT_ERRNO)*/
evt_test->assert_numeric_param(1, (int64_t)0);
/* Parameter 2: exe (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(2, pathname);
/* Parameter 3: args (type: PT_CHARBUFARRAY) */
/* Starting from `1` because the first is `exe`. */
if (expect_truncated)
{
evt_test->assert_charbuf_array_param(3, &expected_newargv[1]);
}
else
{
evt_test->assert_charbuf_array_param(3, &newargv[1]);
}
/* Parameter 4: tid (type: PT_PID) */
evt_test->assert_numeric_param(4, (int64_t)ret_pid);
/* Parameter 5: pid (type: PT_PID) */
/* We are the main thread of the process so it's equal to `tid`. */
evt_test->assert_numeric_param(5, (int64_t)ret_pid);
/* Parameter 6: ptid (type: PT_PID) */
evt_test->assert_numeric_param(6, (int64_t)::getpid());
/* Parameter 7: cwd (type: PT_CHARBUF) */
/* leave the current working directory empty like in the old probe. */
evt_test->assert_empty_param(7);
/* Parameter 14: comm (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(14, comm);
/* Parameter 15: cgroups (type: PT_CHARBUFARRAY) */
evt_test->assert_cgroup_param(15);
/* Parameter 16: env (type: PT_CHARBUFARRAY) */
if (expect_truncated)
{
evt_test->assert_charbuf_array_param(16, &expected_newenviron[0]);
}
else
{
evt_test->assert_charbuf_array_param(16, &newenviron[0]);
}
/* PPM_EXE_WRITABLE is set when the user that executed a process can also write to the executable
* file that is used to spawn it or is its owner or otherwise capable.
*/
evt_test->assert_numeric_param(20, (uint32_t)PPM_EXE_WRITABLE);
/* Parameter 24: exe_file ino (type: PT_UINT64) */
evt_test->assert_numeric_param(24, (uint64_t)1, GREATER_EQUAL);
/* Parameter 25: exe_file ctime (last status change time, epoch value in nanoseconds) (type: PT_ABSTIME) */
evt_test->assert_numeric_param(25, (uint64_t)1000000000000000000, GREATER_EQUAL);
/* Parameter 26: exe_file mtime (last modification time, epoch value in nanoseconds) (type: PT_ABSTIME) */
evt_test->assert_numeric_param(26, (uint64_t)1000000000000000000, GREATER_EQUAL);
/* Parameter 27: euid (type: PT_UID) */
evt_test->assert_numeric_param(27, (uint32_t)geteuid(), EQUAL);
/* Parameter 28: trusted_exepath (type: PT_FSPATH) */
evt_test->assert_charbuf_param(28, pathname);
/*=============================== ASSERT PARAMETERS ===========================*/
evt_test->assert_num_params_pushed(28);
}
TEST(SyscallExit, execveX_not_upperlayer)
{
auto evt_test = get_syscall_event_test(__NR_execve, EXIT_EVENT);
evt_test->enable_capture();
/*=============================== TRIGGER SYSCALL ===========================*/
const char lowerdir[] = "/bin";
const char upperdir[] = "/tmp/upper";
const char target[] = "/tmp/merged";
char tmp_template[] = "/tmp/tmpdir.XXXXXX";
char *mntopts;
/* Create a temporary directory for the work layer */
char *workdir = mkdtemp(tmp_template);
/* Create the overlay mount target directory */
mkdir(upperdir, 0777);
mkdir(target, 0777);
/* Construct the mount options string */
if(asprintf(&mntopts, "lowerdir=%s,upperdir=%s,workdir=%s", lowerdir, upperdir, workdir) == -1){
FAIL() << "Cannot construct mount options string";
};
/* Mount the overlayfs */
if (mount("overlay", target, "overlay", MS_MGC_VAL, mntopts) != 0)
{
FAIL() << "Cannot mount overlay." << std::endl;
}
/* Copy /bin/true to /tmp/merged/uppertrue in the overlay file system */
char true_path[1024], upper_exe_path[1024];
sprintf(true_path, "%s/true", lowerdir);
sprintf(upper_exe_path, "%s/uppertrue", target);
int true_fd = open(true_path, O_RDONLY);
if (true_fd == -1)
{
FAIL() << "Cannot open /bin/true." << std::endl;
}
int upper_exe_fd = open(upper_exe_path, O_WRONLY|O_CREAT, 0777);
if (upper_exe_fd == -1)
{
FAIL() << "Cannot open /tmp/merged/uppertrue." << std::endl;
}
char buf[1024];
ssize_t bytes_read;
while ((bytes_read = read(true_fd, buf, sizeof(buf))) > 0)
{
if (write(upper_exe_fd, buf, bytes_read) != bytes_read)
{
FAIL() << "Cannot write /tmp/merged/uppertrue." << std::endl;
}
}
if (bytes_read == -1)
{
FAIL() << "Error copying /bin/true" << std::endl;
}
if (close(true_fd) == -1)
{
FAIL() << "Error closing /bin/true" << std::endl;
}
if (close(upper_exe_fd) == -1)
{
FAIL() << "Error closing /tmp/merged/uppertrue" << std::endl;
}
/* Prepare the execve args */
const char *pathname = true_path;
const char *comm = "true";
const char *argv[] = {true_path, "randomarg", NULL};
const char *envp[] = {"IN_TEST=yes", "3_ARGUMENT=yes", "2_ARGUMENT=no", NULL};
/* We need to use `SIGCHLD` otherwise the parent won't receive any signal
* when the child terminates.
*/
clone_args cl_args = {};
cl_args.exit_signal = SIGCHLD;
pid_t ret_pid = syscall(__NR_clone3, &cl_args, sizeof(cl_args));
/*
* Call the `execve`
*/
if(ret_pid == 0)
{
syscall(__NR_execve, pathname, argv, envp);
exit(EXIT_FAILURE);
}
assert_syscall_state(SYSCALL_SUCCESS, "clone3", ret_pid, NOT_EQUAL, -1);
/* Catch the child before doing anything else. */
int status = 0;
int options = 0;
assert_syscall_state(SYSCALL_SUCCESS, "wait4", syscall(__NR_wait4, ret_pid, &status, options, NULL), NOT_EQUAL, -1);
if(__WEXITSTATUS(status) == EXIT_FAILURE || __WIFSIGNALED(status) != 0)
{
FAIL() << "The child execve failed." << std::endl;
}
/*=============================== TRIGGER SYSCALL ===========================*/
evt_test->disable_capture();
/* Unmount the overlay file system */
if (umount(target))
{
FAIL() << "Cannot unmount target dir." << std::endl;
}
/* Remove the upper and work directories */
rmdir(upperdir);
rmdir(workdir);
rmdir(target);
/* We search for a child event. */
evt_test->assert_event_presence(ret_pid);
if(HasFatalFailure())
{
return;
}
evt_test->parse_event();
evt_test->assert_header();
/*=============================== ASSERT PARAMETERS ===========================*/
/* Please note here we cannot assert all the params, we check only the possible ones. */
/* Parameter 1: res (type: PT_ERRNO)*/
evt_test->assert_numeric_param(1, (int64_t)0);
/* Parameter 2: exe (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(2, pathname);
/* Parameter 3: args (type: PT_CHARBUFARRAY) */
/* Starting from `1` because the first is `exe`. */
evt_test->assert_charbuf_array_param(3, &argv[1]);
/* Parameter 4: tid (type: PT_PID) */
evt_test->assert_numeric_param(4, (int64_t)ret_pid);
/* Parameter 5: pid (type: PT_PID) */
/* We are the main thread of the process so it's equal to `tid`. */
evt_test->assert_numeric_param(5, (int64_t)ret_pid);
/* Parameter 6: ptid (type: PT_PID) */
evt_test->assert_numeric_param(6, (int64_t)::getpid());
/* Parameter 7: cwd (type: PT_CHARBUF) */
/* leave the current working directory empty like in the old probe. */
evt_test->assert_empty_param(7);
/* Parameter 14: comm (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(14, comm);
/* Parameter 15: cgroups (type: PT_CHARBUFARRAY) */
evt_test->assert_cgroup_param(15);
/* Parameter 16: env (type: PT_CHARBUFARRAY) */
evt_test->assert_charbuf_array_param(16, &envp[0]);
/* PPM_EXE_WRITABLE is set when the user that executed a process can also write to the executable
* file that is used to spawn it or is its owner or otherwise capable.
*/
evt_test->assert_numeric_param(20, (uint32_t)PPM_EXE_WRITABLE, EQUAL);
/* Parameter 24: exe_file ino (type: PT_UINT64) */
evt_test->assert_numeric_param(24, (uint64_t)1, GREATER_EQUAL);
/* Parameter 25: exe_file ctime (last status change time, epoch value in nanoseconds) (type: PT_ABSTIME) */
evt_test->assert_numeric_param(25, (uint64_t)1000000000000000000, GREATER_EQUAL);
/* Parameter 26: exe_file mtime (last modifitrueion time, epoch value in nanoseconds) (type: PT_ABSTIME) */
evt_test->assert_numeric_param(26, (uint64_t)1000000000000000000, GREATER_EQUAL);
/* Parameter 27: euid (type: PT_UID) */
evt_test->assert_numeric_param(27, (uint32_t)geteuid(), EQUAL);
/* Parameter 28: trusted_exepath (type: PT_FSPATH) */
evt_test->assert_charbuf_param(28, "/usr/bin/true");
/*=============================== ASSERT PARAMETERS ===========================*/
evt_test->assert_num_params_pushed(28);
}
TEST(SyscallExit, execveX_upperlayer_success)
{
auto evt_test = get_syscall_event_test(__NR_execve, EXIT_EVENT);
evt_test->enable_capture();
/*=============================== TRIGGER SYSCALL ===========================*/
const char lowerdir[] = "/bin";
const char upperdir[] = "/tmp/upper";
const char target[] = "/tmp/merged";
char tmp_template[] = "/tmp/tmpdir.XXXXXX";
char *mntopts;
/* Create a temporary directory for the work layer */
char *workdir = mkdtemp(tmp_template);
/* Create the overlay mount target directory */
mkdir(upperdir, 0777);
mkdir(target, 0777);
/* Construct the mount options string */
if(asprintf(&mntopts, "lowerdir=%s,upperdir=%s,workdir=%s", lowerdir, upperdir, workdir) == -1){
FAIL() << "Cannot construct mount options string";
};
/* Mount the overlayfs */
if (mount("overlay", target, "overlay", MS_MGC_VAL, mntopts) != 0)
{
FAIL() << "Cannot mount overlay." << std::endl;
}
/* Copy /bin/true to /tmp/merged/uppertrue in the overlay file system */
char true_path[1024], upper_exe_path[1024];
sprintf(true_path, "%s/true", lowerdir);
sprintf(upper_exe_path, "%s/uppertrue", target);
int true_fd = open(true_path, O_RDONLY);
if (true_fd == -1)
{
FAIL() << "Cannot open /bin/true." << std::endl;
}
int upper_exe_fd = open(upper_exe_path, O_WRONLY|O_CREAT, 0777);
if (upper_exe_fd == -1)
{
FAIL() << "Cannot open /tmp/merged/uppertrue." << std::endl;
}
char buf[1024];
ssize_t bytes_read;
while ((bytes_read = read(true_fd, buf, sizeof(buf))) > 0)
{
if (write(upper_exe_fd, buf, bytes_read) != bytes_read)
{
FAIL() << "Cannot write /tmp/merged/uppertrue." << std::endl;
}
}
if (bytes_read == -1)
{
FAIL() << "Error copying /bin/true" << std::endl;
}
if (close(true_fd) == -1)
{
FAIL() << "Error closing /bin/true" << std::endl;
}
if (close(upper_exe_fd) == -1)
{
FAIL() << "Error closing /tmp/merged/uppertrue" << std::endl;
}
/* Prepare the execve args */
const char *pathname = upper_exe_path;
const char *comm = "uppertrue";
const char *argv[] = {upper_exe_path, "randomarg", NULL};
const char *envp[] = {"IN_TEST=yes", "3_ARGUMENT=yes", "2_ARGUMENT=no", NULL};
/* We need to use `SIGCHLD` otherwise the parent won't receive any signal
* when the child terminates.
*/
clone_args cl_args = {};
cl_args.exit_signal = SIGCHLD;
pid_t ret_pid = syscall(__NR_clone3, &cl_args, sizeof(cl_args));
/*
* Call the `execve`
*/
if(ret_pid == 0)
{
syscall(__NR_execve, pathname, argv, envp);
exit(EXIT_FAILURE);
}
assert_syscall_state(SYSCALL_SUCCESS, "clone3", ret_pid, NOT_EQUAL, -1);
/* Catch the child before doing anything else. */
int status = 0;
int options = 0;
assert_syscall_state(SYSCALL_SUCCESS, "wait4", syscall(__NR_wait4, ret_pid, &status, options, NULL), NOT_EQUAL, -1);
if(__WEXITSTATUS(status) == EXIT_FAILURE || __WIFSIGNALED(status) != 0)
{
FAIL() << "The child execve failed." << std::endl;
}
/*=============================== TRIGGER SYSCALL ===========================*/
evt_test->disable_capture();
/* Unmount the overlay file system */
if (umount(target))
{
FAIL() << "Cannot unmount target dir." << std::endl;
}
/* Remove the upper and work directories */
rmdir(upperdir);
rmdir(workdir);
rmdir(target);
/* We search for a child event. */
evt_test->assert_event_presence(ret_pid);
if(HasFatalFailure())
{
return;
}
evt_test->parse_event();
evt_test->assert_header();
/*=============================== ASSERT PARAMETERS ===========================*/
/* Please note here we cannot assert all the params, we check only the possible ones. */
/* Parameter 1: res (type: PT_ERRNO)*/
evt_test->assert_numeric_param(1, (int64_t)0);
/* Parameter 2: exe (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(2, pathname);
/* Parameter 3: args (type: PT_CHARBUFARRAY) */
/* Starting from `1` because the first is `exe`. */
evt_test->assert_charbuf_array_param(3, &argv[1]);
/* Parameter 4: tid (type: PT_PID) */
evt_test->assert_numeric_param(4, (int64_t)ret_pid);
/* Parameter 5: pid (type: PT_PID) */
/* We are the main thread of the process so it's equal to `tid`. */
evt_test->assert_numeric_param(5, (int64_t)ret_pid);
/* Parameter 6: ptid (type: PT_PID) */
evt_test->assert_numeric_param(6, (int64_t)::getpid());
/* Parameter 7: cwd (type: PT_CHARBUF) */
/* leave the current working directory empty like in the old probe. */
evt_test->assert_empty_param(7);
/* Parameter 14: comm (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(14, comm);
/* Parameter 15: cgroups (type: PT_CHARBUFARRAY) */
evt_test->assert_cgroup_param(15);
/* Parameter 16: env (type: PT_CHARBUFARRAY) */
evt_test->assert_charbuf_array_param(16, &envp[0]);
/* PPM_EXE_WRITABLE is set when the user that executed a process can also write to the executable
* file that is used to spawn it or is its owner or otherwise capable.
*/
evt_test->assert_numeric_param(20, (uint32_t)PPM_EXE_WRITABLE|PPM_EXE_UPPER_LAYER);
/* Parameter 24: exe_file ino (type: PT_UINT64) */
evt_test->assert_numeric_param(24, (uint64_t)1, GREATER_EQUAL);
/* Parameter 25: exe_file ctime (last status change time, epoch value in nanoseconds) (type: PT_ABSTIME) */
evt_test->assert_numeric_param(25, (uint64_t)1000000000000000000, GREATER_EQUAL);
/* Parameter 26: exe_file mtime (last modifitrueion time, epoch value in nanoseconds) (type: PT_ABSTIME) */
evt_test->assert_numeric_param(26, (uint64_t)1000000000000000000, GREATER_EQUAL);
/* Parameter 27: euid (type: PT_UID) */
evt_test->assert_numeric_param(27, (uint32_t)geteuid(), EQUAL);
/* Parameter 28: trusted_exepath (type: PT_FSPATH) */
evt_test->assert_charbuf_param(28, "/tmp/merged/uppertrue");
/*=============================== ASSERT PARAMETERS ===========================*/
evt_test->assert_num_params_pushed(28);
}
#if defined(__NR_memfd_create) && defined(__NR_openat) && defined(__NR_read) && defined(__NR_write)
#include <sys/mman.h>
TEST(SyscallExit, execveX_success_memfd)
{
auto evt_test = get_syscall_event_test(__NR_execve, EXIT_EVENT);
evt_test->enable_capture();
/*=============================== TRIGGER SYSCALL ===========================*/
int mem_fd = syscall(__NR_memfd_create, "malware", MFD_CLOEXEC);
assert_syscall_state(SYSCALL_SUCCESS, "memfd_create", mem_fd, NOT_EQUAL, -1);
/* Open the executable to copy */
int fd_to_read = syscall(__NR_openat, 0, "/usr/bin/echo", O_RDWR);
if(fd_to_read < 0)
{
FAIL() << "failed to open the file to read\n";
}
char buf[200];
ssize_t bytes_read = 200;
while(bytes_read != 0)
{
bytes_read = syscall(__NR_read, fd_to_read, buf, sizeof(buf));
if(bytes_read < 0)
{
syscall(__NR_close, fd_to_read);
syscall(__NR_close, mem_fd);
FAIL() << "unable to read from file\n";
}
bytes_read = syscall(__NR_write, mem_fd, buf, bytes_read);
if(bytes_read < 0)
{
syscall(__NR_close, fd_to_read);
syscall(__NR_close, mem_fd);
FAIL() << "unable to write to file\n";
}
}
syscall(__NR_close, fd_to_read);
/* We need to use `SIGCHLD` otherwise the parent won't receive any signal
* when the child terminates.
*/
clone_args cl_args = {};
cl_args.exit_signal = SIGCHLD;
pid_t ret_pid = syscall(__NR_clone3, &cl_args, sizeof(cl_args));
if(ret_pid == 0)
{
char pathname[200];
snprintf(pathname, sizeof(pathname), "/proc/%d/fd/%d", getpid(), mem_fd);
const char *newargv[] = {pathname, "[OUTPUT] SyscallExit.execveX_success_memfd", NULL};
const char *newenviron[] = {"IN_TEST=yes", "3_ARGUMENT=yes", "2_ARGUMENT=no", NULL};
syscall(__NR_execve, pathname, newargv, newenviron);
exit(EXIT_FAILURE);
}
syscall(__NR_close, mem_fd);
assert_syscall_state(SYSCALL_SUCCESS, "clone3", ret_pid, NOT_EQUAL, -1);
/* Catch the child before doing anything else. */
int status = 0;
int options = 0;
assert_syscall_state(SYSCALL_SUCCESS, "wait4", syscall(__NR_wait4, ret_pid, &status, options, NULL), NOT_EQUAL,
-1);
if(__WEXITSTATUS(status) == EXIT_FAILURE || __WIFSIGNALED(status) != 0)
{
FAIL() << "The child execve failed." << std::endl;
}
/*=============================== TRIGGER SYSCALL ===========================*/
evt_test->disable_capture();
/* We search for a child event. */
evt_test->assert_event_presence(ret_pid);
if(HasFatalFailure())
{
return;
}
evt_test->parse_event();
evt_test->assert_header();
/*=============================== ASSERT PARAMETERS ===========================*/
/* Please note here we cannot assert all the params, we check only the possible ones. */
/* Parameter 1: res (type: PT_ERRNO)*/
evt_test->assert_numeric_param(1, (int64_t)0);
/* PPM_EXE_WRITABLE is set when the user that executed a process can also write to the executable
* file that is used to spawn it or is its owner or otherwise capable.
*/
evt_test->assert_numeric_param(20, (uint32_t)PPM_EXE_WRITABLE | PPM_EXE_FROM_MEMFD);
/* Parameter 28: trusted_exepath (type: PT_FSPATH) */
/* In the kmod, we use the "d_path" helper while in BPF we reconstruct the path
* by hand so the result is a little bit different.
* Please note that in the kernel module, we remove the " (deleted)" suffix while
* in BPF we don't add it at all.
*/
if(evt_test->is_kmod_engine())
{
evt_test->assert_charbuf_param(28, "/memfd:malware");
}
else
{
/* In BPF drivers we don't have the correct result but we can reconstruct part of it */
evt_test->assert_charbuf_param(28, "memfd:malware");
}
/*=============================== ASSERT PARAMETERS ===========================*/
evt_test->assert_num_params_pushed(28);
}
#endif
#if defined(__NR_symlinkat) && defined(__NR_unlinkat)
TEST(SyscallExit, execveX_symlink)
{
auto evt_test = get_syscall_event_test(__NR_execve, EXIT_EVENT);
evt_test->enable_capture();
/*=============================== TRIGGER SYSCALL ===========================*/
/* Prepare the execve args */
const char *pathname = "/usr/bin/echo";
const char *linkpath = "target3";
/* Create symlink */
assert_syscall_state(SYSCALL_SUCCESS, "symlinkat", syscall(__NR_symlinkat, pathname, AT_FDCWD, linkpath), NOT_EQUAL, -1);
const char *comm = "target3";
const char *argv[] = {linkpath, "[OUTPUT] SyscallExit.execveX_success test", NULL};
const char *envp[] = {"IN_TEST=yes", "3_ARGUMENT=yes", "2_ARGUMENT=no", NULL};
/* We need to use `SIGCHLD` otherwise the parent won't receive any signal
* when the child terminates.
*/
clone_args cl_args = {};
cl_args.exit_signal = SIGCHLD;
pid_t ret_pid = syscall(__NR_clone3, &cl_args, sizeof(cl_args));
if(ret_pid == 0)
{
syscall(__NR_execve, linkpath, argv, envp);
exit(EXIT_FAILURE);
}
assert_syscall_state(SYSCALL_SUCCESS, "clone3", ret_pid, NOT_EQUAL, -1);
/* Catch the child before doing anything else. */
int status = 0;
int options = 0;
assert_syscall_state(SYSCALL_SUCCESS, "wait4", syscall(__NR_wait4, ret_pid, &status, options, NULL), NOT_EQUAL, -1);
if(__WEXITSTATUS(status) == EXIT_FAILURE || __WIFSIGNALED(status) != 0)
{
FAIL() << "The child execve failed." << std::endl;
}
assert_syscall_state(SYSCALL_SUCCESS, "unlinkat", syscall(__NR_unlinkat, AT_FDCWD, linkpath, 0), NOT_EQUAL, -1);