2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/seccomp.h>
25 #include <sys/prctl.h>
29 #include "alloc-util.h"
32 #include "process-util.h"
33 #include "seccomp-util.h"
35 #include "string-util.h"
38 #include "errno-list.h"
40 const uint32_t seccomp_local_archs
[] = {
42 /* Note: always list the native arch we are compiled as last, so that users can blacklist seccomp(), but our own calls to it still succeed */
44 #if defined(__x86_64__) && defined(__ILP32__)
47 SCMP_ARCH_X32
, /* native */
48 #elif defined(__x86_64__) && !defined(__ILP32__)
51 SCMP_ARCH_X86_64
, /* native */
52 #elif defined(__i386__)
54 #elif defined(__aarch64__)
56 SCMP_ARCH_AARCH64
, /* native */
57 #elif defined(__arm__)
59 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
61 SCMP_ARCH_MIPS
, /* native */
62 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32
64 SCMP_ARCH_MIPSEL
, /* native */
65 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
68 SCMP_ARCH_MIPSEL64N32
,
71 SCMP_ARCH_MIPS64
, /* native */
72 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64
76 SCMP_ARCH_MIPSEL64N32
,
78 SCMP_ARCH_MIPSEL64
, /* native */
79 #elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
84 SCMP_ARCH_MIPSEL64N32
,
85 SCMP_ARCH_MIPS64N32
, /* native */
86 #elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32
92 SCMP_ARCH_MIPSEL64N32
, /* native */
93 #elif defined(__powerpc64__) && __BYTE_ORDER == __BIG_ENDIAN
96 SCMP_ARCH_PPC64
, /* native */
97 #elif defined(__powerpc64__) && __BYTE_ORDER == __LITTLE_ENDIAN
100 SCMP_ARCH_PPC64LE
, /* native */
101 #elif defined(__powerpc__)
103 #elif defined(__s390x__)
105 SCMP_ARCH_S390X
, /* native */
106 #elif defined(__s390__)
112 const char* seccomp_arch_to_string(uint32_t c
) {
113 /* Maintain order used in <seccomp.h>.
115 * Names used here should be the same as those used for ConditionArchitecture=,
116 * except for "subarchitectures" like x32. */
119 case SCMP_ARCH_NATIVE
:
123 case SCMP_ARCH_X86_64
:
129 case SCMP_ARCH_AARCH64
:
133 case SCMP_ARCH_MIPS64
:
135 case SCMP_ARCH_MIPS64N32
:
137 case SCMP_ARCH_MIPSEL
:
139 case SCMP_ARCH_MIPSEL64
:
141 case SCMP_ARCH_MIPSEL64N32
:
142 return "mips64-le-n32";
145 case SCMP_ARCH_PPC64
:
147 case SCMP_ARCH_PPC64LE
:
151 case SCMP_ARCH_S390X
:
158 int seccomp_arch_from_string(const char *n
, uint32_t *ret
) {
164 if (streq(n
, "native"))
165 *ret
= SCMP_ARCH_NATIVE
;
166 else if (streq(n
, "x86"))
167 *ret
= SCMP_ARCH_X86
;
168 else if (streq(n
, "x86-64"))
169 *ret
= SCMP_ARCH_X86_64
;
170 else if (streq(n
, "x32"))
171 *ret
= SCMP_ARCH_X32
;
172 else if (streq(n
, "arm"))
173 *ret
= SCMP_ARCH_ARM
;
174 else if (streq(n
, "arm64"))
175 *ret
= SCMP_ARCH_AARCH64
;
176 else if (streq(n
, "mips"))
177 *ret
= SCMP_ARCH_MIPS
;
178 else if (streq(n
, "mips64"))
179 *ret
= SCMP_ARCH_MIPS64
;
180 else if (streq(n
, "mips64-n32"))
181 *ret
= SCMP_ARCH_MIPS64N32
;
182 else if (streq(n
, "mips-le"))
183 *ret
= SCMP_ARCH_MIPSEL
;
184 else if (streq(n
, "mips64-le"))
185 *ret
= SCMP_ARCH_MIPSEL64
;
186 else if (streq(n
, "mips64-le-n32"))
187 *ret
= SCMP_ARCH_MIPSEL64N32
;
188 else if (streq(n
, "ppc"))
189 *ret
= SCMP_ARCH_PPC
;
190 else if (streq(n
, "ppc64"))
191 *ret
= SCMP_ARCH_PPC64
;
192 else if (streq(n
, "ppc64-le"))
193 *ret
= SCMP_ARCH_PPC64LE
;
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 scmp_filter_ctx seccomp
;
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);
246 seccomp_release(seccomp
);
250 static bool is_basic_seccomp_available(void) {
251 return prctl(PR_GET_SECCOMP
, 0, 0, 0, 0) >= 0;
254 static bool is_seccomp_filter_available(void) {
255 return prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, NULL
, 0, 0) < 0 &&
259 bool is_seccomp_available(void) {
260 static int cached_enabled
= -1;
262 if (cached_enabled
< 0)
264 is_basic_seccomp_available() &&
265 is_seccomp_filter_available();
267 return cached_enabled
;
270 const SyscallFilterSet syscall_filter_sets
[_SYSCALL_FILTER_SET_MAX
] = {
271 [SYSCALL_FILTER_SET_DEFAULT
] = {
273 .help
= "System calls that are always permitted",
300 "getrlimit\0" /* make sure processes can query stack size and such */
320 [SYSCALL_FILTER_SET_BASIC_IO
] = {
339 [SYSCALL_FILTER_SET_CLOCK
] = {
341 .help
= "Change the system time",
349 [SYSCALL_FILTER_SET_CPU_EMULATION
] = {
350 .name
= "@cpu-emulation",
351 .help
= "System calls for CPU emulation functionality",
359 [SYSCALL_FILTER_SET_DEBUG
] = {
361 .help
= "Debugging, performance monitoring and tracing functionality",
366 "process_vm_writev\0"
369 #ifdef __NR_s390_runtime_instr
370 "s390_runtime_instr\0"
372 "sys_debug_setcontext\0"
374 [SYSCALL_FILTER_SET_FILE_SYSTEM
] = {
375 .name
= "@file-system",
376 .help
= "File system operations",
406 "inotify_add_watch\0"
457 [SYSCALL_FILTER_SET_IO_EVENT
] = {
459 .help
= "Event loop system calls",
476 [SYSCALL_FILTER_SET_IPC
] = {
478 .help
= "SysV IPC, POSIX Message Queues or other IPC",
495 "process_vm_writev\0"
505 [SYSCALL_FILTER_SET_KEYRING
] = {
507 .help
= "Kernel keyring access",
513 [SYSCALL_FILTER_SET_MEMLOCK
] = {
515 .help
= "Memory locking control",
523 [SYSCALL_FILTER_SET_MODULE
] = {
525 .help
= "Loading and unloading of kernel modules",
531 [SYSCALL_FILTER_SET_MOUNT
] = {
533 .help
= "Mounting and unmounting of file systems",
541 [SYSCALL_FILTER_SET_NETWORK_IO
] = {
542 .name
= "@network-io",
543 .help
= "Network or Unix socket IO, should not be needed if not network facing",
567 [SYSCALL_FILTER_SET_OBSOLETE
] = {
568 /* some unknown even to libseccomp */
570 .help
= "Unusual, obsolete or unimplemented system calls",
599 [SYSCALL_FILTER_SET_PRIVILEGED
] = {
600 .name
= "@privileged",
601 .help
= "All system calls which need super-user capabilities",
640 [SYSCALL_FILTER_SET_PROCESS
] = {
642 .help
= "Process control, execution, namespaceing operations",
645 "capget\0" /* Able to query arbitrary processes */
654 "rt_tgsigqueueinfo\0"
665 [SYSCALL_FILTER_SET_RAW_IO
] = {
667 .help
= "Raw I/O port access",
674 #ifdef __NR_s390_pci_mmio_read
675 "s390_pci_mmio_read\0"
677 #ifdef __NR_s390_pci_mmio_write
678 "s390_pci_mmio_write\0"
681 [SYSCALL_FILTER_SET_REBOOT
] = {
683 .help
= "Reboot and reboot preparation/kexec",
689 [SYSCALL_FILTER_SET_RESOURCES
] = {
690 .name
= "@resources",
691 .help
= "Alter resource settings",
698 "sched_setaffinity\0"
701 "sched_setscheduler\0"
706 [SYSCALL_FILTER_SET_SETUID
] = {
708 .help
= "Operations for changing user/group credentials",
725 [SYSCALL_FILTER_SET_SIGNAL
] = {
727 .help
= "Process signal handling",
743 [SYSCALL_FILTER_SET_SWAP
] = {
745 .help
= "Enable/disable swap devices",
750 [SYSCALL_FILTER_SET_TIMER
] = {
752 .help
= "Schedule operations by time",
769 const SyscallFilterSet
*syscall_filter_set_find(const char *name
) {
772 if (isempty(name
) || name
[0] != '@')
775 for (i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++)
776 if (streq(syscall_filter_sets
[i
].name
, name
))
777 return syscall_filter_sets
+ i
;
782 static int seccomp_add_syscall_filter_set(scmp_filter_ctx seccomp
, const SyscallFilterSet
*set
, uint32_t action
, char **exclude
);
784 int seccomp_add_syscall_filter_item(scmp_filter_ctx
*seccomp
, const char *name
, uint32_t action
, char **exclude
) {
790 if (strv_contains(exclude
, name
))
793 if (name
[0] == '@') {
794 const SyscallFilterSet
*other
;
796 other
= syscall_filter_set_find(name
);
798 log_debug("Filter set %s is not known!", name
);
802 r
= seccomp_add_syscall_filter_set(seccomp
, other
, action
, exclude
);
808 id
= seccomp_syscall_resolve_name(name
);
809 if (id
== __NR_SCMP_ERROR
) {
810 log_debug("System call %s is not known!", name
);
811 return -EINVAL
; /* Not known at all? Then that's a real error */
814 r
= seccomp_rule_add_exact(seccomp
, action
, id
, 0);
816 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
817 log_debug_errno(r
, "Failed to add rule for system call %s() / %d, ignoring: %m", name
, id
);
823 static int seccomp_add_syscall_filter_set(
824 scmp_filter_ctx seccomp
,
825 const SyscallFilterSet
*set
,
835 NULSTR_FOREACH(sys
, set
->value
) {
836 r
= seccomp_add_syscall_filter_item(seccomp
, sys
, action
, exclude
);
844 int seccomp_load_syscall_filter_set(uint32_t default_action
, const SyscallFilterSet
*set
, uint32_t action
) {
850 /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
851 * earch local arch. */
853 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
854 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
856 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
858 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
862 r
= seccomp_add_syscall_filter_set(seccomp
, set
, action
, NULL
);
864 log_debug_errno(r
, "Failed to add filter set, ignoring: %m");
868 r
= seccomp_load(seccomp
);
869 if (IN_SET(r
, -EPERM
, -EACCES
))
872 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
878 int seccomp_load_syscall_filter_set_raw(uint32_t default_action
, Set
* set
, uint32_t action
) {
882 /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Set* of syscalls, instead of a
883 * SyscallFilterSet* table. */
885 if (set_isempty(set
) && default_action
== SCMP_ACT_ALLOW
)
888 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
889 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
893 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
895 r
= seccomp_init_for_arch(&seccomp
, arch
, default_action
);
899 SET_FOREACH(id
, set
, i
) {
900 r
= seccomp_rule_add_exact(seccomp
, action
, PTR_TO_INT(id
) - 1, 0);
902 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
903 _cleanup_free_
char *n
= NULL
;
905 n
= seccomp_syscall_resolve_num_arch(arch
, PTR_TO_INT(id
) - 1);
906 log_debug_errno(r
, "Failed to add rule for system call %s() / %d, ignoring: %m", strna(n
), PTR_TO_INT(id
) - 1);
910 r
= seccomp_load(seccomp
);
911 if (IN_SET(r
, -EPERM
, -EACCES
))
914 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
920 int seccomp_restrict_namespaces(unsigned long retain
) {
924 if (log_get_max_level() >= LOG_DEBUG
) {
925 _cleanup_free_
char *s
= NULL
;
927 (void) namespace_flag_to_string_many(retain
, &s
);
928 log_debug("Restricting namespace to: %s.", strna(s
));
932 if ((retain
& NAMESPACE_FLAGS_ALL
) == NAMESPACE_FLAGS_ALL
)
935 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
936 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
939 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
941 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
945 if ((retain
& NAMESPACE_FLAGS_ALL
) == 0)
946 /* If every single kind of namespace shall be prohibited, then let's block the whole setns() syscall
948 r
= seccomp_rule_add_exact(
950 SCMP_ACT_ERRNO(EPERM
),
954 /* Otherwise, block only the invocations with the appropriate flags in the loop below, but also the
955 * special invocation with a zero flags argument, right here. */
956 r
= seccomp_rule_add_exact(
958 SCMP_ACT_ERRNO(EPERM
),
961 SCMP_A1(SCMP_CMP_EQ
, 0));
963 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
967 for (i
= 0; namespace_flag_map
[i
].name
; i
++) {
970 f
= namespace_flag_map
[i
].flag
;
971 if ((retain
& f
) == f
) {
972 log_debug("Permitting %s.", namespace_flag_map
[i
].name
);
976 log_debug("Blocking %s.", namespace_flag_map
[i
].name
);
978 r
= seccomp_rule_add_exact(
980 SCMP_ACT_ERRNO(EPERM
),
983 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
985 log_debug_errno(r
, "Failed to add unshare() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
989 /* On s390/s390x the first two parameters to clone are switched */
990 if (!IN_SET(arch
, SCMP_ARCH_S390
, SCMP_ARCH_S390X
))
991 r
= seccomp_rule_add_exact(
993 SCMP_ACT_ERRNO(EPERM
),
996 SCMP_A0(SCMP_CMP_MASKED_EQ
, f
, f
));
998 r
= seccomp_rule_add_exact(
1000 SCMP_ACT_ERRNO(EPERM
),
1003 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1005 log_debug_errno(r
, "Failed to add clone() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1009 if ((retain
& NAMESPACE_FLAGS_ALL
) != 0) {
1010 r
= seccomp_rule_add_exact(
1012 SCMP_ACT_ERRNO(EPERM
),
1015 SCMP_A1(SCMP_CMP_MASKED_EQ
, f
, f
));
1017 log_debug_errno(r
, "Failed to add setns() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1025 r
= seccomp_load(seccomp
);
1026 if (IN_SET(r
, -EPERM
, -EACCES
))
1029 log_debug_errno(r
, "Failed to install namespace restriction rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1035 int seccomp_protect_sysctl(void) {
1039 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1040 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1042 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1044 if (IN_SET(arch
, SCMP_ARCH_X32
, SCMP_ARCH_AARCH64
))
1045 /* No _sysctl syscall */
1048 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1052 r
= seccomp_rule_add_exact(
1054 SCMP_ACT_ERRNO(EPERM
),
1058 log_debug_errno(r
, "Failed to add _sysctl() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1062 r
= seccomp_load(seccomp
);
1063 if (IN_SET(r
, -EPERM
, -EACCES
))
1066 log_debug_errno(r
, "Failed to install sysctl protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1072 int seccomp_restrict_address_families(Set
*address_families
, bool whitelist
) {
1076 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1077 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1081 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1085 case SCMP_ARCH_X86_64
:
1088 case SCMP_ARCH_AARCH64
:
1089 case SCMP_ARCH_PPC64
:
1090 case SCMP_ARCH_PPC64LE
:
1091 /* These we know we support (i.e. are the ones that do not use socketcall()) */
1095 case SCMP_ARCH_S390
:
1096 case SCMP_ARCH_S390X
:
1100 /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
1109 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1114 int af
, first
= 0, last
= 0;
1117 /* If this is a whitelist, we first block the address families that are out of range and then
1118 * everything that is not in the set. First, we find the lowest and highest address family in
1121 SET_FOREACH(afp
, address_families
, i
) {
1122 af
= PTR_TO_INT(afp
);
1124 if (af
<= 0 || af
>= af_max())
1127 if (first
== 0 || af
< first
)
1130 if (last
== 0 || af
> last
)
1134 assert((first
== 0) == (last
== 0));
1138 /* No entries in the valid range, block everything */
1139 r
= seccomp_rule_add_exact(
1141 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1145 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1151 /* Block everything below the first entry */
1152 r
= seccomp_rule_add_exact(
1154 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1157 SCMP_A0(SCMP_CMP_LT
, first
));
1159 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1163 /* Block everything above the last entry */
1164 r
= seccomp_rule_add_exact(
1166 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1169 SCMP_A0(SCMP_CMP_GT
, last
));
1171 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1175 /* Block everything between the first and last entry */
1176 for (af
= 1; af
< af_max(); af
++) {
1178 if (set_contains(address_families
, INT_TO_PTR(af
)))
1181 r
= seccomp_rule_add_exact(
1183 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1186 SCMP_A0(SCMP_CMP_EQ
, af
));
1192 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1200 /* If this is a blacklist, then generate one rule for
1201 * each address family that are then combined in OR
1204 SET_FOREACH(af
, address_families
, i
) {
1206 r
= seccomp_rule_add_exact(
1208 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
1211 SCMP_A0(SCMP_CMP_EQ
, PTR_TO_INT(af
)));
1217 log_debug_errno(r
, "Failed to add socket() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1222 r
= seccomp_load(seccomp
);
1223 if (IN_SET(r
, -EPERM
, -EACCES
))
1226 log_debug_errno(r
, "Failed to install socket family rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1232 int seccomp_restrict_realtime(void) {
1233 static const int permitted_policies
[] = {
1239 int r
, max_policy
= 0;
1243 /* Determine the highest policy constant we want to allow */
1244 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1245 if (permitted_policies
[i
] > max_policy
)
1246 max_policy
= permitted_policies
[i
];
1248 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1249 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1252 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1254 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1258 /* Go through all policies with lower values than that, and block them -- unless they appear in the
1260 for (p
= 0; p
< max_policy
; p
++) {
1263 /* Check if this is in the whitelist. */
1264 for (i
= 0; i
< ELEMENTSOF(permitted_policies
); i
++)
1265 if (permitted_policies
[i
] == p
) {
1273 /* Deny this policy */
1274 r
= seccomp_rule_add_exact(
1276 SCMP_ACT_ERRNO(EPERM
),
1277 SCMP_SYS(sched_setscheduler
),
1279 SCMP_A1(SCMP_CMP_EQ
, p
));
1281 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1286 /* Blacklist all other policies, i.e. the ones with higher values. Note that all comparisons are
1287 * unsigned here, hence no need no check for < 0 values. */
1288 r
= seccomp_rule_add_exact(
1290 SCMP_ACT_ERRNO(EPERM
),
1291 SCMP_SYS(sched_setscheduler
),
1293 SCMP_A1(SCMP_CMP_GT
, max_policy
));
1295 log_debug_errno(r
, "Failed to add scheduler rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1299 r
= seccomp_load(seccomp
);
1300 if (IN_SET(r
, -EPERM
, -EACCES
))
1303 log_debug_errno(r
, "Failed to install realtime protection rules for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1309 static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp
,
1312 unsigned int arg_cnt
,
1313 const struct scmp_arg_cmp arg
) {
1316 r
= seccomp_rule_add_exact(seccomp
, SCMP_ACT_ERRNO(EPERM
), nr
, arg_cnt
, arg
);
1318 _cleanup_free_
char *n
= NULL
;
1320 n
= seccomp_syscall_resolve_num_arch(arch
, nr
);
1321 log_debug_errno(r
, "Failed to add %s() rule for architecture %s, skipping: %m",
1323 seccomp_arch_to_string(arch
));
1329 /* For known architectures, check that syscalls are indeed defined or not. */
1330 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
1331 assert_cc(SCMP_SYS(shmget
) > 0);
1332 assert_cc(SCMP_SYS(shmat
) > 0);
1333 assert_cc(SCMP_SYS(shmdt
) > 0);
1334 #elif defined(__i386__) || defined(__powerpc64__)
1335 assert_cc(SCMP_SYS(shmget
) < 0);
1336 assert_cc(SCMP_SYS(shmat
) < 0);
1337 assert_cc(SCMP_SYS(shmdt
) < 0);
1340 int seccomp_memory_deny_write_execute(void) {
1345 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1346 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1347 int filter_syscall
= 0, block_syscall
= 0, shmat_syscall
= 0;
1349 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch
));
1354 filter_syscall
= SCMP_SYS(mmap2
);
1355 block_syscall
= SCMP_SYS(mmap
);
1358 case SCMP_ARCH_PPC64
:
1359 case SCMP_ARCH_PPC64LE
:
1360 filter_syscall
= SCMP_SYS(mmap
);
1362 /* Note that shmat() isn't available, and the call is multiplexed through ipc().
1363 * We ignore that here, which means there's still a way to get writable/executable
1364 * memory, if an IPC key is mapped like this. That's a pity, but no total loss. */
1369 filter_syscall
= SCMP_SYS(mmap2
); /* arm has only mmap2 */
1370 shmat_syscall
= SCMP_SYS(shmat
);
1373 case SCMP_ARCH_X86_64
:
1375 case SCMP_ARCH_AARCH64
:
1376 filter_syscall
= SCMP_SYS(mmap
); /* amd64, x32, and arm64 have only mmap */
1377 shmat_syscall
= SCMP_SYS(shmat
);
1380 /* Please add more definitions here, if you port systemd to other architectures! */
1382 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__)
1383 #warning "Consider adding the right mmap() syscall definitions here!"
1387 /* Can't filter mmap() on this arch, then skip it */
1388 if (filter_syscall
== 0)
1391 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1395 r
= add_seccomp_syscall_filter(seccomp
, arch
, filter_syscall
,
1397 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
|PROT_WRITE
, PROT_EXEC
|PROT_WRITE
));
1401 if (block_syscall
!= 0) {
1402 r
= add_seccomp_syscall_filter(seccomp
, arch
, block_syscall
, 0, (const struct scmp_arg_cmp
){} );
1407 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(mprotect
),
1409 SCMP_A2(SCMP_CMP_MASKED_EQ
, PROT_EXEC
, PROT_EXEC
));
1413 if (shmat_syscall
!= 0) {
1414 r
= add_seccomp_syscall_filter(seccomp
, arch
, SCMP_SYS(shmat
),
1416 SCMP_A2(SCMP_CMP_MASKED_EQ
, SHM_EXEC
, SHM_EXEC
));
1421 r
= seccomp_load(seccomp
);
1422 if (IN_SET(r
, -EPERM
, -EACCES
))
1425 log_debug_errno(r
, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
1431 int seccomp_restrict_archs(Set
*archs
) {
1432 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1437 /* This installs a filter with no rules, but that restricts the system call architectures to the specified
1440 seccomp
= seccomp_init(SCMP_ACT_ALLOW
);
1444 SET_FOREACH(id
, archs
, i
) {
1445 r
= seccomp_arch_add(seccomp
, PTR_TO_UINT32(id
) - 1);
1452 r
= seccomp_attr_set(seccomp
, SCMP_FLTATR_CTL_NNP
, 0);
1456 return seccomp_load(seccomp
);
1459 int parse_syscall_archs(char **l
, Set
**archs
) {
1460 _cleanup_set_free_ Set
*_archs
;
1467 r
= set_ensure_allocated(&_archs
, NULL
);
1471 STRV_FOREACH(s
, l
) {
1474 r
= seccomp_arch_from_string(*s
, &a
);
1478 r
= set_put(_archs
, UINT32_TO_PTR(a
+ 1));
1489 int seccomp_filter_set_add(Set
*filter
, bool add
, const SyscallFilterSet
*set
) {
1495 NULSTR_FOREACH(i
, set
->value
) {
1498 const SyscallFilterSet
*more
;
1500 more
= syscall_filter_set_find(i
);
1505 r
= seccomp_filter_set_add(filter
, add
, more
);
1511 id
= seccomp_syscall_resolve_name(i
);
1512 if (id
== __NR_SCMP_ERROR
)
1516 r
= set_put(filter
, INT_TO_PTR(id
+ 1));
1520 (void) set_remove(filter
, INT_TO_PTR(id
+ 1));
1527 int seccomp_lock_personality(unsigned long personality
) {
1531 if (personality
>= PERSONALITY_INVALID
)
1534 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
1535 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
1537 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
1541 r
= seccomp_rule_add_exact(
1543 SCMP_ACT_ERRNO(EPERM
),
1544 SCMP_SYS(personality
),
1546 SCMP_A0(SCMP_CMP_NE
, personality
));
1550 r
= seccomp_load(seccomp
);
1551 if (IN_SET(r
, -EPERM
, -EACCES
))
1554 log_debug_errno(r
, "Failed to enable personality lock for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));