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 # ifndef PTRACE_SET_SYSCALL
1712 # define PTRACE_SET_SYSCALL 23
1714 # define SYSCALL_NUM_SET(_regs, _nr) \
1715 EXPECT_EQ(0, ptrace(PTRACE_SET_SYSCALL, tracee, NULL, _nr))
1716 # define SYSCALL_RET(_regs) (_regs).ARM_r0
1717 #elif defined(__aarch64__)
1718 # define ARCH_REGS struct user_pt_regs
1719 # define SYSCALL_NUM(_regs) (_regs).regs[8]
1720 # ifndef NT_ARM_SYSTEM_CALL
1721 # define NT_ARM_SYSTEM_CALL 0x404
1723 # define SYSCALL_NUM_SET(_regs, _nr) \
1726 typeof(_nr) __nr = (_nr); \
1727 __v.iov_base = &__nr; \
1728 __v.iov_len = sizeof(__nr); \
1729 EXPECT_EQ(0, ptrace(PTRACE_SETREGSET, tracee, \
1730 NT_ARM_SYSTEM_CALL, &__v)); \
1732 # define SYSCALL_RET(_regs) (_regs).regs[0]
1733 #elif defined(__riscv) && __riscv_xlen == 64
1734 # define ARCH_REGS struct user_regs_struct
1735 # define SYSCALL_NUM(_regs) (_regs).a7
1736 # define SYSCALL_RET(_regs) (_regs).a0
1737 #elif defined(__csky__)
1738 # define ARCH_REGS struct pt_regs
1739 # if defined(__CSKYABIV2__)
1740 # define SYSCALL_NUM(_regs) (_regs).regs[3]
1742 # define SYSCALL_NUM(_regs) (_regs).regs[9]
1744 # define SYSCALL_RET(_regs) (_regs).a0
1745 #elif defined(__hppa__)
1746 # define ARCH_REGS struct user_regs_struct
1747 # define SYSCALL_NUM(_regs) (_regs).gr[20]
1748 # define SYSCALL_RET(_regs) (_regs).gr[28]
1749 #elif defined(__powerpc__)
1750 # define ARCH_REGS struct pt_regs
1751 # define SYSCALL_NUM(_regs) (_regs).gpr[0]
1752 # define SYSCALL_RET(_regs) (_regs).gpr[3]
1753 # define SYSCALL_RET_SET(_regs, _val) \
1755 typeof(_val) _result = (_val); \
1757 * A syscall error is signaled by CR0 SO bit \
1758 * and the code is stored as a positive value. \
1760 if (_result < 0) { \
1761 SYSCALL_RET(_regs) = -result; \
1762 (_regs).ccr |= 0x10000000; \
1764 SYSCALL_RET(_regs) = result; \
1765 (_regs).ccr &= ~0x10000000; \
1768 #elif defined(__s390__)
1769 # define ARCH_REGS s390_regs
1770 # define SYSCALL_NUM(_regs) (_regs).gprs[2]
1771 # define SYSCALL_RET_SET(_regs, _val) \
1772 TH_LOG("Can't modify syscall return on this architecture")
1773 #elif defined(__mips__)
1774 # include <asm/unistd_nr_n32.h>
1775 # include <asm/unistd_nr_n64.h>
1776 # include <asm/unistd_nr_o32.h>
1777 # define ARCH_REGS struct pt_regs
1778 # define SYSCALL_NUM(_regs) \
1780 typeof((_regs).regs[2]) _nr; \
1781 if ((_regs).regs[2] == __NR_O32_Linux) \
1782 _nr = (_regs).regs[4]; \
1784 _nr = (_regs).regs[2]; \
1787 # define SYSCALL_NUM_SET(_regs, _nr) \
1789 if ((_regs).regs[2] == __NR_O32_Linux) \
1790 (_regs).regs[4] = _nr; \
1792 (_regs).regs[2] = _nr; \
1794 # define SYSCALL_RET_SET(_regs, _val) \
1795 TH_LOG("Can't modify syscall return on this architecture")
1796 #elif defined(__xtensa__)
1797 # define ARCH_REGS struct user_pt_regs
1798 # define SYSCALL_NUM(_regs) (_regs).syscall
1800 * On xtensa syscall return value is in the register
1801 * a2 of the current window which is not fixed.
1803 #define SYSCALL_RET(_regs) (_regs).a[(_regs).windowbase * 4 + 2]
1804 #elif defined(__sh__)
1805 # define ARCH_REGS struct pt_regs
1806 # define SYSCALL_NUM(_regs) (_regs).gpr[3]
1807 # define SYSCALL_RET(_regs) (_regs).gpr[0]
1809 # error "Do not know how to find your architecture's registers and syscalls"
1813 * Most architectures can change the syscall by just updating the
1814 * associated register. This is the default if not defined above.
1816 #ifndef SYSCALL_NUM_SET
1817 # define SYSCALL_NUM_SET(_regs, _nr) \
1819 SYSCALL_NUM(_regs) = (_nr); \
1823 * Most architectures can change the syscall return value by just
1824 * writing to the SYSCALL_RET register. This is the default if not
1825 * defined above. If an architecture cannot set the return value
1826 * (for example when the syscall and return value register is
1827 * shared), report it with TH_LOG() in an arch-specific definition
1828 * of SYSCALL_RET_SET() above, and leave SYSCALL_RET undefined.
1830 #if !defined(SYSCALL_RET) && !defined(SYSCALL_RET_SET)
1831 # error "One of SYSCALL_RET or SYSCALL_RET_SET is needed for this arch"
1833 #ifndef SYSCALL_RET_SET
1834 # define SYSCALL_RET_SET(_regs, _val) \
1836 SYSCALL_RET(_regs) = (_val); \
1840 /* When the syscall return can't be changed, stub out the tests for it. */
1842 # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(-1, action)
1844 # define EXPECT_SYSCALL_RETURN(val, action) \
1848 EXPECT_EQ(-1, action); \
1849 EXPECT_EQ(-(val), errno); \
1851 EXPECT_EQ(val, action); \
1857 * Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for
1858 * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux).
1860 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__)
1861 # define ARCH_GETREGS(_regs) ptrace(PTRACE_GETREGS, tracee, 0, &(_regs))
1862 # define ARCH_SETREGS(_regs) ptrace(PTRACE_SETREGS, tracee, 0, &(_regs))
1864 # define ARCH_GETREGS(_regs) ({ \
1866 __v.iov_base = &(_regs); \
1867 __v.iov_len = sizeof(_regs); \
1868 ptrace(PTRACE_GETREGSET, tracee, NT_PRSTATUS, &__v); \
1870 # define ARCH_SETREGS(_regs) ({ \
1872 __v.iov_base = &(_regs); \
1873 __v.iov_len = sizeof(_regs); \
1874 ptrace(PTRACE_SETREGSET, tracee, NT_PRSTATUS, &__v); \
1878 /* Architecture-specific syscall fetching routine. */
1879 int get_syscall(struct __test_metadata
*_metadata
, pid_t tracee
)
1883 EXPECT_EQ(0, ARCH_GETREGS(regs
)) {
1887 return SYSCALL_NUM(regs
);
1890 /* Architecture-specific syscall changing routine. */
1891 void __change_syscall(struct __test_metadata
*_metadata
,
1892 pid_t tracee
, long *syscall
, long *ret
)
1894 ARCH_REGS orig
, regs
;
1896 /* Do not get/set registers if we have nothing to do. */
1897 if (!syscall
&& !ret
)
1900 EXPECT_EQ(0, ARCH_GETREGS(regs
)) {
1906 SYSCALL_NUM_SET(regs
, *syscall
);
1909 SYSCALL_RET_SET(regs
, *ret
);
1911 /* Flush any register changes made. */
1912 if (memcmp(&orig
, ®s
, sizeof(orig
)) != 0)
1913 EXPECT_EQ(0, ARCH_SETREGS(regs
));
1916 /* Change only syscall number. */
1917 void change_syscall_nr(struct __test_metadata
*_metadata
,
1918 pid_t tracee
, long syscall
)
1920 __change_syscall(_metadata
, tracee
, &syscall
, NULL
);
1923 /* Change syscall return value (and set syscall number to -1). */
1924 void change_syscall_ret(struct __test_metadata
*_metadata
,
1925 pid_t tracee
, long ret
)
1929 __change_syscall(_metadata
, tracee
, &syscall
, &ret
);
1932 void tracer_seccomp(struct __test_metadata
*_metadata
, pid_t tracee
,
1933 int status
, void *args
)
1938 /* Make sure we got the right message. */
1939 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1942 /* Validate and take action on expected syscalls. */
1945 /* change getpid to getppid. */
1946 EXPECT_EQ(__NR_getpid
, get_syscall(_metadata
, tracee
));
1947 change_syscall_nr(_metadata
, tracee
, __NR_getppid
);
1950 /* skip gettid with valid return code. */
1951 EXPECT_EQ(__NR_gettid
, get_syscall(_metadata
, tracee
));
1952 change_syscall_ret(_metadata
, tracee
, 45000);
1955 /* skip openat with error. */
1956 EXPECT_EQ(__NR_openat
, get_syscall(_metadata
, tracee
));
1957 change_syscall_ret(_metadata
, tracee
, -ESRCH
);
1960 /* do nothing (allow getppid) */
1961 EXPECT_EQ(__NR_getppid
, get_syscall(_metadata
, tracee
));
1965 TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg
);
1966 kill(tracee
, SIGKILL
);
1972 FIXTURE(TRACE_syscall
) {
1973 struct sock_fprog prog
;
1974 pid_t tracer
, mytid
, mypid
, parent
;
1978 void tracer_ptrace(struct __test_metadata
*_metadata
, pid_t tracee
,
1979 int status
, void *args
)
1984 long syscall_nr_val
, syscall_ret_val
;
1985 long *syscall_nr
= NULL
, *syscall_ret
= NULL
;
1986 FIXTURE_DATA(TRACE_syscall
) *self
= args
;
1989 * The traditional way to tell PTRACE_SYSCALL entry/exit
1994 /* Make sure we got an appropriate message. */
1995 ret
= ptrace(PTRACE_GETEVENTMSG
, tracee
, NULL
, &msg
);
1997 EXPECT_EQ(entry
? PTRACE_EVENTMSG_SYSCALL_ENTRY
1998 : PTRACE_EVENTMSG_SYSCALL_EXIT
, msg
);
2001 * Some architectures only support setting return values during
2002 * syscall exit under ptrace, and on exit the syscall number may
2003 * no longer be available. Therefore, save the initial sycall
2004 * number here, so it can be examined during both entry and exit
2008 self
->syscall_nr
= get_syscall(_metadata
, tracee
);
2012 syscall_nr
= &syscall_nr_val
;
2013 syscall_ret
= &syscall_ret_val
;
2015 /* Now handle the actual rewriting cases. */
2016 switch (self
->syscall_nr
) {
2018 syscall_nr_val
= __NR_getppid
;
2019 /* Never change syscall return for this case. */
2023 syscall_nr_val
= -1;
2024 syscall_ret_val
= 45000;
2027 syscall_nr_val
= -1;
2028 syscall_ret_val
= -ESRCH
;
2031 /* Unhandled, do nothing. */
2035 __change_syscall(_metadata
, tracee
, syscall_nr
, syscall_ret
);
2038 FIXTURE_VARIANT(TRACE_syscall
) {
2040 * All of the SECCOMP_RET_TRACE behaviors can be tested with either
2041 * SECCOMP_RET_TRACE+PTRACE_CONT or plain ptrace()+PTRACE_SYSCALL.
2042 * This indicates if we should use SECCOMP_RET_TRACE (false), or
2048 FIXTURE_VARIANT_ADD(TRACE_syscall
, ptrace
) {
2052 FIXTURE_VARIANT_ADD(TRACE_syscall
, seccomp
) {
2053 .use_ptrace
= false,
2056 FIXTURE_SETUP(TRACE_syscall
)
2058 struct sock_filter filter
[] = {
2059 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2060 offsetof(struct seccomp_data
, nr
)),
2061 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
2062 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1002),
2063 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_gettid
, 0, 1),
2064 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1003),
2065 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_openat
, 0, 1),
2066 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1004),
2067 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2068 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
| 0x1005),
2069 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2071 struct sock_fprog prog
= {
2072 .len
= (unsigned short)ARRAY_SIZE(filter
),
2077 /* Prepare some testable syscall results. */
2078 self
->mytid
= syscall(__NR_gettid
);
2079 ASSERT_GT(self
->mytid
, 0);
2080 ASSERT_NE(self
->mytid
, 1) {
2081 TH_LOG("Running this test as init is not supported. :)");
2084 self
->mypid
= getpid();
2085 ASSERT_GT(self
->mypid
, 0);
2086 ASSERT_EQ(self
->mytid
, self
->mypid
);
2088 self
->parent
= getppid();
2089 ASSERT_GT(self
->parent
, 0);
2090 ASSERT_NE(self
->parent
, self
->mypid
);
2092 /* Launch tracer. */
2093 self
->tracer
= setup_trace_fixture(_metadata
,
2094 variant
->use_ptrace
? tracer_ptrace
2096 self
, variant
->use_ptrace
);
2098 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2101 if (variant
->use_ptrace
)
2104 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2108 FIXTURE_TEARDOWN(TRACE_syscall
)
2110 teardown_trace_fixture(_metadata
, self
->tracer
);
2113 TEST(negative_ENOSYS
)
2116 * There should be no difference between an "internal" skip
2117 * and userspace asking for syscall "-1".
2120 EXPECT_EQ(-1, syscall(-1));
2121 EXPECT_EQ(errno
, ENOSYS
);
2122 /* And no difference for "still not valid but not -1". */
2124 EXPECT_EQ(-1, syscall(-101));
2125 EXPECT_EQ(errno
, ENOSYS
);
2128 TEST_F(TRACE_syscall
, negative_ENOSYS
)
2130 negative_ENOSYS(_metadata
);
2133 TEST_F(TRACE_syscall
, syscall_allowed
)
2135 /* getppid works as expected (no changes). */
2136 EXPECT_EQ(self
->parent
, syscall(__NR_getppid
));
2137 EXPECT_NE(self
->mypid
, syscall(__NR_getppid
));
2140 TEST_F(TRACE_syscall
, syscall_redirected
)
2142 /* getpid has been redirected to getppid as expected. */
2143 EXPECT_EQ(self
->parent
, syscall(__NR_getpid
));
2144 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
2147 TEST_F(TRACE_syscall
, syscall_errno
)
2149 /* Tracer should skip the open syscall, resulting in ESRCH. */
2150 EXPECT_SYSCALL_RETURN(-ESRCH
, syscall(__NR_openat
));
2153 TEST_F(TRACE_syscall
, syscall_faked
)
2155 /* Tracer skips the gettid syscall and store altered return value. */
2156 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid
));
2159 TEST_F(TRACE_syscall
, skip_after
)
2161 struct sock_filter filter
[] = {
2162 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2163 offsetof(struct seccomp_data
, nr
)),
2164 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2165 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EPERM
),
2166 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2168 struct sock_fprog prog
= {
2169 .len
= (unsigned short)ARRAY_SIZE(filter
),
2174 /* Install additional "errno on getppid" filter. */
2175 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2178 /* Tracer will redirect getpid to getppid, and we should see EPERM. */
2180 EXPECT_EQ(-1, syscall(__NR_getpid
));
2181 EXPECT_EQ(EPERM
, errno
);
2184 TEST_F_SIGNAL(TRACE_syscall
, kill_after
, SIGSYS
)
2186 struct sock_filter filter
[] = {
2187 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2188 offsetof(struct seccomp_data
, nr
)),
2189 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getppid
, 0, 1),
2190 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2191 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2193 struct sock_fprog prog
= {
2194 .len
= (unsigned short)ARRAY_SIZE(filter
),
2199 /* Install additional "death on getppid" filter. */
2200 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2203 /* Tracer will redirect getpid to getppid, and we should die. */
2204 EXPECT_NE(self
->mypid
, syscall(__NR_getpid
));
2207 TEST(seccomp_syscall
)
2209 struct sock_filter filter
[] = {
2210 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2212 struct sock_fprog prog
= {
2213 .len
= (unsigned short)ARRAY_SIZE(filter
),
2218 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
2220 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2223 /* Reject insane operation. */
2224 ret
= seccomp(-1, 0, &prog
);
2225 ASSERT_NE(ENOSYS
, errno
) {
2226 TH_LOG("Kernel does not support seccomp syscall!");
2228 EXPECT_EQ(EINVAL
, errno
) {
2229 TH_LOG("Did not reject crazy op value!");
2232 /* Reject strict with flags or pointer. */
2233 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, -1, NULL
);
2234 EXPECT_EQ(EINVAL
, errno
) {
2235 TH_LOG("Did not reject mode strict with flags!");
2237 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, &prog
);
2238 EXPECT_EQ(EINVAL
, errno
) {
2239 TH_LOG("Did not reject mode strict with uargs!");
2242 /* Reject insane args for filter. */
2243 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, -1, &prog
);
2244 EXPECT_EQ(EINVAL
, errno
) {
2245 TH_LOG("Did not reject crazy filter flags!");
2247 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, NULL
);
2248 EXPECT_EQ(EFAULT
, errno
) {
2249 TH_LOG("Did not reject NULL filter!");
2252 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2253 EXPECT_EQ(0, errno
) {
2254 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s",
2259 TEST(seccomp_syscall_mode_lock
)
2261 struct sock_filter filter
[] = {
2262 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2264 struct sock_fprog prog
= {
2265 .len
= (unsigned short)ARRAY_SIZE(filter
),
2270 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2272 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2275 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2276 ASSERT_NE(ENOSYS
, errno
) {
2277 TH_LOG("Kernel does not support seccomp syscall!");
2280 TH_LOG("Could not install filter!");
2283 /* Make sure neither entry point will switch to strict. */
2284 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_STRICT
, 0, 0, 0);
2285 EXPECT_EQ(EINVAL
, errno
) {
2286 TH_LOG("Switched to mode strict!");
2289 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, 0, NULL
);
2290 EXPECT_EQ(EINVAL
, errno
) {
2291 TH_LOG("Switched to mode strict!");
2296 * Test detection of known and unknown filter flags. Userspace needs to be able
2297 * to check if a filter flag is supported by the current kernel and a good way
2298 * of doing that is by attempting to enter filter mode, with the flag bit in
2299 * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates
2300 * that the flag is valid and EINVAL indicates that the flag is invalid.
2302 TEST(detect_seccomp_filter_flags
)
2304 unsigned int flags
[] = { SECCOMP_FILTER_FLAG_TSYNC
,
2305 SECCOMP_FILTER_FLAG_LOG
,
2306 SECCOMP_FILTER_FLAG_SPEC_ALLOW
,
2307 SECCOMP_FILTER_FLAG_NEW_LISTENER
,
2308 SECCOMP_FILTER_FLAG_TSYNC_ESRCH
};
2309 unsigned int exclusive
[] = {
2310 SECCOMP_FILTER_FLAG_TSYNC
,
2311 SECCOMP_FILTER_FLAG_NEW_LISTENER
};
2312 unsigned int flag
, all_flags
, exclusive_mask
;
2316 /* Test detection of individual known-good filter flags */
2317 for (i
= 0, all_flags
= 0; i
< ARRAY_SIZE(flags
); i
++) {
2321 /* Make sure the flag is a single bit! */
2330 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2331 ASSERT_NE(ENOSYS
, errno
) {
2332 TH_LOG("Kernel does not support seccomp syscall!");
2335 EXPECT_EQ(EFAULT
, errno
) {
2336 TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!",
2344 * Test detection of all known-good filter flags combined. But
2345 * for the exclusive flags we need to mask them out and try them
2346 * individually for the "all flags" testing.
2349 for (i
= 0; i
< ARRAY_SIZE(exclusive
); i
++)
2350 exclusive_mask
|= exclusive
[i
];
2351 for (i
= 0; i
< ARRAY_SIZE(exclusive
); i
++) {
2352 flag
= all_flags
& ~exclusive_mask
;
2353 flag
|= exclusive
[i
];
2355 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2357 EXPECT_EQ(EFAULT
, errno
) {
2358 TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2363 /* Test detection of an unknown filter flags, without exclusives. */
2365 flag
&= ~exclusive_mask
;
2366 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2368 EXPECT_EQ(EINVAL
, errno
) {
2369 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!",
2374 * Test detection of an unknown filter flag that may simply need to be
2375 * added to this test
2377 flag
= flags
[ARRAY_SIZE(flags
) - 1] << 1;
2378 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flag
, NULL
);
2380 EXPECT_EQ(EINVAL
, errno
) {
2381 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?",
2388 struct sock_filter filter
[] = {
2389 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2391 struct sock_fprog prog
= {
2392 .len
= (unsigned short)ARRAY_SIZE(filter
),
2397 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, NULL
, 0, 0);
2399 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2402 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2404 ASSERT_NE(ENOSYS
, errno
) {
2405 TH_LOG("Kernel does not support seccomp syscall!");
2408 TH_LOG("Could not install initial filter with TSYNC!");
2412 #define TSYNC_SIBLINGS 2
2413 struct tsync_sibling
{
2417 pthread_cond_t
*cond
;
2418 pthread_mutex_t
*mutex
;
2421 struct sock_fprog
*prog
;
2422 struct __test_metadata
*metadata
;
2426 * To avoid joining joined threads (which is not allowed by Bionic),
2427 * make sure we both successfully join and clear the tid to skip a
2428 * later join attempt during fixture teardown. Any remaining threads
2429 * will be directly killed during teardown.
2431 #define PTHREAD_JOIN(tid, status) \
2433 int _rc = pthread_join(tid, status); \
2435 TH_LOG("pthread_join of tid %u failed: %d\n", \
2436 (unsigned int)tid, _rc); \
2443 struct sock_fprog root_prog
, apply_prog
;
2444 struct tsync_sibling sibling
[TSYNC_SIBLINGS
];
2446 pthread_cond_t cond
;
2447 pthread_mutex_t mutex
;
2451 FIXTURE_SETUP(TSYNC
)
2453 struct sock_filter root_filter
[] = {
2454 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2456 struct sock_filter apply_filter
[] = {
2457 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2458 offsetof(struct seccomp_data
, nr
)),
2459 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 0, 1),
2460 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2461 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2464 memset(&self
->root_prog
, 0, sizeof(self
->root_prog
));
2465 memset(&self
->apply_prog
, 0, sizeof(self
->apply_prog
));
2466 memset(&self
->sibling
, 0, sizeof(self
->sibling
));
2467 self
->root_prog
.filter
= malloc(sizeof(root_filter
));
2468 ASSERT_NE(NULL
, self
->root_prog
.filter
);
2469 memcpy(self
->root_prog
.filter
, &root_filter
, sizeof(root_filter
));
2470 self
->root_prog
.len
= (unsigned short)ARRAY_SIZE(root_filter
);
2472 self
->apply_prog
.filter
= malloc(sizeof(apply_filter
));
2473 ASSERT_NE(NULL
, self
->apply_prog
.filter
);
2474 memcpy(self
->apply_prog
.filter
, &apply_filter
, sizeof(apply_filter
));
2475 self
->apply_prog
.len
= (unsigned short)ARRAY_SIZE(apply_filter
);
2477 self
->sibling_count
= 0;
2478 pthread_mutex_init(&self
->mutex
, NULL
);
2479 pthread_cond_init(&self
->cond
, NULL
);
2480 sem_init(&self
->started
, 0, 0);
2481 self
->sibling
[0].tid
= 0;
2482 self
->sibling
[0].cond
= &self
->cond
;
2483 self
->sibling
[0].started
= &self
->started
;
2484 self
->sibling
[0].mutex
= &self
->mutex
;
2485 self
->sibling
[0].diverge
= 0;
2486 self
->sibling
[0].num_waits
= 1;
2487 self
->sibling
[0].prog
= &self
->root_prog
;
2488 self
->sibling
[0].metadata
= _metadata
;
2489 self
->sibling
[1].tid
= 0;
2490 self
->sibling
[1].cond
= &self
->cond
;
2491 self
->sibling
[1].started
= &self
->started
;
2492 self
->sibling
[1].mutex
= &self
->mutex
;
2493 self
->sibling
[1].diverge
= 0;
2494 self
->sibling
[1].prog
= &self
->root_prog
;
2495 self
->sibling
[1].num_waits
= 1;
2496 self
->sibling
[1].metadata
= _metadata
;
2499 FIXTURE_TEARDOWN(TSYNC
)
2503 if (self
->root_prog
.filter
)
2504 free(self
->root_prog
.filter
);
2505 if (self
->apply_prog
.filter
)
2506 free(self
->apply_prog
.filter
);
2508 for ( ; sib
< self
->sibling_count
; ++sib
) {
2509 struct tsync_sibling
*s
= &self
->sibling
[sib
];
2514 * If a thread is still running, it may be stuck, so hit
2515 * it over the head really hard.
2517 pthread_kill(s
->tid
, 9);
2519 pthread_mutex_destroy(&self
->mutex
);
2520 pthread_cond_destroy(&self
->cond
);
2521 sem_destroy(&self
->started
);
2524 void *tsync_sibling(void *data
)
2527 struct tsync_sibling
*me
= data
;
2529 me
->system_tid
= syscall(__NR_gettid
);
2531 pthread_mutex_lock(me
->mutex
);
2533 /* Just re-apply the root prog to fork the tree */
2534 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
,
2537 sem_post(me
->started
);
2538 /* Return outside of started so parent notices failures. */
2540 pthread_mutex_unlock(me
->mutex
);
2541 return (void *)SIBLING_EXIT_FAILURE
;
2544 pthread_cond_wait(me
->cond
, me
->mutex
);
2545 me
->num_waits
= me
->num_waits
- 1;
2546 } while (me
->num_waits
);
2547 pthread_mutex_unlock(me
->mutex
);
2549 ret
= prctl(PR_GET_NO_NEW_PRIVS
, 0, 0, 0, 0);
2551 return (void *)SIBLING_EXIT_NEWPRIVS
;
2553 return (void *)SIBLING_EXIT_UNKILLED
;
2556 void tsync_start_sibling(struct tsync_sibling
*sibling
)
2558 pthread_create(&sibling
->tid
, NULL
, tsync_sibling
, (void *)sibling
);
2561 TEST_F(TSYNC
, siblings_fail_prctl
)
2565 struct sock_filter filter
[] = {
2566 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2567 offsetof(struct seccomp_data
, nr
)),
2568 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_prctl
, 0, 1),
2569 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ERRNO
| EINVAL
),
2570 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2572 struct sock_fprog prog
= {
2573 .len
= (unsigned short)ARRAY_SIZE(filter
),
2577 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2578 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2581 /* Check prctl failure detection by requesting sib 0 diverge. */
2582 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
);
2583 ASSERT_NE(ENOSYS
, errno
) {
2584 TH_LOG("Kernel does not support seccomp syscall!");
2587 TH_LOG("setting filter failed");
2590 self
->sibling
[0].diverge
= 1;
2591 tsync_start_sibling(&self
->sibling
[0]);
2592 tsync_start_sibling(&self
->sibling
[1]);
2594 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2595 sem_wait(&self
->started
);
2596 self
->sibling_count
++;
2599 /* Signal the threads to clean up*/
2600 pthread_mutex_lock(&self
->mutex
);
2601 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2602 TH_LOG("cond broadcast non-zero");
2604 pthread_mutex_unlock(&self
->mutex
);
2606 /* Ensure diverging sibling failed to call prctl. */
2607 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2608 EXPECT_EQ(SIBLING_EXIT_FAILURE
, (long)status
);
2609 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2610 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2613 TEST_F(TSYNC
, two_siblings_with_ancestor
)
2618 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2619 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2622 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2623 ASSERT_NE(ENOSYS
, errno
) {
2624 TH_LOG("Kernel does not support seccomp syscall!");
2627 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2629 tsync_start_sibling(&self
->sibling
[0]);
2630 tsync_start_sibling(&self
->sibling
[1]);
2632 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2633 sem_wait(&self
->started
);
2634 self
->sibling_count
++;
2637 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2640 TH_LOG("Could install filter on all threads!");
2642 /* Tell the siblings to test the policy */
2643 pthread_mutex_lock(&self
->mutex
);
2644 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2645 TH_LOG("cond broadcast non-zero");
2647 pthread_mutex_unlock(&self
->mutex
);
2648 /* Ensure they are both killed and don't exit cleanly. */
2649 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2650 EXPECT_EQ(0x0, (long)status
);
2651 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2652 EXPECT_EQ(0x0, (long)status
);
2655 TEST_F(TSYNC
, two_sibling_want_nnp
)
2659 /* start siblings before any prctl() operations */
2660 tsync_start_sibling(&self
->sibling
[0]);
2661 tsync_start_sibling(&self
->sibling
[1]);
2662 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2663 sem_wait(&self
->started
);
2664 self
->sibling_count
++;
2667 /* Tell the siblings to test no policy */
2668 pthread_mutex_lock(&self
->mutex
);
2669 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2670 TH_LOG("cond broadcast non-zero");
2672 pthread_mutex_unlock(&self
->mutex
);
2674 /* Ensure they are both upset about lacking nnp. */
2675 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2676 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2677 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2678 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS
, (long)status
);
2681 TEST_F(TSYNC
, two_siblings_with_no_filter
)
2686 /* start siblings before any prctl() operations */
2687 tsync_start_sibling(&self
->sibling
[0]);
2688 tsync_start_sibling(&self
->sibling
[1]);
2689 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2690 sem_wait(&self
->started
);
2691 self
->sibling_count
++;
2694 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2695 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2698 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2700 ASSERT_NE(ENOSYS
, errno
) {
2701 TH_LOG("Kernel does not support seccomp syscall!");
2704 TH_LOG("Could install filter on all threads!");
2707 /* Tell the siblings to test the policy */
2708 pthread_mutex_lock(&self
->mutex
);
2709 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2710 TH_LOG("cond broadcast non-zero");
2712 pthread_mutex_unlock(&self
->mutex
);
2714 /* Ensure they are both killed and don't exit cleanly. */
2715 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2716 EXPECT_EQ(0x0, (long)status
);
2717 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2718 EXPECT_EQ(0x0, (long)status
);
2721 TEST_F(TSYNC
, two_siblings_with_one_divergence
)
2726 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2727 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2730 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2731 ASSERT_NE(ENOSYS
, errno
) {
2732 TH_LOG("Kernel does not support seccomp syscall!");
2735 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2737 self
->sibling
[0].diverge
= 1;
2738 tsync_start_sibling(&self
->sibling
[0]);
2739 tsync_start_sibling(&self
->sibling
[1]);
2741 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2742 sem_wait(&self
->started
);
2743 self
->sibling_count
++;
2746 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2748 ASSERT_EQ(self
->sibling
[0].system_tid
, ret
) {
2749 TH_LOG("Did not fail on diverged sibling.");
2752 /* Wake the threads */
2753 pthread_mutex_lock(&self
->mutex
);
2754 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2755 TH_LOG("cond broadcast non-zero");
2757 pthread_mutex_unlock(&self
->mutex
);
2759 /* Ensure they are both unkilled. */
2760 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2761 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2762 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2763 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2766 TEST_F(TSYNC
, two_siblings_with_one_divergence_no_tid_in_err
)
2771 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2772 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2775 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2776 ASSERT_NE(ENOSYS
, errno
) {
2777 TH_LOG("Kernel does not support seccomp syscall!");
2780 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2782 self
->sibling
[0].diverge
= 1;
2783 tsync_start_sibling(&self
->sibling
[0]);
2784 tsync_start_sibling(&self
->sibling
[1]);
2786 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2787 sem_wait(&self
->started
);
2788 self
->sibling_count
++;
2791 flags
= SECCOMP_FILTER_FLAG_TSYNC
| \
2792 SECCOMP_FILTER_FLAG_TSYNC_ESRCH
;
2793 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, flags
, &self
->apply_prog
);
2794 ASSERT_EQ(ESRCH
, errno
) {
2795 TH_LOG("Did not return ESRCH for diverged sibling.");
2797 ASSERT_EQ(-1, ret
) {
2798 TH_LOG("Did not fail on diverged sibling.");
2801 /* Wake the threads */
2802 pthread_mutex_lock(&self
->mutex
);
2803 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2804 TH_LOG("cond broadcast non-zero");
2806 pthread_mutex_unlock(&self
->mutex
);
2808 /* Ensure they are both unkilled. */
2809 PTHREAD_JOIN(self
->sibling
[0].tid
, &status
);
2810 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2811 PTHREAD_JOIN(self
->sibling
[1].tid
, &status
);
2812 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2815 TEST_F(TSYNC
, two_siblings_not_under_filter
)
2819 struct timespec delay
= { .tv_nsec
= 100000000 };
2821 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2822 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2826 * Sibling 0 will have its own seccomp policy
2827 * and Sibling 1 will not be under seccomp at
2828 * all. Sibling 1 will enter seccomp and 0
2829 * will cause failure.
2831 self
->sibling
[0].diverge
= 1;
2832 tsync_start_sibling(&self
->sibling
[0]);
2833 tsync_start_sibling(&self
->sibling
[1]);
2835 while (self
->sibling_count
< TSYNC_SIBLINGS
) {
2836 sem_wait(&self
->started
);
2837 self
->sibling_count
++;
2840 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &self
->root_prog
);
2841 ASSERT_NE(ENOSYS
, errno
) {
2842 TH_LOG("Kernel does not support seccomp syscall!");
2845 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2848 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2850 ASSERT_EQ(ret
, self
->sibling
[0].system_tid
) {
2851 TH_LOG("Did not fail on diverged sibling.");
2854 if (ret
== self
->sibling
[0].system_tid
)
2857 pthread_mutex_lock(&self
->mutex
);
2859 /* Increment the other siblings num_waits so we can clean up
2860 * the one we just saw.
2862 self
->sibling
[!sib
].num_waits
+= 1;
2864 /* Signal the thread to clean up*/
2865 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2866 TH_LOG("cond broadcast non-zero");
2868 pthread_mutex_unlock(&self
->mutex
);
2869 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2870 EXPECT_EQ(SIBLING_EXIT_UNKILLED
, (long)status
);
2871 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2872 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2873 nanosleep(&delay
, NULL
);
2874 /* Switch to the remaining sibling */
2877 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2880 TH_LOG("Expected the remaining sibling to sync");
2883 pthread_mutex_lock(&self
->mutex
);
2885 /* If remaining sibling didn't have a chance to wake up during
2886 * the first broadcast, manually reduce the num_waits now.
2888 if (self
->sibling
[sib
].num_waits
> 1)
2889 self
->sibling
[sib
].num_waits
= 1;
2890 ASSERT_EQ(0, pthread_cond_broadcast(&self
->cond
)) {
2891 TH_LOG("cond broadcast non-zero");
2893 pthread_mutex_unlock(&self
->mutex
);
2894 PTHREAD_JOIN(self
->sibling
[sib
].tid
, &status
);
2895 EXPECT_EQ(0, (long)status
);
2896 /* Poll for actual task death. pthread_join doesn't guarantee it. */
2897 while (!kill(self
->sibling
[sib
].system_tid
, 0))
2898 nanosleep(&delay
, NULL
);
2900 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
,
2902 ASSERT_EQ(0, ret
); /* just us chickens */
2905 /* Make sure restarted syscalls are seen directly as "restart_syscall". */
2906 TEST(syscall_restart
)
2913 siginfo_t info
= { };
2914 struct sock_filter filter
[] = {
2915 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
2916 offsetof(struct seccomp_data
, nr
)),
2918 #ifdef __NR_sigreturn
2919 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_sigreturn
, 7, 0),
2921 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_read
, 6, 0),
2922 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_exit
, 5, 0),
2923 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_rt_sigreturn
, 4, 0),
2924 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_nanosleep
, 5, 0),
2925 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_clock_nanosleep
, 4, 0),
2926 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_restart_syscall
, 4, 0),
2928 /* Allow __NR_write for easy logging. */
2929 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_write
, 0, 1),
2930 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
2931 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
2932 /* The nanosleep jump target. */
2933 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x100),
2934 /* The restart_syscall jump target. */
2935 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_TRACE
|0x200),
2937 struct sock_fprog prog
= {
2938 .len
= (unsigned short)ARRAY_SIZE(filter
),
2941 #if defined(__arm__)
2942 struct utsname utsbuf
;
2945 ASSERT_EQ(0, pipe(pipefd
));
2948 ASSERT_LE(0, child_pid
);
2949 if (child_pid
== 0) {
2950 /* Child uses EXPECT not ASSERT to deliver status correctly. */
2952 struct timespec timeout
= { };
2954 /* Attach parent as tracer and stop. */
2955 EXPECT_EQ(0, ptrace(PTRACE_TRACEME
));
2956 EXPECT_EQ(0, raise(SIGSTOP
));
2958 EXPECT_EQ(0, close(pipefd
[1]));
2960 EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
2961 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2964 ret
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
, 0, 0);
2966 TH_LOG("Failed to install filter!");
2969 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2970 TH_LOG("Failed to read() sync from parent");
2972 EXPECT_EQ('.', buf
) {
2973 TH_LOG("Failed to get sync data from read()");
2976 /* Start nanosleep to be interrupted. */
2979 EXPECT_EQ(0, nanosleep(&timeout
, NULL
)) {
2980 TH_LOG("Call to nanosleep() failed (errno %d)", errno
);
2983 /* Read final sync from parent. */
2984 EXPECT_EQ(1, read(pipefd
[0], &buf
, 1)) {
2985 TH_LOG("Failed final read() from parent");
2987 EXPECT_EQ('!', buf
) {
2988 TH_LOG("Failed to get final data from read()");
2991 /* Directly report the status of our test harness results. */
2992 syscall(__NR_exit
, _metadata
->passed
? EXIT_SUCCESS
2995 EXPECT_EQ(0, close(pipefd
[0]));
2997 /* Attach to child, setup options, and release. */
2998 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
2999 ASSERT_EQ(true, WIFSTOPPED(status
));
3000 ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS
, child_pid
, NULL
,
3001 PTRACE_O_TRACESECCOMP
));
3002 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3003 ASSERT_EQ(1, write(pipefd
[1], ".", 1));
3005 /* Wait for nanosleep() to start. */
3006 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3007 ASSERT_EQ(true, WIFSTOPPED(status
));
3008 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
3009 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
3010 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
3011 ASSERT_EQ(0x100, msg
);
3012 ret
= get_syscall(_metadata
, child_pid
);
3013 EXPECT_TRUE(ret
== __NR_nanosleep
|| ret
== __NR_clock_nanosleep
);
3015 /* Might as well check siginfo for sanity while we're here. */
3016 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
3017 ASSERT_EQ(SIGTRAP
, info
.si_signo
);
3018 ASSERT_EQ(SIGTRAP
| (PTRACE_EVENT_SECCOMP
<< 8), info
.si_code
);
3019 EXPECT_EQ(0, info
.si_errno
);
3020 EXPECT_EQ(getuid(), info
.si_uid
);
3021 /* Verify signal delivery came from child (seccomp-triggered). */
3022 EXPECT_EQ(child_pid
, info
.si_pid
);
3024 /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */
3025 ASSERT_EQ(0, kill(child_pid
, SIGSTOP
));
3026 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3027 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3028 ASSERT_EQ(true, WIFSTOPPED(status
));
3029 ASSERT_EQ(SIGSTOP
, WSTOPSIG(status
));
3030 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO
, child_pid
, NULL
, &info
));
3032 * There is no siginfo on SIGSTOP any more, so we can't verify
3033 * signal delivery came from parent now (getpid() == info.si_pid).
3034 * https://lkml.kernel.org/r/CAGXu5jJaZAOzP1qFz66tYrtbuywqb+UN2SOA1VLHpCCOiYvYeg@mail.gmail.com
3035 * At least verify the SIGSTOP via PTRACE_GETSIGINFO.
3037 EXPECT_EQ(SIGSTOP
, info
.si_signo
);
3039 /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */
3040 ASSERT_EQ(0, kill(child_pid
, SIGCONT
));
3041 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3042 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3043 ASSERT_EQ(true, WIFSTOPPED(status
));
3044 ASSERT_EQ(SIGCONT
, WSTOPSIG(status
));
3045 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3047 /* Wait for restart_syscall() to start. */
3048 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3049 ASSERT_EQ(true, WIFSTOPPED(status
));
3050 ASSERT_EQ(SIGTRAP
, WSTOPSIG(status
));
3051 ASSERT_EQ(PTRACE_EVENT_SECCOMP
, (status
>> 16));
3052 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG
, child_pid
, NULL
, &msg
));
3054 ASSERT_EQ(0x200, msg
);
3055 ret
= get_syscall(_metadata
, child_pid
);
3056 #if defined(__arm__)
3059 * - native ARM registers do NOT expose true syscall.
3060 * - compat ARM registers on ARM64 DO expose true syscall.
3062 ASSERT_EQ(0, uname(&utsbuf
));
3063 if (strncmp(utsbuf
.machine
, "arm", 3) == 0) {
3064 EXPECT_EQ(__NR_nanosleep
, ret
);
3068 EXPECT_EQ(__NR_restart_syscall
, ret
);
3071 /* Write again to end test. */
3072 ASSERT_EQ(0, ptrace(PTRACE_CONT
, child_pid
, NULL
, 0));
3073 ASSERT_EQ(1, write(pipefd
[1], "!", 1));
3074 EXPECT_EQ(0, close(pipefd
[1]));
3076 ASSERT_EQ(child_pid
, waitpid(child_pid
, &status
, 0));
3077 if (WIFSIGNALED(status
) || WEXITSTATUS(status
))
3078 _metadata
->passed
= 0;
3081 TEST_SIGNAL(filter_flag_log
, SIGSYS
)
3083 struct sock_filter allow_filter
[] = {
3084 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3086 struct sock_filter kill_filter
[] = {
3087 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
3088 offsetof(struct seccomp_data
, nr
)),
3089 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, __NR_getpid
, 0, 1),
3090 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_KILL
),
3091 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3093 struct sock_fprog allow_prog
= {
3094 .len
= (unsigned short)ARRAY_SIZE(allow_filter
),
3095 .filter
= allow_filter
,
3097 struct sock_fprog kill_prog
= {
3098 .len
= (unsigned short)ARRAY_SIZE(kill_filter
),
3099 .filter
= kill_filter
,
3102 pid_t parent
= getppid();
3104 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3107 /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */
3108 ret
= seccomp(SECCOMP_SET_MODE_STRICT
, SECCOMP_FILTER_FLAG_LOG
,
3110 ASSERT_NE(ENOSYS
, errno
) {
3111 TH_LOG("Kernel does not support seccomp syscall!");
3114 TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!");
3116 EXPECT_EQ(EINVAL
, errno
) {
3117 TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!");
3120 /* Verify that a simple, permissive filter can be added with no flags */
3121 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, 0, &allow_prog
);
3124 /* See if the same filter can be added with the FILTER_FLAG_LOG flag */
3125 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
3127 ASSERT_NE(EINVAL
, errno
) {
3128 TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!");
3132 /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */
3133 ret
= seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_LOG
,
3137 EXPECT_EQ(parent
, syscall(__NR_getppid
));
3138 /* getpid() should never return. */
3139 EXPECT_EQ(0, syscall(__NR_getpid
));
3142 TEST(get_action_avail
)
3144 __u32 actions
[] = { SECCOMP_RET_KILL_THREAD
, SECCOMP_RET_TRAP
,
3145 SECCOMP_RET_ERRNO
, SECCOMP_RET_TRACE
,
3146 SECCOMP_RET_LOG
, SECCOMP_RET_ALLOW
};
3147 __u32 unknown_action
= 0x10000000U
;
3151 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[0]);
3152 ASSERT_NE(ENOSYS
, errno
) {
3153 TH_LOG("Kernel does not support seccomp syscall!");
3155 ASSERT_NE(EINVAL
, errno
) {
3156 TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!");
3160 for (i
= 0; i
< ARRAY_SIZE(actions
); i
++) {
3161 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &actions
[i
]);
3163 TH_LOG("Expected action (0x%X) not available!",
3168 /* Check that an unknown action is handled properly (EOPNOTSUPP) */
3169 ret
= seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &unknown_action
);
3171 EXPECT_EQ(errno
, EOPNOTSUPP
);
3179 struct seccomp_metadata md
;
3182 /* Only real root can get metadata. */
3184 SKIP(return, "get_metadata requires real root");
3188 ASSERT_EQ(0, pipe(pipefd
));
3193 struct sock_filter filter
[] = {
3194 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3196 struct sock_fprog prog
= {
3197 .len
= (unsigned short)ARRAY_SIZE(filter
),
3201 /* one with log, one without */
3202 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
,
3203 SECCOMP_FILTER_FLAG_LOG
, &prog
));
3204 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
));
3206 EXPECT_EQ(0, close(pipefd
[0]));
3207 ASSERT_EQ(1, write(pipefd
[1], "1", 1));
3208 ASSERT_EQ(0, close(pipefd
[1]));
3214 ASSERT_EQ(0, close(pipefd
[1]));
3215 ASSERT_EQ(1, read(pipefd
[0], &buf
, 1));
3217 ASSERT_EQ(0, ptrace(PTRACE_ATTACH
, pid
));
3218 ASSERT_EQ(pid
, waitpid(pid
, NULL
, 0));
3220 /* Past here must not use ASSERT or child process is never killed. */
3224 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
3225 EXPECT_EQ(sizeof(md
), ret
) {
3226 if (errno
== EINVAL
)
3227 SKIP(goto skip
, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)");
3230 EXPECT_EQ(md
.flags
, SECCOMP_FILTER_FLAG_LOG
);
3231 EXPECT_EQ(md
.filter_off
, 0);
3234 ret
= ptrace(PTRACE_SECCOMP_GET_METADATA
, pid
, sizeof(md
), &md
);
3235 EXPECT_EQ(sizeof(md
), ret
);
3236 EXPECT_EQ(md
.flags
, 0);
3237 EXPECT_EQ(md
.filter_off
, 1);
3240 ASSERT_EQ(0, kill(pid
, SIGKILL
));
3243 static int user_notif_syscall(int nr
, unsigned int flags
)
3245 struct sock_filter filter
[] = {
3246 BPF_STMT(BPF_LD
|BPF_W
|BPF_ABS
,
3247 offsetof(struct seccomp_data
, nr
)),
3248 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, nr
, 0, 1),
3249 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_USER_NOTIF
),
3250 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3253 struct sock_fprog prog
= {
3254 .len
= (unsigned short)ARRAY_SIZE(filter
),
3258 return seccomp(SECCOMP_SET_MODE_FILTER
, flags
, &prog
);
3261 #define USER_NOTIF_MAGIC INT_MAX
3262 TEST(user_notification_basic
)
3266 int status
, listener
;
3267 struct seccomp_notif req
= {};
3268 struct seccomp_notif_resp resp
= {};
3269 struct pollfd pollfd
;
3271 struct sock_filter filter
[] = {
3272 BPF_STMT(BPF_RET
|BPF_K
, SECCOMP_RET_ALLOW
),
3274 struct sock_fprog prog
= {
3275 .len
= (unsigned short)ARRAY_SIZE(filter
),
3279 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3281 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3287 /* Check that we get -ENOSYS with no listener attached */
3289 if (user_notif_syscall(__NR_getppid
, 0) < 0)
3291 ret
= syscall(__NR_getppid
);
3292 exit(ret
>= 0 || errno
!= ENOSYS
);
3295 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3296 EXPECT_EQ(true, WIFEXITED(status
));
3297 EXPECT_EQ(0, WEXITSTATUS(status
));
3299 /* Add some no-op filters for grins. */
3300 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3301 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3302 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3303 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER
, 0, &prog
), 0);
3305 /* Check that the basic notification machinery works */
3306 listener
= user_notif_syscall(__NR_getppid
,
3307 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3308 ASSERT_GE(listener
, 0);
3310 /* Installing a second listener in the chain should EBUSY */
3311 EXPECT_EQ(user_notif_syscall(__NR_getppid
,
3312 SECCOMP_FILTER_FLAG_NEW_LISTENER
),
3314 EXPECT_EQ(errno
, EBUSY
);
3320 ret
= syscall(__NR_getppid
);
3321 exit(ret
!= USER_NOTIF_MAGIC
);
3324 pollfd
.fd
= listener
;
3325 pollfd
.events
= POLLIN
| POLLOUT
;
3327 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3328 EXPECT_EQ(pollfd
.revents
, POLLIN
);
3330 /* Test that we can't pass garbage to the kernel. */
3331 memset(&req
, 0, sizeof(req
));
3334 ret
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
);
3336 EXPECT_EQ(EINVAL
, errno
);
3340 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3343 pollfd
.fd
= listener
;
3344 pollfd
.events
= POLLIN
| POLLOUT
;
3346 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3347 EXPECT_EQ(pollfd
.revents
, POLLOUT
);
3349 EXPECT_EQ(req
.data
.nr
, __NR_getppid
);
3353 resp
.val
= USER_NOTIF_MAGIC
;
3355 /* check that we make sure flags == 0 */
3357 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3358 EXPECT_EQ(errno
, EINVAL
);
3361 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3363 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3364 EXPECT_EQ(true, WIFEXITED(status
));
3365 EXPECT_EQ(0, WEXITSTATUS(status
));
3368 TEST(user_notification_with_tsync
)
3373 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3375 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3378 /* these were exclusive */
3379 flags
= SECCOMP_FILTER_FLAG_NEW_LISTENER
|
3380 SECCOMP_FILTER_FLAG_TSYNC
;
3381 ASSERT_EQ(-1, user_notif_syscall(__NR_getppid
, flags
));
3382 ASSERT_EQ(EINVAL
, errno
);
3384 /* but now they're not */
3385 flags
|= SECCOMP_FILTER_FLAG_TSYNC_ESRCH
;
3386 ret
= user_notif_syscall(__NR_getppid
, flags
);
3391 TEST(user_notification_kill_in_middle
)
3396 struct seccomp_notif req
= {};
3397 struct seccomp_notif_resp resp
= {};
3399 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3401 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3404 listener
= user_notif_syscall(__NR_getppid
,
3405 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3406 ASSERT_GE(listener
, 0);
3409 * Check that nothing bad happens when we kill the task in the middle
3416 ret
= syscall(__NR_getppid
);
3417 exit(ret
!= USER_NOTIF_MAGIC
);
3420 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3421 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ID_VALID
, &req
.id
), 0);
3423 EXPECT_EQ(kill(pid
, SIGKILL
), 0);
3424 EXPECT_EQ(waitpid(pid
, NULL
, 0), pid
);
3426 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ID_VALID
, &req
.id
), -1);
3429 ret
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
);
3431 EXPECT_EQ(errno
, ENOENT
);
3434 static int handled
= -1;
3436 static void signal_handler(int signal
)
3438 if (write(handled
, "c", 1) != 1)
3439 perror("write from signal");
3442 TEST(user_notification_signal
)
3446 int status
, listener
, sk_pair
[2];
3447 struct seccomp_notif req
= {};
3448 struct seccomp_notif_resp resp
= {};
3451 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3453 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3456 ASSERT_EQ(socketpair(PF_LOCAL
, SOCK_SEQPACKET
, 0, sk_pair
), 0);
3458 listener
= user_notif_syscall(__NR_gettid
,
3459 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3460 ASSERT_GE(listener
, 0);
3467 handled
= sk_pair
[1];
3468 if (signal(SIGUSR1
, signal_handler
) == SIG_ERR
) {
3473 * ERESTARTSYS behavior is a bit hard to test, because we need
3474 * to rely on a signal that has not yet been handled. Let's at
3475 * least check that the error code gets propagated through, and
3476 * hope that it doesn't break when there is actually a signal :)
3478 ret
= syscall(__NR_gettid
);
3479 exit(!(ret
== -1 && errno
== 512));
3484 memset(&req
, 0, sizeof(req
));
3485 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3487 EXPECT_EQ(kill(pid
, SIGUSR1
), 0);
3490 * Make sure the signal really is delivered, which means we're not
3491 * stuck in the user notification code any more and the notification
3494 EXPECT_EQ(read(sk_pair
[0], &c
, 1), 1);
3497 resp
.error
= -EPERM
;
3500 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3501 EXPECT_EQ(errno
, ENOENT
);
3503 memset(&req
, 0, sizeof(req
));
3504 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3507 resp
.error
= -512; /* -ERESTARTSYS */
3510 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3512 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3513 EXPECT_EQ(true, WIFEXITED(status
));
3514 EXPECT_EQ(0, WEXITSTATUS(status
));
3517 TEST(user_notification_closed_listener
)
3521 int status
, listener
;
3523 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3525 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3528 listener
= user_notif_syscall(__NR_getppid
,
3529 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3530 ASSERT_GE(listener
, 0);
3533 * Check that we get an ENOSYS when the listener is closed.
3539 ret
= syscall(__NR_getppid
);
3540 exit(ret
!= -1 && errno
!= ENOSYS
);
3545 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3546 EXPECT_EQ(true, WIFEXITED(status
));
3547 EXPECT_EQ(0, WEXITSTATUS(status
));
3551 * Check that a pid in a child namespace still shows up as valid in ours.
3553 TEST(user_notification_child_pid_ns
)
3556 int status
, listener
;
3557 struct seccomp_notif req
= {};
3558 struct seccomp_notif_resp resp
= {};
3560 ASSERT_EQ(unshare(CLONE_NEWUSER
| CLONE_NEWPID
), 0) {
3561 if (errno
== EINVAL
)
3562 SKIP(return, "kernel missing CLONE_NEWUSER support");
3565 listener
= user_notif_syscall(__NR_getppid
,
3566 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3567 ASSERT_GE(listener
, 0);
3573 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3575 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3576 EXPECT_EQ(req
.pid
, pid
);
3580 resp
.val
= USER_NOTIF_MAGIC
;
3582 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3584 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3585 EXPECT_EQ(true, WIFEXITED(status
));
3586 EXPECT_EQ(0, WEXITSTATUS(status
));
3591 * Check that a pid in a sibling (i.e. unrelated) namespace shows up as 0, i.e.
3594 TEST(user_notification_sibling_pid_ns
)
3597 int status
, listener
;
3598 struct seccomp_notif req
= {};
3599 struct seccomp_notif_resp resp
= {};
3601 ASSERT_EQ(prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0), 0) {
3602 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3605 listener
= user_notif_syscall(__NR_getppid
,
3606 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3607 ASSERT_GE(listener
, 0);
3613 ASSERT_EQ(unshare(CLONE_NEWPID
), 0);
3619 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3621 EXPECT_EQ(waitpid(pid2
, &status
, 0), pid2
);
3622 EXPECT_EQ(true, WIFEXITED(status
));
3623 EXPECT_EQ(0, WEXITSTATUS(status
));
3624 exit(WEXITSTATUS(status
));
3627 /* Create the sibling ns, and sibling in it. */
3628 ASSERT_EQ(unshare(CLONE_NEWPID
), 0) {
3630 SKIP(return, "CLONE_NEWPID requires CAP_SYS_ADMIN");
3632 ASSERT_EQ(errno
, 0);
3638 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3640 * The pid should be 0, i.e. the task is in some namespace that
3643 EXPECT_EQ(req
.pid
, 0);
3647 resp
.val
= USER_NOTIF_MAGIC
;
3649 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3655 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3656 EXPECT_EQ(true, WIFEXITED(status
));
3657 EXPECT_EQ(0, WEXITSTATUS(status
));
3659 EXPECT_EQ(waitpid(pid2
, &status
, 0), pid2
);
3660 EXPECT_EQ(true, WIFEXITED(status
));
3661 EXPECT_EQ(0, WEXITSTATUS(status
));
3664 TEST(user_notification_fault_recv
)
3667 int status
, listener
;
3668 struct seccomp_notif req
= {};
3669 struct seccomp_notif_resp resp
= {};
3671 ASSERT_EQ(unshare(CLONE_NEWUSER
), 0);
3673 listener
= user_notif_syscall(__NR_getppid
,
3674 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3675 ASSERT_GE(listener
, 0);
3681 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3683 /* Do a bad recv() */
3684 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, NULL
), -1);
3685 EXPECT_EQ(errno
, EFAULT
);
3687 /* We should still be able to receive this notification, though. */
3688 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3689 EXPECT_EQ(req
.pid
, pid
);
3693 resp
.val
= USER_NOTIF_MAGIC
;
3695 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
3697 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3698 EXPECT_EQ(true, WIFEXITED(status
));
3699 EXPECT_EQ(0, WEXITSTATUS(status
));
3702 TEST(seccomp_get_notif_sizes
)
3704 struct seccomp_notif_sizes sizes
;
3706 ASSERT_EQ(seccomp(SECCOMP_GET_NOTIF_SIZES
, 0, &sizes
), 0);
3707 EXPECT_EQ(sizes
.seccomp_notif
, sizeof(struct seccomp_notif
));
3708 EXPECT_EQ(sizes
.seccomp_notif_resp
, sizeof(struct seccomp_notif_resp
));
3711 TEST(user_notification_continue
)
3715 int status
, listener
;
3716 struct seccomp_notif req
= {};
3717 struct seccomp_notif_resp resp
= {};
3718 struct pollfd pollfd
;
3720 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3722 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3725 listener
= user_notif_syscall(__NR_dup
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3726 ASSERT_GE(listener
, 0);
3732 int dup_fd
, pipe_fds
[2];
3735 ASSERT_GE(pipe(pipe_fds
), 0);
3737 dup_fd
= dup(pipe_fds
[0]);
3738 ASSERT_GE(dup_fd
, 0);
3739 EXPECT_NE(pipe_fds
[0], dup_fd
);
3742 ASSERT_EQ(filecmp(self
, self
, pipe_fds
[0], dup_fd
), 0);
3746 pollfd
.fd
= listener
;
3747 pollfd
.events
= POLLIN
| POLLOUT
;
3749 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3750 EXPECT_EQ(pollfd
.revents
, POLLIN
);
3752 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3754 pollfd
.fd
= listener
;
3755 pollfd
.events
= POLLIN
| POLLOUT
;
3757 EXPECT_GT(poll(&pollfd
, 1, -1), 0);
3758 EXPECT_EQ(pollfd
.revents
, POLLOUT
);
3760 EXPECT_EQ(req
.data
.nr
, __NR_dup
);
3763 resp
.flags
= SECCOMP_USER_NOTIF_FLAG_CONTINUE
;
3766 * Verify that setting SECCOMP_USER_NOTIF_FLAG_CONTINUE enforces other
3770 resp
.val
= USER_NOTIF_MAGIC
;
3771 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3772 EXPECT_EQ(errno
, EINVAL
);
3774 resp
.error
= USER_NOTIF_MAGIC
;
3776 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), -1);
3777 EXPECT_EQ(errno
, EINVAL
);
3781 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0) {
3782 if (errno
== EINVAL
)
3783 SKIP(goto skip
, "Kernel does not support SECCOMP_USER_NOTIF_FLAG_CONTINUE");
3787 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3788 EXPECT_EQ(true, WIFEXITED(status
));
3789 EXPECT_EQ(0, WEXITSTATUS(status
)) {
3790 if (WEXITSTATUS(status
) == 2) {
3791 SKIP(return, "Kernel does not support kcmp() syscall");
3797 TEST(user_notification_filter_empty
)
3802 struct pollfd pollfd
;
3803 struct clone_args args
= {
3804 .flags
= CLONE_FILES
,
3805 .exit_signal
= SIGCHLD
,
3808 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3810 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3813 pid
= sys_clone3(&args
, sizeof(args
));
3819 listener
= user_notif_syscall(__NR_mknodat
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3821 _exit(EXIT_FAILURE
);
3823 if (dup2(listener
, 200) != 200)
3824 _exit(EXIT_FAILURE
);
3828 _exit(EXIT_SUCCESS
);
3831 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3832 EXPECT_EQ(true, WIFEXITED(status
));
3833 EXPECT_EQ(0, WEXITSTATUS(status
));
3836 * The seccomp filter has become unused so we should be notified once
3837 * the kernel gets around to cleaning up task struct.
3840 pollfd
.events
= POLLHUP
;
3842 EXPECT_GT(poll(&pollfd
, 1, 2000), 0);
3843 EXPECT_GT((pollfd
.revents
& POLLHUP
) ?: 0, 0);
3846 static void *do_thread(void *data
)
3851 TEST(user_notification_filter_empty_threaded
)
3856 struct pollfd pollfd
;
3857 struct clone_args args
= {
3858 .flags
= CLONE_FILES
,
3859 .exit_signal
= SIGCHLD
,
3862 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3864 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3867 pid
= sys_clone3(&args
, sizeof(args
));
3872 int listener
, status
;
3875 listener
= user_notif_syscall(__NR_dup
, SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3877 _exit(EXIT_FAILURE
);
3879 if (dup2(listener
, 200) != 200)
3880 _exit(EXIT_FAILURE
);
3886 _exit(EXIT_FAILURE
);
3889 _exit(EXIT_SUCCESS
);
3893 _exit(EXIT_FAILURE
);
3896 _exit(EXIT_SUCCESS
);
3898 if (pthread_create(&thread
, NULL
, do_thread
, NULL
) ||
3899 pthread_join(thread
, NULL
))
3900 _exit(EXIT_FAILURE
);
3902 if (pthread_create(&thread
, NULL
, do_thread
, NULL
) ||
3903 pthread_join(thread
, NULL
))
3904 _exit(EXIT_FAILURE
);
3906 if (waitpid(pid1
, &status
, 0) != pid1
|| !WIFEXITED(status
) ||
3907 WEXITSTATUS(status
))
3908 _exit(EXIT_FAILURE
);
3910 if (waitpid(pid2
, &status
, 0) != pid2
|| !WIFEXITED(status
) ||
3911 WEXITSTATUS(status
))
3912 _exit(EXIT_FAILURE
);
3917 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
3918 EXPECT_EQ(true, WIFEXITED(status
));
3919 EXPECT_EQ(0, WEXITSTATUS(status
));
3922 * The seccomp filter has become unused so we should be notified once
3923 * the kernel gets around to cleaning up task struct.
3926 pollfd
.events
= POLLHUP
;
3928 EXPECT_GT(poll(&pollfd
, 1, 2000), 0);
3929 EXPECT_GT((pollfd
.revents
& POLLHUP
) ?: 0, 0);
3932 TEST(user_notification_addfd
)
3936 int status
, listener
, memfd
, fd
;
3937 struct seccomp_notif_addfd addfd
= {};
3938 struct seccomp_notif_addfd_small small
= {};
3939 struct seccomp_notif_addfd_big big
= {};
3940 struct seccomp_notif req
= {};
3941 struct seccomp_notif_resp resp
= {};
3943 struct timespec delay
= { .tv_nsec
= 100000000 };
3945 memfd
= memfd_create("test", 0);
3946 ASSERT_GE(memfd
, 0);
3948 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
3950 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
3953 /* Check that the basic notification machinery works */
3954 listener
= user_notif_syscall(__NR_getppid
,
3955 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
3956 ASSERT_GE(listener
, 0);
3962 if (syscall(__NR_getppid
) != USER_NOTIF_MAGIC
)
3964 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
3967 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
3969 addfd
.srcfd
= memfd
;
3974 /* Verify bad newfd_flags cannot be set */
3975 addfd
.newfd_flags
= ~O_CLOEXEC
;
3976 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
3977 EXPECT_EQ(errno
, EINVAL
);
3978 addfd
.newfd_flags
= O_CLOEXEC
;
3980 /* Verify bad flags cannot be set */
3982 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
3983 EXPECT_EQ(errno
, EINVAL
);
3986 /* Verify that remote_fd cannot be set without setting flags */
3988 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
3989 EXPECT_EQ(errno
, EINVAL
);
3992 /* Verify small size cannot be set */
3993 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD_SMALL
, &small
), -1);
3994 EXPECT_EQ(errno
, EINVAL
);
3996 /* Verify we can't send bits filled in unknown buffer area */
3997 memset(&big
, 0xAA, sizeof(big
));
3999 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD_BIG
, &big
), -1);
4000 EXPECT_EQ(errno
, E2BIG
);
4003 /* Verify we can set an arbitrary remote fd */
4004 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
);
4006 * The child has fds 0(stdin), 1(stdout), 2(stderr), 3(memfd),
4007 * 4(listener), so the newly allocated fd should be 5.
4010 EXPECT_EQ(filecmp(getpid(), pid
, memfd
, fd
), 0);
4012 /* Verify we can set an arbitrary remote fd with large size */
4013 memset(&big
, 0x0, sizeof(big
));
4015 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD_BIG
, &big
);
4018 /* Verify we can set a specific remote fd */
4020 addfd
.flags
= SECCOMP_ADDFD_FLAG_SETFD
;
4021 fd
= ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
);
4023 EXPECT_EQ(filecmp(getpid(), pid
, memfd
, fd
), 0);
4025 /* Resume syscall */
4028 resp
.val
= USER_NOTIF_MAGIC
;
4029 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4032 * This sets the ID of the ADD FD to the last request plus 1. The
4033 * notification ID increments 1 per notification.
4035 addfd
.id
= req
.id
+ 1;
4037 /* This spins until the underlying notification is generated */
4038 while (ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
) != -1 &&
4039 errno
!= -EINPROGRESS
)
4040 nanosleep(&delay
, NULL
);
4042 memset(&req
, 0, sizeof(req
));
4043 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4044 ASSERT_EQ(addfd
.id
, req
.id
);
4048 resp
.val
= USER_NOTIF_MAGIC
;
4049 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4051 /* Wait for child to finish. */
4052 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4053 EXPECT_EQ(true, WIFEXITED(status
));
4054 EXPECT_EQ(0, WEXITSTATUS(status
));
4059 TEST(user_notification_addfd_rlimit
)
4063 int status
, listener
, memfd
;
4064 struct seccomp_notif_addfd addfd
= {};
4065 struct seccomp_notif req
= {};
4066 struct seccomp_notif_resp resp
= {};
4067 const struct rlimit lim
= {
4072 memfd
= memfd_create("test", 0);
4073 ASSERT_GE(memfd
, 0);
4075 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
4077 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
4080 /* Check that the basic notification machinery works */
4081 listener
= user_notif_syscall(__NR_getppid
,
4082 SECCOMP_FILTER_FLAG_NEW_LISTENER
);
4083 ASSERT_GE(listener
, 0);
4089 exit(syscall(__NR_getppid
) != USER_NOTIF_MAGIC
);
4092 ASSERT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_RECV
, &req
), 0);
4094 ASSERT_EQ(prlimit(pid
, RLIMIT_NOFILE
, &lim
, NULL
), 0);
4096 addfd
.srcfd
= memfd
;
4097 addfd
.newfd_flags
= O_CLOEXEC
;
4102 /* Should probably spot check /proc/sys/fs/file-nr */
4103 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4104 EXPECT_EQ(errno
, EMFILE
);
4107 addfd
.flags
= SECCOMP_ADDFD_FLAG_SETFD
;
4108 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_ADDFD
, &addfd
), -1);
4109 EXPECT_EQ(errno
, EBADF
);
4113 resp
.val
= USER_NOTIF_MAGIC
;
4115 EXPECT_EQ(ioctl(listener
, SECCOMP_IOCTL_NOTIF_SEND
, &resp
), 0);
4117 /* Wait for child to finish. */
4118 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
4119 EXPECT_EQ(true, WIFEXITED(status
));
4120 EXPECT_EQ(0, WEXITSTATUS(status
));
4127 * - expand NNP testing
4128 * - better arch-specific TRACE and TRAP handlers.
4129 * - endianness checking when appropriate
4130 * - 64-bit arg prodding
4131 * - arch value testing (x86 modes especially)
4132 * - verify that FILTER_FLAG_LOG filters generate log messages
4133 * - verify that RET_LOG generates log messages