]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-seccomp.c
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!)
128 * bpf (NB: bpffs is not namespaced!)
148 for (i
= 0; i
< ELEMENTSOF(whitelist
); i
++) {
149 if (whitelist
[i
].capability
!= 0 && (cap_list_retain
& (1ULL << whitelist
[i
].capability
)) == 0)
152 r
= seccomp_add_syscall_filter_item(ctx
, whitelist
[i
].name
, SCMP_ACT_ALLOW
, syscall_blacklist
, false);
154 return log_error_errno(r
, "Failed to add syscall filter item %s: %m", whitelist
[i
].name
);
157 STRV_FOREACH(p
, syscall_whitelist
) {
158 r
= seccomp_add_syscall_filter_item(ctx
, *p
, SCMP_ACT_ALLOW
, syscall_blacklist
, false);
160 log_warning_errno(r
, "Failed to add rule for system call %s on %s, ignoring: %m",
161 *p
, seccomp_arch_to_string(arch
));
167 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_whitelist
, char **syscall_blacklist
) {
171 if (!is_seccomp_available()) {
172 log_debug("SECCOMP features not detected in the kernel, disabling SECCOMP filterering");
176 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
177 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
179 log_debug("Applying whitelist on architecture: %s", seccomp_arch_to_string(arch
));
181 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ERRNO(EPERM
));
183 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
185 r
= seccomp_add_default_syscall_filter(seccomp
, arch
, cap_list_retain
, syscall_whitelist
, syscall_blacklist
);
189 r
= seccomp_load(seccomp
);
190 if (ERRNO_IS_SECCOMP_FATAL(r
))
191 return log_error_errno(r
, "Failed to install seccomp filter: %m");
193 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
196 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
197 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
199 log_debug("Applying NETLINK_AUDIT mask on architecture: %s", seccomp_arch_to_string(arch
));
201 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
203 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
206 Audit is broken in containers, much of the userspace audit hookup will fail if running inside a
207 container. We don't care and just turn off creation of audit sockets.
209 This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail with EAFNOSUPPORT which audit userspace uses
210 as indication that audit is disabled in the kernel.
213 r
= seccomp_rule_add_exact(
215 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
218 SCMP_A0(SCMP_CMP_EQ
, AF_NETLINK
),
219 SCMP_A2(SCMP_CMP_EQ
, NETLINK_AUDIT
));
221 log_debug_errno(r
, "Failed to add audit seccomp rule, ignoring: %m");
225 r
= seccomp_load(seccomp
);
226 if (ERRNO_IS_SECCOMP_FATAL(r
))
227 return log_error_errno(r
, "Failed to install seccomp audit filter: %m");
229 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
237 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_whitelist
, char **syscall_blacklist
) {