| 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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "sandbox/linux/seccomp-bpf/codegen.h" | 13 #include "sandbox/linux/seccomp-bpf/codegen.h" |
| 14 #include "sandbox/sandbox_export.h" | 14 #include "sandbox/sandbox_export.h" |
| 15 | 15 |
| 16 namespace sandbox { | 16 namespace sandbox { |
| 17 struct arch_seccomp_data; | 17 struct arch_seccomp_data; |
| 18 namespace bpf_dsl { | 18 namespace bpf_dsl { |
| 19 class SandboxBPFDSLPolicy; | 19 class Policy; |
| 20 } | 20 } |
| 21 | 21 |
| 22 class SANDBOX_EXPORT SandboxBPF { | 22 class SANDBOX_EXPORT SandboxBPF { |
| 23 public: | 23 public: |
| 24 enum SandboxStatus { | 24 enum SandboxStatus { |
| 25 STATUS_UNKNOWN, // Status prior to calling supportsSeccompSandbox() | 25 STATUS_UNKNOWN, // Status prior to calling supportsSeccompSandbox() |
| 26 STATUS_UNSUPPORTED, // The kernel does not appear to support sandboxing | 26 STATUS_UNSUPPORTED, // The kernel does not appear to support sandboxing |
| 27 STATUS_UNAVAILABLE, // Currently unavailable but might work again later | 27 STATUS_UNAVAILABLE, // Currently unavailable but might work again later |
| 28 STATUS_AVAILABLE, // Sandboxing is available but not currently active | 28 STATUS_AVAILABLE, // Sandboxing is available but not currently active |
| 29 STATUS_ENABLED // The sandbox is now active | 29 STATUS_ENABLED // The sandbox is now active |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 72 |
| 73 // The sandbox needs to be able to access files in "/proc/self". If this | 73 // The sandbox needs to be able to access files in "/proc/self". If this |
| 74 // directory is not accessible when "startSandbox()" gets called, the caller | 74 // directory is not accessible when "startSandbox()" gets called, the caller |
| 75 // can provide an already opened file descriptor by calling "set_proc_fd()". | 75 // can provide an already opened file descriptor by calling "set_proc_fd()". |
| 76 // The sandbox becomes the new owner of this file descriptor and will | 76 // The sandbox becomes the new owner of this file descriptor and will |
| 77 // eventually close it when "StartSandbox()" executes. | 77 // eventually close it when "StartSandbox()" executes. |
| 78 void set_proc_fd(int proc_fd); | 78 void set_proc_fd(int proc_fd); |
| 79 | 79 |
| 80 // Set the BPF policy as |policy|. Ownership of |policy| is transfered here | 80 // Set the BPF policy as |policy|. Ownership of |policy| is transfered here |
| 81 // to the sandbox object. | 81 // to the sandbox object. |
| 82 void SetSandboxPolicy(bpf_dsl::SandboxBPFDSLPolicy* policy); | 82 void SetSandboxPolicy(bpf_dsl::Policy* policy); |
| 83 | 83 |
| 84 // UnsafeTraps require some syscalls to always be allowed. | 84 // UnsafeTraps require some syscalls to always be allowed. |
| 85 // This helper function returns true for these calls. | 85 // This helper function returns true for these calls. |
| 86 static bool IsRequiredForUnsafeTrap(int sysno); | 86 static bool IsRequiredForUnsafeTrap(int sysno); |
| 87 | 87 |
| 88 // From within an UnsafeTrap() it is often useful to be able to execute | 88 // From within an UnsafeTrap() it is often useful to be able to execute |
| 89 // the system call that triggered the trap. The ForwardSyscall() method | 89 // the system call that triggered the trap. The ForwardSyscall() method |
| 90 // makes this easy. It is more efficient than calling glibc's syscall() | 90 // makes this easy. It is more efficient than calling glibc's syscall() |
| 91 // function, as it avoid the extra round-trip to the signal handler. And | 91 // function, as it avoid the extra round-trip to the signal handler. And |
| 92 // it automatically does the correct thing to report kernel-style error | 92 // it automatically does the correct thing to report kernel-style error |
| (...skipping 28 matching lines...) Expand all Loading... |
| 121 scoped_ptr<CodeGen::Program> AssembleFilter(bool force_verification); | 121 scoped_ptr<CodeGen::Program> AssembleFilter(bool force_verification); |
| 122 | 122 |
| 123 private: | 123 private: |
| 124 // Get a file descriptor pointing to "/proc", if currently available. | 124 // Get a file descriptor pointing to "/proc", if currently available. |
| 125 int proc_fd() { return proc_fd_; } | 125 int proc_fd() { return proc_fd_; } |
| 126 | 126 |
| 127 // Creates a subprocess and runs "code_in_sandbox" inside of the specified | 127 // Creates a subprocess and runs "code_in_sandbox" inside of the specified |
| 128 // policy. The caller has to make sure that "this" has not yet been | 128 // policy. The caller has to make sure that "this" has not yet been |
| 129 // initialized with any other policies. | 129 // initialized with any other policies. |
| 130 bool RunFunctionInPolicy(void (*code_in_sandbox)(), | 130 bool RunFunctionInPolicy(void (*code_in_sandbox)(), |
| 131 scoped_ptr<bpf_dsl::SandboxBPFDSLPolicy> policy); | 131 scoped_ptr<bpf_dsl::Policy> policy); |
| 132 | 132 |
| 133 // Performs a couple of sanity checks to verify that the kernel supports the | 133 // Performs a couple of sanity checks to verify that the kernel supports the |
| 134 // features that we need for successful sandboxing. | 134 // features that we need for successful sandboxing. |
| 135 // The caller has to make sure that "this" has not yet been initialized with | 135 // The caller has to make sure that "this" has not yet been initialized with |
| 136 // any other policies. | 136 // any other policies. |
| 137 bool KernelSupportSeccompBPF(); | 137 bool KernelSupportSeccompBPF(); |
| 138 | 138 |
| 139 // Assembles and installs a filter based on the policy that has previously | 139 // Assembles and installs a filter based on the policy that has previously |
| 140 // been configured with SetSandboxPolicy(). | 140 // been configured with SetSandboxPolicy(). |
| 141 void InstallFilter(bool must_sync_threads); | 141 void InstallFilter(bool must_sync_threads); |
| 142 | 142 |
| 143 // Verify the correctness of a compiled program by comparing it against the | 143 // Verify the correctness of a compiled program by comparing it against the |
| 144 // current policy. This function should only ever be called by unit tests and | 144 // current policy. This function should only ever be called by unit tests and |
| 145 // by the sandbox internals. It should not be used by production code. | 145 // by the sandbox internals. It should not be used by production code. |
| 146 void VerifyProgram(const CodeGen::Program& program); | 146 void VerifyProgram(const CodeGen::Program& program); |
| 147 | 147 |
| 148 static SandboxStatus status_; | 148 static SandboxStatus status_; |
| 149 | 149 |
| 150 bool quiet_; | 150 bool quiet_; |
| 151 int proc_fd_; | 151 int proc_fd_; |
| 152 bool sandbox_has_started_; | 152 bool sandbox_has_started_; |
| 153 scoped_ptr<bpf_dsl::SandboxBPFDSLPolicy> policy_; | 153 scoped_ptr<bpf_dsl::Policy> policy_; |
| 154 | 154 |
| 155 DISALLOW_COPY_AND_ASSIGN(SandboxBPF); | 155 DISALLOW_COPY_AND_ASSIGN(SandboxBPF); |
| 156 }; | 156 }; |
| 157 | 157 |
| 158 } // namespace sandbox | 158 } // namespace sandbox |
| 159 | 159 |
| 160 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ | 160 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ |
| OLD | NEW |