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();
1077 else if (action
== SCMP_ACT_LOG
)
1080 else if (action
!= SCMP_ACT_ALLOW
&& error
>= 0)
1081 a
= SCMP_ACT_ERRNO(error
);
1083 r
= seccomp_rule_add_exact(seccomp
, a
, id
, 0);
1085 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
1086 _cleanup_free_
char *n
= NULL
;
1089 n
= seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE
, id
);
1090 ignore
= r
== -EDOM
;
1091 if (!ignore
|| log_missing
)
1092 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1093 strna(n
), id
, ignore
? ", ignoring" : "");
1099 r
= seccomp_load(seccomp
);
1100 if (ERRNO_IS_SECCOMP_FATAL(r
))
1103 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1109 int seccomp_parse_syscall_filter(
1113 SeccompParseFlags flags
,
1115 const char *filename
,
1123 if (name
[0] == '@') {
1124 const SyscallFilterSet
*set
;
1127 set
= syscall_filter_set_find(name
);
1129 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1132 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1133 "Unknown system call group, ignoring: %s", name
);
1137 NULSTR_FOREACH(i
, set
->value
) {
1138 /* Call ourselves again, for the group to parse. Note that we downgrade logging here (i.e. take
1139 * away the SECCOMP_PARSE_LOG flag) since any issues in the group table are our own problem,
1140 * not a problem in user configuration data and we shouldn't pretend otherwise by complaining
1142 r
= seccomp_parse_syscall_filter(i
, errno_num
, filter
, flags
&~ SECCOMP_PARSE_LOG
, unit
, filename
, line
);
1149 id
= seccomp_syscall_resolve_name(name
);
1150 if (id
== __NR_SCMP_ERROR
) {
1151 if (!(flags
& SECCOMP_PARSE_PERMISSIVE
))
1154 log_syntax(unit
, flags
& SECCOMP_PARSE_LOG
? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1155 "Failed to parse system call, ignoring: %s", name
);
1159 /* If we previously wanted to forbid a syscall and now
1160 * we want to allow it, then remove it from the list. */
1161 if (!(flags
& SECCOMP_PARSE_INVERT
) == !!(flags
& SECCOMP_PARSE_ALLOW_LIST
)) {
1162 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
));
1166 return flags
& SECCOMP_PARSE_LOG
? log_oom() : -ENOMEM
;
1168 assert_se(hashmap_update(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
)) == 0);
1174 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1180 int seccomp_restrict_namespaces(unsigned long retain
) {
1184 if (DEBUG_LOGGING
) {
1185 _cleanup_free_
char *s
= NULL
;
1187 (void) namespace_flags_to_string(retain
, &s
);
1188 log_debug("Restricting namespace to: %s.", strna(s
));
1192 if ((retain
& NAMESPACE_FLAGS_ALL
) == NAMESPACE_FLAGS_ALL
)
1195 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1196 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1198 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1200 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1204 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
1205 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
1207 r
= seccomp_rule_add_exact(
1209 SCMP_ACT_ERRNO(EPERM
),
1213 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
1214 * special invocation with a zero flags argument, right here. */
1215 r
= seccomp_rule_add_exact(
1217 SCMP_ACT_ERRNO(EPERM
),
1220 SCMP_A1(SCMP_CMP_EQ
, 0));
1222 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1226 for (unsigned i
= 0; namespace_flag_map
[i
].name
; i
++) {
1229 f
= namespace_flag_map
[i
].flag
;
1230 if ((retain
& f
) == f
) {
1231 log_debug("Permitting %s.", namespace_flag_map
[i
].name
);
1235 log_debug("Blocking %s.", namespace_flag_map
[i
].name
);
1237 r
= seccomp_rule_add_exact(
1239 SCMP_ACT_ERRNO(EPERM
),
1242 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1244 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1248 /* On s390/s390x the first two parameters to clone are switched */
1249 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
1250 r
= seccomp_rule_add_exact(
1252 SCMP_ACT_ERRNO(EPERM
),
1255 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1257 r
= seccomp_rule_add_exact(
1259 SCMP_ACT_ERRNO(EPERM
),
1262 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1264 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1268 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1269 r
= seccomp_rule_add_exact(
1271 SCMP_ACT_ERRNO(EPERM
),
1274 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1276 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1284 r
= seccomp_load(seccomp
);
1285 if (ERRNO_IS_SECCOMP_FATAL(r
))
1288 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1294 int seccomp_protect_sysctl(void) {
1298 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1299 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1301 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1305 #ifdef SCMP_ARCH_RISCV64
1310 /* No _sysctl syscall */
1313 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1317 r
= seccomp_rule_add_exact(
1319 SCMP_ACT_ERRNO(EPERM
),
1323 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1327 r
= seccomp_load(seccomp
);
1328 if (ERRNO_IS_SECCOMP_FATAL(r
))
1331 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1337 int seccomp_protect_syslog(void) {
1341 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1342 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1344 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1348 r
= seccomp_rule_add_exact(
1350 SCMP_ACT_ERRNO(EPERM
),
1355 log_debug_errno(r
, "Failed to add syslog() rule for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1359 r
= seccomp_load(seccomp
);
1360 if (ERRNO_IS_SECCOMP_FATAL(r
))
1363 log_debug_errno(r
, "Failed to install syslog protection rules for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1369 int seccomp_restrict_address_families(Set
*address_families
, bool allow_list
) {
1373 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1374 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1377 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1381 case SCMP_ARCH_X86_64
:
1384 case SCMP_ARCH_AARCH64
:
1386 case SCMP_ARCH_PPC64
:
1387 case SCMP_ARCH_PPC64LE
:
1388 case SCMP_ARCH_MIPSEL64N32
:
1389 case SCMP_ARCH_MIPS64N32
:
1390 case SCMP_ARCH_MIPSEL64
:
1391 case SCMP_ARCH_MIPS64
:
1392 #ifdef SCMP_ARCH_RISCV64
1393 case SCMP_ARCH_RISCV64
:
1395 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1399 case SCMP_ARCH_S390
:
1400 case SCMP_ARCH_S390X
:
1402 case SCMP_ARCH_MIPSEL
:
1403 case SCMP_ARCH_MIPS
:
1405 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1414 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1419 int first
= 0, last
= 0;
1422 /* If this is an allow list, we first block the address families that are out of
1423 * range and then everything that is not in the set. First, we find the lowest and
1424 * highest address family in the set. */
1426 SET_FOREACH(afp
, address_families
) {
1427 int af
= PTR_TO_INT(afp
);
1429 if (af
<= 0 || af
>= af_max())
1432 if (first
== 0 || af
< first
)
1435 if (last
== 0 || af
> last
)
1439 assert((first
== 0) == (last
== 0));
1443 /* No entries in the valid range, block everything */
1444 r
= seccomp_rule_add_exact(
1446 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1450 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1456 /* Block everything below the first entry */
1457 r
= seccomp_rule_add_exact(
1459 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1462 SCMP_A0(SCMP_CMP_LT
, first
));
1464 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1468 /* Block everything above the last entry */
1469 r
= seccomp_rule_add_exact(
1471 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1474 SCMP_A0(SCMP_CMP_GT
, last
));
1476 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1480 /* Block everything between the first and last entry */
1481 for (int af
= 1; af
< af_max(); af
++) {
1483 if (set_contains(address_families
, INT_TO_PTR(af
)))
1486 r
= seccomp_rule_add_exact(
1488 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1491 SCMP_A0(SCMP_CMP_EQ
, af
));
1496 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1504 /* If this is a deny list, then generate one rule for each address family that are
1505 * then combined in OR checks. */
1507 SET_FOREACH(af
, address_families
) {
1508 r
= seccomp_rule_add_exact(
1510 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1513 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1518 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1523 r
= seccomp_load(seccomp
);
1524 if (ERRNO_IS_SECCOMP_FATAL(r
))
1527 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1533 int seccomp_restrict_realtime(void) {
1534 static const int permitted_policies
[] = {
1540 int r
, max_policy
= 0;
1544 /* Determine the highest policy constant we want to allow */
1545 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1546 if (permitted_policies
[i
] > max_policy
)
1547 max_policy
= permitted_policies
[i
];
1549 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1550 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1553 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1555 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1559 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1561 for (p
= 0; p
< max_policy
; p
++) {
1564 /* Check if this is in the allow list. */
1565 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1566 if (permitted_policies
[i
] == p
) {
1574 /* Deny this policy */
1575 r
= seccomp_rule_add_exact(
1577 SCMP_ACT_ERRNO(EPERM
),
1578 SCMP_SYS(sched_setscheduler
),
1580 SCMP_A1(SCMP_CMP_EQ
, p
));
1582 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1587 /* Deny-list all other policies, i.e. the ones with higher values. Note that all comparisons
1588 * are unsigned here, hence no need no check for < 0 values. */
1589 r
= seccomp_rule_add_exact(
1591 SCMP_ACT_ERRNO(EPERM
),
1592 SCMP_SYS(sched_setscheduler
),
1594 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1596 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1600 r
= seccomp_load(seccomp
);
1601 if (ERRNO_IS_SECCOMP_FATAL(r
))
1604 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1610 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1614 const struct scmp_arg_cmp arg
) {
1617 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1619 _cleanup_free_
char *n
= NULL
;
1621 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1622 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1624 seccomp_arch_to_string(arch
));
1630 /* For known architectures, check that syscalls are indeed defined or not. */
1631 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || (defined(__riscv) && __riscv_xlen == 64)
1632 assert_cc(SCMP_SYS(shmget
) > 0);
1633 assert_cc(SCMP_SYS(shmat
) > 0);
1634 assert_cc(SCMP_SYS(shmdt
) > 0);
1637 int seccomp_memory_deny_write_execute(void) {
1639 unsigned loaded
= 0;
1641 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1642 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1643 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0, r
;
1645 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1649 /* Note that on some architectures shmat() isn't available, and the call is multiplexed through ipc().
1650 * We ignore that here, which means there's still a way to get writable/executable
1651 * memory, if an IPC key is mapped like this. That's a pity, but no total loss. */
1654 case SCMP_ARCH_S390
:
1655 filter_syscall
= SCMP_SYS(mmap2
);
1656 block_syscall
= SCMP_SYS(mmap
);
1657 /* shmat multiplexed, see above */
1661 case SCMP_ARCH_PPC64
:
1662 case SCMP_ARCH_PPC64LE
:
1663 case SCMP_ARCH_S390X
:
1664 filter_syscall
= SCMP_SYS(mmap
);
1665 /* shmat multiplexed, see above */
1669 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1670 shmat_syscall
= SCMP_SYS(shmat
);
1673 case SCMP_ARCH_X86_64
:
1675 case SCMP_ARCH_AARCH64
:
1676 #ifdef SCMP_ARCH_RISCV64
1677 case SCMP_ARCH_RISCV64
:
1679 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32, arm64 and riscv64 have only mmap */
1680 shmat_syscall
= SCMP_SYS(shmat
);
1683 /* Please add more definitions here, if you port systemd to other architectures! */
1685 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__) && !defined(__s390__) && !defined(__s390x__) && !(defined(__riscv) && __riscv_xlen == 64)
1686 #warning "Consider adding the right mmap() syscall definitions here!"
1690 /* Can't filter mmap() on this arch, then skip it */
1691 if (filter_syscall
== 0)
1694 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1698 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1700 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1704 if (block_syscall
!= 0) {
1705 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1710 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1712 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1716 #ifdef __NR_pkey_mprotect
1717 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(pkey_mprotect
),
1719 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1724 if (shmat_syscall
> 0) {
1725 r
= add_seccomp_syscall_filter(seccomp
, arch
, shmat_syscall
,
1727 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1732 r
= seccomp_load(seccomp
);
1733 if (ERRNO_IS_SECCOMP_FATAL(r
))
1736 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m",
1737 seccomp_arch_to_string(arch
));
1742 log_debug("Failed to install any seccomp rules for MemoryDenyWriteExecute=.");
1747 int seccomp_restrict_archs(Set
*archs
) {
1748 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1752 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1755 * There are some qualifications. However the most important use is to stop processes from bypassing
1756 * system call restrictions, in case they used a broader (multiplexing) syscall which is only available
1757 * in a non-native architecture. There are no holes in this use case, at least so far. */
1759 /* Note libseccomp includes our "native" (current) architecture in the filter by default.
1760 * We do not remove it. For example, our callers expect to be able to call execve() afterwards
1761 * to run a program with the restrictions applied. */
1762 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1766 SET_FOREACH(id
, archs
) {
1767 r
= seccomp_arch_add(seccomp
, PTR_TO_UINT32(id
) - 1);
1768 if (r
< 0 && r
!= -EEXIST
)
1772 /* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
1773 * x32 syscalls should basically match x86-64 for everything except the pointer type.
1774 * The important thing is that you can block the old 32-bit x86 syscalls.
1775 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
1777 if (seccomp_arch_native() == SCMP_ARCH_X32
||
1778 set_contains(archs
, UINT32_TO_PTR(SCMP_ARCH_X32
+ 1))) {
1780 r
= seccomp_arch_add(seccomp
, SCMP_ARCH_X86_64
);
1781 if (r
< 0 && r
!= -EEXIST
)
1785 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1789 r
= seccomp_load(seccomp
);
1790 if (ERRNO_IS_SECCOMP_FATAL(r
))
1793 log_debug_errno(r
, "Failed to restrict system call architectures, skipping: %m");
1798 int parse_syscall_archs(char **l
, Set
**ret_archs
) {
1799 _cleanup_set_free_ Set
*archs
= NULL
;
1806 STRV_FOREACH(s
, l
) {
1809 r
= seccomp_arch_from_string(*s
, &a
);
1813 r
= set_ensure_put(&archs
, NULL
, UINT32_TO_PTR(a
+ 1));
1818 *ret_archs
= TAKE_PTR(archs
);
1822 int seccomp_filter_set_add(Hashmap
*filter
, bool add
, const SyscallFilterSet
*set
) {
1828 NULSTR_FOREACH(i
, set
->value
) {
1831 const SyscallFilterSet
*more
;
1833 more
= syscall_filter_set_find(i
);
1837 r
= seccomp_filter_set_add(filter
, add
, more
);
1843 id
= seccomp_syscall_resolve_name(i
);
1844 if (id
== __NR_SCMP_ERROR
) {
1845 log_debug("Couldn't resolve system call, ignoring: %s", i
);
1850 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(-1));
1854 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1861 int seccomp_lock_personality(unsigned long personality
) {
1865 if (personality
>= PERSONALITY_INVALID
)
1868 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1869 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1871 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1875 r
= seccomp_rule_add_exact(
1877 SCMP_ACT_ERRNO(EPERM
),
1878 SCMP_SYS(personality
),
1880 SCMP_A0(SCMP_CMP_NE
, personality
));
1882 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1886 r
= seccomp_load(seccomp
);
1887 if (ERRNO_IS_SECCOMP_FATAL(r
))
1890 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1896 int seccomp_protect_hostname(void) {
1900 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1901 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1903 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1907 r
= seccomp_rule_add_exact(
1909 SCMP_ACT_ERRNO(EPERM
),
1910 SCMP_SYS(sethostname
),
1913 log_debug_errno(r
, "Failed to add sethostname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1917 r
= seccomp_rule_add_exact(
1919 SCMP_ACT_ERRNO(EPERM
),
1920 SCMP_SYS(setdomainname
),
1923 log_debug_errno(r
, "Failed to add setdomainname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1927 r
= seccomp_load(seccomp
);
1928 if (ERRNO_IS_SECCOMP_FATAL(r
))
1931 log_debug_errno(r
, "Failed to apply hostname restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1937 static int seccomp_restrict_sxid(scmp_filter_ctx seccomp
, mode_t m
) {
1938 /* Checks the mode_t parameter of the following system calls:
1940 * → chmod() + fchmod() + fchmodat()
1941 * → open() + creat() + openat()
1942 * → mkdir() + mkdirat()
1943 * → mknod() + mknodat()
1945 * Returns error if *everything* failed, and 0 otherwise.
1950 r
= seccomp_rule_add_exact(
1952 SCMP_ACT_ERRNO(EPERM
),
1955 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1957 log_debug_errno(r
, "Failed to add filter for chmod: %m");
1961 r
= seccomp_rule_add_exact(
1963 SCMP_ACT_ERRNO(EPERM
),
1966 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1968 log_debug_errno(r
, "Failed to add filter for fchmod: %m");
1972 r
= seccomp_rule_add_exact(
1974 SCMP_ACT_ERRNO(EPERM
),
1977 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
1979 log_debug_errno(r
, "Failed to add filter for fchmodat: %m");
1983 r
= seccomp_rule_add_exact(
1985 SCMP_ACT_ERRNO(EPERM
),
1988 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1990 log_debug_errno(r
, "Failed to add filter for mkdir: %m");
1994 r
= seccomp_rule_add_exact(
1996 SCMP_ACT_ERRNO(EPERM
),
1999 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2001 log_debug_errno(r
, "Failed to add filter for mkdirat: %m");
2005 r
= seccomp_rule_add_exact(
2007 SCMP_ACT_ERRNO(EPERM
),
2010 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2012 log_debug_errno(r
, "Failed to add filter for mknod: %m");
2016 r
= seccomp_rule_add_exact(
2018 SCMP_ACT_ERRNO(EPERM
),
2021 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2023 log_debug_errno(r
, "Failed to add filter for mknodat: %m");
2027 #if SCMP_SYS(open) > 0
2028 r
= seccomp_rule_add_exact(
2030 SCMP_ACT_ERRNO(EPERM
),
2033 SCMP_A1(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2034 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2036 log_debug_errno(r
, "Failed to add filter for open: %m");
2041 r
= seccomp_rule_add_exact(
2043 SCMP_ACT_ERRNO(EPERM
),
2046 SCMP_A2(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2047 SCMP_A3(SCMP_CMP_MASKED_EQ
, m
, m
));
2049 log_debug_errno(r
, "Failed to add filter for openat: %m");
2053 #if defined(__SNR_openat2)
2054 /* The new openat2() system call can't be filtered sensibly, since it moves the flags parameter into
2055 * an indirect structure. Let's block it entirely for now. That should be a reasonably OK thing to do
2056 * for now, since openat2() is very new and code generally needs fallback logic anyway to be
2057 * compatible with kernels that are not absolutely recent. */
2058 r
= seccomp_rule_add_exact(
2060 SCMP_ACT_ERRNO(EPERM
),
2064 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2069 r
= seccomp_rule_add_exact(
2071 SCMP_ACT_ERRNO(EPERM
),
2074 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2076 log_debug_errno(r
, "Failed to add filter for creat: %m");
2083 int seccomp_restrict_suid_sgid(void) {
2087 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2088 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2090 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2094 r
= seccomp_restrict_sxid(seccomp
, S_ISUID
);
2096 log_debug_errno(r
, "Failed to add suid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2098 k
= seccomp_restrict_sxid(seccomp
, S_ISGID
);
2100 log_debug_errno(r
, "Failed to add sgid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2105 r
= seccomp_load(seccomp
);
2106 if (ERRNO_IS_SECCOMP_FATAL(r
))
2109 log_debug_errno(r
, "Failed to apply suid/sgid restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2115 uint32_t scmp_act_kill_process(void) {
2117 /* Returns SCMP_ACT_KILL_PROCESS if it's supported, and SCMP_ACT_KILL_THREAD otherwise. We never
2118 * actually want to use SCMP_ACT_KILL_THREAD as its semantics are nuts (killing arbitrary threads of
2119 * a program is just a bad idea), but on old kernels/old libseccomp it is all we have, and at least
2120 * for single-threaded apps does the right thing. */
2122 #ifdef SCMP_ACT_KILL_PROCESS
2123 if (seccomp_api_get() >= 3)
2124 return SCMP_ACT_KILL_PROCESS
;
2127 return SCMP_ACT_KILL
; /* same as SCMP_ACT_KILL_THREAD */