Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1440)

Unified Diff: chrome/nacl/nacl_helper_linux.cc

Issue 6995121: New NaCl zygote implementation 2 (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Addressing feedback from John Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/nacl/nacl_helper_linux.cc
diff --git a/chrome/nacl/nacl_helper_linux.cc b/chrome/nacl/nacl_helper_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fd86ede17e2040b87d65e51feeb36aa22b977a27
--- /dev/null
+++ b/chrome/nacl/nacl_helper_linux.cc
@@ -0,0 +1,148 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// A mini-zygote specifically for Native Client.
+
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+#include <vector>
+
+#include "base/at_exit.h"
+#include "base/command_line.h"
+#include "base/eintr_wrapper.h"
+#include "base/logging.h"
+#include "base/message_loop.h"
+#include "base/rand_util.h"
+#include "ipc/ipc_channel.h"
+#include "chrome/common/nacl_helper_linux.h"
+#include "chrome/nacl/nacl_listener.h"
+#include "content/common/child_process_info.h"
+#include "content/common/main_function_params.h"
+#include "content/common/unix_domain_socket_posix.h"
+
+static bool g_suid_sandbox_active;
+
+// The child must mimic the behavior of zygote_main_linux.cc on the child
+// side of the fork. See zygote_main_linux.cc:HandleForkRequest from
+// if (!child) {
+// Note: this code doesn't attempt to support SELINUX or the SECCOMP sandbox.
+static void BecomeNaClLoader(const std::vector<int>& child_fds) {
+ VLOG(1) << "NaCl loader: setting up IPC descriptor";
+ close(kNaClZygoteDescriptor); // don't need this any more
+ // Set up browser descriptor as expected by Chrome on fd 3
+ // The zygote takes care of putting the sandbox IPC channel on fd 5
+ int zfd = dup2(child_fds[kNaClBrowserFDIndex], kNaClBrowserDescriptor);
+ if (zfd != kNaClBrowserDescriptor) {
+ LOG(ERROR) << "Could not initialize kNaClBrowserDescriptor";
+ _exit(-1);
+ }
+
+ MessageLoopForIO main_message_loop;
+ NaClListener listener;
+ 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
+ IPC::Channel::MODE_CLIENT, &listener);
+ CHECK(channel.Connect());
+ MessageLoop::current()->Run();
+ _exit(0);
+}
+
+// Some of this code was lifted from
+// content/browser/zygote_main_linux.cc:ForkWithRealPid()
+static void HandleForkRequest(const std::vector<int>& child_fds) {
+ VLOG(1) << "nacl_helper: forking";
+ pid_t childpid = fork();
+ if (childpid < 0) {
+ perror("fork");
+ LOG(ERROR) << "*** HandleForkRequest failed\n";
+ // fall through to parent case below
+ } else if (childpid == 0) { // In the child process.
+ if (g_suid_sandbox_active) {
+ char buffer[1];
+ // Wait until the parent process has discovered our PID. We
+ // should not fork any child processes (which the seccomp
+ // sandbox does) until then, because that can interfere with the
+ // parent's discovery of our PID.
+ int nread = HANDLE_EINTR(read(child_fds[kNaClParentFDIndex], buffer, 1));
+ if (nread != 1 || buffer[0] != 'x') {
+ if (nread < 0) {
+ perror("read");
+ }
+ LOG(ERROR) << "Failed to synch with zygote: read " << nread;
+ } else {
+ VLOG(1) << "NaCl loader is synchronised with Chrome zygote";
+ }
+ }
+ close(child_fds[kNaClDummyFDIndex]);
+ close(child_fds[kNaClParentFDIndex]);
+ BecomeNaClLoader(child_fds);
+ // NOTREACHED
+ return;
+ }
+ // I am the parent.
+ // First, close the dummy_fd so the sandbox won't find me when
+ // looking for the child's pid in /proc. Also close other fds.
+ for (size_t i = 0; i < child_fds.size(); i++) {
+ close(child_fds[i]);
+ }
+ VLOG(1) << "nacl_helper: childpid is " << childpid;
+ // Now tell childpid to the Chrome zygote.
+ if (HANDLE_EINTR(send(kNaClZygoteDescriptor,
+ &childpid, sizeof(childpid), MSG_EOR))
+ != sizeof(childpid)) {
+ LOG(ERROR) << "*** send() to zygote failed";
+ }
+}
+
+int main(int argc, char *argv[]) {
+ CommandLine::Init(argc, argv);
+ base::AtExitManager exit_manager;
+ base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised
+ std::vector<int> empty; // for SendMsg() calls
+
+ g_suid_sandbox_active = (NULL != getenv("SBX_D"));
+
+ // Send the zygote a message to let it know we are ready to help
+ if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor,
+ kNaClHelperStartupAck,
+ sizeof(kNaClHelperStartupAck), empty)) {
+ LOG(ERROR) << "*** send() to zygote failed";
+ }
+
+ while (true) {
+ int badpid = -1;
+ std::vector<int> fds;
+ static const unsigned kMaxMessageLength = 2048;
+ char buf[kMaxMessageLength];
+ const ssize_t msglen = UnixDomainSocket::RecvMsg(kNaClZygoteDescriptor,
+ &buf, sizeof(buf), &fds);
+ if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) {
+ // EOF from the browser. Goodbye!
+ _exit(0);
+ }
+ if (msglen == sizeof(kNaClForkRequest) - 1 &&
+ memcmp(buf, kNaClForkRequest, msglen) == 0) {
+ if (kNaClBrowserFDIndex + 1 == fds.size() ||
+ kNaClParentFDIndex + 1 == fds.size()) {
+ HandleForkRequest(fds);
+ continue; // fork succeeded. Note: child does not return
+ } else {
+ LOG(ERROR) << "nacl_helper: unexpected number of fds, got "
+ << fds.size();
+ }
+ } else {
+ if (msglen != 0) {
+ LOG(ERROR) << "nacl_helper unrecognized request: %s";
+ _exit(-1);
+ }
+ }
+ // if fork fails, send PID=-1 to zygote
+ if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid,
+ sizeof(badpid), empty)) {
+ LOG(ERROR) << "*** send() to zygote failed";
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698