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/scoped_mach_port.h" | 16 #include "base/mac/scoped_mach_port.h" |
16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
19 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
20 #include "content/browser/renderer_host/render_process_host_impl.h" | 21 #include "content/browser/renderer_host/render_process_host_impl.h" |
21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/child_process_data.h" | 23 #include "content/public/browser/child_process_data.h" |
23 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
24 #include "content/public/browser/notification_types.h" | 25 #include "content/public/browser/notification_types.h" |
25 #include "content/public/common/content_switches.h" | 26 #include "content/public/common/content_switches.h" |
26 | 27 |
27 namespace content { | 28 namespace content { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 // Prints a string representation of a Mach error code. | |
32 std::string MachErrorCode(kern_return_t err) { | |
33 return base::StringPrintf("0x%x %s", err, mach_error_string(err)); | |
34 } | |
35 | |
36 // Mach message structure used in the child as a sending message. | 32 // Mach message structure used in the child as a sending message. |
37 struct MachBroker_ChildSendMsg { | 33 struct MachBroker_ChildSendMsg { |
38 mach_msg_header_t header; | 34 mach_msg_header_t header; |
39 mach_msg_body_t body; | 35 mach_msg_body_t body; |
40 mach_msg_port_descriptor_t child_task_port; | 36 mach_msg_port_descriptor_t child_task_port; |
41 }; | 37 }; |
42 | 38 |
43 // Complement to the ChildSendMsg, this is used in the parent for receiving | 39 // Complement to the ChildSendMsg, this is used in the parent for receiving |
44 // a message. Contains a message trailer with audit information. | 40 // a message. Contains a message trailer with audit information. |
45 struct MachBroker_ParentRecvMsg : public MachBroker_ChildSendMsg { | 41 struct MachBroker_ParentRecvMsg : public MachBroker_ChildSendMsg { |
(...skipping 11 matching lines...) Expand all Loading... |
57 } | 53 } |
58 | 54 |
59 bool Init() { | 55 bool Init() { |
60 DCHECK(server_port_ == MACH_PORT_NULL); | 56 DCHECK(server_port_ == MACH_PORT_NULL); |
61 | 57 |
62 mach_port_t port; | 58 mach_port_t port; |
63 kern_return_t kr = mach_port_allocate(mach_task_self(), | 59 kern_return_t kr = mach_port_allocate(mach_task_self(), |
64 MACH_PORT_RIGHT_RECEIVE, | 60 MACH_PORT_RIGHT_RECEIVE, |
65 &port); | 61 &port); |
66 if (kr != KERN_SUCCESS) { | 62 if (kr != KERN_SUCCESS) { |
67 LOG(ERROR) << "Failed to allocate MachBroker server port: " | 63 MACH_LOG(ERROR, kr) << "mach_port_allocate"; |
68 << MachErrorCode(kr); | |
69 return false; | 64 return false; |
70 } | 65 } |
71 | 66 |
72 // Allocate a send right for the server port. | 67 // Allocate a send right for the server port. |
73 kr = mach_port_insert_right( | 68 kr = mach_port_insert_right( |
74 mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND); | 69 mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND); |
75 if (kr != KERN_SUCCESS) { | 70 if (kr != KERN_SUCCESS) { |
76 LOG(ERROR) << "Failed to insert send right for MachBroker server port: " | 71 MACH_LOG(ERROR, kr) << "mach_port_insert_right"; |
77 << MachErrorCode(kr); | |
78 return false; | 72 return false; |
79 } | 73 } |
80 | 74 |
81 server_port_.reset(port); | 75 server_port_.reset(port); |
82 | 76 |
83 // Register the port with the bootstrap server. Because bootstrap_register | 77 // Register the port with the bootstrap server. Because bootstrap_register |
84 // is deprecated, this has to be wraped in an ObjC interface. | 78 // is deprecated, this has to be wraped in an ObjC interface. |
85 NSPort* ns_port = [NSMachPort portWithMachPort:port | 79 NSPort* ns_port = [NSMachPort portWithMachPort:port |
86 options:NSMachPortDeallocateNone]; | 80 options:NSMachPortDeallocateNone]; |
87 NSString* name = base::SysUTF8ToNSString(broker_->GetMachPortName()); | 81 NSString* name = base::SysUTF8ToNSString(broker_->GetMachPortName()); |
88 return [[NSMachBootstrapServer sharedInstance] registerPort:ns_port | 82 return [[NSMachBootstrapServer sharedInstance] registerPort:ns_port |
89 name:name]; | 83 name:name]; |
90 } | 84 } |
91 | 85 |
92 // Implement |PlatformThread::Delegate|. | 86 // Implement |PlatformThread::Delegate|. |
93 virtual void ThreadMain() OVERRIDE { | 87 virtual void ThreadMain() OVERRIDE { |
94 MachBroker_ParentRecvMsg msg; | 88 MachBroker_ParentRecvMsg msg; |
95 bzero(&msg, sizeof(msg)); | 89 bzero(&msg, sizeof(msg)); |
96 msg.header.msgh_size = sizeof(msg); | 90 msg.header.msgh_size = sizeof(msg); |
97 msg.header.msgh_local_port = server_port_.get(); | 91 msg.header.msgh_local_port = server_port_.get(); |
98 | 92 |
| 93 const mach_msg_option_t options = MACH_RCV_MSG | |
| 94 MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) | |
| 95 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); |
| 96 |
99 kern_return_t kr; | 97 kern_return_t kr; |
100 do { | 98 while ((kr = mach_msg(&msg.header, |
| 99 options, |
| 100 0, |
| 101 sizeof(msg), |
| 102 server_port_, |
| 103 MACH_MSG_TIMEOUT_NONE, |
| 104 MACH_PORT_NULL)) == KERN_SUCCESS) { |
101 // Use the kernel audit information to make sure this message is from | 105 // Use the kernel audit information to make sure this message is from |
102 // a task that this process spawned. The kernel audit token contains the | 106 // a task that this process spawned. The kernel audit token contains the |
103 // unspoofable pid of the task that sent the message. | 107 // unspoofable pid of the task that sent the message. |
104 mach_msg_option_t options = MACH_RCV_MSG | | 108 // |
105 MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) | | 109 // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid(). |
106 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); | 110 pid_t child_pid; |
| 111 audit_token_to_au32(msg.trailer.msgh_audit, |
| 112 NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL); |
107 | 113 |
108 kr = mach_msg(&msg.header, options, 0, sizeof(msg), server_port_, | 114 mach_port_t child_task_port = msg.child_task_port.name; |
109 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
110 if (kr == KERN_SUCCESS) { | |
111 // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid(). | |
112 pid_t child_pid; | |
113 audit_token_to_au32(msg.trailer.msgh_audit, | |
114 NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL); | |
115 | 115 |
116 mach_port_t child_task_port = msg.child_task_port.name; | 116 // Take the lock and update the broker information. |
| 117 base::AutoLock lock(broker_->GetLock()); |
| 118 broker_->FinalizePid(child_pid, child_task_port); |
| 119 } |
117 | 120 |
118 // Take the lock and update the broker information. | 121 MACH_LOG(ERROR, kr) << "mach_msg"; |
119 base::AutoLock lock(broker_->GetLock()); | |
120 broker_->FinalizePid(child_pid, child_task_port); | |
121 } | |
122 } while (kr == KERN_SUCCESS); | |
123 | |
124 LOG(ERROR) << "MachBroker thread exiting; mach_msg() likely failed: " | |
125 << MachErrorCode(kr); | |
126 } | 122 } |
127 | 123 |
128 private: | 124 private: |
129 // The MachBroker to use when new child task rights are received. Can be | 125 // The MachBroker to use when new child task rights are received. Can be |
130 // NULL. | 126 // NULL. |
131 MachBroker* broker_; // weak | 127 MachBroker* broker_; // weak |
132 | 128 |
133 base::mac::ScopedMachPort server_port_; | 129 base::mac::ScopedMachPort server_port_; |
134 | 130 |
135 DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate); | 131 DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate); |
136 }; | 132 }; |
137 | 133 |
138 bool MachBroker::ChildSendTaskPortToParent() { | 134 bool MachBroker::ChildSendTaskPortToParent() { |
139 // Look up the named MachBroker port that's been registered with the | 135 // Look up the named MachBroker port that's been registered with the |
140 // bootstrap server. | 136 // bootstrap server. |
141 mach_port_t bootstrap_port; | 137 mach_port_t bootstrap_port; |
142 kern_return_t kr = task_get_bootstrap_port(mach_task_self(), &bootstrap_port); | 138 kern_return_t kr = task_get_bootstrap_port(mach_task_self(), &bootstrap_port); |
143 if (kr != KERN_SUCCESS) { | 139 if (kr != KERN_SUCCESS) { |
144 LOG(ERROR) << "Failed to look up bootstrap port: " << MachErrorCode(kr); | 140 MACH_LOG(ERROR, kr) << "task_get_bootstrap_port"; |
145 return false; | 141 return false; |
146 } | 142 } |
147 | 143 |
148 mach_port_t parent_port; | 144 mach_port_t parent_port; |
149 kr = bootstrap_look_up(bootstrap_port, | 145 kr = bootstrap_look_up(bootstrap_port, |
150 const_cast<char*>(GetMachPortName().c_str()), &parent_port); | 146 const_cast<char*>(GetMachPortName().c_str()), &parent_port); |
151 if (kr != KERN_SUCCESS) { | 147 if (kr != KERN_SUCCESS) { |
152 LOG(ERROR) << "Failed to look up named parent port: " << MachErrorCode(kr); | 148 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up"; |
153 return false; | 149 return false; |
154 } | 150 } |
155 | 151 |
156 // Create the check in message. This will copy a send right on this process' | 152 // Create the check in message. This will copy a send right on this process' |
157 // (the child's) task port and send it to the parent. | 153 // (the child's) task port and send it to the parent. |
158 MachBroker_ChildSendMsg msg; | 154 MachBroker_ChildSendMsg msg; |
159 bzero(&msg, sizeof(msg)); | 155 bzero(&msg, sizeof(msg)); |
160 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND) | | 156 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND) | |
161 MACH_MSGH_BITS_COMPLEX; | 157 MACH_MSGH_BITS_COMPLEX; |
162 msg.header.msgh_remote_port = parent_port; | 158 msg.header.msgh_remote_port = parent_port; |
163 msg.header.msgh_size = sizeof(msg); | 159 msg.header.msgh_size = sizeof(msg); |
164 msg.body.msgh_descriptor_count = 1; | 160 msg.body.msgh_descriptor_count = 1; |
165 msg.child_task_port.name = mach_task_self(); | 161 msg.child_task_port.name = mach_task_self(); |
166 msg.child_task_port.disposition = MACH_MSG_TYPE_PORT_SEND; | 162 msg.child_task_port.disposition = MACH_MSG_TYPE_PORT_SEND; |
167 msg.child_task_port.type = MACH_MSG_PORT_DESCRIPTOR; | 163 msg.child_task_port.type = MACH_MSG_PORT_DESCRIPTOR; |
168 | 164 |
169 kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg), | 165 kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg), |
170 0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL); | 166 0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL); |
171 if (kr != KERN_SUCCESS) { | 167 if (kr != KERN_SUCCESS) { |
172 LOG(ERROR) << "Failed to send task port to parent: " << MachErrorCode(kr); | 168 MACH_LOG(ERROR, kr) << "mach_msg"; |
173 return false; | 169 return false; |
174 } | 170 } |
175 | 171 |
176 return true; | 172 return true; |
177 } | 173 } |
178 | 174 |
179 MachBroker* MachBroker::GetInstance() { | 175 MachBroker* MachBroker::GetInstance() { |
180 return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); | 176 return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); |
181 } | 177 } |
182 | 178 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 } | 268 } |
273 | 269 |
274 void MachBroker::InvalidatePid(base::ProcessHandle pid) { | 270 void MachBroker::InvalidatePid(base::ProcessHandle pid) { |
275 base::AutoLock lock(lock_); | 271 base::AutoLock lock(lock_); |
276 MachBroker::MachMap::iterator it = mach_map_.find(pid); | 272 MachBroker::MachMap::iterator it = mach_map_.find(pid); |
277 if (it == mach_map_.end()) | 273 if (it == mach_map_.end()) |
278 return; | 274 return; |
279 | 275 |
280 kern_return_t kr = mach_port_deallocate(mach_task_self(), | 276 kern_return_t kr = mach_port_deallocate(mach_task_self(), |
281 it->second); | 277 it->second); |
282 LOG_IF(WARNING, kr != KERN_SUCCESS) | 278 MACH_LOG_IF(WARNING, kr != KERN_SUCCESS, kr) << "mach_port_deallocate"; |
283 << "Failed to mach_port_deallocate mach task " << it->second | |
284 << ", error " << MachErrorCode(kr); | |
285 mach_map_.erase(it); | 279 mach_map_.erase(it); |
286 } | 280 } |
287 | 281 |
288 // static | 282 // static |
289 std::string MachBroker::GetMachPortName() { | 283 std::string MachBroker::GetMachPortName() { |
290 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 284 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
291 const bool is_child = command_line->HasSwitch(switches::kProcessType); | 285 const bool is_child = command_line->HasSwitch(switches::kProcessType); |
292 | 286 |
293 // In non-browser (child) processes, use the parent's pid. | 287 // In non-browser (child) processes, use the parent's pid. |
294 const pid_t pid = is_child ? getppid() : getpid(); | 288 const pid_t pid = is_child ? getppid() : getpid(); |
295 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); | 289 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); |
296 } | 290 } |
297 | 291 |
298 void MachBroker::RegisterNotifications() { | 292 void MachBroker::RegisterNotifications() { |
299 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, | 293 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, |
300 NotificationService::AllBrowserContextsAndSources()); | 294 NotificationService::AllBrowserContextsAndSources()); |
301 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 295 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
302 NotificationService::AllBrowserContextsAndSources()); | 296 NotificationService::AllBrowserContextsAndSources()); |
303 | 297 |
304 // No corresponding StopObservingBrowserChildProcesses, | 298 // No corresponding StopObservingBrowserChildProcesses, |
305 // we leak this singleton. | 299 // we leak this singleton. |
306 BrowserChildProcessObserver::Add(this); | 300 BrowserChildProcessObserver::Add(this); |
307 } | 301 } |
308 | 302 |
309 } // namespace content | 303 } // namespace content |
OLD | NEW |