]>
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_allow_list
,
29 char **syscall_deny_list
) {
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!)
145 for (size_t i
= 0; i
< ELEMENTSOF(allow_list
); i
++) {
146 if (allow_list
[i
].capability
!= 0 && (cap_list_retain
& (1ULL << allow_list
[i
].capability
)) == 0)
149 r
= seccomp_add_syscall_filter_item(ctx
, allow_list
[i
].name
, SCMP_ACT_ALLOW
, syscall_deny_list
, false);
151 return log_error_errno(r
, "Failed to add syscall filter item %s: %m", allow_list
[i
].name
);
154 STRV_FOREACH(p
, syscall_allow_list
) {
155 r
= seccomp_add_syscall_filter_item(ctx
, *p
, SCMP_ACT_ALLOW
, syscall_deny_list
, true);
157 log_warning_errno(r
, "Failed to add rule for system call %s on %s, ignoring: %m",
158 *p
, seccomp_arch_to_string(arch
));
164 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_allow_list
, char **syscall_deny_list
) {
168 if (!is_seccomp_available()) {
169 log_debug("SECCOMP features not detected in the kernel, disabling SECCOMP filterering");
173 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
174 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
176 log_debug("Applying allow list on architecture: %s", seccomp_arch_to_string(arch
));
178 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ERRNO(EPERM
));
180 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
182 r
= seccomp_add_default_syscall_filter(seccomp
, arch
, cap_list_retain
, syscall_allow_list
, syscall_deny_list
);
186 r
= seccomp_load(seccomp
);
187 if (ERRNO_IS_SECCOMP_FATAL(r
))
188 return log_error_errno(r
, "Failed to install seccomp filter: %m");
190 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
193 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
194 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
196 log_debug("Applying NETLINK_AUDIT mask on architecture: %s", seccomp_arch_to_string(arch
));
198 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
200 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
203 Audit is broken in containers, much of the userspace audit hookup will fail if running inside a
204 container. We don't care and just turn off creation of audit sockets.
206 This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail with EAFNOSUPPORT which audit userspace uses
207 as indication that audit is disabled in the kernel.
210 r
= seccomp_rule_add_exact(
212 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
215 SCMP_A0(SCMP_CMP_EQ
, AF_NETLINK
),
216 SCMP_A2(SCMP_CMP_EQ
, NETLINK_AUDIT
));
218 log_debug_errno(r
, "Failed to add audit seccomp rule, ignoring: %m");
222 r
= seccomp_load(seccomp
);
223 if (ERRNO_IS_SECCOMP_FATAL(r
))
224 return log_error_errno(r
, "Failed to install seccomp audit filter: %m");
226 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch
));
234 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_allow_list
, char **syscall_deny_list
) {