[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e15a4fa | 2010-02-11 23:09:29 | [diff] [blame] | 5 | #include "chrome/browser/nacl_host/nacl_process_host.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 6 | |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <vector> |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 9 | |
[email protected] | 30c1eea | 2011-10-17 18:40:30 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 12 | #include "base/message_loop.h" |
[email protected] | 9addd1c | 2012-09-15 14:28:24 | [diff] [blame] | 13 | #include "base/metrics/histogram.h" |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 14 | #include "base/path_service.h" |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 15 | #include "base/string_number_conversions.h" |
| 16 | #include "base/string_split.h" |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 17 | #include "base/string_util.h" |
[email protected] | a0a69bf | 2011-09-23 21:40:28 | [diff] [blame] | 18 | #include "base/stringprintf.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 19 | #include "base/utf_string_conversions.h" |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 20 | #include "base/win/windows_version.h" |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 22 | #include "chrome/browser/browser_process.h" |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_info_map.h" |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 24 | #include "chrome/browser/io_thread.h" |
[email protected] | 2e5f2ea4 | 2012-05-09 21:39:21 | [diff] [blame] | 25 | #include "chrome/browser/nacl_host/nacl_browser.h" |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 26 | #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 27 | #include "chrome/common/chrome_constants.h" |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 28 | #include "chrome/common/chrome_paths.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 29 | #include "chrome/common/chrome_switches.h" |
[email protected] | 1657e6d | 2012-03-30 20:28:00 | [diff] [blame] | 30 | #include "chrome/common/chrome_version_info.h" |
[email protected] | 66f409c | 2012-10-04 20:59:04 | [diff] [blame^] | 31 | #include "chrome/common/extensions/url_pattern.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 32 | #include "chrome/common/logging_chrome.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 33 | #include "chrome/common/nacl_cmd_line.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 34 | #include "chrome/common/nacl_messages.h" |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 35 | #include "chrome/common/render_messages.h" |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 36 | #include "chrome/common/url_constants.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 37 | #include "content/public/browser/browser_child_process_host.h" |
| 38 | #include "content/public/browser/child_process_data.h" |
[email protected] | b4b53d18 | 2012-09-12 02:56:55 | [diff] [blame] | 39 | #include "content/public/browser/pepper_helper.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 40 | #include "content/public/common/child_process_host.h" |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 41 | #include "ipc/ipc_channel.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 42 | #include "ipc/ipc_switches.h" |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 43 | #include "native_client/src/shared/imc/nacl_imc.h" |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 44 | #include "net/base/net_util.h" |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 45 | #include "net/base/tcp_listen_socket.h" |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 46 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 47 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 48 | #if defined(OS_POSIX) |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 49 | #include <fcntl.h> |
| 50 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 51 | #include "ipc/ipc_channel_posix.h" |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 52 | #elif defined(OS_WIN) |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 53 | #include <windows.h> |
| 54 | |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 55 | #include "base/threading/thread.h" |
| 56 | #include "base/process_util.h" |
[email protected] | 4c65fb63 | 2012-04-27 00:42:25 | [diff] [blame] | 57 | #include "base/win/scoped_handle.h" |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 58 | #include "chrome/browser/nacl_host/nacl_broker_service_win.h" |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 59 | #include "chrome/common/nacl_debug_exception_handler_win.h" |
[email protected] | e4f6eb023 | 2012-04-17 00:47:50 | [diff] [blame] | 60 | #include "content/public/common/sandbox_init.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 61 | #endif |
| 62 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 63 | using content::BrowserThread; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 64 | using content::ChildProcessData; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 65 | using content::ChildProcessHost; |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 66 | using ppapi::proxy::SerializedHandle; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 67 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 68 | namespace { |
| 69 | |
| 70 | #if defined(OS_WIN) |
| 71 | bool RunningOnWOW64() { |
| 72 | return (base::win::OSInfo::GetInstance()->wow64_status() == |
| 73 | base::win::OSInfo::WOW64_ENABLED); |
| 74 | } |
| 75 | #endif |
| 76 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 77 | void SetCloseOnExec(nacl::Handle fd) { |
| 78 | #if defined(OS_POSIX) |
| 79 | int flags = fcntl(fd, F_GETFD); |
| 80 | CHECK_NE(flags, -1); |
| 81 | int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 82 | CHECK_EQ(rc, 0); |
| 83 | #endif |
| 84 | } |
| 85 | |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 86 | bool ShareHandleToSelLdr( |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 87 | base::ProcessHandle processh, |
| 88 | nacl::Handle sourceh, |
| 89 | bool close_source, |
| 90 | std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) { |
| 91 | #if defined(OS_WIN) |
| 92 | HANDLE channel; |
| 93 | int flags = DUPLICATE_SAME_ACCESS; |
| 94 | if (close_source) |
| 95 | flags |= DUPLICATE_CLOSE_SOURCE; |
| 96 | if (!DuplicateHandle(GetCurrentProcess(), |
| 97 | reinterpret_cast<HANDLE>(sourceh), |
| 98 | processh, |
| 99 | &channel, |
| 100 | 0, // Unused given DUPLICATE_SAME_ACCESS. |
| 101 | FALSE, |
| 102 | flags)) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 103 | DLOG(ERROR) << "DuplicateHandle() failed"; |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | handles_for_sel_ldr->push_back( |
| 107 | reinterpret_cast<nacl::FileDescriptor>(channel)); |
| 108 | #else |
| 109 | nacl::FileDescriptor channel; |
| 110 | channel.fd = sourceh; |
| 111 | channel.auto_close = close_source; |
| 112 | handles_for_sel_ldr->push_back(channel); |
| 113 | #endif |
| 114 | return true; |
| 115 | } |
| 116 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 117 | } // namespace |
| 118 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 119 | struct NaClProcessHost::NaClInternal { |
| 120 | std::vector<nacl::Handle> sockets_for_renderer; |
| 121 | std::vector<nacl::Handle> sockets_for_sel_ldr; |
| 122 | }; |
| 123 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 124 | // ----------------------------------------------------------------------------- |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 125 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 126 | NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host) |
| 127 | : host_(host) { |
| 128 | } |
| 129 | |
| 130 | bool NaClProcessHost::PluginListener::OnMessageReceived( |
| 131 | const IPC::Message& msg) { |
| 132 | return host_->OnUntrustedMessageForwarded(msg); |
| 133 | } |
| 134 | |
[email protected] | 195d4cde | 2012-10-02 18:12:41 | [diff] [blame] | 135 | // TODO(brettw) bug 153036 set the pepper permissions up for dev interfaces. |
[email protected] | 57473ebc | 2012-05-30 07:44:52 | [diff] [blame] | 136 | NaClProcessHost::NaClProcessHost(const GURL& manifest_url, bool off_the_record) |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 137 | : manifest_url_(manifest_url), |
[email protected] | 195d4cde | 2012-10-02 18:12:41 | [diff] [blame] | 138 | permissions_(ppapi::PpapiPermissions::GetForCommandLine(0)), |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 139 | #if defined(OS_WIN) |
| 140 | process_launched_by_broker_(false), |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 141 | #elif defined(OS_LINUX) |
| 142 | wait_for_nacl_gdb_(false), |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 143 | #endif |
| 144 | reply_msg_(NULL), |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 145 | #if defined(OS_WIN) |
| 146 | debug_exception_handler_requested_(false), |
| 147 | #endif |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 148 | internal_(new NaClInternal()), |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 149 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
[email protected] | 57473ebc | 2012-05-30 07:44:52 | [diff] [blame] | 150 | enable_exception_handling_(false), |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 151 | enable_debug_stub_(false), |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 152 | off_the_record_(off_the_record), |
| 153 | enable_ipc_proxy_(false), |
| 154 | ALLOW_THIS_IN_INITIALIZER_LIST(ipc_plugin_listener_(this)) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 155 | process_.reset(content::BrowserChildProcessHost::Create( |
| 156 | content::PROCESS_TYPE_NACL_LOADER, this)); |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 157 | |
| 158 | // Set the display name so the user knows what plugin the process is running. |
| 159 | // We aren't on the UI thread so getting the pref locale for language |
| 160 | // formatting isn't possible, so IDN will be lost, but this is probably OK |
| 161 | // for this use case. |
| 162 | process_->SetName(net::FormatUrl(manifest_url_, std::string())); |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 163 | |
| 164 | // We allow untrusted hardware exception handling to be enabled via |
| 165 | // an env var for consistency with the standalone build of NaCl. |
| 166 | if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 167 | switches::kEnableNaClExceptionHandling) || |
| 168 | getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) { |
| 169 | enable_exception_handling_ = true; |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 170 | } |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 171 | enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch( |
| 172 | switches::kEnableNaClDebug); |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 173 | |
| 174 | enable_ipc_proxy_ = CommandLine::ForCurrentProcess()->HasSwitch( |
| 175 | switches::kEnableNaClIPCProxy); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 178 | NaClProcessHost::~NaClProcessHost() { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 179 | int exit_code; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 180 | process_->GetTerminationStatus(&exit_code); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 181 | std::string message = |
| 182 | base::StringPrintf("NaCl process exited with status %i (0x%x)", |
| 183 | exit_code, exit_code); |
| 184 | if (exit_code == 0) { |
| 185 | LOG(INFO) << message; |
| 186 | } else { |
| 187 | LOG(ERROR) << message; |
| 188 | } |
| 189 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 190 | for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) { |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 191 | if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 192 | NOTREACHED() << "nacl::Close() failed"; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 193 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 194 | } |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 195 | for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) { |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 196 | if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 197 | NOTREACHED() << "nacl::Close() failed"; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 198 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 201 | if (reply_msg_) { |
| 202 | // The process failed to launch for some reason. |
| 203 | // Don't keep the renderer hanging. |
| 204 | reply_msg_->set_reply_error(); |
| 205 | chrome_render_message_filter_->Send(reply_msg_); |
| 206 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 207 | #if defined(OS_WIN) |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 208 | if (process_launched_by_broker_) { |
| 209 | NaClBrokerService::GetInstance()->OnLoaderDied(); |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 210 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 211 | #endif |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 214 | // This is called at browser startup. |
| 215 | // static |
| 216 | void NaClProcessHost::EarlyStartup() { |
| 217 | #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 218 | // Open the IRT file early to make sure that it isn't replaced out from |
| 219 | // under us by autoupdate. |
| 220 | NaClBrowser::GetInstance()->EnsureIrtAvailable(); |
| 221 | #endif |
[email protected] | 9addd1c | 2012-09-15 14:28:24 | [diff] [blame] | 222 | CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 223 | UMA_HISTOGRAM_BOOLEAN( |
| 224 | "NaCl.nacl-gdb", |
| 225 | !cmd->GetSwitchValuePath(switches::kNaClGdb).empty()); |
| 226 | UMA_HISTOGRAM_BOOLEAN( |
| 227 | "NaCl.nacl-gdb-script", |
| 228 | !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty()); |
| 229 | UMA_HISTOGRAM_BOOLEAN( |
| 230 | "NaCl.enable-nacl-debug", |
| 231 | cmd->HasSwitch(switches::kEnableNaClDebug)); |
[email protected] | 66f409c | 2012-10-04 20:59:04 | [diff] [blame^] | 232 | NaClBrowser::GetInstance()->SetDebugPatterns( |
| 233 | cmd->GetSwitchValueASCII(switches::kNaClDebugMask)); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 236 | void NaClProcessHost::Launch( |
[email protected] | 92d5641 | 2011-03-24 20:53:52 | [diff] [blame] | 237 | ChromeRenderMessageFilter* chrome_render_message_filter, |
| 238 | int socket_count, |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 239 | IPC::Message* reply_msg, |
| 240 | scoped_refptr<ExtensionInfoMap> extension_info_map) { |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 241 | chrome_render_message_filter_ = chrome_render_message_filter; |
| 242 | reply_msg_ = reply_msg; |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 243 | extension_info_map_ = extension_info_map; |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 244 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 245 | // Place an arbitrary limit on the number of sockets to limit |
| 246 | // exposure in case the renderer is compromised. We can increase |
| 247 | // this if necessary. |
| 248 | if (socket_count > 8) { |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 249 | delete this; |
| 250 | return; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 253 | // Start getting the IRT open asynchronously while we launch the NaCl process. |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 254 | // We'll make sure this actually finished in StartWithLaunchedProcess, below. |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 255 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 256 | nacl_browser->EnsureAllResourcesAvailable(); |
| 257 | if (!nacl_browser->IsOk()) { |
| 258 | DLOG(ERROR) << "Cannot launch NaCl process"; |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 259 | delete this; |
| 260 | return; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 263 | // Rather than creating a socket pair in the renderer, and passing |
| 264 | // one side through the browser to sel_ldr, socket pairs are created |
| 265 | // in the browser and then passed to the renderer and sel_ldr. |
| 266 | // |
| 267 | // This is mainly for the benefit of Windows, where sockets cannot |
| 268 | // be passed in messages, but are copied via DuplicateHandle(). |
| 269 | // This means the sandboxed renderer cannot send handles to the |
| 270 | // browser process. |
| 271 | |
| 272 | for (int i = 0; i < socket_count; i++) { |
| 273 | nacl::Handle pair[2]; |
| 274 | // Create a connected socket |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 275 | if (nacl::SocketPair(pair) == -1) { |
| 276 | delete this; |
| 277 | return; |
| 278 | } |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 279 | internal_->sockets_for_renderer.push_back(pair[0]); |
| 280 | internal_->sockets_for_sel_ldr.push_back(pair[1]); |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 281 | SetCloseOnExec(pair[0]); |
| 282 | SetCloseOnExec(pair[1]); |
| 283 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 284 | |
| 285 | // Launch the process |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 286 | if (!LaunchSelLdr()) { |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 287 | delete this; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 288 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 289 | } |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 290 | |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 291 | #if defined(OS_WIN) |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 292 | void NaClProcessHost::OnChannelConnected(int32 peer_pid) { |
| 293 | // Set process handle, if it was not set previously. |
| 294 | // This is needed when NaCl process is launched with nacl-gdb. |
[email protected] | 82d1750 | 2012-05-23 19:12:40 | [diff] [blame] | 295 | if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 296 | switches::kNaClGdb).empty()) { |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 297 | base::ProcessHandle process; |
[email protected] | 82d1750 | 2012-05-23 19:12:40 | [diff] [blame] | 298 | DCHECK(process_->GetData().handle == base::kNullProcessHandle); |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 299 | if (base::OpenProcessHandleWithAccess( |
| 300 | peer_pid, |
| 301 | base::kProcessAccessDuplicateHandle | |
| 302 | base::kProcessAccessQueryInformation | |
| 303 | base::kProcessAccessWaitForTermination, |
| 304 | &process)) { |
| 305 | process_->SetHandle(process); |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 306 | if (!StartWithLaunchedProcess()) { |
| 307 | delete this; |
| 308 | return; |
| 309 | } |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 310 | } else { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 311 | DLOG(ERROR) << "Failed to get process handle"; |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 312 | } |
| 313 | } |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 314 | } |
| 315 | #else |
| 316 | void NaClProcessHost::OnChannelConnected(int32 peer_pid) { |
| 317 | } |
| 318 | #endif |
| 319 | |
| 320 | #if defined(OS_WIN) |
| 321 | void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) { |
| 322 | process_launched_by_broker_ = true; |
| 323 | process_->SetHandle(handle); |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 324 | if (!StartWithLaunchedProcess()) |
| 325 | delete this; |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 328 | void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) { |
| 329 | IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release(); |
| 330 | NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success); |
| 331 | Send(reply); |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 332 | } |
| 333 | #endif |
| 334 | |
| 335 | // Needed to handle sync messages in OnMessageRecieved. |
| 336 | bool NaClProcessHost::Send(IPC::Message* msg) { |
| 337 | return process_->Send(msg); |
| 338 | } |
| 339 | |
| 340 | #if defined(OS_WIN) |
| 341 | scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb( |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 342 | const FilePath& nacl_gdb, |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 343 | CommandLine* line) { |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 344 | CommandLine* cmd_line = new CommandLine(nacl_gdb); |
| 345 | // We can't use PrependWrapper because our parameters contain spaces. |
| 346 | cmd_line->AppendArg("--eval-command"); |
| 347 | const FilePath::StringType& irt_path = |
| 348 | NaClBrowser::GetInstance()->GetIrtFilePath().value(); |
| 349 | cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path); |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 350 | FilePath manifest_path = GetManifestPath(); |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 351 | if (!manifest_path.empty()) { |
| 352 | cmd_line->AppendArg("--eval-command"); |
| 353 | cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") + |
| 354 | manifest_path.value()); |
| 355 | } |
[email protected] | 401b9079 | 2012-05-30 11:41:13 | [diff] [blame] | 356 | FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 357 | switches::kNaClGdbScript); |
| 358 | if (!script.empty()) { |
| 359 | cmd_line->AppendArg("--command"); |
| 360 | cmd_line->AppendArgNative(script.value()); |
| 361 | } |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 362 | cmd_line->AppendArg("--args"); |
| 363 | const CommandLine::StringVector& argv = line->argv(); |
| 364 | for (size_t i = 0; i < argv.size(); i++) { |
| 365 | cmd_line->AppendArgNative(argv[i]); |
| 366 | } |
| 367 | return scoped_ptr<CommandLine>(cmd_line); |
| 368 | } |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 369 | #elif defined(OS_LINUX) |
[email protected] | 512d03f | 2012-06-26 01:06:06 | [diff] [blame] | 370 | class NaClProcessHost::NaClGdbWatchDelegate |
| 371 | : public MessageLoopForIO::Watcher { |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 372 | public: |
| 373 | // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_ |
| 374 | NaClGdbWatchDelegate(int fd_read, int fd_write, |
| 375 | const base::Closure& reply) |
| 376 | : fd_read_(fd_read), |
| 377 | fd_write_(fd_write), |
| 378 | reply_(reply) {} |
| 379 | |
| 380 | ~NaClGdbWatchDelegate() { |
| 381 | if (HANDLE_EINTR(close(fd_read_)) != 0) |
| 382 | DLOG(ERROR) << "close(fd_read_) failed"; |
| 383 | if (HANDLE_EINTR(close(fd_write_)) != 0) |
| 384 | DLOG(ERROR) << "close(fd_write_) failed"; |
| 385 | } |
| 386 | |
| 387 | virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 388 | virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} |
| 389 | |
| 390 | private: |
| 391 | int fd_read_; |
| 392 | int fd_write_; |
| 393 | base::Closure reply_; |
| 394 | }; |
| 395 | |
[email protected] | 512d03f | 2012-06-26 01:06:06 | [diff] [blame] | 396 | void NaClProcessHost::NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking( |
| 397 | int fd) { |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 398 | char buf; |
| 399 | if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0') |
| 400 | LOG(ERROR) << "Failed to sync with nacl-gdb"; |
| 401 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_); |
| 402 | } |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 403 | |
| 404 | bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) { |
| 405 | CommandLine::StringType nacl_gdb = |
| 406 | CommandLine::ForCurrentProcess()->GetSwitchValueNative( |
| 407 | switches::kNaClGdb); |
| 408 | CommandLine::StringVector argv; |
| 409 | // We don't support spaces inside arguments in --nacl-gdb switch. |
| 410 | base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv); |
| 411 | CommandLine cmd_line(argv); |
| 412 | cmd_line.AppendArg("--eval-command"); |
| 413 | const FilePath::StringType& irt_path = |
| 414 | NaClBrowser::GetInstance()->GetIrtFilePath().value(); |
| 415 | cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path); |
| 416 | FilePath manifest_path = GetManifestPath(); |
| 417 | if (!manifest_path.empty()) { |
| 418 | cmd_line.AppendArg("--eval-command"); |
| 419 | cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") + |
| 420 | manifest_path.value()); |
| 421 | } |
| 422 | cmd_line.AppendArg("--eval-command"); |
| 423 | cmd_line.AppendArg("attach " + base::IntToString(pid)); |
| 424 | int fds[2]; |
| 425 | if (pipe(fds) != 0) |
| 426 | return false; |
| 427 | // Tell the debugger to send a byte to the writable end of the pipe. |
| 428 | // We use a file descriptor in our process because the debugger will be |
| 429 | // typically launched in a separate terminal, and a lot of terminals close all |
| 430 | // file descriptors before launching external programs. |
| 431 | cmd_line.AppendArg("--eval-command"); |
| 432 | cmd_line.AppendArg("dump binary value /proc/" + |
| 433 | base::IntToString(base::GetCurrentProcId()) + |
| 434 | "/fd/" + base::IntToString(fds[1]) + " (char)0"); |
[email protected] | 401b9079 | 2012-05-30 11:41:13 | [diff] [blame] | 435 | FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 436 | switches::kNaClGdbScript); |
| 437 | if (!script.empty()) { |
| 438 | cmd_line.AppendArg("--command"); |
| 439 | cmd_line.AppendArgNative(script.value()); |
| 440 | } |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 441 | // wait on fds[0] |
| 442 | // If the debugger crashes before attaching to the NaCl process, the user can |
| 443 | // release resources by terminating the NaCl loader in Chrome Task Manager. |
| 444 | nacl_gdb_watcher_delegate_.reset( |
| 445 | new NaClGdbWatchDelegate( |
| 446 | fds[0], fds[1], |
| 447 | base::Bind(&NaClProcessHost::OnNaClGdbAttached, |
| 448 | weak_factory_.GetWeakPtr()))); |
| 449 | MessageLoopForIO::current()->WatchFileDescriptor( |
| 450 | fds[0], |
| 451 | true, |
| 452 | MessageLoopForIO::WATCH_READ, |
| 453 | &nacl_gdb_watcher_, |
| 454 | nacl_gdb_watcher_delegate_.get()); |
| 455 | return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL); |
| 456 | } |
| 457 | |
| 458 | void NaClProcessHost::OnNaClGdbAttached() { |
| 459 | wait_for_nacl_gdb_ = false; |
| 460 | nacl_gdb_watcher_.StopWatchingFileDescriptor(); |
| 461 | nacl_gdb_watcher_delegate_.reset(); |
| 462 | OnProcessLaunched(); |
| 463 | } |
| 464 | #endif |
| 465 | |
| 466 | FilePath NaClProcessHost::GetManifestPath() { |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 467 | const extensions::Extension* extension = extension_info_map_->extensions() |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 468 | .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_)); |
| 469 | if (extension != NULL && manifest_url_.SchemeIs(chrome::kExtensionScheme)) { |
| 470 | std::string path = manifest_url_.path(); |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 471 | TrimString(path, "/", &path); // Remove first slash |
| 472 | return extension->path().AppendASCII(path); |
| 473 | } |
| 474 | return FilePath(); |
| 475 | } |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 476 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 477 | bool NaClProcessHost::LaunchSelLdr() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 478 | std::string channel_id = process_->GetHost()->CreateChannel(); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 479 | if (channel_id.empty()) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 480 | return false; |
| 481 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 482 | CommandLine::StringType nacl_loader_prefix; |
| 483 | #if defined(OS_POSIX) |
| 484 | nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative( |
| 485 | switches::kNaClLoaderCmdPrefix); |
| 486 | #endif // defined(OS_POSIX) |
| 487 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 488 | // Build command line for nacl. |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 489 | |
| 490 | #if defined(OS_MACOSX) |
| 491 | // The Native Client process needs to be able to allocate a 1GB contiguous |
| 492 | // region to use as the client environment's virtual address space. ASLR |
| 493 | // (PIE) interferes with this by making it possible that no gap large enough |
| 494 | // to accomodate this request will exist in the child process' address |
| 495 | // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and |
| 496 | // http://code.google.com/p/nativeclient/issues/detail?id=2043. |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 497 | int flags = ChildProcessHost::CHILD_NO_PIE; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 498 | #elif defined(OS_LINUX) |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 499 | int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF : |
| 500 | ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 501 | #else |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 502 | int flags = ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 503 | #endif |
| 504 | |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 505 | FilePath exe_path = ChildProcessHost::GetChildPath(flags); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 506 | if (exe_path.empty()) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 507 | return false; |
| 508 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 509 | #if defined(OS_WIN) |
| 510 | // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe |
| 511 | if (RunningOnWOW64()) { |
| 512 | FilePath module_path; |
| 513 | if (!PathService::Get(base::FILE_MODULE, &module_path)) |
| 514 | return false; |
| 515 | exe_path = module_path.DirName().Append(chrome::kNaClAppName); |
| 516 | } |
| 517 | #endif |
| 518 | |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 519 | scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path)); |
| 520 | nacl::CopyNaClCommandLineArguments(cmd_line.get()); |
[email protected] | 599e664 | 2010-01-27 18:52:13 | [diff] [blame] | 521 | |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 522 | cmd_line->AppendSwitchASCII(switches::kProcessType, |
| 523 | switches::kNaClLoaderProcess); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 524 | cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id); |
[email protected] | 93156cec | 2011-09-12 21:14:44 | [diff] [blame] | 525 | if (logging::DialogsAreSuppressed()) |
| 526 | cmd_line->AppendSwitch(switches::kNoErrorDialogs); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 527 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 528 | if (!nacl_loader_prefix.empty()) |
| 529 | cmd_line->PrependWrapper(nacl_loader_prefix); |
| 530 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 531 | FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 532 | switches::kNaClGdb); |
| 533 | if (!nacl_gdb.empty()) { |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 534 | #if defined(OS_WIN) |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 535 | cmd_line->AppendSwitch(switches::kNoSandbox); |
| 536 | scoped_ptr<CommandLine> gdb_cmd_line( |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 537 | GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get())); |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 538 | // We can't use process_->Launch() because OnProcessLaunched will be called |
| 539 | // with process_->GetData().handle filled by handle of gdb process. This |
| 540 | // handle will be used to duplicate handles for NaCl process and as |
| 541 | // a result NaCl process will not be able to use them. |
| 542 | // |
| 543 | // So we don't fill process_->GetData().handle and wait for |
| 544 | // OnChannelConnected to get handle of NaCl process from its pid. Then we |
| 545 | // call OnProcessLaunched. |
| 546 | return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL); |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 547 | #elif defined(OS_LINUX) |
| 548 | wait_for_nacl_gdb_ = true; |
| 549 | #endif |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 550 | } |
| 551 | |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 552 | // On Windows we might need to start the broker process to launch a new loader |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 553 | #if defined(OS_WIN) |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 554 | if (RunningOnWOW64()) { |
[email protected] | 26b3c00 | 2012-04-26 19:38:34 | [diff] [blame] | 555 | return NaClBrokerService::GetInstance()->LaunchLoader( |
| 556 | weak_factory_.GetWeakPtr(), channel_id); |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 557 | } else { |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 558 | process_->Launch(FilePath(), cmd_line.release()); |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 559 | } |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 560 | #elif defined(OS_POSIX) |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 561 | process_->Launch(nacl_loader_prefix.empty(), // use_zygote |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 562 | base::EnvironmentVector(), |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 563 | cmd_line.release()); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 564 | #endif |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 565 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 566 | return true; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 567 | } |
| 568 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 569 | bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) { |
| 570 | bool handled = true; |
| 571 | IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg) |
| 572 | IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate, |
| 573 | OnQueryKnownToValidate) |
| 574 | IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate, |
| 575 | OnSetKnownToValidate) |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 576 | #if defined(OS_WIN) |
| 577 | IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler, |
| 578 | OnAttachDebugExceptionHandler) |
| 579 | #endif |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 580 | IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated, |
| 581 | OnPpapiChannelCreated) |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 582 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 583 | IPC_END_MESSAGE_MAP() |
| 584 | return handled; |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 585 | } |
| 586 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 587 | void NaClProcessHost::OnProcessLaunched() { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 588 | if (!StartWithLaunchedProcess()) |
| 589 | delete this; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 590 | } |
| 591 | |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 592 | // Called when the NaClBrowser singleton has been fully initialized. |
| 593 | void NaClProcessHost::OnResourcesReady() { |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 594 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 595 | if (!nacl_browser->IsReady() || !SendStart()) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 596 | DLOG(ERROR) << "Cannot launch NaCl process"; |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 597 | delete this; |
| 598 | } |
| 599 | } |
| 600 | |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 601 | bool NaClProcessHost::ReplyToRenderer( |
| 602 | const IPC::ChannelHandle& channel_handle) { |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 603 | std::vector<nacl::FileDescriptor> handles_for_renderer; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 604 | for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) { |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 605 | #if defined(OS_WIN) |
| 606 | // Copy the handle into the renderer process. |
| 607 | HANDLE handle_in_renderer; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 608 | if (!DuplicateHandle(base::GetCurrentProcessHandle(), |
| 609 | reinterpret_cast<HANDLE>( |
| 610 | internal_->sockets_for_renderer[i]), |
| 611 | chrome_render_message_filter_->peer_handle(), |
| 612 | &handle_in_renderer, |
| 613 | 0, // Unused given DUPLICATE_SAME_ACCESS. |
| 614 | FALSE, |
| 615 | DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 616 | DLOG(ERROR) << "DuplicateHandle() failed"; |
| 617 | return false; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 618 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 619 | handles_for_renderer.push_back( |
| 620 | reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer)); |
| 621 | #else |
| 622 | // No need to dup the imc_handle - we don't pass it anywhere else so |
| 623 | // it cannot be closed. |
| 624 | nacl::FileDescriptor imc_handle; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 625 | imc_handle.fd = internal_->sockets_for_renderer[i]; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 626 | imc_handle.auto_close = true; |
| 627 | handles_for_renderer.push_back(imc_handle); |
| 628 | #endif |
| 629 | } |
| 630 | |
| 631 | #if defined(OS_WIN) |
[email protected] | e4f6eb023 | 2012-04-17 00:47:50 | [diff] [blame] | 632 | // If we are on 64-bit Windows, the NaCl process's sandbox is |
| 633 | // managed by a different process from the renderer's sandbox. We |
| 634 | // need to inform the renderer's sandbox about the NaCl process so |
| 635 | // that the renderer can send handles to the NaCl process using |
| 636 | // BrokerDuplicateHandle(). |
| 637 | if (RunningOnWOW64()) { |
[email protected] | 171fa98d | 2012-04-23 21:34:01 | [diff] [blame] | 638 | if (!content::BrokerAddTargetPeer(process_->GetData().handle)) { |
[email protected] | e4f6eb023 | 2012-04-17 00:47:50 | [diff] [blame] | 639 | DLOG(ERROR) << "Failed to add NaCl process PID"; |
| 640 | return false; |
| 641 | } |
| 642 | } |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 643 | #endif |
| 644 | |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 645 | ChromeViewHostMsg_LaunchNaCl::WriteReplyParams( |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 646 | reply_msg_, handles_for_renderer, channel_handle); |
[email protected] | 92d5641 | 2011-03-24 20:53:52 | [diff] [blame] | 647 | chrome_render_message_filter_->Send(reply_msg_); |
| 648 | chrome_render_message_filter_ = NULL; |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 649 | reply_msg_ = NULL; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 650 | internal_->sockets_for_renderer.clear(); |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 651 | return true; |
| 652 | } |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 653 | |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 654 | // TCP port we chose for NaCl debug stub. It can be any other number. |
| 655 | static const int kDebugStubPort = 4014; |
| 656 | |
| 657 | #if defined(OS_POSIX) |
| 658 | SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() { |
| 659 | SocketDescriptor s = net::TCPListenSocket::CreateAndBind("127.0.0.1", |
| 660 | kDebugStubPort); |
| 661 | if (listen(s, 1)) { |
| 662 | LOG(ERROR) << "listen() failed on debug stub socket"; |
| 663 | return net::TCPListenSocket::kInvalidSocket; |
| 664 | } |
| 665 | return s; |
| 666 | } |
| 667 | #endif |
| 668 | |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 669 | bool NaClProcessHost::StartNaClExecution() { |
| 670 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
| 671 | |
| 672 | nacl::NaClStartParams params; |
[email protected] | 8c67083 | 2012-05-26 04:30:12 | [diff] [blame] | 673 | params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled(); |
| 674 | params.validation_cache_key = nacl_browser->GetValidationCacheKey(); |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 675 | params.version = chrome::VersionInfo().CreateVersionString(); |
| 676 | params.enable_exception_handling = enable_exception_handling_; |
[email protected] | 66f409c | 2012-10-04 20:59:04 | [diff] [blame^] | 677 | params.enable_debug_stub = enable_debug_stub_ && |
| 678 | NaClBrowser::GetInstance()->URLMatchesDebugPatterns(manifest_url_); |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 679 | params.enable_ipc_proxy = enable_ipc_proxy_; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 680 | |
| 681 | base::PlatformFile irt_file = nacl_browser->IrtFile(); |
| 682 | CHECK_NE(irt_file, base::kInvalidPlatformFileValue); |
| 683 | |
| 684 | const ChildProcessData& data = process_->GetData(); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 685 | for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) { |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 686 | if (!ShareHandleToSelLdr(data.handle, |
| 687 | internal_->sockets_for_sel_ldr[i], true, |
| 688 | ¶ms.handles)) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 689 | return false; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 690 | } |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | // Send over the IRT file handle. We don't close our own copy! |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 694 | if (!ShareHandleToSelLdr(data.handle, irt_file, false, ¶ms.handles)) |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 695 | return false; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 696 | |
[email protected] | ab88d154 | 2011-11-18 22:52:00 | [diff] [blame] | 697 | #if defined(OS_MACOSX) |
| 698 | // For dynamic loading support, NaCl requires a file descriptor that |
| 699 | // was created in /tmp, since those created with shm_open() are not |
| 700 | // mappable with PROT_EXEC. Rather than requiring an extra IPC |
| 701 | // round trip out of the sandbox, we create an FD here. |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 702 | base::SharedMemory memory_buffer; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 703 | base::SharedMemoryCreateOptions options; |
| 704 | options.size = 1; |
| 705 | options.executable = true; |
| 706 | if (!memory_buffer.Create(options)) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 707 | DLOG(ERROR) << "Failed to allocate memory buffer"; |
| 708 | return false; |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 709 | } |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 710 | nacl::FileDescriptor memory_fd; |
| 711 | memory_fd.fd = dup(memory_buffer.handle().fd); |
| 712 | if (memory_fd.fd < 0) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 713 | DLOG(ERROR) << "Failed to dup() a file descriptor"; |
| 714 | return false; |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 715 | } |
| 716 | memory_fd.auto_close = true; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 717 | params.handles.push_back(memory_fd); |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 718 | #endif |
| 719 | |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 720 | #if defined(OS_POSIX) |
| 721 | if (enable_debug_stub_) { |
| 722 | SocketDescriptor server_bound_socket = GetDebugStubSocketHandle(); |
| 723 | if (server_bound_socket != net::TCPListenSocket::kInvalidSocket) { |
| 724 | params.debug_stub_server_bound_socket = |
| 725 | nacl::FileDescriptor(server_bound_socket, true); |
| 726 | } |
| 727 | } |
| 728 | #endif |
| 729 | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 730 | process_->Send(new NaClProcessMsg_Start(params)); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 731 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 732 | internal_->sockets_for_sel_ldr.clear(); |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 733 | return true; |
| 734 | } |
| 735 | |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 736 | bool NaClProcessHost::SendStart() { |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 737 | if (!enable_ipc_proxy_) { |
| 738 | if (!ReplyToRenderer(IPC::ChannelHandle())) |
| 739 | return false; |
| 740 | } |
| 741 | return StartNaClExecution(); |
| 742 | } |
| 743 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 744 | // This method is called when NaClProcessHostMsg_PpapiChannelCreated is |
| 745 | // received or PpapiHostMsg_ChannelCreated is forwarded by our plugin |
| 746 | // listener. |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 747 | void NaClProcessHost::OnPpapiChannelCreated( |
| 748 | const IPC::ChannelHandle& channel_handle) { |
| 749 | DCHECK(enable_ipc_proxy_); |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 750 | // If the proxy channel is null, this must be the initial NaCl-Browser IPC |
| 751 | // channel. |
| 752 | if (!ipc_proxy_channel_.get()) { |
| 753 | ipc_proxy_channel_.reset( |
| 754 | new IPC::ChannelProxy(channel_handle, |
| 755 | IPC::Channel::MODE_CLIENT, |
| 756 | &ipc_plugin_listener_, |
| 757 | base::MessageLoopProxy::current())); |
[email protected] | b4b53d18 | 2012-09-12 02:56:55 | [diff] [blame] | 758 | // Enable PPAPI message dispatching to the browser process. |
| 759 | content::EnablePepperSupportForChannel( |
| 760 | ipc_proxy_channel_.get(), |
| 761 | chrome_render_message_filter_->GetHostResolver()); |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 762 | // Send a message to create the NaCl-Renderer channel. The handle is just |
| 763 | // a place holder. |
| 764 | ipc_proxy_channel_->Send( |
| 765 | new PpapiMsg_CreateNaClChannel( |
| 766 | chrome_render_message_filter_->render_process_id(), |
[email protected] | 195d4cde | 2012-10-02 18:12:41 | [diff] [blame] | 767 | permissions_, |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 768 | chrome_render_message_filter_->off_the_record(), |
| 769 | SerializedHandle(SerializedHandle::CHANNEL_HANDLE, |
| 770 | IPC::InvalidPlatformFileForTransit()))); |
| 771 | } else if (reply_msg_) { |
| 772 | // Otherwise, this must be a renderer channel. |
| 773 | ReplyToRenderer(channel_handle); |
| 774 | } else { |
| 775 | // Attempt to open more than 1 renderer channel is not supported. |
| 776 | // Shut down the NaCl process. |
| 777 | process_->GetHost()->ForceShutdown(); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) { |
| 782 | // Handle messages that have been forwarded from our PluginListener. |
| 783 | // These messages come from untrusted code so should be handled with care. |
| 784 | bool handled = true; |
| 785 | IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg) |
| 786 | IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated, |
| 787 | OnPpapiChannelCreated) |
| 788 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 789 | IPC_END_MESSAGE_MAP() |
| 790 | return handled; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 791 | } |
| 792 | |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 793 | bool NaClProcessHost::StartWithLaunchedProcess() { |
| 794 | #if defined(OS_LINUX) |
| 795 | if (wait_for_nacl_gdb_) { |
| 796 | if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) { |
| 797 | // We will be called with wait_for_nacl_gdb_ = false once debugger is |
| 798 | // attached to the program. |
| 799 | return true; |
| 800 | } |
| 801 | DLOG(ERROR) << "Failed to launch debugger"; |
| 802 | // Continue execution without debugger. |
| 803 | } |
| 804 | #endif |
| 805 | |
| 806 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 807 | |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 808 | if (nacl_browser->IsReady()) { |
| 809 | return SendStart(); |
| 810 | } else if (nacl_browser->IsOk()) { |
| 811 | nacl_browser->WaitForResources( |
| 812 | base::Bind(&NaClProcessHost::OnResourcesReady, |
| 813 | weak_factory_.GetWeakPtr())); |
| 814 | return true; |
| 815 | } else { |
| 816 | return false; |
| 817 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 818 | } |
| 819 | |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 820 | void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature, |
| 821 | bool* result) { |
[email protected] | 7cfaf98 | 2012-05-09 18:37:47 | [diff] [blame] | 822 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | 57473ebc | 2012-05-30 07:44:52 | [diff] [blame] | 823 | *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_); |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) { |
[email protected] | 57473ebc | 2012-05-30 07:44:52 | [diff] [blame] | 827 | NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_); |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 828 | } |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 829 | |
| 830 | #if defined(OS_WIN) |
| 831 | void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info, |
| 832 | IPC::Message* reply_msg) { |
| 833 | if (!AttachDebugExceptionHandler(info, reply_msg)) { |
| 834 | // Send failure message. |
| 835 | NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg, |
| 836 | false); |
| 837 | Send(reply_msg); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info, |
| 842 | IPC::Message* reply_msg) { |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 843 | if (!enable_exception_handling_ && !enable_debug_stub_) { |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 844 | DLOG(ERROR) << |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 845 | "Debug exception handler requested by NaCl process when not enabled"; |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 846 | return false; |
| 847 | } |
| 848 | if (debug_exception_handler_requested_) { |
| 849 | // The NaCl process should not request this multiple times. |
| 850 | DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received"; |
| 851 | return false; |
| 852 | } |
| 853 | debug_exception_handler_requested_ = true; |
| 854 | |
| 855 | base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle); |
| 856 | base::win::ScopedHandle process_handle; |
| 857 | // We cannot use process_->GetData().handle because it does not have |
| 858 | // the necessary access rights. We open the new handle here rather |
| 859 | // than in the NaCl broker process in case the NaCl loader process |
| 860 | // dies before the NaCl broker process receives the message we send. |
| 861 | // The debug exception handler uses DebugActiveProcess() to attach, |
| 862 | // but this takes a PID. We need to prevent the NaCl loader's PID |
| 863 | // from being reused before DebugActiveProcess() is called, and |
| 864 | // holding a process handle open achieves this. |
| 865 | if (!base::OpenProcessHandleWithAccess( |
| 866 | nacl_pid, |
| 867 | base::kProcessAccessQueryInformation | |
| 868 | base::kProcessAccessSuspendResume | |
| 869 | base::kProcessAccessTerminate | |
| 870 | base::kProcessAccessVMOperation | |
| 871 | base::kProcessAccessVMRead | |
| 872 | base::kProcessAccessVMWrite | |
| 873 | base::kProcessAccessWaitForTermination, |
| 874 | process_handle.Receive())) { |
| 875 | LOG(ERROR) << "Failed to get process handle"; |
| 876 | return false; |
| 877 | } |
| 878 | |
| 879 | attach_debug_exception_handler_reply_msg_.reset(reply_msg); |
| 880 | // If the NaCl loader is 64-bit, the process running its debug |
| 881 | // exception handler must be 64-bit too, so we use the 64-bit NaCl |
| 882 | // broker process for this. Otherwise, on a 32-bit system, we use |
| 883 | // the 32-bit browser process to run the debug exception handler. |
| 884 | if (RunningOnWOW64()) { |
| 885 | return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler( |
[email protected] | fd37bf6 | 2012-05-04 16:46:48 | [diff] [blame] | 886 | weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info); |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 887 | } else { |
| 888 | NaClStartDebugExceptionHandlerThread( |
[email protected] | fd37bf6 | 2012-05-04 16:46:48 | [diff] [blame] | 889 | process_handle.Take(), info, |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 890 | base::MessageLoopProxy::current(), |
| 891 | base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker, |
| 892 | weak_factory_.GetWeakPtr())); |
| 893 | return true; |
| 894 | } |
| 895 | } |
| 896 | #endif |