| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 | // The ExceptionHandler object installs signal handlers for a number of |
| 31 | // signals. We rely on the signal handler running on the thread which crashed |
| 32 | // in order to identify it. This is true of the synchronous signals (SEGV etc), |
| 33 | // but not true of ABRT. Thus, if you send ABRT to yourself in a program which |
| 34 | // uses ExceptionHandler, you need to use tgkill to direct it to the current |
| 35 | // thread. |
| 36 | // |
| 37 | // The signal flow looks like this: |
| 38 | // |
| 39 | // SignalHandler (uses a global stack of ExceptionHandler objects to find |
| 40 | // | one to handle the signal. If the first rejects it, try |
| 41 | // | the second etc...) |
| 42 | // V |
| 43 | // HandleSignal ----------------------------| (clones a new process which |
| 44 | // | | shares an address space with |
| 45 | // (wait for cloned | the crashed process. This |
| 46 | // process) | allows us to ptrace the crashed |
| 47 | // | | process) |
| 48 | // V V |
| 49 | // (set signal handler to ThreadEntry (static function to bounce |
| 50 | // SIG_DFL and rethrow, | back into the object) |
| 51 | // killing the crashed | |
| 52 | // process) V |
| 53 | // DoDump (writes minidump) |
| 54 | // | |
| 55 | // V |
| 56 | // sys_exit |
| 57 | // |
| 58 |
| 59 | // This code is a little fragmented. Different functions of the ExceptionHandler |
| 60 | // class run in a number of different contexts. Some of them run in a normal |
| 61 | // context and are easy to code, others run in a compromised context and the |
| 62 | // restrictions at the top of minidump_writer.cc apply: no libc and use the |
| 63 | // alternative malloc. Each function should have comment above it detailing the |
| 64 | // context which it runs in. |
| 65 |
| 66 | #include "client/linux/handler/exception_handler.h" |
| 67 |
| 68 | #include <errno.h> |
| 69 | #include <fcntl.h> |
| 70 | #include <linux/limits.h> |
| 71 | #include <pthread.h> |
| 72 | #include <sched.h> |
| 73 | #include <signal.h> |
| 74 | #include <stdio.h> |
| 75 | #include <sys/mman.h> |
| 76 | #include <sys/prctl.h> |
| 77 | #include <sys/syscall.h> |
| 78 | #include <sys/wait.h> |
| 79 | #include <unistd.h> |
| 80 |
| 81 | #include <sys/signal.h> |
| 82 | #include <sys/ucontext.h> |
| 83 | #include <sys/user.h> |
| 84 | #include <ucontext.h> |
| 85 |
| 86 | #include <algorithm> |
| 87 | #include <utility> |
| 88 | #include <vector> |
| 89 |
| 90 | #include "common/basictypes.h" |
| 91 | #include "common/linux/linux_libc_support.h" |
| 92 | #include "common/memory.h" |
| 93 | #include "client/linux/log/log.h" |
| 94 | #include "client/linux/microdump_writer/microdump_writer.h" |
| 95 | #include "client/linux/minidump_writer/linux_dumper.h" |
| 96 | #include "client/linux/minidump_writer/minidump_writer.h" |
| 97 | #include "common/linux/eintr_wrapper.h" |
| 98 | #include "third_party/lss/linux_syscall_support.h" |
| 99 |
| 100 | #if defined(__ANDROID__) |
| 101 | #include "linux/sched.h" |
| 102 | #endif |
| 103 |
| 104 | #ifndef PR_SET_PTRACER |
| 105 | #define PR_SET_PTRACER 0x59616d61 |
| 106 | #endif |
| 107 |
| 108 | // A wrapper for the tgkill syscall: send a signal to a specific thread. |
| 109 | static int tgkill(pid_t tgid, pid_t tid, int sig) { |
| 110 | return syscall(__NR_tgkill, tgid, tid, sig); |
| 111 | return 0; |
| 112 | } |
| 113 |
| 114 | namespace google_breakpad { |
| 115 |
| 116 | namespace { |
| 117 | // The list of signals which we consider to be crashes. The default action for |
| 118 | // all these signals must be Core (see man 7 signal) because we rethrow the |
| 119 | // signal after handling it and expect that it'll be fatal. |
| 120 | const int kExceptionSignals[] = { |
| 121 | SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS |
| 122 | }; |
| 123 | const int kNumHandledSignals = |
| 124 | sizeof(kExceptionSignals) / sizeof(kExceptionSignals[0]); |
| 125 | struct sigaction old_handlers[kNumHandledSignals]; |
| 126 | bool handlers_installed = false; |
| 127 |
| 128 | // InstallAlternateStackLocked will store the newly installed stack in new_stack |
| 129 | // and (if it exists) the previously installed stack in old_stack. |
| 130 | stack_t old_stack; |
| 131 | stack_t new_stack; |
| 132 | bool stack_installed = false; |
| 133 |
| 134 | // Create an alternative stack to run the signal handlers on. This is done since |
| 135 | // the signal might have been caused by a stack overflow. |
| 136 | // Runs before crashing: normal context. |
| 137 | void InstallAlternateStackLocked() { |
| 138 | if (stack_installed) |
| 139 | return; |
| 140 |
| 141 | memset(&old_stack, 0, sizeof(old_stack)); |
| 142 | memset(&new_stack, 0, sizeof(new_stack)); |
| 143 |
| 144 | // SIGSTKSZ may be too small to prevent the signal handlers from overrunning |
| 145 | // the alternative stack. Ensure that the size of the alternative stack is |
| 146 | // large enough. |
| 147 | static const unsigned kSigStackSize = std::max(16384, SIGSTKSZ); |
| 148 |
| 149 | // Only set an alternative stack if there isn't already one, or if the current |
| 150 | // one is too small. |
| 151 | if (sys_sigaltstack(NULL, &old_stack) == -1 || !old_stack.ss_sp || |
| 152 | old_stack.ss_size < kSigStackSize) { |
| 153 | new_stack.ss_sp = calloc(1, kSigStackSize); |
| 154 | new_stack.ss_size = kSigStackSize; |
| 155 |
| 156 | if (sys_sigaltstack(&new_stack, NULL) == -1) { |
| 157 | free(new_stack.ss_sp); |
| 158 | return; |
| 159 | } |
| 160 | stack_installed = true; |
| 161 | } |
| 162 | } |
| 163 |
| 164 | // Runs before crashing: normal context. |
| 165 | void RestoreAlternateStackLocked() { |
| 166 | if (!stack_installed) |
| 167 | return; |
| 168 |
| 169 | stack_t current_stack; |
| 170 | if (sys_sigaltstack(NULL, ¤t_stack) == -1) |
| 171 | return; |
| 172 |
| 173 | // Only restore the old_stack if the current alternative stack is the one |
| 174 | // installed by the call to InstallAlternateStackLocked. |
| 175 | if (current_stack.ss_sp == new_stack.ss_sp) { |
| 176 | if (old_stack.ss_sp) { |
| 177 | if (sys_sigaltstack(&old_stack, NULL) == -1) |
| 178 | return; |
| 179 | } else { |
| 180 | stack_t disable_stack; |
| 181 | disable_stack.ss_flags = SS_DISABLE; |
| 182 | if (sys_sigaltstack(&disable_stack, NULL) == -1) |
| 183 | return; |
| 184 | } |
| 185 | } |
| 186 |
| 187 | free(new_stack.ss_sp); |
| 188 | stack_installed = false; |
| 189 | } |
| 190 |
| 191 | void InstallDefaultHandler(int sig) { |
| 192 | #if defined(__ANDROID__) |
| 193 | // Android L+ expose signal and sigaction symbols that override the system |
| 194 | // ones. There is a bug in these functions where a request to set the handler |
| 195 | // to SIG_DFL is ignored. In that case, an infinite loop is entered as the |
| 196 | // signal is repeatedly sent to breakpad's signal handler. |
| 197 | // To work around this, directly call the system's sigaction. |
| 198 | struct kernel_sigaction sa; |
| 199 | memset(&sa, 0, sizeof(sa)); |
| 200 | sys_sigemptyset(&sa.sa_mask); |
| 201 | sa.sa_handler_ = SIG_DFL; |
| 202 | sa.sa_flags = SA_RESTART; |
| 203 | sys_rt_sigaction(sig, &sa, NULL, sizeof(kernel_sigset_t)); |
| 204 | #else |
| 205 | signal(sig, SIG_DFL); |
| 206 | #endif |
| 207 | } |
| 208 |
| 209 | // The global exception handler stack. This is needed because there may exist |
| 210 | // multiple ExceptionHandler instances in a process. Each will have itself |
| 211 | // registered in this stack. |
| 212 | std::vector<ExceptionHandler*>* g_handler_stack_ = NULL; |
| 213 | pthread_mutex_t g_handler_stack_mutex_ = PTHREAD_MUTEX_INITIALIZER; |
| 214 |
| 215 | } // namespace |
| 216 |
| 217 | // Runs before crashing: normal context. |
| 218 | ExceptionHandler::ExceptionHandler(const MinidumpDescriptor& descriptor, |
| 219 | FilterCallback filter, |
| 220 | MinidumpCallback callback, |
| 221 | void* callback_context, |
| 222 | bool install_handler, |
| 223 | const int server_fd) |
| 224 | : filter_(filter), |
| 225 | callback_(callback), |
| 226 | callback_context_(callback_context), |
| 227 | minidump_descriptor_(descriptor), |
| 228 | crash_handler_(NULL) { |
| 229 | if (server_fd >= 0) |
| 230 | crash_generation_client_.reset(CrashGenerationClient::TryCreate(server_fd)); |
| 231 |
| 232 | if (!IsOutOfProcess() && !minidump_descriptor_.IsFD() && |
| 233 | !minidump_descriptor_.IsMicrodumpOnConsole()) |
| 234 | minidump_descriptor_.UpdatePath(); |
| 235 |
| 236 | #if defined(__ANDROID__) |
| 237 | if (minidump_descriptor_.IsMicrodumpOnConsole()) |
| 238 | logger::initializeCrashLogWriter(); |
| 239 | #endif |
| 240 |
| 241 | pthread_mutex_lock(&g_handler_stack_mutex_); |
| 242 | if (!g_handler_stack_) |
| 243 | g_handler_stack_ = new std::vector<ExceptionHandler*>; |
| 244 | if (install_handler) { |
| 245 | InstallAlternateStackLocked(); |
| 246 | InstallHandlersLocked(); |
| 247 | } |
| 248 | g_handler_stack_->push_back(this); |
| 249 | pthread_mutex_unlock(&g_handler_stack_mutex_); |
| 250 | } |
| 251 |
| 252 | // Runs before crashing: normal context. |
| 253 | ExceptionHandler::~ExceptionHandler() { |
| 254 | pthread_mutex_lock(&g_handler_stack_mutex_); |
| 255 | std::vector<ExceptionHandler*>::iterator handler = |
| 256 | std::find(g_handler_stack_->begin(), g_handler_stack_->end(), this); |
| 257 | g_handler_stack_->erase(handler); |
| 258 | if (g_handler_stack_->empty()) { |
| 259 | delete g_handler_stack_; |
| 260 | g_handler_stack_ = NULL; |
| 261 | RestoreAlternateStackLocked(); |
| 262 | RestoreHandlersLocked(); |
| 263 | } |
| 264 | pthread_mutex_unlock(&g_handler_stack_mutex_); |
| 265 | } |
| 266 |
| 267 | // Runs before crashing: normal context. |
| 268 | // static |
| 269 | bool ExceptionHandler::InstallHandlersLocked() { |
| 270 | if (handlers_installed) |
| 271 | return false; |
| 272 |
| 273 | // Fail if unable to store all the old handlers. |
| 274 | for (int i = 0; i < kNumHandledSignals; ++i) { |
| 275 | if (sigaction(kExceptionSignals[i], NULL, &old_handlers[i]) == -1) |
| 276 | return false; |
| 277 | } |
| 278 |
| 279 | struct sigaction sa; |
| 280 | memset(&sa, 0, sizeof(sa)); |
| 281 | sigemptyset(&sa.sa_mask); |
| 282 |
| 283 | // Mask all exception signals when we're handling one of them. |
| 284 | for (int i = 0; i < kNumHandledSignals; ++i) |
| 285 | sigaddset(&sa.sa_mask, kExceptionSignals[i]); |
| 286 |
| 287 | sa.sa_sigaction = SignalHandler; |
| 288 | sa.sa_flags = SA_ONSTACK | SA_SIGINFO; |
| 289 |
| 290 | for (int i = 0; i < kNumHandledSignals; ++i) { |
| 291 | if (sigaction(kExceptionSignals[i], &sa, NULL) == -1) { |
| 292 | // At this point it is impractical to back out changes, and so failure to |
| 293 | // install a signal is intentionally ignored. |
| 294 | } |
| 295 | } |
| 296 | handlers_installed = true; |
| 297 | return true; |
| 298 | } |
| 299 |
| 300 | // This function runs in a compromised context: see the top of the file. |
| 301 | // Runs on the crashing thread. |
| 302 | // static |
| 303 | void ExceptionHandler::RestoreHandlersLocked() { |
| 304 | if (!handlers_installed) |
| 305 | return; |
| 306 |
| 307 | for (int i = 0; i < kNumHandledSignals; ++i) { |
| 308 | if (sigaction(kExceptionSignals[i], &old_handlers[i], NULL) == -1) { |
| 309 | InstallDefaultHandler(kExceptionSignals[i]); |
| 310 | } |
| 311 | } |
| 312 | handlers_installed = false; |
| 313 | } |
| 314 |
| 315 | // void ExceptionHandler::set_crash_handler(HandlerCallback callback) { |
| 316 | // crash_handler_ = callback; |
| 317 | // } |
| 318 |
| 319 | // This function runs in a compromised context: see the top of the file. |
| 320 | // Runs on the crashing thread. |
| 321 | // static |
| 322 | void ExceptionHandler::SignalHandler(int sig, siginfo_t* info, void* uc) { |
| 323 | // All the exception signals are blocked at this point. |
| 324 | pthread_mutex_lock(&g_handler_stack_mutex_); |
| 325 |
| 326 | // Sometimes, Breakpad runs inside a process where some other buggy code |
| 327 | // saves and restores signal handlers temporarily with 'signal' |
| 328 | // instead of 'sigaction'. This loses the SA_SIGINFO flag associated |
| 329 | // with this function. As a consequence, the values of 'info' and 'uc' |
| 330 | // become totally bogus, generally inducing a crash. |
| 331 | // |
| 332 | // The following code tries to detect this case. When it does, it |
| 333 | // resets the signal handlers with sigaction + SA_SIGINFO and returns. |
| 334 | // This forces the signal to be thrown again, but this time the kernel |
| 335 | // will call the function with the right arguments. |
| 336 | struct sigaction cur_handler; |
| 337 | if (sigaction(sig, NULL, &cur_handler) == 0 && |
| 338 | (cur_handler.sa_flags & SA_SIGINFO) == 0) { |
| 339 | // Reset signal handler with the right flags. |
| 340 | sigemptyset(&cur_handler.sa_mask); |
| 341 | sigaddset(&cur_handler.sa_mask, sig); |
| 342 |
| 343 | cur_handler.sa_sigaction = SignalHandler; |
| 344 | cur_handler.sa_flags = SA_ONSTACK | SA_SIGINFO; |
| 345 |
| 346 | if (sigaction(sig, &cur_handler, NULL) == -1) { |
| 347 | // When resetting the handler fails, try to reset the |
| 348 | // default one to avoid an infinite loop here. |
| 349 | InstallDefaultHandler(sig); |
| 350 | } |
| 351 | pthread_mutex_unlock(&g_handler_stack_mutex_); |
| 352 | return; |
| 353 | } |
| 354 |
| 355 | bool handled = false; |
| 356 | for (int i = g_handler_stack_->size() - 1; !handled && i >= 0; --i) { |
| 357 | handled = (*g_handler_stack_)[i]->HandleSignal(sig, info, uc); |
| 358 | } |
| 359 |
| 360 | // Upon returning from this signal handler, sig will become unmasked and then |
| 361 | // it will be retriggered. If one of the ExceptionHandlers handled it |
| 362 | // successfully, restore the default handler. Otherwise, restore the |
| 363 | // previously installed handler. Then, when the signal is retriggered, it will |
| 364 | // be delivered to the appropriate handler. |
| 365 | if (handled) { |
| 366 | InstallDefaultHandler(sig); |
| 367 | } else { |
| 368 | RestoreHandlersLocked(); |
| 369 | } |
| 370 |
| 371 | pthread_mutex_unlock(&g_handler_stack_mutex_); |
| 372 |
| 373 | // info->si_code <= 0 iff SI_FROMUSER (SI_FROMKERNEL otherwise). |
| 374 | if (info->si_code <= 0 || sig == SIGABRT) { |
| 375 | // This signal was triggered by somebody sending us the signal with kill(). |
| 376 | // In order to retrigger it, we have to queue a new signal by calling |
| 377 | // kill() ourselves. The special case (si_pid == 0 && sig == SIGABRT) is |
| 378 | // due to the kernel sending a SIGABRT from a user request via SysRQ. |
| 379 | if (tgkill(getpid(), syscall(__NR_gettid), sig) < 0) { |
| 380 | // If we failed to kill ourselves (e.g. because a sandbox disallows us |
| 381 | // to do so), we instead resort to terminating our process. This will |
| 382 | // result in an incorrect exit code. |
| 383 | _exit(1); |
| 384 | } |
| 385 | } else { |
| 386 | // This was a synchronous signal triggered by a hard fault (e.g. SIGSEGV). |
| 387 | // No need to reissue the signal. It will automatically trigger again, |
| 388 | // when we return from the signal handler. |
| 389 | } |
| 390 | } |
| 391 |
| 392 | struct ThreadArgument { |
| 393 | pid_t pid; // the crashing process |
| 394 | const MinidumpDescriptor* minidump_descriptor; |
| 395 | ExceptionHandler* handler; |
| 396 | const void* context; // a CrashContext structure |
| 397 | size_t context_size; |
| 398 | }; |
| 399 |
| 400 | // This is the entry function for the cloned process. We are in a compromised |
| 401 | // context here: see the top of the file. |
| 402 | // static |
| 403 | int ExceptionHandler::ThreadEntry(void *arg) { |
| 404 | const ThreadArgument *thread_arg = reinterpret_cast<ThreadArgument*>(arg); |
| 405 |
| 406 | // Block here until the crashing process unblocks us when |
| 407 | // we're allowed to use ptrace |
| 408 | thread_arg->handler->WaitForContinueSignal(); |
| 409 |
| 410 | return thread_arg->handler->DoDump(thread_arg->pid, thread_arg->context, |
| 411 | thread_arg->context_size) == false; |
| 412 | } |
| 413 |
| 414 | // This function runs in a compromised context: see the top of the file. |
| 415 | // Runs on the crashing thread. |
| 416 | bool ExceptionHandler::HandleSignal(int sig, siginfo_t* info, void* uc) { |
| 417 | if (filter_ && !filter_(callback_context_)) |
| 418 | return false; |
| 419 |
| 420 | // Allow ourselves to be dumped if the signal is trusted. |
| 421 | bool signal_trusted = info->si_code > 0; |
| 422 | bool signal_pid_trusted = info->si_code == SI_USER || |
| 423 | info->si_code == SI_TKILL; |
| 424 | if (signal_trusted || (signal_pid_trusted && info->si_pid == getpid())) { |
| 425 | sys_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); |
| 426 | } |
| 427 | CrashContext context; |
| 428 | // Fill in all the holes in the struct to make Valgrind happy. |
| 429 | memset(&context, 0, sizeof(context)); |
| 430 | memcpy(&context.siginfo, info, sizeof(siginfo_t)); |
| 431 | memcpy(&context.context, uc, sizeof(struct ucontext)); |
| 432 | #if defined(__aarch64__) |
| 433 | struct ucontext *uc_ptr = (struct ucontext*)uc; |
| 434 | struct fpsimd_context *fp_ptr = |
| 435 | (struct fpsimd_context*)&uc_ptr->uc_mcontext.__reserved; |
| 436 | if (fp_ptr->head.magic == FPSIMD_MAGIC) { |
| 437 | memcpy(&context.float_state, fp_ptr, sizeof(context.float_state)); |
| 438 | } |
| 439 | #elif !defined(__ARM_EABI__) && !defined(__mips__) |
| 440 | // FP state is not part of user ABI on ARM Linux. |
| 441 | // In case of MIPS Linux FP state is already part of struct ucontext |
| 442 | // and 'float_state' is not a member of CrashContext. |
| 443 | struct ucontext *uc_ptr = (struct ucontext*)uc; |
| 444 | if (uc_ptr->uc_mcontext.fpregs) { |
| 445 | memcpy(&context.float_state, |
| 446 | uc_ptr->uc_mcontext.fpregs, |
| 447 | sizeof(context.float_state)); |
| 448 | } |
| 449 | #endif |
| 450 | context.tid = syscall(__NR_gettid); |
| 451 | if (crash_handler_ != NULL) { |
| 452 | if (crash_handler_(&context, sizeof(context), callback_context_)) { |
| 453 | return true; |
| 454 | } |
| 455 | } |
| 456 | return GenerateDump(&context); |
| 457 | } |
| 458 |
| 459 | // This is a public interface to HandleSignal that allows the client to |
| 460 | // generate a crash dump. This function may run in a compromised context. |
| 461 | bool ExceptionHandler::SimulateSignalDelivery(int sig) { |
| 462 | siginfo_t siginfo = {}; |
| 463 | // Mimic a trusted signal to allow tracing the process (see |
| 464 | // ExceptionHandler::HandleSignal(). |
| 465 | siginfo.si_code = SI_USER; |
| 466 | siginfo.si_pid = getpid(); |
| 467 | struct ucontext context; |
| 468 | getcontext(&context); |
| 469 | return HandleSignal(sig, &siginfo, &context); |
| 470 | } |
| 471 |
| 472 | // This function may run in a compromised context: see the top of the file. |
| 473 | bool ExceptionHandler::GenerateDump(CrashContext *context) { |
| 474 | if (IsOutOfProcess()) |
| 475 | return crash_generation_client_->RequestDump(context, sizeof(*context)); |
| 476 |
| 477 | // Allocating too much stack isn't a problem, and better to err on the side |
| 478 | // of caution than smash it into random locations. |
| 479 | static const unsigned kChildStackSize = 16000; |
| 480 | PageAllocator allocator; |
| 481 | uint8_t* stack = reinterpret_cast<uint8_t*>(allocator.Alloc(kChildStackSize)); |
| 482 | if (!stack) |
| 483 | return false; |
| 484 | // clone() needs the top-most address. (scrub just to be safe) |
| 485 | stack += kChildStackSize; |
| 486 | my_memset(stack - 16, 0, 16); |
| 487 |
| 488 | ThreadArgument thread_arg; |
| 489 | thread_arg.handler = this; |
| 490 | thread_arg.minidump_descriptor = &minidump_descriptor_; |
| 491 | thread_arg.pid = getpid(); |
| 492 | thread_arg.context = context; |
| 493 | thread_arg.context_size = sizeof(*context); |
| 494 |
| 495 | // We need to explicitly enable ptrace of parent processes on some |
| 496 | // kernels, but we need to know the PID of the cloned process before we |
| 497 | // can do this. Create a pipe here which we can use to block the |
| 498 | // cloned process after creating it, until we have explicitly enabled ptrace |
| 499 | if (sys_pipe(fdes) == -1) { |
| 500 | // Creating the pipe failed. We'll log an error but carry on anyway, |
| 501 | // as we'll probably still get a useful crash report. All that will happen |
| 502 | // is the write() and read() calls will fail with EBADF |
| 503 | static const char no_pipe_msg[] = "ExceptionHandler::GenerateDump " |
| 504 | "sys_pipe failed:"; |
| 505 | logger::write(no_pipe_msg, sizeof(no_pipe_msg) - 1); |
| 506 | logger::write(strerror(errno), strlen(strerror(errno))); |
| 507 | logger::write("\n", 1); |
| 508 |
| 509 | // Ensure fdes[0] and fdes[1] are invalid file descriptors. |
| 510 | fdes[0] = fdes[1] = -1; |
| 511 | } |
| 512 |
| 513 | const pid_t child = sys_clone( |
| 514 | ThreadEntry, stack, CLONE_FILES | CLONE_FS | CLONE_UNTRACED, |
| 515 | &thread_arg, NULL, NULL, NULL); |
| 516 | if (child == -1) { |
| 517 | sys_close(fdes[0]); |
| 518 | sys_close(fdes[1]); |
| 519 | return false; |
| 520 | } |
| 521 |
| 522 | // Allow the child to ptrace us |
| 523 | sys_prctl(PR_SET_PTRACER, child, 0, 0, 0); |
| 524 | SendContinueSignalToChild(); |
| 525 | int status; |
| 526 | const int r = HANDLE_EINTR(sys_waitpid(child, &status, __WALL)); |
| 527 |
| 528 | sys_close(fdes[0]); |
| 529 | sys_close(fdes[1]); |
| 530 |
| 531 | if (r == -1) { |
| 532 | static const char msg[] = "ExceptionHandler::GenerateDump waitpid failed:"; |
| 533 | logger::write(msg, sizeof(msg) - 1); |
| 534 | logger::write(strerror(errno), strlen(strerror(errno))); |
| 535 | logger::write("\n", 1); |
| 536 | } |
| 537 |
| 538 | bool success = r != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0; |
| 539 | if (callback_) |
| 540 | success = callback_(minidump_descriptor_, callback_context_, success); |
| 541 | return success; |
| 542 | } |
| 543 |
| 544 | // This function runs in a compromised context: see the top of the file. |
| 545 | void ExceptionHandler::SendContinueSignalToChild() { |
| 546 | static const char okToContinueMessage = 'a'; |
| 547 | int r; |
| 548 | r = HANDLE_EINTR(sys_write(fdes[1], &okToContinueMessage, sizeof(char))); |
| 549 | if (r == -1) { |
| 550 | static const char msg[] = "ExceptionHandler::SendContinueSignalToChild " |
| 551 | "sys_write failed:"; |
| 552 | logger::write(msg, sizeof(msg) - 1); |
| 553 | logger::write(strerror(errno), strlen(strerror(errno))); |
| 554 | logger::write("\n", 1); |
| 555 | } |
| 556 | } |
| 557 |
| 558 | // This function runs in a compromised context: see the top of the file. |
| 559 | // Runs on the cloned process. |
| 560 | void ExceptionHandler::WaitForContinueSignal() { |
| 561 | int r; |
| 562 | char receivedMessage; |
| 563 | r = HANDLE_EINTR(sys_read(fdes[0], &receivedMessage, sizeof(char))); |
| 564 | if (r == -1) { |
| 565 | static const char msg[] = "ExceptionHandler::WaitForContinueSignal " |
| 566 | "sys_read failed:"; |
| 567 | logger::write(msg, sizeof(msg) - 1); |
| 568 | logger::write(strerror(errno), strlen(strerror(errno))); |
| 569 | logger::write("\n", 1); |
| 570 | } |
| 571 | } |
| 572 |
| 573 | // This function runs in a compromised context: see the top of the file. |
| 574 | // Runs on the cloned process. |
| 575 | bool ExceptionHandler::DoDump(pid_t crashing_process, const void* context, |
| 576 | size_t context_size) { |
| 577 | if (minidump_descriptor_.IsMicrodumpOnConsole()) { |
| 578 | return google_breakpad::WriteMicrodump( |
| 579 | crashing_process, |
| 580 | context, |
| 581 | context_size, |
| 582 | mapping_list_, |
| 583 | minidump_descriptor_.microdump_build_fingerprint(), |
| 584 | minidump_descriptor_.microdump_product_info()); |
| 585 | } |
| 586 | if (minidump_descriptor_.IsFD()) { |
| 587 | return google_breakpad::WriteMinidump(minidump_descriptor_.fd(), |
| 588 | minidump_descriptor_.size_limit(), |
| 589 | crashing_process, |
| 590 | context, |
| 591 | context_size, |
| 592 | mapping_list_, |
| 593 | app_memory_list_); |
| 594 | } |
| 595 | return google_breakpad::WriteMinidump(minidump_descriptor_.path(), |
| 596 | minidump_descriptor_.size_limit(), |
| 597 | crashing_process, |
| 598 | context, |
| 599 | context_size, |
| 600 | mapping_list_, |
| 601 | app_memory_list_); |
| 602 | } |
| 603 |
| 604 | // static |
| 605 | bool ExceptionHandler::WriteMinidump(const string& dump_path, |
| 606 | MinidumpCallback callback, |
| 607 | void* callback_context) { |
| 608 | MinidumpDescriptor descriptor(dump_path); |
| 609 | ExceptionHandler eh(descriptor, NULL, callback, callback_context, false, -1); |
| 610 | return eh.WriteMinidump(); |
| 611 | } |
| 612 |
| 613 | // In order to making using EBP to calculate the desired value for ESP |
| 614 | // a valid operation, ensure that this function is compiled with a |
| 615 | // frame pointer using the following attribute. This attribute |
| 616 | // is supported on GCC but not on clang. |
| 617 | #if defined(__i386__) && defined(__GNUC__) && !defined(__clang__) |
| 618 | __attribute__((optimize("no-omit-frame-pointer"))) |
| 619 | #endif |
| 620 | bool ExceptionHandler::WriteMinidump() { |
| 621 | if (!IsOutOfProcess() && !minidump_descriptor_.IsFD() && |
| 622 | !minidump_descriptor_.IsMicrodumpOnConsole()) { |
| 623 | // Update the path of the minidump so that this can be called multiple times |
| 624 | // and new files are created for each minidump. This is done before the |
| 625 | // generation happens, as clients may want to access the MinidumpDescriptor |
| 626 | // after this call to find the exact path to the minidump file. |
| 627 | minidump_descriptor_.UpdatePath(); |
| 628 | } else if (minidump_descriptor_.IsFD()) { |
| 629 | // Reposition the FD to its beginning and resize it to get rid of the |
| 630 | // previous minidump info. |
| 631 | lseek(minidump_descriptor_.fd(), 0, SEEK_SET); |
| 632 | ignore_result(ftruncate(minidump_descriptor_.fd(), 0)); |
| 633 | } |
| 634 |
| 635 | // Allow this process to be dumped. |
| 636 | sys_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); |
| 637 |
| 638 | CrashContext context; |
| 639 | int getcontext_result = getcontext(&context.context); |
| 640 | if (getcontext_result) |
| 641 | return false; |
| 642 |
| 643 | #if defined(__i386__) |
| 644 | // In CPUFillFromUContext in minidumpwriter.cc the stack pointer is retrieved |
| 645 | // from REG_UESP instead of from REG_ESP. REG_UESP is the user stack pointer |
| 646 | // and it only makes sense when running in kernel mode with a different stack |
| 647 | // pointer. When WriteMiniDump is called during normal processing REG_UESP is |
| 648 | // zero which leads to bad minidump files. |
| 649 | if (!context.context.uc_mcontext.gregs[REG_UESP]) { |
| 650 | // If REG_UESP is set to REG_ESP then that includes the stack space for the |
| 651 | // CrashContext object in this function, which is about 128 KB. Since the |
| 652 | // Linux dumper only records 32 KB of stack this would mean that nothing |
| 653 | // useful would be recorded. A better option is to set REG_UESP to REG_EBP, |
| 654 | // perhaps with a small negative offset in case there is any code that |
| 655 | // objects to them being equal. |
| 656 | context.context.uc_mcontext.gregs[REG_UESP] = |
| 657 | context.context.uc_mcontext.gregs[REG_EBP] - 16; |
| 658 | // The stack saving is based off of REG_ESP so it must be set to match the |
| 659 | // new REG_UESP. |
| 660 | context.context.uc_mcontext.gregs[REG_ESP] = |
| 661 | context.context.uc_mcontext.gregs[REG_UESP]; |
| 662 | } |
| 663 | #endif |
| 664 |
| 665 | #if !defined(__ARM_EABI__) && !defined(__aarch64__) && !defined(__mips__) |
| 666 | // FPU state is not part of ARM EABI ucontext_t. |
| 667 | memcpy(&context.float_state, context.context.uc_mcontext.fpregs, |
| 668 | sizeof(context.float_state)); |
| 669 | #endif |
| 670 | context.tid = sys_gettid(); |
| 671 |
| 672 | // Add an exception stream to the minidump for better reporting. |
| 673 | memset(&context.siginfo, 0, sizeof(context.siginfo)); |
| 674 | context.siginfo.si_signo = MD_EXCEPTION_CODE_LIN_DUMP_REQUESTED; |
| 675 | #if defined(__i386__) |
| 676 | context.siginfo.si_addr = |
| 677 | reinterpret_cast<void*>(context.context.uc_mcontext.gregs[REG_EIP]); |
| 678 | #elif defined(__x86_64__) |
| 679 | context.siginfo.si_addr = |
| 680 | reinterpret_cast<void*>(context.context.uc_mcontext.gregs[REG_RIP]); |
| 681 | #elif defined(__arm__) |
| 682 | context.siginfo.si_addr = |
| 683 | reinterpret_cast<void*>(context.context.uc_mcontext.arm_pc); |
| 684 | #elif defined(__aarch64__) |
| 685 | context.siginfo.si_addr = |
| 686 | reinterpret_cast<void*>(context.context.uc_mcontext.pc); |
| 687 | #elif defined(__mips__) |
| 688 | context.siginfo.si_addr = |
| 689 | reinterpret_cast<void*>(context.context.uc_mcontext.pc); |
| 690 | #else |
| 691 | #error "This code has not been ported to your platform yet." |
| 692 | #endif |
| 693 |
| 694 | return GenerateDump(&context); |
| 695 | } |
| 696 |
| 697 | void ExceptionHandler::AddMappingInfo(const string& name, |
| 698 | const uint8_t identifier[sizeof(MDGUID)], |
| 699 | uintptr_t start_address, |
| 700 | size_t mapping_size, |
| 701 | size_t file_offset) { |
| 702 | MappingInfo info; |
| 703 | info.start_addr = start_address; |
| 704 | info.size = mapping_size; |
| 705 | info.offset = file_offset; |
| 706 | strncpy(info.name, name.c_str(), sizeof(info.name) - 1); |
| 707 | info.name[sizeof(info.name) - 1] = '\0'; |
| 708 |
| 709 | MappingEntry mapping; |
| 710 | mapping.first = info; |
| 711 | memcpy(mapping.second, identifier, sizeof(MDGUID)); |
| 712 | mapping_list_.push_back(mapping); |
| 713 | } |
| 714 |
| 715 | void ExceptionHandler::RegisterAppMemory(void* ptr, size_t length) { |
| 716 | AppMemoryList::iterator iter = |
| 717 | std::find(app_memory_list_.begin(), app_memory_list_.end(), ptr); |
| 718 | if (iter != app_memory_list_.end()) { |
| 719 | // Don't allow registering the same pointer twice. |
| 720 | return; |
| 721 | } |
| 722 |
| 723 | AppMemory app_memory; |
| 724 | app_memory.ptr = ptr; |
| 725 | app_memory.length = length; |
| 726 | app_memory_list_.push_back(app_memory); |
| 727 | } |
| 728 |
| 729 | void ExceptionHandler::UnregisterAppMemory(void* ptr) { |
| 730 | AppMemoryList::iterator iter = |
| 731 | std::find(app_memory_list_.begin(), app_memory_list_.end(), ptr); |
| 732 | if (iter != app_memory_list_.end()) { |
| 733 | app_memory_list_.erase(iter); |
| 734 | } |
| 735 | } |
| 736 |
| 737 | // static |
| 738 | bool ExceptionHandler::WriteMinidumpForChild(pid_t child, |
| 739 | pid_t child_blamed_thread, |
| 740 | const string& dump_path, |
| 741 | MinidumpCallback callback, |
| 742 | void* callback_context) { |
| 743 | // This function is not run in a compromised context. |
| 744 | MinidumpDescriptor descriptor(dump_path); |
| 745 | descriptor.UpdatePath(); |
| 746 | if (!google_breakpad::WriteMinidump(descriptor.path(), |
| 747 | child, |
| 748 | child_blamed_thread)) |
| 749 | return false; |
| 750 |
| 751 | return callback ? callback(descriptor, callback_context, true) : true; |
| 752 | } |
| 753 |
| 754 | } // namespace google_breakpad |
| 755 |