| 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 #ifndef SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ | 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ |
| 6 #define SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ | 6 #define SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ |
| 7 | 7 |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 | 11 |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "sandbox/linux/tests/unit_tests.h" | 13 #include "sandbox/linux/tests/unit_tests.h" |
| 14 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 14 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 15 | 15 |
| 16 namespace sandbox { | 16 namespace sandbox { |
| 17 | 17 |
| 18 // A BPF_DEATH_TEST is just the same as a BPF_TEST, but it assumes that the | 18 // A BPF_DEATH_TEST is just the same as a BPF_TEST, but it assumes that the |
| 19 // test will fail with a particular known error condition. Use the DEATH_XXX() | 19 // test will fail with a particular known error condition. Use the DEATH_XXX() |
| 20 // macros from unit_tests.h to specify the expected error condition. | 20 // macros from unit_tests.h to specify the expected error condition. |
| 21 // A BPF_DEATH_TEST is always disabled under ThreadSanitizer, see | 21 // A BPF_DEATH_TEST is always disabled under ThreadSanitizer, see |
| 22 // crbug.com/243968. | 22 // crbug.com/243968. |
| 23 #define BPF_DEATH_TEST(test_case_name, test_name, death, policy, aux...) \ | 23 #define BPF_DEATH_TEST(test_case_name, test_name, death, policy, aux...) \ |
| 24 void BPF_TEST_##test_name(sandbox::BpfTests<aux>::AuxType& BPF_AUX); \ | 24 void BPF_TEST_##test_name(sandbox::BpfTests<aux>::AuxType& BPF_AUX); \ |
| 25 TEST(test_case_name, DISABLE_ON_TSAN(test_name)) { \ | 25 TEST(test_case_name, DISABLE_ON_TSAN(test_name)) { \ |
| 26 sandbox::BpfTests<aux>::TestArgs arg(BPF_TEST_##test_name, policy); \ | 26 sandbox::BpfTests<aux>::TestArgs arg(BPF_TEST_##test_name, policy); \ |
| 27 sandbox::BpfTests<aux>::RunTestInProcess( \ | 27 sandbox::BpfTests<aux>::RunTestInProcess( \ |
| 28 sandbox::BpfTests<aux>::TestWrapper, &arg, \ | 28 sandbox::BpfTests<aux>::TestWrapper, &arg, death); \ |
| 29 death); \ | 29 } \ |
| 30 } \ | |
| 31 void BPF_TEST_##test_name(sandbox::BpfTests<aux>::AuxType& BPF_AUX) | 30 void BPF_TEST_##test_name(sandbox::BpfTests<aux>::AuxType& BPF_AUX) |
| 32 | 31 |
| 33 // BPF_TEST() is a special version of SANDBOX_TEST(). It turns into a no-op, | 32 // BPF_TEST() is a special version of SANDBOX_TEST(). It turns into a no-op, |
| 34 // if the host does not have kernel support for running BPF filters. | 33 // if the host does not have kernel support for running BPF filters. |
| 35 // Also, it takes advantage of the Die class to avoid calling LOG(FATAL), from | 34 // Also, it takes advantage of the Die class to avoid calling LOG(FATAL), from |
| 36 // inside our tests, as we don't need or even want all the error handling that | 35 // inside our tests, as we don't need or even want all the error handling that |
| 37 // LOG(FATAL) would do. | 36 // LOG(FATAL) would do. |
| 38 // BPF_TEST() takes a C++ data type as an optional fourth parameter. If | 37 // BPF_TEST() takes a C++ data type as an optional fourth parameter. If |
| 39 // present, this sets up a variable that can be accessed as "BPF_AUX". This | 38 // present, this sets up a variable that can be accessed as "BPF_AUX". This |
| 40 // variable will be passed as an argument to the "policy" function. Policies | 39 // variable will be passed as an argument to the "policy" function. Policies |
| 41 // would typically use it as an argument to Sandbox::Trap(), if they want to | 40 // would typically use it as an argument to Sandbox::Trap(), if they want to |
| 42 // communicate data between the BPF_TEST() and a Trap() function. | 41 // communicate data between the BPF_TEST() and a Trap() function. |
| 43 #define BPF_TEST(test_case_name, test_name, policy, aux...) \ | 42 #define BPF_TEST(test_case_name, test_name, policy, aux...) \ |
| 44 BPF_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS(), policy, aux) | 43 BPF_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS(), policy, aux) |
| 45 | 44 |
| 46 | |
| 47 // Assertions are handled exactly the same as with a normal SANDBOX_TEST() | 45 // Assertions are handled exactly the same as with a normal SANDBOX_TEST() |
| 48 #define BPF_ASSERT SANDBOX_ASSERT | 46 #define BPF_ASSERT SANDBOX_ASSERT |
| 49 | 47 |
| 50 | |
| 51 // The "Aux" type is optional. We use an "empty" type by default, so that if | 48 // The "Aux" type is optional. We use an "empty" type by default, so that if |
| 52 // the caller doesn't provide any type, all the BPF_AUX related data compiles | 49 // the caller doesn't provide any type, all the BPF_AUX related data compiles |
| 53 // to nothing. | 50 // to nothing. |
| 54 template<class Aux = int[0]> | 51 template <class Aux = int[0]> |
| 55 class BpfTests : public UnitTests { | 52 class BpfTests : public UnitTests { |
| 56 public: | 53 public: |
| 57 typedef Aux AuxType; | 54 typedef Aux AuxType; |
| 58 | 55 |
| 59 class TestArgs { | 56 class TestArgs { |
| 60 public: | 57 public: |
| 61 TestArgs(void (*t)(AuxType&), playground2::Sandbox::EvaluateSyscall p) | 58 TestArgs(void (*t)(AuxType&), playground2::Sandbox::EvaluateSyscall p) |
| 62 : test_(t), | 59 : test_(t), policy_(p), aux_() {} |
| 63 policy_(p), | |
| 64 aux_() { | |
| 65 } | |
| 66 | 60 |
| 67 void (*test() const)(AuxType&) { return test_; } | 61 void (*test() const)(AuxType&) { return test_; } |
| 68 playground2::Sandbox::EvaluateSyscall policy() const { return policy_; } | 62 playground2::Sandbox::EvaluateSyscall policy() const { return policy_; } |
| 69 | 63 |
| 70 private: | 64 private: |
| 71 friend class BpfTests; | 65 friend class BpfTests; |
| 72 | 66 |
| 73 void (*test_)(AuxType&); | 67 void (*test_)(AuxType&); |
| 74 playground2::Sandbox::EvaluateSyscall policy_; | 68 playground2::Sandbox::EvaluateSyscall policy_; |
| 75 AuxType aux_; | 69 AuxType aux_; |
| 76 }; | 70 }; |
| 77 | 71 |
| 78 static void TestWrapper(void *void_arg) { | 72 static void TestWrapper(void* void_arg) { |
| 79 TestArgs *arg = reinterpret_cast<TestArgs *>(void_arg); | 73 TestArgs* arg = reinterpret_cast<TestArgs*>(void_arg); |
| 80 playground2::Die::EnableSimpleExit(); | 74 playground2::Die::EnableSimpleExit(); |
| 81 if (playground2::Sandbox::SupportsSeccompSandbox(-1) == | 75 if (playground2::Sandbox::SupportsSeccompSandbox(-1) == |
| 82 playground2::Sandbox::STATUS_AVAILABLE) { | 76 playground2::Sandbox::STATUS_AVAILABLE) { |
| 83 // Ensure the the sandbox is actually available at this time | 77 // Ensure the the sandbox is actually available at this time |
| 84 int proc_fd; | 78 int proc_fd; |
| 85 BPF_ASSERT((proc_fd = open("/proc", O_RDONLY|O_DIRECTORY)) >= 0); | 79 BPF_ASSERT((proc_fd = open("/proc", O_RDONLY | O_DIRECTORY)) >= 0); |
| 86 BPF_ASSERT(playground2::Sandbox::SupportsSeccompSandbox(proc_fd) == | 80 BPF_ASSERT(playground2::Sandbox::SupportsSeccompSandbox(proc_fd) == |
| 87 playground2::Sandbox::STATUS_AVAILABLE); | 81 playground2::Sandbox::STATUS_AVAILABLE); |
| 88 | 82 |
| 89 // Initialize and then start the sandbox with our custom policy | 83 // Initialize and then start the sandbox with our custom policy |
| 90 playground2::Sandbox sandbox; | 84 playground2::Sandbox sandbox; |
| 91 sandbox.set_proc_fd(proc_fd); | 85 sandbox.set_proc_fd(proc_fd); |
| 92 sandbox.SetSandboxPolicyDeprecated(arg->policy(), &arg->aux_); | 86 sandbox.SetSandboxPolicyDeprecated(arg->policy(), &arg->aux_); |
| 93 sandbox.Sandbox::StartSandbox(); | 87 sandbox.Sandbox::StartSandbox(); |
| 94 | 88 |
| 95 arg->test()(arg->aux_); | 89 arg->test()(arg->aux_); |
| 96 } else { | 90 } else { |
| 97 printf("This BPF test is not fully running in this configuration!\n"); | 91 printf("This BPF test is not fully running in this configuration!\n"); |
| 98 // Android, ARM and Valgrind are the three only configurations where we | 92 // Android, ARM and Valgrind are the three only configurations where we |
| 99 // accept not having kernel BPF support. | 93 // accept not having kernel BPF support. |
| 100 // TODO(jln): remote ARM from this list when possible (crbug.com/243478). | 94 // TODO(jln): remote ARM from this list when possible (crbug.com/243478). |
| 101 if (!IsAndroid() && !IsRunningOnValgrind() && !IsArchitectureArm()) { | 95 if (!IsAndroid() && !IsRunningOnValgrind() && !IsArchitectureArm()) { |
| 102 const bool seccomp_bpf_is_supported = false; | 96 const bool seccomp_bpf_is_supported = false; |
| 103 BPF_ASSERT(seccomp_bpf_is_supported); | 97 BPF_ASSERT(seccomp_bpf_is_supported); |
| 104 } | 98 } |
| 105 // Call the compiler and verify the policy. That's the least we can do, | 99 // Call the compiler and verify the policy. That's the least we can do, |
| 106 // if we don't have kernel support. | 100 // if we don't have kernel support. |
| 107 playground2::Sandbox sandbox; | 101 playground2::Sandbox sandbox; |
| 108 sandbox.SetSandboxPolicyDeprecated(arg->policy(), &arg->aux_); | 102 sandbox.SetSandboxPolicyDeprecated(arg->policy(), &arg->aux_); |
| 109 playground2::Sandbox::Program *program = | 103 playground2::Sandbox::Program* program = |
| 110 sandbox.AssembleFilter(true /* force_verification */); | 104 sandbox.AssembleFilter(true /* force_verification */); |
| 111 delete program; | 105 delete program; |
| 112 sandbox::UnitTests::IgnoreThisTest(); | 106 sandbox::UnitTests::IgnoreThisTest(); |
| 113 } | 107 } |
| 114 } | 108 } |
| 115 | 109 |
| 116 private: | 110 private: |
| 117 DISALLOW_IMPLICIT_CONSTRUCTORS(BpfTests); | 111 DISALLOW_IMPLICIT_CONSTRUCTORS(BpfTests); |
| 118 }; | 112 }; |
| 119 | 113 |
| 120 } // namespace | 114 } // namespace |
| 121 | 115 |
| 122 #endif // SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ | 116 #endif // SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ |
| OLD | NEW |