]> git.proxmox.com Git - mirror_qemu.git/blob - tests/unit/test-seccomp.c
seccomp: add unit test for seccomp filtering
[mirror_qemu.git] / tests / unit / test-seccomp.c
1 /*
2 * QEMU seccomp test suite
3 *
4 * Copyright (c) 2021 Red Hat, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
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"
27
28 #include <unistd.h>
29 #include <sys/syscall.h>
30
31 static void test_seccomp_helper(const char *args, bool killed,
32 int errnum, int (*doit)(void))
33 {
34 if (g_test_subprocess()) {
35 QemuOptsList *olist;
36 QemuOpts *opts;
37 int ret;
38
39 module_call_init(MODULE_INIT_OPTS);
40 olist = qemu_find_opts("sandbox");
41 g_assert(olist != NULL);
42
43 opts = qemu_opts_parse_noisily(olist, args, true);
44 g_assert(opts != NULL);
45
46 parse_sandbox(NULL, opts, &error_abort);
47
48 /* Running in a child process */
49 ret = doit();
50
51 if (errnum != 0) {
52 g_assert(ret != 0);
53 g_assert(errno == errnum);
54 } else {
55 g_assert(ret == 0);
56 }
57
58 _exit(0);
59 } else {
60 /* Running in main test process, spawning the child */
61 g_test_trap_subprocess(NULL, 0, 0);
62 if (killed) {
63 g_test_trap_assert_failed();
64 } else {
65 g_test_trap_assert_passed();
66 }
67 }
68 }
69
70
71 static void test_seccomp_killed(const char *args, int (*doit)(void))
72 {
73 test_seccomp_helper(args, true, 0, doit);
74 }
75
76 static void test_seccomp_errno(const char *args, int errnum, int (*doit)(void))
77 {
78 test_seccomp_helper(args, false, errnum, doit);
79 }
80
81 static void test_seccomp_passed(const char *args, int (*doit)(void))
82 {
83 test_seccomp_helper(args, false, 0, doit);
84 }
85
86 #ifdef SYS_fork
87 static int doit_sys_fork(void)
88 {
89 int ret = syscall(SYS_fork);
90 if (ret < 0) {
91 return ret;
92 }
93 if (ret == 0) {
94 _exit(0);
95 }
96 return 0;
97 }
98
99 static void test_seccomp_sys_fork_on_nospawn(void)
100 {
101 test_seccomp_killed("on,spawn=deny", doit_sys_fork);
102 }
103
104 static void test_seccomp_sys_fork_on(void)
105 {
106 test_seccomp_passed("on", doit_sys_fork);
107 }
108
109 static void test_seccomp_sys_fork_off(void)
110 {
111 test_seccomp_passed("off", doit_sys_fork);
112 }
113 #endif
114
115 static int doit_fork(void)
116 {
117 int ret = fork();
118 if (ret < 0) {
119 return ret;
120 }
121 if (ret == 0) {
122 _exit(0);
123 }
124 return 0;
125 }
126
127 static void test_seccomp_fork_on_nospawn(void)
128 {
129 /* XXX fixme - should be killed */
130 test_seccomp_passed("on,spawn=deny", doit_fork);
131 }
132
133 static void test_seccomp_fork_on(void)
134 {
135 test_seccomp_passed("on", doit_fork);
136 }
137
138 static void test_seccomp_fork_off(void)
139 {
140 test_seccomp_passed("off", doit_fork);
141 }
142
143 static void *noop(void *arg)
144 {
145 return arg;
146 }
147
148 static int doit_thread(void)
149 {
150 pthread_t th;
151 int ret = pthread_create(&th, NULL, noop, NULL);
152 if (ret != 0) {
153 errno = ret;
154 return -1;
155 } else {
156 pthread_join(th, NULL);
157 return 0;
158 }
159 }
160
161 static void test_seccomp_thread_on(void)
162 {
163 test_seccomp_passed("on", doit_thread);
164 }
165
166 static void test_seccomp_thread_on_nospawn(void)
167 {
168 test_seccomp_passed("on,spawn=deny", doit_thread);
169 }
170
171 static void test_seccomp_thread_off(void)
172 {
173 test_seccomp_passed("off", doit_thread);
174 }
175
176 static int doit_sched(void)
177 {
178 struct sched_param param = { .sched_priority = 0 };
179 return sched_setscheduler(getpid(), SCHED_OTHER, &param);
180 }
181
182 static void test_seccomp_sched_on_nores(void)
183 {
184 test_seccomp_errno("on,resourcecontrol=deny", EPERM, doit_sched);
185 }
186
187 static void test_seccomp_sched_on(void)
188 {
189 test_seccomp_passed("on", doit_sched);
190 }
191
192 static void test_seccomp_sched_off(void)
193 {
194 test_seccomp_passed("off", doit_sched);
195 }
196
197 static bool can_play_with_seccomp(void)
198 {
199 g_autofree char *status = NULL;
200 g_auto(GStrv) lines = NULL;
201 size_t i;
202
203 if (!g_file_get_contents("/proc/self/status", &status, NULL, NULL)) {
204 return false;
205 }
206
207 lines = g_strsplit(status, "\n", 0);
208
209 for (i = 0; lines[i] != NULL; i++) {
210 if (g_str_has_prefix(lines[i], "Seccomp:")) {
211 /*
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
216 */
217 if (!strchr(lines[i], '0')) {
218 return false;
219 }
220
221 return true;
222 }
223 }
224
225 /* Doesn't look like seccomp is enabled in the kernel */
226 return false;
227 }
228
229 int main(int argc, char **argv)
230 {
231 g_test_init(&argc, &argv, NULL);
232 if (can_play_with_seccomp()) {
233 #ifdef SYS_fork
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);
240 #endif
241
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);
248
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);
255
256 if (doit_sched() == 0) {
257 /*
258 * musl doesn't impl sched_setscheduler, hence
259 * we check above if it works first
260 */
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);
267 }
268 }
269 return g_test_run();
270 }