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 // A mini-zygote specifically for Native Client. | 5 // A mini-zygote specifically for Native Client. |
6 | 6 |
7 #include "chrome/common/nacl_helper_linux.h" | 7 #include "chrome/common/nacl_helper_linux.h" |
8 | 8 |
9 #include <errno.h> | 9 #include <errno.h> |
10 #include <fcntl.h> | 10 #include <fcntl.h> |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "ipc/ipc_switches.h" | 33 #include "ipc/ipc_switches.h" |
34 #include "sandbox/linux/services/libc_urandom_override.h" | 34 #include "sandbox/linux/services/libc_urandom_override.h" |
35 | 35 |
36 namespace { | 36 namespace { |
37 | 37 |
38 // The child must mimic the behavior of zygote_main_linux.cc on the child | 38 // The child must mimic the behavior of zygote_main_linux.cc on the child |
39 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from | 39 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from |
40 // if (!child) { | 40 // if (!child) { |
41 // Note: this code doesn't attempt to support SELINUX or the SECCOMP sandbox. | 41 // Note: this code doesn't attempt to support SELINUX or the SECCOMP sandbox. |
42 void BecomeNaClLoader(const std::vector<int>& child_fds, | 42 void BecomeNaClLoader(const std::vector<int>& child_fds, |
43 size_t prereserved_sandbox_size) { | 43 size_t prereserved_sandbox_size, |
| 44 int number_of_cores) { |
44 VLOG(1) << "NaCl loader: setting up IPC descriptor"; | 45 VLOG(1) << "NaCl loader: setting up IPC descriptor"; |
45 // don't need zygote FD any more | 46 // don't need zygote FD any more |
46 if (HANDLE_EINTR(close(kNaClZygoteDescriptor)) != 0) | 47 if (HANDLE_EINTR(close(kNaClZygoteDescriptor)) != 0) |
47 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; | 48 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; |
48 base::GlobalDescriptors::GetInstance()->Set(kPrimaryIPCChannel, | 49 base::GlobalDescriptors::GetInstance()->Set(kPrimaryIPCChannel, |
49 child_fds[kNaClBrowserFDIndex]); | 50 child_fds[kNaClBrowserFDIndex]); |
50 | 51 |
51 MessageLoopForIO main_message_loop; | 52 MessageLoopForIO main_message_loop; |
52 NaClListener listener; | 53 NaClListener listener; |
53 listener.set_prereserved_sandbox_size(prereserved_sandbox_size); | 54 listener.set_prereserved_sandbox_size(prereserved_sandbox_size); |
| 55 listener.set_number_of_cores(number_of_cores); |
54 listener.Listen(); | 56 listener.Listen(); |
55 _exit(0); | 57 _exit(0); |
56 } | 58 } |
57 | 59 |
58 // Some of this code was lifted from | 60 // Some of this code was lifted from |
59 // content/browser/zygote_main_linux.cc:ForkWithRealPid() | 61 // content/browser/zygote_main_linux.cc:ForkWithRealPid() |
60 void HandleForkRequest(const std::vector<int>& child_fds, | 62 void HandleForkRequest(const std::vector<int>& child_fds, |
61 size_t prereserved_sandbox_size) { | 63 size_t prereserved_sandbox_size, |
| 64 int number_of_cores) { |
62 VLOG(1) << "nacl_helper: forking"; | 65 VLOG(1) << "nacl_helper: forking"; |
63 pid_t childpid = fork(); | 66 pid_t childpid = fork(); |
64 if (childpid < 0) { | 67 if (childpid < 0) { |
65 perror("fork"); | 68 perror("fork"); |
66 LOG(ERROR) << "*** HandleForkRequest failed\n"; | 69 LOG(ERROR) << "*** HandleForkRequest failed\n"; |
67 // fall through to parent case below | 70 // fall through to parent case below |
68 } else if (childpid == 0) { // In the child process. | 71 } else if (childpid == 0) { // In the child process. |
69 bool validack = false; | 72 bool validack = false; |
70 const size_t kMaxReadSize = 1024; | 73 const size_t kMaxReadSize = 1024; |
71 char buffer[kMaxReadSize]; | 74 char buffer[kMaxReadSize]; |
(...skipping 17 matching lines...) Expand all Loading... |
89 switches::kProcessChannelID, | 92 switches::kProcessChannelID, |
90 std::string(&buffer[len], nread - len)); | 93 std::string(&buffer[len], nread - len)); |
91 validack = true; | 94 validack = true; |
92 } | 95 } |
93 } | 96 } |
94 if (HANDLE_EINTR(close(child_fds[kNaClDummyFDIndex])) != 0) | 97 if (HANDLE_EINTR(close(child_fds[kNaClDummyFDIndex])) != 0) |
95 LOG(ERROR) << "close(child_fds[kNaClDummyFDIndex]) failed"; | 98 LOG(ERROR) << "close(child_fds[kNaClDummyFDIndex]) failed"; |
96 if (HANDLE_EINTR(close(child_fds[kNaClParentFDIndex])) != 0) | 99 if (HANDLE_EINTR(close(child_fds[kNaClParentFDIndex])) != 0) |
97 LOG(ERROR) << "close(child_fds[kNaClParentFDIndex]) failed"; | 100 LOG(ERROR) << "close(child_fds[kNaClParentFDIndex]) failed"; |
98 if (validack) { | 101 if (validack) { |
99 BecomeNaClLoader(child_fds, prereserved_sandbox_size); | 102 BecomeNaClLoader(child_fds, prereserved_sandbox_size, number_of_cores); |
100 } else { | 103 } else { |
101 LOG(ERROR) << "Failed to synch with zygote"; | 104 LOG(ERROR) << "Failed to synch with zygote"; |
102 } | 105 } |
103 // NOTREACHED | 106 // NOTREACHED |
104 return; | 107 return; |
105 } | 108 } |
106 // I am the parent. | 109 // I am the parent. |
107 // First, close the dummy_fd so the sandbox won't find me when | 110 // First, close the dummy_fd so the sandbox won't find me when |
108 // looking for the child's pid in /proc. Also close other fds. | 111 // looking for the child's pid in /proc. Also close other fds. |
109 for (size_t i = 0; i < child_fds.size(); i++) { | 112 for (size_t i = 0; i < child_fds.size(); i++) { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 // Without this line on Linux, HMAC::Init will instantiate a singleton that | 232 // Without this line on Linux, HMAC::Init will instantiate a singleton that |
230 // in turn attempts to open a file. Disabling this behavior avoids a ~70 ms | 233 // in turn attempts to open a file. Disabling this behavior avoids a ~70 ms |
231 // stall the first time HMAC is used. | 234 // stall the first time HMAC is used. |
232 crypto::ForceNSSNoDBInit(); | 235 crypto::ForceNSSNoDBInit(); |
233 // Load shared libraries before sandbox is raised. | 236 // Load shared libraries before sandbox is raised. |
234 // NSS is needed to perform hashing for validation caching. | 237 // NSS is needed to perform hashing for validation caching. |
235 crypto::LoadNSSLibraries(); | 238 crypto::LoadNSSLibraries(); |
236 #endif | 239 #endif |
237 std::vector<int> empty; // for SendMsg() calls | 240 std::vector<int> empty; // for SendMsg() calls |
238 size_t prereserved_sandbox_size = CheckReservedAtZero(); | 241 size_t prereserved_sandbox_size = CheckReservedAtZero(); |
| 242 int number_of_cores = sysconf(_SC_NPROCESSORS_ONLN); |
239 | 243 |
240 CheckRDebug(argv[0]); | 244 CheckRDebug(argv[0]); |
241 | 245 |
242 // Check that IsSandboxed() works. We should not be sandboxed at this point. | 246 // Check that IsSandboxed() works. We should not be sandboxed at this point. |
243 CHECK(!IsSandboxed()) << "Unexpectedly sandboxed!"; | 247 CHECK(!IsSandboxed()) << "Unexpectedly sandboxed!"; |
244 | 248 |
245 // Send the zygote a message to let it know we are ready to help | 249 // Send the zygote a message to let it know we are ready to help |
246 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, | 250 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, |
247 kNaClHelperStartupAck, | 251 kNaClHelperStartupAck, |
248 sizeof(kNaClHelperStartupAck), empty)) { | 252 sizeof(kNaClHelperStartupAck), empty)) { |
(...skipping 16 matching lines...) Expand all Loading... |
265 } | 269 } |
266 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) { | 270 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) { |
267 // EOF from the browser. Goodbye! | 271 // EOF from the browser. Goodbye! |
268 _exit(0); | 272 _exit(0); |
269 } else if (msglen < 0) { | 273 } else if (msglen < 0) { |
270 LOG(ERROR) << "nacl_helper: receive from zygote failed, errno = " | 274 LOG(ERROR) << "nacl_helper: receive from zygote failed, errno = " |
271 << errno; | 275 << errno; |
272 } else if (msglen == sizeof(kNaClForkRequest) - 1 && | 276 } else if (msglen == sizeof(kNaClForkRequest) - 1 && |
273 memcmp(buf, kNaClForkRequest, msglen) == 0) { | 277 memcmp(buf, kNaClForkRequest, msglen) == 0) { |
274 if (kNaClParentFDIndex + 1 == fds.size()) { | 278 if (kNaClParentFDIndex + 1 == fds.size()) { |
275 HandleForkRequest(fds, prereserved_sandbox_size); | 279 HandleForkRequest(fds, prereserved_sandbox_size, number_of_cores); |
276 continue; // fork succeeded. Note: child does not return | 280 continue; // fork succeeded. Note: child does not return |
277 } else { | 281 } else { |
278 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " | 282 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " |
279 << fds.size(); | 283 << fds.size(); |
280 } | 284 } |
281 } else { | 285 } else { |
282 LOG(ERROR) << "nacl_helper unrecognized request: " | 286 LOG(ERROR) << "nacl_helper unrecognized request: " |
283 << base::GetDoubleQuotedJson(std::string(buf, buf + msglen)); | 287 << base::GetDoubleQuotedJson(std::string(buf, buf + msglen)); |
284 _exit(-1); | 288 _exit(-1); |
285 } | 289 } |
286 // if fork fails, send PID=-1 to zygote | 290 // if fork fails, send PID=-1 to zygote |
287 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid, | 291 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid, |
288 sizeof(badpid), empty)) { | 292 sizeof(badpid), empty)) { |
289 LOG(ERROR) << "*** send() to zygote failed"; | 293 LOG(ERROR) << "*** send() to zygote failed"; |
290 } | 294 } |
291 } | 295 } |
292 CHECK(false); // This routine must not return | 296 CHECK(false); // This routine must not return |
293 } | 297 } |
OLD | NEW |