[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 5 | #include "components/nacl/browser/nacl_process_host.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 6 | |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 10 | |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 11 | #include "base/base_switches.h" |
[email protected] | 30c1eea | 2011-10-17 18:40:30 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 13 | #include "base/command_line.h" |
[email protected] | 6d7f55f | 2013-05-14 10:12:56 | [diff] [blame] | 14 | #include "base/file_util.h" |
[email protected] | 467a106e | 2013-07-18 12:07:19 | [diff] [blame] | 15 | #include "base/message_loop/message_loop.h" |
[email protected] | 9addd1c | 2012-09-15 14:28:24 | [diff] [blame] | 16 | #include "base/metrics/histogram.h" |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 17 | #include "base/path_service.h" |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 18 | #include "base/process/launch.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 19 | #include "base/process/process_iterator.h" |
[email protected] | 25a10e23 | 2013-08-23 00:40:08 | [diff] [blame] | 20 | #include "base/rand_util.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 21 | #include "base/strings/string_number_conversions.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 22 | #include "base/strings/string_split.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 23 | #include "base/strings/string_util.h" |
| 24 | #include "base/strings/stringprintf.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 25 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 26 | #include "base/win/windows_version.h" |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 27 | #include "build/build_config.h" |
[email protected] | ef42ee8 | 2013-11-01 05:26:13 | [diff] [blame] | 28 | #include "components/nacl/browser/nacl_browser.h" |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 29 | #include "components/nacl/browser/nacl_host_message_filter.h" |
[email protected] | 0acf1d4 | 2013-07-22 14:54:58 | [diff] [blame] | 30 | #include "components/nacl/common/nacl_cmd_line.h" |
[email protected] | 512b5bd | 2013-07-23 20:24:00 | [diff] [blame] | 31 | #include "components/nacl/common/nacl_host_messages.h" |
| 32 | #include "components/nacl/common/nacl_messages.h" |
[email protected] | d5d38325 | 2013-07-04 14:44:32 | [diff] [blame] | 33 | #include "components/nacl/common/nacl_process_type.h" |
[email protected] | cbb22eb | 2013-06-24 23:53:10 | [diff] [blame] | 34 | #include "components/nacl/common/nacl_switches.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 35 | #include "content/public/browser/browser_child_process_host.h" |
[email protected] | 0c719374 | 2012-11-07 19:05:03 | [diff] [blame] | 36 | #include "content/public/browser/browser_ppapi_host.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 37 | #include "content/public/browser/child_process_data.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 38 | #include "content/public/common/child_process_host.h" |
[email protected] | 67e7687 | 2013-07-08 02:19:34 | [diff] [blame] | 39 | #include "content/public/common/content_switches.h" |
[email protected] | 9e14121 | 2013-01-21 15:21:56 | [diff] [blame] | 40 | #include "content/public/common/process_type.h" |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 41 | #include "ipc/ipc_channel.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 42 | #include "ipc/ipc_switches.h" |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 43 | #include "native_client/src/shared/imc/nacl_imc_c.h" |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 44 | #include "net/base/net_util.h" |
[email protected] | 1946d9e | 2013-04-09 01:43:37 | [diff] [blame] | 45 | #include "net/socket/tcp_listen_socket.h" |
[email protected] | 6fff31c2 | 2013-07-09 02:08:19 | [diff] [blame] | 46 | #include "ppapi/host/host_factory.h" |
[email protected] | 4b30e7d | 2013-04-24 23:52:14 | [diff] [blame] | 47 | #include "ppapi/host/ppapi_host.h" |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 48 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 49 | #include "ppapi/shared_impl/ppapi_nacl_channel_args.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 50 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 51 | #if defined(OS_POSIX) |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 52 | #include <fcntl.h> |
| 53 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 54 | #include "ipc/ipc_channel_posix.h" |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 55 | #elif defined(OS_WIN) |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 56 | #include <windows.h> |
| 57 | |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 58 | #include "base/threading/thread.h" |
[email protected] | 4c65fb63 | 2012-04-27 00:42:25 | [diff] [blame] | 59 | #include "base/win/scoped_handle.h" |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 60 | #include "components/nacl/browser/nacl_broker_service_win.h" |
[email protected] | 512b5bd | 2013-07-23 20:24:00 | [diff] [blame] | 61 | #include "components/nacl/common/nacl_debug_exception_handler_win.h" |
[email protected] | e4f6eb023 | 2012-04-17 00:47:50 | [diff] [blame] | 62 | #include "content/public/common/sandbox_init.h" |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 63 | #include "content/public/common/sandboxed_process_launcher_delegate.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 64 | #endif |
| 65 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 66 | using content::BrowserThread; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 67 | using content::ChildProcessData; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 68 | using content::ChildProcessHost; |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 69 | using ppapi::proxy::SerializedHandle; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 70 | |
[email protected] | 25a10e23 | 2013-08-23 00:40:08 | [diff] [blame] | 71 | #if defined(OS_WIN) |
| 72 | |
| 73 | namespace { |
| 74 | |
| 75 | // Looks for the largest contiguous unallocated region of address |
| 76 | // space and returns it via |*out_addr| and |*out_size|. |
| 77 | void 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 | |
| 98 | namespace nacl { |
| 99 | |
| 100 | // Allocates |size| bytes of address space in the given process at a |
| 101 | // randomised address. |
| 102 | void* 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] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 121 | namespace { |
| 122 | |
| 123 | #if defined(OS_WIN) |
| 124 | bool RunningOnWOW64() { |
| 125 | return (base::win::OSInfo::GetInstance()->wow64_status() == |
| 126 | base::win::OSInfo::WOW64_ENABLED); |
| 127 | } |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 128 | |
| 129 | // NOTE: changes to this class need to be reviewed by the security team. |
| 130 | class NaClSandboxedProcessLauncherDelegate |
| 131 | : public content::SandboxedProcessLauncherDelegate { |
| 132 | public: |
| 133 | NaClSandboxedProcessLauncherDelegate() {} |
| 134 | virtual ~NaClSandboxedProcessLauncherDelegate() {} |
| 135 | |
| 136 | virtual void PostSpawnTarget(base::ProcessHandle process) { |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 137 | // 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] | 25a10e23 | 2013-08-23 00:40:08 | [diff] [blame] | 143 | const SIZE_T kNaClSandboxSize = 1 << 30; |
| 144 | if (!nacl::AllocateAddressSpaceASLR(process, kNaClSandboxSize)) { |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 145 | DLOG(WARNING) << "Failed to reserve address space for Native Client"; |
| 146 | } |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 147 | } |
| 148 | }; |
| 149 | |
| 150 | #endif // OS_WIN |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 151 | |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 152 | void SetCloseOnExec(NaClHandle fd) { |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 153 | #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] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 161 | bool ShareHandleToSelLdr( |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 162 | base::ProcessHandle processh, |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 163 | NaClHandle sourceh, |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 164 | 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] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 178 | LOG(ERROR) << "DuplicateHandle() failed"; |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 179 | 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] | 27b04df | 2012-12-10 23:43:10 | [diff] [blame] | 192 | ppapi::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] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 200 | } // namespace |
| 201 | |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 202 | namespace nacl { |
| 203 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 204 | struct NaClProcessHost::NaClInternal { |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 205 | NaClHandle socket_for_renderer; |
| 206 | NaClHandle socket_for_sel_ldr; |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 207 | |
| 208 | NaClInternal() |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 209 | : socket_for_renderer(NACL_INVALID_HANDLE), |
| 210 | socket_for_sel_ldr(NACL_INVALID_HANDLE) { } |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 211 | }; |
| 212 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 213 | // ----------------------------------------------------------------------------- |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 214 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 215 | NaClProcessHost::PluginListener::PluginListener(NaClProcessHost* host) |
| 216 | : host_(host) { |
| 217 | } |
| 218 | |
| 219 | bool NaClProcessHost::PluginListener::OnMessageReceived( |
| 220 | const IPC::Message& msg) { |
| 221 | return host_->OnUntrustedMessageForwarded(msg); |
| 222 | } |
| 223 | |
[email protected] | 9da4fed | 2012-11-01 17:07:55 | [diff] [blame] | 224 | NaClProcessHost::NaClProcessHost(const GURL& manifest_url, |
[email protected] | e02ff72 | 2012-11-06 03:53:06 | [diff] [blame] | 225 | int render_view_id, |
[email protected] | 9da4fed | 2012-11-01 17:07:55 | [diff] [blame] | 226 | uint32 permission_bits, |
[email protected] | 2e1e6f0 | 2013-01-11 18:22:56 | [diff] [blame] | 227 | bool uses_irt, |
[email protected] | be52462 | 2013-04-23 00:12:19 | [diff] [blame] | 228 | bool enable_dyncode_syscalls, |
[email protected] | 286c095 | 2013-06-13 20:29:26 | [diff] [blame] | 229 | bool enable_exception_handling, |
[email protected] | e17f5ab | 2013-08-19 22:27:07 | [diff] [blame] | 230 | bool enable_crash_throttling, |
[email protected] | 7bb5d2c | 2013-04-29 22:33:49 | [diff] [blame] | 231 | bool off_the_record, |
| 232 | const base::FilePath& profile_directory) |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 233 | : manifest_url_(manifest_url), |
[email protected] | 27b04df | 2012-12-10 23:43:10 | [diff] [blame] | 234 | permissions_(GetNaClPermissions(permission_bits)), |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 235 | #if defined(OS_WIN) |
| 236 | process_launched_by_broker_(false), |
| 237 | #endif |
| 238 | reply_msg_(NULL), |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 239 | #if defined(OS_WIN) |
| 240 | debug_exception_handler_requested_(false), |
| 241 | #endif |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 242 | internal_(new NaClInternal()), |
[email protected] | 9c00909 | 2013-05-01 03:14:09 | [diff] [blame] | 243 | weak_factory_(this), |
[email protected] | 84b8e896 | 2013-06-12 22:24:06 | [diff] [blame] | 244 | uses_irt_(uses_irt), |
[email protected] | 286c095 | 2013-06-13 20:29:26 | [diff] [blame] | 245 | enable_debug_stub_(false), |
[email protected] | be52462 | 2013-04-23 00:12:19 | [diff] [blame] | 246 | enable_dyncode_syscalls_(enable_dyncode_syscalls), |
[email protected] | 286c095 | 2013-06-13 20:29:26 | [diff] [blame] | 247 | enable_exception_handling_(enable_exception_handling), |
[email protected] | e17f5ab | 2013-08-19 22:27:07 | [diff] [blame] | 248 | enable_crash_throttling_(enable_crash_throttling), |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 249 | off_the_record_(off_the_record), |
[email protected] | 7bb5d2c | 2013-04-29 22:33:49 | [diff] [blame] | 250 | profile_directory_(profile_directory), |
[email protected] | 9c00909 | 2013-05-01 03:14:09 | [diff] [blame] | 251 | ipc_plugin_listener_(this), |
[email protected] | e02ff72 | 2012-11-06 03:53:06 | [diff] [blame] | 252 | render_view_id_(render_view_id) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 253 | process_.reset(content::BrowserChildProcessHost::Create( |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 254 | PROCESS_TYPE_NACL_LOADER, this)); |
[email protected] | 87f3559 | 2012-04-08 00:49:16 | [diff] [blame] | 255 | |
| 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] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 261 | |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 262 | enable_debug_stub_ = CommandLine::ForCurrentProcess()->HasSwitch( |
| 263 | switches::kEnableNaClDebug); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 264 | } |
| 265 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 266 | NaClProcessHost::~NaClProcessHost() { |
[email protected] | 6e70b198 | 2013-08-23 21:38:20 | [diff] [blame] | 267 | // Report exit status only if the process was successfully started. |
| 268 | if (process_->GetData().handle != base::kNullProcessHandle) { |
| 269 | int exit_code = 0; |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 270 | process_->GetTerminationStatus(false /* known_dead */, &exit_code); |
[email protected] | 6e70b198 | 2013-08-23 21:38:20 | [diff] [blame] | 271 | std::string message = |
| 272 | base::StringPrintf("NaCl process exited with status %i (0x%x)", |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 273 | exit_code, exit_code); |
[email protected] | 6e70b198 | 2013-08-23 21:38:20 | [diff] [blame] | 274 | if (exit_code == 0) { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 275 | VLOG(1) << message; |
[email protected] | 6e70b198 | 2013-08-23 21:38:20 | [diff] [blame] | 276 | } else { |
| 277 | LOG(ERROR) << message; |
| 278 | } |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 281 | if (internal_->socket_for_renderer != NACL_INVALID_HANDLE) { |
| 282 | if (NaClClose(internal_->socket_for_renderer) != 0) { |
| 283 | NOTREACHED() << "NaClClose() failed"; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 284 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 285 | } |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 286 | |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 287 | if (internal_->socket_for_sel_ldr != NACL_INVALID_HANDLE) { |
| 288 | if (NaClClose(internal_->socket_for_sel_ldr) != 0) { |
| 289 | NOTREACHED() << "NaClClose() failed"; |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 290 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 291 | } |
| 292 | |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 293 | 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] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame] | 297 | nacl_host_message_filter_->Send(reply_msg_); |
[email protected] | 909c240 | 2011-05-09 11:39:04 | [diff] [blame] | 298 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 299 | #if defined(OS_WIN) |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 300 | if (process_launched_by_broker_) { |
| 301 | NaClBrokerService::GetInstance()->OnLoaderDied(); |
[email protected] | 5ca93be | 2012-03-21 20:04:06 | [diff] [blame] | 302 | } |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 303 | #endif |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 304 | } |
| 305 | |
[email protected] | e17f5ab | 2013-08-19 22:27:07 | [diff] [blame] | 306 | void NaClProcessHost::OnProcessCrashed(int exit_status) { |
| 307 | if (enable_crash_throttling_ && |
| 308 | !CommandLine::ForCurrentProcess()->HasSwitch( |
| 309 | switches::kDisablePnaclCrashThrottling)) { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 310 | NaClBrowser::GetInstance()->OnProcessCrashed(); |
[email protected] | e17f5ab | 2013-08-19 22:27:07 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 314 | // This is called at browser startup. |
| 315 | // static |
[email protected] | a2b4c5c | 2013-10-18 02:06:07 | [diff] [blame] | 316 | void NaClProcessHost::EarlyStartup() { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 317 | NaClBrowser::GetInstance()->EarlyStartup(); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 318 | #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] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 321 | NaClBrowser::GetInstance()->EnsureIrtAvailable(); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 322 | #endif |
[email protected] | 9addd1c | 2012-09-15 14:28:24 | [diff] [blame] | 323 | 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] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 333 | NaClBrowser::GetDelegate()->SetDebugPatterns( |
[email protected] | 66f409c | 2012-10-04 20:59:04 | [diff] [blame] | 334 | cmd->GetSwitchValueASCII(switches::kNaClDebugMask)); |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 335 | } |
| 336 | |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 337 | void NaClProcessHost::Launch( |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame] | 338 | NaClHostMessageFilter* nacl_host_message_filter, |
[email protected] | 8f42b4d | 2012-03-24 14:12:33 | [diff] [blame] | 339 | IPC::Message* reply_msg, |
[email protected] | 3a0506cc | 2013-07-01 20:28:31 | [diff] [blame] | 340 | const base::FilePath& manifest_path) { |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame] | 341 | nacl_host_message_filter_ = nacl_host_message_filter; |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 342 | reply_msg_ = reply_msg; |
[email protected] | 3a0506cc | 2013-07-01 20:28:31 | [diff] [blame] | 343 | manifest_path_ = manifest_path; |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 344 | |
[email protected] | e17f5ab | 2013-08-19 22:27:07 | [diff] [blame] | 345 | // 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] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 350 | NaClBrowser::GetInstance()->IsThrottled()) { |
[email protected] | e17f5ab | 2013-08-19 22:27:07 | [diff] [blame] | 351 | SendErrorToRenderer("Process creation was throttled due to excessive" |
| 352 | " crashes"); |
| 353 | delete this; |
| 354 | return; |
| 355 | } |
| 356 | |
[email protected] | 8e39f0ef | 2013-07-31 04:30:46 | [diff] [blame] | 357 | 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] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 373 | // Start getting the IRT open asynchronously while we launch the NaCl process. |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 374 | // We'll make sure this actually finished in StartWithLaunchedProcess, below. |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 375 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 376 | nacl_browser->EnsureAllResourcesAvailable(); |
| 377 | if (!nacl_browser->IsOk()) { |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 378 | SendErrorToRenderer("could not find all the resources needed" |
| 379 | " to launch the process"); |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 380 | delete this; |
| 381 | return; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 384 | // 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] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 393 | NaClHandle pair[2]; |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 394 | // Create a connected socket |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 395 | if (NaClSocketPair(pair) == -1) { |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 396 | SendErrorToRenderer("NaClSocketPair() failed"); |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 397 | delete this; |
| 398 | return; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 399 | } |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 400 | internal_->socket_for_renderer = pair[0]; |
| 401 | internal_->socket_for_sel_ldr = pair[1]; |
| 402 | SetCloseOnExec(pair[0]); |
| 403 | SetCloseOnExec(pair[1]); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 404 | |
| 405 | // Launch the process |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 406 | if (!LaunchSelLdr()) { |
[email protected] | a575da5 | 2012-03-22 13:08:36 | [diff] [blame] | 407 | delete this; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 408 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 409 | } |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 410 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 411 | void NaClProcessHost::OnChannelConnected(int32 peer_pid) { |
[email protected] | 82d1750 | 2012-05-23 19:12:40 | [diff] [blame] | 412 | if (!CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 413 | switches::kNaClGdb).empty()) { |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 414 | LaunchNaClGdb(); |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 415 | } |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 416 | } |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 417 | |
| 418 | #if defined(OS_WIN) |
| 419 | void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) { |
| 420 | process_launched_by_broker_ = true; |
| 421 | process_->SetHandle(handle); |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 422 | if (!StartWithLaunchedProcess()) |
| 423 | delete this; |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 424 | } |
| 425 | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 426 | void 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] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 430 | } |
| 431 | #endif |
| 432 | |
| 433 | // Needed to handle sync messages in OnMessageRecieved. |
| 434 | bool NaClProcessHost::Send(IPC::Message* msg) { |
| 435 | return process_->Send(msg); |
| 436 | } |
| 437 | |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 438 | bool NaClProcessHost::LaunchNaClGdb() { |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 439 | #if defined(OS_WIN) |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 440 | base::FilePath nacl_gdb = |
| 441 | CommandLine::ForCurrentProcess()->GetSwitchValuePath(switches::kNaClGdb); |
| 442 | CommandLine cmd_line(nacl_gdb); |
| 443 | #else |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 444 | 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] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 451 | #endif |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 452 | cmd_line.AppendArg("--eval-command"); |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 453 | base::FilePath::StringType irt_path( |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 454 | NaClBrowser::GetInstance()->GetIrtFilePath().value()); |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 455 | // 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] | 3a0506cc | 2013-07-01 20:28:31 | [diff] [blame] | 460 | if (!manifest_path_.empty()) { |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 461 | cmd_line.AppendArg("--eval-command"); |
[email protected] | 3a0506cc | 2013-07-01 20:28:31 | [diff] [blame] | 462 | base::FilePath::StringType manifest_path_value(manifest_path_.value()); |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 463 | 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] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 467 | } |
| 468 | cmd_line.AppendArg("--eval-command"); |
[email protected] | 0428425 | 2013-06-11 22:30:14 | [diff] [blame] | 469 | cmd_line.AppendArg("target remote :4014"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 470 | base::FilePath script = CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
[email protected] | 401b9079 | 2012-05-30 11:41:13 | [diff] [blame] | 471 | switches::kNaClGdbScript); |
| 472 | if (!script.empty()) { |
| 473 | cmd_line.AppendArg("--command"); |
| 474 | cmd_line.AppendArgNative(script.value()); |
| 475 | } |
[email protected] | 5b97495 | 2012-04-05 18:18:23 | [diff] [blame] | 476 | return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL); |
| 477 | } |
| 478 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 479 | bool NaClProcessHost::LaunchSelLdr() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 480 | std::string channel_id = process_->GetHost()->CreateChannel(); |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 481 | if (channel_id.empty()) { |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 482 | SendErrorToRenderer("CreateChannel() failed"); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 483 | return false; |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 484 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 485 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 486 | 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] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 492 | // Build command line for nacl. |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 493 | |
| 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] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 501 | int flags = ChildProcessHost::CHILD_NO_PIE; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 502 | #elif defined(OS_LINUX) |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 503 | int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF : |
| 504 | ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 505 | #else |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 506 | int flags = ChildProcessHost::CHILD_NORMAL; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 507 | #endif |
| 508 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 509 | base::FilePath exe_path = ChildProcessHost::GetChildPath(flags); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 510 | if (exe_path.empty()) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 511 | return false; |
| 512 | |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 513 | #if defined(OS_WIN) |
| 514 | // On Windows 64-bit NaCl loader is called nacl64.exe instead of chrome.exe |
| 515 | if (RunningOnWOW64()) { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 516 | if (!NaClBrowser::GetInstance()->GetNaCl64ExePath(&exe_path)) { |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 517 | SendErrorToRenderer("could not get path to nacl64.exe"); |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 518 | return false; |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 519 | } |
[email protected] | 31a665e7 | 2012-03-11 12:37:46 | [diff] [blame] | 520 | } |
| 521 | #endif |
| 522 | |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 523 | scoped_ptr<CommandLine> cmd_line(new CommandLine(exe_path)); |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 524 | CopyNaClCommandLineArguments(cmd_line.get()); |
[email protected] | 599e664 | 2010-01-27 18:52:13 | [diff] [blame] | 525 | |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 526 | cmd_line->AppendSwitchASCII(switches::kProcessType, |
| 527 | switches::kNaClLoaderProcess); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 528 | cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id); |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 529 | if (NaClBrowser::GetDelegate()->DialogsAreSuppressed()) |
[email protected] | 93156cec | 2011-09-12 21:14:44 | [diff] [blame] | 530 | cmd_line->AppendSwitch(switches::kNoErrorDialogs); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 531 | |
[email protected] | e3fc75a | 2011-05-05 08:20:42 | [diff] [blame] | 532 | if (!nacl_loader_prefix.empty()) |
| 533 | cmd_line->PrependWrapper(nacl_loader_prefix); |
| 534 | |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 535 | // On Windows we might need to start the broker process to launch a new loader |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 536 | #if defined(OS_WIN) |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 537 | if (RunningOnWOW64()) { |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 538 | if (!NaClBrokerService::GetInstance()->LaunchLoader( |
| 539 | weak_factory_.GetWeakPtr(), channel_id)) { |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 540 | SendErrorToRenderer("broker service did not launch process"); |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 541 | return false; |
| 542 | } |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 543 | } else { |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 544 | process_->Launch(new NaClSandboxedProcessLauncherDelegate, |
| 545 | cmd_line.release()); |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 546 | } |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 547 | #elif defined(OS_POSIX) |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 548 | process_->Launch(nacl_loader_prefix.empty(), // use_zygote |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 549 | base::EnvironmentMap(), |
[email protected] | 33a05af | 2012-03-02 18:15:51 | [diff] [blame] | 550 | cmd_line.release()); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 551 | #endif |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 552 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 553 | return true; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 554 | } |
| 555 | |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 556 | bool 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] | 04183452 | 2013-05-29 00:19:27 | [diff] [blame] | 563 | IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_ResolveFileToken, |
| 564 | OnResolveFileToken) |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 565 | #if defined(OS_WIN) |
| 566 | IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClProcessMsg_AttachDebugExceptionHandler, |
| 567 | OnAttachDebugExceptionHandler) |
| 568 | #endif |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 569 | IPC_MESSAGE_HANDLER(NaClProcessHostMsg_PpapiChannelCreated, |
| 570 | OnPpapiChannelCreated) |
[email protected] | 646e1555 | 2012-04-06 22:01:04 | [diff] [blame] | 571 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 572 | IPC_END_MESSAGE_MAP() |
| 573 | return handled; |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 574 | } |
| 575 | |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 576 | void NaClProcessHost::OnProcessLaunched() { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 577 | if (!StartWithLaunchedProcess()) |
| 578 | delete this; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 579 | } |
| 580 | |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 581 | // Called when the NaClBrowser singleton has been fully initialized. |
| 582 | void NaClProcessHost::OnResourcesReady() { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 583 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 584 | if (!nacl_browser->IsReady()) { |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 585 | SendErrorToRenderer("could not acquire shared resources needed by NaCl"); |
[email protected] | ec7540f | 2013-01-17 00:00:35 | [diff] [blame] | 586 | delete this; |
| 587 | } else if (!SendStart()) { |
[email protected] | 338466a8 | 2011-05-03 04:27:43 | [diff] [blame] | 588 | delete this; |
| 589 | } |
| 590 | } |
| 591 | |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 592 | bool NaClProcessHost::ReplyToRenderer( |
| 593 | const IPC::ChannelHandle& channel_handle) { |
[email protected] | e9e810f | 2013-07-12 19:26:11 | [diff] [blame] | 594 | #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] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 602 | SendErrorToRenderer("BrokerAddTargetPeer() failed"); |
[email protected] | e9e810f | 2013-07-12 19:26:11 | [diff] [blame] | 603 | return false; |
| 604 | } |
| 605 | } |
| 606 | #endif |
| 607 | |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 608 | FileDescriptor handle_for_renderer; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 609 | #if defined(OS_WIN) |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 610 | // 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] | 950be55 | 2013-07-10 19:13:02 | [diff] [blame] | 615 | nacl_host_message_filter_->PeerHandle(), |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 616 | &handle_in_renderer, |
| 617 | 0, // Unused given DUPLICATE_SAME_ACCESS. |
| 618 | FALSE, |
| 619 | DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 620 | SendErrorToRenderer("DuplicateHandle() failed"); |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 621 | return false; |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 622 | } |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 623 | handle_for_renderer = reinterpret_cast<FileDescriptor>( |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 624 | 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] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 628 | FileDescriptor imc_handle; |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 629 | imc_handle.fd = internal_->socket_for_renderer; |
| 630 | imc_handle.auto_close = true; |
| 631 | handle_for_renderer = imc_handle; |
| 632 | #endif |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 633 | |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 634 | const ChildProcessData& data = process_->GetData(); |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 635 | SendMessageToRenderer( |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 636 | NaClLaunchResult(handle_for_renderer, |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 637 | channel_handle, |
| 638 | base::GetProcId(data.handle), |
| 639 | data.id), |
| 640 | std::string() /* error_message */); |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 641 | internal_->socket_for_renderer = NACL_INVALID_HANDLE; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 642 | return true; |
| 643 | } |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 644 | |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 645 | void NaClProcessHost::SendErrorToRenderer(const std::string& error_message) { |
| 646 | LOG(ERROR) << "NaCl process launch failed: " << error_message; |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 647 | SendMessageToRenderer(NaClLaunchResult(), error_message); |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void NaClProcessHost::SendMessageToRenderer( |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 651 | const NaClLaunchResult& result, |
[email protected] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 652 | 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] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 664 | // TCP port we chose for NaCl debug stub. It can be any other number. |
| 665 | static const int kDebugStubPort = 4014; |
| 666 | |
| 667 | #if defined(OS_POSIX) |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 668 | net::SocketDescriptor NaClProcessHost::GetDebugStubSocketHandle() { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 669 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 670 | net::SocketDescriptor s = net::kInvalidSocket; |
[email protected] | 13cad0b | 2012-12-07 17:57:34 | [diff] [blame] | 671 | // 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] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 676 | if (s != net::kInvalidSocket) { |
[email protected] | 13cad0b | 2012-12-07 17:57:34 | [diff] [blame] | 677 | nacl_browser->FireGdbDebugStubPortOpened(port); |
| 678 | } |
| 679 | } else { |
| 680 | s = net::TCPListenSocket::CreateAndBind("127.0.0.1", kDebugStubPort); |
| 681 | } |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 682 | if (s == net::kInvalidSocket) { |
[email protected] | 13cad0b | 2012-12-07 17:57:34 | [diff] [blame] | 683 | LOG(ERROR) << "failed to open socket for debug stub"; |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 684 | return net::kInvalidSocket; |
[email protected] | 13cad0b | 2012-12-07 17:57:34 | [diff] [blame] | 685 | } |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 686 | if (listen(s, 1)) { |
| 687 | LOG(ERROR) << "listen() failed on debug stub socket"; |
[email protected] | 13cad0b | 2012-12-07 17:57:34 | [diff] [blame] | 688 | if (HANDLE_EINTR(close(s)) < 0) |
| 689 | PLOG(ERROR) << "failed to close debug stub socket"; |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 690 | return net::kInvalidSocket; |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 691 | } |
| 692 | return s; |
| 693 | } |
| 694 | #endif |
| 695 | |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 696 | bool NaClProcessHost::StartNaClExecution() { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 697 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 698 | |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 699 | NaClStartParams params; |
[email protected] | 8c67083 | 2012-05-26 04:30:12 | [diff] [blame] | 700 | params.validation_cache_enabled = nacl_browser->ValidationCacheIsEnabled(); |
| 701 | params.validation_cache_key = nacl_browser->GetValidationCacheKey(); |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 702 | params.version = NaClBrowser::GetDelegate()->GetVersionString(); |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 703 | params.enable_exception_handling = enable_exception_handling_; |
[email protected] | 66f409c | 2012-10-04 20:59:04 | [diff] [blame] | 704 | params.enable_debug_stub = enable_debug_stub_ && |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 705 | NaClBrowser::GetDelegate()->URLMatchesDebugPatterns(manifest_url_); |
[email protected] | 7522077b | 2013-01-17 23:39:40 | [diff] [blame] | 706 | // Enable PPAPI proxy channel creation only for renderer processes. |
| 707 | params.enable_ipc_proxy = enable_ppapi_proxy(); |
[email protected] | 2e1e6f0 | 2013-01-11 18:22:56 | [diff] [blame] | 708 | params.uses_irt = uses_irt_; |
[email protected] | be52462 | 2013-04-23 00:12:19 | [diff] [blame] | 709 | params.enable_dyncode_syscalls = enable_dyncode_syscalls_; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 710 | |
| 711 | const ChildProcessData& data = process_->GetData(); |
[email protected] | 6294dd0 | 2013-01-09 17:27:23 | [diff] [blame] | 712 | if (!ShareHandleToSelLdr(data.handle, |
| 713 | internal_->socket_for_sel_ldr, true, |
| 714 | ¶ms.handles)) { |
| 715 | return false; |
[email protected] | 773ebb9 | 2011-11-15 19:06:52 | [diff] [blame] | 716 | } |
| 717 | |
[email protected] | 2e1e6f0 | 2013-01-11 18:22:56 | [diff] [blame] | 718 | if (params.uses_irt) { |
| 719 | base::PlatformFile irt_file = nacl_browser->IrtFile(); |
| 720 | CHECK_NE(irt_file, base::kInvalidPlatformFileValue); |
[email protected] | 2e1e6f0 | 2013-01-11 18:22:56 | [diff] [blame] | 721 | // Send over the IRT file handle. We don't close our own copy! |
| 722 | if (!ShareHandleToSelLdr(data.handle, irt_file, false, ¶ms.handles)) |
| 723 | return false; |
| 724 | } |
[email protected] | c47ec40 | 2010-07-29 10:20:49 | [diff] [blame] | 725 | |
[email protected] | ab88d154 | 2011-11-18 22:52:00 | [diff] [blame] | 726 | #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] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 731 | base::SharedMemory memory_buffer; |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 732 | base::SharedMemoryCreateOptions options; |
| 733 | options.size = 1; |
| 734 | options.executable = true; |
| 735 | if (!memory_buffer.Create(options)) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 736 | DLOG(ERROR) << "Failed to allocate memory buffer"; |
| 737 | return false; |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 738 | } |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 739 | FileDescriptor memory_fd; |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 740 | memory_fd.fd = dup(memory_buffer.handle().fd); |
| 741 | if (memory_fd.fd < 0) { |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 742 | DLOG(ERROR) << "Failed to dup() a file descriptor"; |
| 743 | return false; |
[email protected] | cbbe784 | 2011-11-17 22:01:25 | [diff] [blame] | 744 | } |
| 745 | memory_fd.auto_close = true; |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 746 | params.handles.push_back(memory_fd); |
[email protected] | 2c68bf03 | 2010-11-11 23:16:30 | [diff] [blame] | 747 | #endif |
| 748 | |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 749 | #if defined(OS_POSIX) |
[email protected] | ed637bb | 2012-10-15 17:36:05 | [diff] [blame] | 750 | if (params.enable_debug_stub) { |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 751 | net::SocketDescriptor server_bound_socket = GetDebugStubSocketHandle(); |
| 752 | if (server_bound_socket != net::kInvalidSocket) { |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 753 | params.debug_stub_server_bound_socket = |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 754 | FileDescriptor(server_bound_socket, true); |
[email protected] | d571c03 | 2012-09-14 12:39:18 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | #endif |
| 758 | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 759 | process_->Send(new NaClProcessMsg_Start(params)); |
[email protected] | b39c6d9 | 2012-01-31 16:38:41 | [diff] [blame] | 760 | |
[email protected] | c8e1ea3 | 2013-02-13 02:19:01 | [diff] [blame] | 761 | internal_->socket_for_sel_ldr = NACL_INVALID_HANDLE; |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 762 | return true; |
| 763 | } |
| 764 | |
[email protected] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 765 | bool NaClProcessHost::SendStart() { |
[email protected] | 7522077b | 2013-01-17 23:39:40 | [diff] [blame] | 766 | if (!enable_ppapi_proxy()) { |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 767 | if (!ReplyToRenderer(IPC::ChannelHandle())) |
| 768 | return false; |
| 769 | } |
| 770 | return StartNaClExecution(); |
| 771 | } |
| 772 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 773 | // This method is called when NaClProcessHostMsg_PpapiChannelCreated is |
| 774 | // received or PpapiHostMsg_ChannelCreated is forwarded by our plugin |
| 775 | // listener. |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 776 | void NaClProcessHost::OnPpapiChannelCreated( |
| 777 | const IPC::ChannelHandle& channel_handle) { |
[email protected] | 7522077b | 2013-01-17 23:39:40 | [diff] [blame] | 778 | // Only renderer processes should create a channel. |
| 779 | DCHECK(enable_ppapi_proxy()); |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 780 | // If the proxy channel is null, this must be the initial NaCl-Browser IPC |
| 781 | // channel. |
| 782 | if (!ipc_proxy_channel_.get()) { |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 783 | DCHECK_EQ(PROCESS_TYPE_NACL_LOADER, process_->GetData().process_type); |
[email protected] | 9e14121 | 2013-01-21 15:21:56 | [diff] [blame] | 784 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 785 | ipc_proxy_channel_.reset( |
| 786 | new IPC::ChannelProxy(channel_handle, |
| 787 | IPC::Channel::MODE_CLIENT, |
| 788 | &ipc_plugin_listener_, |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 789 | base::MessageLoopProxy::current().get())); |
[email protected] | 0c719374 | 2012-11-07 19:05:03 | [diff] [blame] | 790 | // Create the browser ppapi host and enable PPAPI message dispatching to the |
| 791 | // browser process. |
[email protected] | adf7cd5 | 2012-12-04 19:37:46 | [diff] [blame] | 792 | ppapi_host_.reset(content::BrowserPpapiHost::CreateExternalPluginProcess( |
[email protected] | 9e14121 | 2013-01-21 15:21:56 | [diff] [blame] | 793 | ipc_proxy_channel_.get(), // sender |
[email protected] | 0c719374 | 2012-11-07 19:05:03 | [diff] [blame] | 794 | permissions_, |
| 795 | process_->GetData().handle, |
[email protected] | b4b53d18 | 2012-09-12 02:56:55 | [diff] [blame] | 796 | ipc_proxy_channel_.get(), |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame] | 797 | nacl_host_message_filter_->render_process_id(), |
[email protected] | 7bb5d2c | 2013-04-29 22:33:49 | [diff] [blame] | 798 | render_view_id_, |
| 799 | profile_directory_)); |
[email protected] | adf7cd5 | 2012-12-04 19:37:46 | [diff] [blame] | 800 | |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 801 | ppapi::PpapiNaClChannelArgs args; |
[email protected] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame] | 802 | args.off_the_record = nacl_host_message_filter_->off_the_record(); |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 803 | 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] | 4b30e7d | 2013-04-24 23:52:14 | [diff] [blame] | 815 | ppapi_host_->GetPpapiHost()->AddHostFactoryFilter( |
| 816 | scoped_ptr<ppapi::host::HostFactory>( |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 817 | NaClBrowser::GetDelegate()->CreatePpapiHostFactory( |
[email protected] | 6fff31c2 | 2013-07-09 02:08:19 | [diff] [blame] | 818 | ppapi_host_.get()))); |
[email protected] | 4b30e7d | 2013-04-24 23:52:14 | [diff] [blame] | 819 | |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 820 | // 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] | 01e5975 | 2013-06-18 00:17:35 | [diff] [blame] | 824 | nacl_host_message_filter_->render_process_id(), |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 825 | args, |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 826 | 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 | |
| 838 | bool 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] | 00d9954 | 2012-04-17 22:48:02 | [diff] [blame] | 848 | } |
| 849 | |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 850 | bool NaClProcessHost::StartWithLaunchedProcess() { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 851 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | 09afc2e | 2012-04-10 17:29:03 | [diff] [blame] | 852 | |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 853 | 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] | 05c7cc7 | 2013-07-22 18:01:30 | [diff] [blame] | 861 | SendErrorToRenderer("previously failed to acquire shared resources"); |
[email protected] | e97990f | 2012-05-15 22:06:06 | [diff] [blame] | 862 | return false; |
| 863 | } |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 864 | } |
| 865 | |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 866 | void NaClProcessHost::OnQueryKnownToValidate(const std::string& signature, |
| 867 | bool* result) { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 868 | NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); |
[email protected] | 57473ebc | 2012-05-30 07:44:52 | [diff] [blame] | 869 | *result = nacl_browser->QueryKnownToValidate(signature, off_the_record_); |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | void NaClProcessHost::OnSetKnownToValidate(const std::string& signature) { |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 873 | NaClBrowser::GetInstance()->SetKnownToValidate( |
[email protected] | ef42ee8 | 2013-11-01 05:26:13 | [diff] [blame] | 874 | signature, off_the_record_); |
[email protected] | 4a0141b | 2012-03-27 01:15:30 | [diff] [blame] | 875 | } |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 876 | |
[email protected] | 04183452 | 2013-05-29 00:19:27 | [diff] [blame] | 877 | void 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 | |
| 899 | void 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] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 926 | if (!NaClBrowser::GetInstance()->GetFilePath( |
[email protected] | ef42ee8 | 2013-11-01 05:26:13 | [diff] [blame] | 927 | file_token_lo, file_token_hi, &file_path)) { |
[email protected] | 04183452 | 2013-05-29 00:19:27 | [diff] [blame] | 928 | NaClProcessMsg_ResolveFileToken::WriteReplyParams( |
| 929 | reply_msg, |
| 930 | IPC::InvalidPlatformFileForTransit(), |
| 931 | base::FilePath(FILE_PATH_LITERAL(""))); |
| 932 | Send(reply_msg); |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | // Scratch space to share between the callbacks. |
| 937 | base::PlatformFile* data = new base::PlatformFile(); |
| 938 | |
| 939 | // Open the file. |
| 940 | if (!content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 941 | FROM_HERE, |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 942 | base::Bind(OpenNaClExecutableImpl, |
[email protected] | 04183452 | 2013-05-29 00:19:27 | [diff] [blame] | 943 | file_path, data), |
| 944 | base::Bind(&NaClProcessHost::FileResolved, |
| 945 | weak_factory_.GetWeakPtr(), |
| 946 | base::Owned(data), |
| 947 | file_path, |
| 948 | reply_msg))) { |
| 949 | NaClProcessMsg_ResolveFileToken::WriteReplyParams( |
| 950 | reply_msg, |
| 951 | IPC::InvalidPlatformFileForTransit(), |
| 952 | base::FilePath(FILE_PATH_LITERAL(""))); |
| 953 | Send(reply_msg); |
| 954 | } |
| 955 | } |
| 956 | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 957 | #if defined(OS_WIN) |
| 958 | void 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 | |
| 968 | bool NaClProcessHost::AttachDebugExceptionHandler(const std::string& info, |
| 969 | IPC::Message* reply_msg) { |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 970 | if (!enable_exception_handling_ && !enable_debug_stub_) { |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 971 | DLOG(ERROR) << |
[email protected] | 2c227ff | 2012-08-11 00:51:32 | [diff] [blame] | 972 | "Debug exception handler requested by NaCl process when not enabled"; |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 973 | 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] | 2917232 | 2013-01-10 17:35:16 | [diff] [blame] | 1000 | base::kProcessAccessDuplicateHandle | |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 1001 | 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] | fd37bf6 | 2012-05-04 16:46:48 | [diff] [blame] | 1014 | weak_factory_.GetWeakPtr(), nacl_pid, process_handle, info); |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 1015 | } else { |
| 1016 | NaClStartDebugExceptionHandlerThread( |
[email protected] | fd37bf6 | 2012-05-04 16:46:48 | [diff] [blame] | 1017 | process_handle.Take(), info, |
[email protected] | ea658884 | 2012-05-03 05:39:38 | [diff] [blame] | 1018 | base::MessageLoopProxy::current(), |
| 1019 | base::Bind(&NaClProcessHost::OnDebugExceptionHandlerLaunchedByBroker, |
| 1020 | weak_factory_.GetWeakPtr())); |
| 1021 | return true; |
| 1022 | } |
| 1023 | } |
| 1024 | #endif |
[email protected] | 7c766e9 | 2013-11-22 20:44:02 | [diff] [blame^] | 1025 | |
| 1026 | } // namespace nacl |