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> |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 #include <limits> | 13 #include <limits> |
14 #include <map> | 14 #include <map> |
15 #include <set> | 15 #include <set> |
16 #include <utility> | 16 #include <utility> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "sandbox/linux/seccomp-bpf/die.h" | 20 #include "sandbox/linux/seccomp-bpf/die.h" |
21 #include "sandbox/linux/seccomp-bpf/errorcode.h" | 21 #include "sandbox/linux/seccomp-bpf/errorcode.h" |
22 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h" | 22 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h" |
23 | 23 |
24 namespace playground2 { | 24 namespace sandbox { |
25 | 25 |
26 struct arch_seccomp_data { | 26 struct arch_seccomp_data { |
27 int nr; | 27 int nr; |
28 uint32_t arch; | 28 uint32_t arch; |
29 uint64_t instruction_pointer; | 29 uint64_t instruction_pointer; |
30 uint64_t args[6]; | 30 uint64_t args[6]; |
31 }; | 31 }; |
32 | 32 |
33 struct arch_sigsys { | 33 struct arch_sigsys { |
34 void* ip; | 34 void* ip; |
35 int nr; | 35 int nr; |
36 unsigned int arch; | 36 unsigned int arch; |
37 }; | 37 }; |
38 | 38 |
39 class CodeGen; | 39 class CodeGen; |
| 40 class SandboxBPFPolicy; |
40 class SandboxUnittestHelper; | 41 class SandboxUnittestHelper; |
41 class SandboxBpfPolicy; | |
42 struct Instruction; | 42 struct Instruction; |
43 | 43 |
44 class Sandbox { | 44 class SandboxBPF { |
45 public: | 45 public: |
46 enum SandboxStatus { | 46 enum SandboxStatus { |
47 STATUS_UNKNOWN, // Status prior to calling supportsSeccompSandbox() | 47 STATUS_UNKNOWN, // Status prior to calling supportsSeccompSandbox() |
48 STATUS_UNSUPPORTED, // The kernel does not appear to support sandboxing | 48 STATUS_UNSUPPORTED, // The kernel does not appear to support sandboxing |
49 STATUS_UNAVAILABLE, // Currently unavailable but might work again later | 49 STATUS_UNAVAILABLE, // Currently unavailable but might work again later |
50 STATUS_AVAILABLE, // Sandboxing is available but not currently active | 50 STATUS_AVAILABLE, // Sandboxing is available but not currently active |
51 STATUS_ENABLED // The sandbox is now active | 51 STATUS_ENABLED // The sandbox is now active |
52 }; | 52 }; |
53 | 53 |
54 // When calling setSandboxPolicy(), the caller can provide an arbitrary | 54 // When calling setSandboxPolicy(), the caller can provide an arbitrary |
55 // pointer in |aux|. This pointer will then be forwarded to the sandbox | 55 // pointer in |aux|. This pointer will then be forwarded to the sandbox |
56 // policy each time a call is made through an EvaluateSyscall function | 56 // policy each time a call is made through an EvaluateSyscall function |
57 // pointer. One common use case would be to pass the "aux" pointer as an | 57 // pointer. One common use case would be to pass the "aux" pointer as an |
58 // argument to Trap() functions. | 58 // argument to Trap() functions. |
59 typedef ErrorCode (*EvaluateSyscall)(Sandbox* sandbox_compiler, | 59 typedef ErrorCode (*EvaluateSyscall)(SandboxBPF* sandbox_compiler, |
60 int system_call_number, | 60 int system_call_number, |
61 void* aux); | 61 void* aux); |
62 typedef std::vector<std::pair<EvaluateSyscall, void*> > Evaluators; | 62 typedef std::vector<std::pair<EvaluateSyscall, void*> > Evaluators; |
63 // A vector of BPF instructions that need to be installed as a filter | 63 // A vector of BPF instructions that need to be installed as a filter |
64 // program in the kernel. | 64 // program in the kernel. |
65 typedef std::vector<struct sock_filter> Program; | 65 typedef std::vector<struct sock_filter> Program; |
66 | 66 |
67 // Constructors and destructors. | 67 // Constructors and destructors. |
68 // NOTE: Setting a policy and starting the sandbox is a one-way operation. | 68 // NOTE: Setting a policy and starting the sandbox is a one-way operation. |
69 // The kernel does not provide any option for unloading a loaded | 69 // The kernel does not provide any option for unloading a loaded |
70 // sandbox. Strictly speaking, that means we should disallow calling | 70 // sandbox. Strictly speaking, that means we should disallow calling |
71 // the destructor, if StartSandbox() has ever been called. In practice, | 71 // the destructor, if StartSandbox() has ever been called. In practice, |
72 // this makes it needlessly complicated to operate on "Sandbox" | 72 // this makes it needlessly complicated to operate on "Sandbox" |
73 // objects. So, we instead opted to allow object destruction. But it | 73 // objects. So, we instead opted to allow object destruction. But it |
74 // should be noted that during its lifetime, the object probably made | 74 // should be noted that during its lifetime, the object probably made |
75 // irreversible state changes to the runtime environment. These changes | 75 // irreversible state changes to the runtime environment. These changes |
76 // stay in effect even after the destructor has been run. | 76 // stay in effect even after the destructor has been run. |
77 Sandbox(); | 77 SandboxBPF(); |
78 ~Sandbox(); | 78 ~SandboxBPF(); |
79 | 79 |
80 // Checks whether a particular system call number is valid on the current | 80 // Checks whether a particular system call number is valid on the current |
81 // architecture. E.g. on ARM there's a non-contiguous range of private | 81 // architecture. E.g. on ARM there's a non-contiguous range of private |
82 // system calls. | 82 // system calls. |
83 static bool IsValidSyscallNumber(int sysnum); | 83 static bool IsValidSyscallNumber(int sysnum); |
84 | 84 |
85 // There are a lot of reasons why the Seccomp sandbox might not be available. | 85 // There are a lot of reasons why the Seccomp sandbox might not be available. |
86 // This could be because the kernel does not support Seccomp mode, or it | 86 // This could be because the kernel does not support Seccomp mode, or it |
87 // could be because another sandbox is already active. | 87 // could be because another sandbox is already active. |
88 // "proc_fd" should be a file descriptor for "/proc", or -1 if not | 88 // "proc_fd" should be a file descriptor for "/proc", or -1 if not |
(...skipping 16 matching lines...) Expand all Loading... |
105 // evaluator. There are different possible uses for this data, but one of the | 105 // evaluator. There are different possible uses for this data, but one of the |
106 // use cases would be for the policy to then forward this pointer to a Trap() | 106 // use cases would be for the policy to then forward this pointer to a Trap() |
107 // handler. In this case, of course, the data that is pointed to must remain | 107 // handler. In this case, of course, the data that is pointed to must remain |
108 // valid for the entire time that Trap() handlers can be called; typically, | 108 // valid for the entire time that Trap() handlers can be called; typically, |
109 // this would be the lifetime of the program. | 109 // this would be the lifetime of the program. |
110 // DEPRECATED: use the policy interface below. | 110 // DEPRECATED: use the policy interface below. |
111 void SetSandboxPolicyDeprecated(EvaluateSyscall syscallEvaluator, void* aux); | 111 void SetSandboxPolicyDeprecated(EvaluateSyscall syscallEvaluator, void* aux); |
112 | 112 |
113 // Set the BPF policy as |policy|. Ownership of |policy| is transfered here | 113 // Set the BPF policy as |policy|. Ownership of |policy| is transfered here |
114 // to the sandbox object. | 114 // to the sandbox object. |
115 void SetSandboxPolicy(SandboxBpfPolicy* policy); | 115 void SetSandboxPolicy(SandboxBPFPolicy* policy); |
116 | 116 |
117 // We can use ErrorCode to request calling of a trap handler. This method | 117 // We can use ErrorCode to request calling of a trap handler. This method |
118 // performs the required wrapping of the callback function into an | 118 // performs the required wrapping of the callback function into an |
119 // ErrorCode object. | 119 // ErrorCode object. |
120 // The "aux" field can carry a pointer to arbitrary data. See EvaluateSyscall | 120 // The "aux" field can carry a pointer to arbitrary data. See EvaluateSyscall |
121 // for a description of how to pass data from SetSandboxPolicy() to a Trap() | 121 // for a description of how to pass data from SetSandboxPolicy() to a Trap() |
122 // handler. | 122 // handler. |
123 ErrorCode Trap(Trap::TrapFnc fnc, const void* aux); | 123 ErrorCode Trap(Trap::TrapFnc fnc, const void* aux); |
124 | 124 |
125 // Calls a user-space trap handler and disables all sandboxing for system | 125 // Calls a user-space trap handler and disables all sandboxing for system |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 EvaluateSyscall syscall_evaluator, | 217 EvaluateSyscall syscall_evaluator, |
218 void* aux); | 218 void* aux); |
219 | 219 |
220 // Performs a couple of sanity checks to verify that the kernel supports the | 220 // Performs a couple of sanity checks to verify that the kernel supports the |
221 // features that we need for successful sandboxing. | 221 // features that we need for successful sandboxing. |
222 // The caller has to make sure that "this" has not yet been initialized with | 222 // The caller has to make sure that "this" has not yet been initialized with |
223 // any other policies. | 223 // any other policies. |
224 bool KernelSupportSeccompBPF(); | 224 bool KernelSupportSeccompBPF(); |
225 | 225 |
226 // Verify that the current policy passes some basic sanity checks. | 226 // Verify that the current policy passes some basic sanity checks. |
227 void PolicySanityChecks(SandboxBpfPolicy* policy); | 227 void PolicySanityChecks(SandboxBPFPolicy* policy); |
228 | 228 |
229 // Assembles and installs a filter based on the policy that has previously | 229 // Assembles and installs a filter based on the policy that has previously |
230 // been configured with SetSandboxPolicy(). | 230 // been configured with SetSandboxPolicy(). |
231 void InstallFilter(); | 231 void InstallFilter(); |
232 | 232 |
233 // Verify the correctness of a compiled program by comparing it against the | 233 // Verify the correctness of a compiled program by comparing it against the |
234 // current policy. This function should only ever be called by unit tests and | 234 // current policy. This function should only ever be called by unit tests and |
235 // by the sandbox internals. It should not be used by production code. | 235 // by the sandbox internals. It should not be used by production code. |
236 void VerifyProgram(const Program& program, bool has_unsafe_traps); | 236 void VerifyProgram(const Program& program, bool has_unsafe_traps); |
237 | 237 |
(...skipping 19 matching lines...) Expand all Loading... |
257 // Returns a BPF program that evaluates the conditional expression in | 257 // Returns a BPF program that evaluates the conditional expression in |
258 // "cond" and returns the appropriate value from the BPF filter program. | 258 // "cond" and returns the appropriate value from the BPF filter program. |
259 // This function recursively calls RetExpression(); it should only ever be | 259 // This function recursively calls RetExpression(); it should only ever be |
260 // called from RetExpression(). | 260 // called from RetExpression(). |
261 Instruction* CondExpression(CodeGen* gen, const ErrorCode& cond); | 261 Instruction* CondExpression(CodeGen* gen, const ErrorCode& cond); |
262 | 262 |
263 static SandboxStatus status_; | 263 static SandboxStatus status_; |
264 | 264 |
265 bool quiet_; | 265 bool quiet_; |
266 int proc_fd_; | 266 int proc_fd_; |
267 scoped_ptr<const SandboxBpfPolicy> policy_; | 267 scoped_ptr<const SandboxBPFPolicy> policy_; |
268 Conds* conds_; | 268 Conds* conds_; |
269 bool sandbox_has_started_; | 269 bool sandbox_has_started_; |
270 | 270 |
271 DISALLOW_COPY_AND_ASSIGN(Sandbox); | 271 DISALLOW_COPY_AND_ASSIGN(SandboxBPF); |
272 }; | 272 }; |
273 | 273 |
274 } // namespace | 274 } // namespace sandbox |
275 | 275 |
276 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ | 276 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ |
OLD | NEW |