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.
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
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
23 #include <linux/filter.h>
24 #include <sys/prctl.h>
25 #include <sys/ptrace.h>
27 #include <linux/prctl.h>
28 #include <linux/ptrace.h>
29 #include <linux/seccomp.h>
31 #include <semaphore.h>
37 #include <linux/elf.h>
39 #include <sys/utsname.h>
40 #include <sys/fcntl.h>
42 #include <sys/times.h>
46 #include <sys/syscall.h>
48 #include "../kselftest_harness.h"
50 #ifndef PR_SET_PTRACER
51 # define PR_SET_PTRACER 0x59616d61
54 #ifndef PR_SET_NO_NEW_PRIVS
55 #define PR_SET_NO_NEW_PRIVS 38
56 #define PR_GET_NO_NEW_PRIVS 39
59 #ifndef PR_SECCOMP_EXT
60 #define PR_SECCOMP_EXT 43
63 #ifndef SECCOMP_EXT_ACT
64 #define SECCOMP_EXT_ACT 1
67 #ifndef SECCOMP_EXT_ACT_TSYNC
68 #define SECCOMP_EXT_ACT_TSYNC 1
71 #ifndef SECCOMP_MODE_STRICT
72 #define SECCOMP_MODE_STRICT 1
75 #ifndef SECCOMP_MODE_FILTER
76 #define SECCOMP_MODE_FILTER 2
79 #ifndef SECCOMP_RET_ALLOW
83 __u64 instruction_pointer
;
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 */
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 */
99 #ifndef SECCOMP_RET_LOG
100 #define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */
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
119 # warning "seccomp syscall number unknown for this architecture"
120 # define __NR_seccomp 0xffff
124 #ifndef SECCOMP_SET_MODE_STRICT
125 #define SECCOMP_SET_MODE_STRICT 0
128 #ifndef SECCOMP_SET_MODE_FILTER
129 #define SECCOMP_SET_MODE_FILTER 1
132 #ifndef SECCOMP_GET_ACTION_AVAIL
133 #define SECCOMP_GET_ACTION_AVAIL 2
136 #ifndef SECCOMP_FILTER_FLAG_TSYNC
137 #define SECCOMP_FILTER_FLAG_TSYNC 1
140 #ifndef SECCOMP_FILTER_FLAG_LOG
141 #define SECCOMP_FILTER_FLAG_LOG 2
144 #ifndef PTRACE_SECCOMP_GET_METADATA
145 #define PTRACE_SECCOMP_GET_METADATA 0x420d
147 struct seccomp_metadata
{
148 __u64 filter_off
; /* Input: which filter */
149 __u64 flags
; /* Output: filter's flags */
154 int seccomp(unsigned int op
, unsigned int flags
, void *args
)
157 return syscall(__NR_seccomp
, op
, flags
, args
);
161 #if __BYTE_ORDER == __LITTLE_ENDIAN
162 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
163 #elif __BYTE_ORDER == __BIG_ENDIAN
164 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]) + sizeof(__u32))
166 #error "wut? Unknown __BYTE_ORDER?!"
169 #define SIBLING_EXIT_UNKILLED 0xbadbeef
170 #define SIBLING_EXIT_FAILURE 0xbadface
171 #define SIBLING_EXIT_NEWPRIVS 0xbadfeed
173 TEST(mode_strict_support
)
177 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
179 TH_LOG("Kernel does not support CONFIG_SECCOMP");
181 syscall(__NR_exit
, 0);
184 TEST_SIGNAL(mode_strict_cannot_call_prctl
, SIGKILL
)
188 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
190 TH_LOG("Kernel does not support CONFIG_SECCOMP");
192 syscall(__NR_prctl
, PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
195 TH_LOG("Unreachable!");
199 /* Note! This doesn't test no new privs behavior */
200 TEST(no_new_privs_support
)
204 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
206 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
210 /* Tests kernel support by checking for a copy_from_user() fault on NULL. */
211 TEST(mode_filter_support
)
215 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
217 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
219 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, NULL
, NULL
);
221 EXPECT_EQ(EFAULT
, errno
) {
222 TH_LOG("Kernel does not support CONFIG_SECCOMP_FILTER!");
226 TEST(mode_filter_without_nnp
)
228 struct sock_filter filter
[] = {
229 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
231 struct sock_fprog prog
= {
232 .len
= (unsigned short)ARRAY_SIZE(filter
),
237 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, NULL
, 0, 0);
239 TH_LOG("Expected 0 or unsupported for NO_NEW_PRIVS");
242 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
243 /* Succeeds with CAP_SYS_ADMIN, fails without */
244 /* TODO(wad) check caps not euid */
247 EXPECT_EQ(EACCES
, errno
);
253 #define MAX_INSNS_PER_PATH 32768
255 TEST(filter_size_limits
)
258 int count
= BPF_MAXINSNS
+ 1;
259 struct sock_filter allow
[] = {
260 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
262 struct sock_filter
*filter
;
263 struct sock_fprog prog
= { };
266 filter
= calloc(count
, sizeof(*filter
));
267 ASSERT_NE(NULL
, filter
);
269 for (i
= 0; i
< count
; i
++)
270 filter
[i
] = allow
[0];
272 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
275 prog
.filter
= filter
;
278 /* Too many filter instructions in a single filter. */
279 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
281 TH_LOG("Installing %d insn filter was allowed", prog
.len
);
284 /* One less is okay, though. */
286 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
288 TH_LOG("Installing %d insn filter wasn't allowed", prog
.len
);
292 TEST(filter_chain_limits
)
295 int count
= BPF_MAXINSNS
;
296 struct sock_filter allow
[] = {
297 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
299 struct sock_filter
*filter
;
300 struct sock_fprog prog
= { };
303 filter
= calloc(count
, sizeof(*filter
));
304 ASSERT_NE(NULL
, filter
);
306 for (i
= 0; i
< count
; i
++)
307 filter
[i
] = allow
[0];
309 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
312 prog
.filter
= filter
;
315 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
320 /* Too many total filter instructions. */
321 for (i
= 0; i
< MAX_INSNS_PER_PATH
; i
++) {
322 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
327 TH_LOG("Allowed %d %d-insn filters (total with penalties:%d)",
328 i
, count
, i
* (count
+ 4));
332 TEST(mode_filter_cannot_move_to_strict
)
334 struct sock_filter filter
[] = {
335 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
337 struct sock_fprog prog
= {
338 .len
= (unsigned short)ARRAY_SIZE(filter
),
343 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
346 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
349 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, 0, 0);
351 EXPECT_EQ(EINVAL
, errno
);
355 TEST(mode_filter_get_seccomp
)
357 struct sock_filter filter
[] = {
358 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
360 struct sock_fprog prog
= {
361 .len
= (unsigned short)ARRAY_SIZE(filter
),
366 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
369 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
372 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
375 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
382 struct sock_filter filter
[] = {
383 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
385 struct sock_fprog prog
= {
386 .len
= (unsigned short)ARRAY_SIZE(filter
),
391 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
394 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
400 struct sock_filter filter
[] = {
402 struct sock_fprog prog
= {
403 .len
= (unsigned short)ARRAY_SIZE(filter
),
408 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
411 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
413 EXPECT_EQ(EINVAL
, errno
);
418 struct sock_filter filter
[] = {
419 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_LOG
),
421 struct sock_fprog prog
= {
422 .len
= (unsigned short)ARRAY_SIZE(filter
),
426 pid_t parent
= getppid();
428 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
431 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
434 /* getppid() should succeed and be logged (no check for logging) */
435 EXPECT_EQ(parent
, syscall(__NR_getppid
));
438 TEST_SIGNAL(unknown_ret_is_kill_inside
, SIGSYS
)
440 struct sock_filter filter
[] = {
441 BPF_STMT(BPF_RET
|BPF_K
, 0x10000000U
),
443 struct sock_fprog prog
= {
444 .len
= (unsigned short)ARRAY_SIZE(filter
),
449 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
452 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
454 EXPECT_EQ(0, syscall(__NR_getpid
)) {
455 TH_LOG("getpid() shouldn't ever return");
459 /* return code >= 0x80000000 is unused. */
460 TEST_SIGNAL(unknown_ret_is_kill_above_allow
, SIGSYS
)
462 struct sock_filter filter
[] = {
463 BPF_STMT(BPF_RET
|BPF_K
, 0x90000000U
),
465 struct sock_fprog prog
= {
466 .len
= (unsigned short)ARRAY_SIZE(filter
),
471 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
474 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
476 EXPECT_EQ(0, syscall(__NR_getpid
)) {
477 TH_LOG("getpid() shouldn't ever return");
481 TEST_SIGNAL(KILL_all
, SIGSYS
)
483 struct sock_filter filter
[] = {
484 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
486 struct sock_fprog prog
= {
487 .len
= (unsigned short)ARRAY_SIZE(filter
),
492 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
495 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
499 TEST_SIGNAL(KILL_one
, SIGSYS
)
501 struct sock_filter filter
[] = {
502 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
503 offsetof(struct seccomp_data
, nr
)),
504 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
505 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
506 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
508 struct sock_fprog prog
= {
509 .len
= (unsigned short)ARRAY_SIZE(filter
),
513 pid_t parent
= getppid();
515 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
518 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
521 EXPECT_EQ(parent
, syscall(__NR_getppid
));
522 /* getpid() should never return. */
523 EXPECT_EQ(0, syscall(__NR_getpid
));
526 TEST_SIGNAL(KILL_one_arg_one
, SIGSYS
)
529 struct sock_filter filter
[] = {
530 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
531 offsetof(struct seccomp_data
, nr
)),
532 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_times
, 1, 0),
533 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
534 /* Only both with lower 32-bit for now. */
535 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(0)),
536 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
,
537 (unsigned long)&fatal_address
, 0, 1),
538 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
539 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
541 struct sock_fprog prog
= {
542 .len
= (unsigned short)ARRAY_SIZE(filter
),
546 pid_t parent
= getppid();
548 clock_t clock
= times(&timebuf
);
550 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
553 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
556 EXPECT_EQ(parent
, syscall(__NR_getppid
));
557 EXPECT_LE(clock
, syscall(__NR_times
, &timebuf
));
558 /* times() should never return. */
559 EXPECT_EQ(0, syscall(__NR_times
, &fatal_address
));
562 TEST_SIGNAL(KILL_one_arg_six
, SIGSYS
)
565 int sysno
= __NR_mmap
;
567 int sysno
= __NR_mmap2
;
569 struct sock_filter filter
[] = {
570 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
571 offsetof(struct seccomp_data
, nr
)),
572 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, sysno
, 1, 0),
573 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
574 /* Only both with lower 32-bit for now. */
575 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(5)),
576 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, 0x0C0FFEE, 0, 1),
577 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
578 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
580 struct sock_fprog prog
= {
581 .len
= (unsigned short)ARRAY_SIZE(filter
),
585 pid_t parent
= getppid();
588 int page_size
= sysconf(_SC_PAGESIZE
);
590 ASSERT_LT(0, page_size
);
592 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
595 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
598 fd
= open("/dev/zero", O_RDONLY
);
601 EXPECT_EQ(parent
, syscall(__NR_getppid
));
602 map1
= (void *)syscall(sysno
,
603 NULL
, page_size
, PROT_READ
, MAP_PRIVATE
, fd
, page_size
);
604 EXPECT_NE(MAP_FAILED
, map1
);
605 /* mmap2() should never return. */
606 map2
= (void *)syscall(sysno
,
607 NULL
, page_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0x0C0FFEE);
608 EXPECT_EQ(MAP_FAILED
, map2
);
610 /* The test failed, so clean up the resources. */
611 munmap(map1
, page_size
);
612 munmap(map2
, page_size
);
616 /* This is a thread task to die via seccomp filter violation. */
617 void *kill_thread(void *data
)
619 bool die
= (bool)data
;
622 prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
623 return (void *)SIBLING_EXIT_FAILURE
;
626 return (void *)SIBLING_EXIT_UNKILLED
;
629 /* Prepare a thread that will kill itself or both of us. */
630 void kill_thread_or_group(struct __test_metadata
*_metadata
, bool kill_process
)
634 /* Kill only when calling __NR_prctl. */
635 struct sock_filter filter_thread
[] = {
636 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
637 offsetof(struct seccomp_data
, nr
)),
638 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
639 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL_THREAD
),
640 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
642 struct sock_fprog prog_thread
= {
643 .len
= (unsigned short)ARRAY_SIZE(filter_thread
),
644 .filter
= filter_thread
,
646 struct sock_filter filter_process
[] = {
647 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
648 offsetof(struct seccomp_data
, nr
)),
649 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
650 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL_PROCESS
),
651 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
653 struct sock_fprog prog_process
= {
654 .len
= (unsigned short)ARRAY_SIZE(filter_process
),
655 .filter
= filter_process
,
658 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
659 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
662 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0,
663 kill_process
? &prog_process
: &prog_thread
));
666 * Add the KILL_THREAD rule again to make sure that the KILL_PROCESS
667 * flag cannot be downgraded by a new filter.
669 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog_thread
));
671 /* Start a thread that will exit immediately. */
672 ASSERT_EQ(0, pthread_create(&thread
, NULL
, kill_thread
, (void *)false));
673 ASSERT_EQ(0, pthread_join(thread
, &status
));
674 ASSERT_EQ(SIBLING_EXIT_UNKILLED
, (unsigned long)status
);
676 /* Start a thread that will die immediately. */
677 ASSERT_EQ(0, pthread_create(&thread
, NULL
, kill_thread
, (void *)true));
678 ASSERT_EQ(0, pthread_join(thread
, &status
));
679 ASSERT_NE(SIBLING_EXIT_FAILURE
, (unsigned long)status
);
682 * If we get here, only the spawned thread died. Let the parent know
683 * the whole process didn't die (i.e. this thread, the spawner,
695 ASSERT_LE(0, child_pid
);
696 if (child_pid
== 0) {
697 kill_thread_or_group(_metadata
, false);
701 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
703 /* If only the thread was killed, we'll see exit 42. */
704 ASSERT_TRUE(WIFEXITED(status
));
705 ASSERT_EQ(42, WEXITSTATUS(status
));
714 ASSERT_LE(0, child_pid
);
715 if (child_pid
== 0) {
716 kill_thread_or_group(_metadata
, true);
720 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
722 /* If the entire process was killed, we'll see SIGSYS. */
723 ASSERT_TRUE(WIFSIGNALED(status
));
724 ASSERT_EQ(SIGSYS
, WTERMSIG(status
));
727 /* TODO(wad) add 64-bit versus 32-bit arg tests. */
728 TEST(arg_out_of_range
)
730 struct sock_filter filter
[] = {
731 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(6)),
732 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
734 struct sock_fprog prog
= {
735 .len
= (unsigned short)ARRAY_SIZE(filter
),
740 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
743 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
745 EXPECT_EQ(EINVAL
, errno
);
748 #define ERRNO_FILTER(name, errno) \
749 struct sock_filter _read_filter_##name[] = { \
750 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, \
751 offsetof(struct seccomp_data, nr)), \
752 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), \
753 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | errno), \
754 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), \
756 struct sock_fprog prog_##name = { \
757 .len = (unsigned short)ARRAY_SIZE(_read_filter_##name), \
758 .filter = _read_filter_##name, \
761 /* Make sure basic errno values are correctly passed through a filter. */
764 ERRNO_FILTER(valid
, E2BIG
);
766 pid_t parent
= getppid();
768 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
771 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_valid
);
774 EXPECT_EQ(parent
, syscall(__NR_getppid
));
775 EXPECT_EQ(-1, read(0, NULL
, 0));
776 EXPECT_EQ(E2BIG
, errno
);
779 /* Make sure an errno of zero is correctly handled by the arch code. */
782 ERRNO_FILTER(zero
, 0);
784 pid_t parent
= getppid();
786 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
789 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_zero
);
792 EXPECT_EQ(parent
, syscall(__NR_getppid
));
793 /* "errno" of 0 is ok. */
794 EXPECT_EQ(0, read(0, NULL
, 0));
798 * The SECCOMP_RET_DATA mask is 16 bits wide, but errno is smaller.
799 * This tests that the errno value gets capped correctly, fixed by
800 * 580c57f10768 ("seccomp: cap SECCOMP_RET_ERRNO data to MAX_ERRNO").
804 ERRNO_FILTER(capped
, 4096);
806 pid_t parent
= getppid();
808 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
811 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_capped
);
814 EXPECT_EQ(parent
, syscall(__NR_getppid
));
815 EXPECT_EQ(-1, read(0, NULL
, 0));
816 EXPECT_EQ(4095, errno
);
820 * Filters are processed in reverse order: last applied is executed first.
821 * Since only the SECCOMP_RET_ACTION mask is tested for return values, the
822 * SECCOMP_RET_DATA mask results will follow the most recently applied
823 * matching filter return (and not the lowest or highest value).
827 ERRNO_FILTER(first
, 11);
828 ERRNO_FILTER(second
, 13);
829 ERRNO_FILTER(third
, 12);
831 pid_t parent
= getppid();
833 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
836 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_first
);
839 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_second
);
842 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_third
);
845 EXPECT_EQ(parent
, syscall(__NR_getppid
));
846 EXPECT_EQ(-1, read(0, NULL
, 0));
847 EXPECT_EQ(12, errno
);
851 struct sock_fprog prog
;
856 struct sock_filter filter
[] = {
857 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
858 offsetof(struct seccomp_data
, nr
)),
859 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
860 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRAP
),
861 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
864 memset(&self
->prog
, 0, sizeof(self
->prog
));
865 self
->prog
.filter
= malloc(sizeof(filter
));
866 ASSERT_NE(NULL
, self
->prog
.filter
);
867 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
868 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
871 FIXTURE_TEARDOWN(TRAP
)
873 if (self
->prog
.filter
)
874 free(self
->prog
.filter
);
877 TEST_F_SIGNAL(TRAP
, dfl
, SIGSYS
)
881 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
884 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
886 syscall(__NR_getpid
);
889 /* Ensure that SIGSYS overrides SIG_IGN */
890 TEST_F_SIGNAL(TRAP
, ign
, SIGSYS
)
894 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
897 signal(SIGSYS
, SIG_IGN
);
899 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
901 syscall(__NR_getpid
);
904 static siginfo_t TRAP_info
;
905 static volatile int TRAP_nr
;
906 static void TRAP_action(int nr
, siginfo_t
*info
, void *void_context
)
908 memcpy(&TRAP_info
, info
, sizeof(TRAP_info
));
912 TEST_F(TRAP
, handler
)
915 struct sigaction act
;
918 memset(&act
, 0, sizeof(act
));
920 sigaddset(&mask
, SIGSYS
);
922 act
.sa_sigaction
= &TRAP_action
;
923 act
.sa_flags
= SA_SIGINFO
;
924 ret
= sigaction(SIGSYS
, &act
, NULL
);
926 TH_LOG("sigaction failed");
928 ret
= sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
930 TH_LOG("sigprocmask failed");
933 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
935 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
938 memset(&TRAP_info
, 0, sizeof(TRAP_info
));
939 /* Expect the registers to be rolled back. (nr = error) may vary
941 ret
= syscall(__NR_getpid
);
942 /* Silence gcc warning about volatile. */
944 EXPECT_EQ(SIGSYS
, test
);
945 struct local_sigsys
{
946 void *_call_addr
; /* calling user insn */
947 int _syscall
; /* triggering system call number */
948 unsigned int _arch
; /* AUDIT_ARCH_* of syscall */
949 } *sigsys
= (struct local_sigsys
*)
951 &(TRAP_info
.si_call_addr
);
955 EXPECT_EQ(__NR_getpid
, sigsys
->_syscall
);
956 /* Make sure arch is non-zero. */
957 EXPECT_NE(0, sigsys
->_arch
);
958 EXPECT_NE(0, (unsigned long)sigsys
->_call_addr
);
961 FIXTURE_DATA(precedence
) {
962 struct sock_fprog allow
;
963 struct sock_fprog log
;
964 struct sock_fprog trace
;
965 struct sock_fprog error
;
966 struct sock_fprog trap
;
967 struct sock_fprog kill
;
970 FIXTURE_SETUP(precedence
)
972 struct sock_filter allow_insns
[] = {
973 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
975 struct sock_filter log_insns
[] = {
976 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
977 offsetof(struct seccomp_data
, nr
)),
978 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
979 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
980 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_LOG
),
982 struct sock_filter trace_insns
[] = {
983 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
984 offsetof(struct seccomp_data
, nr
)),
985 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
986 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
987 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
),
989 struct sock_filter error_insns
[] = {
990 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
991 offsetof(struct seccomp_data
, nr
)),
992 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
993 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
994 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
),
996 struct sock_filter trap_insns
[] = {
997 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
998 offsetof(struct seccomp_data
, nr
)),
999 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1000 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1001 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRAP
),
1003 struct sock_filter kill_insns
[] = {
1004 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1005 offsetof(struct seccomp_data
, nr
)),
1006 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1007 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1008 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1011 memset(self
, 0, sizeof(*self
));
1012 #define FILTER_ALLOC(_x) \
1013 self->_x.filter = malloc(sizeof(_x##_insns)); \
1014 ASSERT_NE(NULL, self->_x.filter); \
1015 memcpy(self->_x.filter, &_x##_insns, sizeof(_x##_insns)); \
1016 self->_x.len = (unsigned short)ARRAY_SIZE(_x##_insns)
1017 FILTER_ALLOC(allow
);
1019 FILTER_ALLOC(trace
);
1020 FILTER_ALLOC(error
);
1025 FIXTURE_TEARDOWN(precedence
)
1027 #define FILTER_FREE(_x) if (self->_x.filter) free(self->_x.filter)
1036 TEST_F(precedence
, allow_ok
)
1038 pid_t parent
, res
= 0;
1042 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1045 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1047 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1049 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1051 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1053 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1055 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1057 /* Should work just fine. */
1058 res
= syscall(__NR_getppid
);
1059 EXPECT_EQ(parent
, res
);
1062 TEST_F_SIGNAL(precedence
, kill_is_highest
, SIGSYS
)
1064 pid_t parent
, res
= 0;
1068 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1071 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1073 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1075 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1077 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1079 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1081 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1083 /* Should work just fine. */
1084 res
= syscall(__NR_getppid
);
1085 EXPECT_EQ(parent
, res
);
1086 /* getpid() should never return. */
1087 res
= syscall(__NR_getpid
);
1091 TEST_F_SIGNAL(precedence
, kill_is_highest_in_any_order
, SIGSYS
)
1097 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1100 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1102 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1104 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1106 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1108 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1110 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1112 /* Should work just fine. */
1113 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1114 /* getpid() should never return. */
1115 EXPECT_EQ(0, syscall(__NR_getpid
));
1118 TEST_F_SIGNAL(precedence
, trap_is_second
, SIGSYS
)
1124 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1127 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1129 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1131 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1133 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1135 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1137 /* Should work just fine. */
1138 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1139 /* getpid() should never return. */
1140 EXPECT_EQ(0, syscall(__NR_getpid
));
1143 TEST_F_SIGNAL(precedence
, trap_is_second_in_any_order
, SIGSYS
)
1149 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1152 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1154 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1156 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1158 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1160 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1162 /* Should work just fine. */
1163 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1164 /* getpid() should never return. */
1165 EXPECT_EQ(0, syscall(__NR_getpid
));
1168 TEST_F(precedence
, errno_is_third
)
1174 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1177 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1179 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1181 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1183 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1185 /* Should work just fine. */
1186 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1187 EXPECT_EQ(0, syscall(__NR_getpid
));
1190 TEST_F(precedence
, errno_is_third_in_any_order
)
1196 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1199 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1201 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1203 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1205 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1207 /* Should work just fine. */
1208 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1209 EXPECT_EQ(0, syscall(__NR_getpid
));
1212 TEST_F(precedence
, trace_is_fourth
)
1218 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1221 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1223 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1225 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1227 /* Should work just fine. */
1228 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1230 EXPECT_EQ(-1, syscall(__NR_getpid
));
1233 TEST_F(precedence
, trace_is_fourth_in_any_order
)
1239 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1242 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1244 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1246 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1248 /* Should work just fine. */
1249 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1251 EXPECT_EQ(-1, syscall(__NR_getpid
));
1254 TEST_F(precedence
, log_is_fifth
)
1256 pid_t mypid
, parent
;
1261 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1264 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1266 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1268 /* Should work just fine. */
1269 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1270 /* Should also work just fine */
1271 EXPECT_EQ(mypid
, syscall(__NR_getpid
));
1274 TEST_F(precedence
, log_is_fifth_in_any_order
)
1276 pid_t mypid
, parent
;
1281 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1284 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1286 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1288 /* Should work just fine. */
1289 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1290 /* Should also work just fine */
1291 EXPECT_EQ(mypid
, syscall(__NR_getpid
));
1294 #ifndef PTRACE_O_TRACESECCOMP
1295 #define PTRACE_O_TRACESECCOMP 0x00000080
1298 /* Catch the Ubuntu 12.04 value error. */
1299 #if PTRACE_EVENT_SECCOMP != 7
1300 #undef PTRACE_EVENT_SECCOMP
1303 #ifndef PTRACE_EVENT_SECCOMP
1304 #define PTRACE_EVENT_SECCOMP 7
1307 #define IS_SECCOMP_EVENT(status) ((status >> 16) == PTRACE_EVENT_SECCOMP)
1308 bool tracer_running
;
1309 void tracer_stop(int sig
)
1311 tracer_running
= false;
1314 typedef void tracer_func_t(struct __test_metadata
*_metadata
,
1315 pid_t tracee
, int status
, void *args
);
1317 void start_tracer(struct __test_metadata
*_metadata
, int fd
, pid_t tracee
,
1318 tracer_func_t tracer_func
, void *args
, bool ptrace_syscall
)
1321 struct sigaction action
= {
1322 .sa_handler
= tracer_stop
,
1325 /* Allow external shutdown. */
1326 tracer_running
= true;
1327 ASSERT_EQ(0, sigaction(SIGUSR1
, &action
, NULL
));
1330 while (ret
== -1 && errno
!= EINVAL
)
1331 ret
= ptrace(PTRACE_ATTACH
, tracee
, NULL
, 0);
1333 kill(tracee
, SIGKILL
);
1335 /* Wait for attach stop */
1338 ret
= ptrace(PTRACE_SETOPTIONS
, tracee
, NULL
, ptrace_syscall
?
1339 PTRACE_O_TRACESYSGOOD
:
1340 PTRACE_O_TRACESECCOMP
);
1342 TH_LOG("Failed to set PTRACE_O_TRACESECCOMP");
1343 kill(tracee
, SIGKILL
);
1345 ret
= ptrace(ptrace_syscall
? PTRACE_SYSCALL
: PTRACE_CONT
,
1349 /* Unblock the tracee */
1350 ASSERT_EQ(1, write(fd
, "A", 1));
1351 ASSERT_EQ(0, close(fd
));
1353 /* Run until we're shut down. Must assert to stop execution. */
1354 while (tracer_running
) {
1357 if (wait(&status
) != tracee
)
1359 if (WIFSIGNALED(status
) || WIFEXITED(status
))
1360 /* Child is dead. Time to go. */
1363 /* Check if this is a seccomp event. */
1364 ASSERT_EQ(!ptrace_syscall
, IS_SECCOMP_EVENT(status
));
1366 tracer_func(_metadata
, tracee
, status
, args
);
1368 ret
= ptrace(ptrace_syscall
? PTRACE_SYSCALL
: PTRACE_CONT
,
1372 /* Directly report the status of our test harness results. */
1373 syscall(__NR_exit
, _metadata
->passed
? EXIT_SUCCESS
: EXIT_FAILURE
);
1376 /* Common tracer setup/teardown functions. */
1377 void cont_handler(int num
)
1379 pid_t
setup_trace_fixture(struct __test_metadata
*_metadata
,
1380 tracer_func_t func
, void *args
, bool ptrace_syscall
)
1385 pid_t tracee
= getpid();
1387 /* Setup a pipe for clean synchronization. */
1388 ASSERT_EQ(0, pipe(pipefd
));
1390 /* Fork a child which we'll promote to tracer */
1391 tracer_pid
= fork();
1392 ASSERT_LE(0, tracer_pid
);
1393 signal(SIGALRM
, cont_handler
);
1394 if (tracer_pid
== 0) {
1396 start_tracer(_metadata
, pipefd
[1], tracee
, func
, args
,
1398 syscall(__NR_exit
, 0);
1401 prctl(PR_SET_PTRACER
, tracer_pid
, 0, 0, 0);
1402 read(pipefd
[0], &sync
, 1);
1407 void teardown_trace_fixture(struct __test_metadata
*_metadata
,
1413 * Extract the exit code from the other process and
1414 * adopt it for ourselves in case its asserts failed.
1416 ASSERT_EQ(0, kill(tracer
, SIGUSR1
));
1417 ASSERT_EQ(tracer
, waitpid(tracer
, &status
, 0));
1418 if (WEXITSTATUS(status
))
1419 _metadata
->passed
= 0;
1423 /* "poke" tracer arguments and function. */
1424 struct tracer_args_poke_t
{
1425 unsigned long poke_addr
;
1428 void tracer_poke(struct __test_metadata
*_metadata
, pid_t tracee
, int status
,
1433 struct tracer_args_poke_t
*info
= (struct tracer_args_poke_t
*)args
;
1435 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1437 /* If this fails, don't try to recover. */
1438 ASSERT_EQ(0x1001, msg
) {
1439 kill(tracee
, SIGKILL
);
1442 * Poke in the message.
1443 * Registers are not touched to try to keep this relatively arch
1446 ret
= ptrace(PTRACE_POKEDATA
, tracee
, info
->poke_addr
, 0x1001);
1450 FIXTURE_DATA(TRACE_poke
) {
1451 struct sock_fprog prog
;
1454 struct tracer_args_poke_t tracer_args
;
1457 FIXTURE_SETUP(TRACE_poke
)
1459 struct sock_filter filter
[] = {
1460 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1461 offsetof(struct seccomp_data
, nr
)),
1462 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
1463 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1001),
1464 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1468 memset(&self
->prog
, 0, sizeof(self
->prog
));
1469 self
->prog
.filter
= malloc(sizeof(filter
));
1470 ASSERT_NE(NULL
, self
->prog
.filter
);
1471 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1472 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1474 /* Set up tracer args. */
1475 self
->tracer_args
.poke_addr
= (unsigned long)&self
->poked
;
1477 /* Launch tracer. */
1478 self
->tracer
= setup_trace_fixture(_metadata
, tracer_poke
,
1479 &self
->tracer_args
, false);
1482 FIXTURE_TEARDOWN(TRACE_poke
)
1484 teardown_trace_fixture(_metadata
, self
->tracer
);
1485 if (self
->prog
.filter
)
1486 free(self
->prog
.filter
);
1489 TEST_F(TRACE_poke
, read_has_side_effects
)
1493 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1496 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1499 EXPECT_EQ(0, self
->poked
);
1500 ret
= read(-1, NULL
, 0);
1502 EXPECT_EQ(0x1001, self
->poked
);
1505 TEST_F(TRACE_poke
, getpid_runs_normally
)
1509 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1512 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1515 EXPECT_EQ(0, self
->poked
);
1516 EXPECT_NE(0, syscall(__NR_getpid
));
1517 EXPECT_EQ(0, self
->poked
);
1520 #if defined(__x86_64__)
1521 # define ARCH_REGS struct user_regs_struct
1522 # define SYSCALL_NUM orig_rax
1523 # define SYSCALL_RET rax
1524 #elif defined(__i386__)
1525 # define ARCH_REGS struct user_regs_struct
1526 # define SYSCALL_NUM orig_eax
1527 # define SYSCALL_RET eax
1528 #elif defined(__arm__)
1529 # define ARCH_REGS struct pt_regs
1530 # define SYSCALL_NUM ARM_r7
1531 # define SYSCALL_RET ARM_r0
1532 #elif defined(__aarch64__)
1533 # define ARCH_REGS struct user_pt_regs
1534 # define SYSCALL_NUM regs[8]
1535 # define SYSCALL_RET regs[0]
1536 #elif defined(__hppa__)
1537 # define ARCH_REGS struct user_regs_struct
1538 # define SYSCALL_NUM gr[20]
1539 # define SYSCALL_RET gr[28]
1540 #elif defined(__powerpc__)
1541 # define ARCH_REGS struct pt_regs
1542 # define SYSCALL_NUM gpr[0]
1543 # define SYSCALL_RET gpr[3]
1544 #elif defined(__s390__)
1545 # define ARCH_REGS s390_regs
1546 # define SYSCALL_NUM gprs[2]
1547 # define SYSCALL_RET gprs[2]
1548 #elif defined(__mips__)
1549 # define ARCH_REGS struct pt_regs
1550 # define SYSCALL_NUM regs[2]
1551 # define SYSCALL_SYSCALL_NUM regs[4]
1552 # define SYSCALL_RET regs[2]
1553 # define SYSCALL_NUM_RET_SHARE_REG
1555 # error "Do not know how to find your architecture's registers and syscalls"
1558 /* When the syscall return can't be changed, stub out the tests for it. */
1559 #ifdef SYSCALL_NUM_RET_SHARE_REG
1560 # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(-1, action)
1562 # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(val, action)
1565 /* Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for
1566 * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux).
1568 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__)
1569 #define HAVE_GETREGS
1572 /* Architecture-specific syscall fetching routine. */
1573 int get_syscall(struct __test_metadata
*_metadata
, pid_t tracee
)
1577 EXPECT_EQ(0, ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
)) {
1578 TH_LOG("PTRACE_GETREGS failed");
1584 iov
.iov_base
= ®s
;
1585 iov
.iov_len
= sizeof(regs
);
1586 EXPECT_EQ(0, ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
)) {
1587 TH_LOG("PTRACE_GETREGSET failed");
1592 #if defined(__mips__)
1593 if (regs
.SYSCALL_NUM
== __NR_O32_Linux
)
1594 return regs
.SYSCALL_SYSCALL_NUM
;
1596 return regs
.SYSCALL_NUM
;
1599 /* Architecture-specific syscall changing routine. */
1600 void change_syscall(struct __test_metadata
*_metadata
,
1601 pid_t tracee
, int syscall
)
1606 ret
= ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
);
1609 iov
.iov_base
= ®s
;
1610 iov
.iov_len
= sizeof(regs
);
1611 ret
= ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1613 EXPECT_EQ(0, ret
) {}
1615 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \
1616 defined(__s390__) || defined(__hppa__)
1618 regs
.SYSCALL_NUM
= syscall
;
1620 #elif defined(__mips__)
1622 if (regs
.SYSCALL_NUM
== __NR_O32_Linux
)
1623 regs
.SYSCALL_SYSCALL_NUM
= syscall
;
1625 regs
.SYSCALL_NUM
= syscall
;
1628 #elif defined(__arm__)
1629 # ifndef PTRACE_SET_SYSCALL
1630 # define PTRACE_SET_SYSCALL 23
1633 ret
= ptrace(PTRACE_SET_SYSCALL
, tracee
, NULL
, syscall
);
1637 #elif defined(__aarch64__)
1638 # ifndef NT_ARM_SYSTEM_CALL
1639 # define NT_ARM_SYSTEM_CALL 0x404
1642 iov
.iov_base
= &syscall
;
1643 iov
.iov_len
= sizeof(syscall
);
1644 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_ARM_SYSTEM_CALL
,
1651 TH_LOG("How is the syscall changed on this architecture?");
1655 /* If syscall is skipped, change return value. */
1657 #ifdef SYSCALL_NUM_RET_SHARE_REG
1658 TH_LOG("Can't modify syscall return on this architecture");
1660 regs
.SYSCALL_RET
= EPERM
;
1664 ret
= ptrace(PTRACE_SETREGS
, tracee
, 0, ®s
);
1666 iov
.iov_base
= ®s
;
1667 iov
.iov_len
= sizeof(regs
);
1668 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1673 void tracer_syscall(struct __test_metadata
*_metadata
, pid_t tracee
,
1674 int status
, void *args
)
1679 /* Make sure we got the right message. */
1680 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1683 /* Validate and take action on expected syscalls. */
1686 /* change getpid to getppid. */
1687 EXPECT_EQ(__NR_getpid
, get_syscall(_metadata
, tracee
));
1688 change_syscall(_metadata
, tracee
, __NR_getppid
);
1692 EXPECT_EQ(__NR_gettid
, get_syscall(_metadata
, tracee
));
1693 change_syscall(_metadata
, tracee
, -1);
1696 /* do nothing (allow getppid) */
1697 EXPECT_EQ(__NR_getppid
, get_syscall(_metadata
, tracee
));
1701 TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg
);
1702 kill(tracee
, SIGKILL
);
1708 void tracer_ptrace(struct __test_metadata
*_metadata
, pid_t tracee
,
1709 int status
, void *args
)
1715 /* Make sure we got an empty message. */
1716 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1720 /* The only way to tell PTRACE_SYSCALL entry/exit is by counting. */
1725 nr
= get_syscall(_metadata
, tracee
);
1727 if (nr
== __NR_getpid
)
1728 change_syscall(_metadata
, tracee
, __NR_getppid
);
1729 if (nr
== __NR_openat
)
1730 change_syscall(_metadata
, tracee
, -1);
1733 FIXTURE_DATA(TRACE_syscall
) {
1734 struct sock_fprog prog
;
1735 pid_t tracer
, mytid
, mypid
, parent
;
1738 FIXTURE_SETUP(TRACE_syscall
)
1740 struct sock_filter filter
[] = {
1741 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1742 offsetof(struct seccomp_data
, nr
)),
1743 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
1744 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1002),
1745 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_gettid
, 0, 1),
1746 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1003),
1747 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1748 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1004),
1749 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1752 memset(&self
->prog
, 0, sizeof(self
->prog
));
1753 self
->prog
.filter
= malloc(sizeof(filter
));
1754 ASSERT_NE(NULL
, self
->prog
.filter
);
1755 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1756 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1758 /* Prepare some testable syscall results. */
1759 self
->mytid
= syscall(__NR_gettid
);
1760 ASSERT_GT(self
->mytid
, 0);
1761 ASSERT_NE(self
->mytid
, 1) {
1762 TH_LOG("Running this test as init is not supported. :)");
1765 self
->mypid
= getpid();
1766 ASSERT_GT(self
->mypid
, 0);
1767 ASSERT_EQ(self
->mytid
, self
->mypid
);
1769 self
->parent
= getppid();
1770 ASSERT_GT(self
->parent
, 0);
1771 ASSERT_NE(self
->parent
, self
->mypid
);
1773 /* Launch tracer. */
1774 self
->tracer
= setup_trace_fixture(_metadata
, tracer_syscall
, NULL
,
1778 FIXTURE_TEARDOWN(TRACE_syscall
)
1780 teardown_trace_fixture(_metadata
, self
->tracer
);
1781 if (self
->prog
.filter
)
1782 free(self
->prog
.filter
);
1785 TEST_F(TRACE_syscall
, ptrace_syscall_redirected
)
1787 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1788 teardown_trace_fixture(_metadata
, self
->tracer
);
1789 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1792 /* Tracer will redirect getpid to getppid. */
1793 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1796 TEST_F(TRACE_syscall
, ptrace_syscall_dropped
)
1798 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1799 teardown_trace_fixture(_metadata
, self
->tracer
);
1800 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1803 /* Tracer should skip the open syscall, resulting in EPERM. */
1804 EXPECT_SYSCALL_RETURN(EPERM
, syscall(__NR_openat
));
1807 TEST_F(TRACE_syscall
, syscall_allowed
)
1811 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1814 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1817 /* getppid works as expected (no changes). */
1818 EXPECT_EQ(self
->parent
, syscall(__NR_getppid
));
1819 EXPECT_NE(self
->mypid
, syscall(__NR_getppid
));
1822 TEST_F(TRACE_syscall
, syscall_redirected
)
1826 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1829 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1832 /* getpid has been redirected to getppid as expected. */
1833 EXPECT_EQ(self
->parent
, syscall(__NR_getpid
));
1834 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1837 TEST_F(TRACE_syscall
, syscall_dropped
)
1841 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1844 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1847 /* gettid has been skipped and an altered return value stored. */
1848 EXPECT_SYSCALL_RETURN(EPERM
, syscall(__NR_gettid
));
1849 EXPECT_NE(self
->mytid
, syscall(__NR_gettid
));
1852 TEST_F(TRACE_syscall
, skip_after_RET_TRACE
)
1854 struct sock_filter filter
[] = {
1855 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1856 offsetof(struct seccomp_data
, nr
)),
1857 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1858 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
1859 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1861 struct sock_fprog prog
= {
1862 .len
= (unsigned short)ARRAY_SIZE(filter
),
1867 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1870 /* Install fixture filter. */
1871 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1874 /* Install "errno on getppid" filter. */
1875 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1878 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
1880 EXPECT_EQ(-1, syscall(__NR_getpid
));
1881 EXPECT_EQ(EPERM
, errno
);
1884 TEST_F_SIGNAL(TRACE_syscall
, kill_after_RET_TRACE
, SIGSYS
)
1886 struct sock_filter filter
[] = {
1887 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1888 offsetof(struct seccomp_data
, nr
)),
1889 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1890 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1891 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1893 struct sock_fprog prog
= {
1894 .len
= (unsigned short)ARRAY_SIZE(filter
),
1899 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1902 /* Install fixture filter. */
1903 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1906 /* Install "death on getppid" filter. */
1907 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1910 /* Tracer will redirect getpid to getppid, and we should die. */
1911 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1914 TEST_F(TRACE_syscall
, skip_after_ptrace
)
1916 struct sock_filter filter
[] = {
1917 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1918 offsetof(struct seccomp_data
, nr
)),
1919 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1920 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
1921 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1923 struct sock_fprog prog
= {
1924 .len
= (unsigned short)ARRAY_SIZE(filter
),
1929 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1930 teardown_trace_fixture(_metadata
, self
->tracer
);
1931 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1934 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1937 /* Install "errno on getppid" filter. */
1938 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1941 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
1942 EXPECT_EQ(-1, syscall(__NR_getpid
));
1943 EXPECT_EQ(EPERM
, errno
);
1946 TEST_F_SIGNAL(TRACE_syscall
, kill_after_ptrace
, SIGSYS
)
1948 struct sock_filter filter
[] = {
1949 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1950 offsetof(struct seccomp_data
, nr
)),
1951 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1952 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1953 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1955 struct sock_fprog prog
= {
1956 .len
= (unsigned short)ARRAY_SIZE(filter
),
1961 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1962 teardown_trace_fixture(_metadata
, self
->tracer
);
1963 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1966 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1969 /* Install "death on getppid" filter. */
1970 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1973 /* Tracer will redirect getpid to getppid, and we should die. */
1974 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1977 TEST(seccomp_syscall
)
1979 struct sock_filter filter
[] = {
1980 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1982 struct sock_fprog prog
= {
1983 .len
= (unsigned short)ARRAY_SIZE(filter
),
1988 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1990 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
1993 /* Reject insane operation. */
1994 ret
= seccomp(-1, 0, &prog
);
1995 ASSERT_NE(ENOSYS
, errno
) {
1996 TH_LOG("Kernel does not support seccomp syscall!");
1998 EXPECT_EQ(EINVAL
, errno
) {
1999 TH_LOG("Did not reject crazy op value!");
2002 /* Reject strict with flags or pointer. */
2003 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, -1, NULL
);
2004 EXPECT_EQ(EINVAL
, errno
) {
2005 TH_LOG("Did not reject mode strict with flags!");
2007 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, &prog
);
2008 EXPECT_EQ(EINVAL
, errno
) {
2009 TH_LOG("Did not reject mode strict with uargs!");
2012 /* Reject insane args for filter. */
2013 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, -1, &prog
);
2014 EXPECT_EQ(EINVAL
, errno
) {
2015 TH_LOG("Did not reject crazy filter flags!");
2017 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, NULL
);
2018 EXPECT_EQ(EFAULT
, errno
) {
2019 TH_LOG("Did not reject NULL filter!");
2022 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2023 EXPECT_EQ(0, errno
) {
2024 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s",
2029 TEST(seccomp_syscall_mode_lock
)
2031 struct sock_filter filter
[] = {
2032 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2034 struct sock_fprog prog
= {
2035 .len
= (unsigned short)ARRAY_SIZE(filter
),
2040 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2042 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2045 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2046 ASSERT_NE(ENOSYS
, errno
) {
2047 TH_LOG("Kernel does not support seccomp syscall!");
2050 TH_LOG("Could not install filter!");
2053 /* Make sure neither entry point will switch to strict. */
2054 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, 0, 0, 0);
2055 EXPECT_EQ(EINVAL
, errno
) {
2056 TH_LOG("Switched to mode strict!");
2059 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, NULL
);
2060 EXPECT_EQ(EINVAL
, errno
) {
2061 TH_LOG("Switched to mode strict!");
2066 * Test detection of known and unknown filter flags. Userspace needs to be able
2067 * to check if a filter flag is supported by the current kernel and a good way
2068 * of doing that is by attempting to enter filter mode, with the flag bit in
2069 * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates
2070 * that the flag is valid and EINVAL indicates that the flag is invalid.
2072 TEST(detect_seccomp_filter_flags
)
2074 unsigned int flags
[] = { SECCOMP_FILTER_FLAG_TSYNC
,
2075 SECCOMP_FILTER_FLAG_LOG
};
2076 unsigned int flag
, all_flags
;
2080 /* Test detection of known-good filter flags */
2081 for (i
= 0, all_flags
= 0; i
< ARRAY_SIZE(flags
); i
++) {
2083 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2084 ASSERT_NE(ENOSYS
, errno
) {
2085 TH_LOG("Kernel does not support seccomp syscall!");
2088 EXPECT_EQ(EFAULT
, errno
) {
2089 TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!",
2096 /* Test detection of all known-good filter flags */
2097 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, all_flags
, NULL
);
2099 EXPECT_EQ(EFAULT
, errno
) {
2100 TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2104 /* Test detection of an unknown filter flag */
2106 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2108 EXPECT_EQ(EINVAL
, errno
) {
2109 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!",
2114 * Test detection of an unknown filter flag that may simply need to be
2115 * added to this test
2117 flag
= flags
[ARRAY_SIZE(flags
) - 1] << 1;
2118 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2120 EXPECT_EQ(EINVAL
, errno
) {
2121 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?",
2128 struct sock_filter filter
[] = {
2129 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2131 struct sock_fprog prog
= {
2132 .len
= (unsigned short)ARRAY_SIZE(filter
),
2137 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2139 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2142 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2144 ASSERT_NE(ENOSYS
, errno
) {
2145 TH_LOG("Kernel does not support seccomp syscall!");
2148 TH_LOG("Could not install initial filter with TSYNC!");
2152 #define TSYNC_SIBLINGS 2
2153 struct tsync_sibling
{
2157 pthread_cond_t
*cond
;
2158 pthread_mutex_t
*mutex
;
2161 struct sock_fprog
*prog
;
2162 struct __test_metadata
*metadata
;
2166 * To avoid joining joined threads (which is not allowed by Bionic),
2167 * make sure we both successfully join and clear the tid to skip a
2168 * later join attempt during fixture teardown. Any remaining threads
2169 * will be directly killed during teardown.
2171 #define PTHREAD_JOIN(tid, status) \
2173 int _rc = pthread_join(tid, status); \
2175 TH_LOG("pthread_join of tid %u failed: %d\n", \
2176 (unsigned int)tid, _rc); \
2182 FIXTURE_DATA(TSYNC
) {
2183 struct sock_fprog root_prog
, apply_prog
;
2184 struct tsync_sibling sibling
[TSYNC_SIBLINGS
];
2186 pthread_cond_t cond
;
2187 pthread_mutex_t mutex
;
2191 FIXTURE_SETUP(TSYNC
)
2193 struct sock_filter root_filter
[] = {
2194 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2196 struct sock_filter apply_filter
[] = {
2197 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2198 offsetof(struct seccomp_data
, nr
)),
2199 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
2200 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2201 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2204 memset(&self
->root_prog
, 0, sizeof(self
->root_prog
));
2205 memset(&self
->apply_prog
, 0, sizeof(self
->apply_prog
));
2206 memset(&self
->sibling
, 0, sizeof(self
->sibling
));
2207 self
->root_prog
.filter
= malloc(sizeof(root_filter
));
2208 ASSERT_NE(NULL
, self
->root_prog
.filter
);
2209 memcpy(self
->root_prog
.filter
, &root_filter
, sizeof(root_filter
));
2210 self
->root_prog
.len
= (unsigned short)ARRAY_SIZE(root_filter
);
2212 self
->apply_prog
.filter
= malloc(sizeof(apply_filter
));
2213 ASSERT_NE(NULL
, self
->apply_prog
.filter
);
2214 memcpy(self
->apply_prog
.filter
, &apply_filter
, sizeof(apply_filter
));
2215 self
->apply_prog
.len
= (unsigned short)ARRAY_SIZE(apply_filter
);
2217 self
->sibling_count
= 0;
2218 pthread_mutex_init(&self
->mutex
, NULL
);
2219 pthread_cond_init(&self
->cond
, NULL
);
2220 sem_init(&self
->started
, 0, 0);
2221 self
->sibling
[0].tid
= 0;
2222 self
->sibling
[0].cond
= &self
->cond
;
2223 self
->sibling
[0].started
= &self
->started
;
2224 self
->sibling
[0].mutex
= &self
->mutex
;
2225 self
->sibling
[0].diverge
= 0;
2226 self
->sibling
[0].num_waits
= 1;
2227 self
->sibling
[0].prog
= &self
->root_prog
;
2228 self
->sibling
[0].metadata
= _metadata
;
2229 self
->sibling
[1].tid
= 0;
2230 self
->sibling
[1].cond
= &self
->cond
;
2231 self
->sibling
[1].started
= &self
->started
;
2232 self
->sibling
[1].mutex
= &self
->mutex
;
2233 self
->sibling
[1].diverge
= 0;
2234 self
->sibling
[1].prog
= &self
->root_prog
;
2235 self
->sibling
[1].num_waits
= 1;
2236 self
->sibling
[1].metadata
= _metadata
;
2239 FIXTURE_TEARDOWN(TSYNC
)
2243 if (self
->root_prog
.filter
)
2244 free(self
->root_prog
.filter
);
2245 if (self
->apply_prog
.filter
)
2246 free(self
->apply_prog
.filter
);
2248 for ( ; sib
< self
->sibling_count
; ++sib
) {
2249 struct tsync_sibling
*s
= &self
->sibling
[sib
];
2254 * If a thread is still running, it may be stuck, so hit
2255 * it over the head really hard.
2257 pthread_kill(s
->tid
, 9);
2259 pthread_mutex_destroy(&self
->mutex
);
2260 pthread_cond_destroy(&self
->cond
);
2261 sem_destroy(&self
->started
);
2264 void *tsync_sibling(void *data
)
2267 struct tsync_sibling
*me
= data
;
2269 me
->system_tid
= syscall(__NR_gettid
);
2271 pthread_mutex_lock(me
->mutex
);
2273 /* Just re-apply the root prog to fork the tree */
2274 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
2277 sem_post(me
->started
);
2278 /* Return outside of started so parent notices failures. */
2280 pthread_mutex_unlock(me
->mutex
);
2281 return (void *)SIBLING_EXIT_FAILURE
;
2284 pthread_cond_wait(me
->cond
, me
->mutex
);
2285 me
->num_waits
= me
->num_waits
- 1;
2286 } while (me
->num_waits
);
2287 pthread_mutex_unlock(me
->mutex
);
2289 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, 0, 0, 0);
2291 return (void *)SIBLING_EXIT_NEWPRIVS
;
2293 return (void *)SIBLING_EXIT_UNKILLED
;
2296 void tsync_start_sibling(struct tsync_sibling
*sibling
)
2298 pthread_create(&sibling
->tid
, NULL
, tsync_sibling
, (void *)sibling
);
2301 TEST_F(TSYNC
, siblings_fail_prctl
)
2305 struct sock_filter filter
[] = {
2306 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2307 offsetof(struct seccomp_data
, nr
)),
2308 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
2309 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EINVAL
),
2310 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2312 struct sock_fprog prog
= {
2313 .len
= (unsigned short)ARRAY_SIZE(filter
),
2317 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2318 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2321 /* Check prctl failure detection by requesting sib 0 diverge. */
2322 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2323 ASSERT_NE(ENOSYS
, errno
) {
2324 TH_LOG("Kernel does not support seccomp syscall!");
2327 TH_LOG("setting filter failed");
2330 self
->sibling
[0].diverge
= 1;
2331 tsync_start_sibling(&self
->sibling
[0]);
2332 tsync_start_sibling(&self
->sibling
[1]);
2334 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2335 sem_wait(&self
->started
);
2336 self
->sibling_count
++;
2339 /* Signal the threads to clean up*/
2340 pthread_mutex_lock(&self
->mutex
);
2341 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2342 TH_LOG("cond broadcast non-zero");
2344 pthread_mutex_unlock(&self
->mutex
);
2346 /* Ensure diverging sibling failed to call prctl. */
2347 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2348 EXPECT_EQ(SIBLING_EXIT_FAILURE
, (long)status
);
2349 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2350 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2353 TEST_F(TSYNC
, two_siblings_with_ancestor
)
2358 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2359 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2362 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2363 ASSERT_NE(ENOSYS
, errno
) {
2364 TH_LOG("Kernel does not support seccomp syscall!");
2367 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2369 tsync_start_sibling(&self
->sibling
[0]);
2370 tsync_start_sibling(&self
->sibling
[1]);
2372 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2373 sem_wait(&self
->started
);
2374 self
->sibling_count
++;
2377 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2380 TH_LOG("Could install filter on all threads!");
2382 /* Tell the siblings to test the policy */
2383 pthread_mutex_lock(&self
->mutex
);
2384 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2385 TH_LOG("cond broadcast non-zero");
2387 pthread_mutex_unlock(&self
->mutex
);
2388 /* Ensure they are both killed and don't exit cleanly. */
2389 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2390 EXPECT_EQ(0x0, (long)status
);
2391 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2392 EXPECT_EQ(0x0, (long)status
);
2395 TEST_F(TSYNC
, two_sibling_want_nnp
)
2399 /* start siblings before any prctl() operations */
2400 tsync_start_sibling(&self
->sibling
[0]);
2401 tsync_start_sibling(&self
->sibling
[1]);
2402 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2403 sem_wait(&self
->started
);
2404 self
->sibling_count
++;
2407 /* Tell the siblings to test no policy */
2408 pthread_mutex_lock(&self
->mutex
);
2409 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2410 TH_LOG("cond broadcast non-zero");
2412 pthread_mutex_unlock(&self
->mutex
);
2414 /* Ensure they are both upset about lacking nnp. */
2415 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2416 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2417 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2418 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2421 TEST_F(TSYNC
, two_siblings_with_no_filter
)
2426 /* start siblings before any prctl() operations */
2427 tsync_start_sibling(&self
->sibling
[0]);
2428 tsync_start_sibling(&self
->sibling
[1]);
2429 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2430 sem_wait(&self
->started
);
2431 self
->sibling_count
++;
2434 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2435 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2438 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2440 ASSERT_NE(ENOSYS
, errno
) {
2441 TH_LOG("Kernel does not support seccomp syscall!");
2444 TH_LOG("Could install filter on all threads!");
2447 /* Tell the siblings to test the policy */
2448 pthread_mutex_lock(&self
->mutex
);
2449 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2450 TH_LOG("cond broadcast non-zero");
2452 pthread_mutex_unlock(&self
->mutex
);
2454 /* Ensure they are both killed and don't exit cleanly. */
2455 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2456 EXPECT_EQ(0x0, (long)status
);
2457 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2458 EXPECT_EQ(0x0, (long)status
);
2461 TEST_F(TSYNC
, two_siblings_with_one_divergence
)
2466 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2467 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2470 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2471 ASSERT_NE(ENOSYS
, errno
) {
2472 TH_LOG("Kernel does not support seccomp syscall!");
2475 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2477 self
->sibling
[0].diverge
= 1;
2478 tsync_start_sibling(&self
->sibling
[0]);
2479 tsync_start_sibling(&self
->sibling
[1]);
2481 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2482 sem_wait(&self
->started
);
2483 self
->sibling_count
++;
2486 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2488 ASSERT_EQ(self
->sibling
[0].system_tid
, ret
) {
2489 TH_LOG("Did not fail on diverged sibling.");
2492 /* Wake the threads */
2493 pthread_mutex_lock(&self
->mutex
);
2494 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2495 TH_LOG("cond broadcast non-zero");
2497 pthread_mutex_unlock(&self
->mutex
);
2499 /* Ensure they are both unkilled. */
2500 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2501 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2502 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2503 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2506 TEST_F(TSYNC
, two_siblings_not_under_filter
)
2511 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2512 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2516 * Sibling 0 will have its own seccomp policy
2517 * and Sibling 1 will not be under seccomp at
2518 * all. Sibling 1 will enter seccomp and 0
2519 * will cause failure.
2521 self
->sibling
[0].diverge
= 1;
2522 tsync_start_sibling(&self
->sibling
[0]);
2523 tsync_start_sibling(&self
->sibling
[1]);
2525 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2526 sem_wait(&self
->started
);
2527 self
->sibling_count
++;
2530 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2531 ASSERT_NE(ENOSYS
, errno
) {
2532 TH_LOG("Kernel does not support seccomp syscall!");
2535 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2538 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2540 ASSERT_EQ(ret
, self
->sibling
[0].system_tid
) {
2541 TH_LOG("Did not fail on diverged sibling.");
2544 if (ret
== self
->sibling
[0].system_tid
)
2547 pthread_mutex_lock(&self
->mutex
);
2549 /* Increment the other siblings num_waits so we can clean up
2550 * the one we just saw.
2552 self
->sibling
[!sib
].num_waits
+= 1;
2554 /* Signal the thread to clean up*/
2555 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2556 TH_LOG("cond broadcast non-zero");
2558 pthread_mutex_unlock(&self
->mutex
);
2559 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2560 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2561 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2562 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2564 /* Switch to the remaining sibling */
2567 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2570 TH_LOG("Expected the remaining sibling to sync");
2573 pthread_mutex_lock(&self
->mutex
);
2575 /* If remaining sibling didn't have a chance to wake up during
2576 * the first broadcast, manually reduce the num_waits now.
2578 if (self
->sibling
[sib
].num_waits
> 1)
2579 self
->sibling
[sib
].num_waits
= 1;
2580 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2581 TH_LOG("cond broadcast non-zero");
2583 pthread_mutex_unlock(&self
->mutex
);
2584 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2585 EXPECT_EQ(0, (long)status
);
2586 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2587 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2590 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2592 ASSERT_EQ(0, ret
); /* just us chickens */
2595 /* Make sure restarted syscalls are seen directly as "restart_syscall". */
2596 TEST(syscall_restart
)
2603 siginfo_t info
= { };
2604 struct sock_filter filter
[] = {
2605 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2606 offsetof(struct seccomp_data
, nr
)),
2608 #ifdef __NR_sigreturn
2609 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_sigreturn
, 6, 0),
2611 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 5, 0),
2612 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_exit
, 4, 0),
2613 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_rt_sigreturn
, 3, 0),
2614 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_nanosleep
, 4, 0),
2615 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_restart_syscall
, 4, 0),
2617 /* Allow __NR_write for easy logging. */
2618 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_write
, 0, 1),
2619 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2620 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2621 /* The nanosleep jump target. */
2622 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x100),
2623 /* The restart_syscall jump target. */
2624 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x200),
2626 struct sock_fprog prog
= {
2627 .len
= (unsigned short)ARRAY_SIZE(filter
),
2630 #if defined(__arm__)
2631 struct utsname utsbuf
;
2634 ASSERT_EQ(0, pipe(pipefd
));
2637 ASSERT_LE(0, child_pid
);
2638 if (child_pid
== 0) {
2639 /* Child uses EXPECT not ASSERT to deliver status correctly. */
2641 struct timespec timeout
= { };
2643 /* Attach parent as tracer and stop. */
2644 EXPECT_EQ(0, ptrace(PTRACE_TRACEME
));
2645 EXPECT_EQ(0, raise(SIGSTOP
));
2647 EXPECT_EQ(0, close(pipefd
[1]));
2649 EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2650 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2653 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2655 TH_LOG("Failed to install filter!");
2658 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2659 TH_LOG("Failed to read() sync from parent");
2661 EXPECT_EQ('.', buf
) {
2662 TH_LOG("Failed to get sync data from read()");
2665 /* Start nanosleep to be interrupted. */
2668 EXPECT_EQ(0, nanosleep(&timeout
, NULL
)) {
2669 TH_LOG("Call to nanosleep() failed (errno %d)", errno
);
2672 /* Read final sync from parent. */
2673 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2674 TH_LOG("Failed final read() from parent");
2676 EXPECT_EQ('!', buf
) {
2677 TH_LOG("Failed to get final data from read()");
2680 /* Directly report the status of our test harness results. */
2681 syscall(__NR_exit
, _metadata
->passed
? EXIT_SUCCESS
2684 EXPECT_EQ(0, close(pipefd
[0]));
2686 /* Attach to child, setup options, and release. */
2687 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2688 ASSERT_EQ(true, WIFSTOPPED(status
));
2689 ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS
, child_pid
, NULL
,
2690 PTRACE_O_TRACESECCOMP
));
2691 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2692 ASSERT_EQ(1, write(pipefd
[1], ".", 1));
2694 /* Wait for nanosleep() to start. */
2695 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2696 ASSERT_EQ(true, WIFSTOPPED(status
));
2697 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2698 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2699 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2700 ASSERT_EQ(0x100, msg
);
2701 EXPECT_EQ(__NR_nanosleep
, get_syscall(_metadata
, child_pid
));
2703 /* Might as well check siginfo for sanity while we're here. */
2704 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2705 ASSERT_EQ(SIGTRAP
, info
.si_signo
);
2706 ASSERT_EQ(SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8), info
.si_code
);
2707 EXPECT_EQ(0, info
.si_errno
);
2708 EXPECT_EQ(getuid(), info
.si_uid
);
2709 /* Verify signal delivery came from child (seccomp-triggered). */
2710 EXPECT_EQ(child_pid
, info
.si_pid
);
2712 /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */
2713 ASSERT_EQ(0, kill(child_pid
, SIGSTOP
));
2714 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2715 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2716 ASSERT_EQ(true, WIFSTOPPED(status
));
2717 ASSERT_EQ(SIGSTOP
, WSTOPSIG(status
));
2718 /* Verify signal delivery came from parent now. */
2719 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2720 EXPECT_EQ(getpid(), info
.si_pid
);
2722 /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */
2723 ASSERT_EQ(0, kill(child_pid
, SIGCONT
));
2724 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2725 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2726 ASSERT_EQ(true, WIFSTOPPED(status
));
2727 ASSERT_EQ(SIGCONT
, WSTOPSIG(status
));
2728 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2730 /* Wait for restart_syscall() to start. */
2731 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2732 ASSERT_EQ(true, WIFSTOPPED(status
));
2733 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2734 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2735 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2737 ASSERT_EQ(0x200, msg
);
2738 ret
= get_syscall(_metadata
, child_pid
);
2739 #if defined(__arm__)
2742 * - native ARM registers do NOT expose true syscall.
2743 * - compat ARM registers on ARM64 DO expose true syscall.
2745 ASSERT_EQ(0, uname(&utsbuf
));
2746 if (strncmp(utsbuf
.machine
, "arm", 3) == 0) {
2747 EXPECT_EQ(__NR_nanosleep
, ret
);
2751 EXPECT_EQ(__NR_restart_syscall
, ret
);
2754 /* Write again to end test. */
2755 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2756 ASSERT_EQ(1, write(pipefd
[1], "!", 1));
2757 EXPECT_EQ(0, close(pipefd
[1]));
2759 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2760 if (WIFSIGNALED(status
) || WEXITSTATUS(status
))
2761 _metadata
->passed
= 0;
2764 TEST_SIGNAL(filter_flag_log
, SIGSYS
)
2766 struct sock_filter allow_filter
[] = {
2767 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2769 struct sock_filter kill_filter
[] = {
2770 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2771 offsetof(struct seccomp_data
, nr
)),
2772 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
2773 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2774 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2776 struct sock_fprog allow_prog
= {
2777 .len
= (unsigned short)ARRAY_SIZE(allow_filter
),
2778 .filter
= allow_filter
,
2780 struct sock_fprog kill_prog
= {
2781 .len
= (unsigned short)ARRAY_SIZE(kill_filter
),
2782 .filter
= kill_filter
,
2785 pid_t parent
= getppid();
2787 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2790 /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */
2791 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, SECCOMP_FILTER_FLAG_LOG
,
2793 ASSERT_NE(ENOSYS
, errno
) {
2794 TH_LOG("Kernel does not support seccomp syscall!");
2797 TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!");
2799 EXPECT_EQ(EINVAL
, errno
) {
2800 TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!");
2803 /* Verify that a simple, permissive filter can be added with no flags */
2804 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &allow_prog
);
2807 /* See if the same filter can be added with the FILTER_FLAG_LOG flag */
2808 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
2810 ASSERT_NE(EINVAL
, errno
) {
2811 TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!");
2815 /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */
2816 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
2820 EXPECT_EQ(parent
, syscall(__NR_getppid
));
2821 /* getpid() should never return. */
2822 EXPECT_EQ(0, syscall(__NR_getpid
));
2825 TEST(get_action_avail
)
2827 __u32 actions
[] = { SECCOMP_RET_KILL_THREAD
, SECCOMP_RET_TRAP
,
2828 SECCOMP_RET_ERRNO
, SECCOMP_RET_TRACE
,
2829 SECCOMP_RET_LOG
, SECCOMP_RET_ALLOW
};
2830 __u32 unknown_action
= 0x10000000U
;
2834 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[0]);
2835 ASSERT_NE(ENOSYS
, errno
) {
2836 TH_LOG("Kernel does not support seccomp syscall!");
2838 ASSERT_NE(EINVAL
, errno
) {
2839 TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!");
2843 for (i
= 0; i
< ARRAY_SIZE(actions
); i
++) {
2844 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[i
]);
2846 TH_LOG("Expected action (0x%X) not available!",
2851 /* Check that an unknown action is handled properly (EOPNOTSUPP) */
2852 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &unknown_action
);
2854 EXPECT_EQ(errno
, EOPNOTSUPP
);
2862 struct seccomp_metadata md
;
2864 ASSERT_EQ(0, pipe(pipefd
));
2869 struct sock_filter filter
[] = {
2870 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2872 struct sock_fprog prog
= {
2873 .len
= (unsigned short)ARRAY_SIZE(filter
),
2877 /* one with log, one without */
2878 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
,
2879 SECCOMP_FILTER_FLAG_LOG
, &prog
));
2880 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
));
2882 ASSERT_EQ(0, close(pipefd
[0]));
2883 ASSERT_EQ(1, write(pipefd
[1], "1", 1));
2884 ASSERT_EQ(0, close(pipefd
[1]));
2890 ASSERT_EQ(0, close(pipefd
[1]));
2891 ASSERT_EQ(1, read(pipefd
[0], &buf
, 1));
2893 ASSERT_EQ(0, ptrace(PTRACE_ATTACH
, pid
));
2894 ASSERT_EQ(pid
, waitpid(pid
, NULL
, 0));
2897 ASSERT_EQ(sizeof(md
), ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
));
2898 EXPECT_EQ(md
.flags
, SECCOMP_FILTER_FLAG_LOG
);
2899 EXPECT_EQ(md
.filter_off
, 0);
2902 ASSERT_EQ(sizeof(md
), ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
));
2903 EXPECT_EQ(md
.flags
, 0);
2904 EXPECT_EQ(md
.filter_off
, 1);
2906 ASSERT_EQ(0, kill(pid
, SIGKILL
));
2911 * - add microbenchmarks
2912 * - expand NNP testing
2913 * - better arch-specific TRACE and TRAP handlers.
2914 * - endianness checking when appropriate
2915 * - 64-bit arg prodding
2916 * - arch value testing (x86 modes especially)
2917 * - verify that FILTER_FLAG_LOG filters generate log messages
2918 * - verify that RET_LOG generates log messages