blob: 6e012899367f7630c27dcf89ca3b3d020505b258 [file] [log] [blame]
Josh Gaocbe70cb2016-10-18 18:17:52 -07001/*
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>
Josh Gaob64dd852017-01-22 18:22:52 -080034#include <linux/futex.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070035#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>
Josh Gao7ae426c2017-02-01 15:33:18 -080042#include <sys/capability.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070043#include <sys/mman.h>
44#include <sys/prctl.h>
45#include <sys/socket.h>
46#include <sys/syscall.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070047#include <sys/uio.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070048#include <sys/un.h>
49#include <sys/wait.h>
50#include <unistd.h>
51
Josh Gaofdf832d2017-08-25 18:00:18 -070052#include <android-base/unique_fd.h>
Christopher Ferrisac225782017-04-25 11:23:10 -070053#include <async_safe/log.h>
Josh Gaoa48b41b2019-12-13 14:11:04 -080054#include <bionic/reserved_signals.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070055#include <cutils/properties.h>
56
57#include <libdebuggerd/utility.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070058
Narayan Kamatha73df602017-05-24 15:07:25 +010059#include "dump_type.h"
Josh Gao2b2ae0c2017-08-21 14:31:17 -070060#include "protocol.h"
Narayan Kamatha73df602017-05-24 15:07:25 +010061
Josh Gao6f9eeec2018-09-12 13:55:47 -070062#include "handler/fallback.h"
63
Josh Gao2b2ae0c2017-08-21 14:31:17 -070064using android::base::Pipe;
Josh Gaoc954ec02018-06-01 15:47:20 -070065
66// We muck with our fds in a 'thread' that doesn't share the same fd table.
67// Close fds in that thread with a raw close syscall instead of going through libc.
68struct FdsanBypassCloser {
69 static void Close(int fd) {
70 syscall(__NR_close, fd);
71 }
72};
73
74using unique_fd = android::base::unique_fd_impl<FdsanBypassCloser>;
Josh Gaofdf832d2017-08-25 18:00:18 -070075
Josh Gaocbe70cb2016-10-18 18:17:52 -070076// see man(2) prctl, specifically the section about PR_GET_NAME
77#define MAX_TASK_NAME_LEN (16)
78
79#if defined(__LP64__)
80#define CRASH_DUMP_NAME "crash_dump64"
81#else
82#define CRASH_DUMP_NAME "crash_dump32"
83#endif
84
85#define CRASH_DUMP_PATH "/system/bin/" CRASH_DUMP_NAME
86
Josh Gao2e7b8e22017-05-04 17:12:57 -070087// Wrappers that directly invoke the respective syscalls, in case the cached values are invalid.
88#pragma GCC poison getpid gettid
89static pid_t __getpid() {
90 return syscall(__NR_getpid);
91}
92
93static pid_t __gettid() {
94 return syscall(__NR_gettid);
95}
96
Christopher Ferrisac225782017-04-25 11:23:10 -070097static inline void futex_wait(volatile void* ftx, int value) {
98 syscall(__NR_futex, ftx, FUTEX_WAIT, value, nullptr, nullptr, 0);
99}
100
Josh Gaoec918092017-03-10 14:44:54 -0800101class ErrnoRestorer {
102 public:
103 ErrnoRestorer() : saved_errno_(errno) {
104 }
105
106 ~ErrnoRestorer() {
107 errno = saved_errno_;
108 }
109
110 private:
111 int saved_errno_;
112};
113
Josh Gao9da1f512018-08-06 15:38:29 -0700114extern "C" void* android_fdsan_get_fd_table();
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800115extern "C" void debuggerd_fallback_handler(siginfo_t*, ucontext_t*, void*);
Josh Gaoe73c9322017-02-08 16:06:26 -0800116
Josh Gaocbe70cb2016-10-18 18:17:52 -0700117static debuggerd_callbacks_t g_callbacks;
118
Josh Gaofca7ca32017-01-23 12:05:35 -0800119// Mutex to ensure only one crashing thread dumps itself.
120static pthread_mutex_t crash_mutex = PTHREAD_MUTEX_INITIALIZER;
121
Christopher Ferrisac225782017-04-25 11:23:10 -0700122// Don't use async_safe_fatal because it exits via abort, which might put us back into
123// a signal handler.
Josh Gao6462bb42017-01-31 13:13:46 -0800124static void __noreturn __printflike(1, 2) fatal(const char* fmt, ...) {
125 va_list args;
126 va_start(args, fmt);
Christopher Ferrisac225782017-04-25 11:23:10 -0700127 async_safe_format_log_va_list(ANDROID_LOG_FATAL, "libc", fmt, args);
Josh Gao6462bb42017-01-31 13:13:46 -0800128 _exit(1);
129}
130
131static void __noreturn __printflike(1, 2) fatal_errno(const char* fmt, ...) {
132 int err = errno;
133 va_list args;
134 va_start(args, fmt);
135
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700136 char buf[256];
Christopher Ferrisac225782017-04-25 11:23:10 -0700137 async_safe_format_buffer_va_list(buf, sizeof(buf), fmt, args);
Josh Gao6462bb42017-01-31 13:13:46 -0800138 fatal("%s: %s", buf, strerror(err));
139}
Josh Gaocbe70cb2016-10-18 18:17:52 -0700140
Josh Gao81e6c0b2017-08-11 15:01:29 -0700141static bool get_main_thread_name(char* buf, size_t len) {
Josh Gaofdf832d2017-08-25 18:00:18 -0700142 unique_fd fd(open("/proc/self/comm", O_RDONLY | O_CLOEXEC));
Josh Gao81e6c0b2017-08-11 15:01:29 -0700143 if (fd == -1) {
144 return false;
145 }
146
147 ssize_t rc = read(fd, buf, len);
Josh Gao81e6c0b2017-08-11 15:01:29 -0700148 if (rc == -1) {
149 return false;
150 } else if (rc == 0) {
151 // Should never happen?
152 return false;
153 }
154
155 // There's a trailing newline, replace it with a NUL.
156 buf[rc - 1] = '\0';
157 return true;
158}
159
Josh Gaocbe70cb2016-10-18 18:17:52 -0700160/*
161 * Writes a summary of the signal to the log file. We do this so that, if
162 * for some reason we're not able to contact debuggerd, there is still some
163 * indication of the failure in the log.
164 *
165 * We could be here as a result of native heap corruption, or while a
166 * mutex is being held, so we don't want to use any libc functions that
167 * could allocate memory or hold a lock.
168 */
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700169static void log_signal_summary(const siginfo_t* info) {
Josh Gao4ed00c82017-01-30 11:27:36 -0800170 char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination
171 if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(thread_name), 0, 0, 0) != 0) {
172 strcpy(thread_name, "<name unknown>");
173 } else {
174 // short names are null terminated by prctl, but the man page
175 // implies that 16 byte names are not.
176 thread_name[MAX_TASK_NAME_LEN] = 0;
177 }
178
Josh Gaoa48b41b2019-12-13 14:11:04 -0800179 if (info->si_signo == BIONIC_SIGNAL_DEBUGGER) {
Josh Gao2e7b8e22017-05-04 17:12:57 -0700180 async_safe_format_log(ANDROID_LOG_INFO, "libc", "Requested dump for tid %d (%s)", __gettid(),
Christopher Ferrisac225782017-04-25 11:23:10 -0700181 thread_name);
Josh Gao4ed00c82017-01-30 11:27:36 -0800182 return;
183 }
184
Elliott Hughes70d8f282018-04-25 17:00:14 -0700185 // Many signals don't have an address or sender.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700186 char addr_desc[32] = ""; // ", fault addr 0x1234"
Elliott Hughes70d8f282018-04-25 17:00:14 -0700187 if (signal_has_si_addr(info)) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700188 async_safe_format_buffer(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700189 }
Elliott Hughes70d8f282018-04-25 17:00:14 -0700190 pid_t self_pid = __getpid();
191 char sender_desc[32] = {}; // " from pid 1234, uid 666"
192 if (signal_has_sender(info, self_pid)) {
193 get_signal_sender(sender_desc, sizeof(sender_desc), info);
194 }
Josh Gao4ed00c82017-01-30 11:27:36 -0800195
Josh Gao81e6c0b2017-08-11 15:01:29 -0700196 char main_thread_name[MAX_TASK_NAME_LEN + 1];
197 if (!get_main_thread_name(main_thread_name, sizeof(main_thread_name))) {
198 strncpy(main_thread_name, "<unknown>", sizeof(main_thread_name));
199 }
200
Elliott Hughes70d8f282018-04-25 17:00:14 -0700201 async_safe_format_log(ANDROID_LOG_FATAL, "libc",
202 "Fatal signal %d (%s), code %d (%s%s)%s in tid %d (%s), pid %d (%s)",
203 info->si_signo, get_signame(info), info->si_code, get_sigcode(info),
204 sender_desc, addr_desc, __gettid(), thread_name, self_pid, main_thread_name);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700205}
206
207/*
208 * Returns true if the handler for signal "signum" has SA_SIGINFO set.
209 */
210static bool have_siginfo(int signum) {
211 struct sigaction old_action;
212 if (sigaction(signum, nullptr, &old_action) < 0) {
Christopher Ferrisac225782017-04-25 11:23:10 -0700213 async_safe_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
214 strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700215 return false;
216 }
217 return (old_action.sa_flags & SA_SIGINFO) != 0;
218}
219
Josh Gaod2069632017-02-09 14:42:38 -0800220static void raise_caps() {
221 // Raise CapInh to match CapPrm, so that we can set the ambient bits.
222 __user_cap_header_struct capheader;
223 memset(&capheader, 0, sizeof(capheader));
224 capheader.version = _LINUX_CAPABILITY_VERSION_3;
225 capheader.pid = 0;
226
227 __user_cap_data_struct capdata[2];
228 if (capget(&capheader, &capdata[0]) == -1) {
229 fatal_errno("capget failed");
230 }
231
232 if (capdata[0].permitted != capdata[0].inheritable ||
233 capdata[1].permitted != capdata[1].inheritable) {
234 capdata[0].inheritable = capdata[0].permitted;
235 capdata[1].inheritable = capdata[1].permitted;
236
237 if (capset(&capheader, &capdata[0]) == -1) {
Christopher Ferrisac225782017-04-25 11:23:10 -0700238 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "capset failed: %s", strerror(errno));
Josh Gaod2069632017-02-09 14:42:38 -0800239 }
240 }
241
242 // Set the ambient capability bits so that crash_dump gets all of our caps and can ptrace us.
243 uint64_t capmask = capdata[0].inheritable;
244 capmask |= static_cast<uint64_t>(capdata[1].inheritable) << 32;
245 for (unsigned long i = 0; i < 64; ++i) {
Josh Gao5ad965b2017-02-16 19:22:25 -0800246 if (capmask & (1ULL << i)) {
Josh Gaod2069632017-02-09 14:42:38 -0800247 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, 0) != 0) {
Christopher Ferrisac225782017-04-25 11:23:10 -0700248 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
249 "failed to raise ambient capability %lu: %s", i, strerror(errno));
Josh Gaod2069632017-02-09 14:42:38 -0800250 }
251 }
252 }
253}
254
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700255static pid_t __fork() {
256 return clone(nullptr, nullptr, 0, nullptr);
257}
258
259// Double-clone, with CLONE_FILES to share the file descriptor table for kcmp validation.
260// Returns 0 in the orphaned child, the pid of the orphan in the original process, or -1 on failure.
261static void create_vm_process() {
262 pid_t first = clone(nullptr, nullptr, CLONE_FILES, nullptr);
263 if (first == -1) {
264 fatal_errno("failed to clone vm process");
265 } else if (first == 0) {
266 drop_capabilities();
267
268 if (clone(nullptr, nullptr, CLONE_FILES, nullptr) == -1) {
269 _exit(errno);
270 }
271
Josh Gao18cb6812019-04-16 13:17:08 -0700272 // crash_dump is ptracing both sides of the fork; it'll let the parent exit,
273 // but keep the orphan stopped to peek at its memory.
274
275 // There appears to be a bug in the kernel where our death causes SIGHUP to
276 // be sent to our process group if we exit while it has stopped jobs (e.g.
277 // because of wait_for_gdb). Use setsid to create a new process group to
278 // avoid hitting this.
279 setsid();
280
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700281 _exit(0);
282 }
283
284 int status;
285 if (TEMP_FAILURE_RETRY(waitpid(first, &status, __WCLONE)) != first) {
286 fatal_errno("failed to waitpid in double fork");
287 } else if (!WIFEXITED(status)) {
288 fatal("intermediate process didn't exit cleanly in double fork (status = %d)", status);
289 } else if (WEXITSTATUS(status)) {
290 fatal("second clone failed: %s", strerror(WEXITSTATUS(status)));
291 }
292}
293
Josh Gaocbe70cb2016-10-18 18:17:52 -0700294struct debugger_thread_info {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700295 pid_t crashing_tid;
296 pid_t pseudothread_tid;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700297 siginfo_t* siginfo;
298 void* ucontext;
299 uintptr_t abort_msg;
Josh Gao9da1f512018-08-06 15:38:29 -0700300 uintptr_t fdsan_table;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700301};
302
303// Logging and contacting debuggerd requires free file descriptors, which we might not have.
304// Work around this by spawning a "thread" that shares its parent's address space, but not its file
305// descriptor table, so that we can close random file descriptors without affecting the original
306// process. Note that this doesn't go through pthread_create, so TLS is shared with the spawning
307// process.
308static void* pseudothread_stack;
309
Narayan Kamatha73df602017-05-24 15:07:25 +0100310static DebuggerdDumpType get_dump_type(const debugger_thread_info* thread_info) {
Josh Gaoa48b41b2019-12-13 14:11:04 -0800311 if (thread_info->siginfo->si_signo == BIONIC_SIGNAL_DEBUGGER &&
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700312 thread_info->siginfo->si_value.sival_int) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100313 return kDebuggerdNativeBacktrace;
314 }
315
316 return kDebuggerdTombstone;
317}
318
Josh Gaocbe70cb2016-10-18 18:17:52 -0700319static int debuggerd_dispatch_pseudothread(void* arg) {
320 debugger_thread_info* thread_info = static_cast<debugger_thread_info*>(arg);
321
322 for (int i = 0; i < 1024; ++i) {
Josh Gaoc954ec02018-06-01 15:47:20 -0700323 // Don't use close to avoid bionic's file descriptor ownership checks.
324 syscall(__NR_close, i);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700325 }
326
327 int devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700328 if (devnull == -1) {
329 fatal_errno("failed to open /dev/null");
330 } else if (devnull != 0) {
331 fatal_errno("expected /dev/null fd to be 0, actually %d", devnull);
332 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700333
334 // devnull will be 0.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700335 TEMP_FAILURE_RETRY(dup2(devnull, 1));
336 TEMP_FAILURE_RETRY(dup2(devnull, 2));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700337
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700338 unique_fd input_read, input_write;
339 unique_fd output_read, output_write;
340 if (!Pipe(&input_read, &input_write) != 0 || !Pipe(&output_read, &output_write)) {
Josh Gao6462bb42017-01-31 13:13:46 -0800341 fatal_errno("failed to create pipe");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700342 }
343
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700344 // ucontext_t is absurdly large on AArch64, so piece it together manually with writev.
Josh Gao9da1f512018-08-06 15:38:29 -0700345 uint32_t version = 2;
346 constexpr size_t expected = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700347
348 errno = 0;
349 if (fcntl(output_write.get(), F_SETPIPE_SZ, expected) < static_cast<int>(expected)) {
Josh Gao9da1f512018-08-06 15:38:29 -0700350 fatal_errno("failed to set pipe buffer size");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700351 }
352
Josh Gao9da1f512018-08-06 15:38:29 -0700353 struct iovec iovs[5] = {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700354 {.iov_base = &version, .iov_len = sizeof(version)},
355 {.iov_base = thread_info->siginfo, .iov_len = sizeof(siginfo_t)},
356 {.iov_base = thread_info->ucontext, .iov_len = sizeof(ucontext_t)},
357 {.iov_base = &thread_info->abort_msg, .iov_len = sizeof(uintptr_t)},
Josh Gao9da1f512018-08-06 15:38:29 -0700358 {.iov_base = &thread_info->fdsan_table, .iov_len = sizeof(uintptr_t)},
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700359 };
360
Josh Gao9da1f512018-08-06 15:38:29 -0700361 ssize_t rc = TEMP_FAILURE_RETRY(writev(output_write.get(), iovs, 5));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700362 if (rc == -1) {
363 fatal_errno("failed to write crash info");
364 } else if (rc != expected) {
365 fatal("failed to write crash info, wrote %zd bytes, expected %zd", rc, expected);
366 }
367
Josh Gaocbe70cb2016-10-18 18:17:52 -0700368 // Don't use fork(2) to avoid calling pthread_atfork handlers.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700369 pid_t crash_dump_pid = __fork();
370 if (crash_dump_pid == -1) {
Christopher Ferrisac225782017-04-25 11:23:10 -0700371 async_safe_format_log(ANDROID_LOG_FATAL, "libc",
372 "failed to fork in debuggerd signal handler: %s", strerror(errno));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700373 } else if (crash_dump_pid == 0) {
374 TEMP_FAILURE_RETRY(dup2(input_write.get(), STDOUT_FILENO));
375 TEMP_FAILURE_RETRY(dup2(output_read.get(), STDIN_FILENO));
376 input_read.reset();
377 input_write.reset();
378 output_read.reset();
379 output_write.reset();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700380
Josh Gaod2069632017-02-09 14:42:38 -0800381 raise_caps();
Josh Gao7ae426c2017-02-01 15:33:18 -0800382
Josh Gao2f11a252017-02-13 14:46:19 -0800383 char main_tid[10];
384 char pseudothread_tid[10];
Narayan Kamatha73df602017-05-24 15:07:25 +0100385 char debuggerd_dump_type[10];
Christopher Ferrisac225782017-04-25 11:23:10 -0700386 async_safe_format_buffer(main_tid, sizeof(main_tid), "%d", thread_info->crashing_tid);
387 async_safe_format_buffer(pseudothread_tid, sizeof(pseudothread_tid), "%d",
388 thread_info->pseudothread_tid);
Narayan Kamatha73df602017-05-24 15:07:25 +0100389 async_safe_format_buffer(debuggerd_dump_type, sizeof(debuggerd_dump_type), "%d",
390 get_dump_type(thread_info));
Josh Gao60515bf2017-02-14 21:03:23 -0800391
Josh Gaocdea7502017-11-01 15:00:40 -0700392 execle(CRASH_DUMP_PATH, CRASH_DUMP_NAME, main_tid, pseudothread_tid, debuggerd_dump_type,
393 nullptr, nullptr);
Josh Gao5e8d68c2019-03-15 15:10:24 -0700394 async_safe_format_log(ANDROID_LOG_FATAL, "libc", "failed to exec crash_dump helper: %s",
395 strerror(errno));
396 return 1;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700397 }
398
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700399 input_write.reset();
400 output_read.reset();
401
402 // crash_dump will ptrace and pause all of our threads, and then write to the pipe to tell
403 // us to fork off a process to read memory from.
404 char buf[4];
405 rc = TEMP_FAILURE_RETRY(read(input_read.get(), &buf, sizeof(buf)));
406 if (rc == -1) {
407 async_safe_format_log(ANDROID_LOG_FATAL, "libc", "read of IPC pipe failed: %s", strerror(errno));
408 return 1;
409 } else if (rc == 0) {
410 async_safe_format_log(ANDROID_LOG_FATAL, "libc", "crash_dump helper failed to exec");
411 return 1;
412 } else if (rc != 1) {
413 async_safe_format_log(ANDROID_LOG_FATAL, "libc",
414 "read of IPC pipe returned unexpected value: %zd", rc);
415 return 1;
416 } else if (buf[0] != '\1') {
417 async_safe_format_log(ANDROID_LOG_FATAL, "libc", "crash_dump helper reported failure");
418 return 1;
419 }
420
421 // crash_dump is ptracing us, fork off a copy of our address space for it to use.
422 create_vm_process();
423
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700424 // Don't leave a zombie child.
425 int status;
426 if (TEMP_FAILURE_RETRY(waitpid(crash_dump_pid, &status, 0)) == -1) {
427 async_safe_format_log(ANDROID_LOG_FATAL, "libc", "failed to wait for crash_dump helper: %s",
428 strerror(errno));
429 } else if (WIFSTOPPED(status) || WIFSIGNALED(status)) {
430 async_safe_format_log(ANDROID_LOG_FATAL, "libc", "crash_dump helper crashed or stopped");
431 }
Josh Gao73020972017-12-20 15:34:35 -0800432
Josh Gaoa48b41b2019-12-13 14:11:04 -0800433 if (thread_info->siginfo->si_signo != BIONIC_SIGNAL_DEBUGGER) {
Josh Gao73020972017-12-20 15:34:35 -0800434 // For crashes, we don't need to minimize pause latency.
435 // Wait for the dump to complete before having the process exit, to avoid being murdered by
436 // ActivityManager or init.
437 TEMP_FAILURE_RETRY(read(input_read, &buf, sizeof(buf)));
438 }
439
Josh Gaocbe70cb2016-10-18 18:17:52 -0700440 return 0;
441}
442
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700443static void resend_signal(siginfo_t* info) {
Josh Gaofca7ca32017-01-23 12:05:35 -0800444 // Signals can either be fatal or nonfatal.
445 // For fatal signals, crash_dump will send us the signal we crashed with
446 // before resuming us, so that processes using waitpid on us will see that we
447 // exited with the correct exit status (e.g. so that sh will report
448 // "Segmentation fault" instead of "Killed"). For this to work, we need
449 // to deregister our signal handler for that signal before continuing.
Josh Gaoa48b41b2019-12-13 14:11:04 -0800450 if (info->si_signo != BIONIC_SIGNAL_DEBUGGER) {
Josh Gaofca7ca32017-01-23 12:05:35 -0800451 signal(info->si_signo, SIG_DFL);
Josh Gao2e7b8e22017-05-04 17:12:57 -0700452 int rc = syscall(SYS_rt_tgsigqueueinfo, __getpid(), __gettid(), info->si_signo, info);
Josh Gao529b3062017-01-25 11:05:11 -0800453 if (rc != 0) {
Josh Gao6462bb42017-01-31 13:13:46 -0800454 fatal_errno("failed to resend signal during crash");
Josh Gao529b3062017-01-25 11:05:11 -0800455 }
Josh Gaofca7ca32017-01-23 12:05:35 -0800456 }
Josh Gaofca7ca32017-01-23 12:05:35 -0800457}
458
Josh Gaocbe70cb2016-10-18 18:17:52 -0700459// Handler that does crash dumping by forking and doing the processing in the child.
460// Do this by ptracing the relevant thread, and then execing debuggerd to do the actual dump.
Josh Gaoe73c9322017-02-08 16:06:26 -0800461static void debuggerd_signal_handler(int signal_number, siginfo_t* info, void* context) {
Josh Gaoec918092017-03-10 14:44:54 -0800462 // Make sure we don't change the value of errno, in case a signal comes in between the process
463 // making a syscall and checking errno.
464 ErrnoRestorer restorer;
465
Josh Gaocbe70cb2016-10-18 18:17:52 -0700466 // It's possible somebody cleared the SA_SIGINFO flag, which would mean
467 // our "info" arg holds an undefined value.
468 if (!have_siginfo(signal_number)) {
469 info = nullptr;
470 }
471
Josh Gao4843c182018-08-27 14:52:33 -0700472 struct siginfo dummy_info = {};
Josh Gaofca7ca32017-01-23 12:05:35 -0800473 if (!info) {
Josh Gao4843c182018-08-27 14:52:33 -0700474 memset(&dummy_info, 0, sizeof(dummy_info));
475 dummy_info.si_signo = signal_number;
476 dummy_info.si_code = SI_USER;
477 dummy_info.si_pid = __getpid();
478 dummy_info.si_uid = getuid();
479 info = &dummy_info;
Josh Gaofca7ca32017-01-23 12:05:35 -0800480 } else if (info->si_code >= 0 || info->si_code == SI_TKILL) {
481 // rt_tgsigqueueinfo(2)'s documentation appears to be incorrect on kernels
482 // that contain commit 66dd34a (3.9+). The manpage claims to only allow
483 // negative si_code values that are not SI_TKILL, but 66dd34a changed the
484 // check to allow all si_code values in calls coming from inside the house.
485 }
486
Josh Gaocbe70cb2016-10-18 18:17:52 -0700487 void* abort_message = nullptr;
Josh Gao6f9eeec2018-09-12 13:55:47 -0700488 uintptr_t si_val = reinterpret_cast<uintptr_t>(info->si_ptr);
Josh Gaoa48b41b2019-12-13 14:11:04 -0800489 if (signal_number == BIONIC_SIGNAL_DEBUGGER) {
Josh Gao4843c182018-08-27 14:52:33 -0700490 if (info->si_code == SI_QUEUE && info->si_pid == __getpid()) {
491 // Allow for the abort message to be explicitly specified via the sigqueue value.
492 // Keep the bottom bit intact for representing whether we want a backtrace or a tombstone.
Josh Gao6f9eeec2018-09-12 13:55:47 -0700493 if (si_val != kDebuggerdFallbackSivalUintptrRequestDump) {
494 abort_message = reinterpret_cast<void*>(si_val & ~1);
495 info->si_ptr = reinterpret_cast<void*>(si_val & 1);
496 }
Josh Gao4843c182018-08-27 14:52:33 -0700497 }
498 } else {
499 if (g_callbacks.get_abort_message) {
500 abort_message = g_callbacks.get_abort_message();
501 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700502 }
Josh Gaoe73c9322017-02-08 16:06:26 -0800503
Christopher Ferris664d2a92017-11-16 19:55:48 -0800504 // If sival_int is ~0, it means that the fallback handler has been called
505 // once before and this function is being called again to dump the stack
506 // of a specific thread. It is possible that the prctl call might return 1,
507 // then return 0 in subsequent calls, so check the sival_int to determine if
508 // the fallback handler should be called first.
Josh Gao6f9eeec2018-09-12 13:55:47 -0700509 if (si_val == kDebuggerdFallbackSivalUintptrRequestDump ||
510 prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) == 1) {
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800511 // This check might be racy if another thread sets NO_NEW_PRIVS, but this should be unlikely,
512 // you can only set NO_NEW_PRIVS to 1, and the effect should be at worst a single missing
513 // ANR trace.
514 debuggerd_fallback_handler(info, static_cast<ucontext_t*>(context), abort_message);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700515 resend_signal(info);
Josh Gaoe73c9322017-02-08 16:06:26 -0800516 return;
517 }
518
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800519 // Only allow one thread to handle a signal at a time.
520 int ret = pthread_mutex_lock(&crash_mutex);
521 if (ret != 0) {
Christopher Ferrisac225782017-04-25 11:23:10 -0700522 async_safe_format_log(ANDROID_LOG_INFO, "libc", "pthread_mutex_lock failed: %s", strerror(ret));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800523 return;
524 }
525
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700526 log_signal_summary(info);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700527
528 debugger_thread_info thread_info = {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700529 .crashing_tid = __gettid(),
Nick Desaulniers67d52aa2019-10-07 23:28:15 -0700530 .pseudothread_tid = -1,
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700531 .siginfo = info,
532 .ucontext = context,
533 .abort_msg = reinterpret_cast<uintptr_t>(abort_message),
Josh Gao9da1f512018-08-06 15:38:29 -0700534 .fdsan_table = reinterpret_cast<uintptr_t>(android_fdsan_get_fd_table()),
Josh Gaocbe70cb2016-10-18 18:17:52 -0700535 };
Josh Gaocbe70cb2016-10-18 18:17:52 -0700536
Josh Gao2f11a252017-02-13 14:46:19 -0800537 // Set PR_SET_DUMPABLE to 1, so that crash_dump can ptrace us.
538 int orig_dumpable = prctl(PR_GET_DUMPABLE);
539 if (prctl(PR_SET_DUMPABLE, 1) != 0) {
540 fatal_errno("failed to set dumpable");
541 }
542
Luis Hector Chavez4841e742017-12-27 12:36:02 -0800543 // On kernels with yama_ptrace enabled, also allow any process to attach.
544 bool restore_orig_ptracer = true;
545 if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) != 0) {
546 if (errno == EINVAL) {
547 // This kernel does not support PR_SET_PTRACER_ANY, or Yama is not enabled.
548 restore_orig_ptracer = false;
549 } else {
550 fatal_errno("failed to set traceable");
551 }
552 }
553
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700554 // Essentially pthread_create without CLONE_FILES, so we still work during file descriptor
555 // exhaustion.
Josh Gaob64dd852017-01-22 18:22:52 -0800556 pid_t child_pid =
557 clone(debuggerd_dispatch_pseudothread, pseudothread_stack,
558 CLONE_THREAD | CLONE_SIGHAND | CLONE_VM | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID,
559 &thread_info, nullptr, nullptr, &thread_info.pseudothread_tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700560 if (child_pid == -1) {
Josh Gao6462bb42017-01-31 13:13:46 -0800561 fatal_errno("failed to spawn debuggerd dispatch thread");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700562 }
563
Josh Gaob64dd852017-01-22 18:22:52 -0800564 // Wait for the child to start...
Christopher Ferrisac225782017-04-25 11:23:10 -0700565 futex_wait(&thread_info.pseudothread_tid, -1);
Josh Gaob64dd852017-01-22 18:22:52 -0800566
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700567 // and then wait for it to terminate.
Christopher Ferrisac225782017-04-25 11:23:10 -0700568 futex_wait(&thread_info.pseudothread_tid, child_pid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700569
Josh Gao2f11a252017-02-13 14:46:19 -0800570 // Restore PR_SET_DUMPABLE to its original value.
571 if (prctl(PR_SET_DUMPABLE, orig_dumpable) != 0) {
572 fatal_errno("failed to restore dumpable");
573 }
574
Luis Hector Chavez4841e742017-12-27 12:36:02 -0800575 // Restore PR_SET_PTRACER to its original value.
576 if (restore_orig_ptracer && prctl(PR_SET_PTRACER, 0) != 0) {
577 fatal_errno("failed to restore traceable");
578 }
579
Josh Gaoa48b41b2019-12-13 14:11:04 -0800580 if (info->si_signo == BIONIC_SIGNAL_DEBUGGER) {
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800581 // If the signal is fatal, don't unlock the mutex to prevent other crashing threads from
582 // starting to dump right before our death.
583 pthread_mutex_unlock(&crash_mutex);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700584 } else {
585 // Resend the signal, so that either gdb or the parent's waitpid sees it.
586 resend_signal(info);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -0800587 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700588}
589
590void debuggerd_init(debuggerd_callbacks_t* callbacks) {
591 if (callbacks) {
592 g_callbacks = *callbacks;
593 }
594
595 void* thread_stack_allocation =
596 mmap(nullptr, PAGE_SIZE * 3, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
597 if (thread_stack_allocation == MAP_FAILED) {
Josh Gao6462bb42017-01-31 13:13:46 -0800598 fatal_errno("failed to allocate debuggerd thread stack");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700599 }
600
601 char* stack = static_cast<char*>(thread_stack_allocation) + PAGE_SIZE;
602 if (mprotect(stack, PAGE_SIZE, PROT_READ | PROT_WRITE) != 0) {
Josh Gao6462bb42017-01-31 13:13:46 -0800603 fatal_errno("failed to mprotect debuggerd thread stack");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700604 }
605
606 // Stack grows negatively, set it to the last byte in the page...
607 stack = (stack + PAGE_SIZE - 1);
608 // and align it.
609 stack -= 15;
610 pseudothread_stack = stack;
611
612 struct sigaction action;
613 memset(&action, 0, sizeof(action));
614 sigfillset(&action.sa_mask);
615 action.sa_sigaction = debuggerd_signal_handler;
616 action.sa_flags = SA_RESTART | SA_SIGINFO;
617
618 // Use the alternate signal stack if available so we can catch stack overflows.
619 action.sa_flags |= SA_ONSTACK;
Josh Gao57594112017-01-22 17:41:15 -0800620 debuggerd_register_handlers(&action);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700621}