Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/zygote/zygote_main.h" | 5 #include "content/zygote/zygote_main.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
| 8 #include <fcntl.h> | |
| 8 #include <pthread.h> | 9 #include <pthread.h> |
| 9 #include <string.h> | 10 #include <string.h> |
| 11 #include <sys/socket.h> | |
| 10 #include <sys/types.h> | 12 #include <sys/types.h> |
| 11 #include <unistd.h> | 13 #include <unistd.h> |
| 12 | 14 |
| 13 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 14 #include "base/bind.h" | 16 #include "base/bind.h" |
| 15 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 16 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 17 #include "base/memory/scoped_vector.h" | 19 #include "base/memory/scoped_vector.h" |
| 18 #include "base/native_library.h" | 20 #include "base/native_library.h" |
| 19 #include "base/pickle.h" | 21 #include "base/pickle.h" |
| 22 #include "base/posix/eintr_wrapper.h" | |
| 20 #include "base/posix/unix_domain_socket_linux.h" | 23 #include "base/posix/unix_domain_socket_linux.h" |
| 21 #include "base/rand_util.h" | 24 #include "base/rand_util.h" |
| 22 #include "base/sys_info.h" | 25 #include "base/sys_info.h" |
| 23 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 24 #include "content/common/child_process_sandbox_support_impl_linux.h" | 27 #include "content/common/child_process_sandbox_support_impl_linux.h" |
| 25 #include "content/common/font_config_ipc_linux.h" | 28 #include "content/common/font_config_ipc_linux.h" |
| 26 #include "content/common/pepper_plugin_list.h" | 29 #include "content/common/pepper_plugin_list.h" |
| 27 #include "content/common/sandbox_linux/sandbox_linux.h" | 30 #include "content/common/sandbox_linux/sandbox_linux.h" |
| 28 #include "content/common/zygote_commands_linux.h" | 31 #include "content/common/zygote_commands_linux.h" |
| 29 #include "content/public/common/content_switches.h" | 32 #include "content/public/common/content_switches.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 40 #include "third_party/skia/include/ports/SkFontConfigInterface.h" | 43 #include "third_party/skia/include/ports/SkFontConfigInterface.h" |
| 41 | 44 |
| 42 #if defined(OS_LINUX) | 45 #if defined(OS_LINUX) |
| 43 #include <sys/prctl.h> | 46 #include <sys/prctl.h> |
| 44 #endif | 47 #endif |
| 45 | 48 |
| 46 #if defined(ENABLE_WEBRTC) | 49 #if defined(ENABLE_WEBRTC) |
| 47 #include "third_party/libjingle/overrides/init_webrtc.h" | 50 #include "third_party/libjingle/overrides/init_webrtc.h" |
| 48 #endif | 51 #endif |
| 49 | 52 |
| 53 #if defined(ADDRESS_SANITIZER) | |
| 54 #include <sanitizer/asan_interface.h> | |
| 55 #endif | |
| 56 | |
| 50 namespace content { | 57 namespace content { |
| 51 | 58 |
| 52 // See http://code.google.com/p/chromium/wiki/LinuxZygote | 59 // See http://code.google.com/p/chromium/wiki/LinuxZygote |
| 53 | 60 |
| 54 static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output, | 61 static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output, |
| 55 char* timezone_out, | 62 char* timezone_out, |
| 56 size_t timezone_out_len) { | 63 size_t timezone_out_len) { |
| 57 Pickle request; | 64 Pickle request; |
| 58 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME); | 65 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME); |
| 59 request.WriteString( | 66 request.WriteString( |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 // Ensure access to the Pepper plugins before the sandbox is turned on. | 323 // Ensure access to the Pepper plugins before the sandbox is turned on. |
| 317 PreloadPepperPlugins(); | 324 PreloadPepperPlugins(); |
| 318 #endif | 325 #endif |
| 319 #if defined(ENABLE_WEBRTC) | 326 #if defined(ENABLE_WEBRTC) |
| 320 InitializeWebRtcModule(); | 327 InitializeWebRtcModule(); |
| 321 #endif | 328 #endif |
| 322 SkFontConfigInterface::SetGlobal( | 329 SkFontConfigInterface::SetGlobal( |
| 323 new FontConfigIPC(GetSandboxFD()))->unref(); | 330 new FontConfigIPC(GetSandboxFD()))->unref(); |
| 324 } | 331 } |
| 325 | 332 |
| 326 static bool CreateInitProcessReaper() { | 333 static bool CreateInitProcessReaper(base::Closure* post_fork_parent_callback) { |
| 327 // The current process becomes init(1), this function returns from a | 334 // The current process becomes init(1), this function returns from a |
| 328 // newly created process. | 335 // newly created process. |
| 329 const bool init_created = sandbox::CreateInitProcessReaper(NULL); | 336 const bool init_created = |
| 337 sandbox::CreateInitProcessReaper(post_fork_parent_callback); | |
| 330 if (!init_created) { | 338 if (!init_created) { |
| 331 LOG(ERROR) << "Error creating an init process to reap zombies"; | 339 LOG(ERROR) << "Error creating an init process to reap zombies"; |
| 332 return false; | 340 return false; |
| 333 } | 341 } |
| 334 return true; | 342 return true; |
| 335 } | 343 } |
| 336 | 344 |
| 337 // Enter the setuid sandbox. This requires the current process to have been | 345 // Enter the setuid sandbox. This requires the current process to have been |
| 338 // created through the setuid sandbox. | 346 // created through the setuid sandbox. |
| 339 static bool EnterSuidSandbox(sandbox::SetuidSandboxClient* setuid_sandbox) { | 347 static bool EnterSuidSandbox(sandbox::SetuidSandboxClient* setuid_sandbox, |
| 348 base::Closure* post_fork_parent_callback) { | |
| 340 DCHECK(setuid_sandbox); | 349 DCHECK(setuid_sandbox); |
| 341 DCHECK(setuid_sandbox->IsSuidSandboxChild()); | 350 DCHECK(setuid_sandbox->IsSuidSandboxChild()); |
| 342 | 351 |
| 343 // Use the SUID sandbox. This still allows the seccomp sandbox to | 352 // Use the SUID sandbox. This still allows the seccomp sandbox to |
| 344 // be enabled by the process later. | 353 // be enabled by the process later. |
| 345 | 354 |
| 346 if (!setuid_sandbox->IsSuidSandboxUpToDate()) { | 355 if (!setuid_sandbox->IsSuidSandboxUpToDate()) { |
| 347 LOG(WARNING) << | 356 LOG(WARNING) << |
| 348 "You are using a wrong version of the setuid binary!\n" | 357 "You are using a wrong version of the setuid binary!\n" |
| 349 "Please read " | 358 "Please read " |
| 350 "https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment." | 359 "https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment." |
| 351 "\n\n"; | 360 "\n\n"; |
| 352 } | 361 } |
| 353 | 362 |
| 354 if (!setuid_sandbox->ChrootMe()) | 363 if (!setuid_sandbox->ChrootMe()) |
| 355 return false; | 364 return false; |
| 356 | 365 |
| 357 if (setuid_sandbox->IsInNewPIDNamespace()) { | 366 if (setuid_sandbox->IsInNewPIDNamespace()) { |
| 358 CHECK_EQ(1, getpid()) | 367 CHECK_EQ(1, getpid()) |
| 359 << "The SUID sandbox created a new PID namespace but Zygote " | 368 << "The SUID sandbox created a new PID namespace but Zygote " |
| 360 "is not the init process. Please, make sure the SUID " | 369 "is not the init process. Please, make sure the SUID " |
| 361 "binary is up to date."; | 370 "binary is up to date."; |
| 362 } | 371 } |
| 363 | 372 |
| 364 if (getpid() == 1) { | 373 if (getpid() == 1) { |
| 365 // The setuid sandbox has created a new PID namespace and we need | 374 // The setuid sandbox has created a new PID namespace and we need |
| 366 // to assume the role of init. | 375 // to assume the role of init. |
| 367 CHECK(CreateInitProcessReaper()); | 376 CHECK(CreateInitProcessReaper(post_fork_parent_callback)); |
| 368 } | 377 } |
| 369 | 378 |
| 370 #if !defined(OS_OPENBSD) | 379 #if !defined(OS_OPENBSD) |
| 371 // Previously, we required that the binary be non-readable. This causes the | 380 // Previously, we required that the binary be non-readable. This causes the |
| 372 // kernel to mark the process as non-dumpable at startup. The thinking was | 381 // kernel to mark the process as non-dumpable at startup. The thinking was |
| 373 // that, although we were putting the renderers into a PID namespace (with | 382 // that, although we were putting the renderers into a PID namespace (with |
| 374 // the SUID sandbox), they would nonetheless be in the /same/ PID | 383 // the SUID sandbox), they would nonetheless be in the /same/ PID |
| 375 // namespace. So they could ptrace each other unless they were non-dumpable. | 384 // namespace. So they could ptrace each other unless they were non-dumpable. |
| 376 // | 385 // |
| 377 // If the binary was readable, then there would be a window between process | 386 // If the binary was readable, then there would be a window between process |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 391 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { | 400 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 392 LOG(ERROR) << "Failed to set non-dumpable flag"; | 401 LOG(ERROR) << "Failed to set non-dumpable flag"; |
| 393 return false; | 402 return false; |
| 394 } | 403 } |
| 395 } | 404 } |
| 396 #endif | 405 #endif |
| 397 | 406 |
| 398 return true; | 407 return true; |
| 399 } | 408 } |
| 400 | 409 |
| 410 #if defined(ADDRESS_SANITIZER) | |
| 411 const size_t kSanitizerMaxMessageLength = 1 * 1024 * 1024; | |
| 412 | |
| 413 // A helper process which collects code coverage data from the renderers over a | |
| 414 // socket and dumps it to a file. See http://crbug.com/336212 for discussion. | |
| 415 static void SanitizerCoverageHelper(int socket_fd, int file_fd) { | |
| 416 scoped_ptr<char[]> buffer(new char[kSanitizerMaxMessageLength]); | |
| 417 while (true) { | |
| 418 ssize_t received_size = HANDLE_EINTR( | |
| 419 recv(socket_fd, buffer.get(), kSanitizerMaxMessageLength, 0)); | |
| 420 PCHECK(received_size >= 0); | |
| 421 if (received_size == 0) | |
| 422 // All clients have closed the socket. We should die. | |
| 423 _exit(0); | |
| 424 PCHECK(file_fd >= 0); | |
| 425 ssize_t written_size = 0; | |
| 426 while (written_size < received_size) { | |
| 427 ssize_t write_res = | |
| 428 HANDLE_EINTR(write(file_fd, buffer.get() + written_size, | |
| 429 received_size - written_size)); | |
| 430 PCHECK(write_res >= 0); | |
| 431 written_size += write_res; | |
| 432 } | |
| 433 PCHECK(0 == HANDLE_EINTR(fsync(file_fd))); | |
| 434 } | |
| 435 } | |
| 436 | |
| 437 // fds[0] is the read end, fds[1] is the write end. | |
| 438 static void CreateSanitizerCoverageSocketPair(int fds[2]) { | |
| 439 PCHECK(0 == socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | |
| 440 PCHECK(0 == shutdown(fds[0], SHUT_WR)); | |
| 441 PCHECK(0 == shutdown(fds[1], SHUT_RD)); | |
| 442 } | |
| 443 | |
| 444 static pid_t ForkSanitizerCoverageHelper(int child_fd, int parent_fd, | |
| 445 base::ScopedFD file_fd) { | |
| 446 pid_t pid = fork(); | |
| 447 PCHECK(pid >= 0); | |
| 448 if (pid == 0) { | |
| 449 // In the child. | |
| 450 PCHECK(0 == IGNORE_EINTR(close(parent_fd))); | |
| 451 SanitizerCoverageHelper(child_fd, file_fd.get()); | |
| 452 _exit(0); | |
| 453 } else { | |
| 454 // In the parent. | |
| 455 PCHECK(0 == IGNORE_EINTR(close(child_fd))); | |
| 456 return pid; | |
| 457 } | |
| 458 } | |
| 459 | |
| 460 void CloseFdPair(const int fds[2]) { | |
| 461 PCHECK(0 == IGNORE_EINTR(close(fds[0]))); | |
| 462 PCHECK(0 == IGNORE_EINTR(close(fds[1]))); | |
| 463 } | |
| 464 #endif // defined(ADDRESS_SANITIZER) | |
| 465 | |
| 401 // If |is_suid_sandbox_child|, then make sure that the setuid sandbox is | 466 // If |is_suid_sandbox_child|, then make sure that the setuid sandbox is |
| 402 // engaged. | 467 // engaged. |
| 403 static void EnterLayerOneSandbox(LinuxSandbox* linux_sandbox, | 468 static void EnterLayerOneSandbox(LinuxSandbox* linux_sandbox, |
| 404 bool is_suid_sandbox_child) { | 469 bool is_suid_sandbox_child, |
| 470 base::Closure* post_fork_parent_callback) { | |
| 405 DCHECK(linux_sandbox); | 471 DCHECK(linux_sandbox); |
| 406 | 472 |
| 407 ZygotePreSandboxInit(); | 473 ZygotePreSandboxInit(); |
| 408 | 474 |
| 409 // Check that the pre-sandbox initialization didn't spawn threads. | 475 // Check that the pre-sandbox initialization didn't spawn threads. |
| 410 #if !defined(THREAD_SANITIZER) | 476 #if !defined(THREAD_SANITIZER) |
| 411 DCHECK(linux_sandbox->IsSingleThreaded()); | 477 DCHECK(linux_sandbox->IsSingleThreaded()); |
| 412 #endif | 478 #endif |
| 413 | 479 |
| 414 sandbox::SetuidSandboxClient* setuid_sandbox = | 480 sandbox::SetuidSandboxClient* setuid_sandbox = |
| 415 linux_sandbox->setuid_sandbox_client(); | 481 linux_sandbox->setuid_sandbox_client(); |
| 416 | 482 |
| 417 if (is_suid_sandbox_child) { | 483 if (is_suid_sandbox_child) { |
| 418 CHECK(EnterSuidSandbox(setuid_sandbox)) << "Failed to enter setuid sandbox"; | 484 CHECK(EnterSuidSandbox(setuid_sandbox, post_fork_parent_callback)) |
| 485 << "Failed to enter setuid sandbox"; | |
| 419 } | 486 } |
| 420 } | 487 } |
| 421 | 488 |
| 422 bool ZygoteMain(const MainFunctionParams& params, | 489 bool ZygoteMain(const MainFunctionParams& params, |
| 423 ScopedVector<ZygoteForkDelegate> fork_delegates) { | 490 ScopedVector<ZygoteForkDelegate> fork_delegates) { |
| 424 g_am_zygote_or_renderer = true; | 491 g_am_zygote_or_renderer = true; |
| 425 sandbox::InitLibcUrandomOverrides(); | 492 sandbox::InitLibcUrandomOverrides(); |
| 426 | 493 |
| 494 base::Closure *post_fork_parent_callback = NULL; | |
| 495 | |
| 427 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); | 496 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); |
| 497 | |
| 498 #if defined(ADDRESS_SANITIZER) | |
| 499 base::ScopedFD sancov_file_fd(__sanitizer_maybe_open_cov_file("zygote")); | |
| 500 int sancov_socket_fds[2] = {-1, -1}; | |
| 501 CreateSanitizerCoverageSocketPair(sancov_socket_fds); | |
| 502 linux_sandbox->sanitizer_args()->coverage_sandboxed = 1; | |
| 503 linux_sandbox->sanitizer_args()->coverage_fd = sancov_socket_fds[1]; | |
| 504 linux_sandbox->sanitizer_args()->coverage_max_block_size = | |
| 505 kSanitizerMaxMessageLength; | |
| 506 // Zygote termination will block until the helper process exits, which will | |
| 507 // not happen until the write end of the socket is closed everywhere. Make | |
| 508 // sure the init process does not hold on to it. | |
| 509 base::Closure close_sancov_socket_fds = | |
| 510 base::Bind(&CloseFdPair, sancov_socket_fds); | |
| 511 post_fork_parent_callback = &close_sancov_socket_fds; | |
| 512 #endif | |
| 513 | |
| 428 // This will pre-initialize the various sandboxes that need it. | 514 // This will pre-initialize the various sandboxes that need it. |
| 429 linux_sandbox->PreinitializeSandbox(); | 515 linux_sandbox->PreinitializeSandbox(); |
| 430 | 516 |
| 431 const bool must_enable_setuid_sandbox = | 517 const bool must_enable_setuid_sandbox = |
| 432 linux_sandbox->setuid_sandbox_client()->IsSuidSandboxChild(); | 518 linux_sandbox->setuid_sandbox_client()->IsSuidSandboxChild(); |
| 433 if (must_enable_setuid_sandbox) { | 519 if (must_enable_setuid_sandbox) { |
| 434 linux_sandbox->setuid_sandbox_client()->CloseDummyFile(); | 520 linux_sandbox->setuid_sandbox_client()->CloseDummyFile(); |
| 435 | 521 |
| 436 // Let the ZygoteHost know we're booting up. | 522 // Let the ZygoteHost know we're booting up. |
| 437 CHECK(UnixDomainSocket::SendMsg(kZygoteSocketPairFd, | 523 CHECK(UnixDomainSocket::SendMsg(kZygoteSocketPairFd, |
| 438 kZygoteBootMessage, | 524 kZygoteBootMessage, |
| 439 sizeof(kZygoteBootMessage), | 525 sizeof(kZygoteBootMessage), |
| 440 std::vector<int>())); | 526 std::vector<int>())); |
| 441 } | 527 } |
| 442 | 528 |
| 443 VLOG(1) << "ZygoteMain: initializing " << fork_delegates.size() | 529 VLOG(1) << "ZygoteMain: initializing " << fork_delegates.size() |
| 444 << " fork delegates"; | 530 << " fork delegates"; |
| 445 for (ScopedVector<ZygoteForkDelegate>::iterator i = fork_delegates.begin(); | 531 for (ScopedVector<ZygoteForkDelegate>::iterator i = fork_delegates.begin(); |
| 446 i != fork_delegates.end(); | 532 i != fork_delegates.end(); |
| 447 ++i) { | 533 ++i) { |
| 448 (*i)->Init(GetSandboxFD(), must_enable_setuid_sandbox); | 534 (*i)->Init(GetSandboxFD(), must_enable_setuid_sandbox); |
| 449 } | 535 } |
| 450 | 536 |
| 451 // Turn on the first layer of the sandbox if the configuration warrants it. | 537 // Turn on the first layer of the sandbox if the configuration warrants it. |
| 452 EnterLayerOneSandbox(linux_sandbox, must_enable_setuid_sandbox); | 538 EnterLayerOneSandbox(linux_sandbox, must_enable_setuid_sandbox, |
| 539 post_fork_parent_callback); | |
| 540 | |
| 541 std::vector<pid_t> extra_children; | |
| 542 std::vector<int> extra_fds; | |
| 543 | |
| 544 #if defined(ADDRESS_SANITIZER) | |
| 545 pid_t sancov_helper_pid = ForkSanitizerCoverageHelper( | |
| 546 sancov_socket_fds[0], sancov_socket_fds[1], sancov_file_fd.Pass()); | |
| 547 // It's important that the zygote reaps the helper before dying. Otherwise, | |
| 548 // the destruction of the PID namespace could kill the helper before it | |
| 549 // completes its I/O tasks. | |
|
jln (very slow on Chromium)
2014/05/27 23:15:00
Let's add more information about the lifetime of s
earthdok
2014/05/28 16:44:51
Done.
| |
| 550 extra_children.push_back(sancov_helper_pid); | |
| 551 // Sanitizer code in the renderers will inherit the write end of the socket | |
| 552 // from the zygote. We must keep it open until the very end of the zygote's | |
| 553 // lifetime, even though we don't explicitly use it. | |
| 554 extra_fds.push_back(sancov_socket_fds[1]); | |
| 555 #endif | |
| 453 | 556 |
| 454 int sandbox_flags = linux_sandbox->GetStatus(); | 557 int sandbox_flags = linux_sandbox->GetStatus(); |
| 455 bool setuid_sandbox_engaged = sandbox_flags & kSandboxLinuxSUID; | 558 bool setuid_sandbox_engaged = sandbox_flags & kSandboxLinuxSUID; |
| 456 CHECK_EQ(must_enable_setuid_sandbox, setuid_sandbox_engaged); | 559 CHECK_EQ(must_enable_setuid_sandbox, setuid_sandbox_engaged); |
| 457 | 560 |
| 458 Zygote zygote(sandbox_flags, fork_delegates.Pass()); | 561 Zygote zygote(sandbox_flags, fork_delegates.Pass(), extra_children, |
| 562 extra_fds); | |
| 459 // This function call can return multiple times, once per fork(). | 563 // This function call can return multiple times, once per fork(). |
| 460 return zygote.ProcessRequests(); | 564 return zygote.ProcessRequests(); |
| 461 } | 565 } |
| 462 | 566 |
| 463 } // namespace content | 567 } // namespace content |
| OLD | NEW |