Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / seccomp / seccomp_bpf.c
blob14cad657bc6aadf4811818cf378e48efa24bdefd
1 /*
2 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by the GPLv2 license.
5 * Test code for seccomp bpf.
6 */
8 #include <sys/types.h>
11 * glibc 2.26 and later have SIGSYS in siginfo_t. Before that,
12 * we need to use the kernel's siginfo.h file and trick glibc
13 * into accepting it.
15 #if !__GLIBC_PREREQ(2, 26)
16 # include <asm/siginfo.h>
17 # define __have_siginfo_t 1
18 # define __have_sigval_t 1
19 # define __have_sigevent_t 1
20 #endif
22 #include <errno.h>
23 #include <linux/filter.h>
24 #include <sys/prctl.h>
25 #include <sys/ptrace.h>
26 #include <sys/user.h>
27 #include <linux/prctl.h>
28 #include <linux/ptrace.h>
29 #include <linux/seccomp.h>
30 #include <pthread.h>
31 #include <semaphore.h>
32 #include <signal.h>
33 #include <stddef.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <time.h>
37 #include <linux/elf.h>
38 #include <sys/uio.h>
39 #include <sys/utsname.h>
40 #include <sys/fcntl.h>
41 #include <sys/mman.h>
42 #include <sys/times.h>
44 #define _GNU_SOURCE
45 #include <unistd.h>
46 #include <sys/syscall.h>
48 #include "../kselftest_harness.h"
50 #ifndef PR_SET_PTRACER
51 # define PR_SET_PTRACER 0x59616d61
52 #endif
54 #ifndef PR_SET_NO_NEW_PRIVS
55 #define PR_SET_NO_NEW_PRIVS 38
56 #define PR_GET_NO_NEW_PRIVS 39
57 #endif
59 #ifndef PR_SECCOMP_EXT
60 #define PR_SECCOMP_EXT 43
61 #endif
63 #ifndef SECCOMP_EXT_ACT
64 #define SECCOMP_EXT_ACT 1
65 #endif
67 #ifndef SECCOMP_EXT_ACT_TSYNC
68 #define SECCOMP_EXT_ACT_TSYNC 1
69 #endif
71 #ifndef SECCOMP_MODE_STRICT
72 #define SECCOMP_MODE_STRICT 1
73 #endif
75 #ifndef SECCOMP_MODE_FILTER
76 #define SECCOMP_MODE_FILTER 2
77 #endif
79 #ifndef SECCOMP_RET_ALLOW
80 struct seccomp_data {
81 int nr;
82 __u32 arch;
83 __u64 instruction_pointer;
84 __u64 args[6];
86 #endif
88 #ifndef SECCOMP_RET_KILL_PROCESS
89 #define SECCOMP_RET_KILL_PROCESS 0x80000000U /* kill the process */
90 #define SECCOMP_RET_KILL_THREAD 0x00000000U /* kill the thread */
91 #endif
92 #ifndef SECCOMP_RET_KILL
93 #define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD
94 #define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */
95 #define SECCOMP_RET_ERRNO 0x00050000U /* returns an errno */
96 #define SECCOMP_RET_TRACE 0x7ff00000U /* pass to a tracer or disallow */
97 #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
98 #endif
99 #ifndef SECCOMP_RET_LOG
100 #define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */
101 #endif
103 #ifndef __NR_seccomp
104 # if defined(__i386__)
105 # define __NR_seccomp 354
106 # elif defined(__x86_64__)
107 # define __NR_seccomp 317
108 # elif defined(__arm__)
109 # define __NR_seccomp 383
110 # elif defined(__aarch64__)
111 # define __NR_seccomp 277
112 # elif defined(__hppa__)
113 # define __NR_seccomp 338
114 # elif defined(__powerpc__)
115 # define __NR_seccomp 358
116 # elif defined(__s390__)
117 # define __NR_seccomp 348
118 # else
119 # warning "seccomp syscall number unknown for this architecture"
120 # define __NR_seccomp 0xffff
121 # endif
122 #endif
124 #ifndef SECCOMP_SET_MODE_STRICT
125 #define SECCOMP_SET_MODE_STRICT 0
126 #endif
128 #ifndef SECCOMP_SET_MODE_FILTER
129 #define SECCOMP_SET_MODE_FILTER 1
130 #endif
132 #ifndef SECCOMP_GET_ACTION_AVAIL
133 #define SECCOMP_GET_ACTION_AVAIL 2
134 #endif
136 #ifndef SECCOMP_FILTER_FLAG_TSYNC
137 #define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
138 #endif
140 #ifndef SECCOMP_FILTER_FLAG_LOG
141 #define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
142 #endif
144 #ifndef SECCOMP_FILTER_FLAG_SPEC_ALLOW
145 #define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
146 #endif
148 #ifndef PTRACE_SECCOMP_GET_METADATA
149 #define PTRACE_SECCOMP_GET_METADATA 0x420d
151 struct seccomp_metadata {
152 __u64 filter_off; /* Input: which filter */
153 __u64 flags; /* Output: filter's flags */
155 #endif
157 #ifndef seccomp
158 int seccomp(unsigned int op, unsigned int flags, void *args)
160 errno = 0;
161 return syscall(__NR_seccomp, op, flags, args);
163 #endif
165 #if __BYTE_ORDER == __LITTLE_ENDIAN
166 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
167 #elif __BYTE_ORDER == __BIG_ENDIAN
168 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]) + sizeof(__u32))
169 #else
170 #error "wut? Unknown __BYTE_ORDER?!"
171 #endif
173 #define SIBLING_EXIT_UNKILLED 0xbadbeef
174 #define SIBLING_EXIT_FAILURE 0xbadface
175 #define SIBLING_EXIT_NEWPRIVS 0xbadfeed
177 TEST(mode_strict_support)
179 long ret;
181 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, NULL, NULL);
182 ASSERT_EQ(0, ret) {
183 TH_LOG("Kernel does not support CONFIG_SECCOMP");
185 syscall(__NR_exit, 0);
188 TEST_SIGNAL(mode_strict_cannot_call_prctl, SIGKILL)
190 long ret;
192 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, NULL, NULL);
193 ASSERT_EQ(0, ret) {
194 TH_LOG("Kernel does not support CONFIG_SECCOMP");
196 syscall(__NR_prctl, PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
197 NULL, NULL, NULL);
198 EXPECT_FALSE(true) {
199 TH_LOG("Unreachable!");
203 /* Note! This doesn't test no new privs behavior */
204 TEST(no_new_privs_support)
206 long ret;
208 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
209 EXPECT_EQ(0, ret) {
210 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
214 /* Tests kernel support by checking for a copy_from_user() fault on NULL. */
215 TEST(mode_filter_support)
217 long ret;
219 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0);
220 ASSERT_EQ(0, ret) {
221 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
223 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL, NULL, NULL);
224 EXPECT_EQ(-1, ret);
225 EXPECT_EQ(EFAULT, errno) {
226 TH_LOG("Kernel does not support CONFIG_SECCOMP_FILTER!");
230 TEST(mode_filter_without_nnp)
232 struct sock_filter filter[] = {
233 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
235 struct sock_fprog prog = {
236 .len = (unsigned short)ARRAY_SIZE(filter),
237 .filter = filter,
239 long ret;
241 ret = prctl(PR_GET_NO_NEW_PRIVS, 0, NULL, 0, 0);
242 ASSERT_LE(0, ret) {
243 TH_LOG("Expected 0 or unsupported for NO_NEW_PRIVS");
245 errno = 0;
246 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
247 /* Succeeds with CAP_SYS_ADMIN, fails without */
248 /* TODO(wad) check caps not euid */
249 if (geteuid()) {
250 EXPECT_EQ(-1, ret);
251 EXPECT_EQ(EACCES, errno);
252 } else {
253 EXPECT_EQ(0, ret);
257 #define MAX_INSNS_PER_PATH 32768
259 TEST(filter_size_limits)
261 int i;
262 int count = BPF_MAXINSNS + 1;
263 struct sock_filter allow[] = {
264 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
266 struct sock_filter *filter;
267 struct sock_fprog prog = { };
268 long ret;
270 filter = calloc(count, sizeof(*filter));
271 ASSERT_NE(NULL, filter);
273 for (i = 0; i < count; i++)
274 filter[i] = allow[0];
276 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
277 ASSERT_EQ(0, ret);
279 prog.filter = filter;
280 prog.len = count;
282 /* Too many filter instructions in a single filter. */
283 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
284 ASSERT_NE(0, ret) {
285 TH_LOG("Installing %d insn filter was allowed", prog.len);
288 /* One less is okay, though. */
289 prog.len -= 1;
290 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
291 ASSERT_EQ(0, ret) {
292 TH_LOG("Installing %d insn filter wasn't allowed", prog.len);
296 TEST(filter_chain_limits)
298 int i;
299 int count = BPF_MAXINSNS;
300 struct sock_filter allow[] = {
301 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
303 struct sock_filter *filter;
304 struct sock_fprog prog = { };
305 long ret;
307 filter = calloc(count, sizeof(*filter));
308 ASSERT_NE(NULL, filter);
310 for (i = 0; i < count; i++)
311 filter[i] = allow[0];
313 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
314 ASSERT_EQ(0, ret);
316 prog.filter = filter;
317 prog.len = 1;
319 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
320 ASSERT_EQ(0, ret);
322 prog.len = count;
324 /* Too many total filter instructions. */
325 for (i = 0; i < MAX_INSNS_PER_PATH; i++) {
326 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
327 if (ret != 0)
328 break;
330 ASSERT_NE(0, ret) {
331 TH_LOG("Allowed %d %d-insn filters (total with penalties:%d)",
332 i, count, i * (count + 4));
336 TEST(mode_filter_cannot_move_to_strict)
338 struct sock_filter filter[] = {
339 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
341 struct sock_fprog prog = {
342 .len = (unsigned short)ARRAY_SIZE(filter),
343 .filter = filter,
345 long ret;
347 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
348 ASSERT_EQ(0, ret);
350 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
351 ASSERT_EQ(0, ret);
353 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, 0, 0);
354 EXPECT_EQ(-1, ret);
355 EXPECT_EQ(EINVAL, errno);
359 TEST(mode_filter_get_seccomp)
361 struct sock_filter filter[] = {
362 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
364 struct sock_fprog prog = {
365 .len = (unsigned short)ARRAY_SIZE(filter),
366 .filter = filter,
368 long ret;
370 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
371 ASSERT_EQ(0, ret);
373 ret = prctl(PR_GET_SECCOMP, 0, 0, 0, 0);
374 EXPECT_EQ(0, ret);
376 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
377 ASSERT_EQ(0, ret);
379 ret = prctl(PR_GET_SECCOMP, 0, 0, 0, 0);
380 EXPECT_EQ(2, ret);
384 TEST(ALLOW_all)
386 struct sock_filter filter[] = {
387 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
389 struct sock_fprog prog = {
390 .len = (unsigned short)ARRAY_SIZE(filter),
391 .filter = filter,
393 long ret;
395 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
396 ASSERT_EQ(0, ret);
398 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
399 ASSERT_EQ(0, ret);
402 TEST(empty_prog)
404 struct sock_filter filter[] = {
406 struct sock_fprog prog = {
407 .len = (unsigned short)ARRAY_SIZE(filter),
408 .filter = filter,
410 long ret;
412 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
413 ASSERT_EQ(0, ret);
415 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
416 EXPECT_EQ(-1, ret);
417 EXPECT_EQ(EINVAL, errno);
420 TEST(log_all)
422 struct sock_filter filter[] = {
423 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_LOG),
425 struct sock_fprog prog = {
426 .len = (unsigned short)ARRAY_SIZE(filter),
427 .filter = filter,
429 long ret;
430 pid_t parent = getppid();
432 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
433 ASSERT_EQ(0, ret);
435 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
436 ASSERT_EQ(0, ret);
438 /* getppid() should succeed and be logged (no check for logging) */
439 EXPECT_EQ(parent, syscall(__NR_getppid));
442 TEST_SIGNAL(unknown_ret_is_kill_inside, SIGSYS)
444 struct sock_filter filter[] = {
445 BPF_STMT(BPF_RET|BPF_K, 0x10000000U),
447 struct sock_fprog prog = {
448 .len = (unsigned short)ARRAY_SIZE(filter),
449 .filter = filter,
451 long ret;
453 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
454 ASSERT_EQ(0, ret);
456 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
457 ASSERT_EQ(0, ret);
458 EXPECT_EQ(0, syscall(__NR_getpid)) {
459 TH_LOG("getpid() shouldn't ever return");
463 /* return code >= 0x80000000 is unused. */
464 TEST_SIGNAL(unknown_ret_is_kill_above_allow, SIGSYS)
466 struct sock_filter filter[] = {
467 BPF_STMT(BPF_RET|BPF_K, 0x90000000U),
469 struct sock_fprog prog = {
470 .len = (unsigned short)ARRAY_SIZE(filter),
471 .filter = filter,
473 long ret;
475 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
476 ASSERT_EQ(0, ret);
478 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
479 ASSERT_EQ(0, ret);
480 EXPECT_EQ(0, syscall(__NR_getpid)) {
481 TH_LOG("getpid() shouldn't ever return");
485 TEST_SIGNAL(KILL_all, SIGSYS)
487 struct sock_filter filter[] = {
488 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
490 struct sock_fprog prog = {
491 .len = (unsigned short)ARRAY_SIZE(filter),
492 .filter = filter,
494 long ret;
496 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
497 ASSERT_EQ(0, ret);
499 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
500 ASSERT_EQ(0, ret);
503 TEST_SIGNAL(KILL_one, SIGSYS)
505 struct sock_filter filter[] = {
506 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
507 offsetof(struct seccomp_data, nr)),
508 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1),
509 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
510 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
512 struct sock_fprog prog = {
513 .len = (unsigned short)ARRAY_SIZE(filter),
514 .filter = filter,
516 long ret;
517 pid_t parent = getppid();
519 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
520 ASSERT_EQ(0, ret);
522 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
523 ASSERT_EQ(0, ret);
525 EXPECT_EQ(parent, syscall(__NR_getppid));
526 /* getpid() should never return. */
527 EXPECT_EQ(0, syscall(__NR_getpid));
530 TEST_SIGNAL(KILL_one_arg_one, SIGSYS)
532 void *fatal_address;
533 struct sock_filter filter[] = {
534 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
535 offsetof(struct seccomp_data, nr)),
536 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_times, 1, 0),
537 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
538 /* Only both with lower 32-bit for now. */
539 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(0)),
540 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,
541 (unsigned long)&fatal_address, 0, 1),
542 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
543 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
545 struct sock_fprog prog = {
546 .len = (unsigned short)ARRAY_SIZE(filter),
547 .filter = filter,
549 long ret;
550 pid_t parent = getppid();
551 struct tms timebuf;
552 clock_t clock = times(&timebuf);
554 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
555 ASSERT_EQ(0, ret);
557 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
558 ASSERT_EQ(0, ret);
560 EXPECT_EQ(parent, syscall(__NR_getppid));
561 EXPECT_LE(clock, syscall(__NR_times, &timebuf));
562 /* times() should never return. */
563 EXPECT_EQ(0, syscall(__NR_times, &fatal_address));
566 TEST_SIGNAL(KILL_one_arg_six, SIGSYS)
568 #ifndef __NR_mmap2
569 int sysno = __NR_mmap;
570 #else
571 int sysno = __NR_mmap2;
572 #endif
573 struct sock_filter filter[] = {
574 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
575 offsetof(struct seccomp_data, nr)),
576 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, sysno, 1, 0),
577 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
578 /* Only both with lower 32-bit for now. */
579 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(5)),
580 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x0C0FFEE, 0, 1),
581 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
582 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
584 struct sock_fprog prog = {
585 .len = (unsigned short)ARRAY_SIZE(filter),
586 .filter = filter,
588 long ret;
589 pid_t parent = getppid();
590 int fd;
591 void *map1, *map2;
592 int page_size = sysconf(_SC_PAGESIZE);
594 ASSERT_LT(0, page_size);
596 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
597 ASSERT_EQ(0, ret);
599 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
600 ASSERT_EQ(0, ret);
602 fd = open("/dev/zero", O_RDONLY);
603 ASSERT_NE(-1, fd);
605 EXPECT_EQ(parent, syscall(__NR_getppid));
606 map1 = (void *)syscall(sysno,
607 NULL, page_size, PROT_READ, MAP_PRIVATE, fd, page_size);
608 EXPECT_NE(MAP_FAILED, map1);
609 /* mmap2() should never return. */
610 map2 = (void *)syscall(sysno,
611 NULL, page_size, PROT_READ, MAP_PRIVATE, fd, 0x0C0FFEE);
612 EXPECT_EQ(MAP_FAILED, map2);
614 /* The test failed, so clean up the resources. */
615 munmap(map1, page_size);
616 munmap(map2, page_size);
617 close(fd);
620 /* This is a thread task to die via seccomp filter violation. */
621 void *kill_thread(void *data)
623 bool die = (bool)data;
625 if (die) {
626 prctl(PR_GET_SECCOMP, 0, 0, 0, 0);
627 return (void *)SIBLING_EXIT_FAILURE;
630 return (void *)SIBLING_EXIT_UNKILLED;
633 /* Prepare a thread that will kill itself or both of us. */
634 void kill_thread_or_group(struct __test_metadata *_metadata, bool kill_process)
636 pthread_t thread;
637 void *status;
638 /* Kill only when calling __NR_prctl. */
639 struct sock_filter filter_thread[] = {
640 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
641 offsetof(struct seccomp_data, nr)),
642 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1),
643 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL_THREAD),
644 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
646 struct sock_fprog prog_thread = {
647 .len = (unsigned short)ARRAY_SIZE(filter_thread),
648 .filter = filter_thread,
650 struct sock_filter filter_process[] = {
651 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
652 offsetof(struct seccomp_data, nr)),
653 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1),
654 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL_PROCESS),
655 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
657 struct sock_fprog prog_process = {
658 .len = (unsigned short)ARRAY_SIZE(filter_process),
659 .filter = filter_process,
662 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
663 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
666 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0,
667 kill_process ? &prog_process : &prog_thread));
670 * Add the KILL_THREAD rule again to make sure that the KILL_PROCESS
671 * flag cannot be downgraded by a new filter.
673 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog_thread));
675 /* Start a thread that will exit immediately. */
676 ASSERT_EQ(0, pthread_create(&thread, NULL, kill_thread, (void *)false));
677 ASSERT_EQ(0, pthread_join(thread, &status));
678 ASSERT_EQ(SIBLING_EXIT_UNKILLED, (unsigned long)status);
680 /* Start a thread that will die immediately. */
681 ASSERT_EQ(0, pthread_create(&thread, NULL, kill_thread, (void *)true));
682 ASSERT_EQ(0, pthread_join(thread, &status));
683 ASSERT_NE(SIBLING_EXIT_FAILURE, (unsigned long)status);
686 * If we get here, only the spawned thread died. Let the parent know
687 * the whole process didn't die (i.e. this thread, the spawner,
688 * stayed running).
690 exit(42);
693 TEST(KILL_thread)
695 int status;
696 pid_t child_pid;
698 child_pid = fork();
699 ASSERT_LE(0, child_pid);
700 if (child_pid == 0) {
701 kill_thread_or_group(_metadata, false);
702 _exit(38);
705 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
707 /* If only the thread was killed, we'll see exit 42. */
708 ASSERT_TRUE(WIFEXITED(status));
709 ASSERT_EQ(42, WEXITSTATUS(status));
712 TEST(KILL_process)
714 int status;
715 pid_t child_pid;
717 child_pid = fork();
718 ASSERT_LE(0, child_pid);
719 if (child_pid == 0) {
720 kill_thread_or_group(_metadata, true);
721 _exit(38);
724 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
726 /* If the entire process was killed, we'll see SIGSYS. */
727 ASSERT_TRUE(WIFSIGNALED(status));
728 ASSERT_EQ(SIGSYS, WTERMSIG(status));
731 /* TODO(wad) add 64-bit versus 32-bit arg tests. */
732 TEST(arg_out_of_range)
734 struct sock_filter filter[] = {
735 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(6)),
736 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
738 struct sock_fprog prog = {
739 .len = (unsigned short)ARRAY_SIZE(filter),
740 .filter = filter,
742 long ret;
744 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
745 ASSERT_EQ(0, ret);
747 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
748 EXPECT_EQ(-1, ret);
749 EXPECT_EQ(EINVAL, errno);
752 #define ERRNO_FILTER(name, errno) \
753 struct sock_filter _read_filter_##name[] = { \
754 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, \
755 offsetof(struct seccomp_data, nr)), \
756 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), \
757 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | errno), \
758 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), \
759 }; \
760 struct sock_fprog prog_##name = { \
761 .len = (unsigned short)ARRAY_SIZE(_read_filter_##name), \
762 .filter = _read_filter_##name, \
765 /* Make sure basic errno values are correctly passed through a filter. */
766 TEST(ERRNO_valid)
768 ERRNO_FILTER(valid, E2BIG);
769 long ret;
770 pid_t parent = getppid();
772 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
773 ASSERT_EQ(0, ret);
775 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_valid);
776 ASSERT_EQ(0, ret);
778 EXPECT_EQ(parent, syscall(__NR_getppid));
779 EXPECT_EQ(-1, read(0, NULL, 0));
780 EXPECT_EQ(E2BIG, errno);
783 /* Make sure an errno of zero is correctly handled by the arch code. */
784 TEST(ERRNO_zero)
786 ERRNO_FILTER(zero, 0);
787 long ret;
788 pid_t parent = getppid();
790 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
791 ASSERT_EQ(0, ret);
793 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_zero);
794 ASSERT_EQ(0, ret);
796 EXPECT_EQ(parent, syscall(__NR_getppid));
797 /* "errno" of 0 is ok. */
798 EXPECT_EQ(0, read(0, NULL, 0));
802 * The SECCOMP_RET_DATA mask is 16 bits wide, but errno is smaller.
803 * This tests that the errno value gets capped correctly, fixed by
804 * 580c57f10768 ("seccomp: cap SECCOMP_RET_ERRNO data to MAX_ERRNO").
806 TEST(ERRNO_capped)
808 ERRNO_FILTER(capped, 4096);
809 long ret;
810 pid_t parent = getppid();
812 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
813 ASSERT_EQ(0, ret);
815 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_capped);
816 ASSERT_EQ(0, ret);
818 EXPECT_EQ(parent, syscall(__NR_getppid));
819 EXPECT_EQ(-1, read(0, NULL, 0));
820 EXPECT_EQ(4095, errno);
824 * Filters are processed in reverse order: last applied is executed first.
825 * Since only the SECCOMP_RET_ACTION mask is tested for return values, the
826 * SECCOMP_RET_DATA mask results will follow the most recently applied
827 * matching filter return (and not the lowest or highest value).
829 TEST(ERRNO_order)
831 ERRNO_FILTER(first, 11);
832 ERRNO_FILTER(second, 13);
833 ERRNO_FILTER(third, 12);
834 long ret;
835 pid_t parent = getppid();
837 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
838 ASSERT_EQ(0, ret);
840 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_first);
841 ASSERT_EQ(0, ret);
843 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_second);
844 ASSERT_EQ(0, ret);
846 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_third);
847 ASSERT_EQ(0, ret);
849 EXPECT_EQ(parent, syscall(__NR_getppid));
850 EXPECT_EQ(-1, read(0, NULL, 0));
851 EXPECT_EQ(12, errno);
854 FIXTURE_DATA(TRAP) {
855 struct sock_fprog prog;
858 FIXTURE_SETUP(TRAP)
860 struct sock_filter filter[] = {
861 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
862 offsetof(struct seccomp_data, nr)),
863 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1),
864 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP),
865 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
868 memset(&self->prog, 0, sizeof(self->prog));
869 self->prog.filter = malloc(sizeof(filter));
870 ASSERT_NE(NULL, self->prog.filter);
871 memcpy(self->prog.filter, filter, sizeof(filter));
872 self->prog.len = (unsigned short)ARRAY_SIZE(filter);
875 FIXTURE_TEARDOWN(TRAP)
877 if (self->prog.filter)
878 free(self->prog.filter);
881 TEST_F_SIGNAL(TRAP, dfl, SIGSYS)
883 long ret;
885 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
886 ASSERT_EQ(0, ret);
888 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog);
889 ASSERT_EQ(0, ret);
890 syscall(__NR_getpid);
893 /* Ensure that SIGSYS overrides SIG_IGN */
894 TEST_F_SIGNAL(TRAP, ign, SIGSYS)
896 long ret;
898 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
899 ASSERT_EQ(0, ret);
901 signal(SIGSYS, SIG_IGN);
903 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog);
904 ASSERT_EQ(0, ret);
905 syscall(__NR_getpid);
908 static siginfo_t TRAP_info;
909 static volatile int TRAP_nr;
910 static void TRAP_action(int nr, siginfo_t *info, void *void_context)
912 memcpy(&TRAP_info, info, sizeof(TRAP_info));
913 TRAP_nr = nr;
916 TEST_F(TRAP, handler)
918 int ret, test;
919 struct sigaction act;
920 sigset_t mask;
922 memset(&act, 0, sizeof(act));
923 sigemptyset(&mask);
924 sigaddset(&mask, SIGSYS);
926 act.sa_sigaction = &TRAP_action;
927 act.sa_flags = SA_SIGINFO;
928 ret = sigaction(SIGSYS, &act, NULL);
929 ASSERT_EQ(0, ret) {
930 TH_LOG("sigaction failed");
932 ret = sigprocmask(SIG_UNBLOCK, &mask, NULL);
933 ASSERT_EQ(0, ret) {
934 TH_LOG("sigprocmask failed");
937 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
938 ASSERT_EQ(0, ret);
939 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog);
940 ASSERT_EQ(0, ret);
941 TRAP_nr = 0;
942 memset(&TRAP_info, 0, sizeof(TRAP_info));
943 /* Expect the registers to be rolled back. (nr = error) may vary
944 * based on arch. */
945 ret = syscall(__NR_getpid);
946 /* Silence gcc warning about volatile. */
947 test = TRAP_nr;
948 EXPECT_EQ(SIGSYS, test);
949 struct local_sigsys {
950 void *_call_addr; /* calling user insn */
951 int _syscall; /* triggering system call number */
952 unsigned int _arch; /* AUDIT_ARCH_* of syscall */
953 } *sigsys = (struct local_sigsys *)
954 #ifdef si_syscall
955 &(TRAP_info.si_call_addr);
956 #else
957 &TRAP_info.si_pid;
958 #endif
959 EXPECT_EQ(__NR_getpid, sigsys->_syscall);
960 /* Make sure arch is non-zero. */
961 EXPECT_NE(0, sigsys->_arch);
962 EXPECT_NE(0, (unsigned long)sigsys->_call_addr);
965 FIXTURE_DATA(precedence) {
966 struct sock_fprog allow;
967 struct sock_fprog log;
968 struct sock_fprog trace;
969 struct sock_fprog error;
970 struct sock_fprog trap;
971 struct sock_fprog kill;
974 FIXTURE_SETUP(precedence)
976 struct sock_filter allow_insns[] = {
977 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
979 struct sock_filter log_insns[] = {
980 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
981 offsetof(struct seccomp_data, nr)),
982 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0),
983 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
984 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_LOG),
986 struct sock_filter trace_insns[] = {
987 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
988 offsetof(struct seccomp_data, nr)),
989 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0),
990 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
991 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE),
993 struct sock_filter error_insns[] = {
994 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
995 offsetof(struct seccomp_data, nr)),
996 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0),
997 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
998 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO),
1000 struct sock_filter trap_insns[] = {
1001 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1002 offsetof(struct seccomp_data, nr)),
1003 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0),
1004 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1005 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP),
1007 struct sock_filter kill_insns[] = {
1008 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1009 offsetof(struct seccomp_data, nr)),
1010 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0),
1011 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1012 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
1015 memset(self, 0, sizeof(*self));
1016 #define FILTER_ALLOC(_x) \
1017 self->_x.filter = malloc(sizeof(_x##_insns)); \
1018 ASSERT_NE(NULL, self->_x.filter); \
1019 memcpy(self->_x.filter, &_x##_insns, sizeof(_x##_insns)); \
1020 self->_x.len = (unsigned short)ARRAY_SIZE(_x##_insns)
1021 FILTER_ALLOC(allow);
1022 FILTER_ALLOC(log);
1023 FILTER_ALLOC(trace);
1024 FILTER_ALLOC(error);
1025 FILTER_ALLOC(trap);
1026 FILTER_ALLOC(kill);
1029 FIXTURE_TEARDOWN(precedence)
1031 #define FILTER_FREE(_x) if (self->_x.filter) free(self->_x.filter)
1032 FILTER_FREE(allow);
1033 FILTER_FREE(log);
1034 FILTER_FREE(trace);
1035 FILTER_FREE(error);
1036 FILTER_FREE(trap);
1037 FILTER_FREE(kill);
1040 TEST_F(precedence, allow_ok)
1042 pid_t parent, res = 0;
1043 long ret;
1045 parent = getppid();
1046 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1047 ASSERT_EQ(0, ret);
1049 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1050 ASSERT_EQ(0, ret);
1051 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1052 ASSERT_EQ(0, ret);
1053 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
1054 ASSERT_EQ(0, ret);
1055 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
1056 ASSERT_EQ(0, ret);
1057 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
1058 ASSERT_EQ(0, ret);
1059 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill);
1060 ASSERT_EQ(0, ret);
1061 /* Should work just fine. */
1062 res = syscall(__NR_getppid);
1063 EXPECT_EQ(parent, res);
1066 TEST_F_SIGNAL(precedence, kill_is_highest, SIGSYS)
1068 pid_t parent, res = 0;
1069 long ret;
1071 parent = getppid();
1072 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1073 ASSERT_EQ(0, ret);
1075 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1076 ASSERT_EQ(0, ret);
1077 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1078 ASSERT_EQ(0, ret);
1079 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
1080 ASSERT_EQ(0, ret);
1081 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
1082 ASSERT_EQ(0, ret);
1083 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
1084 ASSERT_EQ(0, ret);
1085 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill);
1086 ASSERT_EQ(0, ret);
1087 /* Should work just fine. */
1088 res = syscall(__NR_getppid);
1089 EXPECT_EQ(parent, res);
1090 /* getpid() should never return. */
1091 res = syscall(__NR_getpid);
1092 EXPECT_EQ(0, res);
1095 TEST_F_SIGNAL(precedence, kill_is_highest_in_any_order, SIGSYS)
1097 pid_t parent;
1098 long ret;
1100 parent = getppid();
1101 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1102 ASSERT_EQ(0, ret);
1104 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1105 ASSERT_EQ(0, ret);
1106 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill);
1107 ASSERT_EQ(0, ret);
1108 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
1109 ASSERT_EQ(0, ret);
1110 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1111 ASSERT_EQ(0, ret);
1112 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
1113 ASSERT_EQ(0, ret);
1114 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
1115 ASSERT_EQ(0, ret);
1116 /* Should work just fine. */
1117 EXPECT_EQ(parent, syscall(__NR_getppid));
1118 /* getpid() should never return. */
1119 EXPECT_EQ(0, syscall(__NR_getpid));
1122 TEST_F_SIGNAL(precedence, trap_is_second, SIGSYS)
1124 pid_t parent;
1125 long ret;
1127 parent = getppid();
1128 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1129 ASSERT_EQ(0, ret);
1131 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1132 ASSERT_EQ(0, ret);
1133 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1134 ASSERT_EQ(0, ret);
1135 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
1136 ASSERT_EQ(0, ret);
1137 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
1138 ASSERT_EQ(0, ret);
1139 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
1140 ASSERT_EQ(0, ret);
1141 /* Should work just fine. */
1142 EXPECT_EQ(parent, syscall(__NR_getppid));
1143 /* getpid() should never return. */
1144 EXPECT_EQ(0, syscall(__NR_getpid));
1147 TEST_F_SIGNAL(precedence, trap_is_second_in_any_order, SIGSYS)
1149 pid_t parent;
1150 long ret;
1152 parent = getppid();
1153 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1154 ASSERT_EQ(0, ret);
1156 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1157 ASSERT_EQ(0, ret);
1158 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
1159 ASSERT_EQ(0, ret);
1160 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1161 ASSERT_EQ(0, ret);
1162 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
1163 ASSERT_EQ(0, ret);
1164 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
1165 ASSERT_EQ(0, ret);
1166 /* Should work just fine. */
1167 EXPECT_EQ(parent, syscall(__NR_getppid));
1168 /* getpid() should never return. */
1169 EXPECT_EQ(0, syscall(__NR_getpid));
1172 TEST_F(precedence, errno_is_third)
1174 pid_t parent;
1175 long ret;
1177 parent = getppid();
1178 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1179 ASSERT_EQ(0, ret);
1181 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1182 ASSERT_EQ(0, ret);
1183 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1184 ASSERT_EQ(0, ret);
1185 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
1186 ASSERT_EQ(0, ret);
1187 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
1188 ASSERT_EQ(0, ret);
1189 /* Should work just fine. */
1190 EXPECT_EQ(parent, syscall(__NR_getppid));
1191 EXPECT_EQ(0, syscall(__NR_getpid));
1194 TEST_F(precedence, errno_is_third_in_any_order)
1196 pid_t parent;
1197 long ret;
1199 parent = getppid();
1200 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1201 ASSERT_EQ(0, ret);
1203 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1204 ASSERT_EQ(0, ret);
1205 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
1206 ASSERT_EQ(0, ret);
1207 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
1208 ASSERT_EQ(0, ret);
1209 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1210 ASSERT_EQ(0, ret);
1211 /* Should work just fine. */
1212 EXPECT_EQ(parent, syscall(__NR_getppid));
1213 EXPECT_EQ(0, syscall(__NR_getpid));
1216 TEST_F(precedence, trace_is_fourth)
1218 pid_t parent;
1219 long ret;
1221 parent = getppid();
1222 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1223 ASSERT_EQ(0, ret);
1225 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1226 ASSERT_EQ(0, ret);
1227 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1228 ASSERT_EQ(0, ret);
1229 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
1230 ASSERT_EQ(0, ret);
1231 /* Should work just fine. */
1232 EXPECT_EQ(parent, syscall(__NR_getppid));
1233 /* No ptracer */
1234 EXPECT_EQ(-1, syscall(__NR_getpid));
1237 TEST_F(precedence, trace_is_fourth_in_any_order)
1239 pid_t parent;
1240 long ret;
1242 parent = getppid();
1243 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1244 ASSERT_EQ(0, ret);
1246 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
1247 ASSERT_EQ(0, ret);
1248 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1249 ASSERT_EQ(0, ret);
1250 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1251 ASSERT_EQ(0, ret);
1252 /* Should work just fine. */
1253 EXPECT_EQ(parent, syscall(__NR_getppid));
1254 /* No ptracer */
1255 EXPECT_EQ(-1, syscall(__NR_getpid));
1258 TEST_F(precedence, log_is_fifth)
1260 pid_t mypid, parent;
1261 long ret;
1263 mypid = getpid();
1264 parent = getppid();
1265 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1266 ASSERT_EQ(0, ret);
1268 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1269 ASSERT_EQ(0, ret);
1270 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1271 ASSERT_EQ(0, ret);
1272 /* Should work just fine. */
1273 EXPECT_EQ(parent, syscall(__NR_getppid));
1274 /* Should also work just fine */
1275 EXPECT_EQ(mypid, syscall(__NR_getpid));
1278 TEST_F(precedence, log_is_fifth_in_any_order)
1280 pid_t mypid, parent;
1281 long ret;
1283 mypid = getpid();
1284 parent = getppid();
1285 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1286 ASSERT_EQ(0, ret);
1288 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log);
1289 ASSERT_EQ(0, ret);
1290 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
1291 ASSERT_EQ(0, ret);
1292 /* Should work just fine. */
1293 EXPECT_EQ(parent, syscall(__NR_getppid));
1294 /* Should also work just fine */
1295 EXPECT_EQ(mypid, syscall(__NR_getpid));
1298 #ifndef PTRACE_O_TRACESECCOMP
1299 #define PTRACE_O_TRACESECCOMP 0x00000080
1300 #endif
1302 /* Catch the Ubuntu 12.04 value error. */
1303 #if PTRACE_EVENT_SECCOMP != 7
1304 #undef PTRACE_EVENT_SECCOMP
1305 #endif
1307 #ifndef PTRACE_EVENT_SECCOMP
1308 #define PTRACE_EVENT_SECCOMP 7
1309 #endif
1311 #define IS_SECCOMP_EVENT(status) ((status >> 16) == PTRACE_EVENT_SECCOMP)
1312 bool tracer_running;
1313 void tracer_stop(int sig)
1315 tracer_running = false;
1318 typedef void tracer_func_t(struct __test_metadata *_metadata,
1319 pid_t tracee, int status, void *args);
1321 void start_tracer(struct __test_metadata *_metadata, int fd, pid_t tracee,
1322 tracer_func_t tracer_func, void *args, bool ptrace_syscall)
1324 int ret = -1;
1325 struct sigaction action = {
1326 .sa_handler = tracer_stop,
1329 /* Allow external shutdown. */
1330 tracer_running = true;
1331 ASSERT_EQ(0, sigaction(SIGUSR1, &action, NULL));
1333 errno = 0;
1334 while (ret == -1 && errno != EINVAL)
1335 ret = ptrace(PTRACE_ATTACH, tracee, NULL, 0);
1336 ASSERT_EQ(0, ret) {
1337 kill(tracee, SIGKILL);
1339 /* Wait for attach stop */
1340 wait(NULL);
1342 ret = ptrace(PTRACE_SETOPTIONS, tracee, NULL, ptrace_syscall ?
1343 PTRACE_O_TRACESYSGOOD :
1344 PTRACE_O_TRACESECCOMP);
1345 ASSERT_EQ(0, ret) {
1346 TH_LOG("Failed to set PTRACE_O_TRACESECCOMP");
1347 kill(tracee, SIGKILL);
1349 ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT,
1350 tracee, NULL, 0);
1351 ASSERT_EQ(0, ret);
1353 /* Unblock the tracee */
1354 ASSERT_EQ(1, write(fd, "A", 1));
1355 ASSERT_EQ(0, close(fd));
1357 /* Run until we're shut down. Must assert to stop execution. */
1358 while (tracer_running) {
1359 int status;
1361 if (wait(&status) != tracee)
1362 continue;
1363 if (WIFSIGNALED(status) || WIFEXITED(status))
1364 /* Child is dead. Time to go. */
1365 return;
1367 /* Check if this is a seccomp event. */
1368 ASSERT_EQ(!ptrace_syscall, IS_SECCOMP_EVENT(status));
1370 tracer_func(_metadata, tracee, status, args);
1372 ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT,
1373 tracee, NULL, 0);
1374 ASSERT_EQ(0, ret);
1376 /* Directly report the status of our test harness results. */
1377 syscall(__NR_exit, _metadata->passed ? EXIT_SUCCESS : EXIT_FAILURE);
1380 /* Common tracer setup/teardown functions. */
1381 void cont_handler(int num)
1383 pid_t setup_trace_fixture(struct __test_metadata *_metadata,
1384 tracer_func_t func, void *args, bool ptrace_syscall)
1386 char sync;
1387 int pipefd[2];
1388 pid_t tracer_pid;
1389 pid_t tracee = getpid();
1391 /* Setup a pipe for clean synchronization. */
1392 ASSERT_EQ(0, pipe(pipefd));
1394 /* Fork a child which we'll promote to tracer */
1395 tracer_pid = fork();
1396 ASSERT_LE(0, tracer_pid);
1397 signal(SIGALRM, cont_handler);
1398 if (tracer_pid == 0) {
1399 close(pipefd[0]);
1400 start_tracer(_metadata, pipefd[1], tracee, func, args,
1401 ptrace_syscall);
1402 syscall(__NR_exit, 0);
1404 close(pipefd[1]);
1405 prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0);
1406 read(pipefd[0], &sync, 1);
1407 close(pipefd[0]);
1409 return tracer_pid;
1411 void teardown_trace_fixture(struct __test_metadata *_metadata,
1412 pid_t tracer)
1414 if (tracer) {
1415 int status;
1417 * Extract the exit code from the other process and
1418 * adopt it for ourselves in case its asserts failed.
1420 ASSERT_EQ(0, kill(tracer, SIGUSR1));
1421 ASSERT_EQ(tracer, waitpid(tracer, &status, 0));
1422 if (WEXITSTATUS(status))
1423 _metadata->passed = 0;
1427 /* "poke" tracer arguments and function. */
1428 struct tracer_args_poke_t {
1429 unsigned long poke_addr;
1432 void tracer_poke(struct __test_metadata *_metadata, pid_t tracee, int status,
1433 void *args)
1435 int ret;
1436 unsigned long msg;
1437 struct tracer_args_poke_t *info = (struct tracer_args_poke_t *)args;
1439 ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg);
1440 EXPECT_EQ(0, ret);
1441 /* If this fails, don't try to recover. */
1442 ASSERT_EQ(0x1001, msg) {
1443 kill(tracee, SIGKILL);
1446 * Poke in the message.
1447 * Registers are not touched to try to keep this relatively arch
1448 * agnostic.
1450 ret = ptrace(PTRACE_POKEDATA, tracee, info->poke_addr, 0x1001);
1451 EXPECT_EQ(0, ret);
1454 FIXTURE_DATA(TRACE_poke) {
1455 struct sock_fprog prog;
1456 pid_t tracer;
1457 long poked;
1458 struct tracer_args_poke_t tracer_args;
1461 FIXTURE_SETUP(TRACE_poke)
1463 struct sock_filter filter[] = {
1464 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1465 offsetof(struct seccomp_data, nr)),
1466 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1),
1467 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1001),
1468 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1471 self->poked = 0;
1472 memset(&self->prog, 0, sizeof(self->prog));
1473 self->prog.filter = malloc(sizeof(filter));
1474 ASSERT_NE(NULL, self->prog.filter);
1475 memcpy(self->prog.filter, filter, sizeof(filter));
1476 self->prog.len = (unsigned short)ARRAY_SIZE(filter);
1478 /* Set up tracer args. */
1479 self->tracer_args.poke_addr = (unsigned long)&self->poked;
1481 /* Launch tracer. */
1482 self->tracer = setup_trace_fixture(_metadata, tracer_poke,
1483 &self->tracer_args, false);
1486 FIXTURE_TEARDOWN(TRACE_poke)
1488 teardown_trace_fixture(_metadata, self->tracer);
1489 if (self->prog.filter)
1490 free(self->prog.filter);
1493 TEST_F(TRACE_poke, read_has_side_effects)
1495 ssize_t ret;
1497 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1498 ASSERT_EQ(0, ret);
1500 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1501 ASSERT_EQ(0, ret);
1503 EXPECT_EQ(0, self->poked);
1504 ret = read(-1, NULL, 0);
1505 EXPECT_EQ(-1, ret);
1506 EXPECT_EQ(0x1001, self->poked);
1509 TEST_F(TRACE_poke, getpid_runs_normally)
1511 long ret;
1513 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1514 ASSERT_EQ(0, ret);
1516 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1517 ASSERT_EQ(0, ret);
1519 EXPECT_EQ(0, self->poked);
1520 EXPECT_NE(0, syscall(__NR_getpid));
1521 EXPECT_EQ(0, self->poked);
1524 #if defined(__x86_64__)
1525 # define ARCH_REGS struct user_regs_struct
1526 # define SYSCALL_NUM orig_rax
1527 # define SYSCALL_RET rax
1528 #elif defined(__i386__)
1529 # define ARCH_REGS struct user_regs_struct
1530 # define SYSCALL_NUM orig_eax
1531 # define SYSCALL_RET eax
1532 #elif defined(__arm__)
1533 # define ARCH_REGS struct pt_regs
1534 # define SYSCALL_NUM ARM_r7
1535 # define SYSCALL_RET ARM_r0
1536 #elif defined(__aarch64__)
1537 # define ARCH_REGS struct user_pt_regs
1538 # define SYSCALL_NUM regs[8]
1539 # define SYSCALL_RET regs[0]
1540 #elif defined(__hppa__)
1541 # define ARCH_REGS struct user_regs_struct
1542 # define SYSCALL_NUM gr[20]
1543 # define SYSCALL_RET gr[28]
1544 #elif defined(__powerpc__)
1545 # define ARCH_REGS struct pt_regs
1546 # define SYSCALL_NUM gpr[0]
1547 # define SYSCALL_RET gpr[3]
1548 #elif defined(__s390__)
1549 # define ARCH_REGS s390_regs
1550 # define SYSCALL_NUM gprs[2]
1551 # define SYSCALL_RET gprs[2]
1552 #elif defined(__mips__)
1553 # define ARCH_REGS struct pt_regs
1554 # define SYSCALL_NUM regs[2]
1555 # define SYSCALL_SYSCALL_NUM regs[4]
1556 # define SYSCALL_RET regs[2]
1557 # define SYSCALL_NUM_RET_SHARE_REG
1558 #else
1559 # error "Do not know how to find your architecture's registers and syscalls"
1560 #endif
1562 /* When the syscall return can't be changed, stub out the tests for it. */
1563 #ifdef SYSCALL_NUM_RET_SHARE_REG
1564 # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(-1, action)
1565 #else
1566 # define EXPECT_SYSCALL_RETURN(val, action) \
1567 do { \
1568 errno = 0; \
1569 if (val < 0) { \
1570 EXPECT_EQ(-1, action); \
1571 EXPECT_EQ(-(val), errno); \
1572 } else { \
1573 EXPECT_EQ(val, action); \
1575 } while (0)
1576 #endif
1578 /* Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for
1579 * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux).
1581 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__)
1582 #define HAVE_GETREGS
1583 #endif
1585 /* Architecture-specific syscall fetching routine. */
1586 int get_syscall(struct __test_metadata *_metadata, pid_t tracee)
1588 ARCH_REGS regs;
1589 #ifdef HAVE_GETREGS
1590 EXPECT_EQ(0, ptrace(PTRACE_GETREGS, tracee, 0, &regs)) {
1591 TH_LOG("PTRACE_GETREGS failed");
1592 return -1;
1594 #else
1595 struct iovec iov;
1597 iov.iov_base = &regs;
1598 iov.iov_len = sizeof(regs);
1599 EXPECT_EQ(0, ptrace(PTRACE_GETREGSET, tracee, NT_PRSTATUS, &iov)) {
1600 TH_LOG("PTRACE_GETREGSET failed");
1601 return -1;
1603 #endif
1605 #if defined(__mips__)
1606 if (regs.SYSCALL_NUM == __NR_O32_Linux)
1607 return regs.SYSCALL_SYSCALL_NUM;
1608 #endif
1609 return regs.SYSCALL_NUM;
1612 /* Architecture-specific syscall changing routine. */
1613 void change_syscall(struct __test_metadata *_metadata,
1614 pid_t tracee, int syscall, int result)
1616 int ret;
1617 ARCH_REGS regs;
1618 #ifdef HAVE_GETREGS
1619 ret = ptrace(PTRACE_GETREGS, tracee, 0, &regs);
1620 #else
1621 struct iovec iov;
1622 iov.iov_base = &regs;
1623 iov.iov_len = sizeof(regs);
1624 ret = ptrace(PTRACE_GETREGSET, tracee, NT_PRSTATUS, &iov);
1625 #endif
1626 EXPECT_EQ(0, ret) {}
1628 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \
1629 defined(__s390__) || defined(__hppa__)
1631 regs.SYSCALL_NUM = syscall;
1633 #elif defined(__mips__)
1635 if (regs.SYSCALL_NUM == __NR_O32_Linux)
1636 regs.SYSCALL_SYSCALL_NUM = syscall;
1637 else
1638 regs.SYSCALL_NUM = syscall;
1641 #elif defined(__arm__)
1642 # ifndef PTRACE_SET_SYSCALL
1643 # define PTRACE_SET_SYSCALL 23
1644 # endif
1646 ret = ptrace(PTRACE_SET_SYSCALL, tracee, NULL, syscall);
1647 EXPECT_EQ(0, ret);
1650 #elif defined(__aarch64__)
1651 # ifndef NT_ARM_SYSTEM_CALL
1652 # define NT_ARM_SYSTEM_CALL 0x404
1653 # endif
1655 iov.iov_base = &syscall;
1656 iov.iov_len = sizeof(syscall);
1657 ret = ptrace(PTRACE_SETREGSET, tracee, NT_ARM_SYSTEM_CALL,
1658 &iov);
1659 EXPECT_EQ(0, ret);
1662 #else
1663 ASSERT_EQ(1, 0) {
1664 TH_LOG("How is the syscall changed on this architecture?");
1666 #endif
1668 /* If syscall is skipped, change return value. */
1669 if (syscall == -1)
1670 #ifdef SYSCALL_NUM_RET_SHARE_REG
1671 TH_LOG("Can't modify syscall return on this architecture");
1672 #else
1673 regs.SYSCALL_RET = result;
1674 #endif
1676 #ifdef HAVE_GETREGS
1677 ret = ptrace(PTRACE_SETREGS, tracee, 0, &regs);
1678 #else
1679 iov.iov_base = &regs;
1680 iov.iov_len = sizeof(regs);
1681 ret = ptrace(PTRACE_SETREGSET, tracee, NT_PRSTATUS, &iov);
1682 #endif
1683 EXPECT_EQ(0, ret);
1686 void tracer_syscall(struct __test_metadata *_metadata, pid_t tracee,
1687 int status, void *args)
1689 int ret;
1690 unsigned long msg;
1692 /* Make sure we got the right message. */
1693 ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg);
1694 EXPECT_EQ(0, ret);
1696 /* Validate and take action on expected syscalls. */
1697 switch (msg) {
1698 case 0x1002:
1699 /* change getpid to getppid. */
1700 EXPECT_EQ(__NR_getpid, get_syscall(_metadata, tracee));
1701 change_syscall(_metadata, tracee, __NR_getppid, 0);
1702 break;
1703 case 0x1003:
1704 /* skip gettid with valid return code. */
1705 EXPECT_EQ(__NR_gettid, get_syscall(_metadata, tracee));
1706 change_syscall(_metadata, tracee, -1, 45000);
1707 break;
1708 case 0x1004:
1709 /* skip openat with error. */
1710 EXPECT_EQ(__NR_openat, get_syscall(_metadata, tracee));
1711 change_syscall(_metadata, tracee, -1, -ESRCH);
1712 break;
1713 case 0x1005:
1714 /* do nothing (allow getppid) */
1715 EXPECT_EQ(__NR_getppid, get_syscall(_metadata, tracee));
1716 break;
1717 default:
1718 EXPECT_EQ(0, msg) {
1719 TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg);
1720 kill(tracee, SIGKILL);
1726 void tracer_ptrace(struct __test_metadata *_metadata, pid_t tracee,
1727 int status, void *args)
1729 int ret, nr;
1730 unsigned long msg;
1731 static bool entry;
1733 /* Make sure we got an empty message. */
1734 ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg);
1735 EXPECT_EQ(0, ret);
1736 EXPECT_EQ(0, msg);
1738 /* The only way to tell PTRACE_SYSCALL entry/exit is by counting. */
1739 entry = !entry;
1740 if (!entry)
1741 return;
1743 nr = get_syscall(_metadata, tracee);
1745 if (nr == __NR_getpid)
1746 change_syscall(_metadata, tracee, __NR_getppid, 0);
1747 if (nr == __NR_gettid)
1748 change_syscall(_metadata, tracee, -1, 45000);
1749 if (nr == __NR_openat)
1750 change_syscall(_metadata, tracee, -1, -ESRCH);
1753 FIXTURE_DATA(TRACE_syscall) {
1754 struct sock_fprog prog;
1755 pid_t tracer, mytid, mypid, parent;
1758 FIXTURE_SETUP(TRACE_syscall)
1760 struct sock_filter filter[] = {
1761 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1762 offsetof(struct seccomp_data, nr)),
1763 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1),
1764 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1002),
1765 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_gettid, 0, 1),
1766 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1003),
1767 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_openat, 0, 1),
1768 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1004),
1769 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1770 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1005),
1771 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1774 memset(&self->prog, 0, sizeof(self->prog));
1775 self->prog.filter = malloc(sizeof(filter));
1776 ASSERT_NE(NULL, self->prog.filter);
1777 memcpy(self->prog.filter, filter, sizeof(filter));
1778 self->prog.len = (unsigned short)ARRAY_SIZE(filter);
1780 /* Prepare some testable syscall results. */
1781 self->mytid = syscall(__NR_gettid);
1782 ASSERT_GT(self->mytid, 0);
1783 ASSERT_NE(self->mytid, 1) {
1784 TH_LOG("Running this test as init is not supported. :)");
1787 self->mypid = getpid();
1788 ASSERT_GT(self->mypid, 0);
1789 ASSERT_EQ(self->mytid, self->mypid);
1791 self->parent = getppid();
1792 ASSERT_GT(self->parent, 0);
1793 ASSERT_NE(self->parent, self->mypid);
1795 /* Launch tracer. */
1796 self->tracer = setup_trace_fixture(_metadata, tracer_syscall, NULL,
1797 false);
1800 FIXTURE_TEARDOWN(TRACE_syscall)
1802 teardown_trace_fixture(_metadata, self->tracer);
1803 if (self->prog.filter)
1804 free(self->prog.filter);
1807 TEST_F(TRACE_syscall, ptrace_syscall_redirected)
1809 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1810 teardown_trace_fixture(_metadata, self->tracer);
1811 self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL,
1812 true);
1814 /* Tracer will redirect getpid to getppid. */
1815 EXPECT_NE(self->mypid, syscall(__NR_getpid));
1818 TEST_F(TRACE_syscall, ptrace_syscall_errno)
1820 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1821 teardown_trace_fixture(_metadata, self->tracer);
1822 self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL,
1823 true);
1825 /* Tracer should skip the open syscall, resulting in ESRCH. */
1826 EXPECT_SYSCALL_RETURN(-ESRCH, syscall(__NR_openat));
1829 TEST_F(TRACE_syscall, ptrace_syscall_faked)
1831 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1832 teardown_trace_fixture(_metadata, self->tracer);
1833 self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL,
1834 true);
1836 /* Tracer should skip the gettid syscall, resulting fake pid. */
1837 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid));
1840 TEST_F(TRACE_syscall, syscall_allowed)
1842 long ret;
1844 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1845 ASSERT_EQ(0, ret);
1847 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1848 ASSERT_EQ(0, ret);
1850 /* getppid works as expected (no changes). */
1851 EXPECT_EQ(self->parent, syscall(__NR_getppid));
1852 EXPECT_NE(self->mypid, syscall(__NR_getppid));
1855 TEST_F(TRACE_syscall, syscall_redirected)
1857 long ret;
1859 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1860 ASSERT_EQ(0, ret);
1862 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1863 ASSERT_EQ(0, ret);
1865 /* getpid has been redirected to getppid as expected. */
1866 EXPECT_EQ(self->parent, syscall(__NR_getpid));
1867 EXPECT_NE(self->mypid, syscall(__NR_getpid));
1870 TEST_F(TRACE_syscall, syscall_errno)
1872 long ret;
1874 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1875 ASSERT_EQ(0, ret);
1877 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1878 ASSERT_EQ(0, ret);
1880 /* openat has been skipped and an errno return. */
1881 EXPECT_SYSCALL_RETURN(-ESRCH, syscall(__NR_openat));
1884 TEST_F(TRACE_syscall, syscall_faked)
1886 long ret;
1888 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1889 ASSERT_EQ(0, ret);
1891 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1892 ASSERT_EQ(0, ret);
1894 /* gettid has been skipped and an altered return value stored. */
1895 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid));
1898 TEST_F(TRACE_syscall, skip_after_RET_TRACE)
1900 struct sock_filter filter[] = {
1901 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1902 offsetof(struct seccomp_data, nr)),
1903 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1904 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EPERM),
1905 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1907 struct sock_fprog prog = {
1908 .len = (unsigned short)ARRAY_SIZE(filter),
1909 .filter = filter,
1911 long ret;
1913 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1914 ASSERT_EQ(0, ret);
1916 /* Install fixture filter. */
1917 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1918 ASSERT_EQ(0, ret);
1920 /* Install "errno on getppid" filter. */
1921 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
1922 ASSERT_EQ(0, ret);
1924 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
1925 errno = 0;
1926 EXPECT_EQ(-1, syscall(__NR_getpid));
1927 EXPECT_EQ(EPERM, errno);
1930 TEST_F_SIGNAL(TRACE_syscall, kill_after_RET_TRACE, SIGSYS)
1932 struct sock_filter filter[] = {
1933 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1934 offsetof(struct seccomp_data, nr)),
1935 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1936 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
1937 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1939 struct sock_fprog prog = {
1940 .len = (unsigned short)ARRAY_SIZE(filter),
1941 .filter = filter,
1943 long ret;
1945 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1946 ASSERT_EQ(0, ret);
1948 /* Install fixture filter. */
1949 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1950 ASSERT_EQ(0, ret);
1952 /* Install "death on getppid" filter. */
1953 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
1954 ASSERT_EQ(0, ret);
1956 /* Tracer will redirect getpid to getppid, and we should die. */
1957 EXPECT_NE(self->mypid, syscall(__NR_getpid));
1960 TEST_F(TRACE_syscall, skip_after_ptrace)
1962 struct sock_filter filter[] = {
1963 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1964 offsetof(struct seccomp_data, nr)),
1965 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1966 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EPERM),
1967 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1969 struct sock_fprog prog = {
1970 .len = (unsigned short)ARRAY_SIZE(filter),
1971 .filter = filter,
1973 long ret;
1975 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1976 teardown_trace_fixture(_metadata, self->tracer);
1977 self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL,
1978 true);
1980 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1981 ASSERT_EQ(0, ret);
1983 /* Install "errno on getppid" filter. */
1984 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
1985 ASSERT_EQ(0, ret);
1987 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
1988 EXPECT_EQ(-1, syscall(__NR_getpid));
1989 EXPECT_EQ(EPERM, errno);
1992 TEST_F_SIGNAL(TRACE_syscall, kill_after_ptrace, SIGSYS)
1994 struct sock_filter filter[] = {
1995 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1996 offsetof(struct seccomp_data, nr)),
1997 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1998 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
1999 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2001 struct sock_fprog prog = {
2002 .len = (unsigned short)ARRAY_SIZE(filter),
2003 .filter = filter,
2005 long ret;
2007 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
2008 teardown_trace_fixture(_metadata, self->tracer);
2009 self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL,
2010 true);
2012 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
2013 ASSERT_EQ(0, ret);
2015 /* Install "death on getppid" filter. */
2016 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
2017 ASSERT_EQ(0, ret);
2019 /* Tracer will redirect getpid to getppid, and we should die. */
2020 EXPECT_NE(self->mypid, syscall(__NR_getpid));
2023 TEST(seccomp_syscall)
2025 struct sock_filter filter[] = {
2026 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2028 struct sock_fprog prog = {
2029 .len = (unsigned short)ARRAY_SIZE(filter),
2030 .filter = filter,
2032 long ret;
2034 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
2035 ASSERT_EQ(0, ret) {
2036 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2039 /* Reject insane operation. */
2040 ret = seccomp(-1, 0, &prog);
2041 ASSERT_NE(ENOSYS, errno) {
2042 TH_LOG("Kernel does not support seccomp syscall!");
2044 EXPECT_EQ(EINVAL, errno) {
2045 TH_LOG("Did not reject crazy op value!");
2048 /* Reject strict with flags or pointer. */
2049 ret = seccomp(SECCOMP_SET_MODE_STRICT, -1, NULL);
2050 EXPECT_EQ(EINVAL, errno) {
2051 TH_LOG("Did not reject mode strict with flags!");
2053 ret = seccomp(SECCOMP_SET_MODE_STRICT, 0, &prog);
2054 EXPECT_EQ(EINVAL, errno) {
2055 TH_LOG("Did not reject mode strict with uargs!");
2058 /* Reject insane args for filter. */
2059 ret = seccomp(SECCOMP_SET_MODE_FILTER, -1, &prog);
2060 EXPECT_EQ(EINVAL, errno) {
2061 TH_LOG("Did not reject crazy filter flags!");
2063 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, NULL);
2064 EXPECT_EQ(EFAULT, errno) {
2065 TH_LOG("Did not reject NULL filter!");
2068 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog);
2069 EXPECT_EQ(0, errno) {
2070 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s",
2071 strerror(errno));
2075 TEST(seccomp_syscall_mode_lock)
2077 struct sock_filter filter[] = {
2078 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2080 struct sock_fprog prog = {
2081 .len = (unsigned short)ARRAY_SIZE(filter),
2082 .filter = filter,
2084 long ret;
2086 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0);
2087 ASSERT_EQ(0, ret) {
2088 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2091 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog);
2092 ASSERT_NE(ENOSYS, errno) {
2093 TH_LOG("Kernel does not support seccomp syscall!");
2095 EXPECT_EQ(0, ret) {
2096 TH_LOG("Could not install filter!");
2099 /* Make sure neither entry point will switch to strict. */
2100 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, 0, 0, 0);
2101 EXPECT_EQ(EINVAL, errno) {
2102 TH_LOG("Switched to mode strict!");
2105 ret = seccomp(SECCOMP_SET_MODE_STRICT, 0, NULL);
2106 EXPECT_EQ(EINVAL, errno) {
2107 TH_LOG("Switched to mode strict!");
2112 * Test detection of known and unknown filter flags. Userspace needs to be able
2113 * to check if a filter flag is supported by the current kernel and a good way
2114 * of doing that is by attempting to enter filter mode, with the flag bit in
2115 * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates
2116 * that the flag is valid and EINVAL indicates that the flag is invalid.
2118 TEST(detect_seccomp_filter_flags)
2120 unsigned int flags[] = { SECCOMP_FILTER_FLAG_TSYNC,
2121 SECCOMP_FILTER_FLAG_LOG,
2122 SECCOMP_FILTER_FLAG_SPEC_ALLOW };
2123 unsigned int flag, all_flags;
2124 int i;
2125 long ret;
2127 /* Test detection of known-good filter flags */
2128 for (i = 0, all_flags = 0; i < ARRAY_SIZE(flags); i++) {
2129 int bits = 0;
2131 flag = flags[i];
2132 /* Make sure the flag is a single bit! */
2133 while (flag) {
2134 if (flag & 0x1)
2135 bits ++;
2136 flag >>= 1;
2138 ASSERT_EQ(1, bits);
2139 flag = flags[i];
2141 ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL);
2142 ASSERT_NE(ENOSYS, errno) {
2143 TH_LOG("Kernel does not support seccomp syscall!");
2145 EXPECT_EQ(-1, ret);
2146 EXPECT_EQ(EFAULT, errno) {
2147 TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!",
2148 flag);
2151 all_flags |= flag;
2154 /* Test detection of all known-good filter flags */
2155 ret = seccomp(SECCOMP_SET_MODE_FILTER, all_flags, NULL);
2156 EXPECT_EQ(-1, ret);
2157 EXPECT_EQ(EFAULT, errno) {
2158 TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2159 all_flags);
2162 /* Test detection of an unknown filter flag */
2163 flag = -1;
2164 ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL);
2165 EXPECT_EQ(-1, ret);
2166 EXPECT_EQ(EINVAL, errno) {
2167 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!",
2168 flag);
2172 * Test detection of an unknown filter flag that may simply need to be
2173 * added to this test
2175 flag = flags[ARRAY_SIZE(flags) - 1] << 1;
2176 ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL);
2177 EXPECT_EQ(-1, ret);
2178 EXPECT_EQ(EINVAL, errno) {
2179 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported! Does a new flag need to be added to this test?",
2180 flag);
2184 TEST(TSYNC_first)
2186 struct sock_filter filter[] = {
2187 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2189 struct sock_fprog prog = {
2190 .len = (unsigned short)ARRAY_SIZE(filter),
2191 .filter = filter,
2193 long ret;
2195 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0);
2196 ASSERT_EQ(0, ret) {
2197 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2200 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2201 &prog);
2202 ASSERT_NE(ENOSYS, errno) {
2203 TH_LOG("Kernel does not support seccomp syscall!");
2205 EXPECT_EQ(0, ret) {
2206 TH_LOG("Could not install initial filter with TSYNC!");
2210 #define TSYNC_SIBLINGS 2
2211 struct tsync_sibling {
2212 pthread_t tid;
2213 pid_t system_tid;
2214 sem_t *started;
2215 pthread_cond_t *cond;
2216 pthread_mutex_t *mutex;
2217 int diverge;
2218 int num_waits;
2219 struct sock_fprog *prog;
2220 struct __test_metadata *metadata;
2224 * To avoid joining joined threads (which is not allowed by Bionic),
2225 * make sure we both successfully join and clear the tid to skip a
2226 * later join attempt during fixture teardown. Any remaining threads
2227 * will be directly killed during teardown.
2229 #define PTHREAD_JOIN(tid, status) \
2230 do { \
2231 int _rc = pthread_join(tid, status); \
2232 if (_rc) { \
2233 TH_LOG("pthread_join of tid %u failed: %d\n", \
2234 (unsigned int)tid, _rc); \
2235 } else { \
2236 tid = 0; \
2238 } while (0)
2240 FIXTURE_DATA(TSYNC) {
2241 struct sock_fprog root_prog, apply_prog;
2242 struct tsync_sibling sibling[TSYNC_SIBLINGS];
2243 sem_t started;
2244 pthread_cond_t cond;
2245 pthread_mutex_t mutex;
2246 int sibling_count;
2249 FIXTURE_SETUP(TSYNC)
2251 struct sock_filter root_filter[] = {
2252 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2254 struct sock_filter apply_filter[] = {
2255 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
2256 offsetof(struct seccomp_data, nr)),
2257 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1),
2258 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
2259 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2262 memset(&self->root_prog, 0, sizeof(self->root_prog));
2263 memset(&self->apply_prog, 0, sizeof(self->apply_prog));
2264 memset(&self->sibling, 0, sizeof(self->sibling));
2265 self->root_prog.filter = malloc(sizeof(root_filter));
2266 ASSERT_NE(NULL, self->root_prog.filter);
2267 memcpy(self->root_prog.filter, &root_filter, sizeof(root_filter));
2268 self->root_prog.len = (unsigned short)ARRAY_SIZE(root_filter);
2270 self->apply_prog.filter = malloc(sizeof(apply_filter));
2271 ASSERT_NE(NULL, self->apply_prog.filter);
2272 memcpy(self->apply_prog.filter, &apply_filter, sizeof(apply_filter));
2273 self->apply_prog.len = (unsigned short)ARRAY_SIZE(apply_filter);
2275 self->sibling_count = 0;
2276 pthread_mutex_init(&self->mutex, NULL);
2277 pthread_cond_init(&self->cond, NULL);
2278 sem_init(&self->started, 0, 0);
2279 self->sibling[0].tid = 0;
2280 self->sibling[0].cond = &self->cond;
2281 self->sibling[0].started = &self->started;
2282 self->sibling[0].mutex = &self->mutex;
2283 self->sibling[0].diverge = 0;
2284 self->sibling[0].num_waits = 1;
2285 self->sibling[0].prog = &self->root_prog;
2286 self->sibling[0].metadata = _metadata;
2287 self->sibling[1].tid = 0;
2288 self->sibling[1].cond = &self->cond;
2289 self->sibling[1].started = &self->started;
2290 self->sibling[1].mutex = &self->mutex;
2291 self->sibling[1].diverge = 0;
2292 self->sibling[1].prog = &self->root_prog;
2293 self->sibling[1].num_waits = 1;
2294 self->sibling[1].metadata = _metadata;
2297 FIXTURE_TEARDOWN(TSYNC)
2299 int sib = 0;
2301 if (self->root_prog.filter)
2302 free(self->root_prog.filter);
2303 if (self->apply_prog.filter)
2304 free(self->apply_prog.filter);
2306 for ( ; sib < self->sibling_count; ++sib) {
2307 struct tsync_sibling *s = &self->sibling[sib];
2309 if (!s->tid)
2310 continue;
2312 * If a thread is still running, it may be stuck, so hit
2313 * it over the head really hard.
2315 pthread_kill(s->tid, 9);
2317 pthread_mutex_destroy(&self->mutex);
2318 pthread_cond_destroy(&self->cond);
2319 sem_destroy(&self->started);
2322 void *tsync_sibling(void *data)
2324 long ret = 0;
2325 struct tsync_sibling *me = data;
2327 me->system_tid = syscall(__NR_gettid);
2329 pthread_mutex_lock(me->mutex);
2330 if (me->diverge) {
2331 /* Just re-apply the root prog to fork the tree */
2332 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
2333 me->prog, 0, 0);
2335 sem_post(me->started);
2336 /* Return outside of started so parent notices failures. */
2337 if (ret) {
2338 pthread_mutex_unlock(me->mutex);
2339 return (void *)SIBLING_EXIT_FAILURE;
2341 do {
2342 pthread_cond_wait(me->cond, me->mutex);
2343 me->num_waits = me->num_waits - 1;
2344 } while (me->num_waits);
2345 pthread_mutex_unlock(me->mutex);
2347 ret = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
2348 if (!ret)
2349 return (void *)SIBLING_EXIT_NEWPRIVS;
2350 read(0, NULL, 0);
2351 return (void *)SIBLING_EXIT_UNKILLED;
2354 void tsync_start_sibling(struct tsync_sibling *sibling)
2356 pthread_create(&sibling->tid, NULL, tsync_sibling, (void *)sibling);
2359 TEST_F(TSYNC, siblings_fail_prctl)
2361 long ret;
2362 void *status;
2363 struct sock_filter filter[] = {
2364 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
2365 offsetof(struct seccomp_data, nr)),
2366 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1),
2367 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EINVAL),
2368 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2370 struct sock_fprog prog = {
2371 .len = (unsigned short)ARRAY_SIZE(filter),
2372 .filter = filter,
2375 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2376 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2379 /* Check prctl failure detection by requesting sib 0 diverge. */
2380 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog);
2381 ASSERT_NE(ENOSYS, errno) {
2382 TH_LOG("Kernel does not support seccomp syscall!");
2384 ASSERT_EQ(0, ret) {
2385 TH_LOG("setting filter failed");
2388 self->sibling[0].diverge = 1;
2389 tsync_start_sibling(&self->sibling[0]);
2390 tsync_start_sibling(&self->sibling[1]);
2392 while (self->sibling_count < TSYNC_SIBLINGS) {
2393 sem_wait(&self->started);
2394 self->sibling_count++;
2397 /* Signal the threads to clean up*/
2398 pthread_mutex_lock(&self->mutex);
2399 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2400 TH_LOG("cond broadcast non-zero");
2402 pthread_mutex_unlock(&self->mutex);
2404 /* Ensure diverging sibling failed to call prctl. */
2405 PTHREAD_JOIN(self->sibling[0].tid, &status);
2406 EXPECT_EQ(SIBLING_EXIT_FAILURE, (long)status);
2407 PTHREAD_JOIN(self->sibling[1].tid, &status);
2408 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
2411 TEST_F(TSYNC, two_siblings_with_ancestor)
2413 long ret;
2414 void *status;
2416 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2417 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2420 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog);
2421 ASSERT_NE(ENOSYS, errno) {
2422 TH_LOG("Kernel does not support seccomp syscall!");
2424 ASSERT_EQ(0, ret) {
2425 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2427 tsync_start_sibling(&self->sibling[0]);
2428 tsync_start_sibling(&self->sibling[1]);
2430 while (self->sibling_count < TSYNC_SIBLINGS) {
2431 sem_wait(&self->started);
2432 self->sibling_count++;
2435 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2436 &self->apply_prog);
2437 ASSERT_EQ(0, ret) {
2438 TH_LOG("Could install filter on all threads!");
2440 /* Tell the siblings to test the policy */
2441 pthread_mutex_lock(&self->mutex);
2442 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2443 TH_LOG("cond broadcast non-zero");
2445 pthread_mutex_unlock(&self->mutex);
2446 /* Ensure they are both killed and don't exit cleanly. */
2447 PTHREAD_JOIN(self->sibling[0].tid, &status);
2448 EXPECT_EQ(0x0, (long)status);
2449 PTHREAD_JOIN(self->sibling[1].tid, &status);
2450 EXPECT_EQ(0x0, (long)status);
2453 TEST_F(TSYNC, two_sibling_want_nnp)
2455 void *status;
2457 /* start siblings before any prctl() operations */
2458 tsync_start_sibling(&self->sibling[0]);
2459 tsync_start_sibling(&self->sibling[1]);
2460 while (self->sibling_count < TSYNC_SIBLINGS) {
2461 sem_wait(&self->started);
2462 self->sibling_count++;
2465 /* Tell the siblings to test no policy */
2466 pthread_mutex_lock(&self->mutex);
2467 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2468 TH_LOG("cond broadcast non-zero");
2470 pthread_mutex_unlock(&self->mutex);
2472 /* Ensure they are both upset about lacking nnp. */
2473 PTHREAD_JOIN(self->sibling[0].tid, &status);
2474 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS, (long)status);
2475 PTHREAD_JOIN(self->sibling[1].tid, &status);
2476 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS, (long)status);
2479 TEST_F(TSYNC, two_siblings_with_no_filter)
2481 long ret;
2482 void *status;
2484 /* start siblings before any prctl() operations */
2485 tsync_start_sibling(&self->sibling[0]);
2486 tsync_start_sibling(&self->sibling[1]);
2487 while (self->sibling_count < TSYNC_SIBLINGS) {
2488 sem_wait(&self->started);
2489 self->sibling_count++;
2492 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2493 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2496 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2497 &self->apply_prog);
2498 ASSERT_NE(ENOSYS, errno) {
2499 TH_LOG("Kernel does not support seccomp syscall!");
2501 ASSERT_EQ(0, ret) {
2502 TH_LOG("Could install filter on all threads!");
2505 /* Tell the siblings to test the policy */
2506 pthread_mutex_lock(&self->mutex);
2507 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2508 TH_LOG("cond broadcast non-zero");
2510 pthread_mutex_unlock(&self->mutex);
2512 /* Ensure they are both killed and don't exit cleanly. */
2513 PTHREAD_JOIN(self->sibling[0].tid, &status);
2514 EXPECT_EQ(0x0, (long)status);
2515 PTHREAD_JOIN(self->sibling[1].tid, &status);
2516 EXPECT_EQ(0x0, (long)status);
2519 TEST_F(TSYNC, two_siblings_with_one_divergence)
2521 long ret;
2522 void *status;
2524 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2525 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2528 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog);
2529 ASSERT_NE(ENOSYS, errno) {
2530 TH_LOG("Kernel does not support seccomp syscall!");
2532 ASSERT_EQ(0, ret) {
2533 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2535 self->sibling[0].diverge = 1;
2536 tsync_start_sibling(&self->sibling[0]);
2537 tsync_start_sibling(&self->sibling[1]);
2539 while (self->sibling_count < TSYNC_SIBLINGS) {
2540 sem_wait(&self->started);
2541 self->sibling_count++;
2544 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2545 &self->apply_prog);
2546 ASSERT_EQ(self->sibling[0].system_tid, ret) {
2547 TH_LOG("Did not fail on diverged sibling.");
2550 /* Wake the threads */
2551 pthread_mutex_lock(&self->mutex);
2552 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2553 TH_LOG("cond broadcast non-zero");
2555 pthread_mutex_unlock(&self->mutex);
2557 /* Ensure they are both unkilled. */
2558 PTHREAD_JOIN(self->sibling[0].tid, &status);
2559 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
2560 PTHREAD_JOIN(self->sibling[1].tid, &status);
2561 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
2564 TEST_F(TSYNC, two_siblings_not_under_filter)
2566 long ret, sib;
2567 void *status;
2569 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2570 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2574 * Sibling 0 will have its own seccomp policy
2575 * and Sibling 1 will not be under seccomp at
2576 * all. Sibling 1 will enter seccomp and 0
2577 * will cause failure.
2579 self->sibling[0].diverge = 1;
2580 tsync_start_sibling(&self->sibling[0]);
2581 tsync_start_sibling(&self->sibling[1]);
2583 while (self->sibling_count < TSYNC_SIBLINGS) {
2584 sem_wait(&self->started);
2585 self->sibling_count++;
2588 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog);
2589 ASSERT_NE(ENOSYS, errno) {
2590 TH_LOG("Kernel does not support seccomp syscall!");
2592 ASSERT_EQ(0, ret) {
2593 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2596 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2597 &self->apply_prog);
2598 ASSERT_EQ(ret, self->sibling[0].system_tid) {
2599 TH_LOG("Did not fail on diverged sibling.");
2601 sib = 1;
2602 if (ret == self->sibling[0].system_tid)
2603 sib = 0;
2605 pthread_mutex_lock(&self->mutex);
2607 /* Increment the other siblings num_waits so we can clean up
2608 * the one we just saw.
2610 self->sibling[!sib].num_waits += 1;
2612 /* Signal the thread to clean up*/
2613 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2614 TH_LOG("cond broadcast non-zero");
2616 pthread_mutex_unlock(&self->mutex);
2617 PTHREAD_JOIN(self->sibling[sib].tid, &status);
2618 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
2619 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2620 while (!kill(self->sibling[sib].system_tid, 0))
2621 sleep(0.1);
2622 /* Switch to the remaining sibling */
2623 sib = !sib;
2625 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2626 &self->apply_prog);
2627 ASSERT_EQ(0, ret) {
2628 TH_LOG("Expected the remaining sibling to sync");
2631 pthread_mutex_lock(&self->mutex);
2633 /* If remaining sibling didn't have a chance to wake up during
2634 * the first broadcast, manually reduce the num_waits now.
2636 if (self->sibling[sib].num_waits > 1)
2637 self->sibling[sib].num_waits = 1;
2638 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2639 TH_LOG("cond broadcast non-zero");
2641 pthread_mutex_unlock(&self->mutex);
2642 PTHREAD_JOIN(self->sibling[sib].tid, &status);
2643 EXPECT_EQ(0, (long)status);
2644 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2645 while (!kill(self->sibling[sib].system_tid, 0))
2646 sleep(0.1);
2648 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2649 &self->apply_prog);
2650 ASSERT_EQ(0, ret); /* just us chickens */
2653 /* Make sure restarted syscalls are seen directly as "restart_syscall". */
2654 TEST(syscall_restart)
2656 long ret;
2657 unsigned long msg;
2658 pid_t child_pid;
2659 int pipefd[2];
2660 int status;
2661 siginfo_t info = { };
2662 struct sock_filter filter[] = {
2663 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
2664 offsetof(struct seccomp_data, nr)),
2666 #ifdef __NR_sigreturn
2667 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_sigreturn, 6, 0),
2668 #endif
2669 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 5, 0),
2670 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_exit, 4, 0),
2671 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_rt_sigreturn, 3, 0),
2672 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_nanosleep, 4, 0),
2673 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_restart_syscall, 4, 0),
2675 /* Allow __NR_write for easy logging. */
2676 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_write, 0, 1),
2677 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2678 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
2679 /* The nanosleep jump target. */
2680 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE|0x100),
2681 /* The restart_syscall jump target. */
2682 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE|0x200),
2684 struct sock_fprog prog = {
2685 .len = (unsigned short)ARRAY_SIZE(filter),
2686 .filter = filter,
2688 #if defined(__arm__)
2689 struct utsname utsbuf;
2690 #endif
2692 ASSERT_EQ(0, pipe(pipefd));
2694 child_pid = fork();
2695 ASSERT_LE(0, child_pid);
2696 if (child_pid == 0) {
2697 /* Child uses EXPECT not ASSERT to deliver status correctly. */
2698 char buf = ' ';
2699 struct timespec timeout = { };
2701 /* Attach parent as tracer and stop. */
2702 EXPECT_EQ(0, ptrace(PTRACE_TRACEME));
2703 EXPECT_EQ(0, raise(SIGSTOP));
2705 EXPECT_EQ(0, close(pipefd[1]));
2707 EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2708 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2711 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
2712 EXPECT_EQ(0, ret) {
2713 TH_LOG("Failed to install filter!");
2716 EXPECT_EQ(1, read(pipefd[0], &buf, 1)) {
2717 TH_LOG("Failed to read() sync from parent");
2719 EXPECT_EQ('.', buf) {
2720 TH_LOG("Failed to get sync data from read()");
2723 /* Start nanosleep to be interrupted. */
2724 timeout.tv_sec = 1;
2725 errno = 0;
2726 EXPECT_EQ(0, nanosleep(&timeout, NULL)) {
2727 TH_LOG("Call to nanosleep() failed (errno %d)", errno);
2730 /* Read final sync from parent. */
2731 EXPECT_EQ(1, read(pipefd[0], &buf, 1)) {
2732 TH_LOG("Failed final read() from parent");
2734 EXPECT_EQ('!', buf) {
2735 TH_LOG("Failed to get final data from read()");
2738 /* Directly report the status of our test harness results. */
2739 syscall(__NR_exit, _metadata->passed ? EXIT_SUCCESS
2740 : EXIT_FAILURE);
2742 EXPECT_EQ(0, close(pipefd[0]));
2744 /* Attach to child, setup options, and release. */
2745 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2746 ASSERT_EQ(true, WIFSTOPPED(status));
2747 ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS, child_pid, NULL,
2748 PTRACE_O_TRACESECCOMP));
2749 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2750 ASSERT_EQ(1, write(pipefd[1], ".", 1));
2752 /* Wait for nanosleep() to start. */
2753 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2754 ASSERT_EQ(true, WIFSTOPPED(status));
2755 ASSERT_EQ(SIGTRAP, WSTOPSIG(status));
2756 ASSERT_EQ(PTRACE_EVENT_SECCOMP, (status >> 16));
2757 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG, child_pid, NULL, &msg));
2758 ASSERT_EQ(0x100, msg);
2759 EXPECT_EQ(__NR_nanosleep, get_syscall(_metadata, child_pid));
2761 /* Might as well check siginfo for sanity while we're here. */
2762 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child_pid, NULL, &info));
2763 ASSERT_EQ(SIGTRAP, info.si_signo);
2764 ASSERT_EQ(SIGTRAP | (PTRACE_EVENT_SECCOMP << 8), info.si_code);
2765 EXPECT_EQ(0, info.si_errno);
2766 EXPECT_EQ(getuid(), info.si_uid);
2767 /* Verify signal delivery came from child (seccomp-triggered). */
2768 EXPECT_EQ(child_pid, info.si_pid);
2770 /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */
2771 ASSERT_EQ(0, kill(child_pid, SIGSTOP));
2772 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2773 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2774 ASSERT_EQ(true, WIFSTOPPED(status));
2775 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
2776 /* Verify signal delivery came from parent now. */
2777 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child_pid, NULL, &info));
2778 EXPECT_EQ(getpid(), info.si_pid);
2780 /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */
2781 ASSERT_EQ(0, kill(child_pid, SIGCONT));
2782 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2783 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2784 ASSERT_EQ(true, WIFSTOPPED(status));
2785 ASSERT_EQ(SIGCONT, WSTOPSIG(status));
2786 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2788 /* Wait for restart_syscall() to start. */
2789 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2790 ASSERT_EQ(true, WIFSTOPPED(status));
2791 ASSERT_EQ(SIGTRAP, WSTOPSIG(status));
2792 ASSERT_EQ(PTRACE_EVENT_SECCOMP, (status >> 16));
2793 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG, child_pid, NULL, &msg));
2795 ASSERT_EQ(0x200, msg);
2796 ret = get_syscall(_metadata, child_pid);
2797 #if defined(__arm__)
2799 * FIXME:
2800 * - native ARM registers do NOT expose true syscall.
2801 * - compat ARM registers on ARM64 DO expose true syscall.
2803 ASSERT_EQ(0, uname(&utsbuf));
2804 if (strncmp(utsbuf.machine, "arm", 3) == 0) {
2805 EXPECT_EQ(__NR_nanosleep, ret);
2806 } else
2807 #endif
2809 EXPECT_EQ(__NR_restart_syscall, ret);
2812 /* Write again to end test. */
2813 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2814 ASSERT_EQ(1, write(pipefd[1], "!", 1));
2815 EXPECT_EQ(0, close(pipefd[1]));
2817 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2818 if (WIFSIGNALED(status) || WEXITSTATUS(status))
2819 _metadata->passed = 0;
2822 TEST_SIGNAL(filter_flag_log, SIGSYS)
2824 struct sock_filter allow_filter[] = {
2825 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2827 struct sock_filter kill_filter[] = {
2828 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
2829 offsetof(struct seccomp_data, nr)),
2830 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1),
2831 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
2832 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2834 struct sock_fprog allow_prog = {
2835 .len = (unsigned short)ARRAY_SIZE(allow_filter),
2836 .filter = allow_filter,
2838 struct sock_fprog kill_prog = {
2839 .len = (unsigned short)ARRAY_SIZE(kill_filter),
2840 .filter = kill_filter,
2842 long ret;
2843 pid_t parent = getppid();
2845 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
2846 ASSERT_EQ(0, ret);
2848 /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */
2849 ret = seccomp(SECCOMP_SET_MODE_STRICT, SECCOMP_FILTER_FLAG_LOG,
2850 &allow_prog);
2851 ASSERT_NE(ENOSYS, errno) {
2852 TH_LOG("Kernel does not support seccomp syscall!");
2854 EXPECT_NE(0, ret) {
2855 TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!");
2857 EXPECT_EQ(EINVAL, errno) {
2858 TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!");
2861 /* Verify that a simple, permissive filter can be added with no flags */
2862 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &allow_prog);
2863 EXPECT_EQ(0, ret);
2865 /* See if the same filter can be added with the FILTER_FLAG_LOG flag */
2866 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_LOG,
2867 &allow_prog);
2868 ASSERT_NE(EINVAL, errno) {
2869 TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!");
2871 EXPECT_EQ(0, ret);
2873 /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */
2874 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_LOG,
2875 &kill_prog);
2876 EXPECT_EQ(0, ret);
2878 EXPECT_EQ(parent, syscall(__NR_getppid));
2879 /* getpid() should never return. */
2880 EXPECT_EQ(0, syscall(__NR_getpid));
2883 TEST(get_action_avail)
2885 __u32 actions[] = { SECCOMP_RET_KILL_THREAD, SECCOMP_RET_TRAP,
2886 SECCOMP_RET_ERRNO, SECCOMP_RET_TRACE,
2887 SECCOMP_RET_LOG, SECCOMP_RET_ALLOW };
2888 __u32 unknown_action = 0x10000000U;
2889 int i;
2890 long ret;
2892 ret = seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &actions[0]);
2893 ASSERT_NE(ENOSYS, errno) {
2894 TH_LOG("Kernel does not support seccomp syscall!");
2896 ASSERT_NE(EINVAL, errno) {
2897 TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!");
2899 EXPECT_EQ(ret, 0);
2901 for (i = 0; i < ARRAY_SIZE(actions); i++) {
2902 ret = seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &actions[i]);
2903 EXPECT_EQ(ret, 0) {
2904 TH_LOG("Expected action (0x%X) not available!",
2905 actions[i]);
2909 /* Check that an unknown action is handled properly (EOPNOTSUPP) */
2910 ret = seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &unknown_action);
2911 EXPECT_EQ(ret, -1);
2912 EXPECT_EQ(errno, EOPNOTSUPP);
2915 TEST(get_metadata)
2917 pid_t pid;
2918 int pipefd[2];
2919 char buf;
2920 struct seccomp_metadata md;
2921 long ret;
2923 /* Only real root can get metadata. */
2924 if (geteuid()) {
2925 XFAIL(return, "get_metadata requires real root");
2926 return;
2929 ASSERT_EQ(0, pipe(pipefd));
2931 pid = fork();
2932 ASSERT_GE(pid, 0);
2933 if (pid == 0) {
2934 struct sock_filter filter[] = {
2935 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2937 struct sock_fprog prog = {
2938 .len = (unsigned short)ARRAY_SIZE(filter),
2939 .filter = filter,
2942 /* one with log, one without */
2943 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER,
2944 SECCOMP_FILTER_FLAG_LOG, &prog));
2945 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog));
2947 ASSERT_EQ(0, close(pipefd[0]));
2948 ASSERT_EQ(1, write(pipefd[1], "1", 1));
2949 ASSERT_EQ(0, close(pipefd[1]));
2951 while (1)
2952 sleep(100);
2955 ASSERT_EQ(0, close(pipefd[1]));
2956 ASSERT_EQ(1, read(pipefd[0], &buf, 1));
2958 ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid));
2959 ASSERT_EQ(pid, waitpid(pid, NULL, 0));
2961 /* Past here must not use ASSERT or child process is never killed. */
2963 md.filter_off = 0;
2964 errno = 0;
2965 ret = ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md);
2966 EXPECT_EQ(sizeof(md), ret) {
2967 if (errno == EINVAL)
2968 XFAIL(goto skip, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)");
2971 EXPECT_EQ(md.flags, SECCOMP_FILTER_FLAG_LOG);
2972 EXPECT_EQ(md.filter_off, 0);
2974 md.filter_off = 1;
2975 ret = ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md);
2976 EXPECT_EQ(sizeof(md), ret);
2977 EXPECT_EQ(md.flags, 0);
2978 EXPECT_EQ(md.filter_off, 1);
2980 skip:
2981 ASSERT_EQ(0, kill(pid, SIGKILL));
2985 * TODO:
2986 * - add microbenchmarks
2987 * - expand NNP testing
2988 * - better arch-specific TRACE and TRAP handlers.
2989 * - endianness checking when appropriate
2990 * - 64-bit arg prodding
2991 * - arch value testing (x86 modes especially)
2992 * - verify that FILTER_FLAG_LOG filters generate log messages
2993 * - verify that RET_LOG generates log messages
2994 * - ...
2997 TEST_HARNESS_MAIN