| 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 <memory> |
| 14 |
| 13 #include "base/logging.h" | 15 #include "base/logging.h" |
| 14 #include "base/macros.h" | 16 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 17 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" | 18 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" |
| 18 #include "sandbox/linux/bpf_dsl/policy.h" | 19 #include "sandbox/linux/bpf_dsl/policy.h" |
| 19 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 20 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 20 #include "sandbox/linux/services/syscall_wrappers.h" | 21 #include "sandbox/linux/services/syscall_wrappers.h" |
| 21 #include "sandbox/linux/system_headers/linux_syscalls.h" | 22 #include "sandbox/linux/system_headers/linux_syscalls.h" |
| 22 #include "sandbox/linux/tests/unit_tests.h" | 23 #include "sandbox/linux/tests/unit_tests.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 using sandbox::bpf_dsl::Allow; | 26 using sandbox::bpf_dsl::Allow; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 70 |
| 70 TEST(BPFTest, BPFTesterCompatibilityDelegateLeakTest) { | 71 TEST(BPFTest, BPFTesterCompatibilityDelegateLeakTest) { |
| 71 // Don't do anything, simply gives dynamic tools an opportunity to detect | 72 // Don't do anything, simply gives dynamic tools an opportunity to detect |
| 72 // leaks. | 73 // leaks. |
| 73 { | 74 { |
| 74 BPFTesterCompatibilityDelegate<EmptyClassTakingPolicy, FourtyTwo> | 75 BPFTesterCompatibilityDelegate<EmptyClassTakingPolicy, FourtyTwo> |
| 75 simple_delegate(DummyTestFunction); | 76 simple_delegate(DummyTestFunction); |
| 76 } | 77 } |
| 77 { | 78 { |
| 78 // Test polymorphism. | 79 // Test polymorphism. |
| 79 scoped_ptr<BPFTesterDelegate> simple_delegate( | 80 std::unique_ptr<BPFTesterDelegate> simple_delegate( |
| 80 new BPFTesterCompatibilityDelegate<EmptyClassTakingPolicy, FourtyTwo>( | 81 new BPFTesterCompatibilityDelegate<EmptyClassTakingPolicy, FourtyTwo>( |
| 81 DummyTestFunction)); | 82 DummyTestFunction)); |
| 82 } | 83 } |
| 83 } | 84 } |
| 84 | 85 |
| 85 class EnosysPtracePolicy : public bpf_dsl::Policy { | 86 class EnosysPtracePolicy : public bpf_dsl::Policy { |
| 86 public: | 87 public: |
| 87 EnosysPtracePolicy() { my_pid_ = sys_getpid(); } | 88 EnosysPtracePolicy() { my_pid_ = sys_getpid(); } |
| 88 ~EnosysPtracePolicy() override { | 89 ~EnosysPtracePolicy() override { |
| 89 // Policies should be able to bind with the process on which they are | 90 // Policies should be able to bind with the process on which they are |
| (...skipping 16 matching lines...) Expand all Loading... |
| 106 private: | 107 private: |
| 107 pid_t my_pid_; | 108 pid_t my_pid_; |
| 108 DISALLOW_COPY_AND_ASSIGN(EnosysPtracePolicy); | 109 DISALLOW_COPY_AND_ASSIGN(EnosysPtracePolicy); |
| 109 }; | 110 }; |
| 110 | 111 |
| 111 class BasicBPFTesterDelegate : public BPFTesterDelegate { | 112 class BasicBPFTesterDelegate : public BPFTesterDelegate { |
| 112 public: | 113 public: |
| 113 BasicBPFTesterDelegate() {} | 114 BasicBPFTesterDelegate() {} |
| 114 ~BasicBPFTesterDelegate() override {} | 115 ~BasicBPFTesterDelegate() override {} |
| 115 | 116 |
| 116 scoped_ptr<bpf_dsl::Policy> GetSandboxBPFPolicy() override { | 117 std::unique_ptr<bpf_dsl::Policy> GetSandboxBPFPolicy() override { |
| 117 return scoped_ptr<bpf_dsl::Policy>(new EnosysPtracePolicy()); | 118 return std::unique_ptr<bpf_dsl::Policy>(new EnosysPtracePolicy()); |
| 118 } | 119 } |
| 119 void RunTestFunction() override { | 120 void RunTestFunction() override { |
| 120 errno = 0; | 121 errno = 0; |
| 121 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL); | 122 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL); |
| 122 BPF_ASSERT(-1 == ret); | 123 BPF_ASSERT(-1 == ret); |
| 123 BPF_ASSERT(ENOSYS == errno); | 124 BPF_ASSERT(ENOSYS == errno); |
| 124 } | 125 } |
| 125 | 126 |
| 126 private: | 127 private: |
| 127 DISALLOW_COPY_AND_ASSIGN(BasicBPFTesterDelegate); | 128 DISALLOW_COPY_AND_ASSIGN(BasicBPFTesterDelegate); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 145 BPFDeathTestWithInlineTest, | 146 BPFDeathTestWithInlineTest, |
| 146 DEATH_MESSAGE(kHelloMessage), | 147 DEATH_MESSAGE(kHelloMessage), |
| 147 EnosysPtracePolicy) { | 148 EnosysPtracePolicy) { |
| 148 LOG(ERROR) << kHelloMessage; | 149 LOG(ERROR) << kHelloMessage; |
| 149 _exit(1); | 150 _exit(1); |
| 150 } | 151 } |
| 151 | 152 |
| 152 } // namespace | 153 } // namespace |
| 153 | 154 |
| 154 } // namespace sandbox | 155 } // namespace sandbox |
| OLD | NEW |