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_SECCOMP_BPF_SANDBOX_BPF_H__ | 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ |
6 #define SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ | 6 #define SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #include <sys/wait.h> | 10 #include <sys/wait.h> |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // function, as it avoid the extra round-trip to the signal handler. And | 140 // function, as it avoid the extra round-trip to the signal handler. And |
141 // it automatically does the correct thing to report kernel-style error | 141 // it automatically does the correct thing to report kernel-style error |
142 // conditions, rather than setting errno. See the comments for TrapFnc for | 142 // conditions, rather than setting errno. See the comments for TrapFnc for |
143 // details. In other words, the return value from ForwardSyscall() is | 143 // details. In other words, the return value from ForwardSyscall() is |
144 // directly suitable as a return value for a trap handler. | 144 // directly suitable as a return value for a trap handler. |
145 static intptr_t ForwardSyscall(const struct arch_seccomp_data& args); | 145 static intptr_t ForwardSyscall(const struct arch_seccomp_data& args); |
146 | 146 |
147 // We can also use ErrorCode to request evaluation of a conditional | 147 // We can also use ErrorCode to request evaluation of a conditional |
148 // statement based on inspection of system call parameters. | 148 // statement based on inspection of system call parameters. |
149 // This method wrap an ErrorCode object around the conditional statement. | 149 // This method wrap an ErrorCode object around the conditional statement. |
150 // Argument "argno" (1..6) will be compared to "value" using comparator | 150 // Argument "argno" (1..6) will be bitwise-AND'd with "mask" and compared |
151 // "op". If the condition is true "passed" will be returned, otherwise | 151 // to "value"; if equal, then "passed" will be returned, otherwise "failed". |
152 // "failed". | |
153 // If "is32bit" is set, the argument must in the range of 0x0..(1u << 32 - 1) | 152 // If "is32bit" is set, the argument must in the range of 0x0..(1u << 32 - 1) |
154 // If it is outside this range, the sandbox treats the system call just | 153 // If it is outside this range, the sandbox treats the system call just |
155 // the same as any other ABI violation (i.e. it aborts with an error | 154 // the same as any other ABI violation (i.e. it aborts with an error |
156 // message). | 155 // message). |
| 156 ErrorCode CondMaskedEqual(int argno, |
| 157 ErrorCode::ArgType is_32bit, |
| 158 uint64_t mask, |
| 159 uint64_t value, |
| 160 const ErrorCode& passed, |
| 161 const ErrorCode& failed); |
| 162 |
| 163 // Legacy variant of CondMaskedEqual that supports a few comparison |
| 164 // operations, which get converted into masked-equality comparisons. |
157 ErrorCode Cond(int argno, | 165 ErrorCode Cond(int argno, |
158 ErrorCode::ArgType is_32bit, | 166 ErrorCode::ArgType is_32bit, |
159 ErrorCode::Operation op, | 167 ErrorCode::Operation op, |
160 uint64_t value, | 168 uint64_t value, |
161 const ErrorCode& passed, | 169 const ErrorCode& passed, |
162 const ErrorCode& failed); | 170 const ErrorCode& failed); |
163 | 171 |
164 // Kill the program and print an error message. | 172 // Kill the program and print an error message. |
165 ErrorCode Kill(const char* msg); | 173 ErrorCode Kill(const char* msg); |
166 | 174 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 struct Range { | 210 struct Range { |
203 Range(uint32_t f, uint32_t t, const ErrorCode& e) | 211 Range(uint32_t f, uint32_t t, const ErrorCode& e) |
204 : from(f), to(t), err(e) {} | 212 : from(f), to(t), err(e) {} |
205 uint32_t from, to; | 213 uint32_t from, to; |
206 ErrorCode err; | 214 ErrorCode err; |
207 }; | 215 }; |
208 typedef std::vector<Range> Ranges; | 216 typedef std::vector<Range> Ranges; |
209 typedef std::map<uint32_t, ErrorCode> ErrMap; | 217 typedef std::map<uint32_t, ErrorCode> ErrMap; |
210 typedef std::set<ErrorCode, struct ErrorCode::LessThan> Conds; | 218 typedef std::set<ErrorCode, struct ErrorCode::LessThan> Conds; |
211 | 219 |
| 220 // Used by CondExpressionHalf to track which half of the argument it's |
| 221 // emitting instructions for. |
| 222 enum ArgHalf { |
| 223 LowerHalf, |
| 224 UpperHalf, |
| 225 }; |
| 226 |
212 // Get a file descriptor pointing to "/proc", if currently available. | 227 // Get a file descriptor pointing to "/proc", if currently available. |
213 int proc_fd() { return proc_fd_; } | 228 int proc_fd() { return proc_fd_; } |
214 | 229 |
215 // Creates a subprocess and runs "code_in_sandbox" inside of the specified | 230 // Creates a subprocess and runs "code_in_sandbox" inside of the specified |
216 // policy. The caller has to make sure that "this" has not yet been | 231 // policy. The caller has to make sure that "this" has not yet been |
217 // initialized with any other policies. | 232 // initialized with any other policies. |
218 bool RunFunctionInPolicy(void (*code_in_sandbox)(), | 233 bool RunFunctionInPolicy(void (*code_in_sandbox)(), |
219 scoped_ptr<SandboxBPFPolicy> policy); | 234 scoped_ptr<SandboxBPFPolicy> policy); |
220 | 235 |
221 // Performs a couple of sanity checks to verify that the kernel supports the | 236 // Performs a couple of sanity checks to verify that the kernel supports the |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 // CondExpression() and possibly RetExpression() to build a complex set of | 269 // CondExpression() and possibly RetExpression() to build a complex set of |
255 // instructions. | 270 // instructions. |
256 Instruction* RetExpression(CodeGen* gen, const ErrorCode& err); | 271 Instruction* RetExpression(CodeGen* gen, const ErrorCode& err); |
257 | 272 |
258 // Returns a BPF program that evaluates the conditional expression in | 273 // Returns a BPF program that evaluates the conditional expression in |
259 // "cond" and returns the appropriate value from the BPF filter program. | 274 // "cond" and returns the appropriate value from the BPF filter program. |
260 // This function recursively calls RetExpression(); it should only ever be | 275 // This function recursively calls RetExpression(); it should only ever be |
261 // called from RetExpression(). | 276 // called from RetExpression(). |
262 Instruction* CondExpression(CodeGen* gen, const ErrorCode& cond); | 277 Instruction* CondExpression(CodeGen* gen, const ErrorCode& cond); |
263 | 278 |
| 279 // Returns a BPF program that evaluates half of a conditional expression; |
| 280 // it should only ever be called from CondExpression(). |
| 281 Instruction* CondExpressionHalf(CodeGen* gen, |
| 282 const ErrorCode& cond, |
| 283 ArgHalf half, |
| 284 Instruction* passed, |
| 285 Instruction* failed); |
| 286 |
264 static SandboxStatus status_; | 287 static SandboxStatus status_; |
265 | 288 |
266 bool quiet_; | 289 bool quiet_; |
267 int proc_fd_; | 290 int proc_fd_; |
268 scoped_ptr<const SandboxBPFPolicy> policy_; | 291 scoped_ptr<const SandboxBPFPolicy> policy_; |
269 Conds* conds_; | 292 Conds* conds_; |
270 bool sandbox_has_started_; | 293 bool sandbox_has_started_; |
271 | 294 |
272 DISALLOW_COPY_AND_ASSIGN(SandboxBPF); | 295 DISALLOW_COPY_AND_ASSIGN(SandboxBPF); |
273 }; | 296 }; |
274 | 297 |
275 } // namespace sandbox | 298 } // namespace sandbox |
276 | 299 |
277 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ | 300 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ |
OLD | NEW |