blob: 157a8d2f7a7daef4812f9d28fb72a24a52b5bcc9 [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]2e5f2ea42012-05-09 21:39:2128#include "chrome/browser/nacl_host/nacl_browser.h"
[email protected]01e59752013-06-18 00:17:3529#include "chrome/browser/nacl_host/nacl_host_message_filter.h"
[email protected]e17f5ab2013-08-19 22:27:0730#include "chrome/common/chrome_switches.h"
[email protected]6fff31c22013-07-09 02:08:1931#include "components/nacl/common/nacl_browser_delegate.h"
[email protected]0acf1d42013-07-22 14:54:5832#include "components/nacl/common/nacl_cmd_line.h"
[email protected]512b5bd2013-07-23 20:24:0033#include "components/nacl/common/nacl_host_messages.h"
34#include "components/nacl/common/nacl_messages.h"
[email protected]d5d383252013-07-04 14:44:3235#include "components/nacl/common/nacl_process_type.h"
[email protected]cbb22eb2013-06-24 23:53:1036#include "components/nacl/common/nacl_switches.h"
[email protected]4967f792012-01-20 22:14:4037#include "content/public/browser/browser_child_process_host.h"
[email protected]0c7193742012-11-07 19:05:0338#include "content/public/browser/browser_ppapi_host.h"
[email protected]4967f792012-01-20 22:14:4039#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4440#include "content/public/common/child_process_host.h"
[email protected]67e76872013-07-08 02:19:3441#include "content/public/common/content_switches.h"
[email protected]9e141212013-01-21 15:21:5642#include "content/public/common/process_type.h"
[email protected]8510d282012-08-30 19:47:3843#include "ipc/ipc_channel.h"
[email protected]d032f492009-09-29 00:33:4644#include "ipc/ipc_switches.h"
[email protected]c8e1ea32013-02-13 02:19:0145#include "native_client/src/shared/imc/nacl_imc_c.h"
[email protected]87f35592012-04-08 00:49:1646#include "net/base/net_util.h"
[email protected]1946d9e2013-04-09 01:43:3747#include "net/socket/tcp_listen_socket.h"
[email protected]6fff31c22013-07-09 02:08:1948#include "ppapi/host/host_factory.h"
[email protected]4b30e7d2013-04-24 23:52:1449#include "ppapi/host/ppapi_host.h"
[email protected]8510d282012-08-30 19:47:3850#include "ppapi/proxy/ppapi_messages.h"
[email protected]ac07ec52013-04-22 17:32:4551#include "ppapi/shared_impl/ppapi_nacl_channel_args.h"
[email protected]d032f492009-09-29 00:33:4652
[email protected]d032f492009-09-29 00:33:4653#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2054#include <fcntl.h>
55
[email protected]d032f492009-09-29 00:33:4656#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3557#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2058#include <windows.h>
59
[email protected]885c0e92012-11-13 20:27:4260#include "base/threading/thread.h"
[email protected]4c65fb632012-04-27 00:42:2561#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3562#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]512b5bd2013-07-23 20:24:0063#include "components/nacl/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5064#include "content/public/common/sandbox_init.h"
[email protected]34f48682013-03-20 00:30:1865#include "content/public/common/sandboxed_process_launcher_delegate.h"
[email protected]d032f492009-09-29 00:33:4666#endif
67
[email protected]631bb742011-11-02 11:29:3968using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4069using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4470using content::ChildProcessHost;
[email protected]8510d282012-08-30 19:47:3871using ppapi::proxy::SerializedHandle;
[email protected]631bb742011-11-02 11:29:3972
[email protected]25a10e232013-08-23 00:40:0873#if defined(OS_WIN)
74
75namespace {
76
77// Looks for the largest contiguous unallocated region of address
78// space and returns it via |*out_addr| and |*out_size|.
79void FindAddressSpace(base::ProcessHandle process,
80 char** out_addr, size_t* out_size) {
81 *out_addr = NULL;
82 *out_size = 0;
83 char* addr = 0;
84 while (true) {
85 MEMORY_BASIC_INFORMATION info;
86 size_t result = VirtualQueryEx(process, static_cast<void*>(addr),
87 &info, sizeof(info));
88 if (result < sizeof(info))
89 break;
90 if (info.State == MEM_FREE && info.RegionSize > *out_size) {
91 *out_addr = addr;
92 *out_size = info.RegionSize;
93 }
94 addr += info.RegionSize;
95 }
96}
97
98} // namespace
99
100namespace nacl {
101
102// Allocates |size| bytes of address space in the given process at a
103// randomised address.
104void* AllocateAddressSpaceASLR(base::ProcessHandle process, size_t size) {
105 char* addr;
106 size_t avail_size;
107 FindAddressSpace(process, &addr, &avail_size);
108 if (avail_size < size)
109 return NULL;
110 size_t offset = base::RandGenerator(avail_size - size);
111 const int kPageSize = 0x10000;
112 void* request_addr =
113 reinterpret_cast<void*>(reinterpret_cast<uint64>(addr + offset)
114 & ~(kPageSize - 1));
115 return VirtualAllocEx(process, request_addr, size,
116 MEM_RESERVE, PAGE_NOACCESS);
117}
118
119} // namespace nacl
120
121#endif // defined(OS_WIN)
122
[email protected]646e15552012-04-06 22:01:04123namespace {
124
125#if defined(OS_WIN)
126bool RunningOnWOW64() {
127 return (base::win::OSInfo::GetInstance()->wow64_status() ==
128 base::win::OSInfo::WOW64_ENABLED);
129}
[email protected]34f48682013-03-20 00:30:18130
131// NOTE: changes to this class need to be reviewed by the security team.
132class NaClSandboxedProcessLauncherDelegate
133 : public content::SandboxedProcessLauncherDelegate {
134 public:
135 NaClSandboxedProcessLauncherDelegate() {}
136 virtual ~NaClSandboxedProcessLauncherDelegate() {}
137
138 virtual void PostSpawnTarget(base::ProcessHandle process) {
[email protected]34f48682013-03-20 00:30:18139 // For Native Client sel_ldr processes on 32-bit Windows, reserve 1 GB of
140 // address space to prevent later failure due to address space fragmentation
141 // from .dll loading. The NaCl process will attempt to locate this space by
142 // scanning the address space using VirtualQuery.
143 // TODO(bbudge) Handle the --no-sandbox case.
144 // http://code.google.com/p/nativeclient/issues/detail?id=2131
[email protected]25a10e232013-08-23 00:40:08145 const SIZE_T kNaClSandboxSize = 1 << 30;
146 if (!nacl::AllocateAddressSpaceASLR(process, kNaClSandboxSize)) {
[email protected]34f48682013-03-20 00:30:18147 DLOG(WARNING) << "Failed to reserve address space for Native Client";
148 }
[email protected]34f48682013-03-20 00:30:18149 }
150};
151
152#endif // OS_WIN
[email protected]646e15552012-04-06 22:01:04153
[email protected]c8e1ea32013-02-13 02:19:01154void SetCloseOnExec(NaClHandle fd) {
[email protected]646e15552012-04-06 22:01:04155#if defined(OS_POSIX)
156 int flags = fcntl(fd, F_GETFD);
157 CHECK_NE(flags, -1);
158 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
159 CHECK_EQ(rc, 0);
160#endif
161}
162
[email protected]00d99542012-04-17 22:48:02163bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:04164 base::ProcessHandle processh,
[email protected]c8e1ea32013-02-13 02:19:01165 NaClHandle sourceh,
[email protected]646e15552012-04-06 22:01:04166 bool close_source,
167 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
168#if defined(OS_WIN)
169 HANDLE channel;
170 int flags = DUPLICATE_SAME_ACCESS;
171 if (close_source)
172 flags |= DUPLICATE_CLOSE_SOURCE;
173 if (!DuplicateHandle(GetCurrentProcess(),
174 reinterpret_cast<HANDLE>(sourceh),
175 processh,
176 &channel,
177 0, // Unused given DUPLICATE_SAME_ACCESS.
178 FALSE,
179 flags)) {
[email protected]ec7540f2013-01-17 00:00:35180 LOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:04181 return false;
182 }
183 handles_for_sel_ldr->push_back(
184 reinterpret_cast<nacl::FileDescriptor>(channel));
185#else
186 nacl::FileDescriptor channel;
187 channel.fd = sourceh;
188 channel.auto_close = close_source;
189 handles_for_sel_ldr->push_back(channel);
190#endif
191 return true;
192}
193
[email protected]27b04df2012-12-10 23:43:10194ppapi::PpapiPermissions GetNaClPermissions(uint32 permission_bits) {
195 // Only allow NaCl plugins to request certain permissions. We don't want
196 // a compromised renderer to be able to start a nacl plugin with e.g. Flash
197 // permissions which may expand the surface area of the sandbox.
198 uint32 masked_bits = permission_bits & ppapi::PERMISSION_DEV;
199 return ppapi::PpapiPermissions::GetForCommandLine(masked_bits);
200}
201
[email protected]646e15552012-04-06 22:01:04202} // namespace
203
[email protected]1d8a3d1f2011-02-19 07:11:52204struct NaClProcessHost::NaClInternal {
[email protected]c8e1ea32013-02-13 02:19:01205 NaClHandle socket_for_renderer;
206 NaClHandle socket_for_sel_ldr;
[email protected]6294dd02013-01-09 17:27:23207
208 NaClInternal()
[email protected]c8e1ea32013-02-13 02:19:01209 : socket_for_renderer(NACL_INVALID_HANDLE),
210 socket_for_sel_ldr(NACL_INVALID_HANDLE) { }
[email protected]1d8a3d1f2011-02-19 07:11:52211};
212
[email protected]646e15552012-04-06 22:01:04213// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52214
[email protected]8510d282012-08-30 19:47:38215NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host)
216 : host_(host) {
217}
218
219bool NaClProcessHost::PluginListener::OnMessageReceived(
220 const IPC::Message& msg) {
221 return host_->OnUntrustedMessageForwarded(msg);
222}
223
[email protected]9da4fed2012-11-01 17:07:55224NaClProcessHost::NaClProcessHost(const GURL& manifest_url,
[email protected]e02ff722012-11-06 03:53:06225 int render_view_id,
[email protected]9da4fed2012-11-01 17:07:55226 uint32 permission_bits,
[email protected]2e1e6f02013-01-11 18:22:56227 bool uses_irt,
[email protected]be524622013-04-23 00:12:19228 bool enable_dyncode_syscalls,
[email protected]286c0952013-06-13 20:29:26229 bool enable_exception_handling,
[email protected]e17f5ab2013-08-19 22:27:07230 bool enable_crash_throttling,
[email protected]7bb5d2c2013-04-29 22:33:49231 bool off_the_record,
232 const base::FilePath& profile_directory)
[email protected]87f35592012-04-08 00:49:16233 : manifest_url_(manifest_url),
[email protected]27b04df2012-12-10 23:43:10234 permissions_(GetNaClPermissions(permission_bits)),
[email protected]a575da52012-03-22 13:08:36235#if defined(OS_WIN)
236 process_launched_by_broker_(false),
237#endif
238 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38239#if defined(OS_WIN)
240 debug_exception_handler_requested_(false),
241#endif
[email protected]1d8a3d1f2011-02-19 07:11:52242 internal_(new NaClInternal()),
[email protected]9c009092013-05-01 03:14:09243 weak_factory_(this),
[email protected]84b8e8962013-06-12 22:24:06244 uses_irt_(uses_irt),
[email protected]286c0952013-06-13 20:29:26245 enable_debug_stub_(false),
[email protected]be524622013-04-23 00:12:19246 enable_dyncode_syscalls_(enable_dyncode_syscalls),
[email protected]286c0952013-06-13 20:29:26247 enable_exception_handling_(enable_exception_handling),
[email protected]e17f5ab2013-08-19 22:27:07248 enable_crash_throttling_(enable_crash_throttling),
[email protected]8510d282012-08-30 19:47:38249 off_the_record_(off_the_record),
[email protected]7bb5d2c2013-04-29 22:33:49250 profile_directory_(profile_directory),
[email protected]9c009092013-05-01 03:14:09251 ipc_plugin_listener_(this),
[email protected]e02ff722012-11-06 03:53:06252 render_view_id_(render_view_id) {
[email protected]4967f792012-01-20 22:14:40253 process_.reset(content::BrowserChildProcessHost::Create(
[email protected]f3b357692013-03-22 05:16:13254 PROCESS_TYPE_NACL_LOADER, this));
[email protected]87f35592012-04-08 00:49:16255
256 // Set the display name so the user knows what plugin the process is running.
257 // We aren't on the UI thread so getting the pref locale for language
258 // formatting isn't possible, so IDN will be lost, but this is probably OK
259 // for this use case.
260 process_->SetName(net::FormatUrl(manifest_url_, std::string()));
[email protected]5ca93be2012-03-21 20:04:06261
[email protected]2c227ff2012-08-11 00:51:32262 enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch(
263 switches::kEnableNaClDebug);
[email protected]d032f492009-09-29 00:33:46264}
265
[email protected]fb1277e82009-11-21 20:32:30266NaClProcessHost::~NaClProcessHost() {
[email protected]6e70b1982013-08-23 21:38:20267 // Report exit status only if the process was successfully started.
268 if (process_->GetData().handle != base::kNullProcessHandle) {
269 int exit_code = 0;
[email protected]547603d2013-08-27 17:59:19270 process_->GetTerminationStatus(false /* known_dead */, &exit_code);
[email protected]6e70b1982013-08-23 21:38:20271 std::string message =
272 base::StringPrintf("NaCl process exited with status %i (0x%x)",
[email protected]547603d2013-08-27 17:59:19273 exit_code, exit_code);
[email protected]6e70b1982013-08-23 21:38:20274 if (exit_code == 0) {
275 LOG(INFO) << message;
276 } else {
277 LOG(ERROR) << message;
278 }
[email protected]4cb43102011-12-02 20:24:49279 }
280
[email protected]c8e1ea32013-02-13 02:19:01281 if (internal_->socket_for_renderer != NACL_INVALID_HANDLE) {
282 if (NaClClose(internal_->socket_for_renderer) != 0) {
283 NOTREACHED() << "NaClClose() failed";
[email protected]909c2402011-05-09 11:39:04284 }
[email protected]c47ec402010-07-29 10:20:49285 }
[email protected]6294dd02013-01-09 17:27:23286
[email protected]c8e1ea32013-02-13 02:19:01287 if (internal_->socket_for_sel_ldr != NACL_INVALID_HANDLE) {
288 if (NaClClose(internal_->socket_for_sel_ldr) != 0) {
289 NOTREACHED() << "NaClClose() failed";
[email protected]909c2402011-05-09 11:39:04290 }
[email protected]c47ec402010-07-29 10:20:49291 }
292
[email protected]909c2402011-05-09 11:39:04293 if (reply_msg_) {
294 // The process failed to launch for some reason.
295 // Don't keep the renderer hanging.
296 reply_msg_->set_reply_error();
[email protected]01e59752013-06-18 00:17:35297 nacl_host_message_filter_->Send(reply_msg_);
[email protected]909c2402011-05-09 11:39:04298 }
[email protected]b39c6d92012-01-31 16:38:41299#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36300 if (process_launched_by_broker_) {
301 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06302 }
[email protected]b39c6d92012-01-31 16:38:41303#endif
[email protected]fb1277e82009-11-21 20:32:30304}
305
[email protected]e17f5ab2013-08-19 22:27:07306void NaClProcessHost::OnProcessCrashed(int exit_status) {
307 if (enable_crash_throttling_ &&
308 !CommandLine::ForCurrentProcess()->HasSwitch(
309 switches::kDisablePnaclCrashThrottling)) {
310 NaClBrowser::GetInstance()->OnProcessCrashed();
311 }
312}
313
[email protected]773ebb92011-11-15 19:06:52314// This is called at browser startup.
315// static
[email protected]6fff31c22013-07-09 02:08:19316void NaClProcessHost::EarlyStartup(NaClBrowserDelegate* delegate) {
317 NaClBrowser::SetDelegate(delegate);
318 NaClBrowser::GetInstance()->EarlyStartup();
[email protected]773ebb92011-11-15 19:06:52319#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
320 // Open the IRT file early to make sure that it isn't replaced out from
321 // under us by autoupdate.
322 NaClBrowser::GetInstance()->EnsureIrtAvailable();
323#endif
[email protected]9addd1c2012-09-15 14:28:24324 CommandLine* cmd = CommandLine::ForCurrentProcess();
325 UMA_HISTOGRAM_BOOLEAN(
326 "NaCl.nacl-gdb",
327 !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
328 UMA_HISTOGRAM_BOOLEAN(
329 "NaCl.nacl-gdb-script",
330 !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
331 UMA_HISTOGRAM_BOOLEAN(
332 "NaCl.enable-nacl-debug",
333 cmd->HasSwitch(switches::kEnableNaClDebug));
[email protected]66f409c2012-10-04 20:59:04334 NaClBrowser::GetInstance()->SetDebugPatterns(
335 cmd->GetSwitchValueASCII(switches::kNaClDebugMask));
[email protected]773ebb92011-11-15 19:06:52336}
337
[email protected]a575da52012-03-22 13:08:36338void NaClProcessHost::Launch(
[email protected]01e59752013-06-18 00:17:35339 NaClHostMessageFilter* nacl_host_message_filter,
[email protected]8f42b4d2012-03-24 14:12:33340 IPC::Message* reply_msg,
[email protected]3a0506cc2013-07-01 20:28:31341 const base::FilePath& manifest_path) {
[email protected]01e59752013-06-18 00:17:35342 nacl_host_message_filter_ = nacl_host_message_filter;
[email protected]a575da52012-03-22 13:08:36343 reply_msg_ = reply_msg;
[email protected]3a0506cc2013-07-01 20:28:31344 manifest_path_ = manifest_path;
[email protected]a575da52012-03-22 13:08:36345
[email protected]e17f5ab2013-08-19 22:27:07346 // Do not launch the requested NaCl module if NaCl is marked "unstable" due
347 // to too many crashes within a given time period.
348 if (enable_crash_throttling_ &&
349 !CommandLine::ForCurrentProcess()->HasSwitch(
350 switches::kDisablePnaclCrashThrottling) &&
351 NaClBrowser::GetInstance()->IsThrottled()) {
352 SendErrorToRenderer("Process creation was throttled due to excessive"
353 " crashes");
354 delete this;
355 return;
356 }
357
[email protected]8e39f0ef2013-07-31 04:30:46358 const CommandLine* cmd = CommandLine::ForCurrentProcess();
359#if defined(OS_WIN)
360 if (cmd->HasSwitch(switches::kEnableNaClDebug) &&
361 !cmd->HasSwitch(switches::kNoSandbox)) {
362 // We don't switch off sandbox automatically for security reasons.
363 SendErrorToRenderer("NaCl's GDB debug stub requires --no-sandbox flag"
364 " on Windows. See crbug.com/265624.");
365 delete this;
366 return;
367 }
368#endif
369 if (cmd->HasSwitch(switches::kNaClGdb) &&
370 !cmd->HasSwitch(switches::kEnableNaClDebug)) {
371 LOG(WARNING) << "--nacl-gdb flag requires --enable-nacl-debug flag";
372 }
373
[email protected]773ebb92011-11-15 19:06:52374 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03375 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]e97990f2012-05-15 22:06:06376 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
377 nacl_browser->EnsureAllResourcesAvailable();
378 if (!nacl_browser->IsOk()) {
[email protected]05c7cc72013-07-22 18:01:30379 SendErrorToRenderer("could not find all the resources needed"
380 " to launch the process");
[email protected]a575da52012-03-22 13:08:36381 delete this;
382 return;
[email protected]773ebb92011-11-15 19:06:52383 }
384
[email protected]c47ec402010-07-29 10:20:49385 // Rather than creating a socket pair in the renderer, and passing
386 // one side through the browser to sel_ldr, socket pairs are created
387 // in the browser and then passed to the renderer and sel_ldr.
388 //
389 // This is mainly for the benefit of Windows, where sockets cannot
390 // be passed in messages, but are copied via DuplicateHandle().
391 // This means the sandboxed renderer cannot send handles to the
392 // browser process.
393
[email protected]c8e1ea32013-02-13 02:19:01394 NaClHandle pair[2];
[email protected]6294dd02013-01-09 17:27:23395 // Create a connected socket
[email protected]c8e1ea32013-02-13 02:19:01396 if (NaClSocketPair(pair) == -1) {
[email protected]05c7cc72013-07-22 18:01:30397 SendErrorToRenderer("NaClSocketPair() failed");
[email protected]6294dd02013-01-09 17:27:23398 delete this;
399 return;
[email protected]c47ec402010-07-29 10:20:49400 }
[email protected]6294dd02013-01-09 17:27:23401 internal_->socket_for_renderer = pair[0];
402 internal_->socket_for_sel_ldr = pair[1];
403 SetCloseOnExec(pair[0]);
404 SetCloseOnExec(pair[1]);
[email protected]d032f492009-09-29 00:33:46405
406 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30407 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36408 delete this;
[email protected]d032f492009-09-29 00:33:46409 }
[email protected]d032f492009-09-29 00:33:46410}
[email protected]646e15552012-04-06 22:01:04411
[email protected]646e15552012-04-06 22:01:04412void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
[email protected]82d17502012-05-23 19:12:40413 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
414 switches::kNaClGdb).empty()) {
[email protected]04284252013-06-11 22:30:14415 LaunchNaClGdb();
[email protected]646e15552012-04-06 22:01:04416 }
[email protected]646e15552012-04-06 22:01:04417}
[email protected]646e15552012-04-06 22:01:04418
419#if defined(OS_WIN)
420void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
421 process_launched_by_broker_ = true;
422 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03423 if (!StartWithLaunchedProcess())
424 delete this;
[email protected]646e15552012-04-06 22:01:04425}
426
[email protected]ea6588842012-05-03 05:39:38427void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
428 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
429 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
430 Send(reply);
[email protected]646e15552012-04-06 22:01:04431}
432#endif
433
434// Needed to handle sync messages in OnMessageRecieved.
435bool NaClProcessHost::Send(IPC::Message* msg) {
436 return process_->Send(msg);
437}
438
[email protected]04284252013-06-11 22:30:14439bool NaClProcessHost::LaunchNaClGdb() {
[email protected]646e15552012-04-06 22:01:04440#if defined(OS_WIN)
[email protected]04284252013-06-11 22:30:14441 base::FilePath nacl_gdb =
442 CommandLine::ForCurrentProcess()->GetSwitchValuePath(switches::kNaClGdb);
443 CommandLine cmd_line(nacl_gdb);
444#else
[email protected]5b974952012-04-05 18:18:23445 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);
[email protected]04284252013-06-11 22:30:14452#endif
[email protected]5b974952012-04-05 18:18:23453 cmd_line.AppendArg("--eval-command");
[email protected]04284252013-06-11 22:30:14454 base::FilePath::StringType irt_path(
455 NaClBrowser::GetInstance()->GetIrtFilePath().value());
456 // Avoid back slashes because nacl-gdb uses posix escaping rules on Windows.
457 // See issue https://code.google.com/p/nativeclient/issues/detail?id=3482.
458 std::replace(irt_path.begin(), irt_path.end(), '\\', '/');
459 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt \"") + irt_path +
460 FILE_PATH_LITERAL("\""));
[email protected]3a0506cc2013-07-01 20:28:31461 if (!manifest_path_.empty()) {
[email protected]5b974952012-04-05 18:18:23462 cmd_line.AppendArg("--eval-command");
[email protected]3a0506cc2013-07-01 20:28:31463 base::FilePath::StringType manifest_path_value(manifest_path_.value());
[email protected]04284252013-06-11 22:30:14464 std::replace(manifest_path_value.begin(), manifest_path_value.end(),
465 '\\', '/');
466 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest \"") +
467 manifest_path_value + FILE_PATH_LITERAL("\""));
[email protected]5b974952012-04-05 18:18:23468 }
469 cmd_line.AppendArg("--eval-command");
[email protected]04284252013-06-11 22:30:14470 cmd_line.AppendArg("target remote :4014");
[email protected]650b2d52013-02-10 03:41:45471 base::FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
[email protected]401b90792012-05-30 11:41:13472 switches::kNaClGdbScript);
473 if (!script.empty()) {
474 cmd_line.AppendArg("--command");
475 cmd_line.AppendArgNative(script.value());
476 }
[email protected]5b974952012-04-05 18:18:23477 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
478}
479
[email protected]fb1277e82009-11-21 20:32:30480bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40481 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]ec7540f2013-01-17 00:00:35482 if (channel_id.empty()) {
[email protected]05c7cc72013-07-22 18:01:30483 SendErrorToRenderer("CreateChannel() failed");
[email protected]d032f492009-09-29 00:33:46484 return false;
[email protected]ec7540f2013-01-17 00:00:35485 }
[email protected]d032f492009-09-29 00:33:46486
[email protected]e3fc75a2011-05-05 08:20:42487 CommandLine::StringType nacl_loader_prefix;
488#if defined(OS_POSIX)
489 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
490 switches::kNaClLoaderCmdPrefix);
491#endif // defined(OS_POSIX)
492
[email protected]d032f492009-09-29 00:33:46493 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36494
495#if defined(OS_MACOSX)
496 // The Native Client process needs to be able to allocate a 1GB contiguous
497 // region to use as the client environment's virtual address space. ASLR
498 // (PIE) interferes with this by making it possible that no gap large enough
499 // to accomodate this request will exist in the child process' address
500 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
501 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49502 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36503#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49504 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
505 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36506#else
[email protected]4cb43102011-12-02 20:24:49507 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36508#endif
509
[email protected]650b2d52013-02-10 03:41:45510 base::FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30511 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46512 return false;
513
[email protected]31a665e72012-03-11 12:37:46514#if defined(OS_WIN)
515 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
516 if (RunningOnWOW64()) {
[email protected]e804b532013-06-21 14:20:36517 if (!NaClBrowser::GetInstance()->GetNaCl64ExePath(&exe_path)) {
[email protected]05c7cc72013-07-22 18:01:30518 SendErrorToRenderer("could not get path to nacl64.exe");
[email protected]31a665e72012-03-11 12:37:46519 return false;
[email protected]ec7540f2013-01-17 00:00:35520 }
[email protected]31a665e72012-03-11 12:37:46521 }
522#endif
523
[email protected]33a05af2012-03-02 18:15:51524 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
525 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13526
[email protected]05076ba22010-07-30 05:59:57527 cmd_line->AppendSwitchASCII(switches::kProcessType,
528 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44529 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]6fff31c22013-07-09 02:08:19530 if (NaClBrowser::GetDelegate()->DialogsAreSuppressed())
[email protected]93156cec2011-09-12 21:14:44531 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46532
[email protected]e3fc75a2011-05-05 08:20:42533 if (!nacl_loader_prefix.empty())
534 cmd_line->PrependWrapper(nacl_loader_prefix);
535
[email protected]103607e2010-02-01 18:57:09536 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46537#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52538 if (RunningOnWOW64()) {
[email protected]ec7540f2013-01-17 00:00:35539 if (!NaClBrokerService::GetInstance()->LaunchLoader(
540 weak_factory_.GetWeakPtr(), channel_id)) {
[email protected]05c7cc72013-07-22 18:01:30541 SendErrorToRenderer("broker service did not launch process");
[email protected]ec7540f2013-01-17 00:00:35542 return false;
543 }
[email protected]4bdde602010-06-16 03:17:35544 } else {
[email protected]34f48682013-03-20 00:30:18545 process_->Launch(new NaClSandboxedProcessLauncherDelegate,
546 cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35547 }
[email protected]103607e2010-02-01 18:57:09548#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40549 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]b345c482013-08-30 18:00:39550 base::EnvironmentMap(),
[email protected]33a05af2012-03-02 18:15:51551 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09552#endif
[email protected]d032f492009-09-29 00:33:46553
[email protected]fb1277e82009-11-21 20:32:30554 return true;
[email protected]d032f492009-09-29 00:33:46555}
556
[email protected]646e15552012-04-06 22:01:04557bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
558 bool handled = true;
559 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
560 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
561 OnQueryKnownToValidate)
562 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
563 OnSetKnownToValidate)
[email protected]041834522013-05-29 00:19:27564 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_ResolveFileToken,
565 OnResolveFileToken)
[email protected]ea6588842012-05-03 05:39:38566#if defined(OS_WIN)
567 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
568 OnAttachDebugExceptionHandler)
569#endif
[email protected]17b5a8172012-06-22 21:09:09570 IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated,
571 OnPpapiChannelCreated)
[email protected]646e15552012-04-06 22:01:04572 IPC_MESSAGE_UNHANDLED(handled = false)
573 IPC_END_MESSAGE_MAP()
574 return handled;
[email protected]103607e2010-02-01 18:57:09575}
576
[email protected]773ebb92011-11-15 19:06:52577void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03578 if (!StartWithLaunchedProcess())
579 delete this;
[email protected]773ebb92011-11-15 19:06:52580}
581
[email protected]e97990f2012-05-15 22:06:06582// Called when the NaClBrowser singleton has been fully initialized.
583void NaClProcessHost::OnResourcesReady() {
[email protected]773ebb92011-11-15 19:06:52584 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]ec7540f2013-01-17 00:00:35585 if (!nacl_browser->IsReady()) {
[email protected]05c7cc72013-07-22 18:01:30586 SendErrorToRenderer("could not acquire shared resources needed by NaCl");
[email protected]ec7540f2013-01-17 00:00:35587 delete this;
588 } else if (!SendStart()) {
[email protected]338466a82011-05-03 04:27:43589 delete this;
590 }
591}
592
[email protected]17b5a8172012-06-22 21:09:09593bool NaClProcessHost::ReplyToRenderer(
594 const IPC::ChannelHandle& channel_handle) {
[email protected]e9e810f2013-07-12 19:26:11595#if defined(OS_WIN)
596 // If we are on 64-bit Windows, the NaCl process's sandbox is
597 // managed by a different process from the renderer's sandbox. We
598 // need to inform the renderer's sandbox about the NaCl process so
599 // that the renderer can send handles to the NaCl process using
600 // BrokerDuplicateHandle().
601 if (RunningOnWOW64()) {
602 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]05c7cc72013-07-22 18:01:30603 SendErrorToRenderer("BrokerAddTargetPeer() failed");
[email protected]e9e810f2013-07-12 19:26:11604 return false;
605 }
606 }
607#endif
608
[email protected]6294dd02013-01-09 17:27:23609 nacl::FileDescriptor handle_for_renderer;
[email protected]c47ec402010-07-29 10:20:49610#if defined(OS_WIN)
[email protected]6294dd02013-01-09 17:27:23611 // Copy the handle into the renderer process.
612 HANDLE handle_in_renderer;
613 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
614 reinterpret_cast<HANDLE>(
615 internal_->socket_for_renderer),
[email protected]950be552013-07-10 19:13:02616 nacl_host_message_filter_->PeerHandle(),
[email protected]6294dd02013-01-09 17:27:23617 &handle_in_renderer,
618 0, // Unused given DUPLICATE_SAME_ACCESS.
619 FALSE,
620 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]05c7cc72013-07-22 18:01:30621 SendErrorToRenderer("DuplicateHandle() failed");
[email protected]6294dd02013-01-09 17:27:23622 return false;
[email protected]c47ec402010-07-29 10:20:49623 }
[email protected]6294dd02013-01-09 17:27:23624 handle_for_renderer = reinterpret_cast<nacl::FileDescriptor>(
625 handle_in_renderer);
626#else
627 // No need to dup the imc_handle - we don't pass it anywhere else so
628 // it cannot be closed.
629 nacl::FileDescriptor imc_handle;
630 imc_handle.fd = internal_->socket_for_renderer;
631 imc_handle.auto_close = true;
632 handle_for_renderer = imc_handle;
633#endif
[email protected]c47ec402010-07-29 10:20:49634
[email protected]108fd342013-01-04 20:46:54635 const ChildProcessData& data = process_->GetData();
[email protected]05c7cc72013-07-22 18:01:30636 SendMessageToRenderer(
637 nacl::NaClLaunchResult(handle_for_renderer,
638 channel_handle,
639 base::GetProcId(data.handle),
640 data.id),
641 std::string() /* error_message */);
[email protected]c8e1ea32013-02-13 02:19:01642 internal_->socket_for_renderer = NACL_INVALID_HANDLE;
[email protected]00d99542012-04-17 22:48:02643 return true;
644}
[email protected]fb1277e82009-11-21 20:32:30645
[email protected]05c7cc72013-07-22 18:01:30646void NaClProcessHost::SendErrorToRenderer(const std::string& error_message) {
647 LOG(ERROR) << "NaCl process launch failed: " << error_message;
648 SendMessageToRenderer(nacl::NaClLaunchResult(), error_message);
649}
650
651void NaClProcessHost::SendMessageToRenderer(
652 const nacl::NaClLaunchResult& result,
653 const std::string& error_message) {
654 DCHECK(nacl_host_message_filter_);
655 DCHECK(reply_msg_);
656 if (nacl_host_message_filter_ != NULL && reply_msg_ != NULL) {
657 NaClHostMsg_LaunchNaCl::WriteReplyParams(
658 reply_msg_, result, error_message);
659 nacl_host_message_filter_->Send(reply_msg_);
660 nacl_host_message_filter_ = NULL;
661 reply_msg_ = NULL;
662 }
663}
664
[email protected]d571c032012-09-14 12:39:18665// TCP port we chose for NaCl debug stub. It can be any other number.
666static const int kDebugStubPort = 4014;
667
668#if defined(OS_POSIX)
[email protected]bbfef1f2013-08-28 03:00:51669net::SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
[email protected]13cad0b2012-12-07 17:57:34670 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]bbfef1f2013-08-28 03:00:51671 net::SocketDescriptor s = net::kInvalidSocket;
[email protected]13cad0b2012-12-07 17:57:34672 // We allocate currently unused TCP port for debug stub tests. The port
673 // number is passed to the test via debug stub port listener.
674 if (nacl_browser->HasGdbDebugStubPortListener()) {
675 int port;
676 s = net::TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port);
[email protected]bbfef1f2013-08-28 03:00:51677 if (s != net::kInvalidSocket) {
[email protected]13cad0b2012-12-07 17:57:34678 nacl_browser->FireGdbDebugStubPortOpened(port);
679 }
680 } else {
681 s = net::TCPListenSocket::CreateAndBind("127.0.0.1", kDebugStubPort);
682 }
[email protected]bbfef1f2013-08-28 03:00:51683 if (s == net::kInvalidSocket) {
[email protected]13cad0b2012-12-07 17:57:34684 LOG(ERROR) << "failed to open socket for debug stub";
[email protected]bbfef1f2013-08-28 03:00:51685 return net::kInvalidSocket;
[email protected]13cad0b2012-12-07 17:57:34686 }
[email protected]d571c032012-09-14 12:39:18687 if (listen(s, 1)) {
688 LOG(ERROR) << "listen() failed on debug stub socket";
[email protected]13cad0b2012-12-07 17:57:34689 if (HANDLE_EINTR(close(s)) < 0)
690 PLOG(ERROR) << "failed to close debug stub socket";
[email protected]bbfef1f2013-08-28 03:00:51691 return net::kInvalidSocket;
[email protected]d571c032012-09-14 12:39:18692 }
693 return s;
694}
695#endif
696
[email protected]00d99542012-04-17 22:48:02697bool NaClProcessHost::StartNaClExecution() {
698 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
699
700 nacl::NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12701 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
702 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]6fff31c22013-07-09 02:08:19703 params.version = NaClBrowser::GetDelegate()->GetVersionString();
[email protected]00d99542012-04-17 22:48:02704 params.enable_exception_handling = enable_exception_handling_;
[email protected]66f409c2012-10-04 20:59:04705 params.enable_debug_stub = enable_debug_stub_ &&
706 NaClBrowser::GetInstance()->URLMatchesDebugPatterns(manifest_url_);
[email protected]7522077b2013-01-17 23:39:40707 // Enable PPAPI proxy channel creation only for renderer processes.
708 params.enable_ipc_proxy = enable_ppapi_proxy();
[email protected]2e1e6f02013-01-11 18:22:56709 params.uses_irt = uses_irt_;
[email protected]be524622013-04-23 00:12:19710 params.enable_dyncode_syscalls = enable_dyncode_syscalls_;
[email protected]00d99542012-04-17 22:48:02711
712 const ChildProcessData& data = process_->GetData();
[email protected]6294dd02013-01-09 17:27:23713 if (!ShareHandleToSelLdr(data.handle,
714 internal_->socket_for_sel_ldr, true,
715 &params.handles)) {
716 return false;
[email protected]773ebb92011-11-15 19:06:52717 }
718
[email protected]2e1e6f02013-01-11 18:22:56719 if (params.uses_irt) {
720 base::PlatformFile irt_file = nacl_browser->IrtFile();
721 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
[email protected]2e1e6f02013-01-11 18:22:56722 // Send over the IRT file handle. We don't close our own copy!
723 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
724 return false;
725 }
[email protected]c47ec402010-07-29 10:20:49726
[email protected]ab88d1542011-11-18 22:52:00727#if defined(OS_MACOSX)
728 // For dynamic loading support, NaCl requires a file descriptor that
729 // was created in /tmp, since those created with shm_open() are not
730 // mappable with PROT_EXEC. Rather than requiring an extra IPC
731 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25732 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31733 base::SharedMemoryCreateOptions options;
734 options.size = 1;
735 options.executable = true;
736 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03737 DLOG(ERROR) << "Failed to allocate memory buffer";
738 return false;
[email protected]2c68bf032010-11-11 23:16:30739 }
[email protected]cbbe7842011-11-17 22:01:25740 nacl::FileDescriptor memory_fd;
741 memory_fd.fd = dup(memory_buffer.handle().fd);
742 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03743 DLOG(ERROR) << "Failed to dup() a file descriptor";
744 return false;
[email protected]cbbe7842011-11-17 22:01:25745 }
746 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02747 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30748#endif
749
[email protected]d571c032012-09-14 12:39:18750#if defined(OS_POSIX)
[email protected]ed637bb2012-10-15 17:36:05751 if (params.enable_debug_stub) {
[email protected]bbfef1f2013-08-28 03:00:51752 net::SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
753 if (server_bound_socket != net::kInvalidSocket) {
[email protected]d571c032012-09-14 12:39:18754 params.debug_stub_server_bound_socket =
755 nacl::FileDescriptor(server_bound_socket, true);
756 }
757 }
758#endif
759
[email protected]ea6588842012-05-03 05:39:38760 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41761
[email protected]c8e1ea32013-02-13 02:19:01762 internal_->socket_for_sel_ldr = NACL_INVALID_HANDLE;
[email protected]09afc2e2012-04-10 17:29:03763 return true;
764}
765
[email protected]00d99542012-04-17 22:48:02766bool NaClProcessHost::SendStart() {
[email protected]7522077b2013-01-17 23:39:40767 if (!enable_ppapi_proxy()) {
[email protected]17b5a8172012-06-22 21:09:09768 if (!ReplyToRenderer(IPC::ChannelHandle()))
769 return false;
770 }
771 return StartNaClExecution();
772}
773
[email protected]8510d282012-08-30 19:47:38774// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
775// received or PpapiHostMsg_ChannelCreated is forwarded by our plugin
776// listener.
[email protected]17b5a8172012-06-22 21:09:09777void NaClProcessHost::OnPpapiChannelCreated(
778 const IPC::ChannelHandle& channel_handle) {
[email protected]7522077b2013-01-17 23:39:40779 // Only renderer processes should create a channel.
780 DCHECK(enable_ppapi_proxy());
[email protected]8510d282012-08-30 19:47:38781 // If the proxy channel is null, this must be the initial NaCl-Browser IPC
782 // channel.
783 if (!ipc_proxy_channel_.get()) {
[email protected]f3b357692013-03-22 05:16:13784 DCHECK_EQ(PROCESS_TYPE_NACL_LOADER, process_->GetData().process_type);
[email protected]9e141212013-01-21 15:21:56785
[email protected]8510d282012-08-30 19:47:38786 ipc_proxy_channel_.reset(
787 new IPC::ChannelProxy(channel_handle,
788 IPC::Channel::MODE_CLIENT,
789 &ipc_plugin_listener_,
[email protected]cadac622013-06-11 16:46:36790 base::MessageLoopProxy::current().get()));
[email protected]0c7193742012-11-07 19:05:03791 // Create the browser ppapi host and enable PPAPI message dispatching to the
792 // browser process.
[email protected]adf7cd52012-12-04 19:37:46793 ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess(
[email protected]9e141212013-01-21 15:21:56794 ipc_proxy_channel_.get(), // sender
[email protected]0c7193742012-11-07 19:05:03795 permissions_,
796 process_->GetData().handle,
[email protected]b4b53d182012-09-12 02:56:55797 ipc_proxy_channel_.get(),
[email protected]01e59752013-06-18 00:17:35798 nacl_host_message_filter_->render_process_id(),
[email protected]7bb5d2c2013-04-29 22:33:49799 render_view_id_,
800 profile_directory_));
[email protected]adf7cd52012-12-04 19:37:46801
[email protected]ac07ec52013-04-22 17:32:45802 ppapi::PpapiNaClChannelArgs args;
[email protected]01e59752013-06-18 00:17:35803 args.off_the_record = nacl_host_message_filter_->off_the_record();
[email protected]ac07ec52013-04-22 17:32:45804 args.permissions = permissions_;
805 CommandLine* cmdline = CommandLine::ForCurrentProcess();
806 DCHECK(cmdline);
807 std::string flag_whitelist[] = {switches::kV, switches::kVModule};
808 for (size_t i = 0; i < arraysize(flag_whitelist); ++i) {
809 std::string value = cmdline->GetSwitchValueASCII(flag_whitelist[i]);
810 if (!value.empty()) {
811 args.switch_names.push_back(flag_whitelist[i]);
812 args.switch_values.push_back(value);
813 }
814 }
815
[email protected]4b30e7d2013-04-24 23:52:14816 ppapi_host_->GetPpapiHost()->AddHostFactoryFilter(
817 scoped_ptr<ppapi::host::HostFactory>(
[email protected]6fff31c22013-07-09 02:08:19818 NaClBrowser::GetDelegate()->CreatePpapiHostFactory(
819 ppapi_host_.get())));
[email protected]4b30e7d2013-04-24 23:52:14820
[email protected]8510d282012-08-30 19:47:38821 // Send a message to create the NaCl-Renderer channel. The handle is just
822 // a place holder.
823 ipc_proxy_channel_->Send(
824 new PpapiMsg_CreateNaClChannel(
[email protected]01e59752013-06-18 00:17:35825 nacl_host_message_filter_->render_process_id(),
[email protected]ac07ec52013-04-22 17:32:45826 args,
[email protected]8510d282012-08-30 19:47:38827 SerializedHandle(SerializedHandle::CHANNEL_HANDLE,
828 IPC::InvalidPlatformFileForTransit())));
829 } else if (reply_msg_) {
830 // Otherwise, this must be a renderer channel.
831 ReplyToRenderer(channel_handle);
832 } else {
833 // Attempt to open more than 1 renderer channel is not supported.
834 // Shut down the NaCl process.
835 process_->GetHost()->ForceShutdown();
836 }
837}
838
839bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) {
840 // Handle messages that have been forwarded from our PluginListener.
841 // These messages come from untrusted code so should be handled with care.
842 bool handled = true;
843 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
844 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
845 OnPpapiChannelCreated)
846 IPC_MESSAGE_UNHANDLED(handled = false)
847 IPC_END_MESSAGE_MAP()
848 return handled;
[email protected]00d99542012-04-17 22:48:02849}
850
[email protected]09afc2e2012-04-10 17:29:03851bool NaClProcessHost::StartWithLaunchedProcess() {
[email protected]09afc2e2012-04-10 17:29:03852 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03853
[email protected]e97990f2012-05-15 22:06:06854 if (nacl_browser->IsReady()) {
855 return SendStart();
856 } else if (nacl_browser->IsOk()) {
857 nacl_browser->WaitForResources(
858 base::Bind(&NaClProcessHost::OnResourcesReady,
859 weak_factory_.GetWeakPtr()));
860 return true;
861 } else {
[email protected]05c7cc72013-07-22 18:01:30862 SendErrorToRenderer("previously failed to acquire shared resources");
[email protected]e97990f2012-05-15 22:06:06863 return false;
864 }
[email protected]d032f492009-09-29 00:33:46865}
866
[email protected]4a0141b2012-03-27 01:15:30867void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
868 bool* result) {
[email protected]7cfaf982012-05-09 18:37:47869 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52870 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30871}
872
873void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]57473ebc2012-05-30 07:44:52874 NaClBrowser::GetInstance()->SetKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30875}
[email protected]ea6588842012-05-03 05:39:38876
[email protected]041834522013-05-29 00:19:27877void NaClProcessHost::FileResolved(
878 base::PlatformFile* file,
879 const base::FilePath& file_path,
880 IPC::Message* reply_msg) {
881 if (*file != base::kInvalidPlatformFileValue) {
882 IPC::PlatformFileForTransit handle = IPC::GetFileHandleForProcess(
883 *file,
884 process_->GetData().handle,
885 true /* close_source */);
886 NaClProcessMsg_ResolveFileToken::WriteReplyParams(
887 reply_msg,
888 handle,
889 file_path);
890 } else {
891 NaClProcessMsg_ResolveFileToken::WriteReplyParams(
892 reply_msg,
893 IPC::InvalidPlatformFileForTransit(),
894 base::FilePath(FILE_PATH_LITERAL("")));
895 }
896 Send(reply_msg);
897}
898
899void NaClProcessHost::OnResolveFileToken(uint64 file_token_lo,
900 uint64 file_token_hi,
901 IPC::Message* reply_msg) {
902 // Was the file registered?
903 //
904 // Note that the file path cache is of bounded size, and old entries can get
905 // evicted. If a large number of NaCl modules are being launched at once,
906 // resolving the file_token may fail because the path cache was thrashed
907 // while the file_token was in flight. In this case the query fails, and we
908 // need to fall back to the slower path.
909 //
910 // However: each NaCl process will consume 2-3 entries as it starts up, this
911 // means that eviction will not happen unless you start up 33+ NaCl processes
912 // at the same time, and this still requires worst-case timing. As a
913 // practical matter, no entries should be evicted prematurely.
914 // The cache itself should take ~ (150 characters * 2 bytes/char + ~60 bytes
915 // data structure overhead) * 100 = 35k when full, so making it bigger should
916 // not be a problem, if needed.
917 //
918 // Each NaCl process will consume 2-3 entries because the manifest and main
919 // nexe are currently not resolved. Shared libraries will be resolved. They
920 // will be loaded sequentially, so they will only consume a single entry
921 // while the load is in flight.
922 //
923 // TODO(ncbray): track behavior with UMA. If entries are getting evicted or
924 // bogus keys are getting queried, this would be good to know.
925 base::FilePath file_path;
926 if (!NaClBrowser::GetInstance()->GetFilePath(file_token_lo, file_token_hi,
927 &file_path)) {
928 NaClProcessMsg_ResolveFileToken::WriteReplyParams(
929 reply_msg,
930 IPC::InvalidPlatformFileForTransit(),
931 base::FilePath(FILE_PATH_LITERAL("")));
932 Send(reply_msg);
933 return;
934 }
935
936 // Scratch space to share between the callbacks.
937 base::PlatformFile* data = new base::PlatformFile();
938
939 // Open the file.
940 if (!content::BrowserThread::PostBlockingPoolTaskAndReply(
941 FROM_HERE,
942 base::Bind(nacl::OpenNaClExecutableImpl,
943 file_path, data),
944 base::Bind(&NaClProcessHost::FileResolved,
945 weak_factory_.GetWeakPtr(),
946 base::Owned(data),
947 file_path,
948 reply_msg))) {
949 NaClProcessMsg_ResolveFileToken::WriteReplyParams(
950 reply_msg,
951 IPC::InvalidPlatformFileForTransit(),
952 base::FilePath(FILE_PATH_LITERAL("")));
953 Send(reply_msg);
954 }
955}
956
[email protected]ea6588842012-05-03 05:39:38957#if defined(OS_WIN)
958void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
959 IPC::Message* reply_msg) {
960 if (!AttachDebugExceptionHandler(info, reply_msg)) {
961 // Send failure message.
962 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
963 false);
964 Send(reply_msg);
965 }
966}
967
968bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
969 IPC::Message* reply_msg) {
[email protected]2c227ff2012-08-11 00:51:32970 if (!enable_exception_handling_ && !enable_debug_stub_) {
[email protected]ea6588842012-05-03 05:39:38971 DLOG(ERROR) <<
[email protected]2c227ff2012-08-11 00:51:32972 "Debug exception handler requested by NaCl process when not enabled";
[email protected]ea6588842012-05-03 05:39:38973 return false;
974 }
975 if (debug_exception_handler_requested_) {
976 // The NaCl process should not request this multiple times.
977 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
978 return false;
979 }
980 debug_exception_handler_requested_ = true;
981
982 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
983 base::win::ScopedHandle process_handle;
984 // We cannot use process_->GetData().handle because it does not have
985 // the necessary access rights. We open the new handle here rather
986 // than in the NaCl broker process in case the NaCl loader process
987 // dies before the NaCl broker process receives the message we send.
988 // The debug exception handler uses DebugActiveProcess() to attach,
989 // but this takes a PID. We need to prevent the NaCl loader's PID
990 // from being reused before DebugActiveProcess() is called, and
991 // holding a process handle open achieves this.
992 if (!base::OpenProcessHandleWithAccess(
993 nacl_pid,
994 base::kProcessAccessQueryInformation |
995 base::kProcessAccessSuspendResume |
996 base::kProcessAccessTerminate |
997 base::kProcessAccessVMOperation |
998 base::kProcessAccessVMRead |
999 base::kProcessAccessVMWrite |
[email protected]29172322013-01-10 17:35:161000 base::kProcessAccessDuplicateHandle |
[email protected]ea6588842012-05-03 05:39:381001 base::kProcessAccessWaitForTermination,
1002 process_handle.Receive())) {
1003 LOG(ERROR) << "Failed to get process handle";
1004 return false;
1005 }
1006
1007 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
1008 // If the NaCl loader is 64-bit, the process running its debug
1009 // exception handler must be 64-bit too, so we use the 64-bit NaCl
1010 // broker process for this. Otherwise, on a 32-bit system, we use
1011 // the 32-bit browser process to run the debug exception handler.
1012 if (RunningOnWOW64()) {
1013 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:481014 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:381015 } else {
1016 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:481017 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:381018 base::MessageLoopProxy::current(),
1019 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
1020 weak_factory_.GetWeakPtr()));
1021 return true;
1022 }
1023}
1024#endif