1 /*
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
19 #include <assert.h>
20 #include <dirent.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/mman.h>
29 #include <sys/prctl.h>
30 #include <unistd.h>
31
32 // We test both kinds of logging.
33 #include <android-base/logging.h>
34 #include <log/log.h>
35
36 #include "seccomp_policy.h"
37
38 #if defined(STATIC_CRASHER)
39 #include "debuggerd/handler.h"
40 #endif
41
42 extern "C" void android_set_abort_message(const char* msg);
43
44 #if defined(__arm__)
45 // See https://www.kernel.org/doc/Documentation/arm/kernel_user_helpers.txt for details.
46 #define __kuser_helper_version (*(int32_t*) 0xffff0ffc)
47 typedef void * (__kuser_get_tls_t)(void);
48 #define __kuser_get_tls (*(__kuser_get_tls_t*) 0xffff0fe0)
49 typedef int (__kuser_cmpxchg_t)(int oldval, int newval, volatile int *ptr);
50 #define __kuser_cmpxchg (*(__kuser_cmpxchg_t*) 0xffff0fc0)
51 typedef void (__kuser_dmb_t)(void);
52 #define __kuser_dmb (*(__kuser_dmb_t*) 0xffff0fa0)
53 typedef int (__kuser_cmpxchg64_t)(const int64_t*, const int64_t*, volatile int64_t*);
54 #define __kuser_cmpxchg64 (*(__kuser_cmpxchg64_t*) 0xffff0f60)
55 #endif
56
57 #define noinline __attribute__((__noinline__))
58
59 // Avoid name mangling so that stacks are more readable.
60 extern "C" {
61
62 void crash1(void);
63 void crashnostack(void);
64
65 int do_action(const char* arg);
66
maybe_abort()67 noinline void maybe_abort() {
68 if (time(0) != 42) {
69 abort();
70 }
71 }
72
73 char* smash_stack_dummy_buf;
smash_stack_dummy_function(volatile int * plen)74 noinline void smash_stack_dummy_function(volatile int* plen) {
75 smash_stack_dummy_buf[*plen] = 0;
76 }
77
78 // This must be marked with "__attribute__ ((noinline))", to ensure the
79 // compiler generates the proper stack guards around this function.
80 // Assign local array address to global variable to force stack guards.
81 // Use another noinline function to corrupt the stack.
smash_stack(volatile int * plen)82 noinline int smash_stack(volatile int* plen) {
83 printf("%s: deliberately corrupting stack...\n", getprogname());
84
85 char buf[128];
86 smash_stack_dummy_buf = buf;
87 // This should corrupt stack guards and make process abort.
88 smash_stack_dummy_function(plen);
89 return 0;
90 }
91
92 #pragma clang diagnostic push
93 #pragma clang diagnostic ignored "-Winfinite-recursion"
94
95 void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
96
overflow_stack(void * p)97 noinline void overflow_stack(void* p) {
98 void* buf[1];
99 buf[0] = p;
100 global = buf;
101 overflow_stack(&buf);
102 }
103
104 #pragma clang diagnostic pop
105
thread_callback(void * raw_arg)106 noinline void* thread_callback(void* raw_arg) {
107 const char* arg = reinterpret_cast<const char*>(raw_arg);
108 return reinterpret_cast<void*>(static_cast<uintptr_t>(do_action(arg)));
109 }
110
do_action_on_thread(const char * arg)111 noinline int do_action_on_thread(const char* arg) {
112 pthread_t t;
113 pthread_create(&t, nullptr, thread_callback, const_cast<char*>(arg));
114 void* result = nullptr;
115 pthread_join(t, &result);
116 return reinterpret_cast<uintptr_t>(result);
117 }
118
crash_null()119 noinline int crash_null() {
120 int (*null_func)() = nullptr;
121 return null_func();
122 }
123
crash3(int a)124 noinline int crash3(int a) {
125 *reinterpret_cast<int*>(0xdead) = a;
126 return a*4;
127 }
128
crash2(int a)129 noinline int crash2(int a) {
130 a = crash3(a) + 2;
131 return a*3;
132 }
133
crash(int a)134 noinline int crash(int a) {
135 a = crash2(a) + 1;
136 return a*2;
137 }
138
139 #pragma clang diagnostic push
140 #pragma clang diagnostic ignored "-Wfree-nonheap-object"
141
abuse_heap()142 noinline void abuse_heap() {
143 char buf[16];
144 free(buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
145 }
146 #pragma clang diagnostic pop
147
leak()148 noinline void leak() {
149 while (true) {
150 void* mapping =
151 mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
152 static_cast<volatile char*>(mapping)[0] = 'a';
153 }
154 }
155
sigsegv_non_null()156 noinline void sigsegv_non_null() {
157 int* a = (int *)(&do_action);
158 *a = 42;
159 }
160
fprintf_null()161 noinline void fprintf_null() {
162 fprintf(nullptr, "oops");
163 }
164
readdir_null()165 noinline void readdir_null() {
166 readdir(nullptr);
167 }
168
strlen_null()169 noinline int strlen_null() {
170 char* sneaky_null = nullptr;
171 return strlen(sneaky_null);
172 }
173
usage()174 static int usage() {
175 fprintf(stderr, "usage: %s KIND\n", getprogname());
176 fprintf(stderr, "\n");
177 fprintf(stderr, "where KIND is:\n");
178 fprintf(stderr, " smash-stack overwrite a -fstack-protector guard\n");
179 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
180 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
181 fprintf(stderr, "\n");
182 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
183 fprintf(stderr, " call-null cause a crash by calling through a nullptr\n");
184 fprintf(stderr, " leak leak memory until we get OOM-killed\n");
185 fprintf(stderr, "\n");
186 fprintf(stderr, " abort call abort()\n");
187 fprintf(stderr, " abort_with_msg call abort() setting an abort message\n");
188 fprintf(stderr, " abort_with_null_msg call abort() setting a null abort message\n");
189 fprintf(stderr, " assert call assert() without a function\n");
190 fprintf(stderr, " assert2 call assert() with a function\n");
191 fprintf(stderr, " exit call exit(1)\n");
192 fprintf(stderr, "\n");
193 fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
194 fprintf(stderr, " fdsan_file close a file descriptor that's owned by a FILE*\n");
195 fprintf(stderr, " fdsan_dir close a file descriptor that's owned by a DIR*\n");
196 fprintf(stderr, " seccomp fail a seccomp check\n");
197 #if defined(__arm__)
198 fprintf(stderr, " kuser_helper_version call kuser_helper_version\n");
199 fprintf(stderr, " kuser_get_tls call kuser_get_tls\n");
200 fprintf(stderr, " kuser_cmpxchg call kuser_cmpxchg\n");
201 fprintf(stderr, " kuser_memory_barrier call kuser_memory_barrier\n");
202 fprintf(stderr, " kuser_cmpxchg64 call kuser_cmpxchg64\n");
203 #endif
204 fprintf(stderr, " xom read execute-only memory\n");
205 fprintf(stderr, "\n");
206 fprintf(stderr, " LOG_ALWAYS_FATAL call liblog LOG_ALWAYS_FATAL\n");
207 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call liblog LOG_ALWAYS_FATAL_IF\n");
208 fprintf(stderr, " LOG-FATAL call libbase LOG(FATAL)\n");
209 fprintf(stderr, "\n");
210 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
211 fprintf(stderr, " SIGILL cause a SIGILL\n");
212 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
213 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
214 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
215 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
216 fprintf(stderr, "\n");
217 fprintf(stderr, " fprintf-NULL pass a null pointer to fprintf\n");
218 fprintf(stderr, " readdir-NULL pass a null pointer to readdir\n");
219 fprintf(stderr, " strlen-NULL pass a null pointer to strlen\n");
220 fprintf(stderr, " pthread_join-NULL pass a null pointer to pthread_join\n");
221 fprintf(stderr, "\n");
222 fprintf(stderr, " no_new_privs set PR_SET_NO_NEW_PRIVS and then abort\n");
223 fprintf(stderr, "\n");
224 fprintf(stderr, "prefix any of the above with 'thread-' to run on a new thread\n");
225 fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n");
226 fprintf(stderr, "all available file descriptors before crashing.\n");
227 fprintf(stderr, "prefix any of the above with 'wait-' to wait until input is received on stdin\n");
228
229 return EXIT_FAILURE;
230 }
231
do_action(const char * arg)232 noinline int do_action(const char* arg) {
233 // Prefixes.
234 if (!strncmp(arg, "wait-", strlen("wait-"))) {
235 char buf[1];
236 UNUSED(TEMP_FAILURE_RETRY(read(STDIN_FILENO, buf, sizeof(buf))));
237 return do_action(arg + strlen("wait-"));
238 } else if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) {
239 errno = 0;
240 while (errno != EMFILE) {
241 open("/dev/null", O_RDONLY);
242 }
243 return do_action(arg + strlen("exhaustfd-"));
244 } else if (!strncmp(arg, "thread-", strlen("thread-"))) {
245 return do_action_on_thread(arg + strlen("thread-"));
246 }
247
248 // Actions.
249 if (!strcasecmp(arg, "SIGSEGV-non-null")) {
250 sigsegv_non_null();
251 } else if (!strcasecmp(arg, "smash-stack")) {
252 volatile int len = 128;
253 return smash_stack(&len);
254 } else if (!strcasecmp(arg, "stack-overflow")) {
255 overflow_stack(nullptr);
256 } else if (!strcasecmp(arg, "nostack")) {
257 crashnostack();
258 } else if (!strcasecmp(arg, "exit")) {
259 exit(1);
260 } else if (!strcasecmp(arg, "call-null")) {
261 return crash_null();
262 } else if (!strcasecmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
263 return crash(42);
264 } else if (!strcasecmp(arg, "abort")) {
265 maybe_abort();
266 } else if (!strcasecmp(arg, "abort_with_msg")) {
267 android_set_abort_message("Aborting due to crasher");
268 maybe_abort();
269 } else if (!strcasecmp(arg, "abort_with_null")) {
270 android_set_abort_message(nullptr);
271 maybe_abort();
272 } else if (!strcasecmp(arg, "assert")) {
273 __assert("some_file.c", 123, "false");
274 } else if (!strcasecmp(arg, "assert2")) {
275 __assert2("some_file.c", 123, "some_function", "false");
276 #if !defined(__clang_analyzer__)
277 } else if (!strcasecmp(arg, "fortify")) {
278 // FORTIFY is disabled when running clang-tidy and other tools, so this
279 // shouldn't depend on internal implementation details of it.
280 char buf[10];
281 __read_chk(-1, buf, 32, 10);
282 while (true) pause();
283 #endif
284 } else if (!strcasecmp(arg, "fdsan_file")) {
285 FILE* f = fopen("/dev/null", "r");
286 close(fileno(f));
287 } else if (!strcasecmp(arg, "fdsan_dir")) {
288 DIR* d = opendir("/dev/");
289 close(dirfd(d));
290 } else if (!strcasecmp(arg, "LOG(FATAL)")) {
291 LOG(FATAL) << "hello " << 123;
292 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL")) {
293 LOG_ALWAYS_FATAL("hello %s", "world");
294 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL_IF")) {
295 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
296 } else if (!strcasecmp(arg, "SIGFPE")) {
297 raise(SIGFPE);
298 return EXIT_SUCCESS;
299 } else if (!strcasecmp(arg, "SIGILL")) {
300 #if defined(__aarch64__)
301 __asm__ volatile(".word 0\n");
302 #elif defined(__arm__)
303 __asm__ volatile(".word 0xe7f0def0\n");
304 #elif defined(__i386__) || defined(__x86_64__)
305 __asm__ volatile("ud2\n");
306 #else
307 #error
308 #endif
309 } else if (!strcasecmp(arg, "SIGTRAP")) {
310 raise(SIGTRAP);
311 return EXIT_SUCCESS;
312 } else if (!strcasecmp(arg, "fprintf-NULL")) {
313 fprintf_null();
314 } else if (!strcasecmp(arg, "readdir-NULL")) {
315 readdir_null();
316 } else if (!strcasecmp(arg, "strlen-NULL")) {
317 return strlen_null();
318 } else if (!strcasecmp(arg, "pthread_join-NULL")) {
319 return pthread_join(0, nullptr);
320 } else if (!strcasecmp(arg, "heap-usage")) {
321 abuse_heap();
322 } else if (!strcasecmp(arg, "leak")) {
323 leak();
324 } else if (!strcasecmp(arg, "SIGSEGV-unmapped")) {
325 char* map = reinterpret_cast<char*>(
326 mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0));
327 munmap(map, sizeof(int));
328 map[0] = '8';
329 } else if (!strcasecmp(arg, "seccomp")) {
330 set_system_seccomp_filter();
331 syscall(99999);
332 #if defined(__LP64__)
333 } else if (!strcasecmp(arg, "xom")) {
334 // Try to read part of our code, which will fail if XOM is active.
335 printf("*%lx = %lx\n", reinterpret_cast<long>(usage), *reinterpret_cast<long*>(usage));
336 #endif
337 #if defined(__arm__)
338 } else if (!strcasecmp(arg, "kuser_helper_version")) {
339 return __kuser_helper_version;
340 } else if (!strcasecmp(arg, "kuser_get_tls")) {
341 return !__kuser_get_tls();
342 } else if (!strcasecmp(arg, "kuser_cmpxchg")) {
343 return __kuser_cmpxchg(0, 0, 0);
344 } else if (!strcasecmp(arg, "kuser_memory_barrier")) {
345 __kuser_dmb();
346 } else if (!strcasecmp(arg, "kuser_cmpxchg64")) {
347 return __kuser_cmpxchg64(0, 0, 0);
348 #endif
349 } else if (!strcasecmp(arg, "no_new_privs")) {
350 if (prctl(PR_SET_NO_NEW_PRIVS, 1) != 0) {
351 fprintf(stderr, "prctl(PR_SET_NO_NEW_PRIVS, 1) failed: %s\n", strerror(errno));
352 return EXIT_SUCCESS;
353 }
354 abort();
355 } else {
356 return usage();
357 }
358
359 fprintf(stderr, "%s: exiting normally!\n", getprogname());
360 return EXIT_SUCCESS;
361 }
362
main(int argc,char ** argv)363 int main(int argc, char** argv) {
364 #if defined(STATIC_CRASHER)
365 debuggerd_callbacks_t callbacks = {
366 .get_process_info = []() {
367 static struct {
368 size_t size;
369 char msg[32];
370 } msg;
371
372 msg.size = strlen("dummy abort message");
373 memcpy(msg.msg, "dummy abort message", strlen("dummy abort message"));
374 return debugger_process_info{
375 .abort_msg = reinterpret_cast<void*>(&msg),
376 };
377 },
378 .post_dump = nullptr
379 };
380 debuggerd_init(&callbacks);
381 #endif
382
383 if (argc == 1) crash1();
384 else if (argc == 2) return do_action(argv[1]);
385
386 return usage();
387 }
388
389 };
390