blob: f01fee6bd511e41a0ff4ae34f857223f3354eb84 [file] [log] [blame]
Elliott Hughesad88a082012-10-24 18:37:21 -07001/*
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>
Elliott Hughesd3920b32013-02-07 18:39:34 -080035#include <sys/auxv.h>
Elliott Hughesad88a082012-10-24 18:37:21 -070036#include <unistd.h>
37
38#include "bionic_ssp.h"
39#include "logd.h"
40
Elliott Hughesd3920b32013-02-07 18:39:34 -080041uintptr_t __stack_chk_guard = NULL;
Elliott Hughesad88a082012-10-24 18:37:21 -070042
43static void __attribute__((constructor)) __init_stack_check_guard() {
Elliott Hughesd3920b32013-02-07 18:39:34 -080044 // AT_RANDOM is a pointer to 16 bytes of randomness on the stack.
45 __stack_chk_guard = *reinterpret_cast<uintptr_t*>(getauxval(AT_RANDOM));
Elliott Hughesad88a082012-10-24 18:37:21 -070046}
47
48// This is the crash handler.
49// Does a best effort at logging and calls _exit to terminate
50// the process immediately (without atexit handlers, etc.).
51void __stack_chk_fail() {
52 // Immediately block all (but SIGABRT) signal handlers from running code.
53 sigset_t sigmask;
54 sigfillset(&sigmask);
55 sigdelset(&sigmask, SIGABRT);
56 sigprocmask(SIG_BLOCK, &sigmask, NULL);
57
58 // Use /proc/self/exe link to obtain the program name for logging
59 // purposes. If it's not available, we set it to "<unknown>".
60 char path[PATH_MAX];
61 int count;
62 if ((count = readlink("/proc/self/exe", path, sizeof(path) - 1)) == -1) {
63 strlcpy(path, "<unknown>", sizeof(path));
64 } else {
65 path[count] = '\0';
66 }
67
Elliott Hughes1e980b62013-01-17 18:36:06 -080068 // Do a best effort at logging.
69 __libc_android_log_write(ANDROID_LOG_FATAL, path, "stack corruption detected: aborted");
Elliott Hughesad88a082012-10-24 18:37:21 -070070
71 // Make sure there is no default action for SIGABRT.
72 struct sigaction sa;
73 memset(&sa, 0, sizeof(sa));
74 sigemptyset(&sa.sa_mask);
75 sa.sa_flags = 0;
76 sa.sa_handler = SIG_DFL;
77 sigaction(SIGABRT, &sa, NULL);
78
79 // Terminate the process and exit immediately.
80 kill(getpid(), SIGABRT);
81
82 _exit(127);
83}