2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
//#include <cutils/misc.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include <cutils/sockets.h>
|
|
|
|
|
2013-04-24 02:14:56 +02:00
|
|
|
extern const char* __progname;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
void crash1(void);
|
|
|
|
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
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
static void maybeabort() {
|
|
|
|
if(time(0) != 42) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smash_stack(int i) {
|
2013-02-14 23:41:57 +01:00
|
|
|
printf("crasher: deliberately corrupting stack...\n");
|
|
|
|
// Unless there's a "big enough" buffer on the stack, gcc
|
|
|
|
// doesn't bother inserting checks.
|
|
|
|
char buf[8];
|
2013-07-16 02:19:02 +02:00
|
|
|
// If we don't write something relatively unpredictable
|
2013-02-14 23:41:57 +01:00
|
|
|
// into the buffer and then do something with it, gcc
|
|
|
|
// optimizes everything away and just returns a constant.
|
|
|
|
*(int*)(&buf[7]) = (uintptr_t) &buf[0];
|
|
|
|
return *(int*)(&buf[0]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
static void test_call1()
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2013-04-24 02:14:56 +02:00
|
|
|
*((int*) 32) = 1;
|
2009-03-04 04:32:55 +01: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);
|
|
|
|
if(c == 'C') *((unsigned*) 0) = 42;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-06-12 23:04:34 +02:00
|
|
|
static int do_action(const char* arg)
|
2012-12-10 23:15:42 +01:00
|
|
|
{
|
2013-04-24 02:14:56 +02:00
|
|
|
fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
|
|
|
|
|
|
|
|
if (!strncmp(arg, "thread-", strlen("thread-"))) {
|
2012-12-10 23:15:42 +01:00
|
|
|
return do_action_on_thread(arg + strlen("thread-"));
|
2013-04-24 02:14:56 +02:00
|
|
|
} else if (!strcmp(arg,"smash-stack")) {
|
|
|
|
return smash_stack(42);
|
|
|
|
} else if (!strcmp(arg,"stack-overflow")) {
|
|
|
|
overflow_stack(NULL);
|
|
|
|
} else if (!strcmp(arg,"nostack")) {
|
|
|
|
crashnostack();
|
|
|
|
} else if (!strcmp(arg,"ctest")) {
|
|
|
|
return ctest();
|
|
|
|
} else if (!strcmp(arg,"exit")) {
|
|
|
|
exit(1);
|
|
|
|
} else if (!strcmp(arg,"crash")) {
|
|
|
|
return crash(42);
|
|
|
|
} else if (!strcmp(arg,"abort")) {
|
|
|
|
maybeabort();
|
2013-06-12 23:04:34 +02:00
|
|
|
} else if (!strcmp(arg, "heap-usage")) {
|
|
|
|
abuse_heap();
|
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");
|
|
|
|
fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
|
|
|
|
fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
|
2013-06-12 23:04:34 +02:00
|
|
|
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");
|
2013-04-24 02:14:56 +02:00
|
|
|
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, " crash cause a SIGSEGV\n");
|
|
|
|
fprintf(stderr, " abort call abort()\n");
|
|
|
|
fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
|
|
|
|
fprintf(stderr, "on the process' main thread.\n");
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"crasher: built at " __TIME__ "!@\n");
|
2009-03-04 04:32:55 +01: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
|
|
|
}
|