OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/mach_broker_mac.h" | 5 #include "content/browser/mach_broker_mac.h" |
6 | 6 |
7 #include <bsm/libbsm.h> | 7 #include <bsm/libbsm.h> |
8 #include <servers/bootstrap.h> | 8 #include <servers/bootstrap.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/mac/foundation_util.h" | 14 #include "base/mac/foundation_util.h" |
15 #include "base/mac/mach_logging.h" | 15 #include "base/mac/mach_logging.h" |
16 #include "base/mac/scoped_mach_port.h" | |
17 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
19 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
20 #include "base/threading/platform_thread.h" | |
21 #include "content/browser/renderer_host/render_process_host_impl.h" | 19 #include "content/browser/renderer_host/render_process_host_impl.h" |
22 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/child_process_data.h" | 21 #include "content/public/browser/child_process_data.h" |
24 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
25 #include "content/public/browser/notification_types.h" | 23 #include "content/public/browser/notification_types.h" |
26 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
27 | 25 |
28 namespace content { | 26 namespace content { |
29 | 27 |
30 namespace { | 28 namespace { |
31 | 29 |
32 // Mach message structure used in the child as a sending message. | 30 // Mach message structure used in the child as a sending message. |
33 struct MachBroker_ChildSendMsg { | 31 struct MachBroker_ChildSendMsg { |
34 mach_msg_header_t header; | 32 mach_msg_header_t header; |
35 mach_msg_body_t body; | 33 mach_msg_body_t body; |
36 mach_msg_port_descriptor_t child_task_port; | 34 mach_msg_port_descriptor_t child_task_port; |
37 }; | 35 }; |
38 | 36 |
39 // Complement to the ChildSendMsg, this is used in the parent for receiving | 37 // Complement to the ChildSendMsg, this is used in the parent for receiving |
40 // a message. Contains a message trailer with audit information. | 38 // a message. Contains a message trailer with audit information. |
41 struct MachBroker_ParentRecvMsg : public MachBroker_ChildSendMsg { | 39 struct MachBroker_ParentRecvMsg : public MachBroker_ChildSendMsg { |
42 mach_msg_audit_trailer_t trailer; | 40 mach_msg_audit_trailer_t trailer; |
43 }; | 41 }; |
44 | 42 |
45 } // namespace | 43 } // namespace |
46 | 44 |
47 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { | |
48 public: | |
49 explicit MachListenerThreadDelegate(MachBroker* broker) | |
50 : broker_(broker), | |
51 server_port_(MACH_PORT_NULL) { | |
52 DCHECK(broker_); | |
53 } | |
54 | |
55 bool Init() { | |
56 DCHECK(server_port_.get() == MACH_PORT_NULL); | |
57 | |
58 mach_port_t port; | |
59 kern_return_t kr = mach_port_allocate(mach_task_self(), | |
60 MACH_PORT_RIGHT_RECEIVE, | |
61 &port); | |
62 if (kr != KERN_SUCCESS) { | |
63 MACH_LOG(ERROR, kr) << "mach_port_allocate"; | |
64 return false; | |
65 } | |
66 server_port_.reset(port); | |
67 | |
68 // Allocate a send right for the server port. | |
69 kr = mach_port_insert_right( | |
70 mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND); | |
71 if (kr != KERN_SUCCESS) { | |
72 MACH_LOG(ERROR, kr) << "mach_port_insert_right"; | |
73 return false; | |
74 } | |
75 // Deallocate the right after registering with the bootstrap server. | |
76 base::mac::ScopedMachSendRight send_right(port); | |
77 | |
78 // Register the port with the bootstrap server. Because bootstrap_register | |
79 // is deprecated, this has to be wraped in an ObjC interface. | |
80 NSPort* ns_port = [NSMachPort portWithMachPort:port | |
81 options:NSMachPortDeallocateNone]; | |
82 NSString* name = base::SysUTF8ToNSString(broker_->GetMachPortName()); | |
83 return [[NSMachBootstrapServer sharedInstance] registerPort:ns_port | |
84 name:name]; | |
85 } | |
86 | |
87 // Implement |PlatformThread::Delegate|. | |
88 void ThreadMain() override { | |
89 MachBroker_ParentRecvMsg msg; | |
90 bzero(&msg, sizeof(msg)); | |
91 msg.header.msgh_size = sizeof(msg); | |
92 msg.header.msgh_local_port = server_port_.get(); | |
93 | |
94 const mach_msg_option_t options = MACH_RCV_MSG | | |
95 MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) | | |
96 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); | |
97 | |
98 kern_return_t kr; | |
99 while ((kr = mach_msg(&msg.header, | |
100 options, | |
101 0, | |
102 sizeof(msg), | |
103 server_port_, | |
104 MACH_MSG_TIMEOUT_NONE, | |
105 MACH_PORT_NULL)) == KERN_SUCCESS) { | |
106 // Use the kernel audit information to make sure this message is from | |
107 // a task that this process spawned. The kernel audit token contains the | |
108 // unspoofable pid of the task that sent the message. | |
109 // | |
110 // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid(). | |
111 pid_t child_pid; | |
112 audit_token_to_au32(msg.trailer.msgh_audit, | |
113 NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL); | |
114 | |
115 mach_port_t child_task_port = msg.child_task_port.name; | |
116 | |
117 // Take the lock and update the broker information. | |
118 base::AutoLock lock(broker_->GetLock()); | |
119 broker_->FinalizePid(child_pid, child_task_port); | |
120 } | |
121 | |
122 MACH_LOG(ERROR, kr) << "mach_msg"; | |
123 } | |
124 | |
125 private: | |
126 // The MachBroker to use when new child task rights are received. Can be | |
127 // NULL. | |
128 MachBroker* broker_; // weak | |
129 | |
130 base::mac::ScopedMachReceiveRight server_port_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate); | |
133 }; | |
134 | |
135 bool MachBroker::ChildSendTaskPortToParent() { | 45 bool MachBroker::ChildSendTaskPortToParent() { |
136 // Look up the named MachBroker port that's been registered with the | 46 // Look up the named MachBroker port that's been registered with the |
137 // bootstrap server. | 47 // bootstrap server. |
138 mach_port_t parent_port; | 48 mach_port_t parent_port; |
139 kern_return_t kr = bootstrap_look_up(bootstrap_port, | 49 kern_return_t kr = bootstrap_look_up(bootstrap_port, |
140 const_cast<char*>(GetMachPortName().c_str()), &parent_port); | 50 const_cast<char*>(GetMachPortName().c_str()), &parent_port); |
141 if (kr != KERN_SUCCESS) { | 51 if (kr != KERN_SUCCESS) { |
142 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up"; | 52 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up"; |
143 return false; | 53 return false; |
144 } | 54 } |
(...skipping 16 matching lines...) Expand all Loading... |
161 0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL); | 71 0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL); |
162 if (kr != KERN_SUCCESS) { | 72 if (kr != KERN_SUCCESS) { |
163 MACH_LOG(ERROR, kr) << "mach_msg"; | 73 MACH_LOG(ERROR, kr) << "mach_msg"; |
164 return false; | 74 return false; |
165 } | 75 } |
166 | 76 |
167 return true; | 77 return true; |
168 } | 78 } |
169 | 79 |
170 MachBroker* MachBroker::GetInstance() { | 80 MachBroker* MachBroker::GetInstance() { |
171 return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); | 81 return Singleton<MachBroker, LeakySingletonTraits<MachBroker>>::get(); |
172 } | 82 } |
173 | 83 |
174 base::Lock& MachBroker::GetLock() { | 84 base::Lock& MachBroker::GetLock() { |
175 return lock_; | 85 return lock_; |
176 } | 86 } |
177 | 87 |
178 void MachBroker::EnsureRunning() { | 88 void MachBroker::EnsureRunning() { |
179 lock_.AssertAcquired(); | 89 lock_.AssertAcquired(); |
180 | 90 |
181 if (!listener_thread_started_) { | 91 if (initialized_) |
182 listener_thread_started_ = true; | 92 return; |
183 | 93 |
184 BrowserThread::PostTask( | 94 // Do not attempt to reinitialize in the event of failure. |
185 BrowserThread::UI, FROM_HERE, | 95 initialized_ = true; |
186 base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this))); | |
187 | 96 |
188 // Intentional leak. This thread is never joined or reaped. | 97 BrowserThread::PostTask( |
189 MachListenerThreadDelegate* thread = new MachListenerThreadDelegate(this); | 98 BrowserThread::UI, FROM_HERE, |
190 if (thread->Init()) { | 99 base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this))); |
191 base::PlatformThread::CreateNonJoinable(0, thread); | 100 |
192 } else { | 101 if (!Init()) { |
193 LOG(ERROR) << "Failed to initialize the MachListenerThreadDelegate"; | 102 LOG(ERROR) << "Failed to initialize the MachListenerThreadDelegate"; |
194 } | |
195 } | 103 } |
196 } | 104 } |
197 | 105 |
198 void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid, | 106 void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid, |
199 int child_process_id) { | 107 int child_process_id) { |
200 lock_.AssertAcquired(); | 108 lock_.AssertAcquired(); |
201 | 109 |
202 DCHECK_EQ(0u, mach_map_.count(pid)); | 110 DCHECK_EQ(0u, mach_map_.count(pid)); |
203 mach_map_[pid] = MACH_PORT_NULL; | 111 mach_map_[pid] = MACH_PORT_NULL; |
204 child_process_id_map_[child_process_id] = pid; | 112 child_process_id_map_[child_process_id] = pid; |
(...skipping 26 matching lines...) Expand all Loading... |
231 RenderProcessHost* host = Source<RenderProcessHost>(source).ptr(); | 139 RenderProcessHost* host = Source<RenderProcessHost>(source).ptr(); |
232 InvalidateChildProcessId(host->GetID()); | 140 InvalidateChildProcessId(host->GetID()); |
233 break; | 141 break; |
234 } | 142 } |
235 default: | 143 default: |
236 NOTREACHED() << "Unexpected notification"; | 144 NOTREACHED() << "Unexpected notification"; |
237 break; | 145 break; |
238 } | 146 } |
239 } | 147 } |
240 | 148 |
241 MachBroker::MachBroker() : listener_thread_started_(false) { | 149 MachBroker::MachBroker() : initialized_(false) { |
242 } | 150 } |
243 | 151 |
244 MachBroker::~MachBroker() {} | 152 MachBroker::~MachBroker() {} |
245 | 153 |
| 154 bool MachBroker::Init() { |
| 155 DCHECK(server_port_.get() == MACH_PORT_NULL); |
| 156 |
| 157 // Check in with launchd and publish the service name. |
| 158 mach_port_t port; |
| 159 kern_return_t kr = |
| 160 bootstrap_check_in(bootstrap_port, GetMachPortName().c_str(), &port); |
| 161 if (kr != KERN_SUCCESS) { |
| 162 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_check_in"; |
| 163 return false; |
| 164 } |
| 165 server_port_.reset(port); |
| 166 |
| 167 // Start the dispatch source. |
| 168 std::string queue_name = |
| 169 base::StringPrintf("%s.MachBroker", base::mac::BaseBundleID()); |
| 170 dispatch_source_.reset(new base::DispatchSourceMach( |
| 171 queue_name.c_str(), server_port_.get(), ^{ HandleRequest(); })); |
| 172 dispatch_source_->Resume(); |
| 173 |
| 174 return true; |
| 175 } |
| 176 |
| 177 void MachBroker::HandleRequest() { |
| 178 MachBroker_ParentRecvMsg msg; |
| 179 bzero(&msg, sizeof(msg)); |
| 180 msg.header.msgh_size = sizeof(msg); |
| 181 msg.header.msgh_local_port = server_port_.get(); |
| 182 |
| 183 const mach_msg_option_t options = MACH_RCV_MSG | |
| 184 MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) | |
| 185 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); |
| 186 |
| 187 kern_return_t kr = mach_msg(&msg.header, |
| 188 options, |
| 189 0, |
| 190 sizeof(msg), |
| 191 server_port_, |
| 192 MACH_MSG_TIMEOUT_NONE, |
| 193 MACH_PORT_NULL); |
| 194 if (kr != KERN_SUCCESS) { |
| 195 MACH_LOG(ERROR, kr) << "mach_msg"; |
| 196 return; |
| 197 } |
| 198 |
| 199 // Use the kernel audit information to make sure this message is from |
| 200 // a task that this process spawned. The kernel audit token contains the |
| 201 // unspoofable pid of the task that sent the message. |
| 202 // |
| 203 // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid(). |
| 204 pid_t child_pid; |
| 205 audit_token_to_au32(msg.trailer.msgh_audit, |
| 206 NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL); |
| 207 |
| 208 mach_port_t child_task_port = msg.child_task_port.name; |
| 209 |
| 210 // Take the lock and update the broker information. |
| 211 base::AutoLock lock(GetLock()); |
| 212 FinalizePid(child_pid, child_task_port); |
| 213 } |
| 214 |
246 void MachBroker::FinalizePid(base::ProcessHandle pid, | 215 void MachBroker::FinalizePid(base::ProcessHandle pid, |
247 mach_port_t task_port) { | 216 mach_port_t task_port) { |
248 lock_.AssertAcquired(); | 217 lock_.AssertAcquired(); |
249 | 218 |
250 MachMap::iterator it = mach_map_.find(pid); | 219 MachMap::iterator it = mach_map_.find(pid); |
251 if (it == mach_map_.end()) { | 220 if (it == mach_map_.end()) { |
252 // Do nothing for unknown pids. | 221 // Do nothing for unknown pids. |
253 LOG(ERROR) << "Unknown process " << pid << " is sending Mach IPC messages!"; | 222 LOG(ERROR) << "Unknown process " << pid << " is sending Mach IPC messages!"; |
254 return; | 223 return; |
255 } | 224 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 NotificationService::AllBrowserContextsAndSources()); | 261 NotificationService::AllBrowserContextsAndSources()); |
293 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 262 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
294 NotificationService::AllBrowserContextsAndSources()); | 263 NotificationService::AllBrowserContextsAndSources()); |
295 | 264 |
296 // No corresponding StopObservingBrowserChildProcesses, | 265 // No corresponding StopObservingBrowserChildProcesses, |
297 // we leak this singleton. | 266 // we leak this singleton. |
298 BrowserChildProcessObserver::Add(this); | 267 BrowserChildProcessObserver::Add(this); |
299 } | 268 } |
300 | 269 |
301 } // namespace content | 270 } // namespace content |
OLD | NEW |