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 FILE* sneaky_null = nullptr;
163 fprintf(sneaky_null, "oops");
164 }
165
readdir_null()166 noinline void readdir_null() {
167 DIR* sneaky_null = nullptr;
168 readdir(sneaky_null);
169 }
170
strlen_null()171 noinline int strlen_null() {
172 char* sneaky_null = nullptr;
173 return strlen(sneaky_null);
174 }
175
usage()176 static int usage() {
177 fprintf(stderr, "usage: %s KIND\n", getprogname());
178 fprintf(stderr, "\n");
179 fprintf(stderr, "where KIND is:\n");
180 fprintf(stderr, " smash-stack overwrite a -fstack-protector guard\n");
181 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
182 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
183 fprintf(stderr, "\n");
184 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
185 fprintf(stderr, " call-null cause a crash by calling through a nullptr\n");
186 fprintf(stderr, " leak leak memory until we get OOM-killed\n");
187 fprintf(stderr, "\n");
188 fprintf(stderr, " abort call abort()\n");
189 fprintf(stderr, " abort_with_msg call abort() setting an abort message\n");
190 fprintf(stderr, " abort_with_null_msg call abort() setting a null abort message\n");
191 fprintf(stderr, " assert call assert() without a function\n");
192 fprintf(stderr, " assert2 call assert() with a function\n");
193 fprintf(stderr, " exit call exit(1)\n");
194 fprintf(stderr, "\n");
195 fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
196 fprintf(stderr, " fdsan_file close a file descriptor that's owned by a FILE*\n");
197 fprintf(stderr, " fdsan_dir close a file descriptor that's owned by a DIR*\n");
198 fprintf(stderr, " seccomp fail a seccomp check\n");
199 #if defined(__arm__)
200 fprintf(stderr, " kuser_helper_version call kuser_helper_version\n");
201 fprintf(stderr, " kuser_get_tls call kuser_get_tls\n");
202 fprintf(stderr, " kuser_cmpxchg call kuser_cmpxchg\n");
203 fprintf(stderr, " kuser_memory_barrier call kuser_memory_barrier\n");
204 fprintf(stderr, " kuser_cmpxchg64 call kuser_cmpxchg64\n");
205 #endif
206 fprintf(stderr, " xom read execute-only memory\n");
207 fprintf(stderr, "\n");
208 fprintf(stderr, " LOG_ALWAYS_FATAL call liblog LOG_ALWAYS_FATAL\n");
209 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call liblog LOG_ALWAYS_FATAL_IF\n");
210 fprintf(stderr, " LOG-FATAL call libbase LOG(FATAL)\n");
211 fprintf(stderr, "\n");
212 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
213 fprintf(stderr, " SIGILL cause a SIGILL\n");
214 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
215 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
216 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
217 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
218 fprintf(stderr, "\n");
219 fprintf(stderr, " fprintf-NULL pass a null pointer to fprintf\n");
220 fprintf(stderr, " readdir-NULL pass a null pointer to readdir\n");
221 fprintf(stderr, " strlen-NULL pass a null pointer to strlen\n");
222 fprintf(stderr, " pthread_join-NULL pass a null pointer to pthread_join\n");
223 fprintf(stderr, "\n");
224 fprintf(stderr, " no_new_privs set PR_SET_NO_NEW_PRIVS and then abort\n");
225 fprintf(stderr, "\n");
226 fprintf(stderr, "prefix any of the above with 'thread-' to run on a new thread\n");
227 fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n");
228 fprintf(stderr, "all available file descriptors before crashing.\n");
229 fprintf(stderr, "prefix any of the above with 'wait-' to wait until input is received on stdin\n");
230
231 return EXIT_FAILURE;
232 }
233
do_action(const char * arg)234 noinline int do_action(const char* arg) {
235 // Prefixes.
236 if (!strncmp(arg, "wait-", strlen("wait-"))) {
237 char buf[1];
238 UNUSED(TEMP_FAILURE_RETRY(read(STDIN_FILENO, buf, sizeof(buf))));
239 return do_action(arg + strlen("wait-"));
240 } else if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) {
241 errno = 0;
242 while (errno != EMFILE) {
243 open("/dev/null", O_RDONLY);
244 }
245 return do_action(arg + strlen("exhaustfd-"));
246 } else if (!strncmp(arg, "thread-", strlen("thread-"))) {
247 return do_action_on_thread(arg + strlen("thread-"));
248 }
249
250 // Actions.
251 if (!strcasecmp(arg, "SIGSEGV-non-null")) {
252 sigsegv_non_null();
253 } else if (!strcasecmp(arg, "smash-stack")) {
254 volatile int len = 128;
255 return smash_stack(&len);
256 } else if (!strcasecmp(arg, "stack-overflow")) {
257 overflow_stack(nullptr);
258 } else if (!strcasecmp(arg, "nostack")) {
259 crashnostack();
260 } else if (!strcasecmp(arg, "exit")) {
261 exit(1);
262 } else if (!strcasecmp(arg, "call-null")) {
263 return crash_null();
264 } else if (!strcasecmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
265 return crash(42);
266 } else if (!strcasecmp(arg, "abort")) {
267 maybe_abort();
268 } else if (!strcasecmp(arg, "abort_with_msg")) {
269 android_set_abort_message("Aborting due to crasher");
270 maybe_abort();
271 } else if (!strcasecmp(arg, "abort_with_null")) {
272 android_set_abort_message(nullptr);
273 maybe_abort();
274 } else if (!strcasecmp(arg, "assert")) {
275 __assert("some_file.c", 123, "false");
276 } else if (!strcasecmp(arg, "assert2")) {
277 __assert2("some_file.c", 123, "some_function", "false");
278 #if !defined(__clang_analyzer__)
279 } else if (!strcasecmp(arg, "fortify")) {
280 // FORTIFY is disabled when running clang-tidy and other tools, so this
281 // shouldn't depend on internal implementation details of it.
282 char buf[10];
283 __read_chk(-1, buf, 32, 10);
284 while (true) pause();
285 #endif
286 } else if (!strcasecmp(arg, "fdsan_file")) {
287 FILE* f = fopen("/dev/null", "r");
288 close(fileno(f));
289 } else if (!strcasecmp(arg, "fdsan_dir")) {
290 DIR* d = opendir("/dev/");
291 close(dirfd(d));
292 } else if (!strcasecmp(arg, "LOG(FATAL)")) {
293 LOG(FATAL) << "hello " << 123;
294 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL")) {
295 LOG_ALWAYS_FATAL("hello %s", "world");
296 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL_IF")) {
297 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
298 } else if (!strcasecmp(arg, "SIGFPE")) {
299 raise(SIGFPE);
300 return EXIT_SUCCESS;
301 } else if (!strcasecmp(arg, "SIGILL")) {
302 #if defined(__aarch64__)
303 __asm__ volatile(".word 0\n");
304 #elif defined(__arm__)
305 __asm__ volatile(".word 0xe7f0def0\n");
306 #elif defined(__i386__) || defined(__x86_64__)
307 __asm__ volatile("ud2\n");
308 #elif defined(__riscv)
309 __asm__ volatile("unimp\n");
310 #else
311 #error
312 #endif
313 } else if (!strcasecmp(arg, "SIGTRAP")) {
314 raise(SIGTRAP);
315 return EXIT_SUCCESS;
316 } else if (!strcasecmp(arg, "fprintf-NULL")) {
317 fprintf_null();
318 } else if (!strcasecmp(arg, "readdir-NULL")) {
319 readdir_null();
320 } else if (!strcasecmp(arg, "strlen-NULL")) {
321 return strlen_null();
322 } else if (!strcasecmp(arg, "pthread_join-NULL")) {
323 return pthread_join(0, nullptr);
324 } else if (!strcasecmp(arg, "heap-usage")) {
325 abuse_heap();
326 } else if (!strcasecmp(arg, "leak")) {
327 leak();
328 } else if (!strcasecmp(arg, "SIGSEGV-unmapped")) {
329 char* map = reinterpret_cast<char*>(
330 mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0));
331 munmap(map, sizeof(int));
332 map[0] = '8';
333 } else if (!strcasecmp(arg, "seccomp")) {
334 set_system_seccomp_filter();
335 syscall(99999);
336 #if defined(__LP64__)
337 } else if (!strcasecmp(arg, "xom")) {
338 // Try to read part of our code, which will fail if XOM is active.
339 printf("*%lx = %lx\n", reinterpret_cast<long>(usage), *reinterpret_cast<long*>(usage));
340 #endif
341 #if defined(__arm__)
342 } else if (!strcasecmp(arg, "kuser_helper_version")) {
343 return __kuser_helper_version;
344 } else if (!strcasecmp(arg, "kuser_get_tls")) {
345 return !__kuser_get_tls();
346 } else if (!strcasecmp(arg, "kuser_cmpxchg")) {
347 return __kuser_cmpxchg(0, 0, 0);
348 } else if (!strcasecmp(arg, "kuser_memory_barrier")) {
349 __kuser_dmb();
350 } else if (!strcasecmp(arg, "kuser_cmpxchg64")) {
351 return __kuser_cmpxchg64(0, 0, 0);
352 #endif
353 } else if (!strcasecmp(arg, "no_new_privs")) {
354 if (prctl(PR_SET_NO_NEW_PRIVS, 1) != 0) {
355 fprintf(stderr, "prctl(PR_SET_NO_NEW_PRIVS, 1) failed: %s\n", strerror(errno));
356 return EXIT_SUCCESS;
357 }
358 abort();
359 } else {
360 return usage();
361 }
362
363 fprintf(stderr, "%s: exiting normally!\n", getprogname());
364 return EXIT_SUCCESS;
365 }
366
main(int argc,char ** argv)367 int main(int argc, char** argv) {
368 #if defined(STATIC_CRASHER)
369 debuggerd_callbacks_t callbacks = {
370 .get_process_info = []() {
371 static struct {
372 size_t size;
373 char msg[32];
374 } msg;
375
376 msg.size = strlen("dummy abort message");
377 memcpy(msg.msg, "dummy abort message", strlen("dummy abort message"));
378 return debugger_process_info{
379 .abort_msg = reinterpret_cast<void*>(&msg),
380 };
381 },
382 .post_dump = nullptr
383 };
384 debuggerd_init(&callbacks);
385 #endif
386
387 if (argc == 1) crash1();
388 else if (argc == 2) return do_action(argv[1]);
389
390 return usage();
391 }
392
393 };
394