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 "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 close(kNaClZygoteDescriptor); // don't need this any more | |
|
agl
2011/06/27 18:59:32
HANDLE_EINTR()
Brad Chen
2011/06/27 22:06:25
Done.
| |
| 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 int nread = HANDLE_EINTR(read(child_fds[kNaClParentFDIndex], buffer, | |
| 70 kMaxReadSize)); | |
| 71 std::string swtch("--"); | |
|
agl
2011/06/27 18:59:32
s/swtch/switch/
Brad Chen
2011/06/27 22:06:25
Can't use switch because it is a keyword. I will u
| |
| 72 swtch += switches::kProcessChannelID; | |
|
agl
2011/06/27 18:59:32
s/ / /
Brad Chen
2011/06/27 22:06:25
Done.
| |
| 73 swtch += "="; | |
| 74 const size_t len = swtch.length(); | |
| 75 | |
| 76 if (nread < 0) { | |
| 77 perror("read"); | |
| 78 LOG(ERROR) << "read returned " << nread; | |
| 79 } else if (nread > static_cast<int>(len)) { | |
| 80 buffer[nread] = '\0'; | |
|
agl
2011/06/27 18:59:32
BUG: nread may be equal to sizeof(buffer)
Brad Chen
2011/06/27 22:06:25
Done. Code no longer relies on null-termination of
| |
| 81 if (swtch.compare(0, len, buffer, 0, len) == 0) { | |
| 82 fprintf(stderr, "nacl_helper: ack %s\n", buffer); | |
| 83 VLOG(1) << "NaCl loader is synchronised with Chrome zygote"; | |
| 84 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 85 switches::kProcessChannelID, | |
| 86 std::string(&buffer[len])); | |
| 87 validack = true; | |
| 88 } | |
| 89 } | |
| 90 close(child_fds[kNaClDummyFDIndex]); | |
|
agl
2011/06/27 18:59:32
HANDLE_EINTR (and the next line)
Brad Chen
2011/06/27 22:06:25
Done.
| |
| 91 close(child_fds[kNaClParentFDIndex]); | |
| 92 if (validack) { | |
| 93 BecomeNaClLoader(child_fds); | |
| 94 } else { | |
| 95 LOG(ERROR) << "Failed to synch with zygote"; | |
| 96 } | |
| 97 // NOTREACHED | |
| 98 return; | |
| 99 } | |
| 100 // I am the parent. | |
| 101 // First, close the dummy_fd so the sandbox won't find me when | |
| 102 // looking for the child's pid in /proc. Also close other fds. | |
| 103 for (size_t i = 0; i < child_fds.size(); i++) { | |
| 104 close(child_fds[i]); | |
|
agl
2011/06/27 18:59:32
HANDLE_EINTR
Brad Chen
2011/06/27 22:06:25
Done.
| |
| 105 } | |
| 106 VLOG(1) << "nacl_helper: childpid is " << childpid; | |
| 107 // Now tell childpid to the Chrome zygote. | |
| 108 if (HANDLE_EINTR(send(kNaClZygoteDescriptor, | |
| 109 &childpid, sizeof(childpid), MSG_EOR)) | |
| 110 != sizeof(childpid)) { | |
| 111 LOG(ERROR) << "*** send() to zygote failed"; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 } // namespace | |
| 116 | |
| 117 int main(int argc, char *argv[]) { | |
| 118 CommandLine::Init(argc, argv); | |
| 119 base::AtExitManager exit_manager; | |
| 120 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised | |
| 121 std::vector<int> empty; // for SendMsg() calls | |
| 122 | |
| 123 g_suid_sandbox_active = (NULL != getenv("SBX_D")); | |
| 124 | |
| 125 // Send the zygote a message to let it know we are ready to help | |
| 126 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, | |
| 127 kNaClHelperStartupAck, | |
| 128 sizeof(kNaClHelperStartupAck), empty)) { | |
| 129 LOG(ERROR) << "*** send() to zygote failed"; | |
| 130 } | |
| 131 | |
| 132 while (true) { | |
| 133 int badpid = -1; | |
| 134 std::vector<int> fds; | |
| 135 static const unsigned kMaxMessageLength = 2048; | |
| 136 char buf[kMaxMessageLength]; | |
| 137 const ssize_t msglen = UnixDomainSocket::RecvMsg(kNaClZygoteDescriptor, | |
| 138 &buf, sizeof(buf), &fds); | |
| 139 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) { | |
| 140 // EOF from the browser. Goodbye! | |
| 141 _exit(0); | |
| 142 } | |
| 143 if (msglen == sizeof(kNaClForkRequest) - 1 && | |
| 144 memcmp(buf, kNaClForkRequest, msglen) == 0) { | |
| 145 if (kNaClParentFDIndex + 1 == fds.size()) { | |
| 146 HandleForkRequest(fds); | |
| 147 continue; // fork succeeded. Note: child does not return | |
| 148 } else { | |
| 149 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " | |
| 150 << fds.size(); | |
| 151 } | |
| 152 } else { | |
| 153 if (msglen != 0) { | |
| 154 LOG(ERROR) << "nacl_helper unrecognized request: %s"; | |
| 155 _exit(-1); | |
| 156 } | |
| 157 } | |
| 158 // if fork fails, send PID=-1 to zygote | |
| 159 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid, | |
| 160 sizeof(badpid), empty)) { | |
| 161 LOG(ERROR) << "*** send() to zygote failed"; | |
| 162 } | |
| 163 } | |
| 164 } | |
| OLD | NEW |