]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-seccomp.c
008d013af6b7f6f8fa33eac9d510fe0946275c5d
[thirdparty/systemd.git] / src / nspawn / nspawn-seccomp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2016 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <linux/netlink.h>
8 #include <sys/capability.h>
9 #include <sys/types.h>
10
11 #if HAVE_SECCOMP
12 #include <seccomp.h>
13 #endif
14
15 #include "alloc-util.h"
16 #include "log.h"
17 #include "nspawn-seccomp.h"
18 #if HAVE_SECCOMP
19 #include "seccomp-util.h"
20 #endif
21 #include "string-util.h"
22 #include "strv.h"
23
24 #if HAVE_SECCOMP
25
26 static int seccomp_add_default_syscall_filter(
27 scmp_filter_ctx ctx,
28 uint32_t arch,
29 uint64_t cap_list_retain,
30 char **syscall_whitelist,
31 char **syscall_blacklist) {
32
33 static const struct {
34 uint64_t capability;
35 const char* name;
36 } whitelist[] = {
37 /* Let's use set names where we can */
38 { 0, "@aio" },
39 { 0, "@basic-io" },
40 { 0, "@chown" },
41 { 0, "@default" },
42 { 0, "@file-system" },
43 { 0, "@io-event" },
44 { 0, "@ipc" },
45 { 0, "@mount" },
46 { 0, "@network-io" },
47 { 0, "@process" },
48 { 0, "@resources" },
49 { 0, "@setuid" },
50 { 0, "@signal" },
51 { 0, "@sync" },
52 { 0, "@timer" },
53
54 /* The following four are sets we optionally enable, in case the caps have been configured for it */
55 { CAP_SYS_TIME, "@clock" },
56 { CAP_SYS_MODULE, "@module" },
57 { CAP_SYS_RAWIO, "@raw-io" },
58 { CAP_IPC_LOCK, "@memlock" },
59
60 /* Plus a good set of additional syscalls which are not part of any of the groups above */
61 { 0, "brk" },
62 { 0, "capget" },
63 { 0, "capset" },
64 { 0, "copy_file_range" },
65 { 0, "fadvise64" },
66 { 0, "fadvise64_64" },
67 { 0, "flock" },
68 { 0, "get_mempolicy" },
69 { 0, "getcpu" },
70 { 0, "getpriority" },
71 { 0, "getrandom" },
72 { 0, "ioctl" },
73 { 0, "ioprio_get" },
74 { 0, "kcmp" },
75 { 0, "madvise" },
76 { 0, "mincore" },
77 { 0, "mprotect" },
78 { 0, "mremap" },
79 { 0, "name_to_handle_at" },
80 { 0, "oldolduname" },
81 { 0, "olduname" },
82 { 0, "personality" },
83 { 0, "readahead" },
84 { 0, "readdir" },
85 { 0, "remap_file_pages" },
86 { 0, "sched_get_priority_max" },
87 { 0, "sched_get_priority_min" },
88 { 0, "sched_getaffinity" },
89 { 0, "sched_getattr" },
90 { 0, "sched_getparam" },
91 { 0, "sched_getscheduler" },
92 { 0, "sched_rr_get_interval" },
93 { 0, "sched_yield" },
94 { 0, "seccomp" },
95 { 0, "sendfile" },
96 { 0, "sendfile64" },
97 { 0, "setdomainname" },
98 { 0, "setfsgid" },
99 { 0, "setfsgid32" },
100 { 0, "setfsuid" },
101 { 0, "setfsuid32" },
102 { 0, "sethostname" },
103 { 0, "setpgid" },
104 { 0, "setsid" },
105 { 0, "splice" },
106 { 0, "sysinfo" },
107 { 0, "tee" },
108 { 0, "umask" },
109 { 0, "uname" },
110 { 0, "userfaultfd" },
111 { 0, "vmsplice" },
112
113 /* The following individual syscalls are added depending on specified caps */
114 { CAP_SYS_PACCT, "acct" },
115 { CAP_SYS_PTRACE, "process_vm_readv" },
116 { CAP_SYS_PTRACE, "process_vm_writev" },
117 { CAP_SYS_PTRACE, "ptrace" },
118 { CAP_SYS_BOOT, "reboot" },
119 { CAP_SYSLOG, "syslog" },
120 { CAP_SYS_TTY_CONFIG, "vhangup" },
121
122 /*
123 * The following syscalls and groups are knowingly excluded:
124 *
125 * @cpu-emulation
126 * @keyring (NB: keyring is not namespaced!)
127 * @obsolete
128 * @swap
129 *
130 * bpf (NB: bpffs is not namespaced!)
131 * fanotify_init
132 * fanotify_mark
133 * kexec_file_load
134 * kexec_load
135 * lookup_dcookie
136 * nfsservctl
137 * open_by_handle_at
138 * perf_event_open
139 * pkey_alloc
140 * pkey_free
141 * pkey_mprotect
142 * quotactl
143 */
144 };
145
146 int r, c = 0;
147 size_t i;
148 char **p;
149
150 for (i = 0; i < ELEMENTSOF(whitelist); i++) {
151 if (whitelist[i].capability != 0 && (cap_list_retain & (1ULL << whitelist[i].capability)) == 0)
152 continue;
153
154 r = seccomp_add_syscall_filter_item(ctx, whitelist[i].name, SCMP_ACT_ALLOW, syscall_blacklist);
155 if (r < 0)
156 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
157 log_debug_errno(r, "Failed to add rule for system call %s on %s, ignoring: %m", whitelist[i].name, seccomp_arch_to_string(arch));
158 else
159 c++;
160 }
161
162 STRV_FOREACH(p, syscall_whitelist) {
163 r = seccomp_add_syscall_filter_item(ctx, *p, SCMP_ACT_ALLOW, syscall_blacklist);
164 if (r < 0)
165 log_debug_errno(r, "Failed to add rule for system call %s on %s, ignoring: %m", *p, seccomp_arch_to_string(arch));
166 else
167 c++;
168 }
169
170 return c;
171 }
172
173 int setup_seccomp(uint64_t cap_list_retain, char **syscall_whitelist, char **syscall_blacklist) {
174 uint32_t arch;
175 int r;
176
177 if (!is_seccomp_available()) {
178 log_debug("SECCOMP features not detected in the kernel, disabling SECCOMP filterering");
179 return 0;
180 }
181
182 SECCOMP_FOREACH_LOCAL_ARCH(arch) {
183 _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
184
185 log_debug("Applying whitelist on architecture: %s", seccomp_arch_to_string(arch));
186
187 r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ERRNO(EPERM));
188 if (r < 0)
189 return log_error_errno(r, "Failed to allocate seccomp object: %m");
190
191 r = seccomp_add_default_syscall_filter(seccomp, arch, cap_list_retain, syscall_whitelist, syscall_blacklist);
192 if (r < 0)
193 return r;
194
195 r = seccomp_load(seccomp);
196 if (IN_SET(r, -EPERM, -EACCES))
197 return log_error_errno(r, "Failed to install seccomp filter: %m");
198 if (r < 0)
199 log_debug_errno(r, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch));
200 }
201
202 SECCOMP_FOREACH_LOCAL_ARCH(arch) {
203 _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
204
205 log_debug("Applying NETLINK_AUDIT mask on architecture: %s", seccomp_arch_to_string(arch));
206
207 r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW);
208 if (r < 0)
209 return log_error_errno(r, "Failed to allocate seccomp object: %m");
210
211 /*
212 Audit is broken in containers, much of the userspace audit hookup will fail if running inside a
213 container. We don't care and just turn off creation of audit sockets.
214
215 This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail with EAFNOSUPPORT which audit userspace uses
216 as indication that audit is disabled in the kernel.
217 */
218
219 r = seccomp_rule_add_exact(
220 seccomp,
221 SCMP_ACT_ERRNO(EAFNOSUPPORT),
222 SCMP_SYS(socket),
223 2,
224 SCMP_A0(SCMP_CMP_EQ, AF_NETLINK),
225 SCMP_A2(SCMP_CMP_EQ, NETLINK_AUDIT));
226 if (r < 0) {
227 log_debug_errno(r, "Failed to add audit seccomp rule, ignoring: %m");
228 continue;
229 }
230
231 r = seccomp_load(seccomp);
232 if (IN_SET(r, -EPERM, -EACCES))
233 return log_error_errno(r, "Failed to install seccomp audit filter: %m");
234 if (r < 0)
235 log_debug_errno(r, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch));
236 }
237
238 return 0;
239 }
240
241 #else
242
243 int setup_seccomp(uint64_t cap_list_retain, char **syscall_whitelist, char **syscall_blacklist) {
244 return 0;
245 }
246
247 #endif