• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "content/browser/utility_process_host_impl.h"
6 
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/synchronization/lock.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "content/browser/browser_child_process_host_impl.h"
18 #include "content/browser/renderer_host/render_process_host_impl.h"
19 #include "content/common/child_process_host_impl.h"
20 #include "content/common/utility_messages.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/content_browser_client.h"
23 #include "content/public/browser/utility_process_host_client.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/common/process_type.h"
26 #include "content/public/common/sandboxed_process_launcher_delegate.h"
27 #include "ipc/ipc_switches.h"
28 #include "ui/base/ui_base_switches.h"
29 
30 namespace content {
31 
32 // NOTE: changes to this class need to be reviewed by the security team.
33 class UtilitySandboxedProcessLauncherDelegate
34     : public SandboxedProcessLauncherDelegate {
35  public:
UtilitySandboxedProcessLauncherDelegate(const base::FilePath & exposed_dir,bool launch_elevated,bool no_sandbox,base::EnvironmentMap & env,ChildProcessHost * host)36   UtilitySandboxedProcessLauncherDelegate(const base::FilePath& exposed_dir,
37                                           bool launch_elevated, bool no_sandbox,
38                                           base::EnvironmentMap& env,
39                                           ChildProcessHost* host)
40       : exposed_dir_(exposed_dir),
41 #if defined(OS_WIN)
42         launch_elevated_(launch_elevated)
43 #elif defined(OS_POSIX)
44         env_(env),
45         no_sandbox_(no_sandbox),
46         ipc_fd_(host->TakeClientFileDescriptor())
47 #endif  // OS_WIN
48   {}
49 
~UtilitySandboxedProcessLauncherDelegate()50   virtual ~UtilitySandboxedProcessLauncherDelegate() {}
51 
52 #if defined(OS_WIN)
ShouldLaunchElevated()53   virtual bool ShouldLaunchElevated() OVERRIDE {
54     return launch_elevated_;
55   }
PreSandbox(bool * disable_default_policy,base::FilePath * exposed_dir)56   virtual void PreSandbox(bool* disable_default_policy,
57                           base::FilePath* exposed_dir) OVERRIDE {
58     *exposed_dir = exposed_dir_;
59   }
60 #elif defined(OS_POSIX)
61 
ShouldUseZygote()62   virtual bool ShouldUseZygote() OVERRIDE {
63     return !no_sandbox_ && exposed_dir_.empty();
64   }
GetEnvironment()65   virtual base::EnvironmentMap GetEnvironment() OVERRIDE {
66     return env_;
67   }
GetIpcFd()68   virtual int GetIpcFd() OVERRIDE {
69     return ipc_fd_;
70   }
71 #endif  // OS_WIN
72 
73  private:
74 
75  base::FilePath exposed_dir_;
76 
77 #if defined(OS_WIN)
78   bool launch_elevated_;
79 #elif defined(OS_POSIX)
80   base::EnvironmentMap env_;
81   bool no_sandbox_;
82   int ipc_fd_;
83 #endif  // OS_WIN
84 };
85 
86 UtilityMainThreadFactoryFunction g_utility_main_thread_factory = NULL;
87 
Create(UtilityProcessHostClient * client,base::SequencedTaskRunner * client_task_runner)88 UtilityProcessHost* UtilityProcessHost::Create(
89     UtilityProcessHostClient* client,
90     base::SequencedTaskRunner* client_task_runner) {
91   return new UtilityProcessHostImpl(client, client_task_runner);
92 }
93 
RegisterUtilityMainThreadFactory(UtilityMainThreadFactoryFunction create)94 void UtilityProcessHostImpl::RegisterUtilityMainThreadFactory(
95     UtilityMainThreadFactoryFunction create) {
96   g_utility_main_thread_factory = create;
97 }
98 
UtilityProcessHostImpl(UtilityProcessHostClient * client,base::SequencedTaskRunner * client_task_runner)99 UtilityProcessHostImpl::UtilityProcessHostImpl(
100     UtilityProcessHostClient* client,
101     base::SequencedTaskRunner* client_task_runner)
102     : client_(client),
103       client_task_runner_(client_task_runner),
104       is_batch_mode_(false),
105       is_mdns_enabled_(false),
106       no_sandbox_(false),
107       run_elevated_(false),
108 #if defined(OS_LINUX)
109       child_flags_(ChildProcessHost::CHILD_ALLOW_SELF),
110 #else
111       child_flags_(ChildProcessHost::CHILD_NORMAL),
112 #endif
113       started_(false) {
114 }
115 
~UtilityProcessHostImpl()116 UtilityProcessHostImpl::~UtilityProcessHostImpl() {
117   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
118   if (is_batch_mode_)
119     EndBatchMode();
120 }
121 
Send(IPC::Message * message)122 bool UtilityProcessHostImpl::Send(IPC::Message* message) {
123   if (!StartProcess())
124     return false;
125 
126   return process_->Send(message);
127 }
128 
StartBatchMode()129 bool UtilityProcessHostImpl::StartBatchMode()  {
130   CHECK(!is_batch_mode_);
131   is_batch_mode_ = StartProcess();
132   Send(new UtilityMsg_BatchMode_Started());
133   return is_batch_mode_;
134 }
135 
EndBatchMode()136 void UtilityProcessHostImpl::EndBatchMode()  {
137   CHECK(is_batch_mode_);
138   is_batch_mode_ = false;
139   Send(new UtilityMsg_BatchMode_Finished());
140 }
141 
SetExposedDir(const base::FilePath & dir)142 void UtilityProcessHostImpl::SetExposedDir(const base::FilePath& dir) {
143   exposed_dir_ = dir;
144 }
145 
EnableMDns()146 void UtilityProcessHostImpl::EnableMDns() {
147   is_mdns_enabled_ = true;
148 }
149 
DisableSandbox()150 void UtilityProcessHostImpl::DisableSandbox() {
151   no_sandbox_ = true;
152 }
153 
154 #if defined(OS_WIN)
ElevatePrivileges()155 void UtilityProcessHostImpl::ElevatePrivileges() {
156   no_sandbox_ = true;
157   run_elevated_ = true;
158 }
159 #endif
160 
GetData()161 const ChildProcessData& UtilityProcessHostImpl::GetData() {
162   return process_->GetData();
163 }
164 
165 #if defined(OS_POSIX)
166 
SetEnv(const base::EnvironmentMap & env)167 void UtilityProcessHostImpl::SetEnv(const base::EnvironmentMap& env) {
168   env_ = env;
169 }
170 
171 #endif  // OS_POSIX
172 
StartProcess()173 bool UtilityProcessHostImpl::StartProcess() {
174   if (started_)
175     return true;
176   started_ = true;
177 
178   if (is_batch_mode_)
179     return true;
180 
181   // Name must be set or metrics_service will crash in any test which
182   // launches a UtilityProcessHost.
183   process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this));
184   process_->SetName(base::ASCIIToUTF16("utility process"));
185 
186   std::string channel_id = process_->GetHost()->CreateChannel();
187   if (channel_id.empty())
188     return false;
189 
190   if (RenderProcessHost::run_renderer_in_process()) {
191     DCHECK(g_utility_main_thread_factory);
192     // See comment in RenderProcessHostImpl::Init() for the background on why we
193     // support single process mode this way.
194     in_process_thread_.reset(g_utility_main_thread_factory(channel_id));
195     in_process_thread_->Start();
196   } else {
197     const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
198     int child_flags = child_flags_;
199 
200 #if defined(OS_POSIX)
201     bool has_cmd_prefix = browser_command_line.HasSwitch(
202         switches::kUtilityCmdPrefix);
203 
204     // When running under gdb, forking /proc/self/exe ends up forking the gdb
205     // executable instead of Chromium. It is almost safe to assume that no
206     // updates will happen while a developer is running with
207     // |switches::kUtilityCmdPrefix|. See ChildProcessHost::GetChildPath() for
208     // a similar case with Valgrind.
209     if (has_cmd_prefix)
210       child_flags = ChildProcessHost::CHILD_NORMAL;
211 #endif
212 
213     base::FilePath exe_path = ChildProcessHost::GetChildPath(child_flags);
214     if (exe_path.empty()) {
215       NOTREACHED() << "Unable to get utility process binary name.";
216       return false;
217     }
218 
219     CommandLine* cmd_line = new CommandLine(exe_path);
220     cmd_line->AppendSwitchASCII(switches::kProcessType,
221                                 switches::kUtilityProcess);
222     cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
223     std::string locale = GetContentClient()->browser()->GetApplicationLocale();
224     cmd_line->AppendSwitchASCII(switches::kLang, locale);
225 
226     if (no_sandbox_ || browser_command_line.HasSwitch(switches::kNoSandbox))
227       cmd_line->AppendSwitch(switches::kNoSandbox);
228 #if defined(OS_MACOSX)
229     if (browser_command_line.HasSwitch(switches::kEnableSandboxLogging))
230       cmd_line->AppendSwitch(switches::kEnableSandboxLogging);
231 #endif
232     if (browser_command_line.HasSwitch(switches::kDebugPluginLoading))
233       cmd_line->AppendSwitch(switches::kDebugPluginLoading);
234 
235 #if defined(OS_POSIX)
236     if (has_cmd_prefix) {
237       // Launch the utility child process with some prefix
238       // (usually "xterm -e gdb --args").
239       cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative(
240           switches::kUtilityCmdPrefix));
241     }
242 
243     if (!exposed_dir_.empty()) {
244       cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir,
245                                  exposed_dir_);
246     }
247 #endif
248 
249     if (is_mdns_enabled_)
250       cmd_line->AppendSwitch(switches::kUtilityProcessEnableMDns);
251 
252 #if defined(OS_WIN)
253     // Let the utility process know if it is intended to be elevated.
254     if (run_elevated_)
255       cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated);
256 #endif
257 
258     process_->Launch(
259         new UtilitySandboxedProcessLauncherDelegate(exposed_dir_,
260                                                     run_elevated_,
261                                                     no_sandbox_, env_,
262                                                     process_->GetHost()),
263         cmd_line);
264   }
265 
266   return true;
267 }
268 
OnMessageReceived(const IPC::Message & message)269 bool UtilityProcessHostImpl::OnMessageReceived(const IPC::Message& message) {
270   client_task_runner_->PostTask(
271       FROM_HERE,
272       base::Bind(base::IgnoreResult(
273           &UtilityProcessHostClient::OnMessageReceived), client_.get(),
274           message));
275   return true;
276 }
277 
OnProcessLaunchFailed()278 void UtilityProcessHostImpl::OnProcessLaunchFailed() {
279   client_task_runner_->PostTask(
280       FROM_HERE,
281       base::Bind(&UtilityProcessHostClient::OnProcessLaunchFailed,
282                  client_.get()));
283 }
284 
OnProcessCrashed(int exit_code)285 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) {
286   client_task_runner_->PostTask(
287       FROM_HERE,
288       base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(),
289             exit_code));
290 }
291 
292 }  // namespace content
293