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