1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <linux/seccomp.h>
14 #include "alloc-util.h"
16 #include "errno-list.h"
19 #include "nulstr-util.h"
20 #include "process-util.h"
21 #include "seccomp-util.h"
23 #include "string-util.h"
26 const uint32_t seccomp_local_archs
[] = {
28 /* Note: always list the native arch we are compiled as last, so that users can deny-list seccomp(), but our own calls to it still succeed */
30 #if defined(__x86_64__) && defined(__ILP32__)
33 SCMP_ARCH_X32
, /* native */
34 #elif defined(__x86_64__) && !defined(__ILP32__)
37 SCMP_ARCH_X86_64
, /* native */
38 #elif defined(__i386__)
40 #elif defined(__aarch64__)
42 SCMP_ARCH_AARCH64
, /* native */
43 #elif defined(__arm__)
45 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
47 SCMP_ARCH_MIPS
, /* native */
48 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
50 SCMP_ARCH_MIPSEL
, /* native */
51 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
54 SCMP_ARCH_MIPSEL64N32
,
57 SCMP_ARCH_MIPS64
, /* native */
58 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
62 SCMP_ARCH_MIPSEL64N32
,
64 SCMP_ARCH_MIPSEL64
, /* native */
65 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
70 SCMP_ARCH_MIPSEL64N32
,
71 SCMP_ARCH_MIPS64N32
, /* native */
72 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
78 SCMP_ARCH_MIPSEL64N32
, /* native */
79 #elif defined(__powerpc64__) && __BYTE_ORDER == __BIG_ENDIAN
82 SCMP_ARCH_PPC64
, /* native */
83 #elif defined(__powerpc64__) && __BYTE_ORDER == __LITTLE_ENDIAN
86 SCMP_ARCH_PPC64LE
, /* native */
87 #elif defined(__powerpc__)
89 #elif defined(__riscv) && __riscv_xlen == 64 && defined(SCMP_ARCH_RISCV64)
91 #elif defined(__s390x__)
93 SCMP_ARCH_S390X
, /* native */
94 #elif defined(__s390__)
100 const char* seccomp_arch_to_string(uint32_t c
) {
101 /* Maintain order used in <seccomp.h>.
103 * Names used here should be the same as those used for ConditionArchitecture=,
104 * except for "subarchitectures" like x32. */
107 case SCMP_ARCH_NATIVE
:
111 case SCMP_ARCH_X86_64
:
117 case SCMP_ARCH_AARCH64
:
121 case SCMP_ARCH_MIPS64
:
123 case SCMP_ARCH_MIPS64N32
:
125 case SCMP_ARCH_MIPSEL
:
127 case SCMP_ARCH_MIPSEL64
:
129 case SCMP_ARCH_MIPSEL64N32
:
130 return "mips64-le-n32";
133 case SCMP_ARCH_PPC64
:
135 case SCMP_ARCH_PPC64LE
:
137 #ifdef SCMP_ARCH_RISCV64
138 case SCMP_ARCH_RISCV64
:
143 case SCMP_ARCH_S390X
:
150 int seccomp_arch_from_string(const char *n
, uint32_t *ret
) {
156 if (streq(n
, "native"))
157 *ret
= SCMP_ARCH_NATIVE
;
158 else if (streq(n
, "x86"))
159 *ret
= SCMP_ARCH_X86
;
160 else if (streq(n
, "x86-64"))
161 *ret
= SCMP_ARCH_X86_64
;
162 else if (streq(n
, "x32"))
163 *ret
= SCMP_ARCH_X32
;
164 else if (streq(n
, "arm"))
165 *ret
= SCMP_ARCH_ARM
;
166 else if (streq(n
, "arm64"))
167 *ret
= SCMP_ARCH_AARCH64
;
168 else if (streq(n
, "mips"))
169 *ret
= SCMP_ARCH_MIPS
;
170 else if (streq(n
, "mips64"))
171 *ret
= SCMP_ARCH_MIPS64
;
172 else if (streq(n
, "mips64-n32"))
173 *ret
= SCMP_ARCH_MIPS64N32
;
174 else if (streq(n
, "mips-le"))
175 *ret
= SCMP_ARCH_MIPSEL
;
176 else if (streq(n
, "mips64-le"))
177 *ret
= SCMP_ARCH_MIPSEL64
;
178 else if (streq(n
, "mips64-le-n32"))
179 *ret
= SCMP_ARCH_MIPSEL64N32
;
180 else if (streq(n
, "ppc"))
181 *ret
= SCMP_ARCH_PPC
;
182 else if (streq(n
, "ppc64"))
183 *ret
= SCMP_ARCH_PPC64
;
184 else if (streq(n
, "ppc64-le"))
185 *ret
= SCMP_ARCH_PPC64LE
;
186 #ifdef SCMP_ARCH_RISCV64
187 else if (streq(n
, "riscv64"))
188 *ret
= SCMP_ARCH_RISCV64
;
190 else if (streq(n
, "s390"))
191 *ret
= SCMP_ARCH_S390
;
192 else if (streq(n
, "s390x"))
193 *ret
= SCMP_ARCH_S390X
;
200 int seccomp_init_for_arch(scmp_filter_ctx
*ret
, uint32_t arch
, uint32_t default_action
) {
201 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
204 /* Much like seccomp_init(), but initializes the filter for one specific architecture only, without affecting
205 * any others. Also, turns off the NNP fiddling. */
207 seccomp
= seccomp_init(default_action
);
211 if (arch
!= SCMP_ARCH_NATIVE
&&
212 arch
!= seccomp_arch_native()) {
214 r
= seccomp_arch_remove(seccomp
, seccomp_arch_native());
218 r
= seccomp_arch_add(seccomp
, arch
);
222 assert(seccomp_arch_exist(seccomp
, arch
) >= 0);
223 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) == -EEXIST
);
224 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) == -EEXIST
);
226 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) >= 0);
227 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) >= 0);
230 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_ACT_BADARCH
, SCMP_ACT_ALLOW
);
234 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
238 #if SCMP_VER_MAJOR >= 3 || (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 4)
239 if (getenv_bool("SYSTEMD_LOG_SECCOMP") > 0) {
240 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_LOG
, 1);
242 log_debug_errno(r
, "Failed to enable seccomp event logging: %m");
246 *ret
= TAKE_PTR(seccomp
);
250 static bool is_basic_seccomp_available(void) {
251 return prctl(PR_GET_SECCOMP
, 0, 0, 0, 0) >= 0;
254 static bool is_seccomp_filter_available(void) {
255 return prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, 0, 0) < 0 &&
259 bool is_seccomp_available(void) {
260 static int cached_enabled
= -1;
262 if (cached_enabled
< 0)
264 is_basic_seccomp_available() &&
265 is_seccomp_filter_available();
267 return cached_enabled
;
270 const SyscallFilterSet syscall_filter_sets
[_SYSCALL_FILTER_SET_MAX
] = {
271 [SYSCALL_FILTER_SET_DEFAULT
] = {
273 .help
= "System calls that are always permitted",
276 "clock_getres_time64\0"
280 "clock_nanosleep_time64\0"
304 "getrlimit\0" /* make sure processes can query stack size and such */
326 [SYSCALL_FILTER_SET_AIO
] = {
328 .help
= "Asynchronous IO",
334 "io_pgetevents_time64\0"
338 "io_uring_register\0"
341 [SYSCALL_FILTER_SET_BASIC_IO
] = {
362 [SYSCALL_FILTER_SET_CHOWN
] = {
364 .help
= "Change ownership of files and directories",
374 [SYSCALL_FILTER_SET_CLOCK
] = {
376 .help
= "Change the system time",
386 [SYSCALL_FILTER_SET_CPU_EMULATION
] = {
387 .name
= "@cpu-emulation",
388 .help
= "System calls for CPU emulation functionality",
396 [SYSCALL_FILTER_SET_DEBUG
] = {
398 .help
= "Debugging, performance monitoring and tracing functionality",
405 #if defined __s390__ || defined __s390x__
406 "s390_runtime_instr\0"
408 "sys_debug_setcontext\0"
410 [SYSCALL_FILTER_SET_FILE_SYSTEM
] = {
411 .name
= "@file-system",
412 .help
= "File system operations",
443 "inotify_add_watch\0"
494 [SYSCALL_FILTER_SET_IO_EVENT
] = {
496 .help
= "Event loop system calls",
515 [SYSCALL_FILTER_SET_IPC
] = {
517 .help
= "SysV IPC, POSIX Message Queues or other IPC",
525 "mq_timedreceive_time64\0"
527 "mq_timedsend_time64\0"
536 "process_vm_writev\0"
541 "semtimedop_time64\0"
547 [SYSCALL_FILTER_SET_KEYRING
] = {
549 .help
= "Kernel keyring access",
555 [SYSCALL_FILTER_SET_MEMLOCK
] = {
557 .help
= "Memory locking control",
565 [SYSCALL_FILTER_SET_MODULE
] = {
567 .help
= "Loading and unloading of kernel modules",
573 [SYSCALL_FILTER_SET_MOUNT
] = {
575 .help
= "Mounting and unmounting of file systems",
589 [SYSCALL_FILTER_SET_NETWORK_IO
] = {
590 .name
= "@network-io",
591 .help
= "Network or Unix socket IO, should not be needed if not network facing",
616 [SYSCALL_FILTER_SET_OBSOLETE
] = {
617 /* some unknown even to libseccomp */
619 .help
= "Unusual, obsolete or unimplemented system calls",
648 [SYSCALL_FILTER_SET_PKEY
] = {
650 .help
= "System calls used for memory protection keys",
656 [SYSCALL_FILTER_SET_PRIVILEGED
] = {
657 .name
= "@privileged",
658 .help
= "All system calls which need super-user capabilities",
674 "open_by_handle_at\0"
687 "setuid\0" /* We list the explicit system calls here, as @setuid also includes setgid() which is not necessarily privileged */
691 [SYSCALL_FILTER_SET_PROCESS
] = {
693 .help
= "Process control, execution, namespaceing operations",
696 "capget\0" /* Able to query arbitrary processes */
704 "pidfd_send_signal\0"
707 "rt_tgsigqueueinfo\0"
709 "swapcontext\0" /* Some archs e.g. powerpc32 are using it to do userspace context switches */
719 [SYSCALL_FILTER_SET_RAW_IO
] = {
721 .help
= "Raw I/O port access",
728 #if defined __s390__ || defined __s390x__
729 "s390_pci_mmio_read\0"
730 "s390_pci_mmio_write\0"
733 [SYSCALL_FILTER_SET_REBOOT
] = {
735 .help
= "Reboot and reboot preparation/kexec",
741 [SYSCALL_FILTER_SET_RESOURCES
] = {
742 .name
= "@resources",
743 .help
= "Alter resource settings",
750 "sched_setaffinity\0"
753 "sched_setscheduler\0"
758 [SYSCALL_FILTER_SET_SETUID
] = {
760 .help
= "Operations for changing user/group credentials",
777 [SYSCALL_FILTER_SET_SIGNAL
] = {
779 .help
= "Process signal handling",
786 "rt_sigtimedwait_time64\0"
796 [SYSCALL_FILTER_SET_SWAP
] = {
798 .help
= "Enable/disable swap devices",
803 [SYSCALL_FILTER_SET_SYNC
] = {
805 .help
= "Synchronize files and memory to storage",
815 [SYSCALL_FILTER_SET_SYSTEM_SERVICE
] = {
816 .name
= "@system-service",
817 .help
= "General system service operations",
852 "name_to_handle_at\0"
859 "sched_get_priority_max\0"
860 "sched_get_priority_min\0"
861 "sched_getaffinity\0"
864 "sched_getscheduler\0"
865 "sched_rr_get_interval\0"
866 "sched_rr_get_interval_time64\0"
884 [SYSCALL_FILTER_SET_TIMER
] = {
886 .help
= "Schedule operations by time",
900 "timerfd_gettime64\0"
902 "timerfd_settime64\0"
905 [SYSCALL_FILTER_SET_KNOWN
] = {
907 .help
= "All known syscalls declared in the kernel",
909 #include "syscall-list.h"
913 const SyscallFilterSet
*syscall_filter_set_find(const char *name
) {
914 if (isempty(name
) || name
[0] != '@')
917 for (unsigned i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++)
918 if (streq(syscall_filter_sets
[i
].name
, name
))
919 return syscall_filter_sets
+ i
;
924 static int add_syscall_filter_set(
925 scmp_filter_ctx seccomp
,
926 const SyscallFilterSet
*set
,
932 int seccomp_add_syscall_filter_item(
933 scmp_filter_ctx
*seccomp
,
943 if (strv_contains(exclude
, name
))
946 /* Any syscalls that are handled are added to the *added strv. The pointer
947 * must be either NULL or point to a valid pre-initialized possibly-empty strv. */
949 if (name
[0] == '@') {
950 const SyscallFilterSet
*other
;
952 other
= syscall_filter_set_find(name
);
954 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
955 "Filter set %s is not known!",
958 return add_syscall_filter_set(seccomp
, other
, action
, exclude
, log_missing
, added
);
963 id
= seccomp_syscall_resolve_name(name
);
964 if (id
== __NR_SCMP_ERROR
) {
966 log_debug("System call %s is not known, ignoring.", name
);
970 r
= seccomp_rule_add_exact(seccomp
, action
, id
, 0);
972 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
973 bool ignore
= r
== -EDOM
;
975 if (!ignore
|| log_missing
)
976 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
977 name
, id
, ignore
? ", ignoring" : "");
983 r
= strv_extend(added
, name
);
992 static int add_syscall_filter_set(
993 scmp_filter_ctx seccomp
,
994 const SyscallFilterSet
*set
,
1003 /* Any syscalls that are handled are added to the *added strv. It needs to be initialized. */
1008 NULSTR_FOREACH(sys
, set
->value
) {
1009 r
= seccomp_add_syscall_filter_item(seccomp
, sys
, action
, exclude
, log_missing
, added
);
1017 int seccomp_load_syscall_filter_set(uint32_t default_action
, const SyscallFilterSet
*set
, uint32_t action
, bool log_missing
) {
1023 /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
1024 * each local arch. */
1026 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1027 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1029 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1031 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1035 r
= add_syscall_filter_set(seccomp
, set
, action
, NULL
, log_missing
, NULL
);
1037 return log_debug_errno(r
, "Failed to add filter set: %m");
1039 r
= seccomp_load(seccomp
);
1040 if (ERRNO_IS_SECCOMP_FATAL(r
))
1043 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1049 int seccomp_load_syscall_filter_set_raw(uint32_t default_action
, Hashmap
* set
, uint32_t action
, bool log_missing
) {
1053 /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Set* of syscalls, instead of a
1054 * SyscallFilterSet* table. */
1056 if (hashmap_isempty(set
) && default_action
== SCMP_ACT_ALLOW
)
1059 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1060 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1061 void *syscall_id
, *val
;
1063 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1065 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1069 HASHMAP_FOREACH_KEY(val
, syscall_id
, set
) {
1070 uint32_t a
= action
;
1071 int id
= PTR_TO_INT(syscall_id
) - 1;
1072 int error
= PTR_TO_INT(val
);
1074 if (error
== SECCOMP_ERROR_NUMBER_KILL
)
1075 a
= scmp_act_kill_process();
1076 else if (action
!= SCMP_ACT_ALLOW
&& error
>= 0)
1077 a
= SCMP_ACT_ERRNO(error
);
1079 r
= seccomp_rule_add_exact(seccomp
, a
, id
, 0);
1081 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
1082 _cleanup_free_
char *n
= NULL
;
1085 n
= seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE
, id
);
1086 ignore
= r
== -EDOM
;
1087 if (!ignore
|| log_missing
)
1088 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1089 strna(n
), id
, ignore
? ", ignoring" : "");
1095 r
= seccomp_load(seccomp
);
1096 if (ERRNO_IS_SECCOMP_FATAL(r
))
1099 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1105 int seccomp_parse_syscall_filter(
1109 SeccompParseFlags flags
,
1111 const char *filename
,
1119 if (name
[0] == '@') {
1120 const SyscallFilterSet
*set
;
1123 set
= syscall_filter_set_find(name
);
1125 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1128 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1129 "Unknown system call group, ignoring: %s", name
);
1133 NULSTR_FOREACH(i
, set
->value
) {
1134 /* Call ourselves again, for the group to parse. Note that we downgrade logging here (i.e. take
1135 * away the SECCOMP_PARSE_LOG flag) since any issues in the group table are our own problem,
1136 * not a problem in user configuration data and we shouldn't pretend otherwise by complaining
1138 r
= seccomp_parse_syscall_filter(i
, errno_num
, filter
, flags
&~ SECCOMP_PARSE_LOG
, unit
, filename
, line
);
1145 id
= seccomp_syscall_resolve_name(name
);
1146 if (id
== __NR_SCMP_ERROR
) {
1147 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1150 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1151 "Failed to parse system call, ignoring: %s", name
);
1155 /* If we previously wanted to forbid a syscall and now
1156 * we want to allow it, then remove it from the list. */
1157 if (!(flags
& SECCOMP_PARSE_INVERT
) == !!(flags
& SECCOMP_PARSE_ALLOW_LIST
)) {
1158 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
));
1162 return flags
& SECCOMP_PARSE_LOG
? log_oom() : -ENOMEM
;
1164 assert_se(hashmap_update(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
)) == 0);
1170 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1176 int seccomp_restrict_namespaces(unsigned long retain
) {
1180 if (DEBUG_LOGGING
) {
1181 _cleanup_free_
char *s
= NULL
;
1183 (void) namespace_flags_to_string(retain
, &s
);
1184 log_debug("Restricting namespace to: %s.", strna(s
));
1188 if ((retain
& NAMESPACE_FLAGS_ALL
) == NAMESPACE_FLAGS_ALL
)
1191 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1192 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1194 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1196 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1200 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
1201 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
1203 r
= seccomp_rule_add_exact(
1205 SCMP_ACT_ERRNO(EPERM
),
1209 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
1210 * special invocation with a zero flags argument, right here. */
1211 r
= seccomp_rule_add_exact(
1213 SCMP_ACT_ERRNO(EPERM
),
1216 SCMP_A1(SCMP_CMP_EQ
, 0));
1218 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1222 for (unsigned i
= 0; namespace_flag_map
[i
].name
; i
++) {
1225 f
= namespace_flag_map
[i
].flag
;
1226 if ((retain
& f
) == f
) {
1227 log_debug("Permitting %s.", namespace_flag_map
[i
].name
);
1231 log_debug("Blocking %s.", namespace_flag_map
[i
].name
);
1233 r
= seccomp_rule_add_exact(
1235 SCMP_ACT_ERRNO(EPERM
),
1238 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1240 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1244 /* On s390/s390x the first two parameters to clone are switched */
1245 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
1246 r
= seccomp_rule_add_exact(
1248 SCMP_ACT_ERRNO(EPERM
),
1251 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1253 r
= seccomp_rule_add_exact(
1255 SCMP_ACT_ERRNO(EPERM
),
1258 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1260 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1264 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1265 r
= seccomp_rule_add_exact(
1267 SCMP_ACT_ERRNO(EPERM
),
1270 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1272 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1280 r
= seccomp_load(seccomp
);
1281 if (ERRNO_IS_SECCOMP_FATAL(r
))
1284 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1290 int seccomp_protect_sysctl(void) {
1294 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1295 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1297 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1301 #ifdef SCMP_ARCH_RISCV64
1306 /* No _sysctl syscall */
1309 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1313 r
= seccomp_rule_add_exact(
1315 SCMP_ACT_ERRNO(EPERM
),
1319 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1323 r
= seccomp_load(seccomp
);
1324 if (ERRNO_IS_SECCOMP_FATAL(r
))
1327 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1333 int seccomp_protect_syslog(void) {
1337 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1338 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1340 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1344 r
= seccomp_rule_add_exact(
1346 SCMP_ACT_ERRNO(EPERM
),
1351 log_debug_errno(r
, "Failed to add syslog() rule for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1355 r
= seccomp_load(seccomp
);
1356 if (ERRNO_IS_SECCOMP_FATAL(r
))
1359 log_debug_errno(r
, "Failed to install syslog protection rules for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1365 int seccomp_restrict_address_families(Set
*address_families
, bool allow_list
) {
1369 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1370 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1373 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1377 case SCMP_ARCH_X86_64
:
1380 case SCMP_ARCH_AARCH64
:
1382 case SCMP_ARCH_PPC64
:
1383 case SCMP_ARCH_PPC64LE
:
1384 case SCMP_ARCH_MIPSEL64N32
:
1385 case SCMP_ARCH_MIPS64N32
:
1386 case SCMP_ARCH_MIPSEL64
:
1387 case SCMP_ARCH_MIPS64
:
1388 #ifdef SCMP_ARCH_RISCV64
1389 case SCMP_ARCH_RISCV64
:
1391 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1395 case SCMP_ARCH_S390
:
1396 case SCMP_ARCH_S390X
:
1398 case SCMP_ARCH_MIPSEL
:
1399 case SCMP_ARCH_MIPS
:
1401 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1410 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1415 int first
= 0, last
= 0;
1418 /* If this is an allow list, we first block the address families that are out of
1419 * range and then everything that is not in the set. First, we find the lowest and
1420 * highest address family in the set. */
1422 SET_FOREACH(afp
, address_families
) {
1423 int af
= PTR_TO_INT(afp
);
1425 if (af
<= 0 || af
>= af_max())
1428 if (first
== 0 || af
< first
)
1431 if (last
== 0 || af
> last
)
1435 assert((first
== 0) == (last
== 0));
1439 /* No entries in the valid range, block everything */
1440 r
= seccomp_rule_add_exact(
1442 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1446 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1452 /* Block everything below the first entry */
1453 r
= seccomp_rule_add_exact(
1455 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1458 SCMP_A0(SCMP_CMP_LT
, first
));
1460 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1464 /* Block everything above the last entry */
1465 r
= seccomp_rule_add_exact(
1467 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1470 SCMP_A0(SCMP_CMP_GT
, last
));
1472 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1476 /* Block everything between the first and last entry */
1477 for (int af
= 1; af
< af_max(); af
++) {
1479 if (set_contains(address_families
, INT_TO_PTR(af
)))
1482 r
= seccomp_rule_add_exact(
1484 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1487 SCMP_A0(SCMP_CMP_EQ
, af
));
1492 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1500 /* If this is a deny list, then generate one rule for each address family that are
1501 * then combined in OR checks. */
1503 SET_FOREACH(af
, address_families
) {
1504 r
= seccomp_rule_add_exact(
1506 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1509 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1514 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1519 r
= seccomp_load(seccomp
);
1520 if (ERRNO_IS_SECCOMP_FATAL(r
))
1523 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1529 int seccomp_restrict_realtime(void) {
1530 static const int permitted_policies
[] = {
1536 int r
, max_policy
= 0;
1540 /* Determine the highest policy constant we want to allow */
1541 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1542 if (permitted_policies
[i
] > max_policy
)
1543 max_policy
= permitted_policies
[i
];
1545 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1546 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1549 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1551 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1555 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1557 for (p
= 0; p
< max_policy
; p
++) {
1560 /* Check if this is in the allow list. */
1561 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1562 if (permitted_policies
[i
] == p
) {
1570 /* Deny this policy */
1571 r
= seccomp_rule_add_exact(
1573 SCMP_ACT_ERRNO(EPERM
),
1574 SCMP_SYS(sched_setscheduler
),
1576 SCMP_A1(SCMP_CMP_EQ
, p
));
1578 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1583 /* Deny-list all other policies, i.e. the ones with higher values. Note that all comparisons
1584 * are unsigned here, hence no need no check for < 0 values. */
1585 r
= seccomp_rule_add_exact(
1587 SCMP_ACT_ERRNO(EPERM
),
1588 SCMP_SYS(sched_setscheduler
),
1590 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1592 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1596 r
= seccomp_load(seccomp
);
1597 if (ERRNO_IS_SECCOMP_FATAL(r
))
1600 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1606 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1610 const struct scmp_arg_cmp arg
) {
1613 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1615 _cleanup_free_
char *n
= NULL
;
1617 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1618 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1620 seccomp_arch_to_string(arch
));
1626 /* For known architectures, check that syscalls are indeed defined or not. */
1627 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || (defined(__riscv) && __riscv_xlen == 64)
1628 assert_cc(SCMP_SYS(shmget
) > 0);
1629 assert_cc(SCMP_SYS(shmat
) > 0);
1630 assert_cc(SCMP_SYS(shmdt
) > 0);
1633 int seccomp_memory_deny_write_execute(void) {
1635 unsigned loaded
= 0;
1637 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1638 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1639 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0, r
;
1641 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1645 /* Note that on some architectures shmat() isn't available, and the call is multiplexed through ipc().
1646 * We ignore that here, which means there's still a way to get writable/executable
1647 * memory, if an IPC key is mapped like this. That's a pity, but no total loss. */
1650 case SCMP_ARCH_S390
:
1651 filter_syscall
= SCMP_SYS(mmap2
);
1652 block_syscall
= SCMP_SYS(mmap
);
1653 /* shmat multiplexed, see above */
1657 case SCMP_ARCH_PPC64
:
1658 case SCMP_ARCH_PPC64LE
:
1659 case SCMP_ARCH_S390X
:
1660 filter_syscall
= SCMP_SYS(mmap
);
1661 /* shmat multiplexed, see above */
1665 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1666 shmat_syscall
= SCMP_SYS(shmat
);
1669 case SCMP_ARCH_X86_64
:
1671 case SCMP_ARCH_AARCH64
:
1672 #ifdef SCMP_ARCH_RISCV64
1673 case SCMP_ARCH_RISCV64
:
1675 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32, arm64 and riscv64 have only mmap */
1676 shmat_syscall
= SCMP_SYS(shmat
);
1679 /* Please add more definitions here, if you port systemd to other architectures! */
1681 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__) && !defined(__s390__) && !defined(__s390x__) && !(defined(__riscv) && __riscv_xlen == 64)
1682 #warning "Consider adding the right mmap() syscall definitions here!"
1686 /* Can't filter mmap() on this arch, then skip it */
1687 if (filter_syscall
== 0)
1690 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1694 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1696 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1700 if (block_syscall
!= 0) {
1701 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1706 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1708 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1712 #ifdef __NR_pkey_mprotect
1713 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(pkey_mprotect
),
1715 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1720 if (shmat_syscall
> 0) {
1721 r
= add_seccomp_syscall_filter(seccomp
, arch
, shmat_syscall
,
1723 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1728 r
= seccomp_load(seccomp
);
1729 if (ERRNO_IS_SECCOMP_FATAL(r
))
1732 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m",
1733 seccomp_arch_to_string(arch
));
1738 log_debug("Failed to install any seccomp rules for MemoryDenyWriteExecute=.");
1743 int seccomp_restrict_archs(Set
*archs
) {
1744 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1748 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1751 * There are some qualifications. However the most important use is to stop processes from bypassing
1752 * system call restrictions, in case they used a broader (multiplexing) syscall which is only available
1753 * in a non-native architecture. There are no holes in this use case, at least so far. */
1755 /* Note libseccomp includes our "native" (current) architecture in the filter by default.
1756 * We do not remove it. For example, our callers expect to be able to call execve() afterwards
1757 * to run a program with the restrictions applied. */
1758 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1762 SET_FOREACH(id
, archs
) {
1763 r
= seccomp_arch_add(seccomp
, PTR_TO_UINT32(id
) - 1);
1764 if (r
< 0 && r
!= -EEXIST
)
1768 /* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
1769 * x32 syscalls should basically match x86-64 for everything except the pointer type.
1770 * The important thing is that you can block the old 32-bit x86 syscalls.
1771 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
1773 if (seccomp_arch_native() == SCMP_ARCH_X32
||
1774 set_contains(archs
, UINT32_TO_PTR(SCMP_ARCH_X32
+ 1))) {
1776 r
= seccomp_arch_add(seccomp
, SCMP_ARCH_X86_64
);
1777 if (r
< 0 && r
!= -EEXIST
)
1781 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1785 r
= seccomp_load(seccomp
);
1786 if (ERRNO_IS_SECCOMP_FATAL(r
))
1789 log_debug_errno(r
, "Failed to restrict system call architectures, skipping: %m");
1794 int parse_syscall_archs(char **l
, Set
**ret_archs
) {
1795 _cleanup_set_free_ Set
*archs
= NULL
;
1802 STRV_FOREACH(s
, l
) {
1805 r
= seccomp_arch_from_string(*s
, &a
);
1809 r
= set_ensure_put(&archs
, NULL
, UINT32_TO_PTR(a
+ 1));
1814 *ret_archs
= TAKE_PTR(archs
);
1818 int seccomp_filter_set_add(Hashmap
*filter
, bool add
, const SyscallFilterSet
*set
) {
1824 NULSTR_FOREACH(i
, set
->value
) {
1827 const SyscallFilterSet
*more
;
1829 more
= syscall_filter_set_find(i
);
1833 r
= seccomp_filter_set_add(filter
, add
, more
);
1839 id
= seccomp_syscall_resolve_name(i
);
1840 if (id
== __NR_SCMP_ERROR
) {
1841 log_debug("Couldn't resolve system call, ignoring: %s", i
);
1846 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(-1));
1850 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1857 int seccomp_lock_personality(unsigned long personality
) {
1861 if (personality
>= PERSONALITY_INVALID
)
1864 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1865 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1867 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1871 r
= seccomp_rule_add_exact(
1873 SCMP_ACT_ERRNO(EPERM
),
1874 SCMP_SYS(personality
),
1876 SCMP_A0(SCMP_CMP_NE
, personality
));
1878 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1882 r
= seccomp_load(seccomp
);
1883 if (ERRNO_IS_SECCOMP_FATAL(r
))
1886 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1892 int seccomp_protect_hostname(void) {
1896 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1897 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1899 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1903 r
= seccomp_rule_add_exact(
1905 SCMP_ACT_ERRNO(EPERM
),
1906 SCMP_SYS(sethostname
),
1909 log_debug_errno(r
, "Failed to add sethostname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1913 r
= seccomp_rule_add_exact(
1915 SCMP_ACT_ERRNO(EPERM
),
1916 SCMP_SYS(setdomainname
),
1919 log_debug_errno(r
, "Failed to add setdomainname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1923 r
= seccomp_load(seccomp
);
1924 if (ERRNO_IS_SECCOMP_FATAL(r
))
1927 log_debug_errno(r
, "Failed to apply hostname restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1933 static int seccomp_restrict_sxid(scmp_filter_ctx seccomp
, mode_t m
) {
1934 /* Checks the mode_t parameter of the following system calls:
1936 * → chmod() + fchmod() + fchmodat()
1937 * → open() + creat() + openat()
1938 * → mkdir() + mkdirat()
1939 * → mknod() + mknodat()
1941 * Returns error if *everything* failed, and 0 otherwise.
1946 r
= seccomp_rule_add_exact(
1948 SCMP_ACT_ERRNO(EPERM
),
1951 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1953 log_debug_errno(r
, "Failed to add filter for chmod: %m");
1957 r
= seccomp_rule_add_exact(
1959 SCMP_ACT_ERRNO(EPERM
),
1962 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1964 log_debug_errno(r
, "Failed to add filter for fchmod: %m");
1968 r
= seccomp_rule_add_exact(
1970 SCMP_ACT_ERRNO(EPERM
),
1973 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1975 log_debug_errno(r
, "Failed to add filter for fchmodat: %m");
1979 r
= seccomp_rule_add_exact(
1981 SCMP_ACT_ERRNO(EPERM
),
1984 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1986 log_debug_errno(r
, "Failed to add filter for mkdir: %m");
1990 r
= seccomp_rule_add_exact(
1992 SCMP_ACT_ERRNO(EPERM
),
1995 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1997 log_debug_errno(r
, "Failed to add filter for mkdirat: %m");
2001 r
= seccomp_rule_add_exact(
2003 SCMP_ACT_ERRNO(EPERM
),
2006 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2008 log_debug_errno(r
, "Failed to add filter for mknod: %m");
2012 r
= seccomp_rule_add_exact(
2014 SCMP_ACT_ERRNO(EPERM
),
2017 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2019 log_debug_errno(r
, "Failed to add filter for mknodat: %m");
2023 #if SCMP_SYS(open) > 0
2024 r
= seccomp_rule_add_exact(
2026 SCMP_ACT_ERRNO(EPERM
),
2029 SCMP_A1(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2030 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2032 log_debug_errno(r
, "Failed to add filter for open: %m");
2037 r
= seccomp_rule_add_exact(
2039 SCMP_ACT_ERRNO(EPERM
),
2042 SCMP_A2(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2043 SCMP_A3(SCMP_CMP_MASKED_EQ
, m
, m
));
2045 log_debug_errno(r
, "Failed to add filter for openat: %m");
2049 #if defined(__SNR_openat2)
2050 /* The new openat2() system call can't be filtered sensibly, since it moves the flags parameter into
2051 * an indirect structure. Let's block it entirely for now. That should be a reasonably OK thing to do
2052 * for now, since openat2() is very new and code generally needs fallback logic anyway to be
2053 * compatible with kernels that are not absolutely recent. */
2054 r
= seccomp_rule_add_exact(
2056 SCMP_ACT_ERRNO(EPERM
),
2060 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2065 r
= seccomp_rule_add_exact(
2067 SCMP_ACT_ERRNO(EPERM
),
2070 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2072 log_debug_errno(r
, "Failed to add filter for creat: %m");
2079 int seccomp_restrict_suid_sgid(void) {
2083 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2084 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2086 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2090 r
= seccomp_restrict_sxid(seccomp
, S_ISUID
);
2092 log_debug_errno(r
, "Failed to add suid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2094 k
= seccomp_restrict_sxid(seccomp
, S_ISGID
);
2096 log_debug_errno(r
, "Failed to add sgid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2101 r
= seccomp_load(seccomp
);
2102 if (ERRNO_IS_SECCOMP_FATAL(r
))
2105 log_debug_errno(r
, "Failed to apply suid/sgid restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2111 uint32_t scmp_act_kill_process(void) {
2113 /* Returns SCMP_ACT_KILL_PROCESS if it's supported, and SCMP_ACT_KILL_THREAD otherwise. We never
2114 * actually want to use SCMP_ACT_KILL_THREAD as its semantics are nuts (killing arbitrary threads of
2115 * a program is just a bad idea), but on old kernels/old libseccomp it is all we have, and at least
2116 * for single-threaded apps does the right thing. */
2118 #ifdef SCMP_ACT_KILL_PROCESS
2119 if (seccomp_api_get() >= 3)
2120 return SCMP_ACT_KILL_PROCESS
;
2123 return SCMP_ACT_KILL
; /* same as SCMP_ACT_KILL_THREAD */