Chromium Code Reviews| 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 <errno.h> | |
| 8 #include <stdlib.h> | |
| 9 #include <sys/socket.h> | |
| 10 #include <sys/types.h> | |
| 11 | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/at_exit.h" | |
| 15 #include "base/command_line.h" | |
| 16 #include "base/eintr_wrapper.h" | |
| 17 #include "base/logging.h" | |
| 18 #include "base/message_loop.h" | |
| 19 #include "base/rand_util.h" | |
| 20 #include "ipc/ipc_channel.h" | |
| 21 #include "chrome/common/nacl_helper_linux.h" | |
| 22 #include "chrome/nacl/nacl_listener.h" | |
| 23 #include "content/common/child_process_info.h" | |
| 24 #include "content/common/main_function_params.h" | |
| 25 #include "content/common/unix_domain_socket_posix.h" | |
| 26 | |
| 27 static bool g_suid_sandbox_active; | |
| 28 | |
| 29 // The child must mimic the behavior of zygote_main_linux.cc on the child | |
| 30 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from | |
| 31 // if (!child) { | |
| 32 // Note: this code doesn't attempt to support SELINUX or the SECCOMP sandbox. | |
| 33 static void BecomeNaClLoader(const std::vector<int>& child_fds) { | |
| 34 VLOG(1) << "NaCl loader: setting up IPC descriptor"; | |
| 35 close(kNaClZygoteDescriptor); // don't need this any more | |
| 36 // Set up browser descriptor as expected by Chrome on fd 3 | |
| 37 // The zygote takes care of putting the sandbox IPC channel on fd 5 | |
| 38 int zfd = dup2(child_fds[kNaClBrowserFDIndex], kNaClBrowserDescriptor); | |
| 39 if (zfd != kNaClBrowserDescriptor) { | |
| 40 LOG(ERROR) << "Could not initialize kNaClBrowserDescriptor"; | |
| 41 _exit(-1); | |
| 42 } | |
| 43 | |
| 44 MessageLoopForIO main_message_loop; | |
| 45 NaClListener listener; | |
| 46 IPC::Channel channel(ChildProcessInfo::GenerateRandomChannelID(&listener), | |
|
jam
2011/06/22 00:49:52
I don't understand how the IPC works if the launch
Brad Chen
2011/06/22 16:10:31
I was quite surprised when things worked with a ra
| |
| 47 IPC::Channel::MODE_CLIENT, &listener); | |
| 48 CHECK(channel.Connect()); | |
| 49 MessageLoop::current()->Run(); | |
| 50 _exit(0); | |
| 51 } | |
| 52 | |
| 53 // Some of this code was lifted from | |
| 54 // content/browser/zygote_main_linux.cc:ForkWithRealPid() | |
| 55 static void HandleForkRequest(const std::vector<int>& child_fds) { | |
| 56 VLOG(1) << "nacl_helper: forking"; | |
| 57 pid_t childpid = fork(); | |
| 58 if (childpid < 0) { | |
| 59 perror("fork"); | |
| 60 LOG(ERROR) << "*** HandleForkRequest failed\n"; | |
| 61 // fall through to parent case below | |
| 62 } else if (childpid == 0) { // In the child process. | |
| 63 if (g_suid_sandbox_active) { | |
| 64 char buffer[1]; | |
| 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 int nread = HANDLE_EINTR(read(child_fds[kNaClParentFDIndex], buffer, 1)); | |
| 70 if (nread != 1 || buffer[0] != 'x') { | |
| 71 if (nread < 0) { | |
| 72 perror("read"); | |
| 73 } | |
| 74 LOG(ERROR) << "Failed to synch with zygote: read " << nread; | |
| 75 } else { | |
| 76 VLOG(1) << "NaCl loader is synchronised with Chrome zygote"; | |
| 77 } | |
| 78 } | |
| 79 close(child_fds[kNaClDummyFDIndex]); | |
| 80 close(child_fds[kNaClParentFDIndex]); | |
| 81 BecomeNaClLoader(child_fds); | |
| 82 // NOTREACHED | |
| 83 return; | |
| 84 } | |
| 85 // I am the parent. | |
| 86 // First, close the dummy_fd so the sandbox won't find me when | |
| 87 // looking for the child's pid in /proc. Also close other fds. | |
| 88 for (size_t i = 0; i < child_fds.size(); i++) { | |
| 89 close(child_fds[i]); | |
| 90 } | |
| 91 VLOG(1) << "nacl_helper: childpid is " << childpid; | |
| 92 // Now tell childpid to the Chrome zygote. | |
| 93 if (HANDLE_EINTR(send(kNaClZygoteDescriptor, | |
| 94 &childpid, sizeof(childpid), MSG_EOR)) | |
| 95 != sizeof(childpid)) { | |
| 96 LOG(ERROR) << "*** send() to zygote failed"; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 int main(int argc, char *argv[]) { | |
| 101 CommandLine::Init(argc, argv); | |
| 102 base::AtExitManager exit_manager; | |
| 103 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised | |
| 104 std::vector<int> empty; // for SendMsg() calls | |
| 105 | |
| 106 g_suid_sandbox_active = (NULL != getenv("SBX_D")); | |
| 107 | |
| 108 // Send the zygote a message to let it know we are ready to help | |
| 109 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, | |
| 110 kNaClHelperStartupAck, | |
| 111 sizeof(kNaClHelperStartupAck), empty)) { | |
| 112 LOG(ERROR) << "*** send() to zygote failed"; | |
| 113 } | |
| 114 | |
| 115 while (true) { | |
| 116 int badpid = -1; | |
| 117 std::vector<int> fds; | |
| 118 static const unsigned kMaxMessageLength = 2048; | |
| 119 char buf[kMaxMessageLength]; | |
| 120 const ssize_t msglen = UnixDomainSocket::RecvMsg(kNaClZygoteDescriptor, | |
| 121 &buf, sizeof(buf), &fds); | |
| 122 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) { | |
| 123 // EOF from the browser. Goodbye! | |
| 124 _exit(0); | |
| 125 } | |
| 126 if (msglen == sizeof(kNaClForkRequest) - 1 && | |
| 127 memcmp(buf, kNaClForkRequest, msglen) == 0) { | |
| 128 if (kNaClBrowserFDIndex + 1 == fds.size() || | |
| 129 kNaClParentFDIndex + 1 == fds.size()) { | |
| 130 HandleForkRequest(fds); | |
| 131 continue; // fork succeeded. Note: child does not return | |
| 132 } else { | |
| 133 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " | |
| 134 << fds.size(); | |
| 135 } | |
| 136 } else { | |
| 137 if (msglen != 0) { | |
| 138 LOG(ERROR) << "nacl_helper unrecognized request: %s"; | |
| 139 _exit(-1); | |
| 140 } | |
| 141 } | |
| 142 // if fork fails, send PID=-1 to zygote | |
| 143 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid, | |
| 144 sizeof(badpid), empty)) { | |
| 145 LOG(ERROR) << "*** send() to zygote failed"; | |
| 146 } | |
| 147 } | |
| 148 } | |
| OLD | NEW |