2016-10-17 23:28:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2006, The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "crasher"
|
|
|
|
|
2014-04-23 23:57:32 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2016-10-08 01:42:05 +02:00
|
|
|
#include <fcntl.h>
|
2014-04-23 23:57:32 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <signal.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-04-30 18:24:08 +02:00
|
|
|
#include <sys/cdefs.h>
|
2014-07-07 21:33:50 +02:00
|
|
|
#include <sys/mman.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/socket.h>
|
2014-04-23 23:57:32 +02:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-09-29 00:54:45 +02:00
|
|
|
#include <android/log.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <cutils/sockets.h>
|
2014-04-30 18:24:08 +02:00
|
|
|
|
2016-06-16 02:29:00 +02:00
|
|
|
#if defined(STATIC_CRASHER)
|
|
|
|
#include "debuggerd/client.h"
|
|
|
|
#endif
|
|
|
|
|
2014-04-30 18:24:08 +02:00
|
|
|
#ifndef __unused
|
|
|
|
#define __unused __attribute__((__unused__))
|
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-04-24 02:14:56 +02:00
|
|
|
extern const char* __progname;
|
|
|
|
|
2016-05-10 22:29:58 +02:00
|
|
|
extern "C" void crash1(void);
|
|
|
|
extern "C" void crashnostack(void);
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
static int do_action(const char* arg);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-04-23 23:57:32 +02:00
|
|
|
static void maybe_abort() {
|
|
|
|
if (time(0) != 42) {
|
2013-06-12 23:04:34 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 02:46:33 +01:00
|
|
|
static char* smash_stack_dummy_buf;
|
|
|
|
__attribute__ ((noinline)) static void smash_stack_dummy_function(volatile int* plen) {
|
|
|
|
smash_stack_dummy_buf[*plen] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This must be marked with "__attribute__ ((noinline))", to ensure the
|
|
|
|
// compiler generates the proper stack guards around this function.
|
|
|
|
// Assign local array address to global variable to force stack guards.
|
|
|
|
// Use another noinline function to corrupt the stack.
|
|
|
|
__attribute__ ((noinline)) static int smash_stack(volatile int* plen) {
|
2016-06-16 02:29:00 +02:00
|
|
|
printf("%s: deliberately corrupting stack...\n", __progname);
|
2014-12-12 02:46:33 +01:00
|
|
|
|
|
|
|
char buf[128];
|
|
|
|
smash_stack_dummy_buf = buf;
|
|
|
|
// This should corrupt stack guards and make process abort.
|
|
|
|
smash_stack_dummy_function(plen);
|
|
|
|
return 0;
|
2013-02-14 23:41:57 +01:00
|
|
|
}
|
|
|
|
|
2015-10-01 08:30:38 +02:00
|
|
|
#if defined(__clang__)
|
2015-09-30 08:55:14 +02:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Winfinite-recursion"
|
2015-10-01 08:30:38 +02:00
|
|
|
#endif
|
2015-09-30 08:55:14 +02:00
|
|
|
|
2013-07-16 02:19:02 +02:00
|
|
|
static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
__attribute__((noinline)) static void overflow_stack(void* p) {
|
2013-04-24 02:14:56 +02:00
|
|
|
void* buf[1];
|
|
|
|
buf[0] = p;
|
2013-07-16 02:19:02 +02:00
|
|
|
global = buf;
|
2013-04-24 02:14:56 +02:00
|
|
|
overflow_stack(&buf);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-01 08:30:38 +02:00
|
|
|
#if defined(__clang__)
|
2015-09-30 08:55:14 +02:00
|
|
|
#pragma clang diagnostic pop
|
2015-10-01 08:30:38 +02:00
|
|
|
#endif
|
2015-09-30 08:55:14 +02:00
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
static void *noisy(void *x)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2014-02-06 02:50:35 +01:00
|
|
|
char c = (uintptr_t) x;
|
2009-03-04 04:32:55 +01:00
|
|
|
for(;;) {
|
|
|
|
usleep(250*1000);
|
|
|
|
write(2, &c, 1);
|
2014-10-24 01:50:51 +02:00
|
|
|
if(c == 'C') *((volatile unsigned*) 0) = 42;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-02-06 02:50:35 +01:00
|
|
|
return NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
static int ctest()
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
pthread_t thr;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
pthread_create(&thr, &attr, noisy, (void*) 'A');
|
|
|
|
pthread_create(&thr, &attr, noisy, (void*) 'B');
|
|
|
|
pthread_create(&thr, &attr, noisy, (void*) 'C');
|
|
|
|
for(;;) ;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-10 23:15:42 +01:00
|
|
|
static void* thread_callback(void* raw_arg)
|
|
|
|
{
|
2014-02-06 02:50:35 +01:00
|
|
|
return (void*) (uintptr_t) do_action((const char*) raw_arg);
|
2012-12-10 23:15:42 +01:00
|
|
|
}
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
static int do_action_on_thread(const char* arg)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2012-12-10 23:15:42 +01:00
|
|
|
pthread_t t;
|
|
|
|
pthread_create(&t, NULL, thread_callback, (void*) arg);
|
|
|
|
void* result = NULL;
|
|
|
|
pthread_join(t, &result);
|
2014-02-06 02:50:35 +01:00
|
|
|
return (int) (uintptr_t) result;
|
2012-12-10 23:15:42 +01:00
|
|
|
}
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
__attribute__((noinline)) static int crash3(int a) {
|
|
|
|
*((int*) 0xdead) = a;
|
|
|
|
return a*4;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((noinline)) static int crash2(int a) {
|
|
|
|
a = crash3(a) + 2;
|
|
|
|
return a*3;
|
2013-03-08 10:17:35 +01:00
|
|
|
}
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
__attribute__((noinline)) static int crash(int a) {
|
|
|
|
a = crash2(a) + 1;
|
|
|
|
return a*2;
|
2013-03-08 10:17:35 +01:00
|
|
|
}
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
static void abuse_heap() {
|
|
|
|
char buf[16];
|
|
|
|
free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
|
2013-03-08 10:17:35 +01:00
|
|
|
}
|
|
|
|
|
2014-06-17 23:55:47 +02:00
|
|
|
static void sigsegv_non_null() {
|
|
|
|
int* a = (int *)(&do_action);
|
|
|
|
*a = 42;
|
|
|
|
}
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
static int do_action(const char* arg)
|
2012-12-10 23:15:42 +01:00
|
|
|
{
|
2016-06-16 02:29:00 +02:00
|
|
|
fprintf(stderr, "%s: init pid=%d tid=%d\n", __progname, getpid(), gettid());
|
2013-04-24 02:14:56 +02:00
|
|
|
|
2016-11-01 01:37:37 +01:00
|
|
|
if (!strncmp(arg, "wait-", strlen("wait-"))) {
|
|
|
|
char buf[1];
|
|
|
|
TEMP_FAILURE_RETRY(read(STDIN_FILENO, buf, sizeof(buf)));
|
|
|
|
return do_action(arg + strlen("wait-"));
|
|
|
|
} else if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) {
|
2016-10-08 01:42:05 +02:00
|
|
|
errno = 0;
|
|
|
|
while (errno != EMFILE) {
|
|
|
|
open("/dev/null", O_RDONLY);
|
|
|
|
}
|
|
|
|
return do_action(arg + strlen("exhaustfd-"));
|
|
|
|
} else if (!strncmp(arg, "thread-", strlen("thread-"))) {
|
2012-12-10 23:15:42 +01:00
|
|
|
return do_action_on_thread(arg + strlen("thread-"));
|
2014-06-17 23:55:47 +02:00
|
|
|
} else if (!strcmp(arg, "SIGSEGV-non-null")) {
|
|
|
|
sigsegv_non_null();
|
2014-04-23 23:57:32 +02:00
|
|
|
} else if (!strcmp(arg, "smash-stack")) {
|
2014-12-12 02:46:33 +01:00
|
|
|
volatile int len = 128;
|
|
|
|
return smash_stack(&len);
|
2014-04-23 23:57:32 +02:00
|
|
|
} else if (!strcmp(arg, "stack-overflow")) {
|
2013-04-24 02:14:56 +02:00
|
|
|
overflow_stack(NULL);
|
2014-04-23 23:57:32 +02:00
|
|
|
} else if (!strcmp(arg, "nostack")) {
|
2013-04-24 02:14:56 +02:00
|
|
|
crashnostack();
|
2014-04-23 23:57:32 +02:00
|
|
|
} else if (!strcmp(arg, "ctest")) {
|
2013-04-24 02:14:56 +02:00
|
|
|
return ctest();
|
2014-04-23 23:57:32 +02:00
|
|
|
} else if (!strcmp(arg, "exit")) {
|
2013-04-24 02:14:56 +02:00
|
|
|
exit(1);
|
2014-04-26 01:05:34 +02:00
|
|
|
} else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
|
2013-04-24 02:14:56 +02:00
|
|
|
return crash(42);
|
2014-04-23 23:57:32 +02:00
|
|
|
} else if (!strcmp(arg, "abort")) {
|
|
|
|
maybe_abort();
|
|
|
|
} else if (!strcmp(arg, "assert")) {
|
|
|
|
__assert("some_file.c", 123, "false");
|
|
|
|
} else if (!strcmp(arg, "assert2")) {
|
2014-07-15 20:38:47 +02:00
|
|
|
__assert2("some_file.c", 123, "some_function", "false");
|
2016-05-10 22:29:58 +02:00
|
|
|
} else if (!strcmp(arg, "fortify")) {
|
|
|
|
char buf[10];
|
|
|
|
__read_chk(-1, buf, 32, 10);
|
|
|
|
while (true) pause();
|
2014-04-23 23:57:32 +02:00
|
|
|
} else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
|
|
|
|
LOG_ALWAYS_FATAL("hello %s", "world");
|
|
|
|
} else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
|
|
|
|
LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
|
2014-07-15 20:38:47 +02:00
|
|
|
} else if (!strcmp(arg, "SIGFPE")) {
|
|
|
|
raise(SIGFPE);
|
|
|
|
return EXIT_SUCCESS;
|
2014-05-17 02:05:19 +02:00
|
|
|
} else if (!strcmp(arg, "SIGTRAP")) {
|
|
|
|
raise(SIGTRAP);
|
|
|
|
return EXIT_SUCCESS;
|
2013-06-12 23:04:34 +02:00
|
|
|
} else if (!strcmp(arg, "heap-usage")) {
|
|
|
|
abuse_heap();
|
2014-07-07 21:33:50 +02:00
|
|
|
} else if (!strcmp(arg, "SIGSEGV-unmapped")) {
|
2016-05-10 22:29:58 +02:00
|
|
|
char* map = reinterpret_cast<char*>(mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0));
|
2014-07-07 21:33:50 +02:00
|
|
|
munmap(map, sizeof(int));
|
|
|
|
map[0] = '8';
|
2012-12-10 23:15:42 +01:00
|
|
|
}
|
|
|
|
|
2013-04-24 02:14:56 +02:00
|
|
|
fprintf(stderr, "%s OP\n", __progname);
|
|
|
|
fprintf(stderr, "where OP is:\n");
|
2014-04-26 01:05:34 +02:00
|
|
|
fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
|
|
|
|
fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
|
|
|
|
fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
|
|
|
|
fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
|
|
|
|
fprintf(stderr, " nostack crash with a NULL stack pointer\n");
|
|
|
|
fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
|
|
|
|
fprintf(stderr, " exit call exit(1)\n");
|
|
|
|
fprintf(stderr, " abort call abort()\n");
|
|
|
|
fprintf(stderr, " assert call assert() without a function\n");
|
|
|
|
fprintf(stderr, " assert2 call assert() with a function\n");
|
2016-05-10 22:29:58 +02:00
|
|
|
fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
|
2014-04-26 01:05:34 +02:00
|
|
|
fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n");
|
|
|
|
fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n");
|
2014-07-15 20:38:47 +02:00
|
|
|
fprintf(stderr, " SIGFPE cause a SIGFPE\n");
|
2014-06-17 23:55:47 +02:00
|
|
|
fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
|
|
|
|
fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
|
2014-07-07 21:33:50 +02:00
|
|
|
fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
|
2014-05-17 02:05:19 +02:00
|
|
|
fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
|
2013-04-24 02:14:56 +02:00
|
|
|
fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
|
|
|
|
fprintf(stderr, "on the process' main thread.\n");
|
2016-10-08 01:42:05 +02:00
|
|
|
fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n");
|
|
|
|
fprintf(stderr, "all available file descriptors before crashing.\n");
|
2016-11-01 01:37:37 +01:00
|
|
|
fprintf(stderr, "prefix any of the above with 'wait-' to wait until input is received on stdin\n");
|
|
|
|
|
2013-04-24 02:14:56 +02:00
|
|
|
return EXIT_SUCCESS;
|
2012-12-10 23:15:42 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2012-12-10 23:15:42 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2016-06-16 02:29:00 +02:00
|
|
|
fprintf(stderr, "%s: built at " __TIME__ "!@\n", __progname);
|
|
|
|
|
|
|
|
#if defined(STATIC_CRASHER)
|
|
|
|
debuggerd_callbacks_t callbacks = {
|
|
|
|
.get_abort_message = []() {
|
|
|
|
static struct {
|
|
|
|
size_t size;
|
|
|
|
char msg[32];
|
|
|
|
} msg;
|
|
|
|
|
|
|
|
msg.size = strlen("dummy abort message");
|
|
|
|
memcpy(msg.msg, "dummy abort message", strlen("dummy abort message"));
|
|
|
|
return reinterpret_cast<abort_msg_t*>(&msg);
|
|
|
|
},
|
|
|
|
.post_dump = nullptr
|
|
|
|
};
|
|
|
|
debuggerd_init(&callbacks);
|
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-06-16 02:29:00 +02:00
|
|
|
if (argc > 1) {
|
2012-12-10 23:15:42 +01:00
|
|
|
return do_action(argv[1]);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
|
|
|
crash1();
|
|
|
|
}
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
return 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|