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"
21 #include "namespace-util.h"
23 #include "nulstr-util.h"
24 #include "process-util.h"
25 #include "seccomp-util.h"
27 #include "string-util.h"
30 /* This array will be modified at runtime as seccomp_restrict_archs is called. */
31 uint32_t seccomp_local_archs
[] = {
33 /* 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 */
35 #if defined(__x86_64__) && defined(__ILP32__)
38 SCMP_ARCH_X32
, /* native */
39 #elif defined(__x86_64__) && !defined(__ILP32__)
42 SCMP_ARCH_X86_64
, /* native */
43 #elif defined(__i386__)
45 #elif defined(__aarch64__)
47 SCMP_ARCH_AARCH64
, /* native */
48 #elif defined(__arm__)
50 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
52 SCMP_ARCH_MIPS
, /* native */
53 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
55 SCMP_ARCH_MIPSEL
, /* native */
56 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
59 SCMP_ARCH_MIPSEL64N32
,
62 SCMP_ARCH_MIPS64
, /* native */
63 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
67 SCMP_ARCH_MIPSEL64N32
,
69 SCMP_ARCH_MIPSEL64
, /* native */
70 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
75 SCMP_ARCH_MIPSEL64N32
,
76 SCMP_ARCH_MIPS64N32
, /* native */
77 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
83 SCMP_ARCH_MIPSEL64N32
, /* native */
84 #elif defined(__hppa64__) && defined(SCMP_ARCH_PARISC) && defined(SCMP_ARCH_PARISC64)
86 SCMP_ARCH_PARISC64
, /* native */
87 #elif defined(__hppa__) && defined(SCMP_ARCH_PARISC)
89 #elif defined(__powerpc64__) && __BYTE_ORDER == __BIG_ENDIAN
92 SCMP_ARCH_PPC64
, /* native */
93 #elif defined(__powerpc64__) && __BYTE_ORDER == __LITTLE_ENDIAN
96 SCMP_ARCH_PPC64LE
, /* native */
97 #elif defined(__powerpc__)
99 #elif defined(__riscv) && __riscv_xlen == 64 && defined(SCMP_ARCH_RISCV64)
101 #elif defined(__s390x__)
103 SCMP_ARCH_S390X
, /* native */
104 #elif defined(__s390__)
107 SECCOMP_LOCAL_ARCH_END
110 const char* seccomp_arch_to_string(uint32_t c
) {
111 /* Maintain order used in <seccomp.h>.
113 * Names used here should be the same as those used for ConditionArchitecture=,
114 * except for "subarchitectures" like x32. */
117 case SCMP_ARCH_NATIVE
:
121 case SCMP_ARCH_X86_64
:
127 case SCMP_ARCH_AARCH64
:
131 case SCMP_ARCH_MIPS64
:
133 case SCMP_ARCH_MIPS64N32
:
135 case SCMP_ARCH_MIPSEL
:
137 case SCMP_ARCH_MIPSEL64
:
139 case SCMP_ARCH_MIPSEL64N32
:
140 return "mips64-le-n32";
141 #ifdef SCMP_ARCH_PARISC
142 case SCMP_ARCH_PARISC
:
145 #ifdef SCMP_ARCH_PARISC64
146 case SCMP_ARCH_PARISC64
:
151 case SCMP_ARCH_PPC64
:
153 case SCMP_ARCH_PPC64LE
:
155 #ifdef SCMP_ARCH_RISCV64
156 case SCMP_ARCH_RISCV64
:
161 case SCMP_ARCH_S390X
:
168 int seccomp_arch_from_string(const char *n
, uint32_t *ret
) {
174 if (streq(n
, "native"))
175 *ret
= SCMP_ARCH_NATIVE
;
176 else if (streq(n
, "x86"))
177 *ret
= SCMP_ARCH_X86
;
178 else if (streq(n
, "x86-64"))
179 *ret
= SCMP_ARCH_X86_64
;
180 else if (streq(n
, "x32"))
181 *ret
= SCMP_ARCH_X32
;
182 else if (streq(n
, "arm"))
183 *ret
= SCMP_ARCH_ARM
;
184 else if (streq(n
, "arm64"))
185 *ret
= SCMP_ARCH_AARCH64
;
186 else if (streq(n
, "mips"))
187 *ret
= SCMP_ARCH_MIPS
;
188 else if (streq(n
, "mips64"))
189 *ret
= SCMP_ARCH_MIPS64
;
190 else if (streq(n
, "mips64-n32"))
191 *ret
= SCMP_ARCH_MIPS64N32
;
192 else if (streq(n
, "mips-le"))
193 *ret
= SCMP_ARCH_MIPSEL
;
194 else if (streq(n
, "mips64-le"))
195 *ret
= SCMP_ARCH_MIPSEL64
;
196 else if (streq(n
, "mips64-le-n32"))
197 *ret
= SCMP_ARCH_MIPSEL64N32
;
198 #ifdef SCMP_ARCH_PARISC
199 else if (streq(n
, "parisc"))
200 *ret
= SCMP_ARCH_PARISC
;
202 #ifdef SCMP_ARCH_PARISC64
203 else if (streq(n
, "parisc64"))
204 *ret
= SCMP_ARCH_PARISC64
;
206 else if (streq(n
, "ppc"))
207 *ret
= SCMP_ARCH_PPC
;
208 else if (streq(n
, "ppc64"))
209 *ret
= SCMP_ARCH_PPC64
;
210 else if (streq(n
, "ppc64-le"))
211 *ret
= SCMP_ARCH_PPC64LE
;
212 #ifdef SCMP_ARCH_RISCV64
213 else if (streq(n
, "riscv64"))
214 *ret
= SCMP_ARCH_RISCV64
;
216 else if (streq(n
, "s390"))
217 *ret
= SCMP_ARCH_S390
;
218 else if (streq(n
, "s390x"))
219 *ret
= SCMP_ARCH_S390X
;
226 int seccomp_init_for_arch(scmp_filter_ctx
*ret
, uint32_t arch
, uint32_t default_action
) {
227 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
230 /* Much like seccomp_init(), but initializes the filter for one specific architecture only, without affecting
231 * any others. Also, turns off the NNP fiddling. */
233 seccomp
= seccomp_init(default_action
);
237 if (arch
!= SCMP_ARCH_NATIVE
&&
238 arch
!= seccomp_arch_native()) {
240 r
= seccomp_arch_remove(seccomp
, seccomp_arch_native());
244 r
= seccomp_arch_add(seccomp
, arch
);
248 assert(seccomp_arch_exist(seccomp
, arch
) >= 0);
249 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) == -EEXIST
);
250 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) == -EEXIST
);
252 assert(seccomp_arch_exist(seccomp
, SCMP_ARCH_NATIVE
) >= 0);
253 assert(seccomp_arch_exist(seccomp
, seccomp_arch_native()) >= 0);
256 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_ACT_BADARCH
, SCMP_ACT_ALLOW
);
260 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
264 #if SCMP_VER_MAJOR >= 3 || (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 4)
265 if (getenv_bool("SYSTEMD_LOG_SECCOMP") > 0) {
266 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_LOG
, 1);
268 log_debug_errno(r
, "Failed to enable seccomp event logging: %m");
272 *ret
= TAKE_PTR(seccomp
);
276 static bool is_basic_seccomp_available(void) {
277 return prctl(PR_GET_SECCOMP
, 0, 0, 0, 0) >= 0;
280 static bool is_seccomp_filter_available(void) {
281 return prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, 0, 0) < 0 &&
285 bool is_seccomp_available(void) {
286 static int cached_enabled
= -1;
288 if (cached_enabled
< 0) {
291 b
= getenv_bool_secure("SYSTEMD_SECCOMP");
293 if (b
< 0 && b
!= -ENXIO
) /* ENXIO: env var unset */
294 log_debug_errno(b
, "Failed to parse $SYSTEMD_SECCOMP value, ignoring.");
297 is_basic_seccomp_available() &&
298 is_seccomp_filter_available();
300 cached_enabled
= false;
303 return cached_enabled
;
306 const SyscallFilterSet syscall_filter_sets
[_SYSCALL_FILTER_SET_MAX
] = {
307 [SYSCALL_FILTER_SET_DEFAULT
] = {
309 .help
= "System calls that are always permitted",
311 "arch_prctl\0" /* Used during platform-specific initialization by ld-linux.so. */
315 "clock_getres_time64\0"
319 "clock_nanosleep_time64\0"
344 "getrlimit\0" /* make sure processes can query stack size and such */
359 "riscv_flush_icache\0"
362 "sched_getaffinity\0"
372 [SYSCALL_FILTER_SET_AIO
] = {
374 .help
= "Asynchronous IO",
380 "io_pgetevents_time64\0"
384 "io_uring_register\0"
387 [SYSCALL_FILTER_SET_BASIC_IO
] = {
409 [SYSCALL_FILTER_SET_CHOWN
] = {
411 .help
= "Change ownership of files and directories",
421 [SYSCALL_FILTER_SET_CLOCK
] = {
423 .help
= "Change the system time",
432 [SYSCALL_FILTER_SET_CPU_EMULATION
] = {
433 .name
= "@cpu-emulation",
434 .help
= "System calls for CPU emulation functionality",
442 [SYSCALL_FILTER_SET_DEBUG
] = {
444 .help
= "Debugging, performance monitoring and tracing functionality",
451 "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 "s390_pci_mmio_read\0"
776 "s390_pci_mmio_write\0"
778 [SYSCALL_FILTER_SET_REBOOT
] = {
780 .help
= "Reboot and reboot preparation/kexec",
786 [SYSCALL_FILTER_SET_RESOURCES
] = {
787 .name
= "@resources",
788 .help
= "Alter resource settings",
795 "sched_setaffinity\0"
798 "sched_setscheduler\0"
803 [SYSCALL_FILTER_SET_SETUID
] = {
805 .help
= "Operations for changing user/group credentials",
822 [SYSCALL_FILTER_SET_SIGNAL
] = {
824 .help
= "Process signal handling",
831 "rt_sigtimedwait_time64\0"
841 [SYSCALL_FILTER_SET_SWAP
] = {
843 .help
= "Enable/disable swap devices",
848 [SYSCALL_FILTER_SET_SYNC
] = {
850 .help
= "Synchronize files and memory to storage",
860 [SYSCALL_FILTER_SET_SYSTEM_SERVICE
] = {
861 .name
= "@system-service",
862 .help
= "General system service operations",
894 "name_to_handle_at\0"
901 "sched_get_priority_max\0"
902 "sched_get_priority_min\0"
905 "sched_getscheduler\0"
906 "sched_rr_get_interval\0"
907 "sched_rr_get_interval_time64\0"
925 [SYSCALL_FILTER_SET_TIMER
] = {
927 .help
= "Schedule operations by time",
941 "timerfd_gettime64\0"
943 "timerfd_settime64\0"
946 [SYSCALL_FILTER_SET_KNOWN
] = {
948 .help
= "All known syscalls declared in the kernel",
951 #include "syscall-list.h"
955 const SyscallFilterSet
*syscall_filter_set_find(const char *name
) {
956 if (isempty(name
) || name
[0] != '@')
959 for (unsigned i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++)
960 if (streq(syscall_filter_sets
[i
].name
, name
))
961 return syscall_filter_sets
+ i
;
966 static int add_syscall_filter_set(
967 scmp_filter_ctx seccomp
,
968 const SyscallFilterSet
*set
,
974 int seccomp_add_syscall_filter_item(
975 scmp_filter_ctx
*seccomp
,
985 if (strv_contains(exclude
, name
))
988 /* Any syscalls that are handled are added to the *added strv. The pointer
989 * must be either NULL or point to a valid pre-initialized possibly-empty strv. */
991 if (name
[0] == '@') {
992 const SyscallFilterSet
*other
;
994 other
= syscall_filter_set_find(name
);
996 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
997 "Filter set %s is not known!",
1000 return add_syscall_filter_set(seccomp
, other
, action
, exclude
, log_missing
, added
);
1005 id
= seccomp_syscall_resolve_name(name
);
1006 if (id
== __NR_SCMP_ERROR
) {
1008 log_debug("System call %s is not known, ignoring.", name
);
1012 r
= seccomp_rule_add_exact(seccomp
, action
, id
, 0);
1014 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
1015 bool ignore
= r
== -EDOM
;
1017 if (!ignore
|| log_missing
)
1018 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1019 name
, id
, ignore
? ", ignoring" : "");
1025 r
= strv_extend(added
, name
);
1034 static int add_syscall_filter_set(
1035 scmp_filter_ctx seccomp
,
1036 const SyscallFilterSet
*set
,
1045 /* Any syscalls that are handled are added to the *added strv. It needs to be initialized. */
1050 NULSTR_FOREACH(sys
, set
->value
) {
1051 r
= seccomp_add_syscall_filter_item(seccomp
, sys
, action
, exclude
, log_missing
, added
);
1059 int seccomp_load_syscall_filter_set(uint32_t default_action
, const SyscallFilterSet
*set
, uint32_t action
, bool log_missing
) {
1065 /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
1066 * each local arch. */
1068 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1069 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1071 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1073 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1077 r
= add_syscall_filter_set(seccomp
, set
, action
, NULL
, log_missing
, NULL
);
1079 return log_debug_errno(r
, "Failed to add filter set: %m");
1081 r
= seccomp_load(seccomp
);
1082 if (ERRNO_IS_SECCOMP_FATAL(r
))
1085 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1091 int seccomp_load_syscall_filter_set_raw(uint32_t default_action
, Hashmap
* filter
, uint32_t action
, bool log_missing
) {
1095 /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Hashmap* of syscalls, instead
1096 * of a SyscallFilterSet* table. */
1098 if (hashmap_isempty(filter
) && default_action
== SCMP_ACT_ALLOW
)
1101 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1102 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1103 void *syscall_id
, *val
;
1105 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1107 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
1111 HASHMAP_FOREACH_KEY(val
, syscall_id
, filter
) {
1112 uint32_t a
= action
;
1113 int id
= PTR_TO_INT(syscall_id
) - 1;
1114 int error
= PTR_TO_INT(val
);
1116 if (error
== SECCOMP_ERROR_NUMBER_KILL
)
1117 a
= scmp_act_kill_process();
1119 else if (action
== SCMP_ACT_LOG
)
1122 else if (error
>= 0)
1123 a
= SCMP_ACT_ERRNO(error
);
1125 r
= seccomp_rule_add_exact(seccomp
, a
, id
, 0);
1127 /* If the system call is not known on this architecture, then that's
1128 * fine, let's ignore it */
1129 _cleanup_free_
char *n
= NULL
;
1132 n
= seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE
, id
);
1133 ignore
= r
== -EDOM
;
1134 if (!ignore
|| log_missing
)
1135 log_debug_errno(r
, "Failed to add rule for system call %s() / %d%s: %m",
1136 strna(n
), id
, ignore
? ", ignoring" : "");
1142 r
= seccomp_load(seccomp
);
1143 if (ERRNO_IS_SECCOMP_FATAL(r
))
1146 log_debug_errno(r
, "Failed to install system call filter for architecture %s, skipping: %m",
1147 seccomp_arch_to_string(arch
));
1153 int seccomp_parse_syscall_filter(
1157 SeccompParseFlags flags
,
1159 const char *filename
,
1167 if (!FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
) && errno_num
>= 0)
1170 if (name
[0] == '@') {
1171 const SyscallFilterSet
*set
;
1174 set
= syscall_filter_set_find(name
);
1176 if (!FLAGS_SET(flags
, SECCOMP_PARSE_PERMISSIVE
))
1179 log_syntax(unit
, FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1180 "Unknown system call group, ignoring: %s", name
);
1184 NULSTR_FOREACH(i
, set
->value
) {
1185 /* Call ourselves again, for the group to parse. Note that we downgrade logging here (i.e. take
1186 * away the SECCOMP_PARSE_LOG flag) since any issues in the group table are our own problem,
1187 * not a problem in user configuration data and we shouldn't pretend otherwise by complaining
1189 r
= seccomp_parse_syscall_filter(i
, errno_num
, filter
, flags
&~ SECCOMP_PARSE_LOG
, unit
, filename
, line
);
1196 id
= seccomp_syscall_resolve_name(name
);
1197 if (id
== __NR_SCMP_ERROR
) {
1198 if (!FLAGS_SET(flags
, SECCOMP_PARSE_PERMISSIVE
))
1201 log_syntax(unit
, FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? LOG_WARNING
: LOG_DEBUG
, filename
, line
, 0,
1202 "Failed to parse system call, ignoring: %s", name
);
1206 /* If we previously wanted to forbid a syscall and now we want to allow it, then remove
1207 * it from the list. The entries in allow-list with non-negative error value will be
1208 * handled with SCMP_ACT_ERRNO() instead of the default action. */
1209 if (!FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
) == FLAGS_SET(flags
, SECCOMP_PARSE_ALLOW_LIST
) ||
1210 (FLAGS_SET(flags
, SECCOMP_PARSE_INVERT
| SECCOMP_PARSE_ALLOW_LIST
) && errno_num
>= 0)) {
1211 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
));
1215 return FLAGS_SET(flags
, SECCOMP_PARSE_LOG
) ? log_oom() : -ENOMEM
;
1217 assert_se(hashmap_update(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(errno_num
)) == 0);
1223 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1229 int seccomp_restrict_namespaces(unsigned long retain
) {
1233 if (DEBUG_LOGGING
) {
1234 _cleanup_free_
char *s
= NULL
;
1236 (void) namespace_flags_to_string(retain
, &s
);
1237 log_debug("Restricting namespace to: %s.", strna(s
));
1241 if (FLAGS_SET(retain
, NAMESPACE_FLAGS_ALL
))
1244 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1245 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1247 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1249 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1253 /* We cannot filter on individual flags to clone3(), and we need to disable the
1254 * syscall altogether. ENOSYS is used instead of EPERM, so that glibc and other
1255 * users shall fall back to clone(), as if on an older kernel.
1257 * C.f. https://github.com/flatpak/flatpak/commit/a10f52a7565c549612c92b8e736a6698a53db330,
1258 * https://github.com/moby/moby/issues/42680. */
1260 r
= seccomp_rule_add_exact(
1262 SCMP_ACT_ERRNO(ENOSYS
),
1266 log_debug_errno(r
, "Failed to add clone3() rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
1268 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
1269 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
1271 r
= seccomp_rule_add_exact(
1273 SCMP_ACT_ERRNO(EPERM
),
1277 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
1278 * special invocation with a zero flags argument, right here. */
1279 r
= seccomp_rule_add_exact(
1281 SCMP_ACT_ERRNO(EPERM
),
1284 SCMP_A1(SCMP_CMP_EQ
, 0));
1286 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1290 for (unsigned i
= 0; namespace_info
[i
].proc_name
; i
++) {
1293 f
= namespace_info
[i
].clone_flag
;
1294 if (FLAGS_SET(retain
, f
)) {
1295 log_debug("Permitting %s.", namespace_info
[i
].proc_name
);
1299 log_debug("Blocking %s.", namespace_info
[i
].proc_name
);
1301 r
= seccomp_rule_add_exact(
1303 SCMP_ACT_ERRNO(EPERM
),
1306 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1308 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1312 /* On s390/s390x the first two parameters to clone are switched */
1313 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
1314 r
= seccomp_rule_add_exact(
1316 SCMP_ACT_ERRNO(EPERM
),
1319 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
1321 r
= seccomp_rule_add_exact(
1323 SCMP_ACT_ERRNO(EPERM
),
1326 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1328 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1332 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1333 r
= seccomp_rule_add_exact(
1335 SCMP_ACT_ERRNO(EPERM
),
1338 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1340 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1348 r
= seccomp_load(seccomp
);
1349 if (ERRNO_IS_SECCOMP_FATAL(r
))
1352 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1358 int seccomp_protect_sysctl(void) {
1362 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1363 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1365 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1369 #ifdef SCMP_ARCH_RISCV64
1374 /* No _sysctl syscall */
1377 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1381 r
= seccomp_rule_add_exact(
1383 SCMP_ACT_ERRNO(EPERM
),
1387 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1391 r
= seccomp_load(seccomp
);
1392 if (ERRNO_IS_SECCOMP_FATAL(r
))
1395 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1401 int seccomp_protect_syslog(void) {
1405 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1406 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1408 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1412 r
= seccomp_rule_add_exact(
1414 SCMP_ACT_ERRNO(EPERM
),
1419 log_debug_errno(r
, "Failed to add syslog() rule for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1423 r
= seccomp_load(seccomp
);
1424 if (ERRNO_IS_SECCOMP_FATAL(r
))
1427 log_debug_errno(r
, "Failed to install syslog protection rules for architecture %s, skipping %m", seccomp_arch_to_string(arch
));
1433 int seccomp_restrict_address_families(Set
*address_families
, bool allow_list
) {
1437 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1438 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1441 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1445 case SCMP_ARCH_X86_64
:
1448 case SCMP_ARCH_AARCH64
:
1449 case SCMP_ARCH_MIPSEL64N32
:
1450 case SCMP_ARCH_MIPS64N32
:
1451 case SCMP_ARCH_MIPSEL64
:
1452 case SCMP_ARCH_MIPS64
:
1453 #ifdef SCMP_ARCH_RISCV64
1454 case SCMP_ARCH_RISCV64
:
1456 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1460 case SCMP_ARCH_S390
:
1461 case SCMP_ARCH_S390X
:
1463 case SCMP_ARCH_MIPSEL
:
1464 case SCMP_ARCH_MIPS
:
1465 #ifdef SCMP_ARCH_PARISC
1466 case SCMP_ARCH_PARISC
:
1468 #ifdef SCMP_ARCH_PARISC64
1469 case SCMP_ARCH_PARISC64
:
1472 case SCMP_ARCH_PPC64
:
1473 case SCMP_ARCH_PPC64LE
:
1475 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1484 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1489 int first
= 0, last
= 0;
1492 /* If this is an allow list, we first block the address families that are out of
1493 * range and then everything that is not in the set. First, we find the lowest and
1494 * highest address family in the set. */
1496 SET_FOREACH(afp
, address_families
) {
1497 int af
= PTR_TO_INT(afp
);
1499 if (af
<= 0 || af
>= af_max())
1502 if (first
== 0 || af
< first
)
1505 if (last
== 0 || af
> last
)
1509 assert((first
== 0) == (last
== 0));
1513 /* No entries in the valid range, block everything */
1514 r
= seccomp_rule_add_exact(
1516 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1520 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1526 /* Block everything below the first entry */
1527 r
= seccomp_rule_add_exact(
1529 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1532 SCMP_A0(SCMP_CMP_LT
, first
));
1534 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1538 /* Block everything above the last entry */
1539 r
= seccomp_rule_add_exact(
1541 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1544 SCMP_A0(SCMP_CMP_GT
, last
));
1546 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1550 /* Block everything between the first and last entry */
1551 for (int af
= 1; af
< af_max(); af
++) {
1553 if (set_contains(address_families
, INT_TO_PTR(af
)))
1556 r
= seccomp_rule_add_exact(
1558 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1561 SCMP_A0(SCMP_CMP_EQ
, af
));
1566 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1574 /* If this is a deny list, then generate one rule for each address family that are
1575 * then combined in OR checks. */
1577 SET_FOREACH(af
, address_families
) {
1578 r
= seccomp_rule_add_exact(
1580 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1583 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1588 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1593 r
= seccomp_load(seccomp
);
1594 if (ERRNO_IS_SECCOMP_FATAL(r
))
1597 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1603 int seccomp_restrict_realtime_full(int error_code
) {
1604 static const int permitted_policies
[] = {
1610 int r
, max_policy
= 0;
1614 assert(error_code
> 0);
1616 /* Determine the highest policy constant we want to allow */
1617 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1618 if (permitted_policies
[i
] > max_policy
)
1619 max_policy
= permitted_policies
[i
];
1621 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1622 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1625 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1627 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1631 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1633 for (p
= 0; p
< max_policy
; p
++) {
1636 /* Check if this is in the allow list. */
1637 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1638 if (permitted_policies
[i
] == p
) {
1646 /* Deny this policy */
1647 r
= seccomp_rule_add_exact(
1649 SCMP_ACT_ERRNO(error_code
),
1650 SCMP_SYS(sched_setscheduler
),
1652 SCMP_A1(SCMP_CMP_EQ
, p
));
1654 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1659 /* Deny-list all other policies, i.e. the ones with higher values. Note that all comparisons
1660 * are unsigned here, hence no need no check for < 0 values. */
1661 r
= seccomp_rule_add_exact(
1663 SCMP_ACT_ERRNO(error_code
),
1664 SCMP_SYS(sched_setscheduler
),
1666 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1668 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1672 r
= seccomp_load(seccomp
);
1673 if (ERRNO_IS_SECCOMP_FATAL(r
))
1676 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1682 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1686 const struct scmp_arg_cmp arg
) {
1689 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1691 _cleanup_free_
char *n
= NULL
;
1693 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1694 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1696 seccomp_arch_to_string(arch
));
1702 /* For known architectures, check that syscalls are indeed defined or not. */
1703 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || (defined(__riscv) && __riscv_xlen == 64)
1704 assert_cc(SCMP_SYS(shmget
) > 0);
1705 assert_cc(SCMP_SYS(shmat
) > 0);
1706 assert_cc(SCMP_SYS(shmdt
) > 0);
1709 int seccomp_memory_deny_write_execute(void) {
1711 unsigned loaded
= 0;
1713 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1714 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1715 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0, r
;
1717 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1721 /* Note that on some architectures shmat() isn't available, and the call is multiplexed through ipc().
1722 * We ignore that here, which means there's still a way to get writable/executable
1723 * memory, if an IPC key is mapped like this. That's a pity, but no total loss.
1725 * Also, PARISC isn't here right now because it still needs executable memory, but work is in progress
1726 * on that front (kernel work done in 5.18).
1730 case SCMP_ARCH_S390
:
1731 filter_syscall
= SCMP_SYS(mmap2
);
1732 block_syscall
= SCMP_SYS(mmap
);
1733 /* shmat multiplexed, see above */
1737 case SCMP_ARCH_PPC64
:
1738 case SCMP_ARCH_PPC64LE
:
1739 case SCMP_ARCH_S390X
:
1740 filter_syscall
= SCMP_SYS(mmap
);
1741 /* shmat multiplexed, see above */
1745 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1746 shmat_syscall
= SCMP_SYS(shmat
);
1749 case SCMP_ARCH_X86_64
:
1751 case SCMP_ARCH_AARCH64
:
1752 #ifdef SCMP_ARCH_RISCV64
1753 case SCMP_ARCH_RISCV64
:
1755 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32, arm64 and riscv64 have only mmap */
1756 shmat_syscall
= SCMP_SYS(shmat
);
1759 /* Please add more definitions here, if you port systemd to other architectures! */
1761 #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)
1762 #warning "Consider adding the right mmap() syscall definitions here!"
1766 /* Can't filter mmap() on this arch, then skip it */
1767 if (filter_syscall
== 0)
1770 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1774 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1776 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1780 if (block_syscall
!= 0) {
1781 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1786 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1788 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1792 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(pkey_mprotect
),
1794 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1798 if (shmat_syscall
> 0) {
1799 r
= add_seccomp_syscall_filter(seccomp
, arch
, shmat_syscall
,
1801 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1806 r
= seccomp_load(seccomp
);
1807 if (ERRNO_IS_SECCOMP_FATAL(r
))
1810 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m",
1811 seccomp_arch_to_string(arch
));
1816 log_debug("Failed to install any seccomp rules for MemoryDenyWriteExecute=.");
1821 int seccomp_restrict_archs(Set
*archs
) {
1822 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1824 bool blocked_new
= false;
1826 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1829 * There are some qualifications. However the most important use is to stop processes from bypassing
1830 * system call restrictions, in case they used a broader (multiplexing) syscall which is only available
1831 * in a non-native architecture. There are no holes in this use case, at least so far. */
1833 /* Note libseccomp includes our "native" (current) architecture in the filter by default.
1834 * We do not remove it. For example, our callers expect to be able to call execve() afterwards
1835 * to run a program with the restrictions applied. */
1836 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1840 for (unsigned i
= 0; seccomp_local_archs
[i
] != SECCOMP_LOCAL_ARCH_END
; ++i
) {
1841 uint32_t arch
= seccomp_local_archs
[i
];
1843 /* See above comment, our "native" architecture is never blocked. */
1844 if (arch
== seccomp_arch_native())
1847 /* That architecture might have already been blocked by a previous call to seccomp_restrict_archs. */
1848 if (arch
== SECCOMP_LOCAL_ARCH_BLOCKED
)
1851 bool block
= !set_contains(archs
, UINT32_TO_PTR(arch
+ 1));
1853 /* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
1854 * x32 syscalls should basically match x86-64 for everything except the pointer type.
1855 * The important thing is that you can block the old 32-bit x86 syscalls.
1856 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
1857 if (block
&& arch
== SCMP_ARCH_X86_64
&& seccomp_arch_native() == SCMP_ARCH_X32
)
1858 block
= !set_contains(archs
, UINT32_TO_PTR(SCMP_ARCH_X32
+ 1));
1861 seccomp_local_archs
[i
] = SECCOMP_LOCAL_ARCH_BLOCKED
;
1864 r
= seccomp_arch_add(seccomp
, arch
);
1865 if (r
< 0 && r
!= -EEXIST
)
1870 /* All architectures that will be blocked by the seccomp program were
1871 * already blocked. */
1875 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1879 r
= seccomp_load(seccomp
);
1880 if (ERRNO_IS_SECCOMP_FATAL(r
))
1883 log_debug_errno(r
, "Failed to restrict system call architectures, skipping: %m");
1888 int parse_syscall_archs(char **l
, Set
**ret_archs
) {
1889 _cleanup_set_free_ Set
*archs
= NULL
;
1895 STRV_FOREACH(s
, l
) {
1898 r
= seccomp_arch_from_string(*s
, &a
);
1902 r
= set_ensure_put(&archs
, NULL
, UINT32_TO_PTR(a
+ 1));
1907 *ret_archs
= TAKE_PTR(archs
);
1911 int seccomp_filter_set_add(Hashmap
*filter
, bool add
, const SyscallFilterSet
*set
) {
1917 NULSTR_FOREACH(i
, set
->value
) {
1920 const SyscallFilterSet
*more
;
1922 more
= syscall_filter_set_find(i
);
1926 r
= seccomp_filter_set_add(filter
, add
, more
);
1932 id
= seccomp_syscall_resolve_name(i
);
1933 if (id
== __NR_SCMP_ERROR
) {
1934 log_debug("Couldn't resolve system call, ignoring: %s", i
);
1939 r
= hashmap_put(filter
, INT_TO_PTR(id
+ 1), INT_TO_PTR(-1));
1943 (void) hashmap_remove(filter
, INT_TO_PTR(id
+ 1));
1950 int seccomp_lock_personality(unsigned long personality
) {
1954 if (personality
>= PERSONALITY_INVALID
)
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(personality
),
1969 SCMP_A0(SCMP_CMP_NE
, personality
));
1971 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1975 r
= seccomp_load(seccomp
);
1976 if (ERRNO_IS_SECCOMP_FATAL(r
))
1979 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1985 int seccomp_protect_hostname(void) {
1989 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1990 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1992 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1996 r
= seccomp_rule_add_exact(
1998 SCMP_ACT_ERRNO(EPERM
),
1999 SCMP_SYS(sethostname
),
2002 log_debug_errno(r
, "Failed to add sethostname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2006 r
= seccomp_rule_add_exact(
2008 SCMP_ACT_ERRNO(EPERM
),
2009 SCMP_SYS(setdomainname
),
2012 log_debug_errno(r
, "Failed to add setdomainname() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2016 r
= seccomp_load(seccomp
);
2017 if (ERRNO_IS_SECCOMP_FATAL(r
))
2020 log_debug_errno(r
, "Failed to apply hostname restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2026 static int seccomp_restrict_sxid(scmp_filter_ctx seccomp
, mode_t m
) {
2027 /* Checks the mode_t parameter of the following system calls:
2029 * → chmod() + fchmod() + fchmodat()
2030 * → open() + creat() + openat()
2031 * → mkdir() + mkdirat()
2032 * → mknod() + mknodat()
2034 * Returns error if *everything* failed, and 0 otherwise.
2039 r
= seccomp_rule_add_exact(
2041 SCMP_ACT_ERRNO(EPERM
),
2044 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2046 log_debug_errno(r
, "Failed to add filter for chmod: %m");
2050 r
= seccomp_rule_add_exact(
2052 SCMP_ACT_ERRNO(EPERM
),
2055 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2057 log_debug_errno(r
, "Failed to add filter for fchmod: %m");
2061 r
= seccomp_rule_add_exact(
2063 SCMP_ACT_ERRNO(EPERM
),
2066 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2068 log_debug_errno(r
, "Failed to add filter for fchmodat: %m");
2072 r
= seccomp_rule_add_exact(
2074 SCMP_ACT_ERRNO(EPERM
),
2077 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2079 log_debug_errno(r
, "Failed to add filter for mkdir: %m");
2083 r
= seccomp_rule_add_exact(
2085 SCMP_ACT_ERRNO(EPERM
),
2088 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2090 log_debug_errno(r
, "Failed to add filter for mkdirat: %m");
2094 r
= seccomp_rule_add_exact(
2096 SCMP_ACT_ERRNO(EPERM
),
2099 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2101 log_debug_errno(r
, "Failed to add filter for mknod: %m");
2105 r
= seccomp_rule_add_exact(
2107 SCMP_ACT_ERRNO(EPERM
),
2110 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2112 log_debug_errno(r
, "Failed to add filter for mknodat: %m");
2116 r
= seccomp_rule_add_exact(
2118 SCMP_ACT_ERRNO(EPERM
),
2121 SCMP_A1(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2122 SCMP_A2(SCMP_CMP_MASKED_EQ
, m
, m
));
2124 log_debug_errno(r
, "Failed to add filter for open: %m");
2128 r
= seccomp_rule_add_exact(
2130 SCMP_ACT_ERRNO(EPERM
),
2133 SCMP_A2(SCMP_CMP_MASKED_EQ
, O_CREAT
, O_CREAT
),
2134 SCMP_A3(SCMP_CMP_MASKED_EQ
, m
, m
));
2136 log_debug_errno(r
, "Failed to add filter for openat: %m");
2140 #if defined(__SNR_openat2)
2141 /* The new openat2() system call can't be filtered sensibly, since it moves the flags parameter into
2142 * an indirect structure. Let's block it entirely for now. That should be a reasonably OK thing to do
2143 * for now, since openat2() is very new and code generally needs fallback logic anyway to be
2144 * compatible with kernels that are not absolutely recent. We would normally return EPERM for a
2145 * policy check, but this isn't strictly a policy check. Instead, we return ENOSYS to force programs
2146 * to call open() or openat() instead. We can properly enforce policy for those functions. */
2147 r
= seccomp_rule_add_exact(
2149 SCMP_ACT_ERRNO(ENOSYS
),
2153 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2158 r
= seccomp_rule_add_exact(
2160 SCMP_ACT_ERRNO(EPERM
),
2163 SCMP_A1(SCMP_CMP_MASKED_EQ
, m
, m
));
2165 log_debug_errno(r
, "Failed to add filter for creat: %m");
2172 int seccomp_restrict_suid_sgid(void) {
2176 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2177 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2179 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2183 r
= seccomp_restrict_sxid(seccomp
, S_ISUID
);
2185 log_debug_errno(r
, "Failed to add suid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2187 k
= seccomp_restrict_sxid(seccomp
, S_ISGID
);
2189 log_debug_errno(r
, "Failed to add sgid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch
));
2194 r
= seccomp_load(seccomp
);
2195 if (ERRNO_IS_SECCOMP_FATAL(r
))
2198 log_debug_errno(r
, "Failed to apply suid/sgid restrictions for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
2204 uint32_t scmp_act_kill_process(void) {
2206 /* Returns SCMP_ACT_KILL_PROCESS if it's supported, and SCMP_ACT_KILL_THREAD otherwise. We never
2207 * actually want to use SCMP_ACT_KILL_THREAD as its semantics are nuts (killing arbitrary threads of
2208 * a program is just a bad idea), but on old kernels/old libseccomp it is all we have, and at least
2209 * for single-threaded apps does the right thing. */
2211 #ifdef SCMP_ACT_KILL_PROCESS
2212 if (seccomp_api_get() >= 3)
2213 return SCMP_ACT_KILL_PROCESS
;
2216 return SCMP_ACT_KILL
; /* same as SCMP_ACT_KILL_THREAD */
2219 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
2220 _cleanup_free_
char *n
= NULL
;
2229 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
2230 * If errno is omitted, then error is set to -1.
2231 * Empty syscall name is not allowed.
2232 * Here, we do not check that the syscall name is valid or not.
2235 p
= strchr(in
, ':');
2237 e
= seccomp_parse_errno_or_action(p
+ 1);
2241 n
= strndup(in
, p
- in
);
2252 *name
= TAKE_PTR(n
);
2257 static int block_open_flag(scmp_filter_ctx seccomp
, int flag
) {
2261 /* Blocks open() with the specified flag, where flag is O_SYNC or so. This makes these calls return
2262 * EINVAL, in the hope the client code will retry without O_SYNC then. */
2264 r
= seccomp_rule_add_exact(
2266 SCMP_ACT_ERRNO(EINVAL
),
2269 SCMP_A1(SCMP_CMP_MASKED_EQ
, flag
, flag
));
2271 log_debug_errno(r
, "Failed to add filter for open: %m");
2275 r
= seccomp_rule_add_exact(
2277 SCMP_ACT_ERRNO(EINVAL
),
2280 SCMP_A2(SCMP_CMP_MASKED_EQ
, flag
, flag
));
2282 log_debug_errno(r
, "Failed to add filter for openat: %m");
2286 #if defined(__SNR_openat2)
2287 /* The new openat2() system call can't be filtered sensibly, see above. */
2288 r
= seccomp_rule_add_exact(
2290 SCMP_ACT_ERRNO(ENOSYS
),
2294 log_debug_errno(r
, "Failed to add filter for openat2: %m");
2302 int seccomp_suppress_sync(void) {
2306 /* This is mostly identical to SystemCallFilter=~@sync:0, but simpler to use, and separately
2307 * manageable, and also masks O_SYNC/O_DSYNC */
2309 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
2310 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
2313 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
2317 NULSTR_FOREACH(c
, syscall_filter_sets
[SYSCALL_FILTER_SET_SYNC
].value
) {
2320 id
= seccomp_syscall_resolve_name(c
);
2321 if (id
== __NR_SCMP_ERROR
) {
2322 log_debug("System call %s is not known, ignoring.", c
);
2326 r
= seccomp_rule_add_exact(
2328 SCMP_ACT_ERRNO(0), /* success → we want this to be a NOP after all */
2332 log_debug_errno(r
, "Failed to add filter for system call %s, ignoring: %m", c
);
2335 (void) block_open_flag(seccomp
, O_SYNC
);
2336 #if O_DSYNC != O_SYNC
2337 (void) block_open_flag(seccomp
, O_DSYNC
);
2340 r
= seccomp_load(seccomp
);
2341 if (ERRNO_IS_SECCOMP_FATAL(r
))
2344 log_debug_errno(r
, "Failed to apply sync() suppression for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));