| 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 | 9 |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 12 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 13 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 13 #include "sandbox/linux/services/linux_syscalls.h" | 14 #include "sandbox/linux/services/linux_syscalls.h" |
| 14 #include "sandbox/linux/tests/unit_tests.h" | 15 #include "sandbox/linux/tests/unit_tests.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace sandbox { | 18 namespace sandbox { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 EmptyPolicyTakesClass, | 65 EmptyPolicyTakesClass, |
| 65 FourtyTwo /* *BPF_AUX */) { | 66 FourtyTwo /* *BPF_AUX */) { |
| 66 // BPF_AUX should point to an instance of FourtyTwo. | 67 // BPF_AUX should point to an instance of FourtyTwo. |
| 67 BPF_ASSERT(BPF_AUX); | 68 BPF_ASSERT(BPF_AUX); |
| 68 BPF_ASSERT(FourtyTwo::kMagicValue == BPF_AUX->value()); | 69 BPF_ASSERT(FourtyTwo::kMagicValue == BPF_AUX->value()); |
| 69 } | 70 } |
| 70 | 71 |
| 71 void DummyTestFunction(FourtyTwo *fourty_two) { | 72 void DummyTestFunction(FourtyTwo *fourty_two) { |
| 72 } | 73 } |
| 73 | 74 |
| 74 TEST(BPFTest, BPFTesterSimpleDelegateLeakTest) { | 75 TEST(BPFTest, BPFTesterCompatibilityDelegateLeakTest) { |
| 75 // Don't do anything, simply gives dynamic tools an opportunity to detect | 76 // Don't do anything, simply gives dynamic tools an opportunity to detect |
| 76 // leaks. | 77 // leaks. |
| 77 { | 78 { |
| 78 BPFTesterSimpleDelegate<FourtyTwo> simple_delegate(DummyTestFunction, | 79 BPFTesterCompatibilityDelegate<FourtyTwo> simple_delegate( |
| 79 EmptyPolicyTakesClass); | 80 DummyTestFunction, EmptyPolicyTakesClass); |
| 80 } | 81 } |
| 81 { | 82 { |
| 82 // Test polymorphism. | 83 // Test polymorphism. |
| 83 scoped_ptr<BPFTesterDelegate> simple_delegate( | 84 scoped_ptr<BPFTesterDelegate> simple_delegate( |
| 84 new BPFTesterSimpleDelegate<FourtyTwo>(DummyTestFunction, | 85 new BPFTesterCompatibilityDelegate<FourtyTwo>(DummyTestFunction, |
| 85 EmptyPolicyTakesClass)); | 86 EmptyPolicyTakesClass)); |
| 86 } | 87 } |
| 87 } | 88 } |
| 88 | 89 |
| 90 class EnosysPtracePolicy : public SandboxBPFPolicy { |
| 91 public: |
| 92 EnosysPtracePolicy() {} |
| 93 virtual ~EnosysPtracePolicy() {} |
| 94 |
| 95 virtual ErrorCode EvaluateSyscall(SandboxBPF* sandbox_compiler, |
| 96 int system_call_number) const OVERRIDE { |
| 97 if (!SandboxBPF::IsValidSyscallNumber(system_call_number) || |
| 98 system_call_number == __NR_ptrace) { |
| 99 return ErrorCode(ENOSYS); |
| 100 } else { |
| 101 return ErrorCode(ErrorCode::ERR_ALLOWED); |
| 102 } |
| 103 } |
| 104 |
| 105 private: |
| 106 DISALLOW_COPY_AND_ASSIGN(EnosysPtracePolicy); |
| 107 }; |
| 108 |
| 109 class BasicBPFTesterDelegate : public BPFTesterDelegate { |
| 110 public: |
| 111 BasicBPFTesterDelegate() {} |
| 112 virtual ~BasicBPFTesterDelegate() {} |
| 113 |
| 114 virtual scoped_ptr<SandboxBPFPolicy> GetSandboxBPFPolicy() OVERRIDE { |
| 115 return scoped_ptr<SandboxBPFPolicy>(new EnosysPtracePolicy()); |
| 116 } |
| 117 virtual void RunTestFunction() OVERRIDE { |
| 118 errno = 0; |
| 119 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL); |
| 120 BPF_ASSERT(-1 == ret); |
| 121 BPF_ASSERT(ENOSYS == errno); |
| 122 } |
| 123 |
| 124 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(BasicBPFTesterDelegate); |
| 126 }; |
| 127 |
| 128 // This is the most powerful and complex way to create a BPF test, but it |
| 129 // requires a full class definition (BasicBPFTesterDelegate). |
| 130 BPF_TEST_D(BPFTest, BPFTestWithDelegateClass, BasicBPFTesterDelegate); |
| 131 |
| 132 // This is the simplest form of BPF tests. |
| 133 BPF_TEST_C(BPFTest, BPFTestWithInlineTest, EnosysPtracePolicy) { |
| 134 errno = 0; |
| 135 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL); |
| 136 BPF_ASSERT(-1 == ret); |
| 137 BPF_ASSERT(ENOSYS == errno); |
| 138 } |
| 139 |
| 89 } // namespace | 140 } // namespace |
| 90 | 141 |
| 91 } // namespace sandbox | 142 } // namespace sandbox |
| OLD | NEW |