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> |
(...skipping 28 matching lines...) Expand all Loading... |
39 int value_; | 39 int value_; |
40 DISALLOW_COPY_AND_ASSIGN(FourtyTwo); | 40 DISALLOW_COPY_AND_ASSIGN(FourtyTwo); |
41 }; | 41 }; |
42 | 42 |
43 class EmptyClassTakingPolicy : public bpf_dsl::Policy { | 43 class EmptyClassTakingPolicy : public bpf_dsl::Policy { |
44 public: | 44 public: |
45 explicit EmptyClassTakingPolicy(FourtyTwo* fourty_two) { | 45 explicit EmptyClassTakingPolicy(FourtyTwo* fourty_two) { |
46 BPF_ASSERT(fourty_two); | 46 BPF_ASSERT(fourty_two); |
47 BPF_ASSERT(FourtyTwo::kMagicValue == fourty_two->value()); | 47 BPF_ASSERT(FourtyTwo::kMagicValue == fourty_two->value()); |
48 } | 48 } |
49 virtual ~EmptyClassTakingPolicy() {} | 49 ~EmptyClassTakingPolicy() override {} |
50 | 50 |
51 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 51 ResultExpr EvaluateSyscall(int sysno) const override { |
52 DCHECK(SandboxBPF::IsValidSyscallNumber(sysno)); | 52 DCHECK(SandboxBPF::IsValidSyscallNumber(sysno)); |
53 return Allow(); | 53 return Allow(); |
54 } | 54 } |
55 }; | 55 }; |
56 | 56 |
57 BPF_TEST(BPFTest, | 57 BPF_TEST(BPFTest, |
58 BPFAUXPointsToClass, | 58 BPFAUXPointsToClass, |
59 EmptyClassTakingPolicy, | 59 EmptyClassTakingPolicy, |
60 FourtyTwo /* *BPF_AUX */) { | 60 FourtyTwo /* *BPF_AUX */) { |
61 // BPF_AUX should point to an instance of FourtyTwo. | 61 // BPF_AUX should point to an instance of FourtyTwo. |
(...skipping 15 matching lines...) Expand all Loading... |
77 // Test polymorphism. | 77 // Test polymorphism. |
78 scoped_ptr<BPFTesterDelegate> simple_delegate( | 78 scoped_ptr<BPFTesterDelegate> simple_delegate( |
79 new BPFTesterCompatibilityDelegate<EmptyClassTakingPolicy, FourtyTwo>( | 79 new BPFTesterCompatibilityDelegate<EmptyClassTakingPolicy, FourtyTwo>( |
80 DummyTestFunction)); | 80 DummyTestFunction)); |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 class EnosysPtracePolicy : public bpf_dsl::Policy { | 84 class EnosysPtracePolicy : public bpf_dsl::Policy { |
85 public: | 85 public: |
86 EnosysPtracePolicy() { my_pid_ = sys_getpid(); } | 86 EnosysPtracePolicy() { my_pid_ = sys_getpid(); } |
87 virtual ~EnosysPtracePolicy() { | 87 ~EnosysPtracePolicy() override { |
88 // Policies should be able to bind with the process on which they are | 88 // Policies should be able to bind with the process on which they are |
89 // created. They should never be created in a parent process. | 89 // created. They should never be created in a parent process. |
90 BPF_ASSERT_EQ(my_pid_, sys_getpid()); | 90 BPF_ASSERT_EQ(my_pid_, sys_getpid()); |
91 } | 91 } |
92 | 92 |
93 virtual ResultExpr EvaluateSyscall(int system_call_number) const override { | 93 ResultExpr EvaluateSyscall(int system_call_number) const override { |
94 CHECK(SandboxBPF::IsValidSyscallNumber(system_call_number)); | 94 CHECK(SandboxBPF::IsValidSyscallNumber(system_call_number)); |
95 if (system_call_number == __NR_ptrace) { | 95 if (system_call_number == __NR_ptrace) { |
96 // The EvaluateSyscall function should run in the process that created | 96 // The EvaluateSyscall function should run in the process that created |
97 // the current object. | 97 // the current object. |
98 BPF_ASSERT_EQ(my_pid_, sys_getpid()); | 98 BPF_ASSERT_EQ(my_pid_, sys_getpid()); |
99 return Error(ENOSYS); | 99 return Error(ENOSYS); |
100 } else { | 100 } else { |
101 return Allow(); | 101 return Allow(); |
102 } | 102 } |
103 } | 103 } |
104 | 104 |
105 private: | 105 private: |
106 pid_t my_pid_; | 106 pid_t my_pid_; |
107 DISALLOW_COPY_AND_ASSIGN(EnosysPtracePolicy); | 107 DISALLOW_COPY_AND_ASSIGN(EnosysPtracePolicy); |
108 }; | 108 }; |
109 | 109 |
110 class BasicBPFTesterDelegate : public BPFTesterDelegate { | 110 class BasicBPFTesterDelegate : public BPFTesterDelegate { |
111 public: | 111 public: |
112 BasicBPFTesterDelegate() {} | 112 BasicBPFTesterDelegate() {} |
113 virtual ~BasicBPFTesterDelegate() {} | 113 ~BasicBPFTesterDelegate() override {} |
114 | 114 |
115 virtual scoped_ptr<bpf_dsl::Policy> GetSandboxBPFPolicy() override { | 115 scoped_ptr<bpf_dsl::Policy> GetSandboxBPFPolicy() override { |
116 return scoped_ptr<bpf_dsl::Policy>(new EnosysPtracePolicy()); | 116 return scoped_ptr<bpf_dsl::Policy>(new EnosysPtracePolicy()); |
117 } | 117 } |
118 virtual void RunTestFunction() override { | 118 void RunTestFunction() override { |
119 errno = 0; | 119 errno = 0; |
120 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL); | 120 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL); |
121 BPF_ASSERT(-1 == ret); | 121 BPF_ASSERT(-1 == ret); |
122 BPF_ASSERT(ENOSYS == errno); | 122 BPF_ASSERT(ENOSYS == errno); |
123 } | 123 } |
124 | 124 |
125 private: | 125 private: |
126 DISALLOW_COPY_AND_ASSIGN(BasicBPFTesterDelegate); | 126 DISALLOW_COPY_AND_ASSIGN(BasicBPFTesterDelegate); |
127 }; | 127 }; |
128 | 128 |
(...skipping 15 matching lines...) Expand all Loading... |
144 BPFDeathTestWithInlineTest, | 144 BPFDeathTestWithInlineTest, |
145 DEATH_MESSAGE(kHelloMessage), | 145 DEATH_MESSAGE(kHelloMessage), |
146 EnosysPtracePolicy) { | 146 EnosysPtracePolicy) { |
147 LOG(ERROR) << kHelloMessage; | 147 LOG(ERROR) << kHelloMessage; |
148 _exit(1); | 148 _exit(1); |
149 } | 149 } |
150 | 150 |
151 } // namespace | 151 } // namespace |
152 | 152 |
153 } // namespace sandbox | 153 } // namespace sandbox |
OLD | NEW |