| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <sys/prctl.h> |
| 6 #include <sys/utsname.h> |
| 7 |
| 5 #include <ostream> | 8 #include <ostream> |
| 6 | 9 |
| 7 #include "sandbox/linux/seccomp-bpf/bpf_tests.h" | 10 #include "sandbox/linux/seccomp-bpf/bpf_tests.h" |
| 11 #include "sandbox/linux/seccomp-bpf/syscall.h" |
| 8 #include "sandbox/linux/seccomp-bpf/verifier.h" | 12 #include "sandbox/linux/seccomp-bpf/verifier.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 14 |
| 11 using namespace playground2; | 15 using namespace playground2; |
| 12 | 16 |
| 13 namespace { | 17 namespace { |
| 14 | 18 |
| 15 const int kExpectedReturnValue = 42; | 19 const int kExpectedReturnValue = 42; |
| 16 | 20 |
| 17 // This test should execute no matter whether we have kernel support. So, | 21 // This test should execute no matter whether we have kernel support. So, |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 for (int syscall_number = static_cast<int>(__ARM_NR_set_tls + 1); | 261 for (int syscall_number = static_cast<int>(__ARM_NR_set_tls + 1); |
| 258 syscall_number <= static_cast<int>(MAX_PRIVATE_SYSCALL); | 262 syscall_number <= static_cast<int>(MAX_PRIVATE_SYSCALL); |
| 259 ++syscall_number) { | 263 ++syscall_number) { |
| 260 errno = 0; | 264 errno = 0; |
| 261 BPF_ASSERT(syscall(syscall_number) == -1); | 265 BPF_ASSERT(syscall(syscall_number) == -1); |
| 262 BPF_ASSERT(errno == ArmPrivateSysnoToErrno(syscall_number)); | 266 BPF_ASSERT(errno == ArmPrivateSysnoToErrno(syscall_number)); |
| 263 } | 267 } |
| 264 } | 268 } |
| 265 #endif // defined(__arm__) | 269 #endif // defined(__arm__) |
| 266 | 270 |
| 271 intptr_t CountSyscalls(const struct arch_seccomp_data& args, void *aux) { |
| 272 // Count all invocations of our callback function. |
| 273 ++*reinterpret_cast<int *>(aux); |
| 274 |
| 275 // Verify that within the callback function all filtering is temporarily |
| 276 // disabled. |
| 277 BPF_ASSERT(syscall(__NR_getpid) > 1); |
| 278 |
| 279 // Verify that we can now call the underlying system call without causing |
| 280 // infinite recursion. |
| 281 return Sandbox::ForwardSyscall(args); |
| 282 } |
| 283 |
| 284 ErrorCode GreyListedPolicy(int sysno, void *aux) { |
| 285 // The use of UnsafeTrap() causes us to print a warning message. This is |
| 286 // generally desirable, but it results in the unittest failing, as it doesn't |
| 287 // expect any messages on "stderr". So, temporarily disable messages. The |
| 288 // BPF_TEST() is guaranteed to turn messages back on, after the policy |
| 289 // function has completed. |
| 290 Die::SuppressInfoMessages(true); |
| 291 |
| 292 // Some system calls must always be allowed, if our policy wants to make |
| 293 // use of UnsafeTrap() |
| 294 if (sysno == __NR_rt_sigprocmask || |
| 295 sysno == __NR_rt_sigreturn |
| 296 #if defined(__NR_sigprocmask) |
| 297 || sysno == __NR_sigprocmask |
| 298 #endif |
| 299 #if defined(__NR_sigreturn) |
| 300 || sysno == __NR_sigreturn |
| 301 #endif |
| 302 ) { |
| 303 return ErrorCode(ErrorCode::ERR_ALLOWED); |
| 304 } else if (sysno == __NR_getpid) { |
| 305 // Disallow getpid() |
| 306 return ErrorCode(EPERM); |
| 307 } else if (Sandbox::isValidSyscallNumber(sysno)) { |
| 308 // Allow (and count) all other system calls. |
| 309 return Sandbox::UnsafeTrap(CountSyscalls, aux); |
| 310 } else { |
| 311 return ErrorCode(ENOSYS); |
| 312 } |
| 313 } |
| 314 |
| 315 BPF_TEST(SandboxBpf, GreyListedPolicy, |
| 316 GreyListedPolicy, int /* BPF_AUX */) { |
| 317 BPF_ASSERT(syscall(__NR_getpid) == -1); |
| 318 BPF_ASSERT(errno == EPERM); |
| 319 BPF_ASSERT(BPF_AUX == 0); |
| 320 BPF_ASSERT(syscall(__NR_geteuid) == syscall(__NR_getuid)); |
| 321 BPF_ASSERT(BPF_AUX == 2); |
| 322 char name[17] = { }; |
| 323 BPF_ASSERT(!syscall(__NR_prctl, PR_GET_NAME, name, (void *)NULL, |
| 324 (void *)NULL, (void *)NULL)); |
| 325 BPF_ASSERT(BPF_AUX == 3); |
| 326 BPF_ASSERT(*name); |
| 327 } |
| 328 |
| 329 intptr_t PrctlHandler(const struct arch_seccomp_data& args, void *) { |
| 330 if (args.args[0] == PR_CAPBSET_DROP && |
| 331 static_cast<int>(args.args[1]) == -1) { |
| 332 // prctl(PR_CAPBSET_DROP, -1) is never valid. The kernel will always |
| 333 // return an error. But our handler allows this call. |
| 334 return 0; |
| 335 } else { |
| 336 return Sandbox::ForwardSyscall(args); |
| 337 } |
| 338 } |
| 339 |
| 340 ErrorCode PrctlPolicy(int sysno, void *aux) { |
| 341 Die::SuppressInfoMessages(true); |
| 342 |
| 343 if (sysno == __NR_prctl) { |
| 344 // Handle prctl() inside an UnsafeTrap() |
| 345 return Sandbox::UnsafeTrap(PrctlHandler, NULL); |
| 346 } else if (Sandbox::isValidSyscallNumber(sysno)) { |
| 347 // Allow all other system calls. |
| 348 return ErrorCode(ErrorCode::ERR_ALLOWED); |
| 349 } else { |
| 350 return ErrorCode(ENOSYS); |
| 351 } |
| 352 } |
| 353 |
| 354 BPF_TEST(SandboxBpf, ForwardSyscall, PrctlPolicy) { |
| 355 // This call should never be allowed. But our policy will intercept it and |
| 356 // let it pass successfully. |
| 357 BPF_ASSERT(!prctl(PR_CAPBSET_DROP, -1, (void *)NULL, (void *)NULL, |
| 358 (void *)NULL)); |
| 359 |
| 360 // Verify that the call will fail, if it makes it all the way to the kernel. |
| 361 BPF_ASSERT(prctl(PR_CAPBSET_DROP, -2, (void *)NULL, (void *)NULL, |
| 362 (void *)NULL) == -1); |
| 363 |
| 364 // And verify that other uses of prctl() work just fine. |
| 365 char name[17] = { }; |
| 366 BPF_ASSERT(!syscall(__NR_prctl, PR_GET_NAME, name, (void *)NULL, |
| 367 (void *)NULL, (void *)NULL)); |
| 368 BPF_ASSERT(*name); |
| 369 |
| 370 // Finally, verify that system calls other than prctl() are completely |
| 371 // unaffected by our policy. |
| 372 struct utsname uts = { }; |
| 373 BPF_ASSERT(!uname(&uts)); |
| 374 BPF_ASSERT(!strcmp(uts.sysname, "Linux")); |
| 375 } |
| 376 |
| 377 intptr_t AllowRedirectedSyscall(const struct arch_seccomp_data& args, void *) { |
| 378 return Sandbox::ForwardSyscall(args); |
| 379 } |
| 380 |
| 381 ErrorCode RedirectAllSyscallsPolicy(int sysno, void *aux) { |
| 382 Die::SuppressInfoMessages(true); |
| 383 |
| 384 // Some system calls must always be allowed, if our policy wants to make |
| 385 // use of UnsafeTrap() |
| 386 if (sysno == __NR_rt_sigprocmask || |
| 387 sysno == __NR_rt_sigreturn |
| 388 #if defined(__NR_sigprocmask) |
| 389 || sysno == __NR_sigprocmask |
| 390 #endif |
| 391 #if defined(__NR_sigreturn) |
| 392 || sysno == __NR_sigreturn |
| 393 #endif |
| 394 ) { |
| 395 return ErrorCode(ErrorCode::ERR_ALLOWED); |
| 396 } else if (Sandbox::isValidSyscallNumber(sysno)) { |
| 397 return Sandbox::UnsafeTrap(AllowRedirectedSyscall, aux); |
| 398 } else { |
| 399 return ErrorCode(ENOSYS); |
| 400 } |
| 401 } |
| 402 |
| 403 int bus_handler_fd_ = -1; |
| 404 |
| 405 void SigBusHandler(int, siginfo_t *info, void *void_context) { |
| 406 BPF_ASSERT(write(bus_handler_fd_, "\x55", 1) == 1); |
| 407 } |
| 408 |
| 409 BPF_TEST(SandboxBpf, SigBus, RedirectAllSyscallsPolicy) { |
| 410 // We use the SIGBUS bit in the signal mask as a thread-local boolean |
| 411 // value in the implementation of UnsafeTrap(). This is obviously a bit |
| 412 // of a hack that could conceivably interfere with code that uses SIGBUS |
| 413 // in more traditional ways. This test verifies that basic functionality |
| 414 // of SIGBUS is not impacted, but it is certainly possibly to construe |
| 415 // more complex uses of signals where our use of the SIGBUS mask is not |
| 416 // 100% transparent. This is expected behavior. |
| 417 int fds[2]; |
| 418 BPF_ASSERT(pipe(fds) == 0); |
| 419 bus_handler_fd_ = fds[1]; |
| 420 struct sigaction sa = { }; |
| 421 sa.sa_sigaction = SigBusHandler; |
| 422 sa.sa_flags = SA_SIGINFO; |
| 423 BPF_ASSERT(sigaction(SIGBUS, &sa, NULL) == 0); |
| 424 raise(SIGBUS); |
| 425 char c = '\000'; |
| 426 BPF_ASSERT(read(fds[0], &c, 1) == 1); |
| 427 BPF_ASSERT(close(fds[0]) == 0); |
| 428 BPF_ASSERT(close(fds[1]) == 0); |
| 429 BPF_ASSERT(c == 0x55); |
| 430 } |
| 431 |
| 432 BPF_TEST(SandboxBpf, SigMask, RedirectAllSyscallsPolicy) { |
| 433 // Signal masks are potentially tricky to handle. For instance, if we |
| 434 // ever tried to update them from inside a Trap() or UnsafeTrap() handler, |
| 435 // the call to sigreturn() at the end of the signal handler would undo |
| 436 // all of our efforts. So, it makes sense to test that sigprocmask() |
| 437 // works, even if we have a policy in place that makes use of UnsafeTrap(). |
| 438 // In practice, this works because we force sigprocmask() to be handled |
| 439 // entirely in the kernel. |
| 440 sigset_t mask0, mask1, mask2; |
| 441 |
| 442 // Call sigprocmask() to verify that SIGUSR1 wasn't blocked, if we didn't |
| 443 // change the mask (it shouldn't have been, as it isn't blocked by default |
| 444 // in POSIX). |
| 445 sigemptyset(&mask0); |
| 446 BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, &mask1)); |
| 447 BPF_ASSERT(!sigismember(&mask1, SIGUSR1)); |
| 448 |
| 449 // Try again, and this time we verify that we can block it. This |
| 450 // requires a second call to sigprocmask(). |
| 451 sigaddset(&mask0, SIGUSR1); |
| 452 BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, NULL)); |
| 453 BPF_ASSERT(!sigprocmask(SIG_BLOCK, NULL, &mask2)); |
| 454 BPF_ASSERT( sigismember(&mask2, SIGUSR1)); |
| 455 } |
| 456 |
| 457 BPF_TEST(SandboxBpf, UnsafeTrapWithErrno, RedirectAllSyscallsPolicy) { |
| 458 // An UnsafeTrap() (or for that matter, a Trap()) has to report error |
| 459 // conditions by returning an exit code in the range -1..-4096. This |
| 460 // should happen automatically if using ForwardSyscall(). If the TrapFnc() |
| 461 // uses some other method to make system calls, then it is responsible |
| 462 // for computing the correct return code. |
| 463 // This test verifies that ForwardSyscall() does the correct thing. |
| 464 |
| 465 // The glibc system wrapper will ultimately set errno for us. So, from normal |
| 466 // userspace, all of this should be completely transparent. |
| 467 errno = 0; |
| 468 BPF_ASSERT(close(-1) == -1); |
| 469 BPF_ASSERT(errno == EBADF); |
| 470 |
| 471 // Explicitly avoid the glibc wrapper. This is not normally the way anybody |
| 472 // would make system calls, but it allows us to verify that we don't |
| 473 // accidentally mess with errno, when we shouldn't. |
| 474 errno = 0; |
| 475 struct arch_seccomp_data args = { }; |
| 476 args.nr = __NR_close; |
| 477 args.args[0] = -1; |
| 478 BPF_ASSERT(Sandbox::ForwardSyscall(args) == -EBADF); |
| 479 BPF_ASSERT(errno == 0); |
| 480 } |
| 481 |
| 267 } // namespace | 482 } // namespace |
| OLD | NEW |