| 1 |
// this is for separately function testing |
| 2 |
#include <assert.h> |
| 3 |
#include <linux/audit.h> |
| 4 |
#include <linux/bpf_common.h> |
| 5 |
#include <linux/filter.h> |
| 6 |
#include <linux/prctl.h> |
| 7 |
#include <linux/seccomp.h> |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stddef.h> |
| 10 |
#include <stdint.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <sys/prctl.h> |
| 14 |
#include <sys/syscall.h> |
| 15 |
#include <sys/types.h> |
| 16 |
#include <sys/wait.h> |
| 17 |
#include <unistd.h> |
| 18 |
|
| 19 |
#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0])) |
| 20 |
|
| 21 |
enum test_case |
| 22 |
{ |
| 23 |
TEST_TRACE = 0, |
| 24 |
TEST_PROBE = 1, |
| 25 |
TEST_SEIZE = 2, |
| 26 |
TEST_TRACE_PID = 3, |
| 27 |
}; |
| 28 |
|
| 29 |
static const struct sock_filter filters[] = { |
| 30 |
BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 6), |
| 31 |
BPF_STMT (BPF_LD | BPF_W | BPF_ABS, (offsetof (struct seccomp_data, nr))), |
| 32 |
BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, SYS_execve, 2, 0), |
| 33 |
BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, SYS_sendfile, 2, 0), |
| 34 |
BPF_STMT (BPF_RET | BPF_K, SECCOMP_RET_ALLOW), |
| 35 |
BPF_STMT (BPF_RET | BPF_K, SECCOMP_RET_ERRNO | 1), |
| 36 |
BPF_STMT (BPF_LD | BPF_W | BPF_K, SECCOMP_RET_KILL), |
| 37 |
BPF_STMT (BPF_RET | BPF_A, 0), |
| 38 |
}; |
| 39 |
|
| 40 |
static void |
| 41 |
load_filter (bool tofail) |
| 42 |
{ |
| 43 |
prctl (PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 44 |
struct sock_fprog prog = { .len = ARRAY_SIZE (filters), |
| 45 |
.filter = (struct sock_filter *)filters }; |
| 46 |
|
| 47 |
syscall (SYS_seccomp, SECCOMP_SET_MODE_FILTER, NULL, &prog); |
| 48 |
|
| 49 |
// test failed loading |
| 50 |
if (tofail) |
| 51 |
syscall (SYS_seccomp, SECCOMP_SET_MODE_FILTER, NULL, NULL); |
| 52 |
} |
| 53 |
|
| 54 |
int |
| 55 |
main (int argc, char **argv) |
| 56 |
{ |
| 57 |
setvbuf (stdout, NULL, _IOLBF, 0x100); |
| 58 |
int choice = argc < 2 ? 0 : atoi (argv[1]); |
| 59 |
int efd = argc < 3 ? 0 : atoi (argv[2]); |
| 60 |
uint64_t sem; |
| 61 |
|
| 62 |
switch (choice) |
| 63 |
{ |
| 64 |
case TEST_TRACE: |
| 65 |
load_filter (true); |
| 66 |
break; |
| 67 |
case TEST_SEIZE: |
| 68 |
prctl (PR_SET_PTRACER, PR_SET_PTRACER_ANY); |
| 69 |
pid_t pid = getpid (); |
| 70 |
printf ("pid=%d\n", pid); |
| 71 |
|
| 72 |
assert (efd != 0); |
| 73 |
read (efd, &sem, 8); |
| 74 |
pid = fork (); |
| 75 |
if (pid) |
| 76 |
{ |
| 77 |
waitpid (pid, NULL, 0); |
| 78 |
exit (0); |
| 79 |
} |
| 80 |
// child |
| 81 |
load_filter (false); |
| 82 |
printf ("child=%d\n", getpid ()); |
| 83 |
read (efd, &sem, 8); |
| 84 |
break; |
| 85 |
case TEST_PROBE: |
| 86 |
pid = fork (); |
| 87 |
if (pid != 0) |
| 88 |
{ |
| 89 |
wait (NULL); |
| 90 |
exit (0); |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
pid = fork (); |
| 95 |
if (pid != 0) |
| 96 |
exit (0); |
| 97 |
// grandchild process |
| 98 |
pid = getpid (); |
| 99 |
printf ("pid=%d\n", pid); |
| 100 |
|
| 101 |
load_filter (false); |
| 102 |
|
| 103 |
signal (SIGINT, SIG_IGN); |
| 104 |
sleep (100); |
| 105 |
} |
| 106 |
break; |
| 107 |
case TEST_TRACE_PID: |
| 108 |
load_filter (false); |
| 109 |
printf ("pid=%d\n", getpid ()); |
| 110 |
assert (efd != 0); |
| 111 |
read (efd, &sem, 8); |
| 112 |
break; |
| 113 |
default: |
| 114 |
load_filter (true); |
| 115 |
break; |
| 116 |
} |
| 117 |
return 0; |
| 118 |
} |