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