1 /*
2  * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by the GPLv2 license.
4  *
5  * Test code for seccomp bpf.
6  */
7 
8 #include <sys/types.h>
9 #include <asm/siginfo.h>
10 #define __have_siginfo_t 1
11 #define __have_sigval_t 1
12 #define __have_sigevent_t 1
13 
14 #include <errno.h>
15 #include <linux/filter.h>
16 #include <sys/prctl.h>
17 #include <sys/ptrace.h>
18 #include <sys/user.h>
19 #include <linux/prctl.h>
20 #include <linux/ptrace.h>
21 #include <linux/seccomp.h>
22 #include <pthread.h>
23 #include <semaphore.h>
24 #include <signal.h>
25 #include <stddef.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <time.h>
29 #include <linux/elf.h>
30 #include <sys/uio.h>
31 #include <sys/utsname.h>
32 #include <sys/fcntl.h>
33 #include <sys/mman.h>
34 #include <sys/times.h>
35 
36 #define _GNU_SOURCE
37 #include <unistd.h>
38 #include <sys/syscall.h>
39 
40 #include "test_harness.h"
41 
42 #ifndef PR_SET_PTRACER
43 # define PR_SET_PTRACER 0x59616d61
44 #endif
45 
46 #ifndef PR_SET_NO_NEW_PRIVS
47 #define PR_SET_NO_NEW_PRIVS 38
48 #define PR_GET_NO_NEW_PRIVS 39
49 #endif
50 
51 #ifndef PR_SECCOMP_EXT
52 #define PR_SECCOMP_EXT 43
53 #endif
54 
55 #ifndef SECCOMP_EXT_ACT
56 #define SECCOMP_EXT_ACT 1
57 #endif
58 
59 #ifndef SECCOMP_EXT_ACT_TSYNC
60 #define SECCOMP_EXT_ACT_TSYNC 1
61 #endif
62 
63 #ifndef SECCOMP_MODE_STRICT
64 #define SECCOMP_MODE_STRICT 1
65 #endif
66 
67 #ifndef SECCOMP_MODE_FILTER
68 #define SECCOMP_MODE_FILTER 2
69 #endif
70 
71 #ifndef SECCOMP_RET_KILL
72 #define SECCOMP_RET_KILL        0x00000000U /* kill the task immediately */
73 #define SECCOMP_RET_TRAP        0x00030000U /* disallow and force a SIGSYS */
74 #define SECCOMP_RET_ERRNO       0x00050000U /* returns an errno */
75 #define SECCOMP_RET_TRACE       0x7ff00000U /* pass to a tracer or disallow */
76 #define SECCOMP_RET_ALLOW       0x7fff0000U /* allow */
77 
78 /* Masks for the return value sections. */
79 #define SECCOMP_RET_ACTION      0x7fff0000U
80 #define SECCOMP_RET_DATA        0x0000ffffU
81 
82 struct seccomp_data {
83 	int nr;
84 	__u32 arch;
85 	__u64 instruction_pointer;
86 	__u64 args[6];
87 };
88 #endif
89 
90 #if __BYTE_ORDER == __LITTLE_ENDIAN
91 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
92 #elif __BYTE_ORDER == __BIG_ENDIAN
93 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]) + sizeof(__u32))
94 #else
95 #error "wut? Unknown __BYTE_ORDER?!"
96 #endif
97 
98 #define SIBLING_EXIT_UNKILLED	0xbadbeef
99 #define SIBLING_EXIT_FAILURE	0xbadface
100 #define SIBLING_EXIT_NEWPRIVS	0xbadfeed
101 
102 TEST(mode_strict_support)
103 {
104 	long ret;
105 
106 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, NULL, NULL);
107 	ASSERT_EQ(0, ret) {
108 		TH_LOG("Kernel does not support CONFIG_SECCOMP");
109 	}
110 	syscall(__NR_exit, 1);
111 }
112 
113 TEST_SIGNAL(mode_strict_cannot_call_prctl, SIGKILL)
114 {
115 	long ret;
116 
117 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, NULL, NULL);
118 	ASSERT_EQ(0, ret) {
119 		TH_LOG("Kernel does not support CONFIG_SECCOMP");
120 	}
121 	syscall(__NR_prctl, PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
122 		NULL, NULL, NULL);
123 	EXPECT_FALSE(true) {
124 		TH_LOG("Unreachable!");
125 	}
126 }
127 
128 /* Note! This doesn't test no new privs behavior */
129 TEST(no_new_privs_support)
130 {
131 	long ret;
132 
133 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
134 	EXPECT_EQ(0, ret) {
135 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
136 	}
137 }
138 
139 /* Tests kernel support by checking for a copy_from_user() fault on * NULL. */
140 TEST(mode_filter_support)
141 {
142 	long ret;
143 
144 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0);
145 	ASSERT_EQ(0, ret) {
146 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
147 	}
148 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL, NULL, NULL);
149 	EXPECT_EQ(-1, ret);
150 	EXPECT_EQ(EFAULT, errno) {
151 		TH_LOG("Kernel does not support CONFIG_SECCOMP_FILTER!");
152 	}
153 }
154 
155 TEST(mode_filter_without_nnp)
156 {
157 	struct sock_filter filter[] = {
158 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
159 	};
160 	struct sock_fprog prog = {
161 		.len = (unsigned short)ARRAY_SIZE(filter),
162 		.filter = filter,
163 	};
164 	long ret;
165 
166 	ret = prctl(PR_GET_NO_NEW_PRIVS, 0, NULL, 0, 0);
167 	ASSERT_LE(0, ret) {
168 		TH_LOG("Expected 0 or unsupported for NO_NEW_PRIVS");
169 	}
170 	errno = 0;
171 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
172 	/* Succeeds with CAP_SYS_ADMIN, fails without */
173 	/* TODO(wad) check caps not euid */
174 	if (geteuid()) {
175 		EXPECT_EQ(-1, ret);
176 		EXPECT_EQ(EACCES, errno);
177 	} else {
178 		EXPECT_EQ(0, ret);
179 	}
180 }
181 
182 #define MAX_INSNS_PER_PATH 32768
183 
184 TEST(filter_size_limits)
185 {
186 	int i;
187 	int count = BPF_MAXINSNS + 1;
188 	struct sock_filter allow[] = {
189 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
190 	};
191 	struct sock_filter *filter;
192 	struct sock_fprog prog = { };
193 	long ret;
194 
195 	filter = calloc(count, sizeof(*filter));
196 	ASSERT_NE(NULL, filter);
197 
198 	for (i = 0; i < count; i++)
199 		filter[i] = allow[0];
200 
201 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
202 	ASSERT_EQ(0, ret);
203 
204 	prog.filter = filter;
205 	prog.len = count;
206 
207 	/* Too many filter instructions in a single filter. */
208 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
209 	ASSERT_NE(0, ret) {
210 		TH_LOG("Installing %d insn filter was allowed", prog.len);
211 	}
212 
213 	/* One less is okay, though. */
214 	prog.len -= 1;
215 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
216 	ASSERT_EQ(0, ret) {
217 		TH_LOG("Installing %d insn filter wasn't allowed", prog.len);
218 	}
219 }
220 
221 TEST(filter_chain_limits)
222 {
223 	int i;
224 	int count = BPF_MAXINSNS;
225 	struct sock_filter allow[] = {
226 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
227 	};
228 	struct sock_filter *filter;
229 	struct sock_fprog prog = { };
230 	long ret;
231 
232 	filter = calloc(count, sizeof(*filter));
233 	ASSERT_NE(NULL, filter);
234 
235 	for (i = 0; i < count; i++)
236 		filter[i] = allow[0];
237 
238 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
239 	ASSERT_EQ(0, ret);
240 
241 	prog.filter = filter;
242 	prog.len = 1;
243 
244 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
245 	ASSERT_EQ(0, ret);
246 
247 	prog.len = count;
248 
249 	/* Too many total filter instructions. */
250 	for (i = 0; i < MAX_INSNS_PER_PATH; i++) {
251 		ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
252 		if (ret != 0)
253 			break;
254 	}
255 	ASSERT_NE(0, ret) {
256 		TH_LOG("Allowed %d %d-insn filters (total with penalties:%d)",
257 		       i, count, i * (count + 4));
258 	}
259 }
260 
261 TEST(mode_filter_cannot_move_to_strict)
262 {
263 	struct sock_filter filter[] = {
264 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
265 	};
266 	struct sock_fprog prog = {
267 		.len = (unsigned short)ARRAY_SIZE(filter),
268 		.filter = filter,
269 	};
270 	long ret;
271 
272 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
273 	ASSERT_EQ(0, ret);
274 
275 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
276 	ASSERT_EQ(0, ret);
277 
278 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, 0, 0);
279 	EXPECT_EQ(-1, ret);
280 	EXPECT_EQ(EINVAL, errno);
281 }
282 
283 
284 TEST(mode_filter_get_seccomp)
285 {
286 	struct sock_filter filter[] = {
287 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
288 	};
289 	struct sock_fprog prog = {
290 		.len = (unsigned short)ARRAY_SIZE(filter),
291 		.filter = filter,
292 	};
293 	long ret;
294 
295 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
296 	ASSERT_EQ(0, ret);
297 
298 	ret = prctl(PR_GET_SECCOMP, 0, 0, 0, 0);
299 	EXPECT_EQ(0, ret);
300 
301 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
302 	ASSERT_EQ(0, ret);
303 
304 	ret = prctl(PR_GET_SECCOMP, 0, 0, 0, 0);
305 	EXPECT_EQ(2, ret);
306 }
307 
308 
309 TEST(ALLOW_all)
310 {
311 	struct sock_filter filter[] = {
312 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
313 	};
314 	struct sock_fprog prog = {
315 		.len = (unsigned short)ARRAY_SIZE(filter),
316 		.filter = filter,
317 	};
318 	long ret;
319 
320 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
321 	ASSERT_EQ(0, ret);
322 
323 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
324 	ASSERT_EQ(0, ret);
325 }
326 
327 TEST(empty_prog)
328 {
329 	struct sock_filter filter[] = {
330 	};
331 	struct sock_fprog prog = {
332 		.len = (unsigned short)ARRAY_SIZE(filter),
333 		.filter = filter,
334 	};
335 	long ret;
336 
337 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
338 	ASSERT_EQ(0, ret);
339 
340 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
341 	EXPECT_EQ(-1, ret);
342 	EXPECT_EQ(EINVAL, errno);
343 }
344 
345 TEST_SIGNAL(unknown_ret_is_kill_inside, SIGSYS)
346 {
347 	struct sock_filter filter[] = {
348 		BPF_STMT(BPF_RET|BPF_K, 0x10000000U),
349 	};
350 	struct sock_fprog prog = {
351 		.len = (unsigned short)ARRAY_SIZE(filter),
352 		.filter = filter,
353 	};
354 	long ret;
355 
356 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
357 	ASSERT_EQ(0, ret);
358 
359 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
360 	ASSERT_EQ(0, ret);
361 	EXPECT_EQ(0, syscall(__NR_getpid)) {
362 		TH_LOG("getpid() shouldn't ever return");
363 	}
364 }
365 
366 /* return code >= 0x80000000 is unused. */
367 TEST_SIGNAL(unknown_ret_is_kill_above_allow, SIGSYS)
368 {
369 	struct sock_filter filter[] = {
370 		BPF_STMT(BPF_RET|BPF_K, 0x90000000U),
371 	};
372 	struct sock_fprog prog = {
373 		.len = (unsigned short)ARRAY_SIZE(filter),
374 		.filter = filter,
375 	};
376 	long ret;
377 
378 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
379 	ASSERT_EQ(0, ret);
380 
381 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
382 	ASSERT_EQ(0, ret);
383 	EXPECT_EQ(0, syscall(__NR_getpid)) {
384 		TH_LOG("getpid() shouldn't ever return");
385 	}
386 }
387 
388 TEST_SIGNAL(KILL_all, SIGSYS)
389 {
390 	struct sock_filter filter[] = {
391 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
392 	};
393 	struct sock_fprog prog = {
394 		.len = (unsigned short)ARRAY_SIZE(filter),
395 		.filter = filter,
396 	};
397 	long ret;
398 
399 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
400 	ASSERT_EQ(0, ret);
401 
402 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
403 	ASSERT_EQ(0, ret);
404 }
405 
406 TEST_SIGNAL(KILL_one, SIGSYS)
407 {
408 	struct sock_filter filter[] = {
409 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
410 			offsetof(struct seccomp_data, nr)),
411 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1),
412 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
413 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
414 	};
415 	struct sock_fprog prog = {
416 		.len = (unsigned short)ARRAY_SIZE(filter),
417 		.filter = filter,
418 	};
419 	long ret;
420 	pid_t parent = getppid();
421 
422 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
423 	ASSERT_EQ(0, ret);
424 
425 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
426 	ASSERT_EQ(0, ret);
427 
428 	EXPECT_EQ(parent, syscall(__NR_getppid));
429 	/* getpid() should never return. */
430 	EXPECT_EQ(0, syscall(__NR_getpid));
431 }
432 
433 TEST_SIGNAL(KILL_one_arg_one, SIGSYS)
434 {
435 	void *fatal_address;
436 	struct sock_filter filter[] = {
437 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
438 			offsetof(struct seccomp_data, nr)),
439 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_times, 1, 0),
440 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
441 		/* Only both with lower 32-bit for now. */
442 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(0)),
443 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,
444 			(unsigned long)&fatal_address, 0, 1),
445 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
446 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
447 	};
448 	struct sock_fprog prog = {
449 		.len = (unsigned short)ARRAY_SIZE(filter),
450 		.filter = filter,
451 	};
452 	long ret;
453 	pid_t parent = getppid();
454 	struct tms timebuf;
455 	clock_t clock = times(&timebuf);
456 
457 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
458 	ASSERT_EQ(0, ret);
459 
460 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
461 	ASSERT_EQ(0, ret);
462 
463 	EXPECT_EQ(parent, syscall(__NR_getppid));
464 	EXPECT_LE(clock, syscall(__NR_times, &timebuf));
465 	/* times() should never return. */
466 	EXPECT_EQ(0, syscall(__NR_times, &fatal_address));
467 }
468 
469 TEST_SIGNAL(KILL_one_arg_six, SIGSYS)
470 {
471 #ifndef __NR_mmap2
472 	int sysno = __NR_mmap;
473 #else
474 	int sysno = __NR_mmap2;
475 #endif
476 	struct sock_filter filter[] = {
477 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
478 			offsetof(struct seccomp_data, nr)),
479 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, sysno, 1, 0),
480 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
481 		/* Only both with lower 32-bit for now. */
482 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(5)),
483 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x0C0FFEE, 0, 1),
484 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
485 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
486 	};
487 	struct sock_fprog prog = {
488 		.len = (unsigned short)ARRAY_SIZE(filter),
489 		.filter = filter,
490 	};
491 	long ret;
492 	pid_t parent = getppid();
493 	int fd;
494 	void *map1, *map2;
495 	int page_size = sysconf(_SC_PAGESIZE);
496 
497 	ASSERT_LT(0, page_size);
498 
499 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
500 	ASSERT_EQ(0, ret);
501 
502 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
503 	ASSERT_EQ(0, ret);
504 
505 	fd = open("/dev/zero", O_RDONLY);
506 	ASSERT_NE(-1, fd);
507 
508 	EXPECT_EQ(parent, syscall(__NR_getppid));
509 	map1 = (void *)syscall(sysno,
510 		NULL, page_size, PROT_READ, MAP_PRIVATE, fd, page_size);
511 	EXPECT_NE(MAP_FAILED, map1);
512 	/* mmap2() should never return. */
513 	map2 = (void *)syscall(sysno,
514 		 NULL, page_size, PROT_READ, MAP_PRIVATE, fd, 0x0C0FFEE);
515 	EXPECT_EQ(MAP_FAILED, map2);
516 
517 	/* The test failed, so clean up the resources. */
518 	munmap(map1, page_size);
519 	munmap(map2, page_size);
520 	close(fd);
521 }
522 
523 /* TODO(wad) add 64-bit versus 32-bit arg tests. */
524 TEST(arg_out_of_range)
525 {
526 	struct sock_filter filter[] = {
527 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(6)),
528 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
529 	};
530 	struct sock_fprog prog = {
531 		.len = (unsigned short)ARRAY_SIZE(filter),
532 		.filter = filter,
533 	};
534 	long ret;
535 
536 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
537 	ASSERT_EQ(0, ret);
538 
539 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
540 	EXPECT_EQ(-1, ret);
541 	EXPECT_EQ(EINVAL, errno);
542 }
543 
544 TEST(ERRNO_valid)
545 {
546 	struct sock_filter filter[] = {
547 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
548 			offsetof(struct seccomp_data, nr)),
549 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1),
550 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | E2BIG),
551 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
552 	};
553 	struct sock_fprog prog = {
554 		.len = (unsigned short)ARRAY_SIZE(filter),
555 		.filter = filter,
556 	};
557 	long ret;
558 	pid_t parent = getppid();
559 
560 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
561 	ASSERT_EQ(0, ret);
562 
563 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
564 	ASSERT_EQ(0, ret);
565 
566 	EXPECT_EQ(parent, syscall(__NR_getppid));
567 	EXPECT_EQ(-1, read(0, NULL, 0));
568 	EXPECT_EQ(E2BIG, errno);
569 }
570 
571 TEST(ERRNO_zero)
572 {
573 	struct sock_filter filter[] = {
574 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
575 			offsetof(struct seccomp_data, nr)),
576 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1),
577 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | 0),
578 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
579 	};
580 	struct sock_fprog prog = {
581 		.len = (unsigned short)ARRAY_SIZE(filter),
582 		.filter = filter,
583 	};
584 	long ret;
585 	pid_t parent = getppid();
586 
587 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
588 	ASSERT_EQ(0, ret);
589 
590 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
591 	ASSERT_EQ(0, ret);
592 
593 	EXPECT_EQ(parent, syscall(__NR_getppid));
594 	/* "errno" of 0 is ok. */
595 	EXPECT_EQ(0, read(0, NULL, 0));
596 }
597 
598 TEST(ERRNO_capped)
599 {
600 	struct sock_filter filter[] = {
601 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
602 			offsetof(struct seccomp_data, nr)),
603 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1),
604 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | 4096),
605 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
606 	};
607 	struct sock_fprog prog = {
608 		.len = (unsigned short)ARRAY_SIZE(filter),
609 		.filter = filter,
610 	};
611 	long ret;
612 	pid_t parent = getppid();
613 
614 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
615 	ASSERT_EQ(0, ret);
616 
617 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
618 	ASSERT_EQ(0, ret);
619 
620 	EXPECT_EQ(parent, syscall(__NR_getppid));
621 	EXPECT_EQ(-1, read(0, NULL, 0));
622 	EXPECT_EQ(4095, errno);
623 }
624 
625 FIXTURE_DATA(TRAP) {
626 	struct sock_fprog prog;
627 };
628 
629 FIXTURE_SETUP(TRAP)
630 {
631 	struct sock_filter filter[] = {
632 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
633 			offsetof(struct seccomp_data, nr)),
634 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1),
635 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP),
636 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
637 	};
638 
639 	memset(&self->prog, 0, sizeof(self->prog));
640 	self->prog.filter = malloc(sizeof(filter));
641 	ASSERT_NE(NULL, self->prog.filter);
642 	memcpy(self->prog.filter, filter, sizeof(filter));
643 	self->prog.len = (unsigned short)ARRAY_SIZE(filter);
644 }
645 
646 FIXTURE_TEARDOWN(TRAP)
647 {
648 	if (self->prog.filter)
649 		free(self->prog.filter);
650 }
651 
652 TEST_F_SIGNAL(TRAP, dfl, SIGSYS)
653 {
654 	long ret;
655 
656 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
657 	ASSERT_EQ(0, ret);
658 
659 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog);
660 	ASSERT_EQ(0, ret);
661 	syscall(__NR_getpid);
662 }
663 
664 /* Ensure that SIGSYS overrides SIG_IGN */
665 TEST_F_SIGNAL(TRAP, ign, SIGSYS)
666 {
667 	long ret;
668 
669 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
670 	ASSERT_EQ(0, ret);
671 
672 	signal(SIGSYS, SIG_IGN);
673 
674 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog);
675 	ASSERT_EQ(0, ret);
676 	syscall(__NR_getpid);
677 }
678 
679 static struct siginfo TRAP_info;
680 static volatile int TRAP_nr;
681 static void TRAP_action(int nr, siginfo_t *info, void *void_context)
682 {
683 	memcpy(&TRAP_info, info, sizeof(TRAP_info));
684 	TRAP_nr = nr;
685 }
686 
687 TEST_F(TRAP, handler)
688 {
689 	int ret, test;
690 	struct sigaction act;
691 	sigset_t mask;
692 
693 	memset(&act, 0, sizeof(act));
694 	sigemptyset(&mask);
695 	sigaddset(&mask, SIGSYS);
696 
697 	act.sa_sigaction = &TRAP_action;
698 	act.sa_flags = SA_SIGINFO;
699 	ret = sigaction(SIGSYS, &act, NULL);
700 	ASSERT_EQ(0, ret) {
701 		TH_LOG("sigaction failed");
702 	}
703 	ret = sigprocmask(SIG_UNBLOCK, &mask, NULL);
704 	ASSERT_EQ(0, ret) {
705 		TH_LOG("sigprocmask failed");
706 	}
707 
708 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
709 	ASSERT_EQ(0, ret);
710 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog);
711 	ASSERT_EQ(0, ret);
712 	TRAP_nr = 0;
713 	memset(&TRAP_info, 0, sizeof(TRAP_info));
714 	/* Expect the registers to be rolled back. (nr = error) may vary
715 	 * based on arch. */
716 	ret = syscall(__NR_getpid);
717 	/* Silence gcc warning about volatile. */
718 	test = TRAP_nr;
719 	EXPECT_EQ(SIGSYS, test);
720 	struct local_sigsys {
721 		void *_call_addr;	/* calling user insn */
722 		int _syscall;		/* triggering system call number */
723 		unsigned int _arch;	/* AUDIT_ARCH_* of syscall */
724 	} *sigsys = (struct local_sigsys *)
725 #ifdef si_syscall
726 		&(TRAP_info.si_call_addr);
727 #else
728 		&TRAP_info.si_pid;
729 #endif
730 	EXPECT_EQ(__NR_getpid, sigsys->_syscall);
731 	/* Make sure arch is non-zero. */
732 	EXPECT_NE(0, sigsys->_arch);
733 	EXPECT_NE(0, (unsigned long)sigsys->_call_addr);
734 }
735 
736 FIXTURE_DATA(precedence) {
737 	struct sock_fprog allow;
738 	struct sock_fprog trace;
739 	struct sock_fprog error;
740 	struct sock_fprog trap;
741 	struct sock_fprog kill;
742 };
743 
744 FIXTURE_SETUP(precedence)
745 {
746 	struct sock_filter allow_insns[] = {
747 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
748 	};
749 	struct sock_filter trace_insns[] = {
750 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
751 			offsetof(struct seccomp_data, nr)),
752 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0),
753 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
754 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE),
755 	};
756 	struct sock_filter error_insns[] = {
757 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
758 			offsetof(struct seccomp_data, nr)),
759 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0),
760 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
761 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO),
762 	};
763 	struct sock_filter trap_insns[] = {
764 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
765 			offsetof(struct seccomp_data, nr)),
766 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0),
767 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
768 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP),
769 	};
770 	struct sock_filter kill_insns[] = {
771 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
772 			offsetof(struct seccomp_data, nr)),
773 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0),
774 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
775 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
776 	};
777 
778 	memset(self, 0, sizeof(*self));
779 #define FILTER_ALLOC(_x) \
780 	self->_x.filter = malloc(sizeof(_x##_insns)); \
781 	ASSERT_NE(NULL, self->_x.filter); \
782 	memcpy(self->_x.filter, &_x##_insns, sizeof(_x##_insns)); \
783 	self->_x.len = (unsigned short)ARRAY_SIZE(_x##_insns)
784 	FILTER_ALLOC(allow);
785 	FILTER_ALLOC(trace);
786 	FILTER_ALLOC(error);
787 	FILTER_ALLOC(trap);
788 	FILTER_ALLOC(kill);
789 }
790 
791 FIXTURE_TEARDOWN(precedence)
792 {
793 #define FILTER_FREE(_x) if (self->_x.filter) free(self->_x.filter)
794 	FILTER_FREE(allow);
795 	FILTER_FREE(trace);
796 	FILTER_FREE(error);
797 	FILTER_FREE(trap);
798 	FILTER_FREE(kill);
799 }
800 
801 TEST_F(precedence, allow_ok)
802 {
803 	pid_t parent, res = 0;
804 	long ret;
805 
806 	parent = getppid();
807 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
808 	ASSERT_EQ(0, ret);
809 
810 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
811 	ASSERT_EQ(0, ret);
812 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
813 	ASSERT_EQ(0, ret);
814 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
815 	ASSERT_EQ(0, ret);
816 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
817 	ASSERT_EQ(0, ret);
818 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill);
819 	ASSERT_EQ(0, ret);
820 	/* Should work just fine. */
821 	res = syscall(__NR_getppid);
822 	EXPECT_EQ(parent, res);
823 }
824 
825 TEST_F_SIGNAL(precedence, kill_is_highest, SIGSYS)
826 {
827 	pid_t parent, res = 0;
828 	long ret;
829 
830 	parent = getppid();
831 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
832 	ASSERT_EQ(0, ret);
833 
834 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
835 	ASSERT_EQ(0, ret);
836 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
837 	ASSERT_EQ(0, ret);
838 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
839 	ASSERT_EQ(0, ret);
840 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
841 	ASSERT_EQ(0, ret);
842 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill);
843 	ASSERT_EQ(0, ret);
844 	/* Should work just fine. */
845 	res = syscall(__NR_getppid);
846 	EXPECT_EQ(parent, res);
847 	/* getpid() should never return. */
848 	res = syscall(__NR_getpid);
849 	EXPECT_EQ(0, res);
850 }
851 
852 TEST_F_SIGNAL(precedence, kill_is_highest_in_any_order, SIGSYS)
853 {
854 	pid_t parent;
855 	long ret;
856 
857 	parent = getppid();
858 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
859 	ASSERT_EQ(0, ret);
860 
861 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
862 	ASSERT_EQ(0, ret);
863 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill);
864 	ASSERT_EQ(0, ret);
865 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
866 	ASSERT_EQ(0, ret);
867 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
868 	ASSERT_EQ(0, ret);
869 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
870 	ASSERT_EQ(0, ret);
871 	/* Should work just fine. */
872 	EXPECT_EQ(parent, syscall(__NR_getppid));
873 	/* getpid() should never return. */
874 	EXPECT_EQ(0, syscall(__NR_getpid));
875 }
876 
877 TEST_F_SIGNAL(precedence, trap_is_second, SIGSYS)
878 {
879 	pid_t parent;
880 	long ret;
881 
882 	parent = getppid();
883 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
884 	ASSERT_EQ(0, ret);
885 
886 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
887 	ASSERT_EQ(0, ret);
888 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
889 	ASSERT_EQ(0, ret);
890 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
891 	ASSERT_EQ(0, ret);
892 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
893 	ASSERT_EQ(0, ret);
894 	/* Should work just fine. */
895 	EXPECT_EQ(parent, syscall(__NR_getppid));
896 	/* getpid() should never return. */
897 	EXPECT_EQ(0, syscall(__NR_getpid));
898 }
899 
900 TEST_F_SIGNAL(precedence, trap_is_second_in_any_order, SIGSYS)
901 {
902 	pid_t parent;
903 	long ret;
904 
905 	parent = getppid();
906 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
907 	ASSERT_EQ(0, ret);
908 
909 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
910 	ASSERT_EQ(0, ret);
911 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap);
912 	ASSERT_EQ(0, ret);
913 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
914 	ASSERT_EQ(0, ret);
915 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
916 	ASSERT_EQ(0, ret);
917 	/* Should work just fine. */
918 	EXPECT_EQ(parent, syscall(__NR_getppid));
919 	/* getpid() should never return. */
920 	EXPECT_EQ(0, syscall(__NR_getpid));
921 }
922 
923 TEST_F(precedence, errno_is_third)
924 {
925 	pid_t parent;
926 	long ret;
927 
928 	parent = getppid();
929 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
930 	ASSERT_EQ(0, ret);
931 
932 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
933 	ASSERT_EQ(0, ret);
934 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
935 	ASSERT_EQ(0, ret);
936 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
937 	ASSERT_EQ(0, ret);
938 	/* Should work just fine. */
939 	EXPECT_EQ(parent, syscall(__NR_getppid));
940 	EXPECT_EQ(0, syscall(__NR_getpid));
941 }
942 
943 TEST_F(precedence, errno_is_third_in_any_order)
944 {
945 	pid_t parent;
946 	long ret;
947 
948 	parent = getppid();
949 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
950 	ASSERT_EQ(0, ret);
951 
952 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error);
953 	ASSERT_EQ(0, ret);
954 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
955 	ASSERT_EQ(0, ret);
956 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
957 	ASSERT_EQ(0, ret);
958 	/* Should work just fine. */
959 	EXPECT_EQ(parent, syscall(__NR_getppid));
960 	EXPECT_EQ(0, syscall(__NR_getpid));
961 }
962 
963 TEST_F(precedence, trace_is_fourth)
964 {
965 	pid_t parent;
966 	long ret;
967 
968 	parent = getppid();
969 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
970 	ASSERT_EQ(0, ret);
971 
972 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
973 	ASSERT_EQ(0, ret);
974 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
975 	ASSERT_EQ(0, ret);
976 	/* Should work just fine. */
977 	EXPECT_EQ(parent, syscall(__NR_getppid));
978 	/* No ptracer */
979 	EXPECT_EQ(-1, syscall(__NR_getpid));
980 }
981 
982 TEST_F(precedence, trace_is_fourth_in_any_order)
983 {
984 	pid_t parent;
985 	long ret;
986 
987 	parent = getppid();
988 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
989 	ASSERT_EQ(0, ret);
990 
991 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace);
992 	ASSERT_EQ(0, ret);
993 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow);
994 	ASSERT_EQ(0, ret);
995 	/* Should work just fine. */
996 	EXPECT_EQ(parent, syscall(__NR_getppid));
997 	/* No ptracer */
998 	EXPECT_EQ(-1, syscall(__NR_getpid));
999 }
1000 
1001 #ifndef PTRACE_O_TRACESECCOMP
1002 #define PTRACE_O_TRACESECCOMP	0x00000080
1003 #endif
1004 
1005 /* Catch the Ubuntu 12.04 value error. */
1006 #if PTRACE_EVENT_SECCOMP != 7
1007 #undef PTRACE_EVENT_SECCOMP
1008 #endif
1009 
1010 #ifndef PTRACE_EVENT_SECCOMP
1011 #define PTRACE_EVENT_SECCOMP 7
1012 #endif
1013 
1014 #define IS_SECCOMP_EVENT(status) ((status >> 16) == PTRACE_EVENT_SECCOMP)
1015 bool tracer_running;
1016 void tracer_stop(int sig)
1017 {
1018 	tracer_running = false;
1019 }
1020 
1021 typedef void tracer_func_t(struct __test_metadata *_metadata,
1022 			   pid_t tracee, int status, void *args);
1023 
1024 void start_tracer(struct __test_metadata *_metadata, int fd, pid_t tracee,
1025 	    tracer_func_t tracer_func, void *args, bool ptrace_syscall)
1026 {
1027 	int ret = -1;
1028 	struct sigaction action = {
1029 		.sa_handler = tracer_stop,
1030 	};
1031 
1032 	/* Allow external shutdown. */
1033 	tracer_running = true;
1034 	ASSERT_EQ(0, sigaction(SIGUSR1, &action, NULL));
1035 
1036 	errno = 0;
1037 	while (ret == -1 && errno != EINVAL)
1038 		ret = ptrace(PTRACE_ATTACH, tracee, NULL, 0);
1039 	ASSERT_EQ(0, ret) {
1040 		kill(tracee, SIGKILL);
1041 	}
1042 	/* Wait for attach stop */
1043 	wait(NULL);
1044 
1045 	ret = ptrace(PTRACE_SETOPTIONS, tracee, NULL, ptrace_syscall ?
1046 						      PTRACE_O_TRACESYSGOOD :
1047 						      PTRACE_O_TRACESECCOMP);
1048 	ASSERT_EQ(0, ret) {
1049 		TH_LOG("Failed to set PTRACE_O_TRACESECCOMP");
1050 		kill(tracee, SIGKILL);
1051 	}
1052 	ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT,
1053 		     tracee, NULL, 0);
1054 	ASSERT_EQ(0, ret);
1055 
1056 	/* Unblock the tracee */
1057 	ASSERT_EQ(1, write(fd, "A", 1));
1058 	ASSERT_EQ(0, close(fd));
1059 
1060 	/* Run until we're shut down. Must assert to stop execution. */
1061 	while (tracer_running) {
1062 		int status;
1063 
1064 		if (wait(&status) != tracee)
1065 			continue;
1066 		if (WIFSIGNALED(status) || WIFEXITED(status))
1067 			/* Child is dead. Time to go. */
1068 			return;
1069 
1070 		/* Check if this is a seccomp event. */
1071 		ASSERT_EQ(!ptrace_syscall, IS_SECCOMP_EVENT(status));
1072 
1073 		tracer_func(_metadata, tracee, status, args);
1074 
1075 		ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT,
1076 			     tracee, NULL, 0);
1077 		ASSERT_EQ(0, ret);
1078 	}
1079 	/* Directly report the status of our test harness results. */
1080 	syscall(__NR_exit, _metadata->passed ? EXIT_SUCCESS : EXIT_FAILURE);
1081 }
1082 
1083 /* Common tracer setup/teardown functions. */
1084 void cont_handler(int num)
1085 { }
1086 pid_t setup_trace_fixture(struct __test_metadata *_metadata,
1087 			  tracer_func_t func, void *args, bool ptrace_syscall)
1088 {
1089 	char sync;
1090 	int pipefd[2];
1091 	pid_t tracer_pid;
1092 	pid_t tracee = getpid();
1093 
1094 	/* Setup a pipe for clean synchronization. */
1095 	ASSERT_EQ(0, pipe(pipefd));
1096 
1097 	/* Fork a child which we'll promote to tracer */
1098 	tracer_pid = fork();
1099 	ASSERT_LE(0, tracer_pid);
1100 	signal(SIGALRM, cont_handler);
1101 	if (tracer_pid == 0) {
1102 		close(pipefd[0]);
1103 		start_tracer(_metadata, pipefd[1], tracee, func, args,
1104 			     ptrace_syscall);
1105 		syscall(__NR_exit, 0);
1106 	}
1107 	close(pipefd[1]);
1108 	prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0);
1109 	read(pipefd[0], &sync, 1);
1110 	close(pipefd[0]);
1111 
1112 	return tracer_pid;
1113 }
1114 void teardown_trace_fixture(struct __test_metadata *_metadata,
1115 			    pid_t tracer)
1116 {
1117 	if (tracer) {
1118 		int status;
1119 		/*
1120 		 * Extract the exit code from the other process and
1121 		 * adopt it for ourselves in case its asserts failed.
1122 		 */
1123 		ASSERT_EQ(0, kill(tracer, SIGUSR1));
1124 		ASSERT_EQ(tracer, waitpid(tracer, &status, 0));
1125 		if (WEXITSTATUS(status))
1126 			_metadata->passed = 0;
1127 	}
1128 }
1129 
1130 /* "poke" tracer arguments and function. */
1131 struct tracer_args_poke_t {
1132 	unsigned long poke_addr;
1133 };
1134 
1135 void tracer_poke(struct __test_metadata *_metadata, pid_t tracee, int status,
1136 		 void *args)
1137 {
1138 	int ret;
1139 	unsigned long msg;
1140 	struct tracer_args_poke_t *info = (struct tracer_args_poke_t *)args;
1141 
1142 	ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg);
1143 	EXPECT_EQ(0, ret);
1144 	/* If this fails, don't try to recover. */
1145 	ASSERT_EQ(0x1001, msg) {
1146 		kill(tracee, SIGKILL);
1147 	}
1148 	/*
1149 	 * Poke in the message.
1150 	 * Registers are not touched to try to keep this relatively arch
1151 	 * agnostic.
1152 	 */
1153 	ret = ptrace(PTRACE_POKEDATA, tracee, info->poke_addr, 0x1001);
1154 	EXPECT_EQ(0, ret);
1155 }
1156 
1157 FIXTURE_DATA(TRACE_poke) {
1158 	struct sock_fprog prog;
1159 	pid_t tracer;
1160 	long poked;
1161 	struct tracer_args_poke_t tracer_args;
1162 };
1163 
1164 FIXTURE_SETUP(TRACE_poke)
1165 {
1166 	struct sock_filter filter[] = {
1167 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1168 			offsetof(struct seccomp_data, nr)),
1169 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1),
1170 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1001),
1171 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1172 	};
1173 
1174 	self->poked = 0;
1175 	memset(&self->prog, 0, sizeof(self->prog));
1176 	self->prog.filter = malloc(sizeof(filter));
1177 	ASSERT_NE(NULL, self->prog.filter);
1178 	memcpy(self->prog.filter, filter, sizeof(filter));
1179 	self->prog.len = (unsigned short)ARRAY_SIZE(filter);
1180 
1181 	/* Set up tracer args. */
1182 	self->tracer_args.poke_addr = (unsigned long)&self->poked;
1183 
1184 	/* Launch tracer. */
1185 	self->tracer = setup_trace_fixture(_metadata, tracer_poke,
1186 					   &self->tracer_args, false);
1187 }
1188 
1189 FIXTURE_TEARDOWN(TRACE_poke)
1190 {
1191 	teardown_trace_fixture(_metadata, self->tracer);
1192 	if (self->prog.filter)
1193 		free(self->prog.filter);
1194 }
1195 
1196 TEST_F(TRACE_poke, read_has_side_effects)
1197 {
1198 	ssize_t ret;
1199 
1200 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1201 	ASSERT_EQ(0, ret);
1202 
1203 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1204 	ASSERT_EQ(0, ret);
1205 
1206 	EXPECT_EQ(0, self->poked);
1207 	ret = read(-1, NULL, 0);
1208 	EXPECT_EQ(-1, ret);
1209 	EXPECT_EQ(0x1001, self->poked);
1210 }
1211 
1212 TEST_F(TRACE_poke, getpid_runs_normally)
1213 {
1214 	long ret;
1215 
1216 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1217 	ASSERT_EQ(0, ret);
1218 
1219 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1220 	ASSERT_EQ(0, ret);
1221 
1222 	EXPECT_EQ(0, self->poked);
1223 	EXPECT_NE(0, syscall(__NR_getpid));
1224 	EXPECT_EQ(0, self->poked);
1225 }
1226 
1227 #if defined(__x86_64__)
1228 # define ARCH_REGS	struct user_regs_struct
1229 # define SYSCALL_NUM	orig_rax
1230 # define SYSCALL_RET	rax
1231 #elif defined(__i386__)
1232 # define ARCH_REGS	struct user_regs_struct
1233 # define SYSCALL_NUM	orig_eax
1234 # define SYSCALL_RET	eax
1235 #elif defined(__arm__)
1236 # define ARCH_REGS	struct pt_regs
1237 # define SYSCALL_NUM	ARM_r7
1238 # define SYSCALL_RET	ARM_r0
1239 #elif defined(__aarch64__)
1240 # define ARCH_REGS	struct user_pt_regs
1241 # define SYSCALL_NUM	regs[8]
1242 # define SYSCALL_RET	regs[0]
1243 #elif defined(__hppa__)
1244 # define ARCH_REGS	struct user_regs_struct
1245 # define SYSCALL_NUM	gr[20]
1246 # define SYSCALL_RET	gr[28]
1247 #elif defined(__powerpc__)
1248 # define ARCH_REGS	struct pt_regs
1249 # define SYSCALL_NUM	gpr[0]
1250 # define SYSCALL_RET	gpr[3]
1251 #elif defined(__s390__)
1252 # define ARCH_REGS     s390_regs
1253 # define SYSCALL_NUM   gprs[2]
1254 # define SYSCALL_RET   gprs[2]
1255 #elif defined(__mips__)
1256 # define ARCH_REGS	struct pt_regs
1257 # define SYSCALL_NUM	regs[2]
1258 # define SYSCALL_SYSCALL_NUM regs[4]
1259 # define SYSCALL_RET	regs[2]
1260 # define SYSCALL_NUM_RET_SHARE_REG
1261 #else
1262 # error "Do not know how to find your architecture's registers and syscalls"
1263 #endif
1264 
1265 /* Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for
1266  * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux).
1267  */
1268 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__)
1269 #define HAVE_GETREGS
1270 #endif
1271 
1272 /* Architecture-specific syscall fetching routine. */
1273 int get_syscall(struct __test_metadata *_metadata, pid_t tracee)
1274 {
1275 	ARCH_REGS regs;
1276 #ifdef HAVE_GETREGS
1277 	EXPECT_EQ(0, ptrace(PTRACE_GETREGS, tracee, 0, &regs)) {
1278 		TH_LOG("PTRACE_GETREGS failed");
1279 		return -1;
1280 	}
1281 #else
1282 	struct iovec iov;
1283 
1284 	iov.iov_base = &regs;
1285 	iov.iov_len = sizeof(regs);
1286 	EXPECT_EQ(0, ptrace(PTRACE_GETREGSET, tracee, NT_PRSTATUS, &iov)) {
1287 		TH_LOG("PTRACE_GETREGSET failed");
1288 		return -1;
1289 	}
1290 #endif
1291 
1292 #if defined(__mips__)
1293 	if (regs.SYSCALL_NUM == __NR_O32_Linux)
1294 		return regs.SYSCALL_SYSCALL_NUM;
1295 #endif
1296 	return regs.SYSCALL_NUM;
1297 }
1298 
1299 /* Architecture-specific syscall changing routine. */
1300 void change_syscall(struct __test_metadata *_metadata,
1301 		    pid_t tracee, int syscall)
1302 {
1303 	int ret;
1304 	ARCH_REGS regs;
1305 #ifdef HAVE_GETREGS
1306 	ret = ptrace(PTRACE_GETREGS, tracee, 0, &regs);
1307 #else
1308 	struct iovec iov;
1309 	iov.iov_base = &regs;
1310 	iov.iov_len = sizeof(regs);
1311 	ret = ptrace(PTRACE_GETREGSET, tracee, NT_PRSTATUS, &iov);
1312 #endif
1313 	EXPECT_EQ(0, ret);
1314 
1315 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \
1316     defined(__s390__) || defined(__hppa__)
1317 	{
1318 		regs.SYSCALL_NUM = syscall;
1319 	}
1320 #elif defined(__mips__)
1321 	{
1322 		if (regs.SYSCALL_NUM == __NR_O32_Linux)
1323 			regs.SYSCALL_SYSCALL_NUM = syscall;
1324 		else
1325 			regs.SYSCALL_NUM = syscall;
1326 	}
1327 
1328 #elif defined(__arm__)
1329 # ifndef PTRACE_SET_SYSCALL
1330 #  define PTRACE_SET_SYSCALL   23
1331 # endif
1332 	{
1333 		ret = ptrace(PTRACE_SET_SYSCALL, tracee, NULL, syscall);
1334 		EXPECT_EQ(0, ret);
1335 	}
1336 
1337 #elif defined(__aarch64__)
1338 # ifndef NT_ARM_SYSTEM_CALL
1339 #  define NT_ARM_SYSTEM_CALL 0x404
1340 # endif
1341 	{
1342 		iov.iov_base = &syscall;
1343 		iov.iov_len = sizeof(syscall);
1344 		ret = ptrace(PTRACE_SETREGSET, tracee, NT_ARM_SYSTEM_CALL,
1345 			     &iov);
1346 		EXPECT_EQ(0, ret);
1347 	}
1348 
1349 #else
1350 	ASSERT_EQ(1, 0) {
1351 		TH_LOG("How is the syscall changed on this architecture?");
1352 	}
1353 #endif
1354 
1355 	/* If syscall is skipped, change return value. */
1356 	if (syscall == -1)
1357 #ifdef SYSCALL_NUM_RET_SHARE_REG
1358 		TH_LOG("Can't modify syscall return on this architecture");
1359 #else
1360 		regs.SYSCALL_RET = 1;
1361 #endif
1362 
1363 #ifdef HAVE_GETREGS
1364 	ret = ptrace(PTRACE_SETREGS, tracee, 0, &regs);
1365 #else
1366 	iov.iov_base = &regs;
1367 	iov.iov_len = sizeof(regs);
1368 	ret = ptrace(PTRACE_SETREGSET, tracee, NT_PRSTATUS, &iov);
1369 #endif
1370 	EXPECT_EQ(0, ret);
1371 }
1372 
1373 void tracer_syscall(struct __test_metadata *_metadata, pid_t tracee,
1374 		    int status, void *args)
1375 {
1376 	int ret;
1377 	unsigned long msg;
1378 
1379 	/* Make sure we got the right message. */
1380 	ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg);
1381 	EXPECT_EQ(0, ret);
1382 
1383 	/* Validate and take action on expected syscalls. */
1384 	switch (msg) {
1385 	case 0x1002:
1386 		/* change getpid to getppid. */
1387 		EXPECT_EQ(__NR_getpid, get_syscall(_metadata, tracee));
1388 		change_syscall(_metadata, tracee, __NR_getppid);
1389 		break;
1390 	case 0x1003:
1391 		/* skip gettid. */
1392 		EXPECT_EQ(__NR_gettid, get_syscall(_metadata, tracee));
1393 		change_syscall(_metadata, tracee, -1);
1394 		break;
1395 	case 0x1004:
1396 		/* do nothing (allow getppid) */
1397 		EXPECT_EQ(__NR_getppid, get_syscall(_metadata, tracee));
1398 		break;
1399 	default:
1400 		EXPECT_EQ(0, msg) {
1401 			TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg);
1402 			kill(tracee, SIGKILL);
1403 		}
1404 	}
1405 
1406 }
1407 
1408 void tracer_ptrace(struct __test_metadata *_metadata, pid_t tracee,
1409 		   int status, void *args)
1410 {
1411 	int ret, nr;
1412 	unsigned long msg;
1413 	static bool entry;
1414 
1415 	/* Make sure we got an empty message. */
1416 	ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg);
1417 	EXPECT_EQ(0, ret);
1418 	EXPECT_EQ(0, msg);
1419 
1420 	/* The only way to tell PTRACE_SYSCALL entry/exit is by counting. */
1421 	entry = !entry;
1422 	if (!entry)
1423 		return;
1424 
1425 	nr = get_syscall(_metadata, tracee);
1426 
1427 	if (nr == __NR_getpid)
1428 		change_syscall(_metadata, tracee, __NR_getppid);
1429 }
1430 
1431 FIXTURE_DATA(TRACE_syscall) {
1432 	struct sock_fprog prog;
1433 	pid_t tracer, mytid, mypid, parent;
1434 };
1435 
1436 FIXTURE_SETUP(TRACE_syscall)
1437 {
1438 	struct sock_filter filter[] = {
1439 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1440 			offsetof(struct seccomp_data, nr)),
1441 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1),
1442 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1002),
1443 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_gettid, 0, 1),
1444 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1003),
1445 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1446 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1004),
1447 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1448 	};
1449 
1450 	memset(&self->prog, 0, sizeof(self->prog));
1451 	self->prog.filter = malloc(sizeof(filter));
1452 	ASSERT_NE(NULL, self->prog.filter);
1453 	memcpy(self->prog.filter, filter, sizeof(filter));
1454 	self->prog.len = (unsigned short)ARRAY_SIZE(filter);
1455 
1456 	/* Prepare some testable syscall results. */
1457 	self->mytid = syscall(__NR_gettid);
1458 	ASSERT_GT(self->mytid, 0);
1459 	ASSERT_NE(self->mytid, 1) {
1460 		TH_LOG("Running this test as init is not supported. :)");
1461 	}
1462 
1463 	self->mypid = getpid();
1464 	ASSERT_GT(self->mypid, 0);
1465 	ASSERT_EQ(self->mytid, self->mypid);
1466 
1467 	self->parent = getppid();
1468 	ASSERT_GT(self->parent, 0);
1469 	ASSERT_NE(self->parent, self->mypid);
1470 
1471 	/* Launch tracer. */
1472 	self->tracer = setup_trace_fixture(_metadata, tracer_syscall, NULL,
1473 					   false);
1474 }
1475 
1476 FIXTURE_TEARDOWN(TRACE_syscall)
1477 {
1478 	teardown_trace_fixture(_metadata, self->tracer);
1479 	if (self->prog.filter)
1480 		free(self->prog.filter);
1481 }
1482 
1483 TEST_F(TRACE_syscall, syscall_allowed)
1484 {
1485 	long ret;
1486 
1487 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1488 	ASSERT_EQ(0, ret);
1489 
1490 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1491 	ASSERT_EQ(0, ret);
1492 
1493 	/* getppid works as expected (no changes). */
1494 	EXPECT_EQ(self->parent, syscall(__NR_getppid));
1495 	EXPECT_NE(self->mypid, syscall(__NR_getppid));
1496 }
1497 
1498 TEST_F(TRACE_syscall, syscall_redirected)
1499 {
1500 	long ret;
1501 
1502 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1503 	ASSERT_EQ(0, ret);
1504 
1505 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1506 	ASSERT_EQ(0, ret);
1507 
1508 	/* getpid has been redirected to getppid as expected. */
1509 	EXPECT_EQ(self->parent, syscall(__NR_getpid));
1510 	EXPECT_NE(self->mypid, syscall(__NR_getpid));
1511 }
1512 
1513 TEST_F(TRACE_syscall, syscall_dropped)
1514 {
1515 	long ret;
1516 
1517 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1518 	ASSERT_EQ(0, ret);
1519 
1520 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1521 	ASSERT_EQ(0, ret);
1522 
1523 #ifdef SYSCALL_NUM_RET_SHARE_REG
1524 	/* gettid has been skipped */
1525 	EXPECT_EQ(-1, syscall(__NR_gettid));
1526 #else
1527 	/* gettid has been skipped and an altered return value stored. */
1528 	EXPECT_EQ(1, syscall(__NR_gettid));
1529 #endif
1530 	EXPECT_NE(self->mytid, syscall(__NR_gettid));
1531 }
1532 
1533 TEST_F(TRACE_syscall, skip_after_RET_TRACE)
1534 {
1535 	struct sock_filter filter[] = {
1536 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1537 			offsetof(struct seccomp_data, nr)),
1538 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1539 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EPERM),
1540 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1541 	};
1542 	struct sock_fprog prog = {
1543 		.len = (unsigned short)ARRAY_SIZE(filter),
1544 		.filter = filter,
1545 	};
1546 	long ret;
1547 
1548 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1549 	ASSERT_EQ(0, ret);
1550 
1551 	/* Install fixture filter. */
1552 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1553 	ASSERT_EQ(0, ret);
1554 
1555 	/* Install "errno on getppid" filter. */
1556 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
1557 	ASSERT_EQ(0, ret);
1558 
1559 	/* Tracer will redirect getpid to getppid, and we should see EPERM. */
1560 	EXPECT_EQ(-1, syscall(__NR_getpid));
1561 	EXPECT_EQ(EPERM, errno);
1562 }
1563 
1564 TEST_F_SIGNAL(TRACE_syscall, kill_after_RET_TRACE, SIGSYS)
1565 {
1566 	struct sock_filter filter[] = {
1567 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1568 			offsetof(struct seccomp_data, nr)),
1569 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1570 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
1571 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1572 	};
1573 	struct sock_fprog prog = {
1574 		.len = (unsigned short)ARRAY_SIZE(filter),
1575 		.filter = filter,
1576 	};
1577 	long ret;
1578 
1579 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1580 	ASSERT_EQ(0, ret);
1581 
1582 	/* Install fixture filter. */
1583 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0);
1584 	ASSERT_EQ(0, ret);
1585 
1586 	/* Install "death on getppid" filter. */
1587 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
1588 	ASSERT_EQ(0, ret);
1589 
1590 	/* Tracer will redirect getpid to getppid, and we should die. */
1591 	EXPECT_NE(self->mypid, syscall(__NR_getpid));
1592 }
1593 
1594 TEST_F(TRACE_syscall, skip_after_ptrace)
1595 {
1596 	struct sock_filter filter[] = {
1597 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1598 			offsetof(struct seccomp_data, nr)),
1599 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1600 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EPERM),
1601 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1602 	};
1603 	struct sock_fprog prog = {
1604 		.len = (unsigned short)ARRAY_SIZE(filter),
1605 		.filter = filter,
1606 	};
1607 	long ret;
1608 
1609 	/* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1610 	teardown_trace_fixture(_metadata, self->tracer);
1611 	self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL,
1612 					   true);
1613 
1614 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1615 	ASSERT_EQ(0, ret);
1616 
1617 	/* Install "errno on getppid" filter. */
1618 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
1619 	ASSERT_EQ(0, ret);
1620 
1621 	/* Tracer will redirect getpid to getppid, and we should see EPERM. */
1622 	EXPECT_EQ(-1, syscall(__NR_getpid));
1623 	EXPECT_EQ(EPERM, errno);
1624 }
1625 
1626 TEST_F_SIGNAL(TRACE_syscall, kill_after_ptrace, SIGSYS)
1627 {
1628 	struct sock_filter filter[] = {
1629 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1630 			offsetof(struct seccomp_data, nr)),
1631 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
1632 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
1633 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1634 	};
1635 	struct sock_fprog prog = {
1636 		.len = (unsigned short)ARRAY_SIZE(filter),
1637 		.filter = filter,
1638 	};
1639 	long ret;
1640 
1641 	/* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */
1642 	teardown_trace_fixture(_metadata, self->tracer);
1643 	self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL,
1644 					   true);
1645 
1646 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1647 	ASSERT_EQ(0, ret);
1648 
1649 	/* Install "death on getppid" filter. */
1650 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
1651 	ASSERT_EQ(0, ret);
1652 
1653 	/* Tracer will redirect getpid to getppid, and we should die. */
1654 	EXPECT_NE(self->mypid, syscall(__NR_getpid));
1655 }
1656 
1657 #ifndef __NR_seccomp
1658 # if defined(__i386__)
1659 #  define __NR_seccomp 354
1660 # elif defined(__x86_64__)
1661 #  define __NR_seccomp 317
1662 # elif defined(__arm__)
1663 #  define __NR_seccomp 383
1664 # elif defined(__aarch64__)
1665 #  define __NR_seccomp 277
1666 # elif defined(__hppa__)
1667 #  define __NR_seccomp 338
1668 # elif defined(__powerpc__)
1669 #  define __NR_seccomp 358
1670 # elif defined(__s390__)
1671 #  define __NR_seccomp 348
1672 # else
1673 #  warning "seccomp syscall number unknown for this architecture"
1674 #  define __NR_seccomp 0xffff
1675 # endif
1676 #endif
1677 
1678 #ifndef SECCOMP_SET_MODE_STRICT
1679 #define SECCOMP_SET_MODE_STRICT 0
1680 #endif
1681 
1682 #ifndef SECCOMP_SET_MODE_FILTER
1683 #define SECCOMP_SET_MODE_FILTER 1
1684 #endif
1685 
1686 #ifndef SECCOMP_FILTER_FLAG_TSYNC
1687 #define SECCOMP_FILTER_FLAG_TSYNC 1
1688 #endif
1689 
1690 #ifndef seccomp
1691 int seccomp(unsigned int op, unsigned int flags, void *args)
1692 {
1693 	errno = 0;
1694 	return syscall(__NR_seccomp, op, flags, args);
1695 }
1696 #endif
1697 
1698 TEST(seccomp_syscall)
1699 {
1700 	struct sock_filter filter[] = {
1701 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1702 	};
1703 	struct sock_fprog prog = {
1704 		.len = (unsigned short)ARRAY_SIZE(filter),
1705 		.filter = filter,
1706 	};
1707 	long ret;
1708 
1709 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1710 	ASSERT_EQ(0, ret) {
1711 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
1712 	}
1713 
1714 	/* Reject insane operation. */
1715 	ret = seccomp(-1, 0, &prog);
1716 	ASSERT_NE(ENOSYS, errno) {
1717 		TH_LOG("Kernel does not support seccomp syscall!");
1718 	}
1719 	EXPECT_EQ(EINVAL, errno) {
1720 		TH_LOG("Did not reject crazy op value!");
1721 	}
1722 
1723 	/* Reject strict with flags or pointer. */
1724 	ret = seccomp(SECCOMP_SET_MODE_STRICT, -1, NULL);
1725 	EXPECT_EQ(EINVAL, errno) {
1726 		TH_LOG("Did not reject mode strict with flags!");
1727 	}
1728 	ret = seccomp(SECCOMP_SET_MODE_STRICT, 0, &prog);
1729 	EXPECT_EQ(EINVAL, errno) {
1730 		TH_LOG("Did not reject mode strict with uargs!");
1731 	}
1732 
1733 	/* Reject insane args for filter. */
1734 	ret = seccomp(SECCOMP_SET_MODE_FILTER, -1, &prog);
1735 	EXPECT_EQ(EINVAL, errno) {
1736 		TH_LOG("Did not reject crazy filter flags!");
1737 	}
1738 	ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, NULL);
1739 	EXPECT_EQ(EFAULT, errno) {
1740 		TH_LOG("Did not reject NULL filter!");
1741 	}
1742 
1743 	ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog);
1744 	EXPECT_EQ(0, errno) {
1745 		TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s",
1746 			strerror(errno));
1747 	}
1748 }
1749 
1750 TEST(seccomp_syscall_mode_lock)
1751 {
1752 	struct sock_filter filter[] = {
1753 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1754 	};
1755 	struct sock_fprog prog = {
1756 		.len = (unsigned short)ARRAY_SIZE(filter),
1757 		.filter = filter,
1758 	};
1759 	long ret;
1760 
1761 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0);
1762 	ASSERT_EQ(0, ret) {
1763 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
1764 	}
1765 
1766 	ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog);
1767 	ASSERT_NE(ENOSYS, errno) {
1768 		TH_LOG("Kernel does not support seccomp syscall!");
1769 	}
1770 	EXPECT_EQ(0, ret) {
1771 		TH_LOG("Could not install filter!");
1772 	}
1773 
1774 	/* Make sure neither entry point will switch to strict. */
1775 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, 0, 0, 0);
1776 	EXPECT_EQ(EINVAL, errno) {
1777 		TH_LOG("Switched to mode strict!");
1778 	}
1779 
1780 	ret = seccomp(SECCOMP_SET_MODE_STRICT, 0, NULL);
1781 	EXPECT_EQ(EINVAL, errno) {
1782 		TH_LOG("Switched to mode strict!");
1783 	}
1784 }
1785 
1786 TEST(TSYNC_first)
1787 {
1788 	struct sock_filter filter[] = {
1789 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1790 	};
1791 	struct sock_fprog prog = {
1792 		.len = (unsigned short)ARRAY_SIZE(filter),
1793 		.filter = filter,
1794 	};
1795 	long ret;
1796 
1797 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0);
1798 	ASSERT_EQ(0, ret) {
1799 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
1800 	}
1801 
1802 	ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
1803 		      &prog);
1804 	ASSERT_NE(ENOSYS, errno) {
1805 		TH_LOG("Kernel does not support seccomp syscall!");
1806 	}
1807 	EXPECT_EQ(0, ret) {
1808 		TH_LOG("Could not install initial filter with TSYNC!");
1809 	}
1810 }
1811 
1812 #define TSYNC_SIBLINGS 2
1813 struct tsync_sibling {
1814 	pthread_t tid;
1815 	pid_t system_tid;
1816 	sem_t *started;
1817 	pthread_cond_t *cond;
1818 	pthread_mutex_t *mutex;
1819 	int diverge;
1820 	int num_waits;
1821 	struct sock_fprog *prog;
1822 	struct __test_metadata *metadata;
1823 };
1824 
1825 FIXTURE_DATA(TSYNC) {
1826 	struct sock_fprog root_prog, apply_prog;
1827 	struct tsync_sibling sibling[TSYNC_SIBLINGS];
1828 	sem_t started;
1829 	pthread_cond_t cond;
1830 	pthread_mutex_t mutex;
1831 	int sibling_count;
1832 };
1833 
1834 FIXTURE_SETUP(TSYNC)
1835 {
1836 	struct sock_filter root_filter[] = {
1837 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1838 	};
1839 	struct sock_filter apply_filter[] = {
1840 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1841 			offsetof(struct seccomp_data, nr)),
1842 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1),
1843 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
1844 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1845 	};
1846 
1847 	memset(&self->root_prog, 0, sizeof(self->root_prog));
1848 	memset(&self->apply_prog, 0, sizeof(self->apply_prog));
1849 	memset(&self->sibling, 0, sizeof(self->sibling));
1850 	self->root_prog.filter = malloc(sizeof(root_filter));
1851 	ASSERT_NE(NULL, self->root_prog.filter);
1852 	memcpy(self->root_prog.filter, &root_filter, sizeof(root_filter));
1853 	self->root_prog.len = (unsigned short)ARRAY_SIZE(root_filter);
1854 
1855 	self->apply_prog.filter = malloc(sizeof(apply_filter));
1856 	ASSERT_NE(NULL, self->apply_prog.filter);
1857 	memcpy(self->apply_prog.filter, &apply_filter, sizeof(apply_filter));
1858 	self->apply_prog.len = (unsigned short)ARRAY_SIZE(apply_filter);
1859 
1860 	self->sibling_count = 0;
1861 	pthread_mutex_init(&self->mutex, NULL);
1862 	pthread_cond_init(&self->cond, NULL);
1863 	sem_init(&self->started, 0, 0);
1864 	self->sibling[0].tid = 0;
1865 	self->sibling[0].cond = &self->cond;
1866 	self->sibling[0].started = &self->started;
1867 	self->sibling[0].mutex = &self->mutex;
1868 	self->sibling[0].diverge = 0;
1869 	self->sibling[0].num_waits = 1;
1870 	self->sibling[0].prog = &self->root_prog;
1871 	self->sibling[0].metadata = _metadata;
1872 	self->sibling[1].tid = 0;
1873 	self->sibling[1].cond = &self->cond;
1874 	self->sibling[1].started = &self->started;
1875 	self->sibling[1].mutex = &self->mutex;
1876 	self->sibling[1].diverge = 0;
1877 	self->sibling[1].prog = &self->root_prog;
1878 	self->sibling[1].num_waits = 1;
1879 	self->sibling[1].metadata = _metadata;
1880 }
1881 
1882 FIXTURE_TEARDOWN(TSYNC)
1883 {
1884 	int sib = 0;
1885 
1886 	if (self->root_prog.filter)
1887 		free(self->root_prog.filter);
1888 	if (self->apply_prog.filter)
1889 		free(self->apply_prog.filter);
1890 
1891 	for ( ; sib < self->sibling_count; ++sib) {
1892 		struct tsync_sibling *s = &self->sibling[sib];
1893 		void *status;
1894 
1895 		if (!s->tid)
1896 			continue;
1897 		if (pthread_kill(s->tid, 0)) {
1898 			pthread_cancel(s->tid);
1899 			pthread_join(s->tid, &status);
1900 		}
1901 	}
1902 	pthread_mutex_destroy(&self->mutex);
1903 	pthread_cond_destroy(&self->cond);
1904 	sem_destroy(&self->started);
1905 }
1906 
1907 void *tsync_sibling(void *data)
1908 {
1909 	long ret = 0;
1910 	struct tsync_sibling *me = data;
1911 
1912 	me->system_tid = syscall(__NR_gettid);
1913 
1914 	pthread_mutex_lock(me->mutex);
1915 	if (me->diverge) {
1916 		/* Just re-apply the root prog to fork the tree */
1917 		ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1918 				me->prog, 0, 0);
1919 	}
1920 	sem_post(me->started);
1921 	/* Return outside of started so parent notices failures. */
1922 	if (ret) {
1923 		pthread_mutex_unlock(me->mutex);
1924 		return (void *)SIBLING_EXIT_FAILURE;
1925 	}
1926 	do {
1927 		pthread_cond_wait(me->cond, me->mutex);
1928 		me->num_waits = me->num_waits - 1;
1929 	} while (me->num_waits);
1930 	pthread_mutex_unlock(me->mutex);
1931 
1932 	ret = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
1933 	if (!ret)
1934 		return (void *)SIBLING_EXIT_NEWPRIVS;
1935 	read(0, NULL, 0);
1936 	return (void *)SIBLING_EXIT_UNKILLED;
1937 }
1938 
1939 void tsync_start_sibling(struct tsync_sibling *sibling)
1940 {
1941 	pthread_create(&sibling->tid, NULL, tsync_sibling, (void *)sibling);
1942 }
1943 
1944 TEST_F(TSYNC, siblings_fail_prctl)
1945 {
1946 	long ret;
1947 	void *status;
1948 	struct sock_filter filter[] = {
1949 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
1950 			offsetof(struct seccomp_data, nr)),
1951 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1),
1952 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EINVAL),
1953 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
1954 	};
1955 	struct sock_fprog prog = {
1956 		.len = (unsigned short)ARRAY_SIZE(filter),
1957 		.filter = filter,
1958 	};
1959 
1960 	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
1961 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
1962 	}
1963 
1964 	/* Check prctl failure detection by requesting sib 0 diverge. */
1965 	ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog);
1966 	ASSERT_NE(ENOSYS, errno) {
1967 		TH_LOG("Kernel does not support seccomp syscall!");
1968 	}
1969 	ASSERT_EQ(0, ret) {
1970 		TH_LOG("setting filter failed");
1971 	}
1972 
1973 	self->sibling[0].diverge = 1;
1974 	tsync_start_sibling(&self->sibling[0]);
1975 	tsync_start_sibling(&self->sibling[1]);
1976 
1977 	while (self->sibling_count < TSYNC_SIBLINGS) {
1978 		sem_wait(&self->started);
1979 		self->sibling_count++;
1980 	}
1981 
1982 	/* Signal the threads to clean up*/
1983 	pthread_mutex_lock(&self->mutex);
1984 	ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
1985 		TH_LOG("cond broadcast non-zero");
1986 	}
1987 	pthread_mutex_unlock(&self->mutex);
1988 
1989 	/* Ensure diverging sibling failed to call prctl. */
1990 	pthread_join(self->sibling[0].tid, &status);
1991 	EXPECT_EQ(SIBLING_EXIT_FAILURE, (long)status);
1992 	pthread_join(self->sibling[1].tid, &status);
1993 	EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
1994 }
1995 
1996 TEST_F(TSYNC, two_siblings_with_ancestor)
1997 {
1998 	long ret;
1999 	void *status;
2000 
2001 	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2002 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2003 	}
2004 
2005 	ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog);
2006 	ASSERT_NE(ENOSYS, errno) {
2007 		TH_LOG("Kernel does not support seccomp syscall!");
2008 	}
2009 	ASSERT_EQ(0, ret) {
2010 		TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2011 	}
2012 	tsync_start_sibling(&self->sibling[0]);
2013 	tsync_start_sibling(&self->sibling[1]);
2014 
2015 	while (self->sibling_count < TSYNC_SIBLINGS) {
2016 		sem_wait(&self->started);
2017 		self->sibling_count++;
2018 	}
2019 
2020 	ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2021 		      &self->apply_prog);
2022 	ASSERT_EQ(0, ret) {
2023 		TH_LOG("Could install filter on all threads!");
2024 	}
2025 	/* Tell the siblings to test the policy */
2026 	pthread_mutex_lock(&self->mutex);
2027 	ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2028 		TH_LOG("cond broadcast non-zero");
2029 	}
2030 	pthread_mutex_unlock(&self->mutex);
2031 	/* Ensure they are both killed and don't exit cleanly. */
2032 	pthread_join(self->sibling[0].tid, &status);
2033 	EXPECT_EQ(0x0, (long)status);
2034 	pthread_join(self->sibling[1].tid, &status);
2035 	EXPECT_EQ(0x0, (long)status);
2036 }
2037 
2038 TEST_F(TSYNC, two_sibling_want_nnp)
2039 {
2040 	void *status;
2041 
2042 	/* start siblings before any prctl() operations */
2043 	tsync_start_sibling(&self->sibling[0]);
2044 	tsync_start_sibling(&self->sibling[1]);
2045 	while (self->sibling_count < TSYNC_SIBLINGS) {
2046 		sem_wait(&self->started);
2047 		self->sibling_count++;
2048 	}
2049 
2050 	/* Tell the siblings to test no policy */
2051 	pthread_mutex_lock(&self->mutex);
2052 	ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2053 		TH_LOG("cond broadcast non-zero");
2054 	}
2055 	pthread_mutex_unlock(&self->mutex);
2056 
2057 	/* Ensure they are both upset about lacking nnp. */
2058 	pthread_join(self->sibling[0].tid, &status);
2059 	EXPECT_EQ(SIBLING_EXIT_NEWPRIVS, (long)status);
2060 	pthread_join(self->sibling[1].tid, &status);
2061 	EXPECT_EQ(SIBLING_EXIT_NEWPRIVS, (long)status);
2062 }
2063 
2064 TEST_F(TSYNC, two_siblings_with_no_filter)
2065 {
2066 	long ret;
2067 	void *status;
2068 
2069 	/* start siblings before any prctl() operations */
2070 	tsync_start_sibling(&self->sibling[0]);
2071 	tsync_start_sibling(&self->sibling[1]);
2072 	while (self->sibling_count < TSYNC_SIBLINGS) {
2073 		sem_wait(&self->started);
2074 		self->sibling_count++;
2075 	}
2076 
2077 	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2078 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2079 	}
2080 
2081 	ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2082 		      &self->apply_prog);
2083 	ASSERT_NE(ENOSYS, errno) {
2084 		TH_LOG("Kernel does not support seccomp syscall!");
2085 	}
2086 	ASSERT_EQ(0, ret) {
2087 		TH_LOG("Could install filter on all threads!");
2088 	}
2089 
2090 	/* Tell the siblings to test the policy */
2091 	pthread_mutex_lock(&self->mutex);
2092 	ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2093 		TH_LOG("cond broadcast non-zero");
2094 	}
2095 	pthread_mutex_unlock(&self->mutex);
2096 
2097 	/* Ensure they are both killed and don't exit cleanly. */
2098 	pthread_join(self->sibling[0].tid, &status);
2099 	EXPECT_EQ(0x0, (long)status);
2100 	pthread_join(self->sibling[1].tid, &status);
2101 	EXPECT_EQ(0x0, (long)status);
2102 }
2103 
2104 TEST_F(TSYNC, two_siblings_with_one_divergence)
2105 {
2106 	long ret;
2107 	void *status;
2108 
2109 	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2110 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2111 	}
2112 
2113 	ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog);
2114 	ASSERT_NE(ENOSYS, errno) {
2115 		TH_LOG("Kernel does not support seccomp syscall!");
2116 	}
2117 	ASSERT_EQ(0, ret) {
2118 		TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2119 	}
2120 	self->sibling[0].diverge = 1;
2121 	tsync_start_sibling(&self->sibling[0]);
2122 	tsync_start_sibling(&self->sibling[1]);
2123 
2124 	while (self->sibling_count < TSYNC_SIBLINGS) {
2125 		sem_wait(&self->started);
2126 		self->sibling_count++;
2127 	}
2128 
2129 	ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2130 		      &self->apply_prog);
2131 	ASSERT_EQ(self->sibling[0].system_tid, ret) {
2132 		TH_LOG("Did not fail on diverged sibling.");
2133 	}
2134 
2135 	/* Wake the threads */
2136 	pthread_mutex_lock(&self->mutex);
2137 	ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2138 		TH_LOG("cond broadcast non-zero");
2139 	}
2140 	pthread_mutex_unlock(&self->mutex);
2141 
2142 	/* Ensure they are both unkilled. */
2143 	pthread_join(self->sibling[0].tid, &status);
2144 	EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
2145 	pthread_join(self->sibling[1].tid, &status);
2146 	EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
2147 }
2148 
2149 TEST_F(TSYNC, two_siblings_not_under_filter)
2150 {
2151 	long ret, sib;
2152 	void *status;
2153 
2154 	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2155 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2156 	}
2157 
2158 	/*
2159 	 * Sibling 0 will have its own seccomp policy
2160 	 * and Sibling 1 will not be under seccomp at
2161 	 * all. Sibling 1 will enter seccomp and 0
2162 	 * will cause failure.
2163 	 */
2164 	self->sibling[0].diverge = 1;
2165 	tsync_start_sibling(&self->sibling[0]);
2166 	tsync_start_sibling(&self->sibling[1]);
2167 
2168 	while (self->sibling_count < TSYNC_SIBLINGS) {
2169 		sem_wait(&self->started);
2170 		self->sibling_count++;
2171 	}
2172 
2173 	ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog);
2174 	ASSERT_NE(ENOSYS, errno) {
2175 		TH_LOG("Kernel does not support seccomp syscall!");
2176 	}
2177 	ASSERT_EQ(0, ret) {
2178 		TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2179 	}
2180 
2181 	ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2182 		      &self->apply_prog);
2183 	ASSERT_EQ(ret, self->sibling[0].system_tid) {
2184 		TH_LOG("Did not fail on diverged sibling.");
2185 	}
2186 	sib = 1;
2187 	if (ret == self->sibling[0].system_tid)
2188 		sib = 0;
2189 
2190 	pthread_mutex_lock(&self->mutex);
2191 
2192 	/* Increment the other siblings num_waits so we can clean up
2193 	 * the one we just saw.
2194 	 */
2195 	self->sibling[!sib].num_waits += 1;
2196 
2197 	/* Signal the thread to clean up*/
2198 	ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2199 		TH_LOG("cond broadcast non-zero");
2200 	}
2201 	pthread_mutex_unlock(&self->mutex);
2202 	pthread_join(self->sibling[sib].tid, &status);
2203 	EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
2204 	/* Poll for actual task death. pthread_join doesn't guarantee it. */
2205 	while (!kill(self->sibling[sib].system_tid, 0))
2206 		sleep(0.1);
2207 	/* Switch to the remaining sibling */
2208 	sib = !sib;
2209 
2210 	ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2211 		      &self->apply_prog);
2212 	ASSERT_EQ(0, ret) {
2213 		TH_LOG("Expected the remaining sibling to sync");
2214 	};
2215 
2216 	pthread_mutex_lock(&self->mutex);
2217 
2218 	/* If remaining sibling didn't have a chance to wake up during
2219 	 * the first broadcast, manually reduce the num_waits now.
2220 	 */
2221 	if (self->sibling[sib].num_waits > 1)
2222 		self->sibling[sib].num_waits = 1;
2223 	ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2224 		TH_LOG("cond broadcast non-zero");
2225 	}
2226 	pthread_mutex_unlock(&self->mutex);
2227 	pthread_join(self->sibling[sib].tid, &status);
2228 	EXPECT_EQ(0, (long)status);
2229 	/* Poll for actual task death. pthread_join doesn't guarantee it. */
2230 	while (!kill(self->sibling[sib].system_tid, 0))
2231 		sleep(0.1);
2232 
2233 	ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
2234 		      &self->apply_prog);
2235 	ASSERT_EQ(0, ret);  /* just us chickens */
2236 }
2237 
2238 /* Make sure restarted syscalls are seen directly as "restart_syscall". */
2239 TEST(syscall_restart)
2240 {
2241 	long ret;
2242 	unsigned long msg;
2243 	pid_t child_pid;
2244 	int pipefd[2];
2245 	int status;
2246 	siginfo_t info = { };
2247 	struct sock_filter filter[] = {
2248 		BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
2249 			 offsetof(struct seccomp_data, nr)),
2250 
2251 #ifdef __NR_sigreturn
2252 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_sigreturn, 6, 0),
2253 #endif
2254 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 5, 0),
2255 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_exit, 4, 0),
2256 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_rt_sigreturn, 3, 0),
2257 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_nanosleep, 4, 0),
2258 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_restart_syscall, 4, 0),
2259 
2260 		/* Allow __NR_write for easy logging. */
2261 		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_write, 0, 1),
2262 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2263 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL),
2264 		/* The nanosleep jump target. */
2265 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE|0x100),
2266 		/* The restart_syscall jump target. */
2267 		BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE|0x200),
2268 	};
2269 	struct sock_fprog prog = {
2270 		.len = (unsigned short)ARRAY_SIZE(filter),
2271 		.filter = filter,
2272 	};
2273 #if defined(__arm__)
2274 	struct utsname utsbuf;
2275 #endif
2276 
2277 	ASSERT_EQ(0, pipe(pipefd));
2278 
2279 	child_pid = fork();
2280 	ASSERT_LE(0, child_pid);
2281 	if (child_pid == 0) {
2282 		/* Child uses EXPECT not ASSERT to deliver status correctly. */
2283 		char buf = ' ';
2284 		struct timespec timeout = { };
2285 
2286 		/* Attach parent as tracer and stop. */
2287 		EXPECT_EQ(0, ptrace(PTRACE_TRACEME));
2288 		EXPECT_EQ(0, raise(SIGSTOP));
2289 
2290 		EXPECT_EQ(0, close(pipefd[1]));
2291 
2292 		EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2293 			TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2294 		}
2295 
2296 		ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
2297 		EXPECT_EQ(0, ret) {
2298 			TH_LOG("Failed to install filter!");
2299 		}
2300 
2301 		EXPECT_EQ(1, read(pipefd[0], &buf, 1)) {
2302 			TH_LOG("Failed to read() sync from parent");
2303 		}
2304 		EXPECT_EQ('.', buf) {
2305 			TH_LOG("Failed to get sync data from read()");
2306 		}
2307 
2308 		/* Start nanosleep to be interrupted. */
2309 		timeout.tv_sec = 1;
2310 		errno = 0;
2311 		EXPECT_EQ(0, nanosleep(&timeout, NULL)) {
2312 			TH_LOG("Call to nanosleep() failed (errno %d)", errno);
2313 		}
2314 
2315 		/* Read final sync from parent. */
2316 		EXPECT_EQ(1, read(pipefd[0], &buf, 1)) {
2317 			TH_LOG("Failed final read() from parent");
2318 		}
2319 		EXPECT_EQ('!', buf) {
2320 			TH_LOG("Failed to get final data from read()");
2321 		}
2322 
2323 		/* Directly report the status of our test harness results. */
2324 		syscall(__NR_exit, _metadata->passed ? EXIT_SUCCESS
2325 						     : EXIT_FAILURE);
2326 	}
2327 	EXPECT_EQ(0, close(pipefd[0]));
2328 
2329 	/* Attach to child, setup options, and release. */
2330 	ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2331 	ASSERT_EQ(true, WIFSTOPPED(status));
2332 	ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS, child_pid, NULL,
2333 			    PTRACE_O_TRACESECCOMP));
2334 	ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2335 	ASSERT_EQ(1, write(pipefd[1], ".", 1));
2336 
2337 	/* Wait for nanosleep() to start. */
2338 	ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2339 	ASSERT_EQ(true, WIFSTOPPED(status));
2340 	ASSERT_EQ(SIGTRAP, WSTOPSIG(status));
2341 	ASSERT_EQ(PTRACE_EVENT_SECCOMP, (status >> 16));
2342 	ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG, child_pid, NULL, &msg));
2343 	ASSERT_EQ(0x100, msg);
2344 	EXPECT_EQ(__NR_nanosleep, get_syscall(_metadata, child_pid));
2345 
2346 	/* Might as well check siginfo for sanity while we're here. */
2347 	ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child_pid, NULL, &info));
2348 	ASSERT_EQ(SIGTRAP, info.si_signo);
2349 	ASSERT_EQ(SIGTRAP | (PTRACE_EVENT_SECCOMP << 8), info.si_code);
2350 	EXPECT_EQ(0, info.si_errno);
2351 	EXPECT_EQ(getuid(), info.si_uid);
2352 	/* Verify signal delivery came from child (seccomp-triggered). */
2353 	EXPECT_EQ(child_pid, info.si_pid);
2354 
2355 	/* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */
2356 	ASSERT_EQ(0, kill(child_pid, SIGSTOP));
2357 	ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2358 	ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2359 	ASSERT_EQ(true, WIFSTOPPED(status));
2360 	ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
2361 	/* Verify signal delivery came from parent now. */
2362 	ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child_pid, NULL, &info));
2363 	EXPECT_EQ(getpid(), info.si_pid);
2364 
2365 	/* Restart nanosleep with SIGCONT, which triggers restart_syscall. */
2366 	ASSERT_EQ(0, kill(child_pid, SIGCONT));
2367 	ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2368 	ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2369 	ASSERT_EQ(true, WIFSTOPPED(status));
2370 	ASSERT_EQ(SIGCONT, WSTOPSIG(status));
2371 	ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2372 
2373 	/* Wait for restart_syscall() to start. */
2374 	ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2375 	ASSERT_EQ(true, WIFSTOPPED(status));
2376 	ASSERT_EQ(SIGTRAP, WSTOPSIG(status));
2377 	ASSERT_EQ(PTRACE_EVENT_SECCOMP, (status >> 16));
2378 	ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG, child_pid, NULL, &msg));
2379 
2380 	ASSERT_EQ(0x200, msg);
2381 	ret = get_syscall(_metadata, child_pid);
2382 #if defined(__arm__)
2383 	/*
2384 	 * FIXME:
2385 	 * - native ARM registers do NOT expose true syscall.
2386 	 * - compat ARM registers on ARM64 DO expose true syscall.
2387 	 */
2388 	ASSERT_EQ(0, uname(&utsbuf));
2389 	if (strncmp(utsbuf.machine, "arm", 3) == 0) {
2390 		EXPECT_EQ(__NR_nanosleep, ret);
2391 	} else
2392 #endif
2393 	{
2394 		EXPECT_EQ(__NR_restart_syscall, ret);
2395 	}
2396 
2397 	/* Write again to end test. */
2398 	ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0));
2399 	ASSERT_EQ(1, write(pipefd[1], "!", 1));
2400 	EXPECT_EQ(0, close(pipefd[1]));
2401 
2402 	ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
2403 	if (WIFSIGNALED(status) || WEXITSTATUS(status))
2404 		_metadata->passed = 0;
2405 }
2406 
2407 /*
2408  * TODO:
2409  * - add microbenchmarks
2410  * - expand NNP testing
2411  * - better arch-specific TRACE and TRAP handlers.
2412  * - endianness checking when appropriate
2413  * - 64-bit arg prodding
2414  * - arch value testing (x86 modes especially)
2415  * - ...
2416  */
2417 
2418 TEST_HARNESS_MAIN
2419