1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
5 * Test code for seccomp bpf.
12 * glibc 2.26 and later have SIGSYS in siginfo_t. Before that,
13 * we need to use the kernel's siginfo.h file and trick glibc
16 #if !__GLIBC_PREREQ(2, 26)
17 # include <asm/siginfo.h>
18 # define __have_siginfo_t 1
19 # define __have_sigval_t 1
20 # define __have_sigevent_t 1
24 #include <linux/filter.h>
25 #include <sys/prctl.h>
26 #include <sys/ptrace.h>
28 #include <linux/prctl.h>
29 #include <linux/ptrace.h>
30 #include <linux/seccomp.h>
32 #include <semaphore.h>
39 #include <linux/elf.h>
41 #include <sys/utsname.h>
42 #include <sys/fcntl.h>
44 #include <sys/times.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <linux/kcmp.h>
50 #include <sys/syscall.h>
53 #include "../kselftest_harness.h"
55 #ifndef PR_SET_PTRACER
56 # define PR_SET_PTRACER 0x59616d61
59 #ifndef PR_SET_NO_NEW_PRIVS
60 #define PR_SET_NO_NEW_PRIVS 38
61 #define PR_GET_NO_NEW_PRIVS 39
64 #ifndef PR_SECCOMP_EXT
65 #define PR_SECCOMP_EXT 43
68 #ifndef SECCOMP_EXT_ACT
69 #define SECCOMP_EXT_ACT 1
72 #ifndef SECCOMP_EXT_ACT_TSYNC
73 #define SECCOMP_EXT_ACT_TSYNC 1
76 #ifndef SECCOMP_MODE_STRICT
77 #define SECCOMP_MODE_STRICT 1
80 #ifndef SECCOMP_MODE_FILTER
81 #define SECCOMP_MODE_FILTER 2
84 #ifndef SECCOMP_RET_ALLOW
88 __u64 instruction_pointer
;
93 #ifndef SECCOMP_RET_KILL_PROCESS
94 #define SECCOMP_RET_KILL_PROCESS 0x80000000U /* kill the process */
95 #define SECCOMP_RET_KILL_THREAD 0x00000000U /* kill the thread */
97 #ifndef SECCOMP_RET_KILL
98 #define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD
99 #define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */
100 #define SECCOMP_RET_ERRNO 0x00050000U /* returns an errno */
101 #define SECCOMP_RET_TRACE 0x7ff00000U /* pass to a tracer or disallow */
102 #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
104 #ifndef SECCOMP_RET_LOG
105 #define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */
109 # if defined(__i386__)
110 # define __NR_seccomp 354
111 # elif defined(__x86_64__)
112 # define __NR_seccomp 317
113 # elif defined(__arm__)
114 # define __NR_seccomp 383
115 # elif defined(__aarch64__)
116 # define __NR_seccomp 277
117 # elif defined(__riscv)
118 # define __NR_seccomp 277
119 # elif defined(__hppa__)
120 # define __NR_seccomp 338
121 # elif defined(__powerpc__)
122 # define __NR_seccomp 358
123 # elif defined(__s390__)
124 # define __NR_seccomp 348
126 # warning "seccomp syscall number unknown for this architecture"
127 # define __NR_seccomp 0xffff
131 #ifndef SECCOMP_SET_MODE_STRICT
132 #define SECCOMP_SET_MODE_STRICT 0
135 #ifndef SECCOMP_SET_MODE_FILTER
136 #define SECCOMP_SET_MODE_FILTER 1
139 #ifndef SECCOMP_GET_ACTION_AVAIL
140 #define SECCOMP_GET_ACTION_AVAIL 2
143 #ifndef SECCOMP_GET_NOTIF_SIZES
144 #define SECCOMP_GET_NOTIF_SIZES 3
147 #ifndef SECCOMP_FILTER_FLAG_TSYNC
148 #define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
151 #ifndef SECCOMP_FILTER_FLAG_LOG
152 #define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
155 #ifndef SECCOMP_FILTER_FLAG_SPEC_ALLOW
156 #define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
159 #ifndef PTRACE_SECCOMP_GET_METADATA
160 #define PTRACE_SECCOMP_GET_METADATA 0x420d
162 struct seccomp_metadata
{
163 __u64 filter_off
; /* Input: which filter */
164 __u64 flags
; /* Output: filter's flags */
168 #ifndef SECCOMP_FILTER_FLAG_NEW_LISTENER
169 #define SECCOMP_FILTER_FLAG_NEW_LISTENER (1UL << 3)
171 #define SECCOMP_RET_USER_NOTIF 0x7fc00000U
173 #define SECCOMP_IOC_MAGIC '!'
174 #define SECCOMP_IO(nr) _IO(SECCOMP_IOC_MAGIC, nr)
175 #define SECCOMP_IOR(nr, type) _IOR(SECCOMP_IOC_MAGIC, nr, type)
176 #define SECCOMP_IOW(nr, type) _IOW(SECCOMP_IOC_MAGIC, nr, type)
177 #define SECCOMP_IOWR(nr, type) _IOWR(SECCOMP_IOC_MAGIC, nr, type)
179 /* Flags for seccomp notification fd ioctl. */
180 #define SECCOMP_IOCTL_NOTIF_RECV SECCOMP_IOWR(0, struct seccomp_notif)
181 #define SECCOMP_IOCTL_NOTIF_SEND SECCOMP_IOWR(1, \
182 struct seccomp_notif_resp)
183 #define SECCOMP_IOCTL_NOTIF_ID_VALID SECCOMP_IOR(2, __u64)
185 struct seccomp_notif
{
189 struct seccomp_data data
;
192 struct seccomp_notif_resp
{
199 struct seccomp_notif_sizes
{
201 __u16 seccomp_notif_resp
;
206 #ifndef PTRACE_EVENTMSG_SYSCALL_ENTRY
207 #define PTRACE_EVENTMSG_SYSCALL_ENTRY 1
208 #define PTRACE_EVENTMSG_SYSCALL_EXIT 2
211 #ifndef SECCOMP_USER_NOTIF_FLAG_CONTINUE
212 #define SECCOMP_USER_NOTIF_FLAG_CONTINUE 0x00000001
215 #ifndef SECCOMP_FILTER_FLAG_TSYNC_ESRCH
216 #define SECCOMP_FILTER_FLAG_TSYNC_ESRCH (1UL << 4)
220 int seccomp(unsigned int op
, unsigned int flags
, void *args
)
223 return syscall(__NR_seccomp
, op
, flags
, args
);
227 #if __BYTE_ORDER == __LITTLE_ENDIAN
228 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
229 #elif __BYTE_ORDER == __BIG_ENDIAN
230 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]) + sizeof(__u32))
232 #error "wut? Unknown __BYTE_ORDER?!"
235 #define SIBLING_EXIT_UNKILLED 0xbadbeef
236 #define SIBLING_EXIT_FAILURE 0xbadface
237 #define SIBLING_EXIT_NEWPRIVS 0xbadfeed
239 TEST(mode_strict_support
)
243 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
245 TH_LOG("Kernel does not support CONFIG_SECCOMP");
247 syscall(__NR_exit
, 0);
250 TEST_SIGNAL(mode_strict_cannot_call_prctl
, SIGKILL
)
254 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
256 TH_LOG("Kernel does not support CONFIG_SECCOMP");
258 syscall(__NR_prctl
, PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
261 TH_LOG("Unreachable!");
265 /* Note! This doesn't test no new privs behavior */
266 TEST(no_new_privs_support
)
270 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
272 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
276 /* Tests kernel support by checking for a copy_from_user() fault on NULL. */
277 TEST(mode_filter_support
)
281 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
283 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
285 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, NULL
, NULL
);
287 EXPECT_EQ(EFAULT
, errno
) {
288 TH_LOG("Kernel does not support CONFIG_SECCOMP_FILTER!");
292 TEST(mode_filter_without_nnp
)
294 struct sock_filter filter
[] = {
295 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
297 struct sock_fprog prog
= {
298 .len
= (unsigned short)ARRAY_SIZE(filter
),
303 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, NULL
, 0, 0);
305 TH_LOG("Expected 0 or unsupported for NO_NEW_PRIVS");
308 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
309 /* Succeeds with CAP_SYS_ADMIN, fails without */
310 /* TODO(wad) check caps not euid */
313 EXPECT_EQ(EACCES
, errno
);
319 #define MAX_INSNS_PER_PATH 32768
321 TEST(filter_size_limits
)
324 int count
= BPF_MAXINSNS
+ 1;
325 struct sock_filter allow
[] = {
326 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
328 struct sock_filter
*filter
;
329 struct sock_fprog prog
= { };
332 filter
= calloc(count
, sizeof(*filter
));
333 ASSERT_NE(NULL
, filter
);
335 for (i
= 0; i
< count
; i
++)
336 filter
[i
] = allow
[0];
338 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
341 prog
.filter
= filter
;
344 /* Too many filter instructions in a single filter. */
345 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
347 TH_LOG("Installing %d insn filter was allowed", prog
.len
);
350 /* One less is okay, though. */
352 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
354 TH_LOG("Installing %d insn filter wasn't allowed", prog
.len
);
358 TEST(filter_chain_limits
)
361 int count
= BPF_MAXINSNS
;
362 struct sock_filter allow
[] = {
363 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
365 struct sock_filter
*filter
;
366 struct sock_fprog prog
= { };
369 filter
= calloc(count
, sizeof(*filter
));
370 ASSERT_NE(NULL
, filter
);
372 for (i
= 0; i
< count
; i
++)
373 filter
[i
] = allow
[0];
375 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
378 prog
.filter
= filter
;
381 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
386 /* Too many total filter instructions. */
387 for (i
= 0; i
< MAX_INSNS_PER_PATH
; i
++) {
388 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
393 TH_LOG("Allowed %d %d-insn filters (total with penalties:%d)",
394 i
, count
, i
* (count
+ 4));
398 TEST(mode_filter_cannot_move_to_strict
)
400 struct sock_filter filter
[] = {
401 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
403 struct sock_fprog prog
= {
404 .len
= (unsigned short)ARRAY_SIZE(filter
),
409 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
412 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
415 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, 0, 0);
417 EXPECT_EQ(EINVAL
, errno
);
421 TEST(mode_filter_get_seccomp
)
423 struct sock_filter filter
[] = {
424 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
426 struct sock_fprog prog
= {
427 .len
= (unsigned short)ARRAY_SIZE(filter
),
432 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
435 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
438 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
441 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
448 struct sock_filter filter
[] = {
449 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
451 struct sock_fprog prog
= {
452 .len
= (unsigned short)ARRAY_SIZE(filter
),
457 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
460 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
466 struct sock_filter filter
[] = {
468 struct sock_fprog prog
= {
469 .len
= (unsigned short)ARRAY_SIZE(filter
),
474 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
477 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
479 EXPECT_EQ(EINVAL
, errno
);
484 struct sock_filter filter
[] = {
485 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_LOG
),
487 struct sock_fprog prog
= {
488 .len
= (unsigned short)ARRAY_SIZE(filter
),
492 pid_t parent
= getppid();
494 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
497 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
500 /* getppid() should succeed and be logged (no check for logging) */
501 EXPECT_EQ(parent
, syscall(__NR_getppid
));
504 TEST_SIGNAL(unknown_ret_is_kill_inside
, SIGSYS
)
506 struct sock_filter filter
[] = {
507 BPF_STMT(BPF_RET
|BPF_K
, 0x10000000U
),
509 struct sock_fprog prog
= {
510 .len
= (unsigned short)ARRAY_SIZE(filter
),
515 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
518 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
520 EXPECT_EQ(0, syscall(__NR_getpid
)) {
521 TH_LOG("getpid() shouldn't ever return");
525 /* return code >= 0x80000000 is unused. */
526 TEST_SIGNAL(unknown_ret_is_kill_above_allow
, SIGSYS
)
528 struct sock_filter filter
[] = {
529 BPF_STMT(BPF_RET
|BPF_K
, 0x90000000U
),
531 struct sock_fprog prog
= {
532 .len
= (unsigned short)ARRAY_SIZE(filter
),
537 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
540 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
542 EXPECT_EQ(0, syscall(__NR_getpid
)) {
543 TH_LOG("getpid() shouldn't ever return");
547 TEST_SIGNAL(KILL_all
, SIGSYS
)
549 struct sock_filter filter
[] = {
550 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
552 struct sock_fprog prog
= {
553 .len
= (unsigned short)ARRAY_SIZE(filter
),
558 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
561 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
565 TEST_SIGNAL(KILL_one
, SIGSYS
)
567 struct sock_filter filter
[] = {
568 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
569 offsetof(struct seccomp_data
, nr
)),
570 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
571 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
572 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
574 struct sock_fprog prog
= {
575 .len
= (unsigned short)ARRAY_SIZE(filter
),
579 pid_t parent
= getppid();
581 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
584 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
587 EXPECT_EQ(parent
, syscall(__NR_getppid
));
588 /* getpid() should never return. */
589 EXPECT_EQ(0, syscall(__NR_getpid
));
592 TEST_SIGNAL(KILL_one_arg_one
, SIGSYS
)
595 struct sock_filter filter
[] = {
596 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
597 offsetof(struct seccomp_data
, nr
)),
598 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_times
, 1, 0),
599 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
600 /* Only both with lower 32-bit for now. */
601 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(0)),
602 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
,
603 (unsigned long)&fatal_address
, 0, 1),
604 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
605 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
607 struct sock_fprog prog
= {
608 .len
= (unsigned short)ARRAY_SIZE(filter
),
612 pid_t parent
= getppid();
614 clock_t clock
= times(&timebuf
);
616 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
619 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
622 EXPECT_EQ(parent
, syscall(__NR_getppid
));
623 EXPECT_LE(clock
, syscall(__NR_times
, &timebuf
));
624 /* times() should never return. */
625 EXPECT_EQ(0, syscall(__NR_times
, &fatal_address
));
628 TEST_SIGNAL(KILL_one_arg_six
, SIGSYS
)
631 int sysno
= __NR_mmap
;
633 int sysno
= __NR_mmap2
;
635 struct sock_filter filter
[] = {
636 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
637 offsetof(struct seccomp_data
, nr
)),
638 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, sysno
, 1, 0),
639 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
640 /* Only both with lower 32-bit for now. */
641 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(5)),
642 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, 0x0C0FFEE, 0, 1),
643 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
644 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
646 struct sock_fprog prog
= {
647 .len
= (unsigned short)ARRAY_SIZE(filter
),
651 pid_t parent
= getppid();
654 int page_size
= sysconf(_SC_PAGESIZE
);
656 ASSERT_LT(0, page_size
);
658 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
661 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
664 fd
= open("/dev/zero", O_RDONLY
);
667 EXPECT_EQ(parent
, syscall(__NR_getppid
));
668 map1
= (void *)syscall(sysno
,
669 NULL
, page_size
, PROT_READ
, MAP_PRIVATE
, fd
, page_size
);
670 EXPECT_NE(MAP_FAILED
, map1
);
671 /* mmap2() should never return. */
672 map2
= (void *)syscall(sysno
,
673 NULL
, page_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0x0C0FFEE);
674 EXPECT_EQ(MAP_FAILED
, map2
);
676 /* The test failed, so clean up the resources. */
677 munmap(map1
, page_size
);
678 munmap(map2
, page_size
);
682 /* This is a thread task to die via seccomp filter violation. */
683 void *kill_thread(void *data
)
685 bool die
= (bool)data
;
688 prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
689 return (void *)SIBLING_EXIT_FAILURE
;
692 return (void *)SIBLING_EXIT_UNKILLED
;
695 /* Prepare a thread that will kill itself or both of us. */
696 void kill_thread_or_group(struct __test_metadata
*_metadata
, bool kill_process
)
700 /* Kill only when calling __NR_prctl. */
701 struct sock_filter filter_thread
[] = {
702 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
703 offsetof(struct seccomp_data
, nr
)),
704 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
705 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL_THREAD
),
706 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
708 struct sock_fprog prog_thread
= {
709 .len
= (unsigned short)ARRAY_SIZE(filter_thread
),
710 .filter
= filter_thread
,
712 struct sock_filter filter_process
[] = {
713 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
714 offsetof(struct seccomp_data
, nr
)),
715 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
716 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL_PROCESS
),
717 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
719 struct sock_fprog prog_process
= {
720 .len
= (unsigned short)ARRAY_SIZE(filter_process
),
721 .filter
= filter_process
,
724 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
725 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
728 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0,
729 kill_process
? &prog_process
: &prog_thread
));
732 * Add the KILL_THREAD rule again to make sure that the KILL_PROCESS
733 * flag cannot be downgraded by a new filter.
735 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog_thread
));
737 /* Start a thread that will exit immediately. */
738 ASSERT_EQ(0, pthread_create(&thread
, NULL
, kill_thread
, (void *)false));
739 ASSERT_EQ(0, pthread_join(thread
, &status
));
740 ASSERT_EQ(SIBLING_EXIT_UNKILLED
, (unsigned long)status
);
742 /* Start a thread that will die immediately. */
743 ASSERT_EQ(0, pthread_create(&thread
, NULL
, kill_thread
, (void *)true));
744 ASSERT_EQ(0, pthread_join(thread
, &status
));
745 ASSERT_NE(SIBLING_EXIT_FAILURE
, (unsigned long)status
);
748 * If we get here, only the spawned thread died. Let the parent know
749 * the whole process didn't die (i.e. this thread, the spawner,
761 ASSERT_LE(0, child_pid
);
762 if (child_pid
== 0) {
763 kill_thread_or_group(_metadata
, false);
767 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
769 /* If only the thread was killed, we'll see exit 42. */
770 ASSERT_TRUE(WIFEXITED(status
));
771 ASSERT_EQ(42, WEXITSTATUS(status
));
780 ASSERT_LE(0, child_pid
);
781 if (child_pid
== 0) {
782 kill_thread_or_group(_metadata
, true);
786 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
788 /* If the entire process was killed, we'll see SIGSYS. */
789 ASSERT_TRUE(WIFSIGNALED(status
));
790 ASSERT_EQ(SIGSYS
, WTERMSIG(status
));
793 /* TODO(wad) add 64-bit versus 32-bit arg tests. */
794 TEST(arg_out_of_range
)
796 struct sock_filter filter
[] = {
797 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(6)),
798 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
800 struct sock_fprog prog
= {
801 .len
= (unsigned short)ARRAY_SIZE(filter
),
806 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
809 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
811 EXPECT_EQ(EINVAL
, errno
);
814 #define ERRNO_FILTER(name, errno) \
815 struct sock_filter _read_filter_##name[] = { \
816 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, \
817 offsetof(struct seccomp_data, nr)), \
818 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), \
819 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | errno), \
820 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), \
822 struct sock_fprog prog_##name = { \
823 .len = (unsigned short)ARRAY_SIZE(_read_filter_##name), \
824 .filter = _read_filter_##name, \
827 /* Make sure basic errno values are correctly passed through a filter. */
830 ERRNO_FILTER(valid
, E2BIG
);
832 pid_t parent
= getppid();
834 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
837 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_valid
);
840 EXPECT_EQ(parent
, syscall(__NR_getppid
));
841 EXPECT_EQ(-1, read(0, NULL
, 0));
842 EXPECT_EQ(E2BIG
, errno
);
845 /* Make sure an errno of zero is correctly handled by the arch code. */
848 ERRNO_FILTER(zero
, 0);
850 pid_t parent
= getppid();
852 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
855 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_zero
);
858 EXPECT_EQ(parent
, syscall(__NR_getppid
));
859 /* "errno" of 0 is ok. */
860 EXPECT_EQ(0, read(0, NULL
, 0));
864 * The SECCOMP_RET_DATA mask is 16 bits wide, but errno is smaller.
865 * This tests that the errno value gets capped correctly, fixed by
866 * 580c57f10768 ("seccomp: cap SECCOMP_RET_ERRNO data to MAX_ERRNO").
870 ERRNO_FILTER(capped
, 4096);
872 pid_t parent
= getppid();
874 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
877 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_capped
);
880 EXPECT_EQ(parent
, syscall(__NR_getppid
));
881 EXPECT_EQ(-1, read(0, NULL
, 0));
882 EXPECT_EQ(4095, errno
);
886 * Filters are processed in reverse order: last applied is executed first.
887 * Since only the SECCOMP_RET_ACTION mask is tested for return values, the
888 * SECCOMP_RET_DATA mask results will follow the most recently applied
889 * matching filter return (and not the lowest or highest value).
893 ERRNO_FILTER(first
, 11);
894 ERRNO_FILTER(second
, 13);
895 ERRNO_FILTER(third
, 12);
897 pid_t parent
= getppid();
899 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
902 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_first
);
905 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_second
);
908 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_third
);
911 EXPECT_EQ(parent
, syscall(__NR_getppid
));
912 EXPECT_EQ(-1, read(0, NULL
, 0));
913 EXPECT_EQ(12, errno
);
917 struct sock_fprog prog
;
922 struct sock_filter filter
[] = {
923 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
924 offsetof(struct seccomp_data
, nr
)),
925 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
926 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRAP
),
927 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
930 memset(&self
->prog
, 0, sizeof(self
->prog
));
931 self
->prog
.filter
= malloc(sizeof(filter
));
932 ASSERT_NE(NULL
, self
->prog
.filter
);
933 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
934 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
937 FIXTURE_TEARDOWN(TRAP
)
939 if (self
->prog
.filter
)
940 free(self
->prog
.filter
);
943 TEST_F_SIGNAL(TRAP
, dfl
, SIGSYS
)
947 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
950 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
952 syscall(__NR_getpid
);
955 /* Ensure that SIGSYS overrides SIG_IGN */
956 TEST_F_SIGNAL(TRAP
, ign
, SIGSYS
)
960 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
963 signal(SIGSYS
, SIG_IGN
);
965 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
967 syscall(__NR_getpid
);
970 static siginfo_t TRAP_info
;
971 static volatile int TRAP_nr
;
972 static void TRAP_action(int nr
, siginfo_t
*info
, void *void_context
)
974 memcpy(&TRAP_info
, info
, sizeof(TRAP_info
));
978 TEST_F(TRAP
, handler
)
981 struct sigaction act
;
984 memset(&act
, 0, sizeof(act
));
986 sigaddset(&mask
, SIGSYS
);
988 act
.sa_sigaction
= &TRAP_action
;
989 act
.sa_flags
= SA_SIGINFO
;
990 ret
= sigaction(SIGSYS
, &act
, NULL
);
992 TH_LOG("sigaction failed");
994 ret
= sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
996 TH_LOG("sigprocmask failed");
999 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1001 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
1004 memset(&TRAP_info
, 0, sizeof(TRAP_info
));
1005 /* Expect the registers to be rolled back. (nr = error) may vary
1007 ret
= syscall(__NR_getpid
);
1008 /* Silence gcc warning about volatile. */
1010 EXPECT_EQ(SIGSYS
, test
);
1011 struct local_sigsys
{
1012 void *_call_addr
; /* calling user insn */
1013 int _syscall
; /* triggering system call number */
1014 unsigned int _arch
; /* AUDIT_ARCH_* of syscall */
1015 } *sigsys
= (struct local_sigsys
*)
1017 &(TRAP_info
.si_call_addr
);
1021 EXPECT_EQ(__NR_getpid
, sigsys
->_syscall
);
1022 /* Make sure arch is non-zero. */
1023 EXPECT_NE(0, sigsys
->_arch
);
1024 EXPECT_NE(0, (unsigned long)sigsys
->_call_addr
);
1027 FIXTURE(precedence
) {
1028 struct sock_fprog allow
;
1029 struct sock_fprog log
;
1030 struct sock_fprog trace
;
1031 struct sock_fprog error
;
1032 struct sock_fprog trap
;
1033 struct sock_fprog kill
;
1036 FIXTURE_SETUP(precedence
)
1038 struct sock_filter allow_insns
[] = {
1039 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1041 struct sock_filter log_insns
[] = {
1042 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1043 offsetof(struct seccomp_data
, nr
)),
1044 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1045 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1046 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_LOG
),
1048 struct sock_filter trace_insns
[] = {
1049 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1050 offsetof(struct seccomp_data
, nr
)),
1051 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1052 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1053 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
),
1055 struct sock_filter error_insns
[] = {
1056 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1057 offsetof(struct seccomp_data
, nr
)),
1058 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1059 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1060 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
),
1062 struct sock_filter trap_insns
[] = {
1063 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1064 offsetof(struct seccomp_data
, nr
)),
1065 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1066 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1067 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRAP
),
1069 struct sock_filter kill_insns
[] = {
1070 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1071 offsetof(struct seccomp_data
, nr
)),
1072 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1073 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1074 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1077 memset(self
, 0, sizeof(*self
));
1078 #define FILTER_ALLOC(_x) \
1079 self->_x.filter = malloc(sizeof(_x##_insns)); \
1080 ASSERT_NE(NULL, self->_x.filter); \
1081 memcpy(self->_x.filter, &_x##_insns, sizeof(_x##_insns)); \
1082 self->_x.len = (unsigned short)ARRAY_SIZE(_x##_insns)
1083 FILTER_ALLOC(allow
);
1085 FILTER_ALLOC(trace
);
1086 FILTER_ALLOC(error
);
1091 FIXTURE_TEARDOWN(precedence
)
1093 #define FILTER_FREE(_x) if (self->_x.filter) free(self->_x.filter)
1102 TEST_F(precedence
, allow_ok
)
1104 pid_t parent
, res
= 0;
1108 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1111 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1113 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1115 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1117 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1119 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1121 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1123 /* Should work just fine. */
1124 res
= syscall(__NR_getppid
);
1125 EXPECT_EQ(parent
, res
);
1128 TEST_F_SIGNAL(precedence
, kill_is_highest
, SIGSYS
)
1130 pid_t parent
, res
= 0;
1134 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1137 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1139 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1141 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1143 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1145 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1147 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1149 /* Should work just fine. */
1150 res
= syscall(__NR_getppid
);
1151 EXPECT_EQ(parent
, res
);
1152 /* getpid() should never return. */
1153 res
= syscall(__NR_getpid
);
1157 TEST_F_SIGNAL(precedence
, kill_is_highest_in_any_order
, SIGSYS
)
1163 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1166 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1168 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1170 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1172 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1174 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1176 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1178 /* Should work just fine. */
1179 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1180 /* getpid() should never return. */
1181 EXPECT_EQ(0, syscall(__NR_getpid
));
1184 TEST_F_SIGNAL(precedence
, trap_is_second
, SIGSYS
)
1190 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1193 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1195 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1197 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1199 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1201 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1203 /* Should work just fine. */
1204 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1205 /* getpid() should never return. */
1206 EXPECT_EQ(0, syscall(__NR_getpid
));
1209 TEST_F_SIGNAL(precedence
, trap_is_second_in_any_order
, SIGSYS
)
1215 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1218 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1220 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1222 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1224 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1226 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1228 /* Should work just fine. */
1229 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1230 /* getpid() should never return. */
1231 EXPECT_EQ(0, syscall(__NR_getpid
));
1234 TEST_F(precedence
, errno_is_third
)
1240 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1243 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1245 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1247 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1249 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1251 /* Should work just fine. */
1252 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1253 EXPECT_EQ(0, syscall(__NR_getpid
));
1256 TEST_F(precedence
, errno_is_third_in_any_order
)
1262 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1265 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1267 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1269 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1271 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1273 /* Should work just fine. */
1274 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1275 EXPECT_EQ(0, syscall(__NR_getpid
));
1278 TEST_F(precedence
, trace_is_fourth
)
1284 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1287 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1289 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1291 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1293 /* Should work just fine. */
1294 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1296 EXPECT_EQ(-1, syscall(__NR_getpid
));
1299 TEST_F(precedence
, trace_is_fourth_in_any_order
)
1305 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1308 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1310 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1312 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1314 /* Should work just fine. */
1315 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1317 EXPECT_EQ(-1, syscall(__NR_getpid
));
1320 TEST_F(precedence
, log_is_fifth
)
1322 pid_t mypid
, parent
;
1327 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1330 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1332 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1334 /* Should work just fine. */
1335 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1336 /* Should also work just fine */
1337 EXPECT_EQ(mypid
, syscall(__NR_getpid
));
1340 TEST_F(precedence
, log_is_fifth_in_any_order
)
1342 pid_t mypid
, parent
;
1347 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1350 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1352 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1354 /* Should work just fine. */
1355 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1356 /* Should also work just fine */
1357 EXPECT_EQ(mypid
, syscall(__NR_getpid
));
1360 #ifndef PTRACE_O_TRACESECCOMP
1361 #define PTRACE_O_TRACESECCOMP 0x00000080
1364 /* Catch the Ubuntu 12.04 value error. */
1365 #if PTRACE_EVENT_SECCOMP != 7
1366 #undef PTRACE_EVENT_SECCOMP
1369 #ifndef PTRACE_EVENT_SECCOMP
1370 #define PTRACE_EVENT_SECCOMP 7
1373 #define IS_SECCOMP_EVENT(status) ((status >> 16) == PTRACE_EVENT_SECCOMP)
1374 bool tracer_running
;
1375 void tracer_stop(int sig
)
1377 tracer_running
= false;
1380 typedef void tracer_func_t(struct __test_metadata
*_metadata
,
1381 pid_t tracee
, int status
, void *args
);
1383 void start_tracer(struct __test_metadata
*_metadata
, int fd
, pid_t tracee
,
1384 tracer_func_t tracer_func
, void *args
, bool ptrace_syscall
)
1387 struct sigaction action
= {
1388 .sa_handler
= tracer_stop
,
1391 /* Allow external shutdown. */
1392 tracer_running
= true;
1393 ASSERT_EQ(0, sigaction(SIGUSR1
, &action
, NULL
));
1396 while (ret
== -1 && errno
!= EINVAL
)
1397 ret
= ptrace(PTRACE_ATTACH
, tracee
, NULL
, 0);
1399 kill(tracee
, SIGKILL
);
1401 /* Wait for attach stop */
1404 ret
= ptrace(PTRACE_SETOPTIONS
, tracee
, NULL
, ptrace_syscall
?
1405 PTRACE_O_TRACESYSGOOD
:
1406 PTRACE_O_TRACESECCOMP
);
1408 TH_LOG("Failed to set PTRACE_O_TRACESECCOMP");
1409 kill(tracee
, SIGKILL
);
1411 ret
= ptrace(ptrace_syscall
? PTRACE_SYSCALL
: PTRACE_CONT
,
1415 /* Unblock the tracee */
1416 ASSERT_EQ(1, write(fd
, "A", 1));
1417 ASSERT_EQ(0, close(fd
));
1419 /* Run until we're shut down. Must assert to stop execution. */
1420 while (tracer_running
) {
1423 if (wait(&status
) != tracee
)
1425 if (WIFSIGNALED(status
) || WIFEXITED(status
))
1426 /* Child is dead. Time to go. */
1429 /* Check if this is a seccomp event. */
1430 ASSERT_EQ(!ptrace_syscall
, IS_SECCOMP_EVENT(status
));
1432 tracer_func(_metadata
, tracee
, status
, args
);
1434 ret
= ptrace(ptrace_syscall
? PTRACE_SYSCALL
: PTRACE_CONT
,
1438 /* Directly report the status of our test harness results. */
1439 syscall(__NR_exit
, _metadata
->passed
? EXIT_SUCCESS
: EXIT_FAILURE
);
1442 /* Common tracer setup/teardown functions. */
1443 void cont_handler(int num
)
1445 pid_t
setup_trace_fixture(struct __test_metadata
*_metadata
,
1446 tracer_func_t func
, void *args
, bool ptrace_syscall
)
1451 pid_t tracee
= getpid();
1453 /* Setup a pipe for clean synchronization. */
1454 ASSERT_EQ(0, pipe(pipefd
));
1456 /* Fork a child which we'll promote to tracer */
1457 tracer_pid
= fork();
1458 ASSERT_LE(0, tracer_pid
);
1459 signal(SIGALRM
, cont_handler
);
1460 if (tracer_pid
== 0) {
1462 start_tracer(_metadata
, pipefd
[1], tracee
, func
, args
,
1464 syscall(__NR_exit
, 0);
1467 prctl(PR_SET_PTRACER
, tracer_pid
, 0, 0, 0);
1468 read(pipefd
[0], &sync
, 1);
1473 void teardown_trace_fixture(struct __test_metadata
*_metadata
,
1479 * Extract the exit code from the other process and
1480 * adopt it for ourselves in case its asserts failed.
1482 ASSERT_EQ(0, kill(tracer
, SIGUSR1
));
1483 ASSERT_EQ(tracer
, waitpid(tracer
, &status
, 0));
1484 if (WEXITSTATUS(status
))
1485 _metadata
->passed
= 0;
1489 /* "poke" tracer arguments and function. */
1490 struct tracer_args_poke_t
{
1491 unsigned long poke_addr
;
1494 void tracer_poke(struct __test_metadata
*_metadata
, pid_t tracee
, int status
,
1499 struct tracer_args_poke_t
*info
= (struct tracer_args_poke_t
*)args
;
1501 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1503 /* If this fails, don't try to recover. */
1504 ASSERT_EQ(0x1001, msg
) {
1505 kill(tracee
, SIGKILL
);
1508 * Poke in the message.
1509 * Registers are not touched to try to keep this relatively arch
1512 ret
= ptrace(PTRACE_POKEDATA
, tracee
, info
->poke_addr
, 0x1001);
1516 FIXTURE(TRACE_poke
) {
1517 struct sock_fprog prog
;
1520 struct tracer_args_poke_t tracer_args
;
1523 FIXTURE_SETUP(TRACE_poke
)
1525 struct sock_filter filter
[] = {
1526 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1527 offsetof(struct seccomp_data
, nr
)),
1528 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
1529 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1001),
1530 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1534 memset(&self
->prog
, 0, sizeof(self
->prog
));
1535 self
->prog
.filter
= malloc(sizeof(filter
));
1536 ASSERT_NE(NULL
, self
->prog
.filter
);
1537 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1538 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1540 /* Set up tracer args. */
1541 self
->tracer_args
.poke_addr
= (unsigned long)&self
->poked
;
1543 /* Launch tracer. */
1544 self
->tracer
= setup_trace_fixture(_metadata
, tracer_poke
,
1545 &self
->tracer_args
, false);
1548 FIXTURE_TEARDOWN(TRACE_poke
)
1550 teardown_trace_fixture(_metadata
, self
->tracer
);
1551 if (self
->prog
.filter
)
1552 free(self
->prog
.filter
);
1555 TEST_F(TRACE_poke
, read_has_side_effects
)
1559 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1562 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1565 EXPECT_EQ(0, self
->poked
);
1566 ret
= read(-1, NULL
, 0);
1568 EXPECT_EQ(0x1001, self
->poked
);
1571 TEST_F(TRACE_poke
, getpid_runs_normally
)
1575 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1578 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1581 EXPECT_EQ(0, self
->poked
);
1582 EXPECT_NE(0, syscall(__NR_getpid
));
1583 EXPECT_EQ(0, self
->poked
);
1586 #if defined(__x86_64__)
1587 # define ARCH_REGS struct user_regs_struct
1588 # define SYSCALL_NUM orig_rax
1589 # define SYSCALL_RET rax
1590 #elif defined(__i386__)
1591 # define ARCH_REGS struct user_regs_struct
1592 # define SYSCALL_NUM orig_eax
1593 # define SYSCALL_RET eax
1594 #elif defined(__arm__)
1595 # define ARCH_REGS struct pt_regs
1596 # define SYSCALL_NUM ARM_r7
1597 # define SYSCALL_RET ARM_r0
1598 #elif defined(__aarch64__)
1599 # define ARCH_REGS struct user_pt_regs
1600 # define SYSCALL_NUM regs[8]
1601 # define SYSCALL_RET regs[0]
1602 #elif defined(__riscv) && __riscv_xlen == 64
1603 # define ARCH_REGS struct user_regs_struct
1604 # define SYSCALL_NUM a7
1605 # define SYSCALL_RET a0
1606 #elif defined(__hppa__)
1607 # define ARCH_REGS struct user_regs_struct
1608 # define SYSCALL_NUM gr[20]
1609 # define SYSCALL_RET gr[28]
1610 #elif defined(__powerpc__)
1611 # define ARCH_REGS struct pt_regs
1612 # define SYSCALL_NUM gpr[0]
1613 # define SYSCALL_RET gpr[3]
1614 #elif defined(__s390__)
1615 # define ARCH_REGS s390_regs
1616 # define SYSCALL_NUM gprs[2]
1617 # define SYSCALL_RET gprs[2]
1618 #elif defined(__mips__)
1619 # define ARCH_REGS struct pt_regs
1620 # define SYSCALL_NUM regs[2]
1621 # define SYSCALL_SYSCALL_NUM regs[4]
1622 # define SYSCALL_RET regs[2]
1623 # define SYSCALL_NUM_RET_SHARE_REG
1625 # error "Do not know how to find your architecture's registers and syscalls"
1628 /* When the syscall return can't be changed, stub out the tests for it. */
1629 #ifdef SYSCALL_NUM_RET_SHARE_REG
1630 # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(-1, action)
1632 # define EXPECT_SYSCALL_RETURN(val, action) \
1636 EXPECT_EQ(-1, action); \
1637 EXPECT_EQ(-(val), errno); \
1639 EXPECT_EQ(val, action); \
1644 /* Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for
1645 * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux).
1647 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__)
1648 #define HAVE_GETREGS
1651 /* Architecture-specific syscall fetching routine. */
1652 int get_syscall(struct __test_metadata
*_metadata
, pid_t tracee
)
1656 EXPECT_EQ(0, ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
)) {
1657 TH_LOG("PTRACE_GETREGS failed");
1663 iov
.iov_base
= ®s
;
1664 iov
.iov_len
= sizeof(regs
);
1665 EXPECT_EQ(0, ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
)) {
1666 TH_LOG("PTRACE_GETREGSET failed");
1671 #if defined(__mips__)
1672 if (regs
.SYSCALL_NUM
== __NR_O32_Linux
)
1673 return regs
.SYSCALL_SYSCALL_NUM
;
1675 return regs
.SYSCALL_NUM
;
1678 /* Architecture-specific syscall changing routine. */
1679 void change_syscall(struct __test_metadata
*_metadata
,
1680 pid_t tracee
, int syscall
, int result
)
1685 ret
= ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
);
1688 iov
.iov_base
= ®s
;
1689 iov
.iov_len
= sizeof(regs
);
1690 ret
= ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1692 EXPECT_EQ(0, ret
) {}
1694 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \
1695 defined(__s390__) || defined(__hppa__) || defined(__riscv)
1697 regs
.SYSCALL_NUM
= syscall
;
1699 #elif defined(__mips__)
1701 if (regs
.SYSCALL_NUM
== __NR_O32_Linux
)
1702 regs
.SYSCALL_SYSCALL_NUM
= syscall
;
1704 regs
.SYSCALL_NUM
= syscall
;
1707 #elif defined(__arm__)
1708 # ifndef PTRACE_SET_SYSCALL
1709 # define PTRACE_SET_SYSCALL 23
1712 ret
= ptrace(PTRACE_SET_SYSCALL
, tracee
, NULL
, syscall
);
1716 #elif defined(__aarch64__)
1717 # ifndef NT_ARM_SYSTEM_CALL
1718 # define NT_ARM_SYSTEM_CALL 0x404
1721 iov
.iov_base
= &syscall
;
1722 iov
.iov_len
= sizeof(syscall
);
1723 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_ARM_SYSTEM_CALL
,
1730 TH_LOG("How is the syscall changed on this architecture?");
1734 /* If syscall is skipped, change return value. */
1736 #ifdef SYSCALL_NUM_RET_SHARE_REG
1737 TH_LOG("Can't modify syscall return on this architecture");
1739 regs
.SYSCALL_RET
= result
;
1743 ret
= ptrace(PTRACE_SETREGS
, tracee
, 0, ®s
);
1745 iov
.iov_base
= ®s
;
1746 iov
.iov_len
= sizeof(regs
);
1747 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1752 void tracer_syscall(struct __test_metadata
*_metadata
, pid_t tracee
,
1753 int status
, void *args
)
1758 /* Make sure we got the right message. */
1759 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1762 /* Validate and take action on expected syscalls. */
1765 /* change getpid to getppid. */
1766 EXPECT_EQ(__NR_getpid
, get_syscall(_metadata
, tracee
));
1767 change_syscall(_metadata
, tracee
, __NR_getppid
, 0);
1770 /* skip gettid with valid return code. */
1771 EXPECT_EQ(__NR_gettid
, get_syscall(_metadata
, tracee
));
1772 change_syscall(_metadata
, tracee
, -1, 45000);
1775 /* skip openat with error. */
1776 EXPECT_EQ(__NR_openat
, get_syscall(_metadata
, tracee
));
1777 change_syscall(_metadata
, tracee
, -1, -ESRCH
);
1780 /* do nothing (allow getppid) */
1781 EXPECT_EQ(__NR_getppid
, get_syscall(_metadata
, tracee
));
1785 TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg
);
1786 kill(tracee
, SIGKILL
);
1792 void tracer_ptrace(struct __test_metadata
*_metadata
, pid_t tracee
,
1793 int status
, void *args
)
1800 * The traditional way to tell PTRACE_SYSCALL entry/exit
1805 /* Make sure we got an appropriate message. */
1806 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1808 EXPECT_EQ(entry
? PTRACE_EVENTMSG_SYSCALL_ENTRY
1809 : PTRACE_EVENTMSG_SYSCALL_EXIT
, msg
);
1814 nr
= get_syscall(_metadata
, tracee
);
1816 if (nr
== __NR_getpid
)
1817 change_syscall(_metadata
, tracee
, __NR_getppid
, 0);
1818 if (nr
== __NR_gettid
)
1819 change_syscall(_metadata
, tracee
, -1, 45000);
1820 if (nr
== __NR_openat
)
1821 change_syscall(_metadata
, tracee
, -1, -ESRCH
);
1824 FIXTURE(TRACE_syscall
) {
1825 struct sock_fprog prog
;
1826 pid_t tracer
, mytid
, mypid
, parent
;
1829 FIXTURE_SETUP(TRACE_syscall
)
1831 struct sock_filter filter
[] = {
1832 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1833 offsetof(struct seccomp_data
, nr
)),
1834 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
1835 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1002),
1836 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_gettid
, 0, 1),
1837 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1003),
1838 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_openat
, 0, 1),
1839 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1004),
1840 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1841 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1005),
1842 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1845 memset(&self
->prog
, 0, sizeof(self
->prog
));
1846 self
->prog
.filter
= malloc(sizeof(filter
));
1847 ASSERT_NE(NULL
, self
->prog
.filter
);
1848 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1849 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1851 /* Prepare some testable syscall results. */
1852 self
->mytid
= syscall(__NR_gettid
);
1853 ASSERT_GT(self
->mytid
, 0);
1854 ASSERT_NE(self
->mytid
, 1) {
1855 TH_LOG("Running this test as init is not supported. :)");
1858 self
->mypid
= getpid();
1859 ASSERT_GT(self
->mypid
, 0);
1860 ASSERT_EQ(self
->mytid
, self
->mypid
);
1862 self
->parent
= getppid();
1863 ASSERT_GT(self
->parent
, 0);
1864 ASSERT_NE(self
->parent
, self
->mypid
);
1866 /* Launch tracer. */
1867 self
->tracer
= setup_trace_fixture(_metadata
, tracer_syscall
, NULL
,
1871 FIXTURE_TEARDOWN(TRACE_syscall
)
1873 teardown_trace_fixture(_metadata
, self
->tracer
);
1874 if (self
->prog
.filter
)
1875 free(self
->prog
.filter
);
1878 TEST_F(TRACE_syscall
, ptrace_syscall_redirected
)
1880 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1881 teardown_trace_fixture(_metadata
, self
->tracer
);
1882 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1885 /* Tracer will redirect getpid to getppid. */
1886 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1889 TEST_F(TRACE_syscall
, ptrace_syscall_errno
)
1891 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1892 teardown_trace_fixture(_metadata
, self
->tracer
);
1893 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1896 /* Tracer should skip the open syscall, resulting in ESRCH. */
1897 EXPECT_SYSCALL_RETURN(-ESRCH
, syscall(__NR_openat
));
1900 TEST_F(TRACE_syscall
, ptrace_syscall_faked
)
1902 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1903 teardown_trace_fixture(_metadata
, self
->tracer
);
1904 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
1907 /* Tracer should skip the gettid syscall, resulting fake pid. */
1908 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid
));
1911 TEST_F(TRACE_syscall
, syscall_allowed
)
1915 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1918 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1921 /* getppid works as expected (no changes). */
1922 EXPECT_EQ(self
->parent
, syscall(__NR_getppid
));
1923 EXPECT_NE(self
->mypid
, syscall(__NR_getppid
));
1926 TEST_F(TRACE_syscall
, syscall_redirected
)
1930 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1933 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1936 /* getpid has been redirected to getppid as expected. */
1937 EXPECT_EQ(self
->parent
, syscall(__NR_getpid
));
1938 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
1941 TEST_F(TRACE_syscall
, syscall_errno
)
1945 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1948 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1951 /* openat has been skipped and an errno return. */
1952 EXPECT_SYSCALL_RETURN(-ESRCH
, syscall(__NR_openat
));
1955 TEST_F(TRACE_syscall
, syscall_faked
)
1959 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1962 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1965 /* gettid has been skipped and an altered return value stored. */
1966 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid
));
1969 TEST_F(TRACE_syscall
, skip_after_RET_TRACE
)
1971 struct sock_filter filter
[] = {
1972 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1973 offsetof(struct seccomp_data
, nr
)),
1974 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
1975 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
1976 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1978 struct sock_fprog prog
= {
1979 .len
= (unsigned short)ARRAY_SIZE(filter
),
1984 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1987 /* Install fixture filter. */
1988 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1991 /* Install "errno on getppid" filter. */
1992 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
1995 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
1997 EXPECT_EQ(-1, syscall(__NR_getpid
));
1998 EXPECT_EQ(EPERM
, errno
);
2001 TEST_F_SIGNAL(TRACE_syscall
, kill_after_RET_TRACE
, SIGSYS
)
2003 struct sock_filter filter
[] = {
2004 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2005 offsetof(struct seccomp_data
, nr
)),
2006 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2007 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2008 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2010 struct sock_fprog prog
= {
2011 .len
= (unsigned short)ARRAY_SIZE(filter
),
2016 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2019 /* Install fixture filter. */
2020 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
2023 /* Install "death on getppid" filter. */
2024 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2027 /* Tracer will redirect getpid to getppid, and we should die. */
2028 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
2031 TEST_F(TRACE_syscall
, skip_after_ptrace
)
2033 struct sock_filter filter
[] = {
2034 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2035 offsetof(struct seccomp_data
, nr
)),
2036 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2037 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
2038 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2040 struct sock_fprog prog
= {
2041 .len
= (unsigned short)ARRAY_SIZE(filter
),
2046 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
2047 teardown_trace_fixture(_metadata
, self
->tracer
);
2048 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
2051 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2054 /* Install "errno on getppid" filter. */
2055 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2058 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
2059 EXPECT_EQ(-1, syscall(__NR_getpid
));
2060 EXPECT_EQ(EPERM
, errno
);
2063 TEST_F_SIGNAL(TRACE_syscall
, kill_after_ptrace
, SIGSYS
)
2065 struct sock_filter filter
[] = {
2066 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2067 offsetof(struct seccomp_data
, nr
)),
2068 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2069 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2070 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2072 struct sock_fprog prog
= {
2073 .len
= (unsigned short)ARRAY_SIZE(filter
),
2078 /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
2079 teardown_trace_fixture(_metadata
, self
->tracer
);
2080 self
->tracer
= setup_trace_fixture(_metadata
, tracer_ptrace
, NULL
,
2083 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2086 /* Install "death on getppid" filter. */
2087 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2090 /* Tracer will redirect getpid to getppid, and we should die. */
2091 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
2094 TEST(seccomp_syscall
)
2096 struct sock_filter filter
[] = {
2097 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2099 struct sock_fprog prog
= {
2100 .len
= (unsigned short)ARRAY_SIZE(filter
),
2105 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2107 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2110 /* Reject insane operation. */
2111 ret
= seccomp(-1, 0, &prog
);
2112 ASSERT_NE(ENOSYS
, errno
) {
2113 TH_LOG("Kernel does not support seccomp syscall!");
2115 EXPECT_EQ(EINVAL
, errno
) {
2116 TH_LOG("Did not reject crazy op value!");
2119 /* Reject strict with flags or pointer. */
2120 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, -1, NULL
);
2121 EXPECT_EQ(EINVAL
, errno
) {
2122 TH_LOG("Did not reject mode strict with flags!");
2124 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, &prog
);
2125 EXPECT_EQ(EINVAL
, errno
) {
2126 TH_LOG("Did not reject mode strict with uargs!");
2129 /* Reject insane args for filter. */
2130 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, -1, &prog
);
2131 EXPECT_EQ(EINVAL
, errno
) {
2132 TH_LOG("Did not reject crazy filter flags!");
2134 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, NULL
);
2135 EXPECT_EQ(EFAULT
, errno
) {
2136 TH_LOG("Did not reject NULL filter!");
2139 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2140 EXPECT_EQ(0, errno
) {
2141 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s",
2146 TEST(seccomp_syscall_mode_lock
)
2148 struct sock_filter filter
[] = {
2149 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2151 struct sock_fprog prog
= {
2152 .len
= (unsigned short)ARRAY_SIZE(filter
),
2157 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2159 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2162 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2163 ASSERT_NE(ENOSYS
, errno
) {
2164 TH_LOG("Kernel does not support seccomp syscall!");
2167 TH_LOG("Could not install filter!");
2170 /* Make sure neither entry point will switch to strict. */
2171 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, 0, 0, 0);
2172 EXPECT_EQ(EINVAL
, errno
) {
2173 TH_LOG("Switched to mode strict!");
2176 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, NULL
);
2177 EXPECT_EQ(EINVAL
, errno
) {
2178 TH_LOG("Switched to mode strict!");
2183 * Test detection of known and unknown filter flags. Userspace needs to be able
2184 * to check if a filter flag is supported by the current kernel and a good way
2185 * of doing that is by attempting to enter filter mode, with the flag bit in
2186 * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates
2187 * that the flag is valid and EINVAL indicates that the flag is invalid.
2189 TEST(detect_seccomp_filter_flags
)
2191 unsigned int flags
[] = { SECCOMP_FILTER_FLAG_TSYNC
,
2192 SECCOMP_FILTER_FLAG_LOG
,
2193 SECCOMP_FILTER_FLAG_SPEC_ALLOW
,
2194 SECCOMP_FILTER_FLAG_NEW_LISTENER
,
2195 SECCOMP_FILTER_FLAG_TSYNC_ESRCH
};
2196 unsigned int exclusive
[] = {
2197 SECCOMP_FILTER_FLAG_TSYNC
,
2198 SECCOMP_FILTER_FLAG_NEW_LISTENER
};
2199 unsigned int flag
, all_flags
, exclusive_mask
;
2203 /* Test detection of individual known-good filter flags */
2204 for (i
= 0, all_flags
= 0; i
< ARRAY_SIZE(flags
); i
++) {
2208 /* Make sure the flag is a single bit! */
2217 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2218 ASSERT_NE(ENOSYS
, errno
) {
2219 TH_LOG("Kernel does not support seccomp syscall!");
2222 EXPECT_EQ(EFAULT
, errno
) {
2223 TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!",
2231 * Test detection of all known-good filter flags combined. But
2232 * for the exclusive flags we need to mask them out and try them
2233 * individually for the "all flags" testing.
2236 for (i
= 0; i
< ARRAY_SIZE(exclusive
); i
++)
2237 exclusive_mask
|= exclusive
[i
];
2238 for (i
= 0; i
< ARRAY_SIZE(exclusive
); i
++) {
2239 flag
= all_flags
& ~exclusive_mask
;
2240 flag
|= exclusive
[i
];
2242 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2244 EXPECT_EQ(EFAULT
, errno
) {
2245 TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2250 /* Test detection of an unknown filter flags, without exclusives. */
2252 flag
&= ~exclusive_mask
;
2253 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2255 EXPECT_EQ(EINVAL
, errno
) {
2256 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!",
2261 * Test detection of an unknown filter flag that may simply need to be
2262 * added to this test
2264 flag
= flags
[ARRAY_SIZE(flags
) - 1] << 1;
2265 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2267 EXPECT_EQ(EINVAL
, errno
) {
2268 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?",
2275 struct sock_filter filter
[] = {
2276 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2278 struct sock_fprog prog
= {
2279 .len
= (unsigned short)ARRAY_SIZE(filter
),
2284 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2286 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2289 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2291 ASSERT_NE(ENOSYS
, errno
) {
2292 TH_LOG("Kernel does not support seccomp syscall!");
2295 TH_LOG("Could not install initial filter with TSYNC!");
2299 #define TSYNC_SIBLINGS 2
2300 struct tsync_sibling
{
2304 pthread_cond_t
*cond
;
2305 pthread_mutex_t
*mutex
;
2308 struct sock_fprog
*prog
;
2309 struct __test_metadata
*metadata
;
2313 * To avoid joining joined threads (which is not allowed by Bionic),
2314 * make sure we both successfully join and clear the tid to skip a
2315 * later join attempt during fixture teardown. Any remaining threads
2316 * will be directly killed during teardown.
2318 #define PTHREAD_JOIN(tid, status) \
2320 int _rc = pthread_join(tid, status); \
2322 TH_LOG("pthread_join of tid %u failed: %d\n", \
2323 (unsigned int)tid, _rc); \
2330 struct sock_fprog root_prog
, apply_prog
;
2331 struct tsync_sibling sibling
[TSYNC_SIBLINGS
];
2333 pthread_cond_t cond
;
2334 pthread_mutex_t mutex
;
2338 FIXTURE_SETUP(TSYNC
)
2340 struct sock_filter root_filter
[] = {
2341 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2343 struct sock_filter apply_filter
[] = {
2344 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2345 offsetof(struct seccomp_data
, nr
)),
2346 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
2347 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2348 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2351 memset(&self
->root_prog
, 0, sizeof(self
->root_prog
));
2352 memset(&self
->apply_prog
, 0, sizeof(self
->apply_prog
));
2353 memset(&self
->sibling
, 0, sizeof(self
->sibling
));
2354 self
->root_prog
.filter
= malloc(sizeof(root_filter
));
2355 ASSERT_NE(NULL
, self
->root_prog
.filter
);
2356 memcpy(self
->root_prog
.filter
, &root_filter
, sizeof(root_filter
));
2357 self
->root_prog
.len
= (unsigned short)ARRAY_SIZE(root_filter
);
2359 self
->apply_prog
.filter
= malloc(sizeof(apply_filter
));
2360 ASSERT_NE(NULL
, self
->apply_prog
.filter
);
2361 memcpy(self
->apply_prog
.filter
, &apply_filter
, sizeof(apply_filter
));
2362 self
->apply_prog
.len
= (unsigned short)ARRAY_SIZE(apply_filter
);
2364 self
->sibling_count
= 0;
2365 pthread_mutex_init(&self
->mutex
, NULL
);
2366 pthread_cond_init(&self
->cond
, NULL
);
2367 sem_init(&self
->started
, 0, 0);
2368 self
->sibling
[0].tid
= 0;
2369 self
->sibling
[0].cond
= &self
->cond
;
2370 self
->sibling
[0].started
= &self
->started
;
2371 self
->sibling
[0].mutex
= &self
->mutex
;
2372 self
->sibling
[0].diverge
= 0;
2373 self
->sibling
[0].num_waits
= 1;
2374 self
->sibling
[0].prog
= &self
->root_prog
;
2375 self
->sibling
[0].metadata
= _metadata
;
2376 self
->sibling
[1].tid
= 0;
2377 self
->sibling
[1].cond
= &self
->cond
;
2378 self
->sibling
[1].started
= &self
->started
;
2379 self
->sibling
[1].mutex
= &self
->mutex
;
2380 self
->sibling
[1].diverge
= 0;
2381 self
->sibling
[1].prog
= &self
->root_prog
;
2382 self
->sibling
[1].num_waits
= 1;
2383 self
->sibling
[1].metadata
= _metadata
;
2386 FIXTURE_TEARDOWN(TSYNC
)
2390 if (self
->root_prog
.filter
)
2391 free(self
->root_prog
.filter
);
2392 if (self
->apply_prog
.filter
)
2393 free(self
->apply_prog
.filter
);
2395 for ( ; sib
< self
->sibling_count
; ++sib
) {
2396 struct tsync_sibling
*s
= &self
->sibling
[sib
];
2401 * If a thread is still running, it may be stuck, so hit
2402 * it over the head really hard.
2404 pthread_kill(s
->tid
, 9);
2406 pthread_mutex_destroy(&self
->mutex
);
2407 pthread_cond_destroy(&self
->cond
);
2408 sem_destroy(&self
->started
);
2411 void *tsync_sibling(void *data
)
2414 struct tsync_sibling
*me
= data
;
2416 me
->system_tid
= syscall(__NR_gettid
);
2418 pthread_mutex_lock(me
->mutex
);
2420 /* Just re-apply the root prog to fork the tree */
2421 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
2424 sem_post(me
->started
);
2425 /* Return outside of started so parent notices failures. */
2427 pthread_mutex_unlock(me
->mutex
);
2428 return (void *)SIBLING_EXIT_FAILURE
;
2431 pthread_cond_wait(me
->cond
, me
->mutex
);
2432 me
->num_waits
= me
->num_waits
- 1;
2433 } while (me
->num_waits
);
2434 pthread_mutex_unlock(me
->mutex
);
2436 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, 0, 0, 0);
2438 return (void *)SIBLING_EXIT_NEWPRIVS
;
2440 return (void *)SIBLING_EXIT_UNKILLED
;
2443 void tsync_start_sibling(struct tsync_sibling
*sibling
)
2445 pthread_create(&sibling
->tid
, NULL
, tsync_sibling
, (void *)sibling
);
2448 TEST_F(TSYNC
, siblings_fail_prctl
)
2452 struct sock_filter filter
[] = {
2453 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2454 offsetof(struct seccomp_data
, nr
)),
2455 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
2456 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EINVAL
),
2457 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2459 struct sock_fprog prog
= {
2460 .len
= (unsigned short)ARRAY_SIZE(filter
),
2464 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2465 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2468 /* Check prctl failure detection by requesting sib 0 diverge. */
2469 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2470 ASSERT_NE(ENOSYS
, errno
) {
2471 TH_LOG("Kernel does not support seccomp syscall!");
2474 TH_LOG("setting filter failed");
2477 self
->sibling
[0].diverge
= 1;
2478 tsync_start_sibling(&self
->sibling
[0]);
2479 tsync_start_sibling(&self
->sibling
[1]);
2481 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2482 sem_wait(&self
->started
);
2483 self
->sibling_count
++;
2486 /* Signal the threads to clean up*/
2487 pthread_mutex_lock(&self
->mutex
);
2488 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2489 TH_LOG("cond broadcast non-zero");
2491 pthread_mutex_unlock(&self
->mutex
);
2493 /* Ensure diverging sibling failed to call prctl. */
2494 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2495 EXPECT_EQ(SIBLING_EXIT_FAILURE
, (long)status
);
2496 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2497 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2500 TEST_F(TSYNC
, two_siblings_with_ancestor
)
2505 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2506 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2509 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2510 ASSERT_NE(ENOSYS
, errno
) {
2511 TH_LOG("Kernel does not support seccomp syscall!");
2514 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2516 tsync_start_sibling(&self
->sibling
[0]);
2517 tsync_start_sibling(&self
->sibling
[1]);
2519 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2520 sem_wait(&self
->started
);
2521 self
->sibling_count
++;
2524 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2527 TH_LOG("Could install filter on all threads!");
2529 /* Tell the siblings to test the policy */
2530 pthread_mutex_lock(&self
->mutex
);
2531 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2532 TH_LOG("cond broadcast non-zero");
2534 pthread_mutex_unlock(&self
->mutex
);
2535 /* Ensure they are both killed and don't exit cleanly. */
2536 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2537 EXPECT_EQ(0x0, (long)status
);
2538 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2539 EXPECT_EQ(0x0, (long)status
);
2542 TEST_F(TSYNC
, two_sibling_want_nnp
)
2546 /* start siblings before any prctl() operations */
2547 tsync_start_sibling(&self
->sibling
[0]);
2548 tsync_start_sibling(&self
->sibling
[1]);
2549 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2550 sem_wait(&self
->started
);
2551 self
->sibling_count
++;
2554 /* Tell the siblings to test no policy */
2555 pthread_mutex_lock(&self
->mutex
);
2556 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2557 TH_LOG("cond broadcast non-zero");
2559 pthread_mutex_unlock(&self
->mutex
);
2561 /* Ensure they are both upset about lacking nnp. */
2562 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2563 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2564 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2565 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2568 TEST_F(TSYNC
, two_siblings_with_no_filter
)
2573 /* start siblings before any prctl() operations */
2574 tsync_start_sibling(&self
->sibling
[0]);
2575 tsync_start_sibling(&self
->sibling
[1]);
2576 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2577 sem_wait(&self
->started
);
2578 self
->sibling_count
++;
2581 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2582 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2585 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2587 ASSERT_NE(ENOSYS
, errno
) {
2588 TH_LOG("Kernel does not support seccomp syscall!");
2591 TH_LOG("Could install filter on all threads!");
2594 /* Tell the siblings to test the policy */
2595 pthread_mutex_lock(&self
->mutex
);
2596 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2597 TH_LOG("cond broadcast non-zero");
2599 pthread_mutex_unlock(&self
->mutex
);
2601 /* Ensure they are both killed and don't exit cleanly. */
2602 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2603 EXPECT_EQ(0x0, (long)status
);
2604 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2605 EXPECT_EQ(0x0, (long)status
);
2608 TEST_F(TSYNC
, two_siblings_with_one_divergence
)
2613 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2614 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2617 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2618 ASSERT_NE(ENOSYS
, errno
) {
2619 TH_LOG("Kernel does not support seccomp syscall!");
2622 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2624 self
->sibling
[0].diverge
= 1;
2625 tsync_start_sibling(&self
->sibling
[0]);
2626 tsync_start_sibling(&self
->sibling
[1]);
2628 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2629 sem_wait(&self
->started
);
2630 self
->sibling_count
++;
2633 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2635 ASSERT_EQ(self
->sibling
[0].system_tid
, ret
) {
2636 TH_LOG("Did not fail on diverged sibling.");
2639 /* Wake the threads */
2640 pthread_mutex_lock(&self
->mutex
);
2641 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2642 TH_LOG("cond broadcast non-zero");
2644 pthread_mutex_unlock(&self
->mutex
);
2646 /* Ensure they are both unkilled. */
2647 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2648 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2649 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2650 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2653 TEST_F(TSYNC
, two_siblings_with_one_divergence_no_tid_in_err
)
2658 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2659 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2662 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2663 ASSERT_NE(ENOSYS
, errno
) {
2664 TH_LOG("Kernel does not support seccomp syscall!");
2667 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2669 self
->sibling
[0].diverge
= 1;
2670 tsync_start_sibling(&self
->sibling
[0]);
2671 tsync_start_sibling(&self
->sibling
[1]);
2673 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2674 sem_wait(&self
->started
);
2675 self
->sibling_count
++;
2678 flags
= SECCOMP_FILTER_FLAG_TSYNC
| \
2679 SECCOMP_FILTER_FLAG_TSYNC_ESRCH
;
2680 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flags
, &self
->apply_prog
);
2681 ASSERT_EQ(ESRCH
, errno
) {
2682 TH_LOG("Did not return ESRCH for diverged sibling.");
2684 ASSERT_EQ(-1, ret
) {
2685 TH_LOG("Did not fail on diverged sibling.");
2688 /* Wake the threads */
2689 pthread_mutex_lock(&self
->mutex
);
2690 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2691 TH_LOG("cond broadcast non-zero");
2693 pthread_mutex_unlock(&self
->mutex
);
2695 /* Ensure they are both unkilled. */
2696 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2697 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2698 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2699 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2702 TEST_F(TSYNC
, two_siblings_not_under_filter
)
2706 struct timespec delay
= { .tv_nsec
= 100000000 };
2708 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2709 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2713 * Sibling 0 will have its own seccomp policy
2714 * and Sibling 1 will not be under seccomp at
2715 * all. Sibling 1 will enter seccomp and 0
2716 * will cause failure.
2718 self
->sibling
[0].diverge
= 1;
2719 tsync_start_sibling(&self
->sibling
[0]);
2720 tsync_start_sibling(&self
->sibling
[1]);
2722 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2723 sem_wait(&self
->started
);
2724 self
->sibling_count
++;
2727 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2728 ASSERT_NE(ENOSYS
, errno
) {
2729 TH_LOG("Kernel does not support seccomp syscall!");
2732 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2735 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2737 ASSERT_EQ(ret
, self
->sibling
[0].system_tid
) {
2738 TH_LOG("Did not fail on diverged sibling.");
2741 if (ret
== self
->sibling
[0].system_tid
)
2744 pthread_mutex_lock(&self
->mutex
);
2746 /* Increment the other siblings num_waits so we can clean up
2747 * the one we just saw.
2749 self
->sibling
[!sib
].num_waits
+= 1;
2751 /* Signal the thread to clean up*/
2752 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2753 TH_LOG("cond broadcast non-zero");
2755 pthread_mutex_unlock(&self
->mutex
);
2756 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2757 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2758 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2759 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2760 nanosleep(&delay
, NULL
);
2761 /* Switch to the remaining sibling */
2764 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2767 TH_LOG("Expected the remaining sibling to sync");
2770 pthread_mutex_lock(&self
->mutex
);
2772 /* If remaining sibling didn't have a chance to wake up during
2773 * the first broadcast, manually reduce the num_waits now.
2775 if (self
->sibling
[sib
].num_waits
> 1)
2776 self
->sibling
[sib
].num_waits
= 1;
2777 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2778 TH_LOG("cond broadcast non-zero");
2780 pthread_mutex_unlock(&self
->mutex
);
2781 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2782 EXPECT_EQ(0, (long)status
);
2783 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2784 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2785 nanosleep(&delay
, NULL
);
2787 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2789 ASSERT_EQ(0, ret
); /* just us chickens */
2792 /* Make sure restarted syscalls are seen directly as "restart_syscall". */
2793 TEST(syscall_restart
)
2800 siginfo_t info
= { };
2801 struct sock_filter filter
[] = {
2802 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2803 offsetof(struct seccomp_data
, nr
)),
2805 #ifdef __NR_sigreturn
2806 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_sigreturn
, 7, 0),
2808 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 6, 0),
2809 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_exit
, 5, 0),
2810 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_rt_sigreturn
, 4, 0),
2811 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_nanosleep
, 5, 0),
2812 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_clock_nanosleep
, 4, 0),
2813 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_restart_syscall
, 4, 0),
2815 /* Allow __NR_write for easy logging. */
2816 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_write
, 0, 1),
2817 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2818 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2819 /* The nanosleep jump target. */
2820 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x100),
2821 /* The restart_syscall jump target. */
2822 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x200),
2824 struct sock_fprog prog
= {
2825 .len
= (unsigned short)ARRAY_SIZE(filter
),
2828 #if defined(__arm__)
2829 struct utsname utsbuf
;
2832 ASSERT_EQ(0, pipe(pipefd
));
2835 ASSERT_LE(0, child_pid
);
2836 if (child_pid
== 0) {
2837 /* Child uses EXPECT not ASSERT to deliver status correctly. */
2839 struct timespec timeout
= { };
2841 /* Attach parent as tracer and stop. */
2842 EXPECT_EQ(0, ptrace(PTRACE_TRACEME
));
2843 EXPECT_EQ(0, raise(SIGSTOP
));
2845 EXPECT_EQ(0, close(pipefd
[1]));
2847 EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2848 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2851 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2853 TH_LOG("Failed to install filter!");
2856 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2857 TH_LOG("Failed to read() sync from parent");
2859 EXPECT_EQ('.', buf
) {
2860 TH_LOG("Failed to get sync data from read()");
2863 /* Start nanosleep to be interrupted. */
2866 EXPECT_EQ(0, nanosleep(&timeout
, NULL
)) {
2867 TH_LOG("Call to nanosleep() failed (errno %d)", errno
);
2870 /* Read final sync from parent. */
2871 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2872 TH_LOG("Failed final read() from parent");
2874 EXPECT_EQ('!', buf
) {
2875 TH_LOG("Failed to get final data from read()");
2878 /* Directly report the status of our test harness results. */
2879 syscall(__NR_exit
, _metadata
->passed
? EXIT_SUCCESS
2882 EXPECT_EQ(0, close(pipefd
[0]));
2884 /* Attach to child, setup options, and release. */
2885 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2886 ASSERT_EQ(true, WIFSTOPPED(status
));
2887 ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS
, child_pid
, NULL
,
2888 PTRACE_O_TRACESECCOMP
));
2889 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2890 ASSERT_EQ(1, write(pipefd
[1], ".", 1));
2892 /* Wait for nanosleep() to start. */
2893 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2894 ASSERT_EQ(true, WIFSTOPPED(status
));
2895 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2896 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2897 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2898 ASSERT_EQ(0x100, msg
);
2899 ret
= get_syscall(_metadata
, child_pid
);
2900 EXPECT_TRUE(ret
== __NR_nanosleep
|| ret
== __NR_clock_nanosleep
);
2902 /* Might as well check siginfo for sanity while we're here. */
2903 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2904 ASSERT_EQ(SIGTRAP
, info
.si_signo
);
2905 ASSERT_EQ(SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8), info
.si_code
);
2906 EXPECT_EQ(0, info
.si_errno
);
2907 EXPECT_EQ(getuid(), info
.si_uid
);
2908 /* Verify signal delivery came from child (seccomp-triggered). */
2909 EXPECT_EQ(child_pid
, info
.si_pid
);
2911 /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */
2912 ASSERT_EQ(0, kill(child_pid
, SIGSTOP
));
2913 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2914 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2915 ASSERT_EQ(true, WIFSTOPPED(status
));
2916 ASSERT_EQ(SIGSTOP
, WSTOPSIG(status
));
2917 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2919 * There is no siginfo on SIGSTOP any more, so we can't verify
2920 * signal delivery came from parent now (getpid() == info.si_pid).
2921 * https://lkml.kernel.org/r/CAGXu5jJaZAOzP1qFz66tYrtbuywqb+UN2SOA1VLHpCCOiYvYeg@mail.gmail.com
2922 * At least verify the SIGSTOP via PTRACE_GETSIGINFO.
2924 EXPECT_EQ(SIGSTOP
, info
.si_signo
);
2926 /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */
2927 ASSERT_EQ(0, kill(child_pid
, SIGCONT
));
2928 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2929 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2930 ASSERT_EQ(true, WIFSTOPPED(status
));
2931 ASSERT_EQ(SIGCONT
, WSTOPSIG(status
));
2932 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2934 /* Wait for restart_syscall() to start. */
2935 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2936 ASSERT_EQ(true, WIFSTOPPED(status
));
2937 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2938 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2939 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2941 ASSERT_EQ(0x200, msg
);
2942 ret
= get_syscall(_metadata
, child_pid
);
2943 #if defined(__arm__)
2946 * - native ARM registers do NOT expose true syscall.
2947 * - compat ARM registers on ARM64 DO expose true syscall.
2949 ASSERT_EQ(0, uname(&utsbuf
));
2950 if (strncmp(utsbuf
.machine
, "arm", 3) == 0) {
2951 EXPECT_EQ(__NR_nanosleep
, ret
);
2955 EXPECT_EQ(__NR_restart_syscall
, ret
);
2958 /* Write again to end test. */
2959 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2960 ASSERT_EQ(1, write(pipefd
[1], "!", 1));
2961 EXPECT_EQ(0, close(pipefd
[1]));
2963 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2964 if (WIFSIGNALED(status
) || WEXITSTATUS(status
))
2965 _metadata
->passed
= 0;
2968 TEST_SIGNAL(filter_flag_log
, SIGSYS
)
2970 struct sock_filter allow_filter
[] = {
2971 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2973 struct sock_filter kill_filter
[] = {
2974 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2975 offsetof(struct seccomp_data
, nr
)),
2976 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
2977 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2978 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2980 struct sock_fprog allow_prog
= {
2981 .len
= (unsigned short)ARRAY_SIZE(allow_filter
),
2982 .filter
= allow_filter
,
2984 struct sock_fprog kill_prog
= {
2985 .len
= (unsigned short)ARRAY_SIZE(kill_filter
),
2986 .filter
= kill_filter
,
2989 pid_t parent
= getppid();
2991 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2994 /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */
2995 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, SECCOMP_FILTER_FLAG_LOG
,
2997 ASSERT_NE(ENOSYS
, errno
) {
2998 TH_LOG("Kernel does not support seccomp syscall!");
3001 TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!");
3003 EXPECT_EQ(EINVAL
, errno
) {
3004 TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!");
3007 /* Verify that a simple, permissive filter can be added with no flags */
3008 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &allow_prog
);
3011 /* See if the same filter can be added with the FILTER_FLAG_LOG flag */
3012 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
3014 ASSERT_NE(EINVAL
, errno
) {
3015 TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!");
3019 /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */
3020 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
3024 EXPECT_EQ(parent
, syscall(__NR_getppid
));
3025 /* getpid() should never return. */
3026 EXPECT_EQ(0, syscall(__NR_getpid
));
3029 TEST(get_action_avail
)
3031 __u32 actions
[] = { SECCOMP_RET_KILL_THREAD
, SECCOMP_RET_TRAP
,
3032 SECCOMP_RET_ERRNO
, SECCOMP_RET_TRACE
,
3033 SECCOMP_RET_LOG
, SECCOMP_RET_ALLOW
};
3034 __u32 unknown_action
= 0x10000000U
;
3038 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[0]);
3039 ASSERT_NE(ENOSYS
, errno
) {
3040 TH_LOG("Kernel does not support seccomp syscall!");
3042 ASSERT_NE(EINVAL
, errno
) {
3043 TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!");
3047 for (i
= 0; i
< ARRAY_SIZE(actions
); i
++) {
3048 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[i
]);
3050 TH_LOG("Expected action (0x%X) not available!",
3055 /* Check that an unknown action is handled properly (EOPNOTSUPP) */
3056 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &unknown_action
);
3058 EXPECT_EQ(errno
, EOPNOTSUPP
);
3066 struct seccomp_metadata md
;
3069 /* Only real root can get metadata. */
3071 XFAIL(return, "get_metadata requires real root");
3075 ASSERT_EQ(0, pipe(pipefd
));
3080 struct sock_filter filter
[] = {
3081 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3083 struct sock_fprog prog
= {
3084 .len
= (unsigned short)ARRAY_SIZE(filter
),
3088 /* one with log, one without */
3089 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
,
3090 SECCOMP_FILTER_FLAG_LOG
, &prog
));
3091 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
));
3093 EXPECT_EQ(0, close(pipefd
[0]));
3094 ASSERT_EQ(1, write(pipefd
[1], "1", 1));
3095 ASSERT_EQ(0, close(pipefd
[1]));
3101 ASSERT_EQ(0, close(pipefd
[1]));
3102 ASSERT_EQ(1, read(pipefd
[0], &buf
, 1));
3104 ASSERT_EQ(0, ptrace(PTRACE_ATTACH
, pid
));
3105 ASSERT_EQ(pid
, waitpid(pid
, NULL
, 0));
3107 /* Past here must not use ASSERT or child process is never killed. */
3111 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
3112 EXPECT_EQ(sizeof(md
), ret
) {
3113 if (errno
== EINVAL
)
3114 XFAIL(goto skip
, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)");
3117 EXPECT_EQ(md
.flags
, SECCOMP_FILTER_FLAG_LOG
);
3118 EXPECT_EQ(md
.filter_off
, 0);
3121 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
3122 EXPECT_EQ(sizeof(md
), ret
);
3123 EXPECT_EQ(md
.flags
, 0);
3124 EXPECT_EQ(md
.filter_off
, 1);
3127 ASSERT_EQ(0, kill(pid
, SIGKILL
));
3130 static int user_trap_syscall(int nr
, unsigned int flags
)
3132 struct sock_filter filter
[] = {
3133 BPF_STMT(BPF_LD
+BPF_W
+BPF_ABS
,
3134 offsetof(struct seccomp_data
, nr
)),
3135 BPF_JUMP(BPF_JMP
+BPF_JEQ
+BPF_K
, nr
, 0, 1),
3136 BPF_STMT(BPF_RET
+BPF_K
, SECCOMP_RET_USER_NOTIF
),
3137 BPF_STMT(BPF_RET
+BPF_K
, SECCOMP_RET_ALLOW
),
3140 struct sock_fprog prog
= {
3141 .len
= (unsigned short)ARRAY_SIZE(filter
),
3145 return seccomp(SECCOMP_SET_MODE_FILTER
, flags
, &prog
);
3148 #define USER_NOTIF_MAGIC INT_MAX
3149 TEST(user_notification_basic
)
3153 int status
, listener
;
3154 struct seccomp_notif req
= {};
3155 struct seccomp_notif_resp resp
= {};
3156 struct pollfd pollfd
;
3158 struct sock_filter filter
[] = {
3159 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3161 struct sock_fprog prog
= {
3162 .len
= (unsigned short)ARRAY_SIZE(filter
),
3166 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3168 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3174 /* Check that we get -ENOSYS with no listener attached */
3176 if (user_trap_syscall(__NR_getppid
, 0) < 0)
3178 ret
= syscall(__NR_getppid
);
3179 exit(ret
>= 0 || errno
!= ENOSYS
);
3182 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3183 EXPECT_EQ(true, WIFEXITED(status
));
3184 EXPECT_EQ(0, WEXITSTATUS(status
));
3186 /* Add some no-op filters for grins. */
3187 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3188 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3189 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3190 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3192 /* Check that the basic notification machinery works */
3193 listener
= user_trap_syscall(__NR_getppid
,
3194 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3195 ASSERT_GE(listener
, 0);
3197 /* Installing a second listener in the chain should EBUSY */
3198 EXPECT_EQ(user_trap_syscall(__NR_getppid
,
3199 SECCOMP_FILTER_FLAG_NEW_LISTENER
),
3201 EXPECT_EQ(errno
, EBUSY
);
3207 ret
= syscall(__NR_getppid
);
3208 exit(ret
!= USER_NOTIF_MAGIC
);
3211 pollfd
.fd
= listener
;
3212 pollfd
.events
= POLLIN
| POLLOUT
;
3214 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3215 EXPECT_EQ(pollfd
.revents
, POLLIN
);
3217 /* Test that we can't pass garbage to the kernel. */
3218 memset(&req
, 0, sizeof(req
));
3221 ret
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
);
3223 EXPECT_EQ(EINVAL
, errno
);
3227 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3230 pollfd
.fd
= listener
;
3231 pollfd
.events
= POLLIN
| POLLOUT
;
3233 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3234 EXPECT_EQ(pollfd
.revents
, POLLOUT
);
3236 EXPECT_EQ(req
.data
.nr
, __NR_getppid
);
3240 resp
.val
= USER_NOTIF_MAGIC
;
3242 /* check that we make sure flags == 0 */
3244 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3245 EXPECT_EQ(errno
, EINVAL
);
3248 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3250 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3251 EXPECT_EQ(true, WIFEXITED(status
));
3252 EXPECT_EQ(0, WEXITSTATUS(status
));
3255 TEST(user_notification_with_tsync
)
3260 /* these were exclusive */
3261 flags
= SECCOMP_FILTER_FLAG_NEW_LISTENER
|
3262 SECCOMP_FILTER_FLAG_TSYNC
;
3263 ASSERT_EQ(-1, user_trap_syscall(__NR_getppid
, flags
));
3264 ASSERT_EQ(EINVAL
, errno
);
3266 /* but now they're not */
3267 flags
|= SECCOMP_FILTER_FLAG_TSYNC_ESRCH
;
3268 ret
= user_trap_syscall(__NR_getppid
, flags
);
3273 TEST(user_notification_kill_in_middle
)
3278 struct seccomp_notif req
= {};
3279 struct seccomp_notif_resp resp
= {};
3281 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3283 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3286 listener
= user_trap_syscall(__NR_getppid
,
3287 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3288 ASSERT_GE(listener
, 0);
3291 * Check that nothing bad happens when we kill the task in the middle
3298 ret
= syscall(__NR_getppid
);
3299 exit(ret
!= USER_NOTIF_MAGIC
);
3302 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3303 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ID_VALID
, &req
.id
), 0);
3305 EXPECT_EQ(kill(pid
, SIGKILL
), 0);
3306 EXPECT_EQ(waitpid(pid
, NULL
, 0), pid
);
3308 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ID_VALID
, &req
.id
), -1);
3311 ret
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
);
3313 EXPECT_EQ(errno
, ENOENT
);
3316 static int handled
= -1;
3318 static void signal_handler(int signal
)
3320 if (write(handled
, "c", 1) != 1)
3321 perror("write from signal");
3324 TEST(user_notification_signal
)
3328 int status
, listener
, sk_pair
[2];
3329 struct seccomp_notif req
= {};
3330 struct seccomp_notif_resp resp
= {};
3333 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3335 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3338 ASSERT_EQ(socketpair(PF_LOCAL
, SOCK_SEQPACKET
, 0, sk_pair
), 0);
3340 listener
= user_trap_syscall(__NR_gettid
,
3341 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3342 ASSERT_GE(listener
, 0);
3349 handled
= sk_pair
[1];
3350 if (signal(SIGUSR1
, signal_handler
) == SIG_ERR
) {
3355 * ERESTARTSYS behavior is a bit hard to test, because we need
3356 * to rely on a signal that has not yet been handled. Let's at
3357 * least check that the error code gets propagated through, and
3358 * hope that it doesn't break when there is actually a signal :)
3360 ret
= syscall(__NR_gettid
);
3361 exit(!(ret
== -1 && errno
== 512));
3366 memset(&req
, 0, sizeof(req
));
3367 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3369 EXPECT_EQ(kill(pid
, SIGUSR1
), 0);
3372 * Make sure the signal really is delivered, which means we're not
3373 * stuck in the user notification code any more and the notification
3376 EXPECT_EQ(read(sk_pair
[0], &c
, 1), 1);
3379 resp
.error
= -EPERM
;
3382 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3383 EXPECT_EQ(errno
, ENOENT
);
3385 memset(&req
, 0, sizeof(req
));
3386 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3389 resp
.error
= -512; /* -ERESTARTSYS */
3392 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3394 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3395 EXPECT_EQ(true, WIFEXITED(status
));
3396 EXPECT_EQ(0, WEXITSTATUS(status
));
3399 TEST(user_notification_closed_listener
)
3403 int status
, listener
;
3405 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3407 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3410 listener
= user_trap_syscall(__NR_getppid
,
3411 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3412 ASSERT_GE(listener
, 0);
3415 * Check that we get an ENOSYS when the listener is closed.
3421 ret
= syscall(__NR_getppid
);
3422 exit(ret
!= -1 && errno
!= ENOSYS
);
3427 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3428 EXPECT_EQ(true, WIFEXITED(status
));
3429 EXPECT_EQ(0, WEXITSTATUS(status
));
3433 * Check that a pid in a child namespace still shows up as valid in ours.
3435 TEST(user_notification_child_pid_ns
)
3438 int status
, listener
;
3439 struct seccomp_notif req
= {};
3440 struct seccomp_notif_resp resp
= {};
3442 ASSERT_EQ(unshare(CLONE_NEWUSER
| CLONE_NEWPID
), 0);
3444 listener
= user_trap_syscall(__NR_getppid
,
3445 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3446 ASSERT_GE(listener
, 0);
3452 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3454 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3455 EXPECT_EQ(req
.pid
, pid
);
3459 resp
.val
= USER_NOTIF_MAGIC
;
3461 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3463 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3464 EXPECT_EQ(true, WIFEXITED(status
));
3465 EXPECT_EQ(0, WEXITSTATUS(status
));
3470 * Check that a pid in a sibling (i.e. unrelated) namespace shows up as 0, i.e.
3473 TEST(user_notification_sibling_pid_ns
)
3476 int status
, listener
;
3477 struct seccomp_notif req
= {};
3478 struct seccomp_notif_resp resp
= {};
3480 ASSERT_EQ(prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0), 0) {
3481 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3484 listener
= user_trap_syscall(__NR_getppid
,
3485 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3486 ASSERT_GE(listener
, 0);
3492 ASSERT_EQ(unshare(CLONE_NEWPID
), 0);
3498 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3500 EXPECT_EQ(waitpid(pid2
, &status
, 0), pid2
);
3501 EXPECT_EQ(true, WIFEXITED(status
));
3502 EXPECT_EQ(0, WEXITSTATUS(status
));
3503 exit(WEXITSTATUS(status
));
3506 /* Create the sibling ns, and sibling in it. */
3507 ASSERT_EQ(unshare(CLONE_NEWPID
), 0);
3508 ASSERT_EQ(errno
, 0);
3514 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3516 * The pid should be 0, i.e. the task is in some namespace that
3519 EXPECT_EQ(req
.pid
, 0);
3523 resp
.val
= USER_NOTIF_MAGIC
;
3525 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3531 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3532 EXPECT_EQ(true, WIFEXITED(status
));
3533 EXPECT_EQ(0, WEXITSTATUS(status
));
3535 EXPECT_EQ(waitpid(pid2
, &status
, 0), pid2
);
3536 EXPECT_EQ(true, WIFEXITED(status
));
3537 EXPECT_EQ(0, WEXITSTATUS(status
));
3540 TEST(user_notification_fault_recv
)
3543 int status
, listener
;
3544 struct seccomp_notif req
= {};
3545 struct seccomp_notif_resp resp
= {};
3547 ASSERT_EQ(unshare(CLONE_NEWUSER
), 0);
3549 listener
= user_trap_syscall(__NR_getppid
,
3550 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3551 ASSERT_GE(listener
, 0);
3557 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3559 /* Do a bad recv() */
3560 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, NULL
), -1);
3561 EXPECT_EQ(errno
, EFAULT
);
3563 /* We should still be able to receive this notification, though. */
3564 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3565 EXPECT_EQ(req
.pid
, pid
);
3569 resp
.val
= USER_NOTIF_MAGIC
;
3571 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3573 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3574 EXPECT_EQ(true, WIFEXITED(status
));
3575 EXPECT_EQ(0, WEXITSTATUS(status
));
3578 TEST(seccomp_get_notif_sizes
)
3580 struct seccomp_notif_sizes sizes
;
3582 ASSERT_EQ(seccomp(SECCOMP_GET_NOTIF_SIZES
, 0, &sizes
), 0);
3583 EXPECT_EQ(sizes
.seccomp_notif
, sizeof(struct seccomp_notif
));
3584 EXPECT_EQ(sizes
.seccomp_notif_resp
, sizeof(struct seccomp_notif_resp
));
3587 static int filecmp(pid_t pid1
, pid_t pid2
, int fd1
, int fd2
)
3590 return syscall(__NR_kcmp
, pid1
, pid2
, KCMP_FILE
, fd1
, fd2
);
3597 TEST(user_notification_continue
)
3601 int status
, listener
;
3602 struct seccomp_notif req
= {};
3603 struct seccomp_notif_resp resp
= {};
3604 struct pollfd pollfd
;
3606 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3608 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3611 listener
= user_trap_syscall(__NR_dup
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3612 ASSERT_GE(listener
, 0);
3618 int dup_fd
, pipe_fds
[2];
3621 ret
= pipe(pipe_fds
);
3625 dup_fd
= dup(pipe_fds
[0]);
3631 ret
= filecmp(self
, self
, pipe_fds
[0], dup_fd
);
3638 pollfd
.fd
= listener
;
3639 pollfd
.events
= POLLIN
| POLLOUT
;
3641 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3642 EXPECT_EQ(pollfd
.revents
, POLLIN
);
3644 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3646 pollfd
.fd
= listener
;
3647 pollfd
.events
= POLLIN
| POLLOUT
;
3649 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3650 EXPECT_EQ(pollfd
.revents
, POLLOUT
);
3652 EXPECT_EQ(req
.data
.nr
, __NR_dup
);
3655 resp
.flags
= SECCOMP_USER_NOTIF_FLAG_CONTINUE
;
3658 * Verify that setting SECCOMP_USER_NOTIF_FLAG_CONTINUE enforces other
3662 resp
.val
= USER_NOTIF_MAGIC
;
3663 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3664 EXPECT_EQ(errno
, EINVAL
);
3666 resp
.error
= USER_NOTIF_MAGIC
;
3668 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3669 EXPECT_EQ(errno
, EINVAL
);
3673 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0) {
3674 if (errno
== EINVAL
)
3675 XFAIL(goto skip
, "Kernel does not support SECCOMP_USER_NOTIF_FLAG_CONTINUE");
3679 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3680 EXPECT_EQ(true, WIFEXITED(status
));
3681 EXPECT_EQ(0, WEXITSTATUS(status
)) {
3682 if (WEXITSTATUS(status
) == 2) {
3683 XFAIL(return, "Kernel does not support kcmp() syscall");
3691 * - add microbenchmarks
3692 * - expand NNP testing
3693 * - better arch-specific TRACE and TRAP handlers.
3694 * - endianness checking when appropriate
3695 * - 64-bit arg prodding
3696 * - arch value testing (x86 modes especially)
3697 * - verify that FILTER_FLAG_LOG filters generate log messages
3698 * - verify that RET_LOG generates log messages