1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <linux/seccomp.h>
14 #include "alloc-util.h"
15 #include "errno-list.h"
18 #include "nulstr-util.h"
19 #include "process-util.h"
20 #include "seccomp-util.h"
22 #include "string-util.h"
25 const uint32_t seccomp_local_archs
[] = {
27 /* Note: always list the native arch we are compiled as last, so that users can blacklist seccomp(), but our own calls to it still succeed */
29 #if defined(__x86_64__) && defined(__ILP32__)
32 SCMP_ARCH_X32
, /* native */
33 #elif defined(__x86_64__) && !defined(__ILP32__)
36 SCMP_ARCH_X86_64
, /* native */
37 #elif defined(__i386__)
39 #elif defined(__aarch64__)
41 SCMP_ARCH_AARCH64
, /* native */
42 #elif defined(__arm__)
44 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
46 SCMP_ARCH_MIPS
, /* native */
47 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
49 SCMP_ARCH_MIPSEL
, /* native */
50 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
53 SCMP_ARCH_MIPSEL64N32
,
56 SCMP_ARCH_MIPS64
, /* native */
57 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
61 SCMP_ARCH_MIPSEL64N32
,
63 SCMP_ARCH_MIPSEL64
, /* native */
64 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
69 SCMP_ARCH_MIPSEL64N32
,
70 SCMP_ARCH_MIPS64N32
, /* native */
71 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
77 SCMP_ARCH_MIPSEL64N32
, /* native */
78 #elif defined(__powerpc64__) && __BYTE_ORDER == __BIG_ENDIAN
81 SCMP_ARCH_PPC64
, /* native */
82 #elif defined(__powerpc64__) && __BYTE_ORDER == __LITTLE_ENDIAN
85 SCMP_ARCH_PPC64LE
, /* native */
86 #elif defined(__powerpc__)
88 #elif defined(__s390x__)
90 SCMP_ARCH_S390X
, /* native */
91 #elif defined(__s390__)
97 const char* seccomp_arch_to_string(uint32_t c
) {
98 /* Maintain order used in <seccomp.h>.
100 * Names used here should be the same as those used for ConditionArchitecture=,
101 * except for "subarchitectures" like x32. */
104 case SCMP_ARCH_NATIVE
:
108 case SCMP_ARCH_X86_64
:
114 case SCMP_ARCH_AARCH64
:
118 case SCMP_ARCH_MIPS64
:
120 case SCMP_ARCH_MIPS64N32
:
122 case SCMP_ARCH_MIPSEL
:
124 case SCMP_ARCH_MIPSEL64
:
126 case SCMP_ARCH_MIPSEL64N32
:
127 return "mips64-le-n32";
130 case SCMP_ARCH_PPC64
:
132 case SCMP_ARCH_PPC64LE
:
136 case SCMP_ARCH_S390X
:
143 int seccomp_arch_from_string(const char *n
, uint32_t *ret
) {
149 if (streq(n
, "native"))
150 *ret
= SCMP_ARCH_NATIVE
;
151 else if (streq(n
, "x86"))
152 *ret
= SCMP_ARCH_X86
;
153 else if (streq(n
, "x86-64"))
154 *ret
= SCMP_ARCH_X86_64
;
155 else if (streq(n
, "x32"))
156 *ret
= SCMP_ARCH_X32
;
157 else if (streq(n
, "arm"))
158 *ret
= SCMP_ARCH_ARM
;
159 else if (streq(n
, "arm64"))
160 *ret
= SCMP_ARCH_AARCH64
;
161 else if (streq(n
, "mips"))
162 *ret
= SCMP_ARCH_MIPS
;
163 else if (streq(n
, "mips64"))
164 *ret
= SCMP_ARCH_MIPS64
;
165 else if (streq(n
, "mips64-n32"))
166 *ret
= SCMP_ARCH_MIPS64N32
;
167 else if (streq(n
, "mips-le"))
168 *ret
= SCMP_ARCH_MIPSEL
;
169 else if (streq(n
, "mips64-le"))
170 *ret
= SCMP_ARCH_MIPSEL64
;
171 else if (streq(n
, "mips64-le-n32"))
172 *ret
= SCMP_ARCH_MIPSEL64N32
;
173 else if (streq(n
, "ppc"))
174 *ret
= SCMP_ARCH_PPC
;
175 else if (streq(n
, "ppc64"))
176 *ret
= SCMP_ARCH_PPC64
;
177 else if (streq(n
, "ppc64-le"))
178 *ret
= SCMP_ARCH_PPC64LE
;
179 else if (streq(n
, "s390"))
180 *ret
= SCMP_ARCH_S390
;
181 else if (streq(n
, "s390x"))
182 *ret
= SCMP_ARCH_S390X
;
189 int seccomp_init_for_arch(scmp_filter_ctx
*ret
, uint32_t arch
, uint32_t default_action
) {
190 scmp_filter_ctx seccomp
;
193 /* Much like seccomp_init(), but initializes the filter for one specific architecture only, without affecting
194 * any others. Also, turns off the NNP fiddling. */
196 seccomp
= seccomp_init(default_action
);
200 if (arch
!= SCMP_ARCH_NATIVE
&&
201 arch
!= seccomp_arch_native()) {
203 r
= seccomp_arch_remove(seccomp
, seccomp_arch_native());
207 r
= seccomp_arch_add(seccomp
, arch
);
211 assert(seccomp_arch_exist(seccomp
, arch
) >= 0);
212 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) == -EEXIST
);
213 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) == -EEXIST
);
215 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) >= 0);
216 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) >= 0);
219 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_ACT_BADARCH
, SCMP_ACT_ALLOW
);
223 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
231 seccomp_release(seccomp
);
235 static bool is_basic_seccomp_available(void) {
236 return prctl(PR_GET_SECCOMP
, 0, 0, 0, 0) >= 0;
239 static bool is_seccomp_filter_available(void) {
240 return prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, 0, 0) < 0 &&
244 bool is_seccomp_available(void) {
245 static int cached_enabled
= -1;
247 if (cached_enabled
< 0)
249 is_basic_seccomp_available() &&
250 is_seccomp_filter_available();
252 return cached_enabled
;
255 const SyscallFilterSet syscall_filter_sets
[_SYSCALL_FILTER_SET_MAX
] = {
256 [SYSCALL_FILTER_SET_DEFAULT
] = {
258 .help
= "System calls that are always permitted",
261 "clock_getres_time64\0"
265 "clock_nanosleep_time64\0"
289 "getrlimit\0" /* make sure processes can query stack size and such */
311 [SYSCALL_FILTER_SET_AIO
] = {
313 .help
= "Asynchronous IO",
319 "io_pgetevents_time64\0"
323 "io_uring_register\0"
326 [SYSCALL_FILTER_SET_BASIC_IO
] = {
347 [SYSCALL_FILTER_SET_CHOWN
] = {
349 .help
= "Change ownership of files and directories",
359 [SYSCALL_FILTER_SET_CLOCK
] = {
361 .help
= "Change the system time",
371 [SYSCALL_FILTER_SET_CPU_EMULATION
] = {
372 .name
= "@cpu-emulation",
373 .help
= "System calls for CPU emulation functionality",
381 [SYSCALL_FILTER_SET_DEBUG
] = {
383 .help
= "Debugging, performance monitoring and tracing functionality",
389 #ifdef __NR_s390_runtime_instr
390 "s390_runtime_instr\0"
392 "sys_debug_setcontext\0"
394 [SYSCALL_FILTER_SET_FILE_SYSTEM
] = {
395 .name
= "@file-system",
396 .help
= "File system operations",
426 "inotify_add_watch\0"
478 [SYSCALL_FILTER_SET_IO_EVENT
] = {
480 .help
= "Event loop system calls",
499 [SYSCALL_FILTER_SET_IPC
] = {
501 .help
= "SysV IPC, POSIX Message Queues or other IPC",
509 "mq_timedreceive_time64\0"
511 "mq_timedsend_time64\0"
520 "process_vm_writev\0"
525 "semtimedop_time64\0"
531 [SYSCALL_FILTER_SET_KEYRING
] = {
533 .help
= "Kernel keyring access",
539 [SYSCALL_FILTER_SET_MEMLOCK
] = {
541 .help
= "Memory locking control",
549 [SYSCALL_FILTER_SET_MODULE
] = {
551 .help
= "Loading and unloading of kernel modules",
557 [SYSCALL_FILTER_SET_MOUNT
] = {
559 .help
= "Mounting and unmounting of file systems",
573 [SYSCALL_FILTER_SET_NETWORK_IO
] = {
574 .name
= "@network-io",
575 .help
= "Network or Unix socket IO, should not be needed if not network facing",
600 [SYSCALL_FILTER_SET_OBSOLETE
] = {
601 /* some unknown even to libseccomp */
603 .help
= "Unusual, obsolete or unimplemented system calls",
632 [SYSCALL_FILTER_SET_PKEY
] = {
634 .help
= "System calls used for memory protection keys",
640 [SYSCALL_FILTER_SET_PRIVILEGED
] = {
641 .name
= "@privileged",
642 .help
= "All system calls which need super-user capabilities",
658 "open_by_handle_at\0"
671 "setuid\0" /* We list the explicit system calls here, as @setuid also includes setgid() which is not necessarily privileged */
675 [SYSCALL_FILTER_SET_PROCESS
] = {
677 .help
= "Process control, execution, namespaceing operations",
680 "capget\0" /* Able to query arbitrary processes */
688 "pidfd_send_signal\0"
691 "rt_tgsigqueueinfo\0"
693 "swapcontext\0" /* Some archs e.g. powerpc32 are using it to do userspace context switches */
703 [SYSCALL_FILTER_SET_RAW_IO
] = {
705 .help
= "Raw I/O port access",
712 #ifdef __NR_s390_pci_mmio_read
713 "s390_pci_mmio_read\0"
715 #ifdef __NR_s390_pci_mmio_write
716 "s390_pci_mmio_write\0"
719 [SYSCALL_FILTER_SET_REBOOT
] = {
721 .help
= "Reboot and reboot preparation/kexec",
727 [SYSCALL_FILTER_SET_RESOURCES
] = {
728 .name
= "@resources",
729 .help
= "Alter resource settings",
736 "sched_setaffinity\0"
739 "sched_setscheduler\0"
744 [SYSCALL_FILTER_SET_SETUID
] = {
746 .help
= "Operations for changing user/group credentials",
763 [SYSCALL_FILTER_SET_SIGNAL
] = {
765 .help
= "Process signal handling",
772 "rt_sigtimedwait_time64\0"
782 [SYSCALL_FILTER_SET_SWAP
] = {
784 .help
= "Enable/disable swap devices",
789 [SYSCALL_FILTER_SET_SYNC
] = {
791 .help
= "Synchronize files and memory to storage",
801 [SYSCALL_FILTER_SET_SYSTEM_SERVICE
] = {
802 .name
= "@system-service",
803 .help
= "General system service operations",
838 "name_to_handle_at\0"
845 "sched_get_priority_max\0"
846 "sched_get_priority_min\0"
847 "sched_getaffinity\0"
850 "sched_getscheduler\0"
851 "sched_rr_get_interval\0"
852 "sched_rr_get_interval_time64\0"
870 [SYSCALL_FILTER_SET_TIMER
] = {
872 .help
= "Schedule operations by time",
886 "timerfd_gettime64\0"
888 "timerfd_settime64\0"
893 const SyscallFilterSet
*syscall_filter_set_find(const char *name
) {
896 if (isempty(name
) || name
[0] != '@')
899 for (i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++)
900 if (streq(syscall_filter_sets
[i
].name
, name
))
901 return syscall_filter_sets
+ i
;
906 static int seccomp_add_syscall_filter_set(scmp_filter_ctx seccomp
, const SyscallFilterSet
*set
, uint32_t action
, char **exclude
, bool log_missing
);
908 int seccomp_add_syscall_filter_item(scmp_filter_ctx
*seccomp
, const char *name
, uint32_t action
, char **exclude
, bool log_missing
) {
912 if (strv_contains(exclude
, name
))
915 if (name
[0] == '@') {
916 const SyscallFilterSet
*other
;
918 other
= syscall_filter_set_find(name
);
920 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
921 "Filter set %s is not known!",
924 return seccomp_add_syscall_filter_set(seccomp
, other
, action
, exclude
, log_missing
);
929 id
= seccomp_syscall_resolve_name(name
);
930 if (id
== __NR_SCMP_ERROR
) {
932 log_debug("System call %s is not known, ignoring.", name
);
936 r
= seccomp_rule_add_exact(seccomp
, action
, id
, 0);
938 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
939 bool ignore
= r
== -EDOM
;
941 if (!ignore
|| log_missing
)
942 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
943 name
, id
, ignore
? ", ignoring" : "");
952 static int seccomp_add_syscall_filter_set(
953 scmp_filter_ctx seccomp
,
954 const SyscallFilterSet
*set
,
965 NULSTR_FOREACH(sys
, set
->value
) {
966 r
= seccomp_add_syscall_filter_item(seccomp
, sys
, action
, exclude
, log_missing
);
974 int seccomp_load_syscall_filter_set(uint32_t default_action
, const SyscallFilterSet
*set
, uint32_t action
, bool log_missing
) {
980 /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
981 * each local arch. */
983 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
984 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
986 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
988 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
992 r
= seccomp_add_syscall_filter_set(seccomp
, set
, action
, NULL
, log_missing
);
994 return log_debug_errno(r
, "Failed to add filter set: %m");
996 r
= seccomp_load(seccomp
);
997 if (ERRNO_IS_SECCOMP_FATAL(r
))
1000 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1006 int seccomp_load_syscall_filter_set_raw(uint32_t default_action
, Hashmap
* set
, uint32_t action
, bool log_missing
) {
1010 /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Set* of syscalls, instead of a
1011 * SyscallFilterSet* table. */
1013 if (hashmap_isempty(set
) && default_action
== SCMP_ACT_ALLOW
)
1016 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1017 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1019 void *syscall_id
, *val
;
1021 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1023 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1027 HASHMAP_FOREACH_KEY(val
, syscall_id
, set
, i
) {
1028 uint32_t a
= action
;
1029 int id
= PTR_TO_INT(syscall_id
) - 1;
1030 int error
= PTR_TO_INT(val
);
1032 if (action
!= SCMP_ACT_ALLOW
&& error
>= 0)
1033 a
= SCMP_ACT_ERRNO(error
);
1035 r
= seccomp_rule_add_exact(seccomp
, a
, id
, 0);
1037 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
1038 _cleanup_free_
char *n
= NULL
;
1041 n
= seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE
, id
);
1042 ignore
= r
== -EDOM
;
1043 if (!ignore
|| log_missing
)
1044 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1045 strna(n
), id
, ignore
? ", ignoring" : "");
1051 r
= seccomp_load(seccomp
);
1052 if (ERRNO_IS_SECCOMP_FATAL(r
))
1055 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1061 int seccomp_parse_syscall_filter(
1065 SeccompParseFlags flags
,
1067 const char *filename
,
1075 if (name
[0] == '@') {
1076 const SyscallFilterSet
*set
;
1079 set
= syscall_filter_set_find(name
);
1081 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1084 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1085 "Unknown system call group, ignoring: %s", name
);
1089 NULSTR_FOREACH(i
, set
->value
) {
1090 /* Call ourselves again, for the group to parse. Note that we downgrade logging here (i.e. take
1091 * away the SECCOMP_PARSE_LOG flag) since any issues in the group table are our own problem,
1092 * not a problem in user configuration data and we shouldn't pretend otherwise by complaining
1094 r
= seccomp_parse_syscall_filter(i
, errno_num
, filter
, flags
&~ SECCOMP_PARSE_LOG
, unit
, filename
, line
);
1101 id
= seccomp_syscall_resolve_name(name
);
1102 if (id
== __NR_SCMP_ERROR
) {
1103 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1106 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1107 "Failed to parse system call, ignoring: %s", name
);
1111 /* If we previously wanted to forbid a syscall and now
1112 * we want to allow it, then remove it from the list. */
1113 if (!(flags
& SECCOMP_PARSE_INVERT
) == !!(flags
& SECCOMP_PARSE_WHITELIST
)) {
1114 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
));
1118 return flags
& SECCOMP_PARSE_LOG
? log_oom() : -ENOMEM
;
1120 assert_se(hashmap_update(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
)) == 0);
1126 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1132 int seccomp_restrict_namespaces(unsigned long retain
) {
1136 if (DEBUG_LOGGING
) {
1137 _cleanup_free_
char *s
= NULL
;
1139 (void) namespace_flags_to_string(retain
, &s
);
1140 log_debug("Restricting namespace to: %s.", strna(s
));
1144 if ((retain
& NAMESPACE_FLAGS_ALL
) == NAMESPACE_FLAGS_ALL
)
1147 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1148 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1151 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1153 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1157 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
1158 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
1160 r
= seccomp_rule_add_exact(
1162 SCMP_ACT_ERRNO(EPERM
),
1166 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
1167 * special invocation with a zero flags argument, right here. */
1168 r
= seccomp_rule_add_exact(
1170 SCMP_ACT_ERRNO(EPERM
),
1173 SCMP_A1(SCMP_CMP_EQ
, 0));
1175 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1179 for (i
= 0; namespace_flag_map
[i
].name
; i
++) {
1182 f
= namespace_flag_map
[i
].flag
;
1183 if ((retain
& f
) == f
) {
1184 log_debug("Permitting %s.", namespace_flag_map
[i
].name
);
1188 log_debug("Blocking %s.", namespace_flag_map
[i
].name
);
1190 r
= seccomp_rule_add_exact(
1192 SCMP_ACT_ERRNO(EPERM
),
1195 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1197 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1201 /* On s390/s390x the first two parameters to clone are switched */
1202 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
1203 r
= seccomp_rule_add_exact(
1205 SCMP_ACT_ERRNO(EPERM
),
1208 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1210 r
= seccomp_rule_add_exact(
1212 SCMP_ACT_ERRNO(EPERM
),
1215 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1217 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1221 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1222 r
= seccomp_rule_add_exact(
1224 SCMP_ACT_ERRNO(EPERM
),
1227 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1229 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1237 r
= seccomp_load(seccomp
);
1238 if (ERRNO_IS_SECCOMP_FATAL(r
))
1241 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1247 int seccomp_protect_sysctl(void) {
1251 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1252 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1254 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1256 if (IN_SET(arch
, SCMP_ARCH_X32
, SCMP_ARCH_AARCH64
))
1257 /* No _sysctl syscall */
1260 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1264 r
= seccomp_rule_add_exact(
1266 SCMP_ACT_ERRNO(EPERM
),
1270 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1274 r
= seccomp_load(seccomp
);
1275 if (ERRNO_IS_SECCOMP_FATAL(r
))
1278 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1284 int seccomp_protect_syslog(void) {
1288 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1289 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1291 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1295 r
= seccomp_rule_add_exact(
1297 SCMP_ACT_ERRNO(EPERM
),
1302 log_debug_errno(r
, "Failed to add syslog() rule for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1306 r
= seccomp_load(seccomp
);
1307 if (ERRNO_IS_SECCOMP_FATAL(r
))
1310 log_debug_errno(r
, "Failed to install syslog protection rules for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1316 int seccomp_restrict_address_families(Set
*address_families
, bool whitelist
) {
1320 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1321 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1325 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1329 case SCMP_ARCH_X86_64
:
1332 case SCMP_ARCH_AARCH64
:
1334 case SCMP_ARCH_PPC64
:
1335 case SCMP_ARCH_PPC64LE
:
1336 case SCMP_ARCH_MIPSEL64N32
:
1337 case SCMP_ARCH_MIPS64N32
:
1338 case SCMP_ARCH_MIPSEL64
:
1339 case SCMP_ARCH_MIPS64
:
1340 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1344 case SCMP_ARCH_S390
:
1345 case SCMP_ARCH_S390X
:
1347 case SCMP_ARCH_MIPSEL
:
1348 case SCMP_ARCH_MIPS
:
1350 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1359 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1364 int af
, first
= 0, last
= 0;
1367 /* If this is a whitelist, we first block the address families that are out of range and then
1368 * everything that is not in the set. First, we find the lowest and highest address family in
1371 SET_FOREACH(afp
, address_families
, i
) {
1372 af
= PTR_TO_INT(afp
);
1374 if (af
<= 0 || af
>= af_max())
1377 if (first
== 0 || af
< first
)
1380 if (last
== 0 || af
> last
)
1384 assert((first
== 0) == (last
== 0));
1388 /* No entries in the valid range, block everything */
1389 r
= seccomp_rule_add_exact(
1391 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1395 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1401 /* Block everything below the first entry */
1402 r
= seccomp_rule_add_exact(
1404 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1407 SCMP_A0(SCMP_CMP_LT
, first
));
1409 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1413 /* Block everything above the last entry */
1414 r
= seccomp_rule_add_exact(
1416 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1419 SCMP_A0(SCMP_CMP_GT
, last
));
1421 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1425 /* Block everything between the first and last entry */
1426 for (af
= 1; af
< af_max(); af
++) {
1428 if (set_contains(address_families
, INT_TO_PTR(af
)))
1431 r
= seccomp_rule_add_exact(
1433 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1436 SCMP_A0(SCMP_CMP_EQ
, af
));
1441 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1449 /* If this is a blacklist, then generate one rule for
1450 * each address family that are then combined in OR
1453 SET_FOREACH(af
, address_families
, i
) {
1455 r
= seccomp_rule_add_exact(
1457 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1460 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1465 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1470 r
= seccomp_load(seccomp
);
1471 if (ERRNO_IS_SECCOMP_FATAL(r
))
1474 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1480 int seccomp_restrict_realtime(void) {
1481 static const int permitted_policies
[] = {
1487 int r
, max_policy
= 0;
1491 /* Determine the highest policy constant we want to allow */
1492 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1493 if (permitted_policies
[i
] > max_policy
)
1494 max_policy
= permitted_policies
[i
];
1496 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1497 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1500 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1502 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1506 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1508 for (p
= 0; p
< max_policy
; p
++) {
1511 /* Check if this is in the whitelist. */
1512 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1513 if (permitted_policies
[i
] == p
) {
1521 /* Deny this policy */
1522 r
= seccomp_rule_add_exact(
1524 SCMP_ACT_ERRNO(EPERM
),
1525 SCMP_SYS(sched_setscheduler
),
1527 SCMP_A1(SCMP_CMP_EQ
, p
));
1529 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1534 /* Blacklist all other policies, i.e. the ones with higher values. Note that all comparisons are
1535 * unsigned here, hence no need no check for < 0 values. */
1536 r
= seccomp_rule_add_exact(
1538 SCMP_ACT_ERRNO(EPERM
),
1539 SCMP_SYS(sched_setscheduler
),
1541 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1543 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1547 r
= seccomp_load(seccomp
);
1548 if (ERRNO_IS_SECCOMP_FATAL(r
))
1551 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1557 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1561 const struct scmp_arg_cmp arg
) {
1564 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1566 _cleanup_free_
char *n
= NULL
;
1568 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1569 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1571 seccomp_arch_to_string(arch
));
1577 /* For known architectures, check that syscalls are indeed defined or not. */
1578 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
1579 assert_cc(SCMP_SYS(shmget
) > 0);
1580 assert_cc(SCMP_SYS(shmat
) > 0);
1581 assert_cc(SCMP_SYS(shmdt
) > 0);
1584 int seccomp_memory_deny_write_execute(void) {
1588 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1589 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1590 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0;
1592 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1596 /* Note that on some architectures shmat() isn't available, and the call is multiplexed through ipc().
1597 * We ignore that here, which means there's still a way to get writable/executable
1598 * memory, if an IPC key is mapped like this. That's a pity, but no total loss. */
1601 case SCMP_ARCH_S390
:
1602 filter_syscall
= SCMP_SYS(mmap2
);
1603 block_syscall
= SCMP_SYS(mmap
);
1604 /* shmat multiplexed, see above */
1608 case SCMP_ARCH_PPC64
:
1609 case SCMP_ARCH_PPC64LE
:
1610 case SCMP_ARCH_S390X
:
1611 filter_syscall
= SCMP_SYS(mmap
);
1612 /* shmat multiplexed, see above */
1616 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1617 shmat_syscall
= SCMP_SYS(shmat
);
1620 case SCMP_ARCH_X86_64
:
1622 case SCMP_ARCH_AARCH64
:
1623 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32 and arm64 have only mmap */
1624 shmat_syscall
= SCMP_SYS(shmat
);
1627 /* Please add more definitions here, if you port systemd to other architectures! */
1629 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__) && !defined(__s390__) && !defined(__s390x__)
1630 #warning "Consider adding the right mmap() syscall definitions here!"
1634 /* Can't filter mmap() on this arch, then skip it */
1635 if (filter_syscall
== 0)
1638 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1642 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1644 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1648 if (block_syscall
!= 0) {
1649 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1654 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1656 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1660 #ifdef __NR_pkey_mprotect
1661 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(pkey_mprotect
),
1663 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1668 if (shmat_syscall
> 0) {
1669 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(shmat
),
1671 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1676 r
= seccomp_load(seccomp
);
1677 if (ERRNO_IS_SECCOMP_FATAL(r
))
1680 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1686 int seccomp_restrict_archs(Set
*archs
) {
1687 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1692 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1695 * There are some qualifications. However the most important use is to stop processes from bypassing
1696 * system call restrictions, in case they used a broader (multiplexing) syscall which is only available
1697 * in a non-native architecture. There are no holes in this use case, at least so far. */
1699 /* Note libseccomp includes our "native" (current) architecture in the filter by default.
1700 * We do not remove it. For example, our callers expect to be able to call execve() afterwards
1701 * to run a program with the restrictions applied. */
1702 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1706 SET_FOREACH(id
, archs
, i
) {
1707 r
= seccomp_arch_add(seccomp
, PTR_TO_UINT32(id
) - 1);
1708 if (r
< 0 && r
!= -EEXIST
)
1712 /* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
1713 * x32 syscalls should basically match x86-64 for everything except the pointer type.
1714 * The important thing is that you can block the old 32-bit x86 syscalls.
1715 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
1717 if (seccomp_arch_native() == SCMP_ARCH_X32
||
1718 set_contains(archs
, UINT32_TO_PTR(SCMP_ARCH_X32
+ 1))) {
1720 r
= seccomp_arch_add(seccomp
, SCMP_ARCH_X86_64
);
1721 if (r
< 0 && r
!= -EEXIST
)
1725 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1729 r
= seccomp_load(seccomp
);
1730 if (ERRNO_IS_SECCOMP_FATAL(r
))
1733 log_debug_errno(r
, "Failed to restrict system call architectures, skipping: %m");
1738 int parse_syscall_archs(char **l
, Set
**archs
) {
1739 _cleanup_set_free_ Set
*_archs
;
1746 r
= set_ensure_allocated(&_archs
, NULL
);
1750 STRV_FOREACH(s
, l
) {
1753 r
= seccomp_arch_from_string(*s
, &a
);
1757 r
= set_put(_archs
, UINT32_TO_PTR(a
+ 1));
1762 *archs
= TAKE_PTR(_archs
);
1767 int seccomp_filter_set_add(Hashmap
*filter
, bool add
, const SyscallFilterSet
*set
) {
1773 NULSTR_FOREACH(i
, set
->value
) {
1776 const SyscallFilterSet
*more
;
1778 more
= syscall_filter_set_find(i
);
1782 r
= seccomp_filter_set_add(filter
, add
, more
);
1788 id
= seccomp_syscall_resolve_name(i
);
1789 if (id
== __NR_SCMP_ERROR
) {
1790 log_debug("Couldn't resolve system call, ignoring: %s", i
);
1795 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(-1));
1799 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1806 int seccomp_lock_personality(unsigned long personality
) {
1810 if (personality
>= PERSONALITY_INVALID
)
1813 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1814 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1816 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1820 r
= seccomp_rule_add_exact(
1822 SCMP_ACT_ERRNO(EPERM
),
1823 SCMP_SYS(personality
),
1825 SCMP_A0(SCMP_CMP_NE
, personality
));
1827 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1831 r
= seccomp_load(seccomp
);
1832 if (ERRNO_IS_SECCOMP_FATAL(r
))
1835 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1841 int seccomp_protect_hostname(void) {
1845 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1846 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1848 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1852 r
= seccomp_rule_add_exact(
1854 SCMP_ACT_ERRNO(EPERM
),
1855 SCMP_SYS(sethostname
),
1858 log_debug_errno(r
, "Failed to add sethostname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1862 r
= seccomp_rule_add_exact(
1864 SCMP_ACT_ERRNO(EPERM
),
1865 SCMP_SYS(setdomainname
),
1868 log_debug_errno(r
, "Failed to add setdomainname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1872 r
= seccomp_load(seccomp
);
1873 if (ERRNO_IS_SECCOMP_FATAL(r
))
1876 log_debug_errno(r
, "Failed to apply hostname restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1882 static int seccomp_restrict_sxid(scmp_filter_ctx seccomp
, mode_t m
) {
1883 /* Checks the mode_t parameter of the following system calls:
1885 * → chmod() + fchmod() + fchmodat()
1886 * → open() + creat() + openat()
1887 * → mkdir() + mkdirat()
1888 * → mknod() + mknodat()
1890 * Returns error if *everything* failed, and 0 otherwise.
1895 r
= seccomp_rule_add_exact(
1897 SCMP_ACT_ERRNO(EPERM
),
1900 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1902 log_debug_errno(r
, "Failed to add filter for chmod: %m");
1906 r
= seccomp_rule_add_exact(
1908 SCMP_ACT_ERRNO(EPERM
),
1911 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1913 log_debug_errno(r
, "Failed to add filter for fchmod: %m");
1917 r
= seccomp_rule_add_exact(
1919 SCMP_ACT_ERRNO(EPERM
),
1922 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1924 log_debug_errno(r
, "Failed to add filter for fchmodat: %m");
1928 r
= seccomp_rule_add_exact(
1930 SCMP_ACT_ERRNO(EPERM
),
1933 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1935 log_debug_errno(r
, "Failed to add filter for mkdir: %m");
1939 r
= seccomp_rule_add_exact(
1941 SCMP_ACT_ERRNO(EPERM
),
1944 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1946 log_debug_errno(r
, "Failed to add filter for mkdirat: %m");
1950 r
= seccomp_rule_add_exact(
1952 SCMP_ACT_ERRNO(EPERM
),
1955 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1957 log_debug_errno(r
, "Failed to add filter for mknod: %m");
1961 r
= seccomp_rule_add_exact(
1963 SCMP_ACT_ERRNO(EPERM
),
1966 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1968 log_debug_errno(r
, "Failed to add filter for mknodat: %m");
1972 #if SCMP_SYS(open) > 0
1973 r
= seccomp_rule_add_exact(
1975 SCMP_ACT_ERRNO(EPERM
),
1978 SCMP_A1(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
1979 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1981 log_debug_errno(r
, "Failed to add filter for open: %m");
1986 r
= seccomp_rule_add_exact(
1988 SCMP_ACT_ERRNO(EPERM
),
1991 SCMP_A2(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
1992 SCMP_A3(SCMP_CMP_MASKED_EQ
, m
, m
));
1994 log_debug_errno(r
, "Failed to add filter for openat: %m");
1998 r
= seccomp_rule_add_exact(
2000 SCMP_ACT_ERRNO(EPERM
),
2003 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2005 log_debug_errno(r
, "Failed to add filter for creat: %m");
2012 int seccomp_restrict_suid_sgid(void) {
2016 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2017 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2019 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2023 r
= seccomp_restrict_sxid(seccomp
, S_ISUID
);
2025 log_debug_errno(r
, "Failed to add suid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2027 k
= seccomp_restrict_sxid(seccomp
, S_ISGID
);
2029 log_debug_errno(r
, "Failed to add sgid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2034 r
= seccomp_load(seccomp
);
2035 if (ERRNO_IS_SECCOMP_FATAL(r
))
2038 log_debug_errno(r
, "Failed to apply suid/sgid restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2044 uint32_t scmp_act_kill_process(void) {
2046 /* Returns SCMP_ACT_KILL_PROCESS if it's supported, and SCMP_ACT_KILL_THREAD otherwise. We never
2047 * actually want to use SCMP_ACT_KILL_THREAD as its semantics are nuts (killing arbitrary threads of
2048 * a program is just a bad idea), but on old kernels/old libseccomp it is all we have, and at least
2049 * for single-threaded apps does the right thing. */
2051 #ifdef SCMP_ACT_KILL_PROCESS
2052 if (seccomp_api_get() >= 3)
2053 return SCMP_ACT_KILL_PROCESS
;
2056 return SCMP_ACT_KILL
; /* same as SCMP_ACT_KILL_THREAD */