| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 // StartSandbox(), the program should indicate whether or not the sandbox | 58 // StartSandbox(), the program should indicate whether or not the sandbox |
| 59 // should try and engage with multi-thread support. | 59 // should try and engage with multi-thread support. |
| 60 enum SandboxThreadState { | 60 enum SandboxThreadState { |
| 61 PROCESS_INVALID, | 61 PROCESS_INVALID, |
| 62 PROCESS_SINGLE_THREADED, // The program is currently single-threaded. | 62 PROCESS_SINGLE_THREADED, // The program is currently single-threaded. |
| 63 // Note: PROCESS_MULTI_THREADED requires experimental kernel support that | 63 // Note: PROCESS_MULTI_THREADED requires experimental kernel support that |
| 64 // has not been contributed to upstream Linux. | 64 // has not been contributed to upstream Linux. |
| 65 PROCESS_MULTI_THREADED, // The program may be multi-threaded. | 65 PROCESS_MULTI_THREADED, // The program may be multi-threaded. |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // When calling setSandboxPolicy(), the caller can provide an arbitrary |
| 69 // pointer in |aux|. This pointer will then be forwarded to the sandbox |
| 70 // policy each time a call is made through an EvaluateSyscall function |
| 71 // pointer. One common use case would be to pass the "aux" pointer as an |
| 72 // argument to Trap() functions. |
| 73 typedef ErrorCode (*EvaluateSyscall)(SandboxBPF* sandbox_compiler, |
| 74 int system_call_number, |
| 75 void* aux); |
| 68 // A vector of BPF instructions that need to be installed as a filter | 76 // A vector of BPF instructions that need to be installed as a filter |
| 69 // program in the kernel. | 77 // program in the kernel. |
| 70 typedef std::vector<struct sock_filter> Program; | 78 typedef std::vector<struct sock_filter> Program; |
| 71 | 79 |
| 72 // Constructors and destructors. | 80 // Constructors and destructors. |
| 73 // NOTE: Setting a policy and starting the sandbox is a one-way operation. | 81 // NOTE: Setting a policy and starting the sandbox is a one-way operation. |
| 74 // The kernel does not provide any option for unloading a loaded | 82 // The kernel does not provide any option for unloading a loaded |
| 75 // sandbox. Strictly speaking, that means we should disallow calling | 83 // sandbox. Strictly speaking, that means we should disallow calling |
| 76 // the destructor, if StartSandbox() has ever been called. In practice, | 84 // the destructor, if StartSandbox() has ever been called. In practice, |
| 77 // this makes it needlessly complicated to operate on "Sandbox" | 85 // this makes it needlessly complicated to operate on "Sandbox" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 94 // provided by the caller. | 102 // provided by the caller. |
| 95 static SandboxStatus SupportsSeccompSandbox(int proc_fd); | 103 static SandboxStatus SupportsSeccompSandbox(int proc_fd); |
| 96 | 104 |
| 97 // The sandbox needs to be able to access files in "/proc/self". If this | 105 // The sandbox needs to be able to access files in "/proc/self". If this |
| 98 // directory is not accessible when "startSandbox()" gets called, the caller | 106 // directory is not accessible when "startSandbox()" gets called, the caller |
| 99 // can provide an already opened file descriptor by calling "set_proc_fd()". | 107 // can provide an already opened file descriptor by calling "set_proc_fd()". |
| 100 // The sandbox becomes the new owner of this file descriptor and will | 108 // The sandbox becomes the new owner of this file descriptor and will |
| 101 // eventually close it when "StartSandbox()" executes. | 109 // eventually close it when "StartSandbox()" executes. |
| 102 void set_proc_fd(int proc_fd); | 110 void set_proc_fd(int proc_fd); |
| 103 | 111 |
| 112 // The system call evaluator function is called with the system |
| 113 // call number. It can decide to allow the system call unconditionally |
| 114 // by returning ERR_ALLOWED; it can deny the system call unconditionally by |
| 115 // returning an appropriate "errno" value; or it can request inspection |
| 116 // of system call argument(s) by returning a suitable ErrorCode. |
| 117 // The "aux" parameter can be used to pass optional data to the system call |
| 118 // evaluator. There are different possible uses for this data, but one of the |
| 119 // use cases would be for the policy to then forward this pointer to a Trap() |
| 120 // handler. In this case, of course, the data that is pointed to must remain |
| 121 // valid for the entire time that Trap() handlers can be called; typically, |
| 122 // this would be the lifetime of the program. |
| 123 // DEPRECATED: use the policy interface below. |
| 124 void SetSandboxPolicyDeprecated(EvaluateSyscall syscallEvaluator, void* aux); |
| 125 |
| 104 // Set the BPF policy as |policy|. Ownership of |policy| is transfered here | 126 // Set the BPF policy as |policy|. Ownership of |policy| is transfered here |
| 105 // to the sandbox object. | 127 // to the sandbox object. |
| 106 void SetSandboxPolicy(SandboxBPFPolicy* policy); | 128 void SetSandboxPolicy(SandboxBPFPolicy* policy); |
| 107 | 129 |
| 108 // We can use ErrorCode to request calling of a trap handler. This method | 130 // We can use ErrorCode to request calling of a trap handler. This method |
| 109 // performs the required wrapping of the callback function into an | 131 // performs the required wrapping of the callback function into an |
| 110 // ErrorCode object. | 132 // ErrorCode object. |
| 111 // The "aux" field can carry a pointer to arbitrary data. See EvaluateSyscall | 133 // The "aux" field can carry a pointer to arbitrary data. See EvaluateSyscall |
| 112 // for a description of how to pass data from SetSandboxPolicy() to a Trap() | 134 // for a description of how to pass data from SetSandboxPolicy() to a Trap() |
| 113 // handler. | 135 // handler. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 typedef std::map<uint32_t, ErrorCode> ErrMap; | 222 typedef std::map<uint32_t, ErrorCode> ErrMap; |
| 201 typedef std::set<ErrorCode, struct ErrorCode::LessThan> Conds; | 223 typedef std::set<ErrorCode, struct ErrorCode::LessThan> Conds; |
| 202 | 224 |
| 203 // Get a file descriptor pointing to "/proc", if currently available. | 225 // Get a file descriptor pointing to "/proc", if currently available. |
| 204 int proc_fd() { return proc_fd_; } | 226 int proc_fd() { return proc_fd_; } |
| 205 | 227 |
| 206 // Creates a subprocess and runs "code_in_sandbox" inside of the specified | 228 // Creates a subprocess and runs "code_in_sandbox" inside of the specified |
| 207 // policy. The caller has to make sure that "this" has not yet been | 229 // policy. The caller has to make sure that "this" has not yet been |
| 208 // initialized with any other policies. | 230 // initialized with any other policies. |
| 209 bool RunFunctionInPolicy(void (*code_in_sandbox)(), | 231 bool RunFunctionInPolicy(void (*code_in_sandbox)(), |
| 210 scoped_ptr<SandboxBPFPolicy> policy); | 232 EvaluateSyscall syscall_evaluator, |
| 233 void* aux); |
| 211 | 234 |
| 212 // Performs a couple of sanity checks to verify that the kernel supports the | 235 // Performs a couple of sanity checks to verify that the kernel supports the |
| 213 // features that we need for successful sandboxing. | 236 // features that we need for successful sandboxing. |
| 214 // The caller has to make sure that "this" has not yet been initialized with | 237 // The caller has to make sure that "this" has not yet been initialized with |
| 215 // any other policies. | 238 // any other policies. |
| 216 bool KernelSupportSeccompBPF(); | 239 bool KernelSupportSeccompBPF(); |
| 217 | 240 |
| 218 // Verify that the current policy passes some basic sanity checks. | 241 // Verify that the current policy passes some basic sanity checks. |
| 219 void PolicySanityChecks(SandboxBPFPolicy* policy); | 242 void PolicySanityChecks(SandboxBPFPolicy* policy); |
| 220 | 243 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 scoped_ptr<const SandboxBPFPolicy> policy_; | 282 scoped_ptr<const SandboxBPFPolicy> policy_; |
| 260 Conds* conds_; | 283 Conds* conds_; |
| 261 bool sandbox_has_started_; | 284 bool sandbox_has_started_; |
| 262 | 285 |
| 263 DISALLOW_COPY_AND_ASSIGN(SandboxBPF); | 286 DISALLOW_COPY_AND_ASSIGN(SandboxBPF); |
| 264 }; | 287 }; |
| 265 | 288 |
| 266 } // namespace sandbox | 289 } // namespace sandbox |
| 267 | 290 |
| 268 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ | 291 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ |
| OLD | NEW |