OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/browser/bootstrap_sandbox_mac.h" | 5 #include "content/browser/bootstrap_sandbox_mac.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/mac/mac_util.h" | 8 #include "base/mac/mac_util.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "content/browser/mach_broker_mac.h" |
11 #include "content/common/sandbox_init_mac.h" | 12 #include "content/common/sandbox_init_mac.h" |
12 #include "content/public/browser/browser_child_process_observer.h" | 13 #include "content/public/browser/browser_child_process_observer.h" |
13 #include "content/public/browser/child_process_data.h" | 14 #include "content/public/browser/child_process_data.h" |
| 15 #include "content/public/browser/notification_details.h" |
| 16 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" |
| 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/notification_types.h" |
| 20 #include "content/public/browser/render_process_host.h" |
14 #include "content/public/common/sandbox_type_mac.h" | 21 #include "content/public/common/sandbox_type_mac.h" |
15 #include "sandbox/mac/bootstrap_sandbox.h" | 22 #include "sandbox/mac/bootstrap_sandbox.h" |
16 | 23 |
17 namespace content { | 24 namespace content { |
18 | 25 |
19 namespace { | 26 namespace { |
20 | 27 |
21 // This class is responsible for creating the BootstrapSandbox global | 28 // This class is responsible for creating the BootstrapSandbox global |
22 // singleton, as well as registering all associated policies with it. | 29 // singleton, as well as registering all associated policies with it. |
23 class BootstrapSandboxPolicy : public BrowserChildProcessObserver { | 30 class BootstrapSandboxPolicy : public BrowserChildProcessObserver, |
| 31 public NotificationObserver { |
24 public: | 32 public: |
25 static BootstrapSandboxPolicy* GetInstance(); | 33 static BootstrapSandboxPolicy* GetInstance(); |
26 | 34 |
27 sandbox::BootstrapSandbox* sandbox() const { | 35 sandbox::BootstrapSandbox* sandbox() const { |
28 return sandbox_.get(); | 36 return sandbox_.get(); |
29 } | 37 } |
30 | 38 |
31 // BrowserChildProcessObserver: | 39 // BrowserChildProcessObserver: |
32 virtual void BrowserChildProcessHostDisconnected( | 40 virtual void BrowserChildProcessHostDisconnected( |
33 const ChildProcessData& data) OVERRIDE; | 41 const ChildProcessData& data) OVERRIDE; |
34 virtual void BrowserChildProcessCrashed( | 42 virtual void BrowserChildProcessCrashed( |
35 const ChildProcessData& data) OVERRIDE; | 43 const ChildProcessData& data) OVERRIDE; |
36 | 44 |
| 45 // NotificationObserver: |
| 46 virtual void Observe(int type, |
| 47 const NotificationSource& source, |
| 48 const NotificationDetails& details) OVERRIDE; |
| 49 |
37 private: | 50 private: |
38 friend struct DefaultSingletonTraits<BootstrapSandboxPolicy>; | 51 friend struct DefaultSingletonTraits<BootstrapSandboxPolicy>; |
39 BootstrapSandboxPolicy(); | 52 BootstrapSandboxPolicy(); |
40 virtual ~BootstrapSandboxPolicy(); | 53 virtual ~BootstrapSandboxPolicy(); |
41 | 54 |
42 void RegisterSandboxPolicies(); | 55 void RegisterSandboxPolicies(); |
| 56 void RegisterRendererPolicy(); |
| 57 |
| 58 void AddBaselinePolicy(sandbox::BootstrapSandboxPolicy* policy); |
| 59 |
| 60 NotificationRegistrar notification_registrar_; |
43 | 61 |
44 scoped_ptr<sandbox::BootstrapSandbox> sandbox_; | 62 scoped_ptr<sandbox::BootstrapSandbox> sandbox_; |
45 }; | 63 }; |
46 | 64 |
47 BootstrapSandboxPolicy* BootstrapSandboxPolicy::GetInstance() { | 65 BootstrapSandboxPolicy* BootstrapSandboxPolicy::GetInstance() { |
48 return Singleton<BootstrapSandboxPolicy>::get(); | 66 return Singleton<BootstrapSandboxPolicy>::get(); |
49 } | 67 } |
50 | 68 |
51 void BootstrapSandboxPolicy::BrowserChildProcessHostDisconnected( | 69 void BootstrapSandboxPolicy::BrowserChildProcessHostDisconnected( |
52 const ChildProcessData& data) { | 70 const ChildProcessData& data) { |
53 sandbox()->ChildDied(data.handle); | 71 sandbox()->ChildDied(data.handle); |
54 } | 72 } |
55 | 73 |
56 void BootstrapSandboxPolicy::BrowserChildProcessCrashed( | 74 void BootstrapSandboxPolicy::BrowserChildProcessCrashed( |
57 const ChildProcessData& data) { | 75 const ChildProcessData& data) { |
58 sandbox()->ChildDied(data.handle); | 76 sandbox()->ChildDied(data.handle); |
59 } | 77 } |
60 | 78 |
| 79 void BootstrapSandboxPolicy::Observe(int type, |
| 80 const NotificationSource& source, |
| 81 const NotificationDetails& details) { |
| 82 switch (type) { |
| 83 case NOTIFICATION_RENDERER_PROCESS_CLOSED: |
| 84 sandbox()->ChildDied( |
| 85 Details<RenderProcessHost::RendererClosedDetails>(details)->handle); |
| 86 break; |
| 87 default: |
| 88 NOTREACHED() << "Unexpected notification " << type; |
| 89 break; |
| 90 } |
| 91 } |
| 92 |
61 BootstrapSandboxPolicy::BootstrapSandboxPolicy() | 93 BootstrapSandboxPolicy::BootstrapSandboxPolicy() |
62 : sandbox_(sandbox::BootstrapSandbox::Create()) { | 94 : sandbox_(sandbox::BootstrapSandbox::Create()) { |
63 CHECK(sandbox_.get()); | 95 CHECK(sandbox_.get()); |
64 BrowserChildProcessObserver::Add(this); | 96 BrowserChildProcessObserver::Add(this); |
| 97 notification_registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 98 NotificationService::AllBrowserContextsAndSources()); |
65 RegisterSandboxPolicies(); | 99 RegisterSandboxPolicies(); |
66 } | 100 } |
67 | 101 |
68 BootstrapSandboxPolicy::~BootstrapSandboxPolicy() { | 102 BootstrapSandboxPolicy::~BootstrapSandboxPolicy() { |
69 BrowserChildProcessObserver::Remove(this); | 103 BrowserChildProcessObserver::Remove(this); |
70 } | 104 } |
71 | 105 |
72 void BootstrapSandboxPolicy::RegisterSandboxPolicies() { | 106 void BootstrapSandboxPolicy::RegisterSandboxPolicies() { |
| 107 RegisterRendererPolicy(); |
| 108 } |
| 109 |
| 110 void BootstrapSandboxPolicy::RegisterRendererPolicy() { |
| 111 sandbox::BootstrapSandboxPolicy policy; |
| 112 AddBaselinePolicy(&policy); |
| 113 |
| 114 // Permit font queries. |
| 115 policy.rules["com.apple.FontServer"] = sandbox::Rule(sandbox::POLICY_ALLOW); |
| 116 policy.rules["com.apple.FontObjectsServer"] = |
| 117 sandbox::Rule(sandbox::POLICY_ALLOW); |
| 118 |
| 119 // Allow access to the windowserver. This is needed to get the colorspace |
| 120 // during sandbox warmup. Since NSColorSpace conforms to NSCoding, this |
| 121 // should be plumbed over IPC instead <http://crbug.com/265709>. |
| 122 policy.rules["com.apple.windowserver.active"] = |
| 123 sandbox::Rule(sandbox::POLICY_ALLOW); |
| 124 |
| 125 sandbox_->RegisterSandboxPolicy(SANDBOX_TYPE_RENDERER, policy); |
| 126 } |
| 127 |
| 128 void BootstrapSandboxPolicy::AddBaselinePolicy( |
| 129 sandbox::BootstrapSandboxPolicy* policy) { |
| 130 auto& rules = policy->rules; |
| 131 |
| 132 // Allow the child to send its task port to the MachBroker. |
| 133 rules[MachBroker::GetMachPortName()] = sandbox::Rule(sandbox::POLICY_ALLOW); |
| 134 |
| 135 // Allow logging to the syslog. |
| 136 rules["com.apple.system.logger"] = sandbox::Rule(sandbox::POLICY_ALLOW); |
73 } | 137 } |
74 | 138 |
75 } // namespace | 139 } // namespace |
76 | 140 |
77 bool ShouldEnableBootstrapSandbox() { | 141 bool ShouldEnableBootstrapSandbox() { |
78 return base::mac::IsOSMountainLionOrEarlier() || | 142 return base::mac::IsOSMavericksOrEarlier(); |
79 base::mac::IsOSMavericks(); | |
80 } | 143 } |
81 | 144 |
82 sandbox::BootstrapSandbox* GetBootstrapSandbox() { | 145 sandbox::BootstrapSandbox* GetBootstrapSandbox() { |
83 return BootstrapSandboxPolicy::GetInstance()->sandbox(); | 146 return BootstrapSandboxPolicy::GetInstance()->sandbox(); |
84 } | 147 } |
85 | 148 |
86 } // namespace content | 149 } // namespace content |
OLD | NEW |