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> |
11 #include <sys/types.h> | 11 #include <sys/types.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
14 #include <limits> | |
15 | |
14 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
15 #include "base/callback.h" | 17 #include "base/callback.h" |
16 #include "base/command_line.h" | 18 #include "base/command_line.h" |
17 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
18 #include "base/files/scoped_file.h" | 20 #include "base/files/scoped_file.h" |
19 #include "base/logging.h" | 21 #include "base/logging.h" |
20 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
21 #include "base/posix/eintr_wrapper.h" | 23 #include "base/posix/eintr_wrapper.h" |
22 #include "build/build_config.h" | 24 #include "build/build_config.h" |
23 #include "components/nacl/common/nacl_switches.h" | 25 #include "components/nacl/common/nacl_switches.h" |
24 #include "components/nacl/loader/nonsfi/nonsfi_sandbox.h" | 26 #include "components/nacl/loader/nonsfi/nonsfi_sandbox.h" |
25 #include "components/nacl/loader/sandbox_linux/nacl_bpf_sandbox_linux.h" | 27 #include "components/nacl/loader/sandbox_linux/nacl_bpf_sandbox_linux.h" |
26 #include "content/public/common/content_switches.h" | 28 #include "content/public/common/content_switches.h" |
27 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 29 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
28 #include "sandbox/linux/services/credentials.h" | 30 #include "sandbox/linux/services/credentials.h" |
29 #include "sandbox/linux/services/namespace_sandbox.h" | 31 #include "sandbox/linux/services/namespace_sandbox.h" |
30 #include "sandbox/linux/services/proc_util.h" | 32 #include "sandbox/linux/services/proc_util.h" |
33 #include "sandbox/linux/services/resource_limits.h" | |
31 #include "sandbox/linux/services/thread_helpers.h" | 34 #include "sandbox/linux/services/thread_helpers.h" |
32 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" | 35 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" |
33 | 36 |
34 namespace nacl { | 37 namespace nacl { |
35 | 38 |
36 namespace { | 39 namespace { |
37 | 40 |
38 // This is a poor man's check on whether we are sandboxed. | 41 // This is a poor man's check on whether we are sandboxed. |
39 bool IsSandboxed() { | 42 bool IsSandboxed() { |
40 int proc_fd = open("/proc/self/exe", O_RDONLY); | 43 int proc_fd = open("/proc/self/exe", O_RDONLY); |
(...skipping 21 matching lines...) Expand all Loading... | |
62 } | 65 } |
63 | 66 |
64 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) { | 67 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) { |
65 PLOG(ERROR) << "Failed to set non-dumpable flag"; | 68 PLOG(ERROR) << "Failed to set non-dumpable flag"; |
66 return false; | 69 return false; |
67 } | 70 } |
68 | 71 |
69 return prctl(PR_GET_DUMPABLE) == 0; | 72 return prctl(PR_GET_DUMPABLE) == 0; |
70 } | 73 } |
71 | 74 |
75 void RestrictAddressSpaceUsage() { | |
76 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ | |
77 defined(THREAD_SANITIZER) | |
78 // Sanitizers need to reserve huge chunks of the address space. | |
79 return; | |
80 #endif | |
81 | |
82 // Add a limit to the brk() heap that would prevent allocations that can't be | |
Mark Seaborn
2015/02/13 17:11:54
This probably has no benefit, because glibc will f
jln (very slow on Chromium)
2015/02/13 18:00:05
Does NaCl always uses glibc malloc and not tcmallo
| |
83 // indexed by an int. This helps working around typical security bugs. | |
84 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max(); | |
85 CHECK(sandbox::ResourceLimits::Lower(RLIMIT_DATA, kNewDataSegmentMaxSize)); | |
86 | |
87 #if defined(ARCH_CPU_64_BITS) | |
88 // 128 GB. | |
Mark Seaborn
2015/02/13 17:11:54
Can you add some explanation of how we picked this
jln (very slow on Chromium)
2015/02/13 18:00:05
Done.
| |
89 const rlim_t kNewAddressSpaceLimit = 1UL << 37; | |
90 #else | |
91 const rlim_t kNewAddressSpaceLimit = std::numeric_limits<uint32_t>::max(); | |
Mark Seaborn
2015/02/13 17:11:54
Well, this is no limit at all. :-) You might as w
jln (very slow on Chromium)
2015/02/13 18:00:05
This is true because we're enabling this as part o
Mark Seaborn
2015/02/13 18:33:41
Ah, good point. I didn't think of that.
| |
92 #endif | |
93 CHECK(sandbox::ResourceLimits::Lower(RLIMIT_AS, kNewAddressSpaceLimit)); | |
94 } | |
95 | |
72 } // namespace | 96 } // namespace |
73 | 97 |
74 NaClSandbox::NaClSandbox() | 98 NaClSandbox::NaClSandbox() |
75 : layer_one_enabled_(false), | 99 : layer_one_enabled_(false), |
76 layer_one_sealed_(false), | 100 layer_one_sealed_(false), |
77 layer_two_enabled_(false), | 101 layer_two_enabled_(false), |
78 layer_two_is_nonsfi_(false), | 102 layer_two_is_nonsfi_(false), |
79 proc_fd_(-1), | 103 proc_fd_(-1), |
80 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) { | 104 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) { |
81 proc_fd_.reset( | 105 proc_fd_.reset( |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 CHECK_EQ(expected_num_fds, sandbox::ProcUtil::CountOpenFds(proc_fd_.get())); | 170 CHECK_EQ(expected_num_fds, sandbox::ProcUtil::CountOpenFds(proc_fd_.get())); |
147 } | 171 } |
148 | 172 |
149 void NaClSandbox::InitializeLayerTwoSandbox(bool uses_nonsfi_mode) { | 173 void NaClSandbox::InitializeLayerTwoSandbox(bool uses_nonsfi_mode) { |
150 // seccomp-bpf only applies to the current thread, so it's critical to only | 174 // seccomp-bpf only applies to the current thread, so it's critical to only |
151 // have a single thread running here. | 175 // have a single thread running here. |
152 DCHECK(!layer_one_sealed_); | 176 DCHECK(!layer_one_sealed_); |
153 CHECK(IsSingleThreaded()); | 177 CHECK(IsSingleThreaded()); |
154 CheckForExpectedNumberOfOpenFds(); | 178 CheckForExpectedNumberOfOpenFds(); |
155 | 179 |
180 RestrictAddressSpaceUsage(); | |
181 | |
156 base::ScopedFD proc_self_task(GetProcSelfTask(proc_fd_.get())); | 182 base::ScopedFD proc_self_task(GetProcSelfTask(proc_fd_.get())); |
157 | 183 |
158 if (uses_nonsfi_mode) { | 184 if (uses_nonsfi_mode) { |
159 layer_two_enabled_ = | 185 layer_two_enabled_ = |
160 nacl::nonsfi::InitializeBPFSandbox(proc_self_task.Pass()); | 186 nacl::nonsfi::InitializeBPFSandbox(proc_self_task.Pass()); |
161 layer_two_is_nonsfi_ = true; | 187 layer_two_is_nonsfi_ = true; |
162 } else { | 188 } else { |
163 layer_two_enabled_ = nacl::InitializeBPFSandbox(proc_self_task.Pass()); | 189 layer_two_enabled_ = nacl::InitializeBPFSandbox(proc_self_task.Pass()); |
164 } | 190 } |
165 } | 191 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 static const char kNoBpfMsg[] = | 224 static const char kNoBpfMsg[] = |
199 "The seccomp-bpf sandbox is not engaged for NaCl:"; | 225 "The seccomp-bpf sandbox is not engaged for NaCl:"; |
200 if (can_be_no_sandbox) | 226 if (can_be_no_sandbox) |
201 LOG(ERROR) << kNoBpfMsg << kItIsDangerousMsg; | 227 LOG(ERROR) << kNoBpfMsg << kItIsDangerousMsg; |
202 else | 228 else |
203 LOG(FATAL) << kNoBpfMsg << kItIsNotAllowedMsg; | 229 LOG(FATAL) << kNoBpfMsg << kItIsNotAllowedMsg; |
204 } | 230 } |
205 } | 231 } |
206 | 232 |
207 } // namespace nacl | 233 } // namespace nacl |
OLD | NEW |