1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <linux/seccomp.h>
12 /* include missing_syscall_def.h earlier to make __SNR_foo mapped to __NR_foo. */
13 #include "missing_syscall_def.h"
17 #include "alloc-util.h"
19 #include "errno-list.h"
22 #include "nulstr-util.h"
23 #include "process-util.h"
24 #include "seccomp-util.h"
26 #include "string-util.h"
29 /* This array will be modified at runtime as seccomp_restrict_archs is called. */
30 uint32_t seccomp_local_archs
[] = {
32 /* 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 */
34 #if defined(__x86_64__) && defined(__ILP32__)
37 SCMP_ARCH_X32
, /* native */
38 #elif defined(__x86_64__) && !defined(__ILP32__)
41 SCMP_ARCH_X86_64
, /* native */
42 #elif defined(__i386__)
44 #elif defined(__aarch64__)
46 SCMP_ARCH_AARCH64
, /* native */
47 #elif defined(__arm__)
49 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
51 SCMP_ARCH_MIPS
, /* native */
52 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
54 SCMP_ARCH_MIPSEL
, /* native */
55 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
58 SCMP_ARCH_MIPSEL64N32
,
61 SCMP_ARCH_MIPS64
, /* native */
62 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
66 SCMP_ARCH_MIPSEL64N32
,
68 SCMP_ARCH_MIPSEL64
, /* native */
69 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
74 SCMP_ARCH_MIPSEL64N32
,
75 SCMP_ARCH_MIPS64N32
, /* native */
76 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
82 SCMP_ARCH_MIPSEL64N32
, /* native */
83 #elif defined(__powerpc64__) && __BYTE_ORDER == __BIG_ENDIAN
86 SCMP_ARCH_PPC64
, /* native */
87 #elif defined(__powerpc64__) && __BYTE_ORDER == __LITTLE_ENDIAN
90 SCMP_ARCH_PPC64LE
, /* native */
91 #elif defined(__powerpc__)
93 #elif defined(__riscv) && __riscv_xlen == 64 && defined(SCMP_ARCH_RISCV64)
95 #elif defined(__s390x__)
97 SCMP_ARCH_S390X
, /* native */
98 #elif defined(__s390__)
101 SECCOMP_LOCAL_ARCH_END
104 const char* seccomp_arch_to_string(uint32_t c
) {
105 /* Maintain order used in <seccomp.h>.
107 * Names used here should be the same as those used for ConditionArchitecture=,
108 * except for "subarchitectures" like x32. */
111 case SCMP_ARCH_NATIVE
:
115 case SCMP_ARCH_X86_64
:
121 case SCMP_ARCH_AARCH64
:
125 case SCMP_ARCH_MIPS64
:
127 case SCMP_ARCH_MIPS64N32
:
129 case SCMP_ARCH_MIPSEL
:
131 case SCMP_ARCH_MIPSEL64
:
133 case SCMP_ARCH_MIPSEL64N32
:
134 return "mips64-le-n32";
137 case SCMP_ARCH_PPC64
:
139 case SCMP_ARCH_PPC64LE
:
141 #ifdef SCMP_ARCH_RISCV64
142 case SCMP_ARCH_RISCV64
:
147 case SCMP_ARCH_S390X
:
154 int seccomp_arch_from_string(const char *n
, uint32_t *ret
) {
160 if (streq(n
, "native"))
161 *ret
= SCMP_ARCH_NATIVE
;
162 else if (streq(n
, "x86"))
163 *ret
= SCMP_ARCH_X86
;
164 else if (streq(n
, "x86-64"))
165 *ret
= SCMP_ARCH_X86_64
;
166 else if (streq(n
, "x32"))
167 *ret
= SCMP_ARCH_X32
;
168 else if (streq(n
, "arm"))
169 *ret
= SCMP_ARCH_ARM
;
170 else if (streq(n
, "arm64"))
171 *ret
= SCMP_ARCH_AARCH64
;
172 else if (streq(n
, "mips"))
173 *ret
= SCMP_ARCH_MIPS
;
174 else if (streq(n
, "mips64"))
175 *ret
= SCMP_ARCH_MIPS64
;
176 else if (streq(n
, "mips64-n32"))
177 *ret
= SCMP_ARCH_MIPS64N32
;
178 else if (streq(n
, "mips-le"))
179 *ret
= SCMP_ARCH_MIPSEL
;
180 else if (streq(n
, "mips64-le"))
181 *ret
= SCMP_ARCH_MIPSEL64
;
182 else if (streq(n
, "mips64-le-n32"))
183 *ret
= SCMP_ARCH_MIPSEL64N32
;
184 else if (streq(n
, "ppc"))
185 *ret
= SCMP_ARCH_PPC
;
186 else if (streq(n
, "ppc64"))
187 *ret
= SCMP_ARCH_PPC64
;
188 else if (streq(n
, "ppc64-le"))
189 *ret
= SCMP_ARCH_PPC64LE
;
190 #ifdef SCMP_ARCH_RISCV64
191 else if (streq(n
, "riscv64"))
192 *ret
= SCMP_ARCH_RISCV64
;
194 else if (streq(n
, "s390"))
195 *ret
= SCMP_ARCH_S390
;
196 else if (streq(n
, "s390x"))
197 *ret
= SCMP_ARCH_S390X
;
204 int seccomp_init_for_arch(scmp_filter_ctx
*ret
, uint32_t arch
, uint32_t default_action
) {
205 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
208 /* Much like seccomp_init(), but initializes the filter for one specific architecture only, without affecting
209 * any others. Also, turns off the NNP fiddling. */
211 seccomp
= seccomp_init(default_action
);
215 if (arch
!= SCMP_ARCH_NATIVE
&&
216 arch
!= seccomp_arch_native()) {
218 r
= seccomp_arch_remove(seccomp
, seccomp_arch_native());
222 r
= seccomp_arch_add(seccomp
, arch
);
226 assert(seccomp_arch_exist(seccomp
, arch
) >= 0);
227 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) == -EEXIST
);
228 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) == -EEXIST
);
230 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) >= 0);
231 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) >= 0);
234 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_ACT_BADARCH
, SCMP_ACT_ALLOW
);
238 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
242 #if SCMP_VER_MAJOR >= 3 || (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 4)
243 if (getenv_bool("SYSTEMD_LOG_SECCOMP") > 0) {
244 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_LOG
, 1);
246 log_debug_errno(r
, "Failed to enable seccomp event logging: %m");
250 *ret
= TAKE_PTR(seccomp
);
254 static bool is_basic_seccomp_available(void) {
255 return prctl(PR_GET_SECCOMP
, 0, 0, 0, 0) >= 0;
258 static bool is_seccomp_filter_available(void) {
259 return prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, 0, 0) < 0 &&
263 bool is_seccomp_available(void) {
264 static int cached_enabled
= -1;
266 if (cached_enabled
< 0) {
269 b
= getenv_bool_secure("SYSTEMD_SECCOMP");
271 if (b
< 0 && b
!= -ENXIO
) /* ENXIO: env var unset */
272 log_debug_errno(b
, "Failed to parse $SYSTEMD_SECCOMP value, ignoring.");
275 is_basic_seccomp_available() &&
276 is_seccomp_filter_available();
278 cached_enabled
= false;
281 return cached_enabled
;
284 const SyscallFilterSet syscall_filter_sets
[_SYSCALL_FILTER_SET_MAX
] = {
285 [SYSCALL_FILTER_SET_DEFAULT
] = {
287 .help
= "System calls that are always permitted",
289 "arch_prctl\0" /* Used during platform-specific initialization by ld-linux.so. */
293 "clock_getres_time64\0"
297 "clock_nanosleep_time64\0"
322 "getrlimit\0" /* make sure processes can query stack size and such */
339 "sched_getaffinity\0"
349 [SYSCALL_FILTER_SET_AIO
] = {
351 .help
= "Asynchronous IO",
357 "io_pgetevents_time64\0"
361 "io_uring_register\0"
364 [SYSCALL_FILTER_SET_BASIC_IO
] = {
386 [SYSCALL_FILTER_SET_CHOWN
] = {
388 .help
= "Change ownership of files and directories",
398 [SYSCALL_FILTER_SET_CLOCK
] = {
400 .help
= "Change the system time",
409 [SYSCALL_FILTER_SET_CPU_EMULATION
] = {
410 .name
= "@cpu-emulation",
411 .help
= "System calls for CPU emulation functionality",
419 [SYSCALL_FILTER_SET_DEBUG
] = {
421 .help
= "Debugging, performance monitoring and tracing functionality",
428 #if defined __s390__ || defined __s390x__
429 "s390_runtime_instr\0"
431 "sys_debug_setcontext\0"
433 [SYSCALL_FILTER_SET_FILE_SYSTEM
] = {
434 .name
= "@file-system",
435 .help
= "File system operations",
466 "inotify_add_watch\0"
514 [SYSCALL_FILTER_SET_IO_EVENT
] = {
516 .help
= "Event loop system calls",
536 [SYSCALL_FILTER_SET_IPC
] = {
538 .help
= "SysV IPC, POSIX Message Queues or other IPC",
546 "mq_timedreceive_time64\0"
548 "mq_timedsend_time64\0"
558 "process_vm_writev\0"
563 "semtimedop_time64\0"
569 [SYSCALL_FILTER_SET_KEYRING
] = {
571 .help
= "Kernel keyring access",
577 [SYSCALL_FILTER_SET_MEMLOCK
] = {
579 .help
= "Memory locking control",
587 [SYSCALL_FILTER_SET_MODULE
] = {
589 .help
= "Loading and unloading of kernel modules",
595 [SYSCALL_FILTER_SET_MOUNT
] = {
597 .help
= "Mounting and unmounting of file systems",
612 [SYSCALL_FILTER_SET_NETWORK_IO
] = {
613 .name
= "@network-io",
614 .help
= "Network or Unix socket IO, should not be needed if not network facing",
639 [SYSCALL_FILTER_SET_OBSOLETE
] = {
640 /* some unknown even to libseccomp */
642 .help
= "Unusual, obsolete or unimplemented system calls",
672 [SYSCALL_FILTER_SET_PKEY
] = {
674 .help
= "System calls used for memory protection keys",
680 [SYSCALL_FILTER_SET_PRIVILEGED
] = {
681 .name
= "@privileged",
682 .help
= "All system calls which need super-user capabilities",
698 "open_by_handle_at\0"
711 "setuid\0" /* We list the explicit system calls here, as @setuid also includes setgid() which is not necessarily privileged */
715 [SYSCALL_FILTER_SET_PROCESS
] = {
717 .help
= "Process control, execution, namespacing operations",
719 "capget\0" /* Able to query arbitrary processes */
727 "pidfd_send_signal\0"
730 "rt_tgsigqueueinfo\0"
732 "swapcontext\0" /* Some archs e.g. powerpc32 are using it to do userspace context switches */
742 [SYSCALL_FILTER_SET_RAW_IO
] = {
744 .help
= "Raw I/O port access",
751 #if defined __s390__ || defined __s390x__
752 "s390_pci_mmio_read\0"
753 "s390_pci_mmio_write\0"
756 [SYSCALL_FILTER_SET_REBOOT
] = {
758 .help
= "Reboot and reboot preparation/kexec",
764 [SYSCALL_FILTER_SET_RESOURCES
] = {
765 .name
= "@resources",
766 .help
= "Alter resource settings",
773 "sched_setaffinity\0"
776 "sched_setscheduler\0"
781 [SYSCALL_FILTER_SET_SETUID
] = {
783 .help
= "Operations for changing user/group credentials",
800 [SYSCALL_FILTER_SET_SIGNAL
] = {
802 .help
= "Process signal handling",
809 "rt_sigtimedwait_time64\0"
819 [SYSCALL_FILTER_SET_SWAP
] = {
821 .help
= "Enable/disable swap devices",
826 [SYSCALL_FILTER_SET_SYNC
] = {
828 .help
= "Synchronize files and memory to storage",
838 [SYSCALL_FILTER_SET_SYSTEM_SERVICE
] = {
839 .name
= "@system-service",
840 .help
= "General system service operations",
872 "name_to_handle_at\0"
879 "sched_get_priority_max\0"
880 "sched_get_priority_min\0"
883 "sched_getscheduler\0"
884 "sched_rr_get_interval\0"
885 "sched_rr_get_interval_time64\0"
903 [SYSCALL_FILTER_SET_TIMER
] = {
905 .help
= "Schedule operations by time",
919 "timerfd_gettime64\0"
921 "timerfd_settime64\0"
924 [SYSCALL_FILTER_SET_KNOWN
] = {
926 .help
= "All known syscalls declared in the kernel",
928 #include "syscall-list.h"
932 const SyscallFilterSet
*syscall_filter_set_find(const char *name
) {
933 if (isempty(name
) || name
[0] != '@')
936 for (unsigned i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++)
937 if (streq(syscall_filter_sets
[i
].name
, name
))
938 return syscall_filter_sets
+ i
;
943 static int add_syscall_filter_set(
944 scmp_filter_ctx seccomp
,
945 const SyscallFilterSet
*set
,
951 int seccomp_add_syscall_filter_item(
952 scmp_filter_ctx
*seccomp
,
962 if (strv_contains(exclude
, name
))
965 /* Any syscalls that are handled are added to the *added strv. The pointer
966 * must be either NULL or point to a valid pre-initialized possibly-empty strv. */
968 if (name
[0] == '@') {
969 const SyscallFilterSet
*other
;
971 other
= syscall_filter_set_find(name
);
973 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
974 "Filter set %s is not known!",
977 return add_syscall_filter_set(seccomp
, other
, action
, exclude
, log_missing
, added
);
982 id
= seccomp_syscall_resolve_name(name
);
983 if (id
== __NR_SCMP_ERROR
) {
985 log_debug("System call %s is not known, ignoring.", name
);
989 r
= seccomp_rule_add_exact(seccomp
, action
, id
, 0);
991 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
992 bool ignore
= r
== -EDOM
;
994 if (!ignore
|| log_missing
)
995 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
996 name
, id
, ignore
? ", ignoring" : "");
1002 r
= strv_extend(added
, name
);
1011 static int add_syscall_filter_set(
1012 scmp_filter_ctx seccomp
,
1013 const SyscallFilterSet
*set
,
1022 /* Any syscalls that are handled are added to the *added strv. It needs to be initialized. */
1027 NULSTR_FOREACH(sys
, set
->value
) {
1028 r
= seccomp_add_syscall_filter_item(seccomp
, sys
, action
, exclude
, log_missing
, added
);
1036 int seccomp_load_syscall_filter_set(uint32_t default_action
, const SyscallFilterSet
*set
, uint32_t action
, bool log_missing
) {
1042 /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
1043 * each local arch. */
1045 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1046 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1048 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1050 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1054 r
= add_syscall_filter_set(seccomp
, set
, action
, NULL
, log_missing
, NULL
);
1056 return log_debug_errno(r
, "Failed to add filter set: %m");
1058 r
= seccomp_load(seccomp
);
1059 if (ERRNO_IS_SECCOMP_FATAL(r
))
1062 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1068 int seccomp_load_syscall_filter_set_raw(uint32_t default_action
, Hashmap
* filter
, uint32_t action
, bool log_missing
) {
1072 /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Hashmap* of syscalls, instead
1073 * of a SyscallFilterSet* table. */
1075 if (hashmap_isempty(filter
) && default_action
== SCMP_ACT_ALLOW
)
1078 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1079 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1080 void *syscall_id
, *val
;
1082 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1084 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1088 HASHMAP_FOREACH_KEY(val
, syscall_id
, filter
) {
1089 uint32_t a
= action
;
1090 int id
= PTR_TO_INT(syscall_id
) - 1;
1091 int error
= PTR_TO_INT(val
);
1093 if (error
== SECCOMP_ERROR_NUMBER_KILL
)
1094 a
= scmp_act_kill_process();
1096 else if (action
== SCMP_ACT_LOG
)
1099 else if (error
>= 0)
1100 a
= SCMP_ACT_ERRNO(error
);
1102 r
= seccomp_rule_add_exact(seccomp
, a
, id
, 0);
1104 /* If the system call is not known on this architecture, then that's
1105 * fine, let's ignore it */
1106 _cleanup_free_
char *n
= NULL
;
1109 n
= seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE
, id
);
1110 ignore
= r
== -EDOM
;
1111 if (!ignore
|| log_missing
)
1112 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1113 strna(n
), id
, ignore
? ", ignoring" : "");
1119 r
= seccomp_load(seccomp
);
1120 if (ERRNO_IS_SECCOMP_FATAL(r
))
1123 log_debug_errno(r
, "Failed to install systemc call filter for architecture %s, skipping: %m",
1124 seccomp_arch_to_string(arch
));
1130 int seccomp_parse_syscall_filter(
1134 SeccompParseFlags flags
,
1136 const char *filename
,
1144 if (!FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
) && errno_num
>= 0)
1147 if (name
[0] == '@') {
1148 const SyscallFilterSet
*set
;
1151 set
= syscall_filter_set_find(name
);
1153 if (!FLAGS_SET(flags
, SECCOMP_PARSE_PERMISSIVE
))
1156 log_syntax(unit
, FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1157 "Unknown system call group, ignoring: %s", name
);
1161 NULSTR_FOREACH(i
, set
->value
) {
1162 /* Call ourselves again, for the group to parse. Note that we downgrade logging here (i.e. take
1163 * away the SECCOMP_PARSE_LOG flag) since any issues in the group table are our own problem,
1164 * not a problem in user configuration data and we shouldn't pretend otherwise by complaining
1166 r
= seccomp_parse_syscall_filter(i
, errno_num
, filter
, flags
&~ SECCOMP_PARSE_LOG
, unit
, filename
, line
);
1173 id
= seccomp_syscall_resolve_name(name
);
1174 if (id
== __NR_SCMP_ERROR
) {
1175 if (!FLAGS_SET(flags
, SECCOMP_PARSE_PERMISSIVE
))
1178 log_syntax(unit
, FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1179 "Failed to parse system call, ignoring: %s", name
);
1183 /* If we previously wanted to forbid a syscall and now we want to allow it, then remove
1184 * it from the list. The entries in allow-list with non-negative error value will be
1185 * handled with SCMP_ACT_ERRNO() instead of the default action. */
1186 if (!FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
) == FLAGS_SET(flags
, SECCOMP_PARSE_ALLOW_LIST
) ||
1187 (FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
| SECCOMP_PARSE_ALLOW_LIST
) && errno_num
>= 0)) {
1188 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
));
1192 return FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? log_oom() : -ENOMEM
;
1194 assert_se(hashmap_update(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
)) == 0);
1200 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1206 int seccomp_restrict_namespaces(unsigned long retain
) {
1210 if (DEBUG_LOGGING
) {
1211 _cleanup_free_
char *s
= NULL
;
1213 (void) namespace_flags_to_string(retain
, &s
);
1214 log_debug("Restricting namespace to: %s.", strna(s
));
1218 if (FLAGS_SET(retain
, NAMESPACE_FLAGS_ALL
))
1221 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1222 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1224 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1226 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1230 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
1231 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
1233 r
= seccomp_rule_add_exact(
1235 SCMP_ACT_ERRNO(EPERM
),
1239 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
1240 * special invocation with a zero flags argument, right here. */
1241 r
= seccomp_rule_add_exact(
1243 SCMP_ACT_ERRNO(EPERM
),
1246 SCMP_A1(SCMP_CMP_EQ
, 0));
1248 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1252 for (unsigned i
= 0; namespace_flag_map
[i
].name
; i
++) {
1255 f
= namespace_flag_map
[i
].flag
;
1256 if (FLAGS_SET(retain
, f
)) {
1257 log_debug("Permitting %s.", namespace_flag_map
[i
].name
);
1261 log_debug("Blocking %s.", namespace_flag_map
[i
].name
);
1263 r
= seccomp_rule_add_exact(
1265 SCMP_ACT_ERRNO(EPERM
),
1268 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1270 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1274 /* On s390/s390x the first two parameters to clone are switched */
1275 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
1276 r
= seccomp_rule_add_exact(
1278 SCMP_ACT_ERRNO(EPERM
),
1281 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1283 r
= seccomp_rule_add_exact(
1285 SCMP_ACT_ERRNO(EPERM
),
1288 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1290 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1294 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1295 r
= seccomp_rule_add_exact(
1297 SCMP_ACT_ERRNO(EPERM
),
1300 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1302 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1310 r
= seccomp_load(seccomp
);
1311 if (ERRNO_IS_SECCOMP_FATAL(r
))
1314 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1320 int seccomp_protect_sysctl(void) {
1324 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1325 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1327 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1331 #ifdef SCMP_ARCH_RISCV64
1336 /* No _sysctl syscall */
1339 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1343 r
= seccomp_rule_add_exact(
1345 SCMP_ACT_ERRNO(EPERM
),
1349 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1353 r
= seccomp_load(seccomp
);
1354 if (ERRNO_IS_SECCOMP_FATAL(r
))
1357 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1363 int seccomp_protect_syslog(void) {
1367 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1368 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1370 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1374 r
= seccomp_rule_add_exact(
1376 SCMP_ACT_ERRNO(EPERM
),
1381 log_debug_errno(r
, "Failed to add syslog() rule for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1385 r
= seccomp_load(seccomp
);
1386 if (ERRNO_IS_SECCOMP_FATAL(r
))
1389 log_debug_errno(r
, "Failed to install syslog protection rules for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1395 int seccomp_restrict_address_families(Set
*address_families
, bool allow_list
) {
1399 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1400 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1403 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1407 case SCMP_ARCH_X86_64
:
1410 case SCMP_ARCH_AARCH64
:
1411 case SCMP_ARCH_MIPSEL64N32
:
1412 case SCMP_ARCH_MIPS64N32
:
1413 case SCMP_ARCH_MIPSEL64
:
1414 case SCMP_ARCH_MIPS64
:
1415 #ifdef SCMP_ARCH_RISCV64
1416 case SCMP_ARCH_RISCV64
:
1418 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1422 case SCMP_ARCH_S390
:
1423 case SCMP_ARCH_S390X
:
1425 case SCMP_ARCH_MIPSEL
:
1426 case SCMP_ARCH_MIPS
:
1428 case SCMP_ARCH_PPC64
:
1429 case SCMP_ARCH_PPC64LE
:
1431 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1440 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1445 int first
= 0, last
= 0;
1448 /* If this is an allow list, we first block the address families that are out of
1449 * range and then everything that is not in the set. First, we find the lowest and
1450 * highest address family in the set. */
1452 SET_FOREACH(afp
, address_families
) {
1453 int af
= PTR_TO_INT(afp
);
1455 if (af
<= 0 || af
>= af_max())
1458 if (first
== 0 || af
< first
)
1461 if (last
== 0 || af
> last
)
1465 assert((first
== 0) == (last
== 0));
1469 /* No entries in the valid range, block everything */
1470 r
= seccomp_rule_add_exact(
1472 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1476 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1482 /* Block everything below the first entry */
1483 r
= seccomp_rule_add_exact(
1485 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1488 SCMP_A0(SCMP_CMP_LT
, first
));
1490 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1494 /* Block everything above the last entry */
1495 r
= seccomp_rule_add_exact(
1497 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1500 SCMP_A0(SCMP_CMP_GT
, last
));
1502 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1506 /* Block everything between the first and last entry */
1507 for (int af
= 1; af
< af_max(); af
++) {
1509 if (set_contains(address_families
, INT_TO_PTR(af
)))
1512 r
= seccomp_rule_add_exact(
1514 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1517 SCMP_A0(SCMP_CMP_EQ
, af
));
1522 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1530 /* If this is a deny list, then generate one rule for each address family that are
1531 * then combined in OR checks. */
1533 SET_FOREACH(af
, address_families
) {
1534 r
= seccomp_rule_add_exact(
1536 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1539 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1544 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1549 r
= seccomp_load(seccomp
);
1550 if (ERRNO_IS_SECCOMP_FATAL(r
))
1553 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1559 int seccomp_restrict_realtime(void) {
1560 static const int permitted_policies
[] = {
1566 int r
, max_policy
= 0;
1570 /* Determine the highest policy constant we want to allow */
1571 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1572 if (permitted_policies
[i
] > max_policy
)
1573 max_policy
= permitted_policies
[i
];
1575 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1576 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1579 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1581 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1585 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1587 for (p
= 0; p
< max_policy
; p
++) {
1590 /* Check if this is in the allow list. */
1591 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1592 if (permitted_policies
[i
] == p
) {
1600 /* Deny this policy */
1601 r
= seccomp_rule_add_exact(
1603 SCMP_ACT_ERRNO(EPERM
),
1604 SCMP_SYS(sched_setscheduler
),
1606 SCMP_A1(SCMP_CMP_EQ
, p
));
1608 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1613 /* Deny-list all other policies, i.e. the ones with higher values. Note that all comparisons
1614 * are unsigned here, hence no need no check for < 0 values. */
1615 r
= seccomp_rule_add_exact(
1617 SCMP_ACT_ERRNO(EPERM
),
1618 SCMP_SYS(sched_setscheduler
),
1620 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1622 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1626 r
= seccomp_load(seccomp
);
1627 if (ERRNO_IS_SECCOMP_FATAL(r
))
1630 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1636 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1640 const struct scmp_arg_cmp arg
) {
1643 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1645 _cleanup_free_
char *n
= NULL
;
1647 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1648 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1650 seccomp_arch_to_string(arch
));
1656 /* For known architectures, check that syscalls are indeed defined or not. */
1657 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || (defined(__riscv) && __riscv_xlen == 64)
1658 assert_cc(SCMP_SYS(shmget
) > 0);
1659 assert_cc(SCMP_SYS(shmat
) > 0);
1660 assert_cc(SCMP_SYS(shmdt
) > 0);
1663 int seccomp_memory_deny_write_execute(void) {
1665 unsigned loaded
= 0;
1667 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1668 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1669 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0, r
;
1671 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1675 /* Note that on some architectures shmat() isn't available, and the call is multiplexed through ipc().
1676 * We ignore that here, which means there's still a way to get writable/executable
1677 * memory, if an IPC key is mapped like this. That's a pity, but no total loss. */
1680 case SCMP_ARCH_S390
:
1681 filter_syscall
= SCMP_SYS(mmap2
);
1682 block_syscall
= SCMP_SYS(mmap
);
1683 /* shmat multiplexed, see above */
1687 case SCMP_ARCH_PPC64
:
1688 case SCMP_ARCH_PPC64LE
:
1689 case SCMP_ARCH_S390X
:
1690 filter_syscall
= SCMP_SYS(mmap
);
1691 /* shmat multiplexed, see above */
1695 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1696 shmat_syscall
= SCMP_SYS(shmat
);
1699 case SCMP_ARCH_X86_64
:
1701 case SCMP_ARCH_AARCH64
:
1702 #ifdef SCMP_ARCH_RISCV64
1703 case SCMP_ARCH_RISCV64
:
1705 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32, arm64 and riscv64 have only mmap */
1706 shmat_syscall
= SCMP_SYS(shmat
);
1709 /* Please add more definitions here, if you port systemd to other architectures! */
1711 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__) && !defined(__s390__) && !defined(__s390x__) && !(defined(__riscv) && __riscv_xlen == 64)
1712 #warning "Consider adding the right mmap() syscall definitions here!"
1716 /* Can't filter mmap() on this arch, then skip it */
1717 if (filter_syscall
== 0)
1720 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1724 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1726 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1730 if (block_syscall
!= 0) {
1731 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1736 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1738 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1742 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(pkey_mprotect
),
1744 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1748 if (shmat_syscall
> 0) {
1749 r
= add_seccomp_syscall_filter(seccomp
, arch
, shmat_syscall
,
1751 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1756 r
= seccomp_load(seccomp
);
1757 if (ERRNO_IS_SECCOMP_FATAL(r
))
1760 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m",
1761 seccomp_arch_to_string(arch
));
1766 log_debug("Failed to install any seccomp rules for MemoryDenyWriteExecute=.");
1771 int seccomp_restrict_archs(Set
*archs
) {
1772 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1774 bool blocked_new
= false;
1776 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1779 * There are some qualifications. However the most important use is to stop processes from bypassing
1780 * system call restrictions, in case they used a broader (multiplexing) syscall which is only available
1781 * in a non-native architecture. There are no holes in this use case, at least so far. */
1783 /* Note libseccomp includes our "native" (current) architecture in the filter by default.
1784 * We do not remove it. For example, our callers expect to be able to call execve() afterwards
1785 * to run a program with the restrictions applied. */
1786 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1790 for (unsigned i
= 0; seccomp_local_archs
[i
] != SECCOMP_LOCAL_ARCH_END
; ++i
) {
1791 uint32_t arch
= seccomp_local_archs
[i
];
1793 /* See above comment, our "native" architecture is never blocked. */
1794 if (arch
== seccomp_arch_native())
1797 /* That architecture might have already been blocked by a previous call to seccomp_restrict_archs. */
1798 if (arch
== SECCOMP_LOCAL_ARCH_BLOCKED
)
1801 bool block
= !set_contains(archs
, UINT32_TO_PTR(arch
+ 1));
1803 /* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
1804 * x32 syscalls should basically match x86-64 for everything except the pointer type.
1805 * The important thing is that you can block the old 32-bit x86 syscalls.
1806 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
1807 if (block
&& arch
== SCMP_ARCH_X86_64
&& seccomp_arch_native() == SCMP_ARCH_X32
)
1808 block
= !set_contains(archs
, UINT32_TO_PTR(SCMP_ARCH_X32
+ 1));
1811 seccomp_local_archs
[i
] = SECCOMP_LOCAL_ARCH_BLOCKED
;
1814 r
= seccomp_arch_add(seccomp
, arch
);
1815 if (r
< 0 && r
!= -EEXIST
)
1820 /* All architectures that will be blocked by the seccomp program were
1821 * already blocked. */
1825 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1829 r
= seccomp_load(seccomp
);
1830 if (ERRNO_IS_SECCOMP_FATAL(r
))
1833 log_debug_errno(r
, "Failed to restrict system call architectures, skipping: %m");
1838 int parse_syscall_archs(char **l
, Set
**ret_archs
) {
1839 _cleanup_set_free_ Set
*archs
= NULL
;
1846 STRV_FOREACH(s
, l
) {
1849 r
= seccomp_arch_from_string(*s
, &a
);
1853 r
= set_ensure_put(&archs
, NULL
, UINT32_TO_PTR(a
+ 1));
1858 *ret_archs
= TAKE_PTR(archs
);
1862 int seccomp_filter_set_add(Hashmap
*filter
, bool add
, const SyscallFilterSet
*set
) {
1868 NULSTR_FOREACH(i
, set
->value
) {
1871 const SyscallFilterSet
*more
;
1873 more
= syscall_filter_set_find(i
);
1877 r
= seccomp_filter_set_add(filter
, add
, more
);
1883 id
= seccomp_syscall_resolve_name(i
);
1884 if (id
== __NR_SCMP_ERROR
) {
1885 log_debug("Couldn't resolve system call, ignoring: %s", i
);
1890 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(-1));
1894 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1901 int seccomp_lock_personality(unsigned long personality
) {
1905 if (personality
>= PERSONALITY_INVALID
)
1908 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1909 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1911 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1915 r
= seccomp_rule_add_exact(
1917 SCMP_ACT_ERRNO(EPERM
),
1918 SCMP_SYS(personality
),
1920 SCMP_A0(SCMP_CMP_NE
, personality
));
1922 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1926 r
= seccomp_load(seccomp
);
1927 if (ERRNO_IS_SECCOMP_FATAL(r
))
1930 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1936 int seccomp_protect_hostname(void) {
1940 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1941 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1943 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1947 r
= seccomp_rule_add_exact(
1949 SCMP_ACT_ERRNO(EPERM
),
1950 SCMP_SYS(sethostname
),
1953 log_debug_errno(r
, "Failed to add sethostname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1957 r
= seccomp_rule_add_exact(
1959 SCMP_ACT_ERRNO(EPERM
),
1960 SCMP_SYS(setdomainname
),
1963 log_debug_errno(r
, "Failed to add setdomainname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1967 r
= seccomp_load(seccomp
);
1968 if (ERRNO_IS_SECCOMP_FATAL(r
))
1971 log_debug_errno(r
, "Failed to apply hostname restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1977 static int seccomp_restrict_sxid(scmp_filter_ctx seccomp
, mode_t m
) {
1978 /* Checks the mode_t parameter of the following system calls:
1980 * → chmod() + fchmod() + fchmodat()
1981 * → open() + creat() + openat()
1982 * → mkdir() + mkdirat()
1983 * → mknod() + mknodat()
1985 * Returns error if *everything* failed, and 0 otherwise.
1990 r
= seccomp_rule_add_exact(
1992 SCMP_ACT_ERRNO(EPERM
),
1995 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
1997 log_debug_errno(r
, "Failed to add filter for chmod: %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 fchmod: %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 fchmodat: %m");
2023 r
= seccomp_rule_add_exact(
2025 SCMP_ACT_ERRNO(EPERM
),
2028 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2030 log_debug_errno(r
, "Failed to add filter for mkdir: %m");
2034 r
= seccomp_rule_add_exact(
2036 SCMP_ACT_ERRNO(EPERM
),
2039 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2041 log_debug_errno(r
, "Failed to add filter for mkdirat: %m");
2045 r
= seccomp_rule_add_exact(
2047 SCMP_ACT_ERRNO(EPERM
),
2050 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2052 log_debug_errno(r
, "Failed to add filter for mknod: %m");
2056 r
= seccomp_rule_add_exact(
2058 SCMP_ACT_ERRNO(EPERM
),
2061 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2063 log_debug_errno(r
, "Failed to add filter for mknodat: %m");
2067 r
= seccomp_rule_add_exact(
2069 SCMP_ACT_ERRNO(EPERM
),
2072 SCMP_A1(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2073 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2075 log_debug_errno(r
, "Failed to add filter for open: %m");
2079 r
= seccomp_rule_add_exact(
2081 SCMP_ACT_ERRNO(EPERM
),
2084 SCMP_A2(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2085 SCMP_A3(SCMP_CMP_MASKED_EQ
, m
, m
));
2087 log_debug_errno(r
, "Failed to add filter for openat: %m");
2091 #if defined(__SNR_openat2)
2092 /* The new openat2() system call can't be filtered sensibly, since it moves the flags parameter into
2093 * an indirect structure. Let's block it entirely for now. That should be a reasonably OK thing to do
2094 * for now, since openat2() is very new and code generally needs fallback logic anyway to be
2095 * compatible with kernels that are not absolutely recent. We would normally return EPERM for a
2096 * policy check, but this isn't strictly a policy check. Instead, we return ENOSYS to force programs
2097 * to call open() or openat() instead. We can properly enforce policy for those functions. */
2098 r
= seccomp_rule_add_exact(
2100 SCMP_ACT_ERRNO(ENOSYS
),
2104 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2109 r
= seccomp_rule_add_exact(
2111 SCMP_ACT_ERRNO(EPERM
),
2114 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2116 log_debug_errno(r
, "Failed to add filter for creat: %m");
2123 int seccomp_restrict_suid_sgid(void) {
2127 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2128 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2130 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2134 r
= seccomp_restrict_sxid(seccomp
, S_ISUID
);
2136 log_debug_errno(r
, "Failed to add suid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2138 k
= seccomp_restrict_sxid(seccomp
, S_ISGID
);
2140 log_debug_errno(r
, "Failed to add sgid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2145 r
= seccomp_load(seccomp
);
2146 if (ERRNO_IS_SECCOMP_FATAL(r
))
2149 log_debug_errno(r
, "Failed to apply suid/sgid restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2155 uint32_t scmp_act_kill_process(void) {
2157 /* Returns SCMP_ACT_KILL_PROCESS if it's supported, and SCMP_ACT_KILL_THREAD otherwise. We never
2158 * actually want to use SCMP_ACT_KILL_THREAD as its semantics are nuts (killing arbitrary threads of
2159 * a program is just a bad idea), but on old kernels/old libseccomp it is all we have, and at least
2160 * for single-threaded apps does the right thing. */
2162 #ifdef SCMP_ACT_KILL_PROCESS
2163 if (seccomp_api_get() >= 3)
2164 return SCMP_ACT_KILL_PROCESS
;
2167 return SCMP_ACT_KILL
; /* same as SCMP_ACT_KILL_THREAD */
2170 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
2171 _cleanup_free_
char *n
= NULL
;
2180 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
2181 * If errno is omitted, then error is set to -1.
2182 * Empty syscall name is not allowed.
2183 * Here, we do not check that the syscall name is valid or not.
2186 p
= strchr(in
, ':');
2188 e
= seccomp_parse_errno_or_action(p
+ 1);
2192 n
= strndup(in
, p
- in
);
2203 *name
= TAKE_PTR(n
);
2208 static int block_open_flag(scmp_filter_ctx seccomp
, int flag
) {
2212 /* Blocks open() with the specified flag, where flag is O_SYNC or so. This makes these calls return
2213 * EINVAL, in the hope the client code will retry without O_SYNC then. */
2215 r
= seccomp_rule_add_exact(
2217 SCMP_ACT_ERRNO(EINVAL
),
2220 SCMP_A1(SCMP_CMP_MASKED_EQ
, flag
, flag
));
2222 log_debug_errno(r
, "Failed to add filter for open: %m");
2226 r
= seccomp_rule_add_exact(
2228 SCMP_ACT_ERRNO(EINVAL
),
2231 SCMP_A2(SCMP_CMP_MASKED_EQ
, flag
, flag
));
2233 log_debug_errno(r
, "Failed to add filter for openat: %m");
2237 #if defined(__SNR_openat2)
2238 /* The new openat2() system call can't be filtered sensibly, see above. */
2239 r
= seccomp_rule_add_exact(
2241 SCMP_ACT_ERRNO(ENOSYS
),
2245 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2253 int seccomp_suppress_sync(void) {
2257 /* This is mostly identical to SystemCallFilter=~@sync:0, but simpler to use, and separately
2258 * manageable, and also masks O_SYNC/O_DSYNC */
2260 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2261 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2264 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2268 NULSTR_FOREACH(c
, syscall_filter_sets
[SYSCALL_FILTER_SET_SYNC
].value
) {
2271 id
= seccomp_syscall_resolve_name(c
);
2272 if (id
== __NR_SCMP_ERROR
) {
2273 log_debug("System call %s is not known, ignoring.", c
);
2277 r
= seccomp_rule_add_exact(
2279 SCMP_ACT_ERRNO(0), /* success → we want this to be a NOP after all */
2283 log_debug_errno(r
, "Failed to add filter for system call %s, ignoring: %m", c
);
2286 (void) block_open_flag(seccomp
, O_SYNC
);
2287 #if O_DSYNC != O_SYNC
2288 (void) block_open_flag(seccomp
, O_DSYNC
);
2291 r
= seccomp_load(seccomp
);
2292 if (ERRNO_IS_SECCOMP_FATAL(r
))
2295 log_debug_errno(r
, "Failed to apply sync() suppression for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));