1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/seccomp.h>
12 #include "alloc-util.h"
13 #include "errno-list.h"
16 #include "nulstr-util.h"
17 #include "process-util.h"
18 #include "seccomp-util.h"
20 #include "string-util.h"
23 const uint32_t seccomp_local_archs
[] = {
25 /* 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 */
27 #if defined(__x86_64__) && defined(__ILP32__)
30 SCMP_ARCH_X32
, /* native */
31 #elif defined(__x86_64__) && !defined(__ILP32__)
34 SCMP_ARCH_X86_64
, /* native */
35 #elif defined(__i386__)
37 #elif defined(__aarch64__)
39 SCMP_ARCH_AARCH64
, /* native */
40 #elif defined(__arm__)
42 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
44 SCMP_ARCH_MIPS
, /* native */
45 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
47 SCMP_ARCH_MIPSEL
, /* native */
48 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
51 SCMP_ARCH_MIPSEL64N32
,
54 SCMP_ARCH_MIPS64
, /* native */
55 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
59 SCMP_ARCH_MIPSEL64N32
,
61 SCMP_ARCH_MIPSEL64
, /* native */
62 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
67 SCMP_ARCH_MIPSEL64N32
,
68 SCMP_ARCH_MIPS64N32
, /* native */
69 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
75 SCMP_ARCH_MIPSEL64N32
, /* native */
76 #elif defined(__powerpc64__) && __BYTE_ORDER == __BIG_ENDIAN
79 SCMP_ARCH_PPC64
, /* native */
80 #elif defined(__powerpc64__) && __BYTE_ORDER == __LITTLE_ENDIAN
83 SCMP_ARCH_PPC64LE
, /* native */
84 #elif defined(__powerpc__)
86 #elif defined(__s390x__)
88 SCMP_ARCH_S390X
, /* native */
89 #elif defined(__s390__)
95 const char* seccomp_arch_to_string(uint32_t c
) {
96 /* Maintain order used in <seccomp.h>.
98 * Names used here should be the same as those used for ConditionArchitecture=,
99 * except for "subarchitectures" like x32. */
102 case SCMP_ARCH_NATIVE
:
106 case SCMP_ARCH_X86_64
:
112 case SCMP_ARCH_AARCH64
:
116 case SCMP_ARCH_MIPS64
:
118 case SCMP_ARCH_MIPS64N32
:
120 case SCMP_ARCH_MIPSEL
:
122 case SCMP_ARCH_MIPSEL64
:
124 case SCMP_ARCH_MIPSEL64N32
:
125 return "mips64-le-n32";
128 case SCMP_ARCH_PPC64
:
130 case SCMP_ARCH_PPC64LE
:
134 case SCMP_ARCH_S390X
:
141 int seccomp_arch_from_string(const char *n
, uint32_t *ret
) {
147 if (streq(n
, "native"))
148 *ret
= SCMP_ARCH_NATIVE
;
149 else if (streq(n
, "x86"))
150 *ret
= SCMP_ARCH_X86
;
151 else if (streq(n
, "x86-64"))
152 *ret
= SCMP_ARCH_X86_64
;
153 else if (streq(n
, "x32"))
154 *ret
= SCMP_ARCH_X32
;
155 else if (streq(n
, "arm"))
156 *ret
= SCMP_ARCH_ARM
;
157 else if (streq(n
, "arm64"))
158 *ret
= SCMP_ARCH_AARCH64
;
159 else if (streq(n
, "mips"))
160 *ret
= SCMP_ARCH_MIPS
;
161 else if (streq(n
, "mips64"))
162 *ret
= SCMP_ARCH_MIPS64
;
163 else if (streq(n
, "mips64-n32"))
164 *ret
= SCMP_ARCH_MIPS64N32
;
165 else if (streq(n
, "mips-le"))
166 *ret
= SCMP_ARCH_MIPSEL
;
167 else if (streq(n
, "mips64-le"))
168 *ret
= SCMP_ARCH_MIPSEL64
;
169 else if (streq(n
, "mips64-le-n32"))
170 *ret
= SCMP_ARCH_MIPSEL64N32
;
171 else if (streq(n
, "ppc"))
172 *ret
= SCMP_ARCH_PPC
;
173 else if (streq(n
, "ppc64"))
174 *ret
= SCMP_ARCH_PPC64
;
175 else if (streq(n
, "ppc64-le"))
176 *ret
= SCMP_ARCH_PPC64LE
;
177 else if (streq(n
, "s390"))
178 *ret
= SCMP_ARCH_S390
;
179 else if (streq(n
, "s390x"))
180 *ret
= SCMP_ARCH_S390X
;
187 int seccomp_init_for_arch(scmp_filter_ctx
*ret
, uint32_t arch
, uint32_t default_action
) {
188 scmp_filter_ctx seccomp
;
191 /* Much like seccomp_init(), but initializes the filter for one specific architecture only, without affecting
192 * any others. Also, turns off the NNP fiddling. */
194 seccomp
= seccomp_init(default_action
);
198 if (arch
!= SCMP_ARCH_NATIVE
&&
199 arch
!= seccomp_arch_native()) {
201 r
= seccomp_arch_remove(seccomp
, seccomp_arch_native());
205 r
= seccomp_arch_add(seccomp
, arch
);
209 assert(seccomp_arch_exist(seccomp
, arch
) >= 0);
210 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) == -EEXIST
);
211 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) == -EEXIST
);
213 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) >= 0);
214 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) >= 0);
217 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_ACT_BADARCH
, SCMP_ACT_ALLOW
);
221 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
229 seccomp_release(seccomp
);
233 static bool is_basic_seccomp_available(void) {
234 return prctl(PR_GET_SECCOMP
, 0, 0, 0, 0) >= 0;
237 static bool is_seccomp_filter_available(void) {
238 return prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, 0, 0) < 0 &&
242 bool is_seccomp_available(void) {
243 static int cached_enabled
= -1;
245 if (cached_enabled
< 0)
247 is_basic_seccomp_available() &&
248 is_seccomp_filter_available();
250 return cached_enabled
;
253 const SyscallFilterSet syscall_filter_sets
[_SYSCALL_FILTER_SET_MAX
] = {
254 [SYSCALL_FILTER_SET_DEFAULT
] = {
256 .help
= "System calls that are always permitted",
283 "getrlimit\0" /* make sure processes can query stack size and such */
305 [SYSCALL_FILTER_SET_AIO
] = {
307 .help
= "Asynchronous IO",
316 [SYSCALL_FILTER_SET_BASIC_IO
] = {
337 [SYSCALL_FILTER_SET_CHOWN
] = {
339 .help
= "Change ownership of files and directories",
349 [SYSCALL_FILTER_SET_CLOCK
] = {
351 .help
= "Change the system time",
359 [SYSCALL_FILTER_SET_CPU_EMULATION
] = {
360 .name
= "@cpu-emulation",
361 .help
= "System calls for CPU emulation functionality",
369 [SYSCALL_FILTER_SET_DEBUG
] = {
371 .help
= "Debugging, performance monitoring and tracing functionality",
377 #ifdef __NR_s390_runtime_instr
378 "s390_runtime_instr\0"
380 "sys_debug_setcontext\0"
382 [SYSCALL_FILTER_SET_FILE_SYSTEM
] = {
383 .name
= "@file-system",
384 .help
= "File system operations",
414 "inotify_add_watch\0"
465 [SYSCALL_FILTER_SET_IO_EVENT
] = {
467 .help
= "Event loop system calls",
484 [SYSCALL_FILTER_SET_IPC
] = {
486 .help
= "SysV IPC, POSIX Message Queues or other IPC",
503 "process_vm_writev\0"
513 [SYSCALL_FILTER_SET_KEYRING
] = {
515 .help
= "Kernel keyring access",
521 [SYSCALL_FILTER_SET_MEMLOCK
] = {
523 .help
= "Memory locking control",
531 [SYSCALL_FILTER_SET_MODULE
] = {
533 .help
= "Loading and unloading of kernel modules",
539 [SYSCALL_FILTER_SET_MOUNT
] = {
541 .help
= "Mounting and unmounting of file systems",
549 [SYSCALL_FILTER_SET_NETWORK_IO
] = {
550 .name
= "@network-io",
551 .help
= "Network or Unix socket IO, should not be needed if not network facing",
575 [SYSCALL_FILTER_SET_OBSOLETE
] = {
576 /* some unknown even to libseccomp */
578 .help
= "Unusual, obsolete or unimplemented system calls",
607 [SYSCALL_FILTER_SET_PRIVILEGED
] = {
608 .name
= "@privileged",
609 .help
= "All system calls which need super-user capabilities",
624 "open_by_handle_at\0"
637 "setuid\0" /* We list the explicit system calls here, as @setuid also includes setgid() which is not necessarily privileged */
641 [SYSCALL_FILTER_SET_PROCESS
] = {
643 .help
= "Process control, execution, namespaceing operations",
646 "capget\0" /* Able to query arbitrary processes */
654 "rt_tgsigqueueinfo\0"
656 "swapcontext\0" /* Some archs e.g. powerpc32 are using it to do userspace context switches */
666 [SYSCALL_FILTER_SET_RAW_IO
] = {
668 .help
= "Raw I/O port access",
675 #ifdef __NR_s390_pci_mmio_read
676 "s390_pci_mmio_read\0"
678 #ifdef __NR_s390_pci_mmio_write
679 "s390_pci_mmio_write\0"
682 [SYSCALL_FILTER_SET_REBOOT
] = {
684 .help
= "Reboot and reboot preparation/kexec",
690 [SYSCALL_FILTER_SET_RESOURCES
] = {
691 .name
= "@resources",
692 .help
= "Alter resource settings",
699 "sched_setaffinity\0"
702 "sched_setscheduler\0"
707 [SYSCALL_FILTER_SET_SETUID
] = {
709 .help
= "Operations for changing user/group credentials",
726 [SYSCALL_FILTER_SET_SIGNAL
] = {
728 .help
= "Process signal handling",
744 [SYSCALL_FILTER_SET_SWAP
] = {
746 .help
= "Enable/disable swap devices",
751 [SYSCALL_FILTER_SET_SYNC
] = {
753 .help
= "Synchronize files and memory to storage",
762 [SYSCALL_FILTER_SET_SYSTEM_SERVICE
] = {
763 .name
= "@system-service",
764 .help
= "General system service operations",
799 "name_to_handle_at\0"
806 "sched_get_priority_max\0"
807 "sched_get_priority_min\0"
808 "sched_getaffinity\0"
811 "sched_getscheduler\0"
812 "sched_rr_get_interval\0"
830 [SYSCALL_FILTER_SET_TIMER
] = {
832 .help
= "Schedule operations by time",
849 const SyscallFilterSet
*syscall_filter_set_find(const char *name
) {
852 if (isempty(name
) || name
[0] != '@')
855 for (i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++)
856 if (streq(syscall_filter_sets
[i
].name
, name
))
857 return syscall_filter_sets
+ i
;
862 static int seccomp_add_syscall_filter_set(scmp_filter_ctx seccomp
, const SyscallFilterSet
*set
, uint32_t action
, char **exclude
, bool log_missing
);
864 int seccomp_add_syscall_filter_item(scmp_filter_ctx
*seccomp
, const char *name
, uint32_t action
, char **exclude
, bool log_missing
) {
868 if (strv_contains(exclude
, name
))
871 if (name
[0] == '@') {
872 const SyscallFilterSet
*other
;
874 other
= syscall_filter_set_find(name
);
876 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
877 "Filter set %s is not known!",
880 return seccomp_add_syscall_filter_set(seccomp
, other
, action
, exclude
, log_missing
);
885 id
= seccomp_syscall_resolve_name(name
);
886 if (id
== __NR_SCMP_ERROR
) {
888 log_debug("System call %s is not known, ignoring.", name
);
892 r
= seccomp_rule_add_exact(seccomp
, action
, id
, 0);
894 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
895 bool ignore
= r
== -EDOM
;
897 if (!ignore
|| log_missing
)
898 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
899 name
, id
, ignore
? ", ignoring" : "");
908 static int seccomp_add_syscall_filter_set(
909 scmp_filter_ctx seccomp
,
910 const SyscallFilterSet
*set
,
921 NULSTR_FOREACH(sys
, set
->value
) {
922 r
= seccomp_add_syscall_filter_item(seccomp
, sys
, action
, exclude
, log_missing
);
930 int seccomp_load_syscall_filter_set(uint32_t default_action
, const SyscallFilterSet
*set
, uint32_t action
, bool log_missing
) {
936 /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
937 * each local arch. */
939 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
940 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
942 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
944 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
948 r
= seccomp_add_syscall_filter_set(seccomp
, set
, action
, NULL
, log_missing
);
950 return log_debug_errno(r
, "Failed to add filter set: %m");
952 r
= seccomp_load(seccomp
);
953 if (IN_SET(r
, -EPERM
, -EACCES
))
956 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
962 int seccomp_load_syscall_filter_set_raw(uint32_t default_action
, Hashmap
* set
, uint32_t action
, bool log_missing
) {
966 /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Set* of syscalls, instead of a
967 * SyscallFilterSet* table. */
969 if (hashmap_isempty(set
) && default_action
== SCMP_ACT_ALLOW
)
972 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
973 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
975 void *syscall_id
, *val
;
977 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
979 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
983 HASHMAP_FOREACH_KEY(val
, syscall_id
, set
, i
) {
985 int id
= PTR_TO_INT(syscall_id
) - 1;
986 int error
= PTR_TO_INT(val
);
988 if (action
!= SCMP_ACT_ALLOW
&& error
>= 0)
989 a
= SCMP_ACT_ERRNO(error
);
991 r
= seccomp_rule_add_exact(seccomp
, a
, id
, 0);
993 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
994 _cleanup_free_
char *n
= NULL
;
997 n
= seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE
, id
);
999 if (!ignore
|| log_missing
)
1000 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1001 strna(n
), id
, ignore
? ", ignoring" : "");
1007 r
= seccomp_load(seccomp
);
1008 if (IN_SET(r
, -EPERM
, -EACCES
))
1011 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1017 int seccomp_parse_syscall_filter_full(
1021 SeccompParseFlags flags
,
1023 const char *filename
,
1031 if (name
[0] == '@') {
1032 const SyscallFilterSet
*set
;
1035 set
= syscall_filter_set_find(name
);
1037 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1040 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1041 "Unknown system call group, ignoring: %s", name
);
1045 NULSTR_FOREACH(i
, set
->value
) {
1046 /* Call ourselves again, for the group to parse. Note that we downgrade logging here (i.e. take
1047 * away the SECCOMP_PARSE_LOG flag) since any issues in the group table are our own problem,
1048 * not a problem in user configuration data and we shouldn't pretend otherwise by complaining
1050 r
= seccomp_parse_syscall_filter_full(i
, errno_num
, filter
, flags
&~ SECCOMP_PARSE_LOG
, unit
, filename
, line
);
1057 id
= seccomp_syscall_resolve_name(name
);
1058 if (id
== __NR_SCMP_ERROR
) {
1059 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1062 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1063 "Failed to parse system call, ignoring: %s", name
);
1067 /* If we previously wanted to forbid a syscall and now
1068 * we want to allow it, then remove it from the list. */
1069 if (!(flags
& SECCOMP_PARSE_INVERT
) == !!(flags
& SECCOMP_PARSE_WHITELIST
)) {
1070 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
));
1074 return flags
& SECCOMP_PARSE_LOG
? log_oom() : -ENOMEM
;
1076 assert_se(hashmap_update(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
)) == 0);
1082 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1088 int seccomp_restrict_namespaces(unsigned long retain
) {
1092 if (DEBUG_LOGGING
) {
1093 _cleanup_free_
char *s
= NULL
;
1095 (void) namespace_flags_to_string(retain
, &s
);
1096 log_debug("Restricting namespace to: %s.", strna(s
));
1100 if ((retain
& NAMESPACE_FLAGS_ALL
) == NAMESPACE_FLAGS_ALL
)
1103 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1104 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1107 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1109 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1113 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
1114 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
1116 r
= seccomp_rule_add_exact(
1118 SCMP_ACT_ERRNO(EPERM
),
1122 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
1123 * special invocation with a zero flags argument, right here. */
1124 r
= seccomp_rule_add_exact(
1126 SCMP_ACT_ERRNO(EPERM
),
1129 SCMP_A1(SCMP_CMP_EQ
, 0));
1131 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1135 for (i
= 0; namespace_flag_map
[i
].name
; i
++) {
1138 f
= namespace_flag_map
[i
].flag
;
1139 if ((retain
& f
) == f
) {
1140 log_debug("Permitting %s.", namespace_flag_map
[i
].name
);
1144 log_debug("Blocking %s.", namespace_flag_map
[i
].name
);
1146 r
= seccomp_rule_add_exact(
1148 SCMP_ACT_ERRNO(EPERM
),
1151 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1153 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1157 /* On s390/s390x the first two parameters to clone are switched */
1158 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
1159 r
= seccomp_rule_add_exact(
1161 SCMP_ACT_ERRNO(EPERM
),
1164 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1166 r
= seccomp_rule_add_exact(
1168 SCMP_ACT_ERRNO(EPERM
),
1171 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1173 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1177 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1178 r
= seccomp_rule_add_exact(
1180 SCMP_ACT_ERRNO(EPERM
),
1183 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1185 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1193 r
= seccomp_load(seccomp
);
1194 if (IN_SET(r
, -EPERM
, -EACCES
))
1197 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1203 int seccomp_protect_sysctl(void) {
1207 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1208 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1210 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1212 if (IN_SET(arch
, SCMP_ARCH_X32
, SCMP_ARCH_AARCH64
))
1213 /* No _sysctl syscall */
1216 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1220 r
= seccomp_rule_add_exact(
1222 SCMP_ACT_ERRNO(EPERM
),
1226 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1230 r
= seccomp_load(seccomp
);
1231 if (IN_SET(r
, -EPERM
, -EACCES
))
1234 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1240 int seccomp_restrict_address_families(Set
*address_families
, bool whitelist
) {
1244 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1245 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1249 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1253 case SCMP_ARCH_X86_64
:
1256 case SCMP_ARCH_AARCH64
:
1258 case SCMP_ARCH_PPC64
:
1259 case SCMP_ARCH_PPC64LE
:
1260 case SCMP_ARCH_MIPSEL64N32
:
1261 case SCMP_ARCH_MIPS64N32
:
1262 case SCMP_ARCH_MIPSEL64
:
1263 case SCMP_ARCH_MIPS64
:
1264 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1268 case SCMP_ARCH_S390
:
1269 case SCMP_ARCH_S390X
:
1271 case SCMP_ARCH_MIPSEL
:
1272 case SCMP_ARCH_MIPS
:
1274 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1283 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1288 int af
, first
= 0, last
= 0;
1291 /* If this is a whitelist, we first block the address families that are out of range and then
1292 * everything that is not in the set. First, we find the lowest and highest address family in
1295 SET_FOREACH(afp
, address_families
, i
) {
1296 af
= PTR_TO_INT(afp
);
1298 if (af
<= 0 || af
>= af_max())
1301 if (first
== 0 || af
< first
)
1304 if (last
== 0 || af
> last
)
1308 assert((first
== 0) == (last
== 0));
1312 /* No entries in the valid range, block everything */
1313 r
= seccomp_rule_add_exact(
1315 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1319 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1325 /* Block everything below the first entry */
1326 r
= seccomp_rule_add_exact(
1328 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1331 SCMP_A0(SCMP_CMP_LT
, first
));
1333 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1337 /* Block everything above the last entry */
1338 r
= seccomp_rule_add_exact(
1340 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1343 SCMP_A0(SCMP_CMP_GT
, last
));
1345 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1349 /* Block everything between the first and last entry */
1350 for (af
= 1; af
< af_max(); af
++) {
1352 if (set_contains(address_families
, INT_TO_PTR(af
)))
1355 r
= seccomp_rule_add_exact(
1357 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1360 SCMP_A0(SCMP_CMP_EQ
, af
));
1365 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1373 /* If this is a blacklist, then generate one rule for
1374 * each address family that are then combined in OR
1377 SET_FOREACH(af
, address_families
, i
) {
1379 r
= seccomp_rule_add_exact(
1381 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1384 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1389 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1394 r
= seccomp_load(seccomp
);
1395 if (IN_SET(r
, -EPERM
, -EACCES
))
1398 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1404 int seccomp_restrict_realtime(void) {
1405 static const int permitted_policies
[] = {
1411 int r
, max_policy
= 0;
1415 /* Determine the highest policy constant we want to allow */
1416 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1417 if (permitted_policies
[i
] > max_policy
)
1418 max_policy
= permitted_policies
[i
];
1420 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1421 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1424 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1426 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1430 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1432 for (p
= 0; p
< max_policy
; p
++) {
1435 /* Check if this is in the whitelist. */
1436 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1437 if (permitted_policies
[i
] == p
) {
1445 /* Deny this policy */
1446 r
= seccomp_rule_add_exact(
1448 SCMP_ACT_ERRNO(EPERM
),
1449 SCMP_SYS(sched_setscheduler
),
1451 SCMP_A1(SCMP_CMP_EQ
, p
));
1453 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1458 /* Blacklist all other policies, i.e. the ones with higher values. Note that all comparisons are
1459 * unsigned here, hence no need no check for < 0 values. */
1460 r
= seccomp_rule_add_exact(
1462 SCMP_ACT_ERRNO(EPERM
),
1463 SCMP_SYS(sched_setscheduler
),
1465 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1467 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1471 r
= seccomp_load(seccomp
);
1472 if (IN_SET(r
, -EPERM
, -EACCES
))
1475 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1481 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1485 const struct scmp_arg_cmp arg
) {
1488 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1490 _cleanup_free_
char *n
= NULL
;
1492 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1493 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1495 seccomp_arch_to_string(arch
));
1501 /* For known architectures, check that syscalls are indeed defined or not. */
1502 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
1503 assert_cc(SCMP_SYS(shmget
) > 0);
1504 assert_cc(SCMP_SYS(shmat
) > 0);
1505 assert_cc(SCMP_SYS(shmdt
) > 0);
1508 int seccomp_memory_deny_write_execute(void) {
1512 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1513 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1514 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0;
1516 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1521 filter_syscall
= SCMP_SYS(mmap2
);
1522 block_syscall
= SCMP_SYS(mmap
);
1523 shmat_syscall
= SCMP_SYS(shmat
);
1527 case SCMP_ARCH_PPC64
:
1528 case SCMP_ARCH_PPC64LE
:
1529 filter_syscall
= SCMP_SYS(mmap
);
1531 /* Note that shmat() isn't available, and the call is multiplexed through ipc().
1532 * We ignore that here, which means there's still a way to get writable/executable
1533 * memory, if an IPC key is mapped like this. That's a pity, but no total loss. */
1538 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1539 shmat_syscall
= SCMP_SYS(shmat
);
1542 case SCMP_ARCH_X86_64
:
1544 case SCMP_ARCH_AARCH64
:
1545 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32, and arm64 have only mmap */
1546 shmat_syscall
= SCMP_SYS(shmat
);
1549 /* Please add more definitions here, if you port systemd to other architectures! */
1551 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__)
1552 #warning "Consider adding the right mmap() syscall definitions here!"
1556 /* Can't filter mmap() on this arch, then skip it */
1557 if (filter_syscall
== 0)
1560 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1564 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1566 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1570 if (block_syscall
!= 0) {
1571 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1576 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1578 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1582 #ifdef __NR_pkey_mprotect
1583 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(pkey_mprotect
),
1585 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1590 if (shmat_syscall
> 0) {
1591 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(shmat
),
1593 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1598 r
= seccomp_load(seccomp
);
1599 if (IN_SET(r
, -EPERM
, -EACCES
))
1602 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1608 int seccomp_restrict_archs(Set
*archs
) {
1609 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1614 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1617 * There are some qualifications. However the most important use is to stop processes from bypassing
1618 * system call restrictions, in case they used a broader (multiplexing) syscall which is only available
1619 * in a non-native architecture. There are no holes in this use case, at least so far. */
1621 /* Note libseccomp includes our "native" (current) architecture in the filter by default.
1622 * We do not remove it. For example, our callers expect to be able to call execve() afterwards
1623 * to run a program with the restrictions applied. */
1624 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1628 SET_FOREACH(id
, archs
, i
) {
1629 r
= seccomp_arch_add(seccomp
, PTR_TO_UINT32(id
) - 1);
1630 if (r
< 0 && r
!= -EEXIST
)
1634 /* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
1635 * x32 syscalls should basically match x86-64 for everything except the pointer type.
1636 * The important thing is that you can block the old 32-bit x86 syscalls.
1637 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
1639 if (seccomp_arch_native() == SCMP_ARCH_X32
||
1640 set_contains(archs
, UINT32_TO_PTR(SCMP_ARCH_X32
+ 1))) {
1642 r
= seccomp_arch_add(seccomp
, SCMP_ARCH_X86_64
);
1643 if (r
< 0 && r
!= -EEXIST
)
1647 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1651 r
= seccomp_load(seccomp
);
1652 if (IN_SET(r
, -EPERM
, -EACCES
))
1655 log_debug_errno(r
, "Failed to restrict system call architectures, skipping: %m");
1660 int parse_syscall_archs(char **l
, Set
**archs
) {
1661 _cleanup_set_free_ Set
*_archs
;
1668 r
= set_ensure_allocated(&_archs
, NULL
);
1672 STRV_FOREACH(s
, l
) {
1675 r
= seccomp_arch_from_string(*s
, &a
);
1679 r
= set_put(_archs
, UINT32_TO_PTR(a
+ 1));
1684 *archs
= TAKE_PTR(_archs
);
1689 int seccomp_filter_set_add(Hashmap
*filter
, bool add
, const SyscallFilterSet
*set
) {
1695 NULSTR_FOREACH(i
, set
->value
) {
1698 const SyscallFilterSet
*more
;
1700 more
= syscall_filter_set_find(i
);
1704 r
= seccomp_filter_set_add(filter
, add
, more
);
1710 id
= seccomp_syscall_resolve_name(i
);
1711 if (id
== __NR_SCMP_ERROR
) {
1712 log_debug("Couldn't resolve system call, ignoring: %s", i
);
1717 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(-1));
1721 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1728 int seccomp_lock_personality(unsigned long personality
) {
1732 if (personality
>= PERSONALITY_INVALID
)
1735 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1736 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1738 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1742 r
= seccomp_rule_add_exact(
1744 SCMP_ACT_ERRNO(EPERM
),
1745 SCMP_SYS(personality
),
1747 SCMP_A0(SCMP_CMP_NE
, personality
));
1749 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1753 r
= seccomp_load(seccomp
);
1754 if (IN_SET(r
, -EPERM
, -EACCES
))
1757 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1763 int seccomp_protect_hostname(void) {
1767 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1768 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1770 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1774 r
= seccomp_rule_add_exact(
1776 SCMP_ACT_ERRNO(EPERM
),
1777 SCMP_SYS(sethostname
),
1782 r
= seccomp_rule_add_exact(
1784 SCMP_ACT_ERRNO(EPERM
),
1785 SCMP_SYS(setdomainname
),
1790 r
= seccomp_load(seccomp
);
1791 if (IN_SET(r
, -EPERM
, -EACCES
))
1794 log_debug_errno(r
, "Failed to apply hostname restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));