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" | |
34 #include "sandbox/linux/services/thread_helpers.h" | 33 #include "sandbox/linux/services/thread_helpers.h" |
35 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" | 34 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" |
36 | 35 |
36 #if !defined(OS_NACL_NONSFI) | |
37 #include "sandbox/linux/services/resource_limits.h" | |
38 #endif | |
39 | |
37 namespace nacl { | 40 namespace nacl { |
38 | 41 |
39 namespace { | 42 namespace { |
40 | 43 |
41 // This is a poor man's check on whether we are sandboxed. | 44 // This is a poor man's check on whether we are sandboxed. |
42 bool IsSandboxed() { | 45 bool IsSandboxed() { |
43 int proc_fd = open("/proc/self/exe", O_RDONLY); | 46 int proc_fd = open("/proc/self/exe", O_RDONLY); |
44 if (proc_fd >= 0) { | 47 if (proc_fd >= 0) { |
45 PCHECK(0 == IGNORE_EINTR(close(proc_fd))); | 48 PCHECK(0 == IGNORE_EINTR(close(proc_fd))); |
46 return false; | 49 return false; |
47 } | 50 } |
48 return true; | 51 return true; |
49 } | 52 } |
50 | 53 |
51 bool MaybeSetProcessNonDumpable() { | 54 bool MaybeSetProcessNonDumpable() { |
52 const base::CommandLine& command_line = | 55 const base::CommandLine& command_line = |
53 *base::CommandLine::ForCurrentProcess(); | 56 *base::CommandLine::ForCurrentProcess(); |
54 if (command_line.HasSwitch(switches::kAllowSandboxDebugging)) { | 57 if (command_line.HasSwitch(switches::kAllowSandboxDebugging)) { |
55 return true; | 58 return true; |
56 } | 59 } |
57 | 60 |
58 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) { | 61 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) != 0) { |
59 PLOG(ERROR) << "Failed to set non-dumpable flag"; | 62 PLOG(ERROR) << "Failed to set non-dumpable flag"; |
60 return false; | 63 return false; |
61 } | 64 } |
62 | 65 |
63 return prctl(PR_GET_DUMPABLE) == 0; | 66 return prctl(PR_GET_DUMPABLE) == 0; |
64 } | 67 } |
65 | 68 |
69 #if !defined(OS_NACL_NONSFI) | |
70 // Currently Layer-two sandbox is not yet supported on nacl_helper_nonsfi. | |
71 // This function is used only in InitializeLayerTwoSandbox(). | |
72 // TODO(hidehiko): Enable the sandbox. | |
66 void RestrictAddressSpaceUsage() { | 73 void RestrictAddressSpaceUsage() { |
67 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ | 74 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ |
68 defined(THREAD_SANITIZER) | 75 defined(THREAD_SANITIZER) |
69 // Sanitizers need to reserve huge chunks of the address space. | 76 // Sanitizers need to reserve huge chunks of the address space. |
70 return; | 77 return; |
71 #endif | 78 #endif |
72 | 79 |
73 // 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 |
74 // indexed by an int. This helps working around typical security bugs. | 81 // indexed by an int. This helps working around typical security bugs. |
75 // 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... | |
86 // | 93 // |
87 // Set the limit to 128 GB and have some margin. | 94 // Set the limit to 128 GB and have some margin. |
88 const rlim_t kNewAddressSpaceLimit = 1UL << 37; | 95 const rlim_t kNewAddressSpaceLimit = 1UL << 37; |
89 #else | 96 #else |
90 // 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 |
91 // 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. |
92 const rlim_t kNewAddressSpaceLimit = std::numeric_limits<uint32_t>::max(); | 99 const rlim_t kNewAddressSpaceLimit = std::numeric_limits<uint32_t>::max(); |
93 #endif | 100 #endif |
94 CHECK(sandbox::ResourceLimits::Lower(RLIMIT_AS, kNewAddressSpaceLimit)); | 101 CHECK(sandbox::ResourceLimits::Lower(RLIMIT_AS, kNewAddressSpaceLimit)); |
95 } | 102 } |
103 #endif // !OS_NACL_NONSFI | |
96 | 104 |
97 } // namespace | 105 } // namespace |
98 | 106 |
99 NaClSandbox::NaClSandbox() | 107 NaClSandbox::NaClSandbox() |
100 : layer_one_enabled_(false), | 108 : layer_one_enabled_(false), |
101 layer_one_sealed_(false), | 109 layer_one_sealed_(false), |
102 layer_two_enabled_(false), | 110 layer_two_enabled_(false), |
103 layer_two_is_nonsfi_(false), | 111 layer_two_is_nonsfi_(false), |
104 proc_fd_(-1), | 112 proc_fd_(-1), |
105 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) { | 113 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) { |
(...skipping 24 matching lines...) Expand all Loading... | |
130 | 138 |
131 // Make sure that no directory file descriptor is open, as it would bypass | 139 // Make sure that no directory file descriptor is open, as it would bypass |
132 // the setuid sandbox model. | 140 // the setuid sandbox model. |
133 CHECK(!HasOpenDirectory()); | 141 CHECK(!HasOpenDirectory()); |
134 | 142 |
135 // Get sandboxed. | 143 // Get sandboxed. |
136 CHECK(setuid_sandbox_client_->ChrootMe()); | 144 CHECK(setuid_sandbox_client_->ChrootMe()); |
137 CHECK(MaybeSetProcessNonDumpable()); | 145 CHECK(MaybeSetProcessNonDumpable()); |
138 CHECK(IsSandboxed()); | 146 CHECK(IsSandboxed()); |
139 layer_one_enabled_ = true; | 147 layer_one_enabled_ = true; |
148 #if !defined(OS_NACL_NONSFI) | |
149 // Currently namespace sandbox is not yet supported on nacl_helper_nonsfi. | |
Mark Seaborn
2015/03/04 19:32:50
It's odd putting the comment inside the previous i
hidehiko
2015/03/05 05:26:53
Done.
| |
150 // TODO(hidehiko): Enable the sandbox. | |
140 } else if (sandbox::NamespaceSandbox::InNewUserNamespace()) { | 151 } else if (sandbox::NamespaceSandbox::InNewUserNamespace()) { |
141 CHECK(sandbox::Credentials::MoveToNewUserNS()); | 152 CHECK(sandbox::Credentials::MoveToNewUserNS()); |
142 // This relies on SealLayerOneSandbox() to be called later since this | 153 // This relies on SealLayerOneSandbox() to be called later since this |
143 // class is keeping a file descriptor to /proc/. | 154 // class is keeping a file descriptor to /proc/. |
144 CHECK(sandbox::Credentials::DropFileSystemAccess(proc_fd_.get())); | 155 CHECK(sandbox::Credentials::DropFileSystemAccess(proc_fd_.get())); |
145 CHECK(sandbox::Credentials::DropAllCapabilities(proc_fd_.get())); | 156 CHECK(sandbox::Credentials::DropAllCapabilities(proc_fd_.get())); |
146 CHECK(IsSandboxed()); | 157 CHECK(IsSandboxed()); |
147 layer_one_enabled_ = true; | 158 layer_one_enabled_ = true; |
159 #endif // !OS_NACL_NONSFI | |
148 } | 160 } |
149 } | 161 } |
150 | 162 |
163 #if !defined(OS_NACL_NONSFI) | |
164 // Currently Layer-two sandbox is not yet supported on nacl_helper_nonsfi. | |
165 // TODO(hidehiko): Enable the sandbox. | |
166 // Note that CheckForExpectedNumberOfOpenFds() is just referred from | |
167 // InitializeLayerTwoSandbox(). Enable them at once. | |
Mark Seaborn
2015/03/04 19:32:50
Nit: "Enable them at once" -> "We can enable them
hidehiko
2015/03/05 05:26:53
Great to know. Thanks!
| |
151 void NaClSandbox::CheckForExpectedNumberOfOpenFds() { | 168 void NaClSandbox::CheckForExpectedNumberOfOpenFds() { |
152 // We expect to have the following FDs open: | 169 // We expect to have the following FDs open: |
153 // 1-3) stdin, stdout, stderr. | 170 // 1-3) stdin, stdout, stderr. |
154 // 4) The /dev/urandom FD used by base::GetUrandomFD(). | 171 // 4) The /dev/urandom FD used by base::GetUrandomFD(). |
155 // 5) A dummy pipe FD used to overwrite kSandboxIPCChannel. | 172 // 5) A dummy pipe FD used to overwrite kSandboxIPCChannel. |
156 // 6) The socket for the Chrome IPC channel that's connected to the | 173 // 6) The socket for the Chrome IPC channel that's connected to the |
157 // browser process, kPrimaryIPCChannel. | 174 // browser process, kPrimaryIPCChannel. |
158 // We also have an fd for /proc (proc_fd_), but CountOpenFds excludes this. | 175 // We also have an fd for /proc (proc_fd_), but CountOpenFds excludes this. |
159 // | 176 // |
160 // This sanity check ensures that dynamically loaded libraries don't | 177 // This sanity check ensures that dynamically loaded libraries don't |
(...skipping 20 matching lines...) Expand all Loading... | |
181 // Pass proc_fd_ ownership to the BPF sandbox, which guarantees it will | 198 // Pass proc_fd_ ownership to the BPF sandbox, which guarantees it will |
182 // be closed. There is no point in keeping it around since the BPF policy | 199 // be closed. There is no point in keeping it around since the BPF policy |
183 // will prevent its usage. | 200 // will prevent its usage. |
184 if (uses_nonsfi_mode) { | 201 if (uses_nonsfi_mode) { |
185 layer_two_enabled_ = nacl::nonsfi::InitializeBPFSandbox(proc_fd_.Pass()); | 202 layer_two_enabled_ = nacl::nonsfi::InitializeBPFSandbox(proc_fd_.Pass()); |
186 layer_two_is_nonsfi_ = true; | 203 layer_two_is_nonsfi_ = true; |
187 } else { | 204 } else { |
188 layer_two_enabled_ = nacl::InitializeBPFSandbox(proc_fd_.Pass()); | 205 layer_two_enabled_ = nacl::InitializeBPFSandbox(proc_fd_.Pass()); |
189 } | 206 } |
190 } | 207 } |
208 #endif // OS_NACL_NONSFI | |
191 | 209 |
192 void NaClSandbox::SealLayerOneSandbox() { | 210 void NaClSandbox::SealLayerOneSandbox() { |
193 if (proc_fd_.is_valid() && !layer_two_enabled_) { | 211 if (proc_fd_.is_valid() && !layer_two_enabled_) { |
194 // If nothing prevents us, check that there is no superfluous directory | 212 // If nothing prevents us, check that there is no superfluous directory |
195 // open. | 213 // open. |
196 CHECK(!HasOpenDirectory()); | 214 CHECK(!HasOpenDirectory()); |
197 } | 215 } |
198 proc_fd_.reset(); | 216 proc_fd_.reset(); |
199 layer_one_sealed_ = true; | 217 layer_one_sealed_ = true; |
200 } | 218 } |
(...skipping 11 matching lines...) Expand all Loading... | |
212 | 230 |
213 if (!layer_one_enabled_ || !layer_one_sealed_) { | 231 if (!layer_one_enabled_ || !layer_one_sealed_) { |
214 static const char kNoSuidMsg[] = | 232 static const char kNoSuidMsg[] = |
215 "The SUID sandbox is not engaged for NaCl:"; | 233 "The SUID sandbox is not engaged for NaCl:"; |
216 if (can_be_no_sandbox) | 234 if (can_be_no_sandbox) |
217 LOG(ERROR) << kNoSuidMsg << kItIsDangerousMsg; | 235 LOG(ERROR) << kNoSuidMsg << kItIsDangerousMsg; |
218 else | 236 else |
219 LOG(FATAL) << kNoSuidMsg << kItIsNotAllowedMsg; | 237 LOG(FATAL) << kNoSuidMsg << kItIsNotAllowedMsg; |
220 } | 238 } |
221 | 239 |
240 #if !defined(OS_NACL_NONSFI) | |
241 // Currently Layer-two sandbox is not yet supported on nacl_helper_nonsfi. | |
242 // TODO(hidehiko): Enable the sandbox. | |
222 if (!layer_two_enabled_) { | 243 if (!layer_two_enabled_) { |
223 static const char kNoBpfMsg[] = | 244 static const char kNoBpfMsg[] = |
224 "The seccomp-bpf sandbox is not engaged for NaCl:"; | 245 "The seccomp-bpf sandbox is not engaged for NaCl:"; |
225 if (can_be_no_sandbox) | 246 if (can_be_no_sandbox) |
226 LOG(ERROR) << kNoBpfMsg << kItIsDangerousMsg; | 247 LOG(ERROR) << kNoBpfMsg << kItIsDangerousMsg; |
227 else | 248 else |
228 LOG(FATAL) << kNoBpfMsg << kItIsNotAllowedMsg; | 249 LOG(FATAL) << kNoBpfMsg << kItIsNotAllowedMsg; |
229 } | 250 } |
251 #endif | |
230 } | 252 } |
231 | 253 |
232 } // namespace nacl | 254 } // namespace nacl |
OLD | NEW |