| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // A mini-zygote specifically for Native Client. | 5 // A mini-zygote specifically for Native Client. |
| 6 | 6 |
| 7 #include "chrome/common/nacl_helper_linux.h" | 7 #include "chrome/common/nacl_helper_linux.h" |
| 8 | 8 |
| 9 #include <errno.h> | 9 #include <errno.h> |
| 10 #include <stdlib.h> | 10 #include <stdlib.h> |
| 11 #include <sys/socket.h> | 11 #include <sys/socket.h> |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 | 13 |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/at_exit.h" | 17 #include "base/at_exit.h" |
| 18 #include "base/eintr_wrapper.h" | 18 #include "base/eintr_wrapper.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/message_loop.h" | 20 #include "base/message_loop.h" |
| 21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 22 #include "chrome/nacl/nacl_listener.h" | 22 #include "chrome/nacl/nacl_listener.h" |
| 23 #include "content/common/main_function_params.h" | 23 #include "content/common/main_function_params.h" |
| 24 #include "content/common/unix_domain_socket_posix.h" | 24 #include "content/common/unix_domain_socket_posix.h" |
| 25 #include "ipc/ipc_switches.h" | 25 #include "ipc/ipc_switches.h" |
| 26 #include "native_client/src/trusted/service_runtime/sel_memory.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 bool g_suid_sandbox_active; | 30 bool g_suid_sandbox_active; |
| 30 | 31 |
| 31 // The child must mimic the behavior of zygote_main_linux.cc on the child | 32 // 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 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from |
| 33 // if (!child) { | 34 // if (!child) { |
| 34 // Note: this code doesn't attempt to support SELINUX or the SECCOMP sandbox. | 35 // Note: this code doesn't attempt to support SELINUX or the SECCOMP sandbox. |
| 35 void BecomeNaClLoader(const std::vector<int>& child_fds) { | 36 void BecomeNaClLoader(const std::vector<int>& child_fds) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // Now tell childpid to the Chrome zygote. | 110 // Now tell childpid to the Chrome zygote. |
| 110 if (HANDLE_EINTR(send(kNaClZygoteDescriptor, | 111 if (HANDLE_EINTR(send(kNaClZygoteDescriptor, |
| 111 &childpid, sizeof(childpid), MSG_EOR)) | 112 &childpid, sizeof(childpid), MSG_EOR)) |
| 112 != sizeof(childpid)) { | 113 != sizeof(childpid)) { |
| 113 LOG(ERROR) << "*** send() to zygote failed"; | 114 LOG(ERROR) << "*** send() to zygote failed"; |
| 114 } | 115 } |
| 115 } | 116 } |
| 116 | 117 |
| 117 } // namespace | 118 } // namespace |
| 118 | 119 |
| 119 static const void* g_nacl_reserved_space = NULL; | 120 static const char kNaClHelperAtZero[] = "at-zero"; |
| 120 extern "C" __attribute__((visibility("default"))) | |
| 121 const void* nacl_helper_get_1G_address() { | |
| 122 return g_nacl_reserved_space; | |
| 123 } | |
| 124 | 121 |
| 125 // nacl_helper_init does the real work of this module. It is invoked as | 122 int main(int argc, char *argv[]) { |
| 126 // a static constructor and never returns, preventing main() from the | |
| 127 // nacl_helper_bootstrap program from being called. | |
| 128 // | |
| 129 // NOTE This routine must not return. | |
| 130 extern "C" __attribute__((visibility("default"))) | |
| 131 void nacl_helper_init(int argc, char *argv[], | |
| 132 const char *nacl_reserved_space) { | |
| 133 CommandLine::Init(argc, argv); | 123 CommandLine::Init(argc, argv); |
| 134 base::AtExitManager exit_manager; | 124 base::AtExitManager exit_manager; |
| 135 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised | 125 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised |
| 136 std::vector<int> empty; // for SendMsg() calls | 126 std::vector<int> empty; // for SendMsg() calls |
| 137 | 127 |
| 138 g_suid_sandbox_active = (NULL != getenv("SBX_D")); | 128 g_suid_sandbox_active = (NULL != getenv("SBX_D")); |
| 139 g_nacl_reserved_space = nacl_reserved_space; | 129 |
| 140 if (!nacl_reserved_space) { | 130 if (CommandLine::ForCurrentProcess()->HasSwitch(kNaClHelperAtZero)) { |
| 141 VLOG(1) << "nacl_reserved_space is NULL"; | 131 g_nacl_prereserved_sandbox_addr = (void *) (uintptr_t) 0x10000; |
| 142 } else { | |
| 143 VLOG(1) << "nacl_reserved_space is at " | |
| 144 << (void *)nacl_reserved_space; | |
| 145 } | 132 } |
| 133 |
| 146 // Send the zygote a message to let it know we are ready to help | 134 // Send the zygote a message to let it know we are ready to help |
| 147 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, | 135 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, |
| 148 kNaClHelperStartupAck, | 136 kNaClHelperStartupAck, |
| 149 sizeof(kNaClHelperStartupAck), empty)) { | 137 sizeof(kNaClHelperStartupAck), empty)) { |
| 150 LOG(ERROR) << "*** send() to zygote failed"; | 138 LOG(ERROR) << "*** send() to zygote failed"; |
| 151 } | 139 } |
| 152 | 140 |
| 153 while (true) { | 141 while (true) { |
| 154 int badpid = -1; | 142 int badpid = -1; |
| 155 std::vector<int> fds; | 143 std::vector<int> fds; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 177 } | 165 } |
| 178 } | 166 } |
| 179 // if fork fails, send PID=-1 to zygote | 167 // if fork fails, send PID=-1 to zygote |
| 180 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid, | 168 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, &badpid, |
| 181 sizeof(badpid), empty)) { | 169 sizeof(badpid), empty)) { |
| 182 LOG(ERROR) << "*** send() to zygote failed"; | 170 LOG(ERROR) << "*** send() to zygote failed"; |
| 183 } | 171 } |
| 184 } | 172 } |
| 185 CHECK(false); // This routine must not return | 173 CHECK(false); // This routine must not return |
| 186 } | 174 } |
| OLD | NEW |