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) \
1570 EXPECT_EQ(-1, action); \
1571 EXPECT_EQ(-(val), errno); \
1573 EXPECT_EQ(val, action); \
1578 /* Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for
1579 * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux).
1581 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__)
1582 #define HAVE_GETREGS
1585 /* Architecture-specific syscall fetching routine. */
1586 int get_syscall(struct __test_metadata
*_metadata
, pid_t tracee
)
1590 EXPECT_EQ(0, ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
)) {
1591 TH_LOG("PTRACE_GETREGS failed");
1597 iov
.iov_base
= ®s
;
1598 iov
.iov_len
= sizeof(regs
);
1599 EXPECT_EQ(0, ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
)) {
1600 TH_LOG("PTRACE_GETREGSET failed");
1605 #if defined(__mips__)
1606 if (regs
.SYSCALL_NUM
== __NR_O32_Linux
)
1607 return regs
.SYSCALL_SYSCALL_NUM
;
1609 return regs
.SYSCALL_NUM
;
1612 /* Architecture-specific syscall changing routine. */
1613 void change_syscall(struct __test_metadata
*_metadata
,
1614 pid_t tracee
, int syscall
, int result
)
1619 ret
= ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
);
1622 iov
.iov_base
= ®s
;
1623 iov
.iov_len
= sizeof(regs
);
1624 ret
= ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1626 EXPECT_EQ(0, ret
) {}
1628 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \
1629 defined(__s390__) || defined(__hppa__)
1631 regs
.SYSCALL_NUM
= syscall
;
1633 #elif defined(__mips__)
1635 if (regs
.SYSCALL_NUM
== __NR_O32_Linux
)
1636 regs
.SYSCALL_SYSCALL_NUM
= syscall
;
1638 regs
.SYSCALL_NUM
= syscall
;
1641 #elif defined(__arm__)
1642 # ifndef PTRACE_SET_SYSCALL
1643 # define PTRACE_SET_SYSCALL 23
1646 ret
= ptrace(PTRACE_SET_SYSCALL
, tracee
, NULL
, syscall
);
1650 #elif defined(__aarch64__)
1651 # ifndef NT_ARM_SYSTEM_CALL
1652 # define NT_ARM_SYSTEM_CALL 0x404
1655 iov
.iov_base
= &syscall
;
1656 iov
.iov_len
= sizeof(syscall
);
1657 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_ARM_SYSTEM_CALL
,
1664 TH_LOG("How is the syscall changed on this architecture?");
1668 /* If syscall is skipped, change return value. */
1670 #ifdef SYSCALL_NUM_RET_SHARE_REG
1671 TH_LOG("Can't modify syscall return on this architecture");
1673 regs
.SYSCALL_RET
= result
;
1677 ret
= ptrace(PTRACE_SETREGS
, tracee
, 0, ®s
);
1679 iov
.iov_base
= ®s
;
1680 iov
.iov_len
= sizeof(regs
);
1681 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1686 void tracer_syscall(struct __test_metadata
*_metadata
, pid_t tracee
,
1687 int status
, void *args
)
1692 /* Make sure we got the right message. */
1693 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1696 /* Validate and take action on expected syscalls. */
1699 /* change getpid to getppid. */
1700 EXPECT_EQ(__NR_getpid
, get_syscall(_metadata
, tracee
));
1701 change_syscall(_metadata
, tracee
, __NR_getppid
, 0);
1704 /* skip gettid with valid return code. */
1705 EXPECT_EQ(__NR_gettid
, get_syscall(_metadata
, tracee
));
1706 change_syscall(_metadata
, tracee
, -1, 45000);
1709 /* skip openat with error. */
1710 EXPECT_EQ(__NR_openat
, get_syscall(_metadata
, tracee
));
1711 change_syscall(_metadata
, tracee
, -1, -ESRCH
);
1714 /* do nothing (allow getppid) */
1715 EXPECT_EQ(__NR_getppid
, get_syscall(_metadata
, tracee
));
1719 TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg
);
1720 kill(tracee
, SIGKILL
);
1726 void tracer_ptrace(struct __test_metadata
*_metadata
, pid_t tracee
,
1727 int status
, void *args
)
1733 /* Make sure we got an empty message. */
1734 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1738 /* The only way to tell PTRACE_SYSCALL entry/exit is by counting. */
1743 nr
= get_syscall(_metadata
, tracee
);
1745 if (nr
== __NR_getpid
)
1746 change_syscall(_metadata
, tracee
, __NR_getppid
, 0);
1747 if (nr
== __NR_gettid
)
1748 change_syscall(_metadata
, tracee
, -1, 45000);
1749 if (nr
== __NR_openat
)
1750 change_syscall(_metadata
, tracee
, -1, -ESRCH
);
1753 FIXTURE_DATA(TRACE_syscall
) {
1754 struct sock_fprog prog
;
1755 pid_t tracer
, mytid
, mypid
, parent
;
1758 FIXTURE_SETUP(TRACE_syscall
)
1760 struct sock_filter filter
[] = {
1761 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1762 offsetof(struct seccomp_data
, nr
)),
1763 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
1764 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1002),
1765 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_gettid
, 0, 1),
1766 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1003),
1767 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_openat
, 0, 1),
1768 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1004),
1769 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1770 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1005),
1771 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1774 memset(&self
->prog
, 0, sizeof(self
->prog
));
1775 self
->prog
.filter
= malloc(sizeof(filter
));
1776 ASSERT_NE(NULL
, self
->prog
.filter
);
1777 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1778 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1780 /* Prepare some testable syscall results. */
1781 self
->mytid
= syscall(__NR_gettid
);
1782 ASSERT_GT(self
->mytid
, 0);
1783 ASSERT_NE(self
->mytid
, 1) {
1784 TH_LOG("Running this test as init is not supported. :)");
1787 self
->mypid
= getpid();
1788 ASSERT_GT(self
->mypid
, 0);
1789 ASSERT_EQ(self
->mytid
, self
->mypid
);
1791 self
->parent
= getppid();
1792 ASSERT_GT(self
->parent
, 0);
1793 ASSERT_NE(self
->parent
, self
->mypid
);
1795 /* Launch tracer. */
1796 self
->tracer
= setup_trace_fixture(_metadata
, tracer_syscall
, NULL
,
1800 FIXTURE_TEARDOWN(TRACE_syscall
)
1802 teardown_trace_fixture(_metadata
, self
->tracer
);
1803 if (self
->prog
.filter
)
1804 free(self
->prog
.filter
);
1807 TEST_F(TRACE_syscall
, ptrace_syscall_redirected
)
1809 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1810 teardown_trace_fixture(_metadata
, self
->tracer
);
1811 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1814 /* Tracer will redirect getpid to getppid. */
1815 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1818 TEST_F(TRACE_syscall
, ptrace_syscall_errno
)
1820 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1821 teardown_trace_fixture(_metadata
, self
->tracer
);
1822 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1825 /* Tracer should skip the open syscall, resulting in ESRCH. */
1826 EXPECT_SYSCALL_RETURN(-ESRCH
, syscall(__NR_openat
));
1829 TEST_F(TRACE_syscall
, ptrace_syscall_faked
)
1831 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1832 teardown_trace_fixture(_metadata
, self
->tracer
);
1833 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1836 /* Tracer should skip the gettid syscall, resulting fake pid. */
1837 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid
));
1840 TEST_F(TRACE_syscall
, syscall_allowed
)
1844 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1847 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1850 /* getppid works as expected (no changes). */
1851 EXPECT_EQ(self
->parent
, syscall(__NR_getppid
));
1852 EXPECT_NE(self
->mypid
, syscall(__NR_getppid
));
1855 TEST_F(TRACE_syscall
, syscall_redirected
)
1859 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1862 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1865 /* getpid has been redirected to getppid as expected. */
1866 EXPECT_EQ(self
->parent
, syscall(__NR_getpid
));
1867 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1870 TEST_F(TRACE_syscall
, syscall_errno
)
1874 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1877 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1880 /* openat has been skipped and an errno return. */
1881 EXPECT_SYSCALL_RETURN(-ESRCH
, syscall(__NR_openat
));
1884 TEST_F(TRACE_syscall
, syscall_faked
)
1888 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1891 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1894 /* gettid has been skipped and an altered return value stored. */
1895 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid
));
1898 TEST_F(TRACE_syscall
, skip_after_RET_TRACE
)
1900 struct sock_filter filter
[] = {
1901 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1902 offsetof(struct seccomp_data
, nr
)),
1903 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1904 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
1905 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1907 struct sock_fprog prog
= {
1908 .len
= (unsigned short)ARRAY_SIZE(filter
),
1913 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1916 /* Install fixture filter. */
1917 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1920 /* Install "errno on getppid" filter. */
1921 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1924 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
1926 EXPECT_EQ(-1, syscall(__NR_getpid
));
1927 EXPECT_EQ(EPERM
, errno
);
1930 TEST_F_SIGNAL(TRACE_syscall
, kill_after_RET_TRACE
, SIGSYS
)
1932 struct sock_filter filter
[] = {
1933 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1934 offsetof(struct seccomp_data
, nr
)),
1935 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1936 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1937 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1939 struct sock_fprog prog
= {
1940 .len
= (unsigned short)ARRAY_SIZE(filter
),
1945 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1948 /* Install fixture filter. */
1949 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1952 /* Install "death on getppid" filter. */
1953 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1956 /* Tracer will redirect getpid to getppid, and we should die. */
1957 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1960 TEST_F(TRACE_syscall
, skip_after_ptrace
)
1962 struct sock_filter filter
[] = {
1963 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1964 offsetof(struct seccomp_data
, nr
)),
1965 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1966 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
1967 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1969 struct sock_fprog prog
= {
1970 .len
= (unsigned short)ARRAY_SIZE(filter
),
1975 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1976 teardown_trace_fixture(_metadata
, self
->tracer
);
1977 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1980 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1983 /* Install "errno on getppid" filter. */
1984 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1987 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
1988 EXPECT_EQ(-1, syscall(__NR_getpid
));
1989 EXPECT_EQ(EPERM
, errno
);
1992 TEST_F_SIGNAL(TRACE_syscall
, kill_after_ptrace
, SIGSYS
)
1994 struct sock_filter filter
[] = {
1995 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1996 offsetof(struct seccomp_data
, nr
)),
1997 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1998 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1999 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2001 struct sock_fprog prog
= {
2002 .len
= (unsigned short)ARRAY_SIZE(filter
),
2007 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
2008 teardown_trace_fixture(_metadata
, self
->tracer
);
2009 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
2012 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2015 /* Install "death on getppid" filter. */
2016 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2019 /* Tracer will redirect getpid to getppid, and we should die. */
2020 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
2023 TEST(seccomp_syscall
)
2025 struct sock_filter filter
[] = {
2026 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2028 struct sock_fprog prog
= {
2029 .len
= (unsigned short)ARRAY_SIZE(filter
),
2034 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2036 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2039 /* Reject insane operation. */
2040 ret
= seccomp(-1, 0, &prog
);
2041 ASSERT_NE(ENOSYS
, errno
) {
2042 TH_LOG("Kernel does not support seccomp syscall!");
2044 EXPECT_EQ(EINVAL
, errno
) {
2045 TH_LOG("Did not reject crazy op value!");
2048 /* Reject strict with flags or pointer. */
2049 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, -1, NULL
);
2050 EXPECT_EQ(EINVAL
, errno
) {
2051 TH_LOG("Did not reject mode strict with flags!");
2053 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, &prog
);
2054 EXPECT_EQ(EINVAL
, errno
) {
2055 TH_LOG("Did not reject mode strict with uargs!");
2058 /* Reject insane args for filter. */
2059 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, -1, &prog
);
2060 EXPECT_EQ(EINVAL
, errno
) {
2061 TH_LOG("Did not reject crazy filter flags!");
2063 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, NULL
);
2064 EXPECT_EQ(EFAULT
, errno
) {
2065 TH_LOG("Did not reject NULL filter!");
2068 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2069 EXPECT_EQ(0, errno
) {
2070 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s",
2075 TEST(seccomp_syscall_mode_lock
)
2077 struct sock_filter filter
[] = {
2078 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2080 struct sock_fprog prog
= {
2081 .len
= (unsigned short)ARRAY_SIZE(filter
),
2086 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2088 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2091 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2092 ASSERT_NE(ENOSYS
, errno
) {
2093 TH_LOG("Kernel does not support seccomp syscall!");
2096 TH_LOG("Could not install filter!");
2099 /* Make sure neither entry point will switch to strict. */
2100 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, 0, 0, 0);
2101 EXPECT_EQ(EINVAL
, errno
) {
2102 TH_LOG("Switched to mode strict!");
2105 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, NULL
);
2106 EXPECT_EQ(EINVAL
, errno
) {
2107 TH_LOG("Switched to mode strict!");
2112 * Test detection of known and unknown filter flags. Userspace needs to be able
2113 * to check if a filter flag is supported by the current kernel and a good way
2114 * of doing that is by attempting to enter filter mode, with the flag bit in
2115 * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates
2116 * that the flag is valid and EINVAL indicates that the flag is invalid.
2118 TEST(detect_seccomp_filter_flags
)
2120 unsigned int flags
[] = { SECCOMP_FILTER_FLAG_TSYNC
,
2121 SECCOMP_FILTER_FLAG_LOG
,
2122 SECCOMP_FILTER_FLAG_SPEC_ALLOW
};
2123 unsigned int flag
, all_flags
;
2127 /* Test detection of known-good filter flags */
2128 for (i
= 0, all_flags
= 0; i
< ARRAY_SIZE(flags
); i
++) {
2132 /* Make sure the flag is a single bit! */
2141 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2142 ASSERT_NE(ENOSYS
, errno
) {
2143 TH_LOG("Kernel does not support seccomp syscall!");
2146 EXPECT_EQ(EFAULT
, errno
) {
2147 TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!",
2154 /* Test detection of all known-good filter flags */
2155 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, all_flags
, NULL
);
2157 EXPECT_EQ(EFAULT
, errno
) {
2158 TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2162 /* Test detection of an unknown filter flag */
2164 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2166 EXPECT_EQ(EINVAL
, errno
) {
2167 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!",
2172 * Test detection of an unknown filter flag that may simply need to be
2173 * added to this test
2175 flag
= flags
[ARRAY_SIZE(flags
) - 1] << 1;
2176 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2178 EXPECT_EQ(EINVAL
, errno
) {
2179 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported! Does a new flag need to be added to this test?",
2186 struct sock_filter filter
[] = {
2187 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2189 struct sock_fprog prog
= {
2190 .len
= (unsigned short)ARRAY_SIZE(filter
),
2195 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2197 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2200 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2202 ASSERT_NE(ENOSYS
, errno
) {
2203 TH_LOG("Kernel does not support seccomp syscall!");
2206 TH_LOG("Could not install initial filter with TSYNC!");
2210 #define TSYNC_SIBLINGS 2
2211 struct tsync_sibling
{
2215 pthread_cond_t
*cond
;
2216 pthread_mutex_t
*mutex
;
2219 struct sock_fprog
*prog
;
2220 struct __test_metadata
*metadata
;
2224 * To avoid joining joined threads (which is not allowed by Bionic),
2225 * make sure we both successfully join and clear the tid to skip a
2226 * later join attempt during fixture teardown. Any remaining threads
2227 * will be directly killed during teardown.
2229 #define PTHREAD_JOIN(tid, status) \
2231 int _rc = pthread_join(tid, status); \
2233 TH_LOG("pthread_join of tid %u failed: %d\n", \
2234 (unsigned int)tid, _rc); \
2240 FIXTURE_DATA(TSYNC
) {
2241 struct sock_fprog root_prog
, apply_prog
;
2242 struct tsync_sibling sibling
[TSYNC_SIBLINGS
];
2244 pthread_cond_t cond
;
2245 pthread_mutex_t mutex
;
2249 FIXTURE_SETUP(TSYNC
)
2251 struct sock_filter root_filter
[] = {
2252 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2254 struct sock_filter apply_filter
[] = {
2255 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2256 offsetof(struct seccomp_data
, nr
)),
2257 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
2258 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2259 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2262 memset(&self
->root_prog
, 0, sizeof(self
->root_prog
));
2263 memset(&self
->apply_prog
, 0, sizeof(self
->apply_prog
));
2264 memset(&self
->sibling
, 0, sizeof(self
->sibling
));
2265 self
->root_prog
.filter
= malloc(sizeof(root_filter
));
2266 ASSERT_NE(NULL
, self
->root_prog
.filter
);
2267 memcpy(self
->root_prog
.filter
, &root_filter
, sizeof(root_filter
));
2268 self
->root_prog
.len
= (unsigned short)ARRAY_SIZE(root_filter
);
2270 self
->apply_prog
.filter
= malloc(sizeof(apply_filter
));
2271 ASSERT_NE(NULL
, self
->apply_prog
.filter
);
2272 memcpy(self
->apply_prog
.filter
, &apply_filter
, sizeof(apply_filter
));
2273 self
->apply_prog
.len
= (unsigned short)ARRAY_SIZE(apply_filter
);
2275 self
->sibling_count
= 0;
2276 pthread_mutex_init(&self
->mutex
, NULL
);
2277 pthread_cond_init(&self
->cond
, NULL
);
2278 sem_init(&self
->started
, 0, 0);
2279 self
->sibling
[0].tid
= 0;
2280 self
->sibling
[0].cond
= &self
->cond
;
2281 self
->sibling
[0].started
= &self
->started
;
2282 self
->sibling
[0].mutex
= &self
->mutex
;
2283 self
->sibling
[0].diverge
= 0;
2284 self
->sibling
[0].num_waits
= 1;
2285 self
->sibling
[0].prog
= &self
->root_prog
;
2286 self
->sibling
[0].metadata
= _metadata
;
2287 self
->sibling
[1].tid
= 0;
2288 self
->sibling
[1].cond
= &self
->cond
;
2289 self
->sibling
[1].started
= &self
->started
;
2290 self
->sibling
[1].mutex
= &self
->mutex
;
2291 self
->sibling
[1].diverge
= 0;
2292 self
->sibling
[1].prog
= &self
->root_prog
;
2293 self
->sibling
[1].num_waits
= 1;
2294 self
->sibling
[1].metadata
= _metadata
;
2297 FIXTURE_TEARDOWN(TSYNC
)
2301 if (self
->root_prog
.filter
)
2302 free(self
->root_prog
.filter
);
2303 if (self
->apply_prog
.filter
)
2304 free(self
->apply_prog
.filter
);
2306 for ( ; sib
< self
->sibling_count
; ++sib
) {
2307 struct tsync_sibling
*s
= &self
->sibling
[sib
];
2312 * If a thread is still running, it may be stuck, so hit
2313 * it over the head really hard.
2315 pthread_kill(s
->tid
, 9);
2317 pthread_mutex_destroy(&self
->mutex
);
2318 pthread_cond_destroy(&self
->cond
);
2319 sem_destroy(&self
->started
);
2322 void *tsync_sibling(void *data
)
2325 struct tsync_sibling
*me
= data
;
2327 me
->system_tid
= syscall(__NR_gettid
);
2329 pthread_mutex_lock(me
->mutex
);
2331 /* Just re-apply the root prog to fork the tree */
2332 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
2335 sem_post(me
->started
);
2336 /* Return outside of started so parent notices failures. */
2338 pthread_mutex_unlock(me
->mutex
);
2339 return (void *)SIBLING_EXIT_FAILURE
;
2342 pthread_cond_wait(me
->cond
, me
->mutex
);
2343 me
->num_waits
= me
->num_waits
- 1;
2344 } while (me
->num_waits
);
2345 pthread_mutex_unlock(me
->mutex
);
2347 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, 0, 0, 0);
2349 return (void *)SIBLING_EXIT_NEWPRIVS
;
2351 return (void *)SIBLING_EXIT_UNKILLED
;
2354 void tsync_start_sibling(struct tsync_sibling
*sibling
)
2356 pthread_create(&sibling
->tid
, NULL
, tsync_sibling
, (void *)sibling
);
2359 TEST_F(TSYNC
, siblings_fail_prctl
)
2363 struct sock_filter filter
[] = {
2364 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2365 offsetof(struct seccomp_data
, nr
)),
2366 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
2367 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EINVAL
),
2368 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2370 struct sock_fprog prog
= {
2371 .len
= (unsigned short)ARRAY_SIZE(filter
),
2375 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2376 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2379 /* Check prctl failure detection by requesting sib 0 diverge. */
2380 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2381 ASSERT_NE(ENOSYS
, errno
) {
2382 TH_LOG("Kernel does not support seccomp syscall!");
2385 TH_LOG("setting filter failed");
2388 self
->sibling
[0].diverge
= 1;
2389 tsync_start_sibling(&self
->sibling
[0]);
2390 tsync_start_sibling(&self
->sibling
[1]);
2392 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2393 sem_wait(&self
->started
);
2394 self
->sibling_count
++;
2397 /* Signal the threads to clean up*/
2398 pthread_mutex_lock(&self
->mutex
);
2399 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2400 TH_LOG("cond broadcast non-zero");
2402 pthread_mutex_unlock(&self
->mutex
);
2404 /* Ensure diverging sibling failed to call prctl. */
2405 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2406 EXPECT_EQ(SIBLING_EXIT_FAILURE
, (long)status
);
2407 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2408 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2411 TEST_F(TSYNC
, two_siblings_with_ancestor
)
2416 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2417 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2420 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2421 ASSERT_NE(ENOSYS
, errno
) {
2422 TH_LOG("Kernel does not support seccomp syscall!");
2425 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2427 tsync_start_sibling(&self
->sibling
[0]);
2428 tsync_start_sibling(&self
->sibling
[1]);
2430 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2431 sem_wait(&self
->started
);
2432 self
->sibling_count
++;
2435 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2438 TH_LOG("Could install filter on all threads!");
2440 /* Tell the siblings to test the policy */
2441 pthread_mutex_lock(&self
->mutex
);
2442 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2443 TH_LOG("cond broadcast non-zero");
2445 pthread_mutex_unlock(&self
->mutex
);
2446 /* Ensure they are both killed and don't exit cleanly. */
2447 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2448 EXPECT_EQ(0x0, (long)status
);
2449 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2450 EXPECT_EQ(0x0, (long)status
);
2453 TEST_F(TSYNC
, two_sibling_want_nnp
)
2457 /* start siblings before any prctl() operations */
2458 tsync_start_sibling(&self
->sibling
[0]);
2459 tsync_start_sibling(&self
->sibling
[1]);
2460 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2461 sem_wait(&self
->started
);
2462 self
->sibling_count
++;
2465 /* Tell the siblings to test no policy */
2466 pthread_mutex_lock(&self
->mutex
);
2467 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2468 TH_LOG("cond broadcast non-zero");
2470 pthread_mutex_unlock(&self
->mutex
);
2472 /* Ensure they are both upset about lacking nnp. */
2473 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2474 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2475 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2476 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2479 TEST_F(TSYNC
, two_siblings_with_no_filter
)
2484 /* start siblings before any prctl() operations */
2485 tsync_start_sibling(&self
->sibling
[0]);
2486 tsync_start_sibling(&self
->sibling
[1]);
2487 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2488 sem_wait(&self
->started
);
2489 self
->sibling_count
++;
2492 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2493 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2496 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2498 ASSERT_NE(ENOSYS
, errno
) {
2499 TH_LOG("Kernel does not support seccomp syscall!");
2502 TH_LOG("Could install filter on all threads!");
2505 /* Tell the siblings to test the policy */
2506 pthread_mutex_lock(&self
->mutex
);
2507 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2508 TH_LOG("cond broadcast non-zero");
2510 pthread_mutex_unlock(&self
->mutex
);
2512 /* Ensure they are both killed and don't exit cleanly. */
2513 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2514 EXPECT_EQ(0x0, (long)status
);
2515 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2516 EXPECT_EQ(0x0, (long)status
);
2519 TEST_F(TSYNC
, two_siblings_with_one_divergence
)
2524 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2525 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2528 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2529 ASSERT_NE(ENOSYS
, errno
) {
2530 TH_LOG("Kernel does not support seccomp syscall!");
2533 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2535 self
->sibling
[0].diverge
= 1;
2536 tsync_start_sibling(&self
->sibling
[0]);
2537 tsync_start_sibling(&self
->sibling
[1]);
2539 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2540 sem_wait(&self
->started
);
2541 self
->sibling_count
++;
2544 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2546 ASSERT_EQ(self
->sibling
[0].system_tid
, ret
) {
2547 TH_LOG("Did not fail on diverged sibling.");
2550 /* Wake the threads */
2551 pthread_mutex_lock(&self
->mutex
);
2552 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2553 TH_LOG("cond broadcast non-zero");
2555 pthread_mutex_unlock(&self
->mutex
);
2557 /* Ensure they are both unkilled. */
2558 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2559 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2560 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2561 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2564 TEST_F(TSYNC
, two_siblings_not_under_filter
)
2569 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2570 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2574 * Sibling 0 will have its own seccomp policy
2575 * and Sibling 1 will not be under seccomp at
2576 * all. Sibling 1 will enter seccomp and 0
2577 * will cause failure.
2579 self
->sibling
[0].diverge
= 1;
2580 tsync_start_sibling(&self
->sibling
[0]);
2581 tsync_start_sibling(&self
->sibling
[1]);
2583 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2584 sem_wait(&self
->started
);
2585 self
->sibling_count
++;
2588 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2589 ASSERT_NE(ENOSYS
, errno
) {
2590 TH_LOG("Kernel does not support seccomp syscall!");
2593 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2596 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2598 ASSERT_EQ(ret
, self
->sibling
[0].system_tid
) {
2599 TH_LOG("Did not fail on diverged sibling.");
2602 if (ret
== self
->sibling
[0].system_tid
)
2605 pthread_mutex_lock(&self
->mutex
);
2607 /* Increment the other siblings num_waits so we can clean up
2608 * the one we just saw.
2610 self
->sibling
[!sib
].num_waits
+= 1;
2612 /* Signal the thread to clean up*/
2613 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2614 TH_LOG("cond broadcast non-zero");
2616 pthread_mutex_unlock(&self
->mutex
);
2617 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2618 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2619 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2620 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2622 /* Switch to the remaining sibling */
2625 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2628 TH_LOG("Expected the remaining sibling to sync");
2631 pthread_mutex_lock(&self
->mutex
);
2633 /* If remaining sibling didn't have a chance to wake up during
2634 * the first broadcast, manually reduce the num_waits now.
2636 if (self
->sibling
[sib
].num_waits
> 1)
2637 self
->sibling
[sib
].num_waits
= 1;
2638 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2639 TH_LOG("cond broadcast non-zero");
2641 pthread_mutex_unlock(&self
->mutex
);
2642 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2643 EXPECT_EQ(0, (long)status
);
2644 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2645 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2648 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2650 ASSERT_EQ(0, ret
); /* just us chickens */
2653 /* Make sure restarted syscalls are seen directly as "restart_syscall". */
2654 TEST(syscall_restart
)
2661 siginfo_t info
= { };
2662 struct sock_filter filter
[] = {
2663 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2664 offsetof(struct seccomp_data
, nr
)),
2666 #ifdef __NR_sigreturn
2667 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_sigreturn
, 6, 0),
2669 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 5, 0),
2670 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_exit
, 4, 0),
2671 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_rt_sigreturn
, 3, 0),
2672 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_nanosleep
, 4, 0),
2673 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_restart_syscall
, 4, 0),
2675 /* Allow __NR_write for easy logging. */
2676 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_write
, 0, 1),
2677 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2678 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2679 /* The nanosleep jump target. */
2680 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x100),
2681 /* The restart_syscall jump target. */
2682 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x200),
2684 struct sock_fprog prog
= {
2685 .len
= (unsigned short)ARRAY_SIZE(filter
),
2688 #if defined(__arm__)
2689 struct utsname utsbuf
;
2692 ASSERT_EQ(0, pipe(pipefd
));
2695 ASSERT_LE(0, child_pid
);
2696 if (child_pid
== 0) {
2697 /* Child uses EXPECT not ASSERT to deliver status correctly. */
2699 struct timespec timeout
= { };
2701 /* Attach parent as tracer and stop. */
2702 EXPECT_EQ(0, ptrace(PTRACE_TRACEME
));
2703 EXPECT_EQ(0, raise(SIGSTOP
));
2705 EXPECT_EQ(0, close(pipefd
[1]));
2707 EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2708 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2711 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2713 TH_LOG("Failed to install filter!");
2716 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2717 TH_LOG("Failed to read() sync from parent");
2719 EXPECT_EQ('.', buf
) {
2720 TH_LOG("Failed to get sync data from read()");
2723 /* Start nanosleep to be interrupted. */
2726 EXPECT_EQ(0, nanosleep(&timeout
, NULL
)) {
2727 TH_LOG("Call to nanosleep() failed (errno %d)", errno
);
2730 /* Read final sync from parent. */
2731 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2732 TH_LOG("Failed final read() from parent");
2734 EXPECT_EQ('!', buf
) {
2735 TH_LOG("Failed to get final data from read()");
2738 /* Directly report the status of our test harness results. */
2739 syscall(__NR_exit
, _metadata
->passed
? EXIT_SUCCESS
2742 EXPECT_EQ(0, close(pipefd
[0]));
2744 /* Attach to child, setup options, and release. */
2745 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2746 ASSERT_EQ(true, WIFSTOPPED(status
));
2747 ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS
, child_pid
, NULL
,
2748 PTRACE_O_TRACESECCOMP
));
2749 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2750 ASSERT_EQ(1, write(pipefd
[1], ".", 1));
2752 /* Wait for nanosleep() to start. */
2753 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2754 ASSERT_EQ(true, WIFSTOPPED(status
));
2755 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2756 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2757 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2758 ASSERT_EQ(0x100, msg
);
2759 EXPECT_EQ(__NR_nanosleep
, get_syscall(_metadata
, child_pid
));
2761 /* Might as well check siginfo for sanity while we're here. */
2762 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2763 ASSERT_EQ(SIGTRAP
, info
.si_signo
);
2764 ASSERT_EQ(SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8), info
.si_code
);
2765 EXPECT_EQ(0, info
.si_errno
);
2766 EXPECT_EQ(getuid(), info
.si_uid
);
2767 /* Verify signal delivery came from child (seccomp-triggered). */
2768 EXPECT_EQ(child_pid
, info
.si_pid
);
2770 /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */
2771 ASSERT_EQ(0, kill(child_pid
, SIGSTOP
));
2772 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2773 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2774 ASSERT_EQ(true, WIFSTOPPED(status
));
2775 ASSERT_EQ(SIGSTOP
, WSTOPSIG(status
));
2776 /* Verify signal delivery came from parent now. */
2777 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2778 EXPECT_EQ(getpid(), info
.si_pid
);
2780 /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */
2781 ASSERT_EQ(0, kill(child_pid
, SIGCONT
));
2782 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2783 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2784 ASSERT_EQ(true, WIFSTOPPED(status
));
2785 ASSERT_EQ(SIGCONT
, WSTOPSIG(status
));
2786 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2788 /* Wait for restart_syscall() to start. */
2789 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2790 ASSERT_EQ(true, WIFSTOPPED(status
));
2791 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2792 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2793 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2795 ASSERT_EQ(0x200, msg
);
2796 ret
= get_syscall(_metadata
, child_pid
);
2797 #if defined(__arm__)
2800 * - native ARM registers do NOT expose true syscall.
2801 * - compat ARM registers on ARM64 DO expose true syscall.
2803 ASSERT_EQ(0, uname(&utsbuf
));
2804 if (strncmp(utsbuf
.machine
, "arm", 3) == 0) {
2805 EXPECT_EQ(__NR_nanosleep
, ret
);
2809 EXPECT_EQ(__NR_restart_syscall
, ret
);
2812 /* Write again to end test. */
2813 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2814 ASSERT_EQ(1, write(pipefd
[1], "!", 1));
2815 EXPECT_EQ(0, close(pipefd
[1]));
2817 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2818 if (WIFSIGNALED(status
) || WEXITSTATUS(status
))
2819 _metadata
->passed
= 0;
2822 TEST_SIGNAL(filter_flag_log
, SIGSYS
)
2824 struct sock_filter allow_filter
[] = {
2825 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2827 struct sock_filter kill_filter
[] = {
2828 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2829 offsetof(struct seccomp_data
, nr
)),
2830 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
2831 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2832 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2834 struct sock_fprog allow_prog
= {
2835 .len
= (unsigned short)ARRAY_SIZE(allow_filter
),
2836 .filter
= allow_filter
,
2838 struct sock_fprog kill_prog
= {
2839 .len
= (unsigned short)ARRAY_SIZE(kill_filter
),
2840 .filter
= kill_filter
,
2843 pid_t parent
= getppid();
2845 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2848 /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */
2849 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, SECCOMP_FILTER_FLAG_LOG
,
2851 ASSERT_NE(ENOSYS
, errno
) {
2852 TH_LOG("Kernel does not support seccomp syscall!");
2855 TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!");
2857 EXPECT_EQ(EINVAL
, errno
) {
2858 TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!");
2861 /* Verify that a simple, permissive filter can be added with no flags */
2862 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &allow_prog
);
2865 /* See if the same filter can be added with the FILTER_FLAG_LOG flag */
2866 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
2868 ASSERT_NE(EINVAL
, errno
) {
2869 TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!");
2873 /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */
2874 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
2878 EXPECT_EQ(parent
, syscall(__NR_getppid
));
2879 /* getpid() should never return. */
2880 EXPECT_EQ(0, syscall(__NR_getpid
));
2883 TEST(get_action_avail
)
2885 __u32 actions
[] = { SECCOMP_RET_KILL_THREAD
, SECCOMP_RET_TRAP
,
2886 SECCOMP_RET_ERRNO
, SECCOMP_RET_TRACE
,
2887 SECCOMP_RET_LOG
, SECCOMP_RET_ALLOW
};
2888 __u32 unknown_action
= 0x10000000U
;
2892 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[0]);
2893 ASSERT_NE(ENOSYS
, errno
) {
2894 TH_LOG("Kernel does not support seccomp syscall!");
2896 ASSERT_NE(EINVAL
, errno
) {
2897 TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!");
2901 for (i
= 0; i
< ARRAY_SIZE(actions
); i
++) {
2902 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[i
]);
2904 TH_LOG("Expected action (0x%X) not available!",
2909 /* Check that an unknown action is handled properly (EOPNOTSUPP) */
2910 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &unknown_action
);
2912 EXPECT_EQ(errno
, EOPNOTSUPP
);
2920 struct seccomp_metadata md
;
2923 /* Only real root can get metadata. */
2925 XFAIL(return, "get_metadata requires real root");
2929 ASSERT_EQ(0, pipe(pipefd
));
2934 struct sock_filter filter
[] = {
2935 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2937 struct sock_fprog prog
= {
2938 .len
= (unsigned short)ARRAY_SIZE(filter
),
2942 /* one with log, one without */
2943 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
,
2944 SECCOMP_FILTER_FLAG_LOG
, &prog
));
2945 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
));
2947 ASSERT_EQ(0, close(pipefd
[0]));
2948 ASSERT_EQ(1, write(pipefd
[1], "1", 1));
2949 ASSERT_EQ(0, close(pipefd
[1]));
2955 ASSERT_EQ(0, close(pipefd
[1]));
2956 ASSERT_EQ(1, read(pipefd
[0], &buf
, 1));
2958 ASSERT_EQ(0, ptrace(PTRACE_ATTACH
, pid
));
2959 ASSERT_EQ(pid
, waitpid(pid
, NULL
, 0));
2961 /* Past here must not use ASSERT or child process is never killed. */
2965 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
2966 EXPECT_EQ(sizeof(md
), ret
) {
2967 if (errno
== EINVAL
)
2968 XFAIL(goto skip
, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)");
2971 EXPECT_EQ(md
.flags
, SECCOMP_FILTER_FLAG_LOG
);
2972 EXPECT_EQ(md
.filter_off
, 0);
2975 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
2976 EXPECT_EQ(sizeof(md
), ret
);
2977 EXPECT_EQ(md
.flags
, 0);
2978 EXPECT_EQ(md
.filter_off
, 1);
2981 ASSERT_EQ(0, kill(pid
, SIGKILL
));
2986 * - add microbenchmarks
2987 * - expand NNP testing
2988 * - better arch-specific TRACE and TRAP handlers.
2989 * - endianness checking when appropriate
2990 * - 64-bit arg prodding
2991 * - arch value testing (x86 modes especially)
2992 * - verify that FILTER_FLAG_LOG filters generate log messages
2993 * - verify that RET_LOG generates log messages