nacl_broker_listener.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.
  4. #include "components/nacl/broker/nacl_broker_listener.h"
  5. #include <utility>
  6. #include "base/base_switches.h"
  7. #include "base/bind.h"
  8. #include "base/command_line.h"
  9. #include "base/path_service.h"
  10. #include "base/process/launch.h"
  11. #include "base/process/process.h"
  12. #include "base/process/process_handle.h"
  13. #include "base/rand_util.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "base/threading/thread_task_runner_handle.h"
  16. #include "base/win/win_util.h"
  17. #include "components/nacl/common/nacl_cmd_line.h"
  18. #include "components/nacl/common/nacl_debug_exception_handler_win.h"
  19. #include "components/nacl/common/nacl_messages.h"
  20. #include "components/nacl/common/nacl_service.h"
  21. #include "components/nacl/common/nacl_switches.h"
  22. #include "content/public/common/content_switches.h"
  23. #include "content/public/common/sandbox_init_win.h"
  24. #include "ipc/ipc_channel.h"
  25. #include "mojo/public/cpp/platform/platform_channel.h"
  26. #include "mojo/public/cpp/system/invitation.h"
  27. #include "mojo/public/cpp/system/message_pipe.h"
  28. #include "sandbox/policy/mojom/sandbox.mojom.h"
  29. #include "sandbox/policy/win/sandbox_win.h"
  30. #include "sandbox/win/src/sandbox_policy.h"
  31. #include <windows.h>
  32. namespace {
  33. void SendReply(IPC::Channel* channel, int32_t pid, bool result) {
  34. channel->Send(new NaClProcessMsg_DebugExceptionHandlerLaunched(pid, result));
  35. }
  36. } // namespace
  37. NaClBrokerListener::NaClBrokerListener() = default;
  38. NaClBrokerListener::~NaClBrokerListener() = default;
  39. void NaClBrokerListener::Listen() {
  40. NaClService service(base::ThreadTaskRunnerHandle::Get());
  41. channel_ =
  42. IPC::Channel::CreateClient(service.TakeChannelPipe().release(), this,
  43. base::ThreadTaskRunnerHandle::Get());
  44. CHECK(channel_->Connect());
  45. run_loop_.Run();
  46. }
  47. sandbox::mojom::Sandbox NaClBrokerListener::GetSandboxType() {
  48. return sandbox::mojom::Sandbox::kPpapi;
  49. }
  50. std::string NaClBrokerListener::GetSandboxTag() {
  51. return sandbox::policy::SandboxWin::GetSandboxTagForDelegate(
  52. "nacl-broker-listener", GetSandboxType());
  53. }
  54. void NaClBrokerListener::OnChannelConnected(int32_t peer_pid) {
  55. browser_process_ = base::Process::OpenWithExtraPrivileges(peer_pid);
  56. CHECK(browser_process_.IsValid());
  57. }
  58. bool NaClBrokerListener::OnMessageReceived(const IPC::Message& msg) {
  59. bool handled = true;
  60. IPC_BEGIN_MESSAGE_MAP(NaClBrokerListener, msg)
  61. IPC_MESSAGE_HANDLER(NaClProcessMsg_LaunchLoaderThroughBroker,
  62. OnLaunchLoaderThroughBroker)
  63. IPC_MESSAGE_HANDLER(NaClProcessMsg_LaunchDebugExceptionHandler,
  64. OnLaunchDebugExceptionHandler)
  65. IPC_MESSAGE_HANDLER(NaClProcessMsg_StopBroker, OnStopBroker)
  66. IPC_MESSAGE_UNHANDLED(handled = false)
  67. IPC_END_MESSAGE_MAP()
  68. return handled;
  69. }
  70. void NaClBrokerListener::OnChannelError() {
  71. // The browser died unexpectedly, quit to avoid a zombie process.
  72. run_loop_.QuitWhenIdle();
  73. }
  74. void NaClBrokerListener::OnLaunchLoaderThroughBroker(
  75. int launch_id,
  76. mojo::MessagePipeHandle ipc_channel_handle) {
  77. base::ProcessHandle loader_handle_in_browser = 0;
  78. // Create the path to the nacl broker/loader executable - it's the executable
  79. // this code is running in.
  80. base::FilePath exe_path;
  81. base::PathService::Get(base::FILE_EXE, &exe_path);
  82. if (!exe_path.empty()) {
  83. base::CommandLine* cmd_line = new base::CommandLine(exe_path);
  84. nacl::CopyNaClCommandLineArguments(cmd_line);
  85. cmd_line->AppendSwitchASCII(switches::kProcessType,
  86. switches::kNaClLoaderProcess);
  87. // Mojo IPC setup.
  88. mojo::PlatformChannel channel;
  89. base::HandlesToInheritVector handles;
  90. channel.PrepareToPassRemoteEndpoint(&handles, cmd_line);
  91. mojo::OutgoingInvitation invitation;
  92. MojoResult fuse_result = mojo::FuseMessagePipes(
  93. mojo::ScopedMessagePipeHandle(ipc_channel_handle),
  94. invitation.AttachMessagePipe(0));
  95. DCHECK_EQ(MOJO_RESULT_OK, fuse_result);
  96. base::Process loader_process;
  97. sandbox::ResultCode result = content::StartSandboxedProcess(
  98. this, *cmd_line, handles, &loader_process);
  99. if (result == sandbox::SBOX_ALL_OK) {
  100. mojo::OutgoingInvitation::Send(std::move(invitation),
  101. loader_process.Handle(),
  102. channel.TakeLocalEndpoint());
  103. // Note: PROCESS_DUP_HANDLE is necessary here, because:
  104. // 1) The current process is the broker, which is the loader's parent.
  105. // 2) The browser is not the loader's parent, and so only gets the
  106. // access rights we confer here.
  107. // 3) The browser calls DuplicateHandle to set up communications with
  108. // the loader.
  109. // 4) The target process handle to DuplicateHandle needs to have
  110. // PROCESS_DUP_HANDLE access rights.
  111. DuplicateHandle(
  112. ::GetCurrentProcess(), loader_process.Handle(),
  113. browser_process_.Handle(), &loader_handle_in_browser,
  114. PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE,
  115. FALSE, 0);
  116. }
  117. }
  118. channel_->Send(
  119. new NaClProcessMsg_LoaderLaunched(launch_id, loader_handle_in_browser));
  120. }
  121. void NaClBrokerListener::OnLaunchDebugExceptionHandler(
  122. int32_t pid,
  123. base::ProcessHandle process_handle,
  124. const std::string& startup_info) {
  125. NaClStartDebugExceptionHandlerThread(
  126. base::Process(process_handle), startup_info,
  127. base::ThreadTaskRunnerHandle::Get(),
  128. base::BindRepeating(SendReply, channel_.get(), pid));
  129. }
  130. void NaClBrokerListener::OnStopBroker() {
  131. run_loop_.QuitWhenIdle();
  132. }