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" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 // irreversible state changes to the runtime environment. These changes | 52 // irreversible state changes to the runtime environment. These changes |
53 // stay in effect even after the destructor has been run. | 53 // stay in effect even after the destructor has been run. |
54 SandboxBPF(); | 54 SandboxBPF(); |
55 ~SandboxBPF(); | 55 ~SandboxBPF(); |
56 | 56 |
57 // Checks whether a particular system call number is valid on the current | 57 // Checks whether a particular system call number is valid on the current |
58 // architecture. E.g. on ARM there's a non-contiguous range of private | 58 // architecture. E.g. on ARM there's a non-contiguous range of private |
59 // system calls. | 59 // system calls. |
60 static bool IsValidSyscallNumber(int sysnum); | 60 static bool IsValidSyscallNumber(int sysnum); |
61 | 61 |
62 // There are a lot of reasons why the Seccomp sandbox might not be available. | 62 // Detect if the kernel supports the seccomp sandbox. The result of calling |
63 // This could be because the kernel does not support Seccomp mode, or it | 63 // this function will be cached. The first time this function is called, the |
64 // could be because another sandbox is already active. | 64 // running process must be unsandboxed (able to use /proc) and monothreaded. |
65 // "proc_fd" should be a file descriptor for "/proc", or -1 if not | 65 static SandboxStatus SupportsSeccompSandbox(); |
66 // provided by the caller. | |
67 static SandboxStatus SupportsSeccompSandbox(int proc_fd); | |
68 | 66 |
69 // Determines if the kernel has support for the seccomp() system call to | 67 // Determines if the kernel has support for the seccomp() system call to |
70 // synchronize BPF filters across a thread group. | 68 // synchronize BPF filters across a thread group. |
71 static SandboxStatus SupportsSeccompThreadFilterSynchronization(); | 69 static SandboxStatus SupportsSeccompThreadFilterSynchronization(); |
72 | 70 |
73 // The sandbox needs to be able to access files in "/proc/self". If this | 71 // The sandbox needs to be able to access files in "/proc/self/tasks/". If |
Jorge Lucangeli Obes
2014/11/24 23:48:33
Ditto task.
jln (very slow on Chromium)
2014/11/25 01:30:48
Done.
| |
72 // this | |
74 // directory is not accessible when "startSandbox()" gets called, the caller | 73 // directory is not accessible when "startSandbox()" gets called, the caller |
75 // can provide an already opened file descriptor by calling "set_proc_fd()". | 74 // must provide an already opened file descriptor by calling |
76 // The sandbox becomes the new owner of this file descriptor and will | 75 // "set_proc_task_fd()". The sandbox becomes the new owner of this file |
77 // eventually close it when "StartSandbox()" executes. | 76 // descriptor and will eventually close it when "StartSandbox()" executes. |
78 void set_proc_fd(int proc_fd); | 77 void set_proc_task_fd(int proc_task_fd); |
79 | 78 |
80 // Set the BPF policy as |policy|. Ownership of |policy| is transfered here | 79 // Set the BPF policy as |policy|. Ownership of |policy| is transfered here |
81 // to the sandbox object. | 80 // to the sandbox object. |
82 void SetSandboxPolicy(bpf_dsl::Policy* policy); | 81 void SetSandboxPolicy(bpf_dsl::Policy* policy); |
83 | 82 |
84 // UnsafeTraps require some syscalls to always be allowed. | 83 // UnsafeTraps require some syscalls to always be allowed. |
85 // This helper function returns true for these calls. | 84 // This helper function returns true for these calls. |
86 static bool IsRequiredForUnsafeTrap(int sysno); | 85 static bool IsRequiredForUnsafeTrap(int sysno); |
87 | 86 |
88 // From within an UnsafeTrap() it is often useful to be able to execute | 87 // From within an UnsafeTrap() it is often useful to be able to execute |
(...skipping 26 matching lines...) Expand all Loading... | |
115 // Typically, AssembleFilter() is only used by unit tests and by sandbox | 114 // Typically, AssembleFilter() is only used by unit tests and by sandbox |
116 // internals. It should not be used by production code. | 115 // internals. It should not be used by production code. |
117 // For performance reasons, we normally only run the assembled BPF program | 116 // For performance reasons, we normally only run the assembled BPF program |
118 // through the verifier, iff the program was built in debug mode. | 117 // through the verifier, iff the program was built in debug mode. |
119 // But by setting "force_verification", the caller can request that the | 118 // But by setting "force_verification", the caller can request that the |
120 // verifier is run unconditionally. This is useful for unittests. | 119 // verifier is run unconditionally. This is useful for unittests. |
121 scoped_ptr<CodeGen::Program> AssembleFilter(bool force_verification); | 120 scoped_ptr<CodeGen::Program> AssembleFilter(bool force_verification); |
122 | 121 |
123 private: | 122 private: |
124 // Get a file descriptor pointing to "/proc", if currently available. | 123 // Get a file descriptor pointing to "/proc", if currently available. |
125 int proc_fd() { return proc_fd_; } | 124 int proc_task_fd() { return proc_task_fd_; } |
126 | 125 |
127 // Creates a subprocess and runs "code_in_sandbox" inside of the specified | 126 // 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 | 127 // policy. The caller has to make sure that "this" has not yet been |
129 // initialized with any other policies. | 128 // initialized with any other policies. |
130 bool RunFunctionInPolicy(void (*code_in_sandbox)(), | 129 bool RunFunctionInPolicy(void (*code_in_sandbox)(), |
131 scoped_ptr<bpf_dsl::Policy> policy); | 130 scoped_ptr<bpf_dsl::Policy> policy); |
132 | 131 |
133 // Performs a couple of sanity checks to verify that the kernel supports the | 132 // Performs a couple of sanity checks to verify that the kernel supports the |
134 // features that we need for successful sandboxing. | 133 // features that we need for successful sandboxing. |
135 // The caller has to make sure that "this" has not yet been initialized with | 134 // The caller has to make sure that "this" has not yet been initialized with |
136 // any other policies. | 135 // any other policies. |
137 bool KernelSupportSeccompBPF(); | 136 bool KernelSupportSeccompBPF(); |
138 | 137 |
139 // Assembles and installs a filter based on the policy that has previously | 138 // Assembles and installs a filter based on the policy that has previously |
140 // been configured with SetSandboxPolicy(). | 139 // been configured with SetSandboxPolicy(). |
141 void InstallFilter(bool must_sync_threads); | 140 void InstallFilter(bool must_sync_threads); |
142 | 141 |
143 // Verify the correctness of a compiled program by comparing it against the | 142 // 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 | 143 // 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. | 144 // by the sandbox internals. It should not be used by production code. |
146 void VerifyProgram(const CodeGen::Program& program); | 145 void VerifyProgram(const CodeGen::Program& program); |
147 | 146 |
148 static SandboxStatus status_; | 147 static SandboxStatus status_; |
149 | 148 |
150 bool quiet_; | 149 bool quiet_; |
151 int proc_fd_; | 150 int proc_task_fd_; |
152 bool sandbox_has_started_; | 151 bool sandbox_has_started_; |
153 scoped_ptr<bpf_dsl::Policy> policy_; | 152 scoped_ptr<bpf_dsl::Policy> policy_; |
154 | 153 |
155 DISALLOW_COPY_AND_ASSIGN(SandboxBPF); | 154 DISALLOW_COPY_AND_ASSIGN(SandboxBPF); |
156 }; | 155 }; |
157 | 156 |
158 } // namespace sandbox | 157 } // namespace sandbox |
159 | 158 |
160 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ | 159 #endif // SANDBOX_LINUX_SECCOMP_BPF_SANDBOX_BPF_H__ |
OLD | NEW |