]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-seccomp.c
configure: require libseccomp 2.2.0
[mirror_qemu.git] / qemu-seccomp.c
CommitLineData
2f668be7
EO
1/*
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 */
d38ea87a 15#include "qemu/osdep.h"
9d0fdecb
YMZ
16#include "qemu/config-file.h"
17#include "qemu/option.h"
18#include "qemu/module.h"
19#include "qemu/error-report.h"
20#include <sys/prctl.h>
2f668be7 21#include <seccomp.h>
9c17d615 22#include "sysemu/seccomp.h"
bda08a57 23#include <linux/seccomp.h>
2f668be7 24
81bed73b
JH
25/* For some architectures (notably ARM) cacheflush is not supported until
26 * libseccomp 2.2.3, but configure enforces that we are using a more recent
27 * version on those hosts, so it is OK for this check to be less strict.
28 */
47d2067a
AJ
29#if SCMP_VER_MAJOR >= 3
30 #define HAVE_CACHEFLUSH
81bed73b 31#elif SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 2
47d2067a
AJ
32 #define HAVE_CACHEFLUSH
33#endif
34
2f668be7
EO
35struct QemuSeccompSyscall {
36 int32_t num;
1bd6152a 37 uint8_t set;
056de1e8
MAL
38 uint8_t narg;
39 const struct scmp_arg_cmp *arg_cmp;
40};
41
42const struct scmp_arg_cmp sched_setscheduler_arg[] = {
43 SCMP_A1(SCMP_CMP_NE, SCHED_IDLE)
2f668be7
EO
44};
45
1bd6152a
EO
46static const struct QemuSeccompSyscall blacklist[] = {
47 /* default set of syscalls to blacklist */
48 { SCMP_SYS(reboot), QEMU_SECCOMP_SET_DEFAULT },
49 { SCMP_SYS(swapon), QEMU_SECCOMP_SET_DEFAULT },
50 { SCMP_SYS(swapoff), QEMU_SECCOMP_SET_DEFAULT },
51 { SCMP_SYS(syslog), QEMU_SECCOMP_SET_DEFAULT },
52 { SCMP_SYS(mount), QEMU_SECCOMP_SET_DEFAULT },
53 { SCMP_SYS(umount), QEMU_SECCOMP_SET_DEFAULT },
54 { SCMP_SYS(kexec_load), QEMU_SECCOMP_SET_DEFAULT },
55 { SCMP_SYS(afs_syscall), QEMU_SECCOMP_SET_DEFAULT },
56 { SCMP_SYS(break), QEMU_SECCOMP_SET_DEFAULT },
57 { SCMP_SYS(ftime), QEMU_SECCOMP_SET_DEFAULT },
58 { SCMP_SYS(getpmsg), QEMU_SECCOMP_SET_DEFAULT },
59 { SCMP_SYS(gtty), QEMU_SECCOMP_SET_DEFAULT },
60 { SCMP_SYS(lock), QEMU_SECCOMP_SET_DEFAULT },
61 { SCMP_SYS(mpx), QEMU_SECCOMP_SET_DEFAULT },
62 { SCMP_SYS(prof), QEMU_SECCOMP_SET_DEFAULT },
63 { SCMP_SYS(profil), QEMU_SECCOMP_SET_DEFAULT },
64 { SCMP_SYS(putpmsg), QEMU_SECCOMP_SET_DEFAULT },
65 { SCMP_SYS(security), QEMU_SECCOMP_SET_DEFAULT },
66 { SCMP_SYS(stty), QEMU_SECCOMP_SET_DEFAULT },
67 { SCMP_SYS(tuxcall), QEMU_SECCOMP_SET_DEFAULT },
68 { SCMP_SYS(ulimit), QEMU_SECCOMP_SET_DEFAULT },
69 { SCMP_SYS(vserver), QEMU_SECCOMP_SET_DEFAULT },
2b716fa6
EO
70 /* obsolete */
71 { SCMP_SYS(readdir), QEMU_SECCOMP_SET_OBSOLETE },
72 { SCMP_SYS(_sysctl), QEMU_SECCOMP_SET_OBSOLETE },
73 { SCMP_SYS(bdflush), QEMU_SECCOMP_SET_OBSOLETE },
74 { SCMP_SYS(create_module), QEMU_SECCOMP_SET_OBSOLETE },
75 { SCMP_SYS(get_kernel_syms), QEMU_SECCOMP_SET_OBSOLETE },
76 { SCMP_SYS(query_module), QEMU_SECCOMP_SET_OBSOLETE },
77 { SCMP_SYS(sgetmask), QEMU_SECCOMP_SET_OBSOLETE },
78 { SCMP_SYS(ssetmask), QEMU_SECCOMP_SET_OBSOLETE },
79 { SCMP_SYS(sysfs), QEMU_SECCOMP_SET_OBSOLETE },
80 { SCMP_SYS(uselib), QEMU_SECCOMP_SET_OBSOLETE },
81 { SCMP_SYS(ustat), QEMU_SECCOMP_SET_OBSOLETE },
73a1e647
EO
82 /* privileged */
83 { SCMP_SYS(setuid), QEMU_SECCOMP_SET_PRIVILEGED },
84 { SCMP_SYS(setgid), QEMU_SECCOMP_SET_PRIVILEGED },
85 { SCMP_SYS(setpgid), QEMU_SECCOMP_SET_PRIVILEGED },
86 { SCMP_SYS(setsid), QEMU_SECCOMP_SET_PRIVILEGED },
87 { SCMP_SYS(setreuid), QEMU_SECCOMP_SET_PRIVILEGED },
88 { SCMP_SYS(setregid), QEMU_SECCOMP_SET_PRIVILEGED },
89 { SCMP_SYS(setresuid), QEMU_SECCOMP_SET_PRIVILEGED },
90 { SCMP_SYS(setresgid), QEMU_SECCOMP_SET_PRIVILEGED },
91 { SCMP_SYS(setfsuid), QEMU_SECCOMP_SET_PRIVILEGED },
92 { SCMP_SYS(setfsgid), QEMU_SECCOMP_SET_PRIVILEGED },
995a226f
EO
93 /* spawn */
94 { SCMP_SYS(fork), QEMU_SECCOMP_SET_SPAWN },
95 { SCMP_SYS(vfork), QEMU_SECCOMP_SET_SPAWN },
96 { SCMP_SYS(execve), QEMU_SECCOMP_SET_SPAWN },
24f8cdc5
EO
97 /* resource control */
98 { SCMP_SYS(getpriority), QEMU_SECCOMP_SET_RESOURCECTL },
99 { SCMP_SYS(setpriority), QEMU_SECCOMP_SET_RESOURCECTL },
100 { SCMP_SYS(sched_setparam), QEMU_SECCOMP_SET_RESOURCECTL },
101 { SCMP_SYS(sched_getparam), QEMU_SECCOMP_SET_RESOURCECTL },
056de1e8
MAL
102 { SCMP_SYS(sched_setscheduler), QEMU_SECCOMP_SET_RESOURCECTL,
103 ARRAY_SIZE(sched_setscheduler_arg), sched_setscheduler_arg },
24f8cdc5
EO
104 { SCMP_SYS(sched_getscheduler), QEMU_SECCOMP_SET_RESOURCECTL },
105 { SCMP_SYS(sched_setaffinity), QEMU_SECCOMP_SET_RESOURCECTL },
106 { SCMP_SYS(sched_getaffinity), QEMU_SECCOMP_SET_RESOURCECTL },
107 { SCMP_SYS(sched_get_priority_max), QEMU_SECCOMP_SET_RESOURCECTL },
108 { SCMP_SYS(sched_get_priority_min), QEMU_SECCOMP_SET_RESOURCECTL },
2f668be7
EO
109};
110
bda08a57
MAL
111static inline __attribute__((unused)) int
112qemu_seccomp(unsigned int operation, unsigned int flags, void *args)
113{
114#ifdef __NR_seccomp
115 return syscall(__NR_seccomp, operation, flags, args);
116#else
117 errno = ENOSYS;
118 return -1;
119#endif
120}
121
122static uint32_t qemu_seccomp_get_kill_action(void)
123{
124#if defined(SECCOMP_GET_ACTION_AVAIL) && defined(SCMP_ACT_KILL_PROCESS) && \
125 defined(SECCOMP_RET_KILL_PROCESS)
126 {
127 uint32_t action = SECCOMP_RET_KILL_PROCESS;
128
129 if (qemu_seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &action) == 0) {
130 return SCMP_ACT_KILL_PROCESS;
131 }
132 }
133#endif
134
135 return SCMP_ACT_TRAP;
136}
137
2b716fa6 138
9d0fdecb 139static int seccomp_start(uint32_t seccomp_opts)
2f668be7
EO
140{
141 int rc = 0;
142 unsigned int i = 0;
143 scmp_filter_ctx ctx;
bda08a57 144 uint32_t action = qemu_seccomp_get_kill_action();
2f668be7 145
1bd6152a 146 ctx = seccomp_init(SCMP_ACT_ALLOW);
2f668be7 147 if (ctx == NULL) {
2a13f991 148 rc = -1;
2f668be7
EO
149 goto seccomp_return;
150 }
151
1bd6152a 152 for (i = 0; i < ARRAY_SIZE(blacklist); i++) {
2b716fa6
EO
153 if (!(seccomp_opts & blacklist[i].set)) {
154 continue;
155 }
156
bda08a57 157 rc = seccomp_rule_add_array(ctx, action, blacklist[i].num,
056de1e8 158 blacklist[i].narg, blacklist[i].arg_cmp);
2f668be7
EO
159 if (rc < 0) {
160 goto seccomp_return;
161 }
162 }
163
164 rc = seccomp_load(ctx);
165
166 seccomp_return:
167 seccomp_release(ctx);
168 return rc;
169}
9d0fdecb
YMZ
170
171#ifdef CONFIG_SECCOMP
172int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
173{
174 if (qemu_opt_get_bool(opts, "enable", false)) {
175 uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
176 | QEMU_SECCOMP_SET_OBSOLETE;
177 const char *value = NULL;
178
179 value = qemu_opt_get(opts, "obsolete");
180 if (value) {
181 if (g_str_equal(value, "allow")) {
182 seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
183 } else if (g_str_equal(value, "deny")) {
184 /* this is the default option, this if is here
185 * to provide a little bit of consistency for
186 * the command line */
187 } else {
188 error_report("invalid argument for obsolete");
189 return -1;
190 }
191 }
192
193 value = qemu_opt_get(opts, "elevateprivileges");
194 if (value) {
195 if (g_str_equal(value, "deny")) {
196 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
197 } else if (g_str_equal(value, "children")) {
198 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
199
200 /* calling prctl directly because we're
201 * not sure if host has CAP_SYS_ADMIN set*/
202 if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
203 error_report("failed to set no_new_privs "
204 "aborting");
205 return -1;
206 }
207 } else if (g_str_equal(value, "allow")) {
208 /* default value */
209 } else {
210 error_report("invalid argument for elevateprivileges");
211 return -1;
212 }
213 }
214
215 value = qemu_opt_get(opts, "spawn");
216 if (value) {
217 if (g_str_equal(value, "deny")) {
218 seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
219 } else if (g_str_equal(value, "allow")) {
220 /* default value */
221 } else {
222 error_report("invalid argument for spawn");
223 return -1;
224 }
225 }
226
227 value = qemu_opt_get(opts, "resourcecontrol");
228 if (value) {
229 if (g_str_equal(value, "deny")) {
230 seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
231 } else if (g_str_equal(value, "allow")) {
232 /* default value */
233 } else {
234 error_report("invalid argument for resourcecontrol");
235 return -1;
236 }
237 }
238
239 if (seccomp_start(seccomp_opts) < 0) {
240 error_report("failed to install seccomp syscall filter "
241 "in the kernel");
242 return -1;
243 }
244 }
245
246 return 0;
247}
248
249static QemuOptsList qemu_sandbox_opts = {
250 .name = "sandbox",
251 .implied_opt_name = "enable",
252 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
253 .desc = {
254 {
255 .name = "enable",
256 .type = QEMU_OPT_BOOL,
257 },
258 {
259 .name = "obsolete",
260 .type = QEMU_OPT_STRING,
261 },
262 {
263 .name = "elevateprivileges",
264 .type = QEMU_OPT_STRING,
265 },
266 {
267 .name = "spawn",
268 .type = QEMU_OPT_STRING,
269 },
270 {
271 .name = "resourcecontrol",
272 .type = QEMU_OPT_STRING,
273 },
274 { /* end of list */ }
275 },
276};
277
278static void seccomp_register(void)
279{
280 qemu_add_opts(&qemu_sandbox_opts);
281}
282opts_init(seccomp_register);
283#endif