OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "components/nacl/loader/sandbox_linux/nacl_sandbox_linux.h" | 5 #include "components/nacl/loader/sandbox_linux/nacl_sandbox_linux.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/prctl.h> | 9 #include <sys/prctl.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "base/posix/eintr_wrapper.h" | 23 #include "base/posix/eintr_wrapper.h" |
24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
25 #include "components/nacl/common/nacl_switches.h" | 25 #include "components/nacl/common/nacl_switches.h" |
26 #include "components/nacl/loader/nonsfi/nonsfi_sandbox.h" | 26 #include "components/nacl/loader/nonsfi/nonsfi_sandbox.h" |
27 #include "components/nacl/loader/sandbox_linux/nacl_bpf_sandbox_linux.h" | 27 #include "components/nacl/loader/sandbox_linux/nacl_bpf_sandbox_linux.h" |
28 #include "content/public/common/content_switches.h" | 28 #include "content/public/common/content_switches.h" |
29 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 29 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
30 #include "sandbox/linux/services/credentials.h" | 30 #include "sandbox/linux/services/credentials.h" |
31 #include "sandbox/linux/services/namespace_sandbox.h" | 31 #include "sandbox/linux/services/namespace_sandbox.h" |
32 #include "sandbox/linux/services/proc_util.h" | 32 #include "sandbox/linux/services/proc_util.h" |
| 33 #include "sandbox/linux/services/resource_limits.h" |
33 #include "sandbox/linux/services/thread_helpers.h" | 34 #include "sandbox/linux/services/thread_helpers.h" |
34 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" | 35 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" |
35 | 36 |
36 #if !defined(OS_NACL_NONSFI) | |
37 #include "sandbox/linux/services/resource_limits.h" | |
38 #endif | |
39 | 37 |
40 namespace nacl { | 38 namespace nacl { |
41 | 39 |
42 namespace { | 40 namespace { |
43 | 41 |
44 // This is a poor man's check on whether we are sandboxed. | 42 // This is a poor man's check on whether we are sandboxed. |
45 bool IsSandboxed() { | 43 bool IsSandboxed() { |
46 int proc_fd = open("/proc/self/exe", O_RDONLY); | 44 int proc_fd = open("/proc/self/exe", O_RDONLY); |
47 if (proc_fd >= 0) { | 45 if (proc_fd >= 0) { |
48 PCHECK(0 == IGNORE_EINTR(close(proc_fd))); | 46 PCHECK(0 == IGNORE_EINTR(close(proc_fd))); |
(...skipping 10 matching lines...) Expand all Loading... |
59 } | 57 } |
60 | 58 |
61 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) { | 59 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) { |
62 PLOG(ERROR) << "Failed to set non-dumpable flag"; | 60 PLOG(ERROR) << "Failed to set non-dumpable flag"; |
63 return false; | 61 return false; |
64 } | 62 } |
65 | 63 |
66 return prctl(PR_GET_DUMPABLE) == 0; | 64 return prctl(PR_GET_DUMPABLE) == 0; |
67 } | 65 } |
68 | 66 |
69 #if !defined(OS_NACL_NONSFI) | 67 // TODO(hidehiko): Move this to |
70 // Currently Layer-two sandbox is not yet supported on nacl_helper_nonsfi. | 68 // native_client/src/public/linux_syscalls/sys/resource.h |
71 // This function is used only in InitializeLayerTwoSandbox(). | 69 #if !defined(RLIMIT_DATA) |
72 // TODO(hidehiko): Enable the sandbox. | 70 #define RLIMIT_DATA 2 |
| 71 #endif |
| 72 |
73 void RestrictAddressSpaceUsage() { | 73 void RestrictAddressSpaceUsage() { |
74 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ | 74 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ |
75 defined(THREAD_SANITIZER) | 75 defined(THREAD_SANITIZER) |
76 // Sanitizers need to reserve huge chunks of the address space. | 76 // Sanitizers need to reserve huge chunks of the address space. |
77 return; | 77 return; |
78 #endif | 78 #endif |
79 | 79 |
80 // Add a limit to the brk() heap that would prevent allocations that can't be | 80 // Add a limit to the brk() heap that would prevent allocations that can't be |
81 // indexed by an int. This helps working around typical security bugs. | 81 // indexed by an int. This helps working around typical security bugs. |
82 // This could almost certainly be set to zero. GLibc's allocator and others | 82 // This could almost certainly be set to zero. GLibc's allocator and others |
(...skipping 10 matching lines...) Expand all Loading... |
93 // | 93 // |
94 // Set the limit to 128 GB and have some margin. | 94 // Set the limit to 128 GB and have some margin. |
95 const rlim_t kNewAddressSpaceLimit = 1UL << 37; | 95 const rlim_t kNewAddressSpaceLimit = 1UL << 37; |
96 #else | 96 #else |
97 // Some architectures such as X86 allow 32 bits processes to switch to 64 | 97 // Some architectures such as X86 allow 32 bits processes to switch to 64 |
98 // bits when running under 64 bits kernels. Set a limit in case this happens. | 98 // bits when running under 64 bits kernels. Set a limit in case this happens. |
99 const rlim_t kNewAddressSpaceLimit = std::numeric_limits<uint32_t>::max(); | 99 const rlim_t kNewAddressSpaceLimit = std::numeric_limits<uint32_t>::max(); |
100 #endif | 100 #endif |
101 CHECK(sandbox::ResourceLimits::Lower(RLIMIT_AS, kNewAddressSpaceLimit)); | 101 CHECK(sandbox::ResourceLimits::Lower(RLIMIT_AS, kNewAddressSpaceLimit)); |
102 } | 102 } |
103 #endif // !OS_NACL_NONSFI | |
104 | 103 |
105 } // namespace | 104 } // namespace |
106 | 105 |
107 NaClSandbox::NaClSandbox() | 106 NaClSandbox::NaClSandbox() |
108 : layer_one_enabled_(false), | 107 : layer_one_enabled_(false), |
109 layer_one_sealed_(false), | 108 layer_one_sealed_(false), |
110 layer_two_enabled_(false), | 109 layer_two_enabled_(false), |
111 layer_two_is_nonsfi_(false), | 110 layer_two_is_nonsfi_(false), |
112 proc_fd_(-1), | 111 proc_fd_(-1), |
113 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) { | 112 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 // This relies on SealLayerOneSandbox() to be called later since this | 153 // This relies on SealLayerOneSandbox() to be called later since this |
155 // class is keeping a file descriptor to /proc/. | 154 // class is keeping a file descriptor to /proc/. |
156 CHECK(sandbox::Credentials::DropFileSystemAccess(proc_fd_.get())); | 155 CHECK(sandbox::Credentials::DropFileSystemAccess(proc_fd_.get())); |
157 CHECK(sandbox::Credentials::DropAllCapabilities(proc_fd_.get())); | 156 CHECK(sandbox::Credentials::DropAllCapabilities(proc_fd_.get())); |
158 CHECK(IsSandboxed()); | 157 CHECK(IsSandboxed()); |
159 layer_one_enabled_ = true; | 158 layer_one_enabled_ = true; |
160 } | 159 } |
161 #endif // !OS_NACL_NONSFI | 160 #endif // !OS_NACL_NONSFI |
162 } | 161 } |
163 | 162 |
164 #if !defined(OS_NACL_NONSFI) | |
165 // Currently Layer-two sandbox is not yet supported on nacl_helper_nonsfi. | |
166 // TODO(hidehiko): Enable the sandbox. | |
167 // Note that CheckForExpectedNumberOfOpenFds() is just referred from | |
168 // InitializeLayerTwoSandbox(). Enable them together. | |
169 void NaClSandbox::CheckForExpectedNumberOfOpenFds() { | 163 void NaClSandbox::CheckForExpectedNumberOfOpenFds() { |
170 // We expect to have the following FDs open: | 164 // We expect to have the following FDs open: |
171 // 1-3) stdin, stdout, stderr. | 165 // 1-3) stdin, stdout, stderr. |
172 // 4) The /dev/urandom FD used by base::GetUrandomFD(). | 166 // 4) The /dev/urandom FD used by base::GetUrandomFD(). |
173 // 5) A dummy pipe FD used to overwrite kSandboxIPCChannel. | 167 // 5) A dummy pipe FD used to overwrite kSandboxIPCChannel. |
174 // 6) The socket for the Chrome IPC channel that's connected to the | 168 // 6) The socket for the Chrome IPC channel that's connected to the |
175 // browser process, kPrimaryIPCChannel. | 169 // browser process, kPrimaryIPCChannel. |
176 // We also have an fd for /proc (proc_fd_), but CountOpenFds excludes this. | 170 // We also have an fd for /proc (proc_fd_), but CountOpenFds excludes this. |
177 // | 171 // |
178 // This sanity check ensures that dynamically loaded libraries don't | 172 // This sanity check ensures that dynamically loaded libraries don't |
(...skipping 10 matching lines...) Expand all Loading... |
189 | 183 |
190 void NaClSandbox::InitializeLayerTwoSandbox(bool uses_nonsfi_mode) { | 184 void NaClSandbox::InitializeLayerTwoSandbox(bool uses_nonsfi_mode) { |
191 // seccomp-bpf only applies to the current thread, so it's critical to only | 185 // seccomp-bpf only applies to the current thread, so it's critical to only |
192 // have a single thread running here. | 186 // have a single thread running here. |
193 DCHECK(!layer_one_sealed_); | 187 DCHECK(!layer_one_sealed_); |
194 CHECK(IsSingleThreaded()); | 188 CHECK(IsSingleThreaded()); |
195 CheckForExpectedNumberOfOpenFds(); | 189 CheckForExpectedNumberOfOpenFds(); |
196 | 190 |
197 RestrictAddressSpaceUsage(); | 191 RestrictAddressSpaceUsage(); |
198 | 192 |
| 193 #if defined(OS_NACL_NONSFI) |
| 194 layer_two_enabled_ = nacl::nonsfi::InitializeBPFSandbox(proc_fd_.Pass()); |
| 195 layer_two_is_nonsfi_ = true; |
| 196 #else |
199 // Pass proc_fd_ ownership to the BPF sandbox, which guarantees it will | 197 // Pass proc_fd_ ownership to the BPF sandbox, which guarantees it will |
200 // be closed. There is no point in keeping it around since the BPF policy | 198 // be closed. There is no point in keeping it around since the BPF policy |
201 // will prevent its usage. | 199 // will prevent its usage. |
202 if (uses_nonsfi_mode) { | 200 if (uses_nonsfi_mode) { |
203 layer_two_enabled_ = nacl::nonsfi::InitializeBPFSandbox(proc_fd_.Pass()); | 201 layer_two_enabled_ = nacl::nonsfi::InitializeBPFSandbox(proc_fd_.Pass()); |
204 layer_two_is_nonsfi_ = true; | 202 layer_two_is_nonsfi_ = true; |
205 } else { | 203 } else { |
206 layer_two_enabled_ = nacl::InitializeBPFSandbox(proc_fd_.Pass()); | 204 layer_two_enabled_ = nacl::InitializeBPFSandbox(proc_fd_.Pass()); |
207 } | 205 } |
| 206 #endif |
208 } | 207 } |
209 #endif // OS_NACL_NONSFI | |
210 | 208 |
211 void NaClSandbox::SealLayerOneSandbox() { | 209 void NaClSandbox::SealLayerOneSandbox() { |
212 if (proc_fd_.is_valid() && !layer_two_enabled_) { | 210 if (proc_fd_.is_valid() && !layer_two_enabled_) { |
213 // If nothing prevents us, check that there is no superfluous directory | 211 // If nothing prevents us, check that there is no superfluous directory |
214 // open. | 212 // open. |
215 CHECK(!HasOpenDirectory()); | 213 CHECK(!HasOpenDirectory()); |
216 } | 214 } |
217 proc_fd_.reset(); | 215 proc_fd_.reset(); |
218 layer_one_sealed_ = true; | 216 layer_one_sealed_ = true; |
219 } | 217 } |
(...skipping 11 matching lines...) Expand all Loading... |
231 | 229 |
232 if (!layer_one_enabled_ || !layer_one_sealed_) { | 230 if (!layer_one_enabled_ || !layer_one_sealed_) { |
233 static const char kNoSuidMsg[] = | 231 static const char kNoSuidMsg[] = |
234 "The SUID sandbox is not engaged for NaCl:"; | 232 "The SUID sandbox is not engaged for NaCl:"; |
235 if (can_be_no_sandbox) | 233 if (can_be_no_sandbox) |
236 LOG(ERROR) << kNoSuidMsg << kItIsDangerousMsg; | 234 LOG(ERROR) << kNoSuidMsg << kItIsDangerousMsg; |
237 else | 235 else |
238 LOG(FATAL) << kNoSuidMsg << kItIsNotAllowedMsg; | 236 LOG(FATAL) << kNoSuidMsg << kItIsNotAllowedMsg; |
239 } | 237 } |
240 | 238 |
241 #if !defined(OS_NACL_NONSFI) | |
242 // Currently Layer-two sandbox is not yet supported on nacl_helper_nonsfi. | |
243 // TODO(hidehiko): Enable the sandbox. | |
244 if (!layer_two_enabled_) { | 239 if (!layer_two_enabled_) { |
245 static const char kNoBpfMsg[] = | 240 static const char kNoBpfMsg[] = |
246 "The seccomp-bpf sandbox is not engaged for NaCl:"; | 241 "The seccomp-bpf sandbox is not engaged for NaCl:"; |
247 if (can_be_no_sandbox) | 242 if (can_be_no_sandbox) |
248 LOG(ERROR) << kNoBpfMsg << kItIsDangerousMsg; | 243 LOG(ERROR) << kNoBpfMsg << kItIsDangerousMsg; |
249 else | 244 else |
250 LOG(FATAL) << kNoBpfMsg << kItIsNotAllowedMsg; | 245 LOG(FATAL) << kNoBpfMsg << kItIsNotAllowedMsg; |
251 } | 246 } |
252 #endif | |
253 } | 247 } |
254 | 248 |
255 } // namespace nacl | 249 } // namespace nacl |
OLD | NEW |