blob: 5474a47c688cdfe4b9c4c686a8ded8cb1236798c [file] [log] [blame]
[email protected]c83dd912010-04-06 18:50:511// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]9a5d2a52009-05-22 03:37:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]2456c572009-11-09 04:21:515#include "chrome/browser/crash_handler_host_linux.h"
[email protected]9a5d2a52009-05-22 03:37:456
[email protected]9a5d2a52009-05-22 03:37:457#include <stdint.h>
[email protected]85ebe8f2009-10-29 04:02:558#include <stdlib.h>
[email protected]9a5d2a52009-05-22 03:37:459#include <sys/socket.h>
[email protected]662183142010-07-16 19:28:1710#include <sys/syscall.h>
[email protected]4378a822009-07-08 01:15:1411#include <sys/types.h>
[email protected]4378a822009-07-08 01:15:1412#include <unistd.h>
13
[email protected]9a5d2a52009-05-22 03:37:4514#include "base/eintr_wrapper.h"
[email protected]cbd5fd52009-08-26 00:14:2715#include "base/file_path.h"
[email protected]c725d7922009-06-30 00:05:0816#include "base/format_macros.h"
[email protected]85ebe8f2009-10-29 04:02:5517#include "base/linux_util.h"
[email protected]9a5d2a52009-05-22 03:37:4518#include "base/logging.h"
19#include "base/message_loop.h"
[email protected]cbd5fd52009-08-26 00:14:2720#include "base/path_service.h"
[email protected]9a5d2a52009-05-22 03:37:4521#include "base/rand_util.h"
22#include "base/string_util.h"
[email protected]b064f0eb2010-09-02 23:53:2623#include "base/task.h"
24#include "base/thread.h"
[email protected]b07fc5112009-12-02 01:55:0625#include "breakpad/src/client/linux/handler/exception_handler.h"
26#include "breakpad/src/client/linux/minidump_writer/linux_dumper.h"
27#include "breakpad/src/client/linux/minidump_writer/minidump_writer.h"
[email protected]9a5d2a52009-05-22 03:37:4528#include "chrome/app/breakpad_linux.h"
[email protected]ed7e6dd2010-10-12 02:02:4529#include "chrome/browser/browser_thread.h"
[email protected]cbd5fd52009-08-26 00:14:2730#include "chrome/common/chrome_paths.h"
[email protected]99ca9a12010-03-12 18:32:1031#include "chrome/common/env_vars.h"
[email protected]9a5d2a52009-05-22 03:37:4532
[email protected]662183142010-07-16 19:28:1733using google_breakpad::ExceptionHandler;
34
[email protected]b064f0eb2010-09-02 23:53:2635namespace {
36
37// Handles the crash dump and frees the allocated BreakpadInfo struct.
[email protected]ca779662010-11-11 23:28:4338void CrashDumpTask(CrashHandlerHostLinux* handler, BreakpadInfo* info) {
39 if (handler->IsShuttingDown())
40 return;
41
[email protected]b064f0eb2010-09-02 23:53:2642 HandleCrashDump(*info);
43 delete[] info->filename;
44 delete[] info->process_type;
45 delete[] info->crash_url;
46 delete[] info->guid;
47 delete[] info->distro;
48 delete info;
49}
50
51} // namespace
52
[email protected]2456c572009-11-09 04:21:5153// Since classes derived from CrashHandlerHostLinux are singletons, it's only
54// destroyed at the end of the processes lifetime, which is greater in span than
55// the lifetime of the IO message loop.
[email protected]c56428f22010-06-16 02:17:2356DISABLE_RUNNABLE_METHOD_REFCOUNT(CrashHandlerHostLinux);
[email protected]9a5d2a52009-05-22 03:37:4557
[email protected]ca779662010-11-11 23:28:4358CrashHandlerHostLinux::CrashHandlerHostLinux()
59 : shutting_down_(false) {
[email protected]9a5d2a52009-05-22 03:37:4560 int fds[2];
[email protected]2456c572009-11-09 04:21:5161 // We use SOCK_SEQPACKET rather than SOCK_DGRAM to prevent the process from
[email protected]54730a12009-10-07 22:55:4862 // sending datagrams to other sockets on the system. The sandbox may prevent
[email protected]2456c572009-11-09 04:21:5163 // the process from calling socket() to create new sockets, but it'll still
[email protected]54730a12009-10-07 22:55:4864 // inherit some sockets. With PF_UNIX+SOCK_DGRAM, it can call sendmsg to send
65 // a datagram to any (abstract) socket on the same system. With
66 // SOCK_SEQPACKET, this is prevented.
[email protected]c83dd912010-04-06 18:50:5167 CHECK_EQ(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds), 0);
[email protected]9a5d2a52009-05-22 03:37:4568 static const int on = 1;
69
70 // Enable passcred on the server end of the socket
[email protected]c83dd912010-04-06 18:50:5171 CHECK_EQ(setsockopt(fds[1], SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)), 0);
[email protected]9a5d2a52009-05-22 03:37:4572
[email protected]2456c572009-11-09 04:21:5173 process_socket_ = fds[0];
[email protected]9a5d2a52009-05-22 03:37:4574 browser_socket_ = fds[1];
75
[email protected]d04e7662010-10-10 22:24:4876 BrowserThread::PostTask(
77 BrowserThread::IO, FROM_HERE,
[email protected]2456c572009-11-09 04:21:5178 NewRunnableMethod(this, &CrashHandlerHostLinux::Init));
[email protected]9a5d2a52009-05-22 03:37:4579}
80
[email protected]2456c572009-11-09 04:21:5181CrashHandlerHostLinux::~CrashHandlerHostLinux() {
82 HANDLE_EINTR(close(process_socket_));
[email protected]9a5d2a52009-05-22 03:37:4583 HANDLE_EINTR(close(browser_socket_));
84}
85
[email protected]2456c572009-11-09 04:21:5186void CrashHandlerHostLinux::Init() {
[email protected]9a5d2a52009-05-22 03:37:4587 MessageLoopForIO* ml = MessageLoopForIO::current();
88 CHECK(ml->WatchFileDescriptor(
89 browser_socket_, true /* persistent */,
90 MessageLoopForIO::WATCH_READ,
91 &file_descriptor_watcher_, this));
92 ml->AddDestructionObserver(this);
93}
94
[email protected]19eef062010-09-16 19:44:0995void CrashHandlerHostLinux::InitCrashUploaderThread() {
96 SetProcessType();
97 uploader_thread_.reset(
98 new base::Thread(std::string(process_type_ + "_crash_uploader").c_str()));
99 uploader_thread_->Start();
100}
101
[email protected]2456c572009-11-09 04:21:51102void CrashHandlerHostLinux::OnFileCanWriteWithoutBlocking(int fd) {
[email protected]9a5d2a52009-05-22 03:37:45103 DCHECK(false);
104}
105
[email protected]2456c572009-11-09 04:21:51106void CrashHandlerHostLinux::OnFileCanReadWithoutBlocking(int fd) {
[email protected]9a5d2a52009-05-22 03:37:45107 DCHECK_EQ(fd, browser_socket_);
108
[email protected]2456c572009-11-09 04:21:51109 // A process has crashed and has signaled us by writing a datagram
[email protected]9a5d2a52009-05-22 03:37:45110 // to the death signal socket. The datagram contains the crash context needed
111 // for writing the minidump as well as a file descriptor and a credentials
112 // block so that they can't lie about their pid.
113
114 // The length of the control message:
115 static const unsigned kControlMsgSize =
[email protected]603e1952010-08-11 06:44:30116 CMSG_SPACE(2*sizeof(int)) + CMSG_SPACE(sizeof(struct ucred));
[email protected]9a5d2a52009-05-22 03:37:45117 // The length of the regular payload:
118 static const unsigned kCrashContextSize =
[email protected]662183142010-07-16 19:28:17119 sizeof(ExceptionHandler::CrashContext);
[email protected]9a5d2a52009-05-22 03:37:45120
[email protected]c7b1d2f2010-12-03 03:33:13121 const size_t kIovSize = 7;
[email protected]9a5d2a52009-05-22 03:37:45122 struct msghdr msg = {0};
[email protected]c7b1d2f2010-12-03 03:33:13123 struct iovec iov[kIovSize];
[email protected]2eb41e72009-07-15 23:07:34124 char crash_context[kCrashContextSize];
[email protected]b064f0eb2010-09-02 23:53:26125 char* guid = new char[kGuidSize + 1];
126 char* crash_url = new char[kMaxActiveURLSize + 1];
127 char* distro = new char[kDistroSize + 1];
[email protected]662183142010-07-16 19:28:17128 char* tid_buf_addr = NULL;
129 int tid_fd = -1;
[email protected]c7b1d2f2010-12-03 03:33:13130 uint64_t uptime;
[email protected]9a5d2a52009-05-22 03:37:45131 char control[kControlMsgSize];
[email protected]b064f0eb2010-09-02 23:53:26132 const ssize_t expected_msg_size = sizeof(crash_context) +
133 kGuidSize + 1 +
134 kMaxActiveURLSize + 1 +
135 kDistroSize + 1 +
[email protected]c7b1d2f2010-12-03 03:33:13136 sizeof(tid_buf_addr) + sizeof(tid_fd) +
137 sizeof(uptime);
[email protected]2eb41e72009-07-15 23:07:34138
139 iov[0].iov_base = crash_context;
140 iov[0].iov_len = sizeof(crash_context);
141 iov[1].iov_base = guid;
[email protected]b064f0eb2010-09-02 23:53:26142 iov[1].iov_len = kGuidSize + 1;
[email protected]2eb41e72009-07-15 23:07:34143 iov[2].iov_base = crash_url;
[email protected]b064f0eb2010-09-02 23:53:26144 iov[2].iov_len = kMaxActiveURLSize + 1;
[email protected]912c6452009-07-17 05:55:51145 iov[3].iov_base = distro;
[email protected]b064f0eb2010-09-02 23:53:26146 iov[3].iov_len = kDistroSize + 1;
[email protected]662183142010-07-16 19:28:17147 iov[4].iov_base = &tid_buf_addr;
148 iov[4].iov_len = sizeof(tid_buf_addr);
149 iov[5].iov_base = &tid_fd;
150 iov[5].iov_len = sizeof(tid_fd);
[email protected]c7b1d2f2010-12-03 03:33:13151 iov[6].iov_base = &uptime;
152 iov[6].iov_len = sizeof(uptime);
[email protected]2eb41e72009-07-15 23:07:34153 msg.msg_iov = iov;
[email protected]c7b1d2f2010-12-03 03:33:13154 msg.msg_iovlen = kIovSize;
[email protected]9a5d2a52009-05-22 03:37:45155 msg.msg_control = control;
156 msg.msg_controllen = kControlMsgSize;
157
[email protected]2eb41e72009-07-15 23:07:34158 const ssize_t msg_size = HANDLE_EINTR(recvmsg(browser_socket_, &msg, 0));
159 if (msg_size != expected_msg_size) {
[email protected]9a5d2a52009-05-22 03:37:45160 LOG(ERROR) << "Error reading from death signal socket. Crash dumping"
161 << " is disabled."
[email protected]2eb41e72009-07-15 23:07:34162 << " msg_size:" << msg_size
[email protected]9a5d2a52009-05-22 03:37:45163 << " errno:" << errno;
164 file_descriptor_watcher_.StopWatchingFileDescriptor();
165 return;
166 }
167
[email protected]2eb41e72009-07-15 23:07:34168 if (msg.msg_controllen != kControlMsgSize ||
[email protected]9a5d2a52009-05-22 03:37:45169 msg.msg_flags & ~MSG_TRUNC) {
170 LOG(ERROR) << "Received death signal message with the wrong size;"
[email protected]9a5d2a52009-05-22 03:37:45171 << " msg.msg_controllen:" << msg.msg_controllen
172 << " msg.msg_flags:" << msg.msg_flags
173 << " kCrashContextSize:" << kCrashContextSize
174 << " kControlMsgSize:" << kControlMsgSize;
175 return;
176 }
177
[email protected]9a5d2a52009-05-22 03:37:45178 // Walk the control payload an extract the file descriptor and validated pid.
179 pid_t crashing_pid = -1;
[email protected]15e85772010-08-09 20:44:03180 int partner_fd = -1;
[email protected]9a5d2a52009-05-22 03:37:45181 int signal_fd = -1;
182 for (struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); hdr;
183 hdr = CMSG_NXTHDR(&msg, hdr)) {
184 if (hdr->cmsg_level != SOL_SOCKET)
185 continue;
186 if (hdr->cmsg_type == SCM_RIGHTS) {
187 const unsigned len = hdr->cmsg_len -
188 (((uint8_t*)CMSG_DATA(hdr)) - (uint8_t*)hdr);
[email protected]912c6452009-07-17 05:55:51189 DCHECK_EQ(len % sizeof(int), 0u);
[email protected]9a5d2a52009-05-22 03:37:45190 const unsigned num_fds = len / sizeof(int);
[email protected]15e85772010-08-09 20:44:03191 if (num_fds != 2) {
[email protected]2456c572009-11-09 04:21:51192 // A nasty process could try and send us too many descriptors and
[email protected]9a5d2a52009-05-22 03:37:45193 // force a leak.
[email protected]15e85772010-08-09 20:44:03194 LOG(ERROR) << "Death signal contained wrong number of descriptors;"
[email protected]9a5d2a52009-05-22 03:37:45195 << " num_fds:" << num_fds;
196 for (unsigned i = 0; i < num_fds; ++i)
197 HANDLE_EINTR(close(reinterpret_cast<int*>(CMSG_DATA(hdr))[i]));
198 return;
199 } else {
[email protected]15e85772010-08-09 20:44:03200 partner_fd = reinterpret_cast<int*>(CMSG_DATA(hdr))[0];
201 signal_fd = reinterpret_cast<int*>(CMSG_DATA(hdr))[1];
[email protected]9a5d2a52009-05-22 03:37:45202 }
203 } else if (hdr->cmsg_type == SCM_CREDENTIALS) {
204 const struct ucred *cred =
205 reinterpret_cast<struct ucred*>(CMSG_DATA(hdr));
206 crashing_pid = cred->pid;
207 }
208 }
209
[email protected]15e85772010-08-09 20:44:03210 if (crashing_pid == -1 || partner_fd == -1 || signal_fd == -1) {
[email protected]9a5d2a52009-05-22 03:37:45211 LOG(ERROR) << "Death signal message didn't contain all expected control"
212 << " messages";
[email protected]15e85772010-08-09 20:44:03213 if (partner_fd >= 0)
214 HANDLE_EINTR(close(partner_fd));
215 if (signal_fd >= 0)
[email protected]9a5d2a52009-05-22 03:37:45216 HANDLE_EINTR(close(signal_fd));
217 return;
218 }
219
[email protected]4378a822009-07-08 01:15:14220 // Kernel bug workaround (broken in 2.6.30 at least):
221 // The kernel doesn't translate PIDs in SCM_CREDENTIALS across PID
222 // namespaces. Thus |crashing_pid| might be garbage from our point of view.
223 // In the future we can remove this workaround, but we have to wait a couple
224 // of years to be sure that it's worked its way out into the world.
225
[email protected]15e85772010-08-09 20:44:03226 // The crashing process closes its copy of the signal_fd immediately after
227 // calling sendmsg(). We can thus not reliably look for with with
228 // FindProcessHoldingSocket(). But by necessity, it has to keep the
229 // partner_fd open until the crashdump is complete.
[email protected]4378a822009-07-08 01:15:14230 uint64_t inode_number;
[email protected]15e85772010-08-09 20:44:03231 if (!base::FileDescriptorGetInode(&inode_number, partner_fd)) {
[email protected]4378a822009-07-08 01:15:14232 LOG(WARNING) << "Failed to get inode number for passed socket";
[email protected]15e85772010-08-09 20:44:03233 HANDLE_EINTR(close(partner_fd));
[email protected]4378a822009-07-08 01:15:14234 HANDLE_EINTR(close(signal_fd));
235 return;
236 }
[email protected]15e85772010-08-09 20:44:03237 HANDLE_EINTR(close(partner_fd));
[email protected]4378a822009-07-08 01:15:14238
[email protected]662183142010-07-16 19:28:17239 pid_t actual_crashing_pid = -1;
[email protected]15e85772010-08-09 20:44:03240 if (!base::FindProcessHoldingSocket(&actual_crashing_pid, inode_number)) {
[email protected]4378a822009-07-08 01:15:14241 LOG(WARNING) << "Failed to find process holding other end of crash reply "
242 "socket";
243 HANDLE_EINTR(close(signal_fd));
244 return;
245 }
[email protected]15e85772010-08-09 20:44:03246
[email protected]662183142010-07-16 19:28:17247 if (actual_crashing_pid != crashing_pid) {
248 crashing_pid = actual_crashing_pid;
249
250 // The crashing TID set inside the compromised context via sys_gettid()
251 // in ExceptionHandler::HandleSignal is also wrong and needs to be
252 // translated.
253 //
254 // We expect the crashing thread to be in sys_read(), waiting for use to
255 // write to |signal_fd|. Most newer kernels where we have the different pid
256 // namespaces also have /proc/[pid]/syscall, so we can look through
257 // |actual_crashing_pid|'s thread group and find the thread that's in the
258 // read syscall with the right arguments.
259
260 std::string expected_syscall_data;
261 // /proc/[pid]/syscall is formatted as follows:
262 // syscall_number arg1 ... arg6 sp pc
263 // but we just check syscall_number through arg3.
[email protected]a77fa2dc2010-11-15 12:11:11264 base::StringAppendF(&expected_syscall_data, "%d 0x%x %p 0x1 ",
265 SYS_read, tid_fd, tid_buf_addr);
[email protected]662183142010-07-16 19:28:17266 pid_t crashing_tid =
267 base::FindThreadIDWithSyscall(crashing_pid, expected_syscall_data);
268 if (crashing_tid == -1) {
269 // We didn't find the thread we want. Maybe it didn't reach sys_read()
270 // yet, or the kernel doesn't support /proc/[pid]/syscall or the thread
271 // went away. We'll just take a guess here and assume the crashing
272 // thread is the thread group leader.
273 crashing_tid = crashing_pid;
274 }
275
276 ExceptionHandler::CrashContext* bad_context =
277 reinterpret_cast<ExceptionHandler::CrashContext*>(crash_context);
278 bad_context->tid = crashing_tid;
279 }
[email protected]4378a822009-07-08 01:15:14280
[email protected]cbd5fd52009-08-26 00:14:27281 bool upload = true;
282 FilePath dumps_path("/tmp");
[email protected]b064f0eb2010-09-02 23:53:26283 PathService::Get(base::DIR_TEMP, &dumps_path);
[email protected]c83dd912010-04-06 18:50:51284 if (getenv(env_vars::kHeadless)) {
[email protected]cbd5fd52009-08-26 00:14:27285 upload = false;
286 PathService::Get(chrome::DIR_CRASH_DUMPS, &dumps_path);
287 }
[email protected]9a5d2a52009-05-22 03:37:45288 const uint64 rand = base::RandUint64();
289 const std::string minidump_filename =
[email protected]2456c572009-11-09 04:21:51290 StringPrintf("%s/chromium-%s-minidump-%016" PRIx64 ".dmp",
291 dumps_path.value().c_str(), process_type_.c_str(), rand);
[email protected]9a5d2a52009-05-22 03:37:45292 if (!google_breakpad::WriteMinidump(minidump_filename.c_str(),
[email protected]2eb41e72009-07-15 23:07:34293 crashing_pid, crash_context,
[email protected]9a5d2a52009-05-22 03:37:45294 kCrashContextSize)) {
295 LOG(ERROR) << "Failed to write crash dump for pid " << crashing_pid;
296 HANDLE_EINTR(close(signal_fd));
297 }
298
[email protected]2456c572009-11-09 04:21:51299 // Send the done signal to the process: it can exit now.
[email protected]9a5d2a52009-05-22 03:37:45300 memset(&msg, 0, sizeof(msg));
[email protected]2eb41e72009-07-15 23:07:34301 struct iovec done_iov;
302 done_iov.iov_base = const_cast<char*>("\x42");
303 done_iov.iov_len = 1;
304 msg.msg_iov = &done_iov;
[email protected]9a5d2a52009-05-22 03:37:45305 msg.msg_iovlen = 1;
306
307 HANDLE_EINTR(sendmsg(signal_fd, &msg, MSG_DONTWAIT | MSG_NOSIGNAL));
308 HANDLE_EINTR(close(signal_fd));
309
[email protected]9ddbcd92009-09-23 21:27:43310 // Sanitize the string data a bit more
311 guid[kGuidSize] = crash_url[kMaxActiveURLSize] = distro[kDistroSize] = 0;
312
[email protected]b064f0eb2010-09-02 23:53:26313 BreakpadInfo* info = new BreakpadInfo;
314
315 char* minidump_filename_str = new char[minidump_filename.length() + 1];
316 minidump_filename.copy(minidump_filename_str, minidump_filename.length());
317 minidump_filename_str[minidump_filename.length()] = '\0';
318 info->filename = minidump_filename_str;
319
320 info->process_type_length = process_type_.length();
321 char* process_type_str = new char[info->process_type_length + 1];
322 process_type_.copy(process_type_str, info->process_type_length);
323 process_type_str[info->process_type_length] = '\0';
324 info->process_type = process_type_str;
325
326 info->crash_url_length = strlen(crash_url);
327 info->crash_url = crash_url;
328
329 info->guid_length = strlen(guid);
330 info->guid = guid;
331
332 info->distro_length = strlen(distro);
333 info->distro = distro;
334
335 info->upload = upload;
[email protected]c7b1d2f2010-12-03 03:33:13336 info->process_start_time = uptime;
[email protected]b064f0eb2010-09-02 23:53:26337
338 uploader_thread_->message_loop()->PostTask(
339 FROM_HERE,
[email protected]ca779662010-11-11 23:28:43340 NewRunnableFunction(&CrashDumpTask, this, info));
[email protected]9a5d2a52009-05-22 03:37:45341}
342
[email protected]2456c572009-11-09 04:21:51343void CrashHandlerHostLinux::WillDestroyCurrentMessageLoop() {
[email protected]9a5d2a52009-05-22 03:37:45344 file_descriptor_watcher_.StopWatchingFileDescriptor();
[email protected]ca779662010-11-11 23:28:43345
346 // If we are quitting and there are crash dumps in the queue, turn them into
347 // no-ops.
348 shutting_down_ = true;
349 uploader_thread_->Stop();
350}
351
352bool CrashHandlerHostLinux::IsShuttingDown() const {
353 return shutting_down_;
[email protected]9a5d2a52009-05-22 03:37:45354}
[email protected]b064f0eb2010-09-02 23:53:26355
356PluginCrashHandlerHostLinux::PluginCrashHandlerHostLinux() {
[email protected]19eef062010-09-16 19:44:09357 InitCrashUploaderThread();
[email protected]b064f0eb2010-09-02 23:53:26358}
359
360PluginCrashHandlerHostLinux::~PluginCrashHandlerHostLinux() {
361}
362
363void PluginCrashHandlerHostLinux::SetProcessType() {
364 process_type_ = "plugin";
365}
366
367RendererCrashHandlerHostLinux::RendererCrashHandlerHostLinux() {
[email protected]19eef062010-09-16 19:44:09368 InitCrashUploaderThread();
[email protected]b064f0eb2010-09-02 23:53:26369}
370
371RendererCrashHandlerHostLinux::~RendererCrashHandlerHostLinux() {
372}
373
374void RendererCrashHandlerHostLinux::SetProcessType() {
375 process_type_ = "renderer";
376}