| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // A mini-zygote specifically for Native Client. | |
| 6 | |
| 7 #include "chrome/common/nacl_helper_linux.h" | |
| 8 | |
| 9 #include <errno.h> | |
| 10 #include <stdlib.h> | |
| 11 #include <sys/socket.h> | |
| 12 #include <sys/types.h> | |
| 13 | |
| 14 #include <string> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include "base/at_exit.h" | |
| 18 #include "base/eintr_wrapper.h" | |
| 19 #include "base/logging.h" | |
| 20 #include "base/message_loop.h" | |
| 21 #include "base/rand_util.h" | |
| 22 #include "chrome/nacl/nacl_listener.h" | |
| 23 #include "content/common/main_function_params.h" | |
| 24 #include "content/common/unix_domain_socket_posix.h" | |
| 25 #include "ipc/ipc_switches.h" | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 bool g_suid_sandbox_active; | |
| 30 | |
| 31 // The child must mimic the behavior of zygote_main_linux.cc on the child | |
| 32 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from | |
| 33 // if (!child) { | |
| 34 // Note: this code doesn't attempt to support SELINUX or the SECCOMP sandbox. | |
| 35 void BecomeNaClLoader(const std::vector<int>& child_fds) { | |
| 36 VLOG(1) << "NaCl loader: setting up IPC descriptor"; | |
| 37 if (HANDLE_EINTR(close(kNaClZygoteDescriptor)) < 0) | |
| 38 LOG(ERROR) << "close failed: " << errno; | |
| 39 | |
| 40 // Set up browser descriptor as expected by Chrome on fd 3 | |
| 41 // The zygote takes care of putting the sandbox IPC channel on fd 5 | |
| 42 int zfd = dup2(child_fds[kNaClBrowserFDIndex], kNaClBrowserDescriptor); | |
| 43 if (zfd != kNaClBrowserDescriptor) { | |
| 44 LOG(ERROR) << "Could not initialize kNaClBrowserDescriptor"; | |
| 45 _exit(-1); | |
| 46 } | |
| 47 | |
| 48 MessageLoopForIO main_message_loop; | |
| 49 NaClListener *listener = new NaClListener(); | |
| 50 listener->Listen(); | |
| 51 _exit(0); | |
| 52 } | |
| 53 | |
| 54 // Some of this code was lifted from | |
| 55 // content/browser/zygote_main_linux.cc:ForkWithRealPid() | |
| 56 void HandleForkRequest(const std::vector<int>& child_fds) { | |
| 57 VLOG(1) << "nacl_helper: forking"; | |
| 58 pid_t childpid = fork(); | |
| 59 if (childpid < 0) { | |
| 60 perror("fork"); | |
| 61 LOG(ERROR) << "*** HandleForkRequest failed\n"; | |
| 62 // fall through to parent case below | |
| 63 } else if (childpid == 0) { // In the child process. | |
| 64 bool validack = false; | |
| 65 const size_t kMaxReadSize = 1024; | |
| 66 char buffer[kMaxReadSize]; | |
| 67 // Wait until the parent process has discovered our PID. We | |
| 68 // should not fork any child processes (which the seccomp | |
| 69 // sandbox does) until then, because that can interfere with the | |
| 70 // parent's discovery of our PID. | |
| 71 const int nread = HANDLE_EINTR(read(child_fds[kNaClParentFDIndex], buffer, | |
| 72 kMaxReadSize)); | |
| 73 const std::string switch_prefix = std::string("--") + | |
| 74 switches::kProcessChannelID + std::string("="); | |
| 75 const size_t len = switch_prefix.length(); | |
| 76 | |
| 77 if (nread < 0) { | |
| 78 perror("read"); | |
| 79 LOG(ERROR) << "read returned " << nread; | |
| 80 } else if (nread > static_cast<int>(len)) { | |
| 81 if (switch_prefix.compare(0, len, buffer, 0, len) == 0) { | |
| 82 VLOG(1) << "NaCl loader is synchronised with Chrome zygote"; | |
| 83 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 84 switches::kProcessChannelID, | |
| 85 std::string(&buffer[len], nread - len)); | |
| 86 validack = true; | |
| 87 } | |
| 88 } | |
| 89 if (HANDLE_EINTR(close(child_fds[kNaClDummyFDIndex]))) | |
| 90 LOG(ERROR) << "close failed: " << errno; | |
| 91 if (HANDLE_EINTR(close(child_fds[kNaClParentFDIndex]))) | |
| 92 LOG(ERROR) << "close failed: " << errno; | |
| 93 | |
| 94 if (validack) { | |
| 95 BecomeNaClLoader(child_fds); | |
| 96 } else { | |
| 97 LOG(ERROR) << "Failed to synch with zygote"; | |
| 98 } | |
| 99 // NOTREACHED | |
| 100 return; | |
| 101 } | |
| 102 // I am the parent. | |
| 103 // First, close the dummy_fd so the sandbox won't find me when | |
| 104 // looking for the child's pid in /proc. Also close other fds. | |
| 105 for (size_t i = 0; i < child_fds.size(); i++) { | |
| 106 if (HANDLE_EINTR(close(child_fds[i]))) | |
| 107 LOG(ERROR) << "close failed: " << errno; | |
| 108 } | |
| 109 VLOG(1) << "nacl_helper: childpid is " << childpid; | |
| 110 // Now tell childpid to the Chrome zygote. | |
| 111 if (HANDLE_EINTR(send(kNaClZygoteDescriptor, | |
| 112 &childpid, sizeof(childpid), MSG_EOR)) | |
| 113 != sizeof(childpid)) { | |
| 114 LOG(ERROR) << "*** send() to zygote failed"; | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 } // namespace | |
| 119 | |
| 120 int main(int argc, char *argv[]) { | |
| 121 CommandLine::Init(argc, argv); | |
| 122 base::AtExitManager exit_manager; | |
| 123 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised | |
| 124 std::vector<int> empty; // for SendMsg() calls | |
| 125 | |
| 126 g_suid_sandbox_active = (NULL != getenv("SBX_D")); | |
| 127 | |
| 128 // Send the zygote a message to let it know we are ready to help | |
| 129 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, | |
| 130 kNaClHelperStartupAck, | |
| 131 sizeof(kNaClHelperStartupAck), empty)) { | |
| 132 LOG(ERROR) << "*** send() to zygote failed"; | |
| 133 } | |
| 134 | |
| 135 while (true) { | |
| 136 int badpid = -1; | |
| 137 std::vector<int> fds; | |
| 138 static const unsigned kMaxMessageLength = 2048; | |
| 139 char buf[kMaxMessageLength]; | |
| 140 const ssize_t msglen = UnixDomainSocket::RecvMsg(kNaClZygoteDescriptor, | |
| 141 &buf, sizeof(buf), &fds); | |
| 142 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) { | |
| 143 // EOF from the browser. Goodbye! | |
| 144 _exit(0); | |
| 145 } | |
| 146 if (msglen == sizeof(kNaClForkRequest) - 1 && | |
| 147 memcmp(buf, kNaClForkRequest, msglen) == 0) { | |
| 148 if (kNaClParentFDIndex + 1 == fds.size()) { | |
| 149 HandleForkRequest(fds); | |
| 150 continue; // fork succeeded. Note: child does not return | |
| 151 } else { | |
| 152 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " | |
| 153 << fds.size(); | |
| 154 } | |
| 155 } else { | |
| 156 if (msglen != 0) { | |
| 157 LOG(ERROR) << "nacl_helper unrecognized request: %s"; | |
| 158 _exit(-1); | |
| 159 } | |
| 160 } | |
| 161 // if fork fails, send PID=-1 to zygote | |
| 162 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid, | |
| 163 sizeof(badpid), empty)) { | |
| 164 LOG(ERROR) << "*** send() to zygote failed"; | |
| 165 } | |
| 166 } | |
| 167 } | |
| OLD | NEW |