1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <sys/eventfd.h>
8 #include <sys/personality.h>
10 #include <sys/syscall.h>
11 #include <sys/types.h>
14 #include "alloc-util.h"
18 #include "memory-util.h"
19 #include "missing_sched.h"
21 #include "nulstr-util.h"
22 #include "process-util.h"
23 #include "raw-clone.h"
25 #include "seccomp-util.h"
27 #include "string-util.h"
29 #include "tmpfile-util.h"
32 /* __NR_socket may be invalid due to libseccomp */
33 #if !defined(__NR_socket) || __NR_socket < 0 || defined(__i386__) || defined(__s390x__) || defined(__s390__)
34 /* On these archs, socket() is implemented via the socketcall() syscall multiplexer,
35 * and we can't restrict it hence via seccomp. */
36 # define SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN 1
38 # define SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN 0
41 static void test_seccomp_arch_to_string(void) {
45 log_info("/* %s */", __func__
);
47 a
= seccomp_arch_native();
49 name
= seccomp_arch_to_string(a
);
51 assert_se(seccomp_arch_from_string(name
, &b
) >= 0);
55 static void test_architecture_table(void) {
58 log_info("/* %s */", __func__
);
80 assert_se(seccomp_arch_from_string(n
, &c
) >= 0);
81 n2
= seccomp_arch_to_string(c
);
82 log_info("seccomp-arch: %s → 0x%"PRIx32
" → %s", n
, c
, n2
);
83 assert_se(streq_ptr(n
, n2
));
87 static void test_syscall_filter_set_find(void) {
88 log_info("/* %s */", __func__
);
90 assert_se(!syscall_filter_set_find(NULL
));
91 assert_se(!syscall_filter_set_find(""));
92 assert_se(!syscall_filter_set_find("quux"));
93 assert_se(!syscall_filter_set_find("@quux"));
95 assert_se(syscall_filter_set_find("@clock") == syscall_filter_sets
+ SYSCALL_FILTER_SET_CLOCK
);
96 assert_se(syscall_filter_set_find("@default") == syscall_filter_sets
+ SYSCALL_FILTER_SET_DEFAULT
);
97 assert_se(syscall_filter_set_find("@raw-io") == syscall_filter_sets
+ SYSCALL_FILTER_SET_RAW_IO
);
100 static void test_filter_sets(void) {
104 log_info("/* %s */", __func__
);
106 if (!is_seccomp_available()) {
107 log_notice("Seccomp not available, skipping %s", __func__
);
110 if (geteuid() != 0) {
111 log_notice("Not root, skipping %s", __func__
);
115 for (i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++) {
118 log_info("Testing %s", syscall_filter_sets
[i
].name
);
123 if (pid
== 0) { /* Child? */
126 /* If we look at the default set (or one that includes it), allow-list instead of deny-list */
127 if (IN_SET(i
, SYSCALL_FILTER_SET_DEFAULT
, SYSCALL_FILTER_SET_SYSTEM_SERVICE
))
128 r
= seccomp_load_syscall_filter_set(SCMP_ACT_ERRNO(EUCLEAN
), syscall_filter_sets
+ i
, SCMP_ACT_ALLOW
, true);
130 r
= seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW
, syscall_filter_sets
+ i
, SCMP_ACT_ERRNO(EUCLEAN
), true);
134 /* Test the sycall filter with one random system call */
135 fd
= eventfd(0, EFD_NONBLOCK
|EFD_CLOEXEC
);
136 if (IN_SET(i
, SYSCALL_FILTER_SET_IO_EVENT
, SYSCALL_FILTER_SET_DEFAULT
))
137 assert_se(fd
< 0 && errno
== EUCLEAN
);
146 assert_se(wait_for_terminate_and_check(syscall_filter_sets
[i
].name
, pid
, WAIT_LOG
) == EXIT_SUCCESS
);
150 static void test_filter_sets_ordered(void) {
153 log_info("/* %s */", __func__
);
155 /* Ensure "@default" always remains at the beginning of the list */
156 assert_se(SYSCALL_FILTER_SET_DEFAULT
== 0);
157 assert_se(streq(syscall_filter_sets
[0].name
, "@default"));
159 for (i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++) {
160 const char *k
, *p
= NULL
;
162 /* Make sure each group has a description */
163 assert_se(!isempty(syscall_filter_sets
[0].help
));
165 /* Make sure the groups are ordered alphabetically, except for the first entry */
166 assert_se(i
< 2 || strcmp(syscall_filter_sets
[i
-1].name
, syscall_filter_sets
[i
].name
) < 0);
168 NULSTR_FOREACH(k
, syscall_filter_sets
[i
].value
) {
170 /* Ensure each syscall list is in itself ordered, but groups before names */
172 (*p
== '@' && *k
!= '@') ||
173 (((*p
== '@' && *k
== '@') ||
174 (*p
!= '@' && *k
!= '@')) &&
182 static void test_restrict_namespace(void) {
187 if (!have_namespaces()) {
188 log_notice("Testing without namespaces, skipping %s", __func__
);
192 log_info("/* %s */", __func__
);
194 assert_se(namespace_flags_to_string(0, &s
) == 0 && isempty(s
));
196 assert_se(namespace_flags_to_string(CLONE_NEWNS
, &s
) == 0 && streq(s
, "mnt"));
198 assert_se(namespace_flags_to_string(CLONE_NEWNS
|CLONE_NEWIPC
, &s
) == 0 && streq(s
, "ipc mnt"));
200 assert_se(namespace_flags_to_string(CLONE_NEWCGROUP
, &s
) == 0 && streq(s
, "cgroup"));
203 assert_se(namespace_flags_from_string("mnt", &ul
) == 0 && ul
== CLONE_NEWNS
);
204 assert_se(namespace_flags_from_string(NULL
, &ul
) == 0 && ul
== 0);
205 assert_se(namespace_flags_from_string("", &ul
) == 0 && ul
== 0);
206 assert_se(namespace_flags_from_string("uts", &ul
) == 0 && ul
== CLONE_NEWUTS
);
207 assert_se(namespace_flags_from_string("mnt uts ipc", &ul
) == 0 && ul
== (CLONE_NEWNS
|CLONE_NEWUTS
|CLONE_NEWIPC
));
209 assert_se(namespace_flags_to_string(CLONE_NEWUTS
, &s
) == 0 && streq(s
, "uts"));
210 assert_se(namespace_flags_from_string(s
, &ul
) == 0 && ul
== CLONE_NEWUTS
);
212 assert_se(namespace_flags_from_string("ipc", &ul
) == 0 && ul
== CLONE_NEWIPC
);
213 assert_se(namespace_flags_to_string(ul
, &s
) == 0 && streq(s
, "ipc"));
216 assert_se(namespace_flags_to_string(NAMESPACE_FLAGS_ALL
, &s
) == 0);
217 assert_se(streq(s
, "cgroup ipc net mnt pid user uts"));
218 assert_se(namespace_flags_from_string(s
, &ul
) == 0 && ul
== NAMESPACE_FLAGS_ALL
);
221 if (!is_seccomp_available()) {
222 log_notice("Seccomp not available, skipping remaining tests in %s", __func__
);
225 if (geteuid() != 0) {
226 log_notice("Not root, skipping remaining tests in %s", __func__
);
235 assert_se(seccomp_restrict_namespaces(CLONE_NEWNS
|CLONE_NEWNET
) >= 0);
237 assert_se(unshare(CLONE_NEWNS
) == 0);
238 assert_se(unshare(CLONE_NEWNET
) == 0);
239 assert_se(unshare(CLONE_NEWUTS
) == -1);
240 assert_se(errno
== EPERM
);
241 assert_se(unshare(CLONE_NEWIPC
) == -1);
242 assert_se(errno
== EPERM
);
243 assert_se(unshare(CLONE_NEWNET
|CLONE_NEWUTS
) == -1);
244 assert_se(errno
== EPERM
);
246 /* We use fd 0 (stdin) here, which of course will fail with EINVAL on setns(). Except of course our
247 * seccomp filter worked, and hits first and makes it return EPERM */
248 assert_se(setns(0, CLONE_NEWNS
) == -1);
249 assert_se(errno
== EINVAL
);
250 assert_se(setns(0, CLONE_NEWNET
) == -1);
251 assert_se(errno
== EINVAL
);
252 assert_se(setns(0, CLONE_NEWUTS
) == -1);
253 assert_se(errno
== EPERM
);
254 assert_se(setns(0, CLONE_NEWIPC
) == -1);
255 assert_se(errno
== EPERM
);
256 assert_se(setns(0, CLONE_NEWNET
|CLONE_NEWUTS
) == -1);
257 assert_se(errno
== EPERM
);
258 assert_se(setns(0, 0) == -1);
259 assert_se(errno
== EPERM
);
261 pid
= raw_clone(CLONE_NEWNS
);
265 pid
= raw_clone(CLONE_NEWNET
);
269 pid
= raw_clone(CLONE_NEWUTS
);
271 assert_se(errno
== EPERM
);
272 pid
= raw_clone(CLONE_NEWIPC
);
274 assert_se(errno
== EPERM
);
275 pid
= raw_clone(CLONE_NEWNET
|CLONE_NEWUTS
);
277 assert_se(errno
== EPERM
);
282 assert_se(wait_for_terminate_and_check("nsseccomp", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
285 static void test_protect_sysctl(void) {
287 _cleanup_free_
char *seccomp
= NULL
;
289 log_info("/* %s */", __func__
);
291 if (!is_seccomp_available()) {
292 log_notice("Seccomp not available, skipping %s", __func__
);
295 if (geteuid() != 0) {
296 log_notice("Not root, skipping %s", __func__
);
300 /* in containers _sysctl() is likely missing anyway */
301 if (detect_container() > 0) {
302 log_notice("Testing in container, skipping %s", __func__
);
306 assert_se(get_proc_field("/proc/self/status", "Seccomp", WHITESPACE
, &seccomp
) == 0);
307 if (!streq(seccomp
, "0"))
308 log_warning("Warning: seccomp filter detected, results may be unreliable for %s", __func__
);
314 #if defined __NR__sysctl && __NR__sysctl >= 0
315 assert_se(syscall(__NR__sysctl
, NULL
) < 0);
316 assert_se(errno
== EFAULT
);
319 assert_se(seccomp_protect_sysctl() >= 0);
321 #if defined __NR__sysctl && __NR__sysctl >= 0
322 assert_se(syscall(__NR__sysctl
, 0, 0, 0) < 0);
323 assert_se(errno
== EPERM
);
329 assert_se(wait_for_terminate_and_check("sysctlseccomp", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
332 static void test_protect_syslog(void) {
335 log_info("/* %s */", __func__
);
337 if (!is_seccomp_available()) {
338 log_notice("Seccomp not available, skipping %s", __func__
);
341 if (geteuid() != 0) {
342 log_notice("Not root, skipping %s", __func__
);
346 /* in containers syslog() is likely missing anyway */
347 if (detect_container() > 0) {
348 log_notice("Testing in container, skipping %s", __func__
);
356 #if defined __NR_syslog && __NR_syslog >= 0
357 assert_se(syscall(__NR_syslog
, -1, NULL
, 0) < 0);
358 assert_se(errno
== EINVAL
);
361 assert_se(seccomp_protect_syslog() >= 0);
363 #if defined __NR_syslog && __NR_syslog >= 0
364 assert_se(syscall(__NR_syslog
, 0, 0, 0) < 0);
365 assert_se(errno
== EPERM
);
371 assert_se(wait_for_terminate_and_check("syslogseccomp", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
374 static void test_restrict_address_families(void) {
377 log_info("/* %s */", __func__
);
379 if (!is_seccomp_available()) {
380 log_notice("Seccomp not available, skipping %s", __func__
);
383 if (geteuid() != 0) {
384 log_notice("Not root, skipping %s", __func__
);
395 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
399 fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
403 fd
= socket(AF_NETLINK
, SOCK_DGRAM
, 0);
407 assert_se(s
= set_new(NULL
));
408 assert_se(set_put(s
, INT_TO_PTR(AF_UNIX
)) >= 0);
410 assert_se(seccomp_restrict_address_families(s
, false) >= 0);
412 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
416 fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
417 #if SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN
422 assert_se(errno
== EAFNOSUPPORT
);
425 fd
= socket(AF_NETLINK
, SOCK_DGRAM
, 0);
431 assert_se(set_put(s
, INT_TO_PTR(AF_INET
)) >= 0);
433 assert_se(seccomp_restrict_address_families(s
, true) >= 0);
435 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
439 fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
440 #if SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN
445 assert_se(errno
== EAFNOSUPPORT
);
448 fd
= socket(AF_NETLINK
, SOCK_DGRAM
, 0);
449 #if SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN
454 assert_se(errno
== EAFNOSUPPORT
);
460 assert_se(wait_for_terminate_and_check("socketseccomp", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
463 static void test_restrict_realtime(void) {
466 log_info("/* %s */", __func__
);
468 if (!is_seccomp_available()) {
469 log_notice("Seccomp not available, skipping %s", __func__
);
472 if (geteuid() != 0) {
473 log_notice("Not root, skipping %s", __func__
);
477 /* in containers RT privs are likely missing anyway */
478 if (detect_container() > 0) {
479 log_notice("Testing in container, skipping %s", __func__
);
487 assert_se(sched_setscheduler(0, SCHED_FIFO
, &(struct sched_param
) { .sched_priority
= 1 }) >= 0);
488 assert_se(sched_setscheduler(0, SCHED_RR
, &(struct sched_param
) { .sched_priority
= 1 }) >= 0);
489 assert_se(sched_setscheduler(0, SCHED_IDLE
, &(struct sched_param
) { .sched_priority
= 0 }) >= 0);
490 assert_se(sched_setscheduler(0, SCHED_BATCH
, &(struct sched_param
) { .sched_priority
= 0 }) >= 0);
491 assert_se(sched_setscheduler(0, SCHED_OTHER
, &(struct sched_param
) {}) >= 0);
493 assert_se(seccomp_restrict_realtime() >= 0);
495 assert_se(sched_setscheduler(0, SCHED_IDLE
, &(struct sched_param
) { .sched_priority
= 0 }) >= 0);
496 assert_se(sched_setscheduler(0, SCHED_BATCH
, &(struct sched_param
) { .sched_priority
= 0 }) >= 0);
497 assert_se(sched_setscheduler(0, SCHED_OTHER
, &(struct sched_param
) {}) >= 0);
499 assert_se(sched_setscheduler(0, SCHED_FIFO
, &(struct sched_param
) { .sched_priority
= 1 }) < 0);
500 assert_se(errno
== EPERM
);
501 assert_se(sched_setscheduler(0, SCHED_RR
, &(struct sched_param
) { .sched_priority
= 1 }) < 0);
502 assert_se(errno
== EPERM
);
507 assert_se(wait_for_terminate_and_check("realtimeseccomp", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
510 static void test_memory_deny_write_execute_mmap(void) {
513 log_info("/* %s */", __func__
);
515 if (!is_seccomp_available()) {
516 log_notice("Seccomp not available, skipping %s", __func__
);
519 if (geteuid() != 0) {
520 log_notice("Not root, skipping %s", __func__
);
530 p
= mmap(NULL
, page_size(), PROT_WRITE
|PROT_EXEC
, MAP_PRIVATE
|MAP_ANONYMOUS
, -1,0);
531 assert_se(p
!= MAP_FAILED
);
532 assert_se(munmap(p
, page_size()) >= 0);
534 p
= mmap(NULL
, page_size(), PROT_WRITE
|PROT_READ
, MAP_PRIVATE
|MAP_ANONYMOUS
, -1,0);
535 assert_se(p
!= MAP_FAILED
);
536 assert_se(munmap(p
, page_size()) >= 0);
538 assert_se(seccomp_memory_deny_write_execute() >= 0);
540 p
= mmap(NULL
, page_size(), PROT_WRITE
|PROT_EXEC
, MAP_PRIVATE
|MAP_ANONYMOUS
, -1,0);
541 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc64__) || defined(__arm__) || defined(__aarch64__)
542 assert_se(p
== MAP_FAILED
);
543 assert_se(errno
== EPERM
);
545 /* Depending on kernel, libseccomp, and glibc versions, other architectures
546 * might fail or not. Let's not assert success. */
548 assert_se(munmap(p
, page_size()) == 0);
550 p
= mmap(NULL
, page_size(), PROT_WRITE
|PROT_READ
, MAP_PRIVATE
|MAP_ANONYMOUS
, -1,0);
551 assert_se(p
!= MAP_FAILED
);
552 assert_se(munmap(p
, page_size()) >= 0);
557 assert_se(wait_for_terminate_and_check("memoryseccomp-mmap", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
560 static void test_memory_deny_write_execute_shmat(void) {
565 log_info("/* %s */", __func__
);
567 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
568 log_debug("arch %s: SCMP_SYS(mmap) = %d", seccomp_arch_to_string(arch
), SCMP_SYS(mmap
));
569 log_debug("arch %s: SCMP_SYS(mmap2) = %d", seccomp_arch_to_string(arch
), SCMP_SYS(mmap2
));
570 log_debug("arch %s: SCMP_SYS(shmget) = %d", seccomp_arch_to_string(arch
), SCMP_SYS(shmget
));
571 log_debug("arch %s: SCMP_SYS(shmat) = %d", seccomp_arch_to_string(arch
), SCMP_SYS(shmat
));
572 log_debug("arch %s: SCMP_SYS(shmdt) = %d", seccomp_arch_to_string(arch
), SCMP_SYS(shmdt
));
575 if (!is_seccomp_available()) {
576 log_notice("Seccomp not available, skipping %s", __func__
);
579 if (geteuid() != 0) {
580 log_notice("Not root, skipping %s", __func__
);
584 shmid
= shmget(IPC_PRIVATE
, page_size(), 0);
585 assert_se(shmid
>= 0);
593 p
= shmat(shmid
, NULL
, 0);
594 assert_se(p
!= MAP_FAILED
);
595 assert_se(shmdt(p
) == 0);
597 p
= shmat(shmid
, NULL
, SHM_EXEC
);
598 assert_se(p
!= MAP_FAILED
);
599 assert_se(shmdt(p
) == 0);
601 assert_se(seccomp_memory_deny_write_execute() >= 0);
603 p
= shmat(shmid
, NULL
, SHM_EXEC
);
604 log_debug_errno(p
== MAP_FAILED
? errno
: 0, "shmat(SHM_EXEC): %m");
605 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
606 assert_se(p
== MAP_FAILED
);
607 assert_se(errno
== EPERM
);
609 /* Depending on kernel, libseccomp, and glibc versions, other architectures
610 * might fail or not. Let's not assert success. */
612 assert_se(shmdt(p
) == 0);
614 p
= shmat(shmid
, NULL
, 0);
615 log_debug_errno(p
== MAP_FAILED
? errno
: 0, "shmat(0): %m");
616 assert_se(p
!= MAP_FAILED
);
617 assert_se(shmdt(p
) == 0);
622 assert_se(wait_for_terminate_and_check("memoryseccomp-shmat", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
625 static void test_restrict_archs(void) {
628 log_info("/* %s */", __func__
);
630 if (!is_seccomp_available()) {
631 log_notice("Seccomp not available, skipping %s", __func__
);
634 if (geteuid() != 0) {
635 log_notice("Not root, skipping %s", __func__
);
643 _cleanup_set_free_ Set
*s
= NULL
;
645 assert_se(access("/", F_OK
) >= 0);
647 assert_se(s
= set_new(NULL
));
650 assert_se(set_put(s
, UINT32_TO_PTR(SCMP_ARCH_X86
+1)) >= 0);
652 assert_se(seccomp_restrict_archs(s
) >= 0);
654 assert_se(access("/", F_OK
) >= 0);
655 assert_se(seccomp_restrict_archs(NULL
) >= 0);
657 assert_se(access("/", F_OK
) >= 0);
662 assert_se(wait_for_terminate_and_check("archseccomp", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
665 static void test_load_syscall_filter_set_raw(void) {
668 log_info("/* %s */", __func__
);
670 if (!is_seccomp_available()) {
671 log_notice("Seccomp not available, skipping %s", __func__
);
674 if (geteuid() != 0) {
675 log_notice("Not root, skipping %s", __func__
);
683 _cleanup_hashmap_free_ Hashmap
*s
= NULL
;
685 assert_se(access("/", F_OK
) >= 0);
686 assert_se(poll(NULL
, 0, 0) == 0);
688 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW
, NULL
, scmp_act_kill_process(), true) >= 0);
689 assert_se(access("/", F_OK
) >= 0);
690 assert_se(poll(NULL
, 0, 0) == 0);
692 assert_se(s
= hashmap_new(NULL
));
693 #if defined __NR_access && __NR_access >= 0
694 assert_se(hashmap_put(s
, UINT32_TO_PTR(__NR_access
+ 1), INT_TO_PTR(-1)) >= 0);
696 assert_se(hashmap_put(s
, UINT32_TO_PTR(__NR_faccessat
+ 1), INT_TO_PTR(-1)) >= 0);
699 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW
, s
, SCMP_ACT_ERRNO(EUCLEAN
), true) >= 0);
701 assert_se(access("/", F_OK
) < 0);
702 assert_se(errno
== EUCLEAN
);
704 assert_se(poll(NULL
, 0, 0) == 0);
708 assert_se(s
= hashmap_new(NULL
));
709 #if defined __NR_access && __NR_access >= 0
710 assert_se(hashmap_put(s
, UINT32_TO_PTR(__NR_access
+ 1), INT_TO_PTR(EILSEQ
)) >= 0);
712 assert_se(hashmap_put(s
, UINT32_TO_PTR(__NR_faccessat
+ 1), INT_TO_PTR(EILSEQ
)) >= 0);
715 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW
, s
, SCMP_ACT_ERRNO(EUCLEAN
), true) >= 0);
717 assert_se(access("/", F_OK
) < 0);
718 assert_se(errno
== EILSEQ
);
720 assert_se(poll(NULL
, 0, 0) == 0);
724 assert_se(s
= hashmap_new(NULL
));
725 #if defined __NR_poll && __NR_poll >= 0
726 assert_se(hashmap_put(s
, UINT32_TO_PTR(__NR_poll
+ 1), INT_TO_PTR(-1)) >= 0);
728 assert_se(hashmap_put(s
, UINT32_TO_PTR(__NR_ppoll
+ 1), INT_TO_PTR(-1)) >= 0);
731 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW
, s
, SCMP_ACT_ERRNO(EUNATCH
), true) >= 0);
733 assert_se(access("/", F_OK
) < 0);
734 assert_se(errno
== EILSEQ
);
736 assert_se(poll(NULL
, 0, 0) < 0);
737 assert_se(errno
== EUNATCH
);
741 assert_se(s
= hashmap_new(NULL
));
742 #if defined __NR_poll && __NR_poll >= 0
743 assert_se(hashmap_put(s
, UINT32_TO_PTR(__NR_poll
+ 1), INT_TO_PTR(EILSEQ
)) >= 0);
745 assert_se(hashmap_put(s
, UINT32_TO_PTR(__NR_ppoll
+ 1), INT_TO_PTR(EILSEQ
)) >= 0);
748 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW
, s
, SCMP_ACT_ERRNO(EUNATCH
), true) >= 0);
750 assert_se(access("/", F_OK
) < 0);
751 assert_se(errno
== EILSEQ
);
753 assert_se(poll(NULL
, 0, 0) < 0);
754 assert_se(errno
== EILSEQ
);
759 assert_se(wait_for_terminate_and_check("syscallrawseccomp", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
762 static void test_lock_personality(void) {
763 unsigned long current
;
766 log_info("/* %s */", __func__
);
768 if (!is_seccomp_available()) {
769 log_notice("Seccomp not available, skipping %s", __func__
);
772 if (geteuid() != 0) {
773 log_notice("Not root, skipping %s", __func__
);
777 assert_se(opinionated_personality(¤t
) >= 0);
779 log_info("current personality=%lu", current
);
785 assert_se(seccomp_lock_personality(current
) >= 0);
787 assert_se((unsigned long) safe_personality(current
) == current
);
789 /* Note, we also test that safe_personality() works correctly, by checkig whether errno is properly
790 * set, in addition to the return value */
792 assert_se(safe_personality(PER_LINUX
| ADDR_NO_RANDOMIZE
) == -EPERM
);
793 assert_se(errno
== EPERM
);
795 assert_se(safe_personality(PER_LINUX
| MMAP_PAGE_ZERO
) == -EPERM
);
796 assert_se(safe_personality(PER_LINUX
| ADDR_COMPAT_LAYOUT
) == -EPERM
);
797 assert_se(safe_personality(PER_LINUX
| READ_IMPLIES_EXEC
) == -EPERM
);
798 assert_se(safe_personality(PER_LINUX_32BIT
) == -EPERM
);
799 assert_se(safe_personality(PER_SVR4
) == -EPERM
);
800 assert_se(safe_personality(PER_BSD
) == -EPERM
);
801 assert_se(safe_personality(current
== PER_LINUX
? PER_LINUX32
: PER_LINUX
) == -EPERM
);
802 assert_se(safe_personality(PER_LINUX32_3GB
) == -EPERM
);
803 assert_se(safe_personality(PER_UW7
) == -EPERM
);
804 assert_se(safe_personality(0x42) == -EPERM
);
806 assert_se(safe_personality(PERSONALITY_INVALID
) == -EPERM
); /* maybe remove this later */
808 assert_se((unsigned long) personality(current
) == current
);
812 assert_se(wait_for_terminate_and_check("lockpersonalityseccomp", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
815 static int real_open(const char *path
, int flags
, mode_t mode
) {
816 /* glibc internally calls openat() when open() is requested. Let's hence define our own wrapper for
817 * testing purposes that calls the real syscall, on architectures where SYS_open is defined. On
818 * other architectures, let's just fall back to the glibc call. */
820 #if defined __NR_open && __NR_open >= 0
821 return (int) syscall(__NR_open
, path
, flags
, mode
);
823 return open(path
, flags
, mode
);
827 static void test_restrict_suid_sgid(void) {
830 log_info("/* %s */", __func__
);
832 if (!is_seccomp_available()) {
833 log_notice("Seccomp not available, skipping %s", __func__
);
836 if (geteuid() != 0) {
837 log_notice("Not root, skipping %s", __func__
);
845 char path
[] = "/tmp/suidsgidXXXXXX", dir
[] = "/tmp/suidsgiddirXXXXXX";
849 fd
= mkostemp_safe(path
);
852 assert_se(mkdtemp(dir
));
853 z
= strjoina(dir
, "/test");
855 assert_se(chmod(path
, 0755 | S_ISUID
) >= 0);
856 assert_se(chmod(path
, 0755 | S_ISGID
) >= 0);
857 assert_se(chmod(path
, 0755 | S_ISGID
| S_ISUID
) >= 0);
858 assert_se(chmod(path
, 0755) >= 0);
860 assert_se(fchmod(fd
, 0755 | S_ISUID
) >= 0);
861 assert_se(fchmod(fd
, 0755 | S_ISGID
) >= 0);
862 assert_se(fchmod(fd
, 0755 | S_ISGID
| S_ISUID
) >= 0);
863 assert_se(fchmod(fd
, 0755) >= 0);
865 assert_se(fchmodat(AT_FDCWD
, path
, 0755 | S_ISUID
, 0) >= 0);
866 assert_se(fchmodat(AT_FDCWD
, path
, 0755 | S_ISGID
, 0) >= 0);
867 assert_se(fchmodat(AT_FDCWD
, path
, 0755 | S_ISGID
| S_ISUID
, 0) >= 0);
868 assert_se(fchmodat(AT_FDCWD
, path
, 0755, 0) >= 0);
870 k
= real_open(z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISUID
);
872 assert_se(unlink(z
) >= 0);
874 k
= real_open(z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISGID
);
876 assert_se(unlink(z
) >= 0);
878 k
= real_open(z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISUID
| S_ISGID
);
880 assert_se(unlink(z
) >= 0);
882 k
= real_open(z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644);
884 assert_se(unlink(z
) >= 0);
886 k
= creat(z
, 0644 | S_ISUID
);
888 assert_se(unlink(z
) >= 0);
890 k
= creat(z
, 0644 | S_ISGID
);
892 assert_se(unlink(z
) >= 0);
894 k
= creat(z
, 0644 | S_ISUID
| S_ISGID
);
896 assert_se(unlink(z
) >= 0);
900 assert_se(unlink(z
) >= 0);
902 k
= openat(AT_FDCWD
, z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISUID
);
904 assert_se(unlink(z
) >= 0);
906 k
= openat(AT_FDCWD
, z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISGID
);
908 assert_se(unlink(z
) >= 0);
910 k
= openat(AT_FDCWD
, z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISUID
| S_ISGID
);
912 assert_se(unlink(z
) >= 0);
914 k
= openat(AT_FDCWD
, z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644);
916 assert_se(unlink(z
) >= 0);
918 assert_se(mkdir(z
, 0755 | S_ISUID
) >= 0);
919 assert_se(rmdir(z
) >= 0);
920 assert_se(mkdir(z
, 0755 | S_ISGID
) >= 0);
921 assert_se(rmdir(z
) >= 0);
922 assert_se(mkdir(z
, 0755 | S_ISUID
| S_ISGID
) >= 0);
923 assert_se(rmdir(z
) >= 0);
924 assert_se(mkdir(z
, 0755) >= 0);
925 assert_se(rmdir(z
) >= 0);
927 assert_se(mkdirat(AT_FDCWD
, z
, 0755 | S_ISUID
) >= 0);
928 assert_se(rmdir(z
) >= 0);
929 assert_se(mkdirat(AT_FDCWD
, z
, 0755 | S_ISGID
) >= 0);
930 assert_se(rmdir(z
) >= 0);
931 assert_se(mkdirat(AT_FDCWD
, z
, 0755 | S_ISUID
| S_ISGID
) >= 0);
932 assert_se(rmdir(z
) >= 0);
933 assert_se(mkdirat(AT_FDCWD
, z
, 0755) >= 0);
934 assert_se(rmdir(z
) >= 0);
936 assert_se(mknod(z
, S_IFREG
| 0755 | S_ISUID
, 0) >= 0);
937 assert_se(unlink(z
) >= 0);
938 assert_se(mknod(z
, S_IFREG
| 0755 | S_ISGID
, 0) >= 0);
939 assert_se(unlink(z
) >= 0);
940 assert_se(mknod(z
, S_IFREG
| 0755 | S_ISUID
| S_ISGID
, 0) >= 0);
941 assert_se(unlink(z
) >= 0);
942 assert_se(mknod(z
, S_IFREG
| 0755, 0) >= 0);
943 assert_se(unlink(z
) >= 0);
945 assert_se(mknodat(AT_FDCWD
, z
, S_IFREG
| 0755 | S_ISUID
, 0) >= 0);
946 assert_se(unlink(z
) >= 0);
947 assert_se(mknodat(AT_FDCWD
, z
, S_IFREG
| 0755 | S_ISGID
, 0) >= 0);
948 assert_se(unlink(z
) >= 0);
949 assert_se(mknodat(AT_FDCWD
, z
, S_IFREG
| 0755 | S_ISUID
| S_ISGID
, 0) >= 0);
950 assert_se(unlink(z
) >= 0);
951 assert_se(mknodat(AT_FDCWD
, z
, S_IFREG
| 0755, 0) >= 0);
952 assert_se(unlink(z
) >= 0);
954 assert_se(seccomp_restrict_suid_sgid() >= 0);
956 assert_se(chmod(path
, 0775 | S_ISUID
) < 0 && errno
== EPERM
);
957 assert_se(chmod(path
, 0775 | S_ISGID
) < 0 && errno
== EPERM
);
958 assert_se(chmod(path
, 0775 | S_ISGID
| S_ISUID
) < 0 && errno
== EPERM
);
959 assert_se(chmod(path
, 0775) >= 0);
961 assert_se(fchmod(fd
, 0775 | S_ISUID
) < 0 && errno
== EPERM
);
962 assert_se(fchmod(fd
, 0775 | S_ISGID
) < 0 && errno
== EPERM
);
963 assert_se(fchmod(fd
, 0775 | S_ISGID
| S_ISUID
) < 0 && errno
== EPERM
);
964 assert_se(fchmod(fd
, 0775) >= 0);
966 assert_se(fchmodat(AT_FDCWD
, path
, 0755 | S_ISUID
, 0) < 0 && errno
== EPERM
);
967 assert_se(fchmodat(AT_FDCWD
, path
, 0755 | S_ISGID
, 0) < 0 && errno
== EPERM
);
968 assert_se(fchmodat(AT_FDCWD
, path
, 0755 | S_ISGID
| S_ISUID
, 0) < 0 && errno
== EPERM
);
969 assert_se(fchmodat(AT_FDCWD
, path
, 0755, 0) >= 0);
971 assert_se(real_open(z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISUID
) < 0 && errno
== EPERM
);
972 assert_se(real_open(z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISGID
) < 0 && errno
== EPERM
);
973 assert_se(real_open(z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISUID
| S_ISGID
) < 0 && errno
== EPERM
);
974 k
= real_open(z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644);
976 assert_se(unlink(z
) >= 0);
978 assert_se(creat(z
, 0644 | S_ISUID
) < 0 && errno
== EPERM
);
979 assert_se(creat(z
, 0644 | S_ISGID
) < 0 && errno
== EPERM
);
980 assert_se(creat(z
, 0644 | S_ISUID
| S_ISGID
) < 0 && errno
== EPERM
);
983 assert_se(unlink(z
) >= 0);
985 assert_se(openat(AT_FDCWD
, z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISUID
) < 0 && errno
== EPERM
);
986 assert_se(openat(AT_FDCWD
, z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISGID
) < 0 && errno
== EPERM
);
987 assert_se(openat(AT_FDCWD
, z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644 | S_ISUID
| S_ISGID
) < 0 && errno
== EPERM
);
988 k
= openat(AT_FDCWD
, z
, O_CREAT
|O_RDWR
|O_CLOEXEC
|O_EXCL
, 0644);
990 assert_se(unlink(z
) >= 0);
992 assert_se(mkdir(z
, 0755 | S_ISUID
) < 0 && errno
== EPERM
);
993 assert_se(mkdir(z
, 0755 | S_ISGID
) < 0 && errno
== EPERM
);
994 assert_se(mkdir(z
, 0755 | S_ISUID
| S_ISGID
) < 0 && errno
== EPERM
);
995 assert_se(mkdir(z
, 0755) >= 0);
996 assert_se(rmdir(z
) >= 0);
998 assert_se(mkdirat(AT_FDCWD
, z
, 0755 | S_ISUID
) < 0 && errno
== EPERM
);
999 assert_se(mkdirat(AT_FDCWD
, z
, 0755 | S_ISGID
) < 0 && errno
== EPERM
);
1000 assert_se(mkdirat(AT_FDCWD
, z
, 0755 | S_ISUID
| S_ISGID
) < 0 && errno
== EPERM
);
1001 assert_se(mkdirat(AT_FDCWD
, z
, 0755) >= 0);
1002 assert_se(rmdir(z
) >= 0);
1004 assert_se(mknod(z
, S_IFREG
| 0755 | S_ISUID
, 0) < 0 && errno
== EPERM
);
1005 assert_se(mknod(z
, S_IFREG
| 0755 | S_ISGID
, 0) < 0 && errno
== EPERM
);
1006 assert_se(mknod(z
, S_IFREG
| 0755 | S_ISUID
| S_ISGID
, 0) < 0 && errno
== EPERM
);
1007 assert_se(mknod(z
, S_IFREG
| 0755, 0) >= 0);
1008 assert_se(unlink(z
) >= 0);
1010 assert_se(mknodat(AT_FDCWD
, z
, S_IFREG
| 0755 | S_ISUID
, 0) < 0 && errno
== EPERM
);
1011 assert_se(mknodat(AT_FDCWD
, z
, S_IFREG
| 0755 | S_ISGID
, 0) < 0 && errno
== EPERM
);
1012 assert_se(mknodat(AT_FDCWD
, z
, S_IFREG
| 0755 | S_ISUID
| S_ISGID
, 0) < 0 && errno
== EPERM
);
1013 assert_se(mknodat(AT_FDCWD
, z
, S_IFREG
| 0755, 0) >= 0);
1014 assert_se(unlink(z
) >= 0);
1016 assert_se(unlink(path
) >= 0);
1017 assert_se(rm_rf(dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
1019 _exit(EXIT_SUCCESS
);
1022 assert_se(wait_for_terminate_and_check("suidsgidseccomp", pid
, WAIT_LOG
) == EXIT_SUCCESS
);
1025 int main(int argc
, char *argv
[]) {
1026 test_setup_logging(LOG_DEBUG
);
1028 test_seccomp_arch_to_string();
1029 test_architecture_table();
1030 test_syscall_filter_set_find();
1032 test_filter_sets_ordered();
1033 test_restrict_namespace();
1034 test_protect_sysctl();
1035 test_protect_syslog();
1036 test_restrict_address_families();
1037 test_restrict_realtime();
1038 test_memory_deny_write_execute_mmap();
1039 test_memory_deny_write_execute_shmat();
1040 test_restrict_archs();
1041 test_load_syscall_filter_set_raw();
1042 test_lock_personality();
1043 test_restrict_suid_sgid();