1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/nacl/loader/nacl_listener.h"
16 #include "base/command_line.h"
17 #include "base/logging.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/message_loop/message_loop.h"
20 #include "base/rand_util.h"
21 #include "components/nacl/common/nacl_messages.h"
22 #include "components/nacl/common/nacl_renderer_messages.h"
23 #include "components/nacl/loader/nacl_ipc_adapter.h"
24 #include "components/nacl/loader/nacl_validation_db.h"
25 #include "components/nacl/loader/nacl_validation_query.h"
26 #include "ipc/ipc_channel_handle.h"
27 #include "ipc/ipc_switches.h"
28 #include "ipc/ipc_sync_channel.h"
29 #include "ipc/ipc_sync_message_filter.h"
30 #include "native_client/src/public/chrome_main.h"
31 #include "native_client/src/public/nacl_app.h"
32 #include "native_client/src/public/nacl_file_info.h"
33 #include "native_client/src/trusted/service_runtime/include/sys/fcntl.h"
36 #include "base/file_descriptor_posix.h"
40 #include "content/public/common/child_process_sandbox_support_linux.h"
47 #include "content/public/common/sandbox_init.h"
52 NaClListener
* g_listener
;
54 void FatalLogHandler(const char* data
, size_t bytes
) {
55 // We use uint32_t rather than size_t for the case when the browser and NaCl
56 // processes are a mix of 32-bit and 64-bit processes.
57 uint32_t copy_bytes
= std::min
<uint32_t>(static_cast<uint32_t>(bytes
),
58 nacl::kNaClCrashInfoMaxLogSize
);
60 // We copy the length of the crash data to the start of the shared memory
61 // segment so we know how much to copy.
62 memcpy(g_listener
->crash_info_shmem_memory(), ©_bytes
, sizeof(uint32_t));
64 memcpy((char*)g_listener
->crash_info_shmem_memory() + sizeof(uint32_t),
69 #if defined(OS_MACOSX)
71 // On Mac OS X, shm_open() works in the sandbox but does not give us
72 // an FD that we can map as PROT_EXEC. Rather than doing an IPC to
73 // get an executable SHM region when CreateMemoryObject() is called,
74 // we preallocate one on startup, since NaCl's sel_ldr only needs one
75 // of them. This saves a round trip.
77 base::subtle::Atomic32 g_shm_fd
= -1;
79 int CreateMemoryObject(size_t size
, int executable
) {
80 if (executable
&& size
> 0) {
81 int result_fd
= base::subtle::NoBarrier_AtomicExchange(&g_shm_fd
, -1);
82 if (result_fd
!= -1) {
83 // ftruncate() is disallowed by the Mac OS X sandbox and
84 // returns EPERM. Luckily, we can get the same effect with
86 if (lseek(result_fd
, size
- 1, SEEK_SET
) == -1) {
87 LOG(ERROR
) << "lseek() failed: " << errno
;
90 if (write(result_fd
, "", 1) != 1) {
91 LOG(ERROR
) << "write() failed: " << errno
;
97 // Fall back to NaCl's default implementation.
101 #elif defined(OS_LINUX)
103 int CreateMemoryObject(size_t size
, int executable
) {
104 return content::MakeSharedMemorySegmentViaIPC(size
, executable
);
107 #elif defined(OS_WIN)
108 // We wrap the function to convert the bool return value to an int.
109 int BrokerDuplicateHandle(NaClHandle source_handle
,
111 NaClHandle
* target_handle
,
112 uint32_t desired_access
,
114 return content::BrokerDuplicateHandle(source_handle
, process_id
,
115 target_handle
, desired_access
,
119 int AttachDebugExceptionHandler(const void* info
, size_t info_size
) {
120 std::string
info_string(reinterpret_cast<const char*>(info
), info_size
);
122 if (!g_listener
->Send(new NaClProcessMsg_AttachDebugExceptionHandler(
123 info_string
, &result
)))
128 void DebugStubPortSelectedHandler(uint16_t port
) {
129 g_listener
->Send(new NaClProcessHostMsg_DebugStubPortSelected(port
));
134 // Creates the PPAPI IPC channel between the NaCl IRT and the host
135 // (browser/renderer) process, and starts to listen it on the thread where
136 // the given message_loop_proxy runs.
137 // Also, creates and sets the corresponding NaClDesc to the given nap with
139 void SetUpIPCAdapter(IPC::ChannelHandle
* handle
,
140 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
,
143 scoped_refptr
<NaClIPCAdapter
> ipc_adapter(
144 new NaClIPCAdapter(*handle
, message_loop_proxy
.get()));
145 ipc_adapter
->ConnectChannel();
146 #if defined(OS_POSIX)
148 base::FileDescriptor(ipc_adapter
->TakeClientFileDescriptor(), true);
151 // Pass a NaClDesc to the untrusted side. This will hold a ref to the
153 NaClAppSetDesc(nap
, nacl_fd
, ipc_adapter
->MakeNaClDesc());
158 class BrowserValidationDBProxy
: public NaClValidationDB
{
160 explicit BrowserValidationDBProxy(NaClListener
* listener
)
161 : listener_(listener
) {
164 virtual bool QueryKnownToValidate(const std::string
& signature
) OVERRIDE
{
165 // Initialize to false so that if the Send fails to write to the return
166 // value we're safe. For example if the message is (for some reason)
167 // dispatched as an async message the return parameter will not be written.
169 if (!listener_
->Send(new NaClProcessMsg_QueryKnownToValidate(signature
,
171 LOG(ERROR
) << "Failed to query NaCl validation cache.";
177 virtual void SetKnownToValidate(const std::string
& signature
) OVERRIDE
{
178 // Caching is optional: NaCl will still work correctly if the IPC fails.
179 if (!listener_
->Send(new NaClProcessMsg_SetKnownToValidate(signature
))) {
180 LOG(ERROR
) << "Failed to update NaCl validation cache.";
184 virtual bool ResolveFileToken(struct NaClFileToken
* file_token
,
185 int32
* fd
, std::string
* path
) OVERRIDE
{
188 if (!NaClFileTokenIsValid(file_token
)) {
191 IPC::PlatformFileForTransit ipc_fd
= IPC::InvalidPlatformFileForTransit();
192 base::FilePath ipc_path
;
193 if (!listener_
->Send(new NaClProcessMsg_ResolveFileToken(file_token
->lo
,
199 if (ipc_fd
== IPC::InvalidPlatformFileForTransit()) {
202 base::PlatformFile handle
=
203 IPC::PlatformFileForTransitToPlatformFile(ipc_fd
);
205 // On Windows, valid handles are 32 bit unsigned integers so this is safe.
206 *fd
= reinterpret_cast<uintptr_t>(handle
);
210 // It doesn't matter if the path is invalid UTF8 as long as it's consistent
212 *path
= ipc_path
.AsUTF8Unsafe();
217 // The listener never dies, otherwise this might be a dangling reference.
218 NaClListener
* listener_
;
222 NaClListener::NaClListener() : shutdown_event_(true, false),
223 io_thread_("NaCl_IOThread"),
224 #if defined(OS_LINUX)
225 prereserved_sandbox_size_(0),
227 #if defined(OS_POSIX)
228 number_of_cores_(-1), // unknown/error
231 io_thread_
.StartWithOptions(
232 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0));
233 DCHECK(g_listener
== NULL
);
237 NaClListener::~NaClListener() {
239 shutdown_event_
.Signal();
243 bool NaClListener::Send(IPC::Message
* msg
) {
244 DCHECK(main_loop_
!= NULL
);
245 if (base::MessageLoop::current() == main_loop_
) {
246 // This thread owns the channel.
247 return channel_
->Send(msg
);
249 // This thread does not own the channel.
250 return filter_
->Send(msg
);
254 void NaClListener::Listen() {
255 std::string channel_name
=
256 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
257 switches::kProcessChannelID
);
258 channel_
= IPC::SyncChannel::Create(
259 this, io_thread_
.message_loop_proxy().get(), &shutdown_event_
);
260 filter_
= new IPC::SyncMessageFilter(&shutdown_event_
);
261 channel_
->AddFilter(filter_
.get());
262 channel_
->Init(channel_name
, IPC::Channel::MODE_CLIENT
, true);
263 main_loop_
= base::MessageLoop::current();
267 bool NaClListener::OnMessageReceived(const IPC::Message
& msg
) {
269 IPC_BEGIN_MESSAGE_MAP(NaClListener
, msg
)
270 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start
, OnStart
)
271 IPC_MESSAGE_UNHANDLED(handled
= false)
272 IPC_END_MESSAGE_MAP()
276 void NaClListener::OnStart(const nacl::NaClStartParams
& params
) {
277 #if defined(OS_LINUX) || defined(OS_MACOSX)
278 int urandom_fd
= dup(base::GetUrandomFD());
279 if (urandom_fd
< 0) {
280 LOG(ERROR
) << "Failed to dup() the urandom FD";
283 NaClChromeMainSetUrandomFd(urandom_fd
);
285 struct NaClApp
* nap
= NULL
;
286 NaClChromeMainInit();
288 crash_info_shmem_
.reset(new base::SharedMemory(params
.crash_info_shmem_handle
,
290 CHECK(crash_info_shmem_
->Map(nacl::kNaClCrashInfoShmemSize
));
291 NaClSetFatalErrorCallback(&FatalLogHandler
);
293 nap
= NaClAppCreate();
295 LOG(ERROR
) << "NaClAppCreate() failed";
299 IPC::ChannelHandle browser_handle
;
300 IPC::ChannelHandle ppapi_renderer_handle
;
302 if (params
.enable_ipc_proxy
) {
303 browser_handle
= IPC::Channel::GenerateVerifiedChannelID("nacl");
304 ppapi_renderer_handle
= IPC::Channel::GenerateVerifiedChannelID("nacl");
306 // Create the PPAPI IPC channels between the NaCl IRT and the host
307 // (browser/renderer) processes. The IRT uses these channels to
308 // communicate with the host and to initialize the IPC dispatchers.
309 SetUpIPCAdapter(&browser_handle
, io_thread_
.message_loop_proxy(),
310 nap
, NACL_CHROME_DESC_BASE
);
311 SetUpIPCAdapter(&ppapi_renderer_handle
, io_thread_
.message_loop_proxy(),
312 nap
, NACL_CHROME_DESC_BASE
+ 1);
315 trusted_listener_
= new NaClTrustedListener(
316 IPC::Channel::GenerateVerifiedChannelID("nacl"),
317 io_thread_
.message_loop_proxy().get(),
319 if (!Send(new NaClProcessHostMsg_PpapiChannelsCreated(
321 ppapi_renderer_handle
,
322 trusted_listener_
->TakeClientChannelHandle(),
323 IPC::ChannelHandle())))
324 LOG(ERROR
) << "Failed to send IPC channel handle to NaClProcessHost.";
326 std::vector
<nacl::FileDescriptor
> handles
= params
.handles
;
327 struct NaClChromeMainArgs
* args
= NaClChromeMainArgsCreate();
329 LOG(ERROR
) << "NaClChromeMainArgsCreate() failed";
333 #if defined(OS_LINUX) || defined(OS_MACOSX)
334 args
->number_of_cores
= number_of_cores_
;
335 args
->create_memory_object_func
= CreateMemoryObject
;
336 # if defined(OS_MACOSX)
337 CHECK(handles
.size() >= 1);
338 g_shm_fd
= nacl::ToNativeHandle(handles
[handles
.size() - 1]);
343 if (params
.uses_irt
) {
344 CHECK(handles
.size() >= 1);
345 NaClHandle irt_handle
= nacl::ToNativeHandle(handles
[handles
.size() - 1]);
349 args
->irt_fd
= _open_osfhandle(reinterpret_cast<intptr_t>(irt_handle
),
350 _O_RDONLY
| _O_BINARY
);
351 if (args
->irt_fd
< 0) {
352 LOG(ERROR
) << "_open_osfhandle() failed";
356 args
->irt_fd
= irt_handle
;
359 // Otherwise, the IRT handle is not even sent.
363 if (params
.validation_cache_enabled
) {
364 // SHA256 block size.
365 CHECK_EQ(params
.validation_cache_key
.length(), (size_t) 64);
366 // The cache structure is not freed and exists until the NaCl process exits.
367 args
->validation_cache
= CreateValidationCache(
368 new BrowserValidationDBProxy(this), params
.validation_cache_key
,
372 CHECK(handles
.size() == 1);
373 args
->imc_bootstrap_handle
= nacl::ToNativeHandle(handles
[0]);
374 args
->enable_exception_handling
= params
.enable_exception_handling
;
375 args
->enable_debug_stub
= params
.enable_debug_stub
;
376 args
->enable_dyncode_syscalls
= params
.enable_dyncode_syscalls
;
377 if (!params
.enable_dyncode_syscalls
) {
378 // Bound the initial nexe's code segment size under PNaCl to
379 // reduce the chance of a code spraying attack succeeding (see
380 // https://code.google.com/p/nativeclient/issues/detail?id=3572).
381 // We assume that !params.enable_dyncode_syscalls is synonymous
382 // with PNaCl. We can't apply this arbitrary limit outside of
383 // PNaCl because it might break existing NaCl apps, and this limit
384 // is only useful if the dyncode syscalls are disabled.
385 args
->initial_nexe_max_code_bytes
= 64 << 20; // 64 MB
387 // Indicate that this is a PNaCl module.
388 // TODO(jvoung): Plumb through something indicating that this is PNaCl
389 // instead of relying on enable_dyncode_syscalls.
390 args
->pnacl_mode
= 1;
392 #if defined(OS_LINUX) || defined(OS_MACOSX)
393 args
->debug_stub_server_bound_socket_fd
= nacl::ToNativeHandle(
394 params
.debug_stub_server_bound_socket
);
397 args
->broker_duplicate_handle_func
= BrokerDuplicateHandle
;
398 args
->attach_debug_exception_handler_func
= AttachDebugExceptionHandler
;
399 args
->debug_stub_server_port_selected_handler_func
=
400 DebugStubPortSelectedHandler
;
402 #if defined(OS_LINUX)
403 args
->prereserved_sandbox_size
= prereserved_sandbox_size_
;
406 NaClFileInfo nexe_file_info
;
407 base::PlatformFile nexe_file
= IPC::PlatformFileForTransitToPlatformFile(
410 nexe_file_info
.desc
=
411 _open_osfhandle(reinterpret_cast<intptr_t>(nexe_file
),
412 _O_RDONLY
| _O_BINARY
);
413 #elif defined(OS_POSIX)
414 nexe_file_info
.desc
= nexe_file
;
416 #error Unsupported target platform.
418 nexe_file_info
.file_token
.lo
= params
.nexe_token_lo
;
419 nexe_file_info
.file_token
.hi
= params
.nexe_token_hi
;
420 args
->nexe_desc
= NaClDescIoFromFileInfo(nexe_file_info
, NACL_ABI_O_RDONLY
);
423 if (!NaClChromeMainStart(nap
, args
, &exit_status
))
426 // Report the plugin's exit status if the application started successfully.
427 trusted_listener_
->Send(new NaClRendererMsg_ReportExitStatus(exit_status
));
428 NaClExit(exit_status
);