| Index: sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc
|
| diff --git a/sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc b/sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc
|
| index 8ea23d9e188811707396c686a9867317ab54c9c0..037ca07ca3a739aa67d373ce750cad975a1ac2d9 100644
|
| --- a/sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc
|
| +++ b/sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc
|
| @@ -5,6 +5,7 @@
|
| #include <ostream>
|
|
|
| #include "sandbox/linux/seccomp-bpf/bpf_tests.h"
|
| +#include "sandbox/linux/seccomp-bpf/syscall.h"
|
| #include "sandbox/linux/seccomp-bpf/verifier.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| @@ -264,4 +265,145 @@ BPF_TEST(SandboxBpf, ArmPrivatePolicy, ArmPrivatePolicy) {
|
| }
|
| #endif // defined(__arm__)
|
|
|
| +TEST(SandboxBpf, Syscall) {
|
| + // Test that we can do basic system calls
|
| + EXPECT_EQ(Syscall(__NR_getpid), syscall(__NR_getpid));
|
| +
|
| + // Test that Syscall(-1) is handled specially
|
| + EXPECT_NE(Syscall(-1), syscall(-1));
|
| +
|
| + // If possible, test that Syscall(-1) returns the address right after a
|
| + // kernel entry point.
|
| +#if defined(__i386__)
|
| + EXPECT_EQ(((uint16_t *)Syscall(-1))[-1], 0x80CDu); // INT 0x80
|
| +#elif defined(__x86_64__)
|
| + EXPECT_EQ(((uint16_t *)Syscall(-1))[-1], 0x050Fu); // SYSCALL
|
| +#elif defined(__arm__)
|
| +#if defined(__thumb__)
|
| + EXPECT_EQ(((uint16_t *)Syscall(-1))[-1], 0xDF00u); // SWI 0
|
| +#else
|
| + EXPECT_EQ(((uint32_t *)Syscall(-1))[-1], 0xEF000000u); // SVC 0
|
| +#endif
|
| +#else
|
| + #warning Incomplete test case; need port for target platform
|
| +#endif
|
| +}
|
| +
|
| +intptr_t CountSyscalls(const struct arch_seccomp_data& args, void *aux) {
|
| + // Count all invocations of our callback function.
|
| + ++*reinterpret_cast<int *>(aux);
|
| +
|
| + // Verify that within the callback function all filtering is temporarily
|
| + // disabled.
|
| + BPF_ASSERT(syscall(__NR_getpid) > 1);
|
| +
|
| + // Verify that we can now call the underlying system call without causing
|
| + // infinite recursion.
|
| + return (intptr_t)(syscall(args.nr, args.args[0], args.args[1],
|
| + args.args[2], args.args[3],
|
| + args.args[4], args.args[5]));
|
| +}
|
| +
|
| +ErrorCode GreyListedPolicy(int sysno, void *aux) {
|
| + // The use of UnsafeTrap() causes us to print a warning message. This is
|
| + // generally desirable, but it results in the unittest failing, as it doesn't
|
| + // expect any messages on "stderr". So, temporarily disable messages. The
|
| + // BPF_TEST() is guaranteed to turn messages back on, after the policy
|
| + // function has completed.
|
| + Die::SuppressInfoMessages(true);
|
| +
|
| + // Some system calls must always be allowed, if our policy wants to make
|
| + // use of UnsafeTrap()
|
| + if (sysno == __NR_rt_sigprocmask ||
|
| + sysno == __NR_rt_sigreturn
|
| +#if defined(__NR_sigprocmask)
|
| + || sysno == __NR_sigprocmask
|
| +#endif
|
| +#if defined(__NR_sigreturn)
|
| + || sysno == __NR_sigreturn
|
| +#endif
|
| + ) {
|
| + return ErrorCode(ErrorCode::ERR_ALLOWED);
|
| + } else if (sysno == __NR_getpid) {
|
| + // Disallow getpid()
|
| + return ErrorCode(EPERM);
|
| + } else if (Sandbox::isValidSyscallNumber(sysno)) {
|
| + // Allow (and count) all other system calls.
|
| + return Sandbox::UnsafeTrap(CountSyscalls, aux);
|
| + } else {
|
| + return ErrorCode(ENOSYS);
|
| + }
|
| +}
|
| +
|
| +BPF_TEST(SandboxBpf, GreyListedPolicy,
|
| + GreyListedPolicy, int /* BPF_AUX */) {
|
| + BPF_ASSERT(syscall(__NR_getpid) == -1);
|
| + BPF_ASSERT(errno == EPERM);
|
| + BPF_ASSERT(BPF_AUX == 0);
|
| + BPF_ASSERT(syscall(__NR_geteuid) == syscall(__NR_getuid));
|
| + BPF_ASSERT(BPF_AUX == 2);
|
| +}
|
| +
|
| +intptr_t AllowRedirectedSyscall(const struct arch_seccomp_data& args, void *) {
|
| + return (intptr_t)(syscall(args.nr,
|
| + (void *)args.args[0], (void *)args.args[1],
|
| + (void *)args.args[2], (void *)args.args[3],
|
| + (void *)args.args[4], (void *)args.args[5]));
|
| +}
|
| +
|
| +ErrorCode RedirectAllSyscallsPolicy(int sysno, void *aux) {
|
| + Die::SuppressInfoMessages(true);
|
| +
|
| + // Some system calls must always be allowed, if our policy wants to make
|
| + // use of UnsafeTrap()
|
| + if (sysno == __NR_rt_sigprocmask ||
|
| + sysno == __NR_rt_sigreturn
|
| +#if defined(__NR_sigprocmask)
|
| + || sysno == __NR_sigprocmask
|
| +#endif
|
| +#if defined(__NR_sigreturn)
|
| + || sysno == __NR_sigreturn
|
| +#endif
|
| + ) {
|
| + return ErrorCode(ErrorCode::ERR_ALLOWED);
|
| + } else if (Sandbox::isValidSyscallNumber(sysno)) {
|
| + return Sandbox::UnsafeTrap(AllowRedirectedSyscall, aux);
|
| + } else {
|
| + return ErrorCode(ENOSYS);
|
| + }
|
| +}
|
| +
|
| +int bus_handler_fd_ = -1;
|
| +
|
| +void SigBusHandler(int, siginfo_t *info, void *void_context) {
|
| + BPF_ASSERT(write(bus_handler_fd_, "\x55", 1) == 1);
|
| +}
|
| +
|
| +BPF_TEST(SandboxBpf, SigBus, RedirectAllSyscallsPolicy) {
|
| + int fds[2];
|
| + BPF_ASSERT(pipe(fds) == 0);
|
| + bus_handler_fd_ = fds[1];
|
| + struct sigaction sa = { };
|
| + sa.sa_sigaction = SigBusHandler;
|
| + sa.sa_flags = SA_SIGINFO;
|
| + BPF_ASSERT(sigaction(SIGBUS, &sa, NULL) == 0);
|
| + raise(SIGBUS);
|
| + char c = '\000';
|
| + BPF_ASSERT(read(fds[0], &c, 1) == 1);
|
| + BPF_ASSERT(close(fds[0]) == 0);
|
| + BPF_ASSERT(close(fds[1]) == 0);
|
| + BPF_ASSERT(c == 0x55);
|
| +}
|
| +
|
| +BPF_TEST(SandboxBpf, SigMask, RedirectAllSyscallsPolicy) {
|
| + sigset_t mask0, mask1, mask2;
|
| + sigemptyset(&mask0);
|
| + BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, &mask1));
|
| + BPF_ASSERT(!sigismember(&mask1, SIGUSR1));
|
| + sigaddset(&mask0, SIGUSR1);
|
| + BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, NULL));
|
| + BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, &mask2));
|
| + BPF_ASSERT( sigismember(&mask2, SIGUSR1));
|
| +}
|
| +
|
| } // namespace
|
|
|