Skip to content

Commit c954ec0

Browse files
committed
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
1 parent fcf2c01 commit c954ec0

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

base/include/android-base/unique_fd.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ using unique_fd = unique_fd_impl<DefaultCloser>;
151151
#if !defined(_WIN32)
152152

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

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

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

188-
inline bool Socketpair(int type, unique_fd* left, unique_fd* right) {
191+
template <typename Closer>
192+
inline bool Socketpair(int type, unique_fd_impl<Closer>* left, unique_fd_impl<Closer>* right) {
189193
return Socketpair(AF_UNIX, type, 0, left, right);
190194
}
191195

debuggerd/handler/debuggerd_handler.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@
5959
#include "protocol.h"
6060

6161
using android::base::Pipe;
62-
using android::base::unique_fd;
62+
63+
// We muck with our fds in a 'thread' that doesn't share the same fd table.
64+
// Close fds in that thread with a raw close syscall instead of going through libc.
65+
struct FdsanBypassCloser {
66+
static void Close(int fd) {
67+
syscall(__NR_close, fd);
68+
}
69+
};
70+
71+
using unique_fd = android::base::unique_fd_impl<FdsanBypassCloser>;
6372

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

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

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

0 commit comments

Comments
 (0)