blob: 565a0d547cd9556a3a9d595ac202cbc451604d49 [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]7c766e92013-11-22 20:44:025#include "components/nacl/browser/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]ef42ee82013-11-01 05:26:1328#include "components/nacl/browser/nacl_browser.h"
[email protected]7c766e92013-11-22 20:44:0229#include "components/nacl/browser/nacl_host_message_filter.h"
[email protected]0acf1d42013-07-22 14:54:5830#include "components/nacl/common/nacl_cmd_line.h"
[email protected]512b5bd2013-07-23 20:24:0031#include "components/nacl/common/nacl_host_messages.h"
32#include "components/nacl/common/nacl_messages.h"
[email protected]d5d383252013-07-04 14:44:3233#include "components/nacl/common/nacl_process_type.h"
[email protected]cbb22eb2013-06-24 23:53:1034#include "components/nacl/common/nacl_switches.h"
[email protected]4967f792012-01-20 22:14:4035#include "content/public/browser/browser_child_process_host.h"
[email protected]0c7193742012-11-07 19:05:0336#include "content/public/browser/browser_ppapi_host.h"
[email protected]4967f792012-01-20 22:14:4037#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4438#include "content/public/common/child_process_host.h"
[email protected]67e76872013-07-08 02:19:3439#include "content/public/common/content_switches.h"
[email protected]9e141212013-01-21 15:21:5640#include "content/public/common/process_type.h"
[email protected]8510d282012-08-30 19:47:3841#include "ipc/ipc_channel.h"
[email protected]d032f492009-09-29 00:33:4642#include "ipc/ipc_switches.h"
[email protected]c8e1ea32013-02-13 02:19:0143#include "native_client/src/shared/imc/nacl_imc_c.h"
[email protected]87f35592012-04-08 00:49:1644#include "net/base/net_util.h"
[email protected]1946d9e2013-04-09 01:43:3745#include "net/socket/tcp_listen_socket.h"
[email protected]6fff31c22013-07-09 02:08:1946#include "ppapi/host/host_factory.h"
[email protected]4b30e7d2013-04-24 23:52:1447#include "ppapi/host/ppapi_host.h"
[email protected]8510d282012-08-30 19:47:3848#include "ppapi/proxy/ppapi_messages.h"
[email protected]ac07ec52013-04-22 17:32:4549#include "ppapi/shared_impl/ppapi_nacl_channel_args.h"
[email protected]d032f492009-09-29 00:33:4650
[email protected]d032f492009-09-29 00:33:4651#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2052#include <fcntl.h>
53
[email protected]d032f492009-09-29 00:33:4654#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3555#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2056#include <windows.h>
57
[email protected]885c0e92012-11-13 20:27:4258#include "base/threading/thread.h"
[email protected]4c65fb632012-04-27 00:42:2559#include "base/win/scoped_handle.h"
[email protected]7c766e92013-11-22 20:44:0260#include "components/nacl/browser/nacl_broker_service_win.h"
[email protected]512b5bd2013-07-23 20:24:0061#include "components/nacl/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5062#include "content/public/common/sandbox_init.h"
[email protected]34f48682013-03-20 00:30:1863#include "content/public/common/sandboxed_process_launcher_delegate.h"
[email protected]d032f492009-09-29 00:33:4664#endif
65
[email protected]631bb742011-11-02 11:29:3966using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4067using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4468using content::ChildProcessHost;
[email protected]8510d282012-08-30 19:47:3869using ppapi::proxy::SerializedHandle;
[email protected]631bb742011-11-02 11:29:3970
[email protected]25a10e232013-08-23 00:40:0871#if defined(OS_WIN)
72
73namespace {
74
75// Looks for the largest contiguous unallocated region of address
76// space and returns it via |*out_addr| and |*out_size|.
77void FindAddressSpace(base::ProcessHandle process,
78 char** out_addr, size_t* out_size) {
79 *out_addr = NULL;
80 *out_size = 0;
81 char* addr = 0;
82 while (true) {
83 MEMORY_BASIC_INFORMATION info;
84 size_t result = VirtualQueryEx(process, static_cast<void*>(addr),
85 &info, sizeof(info));
86 if (result < sizeof(info))
87 break;
88 if (info.State == MEM_FREE && info.RegionSize > *out_size) {
89 *out_addr = addr;
90 *out_size = info.RegionSize;
91 }
92 addr += info.RegionSize;
93 }
94}
95
96} // namespace
97
98namespace nacl {
99
100// Allocates |size| bytes of address space in the given process at a
101// randomised address.
102void* AllocateAddressSpaceASLR(base::ProcessHandle process, size_t size) {
103 char* addr;
104 size_t avail_size;
105 FindAddressSpace(process, &addr, &avail_size);
106 if (avail_size < size)
107 return NULL;
108 size_t offset = base::RandGenerator(avail_size - size);
109 const int kPageSize = 0x10000;
110 void* request_addr =
111 reinterpret_cast<void*>(reinterpret_cast<uint64>(addr + offset)
112 & ~(kPageSize - 1));
113 return VirtualAllocEx(process, request_addr, size,
114 MEM_RESERVE, PAGE_NOACCESS);
115}
116
117} // namespace nacl
118
119#endif // defined(OS_WIN)
120
[email protected]646e15552012-04-06 22:01:04121namespace {
122
123#if defined(OS_WIN)
124bool RunningOnWOW64() {
125 return (base::win::OSInfo::GetInstance()->wow64_status() ==
126 base::win::OSInfo::WOW64_ENABLED);
127}
[email protected]34f48682013-03-20 00:30:18128
129// NOTE: changes to this class need to be reviewed by the security team.
130class NaClSandboxedProcessLauncherDelegate
131 : public content::SandboxedProcessLauncherDelegate {
132 public:
133 NaClSandboxedProcessLauncherDelegate() {}
134 virtual ~NaClSandboxedProcessLauncherDelegate() {}
135
136 virtual void PostSpawnTarget(base::ProcessHandle process) {
[email protected]34f48682013-03-20 00:30:18137 // For Native Client sel_ldr processes on 32-bit Windows, reserve 1 GB of
138 // address space to prevent later failure due to address space fragmentation
139 // from .dll loading. The NaCl process will attempt to locate this space by
140 // scanning the address space using VirtualQuery.
141 // TODO(bbudge) Handle the --no-sandbox case.
142 // http://code.google.com/p/nativeclient/issues/detail?id=2131
[email protected]25a10e232013-08-23 00:40:08143 const SIZE_T kNaClSandboxSize = 1 << 30;
144 if (!nacl::AllocateAddressSpaceASLR(process, kNaClSandboxSize)) {
[email protected]34f48682013-03-20 00:30:18145 DLOG(WARNING) << "Failed to reserve address space for Native Client";
146 }
[email protected]34f48682013-03-20 00:30:18147 }
148};
149
150#endif // OS_WIN
[email protected]646e15552012-04-06 22:01:04151
[email protected]c8e1ea32013-02-13 02:19:01152void SetCloseOnExec(NaClHandle fd) {
[email protected]646e15552012-04-06 22:01:04153#if defined(OS_POSIX)
154 int flags = fcntl(fd, F_GETFD);
155 CHECK_NE(flags, -1);
156 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
157 CHECK_EQ(rc, 0);
158#endif
159}
160
[email protected]00d99542012-04-17 22:48:02161bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:04162 base::ProcessHandle processh,
[email protected]c8e1ea32013-02-13 02:19:01163 NaClHandle sourceh,
[email protected]646e15552012-04-06 22:01:04164 bool close_source,
165 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
166#if defined(OS_WIN)
167 HANDLE channel;
168 int flags = DUPLICATE_SAME_ACCESS;
169 if (close_source)
170 flags |= DUPLICATE_CLOSE_SOURCE;
171 if (!DuplicateHandle(GetCurrentProcess(),
172 reinterpret_cast<HANDLE>(sourceh),
173 processh,
174 &channel,
175 0, // Unused given DUPLICATE_SAME_ACCESS.
176 FALSE,
177 flags)) {
[email protected]ec7540f2013-01-17 00:00:35178 LOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:04179 return false;
180 }
181 handles_for_sel_ldr->push_back(
182 reinterpret_cast<nacl::FileDescriptor>(channel));
183#else
184 nacl::FileDescriptor channel;
185 channel.fd = sourceh;
186 channel.auto_close = close_source;
187 handles_for_sel_ldr->push_back(channel);
188#endif
189 return true;
190}
191
[email protected]27b04df2012-12-10 23:43:10192ppapi::PpapiPermissions GetNaClPermissions(uint32 permission_bits) {
193 // Only allow NaCl plugins to request certain permissions. We don't want
194 // a compromised renderer to be able to start a nacl plugin with e.g. Flash
195 // permissions which may expand the surface area of the sandbox.
196 uint32 masked_bits = permission_bits & ppapi::PERMISSION_DEV;
197 return ppapi::PpapiPermissions::GetForCommandLine(masked_bits);
198}
199
[email protected]646e15552012-04-06 22:01:04200} // namespace
201
[email protected]7c766e92013-11-22 20:44:02202namespace nacl {
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) {
[email protected]7c766e92013-11-22 20:44:02275 VLOG(1) << message;
[email protected]6e70b1982013-08-23 21:38:20276 } 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)) {
[email protected]7c766e92013-11-22 20:44:02310 NaClBrowser::GetInstance()->OnProcessCrashed();
[email protected]e17f5ab2013-08-19 22:27:07311 }
312}
313
[email protected]773ebb92011-11-15 19:06:52314// This is called at browser startup.
315// static
[email protected]a2b4c5c2013-10-18 02:06:07316void NaClProcessHost::EarlyStartup() {
[email protected]7c766e92013-11-22 20:44:02317 NaClBrowser::GetInstance()->EarlyStartup();
[email protected]773ebb92011-11-15 19:06:52318#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
319 // Open the IRT file early to make sure that it isn't replaced out from
320 // under us by autoupdate.
[email protected]7c766e92013-11-22 20:44:02321 NaClBrowser::GetInstance()->EnsureIrtAvailable();
[email protected]773ebb92011-11-15 19:06:52322#endif
[email protected]9addd1c2012-09-15 14:28:24323 CommandLine* cmd = CommandLine::ForCurrentProcess();
324 UMA_HISTOGRAM_BOOLEAN(
325 "NaCl.nacl-gdb",
326 !cmd->GetSwitchValuePath(switches::kNaClGdb).empty());
327 UMA_HISTOGRAM_BOOLEAN(
328 "NaCl.nacl-gdb-script",
329 !cmd->GetSwitchValuePath(switches::kNaClGdbScript).empty());
330 UMA_HISTOGRAM_BOOLEAN(
331 "NaCl.enable-nacl-debug",
332 cmd->HasSwitch(switches::kEnableNaClDebug));
[email protected]7c766e92013-11-22 20:44:02333 NaClBrowser::GetDelegate()->SetDebugPatterns(
[email protected]66f409c2012-10-04 20:59:04334 cmd->GetSwitchValueASCII(switches::kNaClDebugMask));
[email protected]773ebb92011-11-15 19:06:52335}
336
[email protected]a575da52012-03-22 13:08:36337void NaClProcessHost::Launch(
[email protected]01e59752013-06-18 00:17:35338 NaClHostMessageFilter* nacl_host_message_filter,
[email protected]8f42b4d2012-03-24 14:12:33339 IPC::Message* reply_msg,
[email protected]3a0506cc2013-07-01 20:28:31340 const base::FilePath& manifest_path) {
[email protected]01e59752013-06-18 00:17:35341 nacl_host_message_filter_ = nacl_host_message_filter;
[email protected]a575da52012-03-22 13:08:36342 reply_msg_ = reply_msg;
[email protected]3a0506cc2013-07-01 20:28:31343 manifest_path_ = manifest_path;
[email protected]a575da52012-03-22 13:08:36344
[email protected]e17f5ab2013-08-19 22:27:07345 // Do not launch the requested NaCl module if NaCl is marked "unstable" due
346 // to too many crashes within a given time period.
347 if (enable_crash_throttling_ &&
348 !CommandLine::ForCurrentProcess()->HasSwitch(
349 switches::kDisablePnaclCrashThrottling) &&
[email protected]7c766e92013-11-22 20:44:02350 NaClBrowser::GetInstance()->IsThrottled()) {
[email protected]e17f5ab2013-08-19 22:27:07351 SendErrorToRenderer("Process creation was throttled due to excessive"
352 " crashes");
353 delete this;
354 return;
355 }
356
[email protected]8e39f0ef2013-07-31 04:30:46357 const CommandLine* cmd = CommandLine::ForCurrentProcess();
358#if defined(OS_WIN)
359 if (cmd->HasSwitch(switches::kEnableNaClDebug) &&
360 !cmd->HasSwitch(switches::kNoSandbox)) {
361 // We don't switch off sandbox automatically for security reasons.
362 SendErrorToRenderer("NaCl's GDB debug stub requires --no-sandbox flag"
363 " on Windows. See crbug.com/265624.");
364 delete this;
365 return;
366 }
367#endif
368 if (cmd->HasSwitch(switches::kNaClGdb) &&
369 !cmd->HasSwitch(switches::kEnableNaClDebug)) {
370 LOG(WARNING) << "--nacl-gdb flag requires --enable-nacl-debug flag";
371 }
372
[email protected]773ebb92011-11-15 19:06:52373 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03374 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]7c766e92013-11-22 20:44:02375 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]e97990f2012-05-15 22:06:06376 nacl_browser->EnsureAllResourcesAvailable();
377 if (!nacl_browser->IsOk()) {
[email protected]05c7cc72013-07-22 18:01:30378 SendErrorToRenderer("could not find all the resources needed"
379 " to launch the process");
[email protected]a575da52012-03-22 13:08:36380 delete this;
381 return;
[email protected]773ebb92011-11-15 19:06:52382 }
383
[email protected]c47ec402010-07-29 10:20:49384 // Rather than creating a socket pair in the renderer, and passing
385 // one side through the browser to sel_ldr, socket pairs are created
386 // in the browser and then passed to the renderer and sel_ldr.
387 //
388 // This is mainly for the benefit of Windows, where sockets cannot
389 // be passed in messages, but are copied via DuplicateHandle().
390 // This means the sandboxed renderer cannot send handles to the
391 // browser process.
392
[email protected]c8e1ea32013-02-13 02:19:01393 NaClHandle pair[2];
[email protected]6294dd02013-01-09 17:27:23394 // Create a connected socket
[email protected]c8e1ea32013-02-13 02:19:01395 if (NaClSocketPair(pair) == -1) {
[email protected]05c7cc72013-07-22 18:01:30396 SendErrorToRenderer("NaClSocketPair() failed");
[email protected]6294dd02013-01-09 17:27:23397 delete this;
398 return;
[email protected]c47ec402010-07-29 10:20:49399 }
[email protected]6294dd02013-01-09 17:27:23400 internal_->socket_for_renderer = pair[0];
401 internal_->socket_for_sel_ldr = pair[1];
402 SetCloseOnExec(pair[0]);
403 SetCloseOnExec(pair[1]);
[email protected]d032f492009-09-29 00:33:46404
405 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30406 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36407 delete this;
[email protected]d032f492009-09-29 00:33:46408 }
[email protected]d032f492009-09-29 00:33:46409}
[email protected]646e15552012-04-06 22:01:04410
[email protected]646e15552012-04-06 22:01:04411void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
[email protected]82d17502012-05-23 19:12:40412 if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
413 switches::kNaClGdb).empty()) {
[email protected]04284252013-06-11 22:30:14414 LaunchNaClGdb();
[email protected]646e15552012-04-06 22:01:04415 }
[email protected]646e15552012-04-06 22:01:04416}
[email protected]646e15552012-04-06 22:01:04417
418#if defined(OS_WIN)
419void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
420 process_launched_by_broker_ = true;
421 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03422 if (!StartWithLaunchedProcess())
423 delete this;
[email protected]646e15552012-04-06 22:01:04424}
425
[email protected]ea6588842012-05-03 05:39:38426void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
427 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
428 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
429 Send(reply);
[email protected]646e15552012-04-06 22:01:04430}
431#endif
432
433// Needed to handle sync messages in OnMessageRecieved.
434bool NaClProcessHost::Send(IPC::Message* msg) {
435 return process_->Send(msg);
436}
437
[email protected]04284252013-06-11 22:30:14438bool NaClProcessHost::LaunchNaClGdb() {
[email protected]646e15552012-04-06 22:01:04439#if defined(OS_WIN)
[email protected]04284252013-06-11 22:30:14440 base::FilePath nacl_gdb =
441 CommandLine::ForCurrentProcess()->GetSwitchValuePath(switches::kNaClGdb);
442 CommandLine cmd_line(nacl_gdb);
443#else
[email protected]5b974952012-04-05 18:18:23444 CommandLine::StringType nacl_gdb =
445 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
446 switches::kNaClGdb);
447 CommandLine::StringVector argv;
448 // We don't support spaces inside arguments in --nacl-gdb switch.
449 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
450 CommandLine cmd_line(argv);
[email protected]04284252013-06-11 22:30:14451#endif
[email protected]5b974952012-04-05 18:18:23452 cmd_line.AppendArg("--eval-command");
[email protected]04284252013-06-11 22:30:14453 base::FilePath::StringType irt_path(
[email protected]7c766e92013-11-22 20:44:02454 NaClBrowser::GetInstance()->GetIrtFilePath().value());
[email protected]04284252013-06-11 22:30:14455 // Avoid back slashes because nacl-gdb uses posix escaping rules on Windows.
456 // See issue https://code.google.com/p/nativeclient/issues/detail?id=3482.
457 std::replace(irt_path.begin(), irt_path.end(), '\\', '/');
458 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt \"") + irt_path +
459 FILE_PATH_LITERAL("\""));
[email protected]3a0506cc2013-07-01 20:28:31460 if (!manifest_path_.empty()) {
[email protected]5b974952012-04-05 18:18:23461 cmd_line.AppendArg("--eval-command");
[email protected]3a0506cc2013-07-01 20:28:31462 base::FilePath::StringType manifest_path_value(manifest_path_.value());
[email protected]04284252013-06-11 22:30:14463 std::replace(manifest_path_value.begin(), manifest_path_value.end(),
464 '\\', '/');
465 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest \"") +
466 manifest_path_value + FILE_PATH_LITERAL("\""));
[email protected]5b974952012-04-05 18:18:23467 }
468 cmd_line.AppendArg("--eval-command");
[email protected]04284252013-06-11 22:30:14469 cmd_line.AppendArg("target remote :4014");
[email protected]650b2d52013-02-10 03:41:45470 base::FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
[email protected]401b90792012-05-30 11:41:13471 switches::kNaClGdbScript);
472 if (!script.empty()) {
473 cmd_line.AppendArg("--command");
474 cmd_line.AppendArgNative(script.value());
475 }
[email protected]5b974952012-04-05 18:18:23476 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
477}
478
[email protected]fb1277e82009-11-21 20:32:30479bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40480 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]ec7540f2013-01-17 00:00:35481 if (channel_id.empty()) {
[email protected]05c7cc72013-07-22 18:01:30482 SendErrorToRenderer("CreateChannel() failed");
[email protected]d032f492009-09-29 00:33:46483 return false;
[email protected]ec7540f2013-01-17 00:00:35484 }
[email protected]d032f492009-09-29 00:33:46485
[email protected]e3fc75a2011-05-05 08:20:42486 CommandLine::StringType nacl_loader_prefix;
487#if defined(OS_POSIX)
488 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
489 switches::kNaClLoaderCmdPrefix);
490#endif // defined(OS_POSIX)
491
[email protected]d032f492009-09-29 00:33:46492 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36493
494#if defined(OS_MACOSX)
495 // The Native Client process needs to be able to allocate a 1GB contiguous
496 // region to use as the client environment's virtual address space. ASLR
497 // (PIE) interferes with this by making it possible that no gap large enough
498 // to accomodate this request will exist in the child process' address
499 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
500 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49501 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36502#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49503 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
504 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36505#else
[email protected]4cb43102011-12-02 20:24:49506 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36507#endif
508
[email protected]650b2d52013-02-10 03:41:45509 base::FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30510 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46511 return false;
512
[email protected]31a665e72012-03-11 12:37:46513#if defined(OS_WIN)
514 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
515 if (RunningOnWOW64()) {
[email protected]7c766e92013-11-22 20:44:02516 if (!NaClBrowser::GetInstance()->GetNaCl64ExePath(&exe_path)) {
[email protected]05c7cc72013-07-22 18:01:30517 SendErrorToRenderer("could not get path to nacl64.exe");
[email protected]31a665e72012-03-11 12:37:46518 return false;
[email protected]ec7540f2013-01-17 00:00:35519 }
[email protected]31a665e72012-03-11 12:37:46520 }
521#endif
522
[email protected]33a05af2012-03-02 18:15:51523 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
[email protected]7c766e92013-11-22 20:44:02524 CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13525
[email protected]05076ba22010-07-30 05:59:57526 cmd_line->AppendSwitchASCII(switches::kProcessType,
527 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44528 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]7c766e92013-11-22 20:44:02529 if (NaClBrowser::GetDelegate()->DialogsAreSuppressed())
[email protected]93156cec2011-09-12 21:14:44530 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46531
[email protected]e3fc75a2011-05-05 08:20:42532 if (!nacl_loader_prefix.empty())
533 cmd_line->PrependWrapper(nacl_loader_prefix);
534
[email protected]103607e2010-02-01 18:57:09535 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46536#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52537 if (RunningOnWOW64()) {
[email protected]ec7540f2013-01-17 00:00:35538 if (!NaClBrokerService::GetInstance()->LaunchLoader(
539 weak_factory_.GetWeakPtr(), channel_id)) {
[email protected]05c7cc72013-07-22 18:01:30540 SendErrorToRenderer("broker service did not launch process");
[email protected]ec7540f2013-01-17 00:00:35541 return false;
542 }
[email protected]4bdde602010-06-16 03:17:35543 } else {
[email protected]34f48682013-03-20 00:30:18544 process_->Launch(new NaClSandboxedProcessLauncherDelegate,
545 cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35546 }
[email protected]103607e2010-02-01 18:57:09547#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40548 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]b345c482013-08-30 18:00:39549 base::EnvironmentMap(),
[email protected]33a05af2012-03-02 18:15:51550 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09551#endif
[email protected]d032f492009-09-29 00:33:46552
[email protected]fb1277e82009-11-21 20:32:30553 return true;
[email protected]d032f492009-09-29 00:33:46554}
555
[email protected]646e15552012-04-06 22:01:04556bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
557 bool handled = true;
558 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
559 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
560 OnQueryKnownToValidate)
561 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
562 OnSetKnownToValidate)
[email protected]041834522013-05-29 00:19:27563 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_ResolveFileToken,
564 OnResolveFileToken)
[email protected]ea6588842012-05-03 05:39:38565#if defined(OS_WIN)
566 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
567 OnAttachDebugExceptionHandler)
568#endif
[email protected]17b5a8172012-06-22 21:09:09569 IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated,
570 OnPpapiChannelCreated)
[email protected]646e15552012-04-06 22:01:04571 IPC_MESSAGE_UNHANDLED(handled = false)
572 IPC_END_MESSAGE_MAP()
573 return handled;
[email protected]103607e2010-02-01 18:57:09574}
575
[email protected]773ebb92011-11-15 19:06:52576void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03577 if (!StartWithLaunchedProcess())
578 delete this;
[email protected]773ebb92011-11-15 19:06:52579}
580
[email protected]e97990f2012-05-15 22:06:06581// Called when the NaClBrowser singleton has been fully initialized.
582void NaClProcessHost::OnResourcesReady() {
[email protected]7c766e92013-11-22 20:44:02583 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]ec7540f2013-01-17 00:00:35584 if (!nacl_browser->IsReady()) {
[email protected]05c7cc72013-07-22 18:01:30585 SendErrorToRenderer("could not acquire shared resources needed by NaCl");
[email protected]ec7540f2013-01-17 00:00:35586 delete this;
587 } else if (!SendStart()) {
[email protected]338466a82011-05-03 04:27:43588 delete this;
589 }
590}
591
[email protected]17b5a8172012-06-22 21:09:09592bool NaClProcessHost::ReplyToRenderer(
593 const IPC::ChannelHandle& channel_handle) {
[email protected]e9e810f2013-07-12 19:26:11594#if defined(OS_WIN)
595 // If we are on 64-bit Windows, the NaCl process's sandbox is
596 // managed by a different process from the renderer's sandbox. We
597 // need to inform the renderer's sandbox about the NaCl process so
598 // that the renderer can send handles to the NaCl process using
599 // BrokerDuplicateHandle().
600 if (RunningOnWOW64()) {
601 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]05c7cc72013-07-22 18:01:30602 SendErrorToRenderer("BrokerAddTargetPeer() failed");
[email protected]e9e810f2013-07-12 19:26:11603 return false;
604 }
605 }
606#endif
607
[email protected]7c766e92013-11-22 20:44:02608 FileDescriptor handle_for_renderer;
[email protected]c47ec402010-07-29 10:20:49609#if defined(OS_WIN)
[email protected]6294dd02013-01-09 17:27:23610 // Copy the handle into the renderer process.
611 HANDLE handle_in_renderer;
612 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
613 reinterpret_cast<HANDLE>(
614 internal_->socket_for_renderer),
[email protected]950be552013-07-10 19:13:02615 nacl_host_message_filter_->PeerHandle(),
[email protected]6294dd02013-01-09 17:27:23616 &handle_in_renderer,
617 0, // Unused given DUPLICATE_SAME_ACCESS.
618 FALSE,
619 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]05c7cc72013-07-22 18:01:30620 SendErrorToRenderer("DuplicateHandle() failed");
[email protected]6294dd02013-01-09 17:27:23621 return false;
[email protected]c47ec402010-07-29 10:20:49622 }
[email protected]7c766e92013-11-22 20:44:02623 handle_for_renderer = reinterpret_cast<FileDescriptor>(
[email protected]6294dd02013-01-09 17:27:23624 handle_in_renderer);
625#else
626 // No need to dup the imc_handle - we don't pass it anywhere else so
627 // it cannot be closed.
[email protected]7c766e92013-11-22 20:44:02628 FileDescriptor imc_handle;
[email protected]6294dd02013-01-09 17:27:23629 imc_handle.fd = internal_->socket_for_renderer;
630 imc_handle.auto_close = true;
631 handle_for_renderer = imc_handle;
632#endif
[email protected]c47ec402010-07-29 10:20:49633
[email protected]108fd342013-01-04 20:46:54634 const ChildProcessData& data = process_->GetData();
[email protected]05c7cc72013-07-22 18:01:30635 SendMessageToRenderer(
[email protected]7c766e92013-11-22 20:44:02636 NaClLaunchResult(handle_for_renderer,
[email protected]05c7cc72013-07-22 18:01:30637 channel_handle,
638 base::GetProcId(data.handle),
639 data.id),
640 std::string() /* error_message */);
[email protected]c8e1ea32013-02-13 02:19:01641 internal_->socket_for_renderer = NACL_INVALID_HANDLE;
[email protected]00d99542012-04-17 22:48:02642 return true;
643}
[email protected]fb1277e82009-11-21 20:32:30644
[email protected]05c7cc72013-07-22 18:01:30645void NaClProcessHost::SendErrorToRenderer(const std::string& error_message) {
646 LOG(ERROR) << "NaCl process launch failed: " << error_message;
[email protected]7c766e92013-11-22 20:44:02647 SendMessageToRenderer(NaClLaunchResult(), error_message);
[email protected]05c7cc72013-07-22 18:01:30648}
649
650void NaClProcessHost::SendMessageToRenderer(
[email protected]7c766e92013-11-22 20:44:02651 const NaClLaunchResult& result,
[email protected]05c7cc72013-07-22 18:01:30652 const std::string& error_message) {
653 DCHECK(nacl_host_message_filter_);
654 DCHECK(reply_msg_);
655 if (nacl_host_message_filter_ != NULL && reply_msg_ != NULL) {
656 NaClHostMsg_LaunchNaCl::WriteReplyParams(
657 reply_msg_, result, error_message);
658 nacl_host_message_filter_->Send(reply_msg_);
659 nacl_host_message_filter_ = NULL;
660 reply_msg_ = NULL;
661 }
662}
663
[email protected]d571c032012-09-14 12:39:18664// TCP port we chose for NaCl debug stub. It can be any other number.
665static const int kDebugStubPort = 4014;
666
667#if defined(OS_POSIX)
[email protected]bbfef1f2013-08-28 03:00:51668net::SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() {
[email protected]7c766e92013-11-22 20:44:02669 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]bbfef1f2013-08-28 03:00:51670 net::SocketDescriptor s = net::kInvalidSocket;
[email protected]13cad0b2012-12-07 17:57:34671 // We allocate currently unused TCP port for debug stub tests. The port
672 // number is passed to the test via debug stub port listener.
673 if (nacl_browser->HasGdbDebugStubPortListener()) {
674 int port;
675 s = net::TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port);
[email protected]bbfef1f2013-08-28 03:00:51676 if (s != net::kInvalidSocket) {
[email protected]13cad0b2012-12-07 17:57:34677 nacl_browser->FireGdbDebugStubPortOpened(port);
678 }
679 } else {
680 s = net::TCPListenSocket::CreateAndBind("127.0.0.1", kDebugStubPort);
681 }
[email protected]bbfef1f2013-08-28 03:00:51682 if (s == net::kInvalidSocket) {
[email protected]13cad0b2012-12-07 17:57:34683 LOG(ERROR) << "failed to open socket for debug stub";
[email protected]bbfef1f2013-08-28 03:00:51684 return net::kInvalidSocket;
[email protected]13cad0b2012-12-07 17:57:34685 }
[email protected]d571c032012-09-14 12:39:18686 if (listen(s, 1)) {
687 LOG(ERROR) << "listen() failed on debug stub socket";
[email protected]13cad0b2012-12-07 17:57:34688 if (HANDLE_EINTR(close(s)) < 0)
689 PLOG(ERROR) << "failed to close debug stub socket";
[email protected]bbfef1f2013-08-28 03:00:51690 return net::kInvalidSocket;
[email protected]d571c032012-09-14 12:39:18691 }
692 return s;
693}
694#endif
695
[email protected]00d99542012-04-17 22:48:02696bool NaClProcessHost::StartNaClExecution() {
[email protected]7c766e92013-11-22 20:44:02697 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]00d99542012-04-17 22:48:02698
[email protected]7c766e92013-11-22 20:44:02699 NaClStartParams params;
[email protected]8c670832012-05-26 04:30:12700 params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled();
701 params.validation_cache_key = nacl_browser->GetValidationCacheKey();
[email protected]7c766e92013-11-22 20:44:02702 params.version = NaClBrowser::GetDelegate()->GetVersionString();
[email protected]00d99542012-04-17 22:48:02703 params.enable_exception_handling = enable_exception_handling_;
[email protected]66f409c2012-10-04 20:59:04704 params.enable_debug_stub = enable_debug_stub_ &&
[email protected]7c766e92013-11-22 20:44:02705 NaClBrowser::GetDelegate()->URLMatchesDebugPatterns(manifest_url_);
[email protected]7522077b2013-01-17 23:39:40706 // Enable PPAPI proxy channel creation only for renderer processes.
707 params.enable_ipc_proxy = enable_ppapi_proxy();
[email protected]2e1e6f02013-01-11 18:22:56708 params.uses_irt = uses_irt_;
[email protected]be524622013-04-23 00:12:19709 params.enable_dyncode_syscalls = enable_dyncode_syscalls_;
[email protected]00d99542012-04-17 22:48:02710
711 const ChildProcessData& data = process_->GetData();
[email protected]6294dd02013-01-09 17:27:23712 if (!ShareHandleToSelLdr(data.handle,
713 internal_->socket_for_sel_ldr, true,
714 &params.handles)) {
715 return false;
[email protected]773ebb92011-11-15 19:06:52716 }
717
[email protected]2e1e6f02013-01-11 18:22:56718 if (params.uses_irt) {
719 base::PlatformFile irt_file = nacl_browser->IrtFile();
720 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
[email protected]2e1e6f02013-01-11 18:22:56721 // Send over the IRT file handle. We don't close our own copy!
722 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
723 return false;
724 }
[email protected]c47ec402010-07-29 10:20:49725
[email protected]ab88d1542011-11-18 22:52:00726#if defined(OS_MACOSX)
727 // For dynamic loading support, NaCl requires a file descriptor that
728 // was created in /tmp, since those created with shm_open() are not
729 // mappable with PROT_EXEC. Rather than requiring an extra IPC
730 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25731 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31732 base::SharedMemoryCreateOptions options;
733 options.size = 1;
734 options.executable = true;
735 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03736 DLOG(ERROR) << "Failed to allocate memory buffer";
737 return false;
[email protected]2c68bf032010-11-11 23:16:30738 }
[email protected]7c766e92013-11-22 20:44:02739 FileDescriptor memory_fd;
[email protected]cbbe7842011-11-17 22:01:25740 memory_fd.fd = dup(memory_buffer.handle().fd);
741 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03742 DLOG(ERROR) << "Failed to dup() a file descriptor";
743 return false;
[email protected]cbbe7842011-11-17 22:01:25744 }
745 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02746 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30747#endif
748
[email protected]d571c032012-09-14 12:39:18749#if defined(OS_POSIX)
[email protected]ed637bb2012-10-15 17:36:05750 if (params.enable_debug_stub) {
[email protected]bbfef1f2013-08-28 03:00:51751 net::SocketDescriptor server_bound_socket = GetDebugStubSocketHandle();
752 if (server_bound_socket != net::kInvalidSocket) {
[email protected]d571c032012-09-14 12:39:18753 params.debug_stub_server_bound_socket =
[email protected]7c766e92013-11-22 20:44:02754 FileDescriptor(server_bound_socket, true);
[email protected]d571c032012-09-14 12:39:18755 }
756 }
757#endif
758
[email protected]ea6588842012-05-03 05:39:38759 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41760
[email protected]c8e1ea32013-02-13 02:19:01761 internal_->socket_for_sel_ldr = NACL_INVALID_HANDLE;
[email protected]09afc2e2012-04-10 17:29:03762 return true;
763}
764
[email protected]00d99542012-04-17 22:48:02765bool NaClProcessHost::SendStart() {
[email protected]7522077b2013-01-17 23:39:40766 if (!enable_ppapi_proxy()) {
[email protected]17b5a8172012-06-22 21:09:09767 if (!ReplyToRenderer(IPC::ChannelHandle()))
768 return false;
769 }
770 return StartNaClExecution();
771}
772
[email protected]8510d282012-08-30 19:47:38773// This method is called when NaClProcessHostMsg_PpapiChannelCreated is
774// received or PpapiHostMsg_ChannelCreated is forwarded by our plugin
775// listener.
[email protected]17b5a8172012-06-22 21:09:09776void NaClProcessHost::OnPpapiChannelCreated(
777 const IPC::ChannelHandle& channel_handle) {
[email protected]7522077b2013-01-17 23:39:40778 // Only renderer processes should create a channel.
779 DCHECK(enable_ppapi_proxy());
[email protected]8510d282012-08-30 19:47:38780 // If the proxy channel is null, this must be the initial NaCl-Browser IPC
781 // channel.
782 if (!ipc_proxy_channel_.get()) {
[email protected]f3b357692013-03-22 05:16:13783 DCHECK_EQ(PROCESS_TYPE_NACL_LOADER, process_->GetData().process_type);
[email protected]9e141212013-01-21 15:21:56784
[email protected]8510d282012-08-30 19:47:38785 ipc_proxy_channel_.reset(
786 new IPC::ChannelProxy(channel_handle,
787 IPC::Channel::MODE_CLIENT,
788 &ipc_plugin_listener_,
[email protected]cadac622013-06-11 16:46:36789 base::MessageLoopProxy::current().get()));
[email protected]0c7193742012-11-07 19:05:03790 // Create the browser ppapi host and enable PPAPI message dispatching to the
791 // browser process.
[email protected]adf7cd52012-12-04 19:37:46792 ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess(
[email protected]9e141212013-01-21 15:21:56793 ipc_proxy_channel_.get(), // sender
[email protected]0c7193742012-11-07 19:05:03794 permissions_,
795 process_->GetData().handle,
[email protected]b4b53d182012-09-12 02:56:55796 ipc_proxy_channel_.get(),
[email protected]01e59752013-06-18 00:17:35797 nacl_host_message_filter_->render_process_id(),
[email protected]7bb5d2c2013-04-29 22:33:49798 render_view_id_,
799 profile_directory_));
[email protected]adf7cd52012-12-04 19:37:46800
[email protected]ac07ec52013-04-22 17:32:45801 ppapi::PpapiNaClChannelArgs args;
[email protected]01e59752013-06-18 00:17:35802 args.off_the_record = nacl_host_message_filter_->off_the_record();
[email protected]ac07ec52013-04-22 17:32:45803 args.permissions = permissions_;
804 CommandLine* cmdline = CommandLine::ForCurrentProcess();
805 DCHECK(cmdline);
806 std::string flag_whitelist[] = {switches::kV, switches::kVModule};
807 for (size_t i = 0; i < arraysize(flag_whitelist); ++i) {
808 std::string value = cmdline->GetSwitchValueASCII(flag_whitelist[i]);
809 if (!value.empty()) {
810 args.switch_names.push_back(flag_whitelist[i]);
811 args.switch_values.push_back(value);
812 }
813 }
814
[email protected]4b30e7d2013-04-24 23:52:14815 ppapi_host_->GetPpapiHost()->AddHostFactoryFilter(
816 scoped_ptr<ppapi::host::HostFactory>(
[email protected]7c766e92013-11-22 20:44:02817 NaClBrowser::GetDelegate()->CreatePpapiHostFactory(
[email protected]6fff31c22013-07-09 02:08:19818 ppapi_host_.get())));
[email protected]4b30e7d2013-04-24 23:52:14819
[email protected]8510d282012-08-30 19:47:38820 // Send a message to create the NaCl-Renderer channel. The handle is just
821 // a place holder.
822 ipc_proxy_channel_->Send(
823 new PpapiMsg_CreateNaClChannel(
[email protected]01e59752013-06-18 00:17:35824 nacl_host_message_filter_->render_process_id(),
[email protected]ac07ec52013-04-22 17:32:45825 args,
[email protected]8510d282012-08-30 19:47:38826 SerializedHandle(SerializedHandle::CHANNEL_HANDLE,
827 IPC::InvalidPlatformFileForTransit())));
828 } else if (reply_msg_) {
829 // Otherwise, this must be a renderer channel.
830 ReplyToRenderer(channel_handle);
831 } else {
832 // Attempt to open more than 1 renderer channel is not supported.
833 // Shut down the NaCl process.
834 process_->GetHost()->ForceShutdown();
835 }
836}
837
838bool NaClProcessHost::OnUntrustedMessageForwarded(const IPC::Message& msg) {
839 // Handle messages that have been forwarded from our PluginListener.
840 // These messages come from untrusted code so should be handled with care.
841 bool handled = true;
842 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
843 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
844 OnPpapiChannelCreated)
845 IPC_MESSAGE_UNHANDLED(handled = false)
846 IPC_END_MESSAGE_MAP()
847 return handled;
[email protected]00d99542012-04-17 22:48:02848}
849
[email protected]09afc2e2012-04-10 17:29:03850bool NaClProcessHost::StartWithLaunchedProcess() {
[email protected]7c766e92013-11-22 20:44:02851 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03852
[email protected]e97990f2012-05-15 22:06:06853 if (nacl_browser->IsReady()) {
854 return SendStart();
855 } else if (nacl_browser->IsOk()) {
856 nacl_browser->WaitForResources(
857 base::Bind(&NaClProcessHost::OnResourcesReady,
858 weak_factory_.GetWeakPtr()));
859 return true;
860 } else {
[email protected]05c7cc72013-07-22 18:01:30861 SendErrorToRenderer("previously failed to acquire shared resources");
[email protected]e97990f2012-05-15 22:06:06862 return false;
863 }
[email protected]d032f492009-09-29 00:33:46864}
865
[email protected]4a0141b2012-03-27 01:15:30866void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
867 bool* result) {
[email protected]7c766e92013-11-22 20:44:02868 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]57473ebc2012-05-30 07:44:52869 *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_);
[email protected]4a0141b2012-03-27 01:15:30870}
871
872void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]7c766e92013-11-22 20:44:02873 NaClBrowser::GetInstance()->SetKnownToValidate(
[email protected]ef42ee82013-11-01 05:26:13874 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;
[email protected]7c766e92013-11-22 20:44:02926 if (!NaClBrowser::GetInstance()->GetFilePath(
[email protected]ef42ee82013-11-01 05:26:13927 file_token_lo, file_token_hi, &file_path)) {
[email protected]041834522013-05-29 00:19:27928 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,
[email protected]7c766e92013-11-22 20:44:02942 base::Bind(OpenNaClExecutableImpl,
[email protected]041834522013-05-29 00:19:27943 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
[email protected]7c766e92013-11-22 20:44:021025
1026} // namespace nacl