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 (1UL << 0)
140 #ifndef SECCOMP_FILTER_FLAG_LOG
141 #define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
144 #ifndef SECCOMP_FILTER_FLAG_SPEC_ALLOW
145 #define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
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 */
158 int seccomp(unsigned int op
, unsigned int flags
, void *args
)
161 return syscall(__NR_seccomp
, op
, flags
, args
);
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))
170 #error "wut? Unknown __BYTE_ORDER?!"
173 #define SIBLING_EXIT_UNKILLED 0xbadbeef
174 #define SIBLING_EXIT_FAILURE 0xbadface
175 #define SIBLING_EXIT_NEWPRIVS 0xbadfeed
177 TEST(mode_strict_support
)
181 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
183 TH_LOG("Kernel does not support CONFIG_SECCOMP");
185 syscall(__NR_exit
, 0);
188 TEST_SIGNAL(mode_strict_cannot_call_prctl
, SIGKILL
)
192 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
194 TH_LOG("Kernel does not support CONFIG_SECCOMP");
196 syscall(__NR_prctl
, PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
199 TH_LOG("Unreachable!");
203 /* Note! This doesn't test no new privs behavior */
204 TEST(no_new_privs_support
)
208 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
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
)
219 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
221 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
223 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, NULL
, NULL
);
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
),
241 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, NULL
, 0, 0);
243 TH_LOG("Expected 0 or unsupported for NO_NEW_PRIVS");
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 */
251 EXPECT_EQ(EACCES
, errno
);
257 #define MAX_INSNS_PER_PATH 32768
259 TEST(filter_size_limits
)
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
= { };
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);
279 prog
.filter
= filter
;
282 /* Too many filter instructions in a single filter. */
283 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
285 TH_LOG("Installing %d insn filter was allowed", prog
.len
);
288 /* One less is okay, though. */
290 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
292 TH_LOG("Installing %d insn filter wasn't allowed", prog
.len
);
296 TEST(filter_chain_limits
)
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
= { };
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);
316 prog
.filter
= filter
;
319 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
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);
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
),
347 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
350 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
353 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, 0, 0);
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
),
370 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
373 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
376 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
379 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
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
),
395 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
398 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
404 struct sock_filter filter
[] = {
406 struct sock_fprog prog
= {
407 .len
= (unsigned short)ARRAY_SIZE(filter
),
412 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
415 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
417 EXPECT_EQ(EINVAL
, errno
);
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
),
430 pid_t parent
= getppid();
432 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
435 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
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
),
453 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
456 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
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
),
475 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
478 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
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
),
496 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
499 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
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
),
517 pid_t parent
= getppid();
519 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
522 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
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
)
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
),
550 pid_t parent
= getppid();
552 clock_t clock
= times(&timebuf
);
554 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
557 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
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
)
569 int sysno
= __NR_mmap
;
571 int sysno
= __NR_mmap2
;
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
),
589 pid_t parent
= getppid();
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);
599 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
602 fd
= open("/dev/zero", O_RDONLY
);
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
);
620 /* This is a thread task to die via seccomp filter violation. */
621 void *kill_thread(void *data
)
623 bool die
= (bool)data
;
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
)
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,
699 ASSERT_LE(0, child_pid
);
700 if (child_pid
== 0) {
701 kill_thread_or_group(_metadata
, false);
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
));
718 ASSERT_LE(0, child_pid
);
719 if (child_pid
== 0) {
720 kill_thread_or_group(_metadata
, true);
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
),
744 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
747 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
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), \
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. */
768 ERRNO_FILTER(valid
, E2BIG
);
770 pid_t parent
= getppid();
772 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
775 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_valid
);
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. */
786 ERRNO_FILTER(zero
, 0);
788 pid_t parent
= getppid();
790 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
793 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_zero
);
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").
808 ERRNO_FILTER(capped
, 4096);
810 pid_t parent
= getppid();
812 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
815 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_capped
);
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).
831 ERRNO_FILTER(first
, 11);
832 ERRNO_FILTER(second
, 13);
833 ERRNO_FILTER(third
, 12);
835 pid_t parent
= getppid();
837 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
840 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_first
);
843 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_second
);
846 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_third
);
849 EXPECT_EQ(parent
, syscall(__NR_getppid
));
850 EXPECT_EQ(-1, read(0, NULL
, 0));
851 EXPECT_EQ(12, errno
);
855 struct sock_fprog prog
;
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
)
885 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
888 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
890 syscall(__NR_getpid
);
893 /* Ensure that SIGSYS overrides SIG_IGN */
894 TEST_F_SIGNAL(TRAP
, ign
, SIGSYS
)
898 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
901 signal(SIGSYS
, SIG_IGN
);
903 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
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
));
916 TEST_F(TRAP
, handler
)
919 struct sigaction act
;
922 memset(&act
, 0, sizeof(act
));
924 sigaddset(&mask
, SIGSYS
);
926 act
.sa_sigaction
= &TRAP_action
;
927 act
.sa_flags
= SA_SIGINFO
;
928 ret
= sigaction(SIGSYS
, &act
, NULL
);
930 TH_LOG("sigaction failed");
932 ret
= sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
934 TH_LOG("sigprocmask failed");
937 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
939 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
942 memset(&TRAP_info
, 0, sizeof(TRAP_info
));
943 /* Expect the registers to be rolled back. (nr = error) may vary
945 ret
= syscall(__NR_getpid
);
946 /* Silence gcc warning about volatile. */
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
*)
955 &(TRAP_info
.si_call_addr
);
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
);
1023 FILTER_ALLOC(trace
);
1024 FILTER_ALLOC(error
);
1029 FIXTURE_TEARDOWN(precedence
)
1031 #define FILTER_FREE(_x) if (self->_x.filter) free(self->_x.filter)
1040 TEST_F(precedence
, allow_ok
)
1042 pid_t parent
, res
= 0;
1046 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1049 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1051 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1053 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1055 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1057 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1059 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
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;
1072 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1075 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1077 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1079 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1081 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1083 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1085 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
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
);
1095 TEST_F_SIGNAL(precedence
, kill_is_highest_in_any_order
, SIGSYS
)
1101 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1104 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1106 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1108 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1110 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1112 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1114 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
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
)
1128 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1131 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1133 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1135 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1137 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1139 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
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
)
1153 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1156 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1158 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1160 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1162 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1164 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
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
)
1178 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1181 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1183 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1185 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1187 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
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
)
1200 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1203 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1205 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1207 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1209 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
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
)
1222 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1225 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1227 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1229 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1231 /* Should work just fine. */
1232 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1234 EXPECT_EQ(-1, syscall(__NR_getpid
));
1237 TEST_F(precedence
, trace_is_fourth_in_any_order
)
1243 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1246 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1248 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1250 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1252 /* Should work just fine. */
1253 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1255 EXPECT_EQ(-1, syscall(__NR_getpid
));
1258 TEST_F(precedence
, log_is_fifth
)
1260 pid_t mypid
, parent
;
1265 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1268 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1270 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
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
;
1285 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1288 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1290 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
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
1302 /* Catch the Ubuntu 12.04 value error. */
1303 #if PTRACE_EVENT_SECCOMP != 7
1304 #undef PTRACE_EVENT_SECCOMP
1307 #ifndef PTRACE_EVENT_SECCOMP
1308 #define PTRACE_EVENT_SECCOMP 7
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
)
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
));
1334 while (ret
== -1 && errno
!= EINVAL
)
1335 ret
= ptrace(PTRACE_ATTACH
, tracee
, NULL
, 0);
1337 kill(tracee
, SIGKILL
);
1339 /* Wait for attach stop */
1342 ret
= ptrace(PTRACE_SETOPTIONS
, tracee
, NULL
, ptrace_syscall
?
1343 PTRACE_O_TRACESYSGOOD
:
1344 PTRACE_O_TRACESECCOMP
);
1346 TH_LOG("Failed to set PTRACE_O_TRACESECCOMP");
1347 kill(tracee
, SIGKILL
);
1349 ret
= ptrace(ptrace_syscall
? PTRACE_SYSCALL
: PTRACE_CONT
,
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
) {
1361 if (wait(&status
) != tracee
)
1363 if (WIFSIGNALED(status
) || WIFEXITED(status
))
1364 /* Child is dead. Time to go. */
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
,
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
)
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) {
1400 start_tracer(_metadata
, pipefd
[1], tracee
, func
, args
,
1402 syscall(__NR_exit
, 0);
1405 prctl(PR_SET_PTRACER
, tracer_pid
, 0, 0, 0);
1406 read(pipefd
[0], &sync
, 1);
1411 void teardown_trace_fixture(struct __test_metadata
*_metadata
,
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
,
1437 struct tracer_args_poke_t
*info
= (struct tracer_args_poke_t
*)args
;
1439 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
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
1450 ret
= ptrace(PTRACE_POKEDATA
, tracee
, info
->poke_addr
, 0x1001);
1454 FIXTURE_DATA(TRACE_poke
) {
1455 struct sock_fprog prog
;
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
),
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
)
1497 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1500 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1503 EXPECT_EQ(0, self
->poked
);
1504 ret
= read(-1, NULL
, 0);
1506 EXPECT_EQ(0x1001, self
->poked
);
1509 TEST_F(TRACE_poke
, getpid_runs_normally
)
1513 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1516 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
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
1559 # error "Do not know how to find your architecture's registers and syscalls"
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)
1566 # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(val, action)
1569 /* Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for
1570 * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux).
1572 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__)
1573 #define HAVE_GETREGS
1576 /* Architecture-specific syscall fetching routine. */
1577 int get_syscall(struct __test_metadata
*_metadata
, pid_t tracee
)
1581 EXPECT_EQ(0, ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
)) {
1582 TH_LOG("PTRACE_GETREGS failed");
1588 iov
.iov_base
= ®s
;
1589 iov
.iov_len
= sizeof(regs
);
1590 EXPECT_EQ(0, ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
)) {
1591 TH_LOG("PTRACE_GETREGSET failed");
1596 #if defined(__mips__)
1597 if (regs
.SYSCALL_NUM
== __NR_O32_Linux
)
1598 return regs
.SYSCALL_SYSCALL_NUM
;
1600 return regs
.SYSCALL_NUM
;
1603 /* Architecture-specific syscall changing routine. */
1604 void change_syscall(struct __test_metadata
*_metadata
,
1605 pid_t tracee
, int syscall
)
1610 ret
= ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
);
1613 iov
.iov_base
= ®s
;
1614 iov
.iov_len
= sizeof(regs
);
1615 ret
= ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1617 EXPECT_EQ(0, ret
) {}
1619 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \
1620 defined(__s390__) || defined(__hppa__)
1622 regs
.SYSCALL_NUM
= syscall
;
1624 #elif defined(__mips__)
1626 if (regs
.SYSCALL_NUM
== __NR_O32_Linux
)
1627 regs
.SYSCALL_SYSCALL_NUM
= syscall
;
1629 regs
.SYSCALL_NUM
= syscall
;
1632 #elif defined(__arm__)
1633 # ifndef PTRACE_SET_SYSCALL
1634 # define PTRACE_SET_SYSCALL 23
1637 ret
= ptrace(PTRACE_SET_SYSCALL
, tracee
, NULL
, syscall
);
1641 #elif defined(__aarch64__)
1642 # ifndef NT_ARM_SYSTEM_CALL
1643 # define NT_ARM_SYSTEM_CALL 0x404
1646 iov
.iov_base
= &syscall
;
1647 iov
.iov_len
= sizeof(syscall
);
1648 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_ARM_SYSTEM_CALL
,
1655 TH_LOG("How is the syscall changed on this architecture?");
1659 /* If syscall is skipped, change return value. */
1661 #ifdef SYSCALL_NUM_RET_SHARE_REG
1662 TH_LOG("Can't modify syscall return on this architecture");
1664 regs
.SYSCALL_RET
= EPERM
;
1668 ret
= ptrace(PTRACE_SETREGS
, tracee
, 0, ®s
);
1670 iov
.iov_base
= ®s
;
1671 iov
.iov_len
= sizeof(regs
);
1672 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1677 void tracer_syscall(struct __test_metadata
*_metadata
, pid_t tracee
,
1678 int status
, void *args
)
1683 /* Make sure we got the right message. */
1684 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1687 /* Validate and take action on expected syscalls. */
1690 /* change getpid to getppid. */
1691 EXPECT_EQ(__NR_getpid
, get_syscall(_metadata
, tracee
));
1692 change_syscall(_metadata
, tracee
, __NR_getppid
);
1696 EXPECT_EQ(__NR_gettid
, get_syscall(_metadata
, tracee
));
1697 change_syscall(_metadata
, tracee
, -1);
1700 /* do nothing (allow getppid) */
1701 EXPECT_EQ(__NR_getppid
, get_syscall(_metadata
, tracee
));
1705 TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg
);
1706 kill(tracee
, SIGKILL
);
1712 void tracer_ptrace(struct __test_metadata
*_metadata
, pid_t tracee
,
1713 int status
, void *args
)
1719 /* Make sure we got an empty message. */
1720 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1724 /* The only way to tell PTRACE_SYSCALL entry/exit is by counting. */
1729 nr
= get_syscall(_metadata
, tracee
);
1731 if (nr
== __NR_getpid
)
1732 change_syscall(_metadata
, tracee
, __NR_getppid
);
1733 if (nr
== __NR_openat
)
1734 change_syscall(_metadata
, tracee
, -1);
1737 FIXTURE_DATA(TRACE_syscall
) {
1738 struct sock_fprog prog
;
1739 pid_t tracer
, mytid
, mypid
, parent
;
1742 FIXTURE_SETUP(TRACE_syscall
)
1744 struct sock_filter filter
[] = {
1745 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1746 offsetof(struct seccomp_data
, nr
)),
1747 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
1748 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1002),
1749 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_gettid
, 0, 1),
1750 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1003),
1751 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1752 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1004),
1753 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1756 memset(&self
->prog
, 0, sizeof(self
->prog
));
1757 self
->prog
.filter
= malloc(sizeof(filter
));
1758 ASSERT_NE(NULL
, self
->prog
.filter
);
1759 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1760 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1762 /* Prepare some testable syscall results. */
1763 self
->mytid
= syscall(__NR_gettid
);
1764 ASSERT_GT(self
->mytid
, 0);
1765 ASSERT_NE(self
->mytid
, 1) {
1766 TH_LOG("Running this test as init is not supported. :)");
1769 self
->mypid
= getpid();
1770 ASSERT_GT(self
->mypid
, 0);
1771 ASSERT_EQ(self
->mytid
, self
->mypid
);
1773 self
->parent
= getppid();
1774 ASSERT_GT(self
->parent
, 0);
1775 ASSERT_NE(self
->parent
, self
->mypid
);
1777 /* Launch tracer. */
1778 self
->tracer
= setup_trace_fixture(_metadata
, tracer_syscall
, NULL
,
1782 FIXTURE_TEARDOWN(TRACE_syscall
)
1784 teardown_trace_fixture(_metadata
, self
->tracer
);
1785 if (self
->prog
.filter
)
1786 free(self
->prog
.filter
);
1789 TEST_F(TRACE_syscall
, ptrace_syscall_redirected
)
1791 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1792 teardown_trace_fixture(_metadata
, self
->tracer
);
1793 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1796 /* Tracer will redirect getpid to getppid. */
1797 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1800 TEST_F(TRACE_syscall
, ptrace_syscall_dropped
)
1802 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1803 teardown_trace_fixture(_metadata
, self
->tracer
);
1804 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1807 /* Tracer should skip the open syscall, resulting in EPERM. */
1808 EXPECT_SYSCALL_RETURN(EPERM
, syscall(__NR_openat
));
1811 TEST_F(TRACE_syscall
, syscall_allowed
)
1815 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1818 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1821 /* getppid works as expected (no changes). */
1822 EXPECT_EQ(self
->parent
, syscall(__NR_getppid
));
1823 EXPECT_NE(self
->mypid
, syscall(__NR_getppid
));
1826 TEST_F(TRACE_syscall
, syscall_redirected
)
1830 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1833 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1836 /* getpid has been redirected to getppid as expected. */
1837 EXPECT_EQ(self
->parent
, syscall(__NR_getpid
));
1838 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1841 TEST_F(TRACE_syscall
, syscall_dropped
)
1845 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1848 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1851 /* gettid has been skipped and an altered return value stored. */
1852 EXPECT_SYSCALL_RETURN(EPERM
, syscall(__NR_gettid
));
1853 EXPECT_NE(self
->mytid
, syscall(__NR_gettid
));
1856 TEST_F(TRACE_syscall
, skip_after_RET_TRACE
)
1858 struct sock_filter filter
[] = {
1859 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1860 offsetof(struct seccomp_data
, nr
)),
1861 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1862 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
1863 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1865 struct sock_fprog prog
= {
1866 .len
= (unsigned short)ARRAY_SIZE(filter
),
1871 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1874 /* Install fixture filter. */
1875 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1878 /* Install "errno on getppid" filter. */
1879 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1882 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
1884 EXPECT_EQ(-1, syscall(__NR_getpid
));
1885 EXPECT_EQ(EPERM
, errno
);
1888 TEST_F_SIGNAL(TRACE_syscall
, kill_after_RET_TRACE
, SIGSYS
)
1890 struct sock_filter filter
[] = {
1891 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1892 offsetof(struct seccomp_data
, nr
)),
1893 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1894 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1895 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1897 struct sock_fprog prog
= {
1898 .len
= (unsigned short)ARRAY_SIZE(filter
),
1903 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1906 /* Install fixture filter. */
1907 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1910 /* Install "death on getppid" filter. */
1911 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1914 /* Tracer will redirect getpid to getppid, and we should die. */
1915 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1918 TEST_F(TRACE_syscall
, skip_after_ptrace
)
1920 struct sock_filter filter
[] = {
1921 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1922 offsetof(struct seccomp_data
, nr
)),
1923 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1924 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
1925 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1927 struct sock_fprog prog
= {
1928 .len
= (unsigned short)ARRAY_SIZE(filter
),
1933 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1934 teardown_trace_fixture(_metadata
, self
->tracer
);
1935 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1938 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1941 /* Install "errno on getppid" filter. */
1942 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1945 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
1946 EXPECT_EQ(-1, syscall(__NR_getpid
));
1947 EXPECT_EQ(EPERM
, errno
);
1950 TEST_F_SIGNAL(TRACE_syscall
, kill_after_ptrace
, SIGSYS
)
1952 struct sock_filter filter
[] = {
1953 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1954 offsetof(struct seccomp_data
, nr
)),
1955 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1956 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1957 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1959 struct sock_fprog prog
= {
1960 .len
= (unsigned short)ARRAY_SIZE(filter
),
1965 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1966 teardown_trace_fixture(_metadata
, self
->tracer
);
1967 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1970 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1973 /* Install "death on getppid" filter. */
1974 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1977 /* Tracer will redirect getpid to getppid, and we should die. */
1978 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1981 TEST(seccomp_syscall
)
1983 struct sock_filter filter
[] = {
1984 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1986 struct sock_fprog prog
= {
1987 .len
= (unsigned short)ARRAY_SIZE(filter
),
1992 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1994 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
1997 /* Reject insane operation. */
1998 ret
= seccomp(-1, 0, &prog
);
1999 ASSERT_NE(ENOSYS
, errno
) {
2000 TH_LOG("Kernel does not support seccomp syscall!");
2002 EXPECT_EQ(EINVAL
, errno
) {
2003 TH_LOG("Did not reject crazy op value!");
2006 /* Reject strict with flags or pointer. */
2007 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, -1, NULL
);
2008 EXPECT_EQ(EINVAL
, errno
) {
2009 TH_LOG("Did not reject mode strict with flags!");
2011 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, &prog
);
2012 EXPECT_EQ(EINVAL
, errno
) {
2013 TH_LOG("Did not reject mode strict with uargs!");
2016 /* Reject insane args for filter. */
2017 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, -1, &prog
);
2018 EXPECT_EQ(EINVAL
, errno
) {
2019 TH_LOG("Did not reject crazy filter flags!");
2021 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, NULL
);
2022 EXPECT_EQ(EFAULT
, errno
) {
2023 TH_LOG("Did not reject NULL filter!");
2026 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2027 EXPECT_EQ(0, errno
) {
2028 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s",
2033 TEST(seccomp_syscall_mode_lock
)
2035 struct sock_filter filter
[] = {
2036 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2038 struct sock_fprog prog
= {
2039 .len
= (unsigned short)ARRAY_SIZE(filter
),
2044 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2046 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2049 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2050 ASSERT_NE(ENOSYS
, errno
) {
2051 TH_LOG("Kernel does not support seccomp syscall!");
2054 TH_LOG("Could not install filter!");
2057 /* Make sure neither entry point will switch to strict. */
2058 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, 0, 0, 0);
2059 EXPECT_EQ(EINVAL
, errno
) {
2060 TH_LOG("Switched to mode strict!");
2063 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, NULL
);
2064 EXPECT_EQ(EINVAL
, errno
) {
2065 TH_LOG("Switched to mode strict!");
2070 * Test detection of known and unknown filter flags. Userspace needs to be able
2071 * to check if a filter flag is supported by the current kernel and a good way
2072 * of doing that is by attempting to enter filter mode, with the flag bit in
2073 * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates
2074 * that the flag is valid and EINVAL indicates that the flag is invalid.
2076 TEST(detect_seccomp_filter_flags
)
2078 unsigned int flags
[] = { SECCOMP_FILTER_FLAG_TSYNC
,
2079 SECCOMP_FILTER_FLAG_LOG
,
2080 SECCOMP_FILTER_FLAG_SPEC_ALLOW
};
2081 unsigned int flag
, all_flags
;
2085 /* Test detection of known-good filter flags */
2086 for (i
= 0, all_flags
= 0; i
< ARRAY_SIZE(flags
); i
++) {
2090 /* Make sure the flag is a single bit! */
2099 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2100 ASSERT_NE(ENOSYS
, errno
) {
2101 TH_LOG("Kernel does not support seccomp syscall!");
2104 EXPECT_EQ(EFAULT
, errno
) {
2105 TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!",
2112 /* Test detection of all known-good filter flags */
2113 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, all_flags
, NULL
);
2115 EXPECT_EQ(EFAULT
, errno
) {
2116 TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2120 /* Test detection of an unknown filter flag */
2122 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2124 EXPECT_EQ(EINVAL
, errno
) {
2125 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!",
2130 * Test detection of an unknown filter flag that may simply need to be
2131 * added to this test
2133 flag
= flags
[ARRAY_SIZE(flags
) - 1] << 1;
2134 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2136 EXPECT_EQ(EINVAL
, errno
) {
2137 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?",
2144 struct sock_filter filter
[] = {
2145 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2147 struct sock_fprog prog
= {
2148 .len
= (unsigned short)ARRAY_SIZE(filter
),
2153 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2155 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2158 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2160 ASSERT_NE(ENOSYS
, errno
) {
2161 TH_LOG("Kernel does not support seccomp syscall!");
2164 TH_LOG("Could not install initial filter with TSYNC!");
2168 #define TSYNC_SIBLINGS 2
2169 struct tsync_sibling
{
2173 pthread_cond_t
*cond
;
2174 pthread_mutex_t
*mutex
;
2177 struct sock_fprog
*prog
;
2178 struct __test_metadata
*metadata
;
2182 * To avoid joining joined threads (which is not allowed by Bionic),
2183 * make sure we both successfully join and clear the tid to skip a
2184 * later join attempt during fixture teardown. Any remaining threads
2185 * will be directly killed during teardown.
2187 #define PTHREAD_JOIN(tid, status) \
2189 int _rc = pthread_join(tid, status); \
2191 TH_LOG("pthread_join of tid %u failed: %d\n", \
2192 (unsigned int)tid, _rc); \
2198 FIXTURE_DATA(TSYNC
) {
2199 struct sock_fprog root_prog
, apply_prog
;
2200 struct tsync_sibling sibling
[TSYNC_SIBLINGS
];
2202 pthread_cond_t cond
;
2203 pthread_mutex_t mutex
;
2207 FIXTURE_SETUP(TSYNC
)
2209 struct sock_filter root_filter
[] = {
2210 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2212 struct sock_filter apply_filter
[] = {
2213 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2214 offsetof(struct seccomp_data
, nr
)),
2215 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
2216 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2217 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2220 memset(&self
->root_prog
, 0, sizeof(self
->root_prog
));
2221 memset(&self
->apply_prog
, 0, sizeof(self
->apply_prog
));
2222 memset(&self
->sibling
, 0, sizeof(self
->sibling
));
2223 self
->root_prog
.filter
= malloc(sizeof(root_filter
));
2224 ASSERT_NE(NULL
, self
->root_prog
.filter
);
2225 memcpy(self
->root_prog
.filter
, &root_filter
, sizeof(root_filter
));
2226 self
->root_prog
.len
= (unsigned short)ARRAY_SIZE(root_filter
);
2228 self
->apply_prog
.filter
= malloc(sizeof(apply_filter
));
2229 ASSERT_NE(NULL
, self
->apply_prog
.filter
);
2230 memcpy(self
->apply_prog
.filter
, &apply_filter
, sizeof(apply_filter
));
2231 self
->apply_prog
.len
= (unsigned short)ARRAY_SIZE(apply_filter
);
2233 self
->sibling_count
= 0;
2234 pthread_mutex_init(&self
->mutex
, NULL
);
2235 pthread_cond_init(&self
->cond
, NULL
);
2236 sem_init(&self
->started
, 0, 0);
2237 self
->sibling
[0].tid
= 0;
2238 self
->sibling
[0].cond
= &self
->cond
;
2239 self
->sibling
[0].started
= &self
->started
;
2240 self
->sibling
[0].mutex
= &self
->mutex
;
2241 self
->sibling
[0].diverge
= 0;
2242 self
->sibling
[0].num_waits
= 1;
2243 self
->sibling
[0].prog
= &self
->root_prog
;
2244 self
->sibling
[0].metadata
= _metadata
;
2245 self
->sibling
[1].tid
= 0;
2246 self
->sibling
[1].cond
= &self
->cond
;
2247 self
->sibling
[1].started
= &self
->started
;
2248 self
->sibling
[1].mutex
= &self
->mutex
;
2249 self
->sibling
[1].diverge
= 0;
2250 self
->sibling
[1].prog
= &self
->root_prog
;
2251 self
->sibling
[1].num_waits
= 1;
2252 self
->sibling
[1].metadata
= _metadata
;
2255 FIXTURE_TEARDOWN(TSYNC
)
2259 if (self
->root_prog
.filter
)
2260 free(self
->root_prog
.filter
);
2261 if (self
->apply_prog
.filter
)
2262 free(self
->apply_prog
.filter
);
2264 for ( ; sib
< self
->sibling_count
; ++sib
) {
2265 struct tsync_sibling
*s
= &self
->sibling
[sib
];
2270 * If a thread is still running, it may be stuck, so hit
2271 * it over the head really hard.
2273 pthread_kill(s
->tid
, 9);
2275 pthread_mutex_destroy(&self
->mutex
);
2276 pthread_cond_destroy(&self
->cond
);
2277 sem_destroy(&self
->started
);
2280 void *tsync_sibling(void *data
)
2283 struct tsync_sibling
*me
= data
;
2285 me
->system_tid
= syscall(__NR_gettid
);
2287 pthread_mutex_lock(me
->mutex
);
2289 /* Just re-apply the root prog to fork the tree */
2290 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
2293 sem_post(me
->started
);
2294 /* Return outside of started so parent notices failures. */
2296 pthread_mutex_unlock(me
->mutex
);
2297 return (void *)SIBLING_EXIT_FAILURE
;
2300 pthread_cond_wait(me
->cond
, me
->mutex
);
2301 me
->num_waits
= me
->num_waits
- 1;
2302 } while (me
->num_waits
);
2303 pthread_mutex_unlock(me
->mutex
);
2305 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, 0, 0, 0);
2307 return (void *)SIBLING_EXIT_NEWPRIVS
;
2309 return (void *)SIBLING_EXIT_UNKILLED
;
2312 void tsync_start_sibling(struct tsync_sibling
*sibling
)
2314 pthread_create(&sibling
->tid
, NULL
, tsync_sibling
, (void *)sibling
);
2317 TEST_F(TSYNC
, siblings_fail_prctl
)
2321 struct sock_filter filter
[] = {
2322 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2323 offsetof(struct seccomp_data
, nr
)),
2324 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
2325 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EINVAL
),
2326 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2328 struct sock_fprog prog
= {
2329 .len
= (unsigned short)ARRAY_SIZE(filter
),
2333 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2334 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2337 /* Check prctl failure detection by requesting sib 0 diverge. */
2338 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2339 ASSERT_NE(ENOSYS
, errno
) {
2340 TH_LOG("Kernel does not support seccomp syscall!");
2343 TH_LOG("setting filter failed");
2346 self
->sibling
[0].diverge
= 1;
2347 tsync_start_sibling(&self
->sibling
[0]);
2348 tsync_start_sibling(&self
->sibling
[1]);
2350 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2351 sem_wait(&self
->started
);
2352 self
->sibling_count
++;
2355 /* Signal the threads to clean up*/
2356 pthread_mutex_lock(&self
->mutex
);
2357 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2358 TH_LOG("cond broadcast non-zero");
2360 pthread_mutex_unlock(&self
->mutex
);
2362 /* Ensure diverging sibling failed to call prctl. */
2363 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2364 EXPECT_EQ(SIBLING_EXIT_FAILURE
, (long)status
);
2365 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2366 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2369 TEST_F(TSYNC
, two_siblings_with_ancestor
)
2374 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2375 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2378 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2379 ASSERT_NE(ENOSYS
, errno
) {
2380 TH_LOG("Kernel does not support seccomp syscall!");
2383 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2385 tsync_start_sibling(&self
->sibling
[0]);
2386 tsync_start_sibling(&self
->sibling
[1]);
2388 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2389 sem_wait(&self
->started
);
2390 self
->sibling_count
++;
2393 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2396 TH_LOG("Could install filter on all threads!");
2398 /* Tell the siblings to test the policy */
2399 pthread_mutex_lock(&self
->mutex
);
2400 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2401 TH_LOG("cond broadcast non-zero");
2403 pthread_mutex_unlock(&self
->mutex
);
2404 /* Ensure they are both killed and don't exit cleanly. */
2405 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2406 EXPECT_EQ(0x0, (long)status
);
2407 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2408 EXPECT_EQ(0x0, (long)status
);
2411 TEST_F(TSYNC
, two_sibling_want_nnp
)
2415 /* start siblings before any prctl() operations */
2416 tsync_start_sibling(&self
->sibling
[0]);
2417 tsync_start_sibling(&self
->sibling
[1]);
2418 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2419 sem_wait(&self
->started
);
2420 self
->sibling_count
++;
2423 /* Tell the siblings to test no policy */
2424 pthread_mutex_lock(&self
->mutex
);
2425 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2426 TH_LOG("cond broadcast non-zero");
2428 pthread_mutex_unlock(&self
->mutex
);
2430 /* Ensure they are both upset about lacking nnp. */
2431 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2432 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2433 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2434 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2437 TEST_F(TSYNC
, two_siblings_with_no_filter
)
2442 /* start siblings before any prctl() operations */
2443 tsync_start_sibling(&self
->sibling
[0]);
2444 tsync_start_sibling(&self
->sibling
[1]);
2445 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2446 sem_wait(&self
->started
);
2447 self
->sibling_count
++;
2450 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2451 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2454 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2456 ASSERT_NE(ENOSYS
, errno
) {
2457 TH_LOG("Kernel does not support seccomp syscall!");
2460 TH_LOG("Could install filter on all threads!");
2463 /* Tell the siblings to test the policy */
2464 pthread_mutex_lock(&self
->mutex
);
2465 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2466 TH_LOG("cond broadcast non-zero");
2468 pthread_mutex_unlock(&self
->mutex
);
2470 /* Ensure they are both killed and don't exit cleanly. */
2471 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2472 EXPECT_EQ(0x0, (long)status
);
2473 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2474 EXPECT_EQ(0x0, (long)status
);
2477 TEST_F(TSYNC
, two_siblings_with_one_divergence
)
2482 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2483 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2486 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2487 ASSERT_NE(ENOSYS
, errno
) {
2488 TH_LOG("Kernel does not support seccomp syscall!");
2491 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2493 self
->sibling
[0].diverge
= 1;
2494 tsync_start_sibling(&self
->sibling
[0]);
2495 tsync_start_sibling(&self
->sibling
[1]);
2497 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2498 sem_wait(&self
->started
);
2499 self
->sibling_count
++;
2502 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2504 ASSERT_EQ(self
->sibling
[0].system_tid
, ret
) {
2505 TH_LOG("Did not fail on diverged sibling.");
2508 /* Wake the threads */
2509 pthread_mutex_lock(&self
->mutex
);
2510 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2511 TH_LOG("cond broadcast non-zero");
2513 pthread_mutex_unlock(&self
->mutex
);
2515 /* Ensure they are both unkilled. */
2516 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2517 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2518 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2519 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2522 TEST_F(TSYNC
, two_siblings_not_under_filter
)
2527 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2528 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2532 * Sibling 0 will have its own seccomp policy
2533 * and Sibling 1 will not be under seccomp at
2534 * all. Sibling 1 will enter seccomp and 0
2535 * will cause failure.
2537 self
->sibling
[0].diverge
= 1;
2538 tsync_start_sibling(&self
->sibling
[0]);
2539 tsync_start_sibling(&self
->sibling
[1]);
2541 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2542 sem_wait(&self
->started
);
2543 self
->sibling_count
++;
2546 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2547 ASSERT_NE(ENOSYS
, errno
) {
2548 TH_LOG("Kernel does not support seccomp syscall!");
2551 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2554 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2556 ASSERT_EQ(ret
, self
->sibling
[0].system_tid
) {
2557 TH_LOG("Did not fail on diverged sibling.");
2560 if (ret
== self
->sibling
[0].system_tid
)
2563 pthread_mutex_lock(&self
->mutex
);
2565 /* Increment the other siblings num_waits so we can clean up
2566 * the one we just saw.
2568 self
->sibling
[!sib
].num_waits
+= 1;
2570 /* Signal the thread to clean up*/
2571 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2572 TH_LOG("cond broadcast non-zero");
2574 pthread_mutex_unlock(&self
->mutex
);
2575 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2576 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2577 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2578 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2580 /* Switch to the remaining sibling */
2583 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2586 TH_LOG("Expected the remaining sibling to sync");
2589 pthread_mutex_lock(&self
->mutex
);
2591 /* If remaining sibling didn't have a chance to wake up during
2592 * the first broadcast, manually reduce the num_waits now.
2594 if (self
->sibling
[sib
].num_waits
> 1)
2595 self
->sibling
[sib
].num_waits
= 1;
2596 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2597 TH_LOG("cond broadcast non-zero");
2599 pthread_mutex_unlock(&self
->mutex
);
2600 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2601 EXPECT_EQ(0, (long)status
);
2602 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2603 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2606 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2608 ASSERT_EQ(0, ret
); /* just us chickens */
2611 /* Make sure restarted syscalls are seen directly as "restart_syscall". */
2612 TEST(syscall_restart
)
2619 siginfo_t info
= { };
2620 struct sock_filter filter
[] = {
2621 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2622 offsetof(struct seccomp_data
, nr
)),
2624 #ifdef __NR_sigreturn
2625 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_sigreturn
, 6, 0),
2627 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 5, 0),
2628 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_exit
, 4, 0),
2629 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_rt_sigreturn
, 3, 0),
2630 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_nanosleep
, 4, 0),
2631 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_restart_syscall
, 4, 0),
2633 /* Allow __NR_write for easy logging. */
2634 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_write
, 0, 1),
2635 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2636 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2637 /* The nanosleep jump target. */
2638 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x100),
2639 /* The restart_syscall jump target. */
2640 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x200),
2642 struct sock_fprog prog
= {
2643 .len
= (unsigned short)ARRAY_SIZE(filter
),
2646 #if defined(__arm__)
2647 struct utsname utsbuf
;
2650 ASSERT_EQ(0, pipe(pipefd
));
2653 ASSERT_LE(0, child_pid
);
2654 if (child_pid
== 0) {
2655 /* Child uses EXPECT not ASSERT to deliver status correctly. */
2657 struct timespec timeout
= { };
2659 /* Attach parent as tracer and stop. */
2660 EXPECT_EQ(0, ptrace(PTRACE_TRACEME
));
2661 EXPECT_EQ(0, raise(SIGSTOP
));
2663 EXPECT_EQ(0, close(pipefd
[1]));
2665 EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2666 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2669 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2671 TH_LOG("Failed to install filter!");
2674 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2675 TH_LOG("Failed to read() sync from parent");
2677 EXPECT_EQ('.', buf
) {
2678 TH_LOG("Failed to get sync data from read()");
2681 /* Start nanosleep to be interrupted. */
2684 EXPECT_EQ(0, nanosleep(&timeout
, NULL
)) {
2685 TH_LOG("Call to nanosleep() failed (errno %d)", errno
);
2688 /* Read final sync from parent. */
2689 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2690 TH_LOG("Failed final read() from parent");
2692 EXPECT_EQ('!', buf
) {
2693 TH_LOG("Failed to get final data from read()");
2696 /* Directly report the status of our test harness results. */
2697 syscall(__NR_exit
, _metadata
->passed
? EXIT_SUCCESS
2700 EXPECT_EQ(0, close(pipefd
[0]));
2702 /* Attach to child, setup options, and release. */
2703 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2704 ASSERT_EQ(true, WIFSTOPPED(status
));
2705 ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS
, child_pid
, NULL
,
2706 PTRACE_O_TRACESECCOMP
));
2707 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2708 ASSERT_EQ(1, write(pipefd
[1], ".", 1));
2710 /* Wait for nanosleep() to start. */
2711 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2712 ASSERT_EQ(true, WIFSTOPPED(status
));
2713 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2714 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2715 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2716 ASSERT_EQ(0x100, msg
);
2717 EXPECT_EQ(__NR_nanosleep
, get_syscall(_metadata
, child_pid
));
2719 /* Might as well check siginfo for sanity while we're here. */
2720 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2721 ASSERT_EQ(SIGTRAP
, info
.si_signo
);
2722 ASSERT_EQ(SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8), info
.si_code
);
2723 EXPECT_EQ(0, info
.si_errno
);
2724 EXPECT_EQ(getuid(), info
.si_uid
);
2725 /* Verify signal delivery came from child (seccomp-triggered). */
2726 EXPECT_EQ(child_pid
, info
.si_pid
);
2728 /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */
2729 ASSERT_EQ(0, kill(child_pid
, SIGSTOP
));
2730 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2731 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2732 ASSERT_EQ(true, WIFSTOPPED(status
));
2733 ASSERT_EQ(SIGSTOP
, WSTOPSIG(status
));
2734 /* Verify signal delivery came from parent now. */
2735 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2736 EXPECT_EQ(getpid(), info
.si_pid
);
2738 /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */
2739 ASSERT_EQ(0, kill(child_pid
, SIGCONT
));
2740 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2741 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2742 ASSERT_EQ(true, WIFSTOPPED(status
));
2743 ASSERT_EQ(SIGCONT
, WSTOPSIG(status
));
2744 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2746 /* Wait for restart_syscall() to start. */
2747 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2748 ASSERT_EQ(true, WIFSTOPPED(status
));
2749 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2750 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2751 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2753 ASSERT_EQ(0x200, msg
);
2754 ret
= get_syscall(_metadata
, child_pid
);
2755 #if defined(__arm__)
2758 * - native ARM registers do NOT expose true syscall.
2759 * - compat ARM registers on ARM64 DO expose true syscall.
2761 ASSERT_EQ(0, uname(&utsbuf
));
2762 if (strncmp(utsbuf
.machine
, "arm", 3) == 0) {
2763 EXPECT_EQ(__NR_nanosleep
, ret
);
2767 EXPECT_EQ(__NR_restart_syscall
, ret
);
2770 /* Write again to end test. */
2771 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2772 ASSERT_EQ(1, write(pipefd
[1], "!", 1));
2773 EXPECT_EQ(0, close(pipefd
[1]));
2775 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2776 if (WIFSIGNALED(status
) || WEXITSTATUS(status
))
2777 _metadata
->passed
= 0;
2780 TEST_SIGNAL(filter_flag_log
, SIGSYS
)
2782 struct sock_filter allow_filter
[] = {
2783 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2785 struct sock_filter kill_filter
[] = {
2786 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2787 offsetof(struct seccomp_data
, nr
)),
2788 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
2789 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2790 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2792 struct sock_fprog allow_prog
= {
2793 .len
= (unsigned short)ARRAY_SIZE(allow_filter
),
2794 .filter
= allow_filter
,
2796 struct sock_fprog kill_prog
= {
2797 .len
= (unsigned short)ARRAY_SIZE(kill_filter
),
2798 .filter
= kill_filter
,
2801 pid_t parent
= getppid();
2803 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2806 /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */
2807 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, SECCOMP_FILTER_FLAG_LOG
,
2809 ASSERT_NE(ENOSYS
, errno
) {
2810 TH_LOG("Kernel does not support seccomp syscall!");
2813 TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!");
2815 EXPECT_EQ(EINVAL
, errno
) {
2816 TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!");
2819 /* Verify that a simple, permissive filter can be added with no flags */
2820 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &allow_prog
);
2823 /* See if the same filter can be added with the FILTER_FLAG_LOG flag */
2824 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
2826 ASSERT_NE(EINVAL
, errno
) {
2827 TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!");
2831 /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */
2832 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
2836 EXPECT_EQ(parent
, syscall(__NR_getppid
));
2837 /* getpid() should never return. */
2838 EXPECT_EQ(0, syscall(__NR_getpid
));
2841 TEST(get_action_avail
)
2843 __u32 actions
[] = { SECCOMP_RET_KILL_THREAD
, SECCOMP_RET_TRAP
,
2844 SECCOMP_RET_ERRNO
, SECCOMP_RET_TRACE
,
2845 SECCOMP_RET_LOG
, SECCOMP_RET_ALLOW
};
2846 __u32 unknown_action
= 0x10000000U
;
2850 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[0]);
2851 ASSERT_NE(ENOSYS
, errno
) {
2852 TH_LOG("Kernel does not support seccomp syscall!");
2854 ASSERT_NE(EINVAL
, errno
) {
2855 TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!");
2859 for (i
= 0; i
< ARRAY_SIZE(actions
); i
++) {
2860 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[i
]);
2862 TH_LOG("Expected action (0x%X) not available!",
2867 /* Check that an unknown action is handled properly (EOPNOTSUPP) */
2868 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &unknown_action
);
2870 EXPECT_EQ(errno
, EOPNOTSUPP
);
2878 struct seccomp_metadata md
;
2881 ASSERT_EQ(0, pipe(pipefd
));
2886 struct sock_filter filter
[] = {
2887 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2889 struct sock_fprog prog
= {
2890 .len
= (unsigned short)ARRAY_SIZE(filter
),
2894 /* one with log, one without */
2895 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
,
2896 SECCOMP_FILTER_FLAG_LOG
, &prog
));
2897 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
));
2899 ASSERT_EQ(0, close(pipefd
[0]));
2900 ASSERT_EQ(1, write(pipefd
[1], "1", 1));
2901 ASSERT_EQ(0, close(pipefd
[1]));
2907 ASSERT_EQ(0, close(pipefd
[1]));
2908 ASSERT_EQ(1, read(pipefd
[0], &buf
, 1));
2910 ASSERT_EQ(0, ptrace(PTRACE_ATTACH
, pid
));
2911 ASSERT_EQ(pid
, waitpid(pid
, NULL
, 0));
2913 /* Past here must not use ASSERT or child process is never killed. */
2917 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
2918 EXPECT_EQ(sizeof(md
), ret
) {
2919 if (errno
== EINVAL
)
2920 XFAIL(goto skip
, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)");
2923 EXPECT_EQ(md
.flags
, SECCOMP_FILTER_FLAG_LOG
);
2924 EXPECT_EQ(md
.filter_off
, 0);
2927 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
2928 EXPECT_EQ(sizeof(md
), ret
);
2929 EXPECT_EQ(md
.flags
, 0);
2930 EXPECT_EQ(md
.filter_off
, 1);
2933 ASSERT_EQ(0, kill(pid
, SIGKILL
));
2938 * - add microbenchmarks
2939 * - expand NNP testing
2940 * - better arch-specific TRACE and TRAP handlers.
2941 * - endianness checking when appropriate
2942 * - 64-bit arg prodding
2943 * - arch value testing (x86 modes especially)
2944 * - verify that FILTER_FLAG_LOG filters generate log messages
2945 * - verify that RET_LOG generates log messages