| 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: |
| 30 PolicyCompiler(const Policy* policy, TrapRegistry* registry); | 31 PolicyCompiler(const Policy* policy, TrapRegistry* registry); |
| 31 ~PolicyCompiler(); | 32 ~PolicyCompiler(); |
| 32 | 33 |
| 33 // Compile registers any trap handlers needed by the policy and | 34 // Compile registers any trap handlers needed by the policy and |
| 34 // compiles the policy to a BPF program, which it returns. | 35 // compiles the policy to a BPF program, which it returns. |
| 35 scoped_ptr<CodeGen::Program> Compile(bool verify); | 36 scoped_ptr<CodeGen::Program> Compile(bool verify); |
| 36 | 37 |
| 37 // DangerousSetEscapePC sets the "escape PC" that is allowed to issue any | 38 // DangerousSetEscapePC sets the "escape PC" that is allowed to issue any |
| 38 // system calls, regardless of policy. | 39 // system calls, regardless of policy. |
| 39 void DangerousSetEscapePC(uint64_t escapepc); | 40 void DangerousSetEscapePC(uint64_t escapepc); |
| 40 | 41 |
| 42 // SetPanicFunc sets the trap function used for handling faulty |
| 43 // system call conditions. The function will be called with its aux |
| 44 // argument set to a pointer to a null-terminated character sequence |
| 45 // describing the fault. |
| 46 void SetPanicFunc(TrapRegistry::TrapFnc panic_func); |
| 47 |
| 41 // Error returns an ErrorCode to indicate the system call should fail with | 48 // Error returns an ErrorCode to indicate the system call should fail with |
| 42 // the specified error number. | 49 // the specified error number. |
| 43 ErrorCode Error(int err); | 50 ErrorCode Error(int err); |
| 44 | 51 |
| 45 // Trap returns an ErrorCode to indicate the system call should | 52 // Trap returns an ErrorCode to indicate the system call should |
| 46 // instead invoke a trap handler. | 53 // instead invoke a trap handler. |
| 47 ErrorCode Trap(TrapRegistry::TrapFnc fnc, const void* aux, bool safe); | 54 ErrorCode Trap(TrapRegistry::TrapFnc fnc, const void* aux, bool safe); |
| 48 | 55 |
| 49 // UnsafeTraps require some syscalls to always be allowed. | 56 // UnsafeTraps require some syscalls to always be allowed. |
| 50 // This helper function returns true for these calls. | 57 // This helper function returns true for these calls. |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 // called from RetExpression(). | 142 // called from RetExpression(). |
| 136 CodeGen::Node CondExpression(const ErrorCode& cond); | 143 CodeGen::Node CondExpression(const ErrorCode& cond); |
| 137 | 144 |
| 138 // Returns a BPF program that evaluates half of a conditional expression; | 145 // Returns a BPF program that evaluates half of a conditional expression; |
| 139 // it should only ever be called from CondExpression(). | 146 // it should only ever be called from CondExpression(). |
| 140 CodeGen::Node CondExpressionHalf(const ErrorCode& cond, | 147 CodeGen::Node CondExpressionHalf(const ErrorCode& cond, |
| 141 ArgHalf half, | 148 ArgHalf half, |
| 142 CodeGen::Node passed, | 149 CodeGen::Node passed, |
| 143 CodeGen::Node failed); | 150 CodeGen::Node failed); |
| 144 | 151 |
| 152 bpf_dsl::ResultExpr Panic(const char* msg); |
| 153 |
| 145 const Policy* policy_; | 154 const Policy* policy_; |
| 146 TrapRegistry* registry_; | 155 TrapRegistry* registry_; |
| 147 uint64_t escapepc_; | 156 uint64_t escapepc_; |
| 157 TrapRegistry::TrapFnc 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 |