| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 #include "chrome/app/nacl_fork_delegate_linux.h" | |
| 6 | |
| 7 #include <signal.h> | |
| 8 #include <stdlib.h> | |
| 9 #include <sys/resource.h> | |
| 10 #include <sys/socket.h> | |
| 11 | |
| 12 #include <set> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/command_line.h" | |
| 16 #include "base/files/file_path.h" | |
| 17 #include "base/logging.h" | |
| 18 #include "base/path_service.h" | |
| 19 #include "base/posix/eintr_wrapper.h" | |
| 20 #include "base/posix/unix_domain_socket_linux.h" | |
| 21 #include "base/process_util.h" | |
| 22 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
| 23 #include "chrome/common/chrome_paths.h" | |
| 24 #include "chrome/common/chrome_switches.h" | |
| 25 #include "chrome/common/nacl_helper_linux.h" | |
| 26 | |
| 27 NaClForkDelegate::NaClForkDelegate() | |
| 28 : status_(kNaClHelperUnused), | |
| 29 fd_(-1) {} | |
| 30 | |
| 31 // Note these need to match up with their counterparts in nacl_helper_linux.c | |
| 32 // and nacl_helper_bootstrap_linux.c. | |
| 33 const char kNaClHelperReservedAtZero[] = | |
| 34 "--reserved_at_zero=0xXXXXXXXXXXXXXXXX"; | |
| 35 const char kNaClHelperRDebug[] = "--r_debug=0xXXXXXXXXXXXXXXXX"; | |
| 36 | |
| 37 void NaClForkDelegate::Init(const int sandboxdesc) { | |
| 38 VLOG(1) << "NaClForkDelegate::Init()"; | |
| 39 int fds[2]; | |
| 40 | |
| 41 // Confirm a hard-wired assumption. | |
| 42 // The NaCl constant is from chrome/nacl/nacl_linux_helper.h | |
| 43 DCHECK(kNaClSandboxDescriptor == sandboxdesc); | |
| 44 | |
| 45 CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | |
| 46 base::FileHandleMappingVector fds_to_map; | |
| 47 fds_to_map.push_back(std::make_pair(fds[1], kNaClZygoteDescriptor)); | |
| 48 fds_to_map.push_back(std::make_pair(sandboxdesc, kNaClSandboxDescriptor)); | |
| 49 | |
| 50 status_ = kNaClHelperUnused; | |
| 51 base::FilePath helper_exe; | |
| 52 base::FilePath helper_bootstrap_exe; | |
| 53 if (!PathService::Get(chrome::FILE_NACL_HELPER, &helper_exe)) { | |
| 54 status_ = kNaClHelperMissing; | |
| 55 } else if (!PathService::Get(chrome::FILE_NACL_HELPER_BOOTSTRAP, | |
| 56 &helper_bootstrap_exe)) { | |
| 57 status_ = kNaClHelperBootstrapMissing; | |
| 58 } else if (RunningOnValgrind()) { | |
| 59 status_ = kNaClHelperValgrind; | |
| 60 } else { | |
| 61 CommandLine cmd_line(helper_bootstrap_exe); | |
| 62 cmd_line.AppendArgPath(helper_exe); | |
| 63 cmd_line.AppendArgNative(kNaClHelperReservedAtZero); | |
| 64 cmd_line.AppendArgNative(kNaClHelperRDebug); | |
| 65 base::LaunchOptions options; | |
| 66 options.fds_to_remap = &fds_to_map; | |
| 67 options.clone_flags = CLONE_FS | SIGCHLD; | |
| 68 | |
| 69 // The NaCl processes spawned may need to exceed the ambient soft limit | |
| 70 // on RLIMIT_AS to allocate the untrusted address space and its guard | |
| 71 // regions. The nacl_helper itself cannot just raise its own limit, | |
| 72 // because the existing limit may prevent the initial exec of | |
| 73 // nacl_helper_bootstrap from succeeding, with its large address space | |
| 74 // reservation. | |
| 75 std::set<int> max_these_limits; | |
| 76 max_these_limits.insert(RLIMIT_AS); | |
| 77 options.maximize_rlimits = &max_these_limits; | |
| 78 | |
| 79 if (!base::LaunchProcess(cmd_line.argv(), options, NULL)) | |
| 80 status_ = kNaClHelperLaunchFailed; | |
| 81 // parent and error cases are handled below | |
| 82 } | |
| 83 if (HANDLE_EINTR(close(fds[1])) != 0) | |
| 84 LOG(ERROR) << "close(fds[1]) failed"; | |
| 85 if (status_ == kNaClHelperUnused) { | |
| 86 const ssize_t kExpectedLength = strlen(kNaClHelperStartupAck); | |
| 87 char buf[kExpectedLength]; | |
| 88 | |
| 89 // Wait for ack from nacl_helper, indicating it is ready to help | |
| 90 const ssize_t nread = HANDLE_EINTR(read(fds[0], buf, sizeof(buf))); | |
| 91 if (nread == kExpectedLength && | |
| 92 memcmp(buf, kNaClHelperStartupAck, nread) == 0) { | |
| 93 // all is well | |
| 94 status_ = kNaClHelperSuccess; | |
| 95 fd_ = fds[0]; | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 status_ = kNaClHelperAckFailed; | |
| 100 LOG(ERROR) << "Bad NaCl helper startup ack (" << nread << " bytes)"; | |
| 101 } | |
| 102 // TODO(bradchen): Make this LOG(ERROR) when the NaCl helper | |
| 103 // becomes the default. | |
| 104 fd_ = -1; | |
| 105 if (HANDLE_EINTR(close(fds[0])) != 0) | |
| 106 LOG(ERROR) << "close(fds[0]) failed"; | |
| 107 } | |
| 108 | |
| 109 void NaClForkDelegate::InitialUMA(std::string* uma_name, | |
| 110 int* uma_sample, | |
| 111 int* uma_boundary_value) { | |
| 112 *uma_name = "NaCl.Client.Helper.InitState"; | |
| 113 *uma_sample = status_; | |
| 114 *uma_boundary_value = kNaClHelperStatusBoundary; | |
| 115 } | |
| 116 | |
| 117 NaClForkDelegate::~NaClForkDelegate() { | |
| 118 // side effect of close: delegate process will terminate | |
| 119 if (status_ == kNaClHelperSuccess) { | |
| 120 if (HANDLE_EINTR(close(fd_)) != 0) | |
| 121 LOG(ERROR) << "close(fd_) failed"; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 bool NaClForkDelegate::CanHelp(const std::string& process_type, | |
| 126 std::string* uma_name, | |
| 127 int* uma_sample, | |
| 128 int* uma_boundary_value) { | |
| 129 if (process_type != switches::kNaClLoaderProcess) | |
| 130 return false; | |
| 131 *uma_name = "NaCl.Client.Helper.StateOnFork"; | |
| 132 *uma_sample = status_; | |
| 133 *uma_boundary_value = kNaClHelperStatusBoundary; | |
| 134 return status_ == kNaClHelperSuccess; | |
| 135 } | |
| 136 | |
| 137 pid_t NaClForkDelegate::Fork(const std::vector<int>& fds) { | |
| 138 base::ProcessId naclchild; | |
| 139 VLOG(1) << "NaClForkDelegate::Fork"; | |
| 140 | |
| 141 DCHECK(fds.size() == kNaClParentFDIndex + 1); | |
| 142 if (!UnixDomainSocket::SendMsg(fd_, kNaClForkRequest, | |
| 143 strlen(kNaClForkRequest), fds)) { | |
| 144 LOG(ERROR) << "NaClForkDelegate::Fork: SendMsg failed"; | |
| 145 return -1; | |
| 146 } | |
| 147 int nread = HANDLE_EINTR(read(fd_, &naclchild, sizeof(naclchild))); | |
| 148 if (nread != sizeof(naclchild)) { | |
| 149 LOG(ERROR) << "NaClForkDelegate::Fork: read failed"; | |
| 150 return -1; | |
| 151 } | |
| 152 VLOG(1) << "nacl_child is " << naclchild << " (" << nread << " bytes)"; | |
| 153 return naclchild; | |
| 154 } | |
| 155 | |
| 156 bool NaClForkDelegate::AckChild(const int fd, | |
| 157 const std::string& channel_switch) { | |
| 158 int nwritten = HANDLE_EINTR(write(fd, channel_switch.c_str(), | |
| 159 channel_switch.length())); | |
| 160 if (nwritten != static_cast<int>(channel_switch.length())) { | |
| 161 return false; | |
| 162 } | |
| 163 return true; | |
| 164 } | |
| OLD | NEW |