blob: 5c73e6ad058ef6a9777300beaa607a1bcdb38bd6 [file] [log] [blame]
Eduardo Otubo2f668be2012-08-14 18:44:06 -03001/*
2 * QEMU seccomp mode 2 support with libseccomp
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Eduardo Otubo <eotubo@br.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
14 */
Markus Armbruster65484592018-10-17 10:26:41 +020015
Peter Maydelld38ea872016-01-29 17:50:05 +000016#include "qemu/osdep.h"
Markus Armbruster65484592018-10-17 10:26:41 +020017#include "qapi/error.h"
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +080018#include "qemu/config-file.h"
19#include "qemu/option.h"
20#include "qemu/module.h"
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +080021#include <sys/prctl.h>
Eduardo Otubo2f668be2012-08-14 18:44:06 -030022#include <seccomp.h>
Paolo Bonzini9c17d612012-12-17 18:20:04 +010023#include "sysemu/seccomp.h"
Marc-André Lureaubda08a52018-08-22 19:02:48 +020024#include <linux/seccomp.h>
Eduardo Otubo2f668be2012-08-14 18:44:06 -030025
James Hogan81bed732016-04-08 14:16:33 +010026/* For some architectures (notably ARM) cacheflush is not supported until
27 * libseccomp 2.2.3, but configure enforces that we are using a more recent
28 * version on those hosts, so it is OK for this check to be less strict.
29 */
Andrew Jones47d20672015-11-02 23:53:26 +010030#if SCMP_VER_MAJOR >= 3
31 #define HAVE_CACHEFLUSH
James Hogan81bed732016-04-08 14:16:33 +010032#elif SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 2
Andrew Jones47d20672015-11-02 23:53:26 +010033 #define HAVE_CACHEFLUSH
34#endif
35
Eduardo Otubo2f668be2012-08-14 18:44:06 -030036struct QemuSeccompSyscall {
37 int32_t num;
Eduardo Otubo1bd61522017-02-28 21:13:12 +010038 uint8_t set;
Marc-André Lureau056de1e2018-07-10 16:55:57 +020039 uint8_t narg;
40 const struct scmp_arg_cmp *arg_cmp;
41};
42
43const struct scmp_arg_cmp sched_setscheduler_arg[] = {
44 SCMP_A1(SCMP_CMP_NE, SCHED_IDLE)
Eduardo Otubo2f668be2012-08-14 18:44:06 -030045};
46
Eduardo Otubo1bd61522017-02-28 21:13:12 +010047static const struct QemuSeccompSyscall blacklist[] = {
48 /* default set of syscalls to blacklist */
49 { SCMP_SYS(reboot), QEMU_SECCOMP_SET_DEFAULT },
50 { SCMP_SYS(swapon), QEMU_SECCOMP_SET_DEFAULT },
51 { SCMP_SYS(swapoff), QEMU_SECCOMP_SET_DEFAULT },
52 { SCMP_SYS(syslog), QEMU_SECCOMP_SET_DEFAULT },
53 { SCMP_SYS(mount), QEMU_SECCOMP_SET_DEFAULT },
54 { SCMP_SYS(umount), QEMU_SECCOMP_SET_DEFAULT },
55 { SCMP_SYS(kexec_load), QEMU_SECCOMP_SET_DEFAULT },
56 { SCMP_SYS(afs_syscall), QEMU_SECCOMP_SET_DEFAULT },
57 { SCMP_SYS(break), QEMU_SECCOMP_SET_DEFAULT },
58 { SCMP_SYS(ftime), QEMU_SECCOMP_SET_DEFAULT },
59 { SCMP_SYS(getpmsg), QEMU_SECCOMP_SET_DEFAULT },
60 { SCMP_SYS(gtty), QEMU_SECCOMP_SET_DEFAULT },
61 { SCMP_SYS(lock), QEMU_SECCOMP_SET_DEFAULT },
62 { SCMP_SYS(mpx), QEMU_SECCOMP_SET_DEFAULT },
63 { SCMP_SYS(prof), QEMU_SECCOMP_SET_DEFAULT },
64 { SCMP_SYS(profil), QEMU_SECCOMP_SET_DEFAULT },
65 { SCMP_SYS(putpmsg), QEMU_SECCOMP_SET_DEFAULT },
66 { SCMP_SYS(security), QEMU_SECCOMP_SET_DEFAULT },
67 { SCMP_SYS(stty), QEMU_SECCOMP_SET_DEFAULT },
68 { SCMP_SYS(tuxcall), QEMU_SECCOMP_SET_DEFAULT },
69 { SCMP_SYS(ulimit), QEMU_SECCOMP_SET_DEFAULT },
70 { SCMP_SYS(vserver), QEMU_SECCOMP_SET_DEFAULT },
Eduardo Otubo2b716fa2017-03-01 23:17:29 +010071 /* obsolete */
72 { SCMP_SYS(readdir), QEMU_SECCOMP_SET_OBSOLETE },
73 { SCMP_SYS(_sysctl), QEMU_SECCOMP_SET_OBSOLETE },
74 { SCMP_SYS(bdflush), QEMU_SECCOMP_SET_OBSOLETE },
75 { SCMP_SYS(create_module), QEMU_SECCOMP_SET_OBSOLETE },
76 { SCMP_SYS(get_kernel_syms), QEMU_SECCOMP_SET_OBSOLETE },
77 { SCMP_SYS(query_module), QEMU_SECCOMP_SET_OBSOLETE },
78 { SCMP_SYS(sgetmask), QEMU_SECCOMP_SET_OBSOLETE },
79 { SCMP_SYS(ssetmask), QEMU_SECCOMP_SET_OBSOLETE },
80 { SCMP_SYS(sysfs), QEMU_SECCOMP_SET_OBSOLETE },
81 { SCMP_SYS(uselib), QEMU_SECCOMP_SET_OBSOLETE },
82 { SCMP_SYS(ustat), QEMU_SECCOMP_SET_OBSOLETE },
Eduardo Otubo73a1e642017-03-13 22:13:27 +010083 /* privileged */
84 { SCMP_SYS(setuid), QEMU_SECCOMP_SET_PRIVILEGED },
85 { SCMP_SYS(setgid), QEMU_SECCOMP_SET_PRIVILEGED },
86 { SCMP_SYS(setpgid), QEMU_SECCOMP_SET_PRIVILEGED },
87 { SCMP_SYS(setsid), QEMU_SECCOMP_SET_PRIVILEGED },
88 { SCMP_SYS(setreuid), QEMU_SECCOMP_SET_PRIVILEGED },
89 { SCMP_SYS(setregid), QEMU_SECCOMP_SET_PRIVILEGED },
90 { SCMP_SYS(setresuid), QEMU_SECCOMP_SET_PRIVILEGED },
91 { SCMP_SYS(setresgid), QEMU_SECCOMP_SET_PRIVILEGED },
92 { SCMP_SYS(setfsuid), QEMU_SECCOMP_SET_PRIVILEGED },
93 { SCMP_SYS(setfsgid), QEMU_SECCOMP_SET_PRIVILEGED },
Eduardo Otubo995a2262017-03-13 22:16:01 +010094 /* spawn */
95 { SCMP_SYS(fork), QEMU_SECCOMP_SET_SPAWN },
96 { SCMP_SYS(vfork), QEMU_SECCOMP_SET_SPAWN },
97 { SCMP_SYS(execve), QEMU_SECCOMP_SET_SPAWN },
Eduardo Otubo24f8cdc2017-03-13 22:18:51 +010098 /* resource control */
99 { SCMP_SYS(getpriority), QEMU_SECCOMP_SET_RESOURCECTL },
100 { SCMP_SYS(setpriority), QEMU_SECCOMP_SET_RESOURCECTL },
101 { SCMP_SYS(sched_setparam), QEMU_SECCOMP_SET_RESOURCECTL },
102 { SCMP_SYS(sched_getparam), QEMU_SECCOMP_SET_RESOURCECTL },
Marc-André Lureau056de1e2018-07-10 16:55:57 +0200103 { SCMP_SYS(sched_setscheduler), QEMU_SECCOMP_SET_RESOURCECTL,
104 ARRAY_SIZE(sched_setscheduler_arg), sched_setscheduler_arg },
Eduardo Otubo24f8cdc2017-03-13 22:18:51 +0100105 { SCMP_SYS(sched_getscheduler), QEMU_SECCOMP_SET_RESOURCECTL },
106 { SCMP_SYS(sched_setaffinity), QEMU_SECCOMP_SET_RESOURCECTL },
107 { SCMP_SYS(sched_getaffinity), QEMU_SECCOMP_SET_RESOURCECTL },
108 { SCMP_SYS(sched_get_priority_max), QEMU_SECCOMP_SET_RESOURCECTL },
109 { SCMP_SYS(sched_get_priority_min), QEMU_SECCOMP_SET_RESOURCECTL },
Eduardo Otubo2f668be2012-08-14 18:44:06 -0300110};
111
Marc-André Lureaubda08a52018-08-22 19:02:48 +0200112static inline __attribute__((unused)) int
113qemu_seccomp(unsigned int operation, unsigned int flags, void *args)
114{
115#ifdef __NR_seccomp
116 return syscall(__NR_seccomp, operation, flags, args);
117#else
118 errno = ENOSYS;
119 return -1;
120#endif
121}
122
123static uint32_t qemu_seccomp_get_kill_action(void)
124{
125#if defined(SECCOMP_GET_ACTION_AVAIL) && defined(SCMP_ACT_KILL_PROCESS) && \
126 defined(SECCOMP_RET_KILL_PROCESS)
127 {
128 uint32_t action = SECCOMP_RET_KILL_PROCESS;
129
130 if (qemu_seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &action) == 0) {
131 return SCMP_ACT_KILL_PROCESS;
132 }
133 }
134#endif
135
136 return SCMP_ACT_TRAP;
137}
138
Eduardo Otubo2b716fa2017-03-01 23:17:29 +0100139
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +0800140static int seccomp_start(uint32_t seccomp_opts)
Eduardo Otubo2f668be2012-08-14 18:44:06 -0300141{
142 int rc = 0;
143 unsigned int i = 0;
144 scmp_filter_ctx ctx;
Marc-André Lureaubda08a52018-08-22 19:02:48 +0200145 uint32_t action = qemu_seccomp_get_kill_action();
Eduardo Otubo2f668be2012-08-14 18:44:06 -0300146
Eduardo Otubo1bd61522017-02-28 21:13:12 +0100147 ctx = seccomp_init(SCMP_ACT_ALLOW);
Eduardo Otubo2f668be2012-08-14 18:44:06 -0300148 if (ctx == NULL) {
Corey Bryant2a13f992013-12-18 11:48:11 -0500149 rc = -1;
Eduardo Otubo2f668be2012-08-14 18:44:06 -0300150 goto seccomp_return;
151 }
152
Marc-André Lureau70dfabe2018-08-22 19:02:50 +0200153 rc = seccomp_attr_set(ctx, SCMP_FLTATR_CTL_TSYNC, 1);
154 if (rc != 0) {
155 goto seccomp_return;
156 }
157
Eduardo Otubo1bd61522017-02-28 21:13:12 +0100158 for (i = 0; i < ARRAY_SIZE(blacklist); i++) {
Eduardo Otubo2b716fa2017-03-01 23:17:29 +0100159 if (!(seccomp_opts & blacklist[i].set)) {
160 continue;
161 }
162
Marc-André Lureaubda08a52018-08-22 19:02:48 +0200163 rc = seccomp_rule_add_array(ctx, action, blacklist[i].num,
Marc-André Lureau056de1e2018-07-10 16:55:57 +0200164 blacklist[i].narg, blacklist[i].arg_cmp);
Eduardo Otubo2f668be2012-08-14 18:44:06 -0300165 if (rc < 0) {
166 goto seccomp_return;
167 }
168 }
169
170 rc = seccomp_load(ctx);
171
172 seccomp_return:
173 seccomp_release(ctx);
174 return rc;
175}
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +0800176
177#ifdef CONFIG_SECCOMP
178int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
179{
180 if (qemu_opt_get_bool(opts, "enable", false)) {
181 uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
182 | QEMU_SECCOMP_SET_OBSOLETE;
183 const char *value = NULL;
184
185 value = qemu_opt_get(opts, "obsolete");
186 if (value) {
187 if (g_str_equal(value, "allow")) {
188 seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
189 } else if (g_str_equal(value, "deny")) {
190 /* this is the default option, this if is here
191 * to provide a little bit of consistency for
192 * the command line */
193 } else {
Markus Armbruster65484592018-10-17 10:26:41 +0200194 error_setg(errp, "invalid argument for obsolete");
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +0800195 return -1;
196 }
197 }
198
199 value = qemu_opt_get(opts, "elevateprivileges");
200 if (value) {
201 if (g_str_equal(value, "deny")) {
202 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
203 } else if (g_str_equal(value, "children")) {
204 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
205
206 /* calling prctl directly because we're
207 * not sure if host has CAP_SYS_ADMIN set*/
208 if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
Markus Armbruster65484592018-10-17 10:26:41 +0200209 error_setg(errp, "failed to set no_new_privs aborting");
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +0800210 return -1;
211 }
212 } else if (g_str_equal(value, "allow")) {
213 /* default value */
214 } else {
Markus Armbruster65484592018-10-17 10:26:41 +0200215 error_setg(errp, "invalid argument for elevateprivileges");
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +0800216 return -1;
217 }
218 }
219
220 value = qemu_opt_get(opts, "spawn");
221 if (value) {
222 if (g_str_equal(value, "deny")) {
223 seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
224 } else if (g_str_equal(value, "allow")) {
225 /* default value */
226 } else {
Markus Armbruster65484592018-10-17 10:26:41 +0200227 error_setg(errp, "invalid argument for spawn");
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +0800228 return -1;
229 }
230 }
231
232 value = qemu_opt_get(opts, "resourcecontrol");
233 if (value) {
234 if (g_str_equal(value, "deny")) {
235 seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
236 } else if (g_str_equal(value, "allow")) {
237 /* default value */
238 } else {
Markus Armbruster65484592018-10-17 10:26:41 +0200239 error_setg(errp, "invalid argument for resourcecontrol");
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +0800240 return -1;
241 }
242 }
243
244 if (seccomp_start(seccomp_opts) < 0) {
Markus Armbruster65484592018-10-17 10:26:41 +0200245 error_setg(errp, "failed to install seccomp syscall filter "
246 "in the kernel");
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +0800247 return -1;
248 }
249 }
250
251 return 0;
252}
253
254static QemuOptsList qemu_sandbox_opts = {
255 .name = "sandbox",
256 .implied_opt_name = "enable",
257 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
258 .desc = {
259 {
260 .name = "enable",
261 .type = QEMU_OPT_BOOL,
262 },
263 {
264 .name = "obsolete",
265 .type = QEMU_OPT_STRING,
266 },
267 {
268 .name = "elevateprivileges",
269 .type = QEMU_OPT_STRING,
270 },
271 {
272 .name = "spawn",
273 .type = QEMU_OPT_STRING,
274 },
275 {
276 .name = "resourcecontrol",
277 .type = QEMU_OPT_STRING,
278 },
279 { /* end of list */ }
280 },
281};
282
283static void seccomp_register(void)
284{
Marc-André Lureau57807602018-08-30 16:33:48 +0200285 bool add = false;
286
287 /* FIXME: use seccomp_api_get() >= 2 check when released */
288
289#if defined(SECCOMP_FILTER_FLAG_TSYNC)
290 int check;
291
292 /* check host TSYNC capability, it returns errno == ENOSYS if unavailable */
293 check = qemu_seccomp(SECCOMP_SET_MODE_FILTER,
294 SECCOMP_FILTER_FLAG_TSYNC, NULL);
295 if (check < 0 && errno == EFAULT) {
296 add = true;
297 }
298#endif
299
300 if (add) {
301 qemu_add_opts(&qemu_sandbox_opts);
302 }
Yi Min Zhao9d0fdec2018-05-31 11:29:37 +0800303}
304opts_init(seccomp_register);
305#endif