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

Side by Side Diff: components/breakpad/browser/crash_handler_host_linux.cc

Issue 321433003: Linux: Improve message handling in CrashHandlerHostLinux to prevent file descriptor leaks. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 6 years, 6 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/breakpad/browser/crash_handler_host_linux.h" 5 #include "components/breakpad/browser/crash_handler_host_linux.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <sys/socket.h> 9 #include <sys/socket.h>
10 #include <sys/syscall.h> 10 #include <sys/syscall.h>
11 #include <unistd.h> 11 #include <unistd.h>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/bind_helpers.h" 14 #include "base/bind_helpers.h"
15 #include "base/file_util.h" 15 #include "base/file_util.h"
16 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
17 #include "base/files/scoped_file.h"
17 #include "base/format_macros.h" 18 #include "base/format_macros.h"
18 #include "base/linux_util.h" 19 #include "base/linux_util.h"
19 #include "base/logging.h" 20 #include "base/logging.h"
20 #include "base/message_loop/message_loop.h" 21 #include "base/message_loop/message_loop.h"
21 #include "base/path_service.h" 22 #include "base/path_service.h"
22 #include "base/posix/eintr_wrapper.h" 23 #include "base/posix/eintr_wrapper.h"
23 #include "base/rand_util.h" 24 #include "base/rand_util.h"
24 #include "base/strings/string_util.h" 25 #include "base/strings/string_util.h"
25 #include "base/strings/stringprintf.h" 26 #include "base/strings/stringprintf.h"
26 #include "base/threading/thread.h" 27 #include "base/threading/thread.h"
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 iov[6].iov_base = asan_report.get(); 189 iov[6].iov_base = asan_report.get();
189 iov[6].iov_len = kMaxAsanReportSize + 1; 190 iov[6].iov_len = kMaxAsanReportSize + 1;
190 COMPILE_ASSERT(6 == kCrashIovSize - 1, Incorrect_Number_Of_Iovec_Members); 191 COMPILE_ASSERT(6 == kCrashIovSize - 1, Incorrect_Number_Of_Iovec_Members);
191 #endif 192 #endif
192 msg.msg_iov = iov; 193 msg.msg_iov = iov;
193 msg.msg_iovlen = kCrashIovSize; 194 msg.msg_iovlen = kCrashIovSize;
194 msg.msg_control = control; 195 msg.msg_control = control;
195 msg.msg_controllen = kControlMsgSize; 196 msg.msg_controllen = kControlMsgSize;
196 197
197 const ssize_t msg_size = HANDLE_EINTR(recvmsg(browser_socket_, &msg, 0)); 198 const ssize_t msg_size = HANDLE_EINTR(recvmsg(browser_socket_, &msg, 0));
198 if (msg_size != expected_msg_size) { 199 if (msg_size < 0) {
199 LOG(ERROR) << "Error reading from death signal socket. Crash dumping" 200 LOG(ERROR) << "Error reading from death signal socket. Crash dumping"
200 << " is disabled." 201 << " is disabled."
201 << " msg_size:" << msg_size 202 << " msg_size:" << msg_size
202 << " errno:" << errno; 203 << " errno:" << errno;
203 file_descriptor_watcher_.StopWatchingFileDescriptor(); 204 file_descriptor_watcher_.StopWatchingFileDescriptor();
204 return; 205 return;
205 } 206 }
207 const bool bad_message = (msg_size != expected_msg_size ||
208 msg.msg_controllen != kControlMsgSize ||
209 msg.msg_flags & ~MSG_TRUNC);
210 base::ScopedFD signal_fd;
211 pid_t crashing_pid = -1;
212 if (msg.msg_controllen > 0) {
213 // Walk the control payload and extract the file descriptor and
214 // validated pid.
215 for (struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); hdr;
216 hdr = CMSG_NXTHDR(&msg, hdr)) {
217 if (hdr->cmsg_level != SOL_SOCKET)
218 continue;
219 if (hdr->cmsg_type == SCM_RIGHTS) {
220 const size_t len = hdr->cmsg_len -
221 (((uint8_t*)CMSG_DATA(hdr)) - (uint8_t*)hdr);
222 DCHECK_EQ(0U, len % sizeof(int));
223 const size_t num_fds = len / sizeof(int);
224 if (num_fds != kNumFDs) {
225 // A nasty process could try and send us too many descriptors and
226 // force a leak.
227 LOG(ERROR) << "Death signal contained wrong number of descriptors;"
228 << " num_fds:" << num_fds;
229 for (size_t i = 0; i < num_fds; ++i)
230 close(reinterpret_cast<int*>(CMSG_DATA(hdr))[i]);
231 return;
232 } else {
mdempsky 2014/06/05 21:53:58 (Since the above "if" condition ends in a "return;
Lei Zhang 2014/06/05 21:57:23 Done.
233 DCHECK(!signal_fd.is_valid());
234 int fd = reinterpret_cast<int*>(CMSG_DATA(hdr))[0];
235 if (fd >= 0) {
mdempsky 2014/06/05 21:53:58 You can probably DCHECK this? The kernel should n
Lei Zhang 2014/06/05 21:57:23 Done.
236 signal_fd.reset(fd);
237 }
238 }
239 } else if (hdr->cmsg_type == SCM_CREDENTIALS) {
240 DCHECK_EQ(-1, crashing_pid);
241 const struct ucred *cred =
242 reinterpret_cast<struct ucred*>(CMSG_DATA(hdr));
243 crashing_pid = cred->pid;
244 }
245 }
246 }
206 247
207 if (msg.msg_controllen != kControlMsgSize || 248 if (bad_message) {
208 msg.msg_flags & ~MSG_TRUNC) {
209 LOG(ERROR) << "Received death signal message with the wrong size;" 249 LOG(ERROR) << "Received death signal message with the wrong size;"
210 << " msg.msg_controllen:" << msg.msg_controllen 250 << " msg.msg_controllen:" << msg.msg_controllen
211 << " msg.msg_flags:" << msg.msg_flags 251 << " msg.msg_flags:" << msg.msg_flags
212 << " kCrashContextSize:" << kCrashContextSize 252 << " kCrashContextSize:" << kCrashContextSize
213 << " kControlMsgSize:" << kControlMsgSize; 253 << " kControlMsgSize:" << kControlMsgSize;
214 return; 254 return;
215 } 255 }
216 256 if (crashing_pid == -1 || !signal_fd.is_valid()) {
217 // Walk the control payload an extract the file descriptor and validated pid.
218 pid_t crashing_pid = -1;
219 int signal_fd = -1;
220 for (struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); hdr;
221 hdr = CMSG_NXTHDR(&msg, hdr)) {
222 if (hdr->cmsg_level != SOL_SOCKET)
223 continue;
224 if (hdr->cmsg_type == SCM_RIGHTS) {
225 const size_t len = hdr->cmsg_len -
226 (((uint8_t*)CMSG_DATA(hdr)) - (uint8_t*)hdr);
227 DCHECK_EQ(0U, len % sizeof(int));
228 const size_t num_fds = len / sizeof(int);
229 if (num_fds != kNumFDs) {
230 // A nasty process could try and send us too many descriptors and
231 // force a leak.
232 LOG(ERROR) << "Death signal contained wrong number of descriptors;"
233 << " num_fds:" << num_fds;
234 for (size_t i = 0; i < num_fds; ++i)
235 close(reinterpret_cast<int*>(CMSG_DATA(hdr))[i]);
236 return;
237 } else {
238 signal_fd = reinterpret_cast<int*>(CMSG_DATA(hdr))[0];
239 }
240 } else if (hdr->cmsg_type == SCM_CREDENTIALS) {
241 const struct ucred *cred =
242 reinterpret_cast<struct ucred*>(CMSG_DATA(hdr));
243 crashing_pid = cred->pid;
244 }
245 }
246
247 if (crashing_pid == -1 || signal_fd == -1) {
248 LOG(ERROR) << "Death signal message didn't contain all expected control" 257 LOG(ERROR) << "Death signal message didn't contain all expected control"
249 << " messages"; 258 << " messages";
250 if (signal_fd >= 0)
251 close(signal_fd);
252 return; 259 return;
253 } 260 }
254 261
255 // The crashing TID set inside the compromised context via 262 // The crashing TID set inside the compromised context via
256 // sys_gettid() in ExceptionHandler::HandleSignal might be wrong (if 263 // sys_gettid() in ExceptionHandler::HandleSignal might be wrong (if
257 // the kernel supports PID namespacing) and may need to be 264 // the kernel supports PID namespacing) and may need to be
258 // translated. 265 // translated.
259 // 266 //
260 // We expect the crashing thread to be in sys_read(), waiting for us to 267 // We expect the crashing thread to be in sys_read(), waiting for us to
261 // write to |signal_fd|. Most newer kernels where we have the different pid 268 // write to |signal_fd|. Most newer kernels where we have the different pid
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 326
320 327
321 BrowserThread::GetBlockingPool()->PostSequencedWorkerTask( 328 BrowserThread::GetBlockingPool()->PostSequencedWorkerTask(
322 worker_pool_token_, 329 worker_pool_token_,
323 FROM_HERE, 330 FROM_HERE,
324 base::Bind(&CrashHandlerHostLinux::WriteDumpFile, 331 base::Bind(&CrashHandlerHostLinux::WriteDumpFile,
325 base::Unretained(this), 332 base::Unretained(this),
326 base::Passed(&info), 333 base::Passed(&info),
327 base::Passed(&crash_context), 334 base::Passed(&crash_context),
328 crashing_pid, 335 crashing_pid,
329 signal_fd)); 336 signal_fd.release()));
330 } 337 }
331 338
332 void CrashHandlerHostLinux::WriteDumpFile(scoped_ptr<BreakpadInfo> info, 339 void CrashHandlerHostLinux::WriteDumpFile(scoped_ptr<BreakpadInfo> info,
333 scoped_ptr<char[]> crash_context, 340 scoped_ptr<char[]> crash_context,
334 pid_t crashing_pid, 341 pid_t crashing_pid,
335 int signal_fd) { 342 int signal_fd) {
336 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( 343 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
337 worker_pool_token_)); 344 worker_pool_token_));
338 345
339 // Set |info->distro| here because base::GetLinuxDistro() needs to run on a 346 // Set |info->distro| here because base::GetLinuxDistro() needs to run on a
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 // no-ops. 429 // no-ops.
423 shutting_down_ = true; 430 shutting_down_ = true;
424 uploader_thread_->Stop(); 431 uploader_thread_->Stop();
425 } 432 }
426 433
427 bool CrashHandlerHostLinux::IsShuttingDown() const { 434 bool CrashHandlerHostLinux::IsShuttingDown() const {
428 return shutting_down_; 435 return shutting_down_;
429 } 436 }
430 437
431 } // namespace breakpad 438 } // namespace breakpad
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698