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