2 * QEMU seccomp mode 2 support with libseccomp
4 * Copyright IBM, Corp. 2012
7 * Eduardo Otubo <eotubo@br.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
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>
23 #include "sysemu/seccomp.h"
24 #include <linux/seccomp.h>
26 /* 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.
30 #if SCMP_VER_MAJOR >= 3
31 #define HAVE_CACHEFLUSH
32 #elif SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 2
33 #define HAVE_CACHEFLUSH
36 struct QemuSeccompSyscall
{
40 const struct scmp_arg_cmp
*arg_cmp
;
43 const struct scmp_arg_cmp sched_setscheduler_arg
[] = {
44 /* was SCMP_A1(SCMP_CMP_NE, SCHED_IDLE), but expanded due to GCC 4.x bug */
45 { .arg
= 1, .op
= SCMP_CMP_NE
, .datum_a
= SCHED_IDLE
}
48 static const struct QemuSeccompSyscall blacklist
[] = {
49 /* default set of syscalls to blacklist */
50 { SCMP_SYS(reboot
), QEMU_SECCOMP_SET_DEFAULT
},
51 { SCMP_SYS(swapon
), QEMU_SECCOMP_SET_DEFAULT
},
52 { SCMP_SYS(swapoff
), QEMU_SECCOMP_SET_DEFAULT
},
53 { SCMP_SYS(syslog
), QEMU_SECCOMP_SET_DEFAULT
},
54 { SCMP_SYS(mount
), QEMU_SECCOMP_SET_DEFAULT
},
55 { SCMP_SYS(umount
), QEMU_SECCOMP_SET_DEFAULT
},
56 { SCMP_SYS(kexec_load
), QEMU_SECCOMP_SET_DEFAULT
},
57 { SCMP_SYS(afs_syscall
), QEMU_SECCOMP_SET_DEFAULT
},
58 { SCMP_SYS(break), QEMU_SECCOMP_SET_DEFAULT
},
59 { SCMP_SYS(ftime
), QEMU_SECCOMP_SET_DEFAULT
},
60 { SCMP_SYS(getpmsg
), QEMU_SECCOMP_SET_DEFAULT
},
61 { SCMP_SYS(gtty
), QEMU_SECCOMP_SET_DEFAULT
},
62 { SCMP_SYS(lock
), QEMU_SECCOMP_SET_DEFAULT
},
63 { SCMP_SYS(mpx
), QEMU_SECCOMP_SET_DEFAULT
},
64 { SCMP_SYS(prof
), QEMU_SECCOMP_SET_DEFAULT
},
65 { SCMP_SYS(profil
), QEMU_SECCOMP_SET_DEFAULT
},
66 { SCMP_SYS(putpmsg
), QEMU_SECCOMP_SET_DEFAULT
},
67 { SCMP_SYS(security
), QEMU_SECCOMP_SET_DEFAULT
},
68 { SCMP_SYS(stty
), QEMU_SECCOMP_SET_DEFAULT
},
69 { SCMP_SYS(tuxcall
), QEMU_SECCOMP_SET_DEFAULT
},
70 { SCMP_SYS(ulimit
), QEMU_SECCOMP_SET_DEFAULT
},
71 { SCMP_SYS(vserver
), QEMU_SECCOMP_SET_DEFAULT
},
73 { SCMP_SYS(readdir
), QEMU_SECCOMP_SET_OBSOLETE
},
74 { SCMP_SYS(_sysctl
), QEMU_SECCOMP_SET_OBSOLETE
},
75 { SCMP_SYS(bdflush
), QEMU_SECCOMP_SET_OBSOLETE
},
76 { SCMP_SYS(create_module
), QEMU_SECCOMP_SET_OBSOLETE
},
77 { SCMP_SYS(get_kernel_syms
), QEMU_SECCOMP_SET_OBSOLETE
},
78 { SCMP_SYS(query_module
), QEMU_SECCOMP_SET_OBSOLETE
},
79 { SCMP_SYS(sgetmask
), QEMU_SECCOMP_SET_OBSOLETE
},
80 { SCMP_SYS(ssetmask
), QEMU_SECCOMP_SET_OBSOLETE
},
81 { SCMP_SYS(sysfs
), QEMU_SECCOMP_SET_OBSOLETE
},
82 { SCMP_SYS(uselib
), QEMU_SECCOMP_SET_OBSOLETE
},
83 { SCMP_SYS(ustat
), QEMU_SECCOMP_SET_OBSOLETE
},
85 { SCMP_SYS(setuid
), QEMU_SECCOMP_SET_PRIVILEGED
},
86 { SCMP_SYS(setgid
), QEMU_SECCOMP_SET_PRIVILEGED
},
87 { SCMP_SYS(setpgid
), QEMU_SECCOMP_SET_PRIVILEGED
},
88 { SCMP_SYS(setsid
), QEMU_SECCOMP_SET_PRIVILEGED
},
89 { SCMP_SYS(setreuid
), QEMU_SECCOMP_SET_PRIVILEGED
},
90 { SCMP_SYS(setregid
), QEMU_SECCOMP_SET_PRIVILEGED
},
91 { SCMP_SYS(setresuid
), QEMU_SECCOMP_SET_PRIVILEGED
},
92 { SCMP_SYS(setresgid
), QEMU_SECCOMP_SET_PRIVILEGED
},
93 { SCMP_SYS(setfsuid
), QEMU_SECCOMP_SET_PRIVILEGED
},
94 { SCMP_SYS(setfsgid
), QEMU_SECCOMP_SET_PRIVILEGED
},
96 { SCMP_SYS(fork
), QEMU_SECCOMP_SET_SPAWN
},
97 { SCMP_SYS(vfork
), QEMU_SECCOMP_SET_SPAWN
},
98 { SCMP_SYS(execve
), QEMU_SECCOMP_SET_SPAWN
},
99 /* resource control */
100 { SCMP_SYS(getpriority
), QEMU_SECCOMP_SET_RESOURCECTL
},
101 { SCMP_SYS(setpriority
), QEMU_SECCOMP_SET_RESOURCECTL
},
102 { SCMP_SYS(sched_setparam
), QEMU_SECCOMP_SET_RESOURCECTL
},
103 { SCMP_SYS(sched_getparam
), QEMU_SECCOMP_SET_RESOURCECTL
},
104 { SCMP_SYS(sched_setscheduler
), QEMU_SECCOMP_SET_RESOURCECTL
,
105 ARRAY_SIZE(sched_setscheduler_arg
), sched_setscheduler_arg
},
106 { SCMP_SYS(sched_getscheduler
), QEMU_SECCOMP_SET_RESOURCECTL
},
107 { SCMP_SYS(sched_setaffinity
), QEMU_SECCOMP_SET_RESOURCECTL
},
108 { SCMP_SYS(sched_getaffinity
), QEMU_SECCOMP_SET_RESOURCECTL
},
109 { SCMP_SYS(sched_get_priority_max
), QEMU_SECCOMP_SET_RESOURCECTL
},
110 { SCMP_SYS(sched_get_priority_min
), QEMU_SECCOMP_SET_RESOURCECTL
},
113 static inline __attribute__((unused
)) int
114 qemu_seccomp(unsigned int operation
, unsigned int flags
, void *args
)
117 return syscall(__NR_seccomp
, operation
, flags
, args
);
124 static uint32_t qemu_seccomp_get_kill_action(void)
126 #if defined(SECCOMP_GET_ACTION_AVAIL) && defined(SCMP_ACT_KILL_PROCESS) && \
127 defined(SECCOMP_RET_KILL_PROCESS)
129 uint32_t action
= SECCOMP_RET_KILL_PROCESS
;
131 if (qemu_seccomp(SECCOMP_GET_ACTION_AVAIL
, 0, &action
) == 0) {
132 return SCMP_ACT_KILL_PROCESS
;
137 return SCMP_ACT_TRAP
;
141 static int seccomp_start(uint32_t seccomp_opts
)
146 uint32_t action
= qemu_seccomp_get_kill_action();
148 ctx
= seccomp_init(SCMP_ACT_ALLOW
);
154 rc
= seccomp_attr_set(ctx
, SCMP_FLTATR_CTL_TSYNC
, 1);
159 for (i
= 0; i
< ARRAY_SIZE(blacklist
); i
++) {
160 if (!(seccomp_opts
& blacklist
[i
].set
)) {
164 rc
= seccomp_rule_add_array(ctx
, action
, blacklist
[i
].num
,
165 blacklist
[i
].narg
, blacklist
[i
].arg_cmp
);
171 rc
= seccomp_load(ctx
);
174 seccomp_release(ctx
);
178 #ifdef CONFIG_SECCOMP
179 int parse_sandbox(void *opaque
, QemuOpts
*opts
, Error
**errp
)
181 if (qemu_opt_get_bool(opts
, "enable", false)) {
182 uint32_t seccomp_opts
= QEMU_SECCOMP_SET_DEFAULT
183 | QEMU_SECCOMP_SET_OBSOLETE
;
184 const char *value
= NULL
;
186 value
= qemu_opt_get(opts
, "obsolete");
188 if (g_str_equal(value
, "allow")) {
189 seccomp_opts
&= ~QEMU_SECCOMP_SET_OBSOLETE
;
190 } else if (g_str_equal(value
, "deny")) {
191 /* this is the default option, this if is here
192 * to provide a little bit of consistency for
193 * the command line */
195 error_setg(errp
, "invalid argument for obsolete");
200 value
= qemu_opt_get(opts
, "elevateprivileges");
202 if (g_str_equal(value
, "deny")) {
203 seccomp_opts
|= QEMU_SECCOMP_SET_PRIVILEGED
;
204 } else if (g_str_equal(value
, "children")) {
205 seccomp_opts
|= QEMU_SECCOMP_SET_PRIVILEGED
;
207 /* calling prctl directly because we're
208 * not sure if host has CAP_SYS_ADMIN set*/
209 if (prctl(PR_SET_NO_NEW_PRIVS
, 1)) {
210 error_setg(errp
, "failed to set no_new_privs aborting");
213 } else if (g_str_equal(value
, "allow")) {
216 error_setg(errp
, "invalid argument for elevateprivileges");
221 value
= qemu_opt_get(opts
, "spawn");
223 if (g_str_equal(value
, "deny")) {
224 seccomp_opts
|= QEMU_SECCOMP_SET_SPAWN
;
225 } else if (g_str_equal(value
, "allow")) {
228 error_setg(errp
, "invalid argument for spawn");
233 value
= qemu_opt_get(opts
, "resourcecontrol");
235 if (g_str_equal(value
, "deny")) {
236 seccomp_opts
|= QEMU_SECCOMP_SET_RESOURCECTL
;
237 } else if (g_str_equal(value
, "allow")) {
240 error_setg(errp
, "invalid argument for resourcecontrol");
245 if (seccomp_start(seccomp_opts
) < 0) {
246 error_setg(errp
, "failed to install seccomp syscall filter "
255 static QemuOptsList qemu_sandbox_opts
= {
257 .implied_opt_name
= "enable",
258 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
262 .type
= QEMU_OPT_BOOL
,
266 .type
= QEMU_OPT_STRING
,
269 .name
= "elevateprivileges",
270 .type
= QEMU_OPT_STRING
,
274 .type
= QEMU_OPT_STRING
,
277 .name
= "resourcecontrol",
278 .type
= QEMU_OPT_STRING
,
280 { /* end of list */ }
284 static void seccomp_register(void)
288 /* FIXME: use seccomp_api_get() >= 2 check when released */
290 #if defined(SECCOMP_FILTER_FLAG_TSYNC)
293 /* check host TSYNC capability, it returns errno == ENOSYS if unavailable */
294 check
= qemu_seccomp(SECCOMP_SET_MODE_FILTER
,
295 SECCOMP_FILTER_FLAG_TSYNC
, NULL
);
296 if (check
< 0 && errno
== EFAULT
) {
302 qemu_add_opts(&qemu_sandbox_opts
);
305 opts_init(seccomp_register
);