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