Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c954ec09 authored by Josh Gao's avatar Josh Gao
Browse files

debuggerd_handler: use syscall(__NR_close) instead of close.

Avoid bionic's file descriptor ownership checks by calling the close
syscall manually.

Test: debuggerd_test
Change-Id: I10af6aca0e66fe030fd7a53506ae61c87695641d
parent fcf2c01b
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -151,7 +151,8 @@ using unique_fd = unique_fd_impl<DefaultCloser>;
#if !defined(_WIN32)

// Inline functions, so that they can be used header-only.
inline bool Pipe(unique_fd* read, unique_fd* write) {
template <typename Closer>
inline bool Pipe(unique_fd_impl<Closer>* read, unique_fd_impl<Closer>* write) {
  int pipefd[2];

#if defined(__linux__)
@@ -175,7 +176,9 @@ inline bool Pipe(unique_fd* read, unique_fd* write) {
  return true;
}

inline bool Socketpair(int domain, int type, int protocol, unique_fd* left, unique_fd* right) {
template <typename Closer>
inline bool Socketpair(int domain, int type, int protocol, unique_fd_impl<Closer>* left,
                       unique_fd_impl<Closer>* right) {
  int sockfd[2];
  if (socketpair(domain, type, protocol, sockfd) != 0) {
    return false;
@@ -185,7 +188,8 @@ inline bool Socketpair(int domain, int type, int protocol, unique_fd* left, uniq
  return true;
}

inline bool Socketpair(int type, unique_fd* left, unique_fd* right) {
template <typename Closer>
inline bool Socketpair(int type, unique_fd_impl<Closer>* left, unique_fd_impl<Closer>* right) {
  return Socketpair(AF_UNIX, type, 0, left, right);
}

+12 −2
Original line number Diff line number Diff line
@@ -59,7 +59,16 @@
#include "protocol.h"

using android::base::Pipe;
using android::base::unique_fd;

// We muck with our fds in a 'thread' that doesn't share the same fd table.
// Close fds in that thread with a raw close syscall instead of going through libc.
struct FdsanBypassCloser {
  static void Close(int fd) {
    syscall(__NR_close, fd);
  }
};

using unique_fd = android::base::unique_fd_impl<FdsanBypassCloser>;

// see man(2) prctl, specifically the section about PR_GET_NAME
#define MAX_TASK_NAME_LEN (16)
@@ -299,7 +308,8 @@ static int debuggerd_dispatch_pseudothread(void* arg) {
  debugger_thread_info* thread_info = static_cast<debugger_thread_info*>(arg);

  for (int i = 0; i < 1024; ++i) {
    close(i);
    // Don't use close to avoid bionic's file descriptor ownership checks.
    syscall(__NR_close, i);
  }

  int devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));