blob: 530ebbb50318b7ab873e46560581e670801946ad [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]04284252013-06-11 22:30:147#include <algorithm>
[email protected]a82af392012-02-24 04:40:208#include <string>
9#include <vector>
[email protected]d032f492009-09-29 00:33:4610
[email protected]ac07ec52013-04-22 17:32:4511#include "base/base_switches.h"
[email protected]30c1eea2011-10-17 18:40:3012#include "base/bind.h"
[email protected]103607e2010-02-01 18:57:0913#include "base/command_line.h"
[email protected]6d7f55f2013-05-14 10:12:5614#include "base/file_util.h"
[email protected]467a106e2013-07-18 12:07:1915#include "base/message_loop/message_loop.h"
[email protected]9addd1c2012-09-15 14:28:2416#include "base/metrics/histogram.h"
[email protected]338466a82011-05-03 04:27:4317#include "base/path_service.h"
[email protected]b345c482013-08-30 18:00:3918#include "base/process/launch.h"
[email protected]d09a4ce1c2013-07-24 17:37:0219#include "base/process/process_iterator.h"
[email protected]25a10e232013-08-23 00:40:0820#include "base/rand_util.h"
[email protected]3ea1b182013-02-08 22:38:4121#include "base/strings/string_number_conversions.h"
[email protected]1988e1c2013-02-28 20:27:4222#include "base/strings/string_split.h"
[email protected]f9b294362013-06-10 20:22:3123#include "base/strings/string_util.h"
24#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1825#include "base/strings/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3726#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2027#include "build/build_config.h"
[email protected]01e59752013-06-18 00:17:3528#include "chrome/browser/nacl_host/nacl_host_message_filter.h"
[email protected]e17f5ab2013-08-19 22:27:0729#include "chrome/common/chrome_switches.h"
[email protected]ef42ee82013-11-01 05:26:1330#include "components/nacl/browser/nacl_browser.h"
[email protected]0acf1d42013-07-22 14:54:5831#include "components/nacl/common/nacl_cmd_line.h"
[email protected]512b5bd2013-07-23 20:24:0032#include "components/nacl/common/nacl_host_messages.h"
33#include "components/nacl/common/nacl_messages.h"
[email protected]d5d383252013-07-04 14:44:3234#include "components/nacl/common/nacl_process_type.h"
[email protected]cbb22eb2013-06-24 23:53:1035#include "components/nacl/common/nacl_switches.h"
[email protected]4967f792012-01-20 22:14:4036#include "content/public/browser/browser_child_process_host.h"
[email protected]0c7193742012-11-07 19:05:0337#include "content/public/browser/browser_ppapi_host.h"
[email protected]4967f792012-01-20 22:14:4038#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4439#include "content/public/common/child_process_host.h"
[email protected]67e76872013-07-08 02:19:3440#include "content/public/common/content_switches.h"
[email protected]9e141212013-01-21 15:21:5641#include "content/public/common/process_type.h"
[email protected]8510d282012-08-30 19:47:3842#include "ipc/ipc_channel.h"
[email protected]d032f492009-09-29 00:33:4643#include "ipc/ipc_switches.h"
[email protected]c8e1ea32013-02-13 02:19:0144#include "native_client/src/shared/imc/nacl_imc_c.h"
[email protected]87f35592012-04-08 00:49:1645#include "net/base/net_util.h"
[email protected]1946d9e2013-04-09 01:43:3746#include "net/socket/tcp_listen_socket.h"
[email protected]6fff31c22013-07-09 02:08:1947#include "ppapi/host/host_factory.h"
[email protected]4b30e7d2013-04-24 23:52:1448#include "ppapi/host/ppapi_host.h"
[email protected]8510d282012-08-30 19:47:3849#include "ppapi/proxy/ppapi_messages.h"
[email protected]ac07ec52013-04-22 17:32:4550#include "ppapi/shared_impl/ppapi_nacl_channel_args.h"
[email protected]d032f492009-09-29 00:33:4651
[email protected]d032f492009-09-29 00:33:4652#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2053#include <fcntl.h>
54
[email protected]d032f492009-09-29 00:33:4655#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3556#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2057#include <windows.h>
58
[email protected]885c0e92012-11-13 20:27:4259#include "base/threading/thread.h"
[email protected]4c65fb632012-04-27 00:42:2560#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3561#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]512b5bd2013-07-23 20:24:0062#include "components/nacl/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5063#include "content/public/common/sandbox_init.h"
[email protected]34f48682013-03-20 00:30:1864#include "content/public/common/sandboxed_process_launcher_delegate.h"
[email protected]d032f492009-09-29 00:33:4665#endif
66
[email protected]631bb742011-11-02 11:29:3967using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4068using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4469using content::ChildProcessHost;
[email protected]8510d282012-08-30 19:47:3870using ppapi::proxy::SerializedHandle;
[email protected]631bb742011-11-02 11:29:3971
[email protected]25a10e232013-08-23 00:40:0872#if defined(OS_WIN)
73
74namespace {
75
76// Looks for the largest contiguous unallocated region of address
77// space and returns it via |*out_addr| and |*out_size|.
78void FindAddressSpace(base::ProcessHandle process,
79 char** out_addr, size_t* out_size) {
80 *out_addr = NULL;
81 *out_size = 0;
82 char* addr = 0;
83 while (true) {
84 MEMORY_BASIC_INFORMATION info;
85 size_t result = VirtualQueryEx(process, static_cast<void*>(addr),
86 &info, sizeof(info));
87 if (result < sizeof(info))
88 break;
89 if (info.State == MEM_FREE && info.RegionSize > *out_size) {
90 *out_addr = addr;
91 *out_size = info.RegionSize;
92 }
93 addr += info.RegionSize;
94 }
95}
96
97} // namespace
98
99namespace nacl {
100
101// Allocates |size| bytes of address space in the given process at a
102// randomised address.
103void* AllocateAddressSpaceASLR(base::ProcessHandle process, size_t size) {
104 char* addr;
105 size_t avail_size;
106 FindAddressSpace(process, &addr, &avail_size);
107 if (avail_size < size)
108 return NULL;
109 size_t offset = base::RandGenerator(avail_size - size);
110 const int kPageSize = 0x10000;
111 void* request_addr =
112 reinterpret_cast<void*>(reinterpret_cast<uint64>(addr + offset)
113 & ~(kPageSize - 1));
114 return VirtualAllocEx(process, request_addr, size,
115 MEM_RESERVE, PAGE_NOACCESS);
116}
117
118} // namespace nacl
119
120#endif // defined(OS_WIN)
121
[email protected]646e15552012-04-06 22:01:04122namespace {
123
124#if defined(OS_WIN)
125bool RunningOnWOW64() {
126 return (base::win::OSInfo::GetInstance()->wow64_status() ==
127 base::win::OSInfo::WOW64_ENABLED);
128}
[email protected]34f48682013-03-20 00:30:18129
130// NOTE: changes to this class need to be reviewed by the security team.
131class NaClSandboxedProcessLauncherDelegate
132 : public content::SandboxedProcessLauncherDelegate {
133 public:
134 NaClSandboxedProcessLauncherDelegate() {}
135 virtual ~NaClSandboxedProcessLauncherDelegate() {}
136
137 virtual void PostSpawnTarget(base::ProcessHandle process) {
[email protected]34f48682013-03-20 00:30:18138 // For Native Client sel_ldr processes on 32-bit Windows, reserve 1 GB of
139 // address space to prevent later failure due to address space fragmentation
140 // from .dll loading. The NaCl process will attempt to locate this space by
141 // scanning the address space using VirtualQuery.
142 // TODO(bbudge) Handle the --no-sandbox case.
143 // http://code.google.com/p/nativeclient/issues/detail?id=2131
[email protected]25a10e232013-08-23 00:40:08144 const SIZE_T kNaClSandboxSize = 1 << 30;
145 if (!nacl::AllocateAddressSpaceASLR(process, kNaClSandboxSize)) {
[email protected]34f48682013-03-20 00:30:18146 DLOG(WARNING) << "Failed to reserve address space for Native Client";
147 }
[email protected]34f48682013-03-20 00:30:18148 }
149};
150
151#endif // OS_WIN
[email protected]646e15552012-04-06 22:01:04152
[email protected]c8e1ea32013-02-13 02:19:01153void SetCloseOnExec(NaClHandle fd) {
[email protected]646e15552012-04-06 22:01:04154#if defined(OS_POSIX)
155 int flags = fcntl(fd, F_GETFD);
156 CHECK_NE(flags, -1);
157 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
158 CHECK_EQ(rc, 0);
159#endif
160}
161
[email protected]00d99542012-04-17 22:48:02162bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:04163 base::ProcessHandle processh,
[email protected]c8e1ea32013-02-13 02:19:01164 NaClHandle sourceh,
[email protected]646e15552012-04-06 22:01:04165 bool close_source,
166 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
167#if defined(OS_WIN)
168 HANDLE channel;
169 int flags = DUPLICATE_SAME_ACCESS;
170 if (close_source)
171 flags |= DUPLICATE_CLOSE_SOURCE;
172 if (!DuplicateHandle(GetCurrentProcess(),
173 reinterpret_cast<HANDLE>(sourceh),
174 processh,
175 &channel,
176 0, // Unused given DUPLICATE_SAME_ACCESS.
177 FALSE,
178 flags)) {
[email protected]ec7540f2013-01-17 00:00:35179 LOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:04180 return false;
181 }
182 handles_for_sel_ldr->push_back(
183 reinterpret_cast<nacl::FileDescriptor>(channel));
184#else
185 nacl::FileDescriptor channel;
186 channel.fd = sourceh;
187 channel.auto_close = close_source;
188 handles_for_sel_ldr->push_back(channel);
189#endif
190 return true;
191}
192
[email protected]27b04df2012-12-10 23:43:10193ppapi::PpapiPermissions GetNaClPermissions(uint32 permission_bits) {
194 // Only allow NaCl plugins to request certain permissions. We don't want
195 // a compromised renderer to be able to start a nacl plugin with e.g. Flash
196 // permissions which may expand the surface area of the sandbox.
197 uint32 masked_bits = permission_bits & ppapi::PERMISSION_DEV;
198 return ppapi::PpapiPermissions::GetForCommandLine(masked_bits);
199}
200
[email protected]646e15552012-04-06 22:01:04201} // namespace
202
[email protected]1d8a3d1f2011-02-19 07:11:52203struct NaClProcessHost::NaClInternal {
[email protected]c8e1ea32013-02-13 02:19:01204 NaClHandle socket_for_renderer;
205 NaClHandle socket_for_sel_ldr;
[email protected]6294dd02013-01-09 17:27:23206
207 NaClInternal()
[email protected]c8e1ea32013-02-13 02:19:01208 : socket_for_renderer(NACL_INVALID_HANDLE),
209 socket_for_sel_ldr(NACL_INVALID_HANDLE) { }
[email protected]1d8a3d1f2011-02-19 07:11:52210};
211
[email protected]646e15552012-04-06 22:01:04212// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52213
[email protected]8510d282012-08-30 19:47:38214NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host)
215 : host_(host) {
216}
217
218bool NaClProcessHost::PluginListener::OnMessageReceived(
219 const IPC::Message& msg) {
220 return host_->OnUntrustedMessageForwarded(msg);
221}
222
[email protected]9da4fed2012-11-01 17:07:55223NaClProcessHost::NaClProcessHost(const GURL& manifest_url,
[email protected]e02ff722012-11-06 03:53:06224 int render_view_id,
[email protected]9da4fed2012-11-01 17:07:55225 uint32 permission_bits,
[email protected]2e1e6f02013-01-11 18:22:56226 bool uses_irt,
[email protected]be524622013-04-23 00:12:19227 bool enable_dyncode_syscalls,
[email protected]286c0952013-06-13 20:29:26228 bool enable_exception_handling,
[email protected]e17f5ab2013-08-19 22:27:07229 bool enable_crash_throttling,
[email protected]7bb5d2c2013-04-29 22:33:49230 bool off_the_record,
231 const base::FilePath& profile_directory)
[email protected]87f35592012-04-08 00:49:16232 : manifest_url_(manifest_url),
[email protected]27b04df2012-12-10 23:43:10233 permissions_(GetNaClPermissions(permission_bits)),
[email protected]a575da52012-03-22 13:08:36234#if defined(OS_WIN)
235 process_launched_by_broker_(false),
236#endif
237 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38238#if defined(OS_WIN)
239 debug_exception_handler_requested_(false),
240#endif
[email protected]1d8a3d1f2011-02-19 07:11:52241 internal_(new NaClInternal()),
[email protected]9c009092013-05-01 03:14:09242 weak_factory_(this),
[email protected]84b8e8962013-06-12 22:24:06243 uses_irt_(uses_irt),
[email protected]286c0952013-06-13 20:29:26244 enable_debug_stub_(false),
[email protected]be524622013-04-23 00:12:19245 enable_dyncode_syscalls_(enable_dyncode_syscalls),
[email protected]286c0952013-06-13 20:29:26246 enable_exception_handling_(enable_exception_handling),
[email protected]e17f5ab2013-08-19 22:27:07247 enable_crash_throttling_(enable_crash_throttling),
[email protected]8510d282012-08-30 19:47:38248 off_the_record_(off_the_record),
[email protected]7bb5d2c2013-04-29 22:33:49249 profile_directory_(profile_directory),
[email protected]9c009092013-05-01 03:14:09250 ipc_plugin_listener_(this),
[email protected]e02ff722012-11-06 03:53:06251 render_view_id_(render_view_id) {
[email protected]4967f792012-01-20 22:14:40252 process_.reset(content::BrowserChildProcessHost::Create(
[email protected]f3b357692013-03-22 05:16:13253 PROCESS_TYPE_NACL_LOADER, this));
[email protected]87f35592012-04-08 00:49:16254
255 // Set the display name so the user knows what plugin the process is running.
256 // We aren't on the UI thread so getting the pref locale for language
257 // formatting isn't possible, so IDN will be lost, but this is probably OK
258 // for this use case.
259 process_->SetName(net::FormatUrl(manifest_url_, std::string()));
[email protected]5ca93be2012-03-21 20:04:06260
[email protected]2c227ff2012-08-11 00:51:32261 enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch(
262 switches::kEnableNaClDebug);
[email protected]d032f492009-09-29 00:33:46263}
264
[email protected]fb1277e82009-11-21 20:32:30265NaClProcessHost::~NaClProcessHost() {
[email protected]6e70b1982013-08-23 21:38:20266 // Report exit status only if the process was successfully started.
267 if (process_->GetData().handle != base::kNullProcessHandle) {
268 int exit_code = 0;
[email protected]547603d2013-08-27 17:59:19269 process_->GetTerminationStatus(false /* known_dead */, &exit_code);
[email protected]6e70b1982013-08-23 21:38:20270 std::string message =
271 base::StringPrintf("NaCl process exited with status %i (0x%x)",
[email protected]547603d2013-08-27 17:59:19272 exit_code, exit_code);
[email protected]6e70b1982013-08-23 21:38:20273 if (exit_code == 0) {
274 LOG(INFO) << message;
275 } else {
276 LOG(ERROR) << message;
277 }
[email protected]4cb43102011-12-02 20:24:49278 }
279
[email protected]c8e1ea32013-02-13 02:19:01280 if (internal_->socket_for_renderer != NACL_INVALID_HANDLE) {
281 if (NaClClose(internal_->socket_for_renderer) != 0) {
282 NOTREACHED() << "NaClClose() failed";
[email protected]909c2402011-05-09 11:39:04283 }
[email protected]c47ec402010-07-29 10:20:49284 }
[email protected]6294dd02013-01-09 17:27:23285
[email protected]c8e1ea32013-02-13 02:19:01286 if (internal_->socket_for_sel_ldr != NACL_INVALID_HANDLE) {
287 if (NaClClose(internal_->socket_for_sel_ldr) != 0) {
288 NOTREACHED() << "NaClClose() failed";
[email protected]909c2402011-05-09 11:39:04289 }
[email protected]c47ec402010-07-29 10:20:49290 }
291
[email protected]909c2402011-05-09 11:39:04292 if (reply_msg_) {
293 // The process failed to launch for some reason.
294 // Don't keep the renderer hanging.
295 reply_msg_->set_reply_error();
[email protected]01e59752013-06-18 00:17:35296 nacl_host_message_filter_->Send(reply_msg_);
[email protected]909c2402011-05-09 11:39:04297 }
[email protected]b39c6d92012-01-31 16:38:41298#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36299 if (process_launched_by_broker_) {
300 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06301 }
[email protected]b39c6d92012-01-31 16:38:41302#endif
[email protected]fb1277e82009-11-21 20:32:30303}
304
[email protected]e17f5ab2013-08-19 22:27:07305void NaClProcessHost::OnProcessCrashed(int exit_status) {
306 if (enable_crash_throttling_ &&
307 !CommandLine::ForCurrentProcess()->HasSwitch(
308 switches::kDisablePnaclCrashThrottling)) {
[email protected]ef42ee82013-11-01 05:26:13309 nacl::NaClBrowser::GetInstance()->OnProcessCrashed();
[email protected]e17f5ab2013-08-19 22:27:07310 }
311}
312
[email protected]773ebb92011-11-15 19:06:52313// This is called at browser startup.
314// static
[email protected]a2b4c5c2013-10-18 02:06:07315void NaClProcessHost::EarlyStartup() {
[email protected]ef42ee82013-11-01 05:26:13316 nacl::NaClBrowser::GetInstance()->EarlyStartup();
[email protected]773ebb92011-11-15 19:06:52317#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
318 // Open the IRT file early to make sure that it isn't replaced out from
319 // under us by autoupdate.
[email protected]ef42ee82013-11-01 05:26:13320 nacl::NaClBrowser::GetInstance()->EnsureIrtAvailable();
[email protected]773ebb92011-11-15 19:06:52321#endif
[email protected]9addd1c2012-09-15 14:28:24322 CommandLine* cmd = CommandLine::ForCurrentProcess();
323 UMA_HISTOGRAM_BOOLEAN(
324 "NaCl.nacl-gdb",
325 !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
326 UMA_HISTOGRAM_BOOLEAN(
327 "NaCl.nacl-gdb-script",
328 !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
329 UMA_HISTOGRAM_BOOLEAN(
330 "NaCl.enable-nacl-debug",
331 cmd->HasSwitch(switches::kEnableNaClDebug));
[email protected]ef42ee82013-11-01 05:26:13332 nacl::NaClBrowser::GetDelegate()->SetDebugPatterns(
[email protected]66f409c2012-10-04 20:59:04333 cmd->GetSwitchValueASCII(switches::kNaClDebugMask));
[email protected]773ebb92011-11-15 19:06:52334}
335
[email protected]a575da52012-03-22 13:08:36336void NaClProcessHost::Launch(
[email protected]01e59752013-06-18 00:17:35337 NaClHostMessageFilter* nacl_host_message_filter,
[email protected]8f42b4d2012-03-24 14:12:33338 IPC::Message* reply_msg,
[email protected]3a0506cc2013-07-01 20:28:31339 const base::FilePath& manifest_path) {
[email protected]01e59752013-06-18 00:17:35340 nacl_host_message_filter_ = nacl_host_message_filter;
[email protected]a575da52012-03-22 13:08:36341 reply_msg_ = reply_msg;
[email protected]3a0506cc2013-07-01 20:28:31342 manifest_path_ = manifest_path;
[email protected]a575da52012-03-22 13:08:36343
[email protected]e17f5ab2013-08-19 22:27:07344 // Do not launch the requested NaCl module if NaCl is marked "unstable" due
345 // to too many crashes within a given time period.
346 if (enable_crash_throttling_ &&
347 !CommandLine::ForCurrentProcess()->HasSwitch(
348 switches::kDisablePnaclCrashThrottling) &&
[email protected]ef42ee82013-11-01 05:26:13349 nacl::NaClBrowser::GetInstance()->IsThrottled()) {
[email protected]e17f5ab2013-08-19 22:27:07350 SendErrorToRenderer("Process creation was throttled due to excessive"
351 " crashes");
352 delete this;
353 return;
354 }
355
[email protected]8e39f0ef2013-07-31 04:30:46356 const CommandLine* cmd = CommandLine::ForCurrentProcess();
357#if defined(OS_WIN)
358 if (cmd->HasSwitch(switches::kEnableNaClDebug) &&
359 !cmd->HasSwitch(switches::kNoSandbox)) {
360 // We don't switch off sandbox automatically for security reasons.
361 SendErrorToRenderer("NaCl's GDB debug stub requires --no-sandbox flag"
362 " on Windows. See crbug.com/265624.");
363 delete this;
364 return;
365 }
366#endif
367 if (cmd->HasSwitch(switches::kNaClGdb) &&
368 !cmd->HasSwitch(switches::kEnableNaClDebug)) {
369 LOG(WARNING) << "--nacl-gdb flag requires --enable-nacl-debug flag";
370 }
371
[email protected]773ebb92011-11-15 19:06:52372 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03373 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]ef42ee82013-11-01 05:26:13374 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance();
[email protected]e97990f2012-05-15 22:06:06375 nacl_browser->EnsureAllResourcesAvailable();
376 if (!nacl_browser->IsOk()) {
[email protected]05c7cc72013-07-22 18:01:30377 SendErrorToRenderer("could not find all the resources needed"
378 " to launch the process");
[email protected]a575da52012-03-22 13:08:36379 delete this;
380 return;
[email protected]773ebb92011-11-15 19:06:52381 }
382
[email protected]c47ec402010-07-29 10:20:49383 // Rather than creating a socket pair in the renderer, and passing
384 // one side through the browser to sel_ldr, socket pairs are created
385 // in the browser and then passed to the renderer and sel_ldr.
386 //
387 // This is mainly for the benefit of Windows, where sockets cannot
388 // be passed in messages, but are copied via DuplicateHandle().
389 // This means the sandboxed renderer cannot send handles to the
390 // browser process.
391
[email protected]c8e1ea32013-02-13 02:19:01392 NaClHandle pair[2];
[email protected]6294dd02013-01-09 17:27:23393 // Create a connected socket
[email protected]c8e1ea32013-02-13 02:19:01394 if (NaClSocketPair(pair) == -1) {
[email protected]05c7cc72013-07-22 18:01:30395 SendErrorToRenderer("NaClSocketPair() failed");
[email protected]6294dd02013-01-09 17:27:23396 delete this;
397 return;
[email protected]c47ec402010-07-29 10:20:49398 }
[email protected]6294dd02013-01-09 17:27:23399 internal_->socket_for_renderer = pair[0];
400 internal_->socket_for_sel_ldr = pair[1];
401 SetCloseOnExec(pair[0]);
402 SetCloseOnExec(pair[1]);
[email protected]d032f492009-09-29 00:33:46403
404 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30405 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36406 delete this;
[email protected]d032f492009-09-29 00:33:46407 }
[email protected]d032f492009-09-29 00:33:46408}
[email protected]646e15552012-04-06 22:01:04409
[email protected]646e15552012-04-06 22:01:04410void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
[email protected]82d17502012-05-23 19:12:40411 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
412 switches::kNaClGdb).empty()) {
[email protected]04284252013-06-11 22:30:14413 LaunchNaClGdb();
[email protected]646e15552012-04-06 22:01:04414 }
[email protected]646e15552012-04-06 22:01:04415}
[email protected]646e15552012-04-06 22:01:04416
417#if defined(OS_WIN)
418void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
419 process_launched_by_broker_ = true;
420 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03421 if (!StartWithLaunchedProcess())
422 delete this;
[email protected]646e15552012-04-06 22:01:04423}
424
[email protected]ea6588842012-05-03 05:39:38425void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
426 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
427 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
428 Send(reply);
[email protected]646e15552012-04-06 22:01:04429}
430#endif
431
432// Needed to handle sync messages in OnMessageRecieved.
433bool NaClProcessHost::Send(IPC::Message* msg) {
434 return process_->Send(msg);
435}
436
[email protected]04284252013-06-11 22:30:14437bool NaClProcessHost::LaunchNaClGdb() {
[email protected]646e15552012-04-06 22:01:04438#if defined(OS_WIN)
[email protected]04284252013-06-11 22:30:14439 base::FilePath nacl_gdb =
440 CommandLine::ForCurrentProcess()->GetSwitchValuePath(switches::kNaClGdb);
441 CommandLine cmd_line(nacl_gdb);
442#else
[email protected]5b974952012-04-05 18:18:23443 CommandLine::StringType nacl_gdb =
444 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
445 switches::kNaClGdb);
446 CommandLine::StringVector argv;
447 // We don't support spaces inside arguments in --nacl-gdb switch.
448 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
449 CommandLine cmd_line(argv);
[email protected]04284252013-06-11 22:30:14450#endif
[email protected]5b974952012-04-05 18:18:23451 cmd_line.AppendArg("--eval-command");
[email protected]04284252013-06-11 22:30:14452 base::FilePath::StringType irt_path(
[email protected]ef42ee82013-11-01 05:26:13453 nacl::NaClBrowser::GetInstance()->GetIrtFilePath().value());
[email protected]04284252013-06-11 22:30:14454 // Avoid back slashes because nacl-gdb uses posix escaping rules on Windows.
455 // See issue https://code.google.com/p/nativeclient/issues/detail?id=3482.
456 std::replace(irt_path.begin(), irt_path.end(), '\\', '/');
457 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt \"") + irt_path +
458 FILE_PATH_LITERAL("\""));
[email protected]3a0506cc2013-07-01 20:28:31459 if (!manifest_path_.empty()) {
[email protected]5b974952012-04-05 18:18:23460 cmd_line.AppendArg("--eval-command");
[email protected]3a0506cc2013-07-01 20:28:31461 base::FilePath::StringType manifest_path_value(manifest_path_.value());
[email protected]04284252013-06-11 22:30:14462 std::replace(manifest_path_value.begin(), manifest_path_value.end(),
463 '\\', '/');
464 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest \"") +
465 manifest_path_value + FILE_PATH_LITERAL("\""));
[email protected]5b974952012-04-05 18:18:23466 }
467 cmd_line.AppendArg("--eval-command");
[email protected]04284252013-06-11 22:30:14468 cmd_line.AppendArg("target remote :4014");
[email protected]650b2d52013-02-10 03:41:45469 base::FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
[email protected]401b90792012-05-30 11:41:13470 switches::kNaClGdbScript);
471 if (!script.empty()) {
472 cmd_line.AppendArg("--command");
473 cmd_line.AppendArgNative(script.value());
474 }
[email protected]5b974952012-04-05 18:18:23475 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
476}
477
[email protected]fb1277e82009-11-21 20:32:30478bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40479 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]ec7540f2013-01-17 00:00:35480 if (channel_id.empty()) {
[email protected]05c7cc72013-07-22 18:01:30481 SendErrorToRenderer("CreateChannel() failed");
[email protected]d032f492009-09-29 00:33:46482 return false;
[email protected]ec7540f2013-01-17 00:00:35483 }
[email protected]d032f492009-09-29 00:33:46484
[email protected]e3fc75a2011-05-05 08:20:42485 CommandLine::StringType nacl_loader_prefix;
486#if defined(OS_POSIX)
487 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
488 switches::kNaClLoaderCmdPrefix);
489#endif // defined(OS_POSIX)
490
[email protected]d032f492009-09-29 00:33:46491 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36492
493#if defined(OS_MACOSX)
494 // The Native Client process needs to be able to allocate a 1GB contiguous
495 // region to use as the client environment's virtual address space. ASLR
496 // (PIE) interferes with this by making it possible that no gap large enough
497 // to accomodate this request will exist in the child process' address
498 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
499 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49500 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36501#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49502 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
503 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36504#else
[email protected]4cb43102011-12-02 20:24:49505 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36506#endif
507
[email protected]650b2d52013-02-10 03:41:45508 base::FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30509 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46510 return false;
511
[email protected]31a665e72012-03-11 12:37:46512#if defined(OS_WIN)
513 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
514 if (RunningOnWOW64()) {
[email protected]ef42ee82013-11-01 05:26:13515 if (!nacl::NaClBrowser::GetInstance()->GetNaCl64ExePath(&exe_path)) {
[email protected]05c7cc72013-07-22 18:01:30516 SendErrorToRenderer("could not get path to nacl64.exe");
[email protected]31a665e72012-03-11 12:37:46517 return false;
[email protected]ec7540f2013-01-17 00:00:35518 }
[email protected]31a665e72012-03-11 12:37:46519 }
520#endif
521
[email protected]33a05af2012-03-02 18:15:51522 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
523 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13524
[email protected]05076ba22010-07-30 05:59:57525 cmd_line->AppendSwitchASCII(switches::kProcessType,
526 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44527 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]ef42ee82013-11-01 05:26:13528 if (nacl::NaClBrowser::GetDelegate()->DialogsAreSuppressed())
[email protected]93156cec2011-09-12 21:14:44529 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46530
[email protected]e3fc75a2011-05-05 08:20:42531 if (!nacl_loader_prefix.empty())
532 cmd_line->PrependWrapper(nacl_loader_prefix);
533
[email protected]103607e2010-02-01 18:57:09534 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46535#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52536 if (RunningOnWOW64()) {
[email protected]ec7540f2013-01-17 00:00:35537 if (!NaClBrokerService::GetInstance()->LaunchLoader(
538 weak_factory_.GetWeakPtr(), channel_id)) {
[email protected]05c7cc72013-07-22 18:01:30539 SendErrorToRenderer("broker service did not launch process");
[email protected]ec7540f2013-01-17 00:00:35540 return false;
541 }
[email protected]4bdde602010-06-16 03:17:35542 } else {
[email protected]34f48682013-03-20 00:30:18543 process_->Launch(new NaClSandboxedProcessLauncherDelegate,
544 cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35545 }
[email protected]103607e2010-02-01 18:57:09546#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40547 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]b345c482013-08-30 18:00:39548 base::EnvironmentMap(),
[email protected]33a05af2012-03-02 18:15:51549 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09550#endif
[email protected]d032f492009-09-29 00:33:46551
[email protected]fb1277e82009-11-21 20:32:30552 return true;
[email protected]d032f492009-09-29 00:33:46553}
554
[email protected]646e15552012-04-06 22:01:04555bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
556 bool handled = true;
557 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
558 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
559 OnQueryKnownToValidate)
560 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
561 OnSetKnownToValidate)
[email protected]041834522013-05-29 00:19:27562 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_ResolveFileToken,
563 OnResolveFileToken)
[email protected]ea6588842012-05-03 05:39:38564#if defined(OS_WIN)
565 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
566 OnAttachDebugExceptionHandler)
567#endif
[email protected]17b5a8172012-06-22 21:09:09568 IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated,
569 OnPpapiChannelCreated)
[email protected]646e15552012-04-06 22:01:04570 IPC_MESSAGE_UNHANDLED(handled = false)
571 IPC_END_MESSAGE_MAP()
572 return handled;
[email protected]103607e2010-02-01 18:57:09573}
574
[email protected]773ebb92011-11-15 19:06:52575void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03576 if (!StartWithLaunchedProcess())
577 delete this;
[email protected]773ebb92011-11-15 19:06:52578}
579
[email protected]e97990f2012-05-15 22:06:06580// Called when the NaClBrowser singleton has been fully initialized.
581void NaClProcessHost::OnResourcesReady() {
[email protected]ef42ee82013-11-01 05:26:13582 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance();
[email protected]ec7540f2013-01-17 00:00:35583 if (!nacl_browser->IsReady()) {
[email protected]05c7cc72013-07-22 18:01:30584 SendErrorToRenderer("could not acquire shared resources needed by NaCl");
[email protected]ec7540f2013-01-17 00:00:35585 delete this;
586 } else if (!SendStart()) {
[email protected]338466a82011-05-03 04:27:43587 delete this;
588 }
589}
590
[email protected]17b5a8172012-06-22 21:09:09591bool NaClProcessHost::ReplyToRenderer(
592 const IPC::ChannelHandle& channel_handle) {
[email protected]e9e810f2013-07-12 19:26:11593#if defined(OS_WIN)
594 // If we are on 64-bit Windows, the NaCl process's sandbox is
595 // managed by a different process from the renderer's sandbox. We
596 // need to inform the renderer's sandbox about the NaCl process so
597 // that the renderer can send handles to the NaCl process using
598 // BrokerDuplicateHandle().
599 if (RunningOnWOW64()) {
600 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]05c7cc72013-07-22 18:01:30601 SendErrorToRenderer("BrokerAddTargetPeer() failed");
[email protected]e9e810f2013-07-12 19:26:11602 return false;
603 }
604 }
605#endif
606
[email protected]6294dd02013-01-09 17:27:23607 nacl::FileDescriptor handle_for_renderer;
[email protected]c47ec402010-07-29 10:20:49608#if defined(OS_WIN)
[email protected]6294dd02013-01-09 17:27:23609 // Copy the handle into the renderer process.
610 HANDLE handle_in_renderer;
611 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
612 reinterpret_cast<HANDLE>(
613 internal_->socket_for_renderer),
[email protected]950be552013-07-10 19:13:02614 nacl_host_message_filter_->PeerHandle(),
[email protected]6294dd02013-01-09 17:27:23615 &handle_in_renderer,
616 0, // Unused given DUPLICATE_SAME_ACCESS.
617 FALSE,
618 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]05c7cc72013-07-22 18:01:30619 SendErrorToRenderer("DuplicateHandle() failed");
[email protected]6294dd02013-01-09 17:27:23620 return false;
[email protected]c47ec402010-07-29 10:20:49621 }
[email protected]6294dd02013-01-09 17:27:23622 handle_for_renderer = reinterpret_cast<nacl::FileDescriptor>(
623 handle_in_renderer);
624#else
625 // No need to dup the imc_handle - we don't pass it anywhere else so
626 // it cannot be closed.
627 nacl::FileDescriptor imc_handle;
628 imc_handle.fd = internal_->socket_for_renderer;
629 imc_handle.auto_close = true;
630 handle_for_renderer = imc_handle;
631#endif
[email protected]c47ec402010-07-29 10:20:49632
[email protected]108fd342013-01-04 20:46:54633 const ChildProcessData& data = process_->GetData();
[email protected]05c7cc72013-07-22 18:01:30634 SendMessageToRenderer(
635 nacl::NaClLaunchResult(handle_for_renderer,
636 channel_handle,
637 base::GetProcId(data.handle),
638 data.id),
639 std::string() /* error_message */);
[email protected]c8e1ea32013-02-13 02:19:01640 internal_->socket_for_renderer = NACL_INVALID_HANDLE;
[email protected]00d99542012-04-17 22:48:02641 return true;
642}
[email protected]fb1277e82009-11-21 20:32:30643
[email protected]05c7cc72013-07-22 18:01:30644void NaClProcessHost::SendErrorToRenderer(const std::string& error_message) {
645 LOG(ERROR) << "NaCl process launch failed: " << error_message;
646 SendMessageToRenderer(nacl::NaClLaunchResult(), error_message);
647}
648
649void NaClProcessHost::SendMessageToRenderer(
650 const nacl::NaClLaunchResult& result,
651 const std::string& error_message) {
652 DCHECK(nacl_host_message_filter_);
653 DCHECK(reply_msg_);
654 if (nacl_host_message_filter_ != NULL && reply_msg_ != NULL) {
655 NaClHostMsg_LaunchNaCl::WriteReplyParams(
656 reply_msg_, result, error_message);
657 nacl_host_message_filter_->Send(reply_msg_);
658 nacl_host_message_filter_ = NULL;
659 reply_msg_ = NULL;
660 }
661}
662
[email protected]d571c032012-09-14 12:39:18663// TCP port we chose for NaCl debug stub. It can be any other number.
664static const int kDebugStubPort = 4014;
665
666#if defined(OS_POSIX)
[email protected]bbfef1f2013-08-28 03:00:51667net::SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
[email protected]ef42ee82013-11-01 05:26:13668 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance();
[email protected]bbfef1f2013-08-28 03:00:51669 net::SocketDescriptor s = net::kInvalidSocket;
[email protected]13cad0b2012-12-07 17:57:34670 // We allocate currently unused TCP port for debug stub tests. The port
671 // number is passed to the test via debug stub port listener.
672 if (nacl_browser->HasGdbDebugStubPortListener()) {
673 int port;
674 s = net::TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port);
[email protected]bbfef1f2013-08-28 03:00:51675 if (s != net::kInvalidSocket) {
[email protected]13cad0b2012-12-07 17:57:34676 nacl_browser->FireGdbDebugStubPortOpened(port);
677 }
678 } else {
679 s = net::TCPListenSocket::CreateAndBind("127.0.0.1", kDebugStubPort);
680 }
[email protected]bbfef1f2013-08-28 03:00:51681 if (s == net::kInvalidSocket) {
[email protected]13cad0b2012-12-07 17:57:34682 LOG(ERROR) << "failed to open socket for debug stub";
[email protected]bbfef1f2013-08-28 03:00:51683 return net::kInvalidSocket;
[email protected]13cad0b2012-12-07 17:57:34684 }
[email protected]d571c032012-09-14 12:39:18685 if (listen(s, 1)) {
686 LOG(ERROR) << "listen() failed on debug stub socket";
[email protected]13cad0b2012-12-07 17:57:34687 if (HANDLE_EINTR(close(s)) < 0)
688 PLOG(ERROR) << "failed to close debug stub socket";
[email protected]bbfef1f2013-08-28 03:00:51689 return net::kInvalidSocket;
[email protected]d571c032012-09-14 12:39:18690 }
691 return s;
692}
693#endif
694
[email protected]00d99542012-04-17 22:48:02695bool NaClProcessHost::StartNaClExecution() {
[email protected]ef42ee82013-11-01 05:26:13696 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance();
[email protected]00d99542012-04-17 22:48:02697
698 nacl::NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12699 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
700 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]ef42ee82013-11-01 05:26:13701 params.version = nacl::NaClBrowser::GetDelegate()->GetVersionString();
[email protected]00d99542012-04-17 22:48:02702 params.enable_exception_handling = enable_exception_handling_;
[email protected]66f409c2012-10-04 20:59:04703 params.enable_debug_stub = enable_debug_stub_ &&
[email protected]ef42ee82013-11-01 05:26:13704 nacl::NaClBrowser::GetDelegate()->URLMatchesDebugPatterns(manifest_url_);
[email protected]7522077b2013-01-17 23:39:40705 // Enable PPAPI proxy channel creation only for renderer processes.
706 params.enable_ipc_proxy = enable_ppapi_proxy();
[email protected]2e1e6f02013-01-11 18:22:56707 params.uses_irt = uses_irt_;
[email protected]be524622013-04-23 00:12:19708 params.enable_dyncode_syscalls = enable_dyncode_syscalls_;
[email protected]00d99542012-04-17 22:48:02709
710 const ChildProcessData& data = process_->GetData();
[email protected]6294dd02013-01-09 17:27:23711 if (!ShareHandleToSelLdr(data.handle,
712 internal_->socket_for_sel_ldr, true,
713 &params.handles)) {
714 return false;
[email protected]773ebb92011-11-15 19:06:52715 }
716
[email protected]2e1e6f02013-01-11 18:22:56717 if (params.uses_irt) {
718 base::PlatformFile irt_file = nacl_browser->IrtFile();
719 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
[email protected]2e1e6f02013-01-11 18:22:56720 // Send over the IRT file handle. We don't close our own copy!
721 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
722 return false;
723 }
[email protected]c47ec402010-07-29 10:20:49724
[email protected]ab88d1542011-11-18 22:52:00725#if defined(OS_MACOSX)
726 // For dynamic loading support, NaCl requires a file descriptor that
727 // was created in /tmp, since those created with shm_open() are not
728 // mappable with PROT_EXEC. Rather than requiring an extra IPC
729 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25730 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31731 base::SharedMemoryCreateOptions options;
732 options.size = 1;
733 options.executable = true;
734 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03735 DLOG(ERROR) << "Failed to allocate memory buffer";
736 return false;
[email protected]2c68bf032010-11-11 23:16:30737 }
[email protected]cbbe7842011-11-17 22:01:25738 nacl::FileDescriptor memory_fd;
739 memory_fd.fd = dup(memory_buffer.handle().fd);
740 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03741 DLOG(ERROR) << "Failed to dup() a file descriptor";
742 return false;
[email protected]cbbe7842011-11-17 22:01:25743 }
744 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02745 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30746#endif
747
[email protected]d571c032012-09-14 12:39:18748#if defined(OS_POSIX)
[email protected]ed637bb2012-10-15 17:36:05749 if (params.enable_debug_stub) {
[email protected]bbfef1f2013-08-28 03:00:51750 net::SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
751 if (server_bound_socket != net::kInvalidSocket) {
[email protected]d571c032012-09-14 12:39:18752 params.debug_stub_server_bound_socket =
753 nacl::FileDescriptor(server_bound_socket, true);
754 }
755 }
756#endif
757
[email protected]ea6588842012-05-03 05:39:38758 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41759
[email protected]c8e1ea32013-02-13 02:19:01760 internal_->socket_for_sel_ldr = NACL_INVALID_HANDLE;
[email protected]09afc2e2012-04-10 17:29:03761 return true;
762}
763
[email protected]00d99542012-04-17 22:48:02764bool NaClProcessHost::SendStart() {
[email protected]7522077b2013-01-17 23:39:40765 if (!enable_ppapi_proxy()) {
[email protected]17b5a8172012-06-22 21:09:09766 if (!ReplyToRenderer(IPC::ChannelHandle()))
767 return false;
768 }
769 return StartNaClExecution();
770}
771
[email protected]8510d282012-08-30 19:47:38772// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
773// received or PpapiHostMsg_ChannelCreated is forwarded by our plugin
774// listener.
[email protected]17b5a8172012-06-22 21:09:09775void NaClProcessHost::OnPpapiChannelCreated(
776 const IPC::ChannelHandle& channel_handle) {
[email protected]7522077b2013-01-17 23:39:40777 // Only renderer processes should create a channel.
778 DCHECK(enable_ppapi_proxy());
[email protected]8510d282012-08-30 19:47:38779 // If the proxy channel is null, this must be the initial NaCl-Browser IPC
780 // channel.
781 if (!ipc_proxy_channel_.get()) {
[email protected]f3b357692013-03-22 05:16:13782 DCHECK_EQ(PROCESS_TYPE_NACL_LOADER, process_->GetData().process_type);
[email protected]9e141212013-01-21 15:21:56783
[email protected]8510d282012-08-30 19:47:38784 ipc_proxy_channel_.reset(
785 new IPC::ChannelProxy(channel_handle,
786 IPC::Channel::MODE_CLIENT,
787 &ipc_plugin_listener_,
[email protected]cadac622013-06-11 16:46:36788 base::MessageLoopProxy::current().get()));
[email protected]0c7193742012-11-07 19:05:03789 // Create the browser ppapi host and enable PPAPI message dispatching to the
790 // browser process.
[email protected]adf7cd52012-12-04 19:37:46791 ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess(
[email protected]9e141212013-01-21 15:21:56792 ipc_proxy_channel_.get(), // sender
[email protected]0c7193742012-11-07 19:05:03793 permissions_,
794 process_->GetData().handle,
[email protected]b4b53d182012-09-12 02:56:55795 ipc_proxy_channel_.get(),
[email protected]01e59752013-06-18 00:17:35796 nacl_host_message_filter_->render_process_id(),
[email protected]7bb5d2c2013-04-29 22:33:49797 render_view_id_,
798 profile_directory_));
[email protected]adf7cd52012-12-04 19:37:46799
[email protected]ac07ec52013-04-22 17:32:45800 ppapi::PpapiNaClChannelArgs args;
[email protected]01e59752013-06-18 00:17:35801 args.off_the_record = nacl_host_message_filter_->off_the_record();
[email protected]ac07ec52013-04-22 17:32:45802 args.permissions = permissions_;
803 CommandLine* cmdline = CommandLine::ForCurrentProcess();
804 DCHECK(cmdline);
805 std::string flag_whitelist[] = {switches::kV, switches::kVModule};
806 for (size_t i = 0; i < arraysize(flag_whitelist); ++i) {
807 std::string value = cmdline->GetSwitchValueASCII(flag_whitelist[i]);
808 if (!value.empty()) {
809 args.switch_names.push_back(flag_whitelist[i]);
810 args.switch_values.push_back(value);
811 }
812 }
813
[email protected]4b30e7d2013-04-24 23:52:14814 ppapi_host_->GetPpapiHost()->AddHostFactoryFilter(
815 scoped_ptr<ppapi::host::HostFactory>(
[email protected]ef42ee82013-11-01 05:26:13816 nacl::NaClBrowser::GetDelegate()->CreatePpapiHostFactory(
[email protected]6fff31c22013-07-09 02:08:19817 ppapi_host_.get())));
[email protected]4b30e7d2013-04-24 23:52:14818
[email protected]8510d282012-08-30 19:47:38819 // Send a message to create the NaCl-Renderer channel. The handle is just
820 // a place holder.
821 ipc_proxy_channel_->Send(
822 new PpapiMsg_CreateNaClChannel(
[email protected]01e59752013-06-18 00:17:35823 nacl_host_message_filter_->render_process_id(),
[email protected]ac07ec52013-04-22 17:32:45824 args,
[email protected]8510d282012-08-30 19:47:38825 SerializedHandle(SerializedHandle::CHANNEL_HANDLE,
826 IPC::InvalidPlatformFileForTransit())));
827 } else if (reply_msg_) {
828 // Otherwise, this must be a renderer channel.
829 ReplyToRenderer(channel_handle);
830 } else {
831 // Attempt to open more than 1 renderer channel is not supported.
832 // Shut down the NaCl process.
833 process_->GetHost()->ForceShutdown();
834 }
835}
836
837bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) {
838 // Handle messages that have been forwarded from our PluginListener.
839 // These messages come from untrusted code so should be handled with care.
840 bool handled = true;
841 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
842 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
843 OnPpapiChannelCreated)
844 IPC_MESSAGE_UNHANDLED(handled = false)
845 IPC_END_MESSAGE_MAP()
846 return handled;
[email protected]00d99542012-04-17 22:48:02847}
848
[email protected]09afc2e2012-04-10 17:29:03849bool NaClProcessHost::StartWithLaunchedProcess() {
[email protected]ef42ee82013-11-01 05:26:13850 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03851
[email protected]e97990f2012-05-15 22:06:06852 if (nacl_browser->IsReady()) {
853 return SendStart();
854 } else if (nacl_browser->IsOk()) {
855 nacl_browser->WaitForResources(
856 base::Bind(&NaClProcessHost::OnResourcesReady,
857 weak_factory_.GetWeakPtr()));
858 return true;
859 } else {
[email protected]05c7cc72013-07-22 18:01:30860 SendErrorToRenderer("previously failed to acquire shared resources");
[email protected]e97990f2012-05-15 22:06:06861 return false;
862 }
[email protected]d032f492009-09-29 00:33:46863}
864
[email protected]4a0141b2012-03-27 01:15:30865void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
866 bool* result) {
[email protected]ef42ee82013-11-01 05:26:13867 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52868 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30869}
870
871void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]ef42ee82013-11-01 05:26:13872 nacl::NaClBrowser::GetInstance()->SetKnownToValidate(
873 signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30874}
[email protected]ea6588842012-05-03 05:39:38875
[email protected]041834522013-05-29 00:19:27876void NaClProcessHost::FileResolved(
877 base::PlatformFile* file,
878 const base::FilePath& file_path,
879 IPC::Message* reply_msg) {
880 if (*file != base::kInvalidPlatformFileValue) {
881 IPC::PlatformFileForTransit handle = IPC::GetFileHandleForProcess(
882 *file,
883 process_->GetData().handle,
884 true /* close_source */);
885 NaClProcessMsg_ResolveFileToken::WriteReplyParams(
886 reply_msg,
887 handle,
888 file_path);
889 } else {
890 NaClProcessMsg_ResolveFileToken::WriteReplyParams(
891 reply_msg,
892 IPC::InvalidPlatformFileForTransit(),
893 base::FilePath(FILE_PATH_LITERAL("")));
894 }
895 Send(reply_msg);
896}
897
898void NaClProcessHost::OnResolveFileToken(uint64 file_token_lo,
899 uint64 file_token_hi,
900 IPC::Message* reply_msg) {
901 // Was the file registered?
902 //
903 // Note that the file path cache is of bounded size, and old entries can get
904 // evicted. If a large number of NaCl modules are being launched at once,
905 // resolving the file_token may fail because the path cache was thrashed
906 // while the file_token was in flight. In this case the query fails, and we
907 // need to fall back to the slower path.
908 //
909 // However: each NaCl process will consume 2-3 entries as it starts up, this
910 // means that eviction will not happen unless you start up 33+ NaCl processes
911 // at the same time, and this still requires worst-case timing. As a
912 // practical matter, no entries should be evicted prematurely.
913 // The cache itself should take ~ (150 characters * 2 bytes/char + ~60 bytes
914 // data structure overhead) * 100 = 35k when full, so making it bigger should
915 // not be a problem, if needed.
916 //
917 // Each NaCl process will consume 2-3 entries because the manifest and main
918 // nexe are currently not resolved. Shared libraries will be resolved. They
919 // will be loaded sequentially, so they will only consume a single entry
920 // while the load is in flight.
921 //
922 // TODO(ncbray): track behavior with UMA. If entries are getting evicted or
923 // bogus keys are getting queried, this would be good to know.
924 base::FilePath file_path;
[email protected]ef42ee82013-11-01 05:26:13925 if (!nacl::NaClBrowser::GetInstance()->GetFilePath(
926 file_token_lo, file_token_hi, &file_path)) {
[email protected]041834522013-05-29 00:19:27927 NaClProcessMsg_ResolveFileToken::WriteReplyParams(
928 reply_msg,
929 IPC::InvalidPlatformFileForTransit(),
930 base::FilePath(FILE_PATH_LITERAL("")));
931 Send(reply_msg);
932 return;
933 }
934
935 // Scratch space to share between the callbacks.
936 base::PlatformFile* data = new base::PlatformFile();
937
938 // Open the file.
939 if (!content::BrowserThread::PostBlockingPoolTaskAndReply(
940 FROM_HERE,
941 base::Bind(nacl::OpenNaClExecutableImpl,
942 file_path, data),
943 base::Bind(&NaClProcessHost::FileResolved,
944 weak_factory_.GetWeakPtr(),
945 base::Owned(data),
946 file_path,
947 reply_msg))) {
948 NaClProcessMsg_ResolveFileToken::WriteReplyParams(
949 reply_msg,
950 IPC::InvalidPlatformFileForTransit(),
951 base::FilePath(FILE_PATH_LITERAL("")));
952 Send(reply_msg);
953 }
954}
955
[email protected]ea6588842012-05-03 05:39:38956#if defined(OS_WIN)
957void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
958 IPC::Message* reply_msg) {
959 if (!AttachDebugExceptionHandler(info, reply_msg)) {
960 // Send failure message.
961 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
962 false);
963 Send(reply_msg);
964 }
965}
966
967bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
968 IPC::Message* reply_msg) {
[email protected]2c227ff2012-08-11 00:51:32969 if (!enable_exception_handling_ && !enable_debug_stub_) {
[email protected]ea6588842012-05-03 05:39:38970 DLOG(ERROR) <<
[email protected]2c227ff2012-08-11 00:51:32971 "Debug exception handler requested by NaCl process when not enabled";
[email protected]ea6588842012-05-03 05:39:38972 return false;
973 }
974 if (debug_exception_handler_requested_) {
975 // The NaCl process should not request this multiple times.
976 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
977 return false;
978 }
979 debug_exception_handler_requested_ = true;
980
981 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
982 base::win::ScopedHandle process_handle;
983 // We cannot use process_->GetData().handle because it does not have
984 // the necessary access rights. We open the new handle here rather
985 // than in the NaCl broker process in case the NaCl loader process
986 // dies before the NaCl broker process receives the message we send.
987 // The debug exception handler uses DebugActiveProcess() to attach,
988 // but this takes a PID. We need to prevent the NaCl loader's PID
989 // from being reused before DebugActiveProcess() is called, and
990 // holding a process handle open achieves this.
991 if (!base::OpenProcessHandleWithAccess(
992 nacl_pid,
993 base::kProcessAccessQueryInformation |
994 base::kProcessAccessSuspendResume |
995 base::kProcessAccessTerminate |
996 base::kProcessAccessVMOperation |
997 base::kProcessAccessVMRead |
998 base::kProcessAccessVMWrite |
[email protected]29172322013-01-10 17:35:16999 base::kProcessAccessDuplicateHandle |
[email protected]ea6588842012-05-03 05:39:381000 base::kProcessAccessWaitForTermination,
1001 process_handle.Receive())) {
1002 LOG(ERROR) << "Failed to get process handle";
1003 return false;
1004 }
1005
1006 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
1007 // If the NaCl loader is 64-bit, the process running its debug
1008 // exception handler must be 64-bit too, so we use the 64-bit NaCl
1009 // broker process for this. Otherwise, on a 32-bit system, we use
1010 // the 32-bit browser process to run the debug exception handler.
1011 if (RunningOnWOW64()) {
1012 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:481013 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:381014 } else {
1015 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:481016 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:381017 base::MessageLoopProxy::current(),
1018 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
1019 weak_factory_.GetWeakPtr()));
1020 return true;
1021 }
1022}
1023#endif