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 */
721 /* ia64 as the only architecture has clone2, a replacement for clone, but ia64 doesn't
722 * implement seccomp, so we don't need to list it at all. C.f.
723 * acce2f71779c54086962fefce3833d886c655f62 in the kernel. */
730 "pidfd_send_signal\0"
733 "rt_tgsigqueueinfo\0"
735 "swapcontext\0" /* Some archs e.g. powerpc32 are using it to do userspace context switches */
745 [SYSCALL_FILTER_SET_RAW_IO
] = {
747 .help
= "Raw I/O port access",
754 #if defined __s390__ || defined __s390x__
755 "s390_pci_mmio_read\0"
756 "s390_pci_mmio_write\0"
759 [SYSCALL_FILTER_SET_REBOOT
] = {
761 .help
= "Reboot and reboot preparation/kexec",
767 [SYSCALL_FILTER_SET_RESOURCES
] = {
768 .name
= "@resources",
769 .help
= "Alter resource settings",
776 "sched_setaffinity\0"
779 "sched_setscheduler\0"
784 [SYSCALL_FILTER_SET_SETUID
] = {
786 .help
= "Operations for changing user/group credentials",
803 [SYSCALL_FILTER_SET_SIGNAL
] = {
805 .help
= "Process signal handling",
812 "rt_sigtimedwait_time64\0"
822 [SYSCALL_FILTER_SET_SWAP
] = {
824 .help
= "Enable/disable swap devices",
829 [SYSCALL_FILTER_SET_SYNC
] = {
831 .help
= "Synchronize files and memory to storage",
841 [SYSCALL_FILTER_SET_SYSTEM_SERVICE
] = {
842 .name
= "@system-service",
843 .help
= "General system service operations",
875 "name_to_handle_at\0"
882 "sched_get_priority_max\0"
883 "sched_get_priority_min\0"
886 "sched_getscheduler\0"
887 "sched_rr_get_interval\0"
888 "sched_rr_get_interval_time64\0"
906 [SYSCALL_FILTER_SET_TIMER
] = {
908 .help
= "Schedule operations by time",
922 "timerfd_gettime64\0"
924 "timerfd_settime64\0"
927 [SYSCALL_FILTER_SET_KNOWN
] = {
929 .help
= "All known syscalls declared in the kernel",
931 #include "syscall-list.h"
935 const SyscallFilterSet
*syscall_filter_set_find(const char *name
) {
936 if (isempty(name
) || name
[0] != '@')
939 for (unsigned i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++)
940 if (streq(syscall_filter_sets
[i
].name
, name
))
941 return syscall_filter_sets
+ i
;
946 static int add_syscall_filter_set(
947 scmp_filter_ctx seccomp
,
948 const SyscallFilterSet
*set
,
954 int seccomp_add_syscall_filter_item(
955 scmp_filter_ctx
*seccomp
,
965 if (strv_contains(exclude
, name
))
968 /* Any syscalls that are handled are added to the *added strv. The pointer
969 * must be either NULL or point to a valid pre-initialized possibly-empty strv. */
971 if (name
[0] == '@') {
972 const SyscallFilterSet
*other
;
974 other
= syscall_filter_set_find(name
);
976 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
977 "Filter set %s is not known!",
980 return add_syscall_filter_set(seccomp
, other
, action
, exclude
, log_missing
, added
);
985 id
= seccomp_syscall_resolve_name(name
);
986 if (id
== __NR_SCMP_ERROR
) {
988 log_debug("System call %s is not known, ignoring.", name
);
992 r
= seccomp_rule_add_exact(seccomp
, action
, id
, 0);
994 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
995 bool ignore
= r
== -EDOM
;
997 if (!ignore
|| log_missing
)
998 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
999 name
, id
, ignore
? ", ignoring" : "");
1005 r
= strv_extend(added
, name
);
1014 static int add_syscall_filter_set(
1015 scmp_filter_ctx seccomp
,
1016 const SyscallFilterSet
*set
,
1025 /* Any syscalls that are handled are added to the *added strv. It needs to be initialized. */
1030 NULSTR_FOREACH(sys
, set
->value
) {
1031 r
= seccomp_add_syscall_filter_item(seccomp
, sys
, action
, exclude
, log_missing
, added
);
1039 int seccomp_load_syscall_filter_set(uint32_t default_action
, const SyscallFilterSet
*set
, uint32_t action
, bool log_missing
) {
1045 /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
1046 * each local arch. */
1048 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1049 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1051 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1053 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1057 r
= add_syscall_filter_set(seccomp
, set
, action
, NULL
, log_missing
, NULL
);
1059 return log_debug_errno(r
, "Failed to add filter set: %m");
1061 r
= seccomp_load(seccomp
);
1062 if (ERRNO_IS_SECCOMP_FATAL(r
))
1065 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1071 int seccomp_load_syscall_filter_set_raw(uint32_t default_action
, Hashmap
* filter
, uint32_t action
, bool log_missing
) {
1075 /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Hashmap* of syscalls, instead
1076 * of a SyscallFilterSet* table. */
1078 if (hashmap_isempty(filter
) && default_action
== SCMP_ACT_ALLOW
)
1081 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1082 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1083 void *syscall_id
, *val
;
1085 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1087 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1091 HASHMAP_FOREACH_KEY(val
, syscall_id
, filter
) {
1092 uint32_t a
= action
;
1093 int id
= PTR_TO_INT(syscall_id
) - 1;
1094 int error
= PTR_TO_INT(val
);
1096 if (error
== SECCOMP_ERROR_NUMBER_KILL
)
1097 a
= scmp_act_kill_process();
1099 else if (action
== SCMP_ACT_LOG
)
1102 else if (error
>= 0)
1103 a
= SCMP_ACT_ERRNO(error
);
1105 r
= seccomp_rule_add_exact(seccomp
, a
, id
, 0);
1107 /* If the system call is not known on this architecture, then that's
1108 * fine, let's ignore it */
1109 _cleanup_free_
char *n
= NULL
;
1112 n
= seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE
, id
);
1113 ignore
= r
== -EDOM
;
1114 if (!ignore
|| log_missing
)
1115 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1116 strna(n
), id
, ignore
? ", ignoring" : "");
1122 r
= seccomp_load(seccomp
);
1123 if (ERRNO_IS_SECCOMP_FATAL(r
))
1126 log_debug_errno(r
, "Failed to install systemc call filter for architecture %s, skipping: %m",
1127 seccomp_arch_to_string(arch
));
1133 int seccomp_parse_syscall_filter(
1137 SeccompParseFlags flags
,
1139 const char *filename
,
1147 if (!FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
) && errno_num
>= 0)
1150 if (name
[0] == '@') {
1151 const SyscallFilterSet
*set
;
1154 set
= syscall_filter_set_find(name
);
1156 if (!FLAGS_SET(flags
, SECCOMP_PARSE_PERMISSIVE
))
1159 log_syntax(unit
, FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1160 "Unknown system call group, ignoring: %s", name
);
1164 NULSTR_FOREACH(i
, set
->value
) {
1165 /* Call ourselves again, for the group to parse. Note that we downgrade logging here (i.e. take
1166 * away the SECCOMP_PARSE_LOG flag) since any issues in the group table are our own problem,
1167 * not a problem in user configuration data and we shouldn't pretend otherwise by complaining
1169 r
= seccomp_parse_syscall_filter(i
, errno_num
, filter
, flags
&~ SECCOMP_PARSE_LOG
, unit
, filename
, line
);
1176 id
= seccomp_syscall_resolve_name(name
);
1177 if (id
== __NR_SCMP_ERROR
) {
1178 if (!FLAGS_SET(flags
, SECCOMP_PARSE_PERMISSIVE
))
1181 log_syntax(unit
, FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1182 "Failed to parse system call, ignoring: %s", name
);
1186 /* If we previously wanted to forbid a syscall and now we want to allow it, then remove
1187 * it from the list. The entries in allow-list with non-negative error value will be
1188 * handled with SCMP_ACT_ERRNO() instead of the default action. */
1189 if (!FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
) == FLAGS_SET(flags
, SECCOMP_PARSE_ALLOW_LIST
) ||
1190 (FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
| SECCOMP_PARSE_ALLOW_LIST
) && errno_num
>= 0)) {
1191 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
));
1195 return FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? log_oom() : -ENOMEM
;
1197 assert_se(hashmap_update(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
)) == 0);
1203 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1209 int seccomp_restrict_namespaces(unsigned long retain
) {
1213 if (DEBUG_LOGGING
) {
1214 _cleanup_free_
char *s
= NULL
;
1216 (void) namespace_flags_to_string(retain
, &s
);
1217 log_debug("Restricting namespace to: %s.", strna(s
));
1221 if (FLAGS_SET(retain
, NAMESPACE_FLAGS_ALL
))
1224 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1225 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1227 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1229 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1233 /* We cannot filter on individual flags to clone3(), and we need to disable the
1234 * syscall altogether. ENOSYS is used instead of EPERM, so that glibc and other
1235 * users shall fall back to clone(), as if on an older kernel.
1237 * C.f. https://github.com/flatpak/flatpak/commit/a10f52a7565c549612c92b8e736a6698a53db330,
1238 * https://github.com/moby/moby/issues/42680. */
1240 r
= seccomp_rule_add_exact(
1242 SCMP_ACT_ERRNO(ENOSYS
),
1246 log_debug_errno(r
, "Failed to add clone3() rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
1248 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
1249 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
1251 r
= seccomp_rule_add_exact(
1253 SCMP_ACT_ERRNO(EPERM
),
1257 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
1258 * special invocation with a zero flags argument, right here. */
1259 r
= seccomp_rule_add_exact(
1261 SCMP_ACT_ERRNO(EPERM
),
1264 SCMP_A1(SCMP_CMP_EQ
, 0));
1266 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1270 for (unsigned i
= 0; namespace_flag_map
[i
].name
; i
++) {
1273 f
= namespace_flag_map
[i
].flag
;
1274 if (FLAGS_SET(retain
, f
)) {
1275 log_debug("Permitting %s.", namespace_flag_map
[i
].name
);
1279 log_debug("Blocking %s.", namespace_flag_map
[i
].name
);
1281 r
= seccomp_rule_add_exact(
1283 SCMP_ACT_ERRNO(EPERM
),
1286 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1288 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1292 /* On s390/s390x the first two parameters to clone are switched */
1293 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
1294 r
= seccomp_rule_add_exact(
1296 SCMP_ACT_ERRNO(EPERM
),
1299 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1301 r
= seccomp_rule_add_exact(
1303 SCMP_ACT_ERRNO(EPERM
),
1306 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1308 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1312 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1313 r
= seccomp_rule_add_exact(
1315 SCMP_ACT_ERRNO(EPERM
),
1318 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1320 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1328 r
= seccomp_load(seccomp
);
1329 if (ERRNO_IS_SECCOMP_FATAL(r
))
1332 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1338 int seccomp_protect_sysctl(void) {
1342 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1343 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1345 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1349 #ifdef SCMP_ARCH_RISCV64
1354 /* No _sysctl syscall */
1357 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1361 r
= seccomp_rule_add_exact(
1363 SCMP_ACT_ERRNO(EPERM
),
1367 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1371 r
= seccomp_load(seccomp
);
1372 if (ERRNO_IS_SECCOMP_FATAL(r
))
1375 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1381 int seccomp_protect_syslog(void) {
1385 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1386 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1388 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1392 r
= seccomp_rule_add_exact(
1394 SCMP_ACT_ERRNO(EPERM
),
1399 log_debug_errno(r
, "Failed to add syslog() rule for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1403 r
= seccomp_load(seccomp
);
1404 if (ERRNO_IS_SECCOMP_FATAL(r
))
1407 log_debug_errno(r
, "Failed to install syslog protection rules for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1413 int seccomp_restrict_address_families(Set
*address_families
, bool allow_list
) {
1417 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1418 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1421 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1425 case SCMP_ARCH_X86_64
:
1428 case SCMP_ARCH_AARCH64
:
1429 case SCMP_ARCH_MIPSEL64N32
:
1430 case SCMP_ARCH_MIPS64N32
:
1431 case SCMP_ARCH_MIPSEL64
:
1432 case SCMP_ARCH_MIPS64
:
1433 #ifdef SCMP_ARCH_RISCV64
1434 case SCMP_ARCH_RISCV64
:
1436 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1440 case SCMP_ARCH_S390
:
1441 case SCMP_ARCH_S390X
:
1443 case SCMP_ARCH_MIPSEL
:
1444 case SCMP_ARCH_MIPS
:
1446 case SCMP_ARCH_PPC64
:
1447 case SCMP_ARCH_PPC64LE
:
1449 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1458 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1463 int first
= 0, last
= 0;
1466 /* If this is an allow list, we first block the address families that are out of
1467 * range and then everything that is not in the set. First, we find the lowest and
1468 * highest address family in the set. */
1470 SET_FOREACH(afp
, address_families
) {
1471 int af
= PTR_TO_INT(afp
);
1473 if (af
<= 0 || af
>= af_max())
1476 if (first
== 0 || af
< first
)
1479 if (last
== 0 || af
> last
)
1483 assert((first
== 0) == (last
== 0));
1487 /* No entries in the valid range, block everything */
1488 r
= seccomp_rule_add_exact(
1490 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1494 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1500 /* Block everything below the first entry */
1501 r
= seccomp_rule_add_exact(
1503 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1506 SCMP_A0(SCMP_CMP_LT
, first
));
1508 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1512 /* Block everything above the last entry */
1513 r
= seccomp_rule_add_exact(
1515 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1518 SCMP_A0(SCMP_CMP_GT
, last
));
1520 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1524 /* Block everything between the first and last entry */
1525 for (int af
= 1; af
< af_max(); af
++) {
1527 if (set_contains(address_families
, INT_TO_PTR(af
)))
1530 r
= seccomp_rule_add_exact(
1532 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1535 SCMP_A0(SCMP_CMP_EQ
, af
));
1540 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1548 /* If this is a deny list, then generate one rule for each address family that are
1549 * then combined in OR checks. */
1551 SET_FOREACH(af
, address_families
) {
1552 r
= seccomp_rule_add_exact(
1554 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1557 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1562 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1567 r
= seccomp_load(seccomp
);
1568 if (ERRNO_IS_SECCOMP_FATAL(r
))
1571 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1577 int seccomp_restrict_realtime(void) {
1578 static const int permitted_policies
[] = {
1584 int r
, max_policy
= 0;
1588 /* Determine the highest policy constant we want to allow */
1589 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1590 if (permitted_policies
[i
] > max_policy
)
1591 max_policy
= permitted_policies
[i
];
1593 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1594 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1597 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1599 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1603 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1605 for (p
= 0; p
< max_policy
; p
++) {
1608 /* Check if this is in the allow list. */
1609 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1610 if (permitted_policies
[i
] == p
) {
1618 /* Deny this policy */
1619 r
= seccomp_rule_add_exact(
1621 SCMP_ACT_ERRNO(EPERM
),
1622 SCMP_SYS(sched_setscheduler
),
1624 SCMP_A1(SCMP_CMP_EQ
, p
));
1626 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1631 /* Deny-list all other policies, i.e. the ones with higher values. Note that all comparisons
1632 * are unsigned here, hence no need no check for < 0 values. */
1633 r
= seccomp_rule_add_exact(
1635 SCMP_ACT_ERRNO(EPERM
),
1636 SCMP_SYS(sched_setscheduler
),
1638 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1640 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1644 r
= seccomp_load(seccomp
);
1645 if (ERRNO_IS_SECCOMP_FATAL(r
))
1648 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1654 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1658 const struct scmp_arg_cmp arg
) {
1661 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1663 _cleanup_free_
char *n
= NULL
;
1665 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1666 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1668 seccomp_arch_to_string(arch
));
1674 /* For known architectures, check that syscalls are indeed defined or not. */
1675 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || (defined(__riscv) && __riscv_xlen == 64)
1676 assert_cc(SCMP_SYS(shmget
) > 0);
1677 assert_cc(SCMP_SYS(shmat
) > 0);
1678 assert_cc(SCMP_SYS(shmdt
) > 0);
1681 int seccomp_memory_deny_write_execute(void) {
1683 unsigned loaded
= 0;
1685 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1686 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1687 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0, r
;
1689 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1693 /* Note that on some architectures shmat() isn't available, and the call is multiplexed through ipc().
1694 * We ignore that here, which means there's still a way to get writable/executable
1695 * memory, if an IPC key is mapped like this. That's a pity, but no total loss. */
1698 case SCMP_ARCH_S390
:
1699 filter_syscall
= SCMP_SYS(mmap2
);
1700 block_syscall
= SCMP_SYS(mmap
);
1701 /* shmat multiplexed, see above */
1705 case SCMP_ARCH_PPC64
:
1706 case SCMP_ARCH_PPC64LE
:
1707 case SCMP_ARCH_S390X
:
1708 filter_syscall
= SCMP_SYS(mmap
);
1709 /* shmat multiplexed, see above */
1713 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1714 shmat_syscall
= SCMP_SYS(shmat
);
1717 case SCMP_ARCH_X86_64
:
1719 case SCMP_ARCH_AARCH64
:
1720 #ifdef SCMP_ARCH_RISCV64
1721 case SCMP_ARCH_RISCV64
:
1723 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32, arm64 and riscv64 have only mmap */
1724 shmat_syscall
= SCMP_SYS(shmat
);
1727 /* Please add more definitions here, if you port systemd to other architectures! */
1729 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__) && !defined(__s390__) && !defined(__s390x__) && !(defined(__riscv) && __riscv_xlen == 64)
1730 #warning "Consider adding the right mmap() syscall definitions here!"
1734 /* Can't filter mmap() on this arch, then skip it */
1735 if (filter_syscall
== 0)
1738 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1742 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1744 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1748 if (block_syscall
!= 0) {
1749 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1754 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1756 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1760 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(pkey_mprotect
),
1762 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1766 if (shmat_syscall
> 0) {
1767 r
= add_seccomp_syscall_filter(seccomp
, arch
, shmat_syscall
,
1769 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1774 r
= seccomp_load(seccomp
);
1775 if (ERRNO_IS_SECCOMP_FATAL(r
))
1778 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m",
1779 seccomp_arch_to_string(arch
));
1784 log_debug("Failed to install any seccomp rules for MemoryDenyWriteExecute=.");
1789 int seccomp_restrict_archs(Set
*archs
) {
1790 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1792 bool blocked_new
= false;
1794 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1797 * There are some qualifications. However the most important use is to stop processes from bypassing
1798 * system call restrictions, in case they used a broader (multiplexing) syscall which is only available
1799 * in a non-native architecture. There are no holes in this use case, at least so far. */
1801 /* Note libseccomp includes our "native" (current) architecture in the filter by default.
1802 * We do not remove it. For example, our callers expect to be able to call execve() afterwards
1803 * to run a program with the restrictions applied. */
1804 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1808 for (unsigned i
= 0; seccomp_local_archs
[i
] != SECCOMP_LOCAL_ARCH_END
; ++i
) {
1809 uint32_t arch
= seccomp_local_archs
[i
];
1811 /* See above comment, our "native" architecture is never blocked. */
1812 if (arch
== seccomp_arch_native())
1815 /* That architecture might have already been blocked by a previous call to seccomp_restrict_archs. */
1816 if (arch
== SECCOMP_LOCAL_ARCH_BLOCKED
)
1819 bool block
= !set_contains(archs
, UINT32_TO_PTR(arch
+ 1));
1821 /* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
1822 * x32 syscalls should basically match x86-64 for everything except the pointer type.
1823 * The important thing is that you can block the old 32-bit x86 syscalls.
1824 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
1825 if (block
&& arch
== SCMP_ARCH_X86_64
&& seccomp_arch_native() == SCMP_ARCH_X32
)
1826 block
= !set_contains(archs
, UINT32_TO_PTR(SCMP_ARCH_X32
+ 1));
1829 seccomp_local_archs
[i
] = SECCOMP_LOCAL_ARCH_BLOCKED
;
1832 r
= seccomp_arch_add(seccomp
, arch
);
1833 if (r
< 0 && r
!= -EEXIST
)
1838 /* All architectures that will be blocked by the seccomp program were
1839 * already blocked. */
1843 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1847 r
= seccomp_load(seccomp
);
1848 if (ERRNO_IS_SECCOMP_FATAL(r
))
1851 log_debug_errno(r
, "Failed to restrict system call architectures, skipping: %m");
1856 int parse_syscall_archs(char **l
, Set
**ret_archs
) {
1857 _cleanup_set_free_ Set
*archs
= NULL
;
1863 STRV_FOREACH(s
, l
) {
1866 r
= seccomp_arch_from_string(*s
, &a
);
1870 r
= set_ensure_put(&archs
, NULL
, UINT32_TO_PTR(a
+ 1));
1875 *ret_archs
= TAKE_PTR(archs
);
1879 int seccomp_filter_set_add(Hashmap
*filter
, bool add
, const SyscallFilterSet
*set
) {
1885 NULSTR_FOREACH(i
, set
->value
) {
1888 const SyscallFilterSet
*more
;
1890 more
= syscall_filter_set_find(i
);
1894 r
= seccomp_filter_set_add(filter
, add
, more
);
1900 id
= seccomp_syscall_resolve_name(i
);
1901 if (id
== __NR_SCMP_ERROR
) {
1902 log_debug("Couldn't resolve system call, ignoring: %s", i
);
1907 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(-1));
1911 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1918 int seccomp_lock_personality(unsigned long personality
) {
1922 if (personality
>= PERSONALITY_INVALID
)
1925 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1926 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1928 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1932 r
= seccomp_rule_add_exact(
1934 SCMP_ACT_ERRNO(EPERM
),
1935 SCMP_SYS(personality
),
1937 SCMP_A0(SCMP_CMP_NE
, personality
));
1939 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1943 r
= seccomp_load(seccomp
);
1944 if (ERRNO_IS_SECCOMP_FATAL(r
))
1947 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1953 int seccomp_protect_hostname(void) {
1957 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1958 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1960 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1964 r
= seccomp_rule_add_exact(
1966 SCMP_ACT_ERRNO(EPERM
),
1967 SCMP_SYS(sethostname
),
1970 log_debug_errno(r
, "Failed to add sethostname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1974 r
= seccomp_rule_add_exact(
1976 SCMP_ACT_ERRNO(EPERM
),
1977 SCMP_SYS(setdomainname
),
1980 log_debug_errno(r
, "Failed to add setdomainname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1984 r
= seccomp_load(seccomp
);
1985 if (ERRNO_IS_SECCOMP_FATAL(r
))
1988 log_debug_errno(r
, "Failed to apply hostname restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1994 static int seccomp_restrict_sxid(scmp_filter_ctx seccomp
, mode_t m
) {
1995 /* Checks the mode_t parameter of the following system calls:
1997 * → chmod() + fchmod() + fchmodat()
1998 * → open() + creat() + openat()
1999 * → mkdir() + mkdirat()
2000 * → mknod() + mknodat()
2002 * Returns error if *everything* failed, and 0 otherwise.
2007 r
= seccomp_rule_add_exact(
2009 SCMP_ACT_ERRNO(EPERM
),
2012 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2014 log_debug_errno(r
, "Failed to add filter for chmod: %m");
2018 r
= seccomp_rule_add_exact(
2020 SCMP_ACT_ERRNO(EPERM
),
2023 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2025 log_debug_errno(r
, "Failed to add filter for fchmod: %m");
2029 r
= seccomp_rule_add_exact(
2031 SCMP_ACT_ERRNO(EPERM
),
2034 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2036 log_debug_errno(r
, "Failed to add filter for fchmodat: %m");
2040 r
= seccomp_rule_add_exact(
2042 SCMP_ACT_ERRNO(EPERM
),
2045 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2047 log_debug_errno(r
, "Failed to add filter for mkdir: %m");
2051 r
= seccomp_rule_add_exact(
2053 SCMP_ACT_ERRNO(EPERM
),
2056 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2058 log_debug_errno(r
, "Failed to add filter for mkdirat: %m");
2062 r
= seccomp_rule_add_exact(
2064 SCMP_ACT_ERRNO(EPERM
),
2067 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2069 log_debug_errno(r
, "Failed to add filter for mknod: %m");
2073 r
= seccomp_rule_add_exact(
2075 SCMP_ACT_ERRNO(EPERM
),
2078 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2080 log_debug_errno(r
, "Failed to add filter for mknodat: %m");
2084 r
= seccomp_rule_add_exact(
2086 SCMP_ACT_ERRNO(EPERM
),
2089 SCMP_A1(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2090 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2092 log_debug_errno(r
, "Failed to add filter for open: %m");
2096 r
= seccomp_rule_add_exact(
2098 SCMP_ACT_ERRNO(EPERM
),
2101 SCMP_A2(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2102 SCMP_A3(SCMP_CMP_MASKED_EQ
, m
, m
));
2104 log_debug_errno(r
, "Failed to add filter for openat: %m");
2108 #if defined(__SNR_openat2)
2109 /* The new openat2() system call can't be filtered sensibly, since it moves the flags parameter into
2110 * an indirect structure. Let's block it entirely for now. That should be a reasonably OK thing to do
2111 * for now, since openat2() is very new and code generally needs fallback logic anyway to be
2112 * compatible with kernels that are not absolutely recent. We would normally return EPERM for a
2113 * policy check, but this isn't strictly a policy check. Instead, we return ENOSYS to force programs
2114 * to call open() or openat() instead. We can properly enforce policy for those functions. */
2115 r
= seccomp_rule_add_exact(
2117 SCMP_ACT_ERRNO(ENOSYS
),
2121 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2126 r
= seccomp_rule_add_exact(
2128 SCMP_ACT_ERRNO(EPERM
),
2131 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2133 log_debug_errno(r
, "Failed to add filter for creat: %m");
2140 int seccomp_restrict_suid_sgid(void) {
2144 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2145 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2147 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2151 r
= seccomp_restrict_sxid(seccomp
, S_ISUID
);
2153 log_debug_errno(r
, "Failed to add suid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2155 k
= seccomp_restrict_sxid(seccomp
, S_ISGID
);
2157 log_debug_errno(r
, "Failed to add sgid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2162 r
= seccomp_load(seccomp
);
2163 if (ERRNO_IS_SECCOMP_FATAL(r
))
2166 log_debug_errno(r
, "Failed to apply suid/sgid restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2172 uint32_t scmp_act_kill_process(void) {
2174 /* Returns SCMP_ACT_KILL_PROCESS if it's supported, and SCMP_ACT_KILL_THREAD otherwise. We never
2175 * actually want to use SCMP_ACT_KILL_THREAD as its semantics are nuts (killing arbitrary threads of
2176 * a program is just a bad idea), but on old kernels/old libseccomp it is all we have, and at least
2177 * for single-threaded apps does the right thing. */
2179 #ifdef SCMP_ACT_KILL_PROCESS
2180 if (seccomp_api_get() >= 3)
2181 return SCMP_ACT_KILL_PROCESS
;
2184 return SCMP_ACT_KILL
; /* same as SCMP_ACT_KILL_THREAD */
2187 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
2188 _cleanup_free_
char *n
= NULL
;
2197 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
2198 * If errno is omitted, then error is set to -1.
2199 * Empty syscall name is not allowed.
2200 * Here, we do not check that the syscall name is valid or not.
2203 p
= strchr(in
, ':');
2205 e
= seccomp_parse_errno_or_action(p
+ 1);
2209 n
= strndup(in
, p
- in
);
2220 *name
= TAKE_PTR(n
);
2225 static int block_open_flag(scmp_filter_ctx seccomp
, int flag
) {
2229 /* Blocks open() with the specified flag, where flag is O_SYNC or so. This makes these calls return
2230 * EINVAL, in the hope the client code will retry without O_SYNC then. */
2232 r
= seccomp_rule_add_exact(
2234 SCMP_ACT_ERRNO(EINVAL
),
2237 SCMP_A1(SCMP_CMP_MASKED_EQ
, flag
, flag
));
2239 log_debug_errno(r
, "Failed to add filter for open: %m");
2243 r
= seccomp_rule_add_exact(
2245 SCMP_ACT_ERRNO(EINVAL
),
2248 SCMP_A2(SCMP_CMP_MASKED_EQ
, flag
, flag
));
2250 log_debug_errno(r
, "Failed to add filter for openat: %m");
2254 #if defined(__SNR_openat2)
2255 /* The new openat2() system call can't be filtered sensibly, see above. */
2256 r
= seccomp_rule_add_exact(
2258 SCMP_ACT_ERRNO(ENOSYS
),
2262 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2270 int seccomp_suppress_sync(void) {
2274 /* This is mostly identical to SystemCallFilter=~@sync:0, but simpler to use, and separately
2275 * manageable, and also masks O_SYNC/O_DSYNC */
2277 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2278 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2281 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2285 NULSTR_FOREACH(c
, syscall_filter_sets
[SYSCALL_FILTER_SET_SYNC
].value
) {
2288 id
= seccomp_syscall_resolve_name(c
);
2289 if (id
== __NR_SCMP_ERROR
) {
2290 log_debug("System call %s is not known, ignoring.", c
);
2294 r
= seccomp_rule_add_exact(
2296 SCMP_ACT_ERRNO(0), /* success → we want this to be a NOP after all */
2300 log_debug_errno(r
, "Failed to add filter for system call %s, ignoring: %m", c
);
2303 (void) block_open_flag(seccomp
, O_SYNC
);
2304 #if O_DSYNC != O_SYNC
2305 (void) block_open_flag(seccomp
, O_DSYNC
);
2308 r
= seccomp_load(seccomp
);
2309 if (ERRNO_IS_SECCOMP_FATAL(r
))
2312 log_debug_errno(r
, "Failed to apply sync() suppression for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));