blob: 64a38dd8cc0c8b4041ed560de7ba582a95bcf64d [file] [log] [blame]
Mark Salyzyncfd5b082016-10-17 14:28:00 -07001/*
2 * Copyright 2006, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "crasher"
18
Elliott Hughesda6b2e22014-04-23 14:57:32 -070019#include <assert.h>
Elliott Hughes400628a2016-12-14 17:33:46 -080020#include <dirent.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070021#include <errno.h>
Josh Gao218f7fb2016-10-07 16:42:05 -070022#include <fcntl.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070023#include <pthread.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070024#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Brigid Smith8606eaa2014-07-07 12:33:50 -070028#include <sys/mman.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070029#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
Elliott Hughes400628a2016-12-14 17:33:46 -080031// We test both kinds of logging.
Elliott Hughes400628a2016-12-14 17:33:46 -080032#include <android-base/logging.h>
Mark Salyzyn51c33b72017-01-12 15:44:06 -080033#include <log/log.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070034
Josh Gao9c02dc52016-06-15 17:29:00 -070035#if defined(STATIC_CRASHER)
Josh Gaocbe70cb2016-10-18 18:17:52 -070036#include "debuggerd/handler.h"
Josh Gao9c02dc52016-06-15 17:29:00 -070037#endif
38
Elliott Hughes0ba53592017-02-01 16:59:15 -080039#if defined(__arm__)
40// See https://www.kernel.org/doc/Documentation/arm/kernel_user_helpers.txt for details.
41#define __kuser_helper_version (*(int32_t*) 0xffff0ffc)
42typedef void * (__kuser_get_tls_t)(void);
43#define __kuser_get_tls (*(__kuser_get_tls_t*) 0xffff0fe0)
44typedef int (__kuser_cmpxchg_t)(int oldval, int newval, volatile int *ptr);
45#define __kuser_cmpxchg (*(__kuser_cmpxchg_t*) 0xffff0fc0)
46typedef void (__kuser_dmb_t)(void);
47#define __kuser_dmb (*(__kuser_dmb_t*) 0xffff0fa0)
48typedef int (__kuser_cmpxchg64_t)(const int64_t*, const int64_t*, volatile int64_t*);
49#define __kuser_cmpxchg64 (*(__kuser_cmpxchg64_t*) 0xffff0f60)
50#endif
51
Elliott Hughes400628a2016-12-14 17:33:46 -080052#define noinline __attribute__((__noinline__))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Elliott Hughes400628a2016-12-14 17:33:46 -080054// Avoid name mangling so that stacks are more readable.
55extern "C" {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070056
Elliott Hughes400628a2016-12-14 17:33:46 -080057void crash1(void);
58void crashnostack(void);
Elliott Hughes23d1cad2016-05-10 13:29:58 -070059
Elliott Hughes400628a2016-12-14 17:33:46 -080060int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Elliott Hughes400628a2016-12-14 17:33:46 -080062noinline void maybe_abort() {
Elliott Hughesda6b2e22014-04-23 14:57:32 -070063 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070064 abort();
65 }
66}
67
Elliott Hughes400628a2016-12-14 17:33:46 -080068char* smash_stack_dummy_buf;
69noinline void smash_stack_dummy_function(volatile int* plen) {
Yabin Cui2331b952014-12-11 17:46:33 -080070 smash_stack_dummy_buf[*plen] = 0;
71}
72
73// This must be marked with "__attribute__ ((noinline))", to ensure the
74// compiler generates the proper stack guards around this function.
75// Assign local array address to global variable to force stack guards.
76// Use another noinline function to corrupt the stack.
Elliott Hughes400628a2016-12-14 17:33:46 -080077noinline int smash_stack(volatile int* plen) {
78 printf("%s: deliberately corrupting stack...\n", getprogname());
Yabin Cui2331b952014-12-11 17:46:33 -080079
80 char buf[128];
81 smash_stack_dummy_buf = buf;
82 // This should corrupt stack guards and make process abort.
83 smash_stack_dummy_function(plen);
84 return 0;
Elliott Hughesdf4200e2013-02-14 14:41:57 -080085}
86
Stephen Hines18395cb2015-09-29 23:55:14 -070087#pragma clang diagnostic push
88#pragma clang diagnostic ignored "-Winfinite-recursion"
89
Elliott Hughes400628a2016-12-14 17:33:46 -080090void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
Elliott Hughesb1be27e2013-07-15 17:19:02 -070091
Elliott Hughes400628a2016-12-14 17:33:46 -080092noinline void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070093 void* buf[1];
94 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070095 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070096 overflow_stack(&buf);
97}
98
Stephen Hines18395cb2015-09-29 23:55:14 -070099#pragma clang diagnostic pop
100
Elliott Hughes400628a2016-12-14 17:33:46 -0800101noinline void* thread_callback(void* raw_arg) {
102 const char* arg = reinterpret_cast<const char*>(raw_arg);
103 return reinterpret_cast<void*>(static_cast<uintptr_t>(do_action(arg)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104}
105
Elliott Hughes400628a2016-12-14 17:33:46 -0800106noinline int do_action_on_thread(const char* arg) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800107 pthread_t t;
Elliott Hughes400628a2016-12-14 17:33:46 -0800108 pthread_create(&t, nullptr, thread_callback, const_cast<char*>(arg));
109 void* result = nullptr;
Elliott Hughesaa421302012-12-10 14:15:42 -0800110 pthread_join(t, &result);
Elliott Hughes400628a2016-12-14 17:33:46 -0800111 return reinterpret_cast<uintptr_t>(result);
Elliott Hughesaa421302012-12-10 14:15:42 -0800112}
113
Elliott Hughes400628a2016-12-14 17:33:46 -0800114noinline int crash3(int a) {
115 *reinterpret_cast<int*>(0xdead) = a;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700116 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400117}
118
Elliott Hughes400628a2016-12-14 17:33:46 -0800119noinline int crash2(int a) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700120 a = crash3(a) + 2;
121 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400122}
123
Elliott Hughes400628a2016-12-14 17:33:46 -0800124noinline int crash(int a) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700125 a = crash2(a) + 1;
126 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400127}
128
Elliott Hughes400628a2016-12-14 17:33:46 -0800129noinline void abuse_heap() {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700130 char buf[16];
Elliott Hughes400628a2016-12-14 17:33:46 -0800131 free(buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700132}
133
Elliott Hughes400628a2016-12-14 17:33:46 -0800134noinline void sigsegv_non_null() {
Brigid Smith7b2078e2014-06-17 14:55:47 -0700135 int* a = (int *)(&do_action);
136 *a = 42;
137}
138
Elliott Hughes400628a2016-12-14 17:33:46 -0800139noinline void fprintf_null() {
140 fprintf(nullptr, "oops");
141}
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700142
Elliott Hughes400628a2016-12-14 17:33:46 -0800143noinline void readdir_null() {
144 readdir(nullptr);
145}
146
147noinline int strlen_null() {
148 char* sneaky_null = nullptr;
149 return strlen(sneaky_null);
150}
151
152static int usage() {
153 fprintf(stderr, "usage: %s KIND\n", getprogname());
154 fprintf(stderr, "\n");
155 fprintf(stderr, "where KIND is:\n");
156 fprintf(stderr, " smash-stack overwrite a -fstack-protector guard\n");
157 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800158 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
159 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800160 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
161 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800162 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800163 fprintf(stderr, " abort call abort()\n");
164 fprintf(stderr, " assert call assert() without a function\n");
165 fprintf(stderr, " assert2 call assert() with a function\n");
166 fprintf(stderr, " exit call exit(1)\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800167 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800168 fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800169 fprintf(stderr, " seccomp fail a seccomp check\n");
170#if defined(__arm__)
171 fprintf(stderr, " kuser_helper_version call kuser_helper_version\n");
172 fprintf(stderr, " kuser_get_tls call kuser_get_tls\n");
173 fprintf(stderr, " kuser_cmpxchg call kuser_cmpxchg\n");
174 fprintf(stderr, " kuser_memory_barrier call kuser_memory_barrier\n");
175 fprintf(stderr, " kuser_cmpxchg64 call kuser_cmpxchg64\n");
176#endif
177 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800178 fprintf(stderr, " LOG_ALWAYS_FATAL call liblog LOG_ALWAYS_FATAL\n");
179 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call liblog LOG_ALWAYS_FATAL_IF\n");
180 fprintf(stderr, " LOG-FATAL call libbase LOG(FATAL)\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800181 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800182 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
183 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
184 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
185 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
186 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800187 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800188 fprintf(stderr, " fprintf-NULL pass a null pointer to fprintf\n");
189 fprintf(stderr, " readdir-NULL pass a null pointer to readdir\n");
190 fprintf(stderr, " strlen-NULL pass a null pointer to strlen\n");
191 fprintf(stderr, "\n");
192 fprintf(stderr, "prefix any of the above with 'thread-' to run on a new thread\n");
193 fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n");
194 fprintf(stderr, "all available file descriptors before crashing.\n");
195 fprintf(stderr, "prefix any of the above with 'wait-' to wait until input is received on stdin\n");
196
197 return EXIT_FAILURE;
198}
199
200noinline int do_action(const char* arg) {
201 // Prefixes.
Josh Gao100ce392016-10-31 17:37:37 -0700202 if (!strncmp(arg, "wait-", strlen("wait-"))) {
203 char buf[1];
204 TEMP_FAILURE_RETRY(read(STDIN_FILENO, buf, sizeof(buf)));
205 return do_action(arg + strlen("wait-"));
206 } else if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) {
Josh Gao218f7fb2016-10-07 16:42:05 -0700207 errno = 0;
208 while (errno != EMFILE) {
209 open("/dev/null", O_RDONLY);
210 }
211 return do_action(arg + strlen("exhaustfd-"));
212 } else if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800213 return do_action_on_thread(arg + strlen("thread-"));
Elliott Hughes400628a2016-12-14 17:33:46 -0800214 }
215
216 // Actions.
217 if (!strcasecmp(arg, "SIGSEGV-non-null")) {
Brigid Smith7b2078e2014-06-17 14:55:47 -0700218 sigsegv_non_null();
Elliott Hughes400628a2016-12-14 17:33:46 -0800219 } else if (!strcasecmp(arg, "smash-stack")) {
Yabin Cui2331b952014-12-11 17:46:33 -0800220 volatile int len = 128;
221 return smash_stack(&len);
Elliott Hughes400628a2016-12-14 17:33:46 -0800222 } else if (!strcasecmp(arg, "stack-overflow")) {
223 overflow_stack(nullptr);
224 } else if (!strcasecmp(arg, "nostack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700225 crashnostack();
Elliott Hughes400628a2016-12-14 17:33:46 -0800226 } else if (!strcasecmp(arg, "exit")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700227 exit(1);
Elliott Hughes400628a2016-12-14 17:33:46 -0800228 } else if (!strcasecmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700229 return crash(42);
Elliott Hughes400628a2016-12-14 17:33:46 -0800230 } else if (!strcasecmp(arg, "abort")) {
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700231 maybe_abort();
Elliott Hughes400628a2016-12-14 17:33:46 -0800232 } else if (!strcasecmp(arg, "assert")) {
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700233 __assert("some_file.c", 123, "false");
Elliott Hughes400628a2016-12-14 17:33:46 -0800234 } else if (!strcasecmp(arg, "assert2")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700235 __assert2("some_file.c", 123, "some_function", "false");
Elliott Hughes400628a2016-12-14 17:33:46 -0800236 } else if (!strcasecmp(arg, "fortify")) {
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700237 char buf[10];
238 __read_chk(-1, buf, 32, 10);
239 while (true) pause();
Elliott Hughes400628a2016-12-14 17:33:46 -0800240 } else if (!strcasecmp(arg, "LOG(FATAL)")) {
241 LOG(FATAL) << "hello " << 123;
242 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL")) {
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700243 LOG_ALWAYS_FATAL("hello %s", "world");
Elliott Hughes400628a2016-12-14 17:33:46 -0800244 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL_IF")) {
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700245 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes400628a2016-12-14 17:33:46 -0800246 } else if (!strcasecmp(arg, "SIGFPE")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700247 raise(SIGFPE);
248 return EXIT_SUCCESS;
Elliott Hughes400628a2016-12-14 17:33:46 -0800249 } else if (!strcasecmp(arg, "SIGTRAP")) {
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700250 raise(SIGTRAP);
251 return EXIT_SUCCESS;
Elliott Hughes400628a2016-12-14 17:33:46 -0800252 } else if (!strcasecmp(arg, "fprintf-NULL")) {
253 fprintf_null();
254 } else if (!strcasecmp(arg, "readdir-NULL")) {
255 readdir_null();
256 } else if (!strcasecmp(arg, "strlen-NULL")) {
257 return strlen_null();
258 } else if (!strcasecmp(arg, "heap-usage")) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700259 abuse_heap();
Elliott Hughes400628a2016-12-14 17:33:46 -0800260 } else if (!strcasecmp(arg, "SIGSEGV-unmapped")) {
261 char* map = reinterpret_cast<char*>(mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE,
262 MAP_SHARED | MAP_ANONYMOUS, -1, 0));
Brigid Smith8606eaa2014-07-07 12:33:50 -0700263 munmap(map, sizeof(int));
264 map[0] = '8';
Elliott Hughes0ba53592017-02-01 16:59:15 -0800265 } else if (!strcasecmp(arg, "seccomp")) {
266 syscall(99999);
267#if defined(__arm__)
268 } else if (!strcasecmp(arg, "kuser_helper_version")) {
269 return __kuser_helper_version;
270 } else if (!strcasecmp(arg, "kuser_get_tls")) {
271 return !__kuser_get_tls();
272 } else if (!strcasecmp(arg, "kuser_cmpxchg")) {
273 return __kuser_cmpxchg(0, 0, 0);
274 } else if (!strcasecmp(arg, "kuser_memory_barrier")) {
275 __kuser_dmb();
276 } else if (!strcasecmp(arg, "kuser_cmpxchg64")) {
277 return __kuser_cmpxchg64(0, 0, 0);
278#endif
Elliott Hughes400628a2016-12-14 17:33:46 -0800279 } else {
280 return usage();
Elliott Hughesaa421302012-12-10 14:15:42 -0800281 }
282
Elliott Hughes400628a2016-12-14 17:33:46 -0800283 fprintf(stderr, "%s: exiting normally!\n", getprogname());
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700284 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800285}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286
Elliott Hughes400628a2016-12-14 17:33:46 -0800287int main(int argc, char** argv) {
Josh Gao9c02dc52016-06-15 17:29:00 -0700288#if defined(STATIC_CRASHER)
289 debuggerd_callbacks_t callbacks = {
290 .get_abort_message = []() {
291 static struct {
292 size_t size;
293 char msg[32];
294 } msg;
295
296 msg.size = strlen("dummy abort message");
297 memcpy(msg.msg, "dummy abort message", strlen("dummy abort message"));
298 return reinterpret_cast<abort_msg_t*>(&msg);
299 },
300 .post_dump = nullptr
301 };
302 debuggerd_init(&callbacks);
303#endif
304
Elliott Hughes400628a2016-12-14 17:33:46 -0800305 if (argc == 1) crash1();
306 else if (argc == 2) return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307
Elliott Hughes400628a2016-12-14 17:33:46 -0800308 return usage();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309}
Elliott Hughes400628a2016-12-14 17:33:46 -0800310
311};