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#include <arpa/inet.h>
18#include <dirent.h>
19#include <elf.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <pthread.h>
23#include <signal.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <sys/poll.h>
27#include <sys/prctl.h>
28#include <sys/ptrace.h>
29#include <sys/socket.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32#include <sys/wait.h>
33#include <sys/un.h>
34#include <time.h>
35
36#include <memory>
37#include <set>
38#include <string>
39
40#include <selinux/android.h>
41
42#include <log/logger.h>
43
44#include <android-base/file.h>
45#include <android-base/unique_fd.h>
46#include <cutils/debugger.h>
47#include <cutils/properties.h>
48#include <cutils/sockets.h>
49#include <nativehelper/ScopedFd.h>
50
51#include <linux/input.h>
52
53#include <private/android_filesystem_config.h>
54
55#include "backtrace.h"
56#include "getevent.h"
57#include "signal_sender.h"
58#include "tombstone.h"
59#include "utility.h"
60
61// If the 32 bit executable is compiled on a 64 bit system,
62// use the 32 bit socket name.
63#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
64#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
65#else
66#define SOCKET_NAME DEBUGGER_SOCKET_NAME
67#endif
68
69struct debugger_request_t {
70  debugger_action_t action;
71  pid_t pid, tid;
72  uid_t uid, gid;
73  uintptr_t abort_msg_address;
74  int32_t original_si_code;
75};
76
77static void wait_for_user_action(const debugger_request_t& request) {
78  // Explain how to attach the debugger.
79  ALOGI("***********************************************************\n"
80        "* Process %d has been suspended while crashing.\n"
81        "* To attach gdbserver and start gdb, run this on the host:\n"
82        "*\n"
83        "*     gdbclient.py -p %d\n"
84        "*\n"
85        "* Wait for gdb to start, then press the VOLUME DOWN key\n"
86        "* to let the process continue crashing.\n"
87        "***********************************************************",
88        request.pid, request.tid);
89
90  // Wait for VOLUME DOWN.
91  while (true) {
92    input_event e;
93    if (get_event(&e, -1) == 0) {
94      if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
95        break;
96      }
97    }
98  }
99
100  ALOGI("debuggerd resuming process %d", request.pid);
101}
102
103static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
104  char path[64];
105  snprintf(path, sizeof(path), "/proc/%d/status", tid);
106
107  FILE* fp = fopen(path, "r");
108  if (!fp) {
109    return -1;
110  }
111
112  int fields = 0;
113  char line[1024];
114  while (fgets(line, sizeof(line), fp)) {
115    size_t len = strlen(line);
116    if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
117      *out_pid = atoi(line + 6);
118      fields |= 1;
119    } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
120      *out_uid = atoi(line + 5);
121      fields |= 2;
122    } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
123      *out_gid = atoi(line + 5);
124      fields |= 4;
125    }
126  }
127  fclose(fp);
128  return fields == 7 ? 0 : -1;
129}
130
131/*
132 * Corresponds with debugger_action_t enum type in
133 * include/cutils/debugger.h.
134 */
135static const char *debuggerd_perms[] = {
136  NULL, /* crash is only used on self, no check applied */
137  "dump_tombstone",
138  "dump_backtrace"
139};
140
141static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
142{
143    struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
144
145    if (!req) {
146        ALOGE("No debuggerd request audit data");
147        return 0;
148    }
149
150    snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
151    return 0;
152}
153
154static bool selinux_action_allowed(int s, debugger_request_t* request)
155{
156  char *scon = NULL, *tcon = NULL;
157  const char *tclass = "debuggerd";
158  const char *perm;
159  bool allowed = false;
160
161  if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
162    ALOGE("SELinux:  No permission defined for debugger action %d", request->action);
163    return false;
164  }
165
166  perm = debuggerd_perms[request->action];
167
168  if (getpeercon(s, &scon) < 0) {
169    ALOGE("Cannot get peer context from socket\n");
170    goto out;
171  }
172
173  if (getpidcon(request->tid, &tcon) < 0) {
174    ALOGE("Cannot get context for tid %d\n", request->tid);
175    goto out;
176  }
177
178  allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
179
180out:
181   freecon(scon);
182   freecon(tcon);
183   return allowed;
184}
185
186static bool pid_contains_tid(pid_t pid, pid_t tid) {
187  char task_path[PATH_MAX];
188  if (snprintf(task_path, PATH_MAX, "/proc/%d/task/%d", pid, tid) >= PATH_MAX) {
189    ALOGE("debuggerd: task path overflow (pid = %d, tid = %d)\n", pid, tid);
190    exit(1);
191  }
192
193  return access(task_path, F_OK) == 0;
194}
195
196static int read_request(int fd, debugger_request_t* out_request) {
197  ucred cr;
198  socklen_t len = sizeof(cr);
199  int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
200  if (status != 0) {
201    ALOGE("cannot get credentials");
202    return -1;
203  }
204
205  ALOGV("reading tid");
206  fcntl(fd, F_SETFL, O_NONBLOCK);
207
208  pollfd pollfds[1];
209  pollfds[0].fd = fd;
210  pollfds[0].events = POLLIN;
211  pollfds[0].revents = 0;
212  status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
213  if (status != 1) {
214    ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
215    return -1;
216  }
217
218  debugger_msg_t msg;
219  memset(&msg, 0, sizeof(msg));
220  status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
221  if (status < 0) {
222    ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
223    return -1;
224  }
225  if (status != sizeof(debugger_msg_t)) {
226    ALOGE("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
227    return -1;
228  }
229
230  out_request->action = static_cast<debugger_action_t>(msg.action);
231  out_request->tid = msg.tid;
232  out_request->pid = cr.pid;
233  out_request->uid = cr.uid;
234  out_request->gid = cr.gid;
235  out_request->abort_msg_address = msg.abort_msg_address;
236  out_request->original_si_code = msg.original_si_code;
237
238  if (msg.action == DEBUGGER_ACTION_CRASH) {
239    // Ensure that the tid reported by the crashing process is valid.
240    // This check needs to happen again after ptracing the requested thread to prevent a race.
241    if (!pid_contains_tid(out_request->pid, out_request->tid)) {
242      ALOGE("tid %d does not exist in pid %d. ignoring debug request\n", out_request->tid,
243            out_request->pid);
244      return -1;
245    }
246  } else if (cr.uid == 0 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
247    // Only root or system can ask us to attach to any process and dump it explicitly.
248    // However, system is only allowed to collect backtraces but cannot dump tombstones.
249    status = get_process_info(out_request->tid, &out_request->pid,
250                              &out_request->uid, &out_request->gid);
251    if (status < 0) {
252      ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
253      return -1;
254    }
255
256    if (!selinux_action_allowed(fd, out_request))
257      return -1;
258  } else {
259    // No one else is allowed to dump arbitrary processes.
260    return -1;
261  }
262  return 0;
263}
264
265static int activity_manager_connect() {
266  android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
267  if (amfd.get() < -1) {
268    ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
269    return -1;
270  }
271
272  struct sockaddr_un address;
273  memset(&address, 0, sizeof(address));
274  address.sun_family = AF_UNIX;
275  // The path used here must match the value defined in NativeCrashListener.java.
276  strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
277  if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
278                                 sizeof(address))) == -1) {
279    ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
280    return -1;
281  }
282
283  struct timeval tv;
284  memset(&tv, 0, sizeof(tv));
285  tv.tv_sec = 1;  // tight leash
286  if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
287    ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
288          strerror(errno));
289    return -1;
290  }
291
292  tv.tv_sec = 3;  // 3 seconds on handshake read
293  if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
294    ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
295          strerror(errno));
296    return -1;
297  }
298
299  return amfd.release();
300}
301
302static void activity_manager_write(int pid, int signal, int amfd, const std::string& amfd_data) {
303  if (amfd == -1) {
304    return;
305  }
306
307  // Activity Manager protocol: binary 32-bit network-byte-order ints for the
308  // pid and signal number, followed by the raw text of the dump, culminating
309  // in a zero byte that marks end-of-data.
310  uint32_t datum = htonl(pid);
311  if (!android::base::WriteFully(amfd, &datum, 4)) {
312    ALOGE("AM pid write failed: %s\n", strerror(errno));
313    return;
314  }
315  datum = htonl(signal);
316  if (!android::base::WriteFully(amfd, &datum, 4)) {
317    ALOGE("AM signal write failed: %s\n", strerror(errno));
318    return;
319  }
320
321  if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size())) {
322    ALOGE("AM data write failed: %s\n", strerror(errno));
323    return;
324  }
325
326  // Send EOD to the Activity Manager, then wait for its ack to avoid racing
327  // ahead and killing the target out from under it.
328  uint8_t eodMarker = 0;
329  if (!android::base::WriteFully(amfd, &eodMarker, 1)) {
330    ALOGE("AM eod write failed: %s\n", strerror(errno));
331    return;
332  }
333  // 3 sec timeout reading the ack; we're fine if the read fails.
334  android::base::ReadFully(amfd, &eodMarker, 1);
335}
336
337static bool should_attach_gdb(const debugger_request_t& request) {
338  if (request.action == DEBUGGER_ACTION_CRASH) {
339    return property_get_bool("debug.debuggerd.wait_for_gdb", false);
340  }
341  return false;
342}
343
344#if defined(__LP64__)
345static bool is32bit(pid_t tid) {
346  char* exeline;
347  if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
348    return false;
349  }
350  int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
351  int saved_errno = errno;
352  free(exeline);
353  if (fd == -1) {
354    ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
355    return false;
356  }
357
358  char ehdr[EI_NIDENT];
359  ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
360  close(fd);
361  if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
362    return false;
363  }
364  if (ehdr[EI_CLASS] == ELFCLASS32) {
365    return true;
366  }
367  return false;
368}
369
370static void redirect_to_32(int fd, debugger_request_t* request) {
371  debugger_msg_t msg;
372  memset(&msg, 0, sizeof(msg));
373  msg.tid = request->tid;
374  msg.action = request->action;
375
376  int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
377                                    SOCK_STREAM | SOCK_CLOEXEC);
378  if (sock_fd < 0) {
379    ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
380    return;
381  }
382
383  if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
384    ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
385    close(sock_fd);
386    return;
387  }
388
389  char ack;
390  if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
391    ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
392    close(sock_fd);
393    return;
394  }
395
396  char buffer[1024];
397  ssize_t bytes_read;
398  while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
399    ssize_t bytes_to_send = bytes_read;
400    ssize_t bytes_written;
401    do {
402      bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
403                                               bytes_to_send));
404      if (bytes_written == -1) {
405        if (errno == EAGAIN) {
406          // Retry the write.
407          continue;
408        }
409        ALOGE("Error while writing data to fd: %s", strerror(errno));
410        break;
411      }
412      bytes_to_send -= bytes_written;
413    } while (bytes_written != 0 && bytes_to_send > 0);
414    if (bytes_to_send != 0) {
415        ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
416        break;
417    }
418  }
419  close(sock_fd);
420}
421#endif
422
423// Attach to a thread, and verify that it's still a member of the given process
424static bool ptrace_attach_thread(pid_t pid, pid_t tid) {
425  if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) {
426    return false;
427  }
428
429  // Make sure that the task we attached to is actually part of the pid we're dumping.
430  if (!pid_contains_tid(pid, tid)) {
431    if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
432      ALOGE("debuggerd: failed to detach from thread '%d'", tid);
433      exit(1);
434    }
435    return false;
436  }
437
438  return true;
439}
440
441static void ptrace_siblings(pid_t pid, pid_t main_tid, std::set<pid_t>& tids) {
442  char task_path[PATH_MAX];
443
444  if (snprintf(task_path, PATH_MAX, "/proc/%d/task", pid) >= PATH_MAX) {
445    ALOGE("debuggerd: task path overflow (pid = %d)\n", pid);
446    abort();
447  }
448
449  std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
450
451  // Bail early if the task directory cannot be opened.
452  if (!d) {
453    ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
454    return;
455  }
456
457  struct dirent* de;
458  while ((de = readdir(d.get())) != NULL) {
459    // Ignore "." and "..".
460    if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
461      continue;
462    }
463
464    char* end;
465    pid_t tid = strtoul(de->d_name, &end, 10);
466    if (*end) {
467      continue;
468    }
469
470    if (tid == main_tid) {
471      continue;
472    }
473
474    if (!ptrace_attach_thread(pid, tid)) {
475      ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
476      continue;
477    }
478
479    tids.insert(tid);
480  }
481}
482
483static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
484                         BacktraceMap* backtrace_map, const std::set<pid_t>& siblings,
485                         int* crash_signal, std::string* amfd_data) {
486  if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
487    ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
488    return false;
489  }
490
491  int total_sleep_time_usec = 0;
492  while (true) {
493    int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
494    switch (signal) {
495      case -1:
496        ALOGE("debuggerd: timed out waiting for signal");
497        return false;
498
499      case SIGSTOP:
500        if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
501          ALOGV("debuggerd: stopped -- dumping to tombstone");
502          engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
503                            request.original_si_code, request.abort_msg_address, amfd_data);
504        } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
505          ALOGV("debuggerd: stopped -- dumping to fd");
506          dump_backtrace(fd, backtrace_map, request.pid, request.tid, siblings, nullptr);
507        } else {
508          ALOGV("debuggerd: stopped -- continuing");
509          if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
510            ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
511            return false;
512          }
513          continue;  // loop again
514        }
515        break;
516
517      case SIGABRT:
518      case SIGBUS:
519      case SIGFPE:
520      case SIGILL:
521      case SIGSEGV:
522#ifdef SIGSTKFLT
523      case SIGSTKFLT:
524#endif
525      case SIGSYS:
526      case SIGTRAP:
527        ALOGV("stopped -- fatal signal\n");
528        *crash_signal = signal;
529        engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, signal,
530                          request.original_si_code, request.abort_msg_address, amfd_data);
531        break;
532
533      default:
534        ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
535        break;
536    }
537    break;
538  }
539
540  return true;
541}
542
543static bool drop_privileges() {
544  // AID_LOG: for reading the logs data associated with the crashing process.
545  // AID_READPROC: for reading /proc/<PID>/{comm,cmdline}.
546  gid_t groups[] = { AID_DEBUGGERD, AID_LOG, AID_READPROC };
547  if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
548    ALOGE("debuggerd: failed to setgroups: %s", strerror(errno));
549    return false;
550  }
551
552  if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
553    ALOGE("debuggerd: failed to setresgid: %s", strerror(errno));
554    return false;
555  }
556
557  if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
558    ALOGE("debuggerd: failed to setresuid: %s", strerror(errno));
559    return false;
560  }
561
562  return true;
563}
564
565static void worker_process(int fd, debugger_request_t& request) {
566  // Open the tombstone file if we need it.
567  std::string tombstone_path;
568  int tombstone_fd = -1;
569  switch (request.action) {
570    case DEBUGGER_ACTION_DUMP_TOMBSTONE:
571    case DEBUGGER_ACTION_CRASH:
572      tombstone_fd = open_tombstone(&tombstone_path);
573      if (tombstone_fd == -1) {
574        ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
575        exit(1);
576      }
577      break;
578
579    case DEBUGGER_ACTION_DUMP_BACKTRACE:
580      break;
581
582    default:
583      ALOGE("debuggerd: unexpected request action: %d", request.action);
584      exit(1);
585  }
586
587  // At this point, the thread that made the request is blocked in
588  // a read() call.  If the thread has crashed, then this gives us
589  // time to PTRACE_ATTACH to it before it has a chance to really fault.
590  //
591  // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
592  // won't necessarily have stopped by the time ptrace() returns.  (We
593  // currently assume it does.)  We write to the file descriptor to
594  // ensure that it can run as soon as we call PTRACE_CONT below.
595  // See details in bionic/libc/linker/debugger.c, in function
596  // debugger_signal_handler().
597
598  // Attach to the target process.
599  if (!ptrace_attach_thread(request.pid, request.tid)) {
600    ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
601    exit(1);
602  }
603
604  // DEBUGGER_ACTION_CRASH requests can come from arbitrary processes and the tid field in the
605  // request is sent from the other side. If an attacker can cause a process to be spawned with the
606  // pid of their process, they could trick debuggerd into dumping that process by exiting after
607  // sending the request. Validate the trusted request.uid/gid to defend against this.
608  if (request.action == DEBUGGER_ACTION_CRASH) {
609    pid_t pid;
610    uid_t uid;
611    gid_t gid;
612    if (get_process_info(request.tid, &pid, &uid, &gid) != 0) {
613      ALOGE("debuggerd: failed to get process info for tid '%d'", request.tid);
614      exit(1);
615    }
616
617    if (pid != request.pid || uid != request.uid || gid != request.gid) {
618      ALOGE(
619        "debuggerd: attached task %d does not match request: "
620        "expected pid=%d,uid=%d,gid=%d, actual pid=%d,uid=%d,gid=%d",
621        request.tid, request.pid, request.uid, request.gid, pid, uid, gid);
622      exit(1);
623    }
624  }
625
626  // Don't attach to the sibling threads if we want to attach gdb.
627  // Supposedly, it makes the process less reliable.
628  bool attach_gdb = should_attach_gdb(request);
629  if (attach_gdb) {
630    // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
631    if (init_getevent() != 0) {
632      ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
633      attach_gdb = false;
634    }
635
636  }
637
638  std::set<pid_t> siblings;
639  if (!attach_gdb) {
640    ptrace_siblings(request.pid, request.tid, siblings);
641  }
642
643  // Generate the backtrace map before dropping privileges.
644  std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
645
646  int amfd = -1;
647  std::unique_ptr<std::string> amfd_data;
648  if (request.action == DEBUGGER_ACTION_CRASH) {
649    // Connect to the activity manager before dropping privileges.
650    amfd = activity_manager_connect();
651    amfd_data.reset(new std::string);
652  }
653
654  bool succeeded = false;
655
656  // Now that we've done everything that requires privileges, we can drop them.
657  if (!drop_privileges()) {
658    ALOGE("debuggerd: failed to drop privileges, exiting");
659    _exit(1);
660  }
661
662  int crash_signal = SIGKILL;
663  succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings,
664                           &crash_signal, amfd_data.get());
665  if (succeeded) {
666    if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
667      if (!tombstone_path.empty()) {
668        android::base::WriteFully(fd, tombstone_path.c_str(), tombstone_path.length());
669      }
670    }
671  }
672
673  if (attach_gdb) {
674    // Tell the signal process to send SIGSTOP to the target.
675    if (!send_signal(request.pid, 0, SIGSTOP)) {
676      ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
677      attach_gdb = false;
678    }
679  }
680
681  if (!attach_gdb) {
682    // Tell the Activity Manager about the crashing process. If we are
683    // waiting for gdb to attach, do not send this or Activity Manager
684    // might kill the process before anyone can attach.
685    activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
686  }
687
688  if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
689    ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
690  }
691
692  for (pid_t sibling : siblings) {
693    ptrace(PTRACE_DETACH, sibling, 0, 0);
694  }
695
696  // Send the signal back to the process if it crashed and we're not waiting for gdb.
697  if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
698    if (!send_signal(request.pid, request.tid, crash_signal)) {
699      ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
700    }
701  }
702
703  // Wait for gdb, if requested.
704  if (attach_gdb) {
705    wait_for_user_action(request);
706
707    // Now tell the activity manager about this process.
708    activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
709
710    // Tell the signal process to send SIGCONT to the target.
711    if (!send_signal(request.pid, 0, SIGCONT)) {
712      ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
713    }
714
715    uninit_getevent();
716  }
717
718  close(amfd);
719
720  exit(!succeeded);
721}
722
723static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
724  struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
725  if (should_attach_gdb(request)) {
726    // If wait_for_gdb is enabled, set the timeout to something large.
727    timeout.tv_sec = INT_MAX;
728  }
729
730  sigset_t signal_set;
731  sigemptyset(&signal_set);
732  sigaddset(&signal_set, SIGCHLD);
733
734  bool kill_worker = false;
735  bool kill_target = false;
736  bool kill_self = false;
737
738  int status;
739  siginfo_t siginfo;
740  int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
741  if (signal == SIGCHLD) {
742    pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
743    if (rc != child_pid) {
744      ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
745
746      if (WIFEXITED(status)) {
747        ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
748      } else if (WIFSIGNALED(status)) {
749        ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
750      } else if (WIFSTOPPED(status)) {
751        ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
752      } else if (WIFCONTINUED(status)) {
753        ALOGW("debuggerd: pid %d continued", rc);
754      }
755
756      kill_worker = true;
757      kill_target = true;
758      kill_self = true;
759    } else if (WIFSIGNALED(status)) {
760      ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
761      kill_worker = false;
762      kill_target = true;
763    } else if (WIFSTOPPED(status)) {
764      ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
765      kill_worker = true;
766      kill_target = true;
767    }
768  } else {
769    ALOGE("debuggerd: worker process %d timed out", child_pid);
770    kill_worker = true;
771    kill_target = true;
772  }
773
774  if (kill_worker) {
775    // Something bad happened, kill the worker.
776    if (kill(child_pid, SIGKILL) != 0) {
777      ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
778    } else {
779      waitpid(child_pid, &status, 0);
780    }
781  }
782
783  int exit_signal = SIGCONT;
784  if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
785    ALOGE("debuggerd: killing target %d", request.pid);
786    exit_signal = SIGKILL;
787  } else {
788    ALOGW("debuggerd: resuming target %d", request.pid);
789  }
790
791  if (kill(request.pid, exit_signal) != 0) {
792    ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
793  }
794
795  if (kill_self) {
796    stop_signal_sender();
797    _exit(1);
798  }
799}
800
801static void handle_request(int fd) {
802  ALOGV("handle_request(%d)\n", fd);
803
804  ScopedFd closer(fd);
805  debugger_request_t request;
806  memset(&request, 0, sizeof(request));
807  int status = read_request(fd, &request);
808  if (status != 0) {
809    return;
810  }
811
812  ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
813        request.gid, request.tid);
814
815#if defined(__LP64__)
816  // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
817  // to the 64 bit debuggerd. If the process is a 32 bit executable,
818  // redirect the request to the 32 bit debuggerd.
819  if (is32bit(request.tid)) {
820    // Only dump backtrace and dump tombstone requests can be redirected.
821    if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
822        request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
823      redirect_to_32(fd, &request);
824    } else {
825      ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
826    }
827    return;
828  }
829#endif
830
831  // Fork a child to handle the rest of the request.
832  pid_t fork_pid = fork();
833  if (fork_pid == -1) {
834    ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
835  } else if (fork_pid == 0) {
836    worker_process(fd, request);
837  } else {
838    monitor_worker_process(fork_pid, request);
839  }
840}
841
842static int do_server() {
843  // debuggerd crashes can't be reported to debuggerd.
844  // Reset all of the crash handlers.
845  signal(SIGABRT, SIG_DFL);
846  signal(SIGBUS, SIG_DFL);
847  signal(SIGFPE, SIG_DFL);
848  signal(SIGILL, SIG_DFL);
849  signal(SIGSEGV, SIG_DFL);
850#ifdef SIGSTKFLT
851  signal(SIGSTKFLT, SIG_DFL);
852#endif
853  signal(SIGTRAP, SIG_DFL);
854
855  // Ignore failed writes to closed sockets
856  signal(SIGPIPE, SIG_IGN);
857
858  // Block SIGCHLD so we can sigtimedwait for it.
859  sigset_t sigchld;
860  sigemptyset(&sigchld);
861  sigaddset(&sigchld, SIGCHLD);
862  sigprocmask(SIG_SETMASK, &sigchld, nullptr);
863
864  int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
865                              SOCK_STREAM | SOCK_CLOEXEC);
866  if (s == -1) return 1;
867
868  // Fork a process that stays root, and listens on a pipe to pause and resume the target.
869  if (!start_signal_sender()) {
870    ALOGE("debuggerd: failed to fork signal sender");
871    return 1;
872  }
873
874  ALOGI("debuggerd: starting\n");
875
876  for (;;) {
877    sockaddr_storage ss;
878    sockaddr* addrp = reinterpret_cast<sockaddr*>(&ss);
879    socklen_t alen = sizeof(ss);
880
881    ALOGV("waiting for connection\n");
882    int fd = accept4(s, addrp, &alen, SOCK_CLOEXEC);
883    if (fd == -1) {
884      ALOGE("accept failed: %s\n", strerror(errno));
885      continue;
886    }
887
888    handle_request(fd);
889  }
890  return 0;
891}
892
893static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
894  fprintf(stdout, "Sending request to dump task %d.\n", tid);
895
896  if (dump_backtrace) {
897    fflush(stdout);
898    if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
899      fputs("Error dumping backtrace.\n", stderr);
900      return 1;
901    }
902  } else {
903    char tombstone_path[PATH_MAX];
904    if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
905      fputs("Error dumping tombstone.\n", stderr);
906      return 1;
907    }
908    fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
909  }
910  return 0;
911}
912
913static void usage() {
914  fputs("Usage: -b [<tid>]\n"
915        "  -b dump backtrace to console, otherwise dump full tombstone file\n"
916        "\n"
917        "If tid specified, sends a request to debuggerd to dump that task.\n"
918        "Otherwise, starts the debuggerd server.\n", stderr);
919}
920
921int main(int argc, char** argv) {
922  union selinux_callback cb;
923  if (argc == 1) {
924    cb.func_audit = audit_callback;
925    selinux_set_callback(SELINUX_CB_AUDIT, cb);
926    cb.func_log = selinux_log_callback;
927    selinux_set_callback(SELINUX_CB_LOG, cb);
928    return do_server();
929  }
930
931  bool dump_backtrace = false;
932  bool have_tid = false;
933  pid_t tid = 0;
934  for (int i = 1; i < argc; i++) {
935    if (!strcmp(argv[i], "-b")) {
936      dump_backtrace = true;
937    } else if (!have_tid) {
938      tid = atoi(argv[i]);
939      have_tid = true;
940    } else {
941      usage();
942      return 1;
943    }
944  }
945  if (!have_tid) {
946    usage();
947    return 1;
948  }
949  return do_explicit_dump(tid, dump_backtrace);
950}
951