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",
285 "getrlimit\0" /* make sure processes can query stack size and such */
307 [SYSCALL_FILTER_SET_AIO
] = {
309 .help
= "Asynchronous IO",
318 [SYSCALL_FILTER_SET_BASIC_IO
] = {
339 [SYSCALL_FILTER_SET_CHOWN
] = {
341 .help
= "Change ownership of files and directories",
351 [SYSCALL_FILTER_SET_CLOCK
] = {
353 .help
= "Change the system time",
361 [SYSCALL_FILTER_SET_CPU_EMULATION
] = {
362 .name
= "@cpu-emulation",
363 .help
= "System calls for CPU emulation functionality",
371 [SYSCALL_FILTER_SET_DEBUG
] = {
373 .help
= "Debugging, performance monitoring and tracing functionality",
379 #ifdef __NR_s390_runtime_instr
380 "s390_runtime_instr\0"
382 "sys_debug_setcontext\0"
384 [SYSCALL_FILTER_SET_FILE_SYSTEM
] = {
385 .name
= "@file-system",
386 .help
= "File system operations",
416 "inotify_add_watch\0"
467 [SYSCALL_FILTER_SET_IO_EVENT
] = {
469 .help
= "Event loop system calls",
486 [SYSCALL_FILTER_SET_IPC
] = {
488 .help
= "SysV IPC, POSIX Message Queues or other IPC",
505 "process_vm_writev\0"
515 [SYSCALL_FILTER_SET_KEYRING
] = {
517 .help
= "Kernel keyring access",
523 [SYSCALL_FILTER_SET_MEMLOCK
] = {
525 .help
= "Memory locking control",
533 [SYSCALL_FILTER_SET_MODULE
] = {
535 .help
= "Loading and unloading of kernel modules",
541 [SYSCALL_FILTER_SET_MOUNT
] = {
543 .help
= "Mounting and unmounting of file systems",
551 [SYSCALL_FILTER_SET_NETWORK_IO
] = {
552 .name
= "@network-io",
553 .help
= "Network or Unix socket IO, should not be needed if not network facing",
577 [SYSCALL_FILTER_SET_OBSOLETE
] = {
578 /* some unknown even to libseccomp */
580 .help
= "Unusual, obsolete or unimplemented system calls",
609 [SYSCALL_FILTER_SET_PRIVILEGED
] = {
610 .name
= "@privileged",
611 .help
= "All system calls which need super-user capabilities",
626 "open_by_handle_at\0"
639 "setuid\0" /* We list the explicit system calls here, as @setuid also includes setgid() which is not necessarily privileged */
643 [SYSCALL_FILTER_SET_PROCESS
] = {
645 .help
= "Process control, execution, namespaceing operations",
648 "capget\0" /* Able to query arbitrary processes */
654 "pidfd_send_signal\0"
657 "rt_tgsigqueueinfo\0"
659 "swapcontext\0" /* Some archs e.g. powerpc32 are using it to do userspace context switches */
669 [SYSCALL_FILTER_SET_RAW_IO
] = {
671 .help
= "Raw I/O port access",
678 #ifdef __NR_s390_pci_mmio_read
679 "s390_pci_mmio_read\0"
681 #ifdef __NR_s390_pci_mmio_write
682 "s390_pci_mmio_write\0"
685 [SYSCALL_FILTER_SET_REBOOT
] = {
687 .help
= "Reboot and reboot preparation/kexec",
693 [SYSCALL_FILTER_SET_RESOURCES
] = {
694 .name
= "@resources",
695 .help
= "Alter resource settings",
702 "sched_setaffinity\0"
705 "sched_setscheduler\0"
710 [SYSCALL_FILTER_SET_SETUID
] = {
712 .help
= "Operations for changing user/group credentials",
729 [SYSCALL_FILTER_SET_SIGNAL
] = {
731 .help
= "Process signal handling",
747 [SYSCALL_FILTER_SET_SWAP
] = {
749 .help
= "Enable/disable swap devices",
754 [SYSCALL_FILTER_SET_SYNC
] = {
756 .help
= "Synchronize files and memory to storage",
765 [SYSCALL_FILTER_SET_SYSTEM_SERVICE
] = {
766 .name
= "@system-service",
767 .help
= "General system service operations",
802 "name_to_handle_at\0"
809 "sched_get_priority_max\0"
810 "sched_get_priority_min\0"
811 "sched_getaffinity\0"
814 "sched_getscheduler\0"
815 "sched_rr_get_interval\0"
833 [SYSCALL_FILTER_SET_TIMER
] = {
835 .help
= "Schedule operations by time",
852 const SyscallFilterSet
*syscall_filter_set_find(const char *name
) {
855 if (isempty(name
) || name
[0] != '@')
858 for (i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++)
859 if (streq(syscall_filter_sets
[i
].name
, name
))
860 return syscall_filter_sets
+ i
;
865 static int seccomp_add_syscall_filter_set(scmp_filter_ctx seccomp
, const SyscallFilterSet
*set
, uint32_t action
, char **exclude
, bool log_missing
);
867 int seccomp_add_syscall_filter_item(scmp_filter_ctx
*seccomp
, const char *name
, uint32_t action
, char **exclude
, bool log_missing
) {
871 if (strv_contains(exclude
, name
))
874 if (name
[0] == '@') {
875 const SyscallFilterSet
*other
;
877 other
= syscall_filter_set_find(name
);
879 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
880 "Filter set %s is not known!",
883 return seccomp_add_syscall_filter_set(seccomp
, other
, action
, exclude
, log_missing
);
888 id
= seccomp_syscall_resolve_name(name
);
889 if (id
== __NR_SCMP_ERROR
) {
891 log_debug("System call %s is not known, ignoring.", name
);
895 r
= seccomp_rule_add_exact(seccomp
, action
, id
, 0);
897 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
898 bool ignore
= r
== -EDOM
;
900 if (!ignore
|| log_missing
)
901 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
902 name
, id
, ignore
? ", ignoring" : "");
911 static int seccomp_add_syscall_filter_set(
912 scmp_filter_ctx seccomp
,
913 const SyscallFilterSet
*set
,
924 NULSTR_FOREACH(sys
, set
->value
) {
925 r
= seccomp_add_syscall_filter_item(seccomp
, sys
, action
, exclude
, log_missing
);
933 int seccomp_load_syscall_filter_set(uint32_t default_action
, const SyscallFilterSet
*set
, uint32_t action
, bool log_missing
) {
939 /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
940 * each local arch. */
942 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
943 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
945 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
947 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
951 r
= seccomp_add_syscall_filter_set(seccomp
, set
, action
, NULL
, log_missing
);
953 return log_debug_errno(r
, "Failed to add filter set: %m");
955 r
= seccomp_load(seccomp
);
956 if (ERRNO_IS_SECCOMP_FATAL(r
))
959 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
965 int seccomp_load_syscall_filter_set_raw(uint32_t default_action
, Hashmap
* set
, uint32_t action
, bool log_missing
) {
969 /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Set* of syscalls, instead of a
970 * SyscallFilterSet* table. */
972 if (hashmap_isempty(set
) && default_action
== SCMP_ACT_ALLOW
)
975 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
976 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
978 void *syscall_id
, *val
;
980 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
982 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
986 HASHMAP_FOREACH_KEY(val
, syscall_id
, set
, i
) {
988 int id
= PTR_TO_INT(syscall_id
) - 1;
989 int error
= PTR_TO_INT(val
);
991 if (action
!= SCMP_ACT_ALLOW
&& error
>= 0)
992 a
= SCMP_ACT_ERRNO(error
);
994 r
= seccomp_rule_add_exact(seccomp
, a
, id
, 0);
996 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
997 _cleanup_free_
char *n
= NULL
;
1000 n
= seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE
, id
);
1001 ignore
= r
== -EDOM
;
1002 if (!ignore
|| log_missing
)
1003 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1004 strna(n
), id
, ignore
? ", ignoring" : "");
1010 r
= seccomp_load(seccomp
);
1011 if (ERRNO_IS_SECCOMP_FATAL(r
))
1014 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1020 int seccomp_parse_syscall_filter(
1024 SeccompParseFlags flags
,
1026 const char *filename
,
1034 if (name
[0] == '@') {
1035 const SyscallFilterSet
*set
;
1038 set
= syscall_filter_set_find(name
);
1040 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1043 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1044 "Unknown system call group, ignoring: %s", name
);
1048 NULSTR_FOREACH(i
, set
->value
) {
1049 /* Call ourselves again, for the group to parse. Note that we downgrade logging here (i.e. take
1050 * away the SECCOMP_PARSE_LOG flag) since any issues in the group table are our own problem,
1051 * not a problem in user configuration data and we shouldn't pretend otherwise by complaining
1053 r
= seccomp_parse_syscall_filter(i
, errno_num
, filter
, flags
&~ SECCOMP_PARSE_LOG
, unit
, filename
, line
);
1060 id
= seccomp_syscall_resolve_name(name
);
1061 if (id
== __NR_SCMP_ERROR
) {
1062 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1065 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1066 "Failed to parse system call, ignoring: %s", name
);
1070 /* If we previously wanted to forbid a syscall and now
1071 * we want to allow it, then remove it from the list. */
1072 if (!(flags
& SECCOMP_PARSE_INVERT
) == !!(flags
& SECCOMP_PARSE_WHITELIST
)) {
1073 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
));
1077 return flags
& SECCOMP_PARSE_LOG
? log_oom() : -ENOMEM
;
1079 assert_se(hashmap_update(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
)) == 0);
1085 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1091 int seccomp_restrict_namespaces(unsigned long retain
) {
1095 if (DEBUG_LOGGING
) {
1096 _cleanup_free_
char *s
= NULL
;
1098 (void) namespace_flags_to_string(retain
, &s
);
1099 log_debug("Restricting namespace to: %s.", strna(s
));
1103 if ((retain
& NAMESPACE_FLAGS_ALL
) == NAMESPACE_FLAGS_ALL
)
1106 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1107 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1110 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1112 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1116 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
1117 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
1119 r
= seccomp_rule_add_exact(
1121 SCMP_ACT_ERRNO(EPERM
),
1125 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
1126 * special invocation with a zero flags argument, right here. */
1127 r
= seccomp_rule_add_exact(
1129 SCMP_ACT_ERRNO(EPERM
),
1132 SCMP_A1(SCMP_CMP_EQ
, 0));
1134 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1138 for (i
= 0; namespace_flag_map
[i
].name
; i
++) {
1141 f
= namespace_flag_map
[i
].flag
;
1142 if ((retain
& f
) == f
) {
1143 log_debug("Permitting %s.", namespace_flag_map
[i
].name
);
1147 log_debug("Blocking %s.", namespace_flag_map
[i
].name
);
1149 r
= seccomp_rule_add_exact(
1151 SCMP_ACT_ERRNO(EPERM
),
1154 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1156 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1160 /* On s390/s390x the first two parameters to clone are switched */
1161 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
1162 r
= seccomp_rule_add_exact(
1164 SCMP_ACT_ERRNO(EPERM
),
1167 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1169 r
= seccomp_rule_add_exact(
1171 SCMP_ACT_ERRNO(EPERM
),
1174 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1176 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1180 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1181 r
= seccomp_rule_add_exact(
1183 SCMP_ACT_ERRNO(EPERM
),
1186 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1188 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1196 r
= seccomp_load(seccomp
);
1197 if (ERRNO_IS_SECCOMP_FATAL(r
))
1200 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1206 int seccomp_protect_sysctl(void) {
1210 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1211 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1213 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1215 if (IN_SET(arch
, SCMP_ARCH_X32
, SCMP_ARCH_AARCH64
))
1216 /* No _sysctl syscall */
1219 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1223 r
= seccomp_rule_add_exact(
1225 SCMP_ACT_ERRNO(EPERM
),
1229 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1233 r
= seccomp_load(seccomp
);
1234 if (ERRNO_IS_SECCOMP_FATAL(r
))
1237 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1243 int seccomp_restrict_address_families(Set
*address_families
, bool whitelist
) {
1247 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1248 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1252 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1256 case SCMP_ARCH_X86_64
:
1259 case SCMP_ARCH_AARCH64
:
1261 case SCMP_ARCH_PPC64
:
1262 case SCMP_ARCH_PPC64LE
:
1263 case SCMP_ARCH_MIPSEL64N32
:
1264 case SCMP_ARCH_MIPS64N32
:
1265 case SCMP_ARCH_MIPSEL64
:
1266 case SCMP_ARCH_MIPS64
:
1267 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1271 case SCMP_ARCH_S390
:
1272 case SCMP_ARCH_S390X
:
1274 case SCMP_ARCH_MIPSEL
:
1275 case SCMP_ARCH_MIPS
:
1277 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1286 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1291 int af
, first
= 0, last
= 0;
1294 /* If this is a whitelist, we first block the address families that are out of range and then
1295 * everything that is not in the set. First, we find the lowest and highest address family in
1298 SET_FOREACH(afp
, address_families
, i
) {
1299 af
= PTR_TO_INT(afp
);
1301 if (af
<= 0 || af
>= af_max())
1304 if (first
== 0 || af
< first
)
1307 if (last
== 0 || af
> last
)
1311 assert((first
== 0) == (last
== 0));
1315 /* No entries in the valid range, block everything */
1316 r
= seccomp_rule_add_exact(
1318 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1322 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1328 /* Block everything below the first entry */
1329 r
= seccomp_rule_add_exact(
1331 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1334 SCMP_A0(SCMP_CMP_LT
, first
));
1336 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1340 /* Block everything above the last entry */
1341 r
= seccomp_rule_add_exact(
1343 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1346 SCMP_A0(SCMP_CMP_GT
, last
));
1348 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1352 /* Block everything between the first and last entry */
1353 for (af
= 1; af
< af_max(); af
++) {
1355 if (set_contains(address_families
, INT_TO_PTR(af
)))
1358 r
= seccomp_rule_add_exact(
1360 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1363 SCMP_A0(SCMP_CMP_EQ
, af
));
1368 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1376 /* If this is a blacklist, then generate one rule for
1377 * each address family that are then combined in OR
1380 SET_FOREACH(af
, address_families
, i
) {
1382 r
= seccomp_rule_add_exact(
1384 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1387 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1392 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1397 r
= seccomp_load(seccomp
);
1398 if (ERRNO_IS_SECCOMP_FATAL(r
))
1401 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1407 int seccomp_restrict_realtime(void) {
1408 static const int permitted_policies
[] = {
1414 int r
, max_policy
= 0;
1418 /* Determine the highest policy constant we want to allow */
1419 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1420 if (permitted_policies
[i
] > max_policy
)
1421 max_policy
= permitted_policies
[i
];
1423 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1424 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1427 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1429 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1433 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1435 for (p
= 0; p
< max_policy
; p
++) {
1438 /* Check if this is in the whitelist. */
1439 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1440 if (permitted_policies
[i
] == p
) {
1448 /* Deny this policy */
1449 r
= seccomp_rule_add_exact(
1451 SCMP_ACT_ERRNO(EPERM
),
1452 SCMP_SYS(sched_setscheduler
),
1454 SCMP_A1(SCMP_CMP_EQ
, p
));
1456 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1461 /* Blacklist all other policies, i.e. the ones with higher values. Note that all comparisons are
1462 * unsigned here, hence no need no check for < 0 values. */
1463 r
= seccomp_rule_add_exact(
1465 SCMP_ACT_ERRNO(EPERM
),
1466 SCMP_SYS(sched_setscheduler
),
1468 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1470 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1474 r
= seccomp_load(seccomp
);
1475 if (ERRNO_IS_SECCOMP_FATAL(r
))
1478 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1484 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1488 const struct scmp_arg_cmp arg
) {
1491 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1493 _cleanup_free_
char *n
= NULL
;
1495 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1496 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1498 seccomp_arch_to_string(arch
));
1504 /* For known architectures, check that syscalls are indeed defined or not. */
1505 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
1506 assert_cc(SCMP_SYS(shmget
) > 0);
1507 assert_cc(SCMP_SYS(shmat
) > 0);
1508 assert_cc(SCMP_SYS(shmdt
) > 0);
1511 int seccomp_memory_deny_write_execute(void) {
1515 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1516 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1517 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0;
1519 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1524 filter_syscall
= SCMP_SYS(mmap2
);
1525 block_syscall
= SCMP_SYS(mmap
);
1526 shmat_syscall
= SCMP_SYS(shmat
);
1530 case SCMP_ARCH_PPC64
:
1531 case SCMP_ARCH_PPC64LE
:
1532 filter_syscall
= SCMP_SYS(mmap
);
1534 /* Note that shmat() isn't available, and the call is multiplexed through ipc().
1535 * We ignore that here, which means there's still a way to get writable/executable
1536 * memory, if an IPC key is mapped like this. That's a pity, but no total loss. */
1541 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1542 shmat_syscall
= SCMP_SYS(shmat
);
1545 case SCMP_ARCH_X86_64
:
1547 case SCMP_ARCH_AARCH64
:
1548 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32, and arm64 have only mmap */
1549 shmat_syscall
= SCMP_SYS(shmat
);
1552 /* Please add more definitions here, if you port systemd to other architectures! */
1554 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__)
1555 #warning "Consider adding the right mmap() syscall definitions here!"
1559 /* Can't filter mmap() on this arch, then skip it */
1560 if (filter_syscall
== 0)
1563 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1567 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1569 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1573 if (block_syscall
!= 0) {
1574 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1579 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1581 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1585 #ifdef __NR_pkey_mprotect
1586 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(pkey_mprotect
),
1588 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1593 if (shmat_syscall
> 0) {
1594 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(shmat
),
1596 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1601 r
= seccomp_load(seccomp
);
1602 if (ERRNO_IS_SECCOMP_FATAL(r
))
1605 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1611 int seccomp_restrict_archs(Set
*archs
) {
1612 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1617 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1620 * There are some qualifications. However the most important use is to stop processes from bypassing
1621 * system call restrictions, in case they used a broader (multiplexing) syscall which is only available
1622 * in a non-native architecture. There are no holes in this use case, at least so far. */
1624 /* Note libseccomp includes our "native" (current) architecture in the filter by default.
1625 * We do not remove it. For example, our callers expect to be able to call execve() afterwards
1626 * to run a program with the restrictions applied. */
1627 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1631 SET_FOREACH(id
, archs
, i
) {
1632 r
= seccomp_arch_add(seccomp
, PTR_TO_UINT32(id
) - 1);
1633 if (r
< 0 && r
!= -EEXIST
)
1637 /* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
1638 * x32 syscalls should basically match x86-64 for everything except the pointer type.
1639 * The important thing is that you can block the old 32-bit x86 syscalls.
1640 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
1642 if (seccomp_arch_native() == SCMP_ARCH_X32
||
1643 set_contains(archs
, UINT32_TO_PTR(SCMP_ARCH_X32
+ 1))) {
1645 r
= seccomp_arch_add(seccomp
, SCMP_ARCH_X86_64
);
1646 if (r
< 0 && r
!= -EEXIST
)
1650 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1654 r
= seccomp_load(seccomp
);
1655 if (ERRNO_IS_SECCOMP_FATAL(r
))
1658 log_debug_errno(r
, "Failed to restrict system call architectures, skipping: %m");
1663 int parse_syscall_archs(char **l
, Set
**archs
) {
1664 _cleanup_set_free_ Set
*_archs
;
1671 r
= set_ensure_allocated(&_archs
, NULL
);
1675 STRV_FOREACH(s
, l
) {
1678 r
= seccomp_arch_from_string(*s
, &a
);
1682 r
= set_put(_archs
, UINT32_TO_PTR(a
+ 1));
1687 *archs
= TAKE_PTR(_archs
);
1692 int seccomp_filter_set_add(Hashmap
*filter
, bool add
, const SyscallFilterSet
*set
) {
1698 NULSTR_FOREACH(i
, set
->value
) {
1701 const SyscallFilterSet
*more
;
1703 more
= syscall_filter_set_find(i
);
1707 r
= seccomp_filter_set_add(filter
, add
, more
);
1713 id
= seccomp_syscall_resolve_name(i
);
1714 if (id
== __NR_SCMP_ERROR
) {
1715 log_debug("Couldn't resolve system call, ignoring: %s", i
);
1720 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(-1));
1724 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1731 int seccomp_lock_personality(unsigned long personality
) {
1735 if (personality
>= PERSONALITY_INVALID
)
1738 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1739 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1741 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1745 r
= seccomp_rule_add_exact(
1747 SCMP_ACT_ERRNO(EPERM
),
1748 SCMP_SYS(personality
),
1750 SCMP_A0(SCMP_CMP_NE
, personality
));
1752 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1756 r
= seccomp_load(seccomp
);
1757 if (ERRNO_IS_SECCOMP_FATAL(r
))
1760 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1766 int seccomp_protect_hostname(void) {
1770 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1771 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1773 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1777 r
= seccomp_rule_add_exact(
1779 SCMP_ACT_ERRNO(EPERM
),
1780 SCMP_SYS(sethostname
),
1783 log_debug_errno(r
, "Failed to add sethostname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1787 r
= seccomp_rule_add_exact(
1789 SCMP_ACT_ERRNO(EPERM
),
1790 SCMP_SYS(setdomainname
),
1793 log_debug_errno(r
, "Failed to add setdomainname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1797 r
= seccomp_load(seccomp
);
1798 if (ERRNO_IS_SECCOMP_FATAL(r
))
1801 log_debug_errno(r
, "Failed to apply hostname restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1807 static int seccomp_restrict_sxid(scmp_filter_ctx seccomp
, mode_t m
) {
1808 /* Checks the mode_t parameter of the following system calls:
1810 * → chmod() + fchmod() + fchmodat()
1811 * → open() + creat() + openat()
1812 * → mkdir() + mkdirat()
1813 * → mknod() + mknodat()
1815 * Returns error if *everything* failed, and 0 otherwise.
1820 r
= seccomp_rule_add_exact(
1822 SCMP_ACT_ERRNO(EPERM
),
1825 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1827 log_debug_errno(r
, "Failed to add filter for chmod: %m");
1831 r
= seccomp_rule_add_exact(
1833 SCMP_ACT_ERRNO(EPERM
),
1836 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1838 log_debug_errno(r
, "Failed to add filter for fchmod: %m");
1842 r
= seccomp_rule_add_exact(
1844 SCMP_ACT_ERRNO(EPERM
),
1847 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1849 log_debug_errno(r
, "Failed to add filter for fchmodat: %m");
1853 r
= seccomp_rule_add_exact(
1855 SCMP_ACT_ERRNO(EPERM
),
1858 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1860 log_debug_errno(r
, "Failed to add filter for mkdir: %m");
1864 r
= seccomp_rule_add_exact(
1866 SCMP_ACT_ERRNO(EPERM
),
1869 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1871 log_debug_errno(r
, "Failed to add filter for mkdirat: %m");
1875 r
= seccomp_rule_add_exact(
1877 SCMP_ACT_ERRNO(EPERM
),
1880 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1882 log_debug_errno(r
, "Failed to add filter for mknod: %m");
1886 r
= seccomp_rule_add_exact(
1888 SCMP_ACT_ERRNO(EPERM
),
1891 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1893 log_debug_errno(r
, "Failed to add filter for mknodat: %m");
1897 #if SCMP_SYS(open) > 0
1898 r
= seccomp_rule_add_exact(
1900 SCMP_ACT_ERRNO(EPERM
),
1903 SCMP_A1(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
1904 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1906 log_debug_errno(r
, "Failed to add filter for open: %m");
1911 r
= seccomp_rule_add_exact(
1913 SCMP_ACT_ERRNO(EPERM
),
1916 SCMP_A2(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
1917 SCMP_A3(SCMP_CMP_MASKED_EQ
, m
, m
));
1919 log_debug_errno(r
, "Failed to add filter for openat: %m");
1923 r
= seccomp_rule_add_exact(
1925 SCMP_ACT_ERRNO(EPERM
),
1928 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1930 log_debug_errno(r
, "Failed to add filter for creat: %m");
1937 int seccomp_restrict_suid_sgid(void) {
1941 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1942 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1944 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1948 r
= seccomp_restrict_sxid(seccomp
, S_ISUID
);
1950 log_debug_errno(r
, "Failed to add suid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
1952 k
= seccomp_restrict_sxid(seccomp
, S_ISGID
);
1954 log_debug_errno(r
, "Failed to add sgid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
1959 r
= seccomp_load(seccomp
);
1960 if (ERRNO_IS_SECCOMP_FATAL(r
))
1963 log_debug_errno(r
, "Failed to apply suid/sgid restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1969 uint32_t scmp_act_kill_process(void) {
1971 /* Returns SCMP_ACT_KILL_PROCESS if it's supported, and SCMP_ACT_KILL_THREAD otherwise. We never
1972 * actually want to use SCMP_ACT_KILL_THREAD as its semantics are nuts (killing arbitrary threads of
1973 * a program is just a bad idea), but on old kernels/old libseccomp it is all we have, and at least
1974 * for single-threaded apps does the right thing. */
1976 #ifdef SCMP_ACT_KILL_PROCESS
1977 if (seccomp_api_get() >= 3)
1978 return SCMP_ACT_KILL_PROCESS
;
1981 return SCMP_ACT_KILL
; /* same as SCMP_ACT_KILL_THREAD */