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 HANDLE_EINTR(close(kNaClZygoteDescriptor)); // don't need zygote FD any more |
| 38 // Set up browser descriptor as expected by Chrome on fd 3 |
| 39 // The zygote takes care of putting the sandbox IPC channel on fd 5 |
| 40 int zfd = dup2(child_fds[kNaClBrowserFDIndex], kNaClBrowserDescriptor); |
| 41 if (zfd != kNaClBrowserDescriptor) { |
| 42 LOG(ERROR) << "Could not initialize kNaClBrowserDescriptor"; |
| 43 _exit(-1); |
| 44 } |
| 45 |
| 46 MessageLoopForIO main_message_loop; |
| 47 NaClListener *listener = new NaClListener(); |
| 48 listener->Listen(); |
| 49 _exit(0); |
| 50 } |
| 51 |
| 52 // Some of this code was lifted from |
| 53 // content/browser/zygote_main_linux.cc:ForkWithRealPid() |
| 54 void HandleForkRequest(const std::vector<int>& child_fds) { |
| 55 VLOG(1) << "nacl_helper: forking"; |
| 56 pid_t childpid = fork(); |
| 57 if (childpid < 0) { |
| 58 perror("fork"); |
| 59 LOG(ERROR) << "*** HandleForkRequest failed\n"; |
| 60 // fall through to parent case below |
| 61 } else if (childpid == 0) { // In the child process. |
| 62 bool validack = false; |
| 63 const size_t kMaxReadSize = 1024; |
| 64 char buffer[kMaxReadSize]; |
| 65 // Wait until the parent process has discovered our PID. We |
| 66 // should not fork any child processes (which the seccomp |
| 67 // sandbox does) until then, because that can interfere with the |
| 68 // parent's discovery of our PID. |
| 69 const int nread = HANDLE_EINTR(read(child_fds[kNaClParentFDIndex], buffer, |
| 70 kMaxReadSize)); |
| 71 const std::string switch_prefix = std::string("--") + |
| 72 switches::kProcessChannelID + std::string("="); |
| 73 const size_t len = switch_prefix.length(); |
| 74 |
| 75 if (nread < 0) { |
| 76 perror("read"); |
| 77 LOG(ERROR) << "read returned " << nread; |
| 78 } else if (nread > static_cast<int>(len)) { |
| 79 if (switch_prefix.compare(0, len, buffer, 0, len) == 0) { |
| 80 VLOG(1) << "NaCl loader is synchronised with Chrome zygote"; |
| 81 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 82 switches::kProcessChannelID, |
| 83 std::string(&buffer[len], nread - len)); |
| 84 validack = true; |
| 85 } |
| 86 } |
| 87 HANDLE_EINTR(close(child_fds[kNaClDummyFDIndex])); |
| 88 HANDLE_EINTR(close(child_fds[kNaClParentFDIndex])); |
| 89 if (validack) { |
| 90 BecomeNaClLoader(child_fds); |
| 91 } else { |
| 92 LOG(ERROR) << "Failed to synch with zygote"; |
| 93 } |
| 94 // NOTREACHED |
| 95 return; |
| 96 } |
| 97 // I am the parent. |
| 98 // First, close the dummy_fd so the sandbox won't find me when |
| 99 // looking for the child's pid in /proc. Also close other fds. |
| 100 for (size_t i = 0; i < child_fds.size(); i++) { |
| 101 HANDLE_EINTR(close(child_fds[i])); |
| 102 } |
| 103 VLOG(1) << "nacl_helper: childpid is " << childpid; |
| 104 // Now tell childpid to the Chrome zygote. |
| 105 if (HANDLE_EINTR(send(kNaClZygoteDescriptor, |
| 106 &childpid, sizeof(childpid), MSG_EOR)) |
| 107 != sizeof(childpid)) { |
| 108 LOG(ERROR) << "*** send() to zygote failed"; |
| 109 } |
| 110 } |
| 111 |
| 112 } // namespace |
| 113 |
| 114 int main(int argc, char *argv[]) { |
| 115 CommandLine::Init(argc, argv); |
| 116 base::AtExitManager exit_manager; |
| 117 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised |
| 118 std::vector<int> empty; // for SendMsg() calls |
| 119 |
| 120 g_suid_sandbox_active = (NULL != getenv("SBX_D")); |
| 121 |
| 122 // Send the zygote a message to let it know we are ready to help |
| 123 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, |
| 124 kNaClHelperStartupAck, |
| 125 sizeof(kNaClHelperStartupAck), empty)) { |
| 126 LOG(ERROR) << "*** send() to zygote failed"; |
| 127 } |
| 128 |
| 129 while (true) { |
| 130 int badpid = -1; |
| 131 std::vector<int> fds; |
| 132 static const unsigned kMaxMessageLength = 2048; |
| 133 char buf[kMaxMessageLength]; |
| 134 const ssize_t msglen = UnixDomainSocket::RecvMsg(kNaClZygoteDescriptor, |
| 135 &buf, sizeof(buf), &fds); |
| 136 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) { |
| 137 // EOF from the browser. Goodbye! |
| 138 _exit(0); |
| 139 } |
| 140 if (msglen == sizeof(kNaClForkRequest) - 1 && |
| 141 memcmp(buf, kNaClForkRequest, msglen) == 0) { |
| 142 if (kNaClParentFDIndex + 1 == fds.size()) { |
| 143 HandleForkRequest(fds); |
| 144 continue; // fork succeeded. Note: child does not return |
| 145 } else { |
| 146 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " |
| 147 << fds.size(); |
| 148 } |
| 149 } else { |
| 150 if (msglen != 0) { |
| 151 LOG(ERROR) << "nacl_helper unrecognized request: %s"; |
| 152 _exit(-1); |
| 153 } |
| 154 } |
| 155 // if fork fails, send PID=-1 to zygote |
| 156 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid, |
| 157 sizeof(badpid), empty)) { |
| 158 LOG(ERROR) << "*** send() to zygote failed"; |
| 159 } |
| 160 } |
| 161 } |
OLD | NEW |