1 #include <assert.h> 2 #include <errno.h> 3 #include <pthread.h> 4 #include <sched.h> 5 #include <signal.h> 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <sys/cdefs.h> 10 #include <sys/mman.h> 11 #include <sys/ptrace.h> 12 #include <sys/socket.h> 13 #include <sys/wait.h> 14 #include <unistd.h> 15 16 #include <cutils/sockets.h> 17 #include <log/log.h> 18 19 #ifndef __unused 20 #define __unused __attribute__((__unused__)) 21 #endif 22 23 extern const char* __progname; 24 25 void crash1(void); 26 void crashnostack(void); 27 static int do_action(const char* arg); 28 29 static void maybe_abort() { 30 if (time(0) != 42) { 31 abort(); 32 } 33 } 34 35 static int smash_stack(int i __unused) { 36 printf("crasher: deliberately corrupting stack...\n"); 37 // Unless there's a "big enough" buffer on the stack, gcc 38 // doesn't bother inserting checks. 39 char buf[8]; 40 // If we don't write something relatively unpredictable 41 // into the buffer and then do something with it, gcc 42 // optimizes everything away and just returns a constant. 43 *(int*)(&buf[7]) = (uintptr_t) &buf[0]; 44 return *(int*)(&buf[0]); 45 } 46 47 static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack. 48 49 __attribute__((noinline)) static void overflow_stack(void* p) { 50 void* buf[1]; 51 buf[0] = p; 52 global = buf; 53 overflow_stack(&buf); 54 } 55 56 static void *noisy(void *x) 57 { 58 char c = (uintptr_t) x; 59 for(;;) { 60 usleep(250*1000); 61 write(2, &c, 1); 62 if(c == 'C') *((unsigned*) 0) = 42; 63 } 64 return NULL; 65 } 66 67 static int ctest() 68 { 69 pthread_t thr; 70 pthread_attr_t attr; 71 pthread_attr_init(&attr); 72 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 73 pthread_create(&thr, &attr, noisy, (void*) 'A'); 74 pthread_create(&thr, &attr, noisy, (void*) 'B'); 75 pthread_create(&thr, &attr, noisy, (void*) 'C'); 76 for(;;) ; 77 return 0; 78 } 79 80 static void* thread_callback(void* raw_arg) 81 { 82 return (void*) (uintptr_t) do_action((const char*) raw_arg); 83 } 84 85 static int do_action_on_thread(const char* arg) 86 { 87 pthread_t t; 88 pthread_create(&t, NULL, thread_callback, (void*) arg); 89 void* result = NULL; 90 pthread_join(t, &result); 91 return (int) (uintptr_t) result; 92 } 93 94 __attribute__((noinline)) static int crash3(int a) { 95 *((int*) 0xdead) = a; 96 return a*4; 97 } 98 99 __attribute__((noinline)) static int crash2(int a) { 100 a = crash3(a) + 2; 101 return a*3; 102 } 103 104 __attribute__((noinline)) static int crash(int a) { 105 a = crash2(a) + 1; 106 return a*2; 107 } 108 109 static void abuse_heap() { 110 char buf[16]; 111 free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately. 112 } 113 114 static void sigsegv_non_null() { 115 int* a = (int *)(&do_action); 116 *a = 42; 117 } 118 119 static int do_action(const char* arg) 120 { 121 fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid()); 122 123 if (!strncmp(arg, "thread-", strlen("thread-"))) { 124 return do_action_on_thread(arg + strlen("thread-")); 125 } else if (!strcmp(arg, "SIGSEGV-non-null")) { 126 sigsegv_non_null(); 127 } else if (!strcmp(arg, "smash-stack")) { 128 return smash_stack(42); 129 } else if (!strcmp(arg, "stack-overflow")) { 130 overflow_stack(NULL); 131 } else if (!strcmp(arg, "nostack")) { 132 crashnostack(); 133 } else if (!strcmp(arg, "ctest")) { 134 return ctest(); 135 } else if (!strcmp(arg, "exit")) { 136 exit(1); 137 } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) { 138 return crash(42); 139 } else if (!strcmp(arg, "abort")) { 140 maybe_abort(); 141 } else if (!strcmp(arg, "assert")) { 142 __assert("some_file.c", 123, "false"); 143 } else if (!strcmp(arg, "assert2")) { 144 __assert2("some_file.c", 123, "some_function", "false"); 145 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) { 146 LOG_ALWAYS_FATAL("hello %s", "world"); 147 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) { 148 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world"); 149 } else if (!strcmp(arg, "SIGFPE")) { 150 raise(SIGFPE); 151 return EXIT_SUCCESS; 152 } else if (!strcmp(arg, "SIGPIPE")) { 153 int pipe_fds[2]; 154 pipe(pipe_fds); 155 close(pipe_fds[0]); 156 write(pipe_fds[1], "oops", 4); 157 return EXIT_SUCCESS; 158 } else if (!strcmp(arg, "SIGTRAP")) { 159 raise(SIGTRAP); 160 return EXIT_SUCCESS; 161 } else if (!strcmp(arg, "heap-usage")) { 162 abuse_heap(); 163 } else if (!strcmp(arg, "SIGSEGV-unmapped")) { 164 char* map = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); 165 munmap(map, sizeof(int)); 166 map[0] = '8'; 167 } 168 169 fprintf(stderr, "%s OP\n", __progname); 170 fprintf(stderr, "where OP is:\n"); 171 fprintf(stderr, " smash-stack overwrite a stack-guard canary\n"); 172 fprintf(stderr, " stack-overflow recurse until the stack overflows\n"); 173 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n"); 174 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n"); 175 fprintf(stderr, " nostack crash with a NULL stack pointer\n"); 176 fprintf(stderr, " ctest (obsoleted by thread-crash?)\n"); 177 fprintf(stderr, " exit call exit(1)\n"); 178 fprintf(stderr, " abort call abort()\n"); 179 fprintf(stderr, " assert call assert() without a function\n"); 180 fprintf(stderr, " assert2 call assert() with a function\n"); 181 fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n"); 182 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n"); 183 fprintf(stderr, " SIGFPE cause a SIGFPE\n"); 184 fprintf(stderr, " SIGPIPE cause a SIGPIPE\n"); 185 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n"); 186 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n"); 187 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n"); 188 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n"); 189 fprintf(stderr, "prefix any of the above with 'thread-' to not run\n"); 190 fprintf(stderr, "on the process' main thread.\n"); 191 return EXIT_SUCCESS; 192 } 193 194 int main(int argc, char **argv) 195 { 196 fprintf(stderr,"crasher: built at " __TIME__ "!@\n"); 197 198 if(argc > 1) { 199 return do_action(argv[1]); 200 } else { 201 crash1(); 202 } 203 204 return 0; 205 } 206