]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-seccomp.c
2 This file is part of systemd.
4 Copyright 2016 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/netlink.h>
22 #include <sys/capability.h>
23 #include <sys/types.h>
29 #include "alloc-util.h"
31 #include "nspawn-seccomp.h"
33 #include "seccomp-util.h"
35 #include "string-util.h"
40 static int seccomp_add_default_syscall_filter(
43 uint64_t cap_list_retain
,
44 char **syscall_whitelist
,
45 char **syscall_blacklist
) {
51 /* Let's use set names where we can */
54 { 0, "@file-system" },
65 /* The following four are sets we optionally enable, in case the caps have been configured for it */
66 { CAP_SYS_TIME
, "@clock" },
67 { CAP_SYS_MODULE
, "@module" },
68 { CAP_SYS_RAWIO
, "@raw-io" },
69 { CAP_IPC_LOCK
, "@memlock" },
71 /* Plus a good set of additional syscalls which are not part of any of the groups above */
77 { 0, "copy_file_range" },
79 { 0, "fadvise64_64" },
86 { 0, "get_mempolicy" },
92 { 0, "io_getevents" },
105 { 0, "name_to_handle_at" },
106 { 0, "oldolduname" },
108 { 0, "personality" },
113 { 0, "remap_file_pages" },
114 { 0, "sched_get_priority_max" },
115 { 0, "sched_get_priority_min" },
116 { 0, "sched_getaffinity" },
117 { 0, "sched_getattr" },
118 { 0, "sched_getparam" },
119 { 0, "sched_getscheduler" },
120 { 0, "sched_rr_get_interval" },
121 { 0, "sched_yield" },
125 { 0, "setdomainname" },
130 { 0, "sethostname" },
135 { 0, "sync_file_range" },
142 { 0, "userfaultfd" },
145 /* The following individual syscalls are added depending on specified caps */
146 { CAP_SYS_PACCT
, "acct" },
147 { CAP_SYS_PTRACE
, "process_vm_readv" },
148 { CAP_SYS_PTRACE
, "process_vm_writev" },
149 { CAP_SYS_PTRACE
, "ptrace" },
150 { CAP_SYS_BOOT
, "reboot" },
151 { CAP_SYSLOG
, "syslog" },
152 { CAP_SYS_TTY_CONFIG
, "vhangup" },
155 * The following syscalls and groups are knowingly excluded:
158 * @keyring (NB: keyring is not namespaced!)
162 * bpf (NB: bpffs is not namespaced!)
182 for (i
= 0; i
< ELEMENTSOF(whitelist
); i
++) {
183 if (whitelist
[i
].capability
!= 0 && (cap_list_retain
& (1ULL << whitelist
[i
].capability
)) == 0)
186 r
= seccomp_add_syscall_filter_item(ctx
, whitelist
[i
].name
, SCMP_ACT_ALLOW
, syscall_blacklist
);
188 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
189 log_debug_errno(r
, "Failed to add rule for system call %s on %s, ignoring: %m", whitelist
[i
].name
, seccomp_arch_to_string(arch
));
194 STRV_FOREACH(p
, syscall_whitelist
) {
195 r
= seccomp_add_syscall_filter_item(ctx
, *p
, SCMP_ACT_ALLOW
, syscall_blacklist
);
197 log_debug_errno(r
, "Failed to add rule for system call %s on %s, ignoring: %m", *p
, seccomp_arch_to_string(arch
));
205 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_whitelist
, char **syscall_blacklist
) {
209 if (!is_seccomp_available()) {
210 log_debug("SECCOMP features not detected in the kernel, disabling SECCOMP filterering");
214 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
215 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
217 log_debug("Applying whitelist on architecture: %s", seccomp_arch_to_string(arch
));
219 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ERRNO(EPERM
));
221 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
223 r
= seccomp_add_default_syscall_filter(seccomp
, arch
, cap_list_retain
, syscall_whitelist
, syscall_blacklist
);
227 r
= seccomp_load(seccomp
);
228 if (IN_SET(r
, -EPERM
, -EACCES
))
229 return log_error_errno(r
, "Failed to install seccomp filter: %m");
231 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
234 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
235 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
237 log_debug("Applying NETLINK_AUDIT mask on architecture: %s", seccomp_arch_to_string(arch
));
239 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
241 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
244 Audit is broken in containers, much of the userspace audit hookup will fail if running inside a
245 container. We don't care and just turn off creation of audit sockets.
247 This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail with EAFNOSUPPORT which audit userspace uses
248 as indication that audit is disabled in the kernel.
251 r
= seccomp_rule_add_exact(
253 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
256 SCMP_A0(SCMP_CMP_EQ
, AF_NETLINK
),
257 SCMP_A2(SCMP_CMP_EQ
, NETLINK_AUDIT
));
259 log_debug_errno(r
, "Failed to add audit seccomp rule, ignoring: %m");
263 r
= seccomp_load(seccomp
);
264 if (IN_SET(r
, -EPERM
, -EACCES
))
265 return log_error_errno(r
, "Failed to install seccomp audit filter: %m");
267 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
275 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_whitelist
, char **syscall_blacklist
) {