2 * QEMU seccomp test suite
4 * Copyright (c) 2021 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/config-file.h"
23 #include "qemu/option.h"
24 #include "sysemu/seccomp.h"
25 #include "qapi/error.h"
26 #include "qemu/module.h"
29 #include <sys/syscall.h>
31 static void test_seccomp_helper(const char *args
, bool killed
,
32 int errnum
, int (*doit
)(void))
34 if (g_test_subprocess()) {
39 module_call_init(MODULE_INIT_OPTS
);
40 olist
= qemu_find_opts("sandbox");
41 g_assert(olist
!= NULL
);
43 opts
= qemu_opts_parse_noisily(olist
, args
, true);
44 g_assert(opts
!= NULL
);
46 parse_sandbox(NULL
, opts
, &error_abort
);
48 /* Running in a child process */
53 g_assert(errno
== errnum
);
60 /* Running in main test process, spawning the child */
61 g_test_trap_subprocess(NULL
, 0, 0);
63 g_test_trap_assert_failed();
65 g_test_trap_assert_passed();
71 static void test_seccomp_killed(const char *args
, int (*doit
)(void))
73 test_seccomp_helper(args
, true, 0, doit
);
76 static void test_seccomp_errno(const char *args
, int errnum
, int (*doit
)(void))
78 test_seccomp_helper(args
, false, errnum
, doit
);
81 static void test_seccomp_passed(const char *args
, int (*doit
)(void))
83 test_seccomp_helper(args
, false, 0, doit
);
87 static int doit_sys_fork(void)
89 int ret
= syscall(SYS_fork
);
99 static void test_seccomp_sys_fork_on_nospawn(void)
101 test_seccomp_killed("on,spawn=deny", doit_sys_fork
);
104 static void test_seccomp_sys_fork_on(void)
106 test_seccomp_passed("on", doit_sys_fork
);
109 static void test_seccomp_sys_fork_off(void)
111 test_seccomp_passed("off", doit_sys_fork
);
115 static int doit_fork(void)
127 static void test_seccomp_fork_on_nospawn(void)
129 /* XXX fixme - should be killed */
130 test_seccomp_passed("on,spawn=deny", doit_fork
);
133 static void test_seccomp_fork_on(void)
135 test_seccomp_passed("on", doit_fork
);
138 static void test_seccomp_fork_off(void)
140 test_seccomp_passed("off", doit_fork
);
143 static void *noop(void *arg
)
148 static int doit_thread(void)
151 int ret
= pthread_create(&th
, NULL
, noop
, NULL
);
156 pthread_join(th
, NULL
);
161 static void test_seccomp_thread_on(void)
163 test_seccomp_passed("on", doit_thread
);
166 static void test_seccomp_thread_on_nospawn(void)
168 test_seccomp_passed("on,spawn=deny", doit_thread
);
171 static void test_seccomp_thread_off(void)
173 test_seccomp_passed("off", doit_thread
);
176 static int doit_sched(void)
178 struct sched_param param
= { .sched_priority
= 0 };
179 return sched_setscheduler(getpid(), SCHED_OTHER
, ¶m
);
182 static void test_seccomp_sched_on_nores(void)
184 test_seccomp_errno("on,resourcecontrol=deny", EPERM
, doit_sched
);
187 static void test_seccomp_sched_on(void)
189 test_seccomp_passed("on", doit_sched
);
192 static void test_seccomp_sched_off(void)
194 test_seccomp_passed("off", doit_sched
);
197 static bool can_play_with_seccomp(void)
199 g_autofree
char *status
= NULL
;
200 g_auto(GStrv
) lines
= NULL
;
203 if (!g_file_get_contents("/proc/self/status", &status
, NULL
, NULL
)) {
207 lines
= g_strsplit(status
, "\n", 0);
209 for (i
= 0; lines
[i
] != NULL
; i
++) {
210 if (g_str_has_prefix(lines
[i
], "Seccomp:")) {
212 * "Seccomp: 1" or "Seccomp: 2" indicate we're already
213 * confined, probably as we're inside a container. In
214 * this case our tests might get unexpected results,
215 * so we can't run reliably
217 if (!strchr(lines
[i
], '0')) {
225 /* Doesn't look like seccomp is enabled in the kernel */
229 int main(int argc
, char **argv
)
231 g_test_init(&argc
, &argv
, NULL
);
232 if (can_play_with_seccomp()) {
234 g_test_add_func("/softmmu/seccomp/sys-fork/on",
235 test_seccomp_sys_fork_on
);
236 g_test_add_func("/softmmu/seccomp/sys-fork/on-nospawn",
237 test_seccomp_sys_fork_on_nospawn
);
238 g_test_add_func("/softmmu/seccomp/sys-fork/off",
239 test_seccomp_sys_fork_off
);
242 g_test_add_func("/softmmu/seccomp/fork/on",
243 test_seccomp_fork_on
);
244 g_test_add_func("/softmmu/seccomp/fork/on-nospawn",
245 test_seccomp_fork_on_nospawn
);
246 g_test_add_func("/softmmu/seccomp/fork/off",
247 test_seccomp_fork_off
);
249 g_test_add_func("/softmmu/seccomp/thread/on",
250 test_seccomp_thread_on
);
251 g_test_add_func("/softmmu/seccomp/thread/on-nospawn",
252 test_seccomp_thread_on_nospawn
);
253 g_test_add_func("/softmmu/seccomp/thread/off",
254 test_seccomp_thread_off
);
256 if (doit_sched() == 0) {
258 * musl doesn't impl sched_setscheduler, hence
259 * we check above if it works first
261 g_test_add_func("/softmmu/seccomp/sched/on",
262 test_seccomp_sched_on
);
263 g_test_add_func("/softmmu/seccomp/sched/on-nores",
264 test_seccomp_sched_on_nores
);
265 g_test_add_func("/softmmu/seccomp/sched/off",
266 test_seccomp_sched_off
);