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_BPF_DSL_POLICY_COMPILER_H_ | 5 #ifndef SANDBOX_LINUX_BPF_DSL_POLICY_COMPILER_H_ |
6 #define SANDBOX_LINUX_BPF_DSL_POLICY_COMPILER_H_ | 6 #define SANDBOX_LINUX_BPF_DSL_POLICY_COMPILER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <set> | 11 #include <set> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "sandbox/linux/bpf_dsl/bpf_dsl_forward.h" | 16 #include "sandbox/linux/bpf_dsl/bpf_dsl_forward.h" |
17 #include "sandbox/linux/bpf_dsl/codegen.h" | 17 #include "sandbox/linux/bpf_dsl/codegen.h" |
| 18 #include "sandbox/linux/bpf_dsl/trap_registry.h" |
18 #include "sandbox/linux/seccomp-bpf/errorcode.h" | 19 #include "sandbox/linux/seccomp-bpf/errorcode.h" |
19 #include "sandbox/sandbox_export.h" | 20 #include "sandbox/sandbox_export.h" |
20 | 21 |
21 namespace sandbox { | 22 namespace sandbox { |
22 namespace bpf_dsl { | 23 namespace bpf_dsl { |
23 class Policy; | 24 class Policy; |
24 | 25 |
25 // PolicyCompiler implements the bpf_dsl compiler, allowing users to | 26 // PolicyCompiler implements the bpf_dsl compiler, allowing users to |
26 // transform bpf_dsl policies into BPF programs to be executed by the | 27 // transform bpf_dsl policies into BPF programs to be executed by the |
27 // Linux kernel. | 28 // Linux kernel. |
28 class SANDBOX_EXPORT PolicyCompiler { | 29 class SANDBOX_EXPORT PolicyCompiler { |
29 public: | 30 public: |
| 31 using PanicFunc = bpf_dsl::ResultExpr (*)(const char* error); |
| 32 |
30 PolicyCompiler(const Policy* policy, TrapRegistry* registry); | 33 PolicyCompiler(const Policy* policy, TrapRegistry* registry); |
31 ~PolicyCompiler(); | 34 ~PolicyCompiler(); |
32 | 35 |
33 // Compile registers any trap handlers needed by the policy and | 36 // Compile registers any trap handlers needed by the policy and |
34 // compiles the policy to a BPF program, which it returns. | 37 // compiles the policy to a BPF program, which it returns. |
35 scoped_ptr<CodeGen::Program> Compile(bool verify); | 38 scoped_ptr<CodeGen::Program> Compile(bool verify); |
36 | 39 |
37 // DangerousSetEscapePC sets the "escape PC" that is allowed to issue any | 40 // DangerousSetEscapePC sets the "escape PC" that is allowed to issue any |
38 // system calls, regardless of policy. | 41 // system calls, regardless of policy. |
39 void DangerousSetEscapePC(uint64_t escapepc); | 42 void DangerousSetEscapePC(uint64_t escapepc); |
40 | 43 |
| 44 // SetPanicFunc sets the callback function used for handling faulty |
| 45 // system call conditions. The default behavior is to immediately kill |
| 46 // the process. |
| 47 // TODO(mdempsky): Move this into Policy? |
| 48 void SetPanicFunc(PanicFunc panic_func); |
| 49 |
41 // Error returns an ErrorCode to indicate the system call should fail with | 50 // Error returns an ErrorCode to indicate the system call should fail with |
42 // the specified error number. | 51 // the specified error number. |
43 ErrorCode Error(int err); | 52 ErrorCode Error(int err); |
44 | 53 |
45 // Trap returns an ErrorCode to indicate the system call should | 54 // Trap returns an ErrorCode to indicate the system call should |
46 // instead invoke a trap handler. | 55 // instead invoke a trap handler. |
47 ErrorCode Trap(TrapRegistry::TrapFnc fnc, const void* aux, bool safe); | 56 ErrorCode Trap(TrapRegistry::TrapFnc fnc, const void* aux, bool safe); |
48 | 57 |
49 // UnsafeTraps require some syscalls to always be allowed. | 58 // UnsafeTraps require some syscalls to always be allowed. |
50 // This helper function returns true for these calls. | 59 // This helper function returns true for these calls. |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 // Returns a BPF program that evaluates half of a conditional expression; | 147 // Returns a BPF program that evaluates half of a conditional expression; |
139 // it should only ever be called from CondExpression(). | 148 // it should only ever be called from CondExpression(). |
140 CodeGen::Node CondExpressionHalf(const ErrorCode& cond, | 149 CodeGen::Node CondExpressionHalf(const ErrorCode& cond, |
141 ArgHalf half, | 150 ArgHalf half, |
142 CodeGen::Node passed, | 151 CodeGen::Node passed, |
143 CodeGen::Node failed); | 152 CodeGen::Node failed); |
144 | 153 |
145 const Policy* policy_; | 154 const Policy* policy_; |
146 TrapRegistry* registry_; | 155 TrapRegistry* registry_; |
147 uint64_t escapepc_; | 156 uint64_t escapepc_; |
| 157 PanicFunc panic_func_; |
148 | 158 |
149 Conds conds_; | 159 Conds conds_; |
150 CodeGen gen_; | 160 CodeGen gen_; |
151 bool has_unsafe_traps_; | 161 bool has_unsafe_traps_; |
152 | 162 |
153 DISALLOW_COPY_AND_ASSIGN(PolicyCompiler); | 163 DISALLOW_COPY_AND_ASSIGN(PolicyCompiler); |
154 }; | 164 }; |
155 | 165 |
156 } // namespace bpf_dsl | 166 } // namespace bpf_dsl |
157 } // namespace sandbox | 167 } // namespace sandbox |
158 | 168 |
159 #endif // SANDBOX_LINUX_BPF_DSL_POLICY_COMPILER_H_ | 169 #endif // SANDBOX_LINUX_BPF_DSL_POLICY_COMPILER_H_ |
OLD | NEW |