Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: content/zygote/zygote_main_linux.cc

Issue 897723005: Allow using the namespace sandbox in zygote host. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/public/common/content_switches.cc ('k') | sandbox/linux/services/namespace_sandbox.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <fcntl.h>
9 #include <pthread.h> 9 #include <pthread.h>
10 #include <signal.h> 10 #include <signal.h>
(...skipping 21 matching lines...) Expand all
32 #include "content/common/child_process_sandbox_support_impl_linux.h" 32 #include "content/common/child_process_sandbox_support_impl_linux.h"
33 #include "content/common/font_config_ipc_linux.h" 33 #include "content/common/font_config_ipc_linux.h"
34 #include "content/common/sandbox_linux/sandbox_linux.h" 34 #include "content/common/sandbox_linux/sandbox_linux.h"
35 #include "content/common/zygote_commands_linux.h" 35 #include "content/common/zygote_commands_linux.h"
36 #include "content/public/common/content_switches.h" 36 #include "content/public/common/content_switches.h"
37 #include "content/public/common/main_function_params.h" 37 #include "content/public/common/main_function_params.h"
38 #include "content/public/common/sandbox_linux.h" 38 #include "content/public/common/sandbox_linux.h"
39 #include "content/public/common/zygote_fork_delegate_linux.h" 39 #include "content/public/common/zygote_fork_delegate_linux.h"
40 #include "content/zygote/zygote_linux.h" 40 #include "content/zygote/zygote_linux.h"
41 #include "crypto/nss_util.h" 41 #include "crypto/nss_util.h"
42 #include "sandbox/linux/services/credentials.h"
42 #include "sandbox/linux/services/init_process_reaper.h" 43 #include "sandbox/linux/services/init_process_reaper.h"
43 #include "sandbox/linux/services/libc_urandom_override.h" 44 #include "sandbox/linux/services/libc_urandom_override.h"
45 #include "sandbox/linux/services/namespace_sandbox.h"
44 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" 46 #include "sandbox/linux/suid/client/setuid_sandbox_client.h"
45 #include "third_party/icu/source/i18n/unicode/timezone.h" 47 #include "third_party/icu/source/i18n/unicode/timezone.h"
46 #include "third_party/skia/include/ports/SkFontConfigInterface.h" 48 #include "third_party/skia/include/ports/SkFontConfigInterface.h"
47 49
48 #if defined(OS_LINUX) 50 #if defined(OS_LINUX)
49 #include <sys/prctl.h> 51 #include <sys/prctl.h>
50 #endif 52 #endif
51 53
52 #if defined(USE_OPENSSL) 54 #if defined(USE_OPENSSL)
53 #include <openssl/rand.h> 55 #include <openssl/rand.h>
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 // newly created process. 394 // newly created process.
393 const bool init_created = 395 const bool init_created =
394 sandbox::CreateInitProcessReaper(post_fork_parent_callback); 396 sandbox::CreateInitProcessReaper(post_fork_parent_callback);
395 if (!init_created) { 397 if (!init_created) {
396 LOG(ERROR) << "Error creating an init process to reap zombies"; 398 LOG(ERROR) << "Error creating an init process to reap zombies";
397 return false; 399 return false;
398 } 400 }
399 return true; 401 return true;
400 } 402 }
401 403
404 static bool MaybeSetProcessNonDumpable() {
405 const base::CommandLine& command_line =
406 *base::CommandLine::ForCurrentProcess();
407 if (command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
408 // If sandbox debugging is allowed, install a handler for sandbox-related
409 // crash testing.
410 InstallSandboxCrashTestHandler();
411 return true;
412 }
413
414 if (prctl(PR_SET_DUMPABLE, 0) != 0) {
415 PLOG(ERROR) << "Failed to set non-dumpable flag";
416 return false;
417 }
418
419 return prctl(PR_GET_DUMPABLE) == 0;
420 }
421
402 // Enter the setuid sandbox. This requires the current process to have been 422 // Enter the setuid sandbox. This requires the current process to have been
403 // created through the setuid sandbox. 423 // created through the setuid sandbox.
404 static bool EnterSuidSandbox(sandbox::SetuidSandboxClient* setuid_sandbox, 424 static bool EnterSuidSandbox(sandbox::SetuidSandboxClient* setuid_sandbox,
405 base::Closure* post_fork_parent_callback) { 425 base::Closure* post_fork_parent_callback) {
406 DCHECK(setuid_sandbox); 426 DCHECK(setuid_sandbox);
407 DCHECK(setuid_sandbox->IsSuidSandboxChild()); 427 DCHECK(setuid_sandbox->IsSuidSandboxChild());
408 428
409 // Use the SUID sandbox. This still allows the seccomp sandbox to 429 // Use the SUID sandbox. This still allows the seccomp sandbox to
410 // be enabled by the process later. 430 // be enabled by the process later.
411 431
(...skipping 14 matching lines...) Expand all
426 "is not the init process. Please, make sure the SUID " 446 "is not the init process. Please, make sure the SUID "
427 "binary is up to date."; 447 "binary is up to date.";
428 } 448 }
429 449
430 if (getpid() == 1) { 450 if (getpid() == 1) {
431 // The setuid sandbox has created a new PID namespace and we need 451 // The setuid sandbox has created a new PID namespace and we need
432 // to assume the role of init. 452 // to assume the role of init.
433 CHECK(CreateInitProcessReaper(post_fork_parent_callback)); 453 CHECK(CreateInitProcessReaper(post_fork_parent_callback));
434 } 454 }
435 455
436 #if !defined(OS_OPENBSD) 456 CHECK(MaybeSetProcessNonDumpable());
437 // Previously, we required that the binary be non-readable. This causes the
438 // kernel to mark the process as non-dumpable at startup. The thinking was
439 // that, although we were putting the renderers into a PID namespace (with
440 // the SUID sandbox), they would nonetheless be in the /same/ PID
441 // namespace. So they could ptrace each other unless they were non-dumpable.
442 //
443 // If the binary was readable, then there would be a window between process
444 // startup and the point where we set the non-dumpable flag in which a
445 // compromised renderer could ptrace attach.
446 //
447 // However, now that we have a zygote model, only the (trusted) zygote
448 // exists at this point and we can set the non-dumpable flag which is
449 // inherited by all our renderer children.
450 //
451 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
452 // issues, one can specify --allow-sandbox-debugging to let the process be
453 // dumpable.
454 const base::CommandLine& command_line =
455 *base::CommandLine::ForCurrentProcess();
456 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
457 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
458 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
459 LOG(ERROR) << "Failed to set non-dumpable flag";
460 return false;
461 }
462 } else {
463 // If sandbox debugging is allowed, install a handler for sandbox-related
464 // crash testing.
465 InstallSandboxCrashTestHandler();
466 }
467
468 #endif
469
470 return true; 457 return true;
471 } 458 }
472 459
460 static void EnterNamespaceSandbox(base::Closure* post_fork_parent_callback) {
461 pid_t pid = getpid();
462 if (sandbox::NamespaceSandbox::InNewPidNamespace()) {
463 CHECK_EQ(1, pid);
464 }
465
466 CHECK(sandbox::Credentials::MoveToNewUserNS());
467 CHECK(sandbox::Credentials::DropFileSystemAccess());
468 CHECK(sandbox::Credentials::DropAllCapabilities());
469
470 // This needs to happen after moving to a new user NS, since doing so involves
471 // writing the UID/GID map.
472 CHECK(MaybeSetProcessNonDumpable());
473
474 if (pid == 1) {
475 CHECK(CreateInitProcessReaper(post_fork_parent_callback));
476 }
477 }
478
473 #if defined(ADDRESS_SANITIZER) 479 #if defined(ADDRESS_SANITIZER)
474 const size_t kSanitizerMaxMessageLength = 1 * 1024 * 1024; 480 const size_t kSanitizerMaxMessageLength = 1 * 1024 * 1024;
475 481
476 // A helper process which collects code coverage data from the renderers over a 482 // A helper process which collects code coverage data from the renderers over a
477 // socket and dumps it to a file. See http://crbug.com/336212 for discussion. 483 // socket and dumps it to a file. See http://crbug.com/336212 for discussion.
478 static void SanitizerCoverageHelper(int socket_fd, int file_fd) { 484 static void SanitizerCoverageHelper(int socket_fd, int file_fd) {
479 scoped_ptr<char[]> buffer(new char[kSanitizerMaxMessageLength]); 485 scoped_ptr<char[]> buffer(new char[kSanitizerMaxMessageLength]);
480 while (true) { 486 while (true) {
481 ssize_t received_size = HANDLE_EINTR( 487 ssize_t received_size = HANDLE_EINTR(
482 recv(socket_fd, buffer.get(), kSanitizerMaxMessageLength, 0)); 488 recv(socket_fd, buffer.get(), kSanitizerMaxMessageLength, 0));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 _exit(0); 525 _exit(0);
520 } else { 526 } else {
521 // In the parent. 527 // In the parent.
522 PCHECK(0 == IGNORE_EINTR(close(child_fd))); 528 PCHECK(0 == IGNORE_EINTR(close(child_fd)));
523 return pid; 529 return pid;
524 } 530 }
525 } 531 }
526 532
527 #endif // defined(ADDRESS_SANITIZER) 533 #endif // defined(ADDRESS_SANITIZER)
528 534
529 // If |is_suid_sandbox_child|, then make sure that the setuid sandbox is
530 // engaged.
531 static void EnterLayerOneSandbox(LinuxSandbox* linux_sandbox, 535 static void EnterLayerOneSandbox(LinuxSandbox* linux_sandbox,
532 bool is_suid_sandbox_child, 536 const bool using_layer1_sandbox,
533 base::Closure* post_fork_parent_callback) { 537 base::Closure* post_fork_parent_callback) {
534 DCHECK(linux_sandbox); 538 DCHECK(linux_sandbox);
535 539
536 ZygotePreSandboxInit(); 540 ZygotePreSandboxInit();
537 541
538 // Check that the pre-sandbox initialization didn't spawn threads. 542 // Check that the pre-sandbox initialization didn't spawn threads.
539 #if !defined(THREAD_SANITIZER) 543 #if !defined(THREAD_SANITIZER)
540 DCHECK(linux_sandbox->IsSingleThreaded()); 544 DCHECK(linux_sandbox->IsSingleThreaded());
541 #endif 545 #endif
542 546
543 sandbox::SetuidSandboxClient* setuid_sandbox = 547 sandbox::SetuidSandboxClient* setuid_sandbox =
544 linux_sandbox->setuid_sandbox_client(); 548 linux_sandbox->setuid_sandbox_client();
545 549 if (setuid_sandbox->IsSuidSandboxChild()) {
546 if (is_suid_sandbox_child) {
547 CHECK(EnterSuidSandbox(setuid_sandbox, post_fork_parent_callback)) 550 CHECK(EnterSuidSandbox(setuid_sandbox, post_fork_parent_callback))
548 << "Failed to enter setuid sandbox"; 551 << "Failed to enter setuid sandbox";
552 } else if (sandbox::NamespaceSandbox::InNewUserNamespace()) {
553 EnterNamespaceSandbox(post_fork_parent_callback);
554 } else {
555 CHECK(!using_layer1_sandbox);
549 } 556 }
550 } 557 }
551 558
552 bool ZygoteMain(const MainFunctionParams& params, 559 bool ZygoteMain(const MainFunctionParams& params,
553 ScopedVector<ZygoteForkDelegate> fork_delegates) { 560 ScopedVector<ZygoteForkDelegate> fork_delegates) {
554 g_am_zygote_or_renderer = true; 561 g_am_zygote_or_renderer = true;
555 sandbox::InitLibcUrandomOverrides(); 562 sandbox::InitLibcUrandomOverrides();
556 563
557 std::vector<int> fds_to_close_post_fork; 564 std::vector<int> fds_to_close_post_fork;
558 565
(...skipping 17 matching lines...) Expand all
576 fds_to_close_post_fork.push_back(sancov_socket_fds[1]); 583 fds_to_close_post_fork.push_back(sancov_socket_fds[1]);
577 #endif 584 #endif
578 585
579 // Skip pre-initializing sandbox under --no-sandbox for crbug.com/444900. 586 // Skip pre-initializing sandbox under --no-sandbox for crbug.com/444900.
580 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 587 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
581 switches::kNoSandbox)) { 588 switches::kNoSandbox)) {
582 // This will pre-initialize the various sandboxes that need it. 589 // This will pre-initialize the various sandboxes that need it.
583 linux_sandbox->PreinitializeSandbox(); 590 linux_sandbox->PreinitializeSandbox();
584 } 591 }
585 592
586 const bool must_enable_setuid_sandbox = 593 const bool using_setuid_sandbox =
587 linux_sandbox->setuid_sandbox_client()->IsSuidSandboxChild(); 594 linux_sandbox->setuid_sandbox_client()->IsSuidSandboxChild();
588 if (must_enable_setuid_sandbox) { 595 const bool using_namespace_sandbox =
596 sandbox::NamespaceSandbox::InNewUserNamespace();
597
598 if (using_setuid_sandbox) {
589 linux_sandbox->setuid_sandbox_client()->CloseDummyFile(); 599 linux_sandbox->setuid_sandbox_client()->CloseDummyFile();
590 600
591 // Let the ZygoteHost know we're booting up. 601 // Let the ZygoteHost know we're booting up.
592 CHECK(UnixDomainSocket::SendMsg(kZygoteSocketPairFd, 602 CHECK(UnixDomainSocket::SendMsg(kZygoteSocketPairFd,
593 kZygoteBootMessage, 603 kZygoteBootMessage,
594 sizeof(kZygoteBootMessage), 604 sizeof(kZygoteBootMessage),
595 std::vector<int>())); 605 std::vector<int>()));
596 } 606 }
597 607
598 VLOG(1) << "ZygoteMain: initializing " << fork_delegates.size() 608 VLOG(1) << "ZygoteMain: initializing " << fork_delegates.size()
599 << " fork delegates"; 609 << " fork delegates";
600 for (ScopedVector<ZygoteForkDelegate>::iterator i = fork_delegates.begin(); 610 const bool using_layer1_sandbox =
601 i != fork_delegates.end(); 611 using_setuid_sandbox || using_namespace_sandbox;
602 ++i) { 612 for (ZygoteForkDelegate* fork_delegate : fork_delegates) {
603 (*i)->Init(GetSandboxFD(), must_enable_setuid_sandbox); 613 fork_delegate->Init(GetSandboxFD(), using_layer1_sandbox);
604 } 614 }
605 615
606 const std::vector<int> sandbox_fds_to_close_post_fork = 616 const std::vector<int> sandbox_fds_to_close_post_fork =
607 linux_sandbox->GetFileDescriptorsToClose(); 617 linux_sandbox->GetFileDescriptorsToClose();
608 618
609 fds_to_close_post_fork.insert(fds_to_close_post_fork.end(), 619 fds_to_close_post_fork.insert(fds_to_close_post_fork.end(),
610 sandbox_fds_to_close_post_fork.begin(), 620 sandbox_fds_to_close_post_fork.begin(),
611 sandbox_fds_to_close_post_fork.end()); 621 sandbox_fds_to_close_post_fork.end());
612 base::Closure post_fork_parent_callback = 622 base::Closure post_fork_parent_callback =
613 base::Bind(&CloseFds, fds_to_close_post_fork); 623 base::Bind(&CloseFds, fds_to_close_post_fork);
614 624
615 // Turn on the first layer of the sandbox if the configuration warrants it. 625 // Turn on the first layer of the sandbox if the configuration warrants it.
616 EnterLayerOneSandbox(linux_sandbox, must_enable_setuid_sandbox, 626 EnterLayerOneSandbox(linux_sandbox, using_layer1_sandbox,
617 &post_fork_parent_callback); 627 &post_fork_parent_callback);
618 628
619 // Extra children and file descriptors created that the Zygote must have 629 // Extra children and file descriptors created that the Zygote must have
620 // knowledge of. 630 // knowledge of.
621 std::vector<pid_t> extra_children; 631 std::vector<pid_t> extra_children;
622 std::vector<int> extra_fds; 632 std::vector<int> extra_fds;
623 633
624 #if defined(ADDRESS_SANITIZER) 634 #if defined(ADDRESS_SANITIZER)
625 pid_t sancov_helper_pid = ForkSanitizerCoverageHelper( 635 pid_t sancov_helper_pid = ForkSanitizerCoverageHelper(
626 sancov_socket_fds[0], sancov_socket_fds[1], sancov_file_fd.Pass(), 636 sancov_socket_fds[0], sancov_socket_fds[1], sancov_file_fd.Pass(),
627 sandbox_fds_to_close_post_fork); 637 sandbox_fds_to_close_post_fork);
628 // It's important that the zygote reaps the helper before dying. Otherwise, 638 // It's important that the zygote reaps the helper before dying. Otherwise,
629 // the destruction of the PID namespace could kill the helper before it 639 // the destruction of the PID namespace could kill the helper before it
630 // completes its I/O tasks. |sancov_helper_pid| will exit once the last 640 // completes its I/O tasks. |sancov_helper_pid| will exit once the last
631 // renderer holding the write end of |sancov_socket_fds| closes it. 641 // renderer holding the write end of |sancov_socket_fds| closes it.
632 extra_children.push_back(sancov_helper_pid); 642 extra_children.push_back(sancov_helper_pid);
633 // Sanitizer code in the renderers will inherit the write end of the socket 643 // Sanitizer code in the renderers will inherit the write end of the socket
634 // from the zygote. We must keep it open until the very end of the zygote's 644 // from the zygote. We must keep it open until the very end of the zygote's
635 // lifetime, even though we don't explicitly use it. 645 // lifetime, even though we don't explicitly use it.
636 extra_fds.push_back(sancov_socket_fds[1]); 646 extra_fds.push_back(sancov_socket_fds[1]);
637 #endif 647 #endif
638 648
639 int sandbox_flags = linux_sandbox->GetStatus(); 649 int sandbox_flags = linux_sandbox->GetStatus();
640 bool setuid_sandbox_engaged = sandbox_flags & kSandboxLinuxSUID; 650 bool setuid_sandbox_engaged = sandbox_flags & kSandboxLinuxSUID;
641 CHECK_EQ(must_enable_setuid_sandbox, setuid_sandbox_engaged); 651 CHECK_EQ(using_setuid_sandbox, setuid_sandbox_engaged);
642 652
643 Zygote zygote(sandbox_flags, fork_delegates.Pass(), extra_children, 653 Zygote zygote(sandbox_flags, fork_delegates.Pass(), extra_children,
644 extra_fds); 654 extra_fds);
645 // This function call can return multiple times, once per fork(). 655 // This function call can return multiple times, once per fork().
646 return zygote.ProcessRequests(); 656 return zygote.ProcessRequests();
647 } 657 }
648 658
649 } // namespace content 659 } // namespace content
OLDNEW
« no previous file with comments | « content/public/common/content_switches.cc ('k') | sandbox/linux/services/namespace_sandbox.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698