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
3 // found in the LICENSE file.
5 #include "sandbox/linux/seccomp-bpf/bpf_tests.h"
8 #include <sys/ptrace.h>
9 #include <sys/syscall.h>
10 #include <sys/types.h>
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "build/build_config.h"
16 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
17 #include "sandbox/linux/services/linux_syscalls.h"
18 #include "sandbox/linux/tests/unit_tests.h"
19 #include "testing/gtest/include/gtest/gtest.h"
27 static const int kMagicValue = 42;
28 FourtyTwo() : value_(kMagicValue) {}
29 int value() { return value_; }
33 DISALLOW_COPY_AND_ASSIGN(FourtyTwo);
36 ErrorCode EmptyPolicyTakesClass(SandboxBPF* sandbox,
38 FourtyTwo* fourty_two) {
39 // |aux| should point to an instance of FourtyTwo.
40 BPF_ASSERT(fourty_two);
41 BPF_ASSERT(FourtyTwo::kMagicValue == fourty_two->value());
42 if (!SandboxBPF::IsValidSyscallNumber(sysno)) {
43 return ErrorCode(ENOSYS);
45 return ErrorCode(ErrorCode::ERR_ALLOWED);
51 EmptyPolicyTakesClass,
52 FourtyTwo /* *BPF_AUX */) {
53 // BPF_AUX should point to an instance of FourtyTwo.
55 BPF_ASSERT(FourtyTwo::kMagicValue == BPF_AUX->value());
58 void DummyTestFunction(FourtyTwo *fourty_two) {
61 TEST(BPFTest, BPFTesterCompatibilityDelegateLeakTest) {
62 // Don't do anything, simply gives dynamic tools an opportunity to detect
65 BPFTesterCompatibilityDelegate<FourtyTwo> simple_delegate(
66 DummyTestFunction, EmptyPolicyTakesClass);
70 scoped_ptr<BPFTesterDelegate> simple_delegate(
71 new BPFTesterCompatibilityDelegate<FourtyTwo>(DummyTestFunction,
72 EmptyPolicyTakesClass));
76 class EnosysPtracePolicy : public SandboxBPFPolicy {
78 EnosysPtracePolicy() {
79 my_pid_ = syscall(__NR_getpid);
81 virtual ~EnosysPtracePolicy() {
82 // Policies should be able to bind with the process on which they are
83 // created. They should never be created in a parent process.
84 BPF_ASSERT_EQ(my_pid_, syscall(__NR_getpid));
87 virtual ErrorCode EvaluateSyscall(SandboxBPF* sandbox_compiler,
88 int system_call_number) const OVERRIDE {
89 if (!SandboxBPF::IsValidSyscallNumber(system_call_number)) {
90 return ErrorCode(ENOSYS);
91 } else if (system_call_number == __NR_ptrace) {
92 // The EvaluateSyscall function should run in the process that created
93 // the current object.
94 BPF_ASSERT_EQ(my_pid_, syscall(__NR_getpid));
95 return ErrorCode(ENOSYS);
97 return ErrorCode(ErrorCode::ERR_ALLOWED);
103 DISALLOW_COPY_AND_ASSIGN(EnosysPtracePolicy);
106 class BasicBPFTesterDelegate : public BPFTesterDelegate {
108 BasicBPFTesterDelegate() {}
109 virtual ~BasicBPFTesterDelegate() {}
111 virtual scoped_ptr<SandboxBPFPolicy> GetSandboxBPFPolicy() OVERRIDE {
112 return scoped_ptr<SandboxBPFPolicy>(new EnosysPtracePolicy());
114 virtual void RunTestFunction() OVERRIDE {
116 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL);
117 BPF_ASSERT(-1 == ret);
118 BPF_ASSERT(ENOSYS == errno);
122 DISALLOW_COPY_AND_ASSIGN(BasicBPFTesterDelegate);
125 // This is the most powerful and complex way to create a BPF test, but it
126 // requires a full class definition (BasicBPFTesterDelegate).
127 BPF_TEST_D(BPFTest, BPFTestWithDelegateClass, BasicBPFTesterDelegate);
129 // This is the simplest form of BPF tests.
130 BPF_TEST_C(BPFTest, BPFTestWithInlineTest, EnosysPtracePolicy) {
132 int ret = ptrace(PTRACE_TRACEME, -1, NULL, NULL);
133 BPF_ASSERT(-1 == ret);
134 BPF_ASSERT(ENOSYS == errno);
137 const char kHelloMessage[] = "Hello";
139 BPF_DEATH_TEST_C(BPFTest,
140 BPFDeathTestWithInlineTest,
141 DEATH_MESSAGE(kHelloMessage),
142 EnosysPtracePolicy) {
143 LOG(ERROR) << kHelloMessage;
149 } // namespace sandbox