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