• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "debuggerd/handler.h"
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <linux/futex.h>
35 #include <pthread.h>
36 #include <sched.h>
37 #include <signal.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/capability.h>
43 #include <sys/mman.h>
44 #include <sys/prctl.h>
45 #include <sys/socket.h>
46 #include <sys/syscall.h>
47 #include <sys/uio.h>
48 #include <sys/un.h>
49 #include <sys/wait.h>
50 #include <unistd.h>
51 
52 #include <android-base/unique_fd.h>
53 #include <async_safe/log.h>
54 #include <cutils/properties.h>
55 
56 #include <libdebuggerd/utility.h>
57 
58 #include "dump_type.h"
59 #include "protocol.h"
60 
61 #include "handler/fallback.h"
62 
63 using android::base::Pipe;
64 
65 // We muck with our fds in a 'thread' that doesn't share the same fd table.
66 // Close fds in that thread with a raw close syscall instead of going through libc.
67 struct FdsanBypassCloser {
CloseFdsanBypassCloser68   static void Close(int fd) {
69     syscall(__NR_close, fd);
70   }
71 };
72 
73 using unique_fd = android::base::unique_fd_impl<FdsanBypassCloser>;
74 
75 // see man(2) prctl, specifically the section about PR_GET_NAME
76 #define MAX_TASK_NAME_LEN (16)
77 
78 #if defined(__LP64__)
79 #define CRASH_DUMP_NAME "crash_dump64"
80 #else
81 #define CRASH_DUMP_NAME "crash_dump32"
82 #endif
83 
84 #define CRASH_DUMP_PATH "/system/bin/" CRASH_DUMP_NAME
85 
86 // Wrappers that directly invoke the respective syscalls, in case the cached values are invalid.
87 #pragma GCC poison getpid gettid
__getpid()88 static pid_t __getpid() {
89   return syscall(__NR_getpid);
90 }
91 
__gettid()92 static pid_t __gettid() {
93   return syscall(__NR_gettid);
94 }
95 
futex_wait(volatile void * ftx,int value)96 static inline void futex_wait(volatile void* ftx, int value) {
97   syscall(__NR_futex, ftx, FUTEX_WAIT, value, nullptr, nullptr, 0);
98 }
99 
100 class ErrnoRestorer {
101  public:
ErrnoRestorer()102   ErrnoRestorer() : saved_errno_(errno) {
103   }
104 
~ErrnoRestorer()105   ~ErrnoRestorer() {
106     errno = saved_errno_;
107   }
108 
109  private:
110   int saved_errno_;
111 };
112 
113 extern "C" void* android_fdsan_get_fd_table();
114 extern "C" void debuggerd_fallback_handler(siginfo_t*, ucontext_t*, void*);
115 
116 static debuggerd_callbacks_t g_callbacks;
117 
118 // Mutex to ensure only one crashing thread dumps itself.
119 static pthread_mutex_t crash_mutex = PTHREAD_MUTEX_INITIALIZER;
120 
121 // Don't use async_safe_fatal because it exits via abort, which might put us back into
122 // a signal handler.
fatal(const char * fmt,...)123 static void __noreturn __printflike(1, 2) fatal(const char* fmt, ...) {
124   va_list args;
125   va_start(args, fmt);
126   async_safe_format_log_va_list(ANDROID_LOG_FATAL, "libc", fmt, args);
127   _exit(1);
128 }
129 
fatal_errno(const char * fmt,...)130 static void __noreturn __printflike(1, 2) fatal_errno(const char* fmt, ...) {
131   int err = errno;
132   va_list args;
133   va_start(args, fmt);
134 
135   char buf[256];
136   async_safe_format_buffer_va_list(buf, sizeof(buf), fmt, args);
137   fatal("%s: %s", buf, strerror(err));
138 }
139 
get_main_thread_name(char * buf,size_t len)140 static bool get_main_thread_name(char* buf, size_t len) {
141   unique_fd fd(open("/proc/self/comm", O_RDONLY | O_CLOEXEC));
142   if (fd == -1) {
143     return false;
144   }
145 
146   ssize_t rc = read(fd, buf, len);
147   if (rc == -1) {
148     return false;
149   } else if (rc == 0) {
150     // Should never happen?
151     return false;
152   }
153 
154   // There's a trailing newline, replace it with a NUL.
155   buf[rc - 1] = '\0';
156   return true;
157 }
158 
159 /*
160  * Writes a summary of the signal to the log file.  We do this so that, if
161  * for some reason we're not able to contact debuggerd, there is still some
162  * indication of the failure in the log.
163  *
164  * We could be here as a result of native heap corruption, or while a
165  * mutex is being held, so we don't want to use any libc functions that
166  * could allocate memory or hold a lock.
167  */
log_signal_summary(const siginfo_t * info)168 static void log_signal_summary(const siginfo_t* info) {
169   char thread_name[MAX_TASK_NAME_LEN + 1];  // one more for termination
170   if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(thread_name), 0, 0, 0) != 0) {
171     strcpy(thread_name, "<name unknown>");
172   } else {
173     // short names are null terminated by prctl, but the man page
174     // implies that 16 byte names are not.
175     thread_name[MAX_TASK_NAME_LEN] = 0;
176   }
177 
178   if (info->si_signo == DEBUGGER_SIGNAL) {
179     async_safe_format_log(ANDROID_LOG_INFO, "libc", "Requested dump for tid %d (%s)", __gettid(),
180                           thread_name);
181     return;
182   }
183 
184   // Many signals don't have an address or sender.
185   char addr_desc[32] = "";  // ", fault addr 0x1234"
186   if (signal_has_si_addr(info)) {
187     async_safe_format_buffer(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr);
188   }
189   pid_t self_pid = __getpid();
190   char sender_desc[32] = {};  // " from pid 1234, uid 666"
191   if (signal_has_sender(info, self_pid)) {
192     get_signal_sender(sender_desc, sizeof(sender_desc), info);
193   }
194 
195   char main_thread_name[MAX_TASK_NAME_LEN + 1];
196   if (!get_main_thread_name(main_thread_name, sizeof(main_thread_name))) {
197     strncpy(main_thread_name, "<unknown>", sizeof(main_thread_name));
198   }
199 
200   async_safe_format_log(ANDROID_LOG_FATAL, "libc",
201                         "Fatal signal %d (%s), code %d (%s%s)%s in tid %d (%s), pid %d (%s)",
202                         info->si_signo, get_signame(info), info->si_code, get_sigcode(info),
203                         sender_desc, addr_desc, __gettid(), thread_name, self_pid, main_thread_name);
204 }
205 
206 /*
207  * Returns true if the handler for signal "signum" has SA_SIGINFO set.
208  */
have_siginfo(int signum)209 static bool have_siginfo(int signum) {
210   struct sigaction old_action;
211   if (sigaction(signum, nullptr, &old_action) < 0) {
212     async_safe_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
213                           strerror(errno));
214     return false;
215   }
216   return (old_action.sa_flags & SA_SIGINFO) != 0;
217 }
218 
raise_caps()219 static void raise_caps() {
220   // Raise CapInh to match CapPrm, so that we can set the ambient bits.
221   __user_cap_header_struct capheader;
222   memset(&capheader, 0, sizeof(capheader));
223   capheader.version = _LINUX_CAPABILITY_VERSION_3;
224   capheader.pid = 0;
225 
226   __user_cap_data_struct capdata[2];
227   if (capget(&capheader, &capdata[0]) == -1) {
228     fatal_errno("capget failed");
229   }
230 
231   if (capdata[0].permitted != capdata[0].inheritable ||
232       capdata[1].permitted != capdata[1].inheritable) {
233     capdata[0].inheritable = capdata[0].permitted;
234     capdata[1].inheritable = capdata[1].permitted;
235 
236     if (capset(&capheader, &capdata[0]) == -1) {
237       async_safe_format_log(ANDROID_LOG_ERROR, "libc", "capset failed: %s", strerror(errno));
238     }
239   }
240 
241   // Set the ambient capability bits so that crash_dump gets all of our caps and can ptrace us.
242   uint64_t capmask = capdata[0].inheritable;
243   capmask |= static_cast<uint64_t>(capdata[1].inheritable) << 32;
244   for (unsigned long i = 0; i < 64; ++i) {
245     if (capmask & (1ULL << i)) {
246       if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, 0) != 0) {
247         async_safe_format_log(ANDROID_LOG_ERROR, "libc",
248                               "failed to raise ambient capability %lu: %s", i, strerror(errno));
249       }
250     }
251   }
252 }
253 
__fork()254 static pid_t __fork() {
255   return clone(nullptr, nullptr, 0, nullptr);
256 }
257 
258 // Double-clone, with CLONE_FILES to share the file descriptor table for kcmp validation.
259 // Returns 0 in the orphaned child, the pid of the orphan in the original process, or -1 on failure.
create_vm_process()260 static void create_vm_process() {
261   pid_t first = clone(nullptr, nullptr, CLONE_FILES, nullptr);
262   if (first == -1) {
263     fatal_errno("failed to clone vm process");
264   } else if (first == 0) {
265     drop_capabilities();
266 
267     if (clone(nullptr, nullptr, CLONE_FILES, nullptr) == -1) {
268       _exit(errno);
269     }
270 
271     // crash_dump is ptracing both sides of the fork; it'll let the parent exit,
272     // but keep the orphan stopped to peek at its memory.
273 
274     // There appears to be a bug in the kernel where our death causes SIGHUP to
275     // be sent to our process group if we exit while it has stopped jobs (e.g.
276     // because of wait_for_gdb). Use setsid to create a new process group to
277     // avoid hitting this.
278     setsid();
279 
280     _exit(0);
281   }
282 
283   int status;
284   if (TEMP_FAILURE_RETRY(waitpid(first, &status, __WCLONE)) != first) {
285     fatal_errno("failed to waitpid in double fork");
286   } else if (!WIFEXITED(status)) {
287     fatal("intermediate process didn't exit cleanly in double fork (status = %d)", status);
288   } else if (WEXITSTATUS(status)) {
289     fatal("second clone failed: %s", strerror(WEXITSTATUS(status)));
290   }
291 }
292 
293 struct debugger_thread_info {
294   pid_t crashing_tid;
295   pid_t pseudothread_tid;
296   siginfo_t* siginfo;
297   void* ucontext;
298   uintptr_t abort_msg;
299   uintptr_t fdsan_table;
300 };
301 
302 // Logging and contacting debuggerd requires free file descriptors, which we might not have.
303 // Work around this by spawning a "thread" that shares its parent's address space, but not its file
304 // descriptor table, so that we can close random file descriptors without affecting the original
305 // process. Note that this doesn't go through pthread_create, so TLS is shared with the spawning
306 // process.
307 static void* pseudothread_stack;
308 
get_dump_type(const debugger_thread_info * thread_info)309 static DebuggerdDumpType get_dump_type(const debugger_thread_info* thread_info) {
310   if (thread_info->siginfo->si_signo == DEBUGGER_SIGNAL &&
311       thread_info->siginfo->si_value.sival_int) {
312     return kDebuggerdNativeBacktrace;
313   }
314 
315   return kDebuggerdTombstone;
316 }
317 
debuggerd_dispatch_pseudothread(void * arg)318 static int debuggerd_dispatch_pseudothread(void* arg) {
319   debugger_thread_info* thread_info = static_cast<debugger_thread_info*>(arg);
320 
321   for (int i = 0; i < 1024; ++i) {
322     // Don't use close to avoid bionic's file descriptor ownership checks.
323     syscall(__NR_close, i);
324   }
325 
326   int devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
327   if (devnull == -1) {
328     fatal_errno("failed to open /dev/null");
329   } else if (devnull != 0) {
330     fatal_errno("expected /dev/null fd to be 0, actually %d", devnull);
331   }
332 
333   // devnull will be 0.
334   TEMP_FAILURE_RETRY(dup2(devnull, 1));
335   TEMP_FAILURE_RETRY(dup2(devnull, 2));
336 
337   unique_fd input_read, input_write;
338   unique_fd output_read, output_write;
339   if (!Pipe(&input_read, &input_write) != 0 || !Pipe(&output_read, &output_write)) {
340     fatal_errno("failed to create pipe");
341   }
342 
343   // ucontext_t is absurdly large on AArch64, so piece it together manually with writev.
344   uint32_t version = 2;
345   constexpr size_t expected = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2);
346 
347   errno = 0;
348   if (fcntl(output_write.get(), F_SETPIPE_SZ, expected) < static_cast<int>(expected)) {
349     fatal_errno("failed to set pipe buffer size");
350   }
351 
352   struct iovec iovs[5] = {
353       {.iov_base = &version, .iov_len = sizeof(version)},
354       {.iov_base = thread_info->siginfo, .iov_len = sizeof(siginfo_t)},
355       {.iov_base = thread_info->ucontext, .iov_len = sizeof(ucontext_t)},
356       {.iov_base = &thread_info->abort_msg, .iov_len = sizeof(uintptr_t)},
357       {.iov_base = &thread_info->fdsan_table, .iov_len = sizeof(uintptr_t)},
358   };
359 
360   ssize_t rc = TEMP_FAILURE_RETRY(writev(output_write.get(), iovs, 5));
361   if (rc == -1) {
362     fatal_errno("failed to write crash info");
363   } else if (rc != expected) {
364     fatal("failed to write crash info, wrote %zd bytes, expected %zd", rc, expected);
365   }
366 
367   // Don't use fork(2) to avoid calling pthread_atfork handlers.
368   pid_t crash_dump_pid = __fork();
369   if (crash_dump_pid == -1) {
370     async_safe_format_log(ANDROID_LOG_FATAL, "libc",
371                           "failed to fork in debuggerd signal handler: %s", strerror(errno));
372   } else if (crash_dump_pid == 0) {
373     TEMP_FAILURE_RETRY(dup2(input_write.get(), STDOUT_FILENO));
374     TEMP_FAILURE_RETRY(dup2(output_read.get(), STDIN_FILENO));
375     input_read.reset();
376     input_write.reset();
377     output_read.reset();
378     output_write.reset();
379 
380     raise_caps();
381 
382     char main_tid[10];
383     char pseudothread_tid[10];
384     char debuggerd_dump_type[10];
385     async_safe_format_buffer(main_tid, sizeof(main_tid), "%d", thread_info->crashing_tid);
386     async_safe_format_buffer(pseudothread_tid, sizeof(pseudothread_tid), "%d",
387                              thread_info->pseudothread_tid);
388     async_safe_format_buffer(debuggerd_dump_type, sizeof(debuggerd_dump_type), "%d",
389                              get_dump_type(thread_info));
390 
391     execle(CRASH_DUMP_PATH, CRASH_DUMP_NAME, main_tid, pseudothread_tid, debuggerd_dump_type,
392            nullptr, nullptr);
393     async_safe_format_log(ANDROID_LOG_FATAL, "libc", "failed to exec crash_dump helper: %s",
394                           strerror(errno));
395     return 1;
396   }
397 
398   input_write.reset();
399   output_read.reset();
400 
401   // crash_dump will ptrace and pause all of our threads, and then write to the pipe to tell
402   // us to fork off a process to read memory from.
403   char buf[4];
404   rc = TEMP_FAILURE_RETRY(read(input_read.get(), &buf, sizeof(buf)));
405   if (rc == -1) {
406     async_safe_format_log(ANDROID_LOG_FATAL, "libc", "read of IPC pipe failed: %s", strerror(errno));
407     return 1;
408   } else if (rc == 0) {
409     async_safe_format_log(ANDROID_LOG_FATAL, "libc", "crash_dump helper failed to exec");
410     return 1;
411   } else if (rc != 1) {
412     async_safe_format_log(ANDROID_LOG_FATAL, "libc",
413                           "read of IPC pipe returned unexpected value: %zd", rc);
414     return 1;
415   } else if (buf[0] != '\1') {
416     async_safe_format_log(ANDROID_LOG_FATAL, "libc", "crash_dump helper reported failure");
417     return 1;
418   }
419 
420   // crash_dump is ptracing us, fork off a copy of our address space for it to use.
421   create_vm_process();
422 
423   // Don't leave a zombie child.
424   int status;
425   if (TEMP_FAILURE_RETRY(waitpid(crash_dump_pid, &status, 0)) == -1) {
426     async_safe_format_log(ANDROID_LOG_FATAL, "libc", "failed to wait for crash_dump helper: %s",
427                           strerror(errno));
428   } else if (WIFSTOPPED(status) || WIFSIGNALED(status)) {
429     async_safe_format_log(ANDROID_LOG_FATAL, "libc", "crash_dump helper crashed or stopped");
430   }
431 
432   if (thread_info->siginfo->si_signo != DEBUGGER_SIGNAL) {
433     // For crashes, we don't need to minimize pause latency.
434     // Wait for the dump to complete before having the process exit, to avoid being murdered by
435     // ActivityManager or init.
436     TEMP_FAILURE_RETRY(read(input_read, &buf, sizeof(buf)));
437   }
438 
439   return 0;
440 }
441 
resend_signal(siginfo_t * info)442 static void resend_signal(siginfo_t* info) {
443   // Signals can either be fatal or nonfatal.
444   // For fatal signals, crash_dump will send us the signal we crashed with
445   // before resuming us, so that processes using waitpid on us will see that we
446   // exited with the correct exit status (e.g. so that sh will report
447   // "Segmentation fault" instead of "Killed"). For this to work, we need
448   // to deregister our signal handler for that signal before continuing.
449   if (info->si_signo != DEBUGGER_SIGNAL) {
450     signal(info->si_signo, SIG_DFL);
451     int rc = syscall(SYS_rt_tgsigqueueinfo, __getpid(), __gettid(), info->si_signo, info);
452     if (rc != 0) {
453       fatal_errno("failed to resend signal during crash");
454     }
455   }
456 }
457 
458 // Handler that does crash dumping by forking and doing the processing in the child.
459 // Do this by ptracing the relevant thread, and then execing debuggerd to do the actual dump.
debuggerd_signal_handler(int signal_number,siginfo_t * info,void * context)460 static void debuggerd_signal_handler(int signal_number, siginfo_t* info, void* context) {
461   // Make sure we don't change the value of errno, in case a signal comes in between the process
462   // making a syscall and checking errno.
463   ErrnoRestorer restorer;
464 
465   // It's possible somebody cleared the SA_SIGINFO flag, which would mean
466   // our "info" arg holds an undefined value.
467   if (!have_siginfo(signal_number)) {
468     info = nullptr;
469   }
470 
471   struct siginfo dummy_info = {};
472   if (!info) {
473     memset(&dummy_info, 0, sizeof(dummy_info));
474     dummy_info.si_signo = signal_number;
475     dummy_info.si_code = SI_USER;
476     dummy_info.si_pid = __getpid();
477     dummy_info.si_uid = getuid();
478     info = &dummy_info;
479   } else if (info->si_code >= 0 || info->si_code == SI_TKILL) {
480     // rt_tgsigqueueinfo(2)'s documentation appears to be incorrect on kernels
481     // that contain commit 66dd34a (3.9+). The manpage claims to only allow
482     // negative si_code values that are not SI_TKILL, but 66dd34a changed the
483     // check to allow all si_code values in calls coming from inside the house.
484   }
485 
486   void* abort_message = nullptr;
487   uintptr_t si_val = reinterpret_cast<uintptr_t>(info->si_ptr);
488   if (signal_number == DEBUGGER_SIGNAL) {
489     if (info->si_code == SI_QUEUE && info->si_pid == __getpid()) {
490       // Allow for the abort message to be explicitly specified via the sigqueue value.
491       // Keep the bottom bit intact for representing whether we want a backtrace or a tombstone.
492       if (si_val != kDebuggerdFallbackSivalUintptrRequestDump) {
493         abort_message = reinterpret_cast<void*>(si_val & ~1);
494         info->si_ptr = reinterpret_cast<void*>(si_val & 1);
495       }
496     }
497   } else {
498     if (g_callbacks.get_abort_message) {
499       abort_message = g_callbacks.get_abort_message();
500     }
501   }
502 
503   // If sival_int is ~0, it means that the fallback handler has been called
504   // once before and this function is being called again to dump the stack
505   // of a specific thread. It is possible that the prctl call might return 1,
506   // then return 0 in subsequent calls, so check the sival_int to determine if
507   // the fallback handler should be called first.
508   if (si_val == kDebuggerdFallbackSivalUintptrRequestDump ||
509       prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) == 1) {
510     // This check might be racy if another thread sets NO_NEW_PRIVS, but this should be unlikely,
511     // you can only set NO_NEW_PRIVS to 1, and the effect should be at worst a single missing
512     // ANR trace.
513     debuggerd_fallback_handler(info, static_cast<ucontext_t*>(context), abort_message);
514     resend_signal(info);
515     return;
516   }
517 
518   // Only allow one thread to handle a signal at a time.
519   int ret = pthread_mutex_lock(&crash_mutex);
520   if (ret != 0) {
521     async_safe_format_log(ANDROID_LOG_INFO, "libc", "pthread_mutex_lock failed: %s", strerror(ret));
522     return;
523   }
524 
525   log_signal_summary(info);
526 
527   debugger_thread_info thread_info = {
528       .pseudothread_tid = -1,
529       .crashing_tid = __gettid(),
530       .siginfo = info,
531       .ucontext = context,
532       .abort_msg = reinterpret_cast<uintptr_t>(abort_message),
533       .fdsan_table = reinterpret_cast<uintptr_t>(android_fdsan_get_fd_table()),
534   };
535 
536   // Set PR_SET_DUMPABLE to 1, so that crash_dump can ptrace us.
537   int orig_dumpable = prctl(PR_GET_DUMPABLE);
538   if (prctl(PR_SET_DUMPABLE, 1) != 0) {
539     fatal_errno("failed to set dumpable");
540   }
541 
542   // On kernels with yama_ptrace enabled, also allow any process to attach.
543   bool restore_orig_ptracer = true;
544   if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) != 0) {
545     if (errno == EINVAL) {
546       // This kernel does not support PR_SET_PTRACER_ANY, or Yama is not enabled.
547       restore_orig_ptracer = false;
548     } else {
549       fatal_errno("failed to set traceable");
550     }
551   }
552 
553   // Essentially pthread_create without CLONE_FILES, so we still work during file descriptor
554   // exhaustion.
555   pid_t child_pid =
556     clone(debuggerd_dispatch_pseudothread, pseudothread_stack,
557           CLONE_THREAD | CLONE_SIGHAND | CLONE_VM | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID,
558           &thread_info, nullptr, nullptr, &thread_info.pseudothread_tid);
559   if (child_pid == -1) {
560     fatal_errno("failed to spawn debuggerd dispatch thread");
561   }
562 
563   // Wait for the child to start...
564   futex_wait(&thread_info.pseudothread_tid, -1);
565 
566   // and then wait for it to terminate.
567   futex_wait(&thread_info.pseudothread_tid, child_pid);
568 
569   // Restore PR_SET_DUMPABLE to its original value.
570   if (prctl(PR_SET_DUMPABLE, orig_dumpable) != 0) {
571     fatal_errno("failed to restore dumpable");
572   }
573 
574   // Restore PR_SET_PTRACER to its original value.
575   if (restore_orig_ptracer && prctl(PR_SET_PTRACER, 0) != 0) {
576     fatal_errno("failed to restore traceable");
577   }
578 
579   if (info->si_signo == DEBUGGER_SIGNAL) {
580     // If the signal is fatal, don't unlock the mutex to prevent other crashing threads from
581     // starting to dump right before our death.
582     pthread_mutex_unlock(&crash_mutex);
583   } else {
584     // Resend the signal, so that either gdb or the parent's waitpid sees it.
585     resend_signal(info);
586   }
587 }
588 
debuggerd_init(debuggerd_callbacks_t * callbacks)589 void debuggerd_init(debuggerd_callbacks_t* callbacks) {
590   if (callbacks) {
591     g_callbacks = *callbacks;
592   }
593 
594   void* thread_stack_allocation =
595     mmap(nullptr, PAGE_SIZE * 3, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
596   if (thread_stack_allocation == MAP_FAILED) {
597     fatal_errno("failed to allocate debuggerd thread stack");
598   }
599 
600   char* stack = static_cast<char*>(thread_stack_allocation) + PAGE_SIZE;
601   if (mprotect(stack, PAGE_SIZE, PROT_READ | PROT_WRITE) != 0) {
602     fatal_errno("failed to mprotect debuggerd thread stack");
603   }
604 
605   // Stack grows negatively, set it to the last byte in the page...
606   stack = (stack + PAGE_SIZE - 1);
607   // and align it.
608   stack -= 15;
609   pseudothread_stack = stack;
610 
611   struct sigaction action;
612   memset(&action, 0, sizeof(action));
613   sigfillset(&action.sa_mask);
614   action.sa_sigaction = debuggerd_signal_handler;
615   action.sa_flags = SA_RESTART | SA_SIGINFO;
616 
617   // Use the alternate signal stack if available so we can catch stack overflows.
618   action.sa_flags |= SA_ONSTACK;
619   debuggerd_register_handlers(&action);
620 }
621