blob: 4c296be57167b897c98ed4066952e8006018bdef [file] [log] [blame]
[email protected]b39c6d92012-01-31 16:38:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d032f492009-09-29 00:33:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e15a4fa2010-02-11 23:09:295#include "chrome/browser/nacl_host/nacl_process_host.h"
[email protected]d032f492009-09-29 00:33:466
[email protected]a82af392012-02-24 04:40:207#include <string>
8#include <vector>
[email protected]d032f492009-09-29 00:33:469
[email protected]30c1eea2011-10-17 18:40:3010#include "base/bind.h"
[email protected]103607e2010-02-01 18:57:0911#include "base/command_line.h"
[email protected]5b974952012-04-05 18:18:2312#include "base/message_loop.h"
[email protected]338466a82011-05-03 04:27:4313#include "base/path_service.h"
[email protected]5b974952012-04-05 18:18:2314#include "base/string_number_conversions.h"
15#include "base/string_split.h"
[email protected]8f42b4d2012-03-24 14:12:3316#include "base/string_util.h"
[email protected]a0a69bf2011-09-23 21:40:2817#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2218#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3719#include "base/win/windows_version.h"
[email protected]a82af392012-02-24 04:40:2020#include "build/build_config.h"
[email protected]8f42b4d2012-03-24 14:12:3321#include "chrome/browser/extensions/extension_info_map.h"
[email protected]2e5f2ea42012-05-09 21:39:2122#include "chrome/browser/nacl_host/nacl_browser.h"
[email protected]8f42b4d2012-03-24 14:12:3323#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]31a665e72012-03-11 12:37:4624#include "chrome/common/chrome_constants.h"
[email protected]338466a82011-05-03 04:27:4325#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4626#include "chrome/common/chrome_switches.h"
[email protected]1657e6d2012-03-30 20:28:0027#include "chrome/common/chrome_version_info.h"
[email protected]d032f492009-09-29 00:33:4628#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0929#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4630#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3031#include "chrome/common/render_messages.h"
[email protected]8f42b4d2012-03-24 14:12:3332#include "chrome/common/url_constants.h"
[email protected]4967f792012-01-20 22:14:4033#include "content/public/browser/browser_child_process_host.h"
34#include "content/public/browser/child_process_data.h"
[email protected]4734d0b2011-12-03 07:10:4435#include "content/public/common/child_process_host.h"
[email protected]d032f492009-09-29 00:33:4636#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5237#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]87f35592012-04-08 00:49:1638#include "net/base/net_util.h"
[email protected]d032f492009-09-29 00:33:4639
[email protected]d032f492009-09-29 00:33:4640#if defined(OS_POSIX)
[email protected]a82af392012-02-24 04:40:2041#include <fcntl.h>
42
[email protected]d032f492009-09-29 00:33:4643#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3544#elif defined(OS_WIN)
[email protected]a82af392012-02-24 04:40:2045#include <windows.h>
46
[email protected]b39c6d92012-01-31 16:38:4147#include "base/threading/thread.h"
48#include "base/process_util.h"
[email protected]4c65fb632012-04-27 00:42:2549#include "base/win/scoped_handle.h"
[email protected]4bdde602010-06-16 03:17:3550#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]ea6588842012-05-03 05:39:3851#include "chrome/common/nacl_debug_exception_handler_win.h"
[email protected]e4f6eb0232012-04-17 00:47:5052#include "content/public/common/sandbox_init.h"
[email protected]d032f492009-09-29 00:33:4653#endif
54
[email protected]631bb742011-11-02 11:29:3955using content::BrowserThread;
[email protected]4967f792012-01-20 22:14:4056using content::ChildProcessData;
[email protected]4734d0b2011-12-03 07:10:4457using content::ChildProcessHost;
[email protected]631bb742011-11-02 11:29:3958
[email protected]646e15552012-04-06 22:01:0459namespace {
60
61#if defined(OS_WIN)
62bool RunningOnWOW64() {
63 return (base::win::OSInfo::GetInstance()->wow64_status() ==
64 base::win::OSInfo::WOW64_ENABLED);
65}
66#endif
67
[email protected]646e15552012-04-06 22:01:0468void SetCloseOnExec(nacl::Handle fd) {
69#if defined(OS_POSIX)
70 int flags = fcntl(fd, F_GETFD);
71 CHECK_NE(flags, -1);
72 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
73 CHECK_EQ(rc, 0);
74#endif
75}
76
[email protected]00d99542012-04-17 22:48:0277bool ShareHandleToSelLdr(
[email protected]646e15552012-04-06 22:01:0478 base::ProcessHandle processh,
79 nacl::Handle sourceh,
80 bool close_source,
81 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
82#if defined(OS_WIN)
83 HANDLE channel;
84 int flags = DUPLICATE_SAME_ACCESS;
85 if (close_source)
86 flags |= DUPLICATE_CLOSE_SOURCE;
87 if (!DuplicateHandle(GetCurrentProcess(),
88 reinterpret_cast<HANDLE>(sourceh),
89 processh,
90 &channel,
91 0, // Unused given DUPLICATE_SAME_ACCESS.
92 FALSE,
93 flags)) {
[email protected]09afc2e2012-04-10 17:29:0394 DLOG(ERROR) << "DuplicateHandle() failed";
[email protected]646e15552012-04-06 22:01:0495 return false;
96 }
97 handles_for_sel_ldr->push_back(
98 reinterpret_cast<nacl::FileDescriptor>(channel));
99#else
100 nacl::FileDescriptor channel;
101 channel.fd = sourceh;
102 channel.auto_close = close_source;
103 handles_for_sel_ldr->push_back(channel);
104#endif
105 return true;
106}
107
[email protected]646e15552012-04-06 22:01:04108} // namespace
109
[email protected]1d8a3d1f2011-02-19 07:11:52110struct NaClProcessHost::NaClInternal {
111 std::vector<nacl::Handle> sockets_for_renderer;
112 std::vector<nacl::Handle> sockets_for_sel_ldr;
113};
114
[email protected]646e15552012-04-06 22:01:04115// -----------------------------------------------------------------------------
[email protected]773ebb92011-11-15 19:06:52116
[email protected]87f35592012-04-08 00:49:16117NaClProcessHost::NaClProcessHost(const GURL& manifest_url)
118 : manifest_url_(manifest_url),
[email protected]a575da52012-03-22 13:08:36119#if defined(OS_WIN)
120 process_launched_by_broker_(false),
[email protected]5b974952012-04-05 18:18:23121#elif defined(OS_LINUX)
122 wait_for_nacl_gdb_(false),
[email protected]a575da52012-03-22 13:08:36123#endif
124 reply_msg_(NULL),
[email protected]ea6588842012-05-03 05:39:38125#if defined(OS_WIN)
126 debug_exception_handler_requested_(false),
127#endif
[email protected]1d8a3d1f2011-02-19 07:11:52128 internal_(new NaClInternal()),
[email protected]5ca93be2012-03-21 20:04:06129 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
130 enable_exception_handling_(false) {
[email protected]4967f792012-01-20 22:14:40131 process_.reset(content::BrowserChildProcessHost::Create(
132 content::PROCESS_TYPE_NACL_LOADER, this));
[email protected]87f35592012-04-08 00:49:16133
134 // Set the display name so the user knows what plugin the process is running.
135 // We aren't on the UI thread so getting the pref locale for language
136 // formatting isn't possible, so IDN will be lost, but this is probably OK
137 // for this use case.
138 process_->SetName(net::FormatUrl(manifest_url_, std::string()));
[email protected]5ca93be2012-03-21 20:04:06139
140 // We allow untrusted hardware exception handling to be enabled via
141 // an env var for consistency with the standalone build of NaCl.
142 if (CommandLine::ForCurrentProcess()->HasSwitch(
143 switches::kEnableNaClExceptionHandling) ||
144 getenv("NACL_UNTRUSTED_EXCEPTION_HANDLING") != NULL) {
145 enable_exception_handling_ = true;
[email protected]5ca93be2012-03-21 20:04:06146 }
[email protected]d032f492009-09-29 00:33:46147}
148
[email protected]fb1277e82009-11-21 20:32:30149NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49150 int exit_code;
[email protected]4967f792012-01-20 22:14:40151 process_->GetTerminationStatus(&exit_code);
[email protected]4cb43102011-12-02 20:24:49152 std::string message =
153 base::StringPrintf("NaCl process exited with status %i (0x%x)",
154 exit_code, exit_code);
155 if (exit_code == 0) {
156 LOG(INFO) << message;
157 } else {
158 LOG(ERROR) << message;
159 }
160
[email protected]1d8a3d1f2011-02-19 07:11:52161 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04162 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03163 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04164 }
[email protected]c47ec402010-07-29 10:20:49165 }
[email protected]1d8a3d1f2011-02-19 07:11:52166 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04167 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
[email protected]09afc2e2012-04-10 17:29:03168 NOTREACHED() << "nacl::Close() failed";
[email protected]909c2402011-05-09 11:39:04169 }
[email protected]c47ec402010-07-29 10:20:49170 }
171
[email protected]909c2402011-05-09 11:39:04172 if (reply_msg_) {
173 // The process failed to launch for some reason.
174 // Don't keep the renderer hanging.
175 reply_msg_->set_reply_error();
176 chrome_render_message_filter_->Send(reply_msg_);
177 }
[email protected]b39c6d92012-01-31 16:38:41178#if defined(OS_WIN)
[email protected]a575da52012-03-22 13:08:36179 if (process_launched_by_broker_) {
180 NaClBrokerService::GetInstance()->OnLoaderDied();
[email protected]5ca93be2012-03-21 20:04:06181 }
[email protected]b39c6d92012-01-31 16:38:41182#endif
[email protected]fb1277e82009-11-21 20:32:30183}
184
[email protected]773ebb92011-11-15 19:06:52185// This is called at browser startup.
186// static
187void NaClProcessHost::EarlyStartup() {
188#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
189 // Open the IRT file early to make sure that it isn't replaced out from
190 // under us by autoupdate.
191 NaClBrowser::GetInstance()->EnsureIrtAvailable();
192#endif
193}
194
[email protected]a575da52012-03-22 13:08:36195void NaClProcessHost::Launch(
[email protected]92d56412011-03-24 20:53:52196 ChromeRenderMessageFilter* chrome_render_message_filter,
197 int socket_count,
[email protected]8f42b4d2012-03-24 14:12:33198 IPC::Message* reply_msg,
199 scoped_refptr<ExtensionInfoMap> extension_info_map) {
[email protected]a575da52012-03-22 13:08:36200 chrome_render_message_filter_ = chrome_render_message_filter;
201 reply_msg_ = reply_msg;
[email protected]8f42b4d2012-03-24 14:12:33202 extension_info_map_ = extension_info_map;
[email protected]a575da52012-03-22 13:08:36203
[email protected]c47ec402010-07-29 10:20:49204 // Place an arbitrary limit on the number of sockets to limit
205 // exposure in case the renderer is compromised. We can increase
206 // this if necessary.
207 if (socket_count > 8) {
[email protected]a575da52012-03-22 13:08:36208 delete this;
209 return;
[email protected]c47ec402010-07-29 10:20:49210 }
211
[email protected]773ebb92011-11-15 19:06:52212 // Start getting the IRT open asynchronously while we launch the NaCl process.
[email protected]09afc2e2012-04-10 17:29:03213 // We'll make sure this actually finished in StartWithLaunchedProcess, below.
[email protected]773ebb92011-11-15 19:06:52214 if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) {
[email protected]09afc2e2012-04-10 17:29:03215 DLOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]a575da52012-03-22 13:08:36216 delete this;
217 return;
[email protected]773ebb92011-11-15 19:06:52218 }
219
[email protected]c47ec402010-07-29 10:20:49220 // Rather than creating a socket pair in the renderer, and passing
221 // one side through the browser to sel_ldr, socket pairs are created
222 // in the browser and then passed to the renderer and sel_ldr.
223 //
224 // This is mainly for the benefit of Windows, where sockets cannot
225 // be passed in messages, but are copied via DuplicateHandle().
226 // This means the sandboxed renderer cannot send handles to the
227 // browser process.
228
229 for (int i = 0; i < socket_count; i++) {
230 nacl::Handle pair[2];
231 // Create a connected socket
[email protected]a575da52012-03-22 13:08:36232 if (nacl::SocketPair(pair) == -1) {
233 delete this;
234 return;
235 }
[email protected]1d8a3d1f2011-02-19 07:11:52236 internal_->sockets_for_renderer.push_back(pair[0]);
237 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49238 SetCloseOnExec(pair[0]);
239 SetCloseOnExec(pair[1]);
240 }
[email protected]d032f492009-09-29 00:33:46241
242 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30243 if (!LaunchSelLdr()) {
[email protected]a575da52012-03-22 13:08:36244 delete this;
[email protected]d032f492009-09-29 00:33:46245 }
[email protected]d032f492009-09-29 00:33:46246}
[email protected]646e15552012-04-06 22:01:04247
[email protected]5b974952012-04-05 18:18:23248#if defined(OS_WIN)
[email protected]646e15552012-04-06 22:01:04249void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
250 // Set process handle, if it was not set previously.
251 // This is needed when NaCl process is launched with nacl-gdb.
252 if (process_->GetData().handle == base::kNullProcessHandle) {
253 base::ProcessHandle process;
254 DCHECK(!CommandLine::ForCurrentProcess()->GetSwitchValuePath(
255 switches::kNaClGdb).empty());
256 if (base::OpenProcessHandleWithAccess(
257 peer_pid,
258 base::kProcessAccessDuplicateHandle |
259 base::kProcessAccessQueryInformation |
260 base::kProcessAccessWaitForTermination,
261 &process)) {
262 process_->SetHandle(process);
[email protected]09afc2e2012-04-10 17:29:03263 if (!StartWithLaunchedProcess()) {
264 delete this;
265 return;
266 }
[email protected]646e15552012-04-06 22:01:04267 } else {
[email protected]09afc2e2012-04-10 17:29:03268 DLOG(ERROR) << "Failed to get process handle";
[email protected]646e15552012-04-06 22:01:04269 }
270 }
[email protected]646e15552012-04-06 22:01:04271}
272#else
273void NaClProcessHost::OnChannelConnected(int32 peer_pid) {
274}
275#endif
276
277#if defined(OS_WIN)
278void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
279 process_launched_by_broker_ = true;
280 process_->SetHandle(handle);
[email protected]09afc2e2012-04-10 17:29:03281 if (!StartWithLaunchedProcess())
282 delete this;
[email protected]646e15552012-04-06 22:01:04283}
284
[email protected]ea6588842012-05-03 05:39:38285void NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker(bool success) {
286 IPC::Message* reply = attach_debug_exception_handler_reply_msg_.release();
287 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply, success);
288 Send(reply);
[email protected]646e15552012-04-06 22:01:04289}
290#endif
291
292// Needed to handle sync messages in OnMessageRecieved.
293bool NaClProcessHost::Send(IPC::Message* msg) {
294 return process_->Send(msg);
295}
296
297#if defined(OS_WIN)
298scoped_ptr<CommandLine> NaClProcessHost::GetCommandForLaunchWithGdb(
[email protected]8f42b4d2012-03-24 14:12:33299 const FilePath& nacl_gdb,
[email protected]5b974952012-04-05 18:18:23300 CommandLine* line) {
[email protected]31a665e72012-03-11 12:37:46301 CommandLine* cmd_line = new CommandLine(nacl_gdb);
302 // We can't use PrependWrapper because our parameters contain spaces.
303 cmd_line->AppendArg("--eval-command");
304 const FilePath::StringType& irt_path =
305 NaClBrowser::GetInstance()->GetIrtFilePath().value();
306 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
[email protected]5b974952012-04-05 18:18:23307 FilePath manifest_path = GetManifestPath();
[email protected]8f42b4d2012-03-24 14:12:33308 if (!manifest_path.empty()) {
309 cmd_line->AppendArg("--eval-command");
310 cmd_line->AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
311 manifest_path.value());
312 }
[email protected]31a665e72012-03-11 12:37:46313 cmd_line->AppendArg("--args");
314 const CommandLine::StringVector& argv = line->argv();
315 for (size_t i = 0; i < argv.size(); i++) {
316 cmd_line->AppendArgNative(argv[i]);
317 }
318 return scoped_ptr<CommandLine>(cmd_line);
319}
[email protected]5b974952012-04-05 18:18:23320#elif defined(OS_LINUX)
321namespace {
322class NaClGdbWatchDelegate : public MessageLoopForIO::Watcher {
323 public:
324 // fd_write_ is used by nacl-gdb via /proc/browser_PID/fd/fd_write_
325 NaClGdbWatchDelegate(int fd_read, int fd_write,
326 const base::Closure& reply)
327 : fd_read_(fd_read),
328 fd_write_(fd_write),
329 reply_(reply) {}
330
331 ~NaClGdbWatchDelegate() {
332 if (HANDLE_EINTR(close(fd_read_)) != 0)
333 DLOG(ERROR) << "close(fd_read_) failed";
334 if (HANDLE_EINTR(close(fd_write_)) != 0)
335 DLOG(ERROR) << "close(fd_write_) failed";
336 }
337
338 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
339 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
340
341 private:
342 int fd_read_;
343 int fd_write_;
344 base::Closure reply_;
345};
346
347void NaClGdbWatchDelegate::OnFileCanReadWithoutBlocking(int fd) {
348 char buf;
349 if (HANDLE_EINTR(read(fd_read_, &buf, 1)) != 1 || buf != '\0')
350 LOG(ERROR) << "Failed to sync with nacl-gdb";
351 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, reply_);
352}
353} // namespace
354
355bool NaClProcessHost::LaunchNaClGdb(base::ProcessId pid) {
356 CommandLine::StringType nacl_gdb =
357 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
358 switches::kNaClGdb);
359 CommandLine::StringVector argv;
360 // We don't support spaces inside arguments in --nacl-gdb switch.
361 base::SplitString(nacl_gdb, static_cast<CommandLine::CharType>(' '), &argv);
362 CommandLine cmd_line(argv);
363 cmd_line.AppendArg("--eval-command");
364 const FilePath::StringType& irt_path =
365 NaClBrowser::GetInstance()->GetIrtFilePath().value();
366 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-irt ") + irt_path);
367 FilePath manifest_path = GetManifestPath();
368 if (!manifest_path.empty()) {
369 cmd_line.AppendArg("--eval-command");
370 cmd_line.AppendArgNative(FILE_PATH_LITERAL("nacl-manifest ") +
371 manifest_path.value());
372 }
373 cmd_line.AppendArg("--eval-command");
374 cmd_line.AppendArg("attach " + base::IntToString(pid));
375 int fds[2];
376 if (pipe(fds) != 0)
377 return false;
378 // Tell the debugger to send a byte to the writable end of the pipe.
379 // We use a file descriptor in our process because the debugger will be
380 // typically launched in a separate terminal, and a lot of terminals close all
381 // file descriptors before launching external programs.
382 cmd_line.AppendArg("--eval-command");
383 cmd_line.AppendArg("dump binary value /proc/" +
384 base::IntToString(base::GetCurrentProcId()) +
385 "/fd/" + base::IntToString(fds[1]) + " (char)0");
386 // wait on fds[0]
387 // If the debugger crashes before attaching to the NaCl process, the user can
388 // release resources by terminating the NaCl loader in Chrome Task Manager.
389 nacl_gdb_watcher_delegate_.reset(
390 new NaClGdbWatchDelegate(
391 fds[0], fds[1],
392 base::Bind(&NaClProcessHost::OnNaClGdbAttached,
393 weak_factory_.GetWeakPtr())));
394 MessageLoopForIO::current()->WatchFileDescriptor(
395 fds[0],
396 true,
397 MessageLoopForIO::WATCH_READ,
398 &nacl_gdb_watcher_,
399 nacl_gdb_watcher_delegate_.get());
400 return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
401}
402
403void NaClProcessHost::OnNaClGdbAttached() {
404 wait_for_nacl_gdb_ = false;
405 nacl_gdb_watcher_.StopWatchingFileDescriptor();
406 nacl_gdb_watcher_delegate_.reset();
407 OnProcessLaunched();
408}
409#endif
410
411FilePath NaClProcessHost::GetManifestPath() {
[email protected]5b974952012-04-05 18:18:23412 const Extension* extension = extension_info_map_->extensions()
[email protected]87f35592012-04-08 00:49:16413 .GetExtensionOrAppByURL(ExtensionURLInfo(manifest_url_));
414 if (extension != NULL && manifest_url_.SchemeIs(chrome::kExtensionScheme)) {
415 std::string path = manifest_url_.path();
[email protected]5b974952012-04-05 18:18:23416 TrimString(path, "/", &path); // Remove first slash
417 return extension->path().AppendASCII(path);
418 }
419 return FilePath();
420}
[email protected]31a665e72012-03-11 12:37:46421
[email protected]fb1277e82009-11-21 20:32:30422bool NaClProcessHost::LaunchSelLdr() {
[email protected]4967f792012-01-20 22:14:40423 std::string channel_id = process_->GetHost()->CreateChannel();
[email protected]4734d0b2011-12-03 07:10:44424 if (channel_id.empty())
[email protected]d032f492009-09-29 00:33:46425 return false;
426
[email protected]e3fc75a2011-05-05 08:20:42427 CommandLine::StringType nacl_loader_prefix;
428#if defined(OS_POSIX)
429 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
430 switches::kNaClLoaderCmdPrefix);
431#endif // defined(OS_POSIX)
432
[email protected]d032f492009-09-29 00:33:46433 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36434
435#if defined(OS_MACOSX)
436 // The Native Client process needs to be able to allocate a 1GB contiguous
437 // region to use as the client environment's virtual address space. ASLR
438 // (PIE) interferes with this by making it possible that no gap large enough
439 // to accomodate this request will exist in the child process' address
440 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
441 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49442 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36443#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49444 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
445 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36446#else
[email protected]4cb43102011-12-02 20:24:49447 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36448#endif
449
[email protected]4cb43102011-12-02 20:24:49450 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30451 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46452 return false;
453
[email protected]31a665e72012-03-11 12:37:46454#if defined(OS_WIN)
455 // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe
456 if (RunningOnWOW64()) {
457 FilePath module_path;
458 if (!PathService::Get(base::FILE_MODULE, &module_path))
459 return false;
460 exe_path = module_path.DirName().Append(chrome::kNaClAppName);
461 }
462#endif
463
[email protected]33a05af2012-03-02 18:15:51464 scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path));
465 nacl::CopyNaClCommandLineArguments(cmd_line.get());
[email protected]599e6642010-01-27 18:52:13466
[email protected]05076ba22010-07-30 05:59:57467 cmd_line->AppendSwitchASCII(switches::kProcessType,
468 switches::kNaClLoaderProcess);
[email protected]4734d0b2011-12-03 07:10:44469 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
[email protected]93156cec2011-09-12 21:14:44470 if (logging::DialogsAreSuppressed())
471 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46472
[email protected]e3fc75a2011-05-05 08:20:42473 if (!nacl_loader_prefix.empty())
474 cmd_line->PrependWrapper(nacl_loader_prefix);
475
[email protected]31a665e72012-03-11 12:37:46476 FilePath nacl_gdb = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
477 switches::kNaClGdb);
478 if (!nacl_gdb.empty()) {
[email protected]5b974952012-04-05 18:18:23479#if defined(OS_WIN)
[email protected]31a665e72012-03-11 12:37:46480 cmd_line->AppendSwitch(switches::kNoSandbox);
481 scoped_ptr<CommandLine> gdb_cmd_line(
[email protected]646e15552012-04-06 22:01:04482 GetCommandForLaunchWithGdb(nacl_gdb, cmd_line.get()));
[email protected]31a665e72012-03-11 12:37:46483 // We can't use process_->Launch() because OnProcessLaunched will be called
484 // with process_->GetData().handle filled by handle of gdb process. This
485 // handle will be used to duplicate handles for NaCl process and as
486 // a result NaCl process will not be able to use them.
487 //
488 // So we don't fill process_->GetData().handle and wait for
489 // OnChannelConnected to get handle of NaCl process from its pid. Then we
490 // call OnProcessLaunched.
491 return base::LaunchProcess(*gdb_cmd_line, base::LaunchOptions(), NULL);
[email protected]5b974952012-04-05 18:18:23492#elif defined(OS_LINUX)
493 wait_for_nacl_gdb_ = true;
494#endif
[email protected]31a665e72012-03-11 12:37:46495 }
496
[email protected]103607e2010-02-01 18:57:09497 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46498#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52499 if (RunningOnWOW64()) {
[email protected]26b3c002012-04-26 19:38:34500 return NaClBrokerService::GetInstance()->LaunchLoader(
501 weak_factory_.GetWeakPtr(), channel_id);
[email protected]4bdde602010-06-16 03:17:35502 } else {
[email protected]33a05af2012-03-02 18:15:51503 process_->Launch(FilePath(), cmd_line.release());
[email protected]4bdde602010-06-16 03:17:35504 }
[email protected]103607e2010-02-01 18:57:09505#elif defined(OS_POSIX)
[email protected]4967f792012-01-20 22:14:40506 process_->Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]a82af392012-02-24 04:40:20507 base::EnvironmentVector(),
[email protected]33a05af2012-03-02 18:15:51508 cmd_line.release());
[email protected]103607e2010-02-01 18:57:09509#endif
[email protected]d032f492009-09-29 00:33:46510
[email protected]fb1277e82009-11-21 20:32:30511 return true;
[email protected]d032f492009-09-29 00:33:46512}
513
[email protected]646e15552012-04-06 22:01:04514bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
515 bool handled = true;
516 IPC_BEGIN_MESSAGE_MAP(NaClProcessHost, msg)
517 IPC_MESSAGE_HANDLER(NaClProcessMsg_QueryKnownToValidate,
518 OnQueryKnownToValidate)
519 IPC_MESSAGE_HANDLER(NaClProcessMsg_SetKnownToValidate,
520 OnSetKnownToValidate)
[email protected]ea6588842012-05-03 05:39:38521#if defined(OS_WIN)
522 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler,
523 OnAttachDebugExceptionHandler)
524#endif
[email protected]646e15552012-04-06 22:01:04525 IPC_MESSAGE_UNHANDLED(handled = false)
526 IPC_END_MESSAGE_MAP()
527 return handled;
[email protected]103607e2010-02-01 18:57:09528}
529
[email protected]463ea5f2011-12-03 06:57:47530void NaClProcessHost::OnProcessCrashed(int exit_code) {
531 std::string message = base::StringPrintf(
532 "NaCl process exited with status %i (0x%x)", exit_code, exit_code);
533 LOG(ERROR) << message;
[email protected]103607e2010-02-01 18:57:09534}
535
[email protected]773ebb92011-11-15 19:06:52536void NaClProcessHost::OnProcessLaunched() {
[email protected]09afc2e2012-04-10 17:29:03537 if (!StartWithLaunchedProcess())
538 delete this;
[email protected]773ebb92011-11-15 19:06:52539}
540
541// The asynchronous attempt to get the IRT file open has completed.
542void NaClProcessHost::IrtReady() {
543 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
[email protected]09afc2e2012-04-10 17:29:03544 if (!nacl_browser->IrtAvailable() || !SendStart()) {
545 DLOG(ERROR) << "Cannot launch NaCl process";
[email protected]338466a82011-05-03 04:27:43546 delete this;
547 }
548}
549
[email protected]00d99542012-04-17 22:48:02550bool NaClProcessHost::ReplyToRenderer() {
[email protected]c47ec402010-07-29 10:20:49551 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]1d8a3d1f2011-02-19 07:11:52552 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49553#if defined(OS_WIN)
554 // Copy the handle into the renderer process.
555 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04556 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
557 reinterpret_cast<HANDLE>(
558 internal_->sockets_for_renderer[i]),
559 chrome_render_message_filter_->peer_handle(),
560 &handle_in_renderer,
561 0, // Unused given DUPLICATE_SAME_ACCESS.
562 FALSE,
563 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
[email protected]09afc2e2012-04-10 17:29:03564 DLOG(ERROR) << "DuplicateHandle() failed";
565 return false;
[email protected]909c2402011-05-09 11:39:04566 }
[email protected]c47ec402010-07-29 10:20:49567 handles_for_renderer.push_back(
568 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
569#else
570 // No need to dup the imc_handle - we don't pass it anywhere else so
571 // it cannot be closed.
572 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52573 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49574 imc_handle.auto_close = true;
575 handles_for_renderer.push_back(imc_handle);
576#endif
577 }
578
579#if defined(OS_WIN)
[email protected]e4f6eb0232012-04-17 00:47:50580 // If we are on 64-bit Windows, the NaCl process's sandbox is
581 // managed by a different process from the renderer's sandbox. We
582 // need to inform the renderer's sandbox about the NaCl process so
583 // that the renderer can send handles to the NaCl process using
584 // BrokerDuplicateHandle().
585 if (RunningOnWOW64()) {
[email protected]171fa98d2012-04-23 21:34:01586 if (!content::BrokerAddTargetPeer(process_->GetData().handle)) {
[email protected]e4f6eb0232012-04-17 00:47:50587 DLOG(ERROR) << "Failed to add NaCl process PID";
588 return false;
589 }
590 }
[email protected]fb1277e82009-11-21 20:32:30591#endif
592
[email protected]2ccf45c2011-08-19 23:35:50593 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]171fa98d2012-04-23 21:34:01594 reply_msg_, handles_for_renderer);
[email protected]92d56412011-03-24 20:53:52595 chrome_render_message_filter_->Send(reply_msg_);
596 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30597 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52598 internal_->sockets_for_renderer.clear();
[email protected]00d99542012-04-17 22:48:02599 return true;
600}
[email protected]fb1277e82009-11-21 20:32:30601
[email protected]00d99542012-04-17 22:48:02602bool NaClProcessHost::StartNaClExecution() {
603 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
604
605 nacl::NaClStartParams params;
[email protected]7cfaf982012-05-09 18:37:47606 params.validation_cache_key =
607 nacl_browser->validation_cache.GetValidationCacheKey();
[email protected]00d99542012-04-17 22:48:02608 params.version = chrome::VersionInfo().CreateVersionString();
609 params.enable_exception_handling = enable_exception_handling_;
610
611 base::PlatformFile irt_file = nacl_browser->IrtFile();
612 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
613
614 const ChildProcessData& data = process_->GetData();
[email protected]1d8a3d1f2011-02-19 07:11:52615 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]00d99542012-04-17 22:48:02616 if (!ShareHandleToSelLdr(data.handle,
617 internal_->sockets_for_sel_ldr[i], true,
618 &params.handles)) {
[email protected]09afc2e2012-04-10 17:29:03619 return false;
[email protected]c47ec402010-07-29 10:20:49620 }
[email protected]773ebb92011-11-15 19:06:52621 }
622
623 // Send over the IRT file handle. We don't close our own copy!
[email protected]00d99542012-04-17 22:48:02624 if (!ShareHandleToSelLdr(data.handle, irt_file, false, &params.handles))
[email protected]09afc2e2012-04-10 17:29:03625 return false;
[email protected]c47ec402010-07-29 10:20:49626
[email protected]ab88d1542011-11-18 22:52:00627#if defined(OS_MACOSX)
628 // For dynamic loading support, NaCl requires a file descriptor that
629 // was created in /tmp, since those created with shm_open() are not
630 // mappable with PROT_EXEC. Rather than requiring an extra IPC
631 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25632 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31633 base::SharedMemoryCreateOptions options;
634 options.size = 1;
635 options.executable = true;
636 if (!memory_buffer.Create(options)) {
[email protected]09afc2e2012-04-10 17:29:03637 DLOG(ERROR) << "Failed to allocate memory buffer";
638 return false;
[email protected]2c68bf032010-11-11 23:16:30639 }
[email protected]cbbe7842011-11-17 22:01:25640 nacl::FileDescriptor memory_fd;
641 memory_fd.fd = dup(memory_buffer.handle().fd);
642 if (memory_fd.fd < 0) {
[email protected]09afc2e2012-04-10 17:29:03643 DLOG(ERROR) << "Failed to dup() a file descriptor";
644 return false;
[email protected]cbbe7842011-11-17 22:01:25645 }
646 memory_fd.auto_close = true;
[email protected]00d99542012-04-17 22:48:02647 params.handles.push_back(memory_fd);
[email protected]2c68bf032010-11-11 23:16:30648#endif
649
[email protected]ea6588842012-05-03 05:39:38650 process_->Send(new NaClProcessMsg_Start(params));
[email protected]b39c6d92012-01-31 16:38:41651
[email protected]1d8a3d1f2011-02-19 07:11:52652 internal_->sockets_for_sel_ldr.clear();
[email protected]09afc2e2012-04-10 17:29:03653 return true;
654}
655
[email protected]00d99542012-04-17 22:48:02656bool NaClProcessHost::SendStart() {
657 return ReplyToRenderer() && StartNaClExecution();
658}
659
[email protected]09afc2e2012-04-10 17:29:03660bool NaClProcessHost::StartWithLaunchedProcess() {
661#if defined(OS_LINUX)
662 if (wait_for_nacl_gdb_) {
663 if (LaunchNaClGdb(base::GetProcId(process_->GetData().handle))) {
664 // We will be called with wait_for_nacl_gdb_ = false once debugger is
665 // attached to the program.
666 return true;
667 }
668 DLOG(ERROR) << "Failed to launch debugger";
669 // Continue execution without debugger.
670 }
671#endif
672
673 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
674 if (nacl_browser->IrtAvailable())
675 return SendStart(); // The IRT is already open. Away we go.
676
677 // We're waiting for the IRT to be open.
678 return nacl_browser->MakeIrtAvailable(
679 base::Bind(&NaClProcessHost::IrtReady, weak_factory_.GetWeakPtr()));
[email protected]d032f492009-09-29 00:33:46680}
681
[email protected]4a0141b2012-03-27 01:15:30682void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature,
683 bool* result) {
[email protected]7cfaf982012-05-09 18:37:47684 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
685 *result = nacl_browser->validation_cache.QueryKnownToValidate(signature);
[email protected]4a0141b2012-03-27 01:15:30686}
687
688void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) {
[email protected]7cfaf982012-05-09 18:37:47689 NaClBrowser::GetInstance()->validation_cache.SetKnownToValidate(signature);
[email protected]4a0141b2012-03-27 01:15:30690}
[email protected]ea6588842012-05-03 05:39:38691
692#if defined(OS_WIN)
693void NaClProcessHost::OnAttachDebugExceptionHandler(const std::string& info,
694 IPC::Message* reply_msg) {
695 if (!AttachDebugExceptionHandler(info, reply_msg)) {
696 // Send failure message.
697 NaClProcessMsg_AttachDebugExceptionHandler::WriteReplyParams(reply_msg,
698 false);
699 Send(reply_msg);
700 }
701}
702
703bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info,
704 IPC::Message* reply_msg) {
705 if (!enable_exception_handling_) {
706 DLOG(ERROR) <<
707 "Exception handling requested by NaCl process when not enabled";
708 return false;
709 }
710 if (debug_exception_handler_requested_) {
711 // The NaCl process should not request this multiple times.
712 DLOG(ERROR) << "Multiple AttachDebugExceptionHandler requests received";
713 return false;
714 }
715 debug_exception_handler_requested_ = true;
716
717 base::ProcessId nacl_pid = base::GetProcId(process_->GetData().handle);
718 base::win::ScopedHandle process_handle;
719 // We cannot use process_->GetData().handle because it does not have
720 // the necessary access rights. We open the new handle here rather
721 // than in the NaCl broker process in case the NaCl loader process
722 // dies before the NaCl broker process receives the message we send.
723 // The debug exception handler uses DebugActiveProcess() to attach,
724 // but this takes a PID. We need to prevent the NaCl loader's PID
725 // from being reused before DebugActiveProcess() is called, and
726 // holding a process handle open achieves this.
727 if (!base::OpenProcessHandleWithAccess(
728 nacl_pid,
729 base::kProcessAccessQueryInformation |
730 base::kProcessAccessSuspendResume |
731 base::kProcessAccessTerminate |
732 base::kProcessAccessVMOperation |
733 base::kProcessAccessVMRead |
734 base::kProcessAccessVMWrite |
735 base::kProcessAccessWaitForTermination,
736 process_handle.Receive())) {
737 LOG(ERROR) << "Failed to get process handle";
738 return false;
739 }
740
741 attach_debug_exception_handler_reply_msg_.reset(reply_msg);
742 // If the NaCl loader is 64-bit, the process running its debug
743 // exception handler must be 64-bit too, so we use the 64-bit NaCl
744 // broker process for this. Otherwise, on a 32-bit system, we use
745 // the 32-bit browser process to run the debug exception handler.
746 if (RunningOnWOW64()) {
747 return NaClBrokerService::GetInstance()->LaunchDebugExceptionHandler(
[email protected]fd37bf62012-05-04 16:46:48748 weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info);
[email protected]ea6588842012-05-03 05:39:38749 } else {
750 NaClStartDebugExceptionHandlerThread(
[email protected]fd37bf62012-05-04 16:46:48751 process_handle.Take(), info,
[email protected]ea6588842012-05-03 05:39:38752 base::MessageLoopProxy::current(),
753 base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker,
754 weak_factory_.GetWeakPtr()));
755 return true;
756 }
757}
758#endif