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>
51 #include <sys/syscall.h>
54 #include "../kselftest_harness.h"
55 #include "../clone3/clone3_selftests.h"
57 /* Attempt to de-conflict with the selftests tree. */
59 #define SKIP(s, ...) XFAIL(s, ##__VA_ARGS__)
62 #ifndef PR_SET_PTRACER
63 # define PR_SET_PTRACER 0x59616d61
66 #ifndef PR_SET_NO_NEW_PRIVS
67 #define PR_SET_NO_NEW_PRIVS 38
68 #define PR_GET_NO_NEW_PRIVS 39
71 #ifndef PR_SECCOMP_EXT
72 #define PR_SECCOMP_EXT 43
75 #ifndef SECCOMP_EXT_ACT
76 #define SECCOMP_EXT_ACT 1
79 #ifndef SECCOMP_EXT_ACT_TSYNC
80 #define SECCOMP_EXT_ACT_TSYNC 1
83 #ifndef SECCOMP_MODE_STRICT
84 #define SECCOMP_MODE_STRICT 1
87 #ifndef SECCOMP_MODE_FILTER
88 #define SECCOMP_MODE_FILTER 2
91 #ifndef SECCOMP_RET_ALLOW
95 __u64 instruction_pointer
;
100 #ifndef SECCOMP_RET_KILL_PROCESS
101 #define SECCOMP_RET_KILL_PROCESS 0x80000000U /* kill the process */
102 #define SECCOMP_RET_KILL_THREAD 0x00000000U /* kill the thread */
104 #ifndef SECCOMP_RET_KILL
105 #define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD
106 #define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */
107 #define SECCOMP_RET_ERRNO 0x00050000U /* returns an errno */
108 #define SECCOMP_RET_TRACE 0x7ff00000U /* pass to a tracer or disallow */
109 #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
111 #ifndef SECCOMP_RET_LOG
112 #define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */
116 # if defined(__i386__)
117 # define __NR_seccomp 354
118 # elif defined(__x86_64__)
119 # define __NR_seccomp 317
120 # elif defined(__arm__)
121 # define __NR_seccomp 383
122 # elif defined(__aarch64__)
123 # define __NR_seccomp 277
124 # elif defined(__riscv)
125 # define __NR_seccomp 277
126 # elif defined(__csky__)
127 # define __NR_seccomp 277
128 # elif defined(__hppa__)
129 # define __NR_seccomp 338
130 # elif defined(__powerpc__)
131 # define __NR_seccomp 358
132 # elif defined(__s390__)
133 # define __NR_seccomp 348
134 # elif defined(__xtensa__)
135 # define __NR_seccomp 337
136 # elif defined(__sh__)
137 # define __NR_seccomp 372
139 # warning "seccomp syscall number unknown for this architecture"
140 # define __NR_seccomp 0xffff
144 #ifndef SECCOMP_SET_MODE_STRICT
145 #define SECCOMP_SET_MODE_STRICT 0
148 #ifndef SECCOMP_SET_MODE_FILTER
149 #define SECCOMP_SET_MODE_FILTER 1
152 #ifndef SECCOMP_GET_ACTION_AVAIL
153 #define SECCOMP_GET_ACTION_AVAIL 2
156 #ifndef SECCOMP_GET_NOTIF_SIZES
157 #define SECCOMP_GET_NOTIF_SIZES 3
160 #ifndef SECCOMP_FILTER_FLAG_TSYNC
161 #define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
164 #ifndef SECCOMP_FILTER_FLAG_LOG
165 #define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
168 #ifndef SECCOMP_FILTER_FLAG_SPEC_ALLOW
169 #define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
172 #ifndef PTRACE_SECCOMP_GET_METADATA
173 #define PTRACE_SECCOMP_GET_METADATA 0x420d
175 struct seccomp_metadata
{
176 __u64 filter_off
; /* Input: which filter */
177 __u64 flags
; /* Output: filter's flags */
181 #ifndef SECCOMP_FILTER_FLAG_NEW_LISTENER
182 #define SECCOMP_FILTER_FLAG_NEW_LISTENER (1UL << 3)
185 #ifndef SECCOMP_RET_USER_NOTIF
186 #define SECCOMP_RET_USER_NOTIF 0x7fc00000U
188 #define SECCOMP_IOC_MAGIC '!'
189 #define SECCOMP_IO(nr) _IO(SECCOMP_IOC_MAGIC, nr)
190 #define SECCOMP_IOR(nr, type) _IOR(SECCOMP_IOC_MAGIC, nr, type)
191 #define SECCOMP_IOW(nr, type) _IOW(SECCOMP_IOC_MAGIC, nr, type)
192 #define SECCOMP_IOWR(nr, type) _IOWR(SECCOMP_IOC_MAGIC, nr, type)
194 /* Flags for seccomp notification fd ioctl. */
195 #define SECCOMP_IOCTL_NOTIF_RECV SECCOMP_IOWR(0, struct seccomp_notif)
196 #define SECCOMP_IOCTL_NOTIF_SEND SECCOMP_IOWR(1, \
197 struct seccomp_notif_resp)
198 #define SECCOMP_IOCTL_NOTIF_ID_VALID SECCOMP_IOW(2, __u64)
200 struct seccomp_notif
{
204 struct seccomp_data data
;
207 struct seccomp_notif_resp
{
214 struct seccomp_notif_sizes
{
216 __u16 seccomp_notif_resp
;
221 #ifndef SECCOMP_IOCTL_NOTIF_ADDFD
222 /* On success, the return value is the remote process's added fd number */
223 #define SECCOMP_IOCTL_NOTIF_ADDFD SECCOMP_IOW(3, \
224 struct seccomp_notif_addfd)
226 /* valid flags for seccomp_notif_addfd */
227 #define SECCOMP_ADDFD_FLAG_SETFD (1UL << 0) /* Specify remote fd */
229 struct seccomp_notif_addfd
{
238 struct seccomp_notif_addfd_small
{
242 #define SECCOMP_IOCTL_NOTIF_ADDFD_SMALL \
243 SECCOMP_IOW(3, struct seccomp_notif_addfd_small)
245 struct seccomp_notif_addfd_big
{
247 struct seccomp_notif_addfd addfd
;
248 char buf
[sizeof(struct seccomp_notif_addfd
) + 8];
251 #define SECCOMP_IOCTL_NOTIF_ADDFD_BIG \
252 SECCOMP_IOWR(3, struct seccomp_notif_addfd_big)
254 #ifndef PTRACE_EVENTMSG_SYSCALL_ENTRY
255 #define PTRACE_EVENTMSG_SYSCALL_ENTRY 1
256 #define PTRACE_EVENTMSG_SYSCALL_EXIT 2
259 #ifndef SECCOMP_USER_NOTIF_FLAG_CONTINUE
260 #define SECCOMP_USER_NOTIF_FLAG_CONTINUE 0x00000001
263 #ifndef SECCOMP_FILTER_FLAG_TSYNC_ESRCH
264 #define SECCOMP_FILTER_FLAG_TSYNC_ESRCH (1UL << 4)
268 int seccomp(unsigned int op
, unsigned int flags
, void *args
)
271 return syscall(__NR_seccomp
, op
, flags
, args
);
275 #if __BYTE_ORDER == __LITTLE_ENDIAN
276 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
277 #elif __BYTE_ORDER == __BIG_ENDIAN
278 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]) + sizeof(__u32))
280 #error "wut? Unknown __BYTE_ORDER?!"
283 #define SIBLING_EXIT_UNKILLED 0xbadbeef
284 #define SIBLING_EXIT_FAILURE 0xbadface
285 #define SIBLING_EXIT_NEWPRIVS 0xbadfeed
287 static int __filecmp(pid_t pid1
, pid_t pid2
, int fd1
, int fd2
)
291 return syscall(__NR_kcmp
, pid1
, pid2
, KCMP_FILE
, fd1
, fd2
);
298 /* Have TH_LOG report actual location filecmp() is used. */
299 #define filecmp(pid1, pid2, fd1, fd2) ({ \
302 _ret = __filecmp(pid1, pid2, fd1, fd2); \
304 if (_ret < 0 && errno == ENOSYS) { \
305 TH_LOG("kcmp() syscall missing (test is less accurate)");\
315 ret
= __filecmp(getpid(), getpid(), 1, 1);
317 if (ret
!= 0 && errno
== ENOSYS
)
318 SKIP(return, "Kernel does not support kcmp() (missing CONFIG_CHECKPOINT_RESTORE?)");
321 TEST(mode_strict_support
)
325 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
327 TH_LOG("Kernel does not support CONFIG_SECCOMP");
329 syscall(__NR_exit
, 0);
332 TEST_SIGNAL(mode_strict_cannot_call_prctl
, SIGKILL
)
336 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, NULL
, NULL
);
338 TH_LOG("Kernel does not support CONFIG_SECCOMP");
340 syscall(__NR_prctl
, PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
343 TH_LOG("Unreachable!");
347 /* Note! This doesn't test no new privs behavior */
348 TEST(no_new_privs_support
)
352 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
354 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
358 /* Tests kernel support by checking for a copy_from_user() fault on NULL. */
359 TEST(mode_filter_support
)
363 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
365 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
367 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, NULL
, NULL
);
369 EXPECT_EQ(EFAULT
, errno
) {
370 TH_LOG("Kernel does not support CONFIG_SECCOMP_FILTER!");
374 TEST(mode_filter_without_nnp
)
376 struct sock_filter filter
[] = {
377 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
379 struct sock_fprog prog
= {
380 .len
= (unsigned short)ARRAY_SIZE(filter
),
385 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, NULL
, 0, 0);
387 TH_LOG("Expected 0 or unsupported for NO_NEW_PRIVS");
390 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
391 /* Succeeds with CAP_SYS_ADMIN, fails without */
392 /* TODO(wad) check caps not euid */
395 EXPECT_EQ(EACCES
, errno
);
401 #define MAX_INSNS_PER_PATH 32768
403 TEST(filter_size_limits
)
406 int count
= BPF_MAXINSNS
+ 1;
407 struct sock_filter allow
[] = {
408 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
410 struct sock_filter
*filter
;
411 struct sock_fprog prog
= { };
414 filter
= calloc(count
, sizeof(*filter
));
415 ASSERT_NE(NULL
, filter
);
417 for (i
= 0; i
< count
; i
++)
418 filter
[i
] = allow
[0];
420 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
423 prog
.filter
= filter
;
426 /* Too many filter instructions in a single filter. */
427 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
429 TH_LOG("Installing %d insn filter was allowed", prog
.len
);
432 /* One less is okay, though. */
434 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
436 TH_LOG("Installing %d insn filter wasn't allowed", prog
.len
);
440 TEST(filter_chain_limits
)
443 int count
= BPF_MAXINSNS
;
444 struct sock_filter allow
[] = {
445 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
447 struct sock_filter
*filter
;
448 struct sock_fprog prog
= { };
451 filter
= calloc(count
, sizeof(*filter
));
452 ASSERT_NE(NULL
, filter
);
454 for (i
= 0; i
< count
; i
++)
455 filter
[i
] = allow
[0];
457 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
460 prog
.filter
= filter
;
463 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
468 /* Too many total filter instructions. */
469 for (i
= 0; i
< MAX_INSNS_PER_PATH
; i
++) {
470 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
475 TH_LOG("Allowed %d %d-insn filters (total with penalties:%d)",
476 i
, count
, i
* (count
+ 4));
480 TEST(mode_filter_cannot_move_to_strict
)
482 struct sock_filter filter
[] = {
483 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
485 struct sock_fprog prog
= {
486 .len
= (unsigned short)ARRAY_SIZE(filter
),
491 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
494 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
497 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, NULL
, 0, 0);
499 EXPECT_EQ(EINVAL
, errno
);
503 TEST(mode_filter_get_seccomp
)
505 struct sock_filter filter
[] = {
506 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
508 struct sock_fprog prog
= {
509 .len
= (unsigned short)ARRAY_SIZE(filter
),
514 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
517 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
520 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
523 ret
= prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
530 struct sock_filter filter
[] = {
531 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
533 struct sock_fprog prog
= {
534 .len
= (unsigned short)ARRAY_SIZE(filter
),
539 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
542 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
548 struct sock_filter filter
[] = {
550 struct sock_fprog prog
= {
551 .len
= (unsigned short)ARRAY_SIZE(filter
),
556 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
559 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
561 EXPECT_EQ(EINVAL
, errno
);
566 struct sock_filter filter
[] = {
567 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_LOG
),
569 struct sock_fprog prog
= {
570 .len
= (unsigned short)ARRAY_SIZE(filter
),
574 pid_t parent
= getppid();
576 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
579 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
582 /* getppid() should succeed and be logged (no check for logging) */
583 EXPECT_EQ(parent
, syscall(__NR_getppid
));
586 TEST_SIGNAL(unknown_ret_is_kill_inside
, SIGSYS
)
588 struct sock_filter filter
[] = {
589 BPF_STMT(BPF_RET
|BPF_K
, 0x10000000U
),
591 struct sock_fprog prog
= {
592 .len
= (unsigned short)ARRAY_SIZE(filter
),
597 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
600 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
602 EXPECT_EQ(0, syscall(__NR_getpid
)) {
603 TH_LOG("getpid() shouldn't ever return");
607 /* return code >= 0x80000000 is unused. */
608 TEST_SIGNAL(unknown_ret_is_kill_above_allow
, SIGSYS
)
610 struct sock_filter filter
[] = {
611 BPF_STMT(BPF_RET
|BPF_K
, 0x90000000U
),
613 struct sock_fprog prog
= {
614 .len
= (unsigned short)ARRAY_SIZE(filter
),
619 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
622 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
624 EXPECT_EQ(0, syscall(__NR_getpid
)) {
625 TH_LOG("getpid() shouldn't ever return");
629 TEST_SIGNAL(KILL_all
, SIGSYS
)
631 struct sock_filter filter
[] = {
632 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
634 struct sock_fprog prog
= {
635 .len
= (unsigned short)ARRAY_SIZE(filter
),
640 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
643 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
647 TEST_SIGNAL(KILL_one
, SIGSYS
)
649 struct sock_filter filter
[] = {
650 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
651 offsetof(struct seccomp_data
, nr
)),
652 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
653 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
654 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
656 struct sock_fprog prog
= {
657 .len
= (unsigned short)ARRAY_SIZE(filter
),
661 pid_t parent
= getppid();
663 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
666 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
669 EXPECT_EQ(parent
, syscall(__NR_getppid
));
670 /* getpid() should never return. */
671 EXPECT_EQ(0, syscall(__NR_getpid
));
674 TEST_SIGNAL(KILL_one_arg_one
, SIGSYS
)
677 struct sock_filter filter
[] = {
678 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
679 offsetof(struct seccomp_data
, nr
)),
680 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_times
, 1, 0),
681 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
682 /* Only both with lower 32-bit for now. */
683 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(0)),
684 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
,
685 (unsigned long)&fatal_address
, 0, 1),
686 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
687 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
689 struct sock_fprog prog
= {
690 .len
= (unsigned short)ARRAY_SIZE(filter
),
694 pid_t parent
= getppid();
696 clock_t clock
= times(&timebuf
);
698 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
701 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
704 EXPECT_EQ(parent
, syscall(__NR_getppid
));
705 EXPECT_LE(clock
, syscall(__NR_times
, &timebuf
));
706 /* times() should never return. */
707 EXPECT_EQ(0, syscall(__NR_times
, &fatal_address
));
710 TEST_SIGNAL(KILL_one_arg_six
, SIGSYS
)
713 int sysno
= __NR_mmap
;
715 int sysno
= __NR_mmap2
;
717 struct sock_filter filter
[] = {
718 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
719 offsetof(struct seccomp_data
, nr
)),
720 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, sysno
, 1, 0),
721 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
722 /* Only both with lower 32-bit for now. */
723 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(5)),
724 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, 0x0C0FFEE, 0, 1),
725 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
726 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
728 struct sock_fprog prog
= {
729 .len
= (unsigned short)ARRAY_SIZE(filter
),
733 pid_t parent
= getppid();
736 int page_size
= sysconf(_SC_PAGESIZE
);
738 ASSERT_LT(0, page_size
);
740 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
743 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
746 fd
= open("/dev/zero", O_RDONLY
);
749 EXPECT_EQ(parent
, syscall(__NR_getppid
));
750 map1
= (void *)syscall(sysno
,
751 NULL
, page_size
, PROT_READ
, MAP_PRIVATE
, fd
, page_size
);
752 EXPECT_NE(MAP_FAILED
, map1
);
753 /* mmap2() should never return. */
754 map2
= (void *)syscall(sysno
,
755 NULL
, page_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0x0C0FFEE);
756 EXPECT_EQ(MAP_FAILED
, map2
);
758 /* The test failed, so clean up the resources. */
759 munmap(map1
, page_size
);
760 munmap(map2
, page_size
);
764 /* This is a thread task to die via seccomp filter violation. */
765 void *kill_thread(void *data
)
767 bool die
= (bool)data
;
770 prctl(PR_GET_SECCOMP
, 0, 0, 0, 0);
771 return (void *)SIBLING_EXIT_FAILURE
;
774 return (void *)SIBLING_EXIT_UNKILLED
;
783 /* Prepare a thread that will kill itself or both of us. */
784 void kill_thread_or_group(struct __test_metadata
*_metadata
,
785 enum kill_t kill_how
)
789 /* Kill only when calling __NR_prctl. */
790 struct sock_filter filter_thread
[] = {
791 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
792 offsetof(struct seccomp_data
, nr
)),
793 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
794 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL_THREAD
),
795 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
797 struct sock_fprog prog_thread
= {
798 .len
= (unsigned short)ARRAY_SIZE(filter_thread
),
799 .filter
= filter_thread
,
801 int kill
= kill_how
== KILL_PROCESS
? SECCOMP_RET_KILL_PROCESS
: 0xAAAAAAAAA;
802 struct sock_filter filter_process
[] = {
803 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
804 offsetof(struct seccomp_data
, nr
)),
805 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
806 BPF_STMT(BPF_RET
|BPF_K
, kill
),
807 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
809 struct sock_fprog prog_process
= {
810 .len
= (unsigned short)ARRAY_SIZE(filter_process
),
811 .filter
= filter_process
,
814 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
815 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
818 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0,
819 kill_how
== KILL_THREAD
? &prog_thread
823 * Add the KILL_THREAD rule again to make sure that the KILL_PROCESS
824 * flag cannot be downgraded by a new filter.
826 if (kill_how
== KILL_PROCESS
)
827 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog_thread
));
829 /* Start a thread that will exit immediately. */
830 ASSERT_EQ(0, pthread_create(&thread
, NULL
, kill_thread
, (void *)false));
831 ASSERT_EQ(0, pthread_join(thread
, &status
));
832 ASSERT_EQ(SIBLING_EXIT_UNKILLED
, (unsigned long)status
);
834 /* Start a thread that will die immediately. */
835 ASSERT_EQ(0, pthread_create(&thread
, NULL
, kill_thread
, (void *)true));
836 ASSERT_EQ(0, pthread_join(thread
, &status
));
837 ASSERT_NE(SIBLING_EXIT_FAILURE
, (unsigned long)status
);
840 * If we get here, only the spawned thread died. Let the parent know
841 * the whole process didn't die (i.e. this thread, the spawner,
853 ASSERT_LE(0, child_pid
);
854 if (child_pid
== 0) {
855 kill_thread_or_group(_metadata
, KILL_THREAD
);
859 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
861 /* If only the thread was killed, we'll see exit 42. */
862 ASSERT_TRUE(WIFEXITED(status
));
863 ASSERT_EQ(42, WEXITSTATUS(status
));
872 ASSERT_LE(0, child_pid
);
873 if (child_pid
== 0) {
874 kill_thread_or_group(_metadata
, KILL_PROCESS
);
878 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
880 /* If the entire process was killed, we'll see SIGSYS. */
881 ASSERT_TRUE(WIFSIGNALED(status
));
882 ASSERT_EQ(SIGSYS
, WTERMSIG(status
));
891 ASSERT_LE(0, child_pid
);
892 if (child_pid
== 0) {
893 kill_thread_or_group(_metadata
, RET_UNKNOWN
);
897 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
899 /* If the entire process was killed, we'll see SIGSYS. */
900 EXPECT_TRUE(WIFSIGNALED(status
)) {
901 TH_LOG("Unknown SECCOMP_RET is only killing the thread?");
903 ASSERT_EQ(SIGSYS
, WTERMSIG(status
));
906 /* TODO(wad) add 64-bit versus 32-bit arg tests. */
907 TEST(arg_out_of_range
)
909 struct sock_filter filter
[] = {
910 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
, syscall_arg(6)),
911 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
913 struct sock_fprog prog
= {
914 .len
= (unsigned short)ARRAY_SIZE(filter
),
919 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
922 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
);
924 EXPECT_EQ(EINVAL
, errno
);
927 #define ERRNO_FILTER(name, errno) \
928 struct sock_filter _read_filter_##name[] = { \
929 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, \
930 offsetof(struct seccomp_data, nr)), \
931 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), \
932 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | errno), \
933 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), \
935 struct sock_fprog prog_##name = { \
936 .len = (unsigned short)ARRAY_SIZE(_read_filter_##name), \
937 .filter = _read_filter_##name, \
940 /* Make sure basic errno values are correctly passed through a filter. */
943 ERRNO_FILTER(valid
, E2BIG
);
945 pid_t parent
= getppid();
947 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
950 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_valid
);
953 EXPECT_EQ(parent
, syscall(__NR_getppid
));
954 EXPECT_EQ(-1, read(0, NULL
, 0));
955 EXPECT_EQ(E2BIG
, errno
);
958 /* Make sure an errno of zero is correctly handled by the arch code. */
961 ERRNO_FILTER(zero
, 0);
963 pid_t parent
= getppid();
965 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
968 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_zero
);
971 EXPECT_EQ(parent
, syscall(__NR_getppid
));
972 /* "errno" of 0 is ok. */
973 EXPECT_EQ(0, read(0, NULL
, 0));
977 * The SECCOMP_RET_DATA mask is 16 bits wide, but errno is smaller.
978 * This tests that the errno value gets capped correctly, fixed by
979 * 580c57f10768 ("seccomp: cap SECCOMP_RET_ERRNO data to MAX_ERRNO").
983 ERRNO_FILTER(capped
, 4096);
985 pid_t parent
= getppid();
987 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
990 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_capped
);
993 EXPECT_EQ(parent
, syscall(__NR_getppid
));
994 EXPECT_EQ(-1, read(0, NULL
, 0));
995 EXPECT_EQ(4095, errno
);
999 * Filters are processed in reverse order: last applied is executed first.
1000 * Since only the SECCOMP_RET_ACTION mask is tested for return values, the
1001 * SECCOMP_RET_DATA mask results will follow the most recently applied
1002 * matching filter return (and not the lowest or highest value).
1006 ERRNO_FILTER(first
, 11);
1007 ERRNO_FILTER(second
, 13);
1008 ERRNO_FILTER(third
, 12);
1010 pid_t parent
= getppid();
1012 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1015 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_first
);
1018 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_second
);
1021 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog_third
);
1024 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1025 EXPECT_EQ(-1, read(0, NULL
, 0));
1026 EXPECT_EQ(12, errno
);
1030 struct sock_fprog prog
;
1035 struct sock_filter filter
[] = {
1036 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1037 offsetof(struct seccomp_data
, nr
)),
1038 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
1039 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRAP
),
1040 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1043 memset(&self
->prog
, 0, sizeof(self
->prog
));
1044 self
->prog
.filter
= malloc(sizeof(filter
));
1045 ASSERT_NE(NULL
, self
->prog
.filter
);
1046 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1047 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1050 FIXTURE_TEARDOWN(TRAP
)
1052 if (self
->prog
.filter
)
1053 free(self
->prog
.filter
);
1056 TEST_F_SIGNAL(TRAP
, dfl
, SIGSYS
)
1060 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1063 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
1065 syscall(__NR_getpid
);
1068 /* Ensure that SIGSYS overrides SIG_IGN */
1069 TEST_F_SIGNAL(TRAP
, ign
, SIGSYS
)
1073 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1076 signal(SIGSYS
, SIG_IGN
);
1078 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
1080 syscall(__NR_getpid
);
1083 static siginfo_t TRAP_info
;
1084 static volatile int TRAP_nr
;
1085 static void TRAP_action(int nr
, siginfo_t
*info
, void *void_context
)
1087 memcpy(&TRAP_info
, info
, sizeof(TRAP_info
));
1091 TEST_F(TRAP
, handler
)
1094 struct sigaction act
;
1097 memset(&act
, 0, sizeof(act
));
1099 sigaddset(&mask
, SIGSYS
);
1101 act
.sa_sigaction
= &TRAP_action
;
1102 act
.sa_flags
= SA_SIGINFO
;
1103 ret
= sigaction(SIGSYS
, &act
, NULL
);
1105 TH_LOG("sigaction failed");
1107 ret
= sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
1109 TH_LOG("sigprocmask failed");
1112 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1114 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
);
1117 memset(&TRAP_info
, 0, sizeof(TRAP_info
));
1118 /* Expect the registers to be rolled back. (nr = error) may vary
1120 ret
= syscall(__NR_getpid
);
1121 /* Silence gcc warning about volatile. */
1123 EXPECT_EQ(SIGSYS
, test
);
1124 struct local_sigsys
{
1125 void *_call_addr
; /* calling user insn */
1126 int _syscall
; /* triggering system call number */
1127 unsigned int _arch
; /* AUDIT_ARCH_* of syscall */
1128 } *sigsys
= (struct local_sigsys
*)
1130 &(TRAP_info
.si_call_addr
);
1134 EXPECT_EQ(__NR_getpid
, sigsys
->_syscall
);
1135 /* Make sure arch is non-zero. */
1136 EXPECT_NE(0, sigsys
->_arch
);
1137 EXPECT_NE(0, (unsigned long)sigsys
->_call_addr
);
1140 FIXTURE(precedence
) {
1141 struct sock_fprog allow
;
1142 struct sock_fprog log
;
1143 struct sock_fprog trace
;
1144 struct sock_fprog error
;
1145 struct sock_fprog trap
;
1146 struct sock_fprog kill
;
1149 FIXTURE_SETUP(precedence
)
1151 struct sock_filter allow_insns
[] = {
1152 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1154 struct sock_filter log_insns
[] = {
1155 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1156 offsetof(struct seccomp_data
, nr
)),
1157 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1158 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1159 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_LOG
),
1161 struct sock_filter trace_insns
[] = {
1162 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1163 offsetof(struct seccomp_data
, nr
)),
1164 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1165 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1166 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
),
1168 struct sock_filter error_insns
[] = {
1169 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1170 offsetof(struct seccomp_data
, nr
)),
1171 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1172 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1173 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
),
1175 struct sock_filter trap_insns
[] = {
1176 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1177 offsetof(struct seccomp_data
, nr
)),
1178 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1179 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1180 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRAP
),
1182 struct sock_filter kill_insns
[] = {
1183 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1184 offsetof(struct seccomp_data
, nr
)),
1185 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 1, 0),
1186 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1187 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
1190 memset(self
, 0, sizeof(*self
));
1191 #define FILTER_ALLOC(_x) \
1192 self->_x.filter = malloc(sizeof(_x##_insns)); \
1193 ASSERT_NE(NULL, self->_x.filter); \
1194 memcpy(self->_x.filter, &_x##_insns, sizeof(_x##_insns)); \
1195 self->_x.len = (unsigned short)ARRAY_SIZE(_x##_insns)
1196 FILTER_ALLOC(allow
);
1198 FILTER_ALLOC(trace
);
1199 FILTER_ALLOC(error
);
1204 FIXTURE_TEARDOWN(precedence
)
1206 #define FILTER_FREE(_x) if (self->_x.filter) free(self->_x.filter)
1215 TEST_F(precedence
, allow_ok
)
1217 pid_t parent
, res
= 0;
1221 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1224 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1226 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1228 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1230 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1232 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1234 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1236 /* Should work just fine. */
1237 res
= syscall(__NR_getppid
);
1238 EXPECT_EQ(parent
, res
);
1241 TEST_F_SIGNAL(precedence
, kill_is_highest
, SIGSYS
)
1243 pid_t parent
, res
= 0;
1247 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1250 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1252 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1254 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1256 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1258 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1260 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1262 /* Should work just fine. */
1263 res
= syscall(__NR_getppid
);
1264 EXPECT_EQ(parent
, res
);
1265 /* getpid() should never return. */
1266 res
= syscall(__NR_getpid
);
1270 TEST_F_SIGNAL(precedence
, kill_is_highest_in_any_order
, SIGSYS
)
1276 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1279 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1281 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->kill
);
1283 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1285 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1287 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1289 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1291 /* Should work just fine. */
1292 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1293 /* getpid() should never return. */
1294 EXPECT_EQ(0, syscall(__NR_getpid
));
1297 TEST_F_SIGNAL(precedence
, trap_is_second
, SIGSYS
)
1303 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1306 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1308 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1310 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1312 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1314 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1316 /* Should work just fine. */
1317 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1318 /* getpid() should never return. */
1319 EXPECT_EQ(0, syscall(__NR_getpid
));
1322 TEST_F_SIGNAL(precedence
, trap_is_second_in_any_order
, SIGSYS
)
1328 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1331 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1333 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trap
);
1335 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1337 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1339 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1341 /* Should work just fine. */
1342 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1343 /* getpid() should never return. */
1344 EXPECT_EQ(0, syscall(__NR_getpid
));
1347 TEST_F(precedence
, errno_is_third
)
1353 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1356 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1358 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1360 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1362 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1364 /* Should work just fine. */
1365 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1366 EXPECT_EQ(0, syscall(__NR_getpid
));
1369 TEST_F(precedence
, errno_is_third_in_any_order
)
1375 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1378 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1380 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->error
);
1382 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1384 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1386 /* Should work just fine. */
1387 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1388 EXPECT_EQ(0, syscall(__NR_getpid
));
1391 TEST_F(precedence
, trace_is_fourth
)
1397 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1400 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1402 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1404 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1406 /* Should work just fine. */
1407 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1409 EXPECT_EQ(-1, syscall(__NR_getpid
));
1412 TEST_F(precedence
, trace_is_fourth_in_any_order
)
1418 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1421 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->trace
);
1423 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1425 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1427 /* Should work just fine. */
1428 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1430 EXPECT_EQ(-1, syscall(__NR_getpid
));
1433 TEST_F(precedence
, log_is_fifth
)
1435 pid_t mypid
, parent
;
1440 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1443 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1445 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1447 /* Should work just fine. */
1448 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1449 /* Should also work just fine */
1450 EXPECT_EQ(mypid
, syscall(__NR_getpid
));
1453 TEST_F(precedence
, log_is_fifth_in_any_order
)
1455 pid_t mypid
, parent
;
1460 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1463 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->log
);
1465 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->allow
);
1467 /* Should work just fine. */
1468 EXPECT_EQ(parent
, syscall(__NR_getppid
));
1469 /* Should also work just fine */
1470 EXPECT_EQ(mypid
, syscall(__NR_getpid
));
1473 #ifndef PTRACE_O_TRACESECCOMP
1474 #define PTRACE_O_TRACESECCOMP 0x00000080
1477 /* Catch the Ubuntu 12.04 value error. */
1478 #if PTRACE_EVENT_SECCOMP != 7
1479 #undef PTRACE_EVENT_SECCOMP
1482 #ifndef PTRACE_EVENT_SECCOMP
1483 #define PTRACE_EVENT_SECCOMP 7
1486 #define IS_SECCOMP_EVENT(status) ((status >> 16) == PTRACE_EVENT_SECCOMP)
1487 bool tracer_running
;
1488 void tracer_stop(int sig
)
1490 tracer_running
= false;
1493 typedef void tracer_func_t(struct __test_metadata
*_metadata
,
1494 pid_t tracee
, int status
, void *args
);
1496 void start_tracer(struct __test_metadata
*_metadata
, int fd
, pid_t tracee
,
1497 tracer_func_t tracer_func
, void *args
, bool ptrace_syscall
)
1500 struct sigaction action
= {
1501 .sa_handler
= tracer_stop
,
1504 /* Allow external shutdown. */
1505 tracer_running
= true;
1506 ASSERT_EQ(0, sigaction(SIGUSR1
, &action
, NULL
));
1509 while (ret
== -1 && errno
!= EINVAL
)
1510 ret
= ptrace(PTRACE_ATTACH
, tracee
, NULL
, 0);
1512 kill(tracee
, SIGKILL
);
1514 /* Wait for attach stop */
1517 ret
= ptrace(PTRACE_SETOPTIONS
, tracee
, NULL
, ptrace_syscall
?
1518 PTRACE_O_TRACESYSGOOD
:
1519 PTRACE_O_TRACESECCOMP
);
1521 TH_LOG("Failed to set PTRACE_O_TRACESECCOMP");
1522 kill(tracee
, SIGKILL
);
1524 ret
= ptrace(ptrace_syscall
? PTRACE_SYSCALL
: PTRACE_CONT
,
1528 /* Unblock the tracee */
1529 ASSERT_EQ(1, write(fd
, "A", 1));
1530 ASSERT_EQ(0, close(fd
));
1532 /* Run until we're shut down. Must assert to stop execution. */
1533 while (tracer_running
) {
1536 if (wait(&status
) != tracee
)
1538 if (WIFSIGNALED(status
) || WIFEXITED(status
))
1539 /* Child is dead. Time to go. */
1542 /* Check if this is a seccomp event. */
1543 ASSERT_EQ(!ptrace_syscall
, IS_SECCOMP_EVENT(status
));
1545 tracer_func(_metadata
, tracee
, status
, args
);
1547 ret
= ptrace(ptrace_syscall
? PTRACE_SYSCALL
: PTRACE_CONT
,
1551 /* Directly report the status of our test harness results. */
1552 syscall(__NR_exit
, _metadata
->passed
? EXIT_SUCCESS
: EXIT_FAILURE
);
1555 /* Common tracer setup/teardown functions. */
1556 void cont_handler(int num
)
1558 pid_t
setup_trace_fixture(struct __test_metadata
*_metadata
,
1559 tracer_func_t func
, void *args
, bool ptrace_syscall
)
1564 pid_t tracee
= getpid();
1566 /* Setup a pipe for clean synchronization. */
1567 ASSERT_EQ(0, pipe(pipefd
));
1569 /* Fork a child which we'll promote to tracer */
1570 tracer_pid
= fork();
1571 ASSERT_LE(0, tracer_pid
);
1572 signal(SIGALRM
, cont_handler
);
1573 if (tracer_pid
== 0) {
1575 start_tracer(_metadata
, pipefd
[1], tracee
, func
, args
,
1577 syscall(__NR_exit
, 0);
1580 prctl(PR_SET_PTRACER
, tracer_pid
, 0, 0, 0);
1581 read(pipefd
[0], &sync
, 1);
1587 void teardown_trace_fixture(struct __test_metadata
*_metadata
,
1593 * Extract the exit code from the other process and
1594 * adopt it for ourselves in case its asserts failed.
1596 ASSERT_EQ(0, kill(tracer
, SIGUSR1
));
1597 ASSERT_EQ(tracer
, waitpid(tracer
, &status
, 0));
1598 if (WEXITSTATUS(status
))
1599 _metadata
->passed
= 0;
1603 /* "poke" tracer arguments and function. */
1604 struct tracer_args_poke_t
{
1605 unsigned long poke_addr
;
1608 void tracer_poke(struct __test_metadata
*_metadata
, pid_t tracee
, int status
,
1613 struct tracer_args_poke_t
*info
= (struct tracer_args_poke_t
*)args
;
1615 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1617 /* If this fails, don't try to recover. */
1618 ASSERT_EQ(0x1001, msg
) {
1619 kill(tracee
, SIGKILL
);
1622 * Poke in the message.
1623 * Registers are not touched to try to keep this relatively arch
1626 ret
= ptrace(PTRACE_POKEDATA
, tracee
, info
->poke_addr
, 0x1001);
1630 FIXTURE(TRACE_poke
) {
1631 struct sock_fprog prog
;
1634 struct tracer_args_poke_t tracer_args
;
1637 FIXTURE_SETUP(TRACE_poke
)
1639 struct sock_filter filter
[] = {
1640 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1641 offsetof(struct seccomp_data
, nr
)),
1642 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
1643 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1001),
1644 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
1648 memset(&self
->prog
, 0, sizeof(self
->prog
));
1649 self
->prog
.filter
= malloc(sizeof(filter
));
1650 ASSERT_NE(NULL
, self
->prog
.filter
);
1651 memcpy(self
->prog
.filter
, filter
, sizeof(filter
));
1652 self
->prog
.len
= (unsigned short)ARRAY_SIZE(filter
);
1654 /* Set up tracer args. */
1655 self
->tracer_args
.poke_addr
= (unsigned long)&self
->poked
;
1657 /* Launch tracer. */
1658 self
->tracer
= setup_trace_fixture(_metadata
, tracer_poke
,
1659 &self
->tracer_args
, false);
1662 FIXTURE_TEARDOWN(TRACE_poke
)
1664 teardown_trace_fixture(_metadata
, self
->tracer
);
1665 if (self
->prog
.filter
)
1666 free(self
->prog
.filter
);
1669 TEST_F(TRACE_poke
, read_has_side_effects
)
1673 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1676 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1679 EXPECT_EQ(0, self
->poked
);
1680 ret
= read(-1, NULL
, 0);
1682 EXPECT_EQ(0x1001, self
->poked
);
1685 TEST_F(TRACE_poke
, getpid_runs_normally
)
1689 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1692 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &self
->prog
, 0, 0);
1695 EXPECT_EQ(0, self
->poked
);
1696 EXPECT_NE(0, syscall(__NR_getpid
));
1697 EXPECT_EQ(0, self
->poked
);
1700 #if defined(__x86_64__)
1701 # define ARCH_REGS struct user_regs_struct
1702 # define SYSCALL_NUM(_regs) (_regs).orig_rax
1703 # define SYSCALL_RET(_regs) (_regs).rax
1704 #elif defined(__i386__)
1705 # define ARCH_REGS struct user_regs_struct
1706 # define SYSCALL_NUM(_regs) (_regs).orig_eax
1707 # define SYSCALL_RET(_regs) (_regs).eax
1708 #elif defined(__arm__)
1709 # define ARCH_REGS struct pt_regs
1710 # define SYSCALL_NUM(_regs) (_regs).ARM_r7
1711 # define SYSCALL_RET(_regs) (_regs).ARM_r0
1712 #elif defined(__aarch64__)
1713 # define ARCH_REGS struct user_pt_regs
1714 # define SYSCALL_NUM(_regs) (_regs).regs[8]
1715 # define SYSCALL_RET(_regs) (_regs).regs[0]
1716 #elif defined(__riscv) && __riscv_xlen == 64
1717 # define ARCH_REGS struct user_regs_struct
1718 # define SYSCALL_NUM(_regs) (_regs).a7
1719 # define SYSCALL_RET(_regs) (_regs).a0
1720 #elif defined(__csky__)
1721 # define ARCH_REGS struct pt_regs
1722 # if defined(__CSKYABIV2__)
1723 # define SYSCALL_NUM(_regs) (_regs).regs[3]
1725 # define SYSCALL_NUM(_regs) (_regs).regs[9]
1727 # define SYSCALL_RET(_regs) (_regs).a0
1728 #elif defined(__hppa__)
1729 # define ARCH_REGS struct user_regs_struct
1730 # define SYSCALL_NUM(_regs) (_regs).gr[20]
1731 # define SYSCALL_RET(_regs) (_regs).gr[28]
1732 #elif defined(__powerpc__)
1733 # define ARCH_REGS struct pt_regs
1734 # define SYSCALL_NUM(_regs) (_regs).gpr[0]
1735 # define SYSCALL_RET(_regs) (_regs).gpr[3]
1736 #elif defined(__s390__)
1737 # define ARCH_REGS s390_regs
1738 # define SYSCALL_NUM(_regs) (_regs).gprs[2]
1739 # define SYSCALL_RET(_regs) (_regs).gprs[2]
1740 # define SYSCALL_NUM_RET_SHARE_REG
1741 #elif defined(__mips__)
1742 # define ARCH_REGS struct pt_regs
1743 # define SYSCALL_NUM(_regs) (_regs).regs[2]
1744 # define SYSCALL_SYSCALL_NUM regs[4]
1745 # define SYSCALL_RET(_regs) (_regs).regs[2]
1746 # define SYSCALL_NUM_RET_SHARE_REG
1747 #elif defined(__xtensa__)
1748 # define ARCH_REGS struct user_pt_regs
1749 # define SYSCALL_NUM(_regs) (_regs).syscall
1751 * On xtensa syscall return value is in the register
1752 * a2 of the current window which is not fixed.
1754 #define SYSCALL_RET(_regs) (_regs).a[(_regs).windowbase * 4 + 2]
1755 #elif defined(__sh__)
1756 # define ARCH_REGS struct pt_regs
1757 # define SYSCALL_NUM(_regs) (_regs).gpr[3]
1758 # define SYSCALL_RET(_regs) (_regs).gpr[0]
1760 # error "Do not know how to find your architecture's registers and syscalls"
1764 * Most architectures can change the syscall by just updating the
1765 * associated register. This is the default if not defined above.
1767 #ifndef SYSCALL_NUM_SET
1768 # define SYSCALL_NUM_SET(_regs, _nr) \
1770 SYSCALL_NUM(_regs) = (_nr); \
1774 /* When the syscall return can't be changed, stub out the tests for it. */
1775 #ifdef SYSCALL_NUM_RET_SHARE_REG
1776 # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(-1, action)
1778 # define EXPECT_SYSCALL_RETURN(val, action) \
1782 EXPECT_EQ(-1, action); \
1783 EXPECT_EQ(-(val), errno); \
1785 EXPECT_EQ(val, action); \
1790 /* Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for
1791 * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux).
1793 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__)
1794 #define HAVE_GETREGS
1797 /* Architecture-specific syscall fetching routine. */
1798 int get_syscall(struct __test_metadata
*_metadata
, pid_t tracee
)
1802 EXPECT_EQ(0, ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
)) {
1803 TH_LOG("PTRACE_GETREGS failed");
1809 iov
.iov_base
= ®s
;
1810 iov
.iov_len
= sizeof(regs
);
1811 EXPECT_EQ(0, ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
)) {
1812 TH_LOG("PTRACE_GETREGSET failed");
1817 #if defined(__mips__)
1818 if (SYSCALL_NUM(regs
) == __NR_O32_Linux
)
1819 return regs
.SYSCALL_SYSCALL_NUM
;
1821 return SYSCALL_NUM(regs
);
1824 /* Architecture-specific syscall changing routine. */
1825 void change_syscall(struct __test_metadata
*_metadata
,
1826 pid_t tracee
, int syscall
, int result
)
1831 ret
= ptrace(PTRACE_GETREGS
, tracee
, 0, ®s
);
1834 iov
.iov_base
= ®s
;
1835 iov
.iov_len
= sizeof(regs
);
1836 ret
= ptrace(PTRACE_GETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1838 EXPECT_EQ(0, ret
) {}
1840 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \
1841 defined(__s390__) || defined(__hppa__) || defined(__riscv) || \
1842 defined(__xtensa__) || defined(__csky__) || defined(__sh__)
1844 SYSCALL_NUM_SET(regs
, syscall
);
1846 #elif defined(__mips__)
1848 if (SYSCALL_NUM(regs
) == __NR_O32_Linux
)
1849 regs
.SYSCALL_SYSCALL_NUM
= syscall
;
1851 SYSCALL_NUM_SET(regs
, syscall
);
1854 #elif defined(__arm__)
1855 # ifndef PTRACE_SET_SYSCALL
1856 # define PTRACE_SET_SYSCALL 23
1859 ret
= ptrace(PTRACE_SET_SYSCALL
, tracee
, NULL
, syscall
);
1863 #elif defined(__aarch64__)
1864 # ifndef NT_ARM_SYSTEM_CALL
1865 # define NT_ARM_SYSTEM_CALL 0x404
1868 iov
.iov_base
= &syscall
;
1869 iov
.iov_len
= sizeof(syscall
);
1870 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_ARM_SYSTEM_CALL
,
1877 TH_LOG("How is the syscall changed on this architecture?");
1881 /* If syscall is skipped, change return value. */
1883 #ifdef SYSCALL_NUM_RET_SHARE_REG
1884 TH_LOG("Can't modify syscall return on this architecture");
1886 SYSCALL_RET(regs
) = result
;
1890 ret
= ptrace(PTRACE_SETREGS
, tracee
, 0, ®s
);
1892 iov
.iov_base
= ®s
;
1893 iov
.iov_len
= sizeof(regs
);
1894 ret
= ptrace(PTRACE_SETREGSET
, tracee
, NT_PRSTATUS
, &iov
);
1899 void tracer_seccomp(struct __test_metadata
*_metadata
, pid_t tracee
,
1900 int status
, void *args
)
1905 /* Make sure we got the right message. */
1906 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1909 /* Validate and take action on expected syscalls. */
1912 /* change getpid to getppid. */
1913 EXPECT_EQ(__NR_getpid
, get_syscall(_metadata
, tracee
));
1914 change_syscall(_metadata
, tracee
, __NR_getppid
, 0);
1917 /* skip gettid with valid return code. */
1918 EXPECT_EQ(__NR_gettid
, get_syscall(_metadata
, tracee
));
1919 change_syscall(_metadata
, tracee
, -1, 45000);
1922 /* skip openat with error. */
1923 EXPECT_EQ(__NR_openat
, get_syscall(_metadata
, tracee
));
1924 change_syscall(_metadata
, tracee
, -1, -ESRCH
);
1927 /* do nothing (allow getppid) */
1928 EXPECT_EQ(__NR_getppid
, get_syscall(_metadata
, tracee
));
1932 TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg
);
1933 kill(tracee
, SIGKILL
);
1939 void tracer_ptrace(struct __test_metadata
*_metadata
, pid_t tracee
,
1940 int status
, void *args
)
1947 * The traditional way to tell PTRACE_SYSCALL entry/exit
1952 /* Make sure we got an appropriate message. */
1953 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1955 EXPECT_EQ(entry
? PTRACE_EVENTMSG_SYSCALL_ENTRY
1956 : PTRACE_EVENTMSG_SYSCALL_EXIT
, msg
);
1961 nr
= get_syscall(_metadata
, tracee
);
1963 if (nr
== __NR_getpid
)
1964 change_syscall(_metadata
, tracee
, __NR_getppid
, 0);
1965 if (nr
== __NR_gettid
)
1966 change_syscall(_metadata
, tracee
, -1, 45000);
1967 if (nr
== __NR_openat
)
1968 change_syscall(_metadata
, tracee
, -1, -ESRCH
);
1971 FIXTURE(TRACE_syscall
) {
1972 struct sock_fprog prog
;
1973 pid_t tracer
, mytid
, mypid
, parent
;
1976 FIXTURE_VARIANT(TRACE_syscall
) {
1978 * All of the SECCOMP_RET_TRACE behaviors can be tested with either
1979 * SECCOMP_RET_TRACE+PTRACE_CONT or plain ptrace()+PTRACE_SYSCALL.
1980 * This indicates if we should use SECCOMP_RET_TRACE (false), or
1986 FIXTURE_VARIANT_ADD(TRACE_syscall
, ptrace
) {
1990 FIXTURE_VARIANT_ADD(TRACE_syscall
, seccomp
) {
1991 .use_ptrace
= false,
1994 FIXTURE_SETUP(TRACE_syscall
)
1996 struct sock_filter filter
[] = {
1997 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
1998 offsetof(struct seccomp_data
, nr
)),
1999 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
2000 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1002),
2001 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_gettid
, 0, 1),
2002 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1003),
2003 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_openat
, 0, 1),
2004 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1004),
2005 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2006 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1005),
2007 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2009 struct sock_fprog prog
= {
2010 .len
= (unsigned short)ARRAY_SIZE(filter
),
2015 /* Prepare some testable syscall results. */
2016 self
->mytid
= syscall(__NR_gettid
);
2017 ASSERT_GT(self
->mytid
, 0);
2018 ASSERT_NE(self
->mytid
, 1) {
2019 TH_LOG("Running this test as init is not supported. :)");
2022 self
->mypid
= getpid();
2023 ASSERT_GT(self
->mypid
, 0);
2024 ASSERT_EQ(self
->mytid
, self
->mypid
);
2026 self
->parent
= getppid();
2027 ASSERT_GT(self
->parent
, 0);
2028 ASSERT_NE(self
->parent
, self
->mypid
);
2030 /* Launch tracer. */
2031 self
->tracer
= setup_trace_fixture(_metadata
,
2032 variant
->use_ptrace
? tracer_ptrace
2034 NULL
, variant
->use_ptrace
);
2036 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2039 if (variant
->use_ptrace
)
2042 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2046 FIXTURE_TEARDOWN(TRACE_syscall
)
2048 teardown_trace_fixture(_metadata
, self
->tracer
);
2051 TEST(negative_ENOSYS
)
2054 * There should be no difference between an "internal" skip
2055 * and userspace asking for syscall "-1".
2058 EXPECT_EQ(-1, syscall(-1));
2059 EXPECT_EQ(errno
, ENOSYS
);
2060 /* And no difference for "still not valid but not -1". */
2062 EXPECT_EQ(-1, syscall(-101));
2063 EXPECT_EQ(errno
, ENOSYS
);
2066 TEST_F(TRACE_syscall
, negative_ENOSYS
)
2068 negative_ENOSYS(_metadata
);
2071 TEST_F(TRACE_syscall
, syscall_allowed
)
2073 /* getppid works as expected (no changes). */
2074 EXPECT_EQ(self
->parent
, syscall(__NR_getppid
));
2075 EXPECT_NE(self
->mypid
, syscall(__NR_getppid
));
2078 TEST_F(TRACE_syscall
, syscall_redirected
)
2080 /* getpid has been redirected to getppid as expected. */
2081 EXPECT_EQ(self
->parent
, syscall(__NR_getpid
));
2082 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
2085 TEST_F(TRACE_syscall
, syscall_errno
)
2087 /* Tracer should skip the open syscall, resulting in ESRCH. */
2088 EXPECT_SYSCALL_RETURN(-ESRCH
, syscall(__NR_openat
));
2091 TEST_F(TRACE_syscall
, syscall_faked
)
2093 /* Tracer skips the gettid syscall and store altered return value. */
2094 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid
));
2097 TEST_F(TRACE_syscall
, skip_after
)
2099 struct sock_filter filter
[] = {
2100 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2101 offsetof(struct seccomp_data
, nr
)),
2102 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2103 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
2104 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2106 struct sock_fprog prog
= {
2107 .len
= (unsigned short)ARRAY_SIZE(filter
),
2112 /* Install additional "errno on getppid" filter. */
2113 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2116 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
2118 EXPECT_EQ(-1, syscall(__NR_getpid
));
2119 EXPECT_EQ(EPERM
, errno
);
2122 TEST_F_SIGNAL(TRACE_syscall
, kill_after
, SIGSYS
)
2124 struct sock_filter filter
[] = {
2125 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2126 offsetof(struct seccomp_data
, nr
)),
2127 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2128 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2129 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2131 struct sock_fprog prog
= {
2132 .len
= (unsigned short)ARRAY_SIZE(filter
),
2137 /* Install additional "death on getppid" filter. */
2138 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2141 /* Tracer will redirect getpid to getppid, and we should die. */
2142 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
2145 TEST(seccomp_syscall
)
2147 struct sock_filter filter
[] = {
2148 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2150 struct sock_fprog prog
= {
2151 .len
= (unsigned short)ARRAY_SIZE(filter
),
2156 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2158 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2161 /* Reject insane operation. */
2162 ret
= seccomp(-1, 0, &prog
);
2163 ASSERT_NE(ENOSYS
, errno
) {
2164 TH_LOG("Kernel does not support seccomp syscall!");
2166 EXPECT_EQ(EINVAL
, errno
) {
2167 TH_LOG("Did not reject crazy op value!");
2170 /* Reject strict with flags or pointer. */
2171 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, -1, NULL
);
2172 EXPECT_EQ(EINVAL
, errno
) {
2173 TH_LOG("Did not reject mode strict with flags!");
2175 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, &prog
);
2176 EXPECT_EQ(EINVAL
, errno
) {
2177 TH_LOG("Did not reject mode strict with uargs!");
2180 /* Reject insane args for filter. */
2181 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, -1, &prog
);
2182 EXPECT_EQ(EINVAL
, errno
) {
2183 TH_LOG("Did not reject crazy filter flags!");
2185 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, NULL
);
2186 EXPECT_EQ(EFAULT
, errno
) {
2187 TH_LOG("Did not reject NULL filter!");
2190 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2191 EXPECT_EQ(0, errno
) {
2192 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s",
2197 TEST(seccomp_syscall_mode_lock
)
2199 struct sock_filter filter
[] = {
2200 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2202 struct sock_fprog prog
= {
2203 .len
= (unsigned short)ARRAY_SIZE(filter
),
2208 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2210 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2213 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2214 ASSERT_NE(ENOSYS
, errno
) {
2215 TH_LOG("Kernel does not support seccomp syscall!");
2218 TH_LOG("Could not install filter!");
2221 /* Make sure neither entry point will switch to strict. */
2222 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, 0, 0, 0);
2223 EXPECT_EQ(EINVAL
, errno
) {
2224 TH_LOG("Switched to mode strict!");
2227 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, NULL
);
2228 EXPECT_EQ(EINVAL
, errno
) {
2229 TH_LOG("Switched to mode strict!");
2234 * Test detection of known and unknown filter flags. Userspace needs to be able
2235 * to check if a filter flag is supported by the current kernel and a good way
2236 * of doing that is by attempting to enter filter mode, with the flag bit in
2237 * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates
2238 * that the flag is valid and EINVAL indicates that the flag is invalid.
2240 TEST(detect_seccomp_filter_flags
)
2242 unsigned int flags
[] = { SECCOMP_FILTER_FLAG_TSYNC
,
2243 SECCOMP_FILTER_FLAG_LOG
,
2244 SECCOMP_FILTER_FLAG_SPEC_ALLOW
,
2245 SECCOMP_FILTER_FLAG_NEW_LISTENER
,
2246 SECCOMP_FILTER_FLAG_TSYNC_ESRCH
};
2247 unsigned int exclusive
[] = {
2248 SECCOMP_FILTER_FLAG_TSYNC
,
2249 SECCOMP_FILTER_FLAG_NEW_LISTENER
};
2250 unsigned int flag
, all_flags
, exclusive_mask
;
2254 /* Test detection of individual known-good filter flags */
2255 for (i
= 0, all_flags
= 0; i
< ARRAY_SIZE(flags
); i
++) {
2259 /* Make sure the flag is a single bit! */
2268 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2269 ASSERT_NE(ENOSYS
, errno
) {
2270 TH_LOG("Kernel does not support seccomp syscall!");
2273 EXPECT_EQ(EFAULT
, errno
) {
2274 TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!",
2282 * Test detection of all known-good filter flags combined. But
2283 * for the exclusive flags we need to mask them out and try them
2284 * individually for the "all flags" testing.
2287 for (i
= 0; i
< ARRAY_SIZE(exclusive
); i
++)
2288 exclusive_mask
|= exclusive
[i
];
2289 for (i
= 0; i
< ARRAY_SIZE(exclusive
); i
++) {
2290 flag
= all_flags
& ~exclusive_mask
;
2291 flag
|= exclusive
[i
];
2293 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2295 EXPECT_EQ(EFAULT
, errno
) {
2296 TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2301 /* Test detection of an unknown filter flags, without exclusives. */
2303 flag
&= ~exclusive_mask
;
2304 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2306 EXPECT_EQ(EINVAL
, errno
) {
2307 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!",
2312 * Test detection of an unknown filter flag that may simply need to be
2313 * added to this test
2315 flag
= flags
[ARRAY_SIZE(flags
) - 1] << 1;
2316 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2318 EXPECT_EQ(EINVAL
, errno
) {
2319 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?",
2326 struct sock_filter filter
[] = {
2327 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2329 struct sock_fprog prog
= {
2330 .len
= (unsigned short)ARRAY_SIZE(filter
),
2335 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2337 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2340 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2342 ASSERT_NE(ENOSYS
, errno
) {
2343 TH_LOG("Kernel does not support seccomp syscall!");
2346 TH_LOG("Could not install initial filter with TSYNC!");
2350 #define TSYNC_SIBLINGS 2
2351 struct tsync_sibling
{
2355 pthread_cond_t
*cond
;
2356 pthread_mutex_t
*mutex
;
2359 struct sock_fprog
*prog
;
2360 struct __test_metadata
*metadata
;
2364 * To avoid joining joined threads (which is not allowed by Bionic),
2365 * make sure we both successfully join and clear the tid to skip a
2366 * later join attempt during fixture teardown. Any remaining threads
2367 * will be directly killed during teardown.
2369 #define PTHREAD_JOIN(tid, status) \
2371 int _rc = pthread_join(tid, status); \
2373 TH_LOG("pthread_join of tid %u failed: %d\n", \
2374 (unsigned int)tid, _rc); \
2381 struct sock_fprog root_prog
, apply_prog
;
2382 struct tsync_sibling sibling
[TSYNC_SIBLINGS
];
2384 pthread_cond_t cond
;
2385 pthread_mutex_t mutex
;
2389 FIXTURE_SETUP(TSYNC
)
2391 struct sock_filter root_filter
[] = {
2392 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2394 struct sock_filter apply_filter
[] = {
2395 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2396 offsetof(struct seccomp_data
, nr
)),
2397 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
2398 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2399 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2402 memset(&self
->root_prog
, 0, sizeof(self
->root_prog
));
2403 memset(&self
->apply_prog
, 0, sizeof(self
->apply_prog
));
2404 memset(&self
->sibling
, 0, sizeof(self
->sibling
));
2405 self
->root_prog
.filter
= malloc(sizeof(root_filter
));
2406 ASSERT_NE(NULL
, self
->root_prog
.filter
);
2407 memcpy(self
->root_prog
.filter
, &root_filter
, sizeof(root_filter
));
2408 self
->root_prog
.len
= (unsigned short)ARRAY_SIZE(root_filter
);
2410 self
->apply_prog
.filter
= malloc(sizeof(apply_filter
));
2411 ASSERT_NE(NULL
, self
->apply_prog
.filter
);
2412 memcpy(self
->apply_prog
.filter
, &apply_filter
, sizeof(apply_filter
));
2413 self
->apply_prog
.len
= (unsigned short)ARRAY_SIZE(apply_filter
);
2415 self
->sibling_count
= 0;
2416 pthread_mutex_init(&self
->mutex
, NULL
);
2417 pthread_cond_init(&self
->cond
, NULL
);
2418 sem_init(&self
->started
, 0, 0);
2419 self
->sibling
[0].tid
= 0;
2420 self
->sibling
[0].cond
= &self
->cond
;
2421 self
->sibling
[0].started
= &self
->started
;
2422 self
->sibling
[0].mutex
= &self
->mutex
;
2423 self
->sibling
[0].diverge
= 0;
2424 self
->sibling
[0].num_waits
= 1;
2425 self
->sibling
[0].prog
= &self
->root_prog
;
2426 self
->sibling
[0].metadata
= _metadata
;
2427 self
->sibling
[1].tid
= 0;
2428 self
->sibling
[1].cond
= &self
->cond
;
2429 self
->sibling
[1].started
= &self
->started
;
2430 self
->sibling
[1].mutex
= &self
->mutex
;
2431 self
->sibling
[1].diverge
= 0;
2432 self
->sibling
[1].prog
= &self
->root_prog
;
2433 self
->sibling
[1].num_waits
= 1;
2434 self
->sibling
[1].metadata
= _metadata
;
2437 FIXTURE_TEARDOWN(TSYNC
)
2441 if (self
->root_prog
.filter
)
2442 free(self
->root_prog
.filter
);
2443 if (self
->apply_prog
.filter
)
2444 free(self
->apply_prog
.filter
);
2446 for ( ; sib
< self
->sibling_count
; ++sib
) {
2447 struct tsync_sibling
*s
= &self
->sibling
[sib
];
2452 * If a thread is still running, it may be stuck, so hit
2453 * it over the head really hard.
2455 pthread_kill(s
->tid
, 9);
2457 pthread_mutex_destroy(&self
->mutex
);
2458 pthread_cond_destroy(&self
->cond
);
2459 sem_destroy(&self
->started
);
2462 void *tsync_sibling(void *data
)
2465 struct tsync_sibling
*me
= data
;
2467 me
->system_tid
= syscall(__NR_gettid
);
2469 pthread_mutex_lock(me
->mutex
);
2471 /* Just re-apply the root prog to fork the tree */
2472 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
2475 sem_post(me
->started
);
2476 /* Return outside of started so parent notices failures. */
2478 pthread_mutex_unlock(me
->mutex
);
2479 return (void *)SIBLING_EXIT_FAILURE
;
2482 pthread_cond_wait(me
->cond
, me
->mutex
);
2483 me
->num_waits
= me
->num_waits
- 1;
2484 } while (me
->num_waits
);
2485 pthread_mutex_unlock(me
->mutex
);
2487 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, 0, 0, 0);
2489 return (void *)SIBLING_EXIT_NEWPRIVS
;
2491 return (void *)SIBLING_EXIT_UNKILLED
;
2494 void tsync_start_sibling(struct tsync_sibling
*sibling
)
2496 pthread_create(&sibling
->tid
, NULL
, tsync_sibling
, (void *)sibling
);
2499 TEST_F(TSYNC
, siblings_fail_prctl
)
2503 struct sock_filter filter
[] = {
2504 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2505 offsetof(struct seccomp_data
, nr
)),
2506 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
2507 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EINVAL
),
2508 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2510 struct sock_fprog prog
= {
2511 .len
= (unsigned short)ARRAY_SIZE(filter
),
2515 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2516 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2519 /* Check prctl failure detection by requesting sib 0 diverge. */
2520 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2521 ASSERT_NE(ENOSYS
, errno
) {
2522 TH_LOG("Kernel does not support seccomp syscall!");
2525 TH_LOG("setting filter failed");
2528 self
->sibling
[0].diverge
= 1;
2529 tsync_start_sibling(&self
->sibling
[0]);
2530 tsync_start_sibling(&self
->sibling
[1]);
2532 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2533 sem_wait(&self
->started
);
2534 self
->sibling_count
++;
2537 /* Signal the threads to clean up*/
2538 pthread_mutex_lock(&self
->mutex
);
2539 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2540 TH_LOG("cond broadcast non-zero");
2542 pthread_mutex_unlock(&self
->mutex
);
2544 /* Ensure diverging sibling failed to call prctl. */
2545 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2546 EXPECT_EQ(SIBLING_EXIT_FAILURE
, (long)status
);
2547 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2548 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2551 TEST_F(TSYNC
, two_siblings_with_ancestor
)
2556 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2557 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2560 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2561 ASSERT_NE(ENOSYS
, errno
) {
2562 TH_LOG("Kernel does not support seccomp syscall!");
2565 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2567 tsync_start_sibling(&self
->sibling
[0]);
2568 tsync_start_sibling(&self
->sibling
[1]);
2570 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2571 sem_wait(&self
->started
);
2572 self
->sibling_count
++;
2575 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2578 TH_LOG("Could install filter on all threads!");
2580 /* Tell the siblings to test the policy */
2581 pthread_mutex_lock(&self
->mutex
);
2582 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2583 TH_LOG("cond broadcast non-zero");
2585 pthread_mutex_unlock(&self
->mutex
);
2586 /* Ensure they are both killed and don't exit cleanly. */
2587 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2588 EXPECT_EQ(0x0, (long)status
);
2589 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2590 EXPECT_EQ(0x0, (long)status
);
2593 TEST_F(TSYNC
, two_sibling_want_nnp
)
2597 /* start siblings before any prctl() operations */
2598 tsync_start_sibling(&self
->sibling
[0]);
2599 tsync_start_sibling(&self
->sibling
[1]);
2600 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2601 sem_wait(&self
->started
);
2602 self
->sibling_count
++;
2605 /* Tell the siblings to test no policy */
2606 pthread_mutex_lock(&self
->mutex
);
2607 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2608 TH_LOG("cond broadcast non-zero");
2610 pthread_mutex_unlock(&self
->mutex
);
2612 /* Ensure they are both upset about lacking nnp. */
2613 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2614 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2615 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2616 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2619 TEST_F(TSYNC
, two_siblings_with_no_filter
)
2624 /* start siblings before any prctl() operations */
2625 tsync_start_sibling(&self
->sibling
[0]);
2626 tsync_start_sibling(&self
->sibling
[1]);
2627 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2628 sem_wait(&self
->started
);
2629 self
->sibling_count
++;
2632 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2633 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2636 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2638 ASSERT_NE(ENOSYS
, errno
) {
2639 TH_LOG("Kernel does not support seccomp syscall!");
2642 TH_LOG("Could install filter on all threads!");
2645 /* Tell the siblings to test the policy */
2646 pthread_mutex_lock(&self
->mutex
);
2647 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2648 TH_LOG("cond broadcast non-zero");
2650 pthread_mutex_unlock(&self
->mutex
);
2652 /* Ensure they are both killed and don't exit cleanly. */
2653 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2654 EXPECT_EQ(0x0, (long)status
);
2655 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2656 EXPECT_EQ(0x0, (long)status
);
2659 TEST_F(TSYNC
, two_siblings_with_one_divergence
)
2664 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2665 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2668 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2669 ASSERT_NE(ENOSYS
, errno
) {
2670 TH_LOG("Kernel does not support seccomp syscall!");
2673 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2675 self
->sibling
[0].diverge
= 1;
2676 tsync_start_sibling(&self
->sibling
[0]);
2677 tsync_start_sibling(&self
->sibling
[1]);
2679 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2680 sem_wait(&self
->started
);
2681 self
->sibling_count
++;
2684 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2686 ASSERT_EQ(self
->sibling
[0].system_tid
, ret
) {
2687 TH_LOG("Did not fail on diverged sibling.");
2690 /* Wake the threads */
2691 pthread_mutex_lock(&self
->mutex
);
2692 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2693 TH_LOG("cond broadcast non-zero");
2695 pthread_mutex_unlock(&self
->mutex
);
2697 /* Ensure they are both unkilled. */
2698 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2699 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2700 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2701 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2704 TEST_F(TSYNC
, two_siblings_with_one_divergence_no_tid_in_err
)
2709 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2710 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2713 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2714 ASSERT_NE(ENOSYS
, errno
) {
2715 TH_LOG("Kernel does not support seccomp syscall!");
2718 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2720 self
->sibling
[0].diverge
= 1;
2721 tsync_start_sibling(&self
->sibling
[0]);
2722 tsync_start_sibling(&self
->sibling
[1]);
2724 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2725 sem_wait(&self
->started
);
2726 self
->sibling_count
++;
2729 flags
= SECCOMP_FILTER_FLAG_TSYNC
| \
2730 SECCOMP_FILTER_FLAG_TSYNC_ESRCH
;
2731 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flags
, &self
->apply_prog
);
2732 ASSERT_EQ(ESRCH
, errno
) {
2733 TH_LOG("Did not return ESRCH for diverged sibling.");
2735 ASSERT_EQ(-1, ret
) {
2736 TH_LOG("Did not fail on diverged sibling.");
2739 /* Wake the threads */
2740 pthread_mutex_lock(&self
->mutex
);
2741 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2742 TH_LOG("cond broadcast non-zero");
2744 pthread_mutex_unlock(&self
->mutex
);
2746 /* Ensure they are both unkilled. */
2747 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2748 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2749 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2750 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2753 TEST_F(TSYNC
, two_siblings_not_under_filter
)
2757 struct timespec delay
= { .tv_nsec
= 100000000 };
2759 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2760 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2764 * Sibling 0 will have its own seccomp policy
2765 * and Sibling 1 will not be under seccomp at
2766 * all. Sibling 1 will enter seccomp and 0
2767 * will cause failure.
2769 self
->sibling
[0].diverge
= 1;
2770 tsync_start_sibling(&self
->sibling
[0]);
2771 tsync_start_sibling(&self
->sibling
[1]);
2773 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2774 sem_wait(&self
->started
);
2775 self
->sibling_count
++;
2778 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2779 ASSERT_NE(ENOSYS
, errno
) {
2780 TH_LOG("Kernel does not support seccomp syscall!");
2783 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2786 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2788 ASSERT_EQ(ret
, self
->sibling
[0].system_tid
) {
2789 TH_LOG("Did not fail on diverged sibling.");
2792 if (ret
== self
->sibling
[0].system_tid
)
2795 pthread_mutex_lock(&self
->mutex
);
2797 /* Increment the other siblings num_waits so we can clean up
2798 * the one we just saw.
2800 self
->sibling
[!sib
].num_waits
+= 1;
2802 /* Signal the thread to clean up*/
2803 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2804 TH_LOG("cond broadcast non-zero");
2806 pthread_mutex_unlock(&self
->mutex
);
2807 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2808 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2809 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2810 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2811 nanosleep(&delay
, NULL
);
2812 /* Switch to the remaining sibling */
2815 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2818 TH_LOG("Expected the remaining sibling to sync");
2821 pthread_mutex_lock(&self
->mutex
);
2823 /* If remaining sibling didn't have a chance to wake up during
2824 * the first broadcast, manually reduce the num_waits now.
2826 if (self
->sibling
[sib
].num_waits
> 1)
2827 self
->sibling
[sib
].num_waits
= 1;
2828 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2829 TH_LOG("cond broadcast non-zero");
2831 pthread_mutex_unlock(&self
->mutex
);
2832 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2833 EXPECT_EQ(0, (long)status
);
2834 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2835 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2836 nanosleep(&delay
, NULL
);
2838 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2840 ASSERT_EQ(0, ret
); /* just us chickens */
2843 /* Make sure restarted syscalls are seen directly as "restart_syscall". */
2844 TEST(syscall_restart
)
2851 siginfo_t info
= { };
2852 struct sock_filter filter
[] = {
2853 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2854 offsetof(struct seccomp_data
, nr
)),
2856 #ifdef __NR_sigreturn
2857 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_sigreturn
, 7, 0),
2859 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 6, 0),
2860 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_exit
, 5, 0),
2861 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_rt_sigreturn
, 4, 0),
2862 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_nanosleep
, 5, 0),
2863 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_clock_nanosleep
, 4, 0),
2864 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_restart_syscall
, 4, 0),
2866 /* Allow __NR_write for easy logging. */
2867 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_write
, 0, 1),
2868 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2869 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2870 /* The nanosleep jump target. */
2871 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x100),
2872 /* The restart_syscall jump target. */
2873 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x200),
2875 struct sock_fprog prog
= {
2876 .len
= (unsigned short)ARRAY_SIZE(filter
),
2879 #if defined(__arm__)
2880 struct utsname utsbuf
;
2883 ASSERT_EQ(0, pipe(pipefd
));
2886 ASSERT_LE(0, child_pid
);
2887 if (child_pid
== 0) {
2888 /* Child uses EXPECT not ASSERT to deliver status correctly. */
2890 struct timespec timeout
= { };
2892 /* Attach parent as tracer and stop. */
2893 EXPECT_EQ(0, ptrace(PTRACE_TRACEME
));
2894 EXPECT_EQ(0, raise(SIGSTOP
));
2896 EXPECT_EQ(0, close(pipefd
[1]));
2898 EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2899 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2902 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2904 TH_LOG("Failed to install filter!");
2907 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2908 TH_LOG("Failed to read() sync from parent");
2910 EXPECT_EQ('.', buf
) {
2911 TH_LOG("Failed to get sync data from read()");
2914 /* Start nanosleep to be interrupted. */
2917 EXPECT_EQ(0, nanosleep(&timeout
, NULL
)) {
2918 TH_LOG("Call to nanosleep() failed (errno %d)", errno
);
2921 /* Read final sync from parent. */
2922 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2923 TH_LOG("Failed final read() from parent");
2925 EXPECT_EQ('!', buf
) {
2926 TH_LOG("Failed to get final data from read()");
2929 /* Directly report the status of our test harness results. */
2930 syscall(__NR_exit
, _metadata
->passed
? EXIT_SUCCESS
2933 EXPECT_EQ(0, close(pipefd
[0]));
2935 /* Attach to child, setup options, and release. */
2936 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2937 ASSERT_EQ(true, WIFSTOPPED(status
));
2938 ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS
, child_pid
, NULL
,
2939 PTRACE_O_TRACESECCOMP
));
2940 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2941 ASSERT_EQ(1, write(pipefd
[1], ".", 1));
2943 /* Wait for nanosleep() to start. */
2944 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2945 ASSERT_EQ(true, WIFSTOPPED(status
));
2946 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2947 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2948 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2949 ASSERT_EQ(0x100, msg
);
2950 ret
= get_syscall(_metadata
, child_pid
);
2951 EXPECT_TRUE(ret
== __NR_nanosleep
|| ret
== __NR_clock_nanosleep
);
2953 /* Might as well check siginfo for sanity while we're here. */
2954 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2955 ASSERT_EQ(SIGTRAP
, info
.si_signo
);
2956 ASSERT_EQ(SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8), info
.si_code
);
2957 EXPECT_EQ(0, info
.si_errno
);
2958 EXPECT_EQ(getuid(), info
.si_uid
);
2959 /* Verify signal delivery came from child (seccomp-triggered). */
2960 EXPECT_EQ(child_pid
, info
.si_pid
);
2962 /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */
2963 ASSERT_EQ(0, kill(child_pid
, SIGSTOP
));
2964 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2965 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2966 ASSERT_EQ(true, WIFSTOPPED(status
));
2967 ASSERT_EQ(SIGSTOP
, WSTOPSIG(status
));
2968 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
2970 * There is no siginfo on SIGSTOP any more, so we can't verify
2971 * signal delivery came from parent now (getpid() == info.si_pid).
2972 * https://lkml.kernel.org/r/CAGXu5jJaZAOzP1qFz66tYrtbuywqb+UN2SOA1VLHpCCOiYvYeg@mail.gmail.com
2973 * At least verify the SIGSTOP via PTRACE_GETSIGINFO.
2975 EXPECT_EQ(SIGSTOP
, info
.si_signo
);
2977 /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */
2978 ASSERT_EQ(0, kill(child_pid
, SIGCONT
));
2979 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2980 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2981 ASSERT_EQ(true, WIFSTOPPED(status
));
2982 ASSERT_EQ(SIGCONT
, WSTOPSIG(status
));
2983 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
2985 /* Wait for restart_syscall() to start. */
2986 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2987 ASSERT_EQ(true, WIFSTOPPED(status
));
2988 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
2989 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
2990 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
2992 ASSERT_EQ(0x200, msg
);
2993 ret
= get_syscall(_metadata
, child_pid
);
2994 #if defined(__arm__)
2997 * - native ARM registers do NOT expose true syscall.
2998 * - compat ARM registers on ARM64 DO expose true syscall.
3000 ASSERT_EQ(0, uname(&utsbuf
));
3001 if (strncmp(utsbuf
.machine
, "arm", 3) == 0) {
3002 EXPECT_EQ(__NR_nanosleep
, ret
);
3006 EXPECT_EQ(__NR_restart_syscall
, ret
);
3009 /* Write again to end test. */
3010 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3011 ASSERT_EQ(1, write(pipefd
[1], "!", 1));
3012 EXPECT_EQ(0, close(pipefd
[1]));
3014 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3015 if (WIFSIGNALED(status
) || WEXITSTATUS(status
))
3016 _metadata
->passed
= 0;
3019 TEST_SIGNAL(filter_flag_log
, SIGSYS
)
3021 struct sock_filter allow_filter
[] = {
3022 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3024 struct sock_filter kill_filter
[] = {
3025 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
3026 offsetof(struct seccomp_data
, nr
)),
3027 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
3028 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
3029 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3031 struct sock_fprog allow_prog
= {
3032 .len
= (unsigned short)ARRAY_SIZE(allow_filter
),
3033 .filter
= allow_filter
,
3035 struct sock_fprog kill_prog
= {
3036 .len
= (unsigned short)ARRAY_SIZE(kill_filter
),
3037 .filter
= kill_filter
,
3040 pid_t parent
= getppid();
3042 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3045 /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */
3046 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, SECCOMP_FILTER_FLAG_LOG
,
3048 ASSERT_NE(ENOSYS
, errno
) {
3049 TH_LOG("Kernel does not support seccomp syscall!");
3052 TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!");
3054 EXPECT_EQ(EINVAL
, errno
) {
3055 TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!");
3058 /* Verify that a simple, permissive filter can be added with no flags */
3059 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &allow_prog
);
3062 /* See if the same filter can be added with the FILTER_FLAG_LOG flag */
3063 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
3065 ASSERT_NE(EINVAL
, errno
) {
3066 TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!");
3070 /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */
3071 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
3075 EXPECT_EQ(parent
, syscall(__NR_getppid
));
3076 /* getpid() should never return. */
3077 EXPECT_EQ(0, syscall(__NR_getpid
));
3080 TEST(get_action_avail
)
3082 __u32 actions
[] = { SECCOMP_RET_KILL_THREAD
, SECCOMP_RET_TRAP
,
3083 SECCOMP_RET_ERRNO
, SECCOMP_RET_TRACE
,
3084 SECCOMP_RET_LOG
, SECCOMP_RET_ALLOW
};
3085 __u32 unknown_action
= 0x10000000U
;
3089 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[0]);
3090 ASSERT_NE(ENOSYS
, errno
) {
3091 TH_LOG("Kernel does not support seccomp syscall!");
3093 ASSERT_NE(EINVAL
, errno
) {
3094 TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!");
3098 for (i
= 0; i
< ARRAY_SIZE(actions
); i
++) {
3099 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[i
]);
3101 TH_LOG("Expected action (0x%X) not available!",
3106 /* Check that an unknown action is handled properly (EOPNOTSUPP) */
3107 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &unknown_action
);
3109 EXPECT_EQ(errno
, EOPNOTSUPP
);
3117 struct seccomp_metadata md
;
3120 /* Only real root can get metadata. */
3122 SKIP(return, "get_metadata requires real root");
3126 ASSERT_EQ(0, pipe(pipefd
));
3131 struct sock_filter filter
[] = {
3132 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3134 struct sock_fprog prog
= {
3135 .len
= (unsigned short)ARRAY_SIZE(filter
),
3139 /* one with log, one without */
3140 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
,
3141 SECCOMP_FILTER_FLAG_LOG
, &prog
));
3142 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
));
3144 EXPECT_EQ(0, close(pipefd
[0]));
3145 ASSERT_EQ(1, write(pipefd
[1], "1", 1));
3146 ASSERT_EQ(0, close(pipefd
[1]));
3152 ASSERT_EQ(0, close(pipefd
[1]));
3153 ASSERT_EQ(1, read(pipefd
[0], &buf
, 1));
3155 ASSERT_EQ(0, ptrace(PTRACE_ATTACH
, pid
));
3156 ASSERT_EQ(pid
, waitpid(pid
, NULL
, 0));
3158 /* Past here must not use ASSERT or child process is never killed. */
3162 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
3163 EXPECT_EQ(sizeof(md
), ret
) {
3164 if (errno
== EINVAL
)
3165 SKIP(goto skip
, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)");
3168 EXPECT_EQ(md
.flags
, SECCOMP_FILTER_FLAG_LOG
);
3169 EXPECT_EQ(md
.filter_off
, 0);
3172 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
3173 EXPECT_EQ(sizeof(md
), ret
);
3174 EXPECT_EQ(md
.flags
, 0);
3175 EXPECT_EQ(md
.filter_off
, 1);
3178 ASSERT_EQ(0, kill(pid
, SIGKILL
));
3181 static int user_notif_syscall(int nr
, unsigned int flags
)
3183 struct sock_filter filter
[] = {
3184 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
3185 offsetof(struct seccomp_data
, nr
)),
3186 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, nr
, 0, 1),
3187 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_USER_NOTIF
),
3188 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3191 struct sock_fprog prog
= {
3192 .len
= (unsigned short)ARRAY_SIZE(filter
),
3196 return seccomp(SECCOMP_SET_MODE_FILTER
, flags
, &prog
);
3199 #define USER_NOTIF_MAGIC INT_MAX
3200 TEST(user_notification_basic
)
3204 int status
, listener
;
3205 struct seccomp_notif req
= {};
3206 struct seccomp_notif_resp resp
= {};
3207 struct pollfd pollfd
;
3209 struct sock_filter filter
[] = {
3210 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3212 struct sock_fprog prog
= {
3213 .len
= (unsigned short)ARRAY_SIZE(filter
),
3217 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3219 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3225 /* Check that we get -ENOSYS with no listener attached */
3227 if (user_notif_syscall(__NR_getppid
, 0) < 0)
3229 ret
= syscall(__NR_getppid
);
3230 exit(ret
>= 0 || errno
!= ENOSYS
);
3233 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3234 EXPECT_EQ(true, WIFEXITED(status
));
3235 EXPECT_EQ(0, WEXITSTATUS(status
));
3237 /* Add some no-op filters for grins. */
3238 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3239 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3240 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3241 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3243 /* Check that the basic notification machinery works */
3244 listener
= user_notif_syscall(__NR_getppid
,
3245 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3246 ASSERT_GE(listener
, 0);
3248 /* Installing a second listener in the chain should EBUSY */
3249 EXPECT_EQ(user_notif_syscall(__NR_getppid
,
3250 SECCOMP_FILTER_FLAG_NEW_LISTENER
),
3252 EXPECT_EQ(errno
, EBUSY
);
3258 ret
= syscall(__NR_getppid
);
3259 exit(ret
!= USER_NOTIF_MAGIC
);
3262 pollfd
.fd
= listener
;
3263 pollfd
.events
= POLLIN
| POLLOUT
;
3265 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3266 EXPECT_EQ(pollfd
.revents
, POLLIN
);
3268 /* Test that we can't pass garbage to the kernel. */
3269 memset(&req
, 0, sizeof(req
));
3272 ret
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
);
3274 EXPECT_EQ(EINVAL
, errno
);
3278 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3281 pollfd
.fd
= listener
;
3282 pollfd
.events
= POLLIN
| POLLOUT
;
3284 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3285 EXPECT_EQ(pollfd
.revents
, POLLOUT
);
3287 EXPECT_EQ(req
.data
.nr
, __NR_getppid
);
3291 resp
.val
= USER_NOTIF_MAGIC
;
3293 /* check that we make sure flags == 0 */
3295 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3296 EXPECT_EQ(errno
, EINVAL
);
3299 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3301 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3302 EXPECT_EQ(true, WIFEXITED(status
));
3303 EXPECT_EQ(0, WEXITSTATUS(status
));
3306 TEST(user_notification_with_tsync
)
3311 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3313 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3316 /* these were exclusive */
3317 flags
= SECCOMP_FILTER_FLAG_NEW_LISTENER
|
3318 SECCOMP_FILTER_FLAG_TSYNC
;
3319 ASSERT_EQ(-1, user_notif_syscall(__NR_getppid
, flags
));
3320 ASSERT_EQ(EINVAL
, errno
);
3322 /* but now they're not */
3323 flags
|= SECCOMP_FILTER_FLAG_TSYNC_ESRCH
;
3324 ret
= user_notif_syscall(__NR_getppid
, flags
);
3329 TEST(user_notification_kill_in_middle
)
3334 struct seccomp_notif req
= {};
3335 struct seccomp_notif_resp resp
= {};
3337 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3339 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3342 listener
= user_notif_syscall(__NR_getppid
,
3343 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3344 ASSERT_GE(listener
, 0);
3347 * Check that nothing bad happens when we kill the task in the middle
3354 ret
= syscall(__NR_getppid
);
3355 exit(ret
!= USER_NOTIF_MAGIC
);
3358 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3359 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ID_VALID
, &req
.id
), 0);
3361 EXPECT_EQ(kill(pid
, SIGKILL
), 0);
3362 EXPECT_EQ(waitpid(pid
, NULL
, 0), pid
);
3364 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ID_VALID
, &req
.id
), -1);
3367 ret
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
);
3369 EXPECT_EQ(errno
, ENOENT
);
3372 static int handled
= -1;
3374 static void signal_handler(int signal
)
3376 if (write(handled
, "c", 1) != 1)
3377 perror("write from signal");
3380 TEST(user_notification_signal
)
3384 int status
, listener
, sk_pair
[2];
3385 struct seccomp_notif req
= {};
3386 struct seccomp_notif_resp resp
= {};
3389 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3391 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3394 ASSERT_EQ(socketpair(PF_LOCAL
, SOCK_SEQPACKET
, 0, sk_pair
), 0);
3396 listener
= user_notif_syscall(__NR_gettid
,
3397 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3398 ASSERT_GE(listener
, 0);
3405 handled
= sk_pair
[1];
3406 if (signal(SIGUSR1
, signal_handler
) == SIG_ERR
) {
3411 * ERESTARTSYS behavior is a bit hard to test, because we need
3412 * to rely on a signal that has not yet been handled. Let's at
3413 * least check that the error code gets propagated through, and
3414 * hope that it doesn't break when there is actually a signal :)
3416 ret
= syscall(__NR_gettid
);
3417 exit(!(ret
== -1 && errno
== 512));
3422 memset(&req
, 0, sizeof(req
));
3423 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3425 EXPECT_EQ(kill(pid
, SIGUSR1
), 0);
3428 * Make sure the signal really is delivered, which means we're not
3429 * stuck in the user notification code any more and the notification
3432 EXPECT_EQ(read(sk_pair
[0], &c
, 1), 1);
3435 resp
.error
= -EPERM
;
3438 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3439 EXPECT_EQ(errno
, ENOENT
);
3441 memset(&req
, 0, sizeof(req
));
3442 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3445 resp
.error
= -512; /* -ERESTARTSYS */
3448 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3450 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3451 EXPECT_EQ(true, WIFEXITED(status
));
3452 EXPECT_EQ(0, WEXITSTATUS(status
));
3455 TEST(user_notification_closed_listener
)
3459 int status
, listener
;
3461 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3463 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3466 listener
= user_notif_syscall(__NR_getppid
,
3467 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3468 ASSERT_GE(listener
, 0);
3471 * Check that we get an ENOSYS when the listener is closed.
3477 ret
= syscall(__NR_getppid
);
3478 exit(ret
!= -1 && errno
!= ENOSYS
);
3483 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3484 EXPECT_EQ(true, WIFEXITED(status
));
3485 EXPECT_EQ(0, WEXITSTATUS(status
));
3489 * Check that a pid in a child namespace still shows up as valid in ours.
3491 TEST(user_notification_child_pid_ns
)
3494 int status
, listener
;
3495 struct seccomp_notif req
= {};
3496 struct seccomp_notif_resp resp
= {};
3498 ASSERT_EQ(unshare(CLONE_NEWUSER
| CLONE_NEWPID
), 0) {
3499 if (errno
== EINVAL
)
3500 SKIP(return, "kernel missing CLONE_NEWUSER support");
3503 listener
= user_notif_syscall(__NR_getppid
,
3504 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3505 ASSERT_GE(listener
, 0);
3511 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3513 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3514 EXPECT_EQ(req
.pid
, pid
);
3518 resp
.val
= USER_NOTIF_MAGIC
;
3520 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3522 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3523 EXPECT_EQ(true, WIFEXITED(status
));
3524 EXPECT_EQ(0, WEXITSTATUS(status
));
3529 * Check that a pid in a sibling (i.e. unrelated) namespace shows up as 0, i.e.
3532 TEST(user_notification_sibling_pid_ns
)
3535 int status
, listener
;
3536 struct seccomp_notif req
= {};
3537 struct seccomp_notif_resp resp
= {};
3539 ASSERT_EQ(prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0), 0) {
3540 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3543 listener
= user_notif_syscall(__NR_getppid
,
3544 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3545 ASSERT_GE(listener
, 0);
3551 ASSERT_EQ(unshare(CLONE_NEWPID
), 0);
3557 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3559 EXPECT_EQ(waitpid(pid2
, &status
, 0), pid2
);
3560 EXPECT_EQ(true, WIFEXITED(status
));
3561 EXPECT_EQ(0, WEXITSTATUS(status
));
3562 exit(WEXITSTATUS(status
));
3565 /* Create the sibling ns, and sibling in it. */
3566 ASSERT_EQ(unshare(CLONE_NEWPID
), 0) {
3568 SKIP(return, "CLONE_NEWPID requires CAP_SYS_ADMIN");
3570 ASSERT_EQ(errno
, 0);
3576 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3578 * The pid should be 0, i.e. the task is in some namespace that
3581 EXPECT_EQ(req
.pid
, 0);
3585 resp
.val
= USER_NOTIF_MAGIC
;
3587 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3593 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3594 EXPECT_EQ(true, WIFEXITED(status
));
3595 EXPECT_EQ(0, WEXITSTATUS(status
));
3597 EXPECT_EQ(waitpid(pid2
, &status
, 0), pid2
);
3598 EXPECT_EQ(true, WIFEXITED(status
));
3599 EXPECT_EQ(0, WEXITSTATUS(status
));
3602 TEST(user_notification_fault_recv
)
3605 int status
, listener
;
3606 struct seccomp_notif req
= {};
3607 struct seccomp_notif_resp resp
= {};
3609 ASSERT_EQ(unshare(CLONE_NEWUSER
), 0);
3611 listener
= user_notif_syscall(__NR_getppid
,
3612 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3613 ASSERT_GE(listener
, 0);
3619 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3621 /* Do a bad recv() */
3622 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, NULL
), -1);
3623 EXPECT_EQ(errno
, EFAULT
);
3625 /* We should still be able to receive this notification, though. */
3626 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3627 EXPECT_EQ(req
.pid
, pid
);
3631 resp
.val
= USER_NOTIF_MAGIC
;
3633 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3635 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3636 EXPECT_EQ(true, WIFEXITED(status
));
3637 EXPECT_EQ(0, WEXITSTATUS(status
));
3640 TEST(seccomp_get_notif_sizes
)
3642 struct seccomp_notif_sizes sizes
;
3644 ASSERT_EQ(seccomp(SECCOMP_GET_NOTIF_SIZES
, 0, &sizes
), 0);
3645 EXPECT_EQ(sizes
.seccomp_notif
, sizeof(struct seccomp_notif
));
3646 EXPECT_EQ(sizes
.seccomp_notif_resp
, sizeof(struct seccomp_notif_resp
));
3649 TEST(user_notification_continue
)
3653 int status
, listener
;
3654 struct seccomp_notif req
= {};
3655 struct seccomp_notif_resp resp
= {};
3656 struct pollfd pollfd
;
3658 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3660 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3663 listener
= user_notif_syscall(__NR_dup
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3664 ASSERT_GE(listener
, 0);
3670 int dup_fd
, pipe_fds
[2];
3673 ASSERT_GE(pipe(pipe_fds
), 0);
3675 dup_fd
= dup(pipe_fds
[0]);
3676 ASSERT_GE(dup_fd
, 0);
3677 EXPECT_NE(pipe_fds
[0], dup_fd
);
3680 ASSERT_EQ(filecmp(self
, self
, pipe_fds
[0], dup_fd
), 0);
3684 pollfd
.fd
= listener
;
3685 pollfd
.events
= POLLIN
| POLLOUT
;
3687 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3688 EXPECT_EQ(pollfd
.revents
, POLLIN
);
3690 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3692 pollfd
.fd
= listener
;
3693 pollfd
.events
= POLLIN
| POLLOUT
;
3695 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3696 EXPECT_EQ(pollfd
.revents
, POLLOUT
);
3698 EXPECT_EQ(req
.data
.nr
, __NR_dup
);
3701 resp
.flags
= SECCOMP_USER_NOTIF_FLAG_CONTINUE
;
3704 * Verify that setting SECCOMP_USER_NOTIF_FLAG_CONTINUE enforces other
3708 resp
.val
= USER_NOTIF_MAGIC
;
3709 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3710 EXPECT_EQ(errno
, EINVAL
);
3712 resp
.error
= USER_NOTIF_MAGIC
;
3714 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3715 EXPECT_EQ(errno
, EINVAL
);
3719 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0) {
3720 if (errno
== EINVAL
)
3721 SKIP(goto skip
, "Kernel does not support SECCOMP_USER_NOTIF_FLAG_CONTINUE");
3725 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3726 EXPECT_EQ(true, WIFEXITED(status
));
3727 EXPECT_EQ(0, WEXITSTATUS(status
)) {
3728 if (WEXITSTATUS(status
) == 2) {
3729 SKIP(return, "Kernel does not support kcmp() syscall");
3735 TEST(user_notification_filter_empty
)
3740 struct pollfd pollfd
;
3741 struct clone_args args
= {
3742 .flags
= CLONE_FILES
,
3743 .exit_signal
= SIGCHLD
,
3746 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3748 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3751 pid
= sys_clone3(&args
, sizeof(args
));
3757 listener
= user_notif_syscall(__NR_mknodat
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3759 _exit(EXIT_FAILURE
);
3761 if (dup2(listener
, 200) != 200)
3762 _exit(EXIT_FAILURE
);
3766 _exit(EXIT_SUCCESS
);
3769 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3770 EXPECT_EQ(true, WIFEXITED(status
));
3771 EXPECT_EQ(0, WEXITSTATUS(status
));
3774 * The seccomp filter has become unused so we should be notified once
3775 * the kernel gets around to cleaning up task struct.
3778 pollfd
.events
= POLLHUP
;
3780 EXPECT_GT(poll(&pollfd
, 1, 2000), 0);
3781 EXPECT_GT((pollfd
.revents
& POLLHUP
) ?: 0, 0);
3784 static void *do_thread(void *data
)
3789 TEST(user_notification_filter_empty_threaded
)
3794 struct pollfd pollfd
;
3795 struct clone_args args
= {
3796 .flags
= CLONE_FILES
,
3797 .exit_signal
= SIGCHLD
,
3800 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3802 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3805 pid
= sys_clone3(&args
, sizeof(args
));
3810 int listener
, status
;
3813 listener
= user_notif_syscall(__NR_dup
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3815 _exit(EXIT_FAILURE
);
3817 if (dup2(listener
, 200) != 200)
3818 _exit(EXIT_FAILURE
);
3824 _exit(EXIT_FAILURE
);
3827 _exit(EXIT_SUCCESS
);
3831 _exit(EXIT_FAILURE
);
3834 _exit(EXIT_SUCCESS
);
3836 if (pthread_create(&thread
, NULL
, do_thread
, NULL
) ||
3837 pthread_join(thread
, NULL
))
3838 _exit(EXIT_FAILURE
);
3840 if (pthread_create(&thread
, NULL
, do_thread
, NULL
) ||
3841 pthread_join(thread
, NULL
))
3842 _exit(EXIT_FAILURE
);
3844 if (waitpid(pid1
, &status
, 0) != pid1
|| !WIFEXITED(status
) ||
3845 WEXITSTATUS(status
))
3846 _exit(EXIT_FAILURE
);
3848 if (waitpid(pid2
, &status
, 0) != pid2
|| !WIFEXITED(status
) ||
3849 WEXITSTATUS(status
))
3850 _exit(EXIT_FAILURE
);
3855 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3856 EXPECT_EQ(true, WIFEXITED(status
));
3857 EXPECT_EQ(0, WEXITSTATUS(status
));
3860 * The seccomp filter has become unused so we should be notified once
3861 * the kernel gets around to cleaning up task struct.
3864 pollfd
.events
= POLLHUP
;
3866 EXPECT_GT(poll(&pollfd
, 1, 2000), 0);
3867 EXPECT_GT((pollfd
.revents
& POLLHUP
) ?: 0, 0);
3870 TEST(user_notification_addfd
)
3874 int status
, listener
, memfd
, fd
;
3875 struct seccomp_notif_addfd addfd
= {};
3876 struct seccomp_notif_addfd_small small
= {};
3877 struct seccomp_notif_addfd_big big
= {};
3878 struct seccomp_notif req
= {};
3879 struct seccomp_notif_resp resp
= {};
3881 struct timespec delay
= { .tv_nsec
= 100000000 };
3883 memfd
= memfd_create("test", 0);
3884 ASSERT_GE(memfd
, 0);
3886 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3888 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3891 /* Check that the basic notification machinery works */
3892 listener
= user_notif_syscall(__NR_getppid
,
3893 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3894 ASSERT_GE(listener
, 0);
3900 if (syscall(__NR_getppid
) != USER_NOTIF_MAGIC
)
3902 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3905 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3907 addfd
.srcfd
= memfd
;
3912 /* Verify bad newfd_flags cannot be set */
3913 addfd
.newfd_flags
= ~O_CLOEXEC
;
3914 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
3915 EXPECT_EQ(errno
, EINVAL
);
3916 addfd
.newfd_flags
= O_CLOEXEC
;
3918 /* Verify bad flags cannot be set */
3920 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
3921 EXPECT_EQ(errno
, EINVAL
);
3924 /* Verify that remote_fd cannot be set without setting flags */
3926 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
3927 EXPECT_EQ(errno
, EINVAL
);
3930 /* Verify small size cannot be set */
3931 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD_SMALL
, &small
), -1);
3932 EXPECT_EQ(errno
, EINVAL
);
3934 /* Verify we can't send bits filled in unknown buffer area */
3935 memset(&big
, 0xAA, sizeof(big
));
3937 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD_BIG
, &big
), -1);
3938 EXPECT_EQ(errno
, E2BIG
);
3941 /* Verify we can set an arbitrary remote fd */
3942 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
);
3944 * The child has fds 0(stdin), 1(stdout), 2(stderr), 3(memfd),
3945 * 4(listener), so the newly allocated fd should be 5.
3948 EXPECT_EQ(filecmp(getpid(), pid
, memfd
, fd
), 0);
3950 /* Verify we can set an arbitrary remote fd with large size */
3951 memset(&big
, 0x0, sizeof(big
));
3953 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD_BIG
, &big
);
3956 /* Verify we can set a specific remote fd */
3958 addfd
.flags
= SECCOMP_ADDFD_FLAG_SETFD
;
3959 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
);
3961 EXPECT_EQ(filecmp(getpid(), pid
, memfd
, fd
), 0);
3963 /* Resume syscall */
3966 resp
.val
= USER_NOTIF_MAGIC
;
3967 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3970 * This sets the ID of the ADD FD to the last request plus 1. The
3971 * notification ID increments 1 per notification.
3973 addfd
.id
= req
.id
+ 1;
3975 /* This spins until the underlying notification is generated */
3976 while (ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
) != -1 &&
3977 errno
!= -EINPROGRESS
)
3978 nanosleep(&delay
, NULL
);
3980 memset(&req
, 0, sizeof(req
));
3981 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3982 ASSERT_EQ(addfd
.id
, req
.id
);
3986 resp
.val
= USER_NOTIF_MAGIC
;
3987 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3989 /* Wait for child to finish. */
3990 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3991 EXPECT_EQ(true, WIFEXITED(status
));
3992 EXPECT_EQ(0, WEXITSTATUS(status
));
3997 TEST(user_notification_addfd_rlimit
)
4001 int status
, listener
, memfd
;
4002 struct seccomp_notif_addfd addfd
= {};
4003 struct seccomp_notif req
= {};
4004 struct seccomp_notif_resp resp
= {};
4005 const struct rlimit lim
= {
4010 memfd
= memfd_create("test", 0);
4011 ASSERT_GE(memfd
, 0);
4013 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4015 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4018 /* Check that the basic notification machinery works */
4019 listener
= user_notif_syscall(__NR_getppid
,
4020 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
4021 ASSERT_GE(listener
, 0);
4027 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
4030 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4032 ASSERT_EQ(prlimit(pid
, RLIMIT_NOFILE
, &lim
, NULL
), 0);
4034 addfd
.srcfd
= memfd
;
4035 addfd
.newfd_flags
= O_CLOEXEC
;
4040 /* Should probably spot check /proc/sys/fs/file-nr */
4041 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4042 EXPECT_EQ(errno
, EMFILE
);
4045 addfd
.flags
= SECCOMP_ADDFD_FLAG_SETFD
;
4046 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4047 EXPECT_EQ(errno
, EBADF
);
4051 resp
.val
= USER_NOTIF_MAGIC
;
4053 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4055 /* Wait for child to finish. */
4056 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4057 EXPECT_EQ(true, WIFEXITED(status
));
4058 EXPECT_EQ(0, WEXITSTATUS(status
));
4065 * - expand NNP testing
4066 * - better arch-specific TRACE and TRAP handlers.
4067 * - endianness checking when appropriate
4068 * - 64-bit arg prodding
4069 * - arch value testing (x86 modes especially)
4070 * - verify that FILTER_FLAG_LOG filters generate log messages
4071 * - verify that RET_LOG generates log messages