Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: content/browser/mach_broker_mac.mm

Issue 278923002: Use the new ScopedMachVM class and the MACH_LOG family of logging macros (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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, options, 0, sizeof(msg), server_port_,
Mark Mentovai 2014/05/09 19:06:20 It occurs to me that this sucks. A compromised chi
Robert Sesek 2014/05/09 20:40:01 Switching this to libdispatch would be better. Dis
99 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)) ==
100 KERN_SUCCESS) {
Robert Sesek 2014/05/09 20:40:01 nit? should this be indented +4
Mark Mentovai 2014/05/09 21:09:35 rsesek wrote:
101 // 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
102 // 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
103 // unspoofable pid of the task that sent the message. 103 // unspoofable pid of the task that sent the message.
104 mach_msg_option_t options = MACH_RCV_MSG | 104 //
105 MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) | 105 // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
106 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); 106 pid_t child_pid;
107 audit_token_to_au32(msg.trailer.msgh_audit,
108 NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL);
107 109
108 kr = mach_msg(&msg.header, options, 0, sizeof(msg), server_port_, 110 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 111
116 mach_port_t child_task_port = msg.child_task_port.name; 112 // Take the lock and update the broker information.
113 base::AutoLock lock(broker_->GetLock());
114 broker_->FinalizePid(child_pid, child_task_port);
115 }
117 116
118 // Take the lock and update the broker information. 117 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 } 118 }
127 119
128 private: 120 private:
129 // The MachBroker to use when new child task rights are received. Can be 121 // The MachBroker to use when new child task rights are received. Can be
130 // NULL. 122 // NULL.
131 MachBroker* broker_; // weak 123 MachBroker* broker_; // weak
132 124
133 base::mac::ScopedMachPort server_port_; 125 base::mac::ScopedMachPort server_port_;
134 126
135 DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate); 127 DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate);
136 }; 128 };
137 129
138 bool MachBroker::ChildSendTaskPortToParent() { 130 bool MachBroker::ChildSendTaskPortToParent() {
139 // Look up the named MachBroker port that's been registered with the 131 // Look up the named MachBroker port that's been registered with the
140 // bootstrap server. 132 // bootstrap server.
141 mach_port_t bootstrap_port; 133 mach_port_t bootstrap_port;
142 kern_return_t kr = task_get_bootstrap_port(mach_task_self(), &bootstrap_port); 134 kern_return_t kr = task_get_bootstrap_port(mach_task_self(), &bootstrap_port);
143 if (kr != KERN_SUCCESS) { 135 if (kr != KERN_SUCCESS) {
144 LOG(ERROR) << "Failed to look up bootstrap port: " << MachErrorCode(kr); 136 MACH_LOG(ERROR, kr) << "task_get_bootstrap_port";
145 return false; 137 return false;
146 } 138 }
147 139
148 mach_port_t parent_port; 140 mach_port_t parent_port;
149 kr = bootstrap_look_up(bootstrap_port, 141 kr = bootstrap_look_up(bootstrap_port,
150 const_cast<char*>(GetMachPortName().c_str()), &parent_port); 142 const_cast<char*>(GetMachPortName().c_str()), &parent_port);
151 if (kr != KERN_SUCCESS) { 143 if (kr != KERN_SUCCESS) {
152 LOG(ERROR) << "Failed to look up named parent port: " << MachErrorCode(kr); 144 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up";
153 return false; 145 return false;
154 } 146 }
155 147
156 // Create the check in message. This will copy a send right on this process' 148 // 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. 149 // (the child's) task port and send it to the parent.
158 MachBroker_ChildSendMsg msg; 150 MachBroker_ChildSendMsg msg;
159 bzero(&msg, sizeof(msg)); 151 bzero(&msg, sizeof(msg));
160 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND) | 152 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND) |
161 MACH_MSGH_BITS_COMPLEX; 153 MACH_MSGH_BITS_COMPLEX;
162 msg.header.msgh_remote_port = parent_port; 154 msg.header.msgh_remote_port = parent_port;
163 msg.header.msgh_size = sizeof(msg); 155 msg.header.msgh_size = sizeof(msg);
164 msg.body.msgh_descriptor_count = 1; 156 msg.body.msgh_descriptor_count = 1;
165 msg.child_task_port.name = mach_task_self(); 157 msg.child_task_port.name = mach_task_self();
166 msg.child_task_port.disposition = MACH_MSG_TYPE_PORT_SEND; 158 msg.child_task_port.disposition = MACH_MSG_TYPE_PORT_SEND;
167 msg.child_task_port.type = MACH_MSG_PORT_DESCRIPTOR; 159 msg.child_task_port.type = MACH_MSG_PORT_DESCRIPTOR;
168 160
169 kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg), 161 kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg),
170 0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL); 162 0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL);
171 if (kr != KERN_SUCCESS) { 163 if (kr != KERN_SUCCESS) {
172 LOG(ERROR) << "Failed to send task port to parent: " << MachErrorCode(kr); 164 MACH_LOG(ERROR, kr) << "mach_msg";
173 return false; 165 return false;
174 } 166 }
175 167
176 return true; 168 return true;
177 } 169 }
178 170
179 MachBroker* MachBroker::GetInstance() { 171 MachBroker* MachBroker::GetInstance() {
180 return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); 172 return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get();
181 } 173 }
182 174
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 } 264 }
273 265
274 void MachBroker::InvalidatePid(base::ProcessHandle pid) { 266 void MachBroker::InvalidatePid(base::ProcessHandle pid) {
275 base::AutoLock lock(lock_); 267 base::AutoLock lock(lock_);
276 MachBroker::MachMap::iterator it = mach_map_.find(pid); 268 MachBroker::MachMap::iterator it = mach_map_.find(pid);
277 if (it == mach_map_.end()) 269 if (it == mach_map_.end())
278 return; 270 return;
279 271
280 kern_return_t kr = mach_port_deallocate(mach_task_self(), 272 kern_return_t kr = mach_port_deallocate(mach_task_self(),
281 it->second); 273 it->second);
282 LOG_IF(WARNING, kr != KERN_SUCCESS) 274 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); 275 mach_map_.erase(it);
286 } 276 }
287 277
288 // static 278 // static
289 std::string MachBroker::GetMachPortName() { 279 std::string MachBroker::GetMachPortName() {
290 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 280 const CommandLine* command_line = CommandLine::ForCurrentProcess();
291 const bool is_child = command_line->HasSwitch(switches::kProcessType); 281 const bool is_child = command_line->HasSwitch(switches::kProcessType);
292 282
293 // In non-browser (child) processes, use the parent's pid. 283 // In non-browser (child) processes, use the parent's pid.
294 const pid_t pid = is_child ? getppid() : getpid(); 284 const pid_t pid = is_child ? getppid() : getpid();
295 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); 285 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid);
296 } 286 }
297 287
298 void MachBroker::RegisterNotifications() { 288 void MachBroker::RegisterNotifications() {
299 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, 289 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED,
300 NotificationService::AllBrowserContextsAndSources()); 290 NotificationService::AllBrowserContextsAndSources());
301 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, 291 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED,
302 NotificationService::AllBrowserContextsAndSources()); 292 NotificationService::AllBrowserContextsAndSources());
303 293
304 // No corresponding StopObservingBrowserChildProcesses, 294 // No corresponding StopObservingBrowserChildProcesses,
305 // we leak this singleton. 295 // we leak this singleton.
306 BrowserChildProcessObserver::Add(this); 296 BrowserChildProcessObserver::Add(this);
307 } 297 }
308 298
309 } // namespace content 299 } // namespace content
OLDNEW
« base/process/memory_mac.mm ('K') | « chrome/browser/mac/install_from_dmg.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698