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