1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/netlink.h>
5 #include <sys/capability.h>
6 #include <sys/socket.h>
9 #include "alloc-util.h"
11 #include "nspawn-seccomp.h"
12 #include "seccomp-util.h"
13 #include "string-util.h"
18 static int add_syscall_filters(
21 uint64_t cap_list_retain
,
22 char **syscall_allow_list
,
23 char **syscall_deny_list
) {
29 /* Let's use set names where we can */
34 { 0, "@file-system" },
46 /* The following four are sets we optionally enable, n case the caps have been configured for it */
47 { CAP_SYS_TIME
, "@clock" },
48 { CAP_SYS_MODULE
, "@module" },
49 { CAP_SYS_RAWIO
, "@raw-io" },
50 { CAP_IPC_LOCK
, "@memlock" },
52 /* Plus a good set of additional syscalls which are not part of any of the groups above */
53 { 0, "arm_fadvise64_64" },
57 { 0, "copy_file_range" },
59 { 0, "fadvise64_64" },
61 { 0, "get_mempolicy" },
72 { 0, "name_to_handle_at" },
78 { 0, "remap_file_pages" },
79 { 0, "sched_get_priority_max" },
80 { 0, "sched_get_priority_min" },
81 { 0, "sched_getaffinity" },
82 { 0, "sched_getattr" },
83 { 0, "sched_getparam" },
84 { 0, "sched_getscheduler" },
85 { 0, "sched_rr_get_interval" },
86 { 0, "sched_rr_get_interval_time64" },
90 { 0, "setdomainname" },
103 { 0, "userfaultfd" },
106 /* The following individual syscalls are added depending on specified caps */
107 { CAP_SYS_PACCT
, "acct" },
108 { CAP_SYS_PTRACE
, "process_vm_readv" },
109 { CAP_SYS_PTRACE
, "process_vm_writev" },
110 { CAP_SYS_PTRACE
, "ptrace" },
111 { CAP_SYS_BOOT
, "reboot" },
112 { CAP_SYSLOG
, "syslog" },
113 { CAP_SYS_TTY_CONFIG
, "vhangup" },
116 * The following syscalls and groups are knowingly excluded:
119 * @keyring (NB: keyring is not namespaced!)
137 _cleanup_strv_free_
char **added
= NULL
;
140 for (size_t i
= 0; i
< ELEMENTSOF(allow_list
); i
++) {
141 if (allow_list
[i
].capability
!= 0 && (cap_list_retain
& (1ULL << allow_list
[i
].capability
)) == 0)
144 r
= seccomp_add_syscall_filter_item(ctx
,
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, &added
);
157 log_warning_errno(r
, "Failed to add rule for system call %s on %s, ignoring: %m",
158 *p
, seccomp_arch_to_string(arch
));
161 /* The default action is ENOSYS. Respond with EPERM to all other "known" but not allow-listed
163 r
= seccomp_add_syscall_filter_item(ctx
, "@known", SCMP_ACT_ERRNO(EPERM
), added
, true, NULL
);
165 log_warning_errno(r
, "Failed to add rule for @known set on %s, ignoring: %m",
166 seccomp_arch_to_string(arch
));
168 #if (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 5) || SCMP_VER_MAJOR > 2
169 /* We have a large filter here, so let's turn on the binary tree mode if possible. */
170 r
= seccomp_attr_set(ctx
, SCMP_FLTATR_CTL_OPTIMIZE
, 2);
172 log_warning_errno(r
, "Failed to set SCMP_FLTATR_CTL_OPTIMIZE, ignoring: %m");
178 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_allow_list
, char **syscall_deny_list
) {
182 if (!is_seccomp_available()) {
183 log_debug("SECCOMP features not detected in the kernel or disabled at runtime, disabling SECCOMP filtering");
187 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
188 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
190 log_debug("Applying allow list on architecture: %s", seccomp_arch_to_string(arch
));
192 /* We install ENOSYS as the default action, but it will only apply to syscalls which are not
193 * in the @known set, see above. */
194 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ERRNO(ENOSYS
));
196 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
198 r
= add_syscall_filters(seccomp
, arch
, cap_list_retain
, syscall_allow_list
, syscall_deny_list
);
202 r
= seccomp_load(seccomp
);
203 if (ERRNO_IS_NEG_SECCOMP_FATAL(r
))
204 return log_error_errno(r
, "Failed to install seccomp filter: %m");
206 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m",
207 seccomp_arch_to_string(arch
));
210 SECCOMP_FOREACH_LOCAL_ARCH(arch
) {
211 _cleanup_(seccomp_releasep
) scmp_filter_ctx seccomp
= NULL
;
213 log_debug("Applying NETLINK_AUDIT mask on architecture: %s", seccomp_arch_to_string(arch
));
215 r
= seccomp_init_for_arch(&seccomp
, arch
, SCMP_ACT_ALLOW
);
217 return log_error_errno(r
, "Failed to allocate seccomp object: %m");
220 Audit is broken in containers, much of the userspace audit hookup will fail if running inside a
221 container. We don't care and just turn off creation of audit sockets.
223 This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail with EAFNOSUPPORT which audit userspace uses
224 as indication that audit is disabled in the kernel.
227 r
= seccomp_rule_add_exact(
229 SCMP_ACT_ERRNO(EAFNOSUPPORT
),
232 SCMP_A0(SCMP_CMP_EQ
, AF_NETLINK
),
233 SCMP_A2(SCMP_CMP_EQ
, NETLINK_AUDIT
));
235 log_debug_errno(r
, "Failed to add audit seccomp rule, ignoring: %m");
239 r
= seccomp_load(seccomp
);
240 if (ERRNO_IS_NEG_SECCOMP_FATAL(r
))
241 return log_error_errno(r
, "Failed to install seccomp audit filter: %m");
243 log_debug_errno(r
, "Failed to install filter set for architecture %s, skipping: %m",
244 seccomp_arch_to_string(arch
));
252 int setup_seccomp(uint64_t cap_list_retain
, char **syscall_allow_list
, char **syscall_deny_list
) {