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