1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 4 * 5 * Test code for seccomp bpf. 6 */ 7 8 #define _GNU_SOURCE 9 #include <sys/types.h> 10 11 /* 12 * glibc 2.26 and later have SIGSYS in siginfo_t. Before that, 13 * we need to use the kernel's siginfo.h file and trick glibc 14 * into accepting it. 15 */ 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 23 #include <errno.h> 24 #include <linux/filter.h> 25 #include <sys/prctl.h> 26 #include <sys/ptrace.h> 27 #include <sys/user.h> 28 #include <linux/prctl.h> 29 #include <linux/ptrace.h> 30 #include <linux/seccomp.h> 31 #include <pthread.h> 32 #include <semaphore.h> 33 #include <signal.h> 34 #include <stddef.h> 35 #include <stdbool.h> 36 #include <string.h> 37 #include <time.h> 38 #include <limits.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 #include <sys/socket.h> 46 #include <sys/ioctl.h> 47 #include <linux/kcmp.h> 48 #include <sys/resource.h> 49 50 #include <unistd.h> 51 #include <sys/syscall.h> 52 #include <poll.h> 53 54 #include "../kselftest_harness.h" 55 #include "../clone3/clone3_selftests.h" 56 57 /* Attempt to de-conflict with the selftests tree. */ 58 #ifndef SKIP 59 #define SKIP(s, ...) XFAIL(s, ##__VA_ARGS__) 60 #endif 61 62 #ifndef PR_SET_PTRACER 63 # define PR_SET_PTRACER 0x59616d61 64 #endif 65 66 #ifndef PR_SET_NO_NEW_PRIVS 67 #define PR_SET_NO_NEW_PRIVS 38 68 #define PR_GET_NO_NEW_PRIVS 39 69 #endif 70 71 #ifndef PR_SECCOMP_EXT 72 #define PR_SECCOMP_EXT 43 73 #endif 74 75 #ifndef SECCOMP_EXT_ACT 76 #define SECCOMP_EXT_ACT 1 77 #endif 78 79 #ifndef SECCOMP_EXT_ACT_TSYNC 80 #define SECCOMP_EXT_ACT_TSYNC 1 81 #endif 82 83 #ifndef SECCOMP_MODE_STRICT 84 #define SECCOMP_MODE_STRICT 1 85 #endif 86 87 #ifndef SECCOMP_MODE_FILTER 88 #define SECCOMP_MODE_FILTER 2 89 #endif 90 91 #ifndef SECCOMP_RET_ALLOW 92 struct seccomp_data { 93 int nr; 94 __u32 arch; 95 __u64 instruction_pointer; 96 __u64 args[6]; 97 }; 98 #endif 99 100 #ifndef SECCOMP_RET_KILL_PROCESS 101 #define SECCOMP_RET_KILL_PROCESS 0x80000000U /* kill the process */ 102 #define SECCOMP_RET_KILL_THREAD 0x00000000U /* kill the thread */ 103 #endif 104 #ifndef SECCOMP_RET_KILL 105 #define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD 106 #define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */ 107 #define SECCOMP_RET_ERRNO 0x00050000U /* returns an errno */ 108 #define SECCOMP_RET_TRACE 0x7ff00000U /* pass to a tracer or disallow */ 109 #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */ 110 #endif 111 #ifndef SECCOMP_RET_LOG 112 #define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */ 113 #endif 114 115 #ifndef __NR_seccomp 116 # if defined(__i386__) 117 # define __NR_seccomp 354 118 # elif defined(__x86_64__) 119 # define __NR_seccomp 317 120 # elif defined(__arm__) 121 # define __NR_seccomp 383 122 # elif defined(__aarch64__) 123 # define __NR_seccomp 277 124 # elif defined(__riscv) 125 # define __NR_seccomp 277 126 # elif defined(__csky__) 127 # define __NR_seccomp 277 128 # elif defined(__hppa__) 129 # define __NR_seccomp 338 130 # elif defined(__powerpc__) 131 # define __NR_seccomp 358 132 # elif defined(__s390__) 133 # define __NR_seccomp 348 134 # elif defined(__xtensa__) 135 # define __NR_seccomp 337 136 # elif defined(__sh__) 137 # define __NR_seccomp 372 138 # else 139 # warning "seccomp syscall number unknown for this architecture" 140 # define __NR_seccomp 0xffff 141 # endif 142 #endif 143 144 #ifndef SECCOMP_SET_MODE_STRICT 145 #define SECCOMP_SET_MODE_STRICT 0 146 #endif 147 148 #ifndef SECCOMP_SET_MODE_FILTER 149 #define SECCOMP_SET_MODE_FILTER 1 150 #endif 151 152 #ifndef SECCOMP_GET_ACTION_AVAIL 153 #define SECCOMP_GET_ACTION_AVAIL 2 154 #endif 155 156 #ifndef SECCOMP_GET_NOTIF_SIZES 157 #define SECCOMP_GET_NOTIF_SIZES 3 158 #endif 159 160 #ifndef SECCOMP_FILTER_FLAG_TSYNC 161 #define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0) 162 #endif 163 164 #ifndef SECCOMP_FILTER_FLAG_LOG 165 #define SECCOMP_FILTER_FLAG_LOG (1UL << 1) 166 #endif 167 168 #ifndef SECCOMP_FILTER_FLAG_SPEC_ALLOW 169 #define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2) 170 #endif 171 172 #ifndef PTRACE_SECCOMP_GET_METADATA 173 #define PTRACE_SECCOMP_GET_METADATA 0x420d 174 175 struct seccomp_metadata { 176 __u64 filter_off; /* Input: which filter */ 177 __u64 flags; /* Output: filter's flags */ 178 }; 179 #endif 180 181 #ifndef SECCOMP_FILTER_FLAG_NEW_LISTENER 182 #define SECCOMP_FILTER_FLAG_NEW_LISTENER (1UL << 3) 183 #endif 184 185 #ifndef SECCOMP_RET_USER_NOTIF 186 #define SECCOMP_RET_USER_NOTIF 0x7fc00000U 187 188 #define SECCOMP_IOC_MAGIC '!' 189 #define SECCOMP_IO(nr) _IO(SECCOMP_IOC_MAGIC, nr) 190 #define SECCOMP_IOR(nr, type) _IOR(SECCOMP_IOC_MAGIC, nr, type) 191 #define SECCOMP_IOW(nr, type) _IOW(SECCOMP_IOC_MAGIC, nr, type) 192 #define SECCOMP_IOWR(nr, type) _IOWR(SECCOMP_IOC_MAGIC, nr, type) 193 194 /* Flags for seccomp notification fd ioctl. */ 195 #define SECCOMP_IOCTL_NOTIF_RECV SECCOMP_IOWR(0, struct seccomp_notif) 196 #define SECCOMP_IOCTL_NOTIF_SEND SECCOMP_IOWR(1, \ 197 struct seccomp_notif_resp) 198 #define SECCOMP_IOCTL_NOTIF_ID_VALID SECCOMP_IOW(2, __u64) 199 200 struct seccomp_notif { 201 __u64 id; 202 __u32 pid; 203 __u32 flags; 204 struct seccomp_data data; 205 }; 206 207 struct seccomp_notif_resp { 208 __u64 id; 209 __s64 val; 210 __s32 error; 211 __u32 flags; 212 }; 213 214 struct seccomp_notif_sizes { 215 __u16 seccomp_notif; 216 __u16 seccomp_notif_resp; 217 __u16 seccomp_data; 218 }; 219 #endif 220 221 #ifndef SECCOMP_IOCTL_NOTIF_ADDFD 222 /* On success, the return value is the remote process's added fd number */ 223 #define SECCOMP_IOCTL_NOTIF_ADDFD SECCOMP_IOW(3, \ 224 struct seccomp_notif_addfd) 225 226 /* valid flags for seccomp_notif_addfd */ 227 #define SECCOMP_ADDFD_FLAG_SETFD (1UL << 0) /* Specify remote fd */ 228 229 struct seccomp_notif_addfd { 230 __u64 id; 231 __u32 flags; 232 __u32 srcfd; 233 __u32 newfd; 234 __u32 newfd_flags; 235 }; 236 #endif 237 238 struct seccomp_notif_addfd_small { 239 __u64 id; 240 char weird[4]; 241 }; 242 #define SECCOMP_IOCTL_NOTIF_ADDFD_SMALL \ 243 SECCOMP_IOW(3, struct seccomp_notif_addfd_small) 244 245 struct seccomp_notif_addfd_big { 246 union { 247 struct seccomp_notif_addfd addfd; 248 char buf[sizeof(struct seccomp_notif_addfd) + 8]; 249 }; 250 }; 251 #define SECCOMP_IOCTL_NOTIF_ADDFD_BIG \ 252 SECCOMP_IOWR(3, struct seccomp_notif_addfd_big) 253 254 #ifndef PTRACE_EVENTMSG_SYSCALL_ENTRY 255 #define PTRACE_EVENTMSG_SYSCALL_ENTRY 1 256 #define PTRACE_EVENTMSG_SYSCALL_EXIT 2 257 #endif 258 259 #ifndef SECCOMP_USER_NOTIF_FLAG_CONTINUE 260 #define SECCOMP_USER_NOTIF_FLAG_CONTINUE 0x00000001 261 #endif 262 263 #ifndef SECCOMP_FILTER_FLAG_TSYNC_ESRCH 264 #define SECCOMP_FILTER_FLAG_TSYNC_ESRCH (1UL << 4) 265 #endif 266 267 #ifndef seccomp 268 int seccomp(unsigned int op, unsigned int flags, void *args) 269 { 270 errno = 0; 271 return syscall(__NR_seccomp, op, flags, args); 272 } 273 #endif 274 275 #if __BYTE_ORDER == __LITTLE_ENDIAN 276 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n])) 277 #elif __BYTE_ORDER == __BIG_ENDIAN 278 #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]) + sizeof(__u32)) 279 #else 280 #error "wut? Unknown __BYTE_ORDER?!" 281 #endif 282 283 #define SIBLING_EXIT_UNKILLED 0xbadbeef 284 #define SIBLING_EXIT_FAILURE 0xbadface 285 #define SIBLING_EXIT_NEWPRIVS 0xbadfeed 286 287 static int __filecmp(pid_t pid1, pid_t pid2, int fd1, int fd2) 288 { 289 #ifdef __NR_kcmp 290 errno = 0; 291 return syscall(__NR_kcmp, pid1, pid2, KCMP_FILE, fd1, fd2); 292 #else 293 errno = ENOSYS; 294 return -1; 295 #endif 296 } 297 298 /* Have TH_LOG report actual location filecmp() is used. */ 299 #define filecmp(pid1, pid2, fd1, fd2) ({ \ 300 int _ret; \ 301 \ 302 _ret = __filecmp(pid1, pid2, fd1, fd2); \ 303 if (_ret != 0) { \ 304 if (_ret < 0 && errno == ENOSYS) { \ 305 TH_LOG("kcmp() syscall missing (test is less accurate)");\ 306 _ret = 0; \ 307 } \ 308 } \ 309 _ret; }) 310 311 TEST(kcmp) 312 { 313 int ret; 314 315 ret = __filecmp(getpid(), getpid(), 1, 1); 316 EXPECT_EQ(ret, 0); 317 if (ret != 0 && errno == ENOSYS) 318 SKIP(return, "Kernel does not support kcmp() (missing CONFIG_KCMP?)"); 319 } 320 321 TEST(mode_strict_support) 322 { 323 long ret; 324 325 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, NULL, NULL); 326 ASSERT_EQ(0, ret) { 327 TH_LOG("Kernel does not support CONFIG_SECCOMP"); 328 } 329 syscall(__NR_exit, 0); 330 } 331 332 TEST_SIGNAL(mode_strict_cannot_call_prctl, SIGKILL) 333 { 334 long ret; 335 336 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, NULL, NULL); 337 ASSERT_EQ(0, ret) { 338 TH_LOG("Kernel does not support CONFIG_SECCOMP"); 339 } 340 syscall(__NR_prctl, PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 341 NULL, NULL, NULL); 342 EXPECT_FALSE(true) { 343 TH_LOG("Unreachable!"); 344 } 345 } 346 347 /* Note! This doesn't test no new privs behavior */ 348 TEST(no_new_privs_support) 349 { 350 long ret; 351 352 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 353 EXPECT_EQ(0, ret) { 354 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 355 } 356 } 357 358 /* Tests kernel support by checking for a copy_from_user() fault on NULL. */ 359 TEST(mode_filter_support) 360 { 361 long ret; 362 363 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0); 364 ASSERT_EQ(0, ret) { 365 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 366 } 367 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL, NULL, NULL); 368 EXPECT_EQ(-1, ret); 369 EXPECT_EQ(EFAULT, errno) { 370 TH_LOG("Kernel does not support CONFIG_SECCOMP_FILTER!"); 371 } 372 } 373 374 TEST(mode_filter_without_nnp) 375 { 376 struct sock_filter filter[] = { 377 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 378 }; 379 struct sock_fprog prog = { 380 .len = (unsigned short)ARRAY_SIZE(filter), 381 .filter = filter, 382 }; 383 long ret; 384 385 ret = prctl(PR_GET_NO_NEW_PRIVS, 0, NULL, 0, 0); 386 ASSERT_LE(0, ret) { 387 TH_LOG("Expected 0 or unsupported for NO_NEW_PRIVS"); 388 } 389 errno = 0; 390 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 391 /* Succeeds with CAP_SYS_ADMIN, fails without */ 392 /* TODO(wad) check caps not euid */ 393 if (geteuid()) { 394 EXPECT_EQ(-1, ret); 395 EXPECT_EQ(EACCES, errno); 396 } else { 397 EXPECT_EQ(0, ret); 398 } 399 } 400 401 #define MAX_INSNS_PER_PATH 32768 402 403 TEST(filter_size_limits) 404 { 405 int i; 406 int count = BPF_MAXINSNS + 1; 407 struct sock_filter allow[] = { 408 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 409 }; 410 struct sock_filter *filter; 411 struct sock_fprog prog = { }; 412 long ret; 413 414 filter = calloc(count, sizeof(*filter)); 415 ASSERT_NE(NULL, filter); 416 417 for (i = 0; i < count; i++) 418 filter[i] = allow[0]; 419 420 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 421 ASSERT_EQ(0, ret); 422 423 prog.filter = filter; 424 prog.len = count; 425 426 /* Too many filter instructions in a single filter. */ 427 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 428 ASSERT_NE(0, ret) { 429 TH_LOG("Installing %d insn filter was allowed", prog.len); 430 } 431 432 /* One less is okay, though. */ 433 prog.len -= 1; 434 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 435 ASSERT_EQ(0, ret) { 436 TH_LOG("Installing %d insn filter wasn't allowed", prog.len); 437 } 438 } 439 440 TEST(filter_chain_limits) 441 { 442 int i; 443 int count = BPF_MAXINSNS; 444 struct sock_filter allow[] = { 445 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 446 }; 447 struct sock_filter *filter; 448 struct sock_fprog prog = { }; 449 long ret; 450 451 filter = calloc(count, sizeof(*filter)); 452 ASSERT_NE(NULL, filter); 453 454 for (i = 0; i < count; i++) 455 filter[i] = allow[0]; 456 457 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 458 ASSERT_EQ(0, ret); 459 460 prog.filter = filter; 461 prog.len = 1; 462 463 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 464 ASSERT_EQ(0, ret); 465 466 prog.len = count; 467 468 /* Too many total filter instructions. */ 469 for (i = 0; i < MAX_INSNS_PER_PATH; i++) { 470 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 471 if (ret != 0) 472 break; 473 } 474 ASSERT_NE(0, ret) { 475 TH_LOG("Allowed %d %d-insn filters (total with penalties:%d)", 476 i, count, i * (count + 4)); 477 } 478 } 479 480 TEST(mode_filter_cannot_move_to_strict) 481 { 482 struct sock_filter filter[] = { 483 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 484 }; 485 struct sock_fprog prog = { 486 .len = (unsigned short)ARRAY_SIZE(filter), 487 .filter = filter, 488 }; 489 long ret; 490 491 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 492 ASSERT_EQ(0, ret); 493 494 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 495 ASSERT_EQ(0, ret); 496 497 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, 0, 0); 498 EXPECT_EQ(-1, ret); 499 EXPECT_EQ(EINVAL, errno); 500 } 501 502 503 TEST(mode_filter_get_seccomp) 504 { 505 struct sock_filter filter[] = { 506 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 507 }; 508 struct sock_fprog prog = { 509 .len = (unsigned short)ARRAY_SIZE(filter), 510 .filter = filter, 511 }; 512 long ret; 513 514 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 515 ASSERT_EQ(0, ret); 516 517 ret = prctl(PR_GET_SECCOMP, 0, 0, 0, 0); 518 EXPECT_EQ(0, ret); 519 520 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 521 ASSERT_EQ(0, ret); 522 523 ret = prctl(PR_GET_SECCOMP, 0, 0, 0, 0); 524 EXPECT_EQ(2, ret); 525 } 526 527 528 TEST(ALLOW_all) 529 { 530 struct sock_filter filter[] = { 531 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 532 }; 533 struct sock_fprog prog = { 534 .len = (unsigned short)ARRAY_SIZE(filter), 535 .filter = filter, 536 }; 537 long ret; 538 539 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 540 ASSERT_EQ(0, ret); 541 542 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 543 ASSERT_EQ(0, ret); 544 } 545 546 TEST(empty_prog) 547 { 548 struct sock_filter filter[] = { 549 }; 550 struct sock_fprog prog = { 551 .len = (unsigned short)ARRAY_SIZE(filter), 552 .filter = filter, 553 }; 554 long ret; 555 556 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 557 ASSERT_EQ(0, ret); 558 559 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 560 EXPECT_EQ(-1, ret); 561 EXPECT_EQ(EINVAL, errno); 562 } 563 564 TEST(log_all) 565 { 566 struct sock_filter filter[] = { 567 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_LOG), 568 }; 569 struct sock_fprog prog = { 570 .len = (unsigned short)ARRAY_SIZE(filter), 571 .filter = filter, 572 }; 573 long ret; 574 pid_t parent = getppid(); 575 576 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 577 ASSERT_EQ(0, ret); 578 579 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 580 ASSERT_EQ(0, ret); 581 582 /* getppid() should succeed and be logged (no check for logging) */ 583 EXPECT_EQ(parent, syscall(__NR_getppid)); 584 } 585 586 TEST_SIGNAL(unknown_ret_is_kill_inside, SIGSYS) 587 { 588 struct sock_filter filter[] = { 589 BPF_STMT(BPF_RET|BPF_K, 0x10000000U), 590 }; 591 struct sock_fprog prog = { 592 .len = (unsigned short)ARRAY_SIZE(filter), 593 .filter = filter, 594 }; 595 long ret; 596 597 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 598 ASSERT_EQ(0, ret); 599 600 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 601 ASSERT_EQ(0, ret); 602 EXPECT_EQ(0, syscall(__NR_getpid)) { 603 TH_LOG("getpid() shouldn't ever return"); 604 } 605 } 606 607 /* return code >= 0x80000000 is unused. */ 608 TEST_SIGNAL(unknown_ret_is_kill_above_allow, SIGSYS) 609 { 610 struct sock_filter filter[] = { 611 BPF_STMT(BPF_RET|BPF_K, 0x90000000U), 612 }; 613 struct sock_fprog prog = { 614 .len = (unsigned short)ARRAY_SIZE(filter), 615 .filter = filter, 616 }; 617 long ret; 618 619 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 620 ASSERT_EQ(0, ret); 621 622 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 623 ASSERT_EQ(0, ret); 624 EXPECT_EQ(0, syscall(__NR_getpid)) { 625 TH_LOG("getpid() shouldn't ever return"); 626 } 627 } 628 629 TEST_SIGNAL(KILL_all, SIGSYS) 630 { 631 struct sock_filter filter[] = { 632 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), 633 }; 634 struct sock_fprog prog = { 635 .len = (unsigned short)ARRAY_SIZE(filter), 636 .filter = filter, 637 }; 638 long ret; 639 640 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 641 ASSERT_EQ(0, ret); 642 643 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 644 ASSERT_EQ(0, ret); 645 } 646 647 TEST_SIGNAL(KILL_one, SIGSYS) 648 { 649 struct sock_filter filter[] = { 650 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 651 offsetof(struct seccomp_data, nr)), 652 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1), 653 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), 654 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 655 }; 656 struct sock_fprog prog = { 657 .len = (unsigned short)ARRAY_SIZE(filter), 658 .filter = filter, 659 }; 660 long ret; 661 pid_t parent = getppid(); 662 663 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 664 ASSERT_EQ(0, ret); 665 666 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 667 ASSERT_EQ(0, ret); 668 669 EXPECT_EQ(parent, syscall(__NR_getppid)); 670 /* getpid() should never return. */ 671 EXPECT_EQ(0, syscall(__NR_getpid)); 672 } 673 674 TEST_SIGNAL(KILL_one_arg_one, SIGSYS) 675 { 676 void *fatal_address; 677 struct sock_filter filter[] = { 678 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 679 offsetof(struct seccomp_data, nr)), 680 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_times, 1, 0), 681 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 682 /* Only both with lower 32-bit for now. */ 683 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(0)), 684 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 685 (unsigned long)&fatal_address, 0, 1), 686 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), 687 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 688 }; 689 struct sock_fprog prog = { 690 .len = (unsigned short)ARRAY_SIZE(filter), 691 .filter = filter, 692 }; 693 long ret; 694 pid_t parent = getppid(); 695 struct tms timebuf; 696 clock_t clock = times(&timebuf); 697 698 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 699 ASSERT_EQ(0, ret); 700 701 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 702 ASSERT_EQ(0, ret); 703 704 EXPECT_EQ(parent, syscall(__NR_getppid)); 705 EXPECT_LE(clock, syscall(__NR_times, &timebuf)); 706 /* times() should never return. */ 707 EXPECT_EQ(0, syscall(__NR_times, &fatal_address)); 708 } 709 710 TEST_SIGNAL(KILL_one_arg_six, SIGSYS) 711 { 712 #ifndef __NR_mmap2 713 int sysno = __NR_mmap; 714 #else 715 int sysno = __NR_mmap2; 716 #endif 717 struct sock_filter filter[] = { 718 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 719 offsetof(struct seccomp_data, nr)), 720 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, sysno, 1, 0), 721 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 722 /* Only both with lower 32-bit for now. */ 723 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(5)), 724 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x0C0FFEE, 0, 1), 725 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), 726 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 727 }; 728 struct sock_fprog prog = { 729 .len = (unsigned short)ARRAY_SIZE(filter), 730 .filter = filter, 731 }; 732 long ret; 733 pid_t parent = getppid(); 734 int fd; 735 void *map1, *map2; 736 int page_size = sysconf(_SC_PAGESIZE); 737 738 ASSERT_LT(0, page_size); 739 740 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 741 ASSERT_EQ(0, ret); 742 743 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 744 ASSERT_EQ(0, ret); 745 746 fd = open("/dev/zero", O_RDONLY); 747 ASSERT_NE(-1, fd); 748 749 EXPECT_EQ(parent, syscall(__NR_getppid)); 750 map1 = (void *)syscall(sysno, 751 NULL, page_size, PROT_READ, MAP_PRIVATE, fd, page_size); 752 EXPECT_NE(MAP_FAILED, map1); 753 /* mmap2() should never return. */ 754 map2 = (void *)syscall(sysno, 755 NULL, page_size, PROT_READ, MAP_PRIVATE, fd, 0x0C0FFEE); 756 EXPECT_EQ(MAP_FAILED, map2); 757 758 /* The test failed, so clean up the resources. */ 759 munmap(map1, page_size); 760 munmap(map2, page_size); 761 close(fd); 762 } 763 764 /* This is a thread task to die via seccomp filter violation. */ 765 void *kill_thread(void *data) 766 { 767 bool die = (bool)data; 768 769 if (die) { 770 prctl(PR_GET_SECCOMP, 0, 0, 0, 0); 771 return (void *)SIBLING_EXIT_FAILURE; 772 } 773 774 return (void *)SIBLING_EXIT_UNKILLED; 775 } 776 777 enum kill_t { 778 KILL_THREAD, 779 KILL_PROCESS, 780 RET_UNKNOWN 781 }; 782 783 /* Prepare a thread that will kill itself or both of us. */ 784 void kill_thread_or_group(struct __test_metadata *_metadata, 785 enum kill_t kill_how) 786 { 787 pthread_t thread; 788 void *status; 789 /* Kill only when calling __NR_prctl. */ 790 struct sock_filter filter_thread[] = { 791 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 792 offsetof(struct seccomp_data, nr)), 793 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1), 794 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL_THREAD), 795 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 796 }; 797 struct sock_fprog prog_thread = { 798 .len = (unsigned short)ARRAY_SIZE(filter_thread), 799 .filter = filter_thread, 800 }; 801 int kill = kill_how == KILL_PROCESS ? SECCOMP_RET_KILL_PROCESS : 0xAAAAAAAAA; 802 struct sock_filter filter_process[] = { 803 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 804 offsetof(struct seccomp_data, nr)), 805 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1), 806 BPF_STMT(BPF_RET|BPF_K, kill), 807 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 808 }; 809 struct sock_fprog prog_process = { 810 .len = (unsigned short)ARRAY_SIZE(filter_process), 811 .filter = filter_process, 812 }; 813 814 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { 815 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 816 } 817 818 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, 819 kill_how == KILL_THREAD ? &prog_thread 820 : &prog_process)); 821 822 /* 823 * Add the KILL_THREAD rule again to make sure that the KILL_PROCESS 824 * flag cannot be downgraded by a new filter. 825 */ 826 if (kill_how == KILL_PROCESS) 827 ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog_thread)); 828 829 /* Start a thread that will exit immediately. */ 830 ASSERT_EQ(0, pthread_create(&thread, NULL, kill_thread, (void *)false)); 831 ASSERT_EQ(0, pthread_join(thread, &status)); 832 ASSERT_EQ(SIBLING_EXIT_UNKILLED, (unsigned long)status); 833 834 /* Start a thread that will die immediately. */ 835 ASSERT_EQ(0, pthread_create(&thread, NULL, kill_thread, (void *)true)); 836 ASSERT_EQ(0, pthread_join(thread, &status)); 837 ASSERT_NE(SIBLING_EXIT_FAILURE, (unsigned long)status); 838 839 /* 840 * If we get here, only the spawned thread died. Let the parent know 841 * the whole process didn't die (i.e. this thread, the spawner, 842 * stayed running). 843 */ 844 exit(42); 845 } 846 847 TEST(KILL_thread) 848 { 849 int status; 850 pid_t child_pid; 851 852 child_pid = fork(); 853 ASSERT_LE(0, child_pid); 854 if (child_pid == 0) { 855 kill_thread_or_group(_metadata, KILL_THREAD); 856 _exit(38); 857 } 858 859 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 860 861 /* If only the thread was killed, we'll see exit 42. */ 862 ASSERT_TRUE(WIFEXITED(status)); 863 ASSERT_EQ(42, WEXITSTATUS(status)); 864 } 865 866 TEST(KILL_process) 867 { 868 int status; 869 pid_t child_pid; 870 871 child_pid = fork(); 872 ASSERT_LE(0, child_pid); 873 if (child_pid == 0) { 874 kill_thread_or_group(_metadata, KILL_PROCESS); 875 _exit(38); 876 } 877 878 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 879 880 /* If the entire process was killed, we'll see SIGSYS. */ 881 ASSERT_TRUE(WIFSIGNALED(status)); 882 ASSERT_EQ(SIGSYS, WTERMSIG(status)); 883 } 884 885 TEST(KILL_unknown) 886 { 887 int status; 888 pid_t child_pid; 889 890 child_pid = fork(); 891 ASSERT_LE(0, child_pid); 892 if (child_pid == 0) { 893 kill_thread_or_group(_metadata, RET_UNKNOWN); 894 _exit(38); 895 } 896 897 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 898 899 /* If the entire process was killed, we'll see SIGSYS. */ 900 EXPECT_TRUE(WIFSIGNALED(status)) { 901 TH_LOG("Unknown SECCOMP_RET is only killing the thread?"); 902 } 903 ASSERT_EQ(SIGSYS, WTERMSIG(status)); 904 } 905 906 /* TODO(wad) add 64-bit versus 32-bit arg tests. */ 907 TEST(arg_out_of_range) 908 { 909 struct sock_filter filter[] = { 910 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(6)), 911 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 912 }; 913 struct sock_fprog prog = { 914 .len = (unsigned short)ARRAY_SIZE(filter), 915 .filter = filter, 916 }; 917 long ret; 918 919 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 920 ASSERT_EQ(0, ret); 921 922 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); 923 EXPECT_EQ(-1, ret); 924 EXPECT_EQ(EINVAL, errno); 925 } 926 927 #define ERRNO_FILTER(name, errno) \ 928 struct sock_filter _read_filter_##name[] = { \ 929 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, \ 930 offsetof(struct seccomp_data, nr)), \ 931 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), \ 932 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | errno), \ 933 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), \ 934 }; \ 935 struct sock_fprog prog_##name = { \ 936 .len = (unsigned short)ARRAY_SIZE(_read_filter_##name), \ 937 .filter = _read_filter_##name, \ 938 } 939 940 /* Make sure basic errno values are correctly passed through a filter. */ 941 TEST(ERRNO_valid) 942 { 943 ERRNO_FILTER(valid, E2BIG); 944 long ret; 945 pid_t parent = getppid(); 946 947 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 948 ASSERT_EQ(0, ret); 949 950 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_valid); 951 ASSERT_EQ(0, ret); 952 953 EXPECT_EQ(parent, syscall(__NR_getppid)); 954 EXPECT_EQ(-1, read(0, NULL, 0)); 955 EXPECT_EQ(E2BIG, errno); 956 } 957 958 /* Make sure an errno of zero is correctly handled by the arch code. */ 959 TEST(ERRNO_zero) 960 { 961 ERRNO_FILTER(zero, 0); 962 long ret; 963 pid_t parent = getppid(); 964 965 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 966 ASSERT_EQ(0, ret); 967 968 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_zero); 969 ASSERT_EQ(0, ret); 970 971 EXPECT_EQ(parent, syscall(__NR_getppid)); 972 /* "errno" of 0 is ok. */ 973 EXPECT_EQ(0, read(0, NULL, 0)); 974 } 975 976 /* 977 * The SECCOMP_RET_DATA mask is 16 bits wide, but errno is smaller. 978 * This tests that the errno value gets capped correctly, fixed by 979 * 580c57f10768 ("seccomp: cap SECCOMP_RET_ERRNO data to MAX_ERRNO"). 980 */ 981 TEST(ERRNO_capped) 982 { 983 ERRNO_FILTER(capped, 4096); 984 long ret; 985 pid_t parent = getppid(); 986 987 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 988 ASSERT_EQ(0, ret); 989 990 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_capped); 991 ASSERT_EQ(0, ret); 992 993 EXPECT_EQ(parent, syscall(__NR_getppid)); 994 EXPECT_EQ(-1, read(0, NULL, 0)); 995 EXPECT_EQ(4095, errno); 996 } 997 998 /* 999 * Filters are processed in reverse order: last applied is executed first. 1000 * Since only the SECCOMP_RET_ACTION mask is tested for return values, the 1001 * SECCOMP_RET_DATA mask results will follow the most recently applied 1002 * matching filter return (and not the lowest or highest value). 1003 */ 1004 TEST(ERRNO_order) 1005 { 1006 ERRNO_FILTER(first, 11); 1007 ERRNO_FILTER(second, 13); 1008 ERRNO_FILTER(third, 12); 1009 long ret; 1010 pid_t parent = getppid(); 1011 1012 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1013 ASSERT_EQ(0, ret); 1014 1015 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_first); 1016 ASSERT_EQ(0, ret); 1017 1018 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_second); 1019 ASSERT_EQ(0, ret); 1020 1021 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_third); 1022 ASSERT_EQ(0, ret); 1023 1024 EXPECT_EQ(parent, syscall(__NR_getppid)); 1025 EXPECT_EQ(-1, read(0, NULL, 0)); 1026 EXPECT_EQ(12, errno); 1027 } 1028 1029 FIXTURE(TRAP) { 1030 struct sock_fprog prog; 1031 }; 1032 1033 FIXTURE_SETUP(TRAP) 1034 { 1035 struct sock_filter filter[] = { 1036 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 1037 offsetof(struct seccomp_data, nr)), 1038 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1), 1039 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP), 1040 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 1041 }; 1042 1043 memset(&self->prog, 0, sizeof(self->prog)); 1044 self->prog.filter = malloc(sizeof(filter)); 1045 ASSERT_NE(NULL, self->prog.filter); 1046 memcpy(self->prog.filter, filter, sizeof(filter)); 1047 self->prog.len = (unsigned short)ARRAY_SIZE(filter); 1048 } 1049 1050 FIXTURE_TEARDOWN(TRAP) 1051 { 1052 if (self->prog.filter) 1053 free(self->prog.filter); 1054 } 1055 1056 TEST_F_SIGNAL(TRAP, dfl, SIGSYS) 1057 { 1058 long ret; 1059 1060 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1061 ASSERT_EQ(0, ret); 1062 1063 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog); 1064 ASSERT_EQ(0, ret); 1065 syscall(__NR_getpid); 1066 } 1067 1068 /* Ensure that SIGSYS overrides SIG_IGN */ 1069 TEST_F_SIGNAL(TRAP, ign, SIGSYS) 1070 { 1071 long ret; 1072 1073 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1074 ASSERT_EQ(0, ret); 1075 1076 signal(SIGSYS, SIG_IGN); 1077 1078 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog); 1079 ASSERT_EQ(0, ret); 1080 syscall(__NR_getpid); 1081 } 1082 1083 static siginfo_t TRAP_info; 1084 static volatile int TRAP_nr; 1085 static void TRAP_action(int nr, siginfo_t *info, void *void_context) 1086 { 1087 memcpy(&TRAP_info, info, sizeof(TRAP_info)); 1088 TRAP_nr = nr; 1089 } 1090 1091 TEST_F(TRAP, handler) 1092 { 1093 int ret, test; 1094 struct sigaction act; 1095 sigset_t mask; 1096 1097 memset(&act, 0, sizeof(act)); 1098 sigemptyset(&mask); 1099 sigaddset(&mask, SIGSYS); 1100 1101 act.sa_sigaction = &TRAP_action; 1102 act.sa_flags = SA_SIGINFO; 1103 ret = sigaction(SIGSYS, &act, NULL); 1104 ASSERT_EQ(0, ret) { 1105 TH_LOG("sigaction failed"); 1106 } 1107 ret = sigprocmask(SIG_UNBLOCK, &mask, NULL); 1108 ASSERT_EQ(0, ret) { 1109 TH_LOG("sigprocmask failed"); 1110 } 1111 1112 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1113 ASSERT_EQ(0, ret); 1114 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog); 1115 ASSERT_EQ(0, ret); 1116 TRAP_nr = 0; 1117 memset(&TRAP_info, 0, sizeof(TRAP_info)); 1118 /* Expect the registers to be rolled back. (nr = error) may vary 1119 * based on arch. */ 1120 ret = syscall(__NR_getpid); 1121 /* Silence gcc warning about volatile. */ 1122 test = TRAP_nr; 1123 EXPECT_EQ(SIGSYS, test); 1124 struct local_sigsys { 1125 void *_call_addr; /* calling user insn */ 1126 int _syscall; /* triggering system call number */ 1127 unsigned int _arch; /* AUDIT_ARCH_* of syscall */ 1128 } *sigsys = (struct local_sigsys *) 1129 #ifdef si_syscall 1130 &(TRAP_info.si_call_addr); 1131 #else 1132 &TRAP_info.si_pid; 1133 #endif 1134 EXPECT_EQ(__NR_getpid, sigsys->_syscall); 1135 /* Make sure arch is non-zero. */ 1136 EXPECT_NE(0, sigsys->_arch); 1137 EXPECT_NE(0, (unsigned long)sigsys->_call_addr); 1138 } 1139 1140 FIXTURE(precedence) { 1141 struct sock_fprog allow; 1142 struct sock_fprog log; 1143 struct sock_fprog trace; 1144 struct sock_fprog error; 1145 struct sock_fprog trap; 1146 struct sock_fprog kill; 1147 }; 1148 1149 FIXTURE_SETUP(precedence) 1150 { 1151 struct sock_filter allow_insns[] = { 1152 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 1153 }; 1154 struct sock_filter log_insns[] = { 1155 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 1156 offsetof(struct seccomp_data, nr)), 1157 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), 1158 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 1159 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_LOG), 1160 }; 1161 struct sock_filter trace_insns[] = { 1162 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 1163 offsetof(struct seccomp_data, nr)), 1164 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), 1165 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 1166 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE), 1167 }; 1168 struct sock_filter error_insns[] = { 1169 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 1170 offsetof(struct seccomp_data, nr)), 1171 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), 1172 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 1173 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO), 1174 }; 1175 struct sock_filter trap_insns[] = { 1176 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 1177 offsetof(struct seccomp_data, nr)), 1178 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), 1179 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 1180 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP), 1181 }; 1182 struct sock_filter kill_insns[] = { 1183 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 1184 offsetof(struct seccomp_data, nr)), 1185 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), 1186 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 1187 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), 1188 }; 1189 1190 memset(self, 0, sizeof(*self)); 1191 #define FILTER_ALLOC(_x) \ 1192 self->_x.filter = malloc(sizeof(_x##_insns)); \ 1193 ASSERT_NE(NULL, self->_x.filter); \ 1194 memcpy(self->_x.filter, &_x##_insns, sizeof(_x##_insns)); \ 1195 self->_x.len = (unsigned short)ARRAY_SIZE(_x##_insns) 1196 FILTER_ALLOC(allow); 1197 FILTER_ALLOC(log); 1198 FILTER_ALLOC(trace); 1199 FILTER_ALLOC(error); 1200 FILTER_ALLOC(trap); 1201 FILTER_ALLOC(kill); 1202 } 1203 1204 FIXTURE_TEARDOWN(precedence) 1205 { 1206 #define FILTER_FREE(_x) if (self->_x.filter) free(self->_x.filter) 1207 FILTER_FREE(allow); 1208 FILTER_FREE(log); 1209 FILTER_FREE(trace); 1210 FILTER_FREE(error); 1211 FILTER_FREE(trap); 1212 FILTER_FREE(kill); 1213 } 1214 1215 TEST_F(precedence, allow_ok) 1216 { 1217 pid_t parent, res = 0; 1218 long ret; 1219 1220 parent = getppid(); 1221 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1222 ASSERT_EQ(0, ret); 1223 1224 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1225 ASSERT_EQ(0, ret); 1226 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1227 ASSERT_EQ(0, ret); 1228 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); 1229 ASSERT_EQ(0, ret); 1230 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); 1231 ASSERT_EQ(0, ret); 1232 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); 1233 ASSERT_EQ(0, ret); 1234 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill); 1235 ASSERT_EQ(0, ret); 1236 /* Should work just fine. */ 1237 res = syscall(__NR_getppid); 1238 EXPECT_EQ(parent, res); 1239 } 1240 1241 TEST_F_SIGNAL(precedence, kill_is_highest, SIGSYS) 1242 { 1243 pid_t parent, res = 0; 1244 long ret; 1245 1246 parent = getppid(); 1247 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1248 ASSERT_EQ(0, ret); 1249 1250 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1251 ASSERT_EQ(0, ret); 1252 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1253 ASSERT_EQ(0, ret); 1254 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); 1255 ASSERT_EQ(0, ret); 1256 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); 1257 ASSERT_EQ(0, ret); 1258 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); 1259 ASSERT_EQ(0, ret); 1260 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill); 1261 ASSERT_EQ(0, ret); 1262 /* Should work just fine. */ 1263 res = syscall(__NR_getppid); 1264 EXPECT_EQ(parent, res); 1265 /* getpid() should never return. */ 1266 res = syscall(__NR_getpid); 1267 EXPECT_EQ(0, res); 1268 } 1269 1270 TEST_F_SIGNAL(precedence, kill_is_highest_in_any_order, SIGSYS) 1271 { 1272 pid_t parent; 1273 long ret; 1274 1275 parent = getppid(); 1276 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1277 ASSERT_EQ(0, ret); 1278 1279 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1280 ASSERT_EQ(0, ret); 1281 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill); 1282 ASSERT_EQ(0, ret); 1283 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); 1284 ASSERT_EQ(0, ret); 1285 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1286 ASSERT_EQ(0, ret); 1287 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); 1288 ASSERT_EQ(0, ret); 1289 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); 1290 ASSERT_EQ(0, ret); 1291 /* Should work just fine. */ 1292 EXPECT_EQ(parent, syscall(__NR_getppid)); 1293 /* getpid() should never return. */ 1294 EXPECT_EQ(0, syscall(__NR_getpid)); 1295 } 1296 1297 TEST_F_SIGNAL(precedence, trap_is_second, SIGSYS) 1298 { 1299 pid_t parent; 1300 long ret; 1301 1302 parent = getppid(); 1303 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1304 ASSERT_EQ(0, ret); 1305 1306 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1307 ASSERT_EQ(0, ret); 1308 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1309 ASSERT_EQ(0, ret); 1310 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); 1311 ASSERT_EQ(0, ret); 1312 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); 1313 ASSERT_EQ(0, ret); 1314 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); 1315 ASSERT_EQ(0, ret); 1316 /* Should work just fine. */ 1317 EXPECT_EQ(parent, syscall(__NR_getppid)); 1318 /* getpid() should never return. */ 1319 EXPECT_EQ(0, syscall(__NR_getpid)); 1320 } 1321 1322 TEST_F_SIGNAL(precedence, trap_is_second_in_any_order, SIGSYS) 1323 { 1324 pid_t parent; 1325 long ret; 1326 1327 parent = getppid(); 1328 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1329 ASSERT_EQ(0, ret); 1330 1331 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1332 ASSERT_EQ(0, ret); 1333 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); 1334 ASSERT_EQ(0, ret); 1335 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1336 ASSERT_EQ(0, ret); 1337 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); 1338 ASSERT_EQ(0, ret); 1339 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); 1340 ASSERT_EQ(0, ret); 1341 /* Should work just fine. */ 1342 EXPECT_EQ(parent, syscall(__NR_getppid)); 1343 /* getpid() should never return. */ 1344 EXPECT_EQ(0, syscall(__NR_getpid)); 1345 } 1346 1347 TEST_F(precedence, errno_is_third) 1348 { 1349 pid_t parent; 1350 long ret; 1351 1352 parent = getppid(); 1353 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1354 ASSERT_EQ(0, ret); 1355 1356 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1357 ASSERT_EQ(0, ret); 1358 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1359 ASSERT_EQ(0, ret); 1360 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); 1361 ASSERT_EQ(0, ret); 1362 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); 1363 ASSERT_EQ(0, ret); 1364 /* Should work just fine. */ 1365 EXPECT_EQ(parent, syscall(__NR_getppid)); 1366 EXPECT_EQ(0, syscall(__NR_getpid)); 1367 } 1368 1369 TEST_F(precedence, errno_is_third_in_any_order) 1370 { 1371 pid_t parent; 1372 long ret; 1373 1374 parent = getppid(); 1375 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1376 ASSERT_EQ(0, ret); 1377 1378 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1379 ASSERT_EQ(0, ret); 1380 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); 1381 ASSERT_EQ(0, ret); 1382 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); 1383 ASSERT_EQ(0, ret); 1384 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1385 ASSERT_EQ(0, ret); 1386 /* Should work just fine. */ 1387 EXPECT_EQ(parent, syscall(__NR_getppid)); 1388 EXPECT_EQ(0, syscall(__NR_getpid)); 1389 } 1390 1391 TEST_F(precedence, trace_is_fourth) 1392 { 1393 pid_t parent; 1394 long ret; 1395 1396 parent = getppid(); 1397 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1398 ASSERT_EQ(0, ret); 1399 1400 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1401 ASSERT_EQ(0, ret); 1402 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1403 ASSERT_EQ(0, ret); 1404 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); 1405 ASSERT_EQ(0, ret); 1406 /* Should work just fine. */ 1407 EXPECT_EQ(parent, syscall(__NR_getppid)); 1408 /* No ptracer */ 1409 EXPECT_EQ(-1, syscall(__NR_getpid)); 1410 } 1411 1412 TEST_F(precedence, trace_is_fourth_in_any_order) 1413 { 1414 pid_t parent; 1415 long ret; 1416 1417 parent = getppid(); 1418 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1419 ASSERT_EQ(0, ret); 1420 1421 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); 1422 ASSERT_EQ(0, ret); 1423 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1424 ASSERT_EQ(0, ret); 1425 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1426 ASSERT_EQ(0, ret); 1427 /* Should work just fine. */ 1428 EXPECT_EQ(parent, syscall(__NR_getppid)); 1429 /* No ptracer */ 1430 EXPECT_EQ(-1, syscall(__NR_getpid)); 1431 } 1432 1433 TEST_F(precedence, log_is_fifth) 1434 { 1435 pid_t mypid, parent; 1436 long ret; 1437 1438 mypid = getpid(); 1439 parent = getppid(); 1440 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1441 ASSERT_EQ(0, ret); 1442 1443 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1444 ASSERT_EQ(0, ret); 1445 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1446 ASSERT_EQ(0, ret); 1447 /* Should work just fine. */ 1448 EXPECT_EQ(parent, syscall(__NR_getppid)); 1449 /* Should also work just fine */ 1450 EXPECT_EQ(mypid, syscall(__NR_getpid)); 1451 } 1452 1453 TEST_F(precedence, log_is_fifth_in_any_order) 1454 { 1455 pid_t mypid, parent; 1456 long ret; 1457 1458 mypid = getpid(); 1459 parent = getppid(); 1460 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1461 ASSERT_EQ(0, ret); 1462 1463 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); 1464 ASSERT_EQ(0, ret); 1465 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); 1466 ASSERT_EQ(0, ret); 1467 /* Should work just fine. */ 1468 EXPECT_EQ(parent, syscall(__NR_getppid)); 1469 /* Should also work just fine */ 1470 EXPECT_EQ(mypid, syscall(__NR_getpid)); 1471 } 1472 1473 #ifndef PTRACE_O_TRACESECCOMP 1474 #define PTRACE_O_TRACESECCOMP 0x00000080 1475 #endif 1476 1477 /* Catch the Ubuntu 12.04 value error. */ 1478 #if PTRACE_EVENT_SECCOMP != 7 1479 #undef PTRACE_EVENT_SECCOMP 1480 #endif 1481 1482 #ifndef PTRACE_EVENT_SECCOMP 1483 #define PTRACE_EVENT_SECCOMP 7 1484 #endif 1485 1486 #define IS_SECCOMP_EVENT(status) ((status >> 16) == PTRACE_EVENT_SECCOMP) 1487 bool tracer_running; 1488 void tracer_stop(int sig) 1489 { 1490 tracer_running = false; 1491 } 1492 1493 typedef void tracer_func_t(struct __test_metadata *_metadata, 1494 pid_t tracee, int status, void *args); 1495 1496 void start_tracer(struct __test_metadata *_metadata, int fd, pid_t tracee, 1497 tracer_func_t tracer_func, void *args, bool ptrace_syscall) 1498 { 1499 int ret = -1; 1500 struct sigaction action = { 1501 .sa_handler = tracer_stop, 1502 }; 1503 1504 /* Allow external shutdown. */ 1505 tracer_running = true; 1506 ASSERT_EQ(0, sigaction(SIGUSR1, &action, NULL)); 1507 1508 errno = 0; 1509 while (ret == -1 && errno != EINVAL) 1510 ret = ptrace(PTRACE_ATTACH, tracee, NULL, 0); 1511 ASSERT_EQ(0, ret) { 1512 kill(tracee, SIGKILL); 1513 } 1514 /* Wait for attach stop */ 1515 wait(NULL); 1516 1517 ret = ptrace(PTRACE_SETOPTIONS, tracee, NULL, ptrace_syscall ? 1518 PTRACE_O_TRACESYSGOOD : 1519 PTRACE_O_TRACESECCOMP); 1520 ASSERT_EQ(0, ret) { 1521 TH_LOG("Failed to set PTRACE_O_TRACESECCOMP"); 1522 kill(tracee, SIGKILL); 1523 } 1524 ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT, 1525 tracee, NULL, 0); 1526 ASSERT_EQ(0, ret); 1527 1528 /* Unblock the tracee */ 1529 ASSERT_EQ(1, write(fd, "A", 1)); 1530 ASSERT_EQ(0, close(fd)); 1531 1532 /* Run until we're shut down. Must assert to stop execution. */ 1533 while (tracer_running) { 1534 int status; 1535 1536 if (wait(&status) != tracee) 1537 continue; 1538 if (WIFSIGNALED(status) || WIFEXITED(status)) 1539 /* Child is dead. Time to go. */ 1540 return; 1541 1542 /* Check if this is a seccomp event. */ 1543 ASSERT_EQ(!ptrace_syscall, IS_SECCOMP_EVENT(status)); 1544 1545 tracer_func(_metadata, tracee, status, args); 1546 1547 ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT, 1548 tracee, NULL, 0); 1549 ASSERT_EQ(0, ret); 1550 } 1551 /* Directly report the status of our test harness results. */ 1552 syscall(__NR_exit, _metadata->passed ? EXIT_SUCCESS : EXIT_FAILURE); 1553 } 1554 1555 /* Common tracer setup/teardown functions. */ 1556 void cont_handler(int num) 1557 { } 1558 pid_t setup_trace_fixture(struct __test_metadata *_metadata, 1559 tracer_func_t func, void *args, bool ptrace_syscall) 1560 { 1561 char sync; 1562 int pipefd[2]; 1563 pid_t tracer_pid; 1564 pid_t tracee = getpid(); 1565 1566 /* Setup a pipe for clean synchronization. */ 1567 ASSERT_EQ(0, pipe(pipefd)); 1568 1569 /* Fork a child which we'll promote to tracer */ 1570 tracer_pid = fork(); 1571 ASSERT_LE(0, tracer_pid); 1572 signal(SIGALRM, cont_handler); 1573 if (tracer_pid == 0) { 1574 close(pipefd[0]); 1575 start_tracer(_metadata, pipefd[1], tracee, func, args, 1576 ptrace_syscall); 1577 syscall(__NR_exit, 0); 1578 } 1579 close(pipefd[1]); 1580 prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0); 1581 read(pipefd[0], &sync, 1); 1582 close(pipefd[0]); 1583 1584 return tracer_pid; 1585 } 1586 1587 void teardown_trace_fixture(struct __test_metadata *_metadata, 1588 pid_t tracer) 1589 { 1590 if (tracer) { 1591 int status; 1592 /* 1593 * Extract the exit code from the other process and 1594 * adopt it for ourselves in case its asserts failed. 1595 */ 1596 ASSERT_EQ(0, kill(tracer, SIGUSR1)); 1597 ASSERT_EQ(tracer, waitpid(tracer, &status, 0)); 1598 if (WEXITSTATUS(status)) 1599 _metadata->passed = 0; 1600 } 1601 } 1602 1603 /* "poke" tracer arguments and function. */ 1604 struct tracer_args_poke_t { 1605 unsigned long poke_addr; 1606 }; 1607 1608 void tracer_poke(struct __test_metadata *_metadata, pid_t tracee, int status, 1609 void *args) 1610 { 1611 int ret; 1612 unsigned long msg; 1613 struct tracer_args_poke_t *info = (struct tracer_args_poke_t *)args; 1614 1615 ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg); 1616 EXPECT_EQ(0, ret); 1617 /* If this fails, don't try to recover. */ 1618 ASSERT_EQ(0x1001, msg) { 1619 kill(tracee, SIGKILL); 1620 } 1621 /* 1622 * Poke in the message. 1623 * Registers are not touched to try to keep this relatively arch 1624 * agnostic. 1625 */ 1626 ret = ptrace(PTRACE_POKEDATA, tracee, info->poke_addr, 0x1001); 1627 EXPECT_EQ(0, ret); 1628 } 1629 1630 FIXTURE(TRACE_poke) { 1631 struct sock_fprog prog; 1632 pid_t tracer; 1633 long poked; 1634 struct tracer_args_poke_t tracer_args; 1635 }; 1636 1637 FIXTURE_SETUP(TRACE_poke) 1638 { 1639 struct sock_filter filter[] = { 1640 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 1641 offsetof(struct seccomp_data, nr)), 1642 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), 1643 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1001), 1644 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 1645 }; 1646 1647 self->poked = 0; 1648 memset(&self->prog, 0, sizeof(self->prog)); 1649 self->prog.filter = malloc(sizeof(filter)); 1650 ASSERT_NE(NULL, self->prog.filter); 1651 memcpy(self->prog.filter, filter, sizeof(filter)); 1652 self->prog.len = (unsigned short)ARRAY_SIZE(filter); 1653 1654 /* Set up tracer args. */ 1655 self->tracer_args.poke_addr = (unsigned long)&self->poked; 1656 1657 /* Launch tracer. */ 1658 self->tracer = setup_trace_fixture(_metadata, tracer_poke, 1659 &self->tracer_args, false); 1660 } 1661 1662 FIXTURE_TEARDOWN(TRACE_poke) 1663 { 1664 teardown_trace_fixture(_metadata, self->tracer); 1665 if (self->prog.filter) 1666 free(self->prog.filter); 1667 } 1668 1669 TEST_F(TRACE_poke, read_has_side_effects) 1670 { 1671 ssize_t ret; 1672 1673 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1674 ASSERT_EQ(0, ret); 1675 1676 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); 1677 ASSERT_EQ(0, ret); 1678 1679 EXPECT_EQ(0, self->poked); 1680 ret = read(-1, NULL, 0); 1681 EXPECT_EQ(-1, ret); 1682 EXPECT_EQ(0x1001, self->poked); 1683 } 1684 1685 TEST_F(TRACE_poke, getpid_runs_normally) 1686 { 1687 long ret; 1688 1689 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 1690 ASSERT_EQ(0, ret); 1691 1692 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); 1693 ASSERT_EQ(0, ret); 1694 1695 EXPECT_EQ(0, self->poked); 1696 EXPECT_NE(0, syscall(__NR_getpid)); 1697 EXPECT_EQ(0, self->poked); 1698 } 1699 1700 #if defined(__x86_64__) 1701 # define ARCH_REGS struct user_regs_struct 1702 # define SYSCALL_NUM(_regs) (_regs).orig_rax 1703 # define SYSCALL_RET(_regs) (_regs).rax 1704 #elif defined(__i386__) 1705 # define ARCH_REGS struct user_regs_struct 1706 # define SYSCALL_NUM(_regs) (_regs).orig_eax 1707 # define SYSCALL_RET(_regs) (_regs).eax 1708 #elif defined(__arm__) 1709 # define ARCH_REGS struct pt_regs 1710 # define SYSCALL_NUM(_regs) (_regs).ARM_r7 1711 # ifndef PTRACE_SET_SYSCALL 1712 # define PTRACE_SET_SYSCALL 23 1713 # endif 1714 # define SYSCALL_NUM_SET(_regs, _nr) \ 1715 EXPECT_EQ(0, ptrace(PTRACE_SET_SYSCALL, tracee, NULL, _nr)) 1716 # define SYSCALL_RET(_regs) (_regs).ARM_r0 1717 #elif defined(__aarch64__) 1718 # define ARCH_REGS struct user_pt_regs 1719 # define SYSCALL_NUM(_regs) (_regs).regs[8] 1720 # ifndef NT_ARM_SYSTEM_CALL 1721 # define NT_ARM_SYSTEM_CALL 0x404 1722 # endif 1723 # define SYSCALL_NUM_SET(_regs, _nr) \ 1724 do { \ 1725 struct iovec __v; \ 1726 typeof(_nr) __nr = (_nr); \ 1727 __v.iov_base = &__nr; \ 1728 __v.iov_len = sizeof(__nr); \ 1729 EXPECT_EQ(0, ptrace(PTRACE_SETREGSET, tracee, \ 1730 NT_ARM_SYSTEM_CALL, &__v)); \ 1731 } while (0) 1732 # define SYSCALL_RET(_regs) (_regs).regs[0] 1733 #elif defined(__riscv) && __riscv_xlen == 64 1734 # define ARCH_REGS struct user_regs_struct 1735 # define SYSCALL_NUM(_regs) (_regs).a7 1736 # define SYSCALL_RET(_regs) (_regs).a0 1737 #elif defined(__csky__) 1738 # define ARCH_REGS struct pt_regs 1739 # if defined(__CSKYABIV2__) 1740 # define SYSCALL_NUM(_regs) (_regs).regs[3] 1741 # else 1742 # define SYSCALL_NUM(_regs) (_regs).regs[9] 1743 # endif 1744 # define SYSCALL_RET(_regs) (_regs).a0 1745 #elif defined(__hppa__) 1746 # define ARCH_REGS struct user_regs_struct 1747 # define SYSCALL_NUM(_regs) (_regs).gr[20] 1748 # define SYSCALL_RET(_regs) (_regs).gr[28] 1749 #elif defined(__powerpc__) 1750 # define ARCH_REGS struct pt_regs 1751 # define SYSCALL_NUM(_regs) (_regs).gpr[0] 1752 # define SYSCALL_RET(_regs) (_regs).gpr[3] 1753 # define SYSCALL_RET_SET(_regs, _val) \ 1754 do { \ 1755 typeof(_val) _result = (_val); \ 1756 if ((_regs.trap & 0xfff0) == 0x3000) { \ 1757 /* \ 1758 * scv 0 system call uses -ve result \ 1759 * for error, so no need to adjust. \ 1760 */ \ 1761 SYSCALL_RET(_regs) = _result; \ 1762 } else { \ 1763 /* \ 1764 * A syscall error is signaled by the \ 1765 * CR0 SO bit and the code is stored as \ 1766 * a positive value. \ 1767 */ \ 1768 if (_result < 0) { \ 1769 SYSCALL_RET(_regs) = -_result; \ 1770 (_regs).ccr |= 0x10000000; \ 1771 } else { \ 1772 SYSCALL_RET(_regs) = _result; \ 1773 (_regs).ccr &= ~0x10000000; \ 1774 } \ 1775 } \ 1776 } while (0) 1777 # define SYSCALL_RET_SET_ON_PTRACE_EXIT 1778 #elif defined(__s390__) 1779 # define ARCH_REGS s390_regs 1780 # define SYSCALL_NUM(_regs) (_regs).gprs[2] 1781 # define SYSCALL_RET_SET(_regs, _val) \ 1782 TH_LOG("Can't modify syscall return on this architecture") 1783 #elif defined(__mips__) 1784 # include <asm/unistd_nr_n32.h> 1785 # include <asm/unistd_nr_n64.h> 1786 # include <asm/unistd_nr_o32.h> 1787 # define ARCH_REGS struct pt_regs 1788 # define SYSCALL_NUM(_regs) \ 1789 ({ \ 1790 typeof((_regs).regs[2]) _nr; \ 1791 if ((_regs).regs[2] == __NR_O32_Linux) \ 1792 _nr = (_regs).regs[4]; \ 1793 else \ 1794 _nr = (_regs).regs[2]; \ 1795 _nr; \ 1796 }) 1797 # define SYSCALL_NUM_SET(_regs, _nr) \ 1798 do { \ 1799 if ((_regs).regs[2] == __NR_O32_Linux) \ 1800 (_regs).regs[4] = _nr; \ 1801 else \ 1802 (_regs).regs[2] = _nr; \ 1803 } while (0) 1804 # define SYSCALL_RET_SET(_regs, _val) \ 1805 TH_LOG("Can't modify syscall return on this architecture") 1806 #elif defined(__xtensa__) 1807 # define ARCH_REGS struct user_pt_regs 1808 # define SYSCALL_NUM(_regs) (_regs).syscall 1809 /* 1810 * On xtensa syscall return value is in the register 1811 * a2 of the current window which is not fixed. 1812 */ 1813 #define SYSCALL_RET(_regs) (_regs).a[(_regs).windowbase * 4 + 2] 1814 #elif defined(__sh__) 1815 # define ARCH_REGS struct pt_regs 1816 # define SYSCALL_NUM(_regs) (_regs).regs[3] 1817 # define SYSCALL_RET(_regs) (_regs).regs[0] 1818 #else 1819 # error "Do not know how to find your architecture's registers and syscalls" 1820 #endif 1821 1822 /* 1823 * Most architectures can change the syscall by just updating the 1824 * associated register. This is the default if not defined above. 1825 */ 1826 #ifndef SYSCALL_NUM_SET 1827 # define SYSCALL_NUM_SET(_regs, _nr) \ 1828 do { \ 1829 SYSCALL_NUM(_regs) = (_nr); \ 1830 } while (0) 1831 #endif 1832 /* 1833 * Most architectures can change the syscall return value by just 1834 * writing to the SYSCALL_RET register. This is the default if not 1835 * defined above. If an architecture cannot set the return value 1836 * (for example when the syscall and return value register is 1837 * shared), report it with TH_LOG() in an arch-specific definition 1838 * of SYSCALL_RET_SET() above, and leave SYSCALL_RET undefined. 1839 */ 1840 #if !defined(SYSCALL_RET) && !defined(SYSCALL_RET_SET) 1841 # error "One of SYSCALL_RET or SYSCALL_RET_SET is needed for this arch" 1842 #endif 1843 #ifndef SYSCALL_RET_SET 1844 # define SYSCALL_RET_SET(_regs, _val) \ 1845 do { \ 1846 SYSCALL_RET(_regs) = (_val); \ 1847 } while (0) 1848 #endif 1849 1850 /* When the syscall return can't be changed, stub out the tests for it. */ 1851 #ifndef SYSCALL_RET 1852 # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(-1, action) 1853 #else 1854 # define EXPECT_SYSCALL_RETURN(val, action) \ 1855 do { \ 1856 errno = 0; \ 1857 if (val < 0) { \ 1858 EXPECT_EQ(-1, action); \ 1859 EXPECT_EQ(-(val), errno); \ 1860 } else { \ 1861 EXPECT_EQ(val, action); \ 1862 } \ 1863 } while (0) 1864 #endif 1865 1866 /* 1867 * Some architectures (e.g. powerpc) can only set syscall 1868 * return values on syscall exit during ptrace. 1869 */ 1870 const bool ptrace_entry_set_syscall_nr = true; 1871 const bool ptrace_entry_set_syscall_ret = 1872 #ifndef SYSCALL_RET_SET_ON_PTRACE_EXIT 1873 true; 1874 #else 1875 false; 1876 #endif 1877 1878 /* 1879 * Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for 1880 * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux). 1881 */ 1882 #if defined(__x86_64__) || defined(__i386__) || defined(__mips__) 1883 # define ARCH_GETREGS(_regs) ptrace(PTRACE_GETREGS, tracee, 0, &(_regs)) 1884 # define ARCH_SETREGS(_regs) ptrace(PTRACE_SETREGS, tracee, 0, &(_regs)) 1885 #else 1886 # define ARCH_GETREGS(_regs) ({ \ 1887 struct iovec __v; \ 1888 __v.iov_base = &(_regs); \ 1889 __v.iov_len = sizeof(_regs); \ 1890 ptrace(PTRACE_GETREGSET, tracee, NT_PRSTATUS, &__v); \ 1891 }) 1892 # define ARCH_SETREGS(_regs) ({ \ 1893 struct iovec __v; \ 1894 __v.iov_base = &(_regs); \ 1895 __v.iov_len = sizeof(_regs); \ 1896 ptrace(PTRACE_SETREGSET, tracee, NT_PRSTATUS, &__v); \ 1897 }) 1898 #endif 1899 1900 /* Architecture-specific syscall fetching routine. */ 1901 int get_syscall(struct __test_metadata *_metadata, pid_t tracee) 1902 { 1903 ARCH_REGS regs; 1904 1905 EXPECT_EQ(0, ARCH_GETREGS(regs)) { 1906 return -1; 1907 } 1908 1909 return SYSCALL_NUM(regs); 1910 } 1911 1912 /* Architecture-specific syscall changing routine. */ 1913 void __change_syscall(struct __test_metadata *_metadata, 1914 pid_t tracee, long *syscall, long *ret) 1915 { 1916 ARCH_REGS orig, regs; 1917 1918 /* Do not get/set registers if we have nothing to do. */ 1919 if (!syscall && !ret) 1920 return; 1921 1922 EXPECT_EQ(0, ARCH_GETREGS(regs)) { 1923 return; 1924 } 1925 orig = regs; 1926 1927 if (syscall) 1928 SYSCALL_NUM_SET(regs, *syscall); 1929 1930 if (ret) 1931 SYSCALL_RET_SET(regs, *ret); 1932 1933 /* Flush any register changes made. */ 1934 if (memcmp(&orig, ®s, sizeof(orig)) != 0) 1935 EXPECT_EQ(0, ARCH_SETREGS(regs)); 1936 } 1937 1938 /* Change only syscall number. */ 1939 void change_syscall_nr(struct __test_metadata *_metadata, 1940 pid_t tracee, long syscall) 1941 { 1942 __change_syscall(_metadata, tracee, &syscall, NULL); 1943 } 1944 1945 /* Change syscall return value (and set syscall number to -1). */ 1946 void change_syscall_ret(struct __test_metadata *_metadata, 1947 pid_t tracee, long ret) 1948 { 1949 long syscall = -1; 1950 1951 __change_syscall(_metadata, tracee, &syscall, &ret); 1952 } 1953 1954 void tracer_seccomp(struct __test_metadata *_metadata, pid_t tracee, 1955 int status, void *args) 1956 { 1957 int ret; 1958 unsigned long msg; 1959 1960 /* Make sure we got the right message. */ 1961 ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg); 1962 EXPECT_EQ(0, ret); 1963 1964 /* Validate and take action on expected syscalls. */ 1965 switch (msg) { 1966 case 0x1002: 1967 /* change getpid to getppid. */ 1968 EXPECT_EQ(__NR_getpid, get_syscall(_metadata, tracee)); 1969 change_syscall_nr(_metadata, tracee, __NR_getppid); 1970 break; 1971 case 0x1003: 1972 /* skip gettid with valid return code. */ 1973 EXPECT_EQ(__NR_gettid, get_syscall(_metadata, tracee)); 1974 change_syscall_ret(_metadata, tracee, 45000); 1975 break; 1976 case 0x1004: 1977 /* skip openat with error. */ 1978 EXPECT_EQ(__NR_openat, get_syscall(_metadata, tracee)); 1979 change_syscall_ret(_metadata, tracee, -ESRCH); 1980 break; 1981 case 0x1005: 1982 /* do nothing (allow getppid) */ 1983 EXPECT_EQ(__NR_getppid, get_syscall(_metadata, tracee)); 1984 break; 1985 default: 1986 EXPECT_EQ(0, msg) { 1987 TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg); 1988 kill(tracee, SIGKILL); 1989 } 1990 } 1991 1992 } 1993 1994 FIXTURE(TRACE_syscall) { 1995 struct sock_fprog prog; 1996 pid_t tracer, mytid, mypid, parent; 1997 long syscall_nr; 1998 }; 1999 2000 void tracer_ptrace(struct __test_metadata *_metadata, pid_t tracee, 2001 int status, void *args) 2002 { 2003 int ret; 2004 unsigned long msg; 2005 static bool entry; 2006 long syscall_nr_val, syscall_ret_val; 2007 long *syscall_nr = NULL, *syscall_ret = NULL; 2008 FIXTURE_DATA(TRACE_syscall) *self = args; 2009 2010 /* 2011 * The traditional way to tell PTRACE_SYSCALL entry/exit 2012 * is by counting. 2013 */ 2014 entry = !entry; 2015 2016 /* Make sure we got an appropriate message. */ 2017 ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg); 2018 EXPECT_EQ(0, ret); 2019 EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY 2020 : PTRACE_EVENTMSG_SYSCALL_EXIT, msg); 2021 2022 /* 2023 * Some architectures only support setting return values during 2024 * syscall exit under ptrace, and on exit the syscall number may 2025 * no longer be available. Therefore, save the initial sycall 2026 * number here, so it can be examined during both entry and exit 2027 * phases. 2028 */ 2029 if (entry) 2030 self->syscall_nr = get_syscall(_metadata, tracee); 2031 2032 /* 2033 * Depending on the architecture's syscall setting abilities, we 2034 * pick which things to set during this phase (entry or exit). 2035 */ 2036 if (entry == ptrace_entry_set_syscall_nr) 2037 syscall_nr = &syscall_nr_val; 2038 if (entry == ptrace_entry_set_syscall_ret) 2039 syscall_ret = &syscall_ret_val; 2040 2041 /* Now handle the actual rewriting cases. */ 2042 switch (self->syscall_nr) { 2043 case __NR_getpid: 2044 syscall_nr_val = __NR_getppid; 2045 /* Never change syscall return for this case. */ 2046 syscall_ret = NULL; 2047 break; 2048 case __NR_gettid: 2049 syscall_nr_val = -1; 2050 syscall_ret_val = 45000; 2051 break; 2052 case __NR_openat: 2053 syscall_nr_val = -1; 2054 syscall_ret_val = -ESRCH; 2055 break; 2056 default: 2057 /* Unhandled, do nothing. */ 2058 return; 2059 } 2060 2061 __change_syscall(_metadata, tracee, syscall_nr, syscall_ret); 2062 } 2063 2064 FIXTURE_VARIANT(TRACE_syscall) { 2065 /* 2066 * All of the SECCOMP_RET_TRACE behaviors can be tested with either 2067 * SECCOMP_RET_TRACE+PTRACE_CONT or plain ptrace()+PTRACE_SYSCALL. 2068 * This indicates if we should use SECCOMP_RET_TRACE (false), or 2069 * ptrace (true). 2070 */ 2071 bool use_ptrace; 2072 }; 2073 2074 FIXTURE_VARIANT_ADD(TRACE_syscall, ptrace) { 2075 .use_ptrace = true, 2076 }; 2077 2078 FIXTURE_VARIANT_ADD(TRACE_syscall, seccomp) { 2079 .use_ptrace = false, 2080 }; 2081 2082 FIXTURE_SETUP(TRACE_syscall) 2083 { 2084 struct sock_filter filter[] = { 2085 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2086 offsetof(struct seccomp_data, nr)), 2087 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1), 2088 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1002), 2089 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_gettid, 0, 1), 2090 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1003), 2091 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_openat, 0, 1), 2092 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1004), 2093 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1), 2094 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1005), 2095 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2096 }; 2097 struct sock_fprog prog = { 2098 .len = (unsigned short)ARRAY_SIZE(filter), 2099 .filter = filter, 2100 }; 2101 long ret; 2102 2103 /* Prepare some testable syscall results. */ 2104 self->mytid = syscall(__NR_gettid); 2105 ASSERT_GT(self->mytid, 0); 2106 ASSERT_NE(self->mytid, 1) { 2107 TH_LOG("Running this test as init is not supported. :)"); 2108 } 2109 2110 self->mypid = getpid(); 2111 ASSERT_GT(self->mypid, 0); 2112 ASSERT_EQ(self->mytid, self->mypid); 2113 2114 self->parent = getppid(); 2115 ASSERT_GT(self->parent, 0); 2116 ASSERT_NE(self->parent, self->mypid); 2117 2118 /* Launch tracer. */ 2119 self->tracer = setup_trace_fixture(_metadata, 2120 variant->use_ptrace ? tracer_ptrace 2121 : tracer_seccomp, 2122 self, variant->use_ptrace); 2123 2124 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 2125 ASSERT_EQ(0, ret); 2126 2127 if (variant->use_ptrace) 2128 return; 2129 2130 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 2131 ASSERT_EQ(0, ret); 2132 } 2133 2134 FIXTURE_TEARDOWN(TRACE_syscall) 2135 { 2136 teardown_trace_fixture(_metadata, self->tracer); 2137 } 2138 2139 TEST(negative_ENOSYS) 2140 { 2141 /* 2142 * There should be no difference between an "internal" skip 2143 * and userspace asking for syscall "-1". 2144 */ 2145 errno = 0; 2146 EXPECT_EQ(-1, syscall(-1)); 2147 EXPECT_EQ(errno, ENOSYS); 2148 /* And no difference for "still not valid but not -1". */ 2149 errno = 0; 2150 EXPECT_EQ(-1, syscall(-101)); 2151 EXPECT_EQ(errno, ENOSYS); 2152 } 2153 2154 TEST_F(TRACE_syscall, negative_ENOSYS) 2155 { 2156 negative_ENOSYS(_metadata); 2157 } 2158 2159 TEST_F(TRACE_syscall, syscall_allowed) 2160 { 2161 /* getppid works as expected (no changes). */ 2162 EXPECT_EQ(self->parent, syscall(__NR_getppid)); 2163 EXPECT_NE(self->mypid, syscall(__NR_getppid)); 2164 } 2165 2166 TEST_F(TRACE_syscall, syscall_redirected) 2167 { 2168 /* getpid has been redirected to getppid as expected. */ 2169 EXPECT_EQ(self->parent, syscall(__NR_getpid)); 2170 EXPECT_NE(self->mypid, syscall(__NR_getpid)); 2171 } 2172 2173 TEST_F(TRACE_syscall, syscall_errno) 2174 { 2175 /* Tracer should skip the open syscall, resulting in ESRCH. */ 2176 EXPECT_SYSCALL_RETURN(-ESRCH, syscall(__NR_openat)); 2177 } 2178 2179 TEST_F(TRACE_syscall, syscall_faked) 2180 { 2181 /* Tracer skips the gettid syscall and store altered return value. */ 2182 EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid)); 2183 } 2184 2185 TEST_F(TRACE_syscall, skip_after) 2186 { 2187 struct sock_filter filter[] = { 2188 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2189 offsetof(struct seccomp_data, nr)), 2190 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1), 2191 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EPERM), 2192 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2193 }; 2194 struct sock_fprog prog = { 2195 .len = (unsigned short)ARRAY_SIZE(filter), 2196 .filter = filter, 2197 }; 2198 long ret; 2199 2200 /* Install additional "errno on getppid" filter. */ 2201 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 2202 ASSERT_EQ(0, ret); 2203 2204 /* Tracer will redirect getpid to getppid, and we should see EPERM. */ 2205 errno = 0; 2206 EXPECT_EQ(-1, syscall(__NR_getpid)); 2207 EXPECT_EQ(EPERM, errno); 2208 } 2209 2210 TEST_F_SIGNAL(TRACE_syscall, kill_after, SIGSYS) 2211 { 2212 struct sock_filter filter[] = { 2213 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2214 offsetof(struct seccomp_data, nr)), 2215 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1), 2216 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), 2217 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2218 }; 2219 struct sock_fprog prog = { 2220 .len = (unsigned short)ARRAY_SIZE(filter), 2221 .filter = filter, 2222 }; 2223 long ret; 2224 2225 /* Install additional "death on getppid" filter. */ 2226 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 2227 ASSERT_EQ(0, ret); 2228 2229 /* Tracer will redirect getpid to getppid, and we should die. */ 2230 EXPECT_NE(self->mypid, syscall(__NR_getpid)); 2231 } 2232 2233 TEST(seccomp_syscall) 2234 { 2235 struct sock_filter filter[] = { 2236 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2237 }; 2238 struct sock_fprog prog = { 2239 .len = (unsigned short)ARRAY_SIZE(filter), 2240 .filter = filter, 2241 }; 2242 long ret; 2243 2244 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 2245 ASSERT_EQ(0, ret) { 2246 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2247 } 2248 2249 /* Reject insane operation. */ 2250 ret = seccomp(-1, 0, &prog); 2251 ASSERT_NE(ENOSYS, errno) { 2252 TH_LOG("Kernel does not support seccomp syscall!"); 2253 } 2254 EXPECT_EQ(EINVAL, errno) { 2255 TH_LOG("Did not reject crazy op value!"); 2256 } 2257 2258 /* Reject strict with flags or pointer. */ 2259 ret = seccomp(SECCOMP_SET_MODE_STRICT, -1, NULL); 2260 EXPECT_EQ(EINVAL, errno) { 2261 TH_LOG("Did not reject mode strict with flags!"); 2262 } 2263 ret = seccomp(SECCOMP_SET_MODE_STRICT, 0, &prog); 2264 EXPECT_EQ(EINVAL, errno) { 2265 TH_LOG("Did not reject mode strict with uargs!"); 2266 } 2267 2268 /* Reject insane args for filter. */ 2269 ret = seccomp(SECCOMP_SET_MODE_FILTER, -1, &prog); 2270 EXPECT_EQ(EINVAL, errno) { 2271 TH_LOG("Did not reject crazy filter flags!"); 2272 } 2273 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, NULL); 2274 EXPECT_EQ(EFAULT, errno) { 2275 TH_LOG("Did not reject NULL filter!"); 2276 } 2277 2278 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog); 2279 EXPECT_EQ(0, errno) { 2280 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s", 2281 strerror(errno)); 2282 } 2283 } 2284 2285 TEST(seccomp_syscall_mode_lock) 2286 { 2287 struct sock_filter filter[] = { 2288 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2289 }; 2290 struct sock_fprog prog = { 2291 .len = (unsigned short)ARRAY_SIZE(filter), 2292 .filter = filter, 2293 }; 2294 long ret; 2295 2296 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0); 2297 ASSERT_EQ(0, ret) { 2298 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2299 } 2300 2301 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog); 2302 ASSERT_NE(ENOSYS, errno) { 2303 TH_LOG("Kernel does not support seccomp syscall!"); 2304 } 2305 EXPECT_EQ(0, ret) { 2306 TH_LOG("Could not install filter!"); 2307 } 2308 2309 /* Make sure neither entry point will switch to strict. */ 2310 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, 0, 0, 0); 2311 EXPECT_EQ(EINVAL, errno) { 2312 TH_LOG("Switched to mode strict!"); 2313 } 2314 2315 ret = seccomp(SECCOMP_SET_MODE_STRICT, 0, NULL); 2316 EXPECT_EQ(EINVAL, errno) { 2317 TH_LOG("Switched to mode strict!"); 2318 } 2319 } 2320 2321 /* 2322 * Test detection of known and unknown filter flags. Userspace needs to be able 2323 * to check if a filter flag is supported by the current kernel and a good way 2324 * of doing that is by attempting to enter filter mode, with the flag bit in 2325 * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates 2326 * that the flag is valid and EINVAL indicates that the flag is invalid. 2327 */ 2328 TEST(detect_seccomp_filter_flags) 2329 { 2330 unsigned int flags[] = { SECCOMP_FILTER_FLAG_TSYNC, 2331 SECCOMP_FILTER_FLAG_LOG, 2332 SECCOMP_FILTER_FLAG_SPEC_ALLOW, 2333 SECCOMP_FILTER_FLAG_NEW_LISTENER, 2334 SECCOMP_FILTER_FLAG_TSYNC_ESRCH }; 2335 unsigned int exclusive[] = { 2336 SECCOMP_FILTER_FLAG_TSYNC, 2337 SECCOMP_FILTER_FLAG_NEW_LISTENER }; 2338 unsigned int flag, all_flags, exclusive_mask; 2339 int i; 2340 long ret; 2341 2342 /* Test detection of individual known-good filter flags */ 2343 for (i = 0, all_flags = 0; i < ARRAY_SIZE(flags); i++) { 2344 int bits = 0; 2345 2346 flag = flags[i]; 2347 /* Make sure the flag is a single bit! */ 2348 while (flag) { 2349 if (flag & 0x1) 2350 bits ++; 2351 flag >>= 1; 2352 } 2353 ASSERT_EQ(1, bits); 2354 flag = flags[i]; 2355 2356 ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL); 2357 ASSERT_NE(ENOSYS, errno) { 2358 TH_LOG("Kernel does not support seccomp syscall!"); 2359 } 2360 EXPECT_EQ(-1, ret); 2361 EXPECT_EQ(EFAULT, errno) { 2362 TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!", 2363 flag); 2364 } 2365 2366 all_flags |= flag; 2367 } 2368 2369 /* 2370 * Test detection of all known-good filter flags combined. But 2371 * for the exclusive flags we need to mask them out and try them 2372 * individually for the "all flags" testing. 2373 */ 2374 exclusive_mask = 0; 2375 for (i = 0; i < ARRAY_SIZE(exclusive); i++) 2376 exclusive_mask |= exclusive[i]; 2377 for (i = 0; i < ARRAY_SIZE(exclusive); i++) { 2378 flag = all_flags & ~exclusive_mask; 2379 flag |= exclusive[i]; 2380 2381 ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL); 2382 EXPECT_EQ(-1, ret); 2383 EXPECT_EQ(EFAULT, errno) { 2384 TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!", 2385 flag); 2386 } 2387 } 2388 2389 /* Test detection of an unknown filter flags, without exclusives. */ 2390 flag = -1; 2391 flag &= ~exclusive_mask; 2392 ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL); 2393 EXPECT_EQ(-1, ret); 2394 EXPECT_EQ(EINVAL, errno) { 2395 TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!", 2396 flag); 2397 } 2398 2399 /* 2400 * Test detection of an unknown filter flag that may simply need to be 2401 * added to this test 2402 */ 2403 flag = flags[ARRAY_SIZE(flags) - 1] << 1; 2404 ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL); 2405 EXPECT_EQ(-1, ret); 2406 EXPECT_EQ(EINVAL, errno) { 2407 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?", 2408 flag); 2409 } 2410 } 2411 2412 TEST(TSYNC_first) 2413 { 2414 struct sock_filter filter[] = { 2415 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2416 }; 2417 struct sock_fprog prog = { 2418 .len = (unsigned short)ARRAY_SIZE(filter), 2419 .filter = filter, 2420 }; 2421 long ret; 2422 2423 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0); 2424 ASSERT_EQ(0, ret) { 2425 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2426 } 2427 2428 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, 2429 &prog); 2430 ASSERT_NE(ENOSYS, errno) { 2431 TH_LOG("Kernel does not support seccomp syscall!"); 2432 } 2433 EXPECT_EQ(0, ret) { 2434 TH_LOG("Could not install initial filter with TSYNC!"); 2435 } 2436 } 2437 2438 #define TSYNC_SIBLINGS 2 2439 struct tsync_sibling { 2440 pthread_t tid; 2441 pid_t system_tid; 2442 sem_t *started; 2443 pthread_cond_t *cond; 2444 pthread_mutex_t *mutex; 2445 int diverge; 2446 int num_waits; 2447 struct sock_fprog *prog; 2448 struct __test_metadata *metadata; 2449 }; 2450 2451 /* 2452 * To avoid joining joined threads (which is not allowed by Bionic), 2453 * make sure we both successfully join and clear the tid to skip a 2454 * later join attempt during fixture teardown. Any remaining threads 2455 * will be directly killed during teardown. 2456 */ 2457 #define PTHREAD_JOIN(tid, status) \ 2458 do { \ 2459 int _rc = pthread_join(tid, status); \ 2460 if (_rc) { \ 2461 TH_LOG("pthread_join of tid %u failed: %d\n", \ 2462 (unsigned int)tid, _rc); \ 2463 } else { \ 2464 tid = 0; \ 2465 } \ 2466 } while (0) 2467 2468 FIXTURE(TSYNC) { 2469 struct sock_fprog root_prog, apply_prog; 2470 struct tsync_sibling sibling[TSYNC_SIBLINGS]; 2471 sem_t started; 2472 pthread_cond_t cond; 2473 pthread_mutex_t mutex; 2474 int sibling_count; 2475 }; 2476 2477 FIXTURE_SETUP(TSYNC) 2478 { 2479 struct sock_filter root_filter[] = { 2480 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2481 }; 2482 struct sock_filter apply_filter[] = { 2483 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2484 offsetof(struct seccomp_data, nr)), 2485 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), 2486 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), 2487 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2488 }; 2489 2490 memset(&self->root_prog, 0, sizeof(self->root_prog)); 2491 memset(&self->apply_prog, 0, sizeof(self->apply_prog)); 2492 memset(&self->sibling, 0, sizeof(self->sibling)); 2493 self->root_prog.filter = malloc(sizeof(root_filter)); 2494 ASSERT_NE(NULL, self->root_prog.filter); 2495 memcpy(self->root_prog.filter, &root_filter, sizeof(root_filter)); 2496 self->root_prog.len = (unsigned short)ARRAY_SIZE(root_filter); 2497 2498 self->apply_prog.filter = malloc(sizeof(apply_filter)); 2499 ASSERT_NE(NULL, self->apply_prog.filter); 2500 memcpy(self->apply_prog.filter, &apply_filter, sizeof(apply_filter)); 2501 self->apply_prog.len = (unsigned short)ARRAY_SIZE(apply_filter); 2502 2503 self->sibling_count = 0; 2504 pthread_mutex_init(&self->mutex, NULL); 2505 pthread_cond_init(&self->cond, NULL); 2506 sem_init(&self->started, 0, 0); 2507 self->sibling[0].tid = 0; 2508 self->sibling[0].cond = &self->cond; 2509 self->sibling[0].started = &self->started; 2510 self->sibling[0].mutex = &self->mutex; 2511 self->sibling[0].diverge = 0; 2512 self->sibling[0].num_waits = 1; 2513 self->sibling[0].prog = &self->root_prog; 2514 self->sibling[0].metadata = _metadata; 2515 self->sibling[1].tid = 0; 2516 self->sibling[1].cond = &self->cond; 2517 self->sibling[1].started = &self->started; 2518 self->sibling[1].mutex = &self->mutex; 2519 self->sibling[1].diverge = 0; 2520 self->sibling[1].prog = &self->root_prog; 2521 self->sibling[1].num_waits = 1; 2522 self->sibling[1].metadata = _metadata; 2523 } 2524 2525 FIXTURE_TEARDOWN(TSYNC) 2526 { 2527 int sib = 0; 2528 2529 if (self->root_prog.filter) 2530 free(self->root_prog.filter); 2531 if (self->apply_prog.filter) 2532 free(self->apply_prog.filter); 2533 2534 for ( ; sib < self->sibling_count; ++sib) { 2535 struct tsync_sibling *s = &self->sibling[sib]; 2536 2537 if (!s->tid) 2538 continue; 2539 /* 2540 * If a thread is still running, it may be stuck, so hit 2541 * it over the head really hard. 2542 */ 2543 pthread_kill(s->tid, 9); 2544 } 2545 pthread_mutex_destroy(&self->mutex); 2546 pthread_cond_destroy(&self->cond); 2547 sem_destroy(&self->started); 2548 } 2549 2550 void *tsync_sibling(void *data) 2551 { 2552 long ret = 0; 2553 struct tsync_sibling *me = data; 2554 2555 me->system_tid = syscall(__NR_gettid); 2556 2557 pthread_mutex_lock(me->mutex); 2558 if (me->diverge) { 2559 /* Just re-apply the root prog to fork the tree */ 2560 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 2561 me->prog, 0, 0); 2562 } 2563 sem_post(me->started); 2564 /* Return outside of started so parent notices failures. */ 2565 if (ret) { 2566 pthread_mutex_unlock(me->mutex); 2567 return (void *)SIBLING_EXIT_FAILURE; 2568 } 2569 do { 2570 pthread_cond_wait(me->cond, me->mutex); 2571 me->num_waits = me->num_waits - 1; 2572 } while (me->num_waits); 2573 pthread_mutex_unlock(me->mutex); 2574 2575 ret = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0); 2576 if (!ret) 2577 return (void *)SIBLING_EXIT_NEWPRIVS; 2578 read(0, NULL, 0); 2579 return (void *)SIBLING_EXIT_UNKILLED; 2580 } 2581 2582 void tsync_start_sibling(struct tsync_sibling *sibling) 2583 { 2584 pthread_create(&sibling->tid, NULL, tsync_sibling, (void *)sibling); 2585 } 2586 2587 TEST_F(TSYNC, siblings_fail_prctl) 2588 { 2589 long ret; 2590 void *status; 2591 struct sock_filter filter[] = { 2592 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2593 offsetof(struct seccomp_data, nr)), 2594 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1), 2595 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EINVAL), 2596 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2597 }; 2598 struct sock_fprog prog = { 2599 .len = (unsigned short)ARRAY_SIZE(filter), 2600 .filter = filter, 2601 }; 2602 2603 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { 2604 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2605 } 2606 2607 /* Check prctl failure detection by requesting sib 0 diverge. */ 2608 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog); 2609 ASSERT_NE(ENOSYS, errno) { 2610 TH_LOG("Kernel does not support seccomp syscall!"); 2611 } 2612 ASSERT_EQ(0, ret) { 2613 TH_LOG("setting filter failed"); 2614 } 2615 2616 self->sibling[0].diverge = 1; 2617 tsync_start_sibling(&self->sibling[0]); 2618 tsync_start_sibling(&self->sibling[1]); 2619 2620 while (self->sibling_count < TSYNC_SIBLINGS) { 2621 sem_wait(&self->started); 2622 self->sibling_count++; 2623 } 2624 2625 /* Signal the threads to clean up*/ 2626 pthread_mutex_lock(&self->mutex); 2627 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { 2628 TH_LOG("cond broadcast non-zero"); 2629 } 2630 pthread_mutex_unlock(&self->mutex); 2631 2632 /* Ensure diverging sibling failed to call prctl. */ 2633 PTHREAD_JOIN(self->sibling[0].tid, &status); 2634 EXPECT_EQ(SIBLING_EXIT_FAILURE, (long)status); 2635 PTHREAD_JOIN(self->sibling[1].tid, &status); 2636 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); 2637 } 2638 2639 TEST_F(TSYNC, two_siblings_with_ancestor) 2640 { 2641 long ret; 2642 void *status; 2643 2644 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { 2645 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2646 } 2647 2648 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog); 2649 ASSERT_NE(ENOSYS, errno) { 2650 TH_LOG("Kernel does not support seccomp syscall!"); 2651 } 2652 ASSERT_EQ(0, ret) { 2653 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!"); 2654 } 2655 tsync_start_sibling(&self->sibling[0]); 2656 tsync_start_sibling(&self->sibling[1]); 2657 2658 while (self->sibling_count < TSYNC_SIBLINGS) { 2659 sem_wait(&self->started); 2660 self->sibling_count++; 2661 } 2662 2663 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, 2664 &self->apply_prog); 2665 ASSERT_EQ(0, ret) { 2666 TH_LOG("Could install filter on all threads!"); 2667 } 2668 /* Tell the siblings to test the policy */ 2669 pthread_mutex_lock(&self->mutex); 2670 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { 2671 TH_LOG("cond broadcast non-zero"); 2672 } 2673 pthread_mutex_unlock(&self->mutex); 2674 /* Ensure they are both killed and don't exit cleanly. */ 2675 PTHREAD_JOIN(self->sibling[0].tid, &status); 2676 EXPECT_EQ(0x0, (long)status); 2677 PTHREAD_JOIN(self->sibling[1].tid, &status); 2678 EXPECT_EQ(0x0, (long)status); 2679 } 2680 2681 TEST_F(TSYNC, two_sibling_want_nnp) 2682 { 2683 void *status; 2684 2685 /* start siblings before any prctl() operations */ 2686 tsync_start_sibling(&self->sibling[0]); 2687 tsync_start_sibling(&self->sibling[1]); 2688 while (self->sibling_count < TSYNC_SIBLINGS) { 2689 sem_wait(&self->started); 2690 self->sibling_count++; 2691 } 2692 2693 /* Tell the siblings to test no policy */ 2694 pthread_mutex_lock(&self->mutex); 2695 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { 2696 TH_LOG("cond broadcast non-zero"); 2697 } 2698 pthread_mutex_unlock(&self->mutex); 2699 2700 /* Ensure they are both upset about lacking nnp. */ 2701 PTHREAD_JOIN(self->sibling[0].tid, &status); 2702 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS, (long)status); 2703 PTHREAD_JOIN(self->sibling[1].tid, &status); 2704 EXPECT_EQ(SIBLING_EXIT_NEWPRIVS, (long)status); 2705 } 2706 2707 TEST_F(TSYNC, two_siblings_with_no_filter) 2708 { 2709 long ret; 2710 void *status; 2711 2712 /* start siblings before any prctl() operations */ 2713 tsync_start_sibling(&self->sibling[0]); 2714 tsync_start_sibling(&self->sibling[1]); 2715 while (self->sibling_count < TSYNC_SIBLINGS) { 2716 sem_wait(&self->started); 2717 self->sibling_count++; 2718 } 2719 2720 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { 2721 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2722 } 2723 2724 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, 2725 &self->apply_prog); 2726 ASSERT_NE(ENOSYS, errno) { 2727 TH_LOG("Kernel does not support seccomp syscall!"); 2728 } 2729 ASSERT_EQ(0, ret) { 2730 TH_LOG("Could install filter on all threads!"); 2731 } 2732 2733 /* Tell the siblings to test the policy */ 2734 pthread_mutex_lock(&self->mutex); 2735 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { 2736 TH_LOG("cond broadcast non-zero"); 2737 } 2738 pthread_mutex_unlock(&self->mutex); 2739 2740 /* Ensure they are both killed and don't exit cleanly. */ 2741 PTHREAD_JOIN(self->sibling[0].tid, &status); 2742 EXPECT_EQ(0x0, (long)status); 2743 PTHREAD_JOIN(self->sibling[1].tid, &status); 2744 EXPECT_EQ(0x0, (long)status); 2745 } 2746 2747 TEST_F(TSYNC, two_siblings_with_one_divergence) 2748 { 2749 long ret; 2750 void *status; 2751 2752 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { 2753 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2754 } 2755 2756 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog); 2757 ASSERT_NE(ENOSYS, errno) { 2758 TH_LOG("Kernel does not support seccomp syscall!"); 2759 } 2760 ASSERT_EQ(0, ret) { 2761 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!"); 2762 } 2763 self->sibling[0].diverge = 1; 2764 tsync_start_sibling(&self->sibling[0]); 2765 tsync_start_sibling(&self->sibling[1]); 2766 2767 while (self->sibling_count < TSYNC_SIBLINGS) { 2768 sem_wait(&self->started); 2769 self->sibling_count++; 2770 } 2771 2772 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, 2773 &self->apply_prog); 2774 ASSERT_EQ(self->sibling[0].system_tid, ret) { 2775 TH_LOG("Did not fail on diverged sibling."); 2776 } 2777 2778 /* Wake the threads */ 2779 pthread_mutex_lock(&self->mutex); 2780 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { 2781 TH_LOG("cond broadcast non-zero"); 2782 } 2783 pthread_mutex_unlock(&self->mutex); 2784 2785 /* Ensure they are both unkilled. */ 2786 PTHREAD_JOIN(self->sibling[0].tid, &status); 2787 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); 2788 PTHREAD_JOIN(self->sibling[1].tid, &status); 2789 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); 2790 } 2791 2792 TEST_F(TSYNC, two_siblings_with_one_divergence_no_tid_in_err) 2793 { 2794 long ret, flags; 2795 void *status; 2796 2797 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { 2798 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2799 } 2800 2801 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog); 2802 ASSERT_NE(ENOSYS, errno) { 2803 TH_LOG("Kernel does not support seccomp syscall!"); 2804 } 2805 ASSERT_EQ(0, ret) { 2806 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!"); 2807 } 2808 self->sibling[0].diverge = 1; 2809 tsync_start_sibling(&self->sibling[0]); 2810 tsync_start_sibling(&self->sibling[1]); 2811 2812 while (self->sibling_count < TSYNC_SIBLINGS) { 2813 sem_wait(&self->started); 2814 self->sibling_count++; 2815 } 2816 2817 flags = SECCOMP_FILTER_FLAG_TSYNC | \ 2818 SECCOMP_FILTER_FLAG_TSYNC_ESRCH; 2819 ret = seccomp(SECCOMP_SET_MODE_FILTER, flags, &self->apply_prog); 2820 ASSERT_EQ(ESRCH, errno) { 2821 TH_LOG("Did not return ESRCH for diverged sibling."); 2822 } 2823 ASSERT_EQ(-1, ret) { 2824 TH_LOG("Did not fail on diverged sibling."); 2825 } 2826 2827 /* Wake the threads */ 2828 pthread_mutex_lock(&self->mutex); 2829 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { 2830 TH_LOG("cond broadcast non-zero"); 2831 } 2832 pthread_mutex_unlock(&self->mutex); 2833 2834 /* Ensure they are both unkilled. */ 2835 PTHREAD_JOIN(self->sibling[0].tid, &status); 2836 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); 2837 PTHREAD_JOIN(self->sibling[1].tid, &status); 2838 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); 2839 } 2840 2841 TEST_F(TSYNC, two_siblings_not_under_filter) 2842 { 2843 long ret, sib; 2844 void *status; 2845 struct timespec delay = { .tv_nsec = 100000000 }; 2846 2847 ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { 2848 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2849 } 2850 2851 /* 2852 * Sibling 0 will have its own seccomp policy 2853 * and Sibling 1 will not be under seccomp at 2854 * all. Sibling 1 will enter seccomp and 0 2855 * will cause failure. 2856 */ 2857 self->sibling[0].diverge = 1; 2858 tsync_start_sibling(&self->sibling[0]); 2859 tsync_start_sibling(&self->sibling[1]); 2860 2861 while (self->sibling_count < TSYNC_SIBLINGS) { 2862 sem_wait(&self->started); 2863 self->sibling_count++; 2864 } 2865 2866 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog); 2867 ASSERT_NE(ENOSYS, errno) { 2868 TH_LOG("Kernel does not support seccomp syscall!"); 2869 } 2870 ASSERT_EQ(0, ret) { 2871 TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!"); 2872 } 2873 2874 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, 2875 &self->apply_prog); 2876 ASSERT_EQ(ret, self->sibling[0].system_tid) { 2877 TH_LOG("Did not fail on diverged sibling."); 2878 } 2879 sib = 1; 2880 if (ret == self->sibling[0].system_tid) 2881 sib = 0; 2882 2883 pthread_mutex_lock(&self->mutex); 2884 2885 /* Increment the other siblings num_waits so we can clean up 2886 * the one we just saw. 2887 */ 2888 self->sibling[!sib].num_waits += 1; 2889 2890 /* Signal the thread to clean up*/ 2891 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { 2892 TH_LOG("cond broadcast non-zero"); 2893 } 2894 pthread_mutex_unlock(&self->mutex); 2895 PTHREAD_JOIN(self->sibling[sib].tid, &status); 2896 EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); 2897 /* Poll for actual task death. pthread_join doesn't guarantee it. */ 2898 while (!kill(self->sibling[sib].system_tid, 0)) 2899 nanosleep(&delay, NULL); 2900 /* Switch to the remaining sibling */ 2901 sib = !sib; 2902 2903 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, 2904 &self->apply_prog); 2905 ASSERT_EQ(0, ret) { 2906 TH_LOG("Expected the remaining sibling to sync"); 2907 }; 2908 2909 pthread_mutex_lock(&self->mutex); 2910 2911 /* If remaining sibling didn't have a chance to wake up during 2912 * the first broadcast, manually reduce the num_waits now. 2913 */ 2914 if (self->sibling[sib].num_waits > 1) 2915 self->sibling[sib].num_waits = 1; 2916 ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { 2917 TH_LOG("cond broadcast non-zero"); 2918 } 2919 pthread_mutex_unlock(&self->mutex); 2920 PTHREAD_JOIN(self->sibling[sib].tid, &status); 2921 EXPECT_EQ(0, (long)status); 2922 /* Poll for actual task death. pthread_join doesn't guarantee it. */ 2923 while (!kill(self->sibling[sib].system_tid, 0)) 2924 nanosleep(&delay, NULL); 2925 2926 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, 2927 &self->apply_prog); 2928 ASSERT_EQ(0, ret); /* just us chickens */ 2929 } 2930 2931 /* Make sure restarted syscalls are seen directly as "restart_syscall". */ 2932 TEST(syscall_restart) 2933 { 2934 long ret; 2935 unsigned long msg; 2936 pid_t child_pid; 2937 int pipefd[2]; 2938 int status; 2939 siginfo_t info = { }; 2940 struct sock_filter filter[] = { 2941 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2942 offsetof(struct seccomp_data, nr)), 2943 2944 #ifdef __NR_sigreturn 2945 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_sigreturn, 7, 0), 2946 #endif 2947 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 6, 0), 2948 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_exit, 5, 0), 2949 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_rt_sigreturn, 4, 0), 2950 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_nanosleep, 5, 0), 2951 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_clock_nanosleep, 4, 0), 2952 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_restart_syscall, 4, 0), 2953 2954 /* Allow __NR_write for easy logging. */ 2955 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_write, 0, 1), 2956 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 2957 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), 2958 /* The nanosleep jump target. */ 2959 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE|0x100), 2960 /* The restart_syscall jump target. */ 2961 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE|0x200), 2962 }; 2963 struct sock_fprog prog = { 2964 .len = (unsigned short)ARRAY_SIZE(filter), 2965 .filter = filter, 2966 }; 2967 #if defined(__arm__) 2968 struct utsname utsbuf; 2969 #endif 2970 2971 ASSERT_EQ(0, pipe(pipefd)); 2972 2973 child_pid = fork(); 2974 ASSERT_LE(0, child_pid); 2975 if (child_pid == 0) { 2976 /* Child uses EXPECT not ASSERT to deliver status correctly. */ 2977 char buf = ' '; 2978 struct timespec timeout = { }; 2979 2980 /* Attach parent as tracer and stop. */ 2981 EXPECT_EQ(0, ptrace(PTRACE_TRACEME)); 2982 EXPECT_EQ(0, raise(SIGSTOP)); 2983 2984 EXPECT_EQ(0, close(pipefd[1])); 2985 2986 EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { 2987 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 2988 } 2989 2990 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); 2991 EXPECT_EQ(0, ret) { 2992 TH_LOG("Failed to install filter!"); 2993 } 2994 2995 EXPECT_EQ(1, read(pipefd[0], &buf, 1)) { 2996 TH_LOG("Failed to read() sync from parent"); 2997 } 2998 EXPECT_EQ('.', buf) { 2999 TH_LOG("Failed to get sync data from read()"); 3000 } 3001 3002 /* Start nanosleep to be interrupted. */ 3003 timeout.tv_sec = 1; 3004 errno = 0; 3005 EXPECT_EQ(0, nanosleep(&timeout, NULL)) { 3006 TH_LOG("Call to nanosleep() failed (errno %d)", errno); 3007 } 3008 3009 /* Read final sync from parent. */ 3010 EXPECT_EQ(1, read(pipefd[0], &buf, 1)) { 3011 TH_LOG("Failed final read() from parent"); 3012 } 3013 EXPECT_EQ('!', buf) { 3014 TH_LOG("Failed to get final data from read()"); 3015 } 3016 3017 /* Directly report the status of our test harness results. */ 3018 syscall(__NR_exit, _metadata->passed ? EXIT_SUCCESS 3019 : EXIT_FAILURE); 3020 } 3021 EXPECT_EQ(0, close(pipefd[0])); 3022 3023 /* Attach to child, setup options, and release. */ 3024 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 3025 ASSERT_EQ(true, WIFSTOPPED(status)); 3026 ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS, child_pid, NULL, 3027 PTRACE_O_TRACESECCOMP)); 3028 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); 3029 ASSERT_EQ(1, write(pipefd[1], ".", 1)); 3030 3031 /* Wait for nanosleep() to start. */ 3032 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 3033 ASSERT_EQ(true, WIFSTOPPED(status)); 3034 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)); 3035 ASSERT_EQ(PTRACE_EVENT_SECCOMP, (status >> 16)); 3036 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG, child_pid, NULL, &msg)); 3037 ASSERT_EQ(0x100, msg); 3038 ret = get_syscall(_metadata, child_pid); 3039 EXPECT_TRUE(ret == __NR_nanosleep || ret == __NR_clock_nanosleep); 3040 3041 /* Might as well check siginfo for sanity while we're here. */ 3042 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child_pid, NULL, &info)); 3043 ASSERT_EQ(SIGTRAP, info.si_signo); 3044 ASSERT_EQ(SIGTRAP | (PTRACE_EVENT_SECCOMP << 8), info.si_code); 3045 EXPECT_EQ(0, info.si_errno); 3046 EXPECT_EQ(getuid(), info.si_uid); 3047 /* Verify signal delivery came from child (seccomp-triggered). */ 3048 EXPECT_EQ(child_pid, info.si_pid); 3049 3050 /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */ 3051 ASSERT_EQ(0, kill(child_pid, SIGSTOP)); 3052 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); 3053 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 3054 ASSERT_EQ(true, WIFSTOPPED(status)); 3055 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)); 3056 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child_pid, NULL, &info)); 3057 /* 3058 * There is no siginfo on SIGSTOP any more, so we can't verify 3059 * signal delivery came from parent now (getpid() == info.si_pid). 3060 * https://lkml.kernel.org/r/CAGXu5jJaZAOzP1qFz66tYrtbuywqb+UN2SOA1VLHpCCOiYvYeg@mail.gmail.com 3061 * At least verify the SIGSTOP via PTRACE_GETSIGINFO. 3062 */ 3063 EXPECT_EQ(SIGSTOP, info.si_signo); 3064 3065 /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */ 3066 ASSERT_EQ(0, kill(child_pid, SIGCONT)); 3067 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); 3068 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 3069 ASSERT_EQ(true, WIFSTOPPED(status)); 3070 ASSERT_EQ(SIGCONT, WSTOPSIG(status)); 3071 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); 3072 3073 /* Wait for restart_syscall() to start. */ 3074 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 3075 ASSERT_EQ(true, WIFSTOPPED(status)); 3076 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)); 3077 ASSERT_EQ(PTRACE_EVENT_SECCOMP, (status >> 16)); 3078 ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG, child_pid, NULL, &msg)); 3079 3080 ASSERT_EQ(0x200, msg); 3081 ret = get_syscall(_metadata, child_pid); 3082 #if defined(__arm__) 3083 /* 3084 * FIXME: 3085 * - native ARM registers do NOT expose true syscall. 3086 * - compat ARM registers on ARM64 DO expose true syscall. 3087 */ 3088 ASSERT_EQ(0, uname(&utsbuf)); 3089 if (strncmp(utsbuf.machine, "arm", 3) == 0) { 3090 EXPECT_EQ(__NR_nanosleep, ret); 3091 } else 3092 #endif 3093 { 3094 EXPECT_EQ(__NR_restart_syscall, ret); 3095 } 3096 3097 /* Write again to end test. */ 3098 ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); 3099 ASSERT_EQ(1, write(pipefd[1], "!", 1)); 3100 EXPECT_EQ(0, close(pipefd[1])); 3101 3102 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); 3103 if (WIFSIGNALED(status) || WEXITSTATUS(status)) 3104 _metadata->passed = 0; 3105 } 3106 3107 TEST_SIGNAL(filter_flag_log, SIGSYS) 3108 { 3109 struct sock_filter allow_filter[] = { 3110 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 3111 }; 3112 struct sock_filter kill_filter[] = { 3113 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 3114 offsetof(struct seccomp_data, nr)), 3115 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1), 3116 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), 3117 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 3118 }; 3119 struct sock_fprog allow_prog = { 3120 .len = (unsigned short)ARRAY_SIZE(allow_filter), 3121 .filter = allow_filter, 3122 }; 3123 struct sock_fprog kill_prog = { 3124 .len = (unsigned short)ARRAY_SIZE(kill_filter), 3125 .filter = kill_filter, 3126 }; 3127 long ret; 3128 pid_t parent = getppid(); 3129 3130 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3131 ASSERT_EQ(0, ret); 3132 3133 /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */ 3134 ret = seccomp(SECCOMP_SET_MODE_STRICT, SECCOMP_FILTER_FLAG_LOG, 3135 &allow_prog); 3136 ASSERT_NE(ENOSYS, errno) { 3137 TH_LOG("Kernel does not support seccomp syscall!"); 3138 } 3139 EXPECT_NE(0, ret) { 3140 TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!"); 3141 } 3142 EXPECT_EQ(EINVAL, errno) { 3143 TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!"); 3144 } 3145 3146 /* Verify that a simple, permissive filter can be added with no flags */ 3147 ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &allow_prog); 3148 EXPECT_EQ(0, ret); 3149 3150 /* See if the same filter can be added with the FILTER_FLAG_LOG flag */ 3151 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_LOG, 3152 &allow_prog); 3153 ASSERT_NE(EINVAL, errno) { 3154 TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!"); 3155 } 3156 EXPECT_EQ(0, ret); 3157 3158 /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */ 3159 ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_LOG, 3160 &kill_prog); 3161 EXPECT_EQ(0, ret); 3162 3163 EXPECT_EQ(parent, syscall(__NR_getppid)); 3164 /* getpid() should never return. */ 3165 EXPECT_EQ(0, syscall(__NR_getpid)); 3166 } 3167 3168 TEST(get_action_avail) 3169 { 3170 __u32 actions[] = { SECCOMP_RET_KILL_THREAD, SECCOMP_RET_TRAP, 3171 SECCOMP_RET_ERRNO, SECCOMP_RET_TRACE, 3172 SECCOMP_RET_LOG, SECCOMP_RET_ALLOW }; 3173 __u32 unknown_action = 0x10000000U; 3174 int i; 3175 long ret; 3176 3177 ret = seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &actions[0]); 3178 ASSERT_NE(ENOSYS, errno) { 3179 TH_LOG("Kernel does not support seccomp syscall!"); 3180 } 3181 ASSERT_NE(EINVAL, errno) { 3182 TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!"); 3183 } 3184 EXPECT_EQ(ret, 0); 3185 3186 for (i = 0; i < ARRAY_SIZE(actions); i++) { 3187 ret = seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &actions[i]); 3188 EXPECT_EQ(ret, 0) { 3189 TH_LOG("Expected action (0x%X) not available!", 3190 actions[i]); 3191 } 3192 } 3193 3194 /* Check that an unknown action is handled properly (EOPNOTSUPP) */ 3195 ret = seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &unknown_action); 3196 EXPECT_EQ(ret, -1); 3197 EXPECT_EQ(errno, EOPNOTSUPP); 3198 } 3199 3200 TEST(get_metadata) 3201 { 3202 pid_t pid; 3203 int pipefd[2]; 3204 char buf; 3205 struct seccomp_metadata md; 3206 long ret; 3207 3208 /* Only real root can get metadata. */ 3209 if (geteuid()) { 3210 SKIP(return, "get_metadata requires real root"); 3211 return; 3212 } 3213 3214 ASSERT_EQ(0, pipe(pipefd)); 3215 3216 pid = fork(); 3217 ASSERT_GE(pid, 0); 3218 if (pid == 0) { 3219 struct sock_filter filter[] = { 3220 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 3221 }; 3222 struct sock_fprog prog = { 3223 .len = (unsigned short)ARRAY_SIZE(filter), 3224 .filter = filter, 3225 }; 3226 3227 /* one with log, one without */ 3228 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 3229 SECCOMP_FILTER_FLAG_LOG, &prog)); 3230 EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog)); 3231 3232 EXPECT_EQ(0, close(pipefd[0])); 3233 ASSERT_EQ(1, write(pipefd[1], "1", 1)); 3234 ASSERT_EQ(0, close(pipefd[1])); 3235 3236 while (1) 3237 sleep(100); 3238 } 3239 3240 ASSERT_EQ(0, close(pipefd[1])); 3241 ASSERT_EQ(1, read(pipefd[0], &buf, 1)); 3242 3243 ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid)); 3244 ASSERT_EQ(pid, waitpid(pid, NULL, 0)); 3245 3246 /* Past here must not use ASSERT or child process is never killed. */ 3247 3248 md.filter_off = 0; 3249 errno = 0; 3250 ret = ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md); 3251 EXPECT_EQ(sizeof(md), ret) { 3252 if (errno == EINVAL) 3253 SKIP(goto skip, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)"); 3254 } 3255 3256 EXPECT_EQ(md.flags, SECCOMP_FILTER_FLAG_LOG); 3257 EXPECT_EQ(md.filter_off, 0); 3258 3259 md.filter_off = 1; 3260 ret = ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md); 3261 EXPECT_EQ(sizeof(md), ret); 3262 EXPECT_EQ(md.flags, 0); 3263 EXPECT_EQ(md.filter_off, 1); 3264 3265 skip: 3266 ASSERT_EQ(0, kill(pid, SIGKILL)); 3267 } 3268 3269 static int user_notif_syscall(int nr, unsigned int flags) 3270 { 3271 struct sock_filter filter[] = { 3272 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 3273 offsetof(struct seccomp_data, nr)), 3274 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, nr, 0, 1), 3275 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_USER_NOTIF), 3276 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 3277 }; 3278 3279 struct sock_fprog prog = { 3280 .len = (unsigned short)ARRAY_SIZE(filter), 3281 .filter = filter, 3282 }; 3283 3284 return seccomp(SECCOMP_SET_MODE_FILTER, flags, &prog); 3285 } 3286 3287 #define USER_NOTIF_MAGIC INT_MAX 3288 TEST(user_notification_basic) 3289 { 3290 pid_t pid; 3291 long ret; 3292 int status, listener; 3293 struct seccomp_notif req = {}; 3294 struct seccomp_notif_resp resp = {}; 3295 struct pollfd pollfd; 3296 3297 struct sock_filter filter[] = { 3298 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), 3299 }; 3300 struct sock_fprog prog = { 3301 .len = (unsigned short)ARRAY_SIZE(filter), 3302 .filter = filter, 3303 }; 3304 3305 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3306 ASSERT_EQ(0, ret) { 3307 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3308 } 3309 3310 pid = fork(); 3311 ASSERT_GE(pid, 0); 3312 3313 /* Check that we get -ENOSYS with no listener attached */ 3314 if (pid == 0) { 3315 if (user_notif_syscall(__NR_getppid, 0) < 0) 3316 exit(1); 3317 ret = syscall(__NR_getppid); 3318 exit(ret >= 0 || errno != ENOSYS); 3319 } 3320 3321 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3322 EXPECT_EQ(true, WIFEXITED(status)); 3323 EXPECT_EQ(0, WEXITSTATUS(status)); 3324 3325 /* Add some no-op filters for grins. */ 3326 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog), 0); 3327 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog), 0); 3328 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog), 0); 3329 EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog), 0); 3330 3331 /* Check that the basic notification machinery works */ 3332 listener = user_notif_syscall(__NR_getppid, 3333 SECCOMP_FILTER_FLAG_NEW_LISTENER); 3334 ASSERT_GE(listener, 0); 3335 3336 /* Installing a second listener in the chain should EBUSY */ 3337 EXPECT_EQ(user_notif_syscall(__NR_getppid, 3338 SECCOMP_FILTER_FLAG_NEW_LISTENER), 3339 -1); 3340 EXPECT_EQ(errno, EBUSY); 3341 3342 pid = fork(); 3343 ASSERT_GE(pid, 0); 3344 3345 if (pid == 0) { 3346 ret = syscall(__NR_getppid); 3347 exit(ret != USER_NOTIF_MAGIC); 3348 } 3349 3350 pollfd.fd = listener; 3351 pollfd.events = POLLIN | POLLOUT; 3352 3353 EXPECT_GT(poll(&pollfd, 1, -1), 0); 3354 EXPECT_EQ(pollfd.revents, POLLIN); 3355 3356 /* Test that we can't pass garbage to the kernel. */ 3357 memset(&req, 0, sizeof(req)); 3358 req.pid = -1; 3359 errno = 0; 3360 ret = ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req); 3361 EXPECT_EQ(-1, ret); 3362 EXPECT_EQ(EINVAL, errno); 3363 3364 if (ret) { 3365 req.pid = 0; 3366 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 3367 } 3368 3369 pollfd.fd = listener; 3370 pollfd.events = POLLIN | POLLOUT; 3371 3372 EXPECT_GT(poll(&pollfd, 1, -1), 0); 3373 EXPECT_EQ(pollfd.revents, POLLOUT); 3374 3375 EXPECT_EQ(req.data.nr, __NR_getppid); 3376 3377 resp.id = req.id; 3378 resp.error = 0; 3379 resp.val = USER_NOTIF_MAGIC; 3380 3381 /* check that we make sure flags == 0 */ 3382 resp.flags = 1; 3383 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1); 3384 EXPECT_EQ(errno, EINVAL); 3385 3386 resp.flags = 0; 3387 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); 3388 3389 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3390 EXPECT_EQ(true, WIFEXITED(status)); 3391 EXPECT_EQ(0, WEXITSTATUS(status)); 3392 } 3393 3394 TEST(user_notification_with_tsync) 3395 { 3396 int ret; 3397 unsigned int flags; 3398 3399 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3400 ASSERT_EQ(0, ret) { 3401 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3402 } 3403 3404 /* these were exclusive */ 3405 flags = SECCOMP_FILTER_FLAG_NEW_LISTENER | 3406 SECCOMP_FILTER_FLAG_TSYNC; 3407 ASSERT_EQ(-1, user_notif_syscall(__NR_getppid, flags)); 3408 ASSERT_EQ(EINVAL, errno); 3409 3410 /* but now they're not */ 3411 flags |= SECCOMP_FILTER_FLAG_TSYNC_ESRCH; 3412 ret = user_notif_syscall(__NR_getppid, flags); 3413 close(ret); 3414 ASSERT_LE(0, ret); 3415 } 3416 3417 TEST(user_notification_kill_in_middle) 3418 { 3419 pid_t pid; 3420 long ret; 3421 int listener; 3422 struct seccomp_notif req = {}; 3423 struct seccomp_notif_resp resp = {}; 3424 3425 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3426 ASSERT_EQ(0, ret) { 3427 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3428 } 3429 3430 listener = user_notif_syscall(__NR_getppid, 3431 SECCOMP_FILTER_FLAG_NEW_LISTENER); 3432 ASSERT_GE(listener, 0); 3433 3434 /* 3435 * Check that nothing bad happens when we kill the task in the middle 3436 * of a syscall. 3437 */ 3438 pid = fork(); 3439 ASSERT_GE(pid, 0); 3440 3441 if (pid == 0) { 3442 ret = syscall(__NR_getppid); 3443 exit(ret != USER_NOTIF_MAGIC); 3444 } 3445 3446 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 3447 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &req.id), 0); 3448 3449 EXPECT_EQ(kill(pid, SIGKILL), 0); 3450 EXPECT_EQ(waitpid(pid, NULL, 0), pid); 3451 3452 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &req.id), -1); 3453 3454 resp.id = req.id; 3455 ret = ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp); 3456 EXPECT_EQ(ret, -1); 3457 EXPECT_EQ(errno, ENOENT); 3458 } 3459 3460 static int handled = -1; 3461 3462 static void signal_handler(int signal) 3463 { 3464 if (write(handled, "c", 1) != 1) 3465 perror("write from signal"); 3466 } 3467 3468 TEST(user_notification_signal) 3469 { 3470 pid_t pid; 3471 long ret; 3472 int status, listener, sk_pair[2]; 3473 struct seccomp_notif req = {}; 3474 struct seccomp_notif_resp resp = {}; 3475 char c; 3476 3477 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3478 ASSERT_EQ(0, ret) { 3479 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3480 } 3481 3482 ASSERT_EQ(socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair), 0); 3483 3484 listener = user_notif_syscall(__NR_gettid, 3485 SECCOMP_FILTER_FLAG_NEW_LISTENER); 3486 ASSERT_GE(listener, 0); 3487 3488 pid = fork(); 3489 ASSERT_GE(pid, 0); 3490 3491 if (pid == 0) { 3492 close(sk_pair[0]); 3493 handled = sk_pair[1]; 3494 if (signal(SIGUSR1, signal_handler) == SIG_ERR) { 3495 perror("signal"); 3496 exit(1); 3497 } 3498 /* 3499 * ERESTARTSYS behavior is a bit hard to test, because we need 3500 * to rely on a signal that has not yet been handled. Let's at 3501 * least check that the error code gets propagated through, and 3502 * hope that it doesn't break when there is actually a signal :) 3503 */ 3504 ret = syscall(__NR_gettid); 3505 exit(!(ret == -1 && errno == 512)); 3506 } 3507 3508 close(sk_pair[1]); 3509 3510 memset(&req, 0, sizeof(req)); 3511 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 3512 3513 EXPECT_EQ(kill(pid, SIGUSR1), 0); 3514 3515 /* 3516 * Make sure the signal really is delivered, which means we're not 3517 * stuck in the user notification code any more and the notification 3518 * should be dead. 3519 */ 3520 EXPECT_EQ(read(sk_pair[0], &c, 1), 1); 3521 3522 resp.id = req.id; 3523 resp.error = -EPERM; 3524 resp.val = 0; 3525 3526 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1); 3527 EXPECT_EQ(errno, ENOENT); 3528 3529 memset(&req, 0, sizeof(req)); 3530 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 3531 3532 resp.id = req.id; 3533 resp.error = -512; /* -ERESTARTSYS */ 3534 resp.val = 0; 3535 3536 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); 3537 3538 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3539 EXPECT_EQ(true, WIFEXITED(status)); 3540 EXPECT_EQ(0, WEXITSTATUS(status)); 3541 } 3542 3543 TEST(user_notification_closed_listener) 3544 { 3545 pid_t pid; 3546 long ret; 3547 int status, listener; 3548 3549 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3550 ASSERT_EQ(0, ret) { 3551 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3552 } 3553 3554 listener = user_notif_syscall(__NR_getppid, 3555 SECCOMP_FILTER_FLAG_NEW_LISTENER); 3556 ASSERT_GE(listener, 0); 3557 3558 /* 3559 * Check that we get an ENOSYS when the listener is closed. 3560 */ 3561 pid = fork(); 3562 ASSERT_GE(pid, 0); 3563 if (pid == 0) { 3564 close(listener); 3565 ret = syscall(__NR_getppid); 3566 exit(ret != -1 && errno != ENOSYS); 3567 } 3568 3569 close(listener); 3570 3571 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3572 EXPECT_EQ(true, WIFEXITED(status)); 3573 EXPECT_EQ(0, WEXITSTATUS(status)); 3574 } 3575 3576 /* 3577 * Check that a pid in a child namespace still shows up as valid in ours. 3578 */ 3579 TEST(user_notification_child_pid_ns) 3580 { 3581 pid_t pid; 3582 int status, listener; 3583 struct seccomp_notif req = {}; 3584 struct seccomp_notif_resp resp = {}; 3585 3586 ASSERT_EQ(unshare(CLONE_NEWUSER | CLONE_NEWPID), 0) { 3587 if (errno == EINVAL) 3588 SKIP(return, "kernel missing CLONE_NEWUSER support"); 3589 }; 3590 3591 listener = user_notif_syscall(__NR_getppid, 3592 SECCOMP_FILTER_FLAG_NEW_LISTENER); 3593 ASSERT_GE(listener, 0); 3594 3595 pid = fork(); 3596 ASSERT_GE(pid, 0); 3597 3598 if (pid == 0) 3599 exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC); 3600 3601 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 3602 EXPECT_EQ(req.pid, pid); 3603 3604 resp.id = req.id; 3605 resp.error = 0; 3606 resp.val = USER_NOTIF_MAGIC; 3607 3608 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); 3609 3610 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3611 EXPECT_EQ(true, WIFEXITED(status)); 3612 EXPECT_EQ(0, WEXITSTATUS(status)); 3613 close(listener); 3614 } 3615 3616 /* 3617 * Check that a pid in a sibling (i.e. unrelated) namespace shows up as 0, i.e. 3618 * invalid. 3619 */ 3620 TEST(user_notification_sibling_pid_ns) 3621 { 3622 pid_t pid, pid2; 3623 int status, listener; 3624 struct seccomp_notif req = {}; 3625 struct seccomp_notif_resp resp = {}; 3626 3627 ASSERT_EQ(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0), 0) { 3628 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3629 } 3630 3631 listener = user_notif_syscall(__NR_getppid, 3632 SECCOMP_FILTER_FLAG_NEW_LISTENER); 3633 ASSERT_GE(listener, 0); 3634 3635 pid = fork(); 3636 ASSERT_GE(pid, 0); 3637 3638 if (pid == 0) { 3639 ASSERT_EQ(unshare(CLONE_NEWPID), 0); 3640 3641 pid2 = fork(); 3642 ASSERT_GE(pid2, 0); 3643 3644 if (pid2 == 0) 3645 exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC); 3646 3647 EXPECT_EQ(waitpid(pid2, &status, 0), pid2); 3648 EXPECT_EQ(true, WIFEXITED(status)); 3649 EXPECT_EQ(0, WEXITSTATUS(status)); 3650 exit(WEXITSTATUS(status)); 3651 } 3652 3653 /* Create the sibling ns, and sibling in it. */ 3654 ASSERT_EQ(unshare(CLONE_NEWPID), 0) { 3655 if (errno == EPERM) 3656 SKIP(return, "CLONE_NEWPID requires CAP_SYS_ADMIN"); 3657 } 3658 ASSERT_EQ(errno, 0); 3659 3660 pid2 = fork(); 3661 ASSERT_GE(pid2, 0); 3662 3663 if (pid2 == 0) { 3664 ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 3665 /* 3666 * The pid should be 0, i.e. the task is in some namespace that 3667 * we can't "see". 3668 */ 3669 EXPECT_EQ(req.pid, 0); 3670 3671 resp.id = req.id; 3672 resp.error = 0; 3673 resp.val = USER_NOTIF_MAGIC; 3674 3675 ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); 3676 exit(0); 3677 } 3678 3679 close(listener); 3680 3681 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3682 EXPECT_EQ(true, WIFEXITED(status)); 3683 EXPECT_EQ(0, WEXITSTATUS(status)); 3684 3685 EXPECT_EQ(waitpid(pid2, &status, 0), pid2); 3686 EXPECT_EQ(true, WIFEXITED(status)); 3687 EXPECT_EQ(0, WEXITSTATUS(status)); 3688 } 3689 3690 TEST(user_notification_fault_recv) 3691 { 3692 pid_t pid; 3693 int status, listener; 3694 struct seccomp_notif req = {}; 3695 struct seccomp_notif_resp resp = {}; 3696 3697 ASSERT_EQ(unshare(CLONE_NEWUSER), 0); 3698 3699 listener = user_notif_syscall(__NR_getppid, 3700 SECCOMP_FILTER_FLAG_NEW_LISTENER); 3701 ASSERT_GE(listener, 0); 3702 3703 pid = fork(); 3704 ASSERT_GE(pid, 0); 3705 3706 if (pid == 0) 3707 exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC); 3708 3709 /* Do a bad recv() */ 3710 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, NULL), -1); 3711 EXPECT_EQ(errno, EFAULT); 3712 3713 /* We should still be able to receive this notification, though. */ 3714 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 3715 EXPECT_EQ(req.pid, pid); 3716 3717 resp.id = req.id; 3718 resp.error = 0; 3719 resp.val = USER_NOTIF_MAGIC; 3720 3721 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); 3722 3723 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3724 EXPECT_EQ(true, WIFEXITED(status)); 3725 EXPECT_EQ(0, WEXITSTATUS(status)); 3726 } 3727 3728 TEST(seccomp_get_notif_sizes) 3729 { 3730 struct seccomp_notif_sizes sizes; 3731 3732 ASSERT_EQ(seccomp(SECCOMP_GET_NOTIF_SIZES, 0, &sizes), 0); 3733 EXPECT_EQ(sizes.seccomp_notif, sizeof(struct seccomp_notif)); 3734 EXPECT_EQ(sizes.seccomp_notif_resp, sizeof(struct seccomp_notif_resp)); 3735 } 3736 3737 TEST(user_notification_continue) 3738 { 3739 pid_t pid; 3740 long ret; 3741 int status, listener; 3742 struct seccomp_notif req = {}; 3743 struct seccomp_notif_resp resp = {}; 3744 struct pollfd pollfd; 3745 3746 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3747 ASSERT_EQ(0, ret) { 3748 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3749 } 3750 3751 listener = user_notif_syscall(__NR_dup, SECCOMP_FILTER_FLAG_NEW_LISTENER); 3752 ASSERT_GE(listener, 0); 3753 3754 pid = fork(); 3755 ASSERT_GE(pid, 0); 3756 3757 if (pid == 0) { 3758 int dup_fd, pipe_fds[2]; 3759 pid_t self; 3760 3761 ASSERT_GE(pipe(pipe_fds), 0); 3762 3763 dup_fd = dup(pipe_fds[0]); 3764 ASSERT_GE(dup_fd, 0); 3765 EXPECT_NE(pipe_fds[0], dup_fd); 3766 3767 self = getpid(); 3768 ASSERT_EQ(filecmp(self, self, pipe_fds[0], dup_fd), 0); 3769 exit(0); 3770 } 3771 3772 pollfd.fd = listener; 3773 pollfd.events = POLLIN | POLLOUT; 3774 3775 EXPECT_GT(poll(&pollfd, 1, -1), 0); 3776 EXPECT_EQ(pollfd.revents, POLLIN); 3777 3778 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 3779 3780 pollfd.fd = listener; 3781 pollfd.events = POLLIN | POLLOUT; 3782 3783 EXPECT_GT(poll(&pollfd, 1, -1), 0); 3784 EXPECT_EQ(pollfd.revents, POLLOUT); 3785 3786 EXPECT_EQ(req.data.nr, __NR_dup); 3787 3788 resp.id = req.id; 3789 resp.flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE; 3790 3791 /* 3792 * Verify that setting SECCOMP_USER_NOTIF_FLAG_CONTINUE enforces other 3793 * args be set to 0. 3794 */ 3795 resp.error = 0; 3796 resp.val = USER_NOTIF_MAGIC; 3797 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1); 3798 EXPECT_EQ(errno, EINVAL); 3799 3800 resp.error = USER_NOTIF_MAGIC; 3801 resp.val = 0; 3802 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1); 3803 EXPECT_EQ(errno, EINVAL); 3804 3805 resp.error = 0; 3806 resp.val = 0; 3807 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0) { 3808 if (errno == EINVAL) 3809 SKIP(goto skip, "Kernel does not support SECCOMP_USER_NOTIF_FLAG_CONTINUE"); 3810 } 3811 3812 skip: 3813 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3814 EXPECT_EQ(true, WIFEXITED(status)); 3815 EXPECT_EQ(0, WEXITSTATUS(status)) { 3816 if (WEXITSTATUS(status) == 2) { 3817 SKIP(return, "Kernel does not support kcmp() syscall"); 3818 return; 3819 } 3820 } 3821 } 3822 3823 TEST(user_notification_filter_empty) 3824 { 3825 pid_t pid; 3826 long ret; 3827 int status; 3828 struct pollfd pollfd; 3829 struct __clone_args args = { 3830 .flags = CLONE_FILES, 3831 .exit_signal = SIGCHLD, 3832 }; 3833 3834 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3835 ASSERT_EQ(0, ret) { 3836 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3837 } 3838 3839 pid = sys_clone3(&args, sizeof(args)); 3840 ASSERT_GE(pid, 0); 3841 3842 if (pid == 0) { 3843 int listener; 3844 3845 listener = user_notif_syscall(__NR_mknodat, SECCOMP_FILTER_FLAG_NEW_LISTENER); 3846 if (listener < 0) 3847 _exit(EXIT_FAILURE); 3848 3849 if (dup2(listener, 200) != 200) 3850 _exit(EXIT_FAILURE); 3851 3852 close(listener); 3853 3854 _exit(EXIT_SUCCESS); 3855 } 3856 3857 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3858 EXPECT_EQ(true, WIFEXITED(status)); 3859 EXPECT_EQ(0, WEXITSTATUS(status)); 3860 3861 /* 3862 * The seccomp filter has become unused so we should be notified once 3863 * the kernel gets around to cleaning up task struct. 3864 */ 3865 pollfd.fd = 200; 3866 pollfd.events = POLLHUP; 3867 3868 EXPECT_GT(poll(&pollfd, 1, 2000), 0); 3869 EXPECT_GT((pollfd.revents & POLLHUP) ?: 0, 0); 3870 } 3871 3872 static void *do_thread(void *data) 3873 { 3874 return NULL; 3875 } 3876 3877 TEST(user_notification_filter_empty_threaded) 3878 { 3879 pid_t pid; 3880 long ret; 3881 int status; 3882 struct pollfd pollfd; 3883 struct __clone_args args = { 3884 .flags = CLONE_FILES, 3885 .exit_signal = SIGCHLD, 3886 }; 3887 3888 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3889 ASSERT_EQ(0, ret) { 3890 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3891 } 3892 3893 pid = sys_clone3(&args, sizeof(args)); 3894 ASSERT_GE(pid, 0); 3895 3896 if (pid == 0) { 3897 pid_t pid1, pid2; 3898 int listener, status; 3899 pthread_t thread; 3900 3901 listener = user_notif_syscall(__NR_dup, SECCOMP_FILTER_FLAG_NEW_LISTENER); 3902 if (listener < 0) 3903 _exit(EXIT_FAILURE); 3904 3905 if (dup2(listener, 200) != 200) 3906 _exit(EXIT_FAILURE); 3907 3908 close(listener); 3909 3910 pid1 = fork(); 3911 if (pid1 < 0) 3912 _exit(EXIT_FAILURE); 3913 3914 if (pid1 == 0) 3915 _exit(EXIT_SUCCESS); 3916 3917 pid2 = fork(); 3918 if (pid2 < 0) 3919 _exit(EXIT_FAILURE); 3920 3921 if (pid2 == 0) 3922 _exit(EXIT_SUCCESS); 3923 3924 if (pthread_create(&thread, NULL, do_thread, NULL) || 3925 pthread_join(thread, NULL)) 3926 _exit(EXIT_FAILURE); 3927 3928 if (pthread_create(&thread, NULL, do_thread, NULL) || 3929 pthread_join(thread, NULL)) 3930 _exit(EXIT_FAILURE); 3931 3932 if (waitpid(pid1, &status, 0) != pid1 || !WIFEXITED(status) || 3933 WEXITSTATUS(status)) 3934 _exit(EXIT_FAILURE); 3935 3936 if (waitpid(pid2, &status, 0) != pid2 || !WIFEXITED(status) || 3937 WEXITSTATUS(status)) 3938 _exit(EXIT_FAILURE); 3939 3940 exit(EXIT_SUCCESS); 3941 } 3942 3943 EXPECT_EQ(waitpid(pid, &status, 0), pid); 3944 EXPECT_EQ(true, WIFEXITED(status)); 3945 EXPECT_EQ(0, WEXITSTATUS(status)); 3946 3947 /* 3948 * The seccomp filter has become unused so we should be notified once 3949 * the kernel gets around to cleaning up task struct. 3950 */ 3951 pollfd.fd = 200; 3952 pollfd.events = POLLHUP; 3953 3954 EXPECT_GT(poll(&pollfd, 1, 2000), 0); 3955 EXPECT_GT((pollfd.revents & POLLHUP) ?: 0, 0); 3956 } 3957 3958 TEST(user_notification_addfd) 3959 { 3960 pid_t pid; 3961 long ret; 3962 int status, listener, memfd, fd; 3963 struct seccomp_notif_addfd addfd = {}; 3964 struct seccomp_notif_addfd_small small = {}; 3965 struct seccomp_notif_addfd_big big = {}; 3966 struct seccomp_notif req = {}; 3967 struct seccomp_notif_resp resp = {}; 3968 /* 100 ms */ 3969 struct timespec delay = { .tv_nsec = 100000000 }; 3970 3971 memfd = memfd_create("test", 0); 3972 ASSERT_GE(memfd, 0); 3973 3974 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 3975 ASSERT_EQ(0, ret) { 3976 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 3977 } 3978 3979 /* Check that the basic notification machinery works */ 3980 listener = user_notif_syscall(__NR_getppid, 3981 SECCOMP_FILTER_FLAG_NEW_LISTENER); 3982 ASSERT_GE(listener, 0); 3983 3984 pid = fork(); 3985 ASSERT_GE(pid, 0); 3986 3987 if (pid == 0) { 3988 if (syscall(__NR_getppid) != USER_NOTIF_MAGIC) 3989 exit(1); 3990 exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC); 3991 } 3992 3993 ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 3994 3995 addfd.srcfd = memfd; 3996 addfd.newfd = 0; 3997 addfd.id = req.id; 3998 addfd.flags = 0x0; 3999 4000 /* Verify bad newfd_flags cannot be set */ 4001 addfd.newfd_flags = ~O_CLOEXEC; 4002 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd), -1); 4003 EXPECT_EQ(errno, EINVAL); 4004 addfd.newfd_flags = O_CLOEXEC; 4005 4006 /* Verify bad flags cannot be set */ 4007 addfd.flags = 0xff; 4008 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd), -1); 4009 EXPECT_EQ(errno, EINVAL); 4010 addfd.flags = 0; 4011 4012 /* Verify that remote_fd cannot be set without setting flags */ 4013 addfd.newfd = 1; 4014 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd), -1); 4015 EXPECT_EQ(errno, EINVAL); 4016 addfd.newfd = 0; 4017 4018 /* Verify small size cannot be set */ 4019 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD_SMALL, &small), -1); 4020 EXPECT_EQ(errno, EINVAL); 4021 4022 /* Verify we can't send bits filled in unknown buffer area */ 4023 memset(&big, 0xAA, sizeof(big)); 4024 big.addfd = addfd; 4025 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD_BIG, &big), -1); 4026 EXPECT_EQ(errno, E2BIG); 4027 4028 4029 /* Verify we can set an arbitrary remote fd */ 4030 fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd); 4031 EXPECT_GE(fd, 0); 4032 EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0); 4033 4034 /* Verify we can set an arbitrary remote fd with large size */ 4035 memset(&big, 0x0, sizeof(big)); 4036 big.addfd = addfd; 4037 fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD_BIG, &big); 4038 EXPECT_GE(fd, 0); 4039 4040 /* Verify we can set a specific remote fd */ 4041 addfd.newfd = 42; 4042 addfd.flags = SECCOMP_ADDFD_FLAG_SETFD; 4043 fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd); 4044 EXPECT_EQ(fd, 42); 4045 EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0); 4046 4047 /* Resume syscall */ 4048 resp.id = req.id; 4049 resp.error = 0; 4050 resp.val = USER_NOTIF_MAGIC; 4051 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); 4052 4053 /* 4054 * This sets the ID of the ADD FD to the last request plus 1. The 4055 * notification ID increments 1 per notification. 4056 */ 4057 addfd.id = req.id + 1; 4058 4059 /* This spins until the underlying notification is generated */ 4060 while (ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd) != -1 && 4061 errno != -EINPROGRESS) 4062 nanosleep(&delay, NULL); 4063 4064 memset(&req, 0, sizeof(req)); 4065 ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 4066 ASSERT_EQ(addfd.id, req.id); 4067 4068 resp.id = req.id; 4069 resp.error = 0; 4070 resp.val = USER_NOTIF_MAGIC; 4071 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); 4072 4073 /* Wait for child to finish. */ 4074 EXPECT_EQ(waitpid(pid, &status, 0), pid); 4075 EXPECT_EQ(true, WIFEXITED(status)); 4076 EXPECT_EQ(0, WEXITSTATUS(status)); 4077 4078 close(memfd); 4079 } 4080 4081 TEST(user_notification_addfd_rlimit) 4082 { 4083 pid_t pid; 4084 long ret; 4085 int status, listener, memfd; 4086 struct seccomp_notif_addfd addfd = {}; 4087 struct seccomp_notif req = {}; 4088 struct seccomp_notif_resp resp = {}; 4089 const struct rlimit lim = { 4090 .rlim_cur = 0, 4091 .rlim_max = 0, 4092 }; 4093 4094 memfd = memfd_create("test", 0); 4095 ASSERT_GE(memfd, 0); 4096 4097 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); 4098 ASSERT_EQ(0, ret) { 4099 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); 4100 } 4101 4102 /* Check that the basic notification machinery works */ 4103 listener = user_notif_syscall(__NR_getppid, 4104 SECCOMP_FILTER_FLAG_NEW_LISTENER); 4105 ASSERT_GE(listener, 0); 4106 4107 pid = fork(); 4108 ASSERT_GE(pid, 0); 4109 4110 if (pid == 0) 4111 exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC); 4112 4113 4114 ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); 4115 4116 ASSERT_EQ(prlimit(pid, RLIMIT_NOFILE, &lim, NULL), 0); 4117 4118 addfd.srcfd = memfd; 4119 addfd.newfd_flags = O_CLOEXEC; 4120 addfd.newfd = 0; 4121 addfd.id = req.id; 4122 addfd.flags = 0; 4123 4124 /* Should probably spot check /proc/sys/fs/file-nr */ 4125 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd), -1); 4126 EXPECT_EQ(errno, EMFILE); 4127 4128 addfd.newfd = 100; 4129 addfd.flags = SECCOMP_ADDFD_FLAG_SETFD; 4130 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd), -1); 4131 EXPECT_EQ(errno, EBADF); 4132 4133 resp.id = req.id; 4134 resp.error = 0; 4135 resp.val = USER_NOTIF_MAGIC; 4136 4137 EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); 4138 4139 /* Wait for child to finish. */ 4140 EXPECT_EQ(waitpid(pid, &status, 0), pid); 4141 EXPECT_EQ(true, WIFEXITED(status)); 4142 EXPECT_EQ(0, WEXITSTATUS(status)); 4143 4144 close(memfd); 4145 } 4146 4147 /* 4148 * TODO: 4149 * - expand NNP testing 4150 * - better arch-specific TRACE and TRAP handlers. 4151 * - endianness checking when appropriate 4152 * - 64-bit arg prodding 4153 * - arch value testing (x86 modes especially) 4154 * - verify that FILTER_FLAG_LOG filters generate log messages 4155 * - verify that RET_LOG generates log messages 4156 */ 4157 4158 TEST_HARNESS_MAIN 4159