1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include "qemu/osdep.h"
17#include "qapi/error.h"
18#include "qemu/config-file.h"
19#include "qemu/option.h"
20#include "qemu/module.h"
21#include <sys/prctl.h>
22#include <seccomp.h>
23#include "sysemu/seccomp.h"
24#include <linux/seccomp.h>
25
26
27
28
29
30#if SCMP_VER_MAJOR >= 3
31 #define HAVE_CACHEFLUSH
32#elif SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 2
33 #define HAVE_CACHEFLUSH
34#endif
35
36struct QemuSeccompSyscall {
37 int32_t num;
38 uint8_t set;
39 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)
45};
46
47static const struct QemuSeccompSyscall blacklist[] = {
48
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 },
71
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 },
83
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 },
94
95 { SCMP_SYS(fork), QEMU_SECCOMP_SET_SPAWN },
96 { SCMP_SYS(vfork), QEMU_SECCOMP_SET_SPAWN },
97 { SCMP_SYS(execve), QEMU_SECCOMP_SET_SPAWN },
98
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 },
103 { SCMP_SYS(sched_setscheduler), QEMU_SECCOMP_SET_RESOURCECTL,
104 ARRAY_SIZE(sched_setscheduler_arg), sched_setscheduler_arg },
105 { 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 },
110};
111
112static 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_action(int set)
124{
125 switch (set) {
126 case QEMU_SECCOMP_SET_DEFAULT:
127 case QEMU_SECCOMP_SET_OBSOLETE:
128 case QEMU_SECCOMP_SET_PRIVILEGED:
129 case QEMU_SECCOMP_SET_SPAWN: {
130#if defined(SECCOMP_GET_ACTION_AVAIL) && defined(SCMP_ACT_KILL_PROCESS) && \
131 defined(SECCOMP_RET_KILL_PROCESS)
132 static int kill_process = -1;
133 if (kill_process == -1) {
134 uint32_t action = SECCOMP_RET_KILL_PROCESS;
135
136 if (qemu_seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &action) == 0) {
137 kill_process = 1;
138 }
139 kill_process = 0;
140 }
141 if (kill_process == 1) {
142 return SCMP_ACT_KILL_PROCESS;
143 }
144#endif
145 return SCMP_ACT_TRAP;
146 }
147
148 case QEMU_SECCOMP_SET_RESOURCECTL:
149 return SCMP_ACT_ERRNO(EPERM);
150
151 default:
152 g_assert_not_reached();
153 }
154}
155
156
157static int seccomp_start(uint32_t seccomp_opts)
158{
159 int rc = 0;
160 unsigned int i = 0;
161 scmp_filter_ctx ctx;
162
163 ctx = seccomp_init(SCMP_ACT_ALLOW);
164 if (ctx == NULL) {
165 rc = -1;
166 goto seccomp_return;
167 }
168
169 rc = seccomp_attr_set(ctx, SCMP_FLTATR_CTL_TSYNC, 1);
170 if (rc != 0) {
171 goto seccomp_return;
172 }
173
174 for (i = 0; i < ARRAY_SIZE(blacklist); i++) {
175 uint32_t action;
176 if (!(seccomp_opts & blacklist[i].set)) {
177 continue;
178 }
179
180 action = qemu_seccomp_get_action(blacklist[i].set);
181 rc = seccomp_rule_add_array(ctx, action, blacklist[i].num,
182 blacklist[i].narg, blacklist[i].arg_cmp);
183 if (rc < 0) {
184 goto seccomp_return;
185 }
186 }
187
188 rc = seccomp_load(ctx);
189
190 seccomp_return:
191 seccomp_release(ctx);
192 return rc;
193}
194
195#ifdef CONFIG_SECCOMP
196int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
197{
198 if (qemu_opt_get_bool(opts, "enable", false)) {
199 uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
200 | QEMU_SECCOMP_SET_OBSOLETE;
201 const char *value = NULL;
202
203 value = qemu_opt_get(opts, "obsolete");
204 if (value) {
205 if (g_str_equal(value, "allow")) {
206 seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
207 } else if (g_str_equal(value, "deny")) {
208
209
210
211 } else {
212 error_setg(errp, "invalid argument for obsolete");
213 return -1;
214 }
215 }
216
217 value = qemu_opt_get(opts, "elevateprivileges");
218 if (value) {
219 if (g_str_equal(value, "deny")) {
220 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
221 } else if (g_str_equal(value, "children")) {
222 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
223
224
225
226 if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
227 error_setg(errp, "failed to set no_new_privs aborting");
228 return -1;
229 }
230 } else if (g_str_equal(value, "allow")) {
231
232 } else {
233 error_setg(errp, "invalid argument for elevateprivileges");
234 return -1;
235 }
236 }
237
238 value = qemu_opt_get(opts, "spawn");
239 if (value) {
240 if (g_str_equal(value, "deny")) {
241 seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
242 } else if (g_str_equal(value, "allow")) {
243
244 } else {
245 error_setg(errp, "invalid argument for spawn");
246 return -1;
247 }
248 }
249
250 value = qemu_opt_get(opts, "resourcecontrol");
251 if (value) {
252 if (g_str_equal(value, "deny")) {
253 seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
254 } else if (g_str_equal(value, "allow")) {
255
256 } else {
257 error_setg(errp, "invalid argument for resourcecontrol");
258 return -1;
259 }
260 }
261
262 if (seccomp_start(seccomp_opts) < 0) {
263 error_setg(errp, "failed to install seccomp syscall filter "
264 "in the kernel");
265 return -1;
266 }
267 }
268
269 return 0;
270}
271
272static QemuOptsList qemu_sandbox_opts = {
273 .name = "sandbox",
274 .implied_opt_name = "enable",
275 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
276 .desc = {
277 {
278 .name = "enable",
279 .type = QEMU_OPT_BOOL,
280 },
281 {
282 .name = "obsolete",
283 .type = QEMU_OPT_STRING,
284 },
285 {
286 .name = "elevateprivileges",
287 .type = QEMU_OPT_STRING,
288 },
289 {
290 .name = "spawn",
291 .type = QEMU_OPT_STRING,
292 },
293 {
294 .name = "resourcecontrol",
295 .type = QEMU_OPT_STRING,
296 },
297 { }
298 },
299};
300
301static void seccomp_register(void)
302{
303 bool add = false;
304
305
306
307#if defined(SECCOMP_FILTER_FLAG_TSYNC)
308 int check;
309
310
311 check = qemu_seccomp(SECCOMP_SET_MODE_FILTER,
312 SECCOMP_FILTER_FLAG_TSYNC, NULL);
313 if (check < 0 && errno == EFAULT) {
314 add = true;
315 }
316#endif
317
318 if (add) {
319 qemu_add_opts(&qemu_sandbox_opts);
320 }
321}
322opts_init(seccomp_register);
323#endif
324