| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sandbox/linux/seccomp-bpf/bpf_tests.h" | 5 #include "sandbox/linux/seccomp-bpf/bpf_tests.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/ptrace.h> | 8 #include <sys/ptrace.h> |
| 9 #include <sys/syscall.h> | 9 #include <sys/syscall.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <unistd.h> | 11 #include <unistd.h> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" | 16 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" |
| 17 #include "sandbox/linux/bpf_dsl/policy.h" |
| 17 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 18 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 18 #include "sandbox/linux/services/linux_syscalls.h" | 19 #include "sandbox/linux/services/linux_syscalls.h" |
| 19 #include "sandbox/linux/tests/unit_tests.h" | 20 #include "sandbox/linux/tests/unit_tests.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 using sandbox::bpf_dsl::Allow; | 23 using sandbox::bpf_dsl::Allow; |
| 23 using sandbox::bpf_dsl::Error; | 24 using sandbox::bpf_dsl::Error; |
| 24 using sandbox::bpf_dsl::ResultExpr; | 25 using sandbox::bpf_dsl::ResultExpr; |
| 25 using sandbox::bpf_dsl::SandboxBPFDSLPolicy; | |
| 26 | 26 |
| 27 namespace sandbox { | 27 namespace sandbox { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 class FourtyTwo { | 31 class FourtyTwo { |
| 32 public: | 32 public: |
| 33 static const int kMagicValue = 42; | 33 static const int kMagicValue = 42; |
| 34 FourtyTwo() : value_(kMagicValue) {} | 34 FourtyTwo() : value_(kMagicValue) {} |
| 35 int value() { return value_; } | 35 int value() { return value_; } |
| 36 | 36 |
| 37 private: | 37 private: |
| 38 int value_; | 38 int value_; |
| 39 DISALLOW_COPY_AND_ASSIGN(FourtyTwo); | 39 DISALLOW_COPY_AND_ASSIGN(FourtyTwo); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 class EmptyClassTakingPolicy : public SandboxBPFDSLPolicy { | 42 class EmptyClassTakingPolicy : public bpf_dsl::Policy { |
| 43 public: | 43 public: |
| 44 explicit EmptyClassTakingPolicy(FourtyTwo* fourty_two) { | 44 explicit EmptyClassTakingPolicy(FourtyTwo* fourty_two) { |
| 45 BPF_ASSERT(fourty_two); | 45 BPF_ASSERT(fourty_two); |
| 46 BPF_ASSERT(FourtyTwo::kMagicValue == fourty_two->value()); | 46 BPF_ASSERT(FourtyTwo::kMagicValue == fourty_two->value()); |
| 47 } | 47 } |
| 48 virtual ~EmptyClassTakingPolicy() {} | 48 virtual ~EmptyClassTakingPolicy() {} |
| 49 | 49 |
| 50 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 50 virtual ResultExpr EvaluateSyscall(int sysno) const override { |
| 51 DCHECK(SandboxBPF::IsValidSyscallNumber(sysno)); | 51 DCHECK(SandboxBPF::IsValidSyscallNumber(sysno)); |
| 52 return Allow(); | 52 return Allow(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 73 simple_delegate(DummyTestFunction); | 73 simple_delegate(DummyTestFunction); |
| 74 } | 74 } |
| 75 { | 75 { |
| 76 // Test polymorphism. | 76 // Test polymorphism. |
| 77 scoped_ptr<BPFTesterDelegate> simple_delegate( | 77 scoped_ptr<BPFTesterDelegate> simple_delegate( |
| 78 new BPFTesterCompatibilityDelegate<EmptyClassTakingPolicy, FourtyTwo>( | 78 new BPFTesterCompatibilityDelegate<EmptyClassTakingPolicy, FourtyTwo>( |
| 79 DummyTestFunction)); | 79 DummyTestFunction)); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 class EnosysPtracePolicy : public SandboxBPFDSLPolicy { | 83 class EnosysPtracePolicy : public bpf_dsl::Policy { |
| 84 public: | 84 public: |
| 85 EnosysPtracePolicy() { | 85 EnosysPtracePolicy() { |
| 86 my_pid_ = syscall(__NR_getpid); | 86 my_pid_ = syscall(__NR_getpid); |
| 87 } | 87 } |
| 88 virtual ~EnosysPtracePolicy() { | 88 virtual ~EnosysPtracePolicy() { |
| 89 // Policies should be able to bind with the process on which they are | 89 // Policies should be able to bind with the process on which they are |
| 90 // created. They should never be created in a parent process. | 90 // created. They should never be created in a parent process. |
| 91 BPF_ASSERT_EQ(my_pid_, syscall(__NR_getpid)); | 91 BPF_ASSERT_EQ(my_pid_, syscall(__NR_getpid)); |
| 92 } | 92 } |
| 93 | 93 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 106 private: | 106 private: |
| 107 pid_t my_pid_; | 107 pid_t my_pid_; |
| 108 DISALLOW_COPY_AND_ASSIGN(EnosysPtracePolicy); | 108 DISALLOW_COPY_AND_ASSIGN(EnosysPtracePolicy); |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 class BasicBPFTesterDelegate : public BPFTesterDelegate { | 111 class BasicBPFTesterDelegate : public BPFTesterDelegate { |
| 112 public: | 112 public: |
| 113 BasicBPFTesterDelegate() {} | 113 BasicBPFTesterDelegate() {} |
| 114 virtual ~BasicBPFTesterDelegate() {} | 114 virtual ~BasicBPFTesterDelegate() {} |
| 115 | 115 |
| 116 virtual scoped_ptr<bpf_dsl::SandboxBPFDSLPolicy> GetSandboxBPFPolicy() | 116 virtual scoped_ptr<bpf_dsl::Policy> GetSandboxBPFPolicy() override { |
| 117 override { | 117 return scoped_ptr<bpf_dsl::Policy>(new EnosysPtracePolicy()); |
| 118 return scoped_ptr<bpf_dsl::SandboxBPFDSLPolicy>(new EnosysPtracePolicy()); | |
| 119 } | 118 } |
| 120 virtual void RunTestFunction() override { | 119 virtual void RunTestFunction() override { |
| 121 errno = 0; | 120 errno = 0; |
| 122 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL); | 121 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL); |
| 123 BPF_ASSERT(-1 == ret); | 122 BPF_ASSERT(-1 == ret); |
| 124 BPF_ASSERT(ENOSYS == errno); | 123 BPF_ASSERT(ENOSYS == errno); |
| 125 } | 124 } |
| 126 | 125 |
| 127 private: | 126 private: |
| 128 DISALLOW_COPY_AND_ASSIGN(BasicBPFTesterDelegate); | 127 DISALLOW_COPY_AND_ASSIGN(BasicBPFTesterDelegate); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 146 BPFDeathTestWithInlineTest, | 145 BPFDeathTestWithInlineTest, |
| 147 DEATH_MESSAGE(kHelloMessage), | 146 DEATH_MESSAGE(kHelloMessage), |
| 148 EnosysPtracePolicy) { | 147 EnosysPtracePolicy) { |
| 149 LOG(ERROR) << kHelloMessage; | 148 LOG(ERROR) << kHelloMessage; |
| 150 _exit(1); | 149 _exit(1); |
| 151 } | 150 } |
| 152 | 151 |
| 153 } // namespace | 152 } // namespace |
| 154 | 153 |
| 155 } // namespace sandbox | 154 } // namespace sandbox |
| OLD | NEW |