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"
28 #include <sys/syscall.h>
30 static void test_seccomp_helper(const char *args
, bool killed
,
31 int errnum
, int (*doit
)(void))
33 if (g_test_subprocess()) {
38 module_call_init(MODULE_INIT_OPTS
);
39 olist
= qemu_find_opts("sandbox");
40 g_assert(olist
!= NULL
);
42 opts
= qemu_opts_parse_noisily(olist
, args
, true);
43 g_assert(opts
!= NULL
);
45 parse_sandbox(NULL
, opts
, &error_abort
);
47 /* Running in a child process */
52 g_assert(errno
== errnum
);
59 /* Running in main test process, spawning the child */
60 g_test_trap_subprocess(NULL
, 0, 0);
62 g_test_trap_assert_failed();
64 g_test_trap_assert_passed();
70 static void test_seccomp_killed(const char *args
, int (*doit
)(void))
72 test_seccomp_helper(args
, true, 0, doit
);
75 static void test_seccomp_errno(const char *args
, int errnum
, int (*doit
)(void))
77 test_seccomp_helper(args
, false, errnum
, doit
);
80 static void test_seccomp_passed(const char *args
, int (*doit
)(void))
82 test_seccomp_helper(args
, false, 0, doit
);
86 static int doit_sys_fork(void)
88 int ret
= syscall(SYS_fork
);
98 static void test_seccomp_sys_fork_on_nospawn(void)
100 test_seccomp_killed("on,spawn=deny", doit_sys_fork
);
103 static void test_seccomp_sys_fork_on(void)
105 test_seccomp_passed("on", doit_sys_fork
);
108 static void test_seccomp_sys_fork_off(void)
110 test_seccomp_passed("off", doit_sys_fork
);
114 static int doit_fork(void)
126 static void test_seccomp_fork_on_nospawn(void)
128 test_seccomp_killed("on,spawn=deny", doit_fork
);
131 static void test_seccomp_fork_on(void)
133 test_seccomp_passed("on", doit_fork
);
136 static void test_seccomp_fork_off(void)
138 test_seccomp_passed("off", doit_fork
);
141 static void *noop(void *arg
)
146 static int doit_thread(void)
149 int ret
= pthread_create(&th
, NULL
, noop
, NULL
);
154 pthread_join(th
, NULL
);
159 static void test_seccomp_thread_on(void)
161 test_seccomp_passed("on", doit_thread
);
164 static void test_seccomp_thread_on_nospawn(void)
166 test_seccomp_passed("on,spawn=deny", doit_thread
);
169 static void test_seccomp_thread_off(void)
171 test_seccomp_passed("off", doit_thread
);
174 static int doit_sched(void)
176 struct sched_param param
= { .sched_priority
= 0 };
177 return sched_setscheduler(getpid(), SCHED_OTHER
, ¶m
);
180 static void test_seccomp_sched_on_nores(void)
182 test_seccomp_errno("on,resourcecontrol=deny", EPERM
, doit_sched
);
185 static void test_seccomp_sched_on(void)
187 test_seccomp_passed("on", doit_sched
);
190 static void test_seccomp_sched_off(void)
192 test_seccomp_passed("off", doit_sched
);
195 static bool can_play_with_seccomp(void)
197 g_autofree
char *status
= NULL
;
198 g_auto(GStrv
) lines
= NULL
;
201 if (!g_file_get_contents("/proc/self/status", &status
, NULL
, NULL
)) {
205 lines
= g_strsplit(status
, "\n", 0);
207 for (i
= 0; lines
[i
] != NULL
; i
++) {
208 if (g_str_has_prefix(lines
[i
], "Seccomp:")) {
210 * "Seccomp: 1" or "Seccomp: 2" indicate we're already
211 * confined, probably as we're inside a container. In
212 * this case our tests might get unexpected results,
213 * so we can't run reliably
215 if (!strchr(lines
[i
], '0')) {
223 /* Doesn't look like seccomp is enabled in the kernel */
227 int main(int argc
, char **argv
)
229 g_test_init(&argc
, &argv
, NULL
);
230 if (can_play_with_seccomp()) {
232 g_test_add_func("/seccomp/sys-fork/on",
233 test_seccomp_sys_fork_on
);
234 g_test_add_func("/seccomp/sys-fork/on-nospawn",
235 test_seccomp_sys_fork_on_nospawn
);
236 g_test_add_func("/seccomp/sys-fork/off",
237 test_seccomp_sys_fork_off
);
240 g_test_add_func("/seccomp/fork/on",
241 test_seccomp_fork_on
);
242 g_test_add_func("/seccomp/fork/on-nospawn",
243 test_seccomp_fork_on_nospawn
);
244 g_test_add_func("/seccomp/fork/off",
245 test_seccomp_fork_off
);
247 g_test_add_func("/seccomp/thread/on",
248 test_seccomp_thread_on
);
249 g_test_add_func("/seccomp/thread/on-nospawn",
250 test_seccomp_thread_on_nospawn
);
251 g_test_add_func("/seccomp/thread/off",
252 test_seccomp_thread_off
);
254 if (doit_sched() == 0) {
256 * musl doesn't impl sched_setscheduler, hence
257 * we check above if it works first
259 g_test_add_func("/seccomp/sched/on",
260 test_seccomp_sched_on
);
261 g_test_add_func("/seccomp/sched/on-nores",
262 test_seccomp_sched_on_nores
);
263 g_test_add_func("/seccomp/sched/off",
264 test_seccomp_sched_off
);