blob: 61ffd7d1f059adc91f89f6e92bfe3583a367c36b [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]b07fc5112009-12-02 01:55:0623#include "breakpad/src/client/linux/handler/exception_handler.h"
24#include "breakpad/src/client/linux/minidump_writer/linux_dumper.h"
25#include "breakpad/src/client/linux/minidump_writer/minidump_writer.h"
[email protected]9a5d2a52009-05-22 03:37:4526#include "chrome/app/breakpad_linux.h"
27#include "chrome/browser/chrome_thread.h"
[email protected]cbd5fd52009-08-26 00:14:2728#include "chrome/common/chrome_paths.h"
[email protected]99ca9a12010-03-12 18:32:1029#include "chrome/common/env_vars.h"
[email protected]9a5d2a52009-05-22 03:37:4530
[email protected]662183142010-07-16 19:28:1731using google_breakpad::ExceptionHandler;
32
[email protected]2456c572009-11-09 04:21:5133// Since classes derived from CrashHandlerHostLinux are singletons, it's only
34// destroyed at the end of the processes lifetime, which is greater in span than
35// the lifetime of the IO message loop.
[email protected]c56428f22010-06-16 02:17:2336DISABLE_RUNNABLE_METHOD_REFCOUNT(CrashHandlerHostLinux);
[email protected]9a5d2a52009-05-22 03:37:4537
[email protected]2456c572009-11-09 04:21:5138CrashHandlerHostLinux::CrashHandlerHostLinux()
39 : process_socket_(-1),
[email protected]9a5d2a52009-05-22 03:37:4540 browser_socket_(-1) {
41 int fds[2];
[email protected]2456c572009-11-09 04:21:5142 // We use SOCK_SEQPACKET rather than SOCK_DGRAM to prevent the process from
[email protected]54730a12009-10-07 22:55:4843 // sending datagrams to other sockets on the system. The sandbox may prevent
[email protected]2456c572009-11-09 04:21:5144 // the process from calling socket() to create new sockets, but it'll still
[email protected]54730a12009-10-07 22:55:4845 // inherit some sockets. With PF_UNIX+SOCK_DGRAM, it can call sendmsg to send
46 // a datagram to any (abstract) socket on the same system. With
47 // SOCK_SEQPACKET, this is prevented.
[email protected]c83dd912010-04-06 18:50:5148 CHECK_EQ(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds), 0);
[email protected]9a5d2a52009-05-22 03:37:4549 static const int on = 1;
50
51 // Enable passcred on the server end of the socket
[email protected]c83dd912010-04-06 18:50:5152 CHECK_EQ(setsockopt(fds[1], SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)), 0);
[email protected]9a5d2a52009-05-22 03:37:4553
[email protected]2456c572009-11-09 04:21:5154 process_socket_ = fds[0];
[email protected]9a5d2a52009-05-22 03:37:4555 browser_socket_ = fds[1];
56
[email protected]45c9ad072009-10-27 04:34:3157 ChromeThread::PostTask(
58 ChromeThread::IO, FROM_HERE,
[email protected]2456c572009-11-09 04:21:5159 NewRunnableMethod(this, &CrashHandlerHostLinux::Init));
[email protected]9a5d2a52009-05-22 03:37:4560}
61
[email protected]2456c572009-11-09 04:21:5162CrashHandlerHostLinux::~CrashHandlerHostLinux() {
63 HANDLE_EINTR(close(process_socket_));
[email protected]9a5d2a52009-05-22 03:37:4564 HANDLE_EINTR(close(browser_socket_));
65}
66
[email protected]2456c572009-11-09 04:21:5167void CrashHandlerHostLinux::Init() {
[email protected]9a5d2a52009-05-22 03:37:4568 MessageLoopForIO* ml = MessageLoopForIO::current();
69 CHECK(ml->WatchFileDescriptor(
70 browser_socket_, true /* persistent */,
71 MessageLoopForIO::WATCH_READ,
72 &file_descriptor_watcher_, this));
73 ml->AddDestructionObserver(this);
74}
75
[email protected]2456c572009-11-09 04:21:5176void CrashHandlerHostLinux::OnFileCanWriteWithoutBlocking(int fd) {
[email protected]9a5d2a52009-05-22 03:37:4577 DCHECK(false);
78}
79
[email protected]2456c572009-11-09 04:21:5180void CrashHandlerHostLinux::OnFileCanReadWithoutBlocking(int fd) {
[email protected]9a5d2a52009-05-22 03:37:4581 DCHECK_EQ(fd, browser_socket_);
82
[email protected]2456c572009-11-09 04:21:5183 // A process has crashed and has signaled us by writing a datagram
[email protected]9a5d2a52009-05-22 03:37:4584 // to the death signal socket. The datagram contains the crash context needed
85 // for writing the minidump as well as a file descriptor and a credentials
86 // block so that they can't lie about their pid.
87
88 // The length of the control message:
89 static const unsigned kControlMsgSize =
90 CMSG_SPACE(sizeof(int)) + CMSG_SPACE(sizeof(struct ucred));
91 // The length of the regular payload:
92 static const unsigned kCrashContextSize =
[email protected]662183142010-07-16 19:28:1793 sizeof(ExceptionHandler::CrashContext);
[email protected]9a5d2a52009-05-22 03:37:4594
95 struct msghdr msg = {0};
[email protected]662183142010-07-16 19:28:1796 struct iovec iov[6];
[email protected]2eb41e72009-07-15 23:07:3497 char crash_context[kCrashContextSize];
98 char guid[kGuidSize + 1];
99 char crash_url[kMaxActiveURLSize + 1];
[email protected]912c6452009-07-17 05:55:51100 char distro[kDistroSize + 1];
[email protected]662183142010-07-16 19:28:17101 char* tid_buf_addr = NULL;
102 int tid_fd = -1;
[email protected]9a5d2a52009-05-22 03:37:45103 char control[kControlMsgSize];
[email protected]2eb41e72009-07-15 23:07:34104 const ssize_t expected_msg_size = sizeof(crash_context) + sizeof(guid) +
[email protected]662183142010-07-16 19:28:17105 sizeof(crash_url) + sizeof(distro) +
106 sizeof(tid_buf_addr) + sizeof(tid_fd);
[email protected]2eb41e72009-07-15 23:07:34107
108 iov[0].iov_base = crash_context;
109 iov[0].iov_len = sizeof(crash_context);
110 iov[1].iov_base = guid;
111 iov[1].iov_len = sizeof(guid);
112 iov[2].iov_base = crash_url;
113 iov[2].iov_len = sizeof(crash_url);
[email protected]912c6452009-07-17 05:55:51114 iov[3].iov_base = distro;
115 iov[3].iov_len = sizeof(distro);
[email protected]662183142010-07-16 19:28:17116 iov[4].iov_base = &tid_buf_addr;
117 iov[4].iov_len = sizeof(tid_buf_addr);
118 iov[5].iov_base = &tid_fd;
119 iov[5].iov_len = sizeof(tid_fd);
[email protected]2eb41e72009-07-15 23:07:34120 msg.msg_iov = iov;
[email protected]662183142010-07-16 19:28:17121 msg.msg_iovlen = 6;
[email protected]9a5d2a52009-05-22 03:37:45122 msg.msg_control = control;
123 msg.msg_controllen = kControlMsgSize;
124
[email protected]2eb41e72009-07-15 23:07:34125 const ssize_t msg_size = HANDLE_EINTR(recvmsg(browser_socket_, &msg, 0));
126 if (msg_size != expected_msg_size) {
[email protected]9a5d2a52009-05-22 03:37:45127 LOG(ERROR) << "Error reading from death signal socket. Crash dumping"
128 << " is disabled."
[email protected]2eb41e72009-07-15 23:07:34129 << " msg_size:" << msg_size
[email protected]9a5d2a52009-05-22 03:37:45130 << " errno:" << errno;
131 file_descriptor_watcher_.StopWatchingFileDescriptor();
132 return;
133 }
134
[email protected]2eb41e72009-07-15 23:07:34135 if (msg.msg_controllen != kControlMsgSize ||
[email protected]9a5d2a52009-05-22 03:37:45136 msg.msg_flags & ~MSG_TRUNC) {
137 LOG(ERROR) << "Received death signal message with the wrong size;"
[email protected]9a5d2a52009-05-22 03:37:45138 << " msg.msg_controllen:" << msg.msg_controllen
139 << " msg.msg_flags:" << msg.msg_flags
140 << " kCrashContextSize:" << kCrashContextSize
141 << " kControlMsgSize:" << kControlMsgSize;
142 return;
143 }
144
[email protected]9a5d2a52009-05-22 03:37:45145 // Walk the control payload an extract the file descriptor and validated pid.
146 pid_t crashing_pid = -1;
147 int signal_fd = -1;
148 for (struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); hdr;
149 hdr = CMSG_NXTHDR(&msg, hdr)) {
150 if (hdr->cmsg_level != SOL_SOCKET)
151 continue;
152 if (hdr->cmsg_type == SCM_RIGHTS) {
153 const unsigned len = hdr->cmsg_len -
154 (((uint8_t*)CMSG_DATA(hdr)) - (uint8_t*)hdr);
[email protected]912c6452009-07-17 05:55:51155 DCHECK_EQ(len % sizeof(int), 0u);
[email protected]9a5d2a52009-05-22 03:37:45156 const unsigned num_fds = len / sizeof(int);
157 if (num_fds > 1 || num_fds == 0) {
[email protected]2456c572009-11-09 04:21:51158 // A nasty process could try and send us too many descriptors and
[email protected]9a5d2a52009-05-22 03:37:45159 // force a leak.
160 LOG(ERROR) << "Death signal contained too many descriptors;"
161 << " num_fds:" << num_fds;
162 for (unsigned i = 0; i < num_fds; ++i)
163 HANDLE_EINTR(close(reinterpret_cast<int*>(CMSG_DATA(hdr))[i]));
164 return;
165 } else {
166 signal_fd = reinterpret_cast<int*>(CMSG_DATA(hdr))[0];
167 }
168 } else if (hdr->cmsg_type == SCM_CREDENTIALS) {
169 const struct ucred *cred =
170 reinterpret_cast<struct ucred*>(CMSG_DATA(hdr));
171 crashing_pid = cred->pid;
172 }
173 }
174
175 if (crashing_pid == -1 || signal_fd == -1) {
176 LOG(ERROR) << "Death signal message didn't contain all expected control"
177 << " messages";
178 if (signal_fd)
179 HANDLE_EINTR(close(signal_fd));
180 return;
181 }
182
[email protected]4378a822009-07-08 01:15:14183 // Kernel bug workaround (broken in 2.6.30 at least):
184 // The kernel doesn't translate PIDs in SCM_CREDENTIALS across PID
185 // namespaces. Thus |crashing_pid| might be garbage from our point of view.
186 // In the future we can remove this workaround, but we have to wait a couple
187 // of years to be sure that it's worked its way out into the world.
188
189 uint64_t inode_number;
[email protected]85ebe8f2009-10-29 04:02:55190 if (!base::FileDescriptorGetInode(&inode_number, signal_fd)) {
[email protected]4378a822009-07-08 01:15:14191 LOG(WARNING) << "Failed to get inode number for passed socket";
192 HANDLE_EINTR(close(signal_fd));
193 return;
194 }
195
[email protected]662183142010-07-16 19:28:17196 pid_t actual_crashing_pid = -1;
197 if (!base::FindProcessHoldingSocket(&actual_crashing_pid, inode_number - 1)) {
[email protected]4378a822009-07-08 01:15:14198 LOG(WARNING) << "Failed to find process holding other end of crash reply "
199 "socket";
200 HANDLE_EINTR(close(signal_fd));
201 return;
202 }
[email protected]662183142010-07-16 19:28:17203 if (actual_crashing_pid != crashing_pid) {
204 crashing_pid = actual_crashing_pid;
205
206 // The crashing TID set inside the compromised context via sys_gettid()
207 // in ExceptionHandler::HandleSignal is also wrong and needs to be
208 // translated.
209 //
210 // We expect the crashing thread to be in sys_read(), waiting for use to
211 // write to |signal_fd|. Most newer kernels where we have the different pid
212 // namespaces also have /proc/[pid]/syscall, so we can look through
213 // |actual_crashing_pid|'s thread group and find the thread that's in the
214 // read syscall with the right arguments.
215
216 std::string expected_syscall_data;
217 // /proc/[pid]/syscall is formatted as follows:
218 // syscall_number arg1 ... arg6 sp pc
219 // but we just check syscall_number through arg3.
220 StringAppendF(&expected_syscall_data, "%d 0x%x %p 0x1 ",
221 SYS_read, tid_fd, tid_buf_addr);
222 pid_t crashing_tid =
223 base::FindThreadIDWithSyscall(crashing_pid, expected_syscall_data);
224 if (crashing_tid == -1) {
225 // We didn't find the thread we want. Maybe it didn't reach sys_read()
226 // yet, or the kernel doesn't support /proc/[pid]/syscall or the thread
227 // went away. We'll just take a guess here and assume the crashing
228 // thread is the thread group leader.
229 crashing_tid = crashing_pid;
230 }
231
232 ExceptionHandler::CrashContext* bad_context =
233 reinterpret_cast<ExceptionHandler::CrashContext*>(crash_context);
234 bad_context->tid = crashing_tid;
235 }
[email protected]4378a822009-07-08 01:15:14236
[email protected]cbd5fd52009-08-26 00:14:27237 bool upload = true;
238 FilePath dumps_path("/tmp");
[email protected]c83dd912010-04-06 18:50:51239 if (getenv(env_vars::kHeadless)) {
[email protected]cbd5fd52009-08-26 00:14:27240 upload = false;
241 PathService::Get(chrome::DIR_CRASH_DUMPS, &dumps_path);
242 }
[email protected]9a5d2a52009-05-22 03:37:45243 const uint64 rand = base::RandUint64();
244 const std::string minidump_filename =
[email protected]2456c572009-11-09 04:21:51245 StringPrintf("%s/chromium-%s-minidump-%016" PRIx64 ".dmp",
246 dumps_path.value().c_str(), process_type_.c_str(), rand);
[email protected]9a5d2a52009-05-22 03:37:45247 if (!google_breakpad::WriteMinidump(minidump_filename.c_str(),
[email protected]2eb41e72009-07-15 23:07:34248 crashing_pid, crash_context,
[email protected]9a5d2a52009-05-22 03:37:45249 kCrashContextSize)) {
250 LOG(ERROR) << "Failed to write crash dump for pid " << crashing_pid;
251 HANDLE_EINTR(close(signal_fd));
252 }
253
[email protected]2456c572009-11-09 04:21:51254 // Send the done signal to the process: it can exit now.
[email protected]9a5d2a52009-05-22 03:37:45255 memset(&msg, 0, sizeof(msg));
[email protected]2eb41e72009-07-15 23:07:34256 struct iovec done_iov;
257 done_iov.iov_base = const_cast<char*>("\x42");
258 done_iov.iov_len = 1;
259 msg.msg_iov = &done_iov;
[email protected]9a5d2a52009-05-22 03:37:45260 msg.msg_iovlen = 1;
261
262 HANDLE_EINTR(sendmsg(signal_fd, &msg, MSG_DONTWAIT | MSG_NOSIGNAL));
263 HANDLE_EINTR(close(signal_fd));
264
[email protected]9ddbcd92009-09-23 21:27:43265 // Sanitize the string data a bit more
266 guid[kGuidSize] = crash_url[kMaxActiveURLSize] = distro[kDistroSize] = 0;
267
[email protected]912c6452009-07-17 05:55:51268 BreakpadInfo info;
269 info.filename = minidump_filename.c_str();
[email protected]2456c572009-11-09 04:21:51270 info.process_type = process_type_.c_str();
271 info.process_type_length = process_type_.length();
[email protected]912c6452009-07-17 05:55:51272 info.crash_url = crash_url;
273 info.crash_url_length = strlen(crash_url);
274 info.guid = guid;
275 info.guid_length = strlen(guid);
276 info.distro = distro;
277 info.distro_length = strlen(distro);
[email protected]cbd5fd52009-08-26 00:14:27278 info.upload = upload;
279 HandleCrashDump(info);
[email protected]9a5d2a52009-05-22 03:37:45280}
281
[email protected]2456c572009-11-09 04:21:51282void CrashHandlerHostLinux::WillDestroyCurrentMessageLoop() {
[email protected]9a5d2a52009-05-22 03:37:45283 file_descriptor_watcher_.StopWatchingFileDescriptor();
284}