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>
48 #include <sys/resource.h>
49 #include <sys/capability.h>
52 #include <sys/syscall.h>
55 #include "../kselftest_harness.h"
56 #include "../clone3/clone3_selftests.h"
58 /* Attempt to de-conflict with the selftests tree. */
60 #define SKIP(s, ...) XFAIL(s, ##__VA_ARGS__)
64 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
67 #ifndef PR_SET_PTRACER
68 # define PR_SET_PTRACER 0x59616d61
71 #ifndef PR_SET_NO_NEW_PRIVS
72 #define PR_SET_NO_NEW_PRIVS 38
73 #define PR_GET_NO_NEW_PRIVS 39
76 #ifndef PR_SECCOMP_EXT
77 #define PR_SECCOMP_EXT 43
80 #ifndef SECCOMP_EXT_ACT
81 #define SECCOMP_EXT_ACT 1
84 #ifndef SECCOMP_EXT_ACT_TSYNC
85 #define SECCOMP_EXT_ACT_TSYNC 1
88 #ifndef SECCOMP_MODE_STRICT
89 #define SECCOMP_MODE_STRICT 1
92 #ifndef SECCOMP_MODE_FILTER
93 #define SECCOMP_MODE_FILTER 2
96 #ifndef SECCOMP_RET_ALLOW
100 __u64 instruction_pointer
;
105 #ifndef SECCOMP_RET_KILL_PROCESS
106 #define SECCOMP_RET_KILL_PROCESS 0x80000000U /* kill the process */
107 #define SECCOMP_RET_KILL_THREAD 0x00000000U /* kill the thread */
109 #ifndef SECCOMP_RET_KILL
110 #define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD
111 #define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */
112 #define SECCOMP_RET_ERRNO 0x00050000U /* returns an errno */
113 #define SECCOMP_RET_TRACE 0x7ff00000U /* pass to a tracer or disallow */
114 #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
116 #ifndef SECCOMP_RET_LOG
117 #define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */
121 # if defined(__i386__)
122 # define __NR_seccomp 354
123 # elif defined(__x86_64__)
124 # define __NR_seccomp 317
125 # elif defined(__arm__)
126 # define __NR_seccomp 383
127 # elif defined(__aarch64__)
128 # define __NR_seccomp 277
129 # elif defined(__riscv)
130 # define __NR_seccomp 277
131 # elif defined(__csky__)
132 # define __NR_seccomp 277
133 # elif defined(__loongarch__)
134 # define __NR_seccomp 277
135 # elif defined(__hppa__)
136 # define __NR_seccomp 338
137 # elif defined(__powerpc__)
138 # define __NR_seccomp 358
139 # elif defined(__s390__)
140 # define __NR_seccomp 348
141 # elif defined(__xtensa__)
142 # define __NR_seccomp 337
143 # elif defined(__sh__)
144 # define __NR_seccomp 372
145 # elif defined(__mc68000__)
146 # define __NR_seccomp 380
148 # warning "seccomp syscall number unknown for this architecture"
149 # define __NR_seccomp 0xffff
153 #ifndef SECCOMP_SET_MODE_STRICT
154 #define SECCOMP_SET_MODE_STRICT 0
157 #ifndef SECCOMP_SET_MODE_FILTER
158 #define SECCOMP_SET_MODE_FILTER 1
161 #ifndef SECCOMP_GET_ACTION_AVAIL
162 #define SECCOMP_GET_ACTION_AVAIL 2
165 #ifndef SECCOMP_GET_NOTIF_SIZES
166 #define SECCOMP_GET_NOTIF_SIZES 3
169 #ifndef SECCOMP_FILTER_FLAG_TSYNC
170 #define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
173 #ifndef SECCOMP_FILTER_FLAG_LOG
174 #define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
177 #ifndef SECCOMP_FILTER_FLAG_SPEC_ALLOW
178 #define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
181 #ifndef PTRACE_SECCOMP_GET_METADATA
182 #define PTRACE_SECCOMP_GET_METADATA 0x420d
184 struct seccomp_metadata
{
185 __u64 filter_off
; /* Input: which filter */
186 __u64 flags
; /* Output: filter's flags */
190 #ifndef SECCOMP_FILTER_FLAG_NEW_LISTENER
191 #define SECCOMP_FILTER_FLAG_NEW_LISTENER (1UL << 3)
194 #ifndef SECCOMP_RET_USER_NOTIF
195 #define SECCOMP_RET_USER_NOTIF 0x7fc00000U
197 #define SECCOMP_IOC_MAGIC '!'
198 #define SECCOMP_IO(nr) _IO(SECCOMP_IOC_MAGIC, nr)
199 #define SECCOMP_IOR(nr, type) _IOR(SECCOMP_IOC_MAGIC, nr, type)
200 #define SECCOMP_IOW(nr, type) _IOW(SECCOMP_IOC_MAGIC, nr, type)
201 #define SECCOMP_IOWR(nr, type) _IOWR(SECCOMP_IOC_MAGIC, nr, type)
203 /* Flags for seccomp notification fd ioctl. */
204 #define SECCOMP_IOCTL_NOTIF_RECV SECCOMP_IOWR(0, struct seccomp_notif)
205 #define SECCOMP_IOCTL_NOTIF_SEND SECCOMP_IOWR(1, \
206 struct seccomp_notif_resp)
207 #define SECCOMP_IOCTL_NOTIF_ID_VALID SECCOMP_IOW(2, __u64)
209 struct seccomp_notif
{
213 struct seccomp_data data
;
216 struct seccomp_notif_resp
{
223 struct seccomp_notif_sizes
{
225 __u16 seccomp_notif_resp
;
230 #ifndef SECCOMP_IOCTL_NOTIF_ADDFD
231 /* On success, the return value is the remote process's added fd number */
232 #define SECCOMP_IOCTL_NOTIF_ADDFD SECCOMP_IOW(3, \
233 struct seccomp_notif_addfd)
235 /* valid flags for seccomp_notif_addfd */
236 #define SECCOMP_ADDFD_FLAG_SETFD (1UL << 0) /* Specify remote fd */
238 struct seccomp_notif_addfd
{
247 #ifndef SECCOMP_ADDFD_FLAG_SEND
248 #define SECCOMP_ADDFD_FLAG_SEND (1UL << 1) /* Addfd and return it, atomically */
251 struct seccomp_notif_addfd_small
{
255 #define SECCOMP_IOCTL_NOTIF_ADDFD_SMALL \
256 SECCOMP_IOW(3, struct seccomp_notif_addfd_small)
258 struct seccomp_notif_addfd_big
{
260 struct seccomp_notif_addfd addfd
;
261 char buf
[sizeof(struct seccomp_notif_addfd
) + 8];
264 #define SECCOMP_IOCTL_NOTIF_ADDFD_BIG \
265 SECCOMP_IOWR(3, struct seccomp_notif_addfd_big)
267 #ifndef PTRACE_EVENTMSG_SYSCALL_ENTRY
268 #define PTRACE_EVENTMSG_SYSCALL_ENTRY 1
269 #define PTRACE_EVENTMSG_SYSCALL_EXIT 2
272 #ifndef SECCOMP_USER_NOTIF_FLAG_CONTINUE
273 #define SECCOMP_USER_NOTIF_FLAG_CONTINUE 0x00000001
276 #ifndef SECCOMP_FILTER_FLAG_TSYNC_ESRCH
277 #define SECCOMP_FILTER_FLAG_TSYNC_ESRCH (1UL << 4)
280 #ifndef SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV
281 #define SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV (1UL << 5)
285 int seccomp(unsigned int op
, unsigned int flags
, void *args
)
288 return syscall(__NR_seccomp
, op
, flags
, args
);
292 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
293 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
294 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
295 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]) + sizeof(__u32))
297 #error "wut? Unknown __BYTE_ORDER__?!"
300 #define SIBLING_EXIT_UNKILLED 0xbadbeef
301 #define SIBLING_EXIT_FAILURE 0xbadface
302 #define SIBLING_EXIT_NEWPRIVS 0xbadfeed
304 static int __filecmp(pid_t pid1
, pid_t pid2
, int fd1
, int fd2
)
308 return syscall(__NR_kcmp
, pid1
, pid2
, KCMP_FILE
, fd1
, fd2
);
315 /* Have TH_LOG report actual location filecmp() is used. */
316 #define filecmp(pid1, pid2, fd1, fd2) ({ \
319 _ret = __filecmp(pid1, pid2, fd1, fd2); \
321 if (_ret < 0 && errno == ENOSYS) { \
322 TH_LOG("kcmp() syscall missing (test is less accurate)");\
332 ret
= __filecmp(getpid(), getpid(), 1, 1);
334 if (ret
!= 0 && errno
== ENOSYS
)
335 SKIP(return, "Kernel does not support kcmp() (missing CONFIG_KCMP?)");
338 TEST(mode_strict_support
)
342 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
344 TH_LOG("Kernel does not support CONFIG_SECCOMP");
346 syscall(__NR_exit
, 0);
349 TEST_SIGNAL(mode_strict_cannot_call_prctl
, SIGKILL
)
353 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
355 TH_LOG("Kernel does not support CONFIG_SECCOMP");
357 syscall(__NR_prctl
, PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
360 TH_LOG("Unreachable!");
364 /* Note! This doesn't test no new privs behavior */
365 TEST(no_new_privs_support
)
369 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
371 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
375 /* Tests kernel support by checking for a copy_from_user() fault on NULL. */
376 TEST(mode_filter_support
)
380 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
382 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
384 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, NULL
, NULL
);
386 EXPECT_EQ(EFAULT
, errno
) {
387 TH_LOG("Kernel does not support CONFIG_SECCOMP_FILTER!");
391 TEST(mode_filter_without_nnp
)
393 struct sock_filter filter
[] = {
394 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
396 struct sock_fprog prog
= {
397 .len
= (unsigned short)ARRAY_SIZE(filter
),
401 cap_t cap
= cap_get_proc();
402 cap_flag_value_t is_cap_sys_admin
= 0;
404 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, NULL
, 0, 0);
406 TH_LOG("Expected 0 or unsupported for NO_NEW_PRIVS");
409 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
410 /* Succeeds with CAP_SYS_ADMIN, fails without */
411 cap_get_flag(cap
, CAP_SYS_ADMIN
, CAP_EFFECTIVE
, &is_cap_sys_admin
);
412 if (!is_cap_sys_admin
) {
414 EXPECT_EQ(EACCES
, errno
);
420 #define MAX_INSNS_PER_PATH 32768
422 TEST(filter_size_limits
)
425 int count
= BPF_MAXINSNS
+ 1;
426 struct sock_filter allow
[] = {
427 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
429 struct sock_filter
*filter
;
430 struct sock_fprog prog
= { };
433 filter
= calloc(count
, sizeof(*filter
));
434 ASSERT_NE(NULL
, filter
);
436 for (i
= 0; i
< count
; i
++)
437 filter
[i
] = allow
[0];
439 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
442 prog
.filter
= filter
;
445 /* Too many filter instructions in a single filter. */
446 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
448 TH_LOG("Installing %d insn filter was allowed", prog
.len
);
451 /* One less is okay, though. */
453 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
455 TH_LOG("Installing %d insn filter wasn't allowed", prog
.len
);
459 TEST(filter_chain_limits
)
462 int count
= BPF_MAXINSNS
;
463 struct sock_filter allow
[] = {
464 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
466 struct sock_filter
*filter
;
467 struct sock_fprog prog
= { };
470 filter
= calloc(count
, sizeof(*filter
));
471 ASSERT_NE(NULL
, filter
);
473 for (i
= 0; i
< count
; i
++)
474 filter
[i
] = allow
[0];
476 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
479 prog
.filter
= filter
;
482 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
487 /* Too many total filter instructions. */
488 for (i
= 0; i
< MAX_INSNS_PER_PATH
; i
++) {
489 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
494 TH_LOG("Allowed %d %d-insn filters (total with penalties:%d)",
495 i
, count
, i
* (count
+ 4));
499 TEST(mode_filter_cannot_move_to_strict
)
501 struct sock_filter filter
[] = {
502 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
504 struct sock_fprog prog
= {
505 .len
= (unsigned short)ARRAY_SIZE(filter
),
510 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
513 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
516 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, 0, 0);
518 EXPECT_EQ(EINVAL
, errno
);
522 TEST(mode_filter_get_seccomp
)
524 struct sock_filter filter
[] = {
525 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
527 struct sock_fprog prog
= {
528 .len
= (unsigned short)ARRAY_SIZE(filter
),
533 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
536 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
539 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
542 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
549 struct sock_filter filter
[] = {
550 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
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
);
567 struct sock_filter filter
[] = {
569 struct sock_fprog prog
= {
570 .len
= (unsigned short)ARRAY_SIZE(filter
),
575 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
578 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
580 EXPECT_EQ(EINVAL
, errno
);
585 struct sock_filter filter
[] = {
586 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_LOG
),
588 struct sock_fprog prog
= {
589 .len
= (unsigned short)ARRAY_SIZE(filter
),
593 pid_t parent
= getppid();
595 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
598 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
601 /* getppid() should succeed and be logged (no check for logging) */
602 EXPECT_EQ(parent
, syscall(__NR_getppid
));
605 TEST_SIGNAL(unknown_ret_is_kill_inside
, SIGSYS
)
607 struct sock_filter filter
[] = {
608 BPF_STMT(BPF_RET
|BPF_K
, 0x10000000U
),
610 struct sock_fprog prog
= {
611 .len
= (unsigned short)ARRAY_SIZE(filter
),
616 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
619 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
621 EXPECT_EQ(0, syscall(__NR_getpid
)) {
622 TH_LOG("getpid() shouldn't ever return");
626 /* return code >= 0x80000000 is unused. */
627 TEST_SIGNAL(unknown_ret_is_kill_above_allow
, SIGSYS
)
629 struct sock_filter filter
[] = {
630 BPF_STMT(BPF_RET
|BPF_K
, 0x90000000U
),
632 struct sock_fprog prog
= {
633 .len
= (unsigned short)ARRAY_SIZE(filter
),
638 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
641 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
643 EXPECT_EQ(0, syscall(__NR_getpid
)) {
644 TH_LOG("getpid() shouldn't ever return");
648 TEST_SIGNAL(KILL_all
, SIGSYS
)
650 struct sock_filter filter
[] = {
651 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
653 struct sock_fprog prog
= {
654 .len
= (unsigned short)ARRAY_SIZE(filter
),
659 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
662 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
666 TEST_SIGNAL(KILL_one
, SIGSYS
)
668 struct sock_filter filter
[] = {
669 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
670 offsetof(struct seccomp_data
, nr
)),
671 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
672 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
673 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
675 struct sock_fprog prog
= {
676 .len
= (unsigned short)ARRAY_SIZE(filter
),
680 pid_t parent
= getppid();
682 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
685 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
688 EXPECT_EQ(parent
, syscall(__NR_getppid
));
689 /* getpid() should never return. */
690 EXPECT_EQ(0, syscall(__NR_getpid
));
693 TEST_SIGNAL(KILL_one_arg_one
, SIGSYS
)
696 struct sock_filter filter
[] = {
697 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
698 offsetof(struct seccomp_data
, nr
)),
699 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_times
, 1, 0),
700 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
701 /* Only both with lower 32-bit for now. */
702 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(0)),
703 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
,
704 (unsigned long)&fatal_address
, 0, 1),
705 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
706 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
708 struct sock_fprog prog
= {
709 .len
= (unsigned short)ARRAY_SIZE(filter
),
713 pid_t parent
= getppid();
715 clock_t clock
= times(&timebuf
);
717 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
720 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
723 EXPECT_EQ(parent
, syscall(__NR_getppid
));
724 EXPECT_LE(clock
, syscall(__NR_times
, &timebuf
));
725 /* times() should never return. */
726 EXPECT_EQ(0, syscall(__NR_times
, &fatal_address
));
729 TEST_SIGNAL(KILL_one_arg_six
, SIGSYS
)
732 int sysno
= __NR_mmap
;
734 int sysno
= __NR_mmap2
;
736 struct sock_filter filter
[] = {
737 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
738 offsetof(struct seccomp_data
, nr
)),
739 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, sysno
, 1, 0),
740 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
741 /* Only both with lower 32-bit for now. */
742 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(5)),
743 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, 0x0C0FFEE, 0, 1),
744 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
745 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
747 struct sock_fprog prog
= {
748 .len
= (unsigned short)ARRAY_SIZE(filter
),
752 pid_t parent
= getppid();
755 int page_size
= sysconf(_SC_PAGESIZE
);
757 ASSERT_LT(0, page_size
);
759 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
762 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
765 fd
= open("/dev/zero", O_RDONLY
);
768 EXPECT_EQ(parent
, syscall(__NR_getppid
));
769 map1
= (void *)syscall(sysno
,
770 NULL
, page_size
, PROT_READ
, MAP_PRIVATE
, fd
, page_size
);
771 EXPECT_NE(MAP_FAILED
, map1
);
772 /* mmap2() should never return. */
773 map2
= (void *)syscall(sysno
,
774 NULL
, page_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0x0C0FFEE);
775 EXPECT_EQ(MAP_FAILED
, map2
);
777 /* The test failed, so clean up the resources. */
778 munmap(map1
, page_size
);
779 munmap(map2
, page_size
);
783 /* This is a thread task to die via seccomp filter violation. */
784 void *kill_thread(void *data
)
786 bool die
= (bool)data
;
789 syscall(__NR_getpid
);
790 return (void *)SIBLING_EXIT_FAILURE
;
793 return (void *)SIBLING_EXIT_UNKILLED
;
802 /* Prepare a thread that will kill itself or both of us. */
803 void kill_thread_or_group(struct __test_metadata
*_metadata
,
804 enum kill_t kill_how
)
808 /* Kill only when calling __NR_getpid. */
809 struct sock_filter filter_thread
[] = {
810 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
811 offsetof(struct seccomp_data
, nr
)),
812 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
813 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL_THREAD
),
814 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
816 struct sock_fprog prog_thread
= {
817 .len
= (unsigned short)ARRAY_SIZE(filter_thread
),
818 .filter
= filter_thread
,
820 int kill
= kill_how
== KILL_PROCESS
? SECCOMP_RET_KILL_PROCESS
: 0xAAAAAAAA;
821 struct sock_filter filter_process
[] = {
822 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
823 offsetof(struct seccomp_data
, nr
)),
824 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
825 BPF_STMT(BPF_RET
|BPF_K
, kill
),
826 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
828 struct sock_fprog prog_process
= {
829 .len
= (unsigned short)ARRAY_SIZE(filter_process
),
830 .filter
= filter_process
,
833 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
834 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
837 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0,
838 kill_how
== KILL_THREAD
? &prog_thread
842 * Add the KILL_THREAD rule again to make sure that the KILL_PROCESS
843 * flag cannot be downgraded by a new filter.
845 if (kill_how
== KILL_PROCESS
)
846 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog_thread
));
848 /* Start a thread that will exit immediately. */
849 ASSERT_EQ(0, pthread_create(&thread
, NULL
, kill_thread
, (void *)false));
850 ASSERT_EQ(0, pthread_join(thread
, &status
));
851 ASSERT_EQ(SIBLING_EXIT_UNKILLED
, (unsigned long)status
);
853 /* Start a thread that will die immediately. */
854 ASSERT_EQ(0, pthread_create(&thread
, NULL
, kill_thread
, (void *)true));
855 ASSERT_EQ(0, pthread_join(thread
, &status
));
856 ASSERT_NE(SIBLING_EXIT_FAILURE
, (unsigned long)status
);
859 * If we get here, only the spawned thread died. Let the parent know
860 * the whole process didn't die (i.e. this thread, the spawner,
872 ASSERT_LE(0, child_pid
);
873 if (child_pid
== 0) {
874 kill_thread_or_group(_metadata
, KILL_THREAD
);
878 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
880 /* If only the thread was killed, we'll see exit 42. */
881 ASSERT_TRUE(WIFEXITED(status
));
882 ASSERT_EQ(42, WEXITSTATUS(status
));
891 ASSERT_LE(0, child_pid
);
892 if (child_pid
== 0) {
893 kill_thread_or_group(_metadata
, KILL_PROCESS
);
897 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
899 /* If the entire process was killed, we'll see SIGSYS. */
900 ASSERT_TRUE(WIFSIGNALED(status
));
901 ASSERT_EQ(SIGSYS
, WTERMSIG(status
));
910 ASSERT_LE(0, child_pid
);
911 if (child_pid
== 0) {
912 kill_thread_or_group(_metadata
, RET_UNKNOWN
);
916 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
918 /* If the entire process was killed, we'll see SIGSYS. */
919 EXPECT_TRUE(WIFSIGNALED(status
)) {
920 TH_LOG("Unknown SECCOMP_RET is only killing the thread?");
922 ASSERT_EQ(SIGSYS
, WTERMSIG(status
));
925 /* TODO(wad) add 64-bit versus 32-bit arg tests. */
926 TEST(arg_out_of_range
)
928 struct sock_filter filter
[] = {
929 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(6)),
930 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
932 struct sock_fprog prog
= {
933 .len
= (unsigned short)ARRAY_SIZE(filter
),
938 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
941 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
943 EXPECT_EQ(EINVAL
, errno
);
946 #define ERRNO_FILTER(name, errno) \
947 struct sock_filter _read_filter_##name[] = { \
948 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, \
949 offsetof(struct seccomp_data, nr)), \
950 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), \
951 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | errno), \
952 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), \
954 struct sock_fprog prog_##name = { \
955 .len = (unsigned short)ARRAY_SIZE(_read_filter_##name), \
956 .filter = _read_filter_##name, \
959 /* Make sure basic errno values are correctly passed through a filter. */
962 ERRNO_FILTER(valid
, E2BIG
);
964 pid_t parent
= getppid();
966 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
969 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_valid
);
972 EXPECT_EQ(parent
, syscall(__NR_getppid
));
973 EXPECT_EQ(-1, read(-1, NULL
, 0));
974 EXPECT_EQ(E2BIG
, errno
);
977 /* Make sure an errno of zero is correctly handled by the arch code. */
980 ERRNO_FILTER(zero
, 0);
982 pid_t parent
= getppid();
984 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
987 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_zero
);
990 EXPECT_EQ(parent
, syscall(__NR_getppid
));
991 /* "errno" of 0 is ok. */
992 EXPECT_EQ(0, read(-1, NULL
, 0));
996 * The SECCOMP_RET_DATA mask is 16 bits wide, but errno is smaller.
997 * This tests that the errno value gets capped correctly, fixed by
998 * 580c57f10768 ("seccomp: cap SECCOMP_RET_ERRNO data to MAX_ERRNO").
1002 ERRNO_FILTER(capped
, 4096);
1004 pid_t parent
= getppid();
1006 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1009 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_capped
);
1012 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1013 EXPECT_EQ(-1, read(-1, NULL
, 0));
1014 EXPECT_EQ(4095, errno
);
1018 * Filters are processed in reverse order: last applied is executed first.
1019 * Since only the SECCOMP_RET_ACTION mask is tested for return values, the
1020 * SECCOMP_RET_DATA mask results will follow the most recently applied
1021 * matching filter return (and not the lowest or highest value).
1025 ERRNO_FILTER(first
, 11);
1026 ERRNO_FILTER(second
, 13);
1027 ERRNO_FILTER(third
, 12);
1029 pid_t parent
= getppid();
1031 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1034 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_first
);
1037 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_second
);
1040 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_third
);
1043 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1044 EXPECT_EQ(-1, read(-1, NULL
, 0));
1045 EXPECT_EQ(12, errno
);
1049 struct sock_fprog prog
;
1054 struct sock_filter filter
[] = {
1055 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1056 offsetof(struct seccomp_data
, nr
)),
1057 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
1058 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRAP
),
1059 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1062 memset(&self
->prog
, 0, sizeof(self
->prog
));
1063 self
->prog
.filter
= malloc(sizeof(filter
));
1064 ASSERT_NE(NULL
, self
->prog
.filter
);
1065 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1066 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1069 FIXTURE_TEARDOWN(TRAP
)
1071 if (self
->prog
.filter
)
1072 free(self
->prog
.filter
);
1075 TEST_F_SIGNAL(TRAP
, dfl
, SIGSYS
)
1079 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1082 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
1084 syscall(__NR_getpid
);
1087 /* Ensure that SIGSYS overrides SIG_IGN */
1088 TEST_F_SIGNAL(TRAP
, ign
, SIGSYS
)
1092 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1095 signal(SIGSYS
, SIG_IGN
);
1097 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
1099 syscall(__NR_getpid
);
1102 static siginfo_t TRAP_info
;
1103 static volatile int TRAP_nr
;
1104 static void TRAP_action(int nr
, siginfo_t
*info
, void *void_context
)
1106 memcpy(&TRAP_info
, info
, sizeof(TRAP_info
));
1110 TEST_F(TRAP
, handler
)
1113 struct sigaction act
;
1116 memset(&act
, 0, sizeof(act
));
1118 sigaddset(&mask
, SIGSYS
);
1120 act
.sa_sigaction
= &TRAP_action
;
1121 act
.sa_flags
= SA_SIGINFO
;
1122 ret
= sigaction(SIGSYS
, &act
, NULL
);
1124 TH_LOG("sigaction failed");
1126 ret
= sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
1128 TH_LOG("sigprocmask failed");
1131 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1133 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
1136 memset(&TRAP_info
, 0, sizeof(TRAP_info
));
1137 /* Expect the registers to be rolled back. (nr = error) may vary
1139 ret
= syscall(__NR_getpid
);
1140 /* Silence gcc warning about volatile. */
1142 EXPECT_EQ(SIGSYS
, test
);
1143 struct local_sigsys
{
1144 void *_call_addr
; /* calling user insn */
1145 int _syscall
; /* triggering system call number */
1146 unsigned int _arch
; /* AUDIT_ARCH_* of syscall */
1147 } *sigsys
= (struct local_sigsys
*)
1149 &(TRAP_info
.si_call_addr
);
1153 EXPECT_EQ(__NR_getpid
, sigsys
->_syscall
);
1154 /* Make sure arch is non-zero. */
1155 EXPECT_NE(0, sigsys
->_arch
);
1156 EXPECT_NE(0, (unsigned long)sigsys
->_call_addr
);
1159 FIXTURE(precedence
) {
1160 struct sock_fprog allow
;
1161 struct sock_fprog log
;
1162 struct sock_fprog trace
;
1163 struct sock_fprog error
;
1164 struct sock_fprog trap
;
1165 struct sock_fprog kill
;
1168 FIXTURE_SETUP(precedence
)
1170 struct sock_filter allow_insns
[] = {
1171 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1173 struct sock_filter log_insns
[] = {
1174 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1175 offsetof(struct seccomp_data
, nr
)),
1176 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1177 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1178 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_LOG
),
1180 struct sock_filter trace_insns
[] = {
1181 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1182 offsetof(struct seccomp_data
, nr
)),
1183 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1184 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1185 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
),
1187 struct sock_filter error_insns
[] = {
1188 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1189 offsetof(struct seccomp_data
, nr
)),
1190 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1191 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1192 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
),
1194 struct sock_filter trap_insns
[] = {
1195 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1196 offsetof(struct seccomp_data
, nr
)),
1197 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1198 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1199 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRAP
),
1201 struct sock_filter kill_insns
[] = {
1202 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1203 offsetof(struct seccomp_data
, nr
)),
1204 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1205 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1206 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1209 memset(self
, 0, sizeof(*self
));
1210 #define FILTER_ALLOC(_x) \
1211 self->_x.filter = malloc(sizeof(_x##_insns)); \
1212 ASSERT_NE(NULL, self->_x.filter); \
1213 memcpy(self->_x.filter, &_x##_insns, sizeof(_x##_insns)); \
1214 self->_x.len = (unsigned short)ARRAY_SIZE(_x##_insns)
1215 FILTER_ALLOC(allow
);
1217 FILTER_ALLOC(trace
);
1218 FILTER_ALLOC(error
);
1223 FIXTURE_TEARDOWN(precedence
)
1225 #define FILTER_FREE(_x) if (self->_x.filter) free(self->_x.filter)
1234 TEST_F(precedence
, allow_ok
)
1236 pid_t parent
, res
= 0;
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 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1253 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1255 /* Should work just fine. */
1256 res
= syscall(__NR_getppid
);
1257 EXPECT_EQ(parent
, res
);
1260 TEST_F_SIGNAL(precedence
, kill_is_highest
, SIGSYS
)
1262 pid_t parent
, res
= 0;
1266 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1269 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1271 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1273 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1275 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1277 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1279 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1281 /* Should work just fine. */
1282 res
= syscall(__NR_getppid
);
1283 EXPECT_EQ(parent
, res
);
1284 /* getpid() should never return. */
1285 res
= syscall(__NR_getpid
);
1289 TEST_F_SIGNAL(precedence
, kill_is_highest_in_any_order
, SIGSYS
)
1295 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1298 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1300 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1302 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1304 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1306 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1308 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1310 /* Should work just fine. */
1311 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1312 /* getpid() should never return. */
1313 EXPECT_EQ(0, syscall(__NR_getpid
));
1316 TEST_F_SIGNAL(precedence
, trap_is_second
, SIGSYS
)
1322 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1325 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1327 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1329 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1331 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1333 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1335 /* Should work just fine. */
1336 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1337 /* getpid() should never return. */
1338 EXPECT_EQ(0, syscall(__NR_getpid
));
1341 TEST_F_SIGNAL(precedence
, trap_is_second_in_any_order
, SIGSYS
)
1347 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1350 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1352 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1354 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1356 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1358 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1360 /* Should work just fine. */
1361 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1362 /* getpid() should never return. */
1363 EXPECT_EQ(0, syscall(__NR_getpid
));
1366 TEST_F(precedence
, errno_is_third
)
1372 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1375 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1377 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1379 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1381 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1383 /* Should work just fine. */
1384 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1385 EXPECT_EQ(0, syscall(__NR_getpid
));
1388 TEST_F(precedence
, errno_is_third_in_any_order
)
1394 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1397 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1399 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1401 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1403 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1405 /* Should work just fine. */
1406 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1407 EXPECT_EQ(0, syscall(__NR_getpid
));
1410 TEST_F(precedence
, trace_is_fourth
)
1416 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1419 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1421 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1423 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1425 /* Should work just fine. */
1426 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1428 EXPECT_EQ(-1, syscall(__NR_getpid
));
1431 TEST_F(precedence
, trace_is_fourth_in_any_order
)
1437 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1440 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1442 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1444 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1446 /* Should work just fine. */
1447 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1449 EXPECT_EQ(-1, syscall(__NR_getpid
));
1452 TEST_F(precedence
, log_is_fifth
)
1454 pid_t mypid
, parent
;
1459 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1462 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1464 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1466 /* Should work just fine. */
1467 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1468 /* Should also work just fine */
1469 EXPECT_EQ(mypid
, syscall(__NR_getpid
));
1472 TEST_F(precedence
, log_is_fifth_in_any_order
)
1474 pid_t mypid
, parent
;
1479 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1482 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1484 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1486 /* Should work just fine. */
1487 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1488 /* Should also work just fine */
1489 EXPECT_EQ(mypid
, syscall(__NR_getpid
));
1492 #ifndef PTRACE_O_TRACESECCOMP
1493 #define PTRACE_O_TRACESECCOMP 0x00000080
1496 /* Catch the Ubuntu 12.04 value error. */
1497 #if PTRACE_EVENT_SECCOMP != 7
1498 #undef PTRACE_EVENT_SECCOMP
1501 #ifndef PTRACE_EVENT_SECCOMP
1502 #define PTRACE_EVENT_SECCOMP 7
1505 #define PTRACE_EVENT_MASK(status) ((status) >> 16)
1506 bool tracer_running
;
1507 void tracer_stop(int sig
)
1509 tracer_running
= false;
1512 typedef void tracer_func_t(struct __test_metadata
*_metadata
,
1513 pid_t tracee
, int status
, void *args
);
1515 void start_tracer(struct __test_metadata
*_metadata
, int fd
, pid_t tracee
,
1516 tracer_func_t tracer_func
, void *args
, bool ptrace_syscall
)
1519 struct sigaction action
= {
1520 .sa_handler
= tracer_stop
,
1523 /* Allow external shutdown. */
1524 tracer_running
= true;
1525 ASSERT_EQ(0, sigaction(SIGUSR1
, &action
, NULL
));
1528 while (ret
== -1 && errno
!= EINVAL
)
1529 ret
= ptrace(PTRACE_ATTACH
, tracee
, NULL
, 0);
1531 kill(tracee
, SIGKILL
);
1533 /* Wait for attach stop */
1536 ret
= ptrace(PTRACE_SETOPTIONS
, tracee
, NULL
, ptrace_syscall
?
1537 PTRACE_O_TRACESYSGOOD
:
1538 PTRACE_O_TRACESECCOMP
);
1540 TH_LOG("Failed to set PTRACE_O_TRACESECCOMP");
1541 kill(tracee
, SIGKILL
);
1543 ret
= ptrace(ptrace_syscall
? PTRACE_SYSCALL
: PTRACE_CONT
,
1547 /* Unblock the tracee */
1548 ASSERT_EQ(1, write(fd
, "A", 1));
1549 ASSERT_EQ(0, close(fd
));
1551 /* Run until we're shut down. Must assert to stop execution. */
1552 while (tracer_running
) {
1555 if (wait(&status
) != tracee
)
1558 if (WIFSIGNALED(status
)) {
1559 /* Child caught a fatal signal. */
1562 if (WIFEXITED(status
)) {
1563 /* Child exited with code. */
1567 /* Check if we got an expected event. */
1568 ASSERT_EQ(WIFCONTINUED(status
), false);
1569 ASSERT_EQ(WIFSTOPPED(status
), true);
1570 ASSERT_EQ(WSTOPSIG(status
) & SIGTRAP
, SIGTRAP
) {
1571 TH_LOG("Unexpected WSTOPSIG: %d", WSTOPSIG(status
));
1574 tracer_func(_metadata
, tracee
, status
, args
);
1576 ret
= ptrace(ptrace_syscall
? PTRACE_SYSCALL
: PTRACE_CONT
,
1580 /* Directly report the status of our test harness results. */
1581 syscall(__NR_exit
, _metadata
->exit_code
);
1584 /* Common tracer setup/teardown functions. */
1585 void cont_handler(int num
)
1587 pid_t
setup_trace_fixture(struct __test_metadata
*_metadata
,
1588 tracer_func_t func
, void *args
, bool ptrace_syscall
)
1593 pid_t tracee
= getpid();
1595 /* Setup a pipe for clean synchronization. */
1596 ASSERT_EQ(0, pipe(pipefd
));
1598 /* Fork a child which we'll promote to tracer */
1599 tracer_pid
= fork();
1600 ASSERT_LE(0, tracer_pid
);
1601 signal(SIGALRM
, cont_handler
);
1602 if (tracer_pid
== 0) {
1604 start_tracer(_metadata
, pipefd
[1], tracee
, func
, args
,
1606 syscall(__NR_exit
, 0);
1609 prctl(PR_SET_PTRACER
, tracer_pid
, 0, 0, 0);
1610 read(pipefd
[0], &sync
, 1);
1616 void teardown_trace_fixture(struct __test_metadata
*_metadata
,
1622 * Extract the exit code from the other process and
1623 * adopt it for ourselves in case its asserts failed.
1625 ASSERT_EQ(0, kill(tracer
, SIGUSR1
));
1626 ASSERT_EQ(tracer
, waitpid(tracer
, &status
, 0));
1627 if (WEXITSTATUS(status
))
1628 _metadata
->exit_code
= KSFT_FAIL
;
1632 /* "poke" tracer arguments and function. */
1633 struct tracer_args_poke_t
{
1634 unsigned long poke_addr
;
1637 void tracer_poke(struct __test_metadata
*_metadata
, pid_t tracee
, int status
,
1642 struct tracer_args_poke_t
*info
= (struct tracer_args_poke_t
*)args
;
1644 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1646 /* If this fails, don't try to recover. */
1647 ASSERT_EQ(0x1001, msg
) {
1648 kill(tracee
, SIGKILL
);
1651 * Poke in the message.
1652 * Registers are not touched to try to keep this relatively arch
1655 ret
= ptrace(PTRACE_POKEDATA
, tracee
, info
->poke_addr
, 0x1001);
1659 FIXTURE(TRACE_poke
) {
1660 struct sock_fprog prog
;
1663 struct tracer_args_poke_t tracer_args
;
1666 FIXTURE_SETUP(TRACE_poke
)
1668 struct sock_filter filter
[] = {
1669 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1670 offsetof(struct seccomp_data
, nr
)),
1671 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
1672 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1001),
1673 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1677 memset(&self
->prog
, 0, sizeof(self
->prog
));
1678 self
->prog
.filter
= malloc(sizeof(filter
));
1679 ASSERT_NE(NULL
, self
->prog
.filter
);
1680 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1681 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1683 /* Set up tracer args. */
1684 self
->tracer_args
.poke_addr
= (unsigned long)&self
->poked
;
1686 /* Launch tracer. */
1687 self
->tracer
= setup_trace_fixture(_metadata
, tracer_poke
,
1688 &self
->tracer_args
, false);
1691 FIXTURE_TEARDOWN(TRACE_poke
)
1693 teardown_trace_fixture(_metadata
, self
->tracer
);
1694 if (self
->prog
.filter
)
1695 free(self
->prog
.filter
);
1698 TEST_F(TRACE_poke
, read_has_side_effects
)
1702 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1705 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1708 EXPECT_EQ(0, self
->poked
);
1709 ret
= read(-1, NULL
, 0);
1711 EXPECT_EQ(0x1001, self
->poked
);
1714 TEST_F(TRACE_poke
, getpid_runs_normally
)
1718 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1721 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1724 EXPECT_EQ(0, self
->poked
);
1725 EXPECT_NE(0, syscall(__NR_getpid
));
1726 EXPECT_EQ(0, self
->poked
);
1729 #if defined(__x86_64__)
1730 # define ARCH_REGS struct user_regs_struct
1731 # define SYSCALL_NUM(_regs) (_regs).orig_rax
1732 # define SYSCALL_RET(_regs) (_regs).rax
1733 #elif defined(__i386__)
1734 # define ARCH_REGS struct user_regs_struct
1735 # define SYSCALL_NUM(_regs) (_regs).orig_eax
1736 # define SYSCALL_RET(_regs) (_regs).eax
1737 #elif defined(__arm__)
1738 # define ARCH_REGS struct pt_regs
1739 # define SYSCALL_NUM(_regs) (_regs).ARM_r7
1740 # ifndef PTRACE_SET_SYSCALL
1741 # define PTRACE_SET_SYSCALL 23
1743 # define SYSCALL_NUM_SET(_regs, _nr) \
1744 EXPECT_EQ(0, ptrace(PTRACE_SET_SYSCALL, tracee, NULL, _nr))
1745 # define SYSCALL_RET(_regs) (_regs).ARM_r0
1746 #elif defined(__aarch64__)
1747 # define ARCH_REGS struct user_pt_regs
1748 # define SYSCALL_NUM(_regs) (_regs).regs[8]
1749 # ifndef NT_ARM_SYSTEM_CALL
1750 # define NT_ARM_SYSTEM_CALL 0x404
1752 # define SYSCALL_NUM_SET(_regs, _nr) \
1755 typeof(_nr) __nr = (_nr); \
1756 __v.iov_base = &__nr; \
1757 __v.iov_len = sizeof(__nr); \
1758 EXPECT_EQ(0, ptrace(PTRACE_SETREGSET, tracee, \
1759 NT_ARM_SYSTEM_CALL, &__v)); \
1761 # define SYSCALL_RET(_regs) (_regs).regs[0]
1762 #elif defined(__loongarch__)
1763 # define ARCH_REGS struct user_pt_regs
1764 # define SYSCALL_NUM(_regs) (_regs).regs[11]
1765 # define SYSCALL_RET(_regs) (_regs).regs[4]
1766 #elif defined(__riscv) && __riscv_xlen == 64
1767 # define ARCH_REGS struct user_regs_struct
1768 # define SYSCALL_NUM(_regs) (_regs).a7
1769 # define SYSCALL_RET(_regs) (_regs).a0
1770 #elif defined(__csky__)
1771 # define ARCH_REGS struct pt_regs
1772 # if defined(__CSKYABIV2__)
1773 # define SYSCALL_NUM(_regs) (_regs).regs[3]
1775 # define SYSCALL_NUM(_regs) (_regs).regs[9]
1777 # define SYSCALL_RET(_regs) (_regs).a0
1778 #elif defined(__hppa__)
1779 # define ARCH_REGS struct user_regs_struct
1780 # define SYSCALL_NUM(_regs) (_regs).gr[20]
1781 # define SYSCALL_RET(_regs) (_regs).gr[28]
1782 #elif defined(__powerpc__)
1783 # define ARCH_REGS struct pt_regs
1784 # define SYSCALL_NUM(_regs) (_regs).gpr[0]
1785 # define SYSCALL_RET(_regs) (_regs).gpr[3]
1786 # define SYSCALL_RET_SET(_regs, _val) \
1788 typeof(_val) _result = (_val); \
1789 if ((_regs.trap & 0xfff0) == 0x3000) { \
1791 * scv 0 system call uses -ve result \
1792 * for error, so no need to adjust. \
1794 SYSCALL_RET(_regs) = _result; \
1797 * A syscall error is signaled by the \
1798 * CR0 SO bit and the code is stored as \
1799 * a positive value. \
1801 if (_result < 0) { \
1802 SYSCALL_RET(_regs) = -_result; \
1803 (_regs).ccr |= 0x10000000; \
1805 SYSCALL_RET(_regs) = _result; \
1806 (_regs).ccr &= ~0x10000000; \
1810 # define SYSCALL_RET_SET_ON_PTRACE_EXIT
1811 #elif defined(__s390__)
1812 # define ARCH_REGS s390_regs
1813 # define SYSCALL_NUM(_regs) (_regs).gprs[2]
1814 # define SYSCALL_RET_SET(_regs, _val) \
1815 TH_LOG("Can't modify syscall return on this architecture")
1816 #elif defined(__mips__)
1817 # include <asm/unistd_nr_n32.h>
1818 # include <asm/unistd_nr_n64.h>
1819 # include <asm/unistd_nr_o32.h>
1820 # define ARCH_REGS struct pt_regs
1821 # define SYSCALL_NUM(_regs) \
1823 typeof((_regs).regs[2]) _nr; \
1824 if ((_regs).regs[2] == __NR_O32_Linux) \
1825 _nr = (_regs).regs[4]; \
1827 _nr = (_regs).regs[2]; \
1830 # define SYSCALL_NUM_SET(_regs, _nr) \
1832 if ((_regs).regs[2] == __NR_O32_Linux) \
1833 (_regs).regs[4] = _nr; \
1835 (_regs).regs[2] = _nr; \
1837 # define SYSCALL_RET_SET(_regs, _val) \
1838 TH_LOG("Can't modify syscall return on this architecture")
1839 #elif defined(__xtensa__)
1840 # define ARCH_REGS struct user_pt_regs
1841 # define SYSCALL_NUM(_regs) (_regs).syscall
1843 * On xtensa syscall return value is in the register
1844 * a2 of the current window which is not fixed.
1846 #define SYSCALL_RET(_regs) (_regs).a[(_regs).windowbase * 4 + 2]
1847 #elif defined(__sh__)
1848 # define ARCH_REGS struct pt_regs
1849 # define SYSCALL_NUM(_regs) (_regs).regs[3]
1850 # define SYSCALL_RET(_regs) (_regs).regs[0]
1851 #elif defined(__mc68000__)
1852 # define ARCH_REGS struct user_regs_struct
1853 # define SYSCALL_NUM(_regs) (_regs).orig_d0
1854 # define SYSCALL_RET(_regs) (_regs).d0
1856 # error "Do not know how to find your architecture's registers and syscalls"
1860 * Most architectures can change the syscall by just updating the
1861 * associated register. This is the default if not defined above.
1863 #ifndef SYSCALL_NUM_SET
1864 # define SYSCALL_NUM_SET(_regs, _nr) \
1866 SYSCALL_NUM(_regs) = (_nr); \
1870 * Most architectures can change the syscall return value by just
1871 * writing to the SYSCALL_RET register. This is the default if not
1872 * defined above. If an architecture cannot set the return value
1873 * (for example when the syscall and return value register is
1874 * shared), report it with TH_LOG() in an arch-specific definition
1875 * of SYSCALL_RET_SET() above, and leave SYSCALL_RET undefined.
1877 #if !defined(SYSCALL_RET) && !defined(SYSCALL_RET_SET)
1878 # error "One of SYSCALL_RET or SYSCALL_RET_SET is needed for this arch"
1880 #ifndef SYSCALL_RET_SET
1881 # define SYSCALL_RET_SET(_regs, _val) \
1883 SYSCALL_RET(_regs) = (_val); \
1887 /* When the syscall return can't be changed, stub out the tests for it. */
1889 # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(-1, action)
1891 # define EXPECT_SYSCALL_RETURN(val, action) \
1895 EXPECT_EQ(-1, action); \
1896 EXPECT_EQ(-(val), errno); \
1898 EXPECT_EQ(val, action); \
1904 * Some architectures (e.g. powerpc) can only set syscall
1905 * return values on syscall exit during ptrace.
1907 const bool ptrace_entry_set_syscall_nr
= true;
1908 const bool ptrace_entry_set_syscall_ret
=
1909 #ifndef SYSCALL_RET_SET_ON_PTRACE_EXIT
1916 * Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for
1917 * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux).
1919 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__) || defined(__mc68000__)
1920 # define ARCH_GETREGS(_regs) ptrace(PTRACE_GETREGS, tracee, 0, &(_regs))
1921 # define ARCH_SETREGS(_regs) ptrace(PTRACE_SETREGS, tracee, 0, &(_regs))
1923 # define ARCH_GETREGS(_regs) ({ \
1925 __v.iov_base = &(_regs); \
1926 __v.iov_len = sizeof(_regs); \
1927 ptrace(PTRACE_GETREGSET, tracee, NT_PRSTATUS, &__v); \
1929 # define ARCH_SETREGS(_regs) ({ \
1931 __v.iov_base = &(_regs); \
1932 __v.iov_len = sizeof(_regs); \
1933 ptrace(PTRACE_SETREGSET, tracee, NT_PRSTATUS, &__v); \
1937 /* Architecture-specific syscall fetching routine. */
1938 int get_syscall(struct __test_metadata
*_metadata
, pid_t tracee
)
1942 EXPECT_EQ(0, ARCH_GETREGS(regs
)) {
1946 return SYSCALL_NUM(regs
);
1949 /* Architecture-specific syscall changing routine. */
1950 void __change_syscall(struct __test_metadata
*_metadata
,
1951 pid_t tracee
, long *syscall
, long *ret
)
1953 ARCH_REGS orig
, regs
;
1955 /* Do not get/set registers if we have nothing to do. */
1956 if (!syscall
&& !ret
)
1959 EXPECT_EQ(0, ARCH_GETREGS(regs
)) {
1965 SYSCALL_NUM_SET(regs
, *syscall
);
1968 SYSCALL_RET_SET(regs
, *ret
);
1970 /* Flush any register changes made. */
1971 if (memcmp(&orig
, ®s
, sizeof(orig
)) != 0)
1972 EXPECT_EQ(0, ARCH_SETREGS(regs
));
1975 /* Change only syscall number. */
1976 void change_syscall_nr(struct __test_metadata
*_metadata
,
1977 pid_t tracee
, long syscall
)
1979 __change_syscall(_metadata
, tracee
, &syscall
, NULL
);
1982 /* Change syscall return value (and set syscall number to -1). */
1983 void change_syscall_ret(struct __test_metadata
*_metadata
,
1984 pid_t tracee
, long ret
)
1988 __change_syscall(_metadata
, tracee
, &syscall
, &ret
);
1991 void tracer_seccomp(struct __test_metadata
*_metadata
, pid_t tracee
,
1992 int status
, void *args
)
1997 EXPECT_EQ(PTRACE_EVENT_MASK(status
), PTRACE_EVENT_SECCOMP
) {
1998 TH_LOG("Unexpected ptrace event: %d", PTRACE_EVENT_MASK(status
));
2002 /* Make sure we got the right message. */
2003 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
2006 /* Validate and take action on expected syscalls. */
2009 /* change getpid to getppid. */
2010 EXPECT_EQ(__NR_getpid
, get_syscall(_metadata
, tracee
));
2011 change_syscall_nr(_metadata
, tracee
, __NR_getppid
);
2014 /* skip gettid with valid return code. */
2015 EXPECT_EQ(__NR_gettid
, get_syscall(_metadata
, tracee
));
2016 change_syscall_ret(_metadata
, tracee
, 45000);
2019 /* skip openat with error. */
2020 EXPECT_EQ(__NR_openat
, get_syscall(_metadata
, tracee
));
2021 change_syscall_ret(_metadata
, tracee
, -ESRCH
);
2024 /* do nothing (allow getppid) */
2025 EXPECT_EQ(__NR_getppid
, get_syscall(_metadata
, tracee
));
2029 TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg
);
2030 kill(tracee
, SIGKILL
);
2036 FIXTURE(TRACE_syscall
) {
2037 struct sock_fprog prog
;
2038 pid_t tracer
, mytid
, mypid
, parent
;
2042 void tracer_ptrace(struct __test_metadata
*_metadata
, pid_t tracee
,
2043 int status
, void *args
)
2048 long syscall_nr_val
, syscall_ret_val
;
2049 long *syscall_nr
= NULL
, *syscall_ret
= NULL
;
2050 FIXTURE_DATA(TRACE_syscall
) *self
= args
;
2052 EXPECT_EQ(WSTOPSIG(status
) & 0x80, 0x80) {
2053 TH_LOG("Unexpected WSTOPSIG: %d", WSTOPSIG(status
));
2058 * The traditional way to tell PTRACE_SYSCALL entry/exit
2063 /* Make sure we got an appropriate message. */
2064 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
2066 EXPECT_EQ(entry
? PTRACE_EVENTMSG_SYSCALL_ENTRY
2067 : PTRACE_EVENTMSG_SYSCALL_EXIT
, msg
);
2070 * Some architectures only support setting return values during
2071 * syscall exit under ptrace, and on exit the syscall number may
2072 * no longer be available. Therefore, save the initial sycall
2073 * number here, so it can be examined during both entry and exit
2077 self
->syscall_nr
= get_syscall(_metadata
, tracee
);
2080 * Depending on the architecture's syscall setting abilities, we
2081 * pick which things to set during this phase (entry or exit).
2083 if (entry
== ptrace_entry_set_syscall_nr
)
2084 syscall_nr
= &syscall_nr_val
;
2085 if (entry
== ptrace_entry_set_syscall_ret
)
2086 syscall_ret
= &syscall_ret_val
;
2088 /* Now handle the actual rewriting cases. */
2089 switch (self
->syscall_nr
) {
2091 syscall_nr_val
= __NR_getppid
;
2092 /* Never change syscall return for this case. */
2096 syscall_nr_val
= -1;
2097 syscall_ret_val
= 45000;
2100 syscall_nr_val
= -1;
2101 syscall_ret_val
= -ESRCH
;
2104 /* Unhandled, do nothing. */
2108 __change_syscall(_metadata
, tracee
, syscall_nr
, syscall_ret
);
2111 FIXTURE_VARIANT(TRACE_syscall
) {
2113 * All of the SECCOMP_RET_TRACE behaviors can be tested with either
2114 * SECCOMP_RET_TRACE+PTRACE_CONT or plain ptrace()+PTRACE_SYSCALL.
2115 * This indicates if we should use SECCOMP_RET_TRACE (false), or
2121 FIXTURE_VARIANT_ADD(TRACE_syscall
, ptrace
) {
2125 FIXTURE_VARIANT_ADD(TRACE_syscall
, seccomp
) {
2126 .use_ptrace
= false,
2129 FIXTURE_SETUP(TRACE_syscall
)
2131 struct sock_filter filter
[] = {
2132 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2133 offsetof(struct seccomp_data
, nr
)),
2134 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
2135 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1002),
2136 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_gettid
, 0, 1),
2137 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1003),
2138 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_openat
, 0, 1),
2139 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1004),
2140 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2141 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1005),
2142 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2144 struct sock_fprog prog
= {
2145 .len
= (unsigned short)ARRAY_SIZE(filter
),
2150 /* Prepare some testable syscall results. */
2151 self
->mytid
= syscall(__NR_gettid
);
2152 ASSERT_GT(self
->mytid
, 0);
2153 ASSERT_NE(self
->mytid
, 1) {
2154 TH_LOG("Running this test as init is not supported. :)");
2157 self
->mypid
= getpid();
2158 ASSERT_GT(self
->mypid
, 0);
2159 ASSERT_EQ(self
->mytid
, self
->mypid
);
2161 self
->parent
= getppid();
2162 ASSERT_GT(self
->parent
, 0);
2163 ASSERT_NE(self
->parent
, self
->mypid
);
2165 /* Launch tracer. */
2166 self
->tracer
= setup_trace_fixture(_metadata
,
2167 variant
->use_ptrace
? tracer_ptrace
2169 self
, variant
->use_ptrace
);
2171 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2174 /* Do not install seccomp rewrite filters, as we'll use ptrace instead. */
2175 if (variant
->use_ptrace
)
2178 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2182 FIXTURE_TEARDOWN(TRACE_syscall
)
2184 teardown_trace_fixture(_metadata
, self
->tracer
);
2187 TEST(negative_ENOSYS
)
2189 #if defined(__arm__)
2190 SKIP(return, "arm32 does not support calling syscall -1");
2193 * There should be no difference between an "internal" skip
2194 * and userspace asking for syscall "-1".
2197 EXPECT_EQ(-1, syscall(-1));
2198 EXPECT_EQ(errno
, ENOSYS
);
2199 /* And no difference for "still not valid but not -1". */
2201 EXPECT_EQ(-1, syscall(-101));
2202 EXPECT_EQ(errno
, ENOSYS
);
2205 TEST_F(TRACE_syscall
, negative_ENOSYS
)
2207 negative_ENOSYS(_metadata
);
2210 TEST_F(TRACE_syscall
, syscall_allowed
)
2212 /* getppid works as expected (no changes). */
2213 EXPECT_EQ(self
->parent
, syscall(__NR_getppid
));
2214 EXPECT_NE(self
->mypid
, syscall(__NR_getppid
));
2217 TEST_F(TRACE_syscall
, syscall_redirected
)
2219 /* getpid has been redirected to getppid as expected. */
2220 EXPECT_EQ(self
->parent
, syscall(__NR_getpid
));
2221 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
2224 TEST_F(TRACE_syscall
, syscall_errno
)
2226 /* Tracer should skip the open syscall, resulting in ESRCH. */
2227 EXPECT_SYSCALL_RETURN(-ESRCH
, syscall(__NR_openat
));
2230 TEST_F(TRACE_syscall
, syscall_faked
)
2232 /* Tracer skips the gettid syscall and store altered return value. */
2233 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid
));
2236 TEST_F_SIGNAL(TRACE_syscall
, kill_immediate
, SIGSYS
)
2238 struct sock_filter filter
[] = {
2239 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2240 offsetof(struct seccomp_data
, nr
)),
2241 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_mknodat
, 0, 1),
2242 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL_THREAD
),
2243 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2245 struct sock_fprog prog
= {
2246 .len
= (unsigned short)ARRAY_SIZE(filter
),
2251 /* Install "kill on mknodat" filter. */
2252 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2255 /* This should immediately die with SIGSYS, regardless of tracer. */
2256 EXPECT_EQ(-1, syscall(__NR_mknodat
, -1, NULL
, 0, 0));
2259 TEST_F(TRACE_syscall
, skip_after
)
2261 struct sock_filter filter
[] = {
2262 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2263 offsetof(struct seccomp_data
, nr
)),
2264 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2265 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
2266 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2268 struct sock_fprog prog
= {
2269 .len
= (unsigned short)ARRAY_SIZE(filter
),
2274 /* Install additional "errno on getppid" filter. */
2275 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2278 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
2280 EXPECT_EQ(-1, syscall(__NR_getpid
));
2281 EXPECT_EQ(EPERM
, errno
);
2284 TEST_F_SIGNAL(TRACE_syscall
, kill_after
, SIGSYS
)
2286 struct sock_filter filter
[] = {
2287 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2288 offsetof(struct seccomp_data
, nr
)),
2289 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2290 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2291 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2293 struct sock_fprog prog
= {
2294 .len
= (unsigned short)ARRAY_SIZE(filter
),
2299 /* Install additional "death on getppid" filter. */
2300 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2303 /* Tracer will redirect getpid to getppid, and we should die. */
2304 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
2307 TEST(seccomp_syscall
)
2309 struct sock_filter filter
[] = {
2310 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2312 struct sock_fprog prog
= {
2313 .len
= (unsigned short)ARRAY_SIZE(filter
),
2318 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2320 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2323 /* Reject insane operation. */
2324 ret
= seccomp(-1, 0, &prog
);
2325 ASSERT_NE(ENOSYS
, errno
) {
2326 TH_LOG("Kernel does not support seccomp syscall!");
2328 EXPECT_EQ(EINVAL
, errno
) {
2329 TH_LOG("Did not reject crazy op value!");
2332 /* Reject strict with flags or pointer. */
2333 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, -1, NULL
);
2334 EXPECT_EQ(EINVAL
, errno
) {
2335 TH_LOG("Did not reject mode strict with flags!");
2337 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, &prog
);
2338 EXPECT_EQ(EINVAL
, errno
) {
2339 TH_LOG("Did not reject mode strict with uargs!");
2342 /* Reject insane args for filter. */
2343 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, -1, &prog
);
2344 EXPECT_EQ(EINVAL
, errno
) {
2345 TH_LOG("Did not reject crazy filter flags!");
2347 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, NULL
);
2348 EXPECT_EQ(EFAULT
, errno
) {
2349 TH_LOG("Did not reject NULL filter!");
2352 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2353 EXPECT_EQ(0, errno
) {
2354 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s",
2359 TEST(seccomp_syscall_mode_lock
)
2361 struct sock_filter filter
[] = {
2362 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2364 struct sock_fprog prog
= {
2365 .len
= (unsigned short)ARRAY_SIZE(filter
),
2370 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2372 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2375 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2376 ASSERT_NE(ENOSYS
, errno
) {
2377 TH_LOG("Kernel does not support seccomp syscall!");
2380 TH_LOG("Could not install filter!");
2383 /* Make sure neither entry point will switch to strict. */
2384 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, 0, 0, 0);
2385 EXPECT_EQ(EINVAL
, errno
) {
2386 TH_LOG("Switched to mode strict!");
2389 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, NULL
);
2390 EXPECT_EQ(EINVAL
, errno
) {
2391 TH_LOG("Switched to mode strict!");
2396 * Test detection of known and unknown filter flags. Userspace needs to be able
2397 * to check if a filter flag is supported by the current kernel and a good way
2398 * of doing that is by attempting to enter filter mode, with the flag bit in
2399 * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates
2400 * that the flag is valid and EINVAL indicates that the flag is invalid.
2402 TEST(detect_seccomp_filter_flags
)
2404 unsigned int flags
[] = { SECCOMP_FILTER_FLAG_TSYNC
,
2405 SECCOMP_FILTER_FLAG_LOG
,
2406 SECCOMP_FILTER_FLAG_SPEC_ALLOW
,
2407 SECCOMP_FILTER_FLAG_NEW_LISTENER
,
2408 SECCOMP_FILTER_FLAG_TSYNC_ESRCH
};
2409 unsigned int exclusive
[] = {
2410 SECCOMP_FILTER_FLAG_TSYNC
,
2411 SECCOMP_FILTER_FLAG_NEW_LISTENER
};
2412 unsigned int flag
, all_flags
, exclusive_mask
;
2416 /* Test detection of individual known-good filter flags */
2417 for (i
= 0, all_flags
= 0; i
< ARRAY_SIZE(flags
); i
++) {
2421 /* Make sure the flag is a single bit! */
2430 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2431 ASSERT_NE(ENOSYS
, errno
) {
2432 TH_LOG("Kernel does not support seccomp syscall!");
2435 EXPECT_EQ(EFAULT
, errno
) {
2436 TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!",
2444 * Test detection of all known-good filter flags combined. But
2445 * for the exclusive flags we need to mask them out and try them
2446 * individually for the "all flags" testing.
2449 for (i
= 0; i
< ARRAY_SIZE(exclusive
); i
++)
2450 exclusive_mask
|= exclusive
[i
];
2451 for (i
= 0; i
< ARRAY_SIZE(exclusive
); i
++) {
2452 flag
= all_flags
& ~exclusive_mask
;
2453 flag
|= exclusive
[i
];
2455 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2457 EXPECT_EQ(EFAULT
, errno
) {
2458 TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2463 /* Test detection of an unknown filter flags, without exclusives. */
2465 flag
&= ~exclusive_mask
;
2466 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2468 EXPECT_EQ(EINVAL
, errno
) {
2469 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!",
2474 * Test detection of an unknown filter flag that may simply need to be
2475 * added to this test
2477 flag
= flags
[ARRAY_SIZE(flags
) - 1] << 1;
2478 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2480 EXPECT_EQ(EINVAL
, errno
) {
2481 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?",
2488 struct sock_filter filter
[] = {
2489 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2491 struct sock_fprog prog
= {
2492 .len
= (unsigned short)ARRAY_SIZE(filter
),
2497 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2499 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2502 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2504 ASSERT_NE(ENOSYS
, errno
) {
2505 TH_LOG("Kernel does not support seccomp syscall!");
2508 TH_LOG("Could not install initial filter with TSYNC!");
2512 #define TSYNC_SIBLINGS 2
2513 struct tsync_sibling
{
2517 pthread_cond_t
*cond
;
2518 pthread_mutex_t
*mutex
;
2521 struct sock_fprog
*prog
;
2522 struct __test_metadata
*metadata
;
2526 * To avoid joining joined threads (which is not allowed by Bionic),
2527 * make sure we both successfully join and clear the tid to skip a
2528 * later join attempt during fixture teardown. Any remaining threads
2529 * will be directly killed during teardown.
2531 #define PTHREAD_JOIN(tid, status) \
2533 int _rc = pthread_join(tid, status); \
2535 TH_LOG("pthread_join of tid %u failed: %d\n", \
2536 (unsigned int)tid, _rc); \
2543 struct sock_fprog root_prog
, apply_prog
;
2544 struct tsync_sibling sibling
[TSYNC_SIBLINGS
];
2546 pthread_cond_t cond
;
2547 pthread_mutex_t mutex
;
2551 FIXTURE_SETUP(TSYNC
)
2553 struct sock_filter root_filter
[] = {
2554 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2556 struct sock_filter apply_filter
[] = {
2557 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2558 offsetof(struct seccomp_data
, nr
)),
2559 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
2560 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2561 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2564 memset(&self
->root_prog
, 0, sizeof(self
->root_prog
));
2565 memset(&self
->apply_prog
, 0, sizeof(self
->apply_prog
));
2566 memset(&self
->sibling
, 0, sizeof(self
->sibling
));
2567 self
->root_prog
.filter
= malloc(sizeof(root_filter
));
2568 ASSERT_NE(NULL
, self
->root_prog
.filter
);
2569 memcpy(self
->root_prog
.filter
, &root_filter
, sizeof(root_filter
));
2570 self
->root_prog
.len
= (unsigned short)ARRAY_SIZE(root_filter
);
2572 self
->apply_prog
.filter
= malloc(sizeof(apply_filter
));
2573 ASSERT_NE(NULL
, self
->apply_prog
.filter
);
2574 memcpy(self
->apply_prog
.filter
, &apply_filter
, sizeof(apply_filter
));
2575 self
->apply_prog
.len
= (unsigned short)ARRAY_SIZE(apply_filter
);
2577 self
->sibling_count
= 0;
2578 pthread_mutex_init(&self
->mutex
, NULL
);
2579 pthread_cond_init(&self
->cond
, NULL
);
2580 sem_init(&self
->started
, 0, 0);
2581 self
->sibling
[0].tid
= 0;
2582 self
->sibling
[0].cond
= &self
->cond
;
2583 self
->sibling
[0].started
= &self
->started
;
2584 self
->sibling
[0].mutex
= &self
->mutex
;
2585 self
->sibling
[0].diverge
= 0;
2586 self
->sibling
[0].num_waits
= 1;
2587 self
->sibling
[0].prog
= &self
->root_prog
;
2588 self
->sibling
[0].metadata
= _metadata
;
2589 self
->sibling
[1].tid
= 0;
2590 self
->sibling
[1].cond
= &self
->cond
;
2591 self
->sibling
[1].started
= &self
->started
;
2592 self
->sibling
[1].mutex
= &self
->mutex
;
2593 self
->sibling
[1].diverge
= 0;
2594 self
->sibling
[1].prog
= &self
->root_prog
;
2595 self
->sibling
[1].num_waits
= 1;
2596 self
->sibling
[1].metadata
= _metadata
;
2599 FIXTURE_TEARDOWN(TSYNC
)
2603 if (self
->root_prog
.filter
)
2604 free(self
->root_prog
.filter
);
2605 if (self
->apply_prog
.filter
)
2606 free(self
->apply_prog
.filter
);
2608 for ( ; sib
< self
->sibling_count
; ++sib
) {
2609 struct tsync_sibling
*s
= &self
->sibling
[sib
];
2614 * If a thread is still running, it may be stuck, so hit
2615 * it over the head really hard.
2617 pthread_kill(s
->tid
, 9);
2619 pthread_mutex_destroy(&self
->mutex
);
2620 pthread_cond_destroy(&self
->cond
);
2621 sem_destroy(&self
->started
);
2624 void *tsync_sibling(void *data
)
2627 struct tsync_sibling
*me
= data
;
2629 me
->system_tid
= syscall(__NR_gettid
);
2631 pthread_mutex_lock(me
->mutex
);
2633 /* Just re-apply the root prog to fork the tree */
2634 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
2637 sem_post(me
->started
);
2638 /* Return outside of started so parent notices failures. */
2640 pthread_mutex_unlock(me
->mutex
);
2641 return (void *)SIBLING_EXIT_FAILURE
;
2644 pthread_cond_wait(me
->cond
, me
->mutex
);
2645 me
->num_waits
= me
->num_waits
- 1;
2646 } while (me
->num_waits
);
2647 pthread_mutex_unlock(me
->mutex
);
2649 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, 0, 0, 0);
2651 return (void *)SIBLING_EXIT_NEWPRIVS
;
2653 return (void *)SIBLING_EXIT_UNKILLED
;
2656 void tsync_start_sibling(struct tsync_sibling
*sibling
)
2658 pthread_create(&sibling
->tid
, NULL
, tsync_sibling
, (void *)sibling
);
2661 TEST_F(TSYNC
, siblings_fail_prctl
)
2665 struct sock_filter filter
[] = {
2666 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2667 offsetof(struct seccomp_data
, nr
)),
2668 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
2669 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EINVAL
),
2670 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2672 struct sock_fprog prog
= {
2673 .len
= (unsigned short)ARRAY_SIZE(filter
),
2677 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2678 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2681 /* Check prctl failure detection by requesting sib 0 diverge. */
2682 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2683 ASSERT_NE(ENOSYS
, errno
) {
2684 TH_LOG("Kernel does not support seccomp syscall!");
2687 TH_LOG("setting filter failed");
2690 self
->sibling
[0].diverge
= 1;
2691 tsync_start_sibling(&self
->sibling
[0]);
2692 tsync_start_sibling(&self
->sibling
[1]);
2694 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2695 sem_wait(&self
->started
);
2696 self
->sibling_count
++;
2699 /* Signal the threads to clean up*/
2700 pthread_mutex_lock(&self
->mutex
);
2701 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2702 TH_LOG("cond broadcast non-zero");
2704 pthread_mutex_unlock(&self
->mutex
);
2706 /* Ensure diverging sibling failed to call prctl. */
2707 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2708 EXPECT_EQ(SIBLING_EXIT_FAILURE
, (long)status
);
2709 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2710 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2713 TEST_F(TSYNC
, two_siblings_with_ancestor
)
2718 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2719 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2722 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2723 ASSERT_NE(ENOSYS
, errno
) {
2724 TH_LOG("Kernel does not support seccomp syscall!");
2727 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2729 tsync_start_sibling(&self
->sibling
[0]);
2730 tsync_start_sibling(&self
->sibling
[1]);
2732 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2733 sem_wait(&self
->started
);
2734 self
->sibling_count
++;
2737 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2740 TH_LOG("Could install filter on all threads!");
2742 /* Tell the siblings to test the policy */
2743 pthread_mutex_lock(&self
->mutex
);
2744 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2745 TH_LOG("cond broadcast non-zero");
2747 pthread_mutex_unlock(&self
->mutex
);
2748 /* Ensure they are both killed and don't exit cleanly. */
2749 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2750 EXPECT_EQ(0x0, (long)status
);
2751 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2752 EXPECT_EQ(0x0, (long)status
);
2755 TEST_F(TSYNC
, two_sibling_want_nnp
)
2759 /* start siblings before any prctl() operations */
2760 tsync_start_sibling(&self
->sibling
[0]);
2761 tsync_start_sibling(&self
->sibling
[1]);
2762 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2763 sem_wait(&self
->started
);
2764 self
->sibling_count
++;
2767 /* Tell the siblings to test no policy */
2768 pthread_mutex_lock(&self
->mutex
);
2769 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2770 TH_LOG("cond broadcast non-zero");
2772 pthread_mutex_unlock(&self
->mutex
);
2774 /* Ensure they are both upset about lacking nnp. */
2775 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2776 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2777 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2778 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2781 TEST_F(TSYNC
, two_siblings_with_no_filter
)
2786 /* start siblings before any prctl() operations */
2787 tsync_start_sibling(&self
->sibling
[0]);
2788 tsync_start_sibling(&self
->sibling
[1]);
2789 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2790 sem_wait(&self
->started
);
2791 self
->sibling_count
++;
2794 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2795 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2798 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2800 ASSERT_NE(ENOSYS
, errno
) {
2801 TH_LOG("Kernel does not support seccomp syscall!");
2804 TH_LOG("Could install filter on all threads!");
2807 /* Tell the siblings to test the policy */
2808 pthread_mutex_lock(&self
->mutex
);
2809 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2810 TH_LOG("cond broadcast non-zero");
2812 pthread_mutex_unlock(&self
->mutex
);
2814 /* Ensure they are both killed and don't exit cleanly. */
2815 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2816 EXPECT_EQ(0x0, (long)status
);
2817 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2818 EXPECT_EQ(0x0, (long)status
);
2821 TEST_F(TSYNC
, two_siblings_with_one_divergence
)
2826 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2827 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2830 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2831 ASSERT_NE(ENOSYS
, errno
) {
2832 TH_LOG("Kernel does not support seccomp syscall!");
2835 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2837 self
->sibling
[0].diverge
= 1;
2838 tsync_start_sibling(&self
->sibling
[0]);
2839 tsync_start_sibling(&self
->sibling
[1]);
2841 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2842 sem_wait(&self
->started
);
2843 self
->sibling_count
++;
2846 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2848 ASSERT_EQ(self
->sibling
[0].system_tid
, ret
) {
2849 TH_LOG("Did not fail on diverged sibling.");
2852 /* Wake the threads */
2853 pthread_mutex_lock(&self
->mutex
);
2854 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2855 TH_LOG("cond broadcast non-zero");
2857 pthread_mutex_unlock(&self
->mutex
);
2859 /* Ensure they are both unkilled. */
2860 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2861 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2862 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2863 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2866 TEST_F(TSYNC
, two_siblings_with_one_divergence_no_tid_in_err
)
2871 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2872 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2875 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2876 ASSERT_NE(ENOSYS
, errno
) {
2877 TH_LOG("Kernel does not support seccomp syscall!");
2880 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2882 self
->sibling
[0].diverge
= 1;
2883 tsync_start_sibling(&self
->sibling
[0]);
2884 tsync_start_sibling(&self
->sibling
[1]);
2886 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2887 sem_wait(&self
->started
);
2888 self
->sibling_count
++;
2891 flags
= SECCOMP_FILTER_FLAG_TSYNC
| \
2892 SECCOMP_FILTER_FLAG_TSYNC_ESRCH
;
2893 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flags
, &self
->apply_prog
);
2894 ASSERT_EQ(ESRCH
, errno
) {
2895 TH_LOG("Did not return ESRCH for diverged sibling.");
2897 ASSERT_EQ(-1, ret
) {
2898 TH_LOG("Did not fail on diverged sibling.");
2901 /* Wake the threads */
2902 pthread_mutex_lock(&self
->mutex
);
2903 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2904 TH_LOG("cond broadcast non-zero");
2906 pthread_mutex_unlock(&self
->mutex
);
2908 /* Ensure they are both unkilled. */
2909 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2910 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2911 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2912 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2915 TEST_F(TSYNC
, two_siblings_not_under_filter
)
2919 struct timespec delay
= { .tv_nsec
= 100000000 };
2921 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2922 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2926 * Sibling 0 will have its own seccomp policy
2927 * and Sibling 1 will not be under seccomp at
2928 * all. Sibling 1 will enter seccomp and 0
2929 * will cause failure.
2931 self
->sibling
[0].diverge
= 1;
2932 tsync_start_sibling(&self
->sibling
[0]);
2933 tsync_start_sibling(&self
->sibling
[1]);
2935 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2936 sem_wait(&self
->started
);
2937 self
->sibling_count
++;
2940 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2941 ASSERT_NE(ENOSYS
, errno
) {
2942 TH_LOG("Kernel does not support seccomp syscall!");
2945 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2948 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2950 ASSERT_EQ(ret
, self
->sibling
[0].system_tid
) {
2951 TH_LOG("Did not fail on diverged sibling.");
2954 if (ret
== self
->sibling
[0].system_tid
)
2957 pthread_mutex_lock(&self
->mutex
);
2959 /* Increment the other siblings num_waits so we can clean up
2960 * the one we just saw.
2962 self
->sibling
[!sib
].num_waits
+= 1;
2964 /* Signal the thread to clean up*/
2965 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2966 TH_LOG("cond broadcast non-zero");
2968 pthread_mutex_unlock(&self
->mutex
);
2969 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2970 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2971 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2972 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2973 nanosleep(&delay
, NULL
);
2974 /* Switch to the remaining sibling */
2977 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2980 TH_LOG("Expected the remaining sibling to sync");
2983 pthread_mutex_lock(&self
->mutex
);
2985 /* If remaining sibling didn't have a chance to wake up during
2986 * the first broadcast, manually reduce the num_waits now.
2988 if (self
->sibling
[sib
].num_waits
> 1)
2989 self
->sibling
[sib
].num_waits
= 1;
2990 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2991 TH_LOG("cond broadcast non-zero");
2993 pthread_mutex_unlock(&self
->mutex
);
2994 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2995 EXPECT_EQ(0, (long)status
);
2996 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2997 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2998 nanosleep(&delay
, NULL
);
3000 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
3002 ASSERT_EQ(0, ret
); /* just us chickens */
3005 /* Make sure restarted syscalls are seen directly as "restart_syscall". */
3006 TEST(syscall_restart
)
3013 siginfo_t info
= { };
3014 struct sock_filter filter
[] = {
3015 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
3016 offsetof(struct seccomp_data
, nr
)),
3018 #ifdef __NR_sigreturn
3019 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_sigreturn
, 7, 0),
3021 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 6, 0),
3022 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_exit
, 5, 0),
3023 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_rt_sigreturn
, 4, 0),
3024 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_nanosleep
, 5, 0),
3025 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_clock_nanosleep
, 4, 0),
3026 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_restart_syscall
, 4, 0),
3028 /* Allow __NR_write for easy logging. */
3029 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_write
, 0, 1),
3030 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3031 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
3032 /* The nanosleep jump target. */
3033 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x100),
3034 /* The restart_syscall jump target. */
3035 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x200),
3037 struct sock_fprog prog
= {
3038 .len
= (unsigned short)ARRAY_SIZE(filter
),
3041 #if defined(__arm__)
3042 struct utsname utsbuf
;
3045 ASSERT_EQ(0, pipe(pipefd
));
3048 ASSERT_LE(0, child_pid
);
3049 if (child_pid
== 0) {
3050 /* Child uses EXPECT not ASSERT to deliver status correctly. */
3052 struct timespec timeout
= { };
3054 /* Attach parent as tracer and stop. */
3055 EXPECT_EQ(0, ptrace(PTRACE_TRACEME
));
3056 EXPECT_EQ(0, raise(SIGSTOP
));
3058 EXPECT_EQ(0, close(pipefd
[1]));
3060 EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
3061 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3064 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
3066 TH_LOG("Failed to install filter!");
3069 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
3070 TH_LOG("Failed to read() sync from parent");
3072 EXPECT_EQ('.', buf
) {
3073 TH_LOG("Failed to get sync data from read()");
3076 /* Start nanosleep to be interrupted. */
3079 EXPECT_EQ(0, nanosleep(&timeout
, NULL
)) {
3080 TH_LOG("Call to nanosleep() failed (errno %d: %s)",
3081 errno
, strerror(errno
));
3084 /* Read final sync from parent. */
3085 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
3086 TH_LOG("Failed final read() from parent");
3088 EXPECT_EQ('!', buf
) {
3089 TH_LOG("Failed to get final data from read()");
3092 /* Directly report the status of our test harness results. */
3093 syscall(__NR_exit
, _metadata
->exit_code
);
3095 EXPECT_EQ(0, close(pipefd
[0]));
3097 /* Attach to child, setup options, and release. */
3098 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3099 ASSERT_EQ(true, WIFSTOPPED(status
));
3100 ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS
, child_pid
, NULL
,
3101 PTRACE_O_TRACESECCOMP
));
3102 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3103 ASSERT_EQ(1, write(pipefd
[1], ".", 1));
3105 /* Wait for nanosleep() to start. */
3106 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3107 ASSERT_EQ(true, WIFSTOPPED(status
));
3108 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
3109 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
3110 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
3111 ASSERT_EQ(0x100, msg
);
3112 ret
= get_syscall(_metadata
, child_pid
);
3113 EXPECT_TRUE(ret
== __NR_nanosleep
|| ret
== __NR_clock_nanosleep
);
3115 /* Might as well check siginfo for sanity while we're here. */
3116 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
3117 ASSERT_EQ(SIGTRAP
, info
.si_signo
);
3118 ASSERT_EQ(SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8), info
.si_code
);
3119 EXPECT_EQ(0, info
.si_errno
);
3120 EXPECT_EQ(getuid(), info
.si_uid
);
3121 /* Verify signal delivery came from child (seccomp-triggered). */
3122 EXPECT_EQ(child_pid
, info
.si_pid
);
3124 /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */
3125 ASSERT_EQ(0, kill(child_pid
, SIGSTOP
));
3126 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3127 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3128 ASSERT_EQ(true, WIFSTOPPED(status
));
3129 ASSERT_EQ(SIGSTOP
, WSTOPSIG(status
));
3130 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
3132 * There is no siginfo on SIGSTOP any more, so we can't verify
3133 * signal delivery came from parent now (getpid() == info.si_pid).
3134 * https://lkml.kernel.org/r/CAGXu5jJaZAOzP1qFz66tYrtbuywqb+UN2SOA1VLHpCCOiYvYeg@mail.gmail.com
3135 * At least verify the SIGSTOP via PTRACE_GETSIGINFO.
3137 EXPECT_EQ(SIGSTOP
, info
.si_signo
);
3139 /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */
3140 ASSERT_EQ(0, kill(child_pid
, SIGCONT
));
3141 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3142 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3143 ASSERT_EQ(true, WIFSTOPPED(status
));
3144 ASSERT_EQ(SIGCONT
, WSTOPSIG(status
));
3145 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3147 /* Wait for restart_syscall() to start. */
3148 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3149 ASSERT_EQ(true, WIFSTOPPED(status
));
3150 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
3151 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
3152 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
3154 ASSERT_EQ(0x200, msg
);
3155 ret
= get_syscall(_metadata
, child_pid
);
3156 #if defined(__arm__)
3159 * - native ARM registers do NOT expose true syscall.
3160 * - compat ARM registers on ARM64 DO expose true syscall.
3162 ASSERT_EQ(0, uname(&utsbuf
));
3163 if (strncmp(utsbuf
.machine
, "arm", 3) == 0) {
3164 EXPECT_EQ(__NR_nanosleep
, ret
);
3168 EXPECT_EQ(__NR_restart_syscall
, ret
);
3171 /* Write again to end test. */
3172 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3173 ASSERT_EQ(1, write(pipefd
[1], "!", 1));
3174 EXPECT_EQ(0, close(pipefd
[1]));
3176 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3177 if (WIFSIGNALED(status
) || WEXITSTATUS(status
))
3178 _metadata
->exit_code
= KSFT_FAIL
;
3181 TEST_SIGNAL(filter_flag_log
, SIGSYS
)
3183 struct sock_filter allow_filter
[] = {
3184 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3186 struct sock_filter kill_filter
[] = {
3187 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
3188 offsetof(struct seccomp_data
, nr
)),
3189 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
3190 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
3191 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3193 struct sock_fprog allow_prog
= {
3194 .len
= (unsigned short)ARRAY_SIZE(allow_filter
),
3195 .filter
= allow_filter
,
3197 struct sock_fprog kill_prog
= {
3198 .len
= (unsigned short)ARRAY_SIZE(kill_filter
),
3199 .filter
= kill_filter
,
3202 pid_t parent
= getppid();
3204 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3207 /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */
3208 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, SECCOMP_FILTER_FLAG_LOG
,
3210 ASSERT_NE(ENOSYS
, errno
) {
3211 TH_LOG("Kernel does not support seccomp syscall!");
3214 TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!");
3216 EXPECT_EQ(EINVAL
, errno
) {
3217 TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!");
3220 /* Verify that a simple, permissive filter can be added with no flags */
3221 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &allow_prog
);
3224 /* See if the same filter can be added with the FILTER_FLAG_LOG flag */
3225 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
3227 ASSERT_NE(EINVAL
, errno
) {
3228 TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!");
3232 /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */
3233 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
3237 EXPECT_EQ(parent
, syscall(__NR_getppid
));
3238 /* getpid() should never return. */
3239 EXPECT_EQ(0, syscall(__NR_getpid
));
3242 TEST(get_action_avail
)
3244 __u32 actions
[] = { SECCOMP_RET_KILL_THREAD
, SECCOMP_RET_TRAP
,
3245 SECCOMP_RET_ERRNO
, SECCOMP_RET_TRACE
,
3246 SECCOMP_RET_LOG
, SECCOMP_RET_ALLOW
};
3247 __u32 unknown_action
= 0x10000000U
;
3251 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[0]);
3252 ASSERT_NE(ENOSYS
, errno
) {
3253 TH_LOG("Kernel does not support seccomp syscall!");
3255 ASSERT_NE(EINVAL
, errno
) {
3256 TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!");
3260 for (i
= 0; i
< ARRAY_SIZE(actions
); i
++) {
3261 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[i
]);
3263 TH_LOG("Expected action (0x%X) not available!",
3268 /* Check that an unknown action is handled properly (EOPNOTSUPP) */
3269 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &unknown_action
);
3271 EXPECT_EQ(errno
, EOPNOTSUPP
);
3279 struct seccomp_metadata md
;
3282 /* Only real root can get metadata. */
3284 SKIP(return, "get_metadata requires real root");
3288 ASSERT_EQ(0, pipe(pipefd
));
3293 struct sock_filter filter
[] = {
3294 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3296 struct sock_fprog prog
= {
3297 .len
= (unsigned short)ARRAY_SIZE(filter
),
3301 /* one with log, one without */
3302 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
,
3303 SECCOMP_FILTER_FLAG_LOG
, &prog
));
3304 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
));
3306 EXPECT_EQ(0, close(pipefd
[0]));
3307 ASSERT_EQ(1, write(pipefd
[1], "1", 1));
3308 ASSERT_EQ(0, close(pipefd
[1]));
3314 ASSERT_EQ(0, close(pipefd
[1]));
3315 ASSERT_EQ(1, read(pipefd
[0], &buf
, 1));
3317 ASSERT_EQ(0, ptrace(PTRACE_ATTACH
, pid
));
3318 ASSERT_EQ(pid
, waitpid(pid
, NULL
, 0));
3320 /* Past here must not use ASSERT or child process is never killed. */
3324 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
3325 EXPECT_EQ(sizeof(md
), ret
) {
3326 if (errno
== EINVAL
)
3327 SKIP(goto skip
, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)");
3330 EXPECT_EQ(md
.flags
, SECCOMP_FILTER_FLAG_LOG
);
3331 EXPECT_EQ(md
.filter_off
, 0);
3334 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
3335 EXPECT_EQ(sizeof(md
), ret
);
3336 EXPECT_EQ(md
.flags
, 0);
3337 EXPECT_EQ(md
.filter_off
, 1);
3340 ASSERT_EQ(0, kill(pid
, SIGKILL
));
3343 static int user_notif_syscall(int nr
, unsigned int flags
)
3345 struct sock_filter filter
[] = {
3346 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
3347 offsetof(struct seccomp_data
, nr
)),
3348 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, nr
, 0, 1),
3349 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_USER_NOTIF
),
3350 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3353 struct sock_fprog prog
= {
3354 .len
= (unsigned short)ARRAY_SIZE(filter
),
3358 return seccomp(SECCOMP_SET_MODE_FILTER
, flags
, &prog
);
3361 #define USER_NOTIF_MAGIC INT_MAX
3362 TEST(user_notification_basic
)
3366 int status
, listener
;
3367 struct seccomp_notif req
= {};
3368 struct seccomp_notif_resp resp
= {};
3369 struct pollfd pollfd
;
3371 struct sock_filter filter
[] = {
3372 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3374 struct sock_fprog prog
= {
3375 .len
= (unsigned short)ARRAY_SIZE(filter
),
3379 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3381 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3387 /* Check that we get -ENOSYS with no listener attached */
3389 if (user_notif_syscall(__NR_getppid
, 0) < 0)
3391 ret
= syscall(__NR_getppid
);
3392 exit(ret
>= 0 || errno
!= ENOSYS
);
3395 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3396 EXPECT_EQ(true, WIFEXITED(status
));
3397 EXPECT_EQ(0, WEXITSTATUS(status
));
3399 /* Add some no-op filters for grins. */
3400 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3401 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3402 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3403 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3405 /* Check that the basic notification machinery works */
3406 listener
= user_notif_syscall(__NR_getppid
,
3407 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3408 ASSERT_GE(listener
, 0);
3410 /* Installing a second listener in the chain should EBUSY */
3411 EXPECT_EQ(user_notif_syscall(__NR_getppid
,
3412 SECCOMP_FILTER_FLAG_NEW_LISTENER
),
3414 EXPECT_EQ(errno
, EBUSY
);
3420 ret
= syscall(__NR_getppid
);
3421 exit(ret
!= USER_NOTIF_MAGIC
);
3424 pollfd
.fd
= listener
;
3425 pollfd
.events
= POLLIN
| POLLOUT
;
3427 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3428 EXPECT_EQ(pollfd
.revents
, POLLIN
);
3430 /* Test that we can't pass garbage to the kernel. */
3431 memset(&req
, 0, sizeof(req
));
3434 ret
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
);
3436 EXPECT_EQ(EINVAL
, errno
);
3440 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3443 pollfd
.fd
= listener
;
3444 pollfd
.events
= POLLIN
| POLLOUT
;
3446 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3447 EXPECT_EQ(pollfd
.revents
, POLLOUT
);
3449 EXPECT_EQ(req
.data
.nr
, __NR_getppid
);
3453 resp
.val
= USER_NOTIF_MAGIC
;
3455 /* check that we make sure flags == 0 */
3457 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3458 EXPECT_EQ(errno
, EINVAL
);
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
));
3468 TEST(user_notification_with_tsync
)
3473 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3475 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3478 /* these were exclusive */
3479 flags
= SECCOMP_FILTER_FLAG_NEW_LISTENER
|
3480 SECCOMP_FILTER_FLAG_TSYNC
;
3481 ASSERT_EQ(-1, user_notif_syscall(__NR_getppid
, flags
));
3482 ASSERT_EQ(EINVAL
, errno
);
3484 /* but now they're not */
3485 flags
|= SECCOMP_FILTER_FLAG_TSYNC_ESRCH
;
3486 ret
= user_notif_syscall(__NR_getppid
, flags
);
3491 TEST(user_notification_kill_in_middle
)
3496 struct seccomp_notif req
= {};
3497 struct seccomp_notif_resp resp
= {};
3499 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3501 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3504 listener
= user_notif_syscall(__NR_getppid
,
3505 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3506 ASSERT_GE(listener
, 0);
3509 * Check that nothing bad happens when we kill the task in the middle
3516 ret
= syscall(__NR_getppid
);
3517 exit(ret
!= USER_NOTIF_MAGIC
);
3520 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3521 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ID_VALID
, &req
.id
), 0);
3523 EXPECT_EQ(kill(pid
, SIGKILL
), 0);
3524 EXPECT_EQ(waitpid(pid
, NULL
, 0), pid
);
3526 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ID_VALID
, &req
.id
), -1);
3529 ret
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
);
3531 EXPECT_EQ(errno
, ENOENT
);
3534 static int handled
= -1;
3536 static void signal_handler(int signal
)
3538 if (write(handled
, "c", 1) != 1)
3539 perror("write from signal");
3542 TEST(user_notification_signal
)
3546 int status
, listener
, sk_pair
[2];
3547 struct seccomp_notif req
= {};
3548 struct seccomp_notif_resp resp
= {};
3551 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3553 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3556 ASSERT_EQ(socketpair(PF_LOCAL
, SOCK_SEQPACKET
, 0, sk_pair
), 0);
3558 listener
= user_notif_syscall(__NR_gettid
,
3559 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3560 ASSERT_GE(listener
, 0);
3567 handled
= sk_pair
[1];
3568 if (signal(SIGUSR1
, signal_handler
) == SIG_ERR
) {
3573 * ERESTARTSYS behavior is a bit hard to test, because we need
3574 * to rely on a signal that has not yet been handled. Let's at
3575 * least check that the error code gets propagated through, and
3576 * hope that it doesn't break when there is actually a signal :)
3578 ret
= syscall(__NR_gettid
);
3579 exit(!(ret
== -1 && errno
== 512));
3584 memset(&req
, 0, sizeof(req
));
3585 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3587 EXPECT_EQ(kill(pid
, SIGUSR1
), 0);
3590 * Make sure the signal really is delivered, which means we're not
3591 * stuck in the user notification code any more and the notification
3594 EXPECT_EQ(read(sk_pair
[0], &c
, 1), 1);
3597 resp
.error
= -EPERM
;
3600 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3601 EXPECT_EQ(errno
, ENOENT
);
3603 memset(&req
, 0, sizeof(req
));
3604 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3607 resp
.error
= -512; /* -ERESTARTSYS */
3610 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3612 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3613 EXPECT_EQ(true, WIFEXITED(status
));
3614 EXPECT_EQ(0, WEXITSTATUS(status
));
3617 TEST(user_notification_closed_listener
)
3621 int status
, listener
;
3623 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3625 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3628 listener
= user_notif_syscall(__NR_getppid
,
3629 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3630 ASSERT_GE(listener
, 0);
3633 * Check that we get an ENOSYS when the listener is closed.
3639 ret
= syscall(__NR_getppid
);
3640 exit(ret
!= -1 && errno
!= ENOSYS
);
3645 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3646 EXPECT_EQ(true, WIFEXITED(status
));
3647 EXPECT_EQ(0, WEXITSTATUS(status
));
3651 * Check that a pid in a child namespace still shows up as valid in ours.
3653 TEST(user_notification_child_pid_ns
)
3656 int status
, listener
;
3657 struct seccomp_notif req
= {};
3658 struct seccomp_notif_resp resp
= {};
3660 ASSERT_EQ(unshare(CLONE_NEWUSER
| CLONE_NEWPID
), 0) {
3661 if (errno
== EINVAL
)
3662 SKIP(return, "kernel missing CLONE_NEWUSER support");
3665 listener
= user_notif_syscall(__NR_getppid
,
3666 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3667 ASSERT_GE(listener
, 0);
3673 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3675 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3676 EXPECT_EQ(req
.pid
, pid
);
3680 resp
.val
= USER_NOTIF_MAGIC
;
3682 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3684 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3685 EXPECT_EQ(true, WIFEXITED(status
));
3686 EXPECT_EQ(0, WEXITSTATUS(status
));
3691 * Check that a pid in a sibling (i.e. unrelated) namespace shows up as 0, i.e.
3694 TEST(user_notification_sibling_pid_ns
)
3697 int status
, listener
;
3698 struct seccomp_notif req
= {};
3699 struct seccomp_notif_resp resp
= {};
3701 ASSERT_EQ(prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0), 0) {
3702 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3705 listener
= user_notif_syscall(__NR_getppid
,
3706 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3707 ASSERT_GE(listener
, 0);
3713 ASSERT_EQ(unshare(CLONE_NEWPID
), 0) {
3715 SKIP(return, "CLONE_NEWPID requires CAP_SYS_ADMIN");
3716 else if (errno
== EINVAL
)
3717 SKIP(return, "CLONE_NEWPID is invalid (missing CONFIG_PID_NS?)");
3724 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3726 EXPECT_EQ(waitpid(pid2
, &status
, 0), pid2
);
3727 EXPECT_EQ(true, WIFEXITED(status
));
3728 EXPECT_EQ(0, WEXITSTATUS(status
));
3729 exit(WEXITSTATUS(status
));
3732 /* Create the sibling ns, and sibling in it. */
3733 ASSERT_EQ(unshare(CLONE_NEWPID
), 0) {
3735 SKIP(return, "CLONE_NEWPID requires CAP_SYS_ADMIN");
3736 else if (errno
== EINVAL
)
3737 SKIP(return, "CLONE_NEWPID is invalid (missing CONFIG_PID_NS?)");
3739 ASSERT_EQ(errno
, 0);
3745 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3747 * The pid should be 0, i.e. the task is in some namespace that
3750 EXPECT_EQ(req
.pid
, 0);
3754 resp
.val
= USER_NOTIF_MAGIC
;
3756 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3762 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3763 EXPECT_EQ(true, WIFEXITED(status
));
3764 EXPECT_EQ(0, WEXITSTATUS(status
));
3766 EXPECT_EQ(waitpid(pid2
, &status
, 0), pid2
);
3767 EXPECT_EQ(true, WIFEXITED(status
));
3768 EXPECT_EQ(0, WEXITSTATUS(status
));
3771 TEST(user_notification_fault_recv
)
3774 int status
, listener
;
3775 struct seccomp_notif req
= {};
3776 struct seccomp_notif_resp resp
= {};
3778 ASSERT_EQ(unshare(CLONE_NEWUSER
), 0) {
3779 if (errno
== EINVAL
)
3780 SKIP(return, "kernel missing CLONE_NEWUSER support");
3783 listener
= user_notif_syscall(__NR_getppid
,
3784 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3785 ASSERT_GE(listener
, 0);
3791 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3793 /* Do a bad recv() */
3794 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, NULL
), -1);
3795 EXPECT_EQ(errno
, EFAULT
);
3797 /* We should still be able to receive this notification, though. */
3798 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3799 EXPECT_EQ(req
.pid
, pid
);
3803 resp
.val
= USER_NOTIF_MAGIC
;
3805 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3807 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3808 EXPECT_EQ(true, WIFEXITED(status
));
3809 EXPECT_EQ(0, WEXITSTATUS(status
));
3812 TEST(seccomp_get_notif_sizes
)
3814 struct seccomp_notif_sizes sizes
;
3816 ASSERT_EQ(seccomp(SECCOMP_GET_NOTIF_SIZES
, 0, &sizes
), 0);
3817 EXPECT_EQ(sizes
.seccomp_notif
, sizeof(struct seccomp_notif
));
3818 EXPECT_EQ(sizes
.seccomp_notif_resp
, sizeof(struct seccomp_notif_resp
));
3821 TEST(user_notification_continue
)
3825 int status
, listener
;
3826 struct seccomp_notif req
= {};
3827 struct seccomp_notif_resp resp
= {};
3828 struct pollfd pollfd
;
3830 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3832 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3835 listener
= user_notif_syscall(__NR_dup
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3836 ASSERT_GE(listener
, 0);
3842 int dup_fd
, pipe_fds
[2];
3845 ASSERT_GE(pipe(pipe_fds
), 0);
3847 dup_fd
= dup(pipe_fds
[0]);
3848 ASSERT_GE(dup_fd
, 0);
3849 EXPECT_NE(pipe_fds
[0], dup_fd
);
3852 ASSERT_EQ(filecmp(self
, self
, pipe_fds
[0], dup_fd
), 0);
3856 pollfd
.fd
= listener
;
3857 pollfd
.events
= POLLIN
| POLLOUT
;
3859 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3860 EXPECT_EQ(pollfd
.revents
, POLLIN
);
3862 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3864 pollfd
.fd
= listener
;
3865 pollfd
.events
= POLLIN
| POLLOUT
;
3867 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3868 EXPECT_EQ(pollfd
.revents
, POLLOUT
);
3870 EXPECT_EQ(req
.data
.nr
, __NR_dup
);
3873 resp
.flags
= SECCOMP_USER_NOTIF_FLAG_CONTINUE
;
3876 * Verify that setting SECCOMP_USER_NOTIF_FLAG_CONTINUE enforces other
3880 resp
.val
= USER_NOTIF_MAGIC
;
3881 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3882 EXPECT_EQ(errno
, EINVAL
);
3884 resp
.error
= USER_NOTIF_MAGIC
;
3886 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3887 EXPECT_EQ(errno
, EINVAL
);
3891 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0) {
3892 if (errno
== EINVAL
)
3893 SKIP(goto skip
, "Kernel does not support SECCOMP_USER_NOTIF_FLAG_CONTINUE");
3897 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3898 EXPECT_EQ(true, WIFEXITED(status
));
3899 EXPECT_EQ(0, WEXITSTATUS(status
)) {
3900 if (WEXITSTATUS(status
) == 2) {
3901 SKIP(return, "Kernel does not support kcmp() syscall");
3907 TEST(user_notification_filter_empty
)
3912 struct pollfd pollfd
;
3913 struct __clone_args args
= {
3914 .flags
= CLONE_FILES
,
3915 .exit_signal
= SIGCHLD
,
3918 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3920 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3923 if (__NR_clone3
< 0)
3924 SKIP(return, "Test not built with clone3 support");
3926 pid
= sys_clone3(&args
, sizeof(args
));
3932 listener
= user_notif_syscall(__NR_mknodat
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3934 _exit(EXIT_FAILURE
);
3936 if (dup2(listener
, 200) != 200)
3937 _exit(EXIT_FAILURE
);
3941 _exit(EXIT_SUCCESS
);
3944 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3945 EXPECT_EQ(true, WIFEXITED(status
));
3946 EXPECT_EQ(0, WEXITSTATUS(status
));
3949 * The seccomp filter has become unused so we should be notified once
3950 * the kernel gets around to cleaning up task struct.
3953 pollfd
.events
= POLLHUP
;
3955 EXPECT_GT(poll(&pollfd
, 1, 2000), 0);
3956 EXPECT_GT((pollfd
.revents
& POLLHUP
) ?: 0, 0);
3959 TEST(user_ioctl_notification_filter_empty
)
3964 struct __clone_args args
= {
3965 .flags
= CLONE_FILES
,
3966 .exit_signal
= SIGCHLD
,
3968 struct seccomp_notif req
= {};
3970 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3972 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3975 if (__NR_clone3
< 0)
3976 SKIP(return, "Test not built with clone3 support");
3978 ASSERT_EQ(0, pipe(p
));
3980 pid
= sys_clone3(&args
, sizeof(args
));
3986 listener
= user_notif_syscall(__NR_mknodat
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3988 _exit(EXIT_FAILURE
);
3990 if (dup2(listener
, 200) != 200)
3991 _exit(EXIT_FAILURE
);
3996 _exit(EXIT_SUCCESS
);
3998 if (read(p
[0], &status
, 1) != 0)
3999 _exit(EXIT_SUCCESS
);
4002 * The seccomp filter has become unused so we should be notified once
4003 * the kernel gets around to cleaning up task struct.
4005 EXPECT_EQ(ioctl(200, SECCOMP_IOCTL_NOTIF_RECV
, &req
), -1);
4006 EXPECT_EQ(errno
, ENOENT
);
4008 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4009 EXPECT_EQ(true, WIFEXITED(status
));
4010 EXPECT_EQ(0, WEXITSTATUS(status
));
4013 static void *do_thread(void *data
)
4018 TEST(user_notification_filter_empty_threaded
)
4023 struct pollfd pollfd
;
4024 struct __clone_args args
= {
4025 .flags
= CLONE_FILES
,
4026 .exit_signal
= SIGCHLD
,
4029 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4031 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4034 if (__NR_clone3
< 0)
4035 SKIP(return, "Test not built with clone3 support");
4037 pid
= sys_clone3(&args
, sizeof(args
));
4042 int listener
, status
;
4045 listener
= user_notif_syscall(__NR_dup
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
4047 _exit(EXIT_FAILURE
);
4049 if (dup2(listener
, 200) != 200)
4050 _exit(EXIT_FAILURE
);
4056 _exit(EXIT_FAILURE
);
4059 _exit(EXIT_SUCCESS
);
4063 _exit(EXIT_FAILURE
);
4066 _exit(EXIT_SUCCESS
);
4068 if (pthread_create(&thread
, NULL
, do_thread
, NULL
) ||
4069 pthread_join(thread
, NULL
))
4070 _exit(EXIT_FAILURE
);
4072 if (pthread_create(&thread
, NULL
, do_thread
, NULL
) ||
4073 pthread_join(thread
, NULL
))
4074 _exit(EXIT_FAILURE
);
4076 if (waitpid(pid1
, &status
, 0) != pid1
|| !WIFEXITED(status
) ||
4077 WEXITSTATUS(status
))
4078 _exit(EXIT_FAILURE
);
4080 if (waitpid(pid2
, &status
, 0) != pid2
|| !WIFEXITED(status
) ||
4081 WEXITSTATUS(status
))
4082 _exit(EXIT_FAILURE
);
4087 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4088 EXPECT_EQ(true, WIFEXITED(status
));
4089 EXPECT_EQ(0, WEXITSTATUS(status
));
4092 * The seccomp filter has become unused so we should be notified once
4093 * the kernel gets around to cleaning up task struct.
4096 pollfd
.events
= POLLHUP
;
4098 EXPECT_GT(poll(&pollfd
, 1, 2000), 0);
4099 EXPECT_GT((pollfd
.revents
& POLLHUP
) ?: 0, 0);
4103 int get_next_fd(int prev_fd
)
4105 for (int i
= prev_fd
+ 1; i
< FD_SETSIZE
; ++i
) {
4106 if (fcntl(i
, F_GETFD
) == -1)
4109 _exit(EXIT_FAILURE
);
4112 TEST(user_notification_addfd
)
4116 int status
, listener
, memfd
, fd
, nextfd
;
4117 struct seccomp_notif_addfd addfd
= {};
4118 struct seccomp_notif_addfd_small small
= {};
4119 struct seccomp_notif_addfd_big big
= {};
4120 struct seccomp_notif req
= {};
4121 struct seccomp_notif_resp resp
= {};
4123 struct timespec delay
= { .tv_nsec
= 100000000 };
4125 /* There may be arbitrary already-open fds at test start. */
4126 memfd
= memfd_create("test", 0);
4127 ASSERT_GE(memfd
, 0);
4128 nextfd
= get_next_fd(memfd
);
4130 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4132 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4136 /* Check that the basic notification machinery works */
4137 listener
= user_notif_syscall(__NR_getppid
,
4138 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
4139 ASSERT_EQ(listener
, nextfd
);
4140 nextfd
= get_next_fd(nextfd
);
4146 /* fds will be added and this value is expected */
4147 if (syscall(__NR_getppid
) != USER_NOTIF_MAGIC
)
4150 /* Atomic addfd+send is received here. Check it is a valid fd */
4151 if (fcntl(syscall(__NR_getppid
), F_GETFD
) == -1)
4154 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
4157 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4159 addfd
.srcfd
= memfd
;
4164 /* Verify bad newfd_flags cannot be set */
4165 addfd
.newfd_flags
= ~O_CLOEXEC
;
4166 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4167 EXPECT_EQ(errno
, EINVAL
);
4168 addfd
.newfd_flags
= O_CLOEXEC
;
4170 /* Verify bad flags cannot be set */
4172 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4173 EXPECT_EQ(errno
, EINVAL
);
4176 /* Verify that remote_fd cannot be set without setting flags */
4178 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4179 EXPECT_EQ(errno
, EINVAL
);
4182 /* Verify small size cannot be set */
4183 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD_SMALL
, &small
), -1);
4184 EXPECT_EQ(errno
, EINVAL
);
4186 /* Verify we can't send bits filled in unknown buffer area */
4187 memset(&big
, 0xAA, sizeof(big
));
4189 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD_BIG
, &big
), -1);
4190 EXPECT_EQ(errno
, E2BIG
);
4193 /* Verify we can set an arbitrary remote fd */
4194 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
);
4195 EXPECT_EQ(fd
, nextfd
);
4196 nextfd
= get_next_fd(nextfd
);
4197 EXPECT_EQ(filecmp(getpid(), pid
, memfd
, fd
), 0);
4199 /* Verify we can set an arbitrary remote fd with large size */
4200 memset(&big
, 0x0, sizeof(big
));
4202 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD_BIG
, &big
);
4203 EXPECT_EQ(fd
, nextfd
);
4204 nextfd
= get_next_fd(nextfd
);
4206 /* Verify we can set a specific remote fd */
4208 addfd
.flags
= SECCOMP_ADDFD_FLAG_SETFD
;
4209 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
);
4211 EXPECT_EQ(filecmp(getpid(), pid
, memfd
, fd
), 0);
4213 /* Resume syscall */
4216 resp
.val
= USER_NOTIF_MAGIC
;
4217 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4220 * This sets the ID of the ADD FD to the last request plus 1. The
4221 * notification ID increments 1 per notification.
4223 addfd
.id
= req
.id
+ 1;
4225 /* This spins until the underlying notification is generated */
4226 while (ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
) != -1 &&
4227 errno
!= -EINPROGRESS
)
4228 nanosleep(&delay
, NULL
);
4230 memset(&req
, 0, sizeof(req
));
4231 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4232 ASSERT_EQ(addfd
.id
, req
.id
);
4234 /* Verify we can do an atomic addfd and send */
4236 addfd
.flags
= SECCOMP_ADDFD_FLAG_SEND
;
4237 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
);
4239 * Child has earlier "low" fds and now 42, so we expect the next
4240 * lowest available fd to be assigned here.
4242 EXPECT_EQ(fd
, nextfd
);
4243 nextfd
= get_next_fd(nextfd
);
4244 ASSERT_EQ(filecmp(getpid(), pid
, memfd
, fd
), 0);
4247 * This sets the ID of the ADD FD to the last request plus 1. The
4248 * notification ID increments 1 per notification.
4250 addfd
.id
= req
.id
+ 1;
4252 /* This spins until the underlying notification is generated */
4253 while (ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
) != -1 &&
4254 errno
!= -EINPROGRESS
)
4255 nanosleep(&delay
, NULL
);
4257 memset(&req
, 0, sizeof(req
));
4258 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4259 ASSERT_EQ(addfd
.id
, req
.id
);
4263 resp
.val
= USER_NOTIF_MAGIC
;
4264 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4266 /* Wait for child to finish. */
4267 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4268 EXPECT_EQ(true, WIFEXITED(status
));
4269 EXPECT_EQ(0, WEXITSTATUS(status
));
4274 TEST(user_notification_addfd_rlimit
)
4278 int status
, listener
, memfd
;
4279 struct seccomp_notif_addfd addfd
= {};
4280 struct seccomp_notif req
= {};
4281 struct seccomp_notif_resp resp
= {};
4282 const struct rlimit lim
= {
4287 memfd
= memfd_create("test", 0);
4288 ASSERT_GE(memfd
, 0);
4290 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4292 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4295 /* Check that the basic notification machinery works */
4296 listener
= user_notif_syscall(__NR_getppid
,
4297 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
4298 ASSERT_GE(listener
, 0);
4304 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
4307 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4309 ASSERT_EQ(prlimit(pid
, RLIMIT_NOFILE
, &lim
, NULL
), 0);
4311 addfd
.srcfd
= memfd
;
4312 addfd
.newfd_flags
= O_CLOEXEC
;
4317 /* Should probably spot check /proc/sys/fs/file-nr */
4318 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4319 EXPECT_EQ(errno
, EMFILE
);
4321 addfd
.flags
= SECCOMP_ADDFD_FLAG_SEND
;
4322 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4323 EXPECT_EQ(errno
, EMFILE
);
4326 addfd
.flags
= SECCOMP_ADDFD_FLAG_SETFD
;
4327 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4328 EXPECT_EQ(errno
, EBADF
);
4332 resp
.val
= USER_NOTIF_MAGIC
;
4334 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4336 /* Wait for child to finish. */
4337 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4338 EXPECT_EQ(true, WIFEXITED(status
));
4339 EXPECT_EQ(0, WEXITSTATUS(status
));
4344 #ifndef SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP
4345 #define SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP (1UL << 0)
4346 #define SECCOMP_IOCTL_NOTIF_SET_FLAGS SECCOMP_IOW(4, __u64)
4349 TEST(user_notification_sync
)
4351 struct seccomp_notif req
= {};
4352 struct seccomp_notif_resp resp
= {};
4353 int status
, listener
;
4357 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4359 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4362 listener
= user_notif_syscall(__NR_getppid
,
4363 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
4364 ASSERT_GE(listener
, 0);
4366 /* Try to set invalid flags. */
4367 EXPECT_SYSCALL_RETURN(-EINVAL
,
4368 ioctl(listener
, SECCOMP_IOCTL_NOTIF_SET_FLAGS
, 0xffffffff, 0));
4370 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SET_FLAGS
,
4371 SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP
, 0), 0);
4376 ret
= syscall(__NR_getppid
);
4377 ASSERT_EQ(ret
, USER_NOTIF_MAGIC
) {
4384 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4386 ASSERT_EQ(req
.data
.nr
, __NR_getppid
);
4390 resp
.val
= USER_NOTIF_MAGIC
;
4392 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4394 ASSERT_EQ(waitpid(pid
, &status
, 0), pid
);
4395 ASSERT_EQ(status
, 0);
4399 /* Make sure PTRACE_O_SUSPEND_SECCOMP requires CAP_SYS_ADMIN. */
4400 FIXTURE(O_SUSPEND_SECCOMP
) {
4404 FIXTURE_SETUP(O_SUSPEND_SECCOMP
)
4406 ERRNO_FILTER(block_read
, E2BIG
);
4407 cap_value_t cap_list
[] = { CAP_SYS_ADMIN
};
4412 /* make sure we don't have CAP_SYS_ADMIN */
4413 caps
= cap_get_proc();
4414 ASSERT_NE(NULL
, caps
);
4415 ASSERT_EQ(0, cap_set_flag(caps
, CAP_EFFECTIVE
, 1, cap_list
, CAP_CLEAR
));
4416 ASSERT_EQ(0, cap_set_proc(caps
));
4419 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0));
4420 ASSERT_EQ(0, prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_block_read
));
4423 ASSERT_GE(self
->pid
, 0);
4425 if (self
->pid
== 0) {
4432 FIXTURE_TEARDOWN(O_SUSPEND_SECCOMP
)
4435 kill(self
->pid
, SIGKILL
);
4438 TEST_F(O_SUSPEND_SECCOMP
, setoptions
)
4442 ASSERT_EQ(0, ptrace(PTRACE_ATTACH
, self
->pid
, NULL
, 0));
4443 ASSERT_EQ(self
->pid
, wait(&wstatus
));
4444 ASSERT_EQ(-1, ptrace(PTRACE_SETOPTIONS
, self
->pid
, NULL
, PTRACE_O_SUSPEND_SECCOMP
));
4445 if (errno
== EINVAL
)
4446 SKIP(return, "Kernel does not support PTRACE_O_SUSPEND_SECCOMP (missing CONFIG_CHECKPOINT_RESTORE?)");
4447 ASSERT_EQ(EPERM
, errno
);
4450 TEST_F(O_SUSPEND_SECCOMP
, seize
)
4454 ret
= ptrace(PTRACE_SEIZE
, self
->pid
, NULL
, PTRACE_O_SUSPEND_SECCOMP
);
4456 if (errno
== EINVAL
)
4457 SKIP(return, "Kernel does not support PTRACE_O_SUSPEND_SECCOMP (missing CONFIG_CHECKPOINT_RESTORE?)");
4458 ASSERT_EQ(EPERM
, errno
);
4462 * get_nth - Get the nth, space separated entry in a file.
4464 * Returns the length of the read field.
4465 * Throws error if field is zero-lengthed.
4467 static ssize_t
get_nth(struct __test_metadata
*_metadata
, const char *path
,
4468 const unsigned int position
, char **entry
)
4476 f
= fopen(path
, "r");
4477 ASSERT_NE(f
, NULL
) {
4478 TH_LOG("Could not open %s: %s", path
, strerror(errno
));
4481 for (i
= 0; i
< position
; i
++) {
4482 nread
= getdelim(&line
, &len
, ' ', f
);
4483 ASSERT_GE(nread
, 0) {
4484 TH_LOG("Failed to read %d entry in file %s", i
, path
);
4489 ASSERT_GT(nread
, 0) {
4490 TH_LOG("Entry in file %s had zero length", path
);
4497 /* For a given PID, get the task state (D, R, etc...) */
4498 static char get_proc_stat(struct __test_metadata
*_metadata
, pid_t pid
)
4500 char proc_path
[100] = {0};
4504 snprintf(proc_path
, sizeof(proc_path
), "/proc/%d/stat", pid
);
4505 ASSERT_EQ(get_nth(_metadata
, proc_path
, 3, &line
), 1);
4513 TEST(user_notification_fifo
)
4515 struct seccomp_notif_resp resp
= {};
4516 struct seccomp_notif req
= {};
4517 int i
, status
, listener
;
4522 struct timespec delay
= { .tv_nsec
= 100000000 };
4524 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4526 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4529 /* Setup a listener */
4530 listener
= user_notif_syscall(__NR_getppid
,
4531 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
4532 ASSERT_GE(listener
, 0);
4538 ret
= syscall(__NR_getppid
);
4539 exit(ret
!= USER_NOTIF_MAGIC
);
4542 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4543 baseid
= req
.id
+ 1;
4547 resp
.val
= USER_NOTIF_MAGIC
;
4549 /* check that we make sure flags == 0 */
4550 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4552 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4553 EXPECT_EQ(true, WIFEXITED(status
));
4554 EXPECT_EQ(0, WEXITSTATUS(status
));
4556 /* Start children, and generate notifications */
4557 for (i
= 0; i
< ARRAY_SIZE(pids
); i
++) {
4560 ret
= syscall(__NR_getppid
);
4561 exit(ret
!= USER_NOTIF_MAGIC
);
4566 /* This spins until all of the children are sleeping */
4568 for (i
= 0; i
< ARRAY_SIZE(pids
); i
++) {
4569 if (get_proc_stat(_metadata
, pids
[i
]) != 'S') {
4570 nanosleep(&delay
, NULL
);
4575 /* Read the notifications in order (and respond) */
4576 for (i
= 0; i
< ARRAY_SIZE(pids
); i
++) {
4577 memset(&req
, 0, sizeof(req
));
4578 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4579 EXPECT_EQ(req
.id
, baseid
+ i
);
4581 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4584 /* Make sure notifications were received */
4585 for (i
= 0; i
< ARRAY_SIZE(pids
); i
++) {
4586 EXPECT_EQ(waitpid(pids
[i
], &status
, 0), pids
[i
]);
4587 EXPECT_EQ(true, WIFEXITED(status
));
4588 EXPECT_EQ(0, WEXITSTATUS(status
));
4592 /* get_proc_syscall - Get the syscall in progress for a given pid
4594 * Returns the current syscall number for a given process
4595 * Returns -1 if not in syscall (running or blocked)
4597 static long get_proc_syscall(struct __test_metadata
*_metadata
, int pid
)
4599 char proc_path
[100] = {0};
4604 snprintf(proc_path
, sizeof(proc_path
), "/proc/%d/syscall", pid
);
4605 nread
= get_nth(_metadata
, proc_path
, 1, &line
);
4606 ASSERT_GT(nread
, 0);
4608 if (!strncmp("running", line
, MIN(7, nread
)))
4609 ret
= strtol(line
, NULL
, 16);
4615 /* Ensure non-fatal signals prior to receive are unmodified */
4616 TEST(user_notification_wait_killable_pre_notification
)
4618 struct sigaction new_action
= {
4619 .sa_handler
= signal_handler
,
4621 int listener
, status
, sk_pair
[2];
4626 struct timespec delay
= { .tv_nsec
= 100000000 };
4628 ASSERT_EQ(sigemptyset(&new_action
.sa_mask
), 0);
4630 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4633 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4636 ASSERT_EQ(socketpair(PF_LOCAL
, SOCK_SEQPACKET
, 0, sk_pair
), 0);
4638 listener
= user_notif_syscall(
4639 __NR_getppid
, SECCOMP_FILTER_FLAG_NEW_LISTENER
|
4640 SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV
);
4641 ASSERT_GE(listener
, 0);
4644 * Check that we can kill the process with SIGUSR1 prior to receiving
4645 * the notification. SIGUSR1 is wired up to a custom signal handler,
4646 * and make sure it gets called.
4653 handled
= sk_pair
[1];
4655 /* Setup the non-fatal sigaction without SA_RESTART */
4656 if (sigaction(SIGUSR1
, &new_action
, NULL
)) {
4657 perror("sigaction");
4661 ret
= syscall(__NR_getppid
);
4662 /* Make sure we got a return from a signal interruption */
4663 exit(ret
!= -1 || errno
!= EINTR
);
4667 * Make sure we've gotten to the seccomp user notification wait
4668 * from getppid prior to sending any signals
4670 while (get_proc_syscall(_metadata
, pid
) != __NR_getppid
&&
4671 get_proc_stat(_metadata
, pid
) != 'S')
4672 nanosleep(&delay
, NULL
);
4674 /* Send non-fatal kill signal */
4675 EXPECT_EQ(kill(pid
, SIGUSR1
), 0);
4677 /* wait for process to exit (exit checks for EINTR) */
4678 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4679 EXPECT_EQ(true, WIFEXITED(status
));
4680 EXPECT_EQ(0, WEXITSTATUS(status
));
4682 EXPECT_EQ(read(sk_pair
[0], &c
, 1), 1);
4685 /* Ensure non-fatal signals after receive are blocked */
4686 TEST(user_notification_wait_killable
)
4688 struct sigaction new_action
= {
4689 .sa_handler
= signal_handler
,
4691 struct seccomp_notif_resp resp
= {};
4692 struct seccomp_notif req
= {};
4693 int listener
, status
, sk_pair
[2];
4698 struct timespec delay
= { .tv_nsec
= 100000000 };
4700 ASSERT_EQ(sigemptyset(&new_action
.sa_mask
), 0);
4702 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4705 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4708 ASSERT_EQ(socketpair(PF_LOCAL
, SOCK_SEQPACKET
, 0, sk_pair
), 0);
4710 listener
= user_notif_syscall(
4711 __NR_getppid
, SECCOMP_FILTER_FLAG_NEW_LISTENER
|
4712 SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV
);
4713 ASSERT_GE(listener
, 0);
4720 handled
= sk_pair
[1];
4722 /* Setup the sigaction without SA_RESTART */
4723 if (sigaction(SIGUSR1
, &new_action
, NULL
)) {
4724 perror("sigaction");
4728 /* Make sure that the syscall is completed (no EINTR) */
4729 ret
= syscall(__NR_getppid
);
4730 exit(ret
!= USER_NOTIF_MAGIC
);
4734 * Get the notification, to make move the notifying process into a
4735 * non-preemptible (TASK_KILLABLE) state.
4737 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4738 /* Send non-fatal kill signal */
4739 EXPECT_EQ(kill(pid
, SIGUSR1
), 0);
4742 * Make sure the task enters moves to TASK_KILLABLE by waiting for
4743 * D (Disk Sleep) state after receiving non-fatal signal.
4745 while (get_proc_stat(_metadata
, pid
) != 'D')
4746 nanosleep(&delay
, NULL
);
4749 resp
.val
= USER_NOTIF_MAGIC
;
4750 /* Make sure the notification is found and able to be replied to */
4751 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4754 * Make sure that the signal handler does get called once we're back in
4757 EXPECT_EQ(read(sk_pair
[0], &c
, 1), 1);
4758 /* wait for process to exit (exit checks for USER_NOTIF_MAGIC) */
4759 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4760 EXPECT_EQ(true, WIFEXITED(status
));
4761 EXPECT_EQ(0, WEXITSTATUS(status
));
4764 /* Ensure fatal signals after receive are not blocked */
4765 TEST(user_notification_wait_killable_fatal
)
4767 struct seccomp_notif req
= {};
4768 int listener
, status
;
4772 struct timespec delay
= { .tv_nsec
= 100000000 };
4774 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4777 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4780 listener
= user_notif_syscall(
4781 __NR_getppid
, SECCOMP_FILTER_FLAG_NEW_LISTENER
|
4782 SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV
);
4783 ASSERT_GE(listener
, 0);
4789 /* This should never complete as it should get a SIGTERM */
4790 syscall(__NR_getppid
);
4794 while (get_proc_stat(_metadata
, pid
) != 'S')
4795 nanosleep(&delay
, NULL
);
4798 * Get the notification, to make move the notifying process into a
4799 * non-preemptible (TASK_KILLABLE) state.
4801 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4802 /* Kill the process with a fatal signal */
4803 EXPECT_EQ(kill(pid
, SIGTERM
), 0);
4806 * Wait for the process to exit, and make sure the process terminated
4807 * due to the SIGTERM signal.
4809 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4810 EXPECT_EQ(true, WIFSIGNALED(status
));
4811 EXPECT_EQ(SIGTERM
, WTERMSIG(status
));
4814 struct tsync_vs_thread_leader_args
{
4818 static void *tsync_vs_dead_thread_leader_sibling(void *_args
)
4820 struct sock_filter allow_filter
[] = {
4821 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
4823 struct sock_fprog allow_prog
= {
4824 .len
= (unsigned short)ARRAY_SIZE(allow_filter
),
4825 .filter
= allow_filter
,
4827 struct tsync_vs_thread_leader_args
*args
= _args
;
4831 ret
= pthread_join(args
->leader
, &retval
);
4834 if (retval
!= _args
)
4836 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
, &allow_prog
);
4844 * Ensure that a dead thread leader doesn't prevent installing new filters with
4845 * SECCOMP_FILTER_FLAG_TSYNC from other threads.
4847 TEST(tsync_vs_dead_thread_leader
)
4853 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4855 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4862 struct sock_filter allow_filter
[] = {
4863 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
4865 struct sock_fprog allow_prog
= {
4866 .len
= (unsigned short)ARRAY_SIZE(allow_filter
),
4867 .filter
= allow_filter
,
4869 struct tsync_vs_thread_leader_args
*args
;
4872 args
= malloc(sizeof(*args
));
4873 ASSERT_NE(NULL
, args
);
4874 args
->leader
= pthread_self();
4876 ret
= pthread_create(&sibling
, NULL
,
4877 tsync_vs_dead_thread_leader_sibling
, args
);
4880 /* Install a new filter just to the leader thread. */
4881 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &allow_prog
);
4887 EXPECT_EQ(pid
, waitpid(pid
, &status
, 0));
4888 EXPECT_EQ(0, status
);
4893 * - expand NNP testing
4894 * - better arch-specific TRACE and TRAP handlers.
4895 * - endianness checking when appropriate
4896 * - 64-bit arg prodding
4897 * - arch value testing (x86 modes especially)
4898 * - verify that FILTER_FLAG_LOG filters generate log messages
4899 * - verify that RET_LOG generates log messages