Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 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 copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <ctype.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <signal.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #include "bionic_ssp.h" |
| 38 | #include "logd.h" |
| 39 | |
| 40 | void* __stack_chk_guard = NULL; |
| 41 | |
| 42 | static void __attribute__((constructor)) __init_stack_check_guard() { |
| 43 | __stack_chk_guard = __generate_stack_chk_guard(); |
| 44 | } |
| 45 | |
| 46 | // This is the crash handler. |
| 47 | // Does a best effort at logging and calls _exit to terminate |
| 48 | // the process immediately (without atexit handlers, etc.). |
| 49 | void __stack_chk_fail() { |
| 50 | // Immediately block all (but SIGABRT) signal handlers from running code. |
| 51 | sigset_t sigmask; |
| 52 | sigfillset(&sigmask); |
| 53 | sigdelset(&sigmask, SIGABRT); |
| 54 | sigprocmask(SIG_BLOCK, &sigmask, NULL); |
| 55 | |
| 56 | // Use /proc/self/exe link to obtain the program name for logging |
| 57 | // purposes. If it's not available, we set it to "<unknown>". |
| 58 | char path[PATH_MAX]; |
| 59 | int count; |
| 60 | if ((count = readlink("/proc/self/exe", path, sizeof(path) - 1)) == -1) { |
| 61 | strlcpy(path, "<unknown>", sizeof(path)); |
| 62 | } else { |
| 63 | path[count] = '\0'; |
| 64 | } |
| 65 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 66 | // Do a best effort at logging. |
| 67 | __libc_android_log_write(ANDROID_LOG_FATAL, path, "stack corruption detected: aborted"); |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 68 | |
| 69 | // Make sure there is no default action for SIGABRT. |
| 70 | struct sigaction sa; |
| 71 | memset(&sa, 0, sizeof(sa)); |
| 72 | sigemptyset(&sa.sa_mask); |
| 73 | sa.sa_flags = 0; |
| 74 | sa.sa_handler = SIG_DFL; |
| 75 | sigaction(SIGABRT, &sa, NULL); |
| 76 | |
| 77 | // Terminate the process and exit immediately. |
| 78 | kill(getpid(), SIGABRT); |
| 79 | |
| 80 | _exit(127); |
| 81 | } |