| 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/seccomp-bpf/codegen.h" | 17 #include "sandbox/linux/seccomp-bpf/codegen.h" |
| 17 #include "sandbox/linux/seccomp-bpf/errorcode.h" | 18 #include "sandbox/linux/seccomp-bpf/errorcode.h" |
| 18 #include "sandbox/sandbox_export.h" | 19 #include "sandbox/sandbox_export.h" |
| 19 | 20 |
| 20 namespace sandbox { | 21 namespace sandbox { |
| 21 namespace bpf_dsl { | 22 namespace bpf_dsl { |
| 22 class Policy; | 23 class Policy; |
| 23 | 24 |
| 24 // PolicyCompiler implements the bpf_dsl compiler, allowing users to | 25 // PolicyCompiler implements the bpf_dsl compiler, allowing users to |
| 25 // transform bpf_dsl policies into BPF programs to be executed by the | 26 // transform bpf_dsl policies into BPF programs to be executed by the |
| 26 // Linux kernel. | 27 // Linux kernel. |
| 27 class SANDBOX_EXPORT PolicyCompiler { | 28 class SANDBOX_EXPORT PolicyCompiler { |
| 28 public: | 29 public: |
| 29 PolicyCompiler(const Policy* policy, TrapRegistry* registry); | 30 PolicyCompiler(const Policy* policy, TrapRegistry* registry); |
| 30 ~PolicyCompiler(); | 31 ~PolicyCompiler(); |
| 31 | 32 |
| 32 // Compile registers any trap handlers needed by the policy and | 33 // Compile registers any trap handlers needed by the policy and |
| 33 // compiles the policy to a BPF program, which it returns. | 34 // compiles the policy to a BPF program, which it returns. |
| 34 scoped_ptr<CodeGen::Program> Compile(); | 35 scoped_ptr<CodeGen::Program> Compile(); |
| 35 | 36 |
| 36 // Error returns an ErrorCode to indicate the system call should fail with | 37 // Error returns an ErrorCode to indicate the system call should fail with |
| 37 // the specified error number. | 38 // the specified error number. |
| 38 ErrorCode Error(int err); | 39 ErrorCode Error(int err); |
| 39 | 40 |
| 40 // We can use ErrorCode to request calling of a trap handler. This method | 41 // Trap returns an ErrorCode to indicate the system call should |
| 41 // performs the required wrapping of the callback function into an | 42 // instead invoke a trap handler. |
| 42 // ErrorCode object. | 43 ErrorCode Trap(TrapRegistry::TrapFnc fnc, const void* aux, bool safe); |
| 43 // The "aux" field can carry a pointer to arbitrary data. See EvaluateSyscall | |
| 44 // for a description of how to pass data from SetSandboxPolicy() to a Trap() | |
| 45 // handler. | |
| 46 ErrorCode Trap(TrapRegistry::TrapFnc fnc, const void* aux); | |
| 47 | |
| 48 // Calls a user-space trap handler and disables all sandboxing for system | |
| 49 // calls made from this trap handler. | |
| 50 // This feature is available only if explicitly enabled by the user having | |
| 51 // set the CHROME_SANDBOX_DEBUGGING environment variable. | |
| 52 // Returns an ET_INVALID ErrorCode, if called when not enabled. | |
| 53 // NOTE: This feature, by definition, disables all security features of | |
| 54 // the sandbox. It should never be used in production, but it can be | |
| 55 // very useful to diagnose code that is incompatible with the sandbox. | |
| 56 // If even a single system call returns "UnsafeTrap", the security of | |
| 57 // entire sandbox should be considered compromised. | |
| 58 ErrorCode UnsafeTrap(TrapRegistry::TrapFnc fnc, const void* aux); | |
| 59 | 44 |
| 60 // UnsafeTraps require some syscalls to always be allowed. | 45 // UnsafeTraps require some syscalls to always be allowed. |
| 61 // This helper function returns true for these calls. | 46 // This helper function returns true for these calls. |
| 62 static bool IsRequiredForUnsafeTrap(int sysno); | 47 static bool IsRequiredForUnsafeTrap(int sysno); |
| 63 | 48 |
| 64 // We can also use ErrorCode to request evaluation of a conditional | 49 // We can also use ErrorCode to request evaluation of a conditional |
| 65 // statement based on inspection of system call parameters. | 50 // statement based on inspection of system call parameters. |
| 66 // This method wrap an ErrorCode object around the conditional statement. | 51 // This method wrap an ErrorCode object around the conditional statement. |
| 67 // Argument "argno" (1..6) will be bitwise-AND'd with "mask" and compared | 52 // Argument "argno" (1..6) will be bitwise-AND'd with "mask" and compared |
| 68 // to "value"; if equal, then "passed" will be returned, otherwise "failed". | 53 // to "value"; if equal, then "passed" will be returned, otherwise "failed". |
| 69 // If "is32bit" is set, the argument must in the range of 0x0..(1u << 32 - 1) | 54 // If "is32bit" is set, the argument must in the range of 0x0..(1u << 32 - 1) |
| 70 // If it is outside this range, the sandbox treats the system call just | 55 // If it is outside this range, the sandbox treats the system call just |
| 71 // the same as any other ABI violation (i.e. it aborts with an error | 56 // the same as any other ABI violation (i.e. it aborts with an error |
| 72 // message). | 57 // message). |
| 73 ErrorCode CondMaskedEqual(int argno, | 58 ErrorCode CondMaskedEqual(int argno, |
| 74 ErrorCode::ArgType is_32bit, | 59 ErrorCode::ArgType is_32bit, |
| 75 uint64_t mask, | 60 uint64_t mask, |
| 76 uint64_t value, | 61 uint64_t value, |
| 77 const ErrorCode& passed, | 62 const ErrorCode& passed, |
| 78 const ErrorCode& failed); | 63 const ErrorCode& failed); |
| 79 | 64 |
| 80 // Kill the program and print an error message. | |
| 81 ErrorCode Kill(const char* msg); | |
| 82 | |
| 83 // Returns the fatal ErrorCode that is used to indicate that somebody | 65 // Returns the fatal ErrorCode that is used to indicate that somebody |
| 84 // attempted to pass a 64bit value in a 32bit system call argument. | 66 // attempted to pass a 64bit value in a 32bit system call argument. |
| 85 // This method is primarily needed for testing purposes. | 67 // This method is primarily needed for testing purposes. |
| 86 ErrorCode Unexpected64bitArgument(); | 68 ErrorCode Unexpected64bitArgument(); |
| 87 | 69 |
| 88 private: | 70 private: |
| 89 struct Range; | 71 struct Range; |
| 90 typedef std::vector<Range> Ranges; | 72 typedef std::vector<Range> Ranges; |
| 91 typedef std::map<uint32_t, ErrorCode> ErrMap; | |
| 92 typedef std::set<ErrorCode, struct ErrorCode::LessThan> Conds; | 73 typedef std::set<ErrorCode, struct ErrorCode::LessThan> Conds; |
| 93 | 74 |
| 94 // Used by CondExpressionHalf to track which half of the argument it's | 75 // Used by CondExpressionHalf to track which half of the argument it's |
| 95 // emitting instructions for. | 76 // emitting instructions for. |
| 96 enum ArgHalf { | 77 enum ArgHalf { |
| 97 LowerHalf, | 78 LowerHalf, |
| 98 UpperHalf, | 79 UpperHalf, |
| 99 }; | 80 }; |
| 100 | 81 |
| 101 // Compile the configured policy into a complete instruction sequence. | 82 // Compile the configured policy into a complete instruction sequence. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 126 // sorted in ascending order of system call numbers. There are no gaps in the | 107 // sorted in ascending order of system call numbers. There are no gaps in the |
| 127 // ranges. System calls with identical ErrorCodes are coalesced into a single | 108 // ranges. System calls with identical ErrorCodes are coalesced into a single |
| 128 // range. | 109 // range. |
| 129 void FindRanges(Ranges* ranges); | 110 void FindRanges(Ranges* ranges); |
| 130 | 111 |
| 131 // Returns a BPF program snippet that implements a jump table for the | 112 // Returns a BPF program snippet that implements a jump table for the |
| 132 // given range of system call numbers. This function runs recursively. | 113 // given range of system call numbers. This function runs recursively. |
| 133 CodeGen::Node AssembleJumpTable(Ranges::const_iterator start, | 114 CodeGen::Node AssembleJumpTable(Ranges::const_iterator start, |
| 134 Ranges::const_iterator stop); | 115 Ranges::const_iterator stop); |
| 135 | 116 |
| 117 // CompileResult compiles an individual result expression into a |
| 118 // CodeGen node. |
| 119 CodeGen::Node CompileResult(const ResultExpr& res); |
| 120 |
| 136 // Returns a BPF program snippet that makes the BPF filter program exit | 121 // Returns a BPF program snippet that makes the BPF filter program exit |
| 137 // with the given ErrorCode "err". N.B. the ErrorCode may very well be a | 122 // with the given ErrorCode "err". N.B. the ErrorCode may very well be a |
| 138 // conditional expression; if so, this function will recursively call | 123 // conditional expression; if so, this function will recursively call |
| 139 // CondExpression() and possibly RetExpression() to build a complex set of | 124 // CondExpression() and possibly RetExpression() to build a complex set of |
| 140 // instructions. | 125 // instructions. |
| 141 CodeGen::Node RetExpression(const ErrorCode& err); | 126 CodeGen::Node RetExpression(const ErrorCode& err); |
| 142 | 127 |
| 143 // Returns a BPF program that evaluates the conditional expression in | 128 // Returns a BPF program that evaluates the conditional expression in |
| 144 // "cond" and returns the appropriate value from the BPF filter program. | 129 // "cond" and returns the appropriate value from the BPF filter program. |
| 145 // This function recursively calls RetExpression(); it should only ever be | 130 // This function recursively calls RetExpression(); it should only ever be |
| 146 // called from RetExpression(). | 131 // called from RetExpression(). |
| 147 CodeGen::Node CondExpression(const ErrorCode& cond); | 132 CodeGen::Node CondExpression(const ErrorCode& cond); |
| 148 | 133 |
| 149 // Returns a BPF program that evaluates half of a conditional expression; | 134 // Returns a BPF program that evaluates half of a conditional expression; |
| 150 // it should only ever be called from CondExpression(). | 135 // it should only ever be called from CondExpression(). |
| 151 CodeGen::Node CondExpressionHalf(const ErrorCode& cond, | 136 CodeGen::Node CondExpressionHalf(const ErrorCode& cond, |
| 152 ArgHalf half, | 137 ArgHalf half, |
| 153 CodeGen::Node passed, | 138 CodeGen::Node passed, |
| 154 CodeGen::Node failed); | 139 CodeGen::Node failed); |
| 155 | 140 |
| 156 // MakeTrap is the common implementation for Trap and UnsafeTrap. | |
| 157 ErrorCode MakeTrap(TrapRegistry::TrapFnc fnc, const void* aux, bool safe); | |
| 158 | |
| 159 const Policy* policy_; | 141 const Policy* policy_; |
| 160 TrapRegistry* registry_; | 142 TrapRegistry* registry_; |
| 161 | 143 |
| 162 Conds conds_; | 144 Conds conds_; |
| 163 CodeGen gen_; | 145 CodeGen gen_; |
| 164 bool has_unsafe_traps_; | 146 bool has_unsafe_traps_; |
| 165 | 147 |
| 166 DISALLOW_COPY_AND_ASSIGN(PolicyCompiler); | 148 DISALLOW_COPY_AND_ASSIGN(PolicyCompiler); |
| 167 }; | 149 }; |
| 168 | 150 |
| 169 } // namespace bpf_dsl | 151 } // namespace bpf_dsl |
| 170 } // namespace sandbox | 152 } // namespace sandbox |
| 171 | 153 |
| 172 #endif // SANDBOX_LINUX_BPF_DSL_POLICY_COMPILER_H_ | 154 #endif // SANDBOX_LINUX_BPF_DSL_POLICY_COMPILER_H_ |
| OLD | NEW |