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(__hppa64__) && defined(SCMP_ARCH_PARISC) && defined(SCMP_ARCH_PARISC64)
85 SCMP_ARCH_PARISC64
, /* native */
86 #elif defined(__hppa__) && defined(SCMP_ARCH_PARISC)
88 #elif defined(__powerpc64__) && __BYTE_ORDER == __BIG_ENDIAN
91 SCMP_ARCH_PPC64
, /* native */
92 #elif defined(__powerpc64__) && __BYTE_ORDER == __LITTLE_ENDIAN
95 SCMP_ARCH_PPC64LE
, /* native */
96 #elif defined(__powerpc__)
98 #elif defined(__riscv) && __riscv_xlen == 64 && defined(SCMP_ARCH_RISCV64)
100 #elif defined(__s390x__)
102 SCMP_ARCH_S390X
, /* native */
103 #elif defined(__s390__)
106 SECCOMP_LOCAL_ARCH_END
109 const char* seccomp_arch_to_string(uint32_t c
) {
110 /* Maintain order used in <seccomp.h>.
112 * Names used here should be the same as those used for ConditionArchitecture=,
113 * except for "subarchitectures" like x32. */
116 case SCMP_ARCH_NATIVE
:
120 case SCMP_ARCH_X86_64
:
126 case SCMP_ARCH_AARCH64
:
130 case SCMP_ARCH_MIPS64
:
132 case SCMP_ARCH_MIPS64N32
:
134 case SCMP_ARCH_MIPSEL
:
136 case SCMP_ARCH_MIPSEL64
:
138 case SCMP_ARCH_MIPSEL64N32
:
139 return "mips64-le-n32";
140 #ifdef SCMP_ARCH_PARISC
141 case SCMP_ARCH_PARISC
:
144 #ifdef SCMP_ARCH_PARISC64
145 case SCMP_ARCH_PARISC64
:
150 case SCMP_ARCH_PPC64
:
152 case SCMP_ARCH_PPC64LE
:
154 #ifdef SCMP_ARCH_RISCV64
155 case SCMP_ARCH_RISCV64
:
160 case SCMP_ARCH_S390X
:
167 int seccomp_arch_from_string(const char *n
, uint32_t *ret
) {
173 if (streq(n
, "native"))
174 *ret
= SCMP_ARCH_NATIVE
;
175 else if (streq(n
, "x86"))
176 *ret
= SCMP_ARCH_X86
;
177 else if (streq(n
, "x86-64"))
178 *ret
= SCMP_ARCH_X86_64
;
179 else if (streq(n
, "x32"))
180 *ret
= SCMP_ARCH_X32
;
181 else if (streq(n
, "arm"))
182 *ret
= SCMP_ARCH_ARM
;
183 else if (streq(n
, "arm64"))
184 *ret
= SCMP_ARCH_AARCH64
;
185 else if (streq(n
, "mips"))
186 *ret
= SCMP_ARCH_MIPS
;
187 else if (streq(n
, "mips64"))
188 *ret
= SCMP_ARCH_MIPS64
;
189 else if (streq(n
, "mips64-n32"))
190 *ret
= SCMP_ARCH_MIPS64N32
;
191 else if (streq(n
, "mips-le"))
192 *ret
= SCMP_ARCH_MIPSEL
;
193 else if (streq(n
, "mips64-le"))
194 *ret
= SCMP_ARCH_MIPSEL64
;
195 else if (streq(n
, "mips64-le-n32"))
196 *ret
= SCMP_ARCH_MIPSEL64N32
;
197 #ifdef SCMP_ARCH_PARISC
198 else if (streq(n
, "parisc"))
199 *ret
= SCMP_ARCH_PARISC
;
201 #ifdef SCMP_ARCH_PARISC64
202 else if (streq(n
, "parisc64"))
203 *ret
= SCMP_ARCH_PARISC64
;
205 else if (streq(n
, "ppc"))
206 *ret
= SCMP_ARCH_PPC
;
207 else if (streq(n
, "ppc64"))
208 *ret
= SCMP_ARCH_PPC64
;
209 else if (streq(n
, "ppc64-le"))
210 *ret
= SCMP_ARCH_PPC64LE
;
211 #ifdef SCMP_ARCH_RISCV64
212 else if (streq(n
, "riscv64"))
213 *ret
= SCMP_ARCH_RISCV64
;
215 else if (streq(n
, "s390"))
216 *ret
= SCMP_ARCH_S390
;
217 else if (streq(n
, "s390x"))
218 *ret
= SCMP_ARCH_S390X
;
225 int seccomp_init_for_arch(scmp_filter_ctx
*ret
, uint32_t arch
, uint32_t default_action
) {
226 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
229 /* Much like seccomp_init(), but initializes the filter for one specific architecture only, without affecting
230 * any others. Also, turns off the NNP fiddling. */
232 seccomp
= seccomp_init(default_action
);
236 if (arch
!= SCMP_ARCH_NATIVE
&&
237 arch
!= seccomp_arch_native()) {
239 r
= seccomp_arch_remove(seccomp
, seccomp_arch_native());
243 r
= seccomp_arch_add(seccomp
, arch
);
247 assert(seccomp_arch_exist(seccomp
, arch
) >= 0);
248 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) == -EEXIST
);
249 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) == -EEXIST
);
251 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) >= 0);
252 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) >= 0);
255 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_ACT_BADARCH
, SCMP_ACT_ALLOW
);
259 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
263 #if SCMP_VER_MAJOR >= 3 || (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 4)
264 if (getenv_bool("SYSTEMD_LOG_SECCOMP") > 0) {
265 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_LOG
, 1);
267 log_debug_errno(r
, "Failed to enable seccomp event logging: %m");
271 *ret
= TAKE_PTR(seccomp
);
275 static bool is_basic_seccomp_available(void) {
276 return prctl(PR_GET_SECCOMP
, 0, 0, 0, 0) >= 0;
279 static bool is_seccomp_filter_available(void) {
280 return prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, 0, 0) < 0 &&
284 bool is_seccomp_available(void) {
285 static int cached_enabled
= -1;
287 if (cached_enabled
< 0) {
290 b
= getenv_bool_secure("SYSTEMD_SECCOMP");
292 if (b
< 0 && b
!= -ENXIO
) /* ENXIO: env var unset */
293 log_debug_errno(b
, "Failed to parse $SYSTEMD_SECCOMP value, ignoring.");
296 is_basic_seccomp_available() &&
297 is_seccomp_filter_available();
299 cached_enabled
= false;
302 return cached_enabled
;
305 const SyscallFilterSet syscall_filter_sets
[_SYSCALL_FILTER_SET_MAX
] = {
306 [SYSCALL_FILTER_SET_DEFAULT
] = {
308 .help
= "System calls that are always permitted",
310 "arch_prctl\0" /* Used during platform-specific initialization by ld-linux.so. */
314 "clock_getres_time64\0"
318 "clock_nanosleep_time64\0"
343 "getrlimit\0" /* make sure processes can query stack size and such */
360 "sched_getaffinity\0"
370 [SYSCALL_FILTER_SET_AIO
] = {
372 .help
= "Asynchronous IO",
378 "io_pgetevents_time64\0"
382 "io_uring_register\0"
385 [SYSCALL_FILTER_SET_BASIC_IO
] = {
407 [SYSCALL_FILTER_SET_CHOWN
] = {
409 .help
= "Change ownership of files and directories",
419 [SYSCALL_FILTER_SET_CLOCK
] = {
421 .help
= "Change the system time",
430 [SYSCALL_FILTER_SET_CPU_EMULATION
] = {
431 .name
= "@cpu-emulation",
432 .help
= "System calls for CPU emulation functionality",
440 [SYSCALL_FILTER_SET_DEBUG
] = {
442 .help
= "Debugging, performance monitoring and tracing functionality",
449 #if defined __s390__ || defined __s390x__
450 "s390_runtime_instr\0"
452 "sys_debug_setcontext\0"
454 [SYSCALL_FILTER_SET_FILE_SYSTEM
] = {
455 .name
= "@file-system",
456 .help
= "File system operations",
487 "inotify_add_watch\0"
535 [SYSCALL_FILTER_SET_IO_EVENT
] = {
537 .help
= "Event loop system calls",
557 [SYSCALL_FILTER_SET_IPC
] = {
559 .help
= "SysV IPC, POSIX Message Queues or other IPC",
567 "mq_timedreceive_time64\0"
569 "mq_timedsend_time64\0"
579 "process_vm_writev\0"
584 "semtimedop_time64\0"
590 [SYSCALL_FILTER_SET_KEYRING
] = {
592 .help
= "Kernel keyring access",
598 [SYSCALL_FILTER_SET_MEMLOCK
] = {
600 .help
= "Memory locking control",
608 [SYSCALL_FILTER_SET_MODULE
] = {
610 .help
= "Loading and unloading of kernel modules",
616 [SYSCALL_FILTER_SET_MOUNT
] = {
618 .help
= "Mounting and unmounting of file systems",
633 [SYSCALL_FILTER_SET_NETWORK_IO
] = {
634 .name
= "@network-io",
635 .help
= "Network or Unix socket IO, should not be needed if not network facing",
660 [SYSCALL_FILTER_SET_OBSOLETE
] = {
661 /* some unknown even to libseccomp */
663 .help
= "Unusual, obsolete or unimplemented system calls",
693 [SYSCALL_FILTER_SET_PKEY
] = {
695 .help
= "System calls used for memory protection keys",
701 [SYSCALL_FILTER_SET_PRIVILEGED
] = {
702 .name
= "@privileged",
703 .help
= "All system calls which need super-user capabilities",
719 "open_by_handle_at\0"
732 "setuid\0" /* We list the explicit system calls here, as @setuid also includes setgid() which is not necessarily privileged */
736 [SYSCALL_FILTER_SET_PROCESS
] = {
738 .help
= "Process control, execution, namespacing operations",
740 "capget\0" /* Able to query arbitrary processes */
742 /* ia64 as the only architecture has clone2, a replacement for clone, but ia64 doesn't
743 * implement seccomp, so we don't need to list it at all. C.f.
744 * acce2f71779c54086962fefce3833d886c655f62 in the kernel. */
751 "pidfd_send_signal\0"
754 "rt_tgsigqueueinfo\0"
756 "swapcontext\0" /* Some archs e.g. powerpc32 are using it to do userspace context switches */
766 [SYSCALL_FILTER_SET_RAW_IO
] = {
768 .help
= "Raw I/O port access",
775 #if defined __s390__ || defined __s390x__
776 "s390_pci_mmio_read\0"
777 "s390_pci_mmio_write\0"
780 [SYSCALL_FILTER_SET_REBOOT
] = {
782 .help
= "Reboot and reboot preparation/kexec",
788 [SYSCALL_FILTER_SET_RESOURCES
] = {
789 .name
= "@resources",
790 .help
= "Alter resource settings",
797 "sched_setaffinity\0"
800 "sched_setscheduler\0"
805 [SYSCALL_FILTER_SET_SETUID
] = {
807 .help
= "Operations for changing user/group credentials",
824 [SYSCALL_FILTER_SET_SIGNAL
] = {
826 .help
= "Process signal handling",
833 "rt_sigtimedwait_time64\0"
843 [SYSCALL_FILTER_SET_SWAP
] = {
845 .help
= "Enable/disable swap devices",
850 [SYSCALL_FILTER_SET_SYNC
] = {
852 .help
= "Synchronize files and memory to storage",
862 [SYSCALL_FILTER_SET_SYSTEM_SERVICE
] = {
863 .name
= "@system-service",
864 .help
= "General system service operations",
896 "name_to_handle_at\0"
903 "sched_get_priority_max\0"
904 "sched_get_priority_min\0"
907 "sched_getscheduler\0"
908 "sched_rr_get_interval\0"
909 "sched_rr_get_interval_time64\0"
927 [SYSCALL_FILTER_SET_TIMER
] = {
929 .help
= "Schedule operations by time",
943 "timerfd_gettime64\0"
945 "timerfd_settime64\0"
948 [SYSCALL_FILTER_SET_KNOWN
] = {
950 .help
= "All known syscalls declared in the kernel",
953 #include "syscall-list.h"
957 const SyscallFilterSet
*syscall_filter_set_find(const char *name
) {
958 if (isempty(name
) || name
[0] != '@')
961 for (unsigned i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++)
962 if (streq(syscall_filter_sets
[i
].name
, name
))
963 return syscall_filter_sets
+ i
;
968 static int add_syscall_filter_set(
969 scmp_filter_ctx seccomp
,
970 const SyscallFilterSet
*set
,
976 int seccomp_add_syscall_filter_item(
977 scmp_filter_ctx
*seccomp
,
987 if (strv_contains(exclude
, name
))
990 /* Any syscalls that are handled are added to the *added strv. The pointer
991 * must be either NULL or point to a valid pre-initialized possibly-empty strv. */
993 if (name
[0] == '@') {
994 const SyscallFilterSet
*other
;
996 other
= syscall_filter_set_find(name
);
998 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
999 "Filter set %s is not known!",
1002 return add_syscall_filter_set(seccomp
, other
, action
, exclude
, log_missing
, added
);
1007 id
= seccomp_syscall_resolve_name(name
);
1008 if (id
== __NR_SCMP_ERROR
) {
1010 log_debug("System call %s is not known, ignoring.", name
);
1014 r
= seccomp_rule_add_exact(seccomp
, action
, id
, 0);
1016 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
1017 bool ignore
= r
== -EDOM
;
1019 if (!ignore
|| log_missing
)
1020 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1021 name
, id
, ignore
? ", ignoring" : "");
1027 r
= strv_extend(added
, name
);
1036 static int add_syscall_filter_set(
1037 scmp_filter_ctx seccomp
,
1038 const SyscallFilterSet
*set
,
1047 /* Any syscalls that are handled are added to the *added strv. It needs to be initialized. */
1052 NULSTR_FOREACH(sys
, set
->value
) {
1053 r
= seccomp_add_syscall_filter_item(seccomp
, sys
, action
, exclude
, log_missing
, added
);
1061 int seccomp_load_syscall_filter_set(uint32_t default_action
, const SyscallFilterSet
*set
, uint32_t action
, bool log_missing
) {
1067 /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
1068 * each local arch. */
1070 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1071 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1073 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1075 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1079 r
= add_syscall_filter_set(seccomp
, set
, action
, NULL
, log_missing
, NULL
);
1081 return log_debug_errno(r
, "Failed to add filter set: %m");
1083 r
= seccomp_load(seccomp
);
1084 if (ERRNO_IS_SECCOMP_FATAL(r
))
1087 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1093 int seccomp_load_syscall_filter_set_raw(uint32_t default_action
, Hashmap
* filter
, uint32_t action
, bool log_missing
) {
1097 /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Hashmap* of syscalls, instead
1098 * of a SyscallFilterSet* table. */
1100 if (hashmap_isempty(filter
) && default_action
== SCMP_ACT_ALLOW
)
1103 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1104 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1105 void *syscall_id
, *val
;
1107 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1109 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1113 HASHMAP_FOREACH_KEY(val
, syscall_id
, filter
) {
1114 uint32_t a
= action
;
1115 int id
= PTR_TO_INT(syscall_id
) - 1;
1116 int error
= PTR_TO_INT(val
);
1118 if (error
== SECCOMP_ERROR_NUMBER_KILL
)
1119 a
= scmp_act_kill_process();
1121 else if (action
== SCMP_ACT_LOG
)
1124 else if (error
>= 0)
1125 a
= SCMP_ACT_ERRNO(error
);
1127 r
= seccomp_rule_add_exact(seccomp
, a
, id
, 0);
1129 /* If the system call is not known on this architecture, then that's
1130 * fine, let's ignore it */
1131 _cleanup_free_
char *n
= NULL
;
1134 n
= seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE
, id
);
1135 ignore
= r
== -EDOM
;
1136 if (!ignore
|| log_missing
)
1137 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1138 strna(n
), id
, ignore
? ", ignoring" : "");
1144 r
= seccomp_load(seccomp
);
1145 if (ERRNO_IS_SECCOMP_FATAL(r
))
1148 log_debug_errno(r
, "Failed to install system call filter for architecture %s, skipping: %m",
1149 seccomp_arch_to_string(arch
));
1155 int seccomp_parse_syscall_filter(
1159 SeccompParseFlags flags
,
1161 const char *filename
,
1169 if (!FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
) && errno_num
>= 0)
1172 if (name
[0] == '@') {
1173 const SyscallFilterSet
*set
;
1176 set
= syscall_filter_set_find(name
);
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 "Unknown system call group, ignoring: %s", name
);
1186 NULSTR_FOREACH(i
, set
->value
) {
1187 /* Call ourselves again, for the group to parse. Note that we downgrade logging here (i.e. take
1188 * away the SECCOMP_PARSE_LOG flag) since any issues in the group table are our own problem,
1189 * not a problem in user configuration data and we shouldn't pretend otherwise by complaining
1191 r
= seccomp_parse_syscall_filter(i
, errno_num
, filter
, flags
&~ SECCOMP_PARSE_LOG
, unit
, filename
, line
);
1198 id
= seccomp_syscall_resolve_name(name
);
1199 if (id
== __NR_SCMP_ERROR
) {
1200 if (!FLAGS_SET(flags
, SECCOMP_PARSE_PERMISSIVE
))
1203 log_syntax(unit
, FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1204 "Failed to parse system call, ignoring: %s", name
);
1208 /* If we previously wanted to forbid a syscall and now we want to allow it, then remove
1209 * it from the list. The entries in allow-list with non-negative error value will be
1210 * handled with SCMP_ACT_ERRNO() instead of the default action. */
1211 if (!FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
) == FLAGS_SET(flags
, SECCOMP_PARSE_ALLOW_LIST
) ||
1212 (FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
| SECCOMP_PARSE_ALLOW_LIST
) && errno_num
>= 0)) {
1213 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
));
1217 return FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? log_oom() : -ENOMEM
;
1219 assert_se(hashmap_update(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
)) == 0);
1225 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1231 int seccomp_restrict_namespaces(unsigned long retain
) {
1235 if (DEBUG_LOGGING
) {
1236 _cleanup_free_
char *s
= NULL
;
1238 (void) namespace_flags_to_string(retain
, &s
);
1239 log_debug("Restricting namespace to: %s.", strna(s
));
1243 if (FLAGS_SET(retain
, NAMESPACE_FLAGS_ALL
))
1246 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1247 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1249 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1251 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1255 /* We cannot filter on individual flags to clone3(), and we need to disable the
1256 * syscall altogether. ENOSYS is used instead of EPERM, so that glibc and other
1257 * users shall fall back to clone(), as if on an older kernel.
1259 * C.f. https://github.com/flatpak/flatpak/commit/a10f52a7565c549612c92b8e736a6698a53db330,
1260 * https://github.com/moby/moby/issues/42680. */
1262 r
= seccomp_rule_add_exact(
1264 SCMP_ACT_ERRNO(ENOSYS
),
1268 log_debug_errno(r
, "Failed to add clone3() rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
1270 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
1271 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
1273 r
= seccomp_rule_add_exact(
1275 SCMP_ACT_ERRNO(EPERM
),
1279 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
1280 * special invocation with a zero flags argument, right here. */
1281 r
= seccomp_rule_add_exact(
1283 SCMP_ACT_ERRNO(EPERM
),
1286 SCMP_A1(SCMP_CMP_EQ
, 0));
1288 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1292 for (unsigned i
= 0; namespace_flag_map
[i
].name
; i
++) {
1295 f
= namespace_flag_map
[i
].flag
;
1296 if (FLAGS_SET(retain
, f
)) {
1297 log_debug("Permitting %s.", namespace_flag_map
[i
].name
);
1301 log_debug("Blocking %s.", namespace_flag_map
[i
].name
);
1303 r
= seccomp_rule_add_exact(
1305 SCMP_ACT_ERRNO(EPERM
),
1308 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1310 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1314 /* On s390/s390x the first two parameters to clone are switched */
1315 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
1316 r
= seccomp_rule_add_exact(
1318 SCMP_ACT_ERRNO(EPERM
),
1321 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1323 r
= seccomp_rule_add_exact(
1325 SCMP_ACT_ERRNO(EPERM
),
1328 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1330 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1334 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1335 r
= seccomp_rule_add_exact(
1337 SCMP_ACT_ERRNO(EPERM
),
1340 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1342 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1350 r
= seccomp_load(seccomp
);
1351 if (ERRNO_IS_SECCOMP_FATAL(r
))
1354 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1360 int seccomp_protect_sysctl(void) {
1364 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1365 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1367 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1371 #ifdef SCMP_ARCH_RISCV64
1376 /* No _sysctl syscall */
1379 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1383 r
= seccomp_rule_add_exact(
1385 SCMP_ACT_ERRNO(EPERM
),
1389 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1393 r
= seccomp_load(seccomp
);
1394 if (ERRNO_IS_SECCOMP_FATAL(r
))
1397 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1403 int seccomp_protect_syslog(void) {
1407 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1408 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1410 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1414 r
= seccomp_rule_add_exact(
1416 SCMP_ACT_ERRNO(EPERM
),
1421 log_debug_errno(r
, "Failed to add syslog() rule for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1425 r
= seccomp_load(seccomp
);
1426 if (ERRNO_IS_SECCOMP_FATAL(r
))
1429 log_debug_errno(r
, "Failed to install syslog protection rules for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1435 int seccomp_restrict_address_families(Set
*address_families
, bool allow_list
) {
1439 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1440 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1443 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1447 case SCMP_ARCH_X86_64
:
1450 case SCMP_ARCH_AARCH64
:
1451 case SCMP_ARCH_MIPSEL64N32
:
1452 case SCMP_ARCH_MIPS64N32
:
1453 case SCMP_ARCH_MIPSEL64
:
1454 case SCMP_ARCH_MIPS64
:
1455 #ifdef SCMP_ARCH_RISCV64
1456 case SCMP_ARCH_RISCV64
:
1458 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1462 case SCMP_ARCH_S390
:
1463 case SCMP_ARCH_S390X
:
1465 case SCMP_ARCH_MIPSEL
:
1466 case SCMP_ARCH_MIPS
:
1467 #ifdef SCMP_ARCH_PARISC
1468 case SCMP_ARCH_PARISC
:
1470 #ifdef SCMP_ARCH_PARISC64
1471 case SCMP_ARCH_PARISC64
:
1474 case SCMP_ARCH_PPC64
:
1475 case SCMP_ARCH_PPC64LE
:
1477 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1486 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1491 int first
= 0, last
= 0;
1494 /* If this is an allow list, we first block the address families that are out of
1495 * range and then everything that is not in the set. First, we find the lowest and
1496 * highest address family in the set. */
1498 SET_FOREACH(afp
, address_families
) {
1499 int af
= PTR_TO_INT(afp
);
1501 if (af
<= 0 || af
>= af_max())
1504 if (first
== 0 || af
< first
)
1507 if (last
== 0 || af
> last
)
1511 assert((first
== 0) == (last
== 0));
1515 /* No entries in the valid range, block everything */
1516 r
= seccomp_rule_add_exact(
1518 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1522 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1528 /* Block everything below the first entry */
1529 r
= seccomp_rule_add_exact(
1531 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1534 SCMP_A0(SCMP_CMP_LT
, first
));
1536 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1540 /* Block everything above the last entry */
1541 r
= seccomp_rule_add_exact(
1543 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1546 SCMP_A0(SCMP_CMP_GT
, last
));
1548 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1552 /* Block everything between the first and last entry */
1553 for (int af
= 1; af
< af_max(); af
++) {
1555 if (set_contains(address_families
, INT_TO_PTR(af
)))
1558 r
= seccomp_rule_add_exact(
1560 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1563 SCMP_A0(SCMP_CMP_EQ
, af
));
1568 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1576 /* If this is a deny list, then generate one rule for each address family that are
1577 * then combined in OR checks. */
1579 SET_FOREACH(af
, address_families
) {
1580 r
= seccomp_rule_add_exact(
1582 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1585 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1590 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1595 r
= seccomp_load(seccomp
);
1596 if (ERRNO_IS_SECCOMP_FATAL(r
))
1599 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1605 int seccomp_restrict_realtime_full(int error_code
) {
1606 static const int permitted_policies
[] = {
1612 int r
, max_policy
= 0;
1616 assert(error_code
> 0);
1618 /* Determine the highest policy constant we want to allow */
1619 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1620 if (permitted_policies
[i
] > max_policy
)
1621 max_policy
= permitted_policies
[i
];
1623 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1624 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1627 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1629 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1633 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1635 for (p
= 0; p
< max_policy
; p
++) {
1638 /* Check if this is in the allow list. */
1639 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1640 if (permitted_policies
[i
] == p
) {
1648 /* Deny this policy */
1649 r
= seccomp_rule_add_exact(
1651 SCMP_ACT_ERRNO(error_code
),
1652 SCMP_SYS(sched_setscheduler
),
1654 SCMP_A1(SCMP_CMP_EQ
, p
));
1656 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1661 /* Deny-list all other policies, i.e. the ones with higher values. Note that all comparisons
1662 * are unsigned here, hence no need no check for < 0 values. */
1663 r
= seccomp_rule_add_exact(
1665 SCMP_ACT_ERRNO(error_code
),
1666 SCMP_SYS(sched_setscheduler
),
1668 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1670 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1674 r
= seccomp_load(seccomp
);
1675 if (ERRNO_IS_SECCOMP_FATAL(r
))
1678 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1684 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1688 const struct scmp_arg_cmp arg
) {
1691 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1693 _cleanup_free_
char *n
= NULL
;
1695 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1696 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1698 seccomp_arch_to_string(arch
));
1704 /* For known architectures, check that syscalls are indeed defined or not. */
1705 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || (defined(__riscv) && __riscv_xlen == 64)
1706 assert_cc(SCMP_SYS(shmget
) > 0);
1707 assert_cc(SCMP_SYS(shmat
) > 0);
1708 assert_cc(SCMP_SYS(shmdt
) > 0);
1711 int seccomp_memory_deny_write_execute(void) {
1713 unsigned loaded
= 0;
1715 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1716 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1717 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0, r
;
1719 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1723 /* Note that on some architectures shmat() isn't available, and the call is multiplexed through ipc().
1724 * We ignore that here, which means there's still a way to get writable/executable
1725 * memory, if an IPC key is mapped like this. That's a pity, but no total loss.
1727 * Also, PARISC isn't here right now because it still needs executable memory, but work is in progress
1728 * on that front (kernel work done in 5.18).
1732 case SCMP_ARCH_S390
:
1733 filter_syscall
= SCMP_SYS(mmap2
);
1734 block_syscall
= SCMP_SYS(mmap
);
1735 /* shmat multiplexed, see above */
1739 case SCMP_ARCH_PPC64
:
1740 case SCMP_ARCH_PPC64LE
:
1741 case SCMP_ARCH_S390X
:
1742 filter_syscall
= SCMP_SYS(mmap
);
1743 /* shmat multiplexed, see above */
1747 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1748 shmat_syscall
= SCMP_SYS(shmat
);
1751 case SCMP_ARCH_X86_64
:
1753 case SCMP_ARCH_AARCH64
:
1754 #ifdef SCMP_ARCH_RISCV64
1755 case SCMP_ARCH_RISCV64
:
1757 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32, arm64 and riscv64 have only mmap */
1758 shmat_syscall
= SCMP_SYS(shmat
);
1761 /* Please add more definitions here, if you port systemd to other architectures! */
1763 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__hppa__) && !defined(__hppa64__) && !defined(__powerpc__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__) && !defined(__s390__) && !defined(__s390x__) && !(defined(__riscv) && __riscv_xlen == 64)
1764 #warning "Consider adding the right mmap() syscall definitions here!"
1768 /* Can't filter mmap() on this arch, then skip it */
1769 if (filter_syscall
== 0)
1772 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1776 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1778 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1782 if (block_syscall
!= 0) {
1783 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1788 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1790 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1794 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(pkey_mprotect
),
1796 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1800 if (shmat_syscall
> 0) {
1801 r
= add_seccomp_syscall_filter(seccomp
, arch
, shmat_syscall
,
1803 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1808 r
= seccomp_load(seccomp
);
1809 if (ERRNO_IS_SECCOMP_FATAL(r
))
1812 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m",
1813 seccomp_arch_to_string(arch
));
1818 log_debug("Failed to install any seccomp rules for MemoryDenyWriteExecute=.");
1823 int seccomp_restrict_archs(Set
*archs
) {
1824 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1826 bool blocked_new
= false;
1828 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1831 * There are some qualifications. However the most important use is to stop processes from bypassing
1832 * system call restrictions, in case they used a broader (multiplexing) syscall which is only available
1833 * in a non-native architecture. There are no holes in this use case, at least so far. */
1835 /* Note libseccomp includes our "native" (current) architecture in the filter by default.
1836 * We do not remove it. For example, our callers expect to be able to call execve() afterwards
1837 * to run a program with the restrictions applied. */
1838 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1842 for (unsigned i
= 0; seccomp_local_archs
[i
] != SECCOMP_LOCAL_ARCH_END
; ++i
) {
1843 uint32_t arch
= seccomp_local_archs
[i
];
1845 /* See above comment, our "native" architecture is never blocked. */
1846 if (arch
== seccomp_arch_native())
1849 /* That architecture might have already been blocked by a previous call to seccomp_restrict_archs. */
1850 if (arch
== SECCOMP_LOCAL_ARCH_BLOCKED
)
1853 bool block
= !set_contains(archs
, UINT32_TO_PTR(arch
+ 1));
1855 /* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
1856 * x32 syscalls should basically match x86-64 for everything except the pointer type.
1857 * The important thing is that you can block the old 32-bit x86 syscalls.
1858 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
1859 if (block
&& arch
== SCMP_ARCH_X86_64
&& seccomp_arch_native() == SCMP_ARCH_X32
)
1860 block
= !set_contains(archs
, UINT32_TO_PTR(SCMP_ARCH_X32
+ 1));
1863 seccomp_local_archs
[i
] = SECCOMP_LOCAL_ARCH_BLOCKED
;
1866 r
= seccomp_arch_add(seccomp
, arch
);
1867 if (r
< 0 && r
!= -EEXIST
)
1872 /* All architectures that will be blocked by the seccomp program were
1873 * already blocked. */
1877 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1881 r
= seccomp_load(seccomp
);
1882 if (ERRNO_IS_SECCOMP_FATAL(r
))
1885 log_debug_errno(r
, "Failed to restrict system call architectures, skipping: %m");
1890 int parse_syscall_archs(char **l
, Set
**ret_archs
) {
1891 _cleanup_set_free_ Set
*archs
= NULL
;
1897 STRV_FOREACH(s
, l
) {
1900 r
= seccomp_arch_from_string(*s
, &a
);
1904 r
= set_ensure_put(&archs
, NULL
, UINT32_TO_PTR(a
+ 1));
1909 *ret_archs
= TAKE_PTR(archs
);
1913 int seccomp_filter_set_add(Hashmap
*filter
, bool add
, const SyscallFilterSet
*set
) {
1919 NULSTR_FOREACH(i
, set
->value
) {
1922 const SyscallFilterSet
*more
;
1924 more
= syscall_filter_set_find(i
);
1928 r
= seccomp_filter_set_add(filter
, add
, more
);
1934 id
= seccomp_syscall_resolve_name(i
);
1935 if (id
== __NR_SCMP_ERROR
) {
1936 log_debug("Couldn't resolve system call, ignoring: %s", i
);
1941 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(-1));
1945 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1952 int seccomp_lock_personality(unsigned long personality
) {
1956 if (personality
>= PERSONALITY_INVALID
)
1959 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1960 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1962 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1966 r
= seccomp_rule_add_exact(
1968 SCMP_ACT_ERRNO(EPERM
),
1969 SCMP_SYS(personality
),
1971 SCMP_A0(SCMP_CMP_NE
, personality
));
1973 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1977 r
= seccomp_load(seccomp
);
1978 if (ERRNO_IS_SECCOMP_FATAL(r
))
1981 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1987 int seccomp_protect_hostname(void) {
1991 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1992 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1994 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1998 r
= seccomp_rule_add_exact(
2000 SCMP_ACT_ERRNO(EPERM
),
2001 SCMP_SYS(sethostname
),
2004 log_debug_errno(r
, "Failed to add sethostname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2008 r
= seccomp_rule_add_exact(
2010 SCMP_ACT_ERRNO(EPERM
),
2011 SCMP_SYS(setdomainname
),
2014 log_debug_errno(r
, "Failed to add setdomainname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2018 r
= seccomp_load(seccomp
);
2019 if (ERRNO_IS_SECCOMP_FATAL(r
))
2022 log_debug_errno(r
, "Failed to apply hostname restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2028 static int seccomp_restrict_sxid(scmp_filter_ctx seccomp
, mode_t m
) {
2029 /* Checks the mode_t parameter of the following system calls:
2031 * → chmod() + fchmod() + fchmodat()
2032 * → open() + creat() + openat()
2033 * → mkdir() + mkdirat()
2034 * → mknod() + mknodat()
2036 * Returns error if *everything* failed, and 0 otherwise.
2041 r
= seccomp_rule_add_exact(
2043 SCMP_ACT_ERRNO(EPERM
),
2046 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2048 log_debug_errno(r
, "Failed to add filter for chmod: %m");
2052 r
= seccomp_rule_add_exact(
2054 SCMP_ACT_ERRNO(EPERM
),
2057 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2059 log_debug_errno(r
, "Failed to add filter for fchmod: %m");
2063 r
= seccomp_rule_add_exact(
2065 SCMP_ACT_ERRNO(EPERM
),
2068 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2070 log_debug_errno(r
, "Failed to add filter for fchmodat: %m");
2074 r
= seccomp_rule_add_exact(
2076 SCMP_ACT_ERRNO(EPERM
),
2079 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2081 log_debug_errno(r
, "Failed to add filter for mkdir: %m");
2085 r
= seccomp_rule_add_exact(
2087 SCMP_ACT_ERRNO(EPERM
),
2090 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2092 log_debug_errno(r
, "Failed to add filter for mkdirat: %m");
2096 r
= seccomp_rule_add_exact(
2098 SCMP_ACT_ERRNO(EPERM
),
2101 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2103 log_debug_errno(r
, "Failed to add filter for mknod: %m");
2107 r
= seccomp_rule_add_exact(
2109 SCMP_ACT_ERRNO(EPERM
),
2112 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2114 log_debug_errno(r
, "Failed to add filter for mknodat: %m");
2118 r
= seccomp_rule_add_exact(
2120 SCMP_ACT_ERRNO(EPERM
),
2123 SCMP_A1(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2124 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2126 log_debug_errno(r
, "Failed to add filter for open: %m");
2130 r
= seccomp_rule_add_exact(
2132 SCMP_ACT_ERRNO(EPERM
),
2135 SCMP_A2(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2136 SCMP_A3(SCMP_CMP_MASKED_EQ
, m
, m
));
2138 log_debug_errno(r
, "Failed to add filter for openat: %m");
2142 #if defined(__SNR_openat2)
2143 /* The new openat2() system call can't be filtered sensibly, since it moves the flags parameter into
2144 * an indirect structure. Let's block it entirely for now. That should be a reasonably OK thing to do
2145 * for now, since openat2() is very new and code generally needs fallback logic anyway to be
2146 * compatible with kernels that are not absolutely recent. We would normally return EPERM for a
2147 * policy check, but this isn't strictly a policy check. Instead, we return ENOSYS to force programs
2148 * to call open() or openat() instead. We can properly enforce policy for those functions. */
2149 r
= seccomp_rule_add_exact(
2151 SCMP_ACT_ERRNO(ENOSYS
),
2155 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2160 r
= seccomp_rule_add_exact(
2162 SCMP_ACT_ERRNO(EPERM
),
2165 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2167 log_debug_errno(r
, "Failed to add filter for creat: %m");
2174 int seccomp_restrict_suid_sgid(void) {
2178 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2179 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2181 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2185 r
= seccomp_restrict_sxid(seccomp
, S_ISUID
);
2187 log_debug_errno(r
, "Failed to add suid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2189 k
= seccomp_restrict_sxid(seccomp
, S_ISGID
);
2191 log_debug_errno(r
, "Failed to add sgid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2196 r
= seccomp_load(seccomp
);
2197 if (ERRNO_IS_SECCOMP_FATAL(r
))
2200 log_debug_errno(r
, "Failed to apply suid/sgid restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2206 uint32_t scmp_act_kill_process(void) {
2208 /* Returns SCMP_ACT_KILL_PROCESS if it's supported, and SCMP_ACT_KILL_THREAD otherwise. We never
2209 * actually want to use SCMP_ACT_KILL_THREAD as its semantics are nuts (killing arbitrary threads of
2210 * a program is just a bad idea), but on old kernels/old libseccomp it is all we have, and at least
2211 * for single-threaded apps does the right thing. */
2213 #ifdef SCMP_ACT_KILL_PROCESS
2214 if (seccomp_api_get() >= 3)
2215 return SCMP_ACT_KILL_PROCESS
;
2218 return SCMP_ACT_KILL
; /* same as SCMP_ACT_KILL_THREAD */
2221 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
2222 _cleanup_free_
char *n
= NULL
;
2231 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
2232 * If errno is omitted, then error is set to -1.
2233 * Empty syscall name is not allowed.
2234 * Here, we do not check that the syscall name is valid or not.
2237 p
= strchr(in
, ':');
2239 e
= seccomp_parse_errno_or_action(p
+ 1);
2243 n
= strndup(in
, p
- in
);
2254 *name
= TAKE_PTR(n
);
2259 static int block_open_flag(scmp_filter_ctx seccomp
, int flag
) {
2263 /* Blocks open() with the specified flag, where flag is O_SYNC or so. This makes these calls return
2264 * EINVAL, in the hope the client code will retry without O_SYNC then. */
2266 r
= seccomp_rule_add_exact(
2268 SCMP_ACT_ERRNO(EINVAL
),
2271 SCMP_A1(SCMP_CMP_MASKED_EQ
, flag
, flag
));
2273 log_debug_errno(r
, "Failed to add filter for open: %m");
2277 r
= seccomp_rule_add_exact(
2279 SCMP_ACT_ERRNO(EINVAL
),
2282 SCMP_A2(SCMP_CMP_MASKED_EQ
, flag
, flag
));
2284 log_debug_errno(r
, "Failed to add filter for openat: %m");
2288 #if defined(__SNR_openat2)
2289 /* The new openat2() system call can't be filtered sensibly, see above. */
2290 r
= seccomp_rule_add_exact(
2292 SCMP_ACT_ERRNO(ENOSYS
),
2296 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2304 int seccomp_suppress_sync(void) {
2308 /* This is mostly identical to SystemCallFilter=~@sync:0, but simpler to use, and separately
2309 * manageable, and also masks O_SYNC/O_DSYNC */
2311 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2312 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2315 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2319 NULSTR_FOREACH(c
, syscall_filter_sets
[SYSCALL_FILTER_SET_SYNC
].value
) {
2322 id
= seccomp_syscall_resolve_name(c
);
2323 if (id
== __NR_SCMP_ERROR
) {
2324 log_debug("System call %s is not known, ignoring.", c
);
2328 r
= seccomp_rule_add_exact(
2330 SCMP_ACT_ERRNO(0), /* success → we want this to be a NOP after all */
2334 log_debug_errno(r
, "Failed to add filter for system call %s, ignoring: %m", c
);
2337 (void) block_open_flag(seccomp
, O_SYNC
);
2338 #if O_DSYNC != O_SYNC
2339 (void) block_open_flag(seccomp
, O_DSYNC
);
2342 r
= seccomp_load(seccomp
);
2343 if (ERRNO_IS_SECCOMP_FATAL(r
))
2346 log_debug_errno(r
, "Failed to apply sync() suppression for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));