blob: 26a1df5673dc7053c09b04a6747a045e8aaa1c6d [file] [log] [blame]
[email protected]b39c6d92012-01-31 16:38:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d032f492009-09-29 00:33:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e15a4fa2010-02-11 23:09:295#include "chrome/browser/nacl_host/nacl_process_host.h"
[email protected]d032f492009-09-29 00:33:466
[email protected]a82af392012-02-24 04:40:207#include <string>
8#include <vector>
[email protected]d032f492009-09-29 00:33:469
[email protected]ac07ec52013-04-22 17:32:4510#include "base/base_switches.h"
[email protected]30c1eea2011-10-17 18:40:3011#include "base/bind.h"
[email protected]103607e2010-02-01 18:57:0912#include "base/command_line.h"
[email protected]6d7f55f2013-05-14 10:12:5613#include "base/file_util.h"
[email protected]5b974952012-04-05 18:18:2314#include "base/message_loop.h"
[email protected]9addd1c2012-09-15 14:28:2415#include "base/metrics/histogram.h"
[email protected]338466a82011-05-03 04:27:4316#include "base/path_service.h"
[email protected]108fd342013-01-04 20:46:5417#include "base/process_util.h"
[email protected]8f42b4d2012-03-24 14:12:3318#include "base/string_util.h"
[email protected]a0a69bf2011-09-23 21:40:2819#include "base/stringprintf.h"
[email protected]3ea1b182013-02-08 22:38:4120#include "base/strings/string_number_conversions.h"
[email protected]1988e1c2013-02-28 20:27:4221#include "base/strings/string_split.h"
[email protected]be1ce6a72010-08-03 14:35:2222#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3723#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2024#include "build/build_config.h"
[email protected]8510d282012-08-30 19:47:3825#include "chrome/browser/browser_process.h"
[email protected]8f42b4d2012-03-24 14:12:3326#include "chrome/browser/extensions/extension_info_map.h"
[email protected]8510d282012-08-30 19:47:3827#include "chrome/browser/io_thread.h"
[email protected]2e5f2ea42012-05-09 21:39:2128#include "chrome/browser/nacl_host/nacl_browser.h"
[email protected]8f42b4d2012-03-24 14:12:3329#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]4b30e7d2013-04-24 23:52:1430#include "chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.h"
[email protected]31a665e72012-03-11 12:37:4631#include "chrome/common/chrome_constants.h"
[email protected]338466a82011-05-03 04:27:4332#include "chrome/common/chrome_paths.h"
[email protected]f3b357692013-03-22 05:16:1333#include "chrome/common/chrome_process_type.h"
[email protected]d032f492009-09-29 00:33:4634#include "chrome/common/chrome_switches.h"
[email protected]1657e6d2012-03-30 20:28:0035#include "chrome/common/chrome_version_info.h"
[email protected]d032f492009-09-29 00:33:4636#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0937#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4638#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3039#include "chrome/common/render_messages.h"
[email protected]8f42b4d2012-03-24 14:12:3340#include "chrome/common/url_constants.h"
[email protected]4967f792012-01-20 22:14:4041#include "content/public/browser/browser_child_process_host.h"
[email protected]0c7193742012-11-07 19:05:0342#include "content/public/browser/browser_ppapi_host.h"
[email protected]4967f792012-01-20 22:14:4043#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4444#include "content/public/common/child_process_host.h"
[email protected]9e141212013-01-21 15:21:5645#include "content/public/common/process_type.h"
[email protected]885c0e92012-11-13 20:27:4246#include "extensions/common/constants.h"
47#include "extensions/common/url_pattern.h"
[email protected]8510d282012-08-30 19:47:3848#include "ipc/ipc_channel.h"
[email protected]d032f492009-09-29 00:33:4649#include "ipc/ipc_switches.h"
[email protected]c8e1ea32013-02-13 02:19:0150#include "native_client/src/shared/imc/nacl_imc_c.h"
[email protected]87f35592012-04-08 00:49:1651#include "net/base/net_util.h"
[email protected]1946d9e2013-04-09 01:43:3752#include "net/socket/tcp_listen_socket.h"
[email protected]4b30e7d2013-04-24 23:52:1453#include "ppapi/host/ppapi_host.h"
[email protected]8510d282012-08-30 19:47:3854#include "ppapi/proxy/ppapi_messages.h"
[email protected]ac07ec52013-04-22 17:32:4555#include "ppapi/shared_impl/ppapi_nacl_channel_args.h"
[email protected]d032f492009-09-29 00:33:4656
[email protected]d032f492009-09-29 00:33:4657#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2058#include <fcntl.h>
59
[email protected]d032f492009-09-29 00:33:4660#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3561#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2062#include <windows.h>
63
[email protected]885c0e92012-11-13 20:27:4264#include "base/threading/thread.h"
[email protected]4c65fb632012-04-27 00:42:2565#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3566#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]ea6588842012-05-03 05:39:3867#include "chrome/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5068#include "content/public/common/sandbox_init.h"
[email protected]34f48682013-03-20 00:30:1869#include "content/public/common/sandboxed_process_launcher_delegate.h"
[email protected]d032f492009-09-29 00:33:4670#endif
71
[email protected]631bb742011-11-02 11:29:3972using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4073using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4474using content::ChildProcessHost;
[email protected]8510d282012-08-30 19:47:3875using ppapi::proxy::SerializedHandle;
[email protected]631bb742011-11-02 11:29:3976
[email protected]646e15552012-04-06 22:01:0477namespace {
78
79#if defined(OS_WIN)
80bool RunningOnWOW64() {
81 return (base::win::OSInfo::GetInstance()->wow64_status() ==
82 base::win::OSInfo::WOW64_ENABLED);
83}
[email protected]34f48682013-03-20 00:30:1884
85// NOTE: changes to this class need to be reviewed by the security team.
86class NaClSandboxedProcessLauncherDelegate
87 : public content::SandboxedProcessLauncherDelegate {
88 public:
89 NaClSandboxedProcessLauncherDelegate() {}
90 virtual ~NaClSandboxedProcessLauncherDelegate() {}
91
92 virtual void PostSpawnTarget(base::ProcessHandle process) {
93#if !defined(NACL_WIN64)
94 // For Native Client sel_ldr processes on 32-bit Windows, reserve 1 GB of
95 // address space to prevent later failure due to address space fragmentation
96 // from .dll loading. The NaCl process will attempt to locate this space by
97 // scanning the address space using VirtualQuery.
98 // TODO(bbudge) Handle the --no-sandbox case.
99 // http://code.google.com/p/nativeclient/issues/detail?id=2131
100 const SIZE_T kOneGigabyte = 1 << 30;
101 void* nacl_mem = VirtualAllocEx(process,
102 NULL,
103 kOneGigabyte,
104 MEM_RESERVE,
105 PAGE_NOACCESS);
106 if (!nacl_mem) {
107 DLOG(WARNING) << "Failed to reserve address space for Native Client";
108 }
109#endif // !defined(NACL_WIN64)
110 }
111};
112
113#endif // OS_WIN
[email protected]646e15552012-04-06 22:01:04114
[email protected]c8e1ea32013-02-13 02:19:01115void SetCloseOnExec(NaClHandle fd) {
[email protected]646e15552012-04-06 22:01:04116#if defined(OS_POSIX)
117 int flags = fcntl(fd, F_GETFD);
118 CHECK_NE(flags, -1);
119 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
120 CHECK_EQ(rc, 0);
121#endif
122}
123
[email protected]00d99542012-04-17 22:48:02124bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:04125 base::ProcessHandle processh,
[email protected]c8e1ea32013-02-13 02:19:01126 NaClHandle sourceh,
[email protected]646e15552012-04-06 22:01:04127 bool close_source,
128 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
129#if defined(OS_WIN)
130 HANDLE channel;
131 int flags = DUPLICATE_SAME_ACCESS;
132 if (close_source)
133 flags |= DUPLICATE_CLOSE_SOURCE;
134 if (!DuplicateHandle(GetCurrentProcess(),
135 reinterpret_cast<HANDLE>(sourceh),
136 processh,
137 &channel,
138 0, // Unused given DUPLICATE_SAME_ACCESS.
139 FALSE,
140 flags)) {
[email protected]ec7540f2013-01-17 00:00:35141 LOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:04142 return false;
143 }
144 handles_for_sel_ldr->push_back(
145 reinterpret_cast<nacl::FileDescriptor>(channel));
146#else
147 nacl::FileDescriptor channel;
148 channel.fd = sourceh;
149 channel.auto_close = close_source;
150 handles_for_sel_ldr->push_back(channel);
151#endif
152 return true;
153}
154
[email protected]27b04df2012-12-10 23:43:10155ppapi::PpapiPermissions GetNaClPermissions(uint32 permission_bits) {
156 // Only allow NaCl plugins to request certain permissions. We don't want
157 // a compromised renderer to be able to start a nacl plugin with e.g. Flash
158 // permissions which may expand the surface area of the sandbox.
159 uint32 masked_bits = permission_bits & ppapi::PERMISSION_DEV;
160 return ppapi::PpapiPermissions::GetForCommandLine(masked_bits);
161}
162
[email protected]646e15552012-04-06 22:01:04163} // namespace
164
[email protected]1d8a3d1f2011-02-19 07:11:52165struct NaClProcessHost::NaClInternal {
[email protected]c8e1ea32013-02-13 02:19:01166 NaClHandle socket_for_renderer;
167 NaClHandle socket_for_sel_ldr;
[email protected]6294dd02013-01-09 17:27:23168
169 NaClInternal()
[email protected]c8e1ea32013-02-13 02:19:01170 : socket_for_renderer(NACL_INVALID_HANDLE),
171 socket_for_sel_ldr(NACL_INVALID_HANDLE) { }
[email protected]1d8a3d1f2011-02-19 07:11:52172};
173
[email protected]646e15552012-04-06 22:01:04174// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52175
[email protected]8510d282012-08-30 19:47:38176NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host)
177 : host_(host) {
178}
179
180bool NaClProcessHost::PluginListener::OnMessageReceived(
181 const IPC::Message& msg) {
182 return host_->OnUntrustedMessageForwarded(msg);
183}
184
[email protected]9da4fed2012-11-01 17:07:55185NaClProcessHost::NaClProcessHost(const GURL& manifest_url,
[email protected]e02ff722012-11-06 03:53:06186 int render_view_id,
[email protected]9da4fed2012-11-01 17:07:55187 uint32 permission_bits,
[email protected]2e1e6f02013-01-11 18:22:56188 bool uses_irt,
[email protected]be524622013-04-23 00:12:19189 bool enable_dyncode_syscalls,
[email protected]7bb5d2c2013-04-29 22:33:49190 bool off_the_record,
191 const base::FilePath& profile_directory)
[email protected]87f35592012-04-08 00:49:16192 : manifest_url_(manifest_url),
[email protected]27b04df2012-12-10 23:43:10193 permissions_(GetNaClPermissions(permission_bits)),
[email protected]a575da52012-03-22 13:08:36194#if defined(OS_WIN)
195 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23196#elif defined(OS_LINUX)
197 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36198#endif
199 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38200#if defined(OS_WIN)
201 debug_exception_handler_requested_(false),
202#endif
[email protected]1d8a3d1f2011-02-19 07:11:52203 internal_(new NaClInternal()),
[email protected]9c009092013-05-01 03:14:09204 weak_factory_(this),
[email protected]d4062452013-04-17 05:08:35205 enable_exception_handling_(true),
[email protected]2c227ff2012-08-11 00:51:32206 enable_debug_stub_(false),
[email protected]2e1e6f02013-01-11 18:22:56207 uses_irt_(uses_irt),
[email protected]be524622013-04-23 00:12:19208 enable_dyncode_syscalls_(enable_dyncode_syscalls),
[email protected]8510d282012-08-30 19:47:38209 off_the_record_(off_the_record),
[email protected]7bb5d2c2013-04-29 22:33:49210 profile_directory_(profile_directory),
[email protected]9c009092013-05-01 03:14:09211 ipc_plugin_listener_(this),
[email protected]e02ff722012-11-06 03:53:06212 render_view_id_(render_view_id) {
[email protected]4967f792012-01-20 22:14:40213 process_.reset(content::BrowserChildProcessHost::Create(
[email protected]f3b357692013-03-22 05:16:13214 PROCESS_TYPE_NACL_LOADER, this));
[email protected]87f35592012-04-08 00:49:16215
216 // Set the display name so the user knows what plugin the process is running.
217 // We aren't on the UI thread so getting the pref locale for language
218 // formatting isn't possible, so IDN will be lost, but this is probably OK
219 // for this use case.
220 process_->SetName(net::FormatUrl(manifest_url_, std::string()));
[email protected]5ca93be2012-03-21 20:04:06221
[email protected]2c227ff2012-08-11 00:51:32222 enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch(
223 switches::kEnableNaClDebug);
[email protected]d032f492009-09-29 00:33:46224}
225
[email protected]fb1277e82009-11-21 20:32:30226NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49227 int exit_code;
[email protected]4967f792012-01-20 22:14:40228 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49229 std::string message =
230 base::StringPrintf("NaCl process exited with status %i (0x%x)",
231 exit_code, exit_code);
232 if (exit_code == 0) {
233 LOG(INFO) << message;
234 } else {
235 LOG(ERROR) << message;
236 }
237
[email protected]c8e1ea32013-02-13 02:19:01238 if (internal_->socket_for_renderer != NACL_INVALID_HANDLE) {
239 if (NaClClose(internal_->socket_for_renderer) != 0) {
240 NOTREACHED() << "NaClClose() failed";
[email protected]909c2402011-05-09 11:39:04241 }
[email protected]c47ec402010-07-29 10:20:49242 }
[email protected]6294dd02013-01-09 17:27:23243
[email protected]c8e1ea32013-02-13 02:19:01244 if (internal_->socket_for_sel_ldr != NACL_INVALID_HANDLE) {
245 if (NaClClose(internal_->socket_for_sel_ldr) != 0) {
246 NOTREACHED() << "NaClClose() failed";
[email protected]909c2402011-05-09 11:39:04247 }
[email protected]c47ec402010-07-29 10:20:49248 }
249
[email protected]909c2402011-05-09 11:39:04250 if (reply_msg_) {
251 // The process failed to launch for some reason.
252 // Don't keep the renderer hanging.
253 reply_msg_->set_reply_error();
254 chrome_render_message_filter_->Send(reply_msg_);
255 }
[email protected]b39c6d92012-01-31 16:38:41256#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36257 if (process_launched_by_broker_) {
258 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06259 }
[email protected]b39c6d92012-01-31 16:38:41260#endif
[email protected]fb1277e82009-11-21 20:32:30261}
262
[email protected]773ebb92011-11-15 19:06:52263// This is called at browser startup.
264// static
265void NaClProcessHost::EarlyStartup() {
266#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
267 // Open the IRT file early to make sure that it isn't replaced out from
268 // under us by autoupdate.
269 NaClBrowser::GetInstance()->EnsureIrtAvailable();
270#endif
[email protected]9addd1c2012-09-15 14:28:24271 CommandLine* cmd = CommandLine::ForCurrentProcess();
272 UMA_HISTOGRAM_BOOLEAN(
273 "NaCl.nacl-gdb",
274 !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
275 UMA_HISTOGRAM_BOOLEAN(
276 "NaCl.nacl-gdb-script",
277 !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
278 UMA_HISTOGRAM_BOOLEAN(
279 "NaCl.enable-nacl-debug",
280 cmd->HasSwitch(switches::kEnableNaClDebug));
[email protected]66f409c2012-10-04 20:59:04281 NaClBrowser::GetInstance()->SetDebugPatterns(
282 cmd->GetSwitchValueASCII(switches::kNaClDebugMask));
[email protected]773ebb92011-11-15 19:06:52283}
284
[email protected]a575da52012-03-22 13:08:36285void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52286 ChromeRenderMessageFilter* chrome_render_message_filter,
[email protected]8f42b4d2012-03-24 14:12:33287 IPC::Message* reply_msg,
288 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36289 chrome_render_message_filter_ = chrome_render_message_filter;
290 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33291 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36292
[email protected]773ebb92011-11-15 19:06:52293 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03294 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]e97990f2012-05-15 22:06:06295 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
296 nacl_browser->EnsureAllResourcesAvailable();
297 if (!nacl_browser->IsOk()) {
[email protected]ec7540f2013-01-17 00:00:35298 LOG(ERROR) << "NaCl process launch failed: could not find all the "
299 "resources needed to launch the process";
[email protected]a575da52012-03-22 13:08:36300 delete this;
301 return;
[email protected]773ebb92011-11-15 19:06:52302 }
303
[email protected]c47ec402010-07-29 10:20:49304 // Rather than creating a socket pair in the renderer, and passing
305 // one side through the browser to sel_ldr, socket pairs are created
306 // in the browser and then passed to the renderer and sel_ldr.
307 //
308 // This is mainly for the benefit of Windows, where sockets cannot
309 // be passed in messages, but are copied via DuplicateHandle().
310 // This means the sandboxed renderer cannot send handles to the
311 // browser process.
312
[email protected]c8e1ea32013-02-13 02:19:01313 NaClHandle pair[2];
[email protected]6294dd02013-01-09 17:27:23314 // Create a connected socket
[email protected]c8e1ea32013-02-13 02:19:01315 if (NaClSocketPair(pair) == -1) {
[email protected]ec7540f2013-01-17 00:00:35316 LOG(ERROR) << "NaCl process launch failed: could not create a socket pair";
[email protected]6294dd02013-01-09 17:27:23317 delete this;
318 return;
[email protected]c47ec402010-07-29 10:20:49319 }
[email protected]6294dd02013-01-09 17:27:23320 internal_->socket_for_renderer = pair[0];
321 internal_->socket_for_sel_ldr = pair[1];
322 SetCloseOnExec(pair[0]);
323 SetCloseOnExec(pair[1]);
[email protected]d032f492009-09-29 00:33:46324
325 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30326 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36327 delete this;
[email protected]d032f492009-09-29 00:33:46328 }
[email protected]d032f492009-09-29 00:33:46329}
[email protected]646e15552012-04-06 22:01:04330
[email protected]5b974952012-04-05 18:18:23331#if defined(OS_WIN)
[email protected]646e15552012-04-06 22:01:04332void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
333 // Set process handle, if it was not set previously.
334 // This is needed when NaCl process is launched with nacl-gdb.
[email protected]82d17502012-05-23 19:12:40335 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
336 switches::kNaClGdb).empty()) {
[email protected]646e15552012-04-06 22:01:04337 base::ProcessHandle process;
[email protected]82d17502012-05-23 19:12:40338 DCHECK(process_->GetData().handle == base::kNullProcessHandle);
[email protected]646e15552012-04-06 22:01:04339 if (base::OpenProcessHandleWithAccess(
340 peer_pid,
341 base::kProcessAccessDuplicateHandle |
342 base::kProcessAccessQueryInformation |
343 base::kProcessAccessWaitForTermination,
344 &process)) {
345 process_->SetHandle(process);
[email protected]09afc2e2012-04-10 17:29:03346 if (!StartWithLaunchedProcess()) {
347 delete this;
348 return;
349 }
[email protected]646e15552012-04-06 22:01:04350 } else {
[email protected]ec7540f2013-01-17 00:00:35351 LOG(ERROR) << "Failed to get process handle";
[email protected]646e15552012-04-06 22:01:04352 }
353 }
[email protected]646e15552012-04-06 22:01:04354}
355#else
356void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
357}
358#endif
359
360#if defined(OS_WIN)
361void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
362 process_launched_by_broker_ = true;
363 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03364 if (!StartWithLaunchedProcess())
365 delete this;
[email protected]646e15552012-04-06 22:01:04366}
367
[email protected]ea6588842012-05-03 05:39:38368void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
369 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
370 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
371 Send(reply);
[email protected]646e15552012-04-06 22:01:04372}
373#endif
374
375// Needed to handle sync messages in OnMessageRecieved.
376bool NaClProcessHost::Send(IPC::Message* msg) {
377 return process_->Send(msg);
378}
379
380#if defined(OS_WIN)
381scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb(
[email protected]650b2d52013-02-10 03:41:45382 const base::FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23383 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46384 CommandLine* cmd_line = new CommandLine(nacl_gdb);
385 // We can't use PrependWrapper because our parameters contain spaces.
386 cmd_line->AppendArg("--eval-command");
[email protected]650b2d52013-02-10 03:41:45387 const base::FilePath::StringType& irt_path =
[email protected]31a665e72012-03-11 12:37:46388 NaClBrowser::GetInstance()->GetIrtFilePath().value();
389 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]650b2d52013-02-10 03:41:45390 base::FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33391 if (!manifest_path.empty()) {
392 cmd_line->AppendArg("--eval-command");
393 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
394 manifest_path.value());
395 }
[email protected]650b2d52013-02-10 03:41:45396 base::FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
[email protected]401b90792012-05-30 11:41:13397 switches::kNaClGdbScript);
398 if (!script.empty()) {
399 cmd_line->AppendArg("--command");
400 cmd_line->AppendArgNative(script.value());
401 }
[email protected]31a665e72012-03-11 12:37:46402 cmd_line->AppendArg("--args");
403 const CommandLine::StringVector& argv = line->argv();
404 for (size_t i = 0; i < argv.size(); i++) {
405 cmd_line->AppendArgNative(argv[i]);
406 }
407 return scoped_ptr<CommandLine>(cmd_line);
408}
[email protected]5b974952012-04-05 18:18:23409#elif defined(OS_LINUX)
[email protected]512d03f2012-06-26 01:06:06410class NaClProcessHost::NaClGdbWatchDelegate
411 : public MessageLoopForIO::Watcher {
[email protected]5b974952012-04-05 18:18:23412 public:
413 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
414 NaClGdbWatchDelegate(int fd_read, int fd_write,
415 const base::Closure& reply)
416 : fd_read_(fd_read),
417 fd_write_(fd_write),
418 reply_(reply) {}
419
[email protected]b94584a2013-02-07 03:02:08420 virtual ~NaClGdbWatchDelegate() {
[email protected]5b974952012-04-05 18:18:23421 if (HANDLE_EINTR(close(fd_read_)) != 0)
422 DLOG(ERROR) << "close(fd_read_) failed";
423 if (HANDLE_EINTR(close(fd_write_)) != 0)
424 DLOG(ERROR) << "close(fd_write_) failed";
425 }
426
427 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
428 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
429
430 private:
431 int fd_read_;
432 int fd_write_;
433 base::Closure reply_;
434};
435
[email protected]512d03f2012-06-26 01:06:06436void NaClProcessHost::NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(
437 int fd) {
[email protected]5b974952012-04-05 18:18:23438 char buf;
439 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
440 LOG(ERROR) << "Failed to sync with nacl-gdb";
441 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
442}
[email protected]5b974952012-04-05 18:18:23443
444bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
445 CommandLine::StringType nacl_gdb =
446 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
447 switches::kNaClGdb);
448 CommandLine::StringVector argv;
449 // We don't support spaces inside arguments in --nacl-gdb switch.
450 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
451 CommandLine cmd_line(argv);
452 cmd_line.AppendArg("--eval-command");
[email protected]650b2d52013-02-10 03:41:45453 const base::FilePath::StringType& irt_path =
[email protected]5b974952012-04-05 18:18:23454 NaClBrowser::GetInstance()->GetIrtFilePath().value();
455 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]650b2d52013-02-10 03:41:45456 base::FilePath manifest_path = GetManifestPath();
[email protected]5b974952012-04-05 18:18:23457 if (!manifest_path.empty()) {
458 cmd_line.AppendArg("--eval-command");
459 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
460 manifest_path.value());
461 }
462 cmd_line.AppendArg("--eval-command");
463 cmd_line.AppendArg("attach " + base::IntToString(pid));
464 int fds[2];
465 if (pipe(fds) != 0)
466 return false;
467 // Tell the debugger to send a byte to the writable end of the pipe.
468 // We use a file descriptor in our process because the debugger will be
469 // typically launched in a separate terminal, and a lot of terminals close all
470 // file descriptors before launching external programs.
471 cmd_line.AppendArg("--eval-command");
472 cmd_line.AppendArg("dump binary value /proc/" +
473 base::IntToString(base::GetCurrentProcId()) +
474 "/fd/" + base::IntToString(fds[1]) + " (char)0");
[email protected]650b2d52013-02-10 03:41:45475 base::FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
[email protected]401b90792012-05-30 11:41:13476 switches::kNaClGdbScript);
477 if (!script.empty()) {
478 cmd_line.AppendArg("--command");
479 cmd_line.AppendArgNative(script.value());
480 }
[email protected]5b974952012-04-05 18:18:23481 // wait on fds[0]
482 // If the debugger crashes before attaching to the NaCl process, the user can
483 // release resources by terminating the NaCl loader in Chrome Task Manager.
484 nacl_gdb_watcher_delegate_.reset(
485 new NaClGdbWatchDelegate(
486 fds[0], fds[1],
487 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
488 weak_factory_.GetWeakPtr())));
489 MessageLoopForIO::current()->WatchFileDescriptor(
490 fds[0],
491 true,
492 MessageLoopForIO::WATCH_READ,
493 &nacl_gdb_watcher_,
494 nacl_gdb_watcher_delegate_.get());
495 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
496}
497
498void NaClProcessHost::OnNaClGdbAttached() {
499 wait_for_nacl_gdb_ = false;
500 nacl_gdb_watcher_.StopWatchingFileDescriptor();
501 nacl_gdb_watcher_delegate_.reset();
502 OnProcessLaunched();
503}
504#endif
505
[email protected]650b2d52013-02-10 03:41:45506base::FilePath NaClProcessHost::GetManifestPath() {
[email protected]1c321ee2012-05-21 03:02:34507 const extensions::Extension* extension = extension_info_map_->extensions()
[email protected]87f35592012-04-08 00:49:16508 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_));
[email protected]885c0e92012-11-13 20:27:42509 if (extension != NULL &&
510 manifest_url_.SchemeIs(extensions::kExtensionScheme)) {
[email protected]87f35592012-04-08 00:49:16511 std::string path = manifest_url_.path();
[email protected]5b974952012-04-05 18:18:23512 TrimString(path, "/", &path); // Remove first slash
513 return extension->path().AppendASCII(path);
514 }
[email protected]650b2d52013-02-10 03:41:45515 return base::FilePath();
[email protected]5b974952012-04-05 18:18:23516}
[email protected]31a665e72012-03-11 12:37:46517
[email protected]fb1277e82009-11-21 20:32:30518bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40519 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]ec7540f2013-01-17 00:00:35520 if (channel_id.empty()) {
521 LOG(ERROR) << "NaCl process launch failed: could not create channel";
[email protected]d032f492009-09-29 00:33:46522 return false;
[email protected]ec7540f2013-01-17 00:00:35523 }
[email protected]d032f492009-09-29 00:33:46524
[email protected]e3fc75a2011-05-05 08:20:42525 CommandLine::StringType nacl_loader_prefix;
526#if defined(OS_POSIX)
527 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
528 switches::kNaClLoaderCmdPrefix);
529#endif // defined(OS_POSIX)
530
[email protected]d032f492009-09-29 00:33:46531 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36532
533#if defined(OS_MACOSX)
534 // The Native Client process needs to be able to allocate a 1GB contiguous
535 // region to use as the client environment's virtual address space. ASLR
536 // (PIE) interferes with this by making it possible that no gap large enough
537 // to accomodate this request will exist in the child process' address
538 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
539 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49540 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36541#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49542 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
543 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36544#else
[email protected]4cb43102011-12-02 20:24:49545 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36546#endif
547
[email protected]650b2d52013-02-10 03:41:45548 base::FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30549 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46550 return false;
551
[email protected]31a665e72012-03-11 12:37:46552#if defined(OS_WIN)
553 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
554 if (RunningOnWOW64()) {
[email protected]650b2d52013-02-10 03:41:45555 base::FilePath module_path;
[email protected]ec7540f2013-01-17 00:00:35556 if (!PathService::Get(base::FILE_MODULE, &module_path)) {
557 LOG(ERROR) << "NaCl process launch failed: could not resolve module";
[email protected]31a665e72012-03-11 12:37:46558 return false;
[email protected]ec7540f2013-01-17 00:00:35559 }
[email protected]31a665e72012-03-11 12:37:46560 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
561 }
562#endif
563
[email protected]33a05af2012-03-02 18:15:51564 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
565 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13566
[email protected]05076ba22010-07-30 05:59:57567 cmd_line->AppendSwitchASCII(switches::kProcessType,
568 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44569 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44570 if (logging::DialogsAreSuppressed())
571 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46572
[email protected]e3fc75a2011-05-05 08:20:42573 if (!nacl_loader_prefix.empty())
574 cmd_line->PrependWrapper(nacl_loader_prefix);
575
[email protected]650b2d52013-02-10 03:41:45576 base::FilePath nacl_gdb =
577 CommandLine::ForCurrentProcess()->GetSwitchValuePath(switches::kNaClGdb);
[email protected]31a665e72012-03-11 12:37:46578 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23579#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46580 cmd_line->AppendSwitch(switches::kNoSandbox);
581 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]646e15552012-04-06 22:01:04582 GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46583 // We can't use process_->Launch() because OnProcessLaunched will be called
584 // with process_->GetData().handle filled by handle of gdb process. This
585 // handle will be used to duplicate handles for NaCl process and as
586 // a result NaCl process will not be able to use them.
587 //
588 // So we don't fill process_->GetData().handle and wait for
589 // OnChannelConnected to get handle of NaCl process from its pid. Then we
590 // call OnProcessLaunched.
591 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23592#elif defined(OS_LINUX)
593 wait_for_nacl_gdb_ = true;
594#endif
[email protected]31a665e72012-03-11 12:37:46595 }
596
[email protected]103607e2010-02-01 18:57:09597 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46598#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52599 if (RunningOnWOW64()) {
[email protected]ec7540f2013-01-17 00:00:35600 if (!NaClBrokerService::GetInstance()->LaunchLoader(
601 weak_factory_.GetWeakPtr(), channel_id)) {
602 LOG(ERROR) << "NaCl process launch failed: broker service did not launch "
603 "process";
604 return false;
605 }
[email protected]4bdde602010-06-16 03:17:35606 } else {
[email protected]34f48682013-03-20 00:30:18607 process_->Launch(new NaClSandboxedProcessLauncherDelegate,
608 cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35609 }
[email protected]103607e2010-02-01 18:57:09610#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40611 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20612 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51613 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09614#endif
[email protected]d032f492009-09-29 00:33:46615
[email protected]fb1277e82009-11-21 20:32:30616 return true;
[email protected]d032f492009-09-29 00:33:46617}
618
[email protected]646e15552012-04-06 22:01:04619bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
620 bool handled = true;
621 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
622 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
623 OnQueryKnownToValidate)
624 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
625 OnSetKnownToValidate)
[email protected]ea6588842012-05-03 05:39:38626#if defined(OS_WIN)
627 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
628 OnAttachDebugExceptionHandler)
629#endif
[email protected]17b5a8172012-06-22 21:09:09630 IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated,
631 OnPpapiChannelCreated)
[email protected]646e15552012-04-06 22:01:04632 IPC_MESSAGE_UNHANDLED(handled = false)
633 IPC_END_MESSAGE_MAP()
634 return handled;
[email protected]103607e2010-02-01 18:57:09635}
636
[email protected]773ebb92011-11-15 19:06:52637void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03638 if (!StartWithLaunchedProcess())
639 delete this;
[email protected]773ebb92011-11-15 19:06:52640}
641
[email protected]e97990f2012-05-15 22:06:06642// Called when the NaClBrowser singleton has been fully initialized.
643void NaClProcessHost::OnResourcesReady() {
[email protected]773ebb92011-11-15 19:06:52644 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]ec7540f2013-01-17 00:00:35645 if (!nacl_browser->IsReady()) {
646 LOG(ERROR) << "NaCl process launch failed: could not acquire shared "
647 "resources needed by NaCl";
648 delete this;
649 } else if (!SendStart()) {
[email protected]338466a82011-05-03 04:27:43650 delete this;
651 }
652}
653
[email protected]17b5a8172012-06-22 21:09:09654bool NaClProcessHost::ReplyToRenderer(
655 const IPC::ChannelHandle& channel_handle) {
[email protected]6294dd02013-01-09 17:27:23656 nacl::FileDescriptor handle_for_renderer;
[email protected]c47ec402010-07-29 10:20:49657#if defined(OS_WIN)
[email protected]6294dd02013-01-09 17:27:23658 // Copy the handle into the renderer process.
659 HANDLE handle_in_renderer;
660 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
661 reinterpret_cast<HANDLE>(
662 internal_->socket_for_renderer),
663 chrome_render_message_filter_->peer_handle(),
664 &handle_in_renderer,
665 0, // Unused given DUPLICATE_SAME_ACCESS.
666 FALSE,
667 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]ec7540f2013-01-17 00:00:35668 LOG(ERROR) << "DuplicateHandle() failed";
[email protected]6294dd02013-01-09 17:27:23669 return false;
[email protected]c47ec402010-07-29 10:20:49670 }
[email protected]6294dd02013-01-09 17:27:23671 handle_for_renderer = reinterpret_cast<nacl::FileDescriptor>(
672 handle_in_renderer);
673#else
674 // No need to dup the imc_handle - we don't pass it anywhere else so
675 // it cannot be closed.
676 nacl::FileDescriptor imc_handle;
677 imc_handle.fd = internal_->socket_for_renderer;
678 imc_handle.auto_close = true;
679 handle_for_renderer = imc_handle;
680#endif
[email protected]c47ec402010-07-29 10:20:49681
682#if defined(OS_WIN)
[email protected]e4f6eb0232012-04-17 00:47:50683 // If we are on 64-bit Windows, the NaCl process's sandbox is
684 // managed by a different process from the renderer's sandbox. We
685 // need to inform the renderer's sandbox about the NaCl process so
686 // that the renderer can send handles to the NaCl process using
687 // BrokerDuplicateHandle().
688 if (RunningOnWOW64()) {
[email protected]171fa98d2012-04-23 21:34:01689 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]ec7540f2013-01-17 00:00:35690 LOG(ERROR) << "Failed to add NaCl process PID";
[email protected]e4f6eb0232012-04-17 00:47:50691 return false;
692 }
693 }
[email protected]fb1277e82009-11-21 20:32:30694#endif
695
[email protected]108fd342013-01-04 20:46:54696 const ChildProcessData& data = process_->GetData();
[email protected]2ccf45c2011-08-19 23:35:50697 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]6294dd02013-01-09 17:27:23698 reply_msg_, handle_for_renderer,
[email protected]108fd342013-01-04 20:46:54699 channel_handle, base::GetProcId(data.handle), data.id);
[email protected]92d56412011-03-24 20:53:52700 chrome_render_message_filter_->Send(reply_msg_);
701 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30702 reply_msg_ = NULL;
[email protected]c8e1ea32013-02-13 02:19:01703 internal_->socket_for_renderer = NACL_INVALID_HANDLE;
[email protected]00d99542012-04-17 22:48:02704 return true;
705}
[email protected]fb1277e82009-11-21 20:32:30706
[email protected]d571c032012-09-14 12:39:18707// TCP port we chose for NaCl debug stub. It can be any other number.
708static const int kDebugStubPort = 4014;
709
710#if defined(OS_POSIX)
711SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
[email protected]13cad0b2012-12-07 17:57:34712 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
713 SocketDescriptor s;
714 // We allocate currently unused TCP port for debug stub tests. The port
715 // number is passed to the test via debug stub port listener.
716 if (nacl_browser->HasGdbDebugStubPortListener()) {
717 int port;
718 s = net::TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port);
719 if (s != net::TCPListenSocket::kInvalidSocket) {
720 nacl_browser->FireGdbDebugStubPortOpened(port);
721 }
722 } else {
723 s = net::TCPListenSocket::CreateAndBind("127.0.0.1", kDebugStubPort);
724 }
725 if (s == net::TCPListenSocket::kInvalidSocket) {
726 LOG(ERROR) << "failed to open socket for debug stub";
727 return net::TCPListenSocket::kInvalidSocket;
728 }
[email protected]d571c032012-09-14 12:39:18729 if (listen(s, 1)) {
730 LOG(ERROR) << "listen() failed on debug stub socket";
[email protected]13cad0b2012-12-07 17:57:34731 if (HANDLE_EINTR(close(s)) < 0)
732 PLOG(ERROR) << "failed to close debug stub socket";
[email protected]d571c032012-09-14 12:39:18733 return net::TCPListenSocket::kInvalidSocket;
734 }
735 return s;
736}
737#endif
738
[email protected]00d99542012-04-17 22:48:02739bool NaClProcessHost::StartNaClExecution() {
740 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
741
742 nacl::NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12743 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
744 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]00d99542012-04-17 22:48:02745 params.version = chrome::VersionInfo().CreateVersionString();
746 params.enable_exception_handling = enable_exception_handling_;
[email protected]66f409c2012-10-04 20:59:04747 params.enable_debug_stub = enable_debug_stub_ &&
748 NaClBrowser::GetInstance()->URLMatchesDebugPatterns(manifest_url_);
[email protected]7522077b2013-01-17 23:39:40749 // Enable PPAPI proxy channel creation only for renderer processes.
750 params.enable_ipc_proxy = enable_ppapi_proxy();
[email protected]2e1e6f02013-01-11 18:22:56751 params.uses_irt = uses_irt_;
[email protected]be524622013-04-23 00:12:19752 params.enable_dyncode_syscalls = enable_dyncode_syscalls_;
[email protected]00d99542012-04-17 22:48:02753
754 const ChildProcessData& data = process_->GetData();
[email protected]6294dd02013-01-09 17:27:23755 if (!ShareHandleToSelLdr(data.handle,
756 internal_->socket_for_sel_ldr, true,
757 &params.handles)) {
758 return false;
[email protected]773ebb92011-11-15 19:06:52759 }
760
[email protected]2e1e6f02013-01-11 18:22:56761 if (params.uses_irt) {
762 base::PlatformFile irt_file = nacl_browser->IrtFile();
763 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
764
765 // Send over the IRT file handle. We don't close our own copy!
766 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
767 return false;
768 }
[email protected]c47ec402010-07-29 10:20:49769
[email protected]ab88d1542011-11-18 22:52:00770#if defined(OS_MACOSX)
771 // For dynamic loading support, NaCl requires a file descriptor that
772 // was created in /tmp, since those created with shm_open() are not
773 // mappable with PROT_EXEC. Rather than requiring an extra IPC
774 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25775 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31776 base::SharedMemoryCreateOptions options;
777 options.size = 1;
778 options.executable = true;
779 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03780 DLOG(ERROR) << "Failed to allocate memory buffer";
781 return false;
[email protected]2c68bf032010-11-11 23:16:30782 }
[email protected]cbbe7842011-11-17 22:01:25783 nacl::FileDescriptor memory_fd;
784 memory_fd.fd = dup(memory_buffer.handle().fd);
785 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03786 DLOG(ERROR) << "Failed to dup() a file descriptor";
787 return false;
[email protected]cbbe7842011-11-17 22:01:25788 }
789 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02790 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30791#endif
792
[email protected]d571c032012-09-14 12:39:18793#if defined(OS_POSIX)
[email protected]ed637bb2012-10-15 17:36:05794 if (params.enable_debug_stub) {
[email protected]d571c032012-09-14 12:39:18795 SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
796 if (server_bound_socket != net::TCPListenSocket::kInvalidSocket) {
797 params.debug_stub_server_bound_socket =
798 nacl::FileDescriptor(server_bound_socket, true);
799 }
800 }
801#endif
802
[email protected]ea6588842012-05-03 05:39:38803 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41804
[email protected]c8e1ea32013-02-13 02:19:01805 internal_->socket_for_sel_ldr = NACL_INVALID_HANDLE;
[email protected]09afc2e2012-04-10 17:29:03806 return true;
807}
808
[email protected]00d99542012-04-17 22:48:02809bool NaClProcessHost::SendStart() {
[email protected]7522077b2013-01-17 23:39:40810 if (!enable_ppapi_proxy()) {
[email protected]17b5a8172012-06-22 21:09:09811 if (!ReplyToRenderer(IPC::ChannelHandle()))
812 return false;
813 }
814 return StartNaClExecution();
815}
816
[email protected]8510d282012-08-30 19:47:38817// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
818// received or PpapiHostMsg_ChannelCreated is forwarded by our plugin
819// listener.
[email protected]17b5a8172012-06-22 21:09:09820void NaClProcessHost::OnPpapiChannelCreated(
821 const IPC::ChannelHandle& channel_handle) {
[email protected]7522077b2013-01-17 23:39:40822 // Only renderer processes should create a channel.
823 DCHECK(enable_ppapi_proxy());
[email protected]8510d282012-08-30 19:47:38824 // If the proxy channel is null, this must be the initial NaCl-Browser IPC
825 // channel.
826 if (!ipc_proxy_channel_.get()) {
[email protected]f3b357692013-03-22 05:16:13827 DCHECK_EQ(PROCESS_TYPE_NACL_LOADER, process_->GetData().process_type);
[email protected]9e141212013-01-21 15:21:56828
[email protected]8510d282012-08-30 19:47:38829 ipc_proxy_channel_.reset(
830 new IPC::ChannelProxy(channel_handle,
831 IPC::Channel::MODE_CLIENT,
832 &ipc_plugin_listener_,
833 base::MessageLoopProxy::current()));
[email protected]0c7193742012-11-07 19:05:03834 // Create the browser ppapi host and enable PPAPI message dispatching to the
835 // browser process.
[email protected]adf7cd52012-12-04 19:37:46836 ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess(
[email protected]9e141212013-01-21 15:21:56837 ipc_proxy_channel_.get(), // sender
[email protected]0c7193742012-11-07 19:05:03838 permissions_,
839 process_->GetData().handle,
[email protected]b4b53d182012-09-12 02:56:55840 ipc_proxy_channel_.get(),
[email protected]e02ff722012-11-06 03:53:06841 chrome_render_message_filter_->GetHostResolver(),
842 chrome_render_message_filter_->render_process_id(),
[email protected]7bb5d2c2013-04-29 22:33:49843 render_view_id_,
844 profile_directory_));
[email protected]adf7cd52012-12-04 19:37:46845
[email protected]ac07ec52013-04-22 17:32:45846 ppapi::PpapiNaClChannelArgs args;
847 args.off_the_record = chrome_render_message_filter_->off_the_record();
848 args.permissions = permissions_;
849 CommandLine* cmdline = CommandLine::ForCurrentProcess();
850 DCHECK(cmdline);
851 std::string flag_whitelist[] = {switches::kV, switches::kVModule};
852 for (size_t i = 0; i < arraysize(flag_whitelist); ++i) {
853 std::string value = cmdline->GetSwitchValueASCII(flag_whitelist[i]);
854 if (!value.empty()) {
855 args.switch_names.push_back(flag_whitelist[i]);
856 args.switch_values.push_back(value);
857 }
858 }
859
[email protected]4b30e7d2013-04-24 23:52:14860 ppapi_host_->GetPpapiHost()->AddHostFactoryFilter(
861 scoped_ptr<ppapi::host::HostFactory>(
862 new chrome::ChromeBrowserPepperHostFactory(ppapi_host_.get())));
863
[email protected]8510d282012-08-30 19:47:38864 // Send a message to create the NaCl-Renderer channel. The handle is just
865 // a place holder.
866 ipc_proxy_channel_->Send(
867 new PpapiMsg_CreateNaClChannel(
868 chrome_render_message_filter_->render_process_id(),
[email protected]ac07ec52013-04-22 17:32:45869 args,
[email protected]8510d282012-08-30 19:47:38870 SerializedHandle(SerializedHandle::CHANNEL_HANDLE,
871 IPC::InvalidPlatformFileForTransit())));
872 } else if (reply_msg_) {
873 // Otherwise, this must be a renderer channel.
874 ReplyToRenderer(channel_handle);
875 } else {
876 // Attempt to open more than 1 renderer channel is not supported.
877 // Shut down the NaCl process.
878 process_->GetHost()->ForceShutdown();
879 }
880}
881
882bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) {
883 // Handle messages that have been forwarded from our PluginListener.
884 // These messages come from untrusted code so should be handled with care.
885 bool handled = true;
886 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
887 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
888 OnPpapiChannelCreated)
889 IPC_MESSAGE_UNHANDLED(handled = false)
890 IPC_END_MESSAGE_MAP()
891 return handled;
[email protected]00d99542012-04-17 22:48:02892}
893
[email protected]09afc2e2012-04-10 17:29:03894bool NaClProcessHost::StartWithLaunchedProcess() {
895#if defined(OS_LINUX)
896 if (wait_for_nacl_gdb_) {
897 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
898 // We will be called with wait_for_nacl_gdb_ = false once debugger is
899 // attached to the program.
900 return true;
901 }
902 DLOG(ERROR) << "Failed to launch debugger";
903 // Continue execution without debugger.
904 }
905#endif
906
907 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03908
[email protected]e97990f2012-05-15 22:06:06909 if (nacl_browser->IsReady()) {
910 return SendStart();
911 } else if (nacl_browser->IsOk()) {
912 nacl_browser->WaitForResources(
913 base::Bind(&NaClProcessHost::OnResourcesReady,
914 weak_factory_.GetWeakPtr()));
915 return true;
916 } else {
[email protected]ec7540f2013-01-17 00:00:35917 LOG(ERROR) << "NaCl process failed to launch: previously failed to acquire "
918 "shared resources";
[email protected]e97990f2012-05-15 22:06:06919 return false;
920 }
[email protected]d032f492009-09-29 00:33:46921}
922
[email protected]4a0141b2012-03-27 01:15:30923void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
924 bool* result) {
[email protected]7cfaf982012-05-09 18:37:47925 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52926 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30927}
928
929void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]57473ebc2012-05-30 07:44:52930 NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30931}
[email protected]ea6588842012-05-03 05:39:38932
933#if defined(OS_WIN)
934void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
935 IPC::Message* reply_msg) {
936 if (!AttachDebugExceptionHandler(info, reply_msg)) {
937 // Send failure message.
938 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
939 false);
940 Send(reply_msg);
941 }
942}
943
944bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
945 IPC::Message* reply_msg) {
[email protected]2c227ff2012-08-11 00:51:32946 if (!enable_exception_handling_ && !enable_debug_stub_) {
[email protected]ea6588842012-05-03 05:39:38947 DLOG(ERROR) <<
[email protected]2c227ff2012-08-11 00:51:32948 "Debug exception handler requested by NaCl process when not enabled";
[email protected]ea6588842012-05-03 05:39:38949 return false;
950 }
951 if (debug_exception_handler_requested_) {
952 // The NaCl process should not request this multiple times.
953 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
954 return false;
955 }
956 debug_exception_handler_requested_ = true;
957
958 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
959 base::win::ScopedHandle process_handle;
960 // We cannot use process_->GetData().handle because it does not have
961 // the necessary access rights. We open the new handle here rather
962 // than in the NaCl broker process in case the NaCl loader process
963 // dies before the NaCl broker process receives the message we send.
964 // The debug exception handler uses DebugActiveProcess() to attach,
965 // but this takes a PID. We need to prevent the NaCl loader's PID
966 // from being reused before DebugActiveProcess() is called, and
967 // holding a process handle open achieves this.
968 if (!base::OpenProcessHandleWithAccess(
969 nacl_pid,
970 base::kProcessAccessQueryInformation |
971 base::kProcessAccessSuspendResume |
972 base::kProcessAccessTerminate |
973 base::kProcessAccessVMOperation |
974 base::kProcessAccessVMRead |
975 base::kProcessAccessVMWrite |
[email protected]29172322013-01-10 17:35:16976 base::kProcessAccessDuplicateHandle |
[email protected]ea6588842012-05-03 05:39:38977 base::kProcessAccessWaitForTermination,
978 process_handle.Receive())) {
979 LOG(ERROR) << "Failed to get process handle";
980 return false;
981 }
982
983 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
984 // If the NaCl loader is 64-bit, the process running its debug
985 // exception handler must be 64-bit too, so we use the 64-bit NaCl
986 // broker process for this. Otherwise, on a 32-bit system, we use
987 // the 32-bit browser process to run the debug exception handler.
988 if (RunningOnWOW64()) {
989 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:48990 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:38991 } else {
992 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:48993 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:38994 base::MessageLoopProxy::current(),
995 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
996 weak_factory_.GetWeakPtr()));
997 return true;
998 }
999}
1000#endif