]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-seccomp.c
0b39cda9ba9b8f4c7dc4e56f4938a4cd1e458bdd
1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/netlink.h>
5 #include <sys/capability.h>
6 #include <sys/socket.h>
13 #include "alloc-util.h"
15 #include "nspawn-seccomp.h"
17 #include "seccomp-util.h"
19 #include "string-util.h"
24 static int seccomp_add_default_syscall_filter(
27 uint64_t cap_list_retain
,
28 char **syscall_whitelist
,
29 char **syscall_blacklist
) {
35 /* Let's use set names where we can */
40 { 0, "@file-system" },
52 /* The following four are sets we optionally enable, in case the caps have been configured for it */
53 { CAP_SYS_TIME
, "@clock" },
54 { CAP_SYS_MODULE
, "@module" },
55 { CAP_SYS_RAWIO
, "@raw-io" },
56 { CAP_IPC_LOCK
, "@memlock" },
58 /* Plus a good set of additional syscalls which are not part of any of the groups above */
62 { 0, "copy_file_range" },
64 { 0, "fadvise64_64" },
66 { 0, "get_mempolicy" },
77 { 0, "name_to_handle_at" },
83 { 0, "remap_file_pages" },
84 { 0, "sched_get_priority_max" },
85 { 0, "sched_get_priority_min" },
86 { 0, "sched_getaffinity" },
87 { 0, "sched_getattr" },
88 { 0, "sched_getparam" },
89 { 0, "sched_getscheduler" },
90 { 0, "sched_rr_get_interval" },
95 { 0, "setdomainname" },
100 { 0, "sethostname" },
108 { 0, "userfaultfd" },
111 /* The following individual syscalls are added depending on specified caps */
112 { CAP_SYS_PACCT
, "acct" },
113 { CAP_SYS_PTRACE
, "process_vm_readv" },
114 { CAP_SYS_PTRACE
, "process_vm_writev" },
115 { CAP_SYS_PTRACE
, "ptrace" },
116 { CAP_SYS_BOOT
, "reboot" },
117 { CAP_SYSLOG
, "syslog" },
118 { CAP_SYS_TTY_CONFIG
, "vhangup" },
121 * The following syscalls and groups are knowingly excluded:
124 * @keyring (NB: keyring is not namespaced!)
129 * bpf (NB: bpffs is not namespaced!)
146 for (i
= 0; i
< ELEMENTSOF(whitelist
); i
++) {
147 if (whitelist
[i
].capability
!= 0 && (cap_list_retain
& (1ULL << whitelist
[i
].capability
)) == 0)
150 r
= seccomp_add_syscall_filter_item(ctx
, whitelist
[i
].name
, SCMP_ACT_ALLOW
, syscall_blacklist
, false);
152 return log_error_errno(r
, "Failed to add syscall filter item %s: %m", whitelist
[i
].name
);
155 STRV_FOREACH(p
, syscall_whitelist
) {
156 r
= seccomp_add_syscall_filter_item(ctx
, *p
, SCMP_ACT_ALLOW
, syscall_blacklist
, false);
158 log_warning_errno(r
, "Failed to add rule for system call %s on %s, ignoring: %m",
159 *p
, seccomp_arch_to_string(arch
));
165 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_whitelist
, char **syscall_blacklist
) {
169 if (!is_seccomp_available()) {
170 log_debug("SECCOMP features not detected in the kernel, disabling SECCOMP filterering");
174 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
175 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
177 log_debug("Applying whitelist on architecture: %s", seccomp_arch_to_string(arch
));
179 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ERRNO(EPERM
));
181 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
183 r
= seccomp_add_default_syscall_filter(seccomp
, arch
, cap_list_retain
, syscall_whitelist
, syscall_blacklist
);
187 r
= seccomp_load(seccomp
);
188 if (ERRNO_IS_SECCOMP_FATAL(r
))
189 return log_error_errno(r
, "Failed to install seccomp filter: %m");
191 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
194 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
195 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
197 log_debug("Applying NETLINK_AUDIT mask on architecture: %s", seccomp_arch_to_string(arch
));
199 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
201 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
204 Audit is broken in containers, much of the userspace audit hookup will fail if running inside a
205 container. We don't care and just turn off creation of audit sockets.
207 This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail with EAFNOSUPPORT which audit userspace uses
208 as indication that audit is disabled in the kernel.
211 r
= seccomp_rule_add_exact(
213 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
216 SCMP_A0(SCMP_CMP_EQ
, AF_NETLINK
),
217 SCMP_A2(SCMP_CMP_EQ
, NETLINK_AUDIT
));
219 log_debug_errno(r
, "Failed to add audit seccomp rule, ignoring: %m");
223 r
= seccomp_load(seccomp
);
224 if (ERRNO_IS_SECCOMP_FATAL(r
))
225 return log_error_errno(r
, "Failed to install seccomp audit filter: %m");
227 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
235 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_whitelist
, char **syscall_blacklist
) {