Fix experimental app list search box disappearing on profile switch.
[chromium-blink-merge.git] / components / nacl / loader / nacl_helper_linux.cc
blob6f7981920e81d89327293f201264684e37373cd6
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 // A mini-zygote specifically for Native Client.
7 #include "components/nacl/loader/nacl_helper_linux.h"
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/socket.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
18 #include <string>
19 #include <vector>
21 #include "base/at_exit.h"
22 #include "base/command_line.h"
23 #include "base/files/scoped_file.h"
24 #include "base/logging.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "base/memory/scoped_vector.h"
27 #include "base/message_loop/message_loop.h"
28 #include "base/posix/eintr_wrapper.h"
29 #include "base/posix/global_descriptors.h"
30 #include "base/posix/unix_domain_socket_linux.h"
31 #include "base/process/kill.h"
32 #include "base/process/process_handle.h"
33 #include "base/rand_util.h"
34 #include "components/nacl/common/nacl_switches.h"
35 #include "components/nacl/loader/nacl_listener.h"
36 #include "components/nacl/loader/nonsfi/nonsfi_listener.h"
37 #include "components/nacl/loader/sandbox_linux/nacl_sandbox_linux.h"
38 #include "content/public/common/content_descriptors.h"
39 #include "content/public/common/send_zygote_child_ping_linux.h"
40 #include "content/public/common/zygote_fork_delegate_linux.h"
41 #include "crypto/nss_util.h"
42 #include "ipc/ipc_descriptors.h"
43 #include "ipc/ipc_switches.h"
44 #include "sandbox/linux/services/libc_urandom_override.h"
46 #if defined(OS_NACL_NONSFI)
47 #include "native_client/src/public/nonsfi/irt_exception_handling.h"
48 #else
49 #include <link.h>
50 #include "components/nacl/loader/nonsfi/irt_exception_handling.h"
51 #endif
53 namespace {
55 struct NaClLoaderSystemInfo {
56 size_t prereserved_sandbox_size;
57 long number_of_cores;
60 // Replace |file_descriptor| with the reading end of a closed pipe.
61 void ReplaceFDWithDummy(int file_descriptor) {
62 // Make sure that file_descriptor is an open descriptor.
63 PCHECK(-1 != fcntl(file_descriptor, F_GETFD, 0));
64 int pipefd[2];
65 PCHECK(0 == pipe(pipefd));
66 PCHECK(-1 != dup2(pipefd[0], file_descriptor));
67 PCHECK(0 == IGNORE_EINTR(close(pipefd[0])));
68 PCHECK(0 == IGNORE_EINTR(close(pipefd[1])));
71 // The child must mimic the behavior of zygote_main_linux.cc on the child
72 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from
73 // if (!child) {
74 void BecomeNaClLoader(base::ScopedFD browser_fd,
75 const NaClLoaderSystemInfo& system_info,
76 bool uses_nonsfi_mode,
77 nacl::NaClSandbox* nacl_sandbox) {
78 #if !defined(OS_NACL_NONSFI)
79 // Currently sandbox is disabled for nacl_helper_nonsfi.
80 // TODO(hidehiko): Enable sandbox.
81 DCHECK(nacl_sandbox);
82 #endif
83 VLOG(1) << "NaCl loader: setting up IPC descriptor";
84 // Close or shutdown IPC channels that we don't need anymore.
85 PCHECK(0 == IGNORE_EINTR(close(kNaClZygoteDescriptor)));
86 // In Non-SFI mode, it's important to close any non-expected IPC channels.
87 if (uses_nonsfi_mode) {
88 // The low-level kSandboxIPCChannel is used by renderers and NaCl for
89 // various operations. See the LinuxSandbox::METHOD_* methods. NaCl uses
90 // LinuxSandbox::METHOD_MAKE_SHARED_MEMORY_SEGMENT in SFI mode, so this
91 // should only be closed in Non-SFI mode.
92 // This file descriptor is insidiously used by a number of APIs. Closing it
93 // could lead to difficult to debug issues. Instead of closing it, replace
94 // it with a dummy.
95 const int sandbox_ipc_channel =
96 base::GlobalDescriptors::kBaseDescriptor + kSandboxIPCChannel;
98 ReplaceFDWithDummy(sandbox_ipc_channel);
100 // Install crash signal handlers before disallowing system calls.
101 #if defined(OS_NACL_NONSFI)
102 nonsfi_initialize_signal_handler();
103 #else
104 nacl::nonsfi::InitializeSignalHandler();
105 #endif
108 // Always ignore SIGPIPE, for consistency with other Chrome processes and
109 // because some IPC code, such as sync_socket_posix.cc, requires this.
110 // We do this before seccomp-bpf is initialized.
111 PCHECK(signal(SIGPIPE, SIG_IGN) != SIG_ERR);
113 #if !defined(OS_NACL_NONSFI)
114 // Currently sandbox is disabled for nacl_helper_nonsfi.
115 // TODO(hidehiko): Enable sandbox.
116 // Finish layer-1 sandbox initialization and initialize the layer-2 sandbox.
117 CHECK(!nacl_sandbox->HasOpenDirectory());
118 nacl_sandbox->InitializeLayerTwoSandbox(uses_nonsfi_mode);
119 nacl_sandbox->SealLayerOneSandbox();
120 nacl_sandbox->CheckSandboxingStateWithPolicy();
121 #endif
123 base::GlobalDescriptors::GetInstance()->Set(kPrimaryIPCChannel,
124 browser_fd.release());
126 base::MessageLoopForIO main_message_loop;
127 #if defined(OS_NACL_NONSFI)
128 CHECK(uses_nonsfi_mode);
129 nacl::nonsfi::NonSfiListener listener;
130 listener.Listen();
131 #else
132 // TODO(hidehiko): Drop Non-SFI supporting from nacl_helper after the
133 // nacl_helper_nonsfi switching is done.
134 if (uses_nonsfi_mode) {
135 nacl::nonsfi::NonSfiListener listener;
136 listener.Listen();
137 } else {
138 NaClListener listener;
139 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size);
140 listener.set_number_of_cores(system_info.number_of_cores);
141 listener.Listen();
143 #endif
144 _exit(0);
147 // Start the NaCl loader in a child created by the NaCl loader Zygote.
148 void ChildNaClLoaderInit(ScopedVector<base::ScopedFD> child_fds,
149 const NaClLoaderSystemInfo& system_info,
150 bool uses_nonsfi_mode,
151 nacl::NaClSandbox* nacl_sandbox,
152 const std::string& channel_id) {
153 DCHECK(child_fds.size() >
154 std::max(content::ZygoteForkDelegate::kPIDOracleFDIndex,
155 content::ZygoteForkDelegate::kBrowserFDIndex));
157 // Ping the PID oracle socket.
158 CHECK(content::SendZygoteChildPing(
159 child_fds[content::ZygoteForkDelegate::kPIDOracleFDIndex]->get()));
161 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
162 switches::kProcessChannelID, channel_id);
164 // Save the browser socket and close the rest.
165 base::ScopedFD browser_fd(
166 child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]->Pass());
167 child_fds.clear();
169 BecomeNaClLoader(
170 browser_fd.Pass(), system_info, uses_nonsfi_mode, nacl_sandbox);
171 _exit(1);
174 // Handle a fork request from the Zygote.
175 // Some of this code was lifted from
176 // content/browser/zygote_main_linux.cc:ForkWithRealPid()
177 bool HandleForkRequest(ScopedVector<base::ScopedFD> child_fds,
178 const NaClLoaderSystemInfo& system_info,
179 nacl::NaClSandbox* nacl_sandbox,
180 PickleIterator* input_iter,
181 Pickle* output_pickle) {
182 bool uses_nonsfi_mode;
183 if (!input_iter->ReadBool(&uses_nonsfi_mode)) {
184 LOG(ERROR) << "Could not read uses_nonsfi_mode status";
185 return false;
188 std::string channel_id;
189 if (!input_iter->ReadString(&channel_id)) {
190 LOG(ERROR) << "Could not read channel_id string";
191 return false;
194 if (content::ZygoteForkDelegate::kNumPassedFDs != child_fds.size()) {
195 LOG(ERROR) << "nacl_helper: unexpected number of fds, got "
196 << child_fds.size();
197 return false;
200 VLOG(1) << "nacl_helper: forking";
201 pid_t child_pid = fork();
202 if (child_pid < 0) {
203 PLOG(ERROR) << "*** fork() failed.";
206 if (child_pid == 0) {
207 ChildNaClLoaderInit(child_fds.Pass(),
208 system_info,
209 uses_nonsfi_mode,
210 nacl_sandbox,
211 channel_id);
212 NOTREACHED();
215 // I am the parent.
216 // First, close the dummy_fd so the sandbox won't find me when
217 // looking for the child's pid in /proc. Also close other fds.
218 child_fds.clear();
219 VLOG(1) << "nacl_helper: child_pid is " << child_pid;
221 // Now send child_pid (eventually -1 if fork failed) to the Chrome Zygote.
222 output_pickle->WriteInt(child_pid);
223 return true;
226 bool HandleGetTerminationStatusRequest(PickleIterator* input_iter,
227 Pickle* output_pickle) {
228 pid_t child_to_wait;
229 if (!input_iter->ReadInt(&child_to_wait)) {
230 LOG(ERROR) << "Could not read pid to wait for";
231 return false;
234 bool known_dead;
235 if (!input_iter->ReadBool(&known_dead)) {
236 LOG(ERROR) << "Could not read known_dead status";
237 return false;
239 // TODO(jln): With NaCl, known_dead seems to never be set to true (unless
240 // called from the Zygote's kZygoteCommandReap command). This means that we
241 // will sometimes detect the process as still running when it's not. Fix
242 // this!
244 int exit_code;
245 base::TerminationStatus status;
246 if (known_dead)
247 status = base::GetKnownDeadTerminationStatus(child_to_wait, &exit_code);
248 else
249 status = base::GetTerminationStatus(child_to_wait, &exit_code);
250 output_pickle->WriteInt(static_cast<int>(status));
251 output_pickle->WriteInt(exit_code);
252 return true;
255 // Honor a command |command_type|. Eventual command parameters are
256 // available in |input_iter| and eventual file descriptors attached to
257 // the command are in |attached_fds|.
258 // Reply to the command on |reply_fds|.
259 bool HonorRequestAndReply(int reply_fd,
260 int command_type,
261 ScopedVector<base::ScopedFD> attached_fds,
262 const NaClLoaderSystemInfo& system_info,
263 nacl::NaClSandbox* nacl_sandbox,
264 PickleIterator* input_iter) {
265 Pickle write_pickle;
266 bool have_to_reply = false;
267 // Commands must write anything to send back to |write_pickle|.
268 switch (command_type) {
269 case nacl::kNaClForkRequest:
270 have_to_reply = HandleForkRequest(attached_fds.Pass(),
271 system_info,
272 nacl_sandbox,
273 input_iter,
274 &write_pickle);
275 break;
276 case nacl::kNaClGetTerminationStatusRequest:
277 have_to_reply =
278 HandleGetTerminationStatusRequest(input_iter, &write_pickle);
279 break;
280 default:
281 LOG(ERROR) << "Unsupported command from Zygote";
282 return false;
284 if (!have_to_reply)
285 return false;
286 const std::vector<int> empty; // We never send file descriptors back.
287 if (!UnixDomainSocket::SendMsg(reply_fd, write_pickle.data(),
288 write_pickle.size(), empty)) {
289 LOG(ERROR) << "*** send() to zygote failed";
290 return false;
292 return true;
295 // Read a request from the Zygote from |zygote_ipc_fd| and handle it.
296 // Die on EOF from |zygote_ipc_fd|.
297 bool HandleZygoteRequest(int zygote_ipc_fd,
298 const NaClLoaderSystemInfo& system_info,
299 nacl::NaClSandbox* nacl_sandbox) {
300 ScopedVector<base::ScopedFD> fds;
301 char buf[kNaClMaxIPCMessageLength];
302 const ssize_t msglen = UnixDomainSocket::RecvMsg(zygote_ipc_fd,
303 &buf, sizeof(buf), &fds);
304 #if !defined(OS_NACL_NONSFI)
305 // Currently sandbox is disabled for nacl_helper_nonsfi.
306 // TODO(hidehiko): Enable sandbox.
307 // If the Zygote has started handling requests, we should be sandboxed via
308 // the setuid sandbox.
309 if (!nacl_sandbox->layer_one_enabled()) {
310 LOG(ERROR) << "NaCl helper process running without a sandbox!\n"
311 << "Most likely you need to configure your SUID sandbox "
312 << "correctly";
314 #endif
315 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) {
316 // EOF from the browser. Goodbye!
317 _exit(0);
319 if (msglen < 0) {
320 PLOG(ERROR) << "nacl_helper: receive from zygote failed";
321 return false;
324 Pickle read_pickle(buf, msglen);
325 PickleIterator read_iter(read_pickle);
326 int command_type;
327 if (!read_iter.ReadInt(&command_type)) {
328 LOG(ERROR) << "Unable to read command from Zygote";
329 return false;
331 return HonorRequestAndReply(zygote_ipc_fd,
332 command_type,
333 fds.Pass(),
334 system_info,
335 nacl_sandbox,
336 &read_iter);
339 #if !defined(OS_NACL_NONSFI)
340 static const char kNaClHelperReservedAtZero[] = "reserved_at_zero";
341 static const char kNaClHelperRDebug[] = "r_debug";
343 // Since we were started by nacl_helper_bootstrap rather than in the
344 // usual way, the debugger cannot figure out where our executable
345 // or the dynamic linker or the shared libraries are in memory,
346 // so it won't find any symbols. But we can fake it out to find us.
348 // The zygote passes --r_debug=0xXXXXXXXXXXXXXXXX.
349 // nacl_helper_bootstrap replaces the Xs with the address of its _r_debug
350 // structure. The debugger will look for that symbol by name to
351 // discover the addresses of key dynamic linker data structures.
352 // Since all it knows about is the original main executable, which
353 // is the bootstrap program, it finds the symbol defined there. The
354 // dynamic linker's structure is somewhere else, but it is filled in
355 // after initialization. The parts that really matter to the
356 // debugger never change. So we just copy the contents of the
357 // dynamic linker's structure into the address provided by the option.
358 // Hereafter, if someone attaches a debugger (or examines a core dump),
359 // the debugger will find all the symbols in the normal way.
361 // Non-SFI mode does not use nacl_helper_bootstrap, so it doesn't need to
362 // process the --r_debug option.
363 static void CheckRDebug(char* argv0) {
364 std::string r_debug_switch_value =
365 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
366 kNaClHelperRDebug);
367 if (!r_debug_switch_value.empty()) {
368 char* endp;
369 uintptr_t r_debug_addr = strtoul(r_debug_switch_value.c_str(), &endp, 0);
370 if (r_debug_addr != 0 && *endp == '\0') {
371 r_debug* bootstrap_r_debug = reinterpret_cast<r_debug*>(r_debug_addr);
372 *bootstrap_r_debug = _r_debug;
374 // Since the main executable (the bootstrap program) does not
375 // have a dynamic section, the debugger will not skip the
376 // first element of the link_map list as it usually would for
377 // an executable or PIE that was loaded normally. But the
378 // dynamic linker has set l_name for the PIE to "" as is
379 // normal for the main executable. So the debugger doesn't
380 // know which file it is. Fill in the actual file name, which
381 // came in as our argv[0].
382 link_map* l = _r_debug.r_map;
383 if (l->l_name[0] == '\0')
384 l->l_name = argv0;
389 // The zygote passes --reserved_at_zero=0xXXXXXXXXXXXXXXXX.
390 // nacl_helper_bootstrap replaces the Xs with the amount of prereserved
391 // sandbox memory.
393 // CheckReservedAtZero parses the value of the argument reserved_at_zero
394 // and returns the amount of prereserved sandbox memory.
395 static size_t CheckReservedAtZero() {
396 size_t prereserved_sandbox_size = 0;
397 std::string reserved_at_zero_switch_value =
398 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
399 kNaClHelperReservedAtZero);
400 if (!reserved_at_zero_switch_value.empty()) {
401 char* endp;
402 prereserved_sandbox_size =
403 strtoul(reserved_at_zero_switch_value.c_str(), &endp, 0);
404 if (*endp != '\0')
405 LOG(ERROR) << "Could not parse reserved_at_zero argument value of "
406 << reserved_at_zero_switch_value;
408 return prereserved_sandbox_size;
410 #endif
412 } // namespace
414 #if defined(ADDRESS_SANITIZER)
415 // Do not install the SIGSEGV handler in ASan. This should make the NaCl
416 // platform qualification test pass.
417 static const char kAsanDefaultOptionsNaCl[] = "handle_segv=0";
419 // Override the default ASan options for the NaCl helper.
420 // __asan_default_options should not be instrumented, because it is called
421 // before ASan is initialized.
422 extern "C"
423 __attribute__((no_sanitize_address))
424 // The function isn't referenced from the executable itself. Make sure it isn't
425 // stripped by the linker.
426 __attribute__((used))
427 __attribute__((visibility("default")))
428 const char* __asan_default_options() {
429 return kAsanDefaultOptionsNaCl;
431 #endif
433 int main(int argc, char* argv[]) {
434 base::CommandLine::Init(argc, argv);
435 base::AtExitManager exit_manager;
436 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised
438 #if !defined(OS_NACL_NONSFI)
439 // NSS is only needed for SFI NaCl.
440 // Allows NSS to fopen() /dev/urandom.
441 sandbox::InitLibcUrandomOverrides();
442 #if defined(USE_NSS)
443 // Configure NSS for use inside the NaCl process.
444 // The fork check has not caused problems for NaCl, but this appears to be
445 // best practice (see other places LoadNSSLibraries is called.)
446 crypto::DisableNSSForkCheck();
447 // Without this line on Linux, HMAC::Init will instantiate a singleton that
448 // in turn attempts to open a file. Disabling this behavior avoids a ~70 ms
449 // stall the first time HMAC is used.
450 crypto::ForceNSSNoDBInit();
451 // Load shared libraries before sandbox is raised.
452 // NSS is needed to perform hashing for validation caching.
453 crypto::LoadNSSLibraries();
454 #endif // defined(USE_NSS)
455 #endif // defined(OS_NACL_NONSFI)
456 const NaClLoaderSystemInfo system_info = {
457 #if !defined(OS_NACL_NONSFI)
458 // These are not used by nacl_helper_nonsfi.
459 CheckReservedAtZero(),
460 sysconf(_SC_NPROCESSORS_ONLN)
461 #endif
464 #if !defined(OS_NACL_NONSFI)
465 CheckRDebug(argv[0]);
466 #endif
468 #if defined(OS_NACL_NONSFI)
469 // Currently sandbox is disabled for nacl_helper_nonsfi.
470 // TODO(hidehiko): Enable sandbox.
471 scoped_ptr<nacl::NaClSandbox> nacl_sandbox;
472 #else
473 scoped_ptr<nacl::NaClSandbox> nacl_sandbox(new nacl::NaClSandbox);
474 // Make sure that the early initialization did not start any spurious
475 // threads.
476 #if !defined(THREAD_SANITIZER)
477 CHECK(nacl_sandbox->IsSingleThreaded());
478 #endif
480 const bool is_init_process = 1 == getpid();
481 nacl_sandbox->InitializeLayerOneSandbox();
482 CHECK_EQ(is_init_process, nacl_sandbox->layer_one_enabled());
483 #endif // defined(OS_NACL_NONSFI)
485 const std::vector<int> empty;
486 // Send the zygote a message to let it know we are ready to help
487 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor,
488 kNaClHelperStartupAck,
489 sizeof(kNaClHelperStartupAck), empty)) {
490 LOG(ERROR) << "*** send() to zygote failed";
493 // Now handle requests from the Zygote.
494 while (true) {
495 bool request_handled = HandleZygoteRequest(
496 kNaClZygoteDescriptor, system_info, nacl_sandbox.get());
497 // Do not turn this into a CHECK() without thinking about robustness
498 // against malicious IPC requests.
499 DCHECK(request_handled);
501 NOTREACHED();