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 test_seccomp_killed("on,spawn=deny", doit_fork
);
132 static void test_seccomp_fork_on(void)
134 test_seccomp_passed("on", doit_fork
);
137 static void test_seccomp_fork_off(void)
139 test_seccomp_passed("off", doit_fork
);
142 static void *noop(void *arg
)
147 static int doit_thread(void)
150 int ret
= pthread_create(&th
, NULL
, noop
, NULL
);
155 pthread_join(th
, NULL
);
160 static void test_seccomp_thread_on(void)
162 test_seccomp_passed("on", doit_thread
);
165 static void test_seccomp_thread_on_nospawn(void)
167 test_seccomp_passed("on,spawn=deny", doit_thread
);
170 static void test_seccomp_thread_off(void)
172 test_seccomp_passed("off", doit_thread
);
175 static int doit_sched(void)
177 struct sched_param param
= { .sched_priority
= 0 };
178 return sched_setscheduler(getpid(), SCHED_OTHER
, ¶m
);
181 static void test_seccomp_sched_on_nores(void)
183 test_seccomp_errno("on,resourcecontrol=deny", EPERM
, doit_sched
);
186 static void test_seccomp_sched_on(void)
188 test_seccomp_passed("on", doit_sched
);
191 static void test_seccomp_sched_off(void)
193 test_seccomp_passed("off", doit_sched
);
196 static bool can_play_with_seccomp(void)
198 g_autofree
char *status
= NULL
;
199 g_auto(GStrv
) lines
= NULL
;
202 if (!g_file_get_contents("/proc/self/status", &status
, NULL
, NULL
)) {
206 lines
= g_strsplit(status
, "\n", 0);
208 for (i
= 0; lines
[i
] != NULL
; i
++) {
209 if (g_str_has_prefix(lines
[i
], "Seccomp:")) {
211 * "Seccomp: 1" or "Seccomp: 2" indicate we're already
212 * confined, probably as we're inside a container. In
213 * this case our tests might get unexpected results,
214 * so we can't run reliably
216 if (!strchr(lines
[i
], '0')) {
224 /* Doesn't look like seccomp is enabled in the kernel */
228 int main(int argc
, char **argv
)
230 g_test_init(&argc
, &argv
, NULL
);
231 if (can_play_with_seccomp()) {
233 g_test_add_func("/softmmu/seccomp/sys-fork/on",
234 test_seccomp_sys_fork_on
);
235 g_test_add_func("/softmmu/seccomp/sys-fork/on-nospawn",
236 test_seccomp_sys_fork_on_nospawn
);
237 g_test_add_func("/softmmu/seccomp/sys-fork/off",
238 test_seccomp_sys_fork_off
);
241 g_test_add_func("/softmmu/seccomp/fork/on",
242 test_seccomp_fork_on
);
243 g_test_add_func("/softmmu/seccomp/fork/on-nospawn",
244 test_seccomp_fork_on_nospawn
);
245 g_test_add_func("/softmmu/seccomp/fork/off",
246 test_seccomp_fork_off
);
248 g_test_add_func("/softmmu/seccomp/thread/on",
249 test_seccomp_thread_on
);
250 g_test_add_func("/softmmu/seccomp/thread/on-nospawn",
251 test_seccomp_thread_on_nospawn
);
252 g_test_add_func("/softmmu/seccomp/thread/off",
253 test_seccomp_thread_off
);
255 if (doit_sched() == 0) {
257 * musl doesn't impl sched_setscheduler, hence
258 * we check above if it works first
260 g_test_add_func("/softmmu/seccomp/sched/on",
261 test_seccomp_sched_on
);
262 g_test_add_func("/softmmu/seccomp/sched/on-nores",
263 test_seccomp_sched_on_nores
);
264 g_test_add_func("/softmmu/seccomp/sched/off",
265 test_seccomp_sched_off
);