From f1e5f2ba0153a1d3a9bb3c3dae3925236c58b077 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Thu, 26 May 2016 19:01:14 +0100 Subject: [PATCH] don't call dlsym() from close() hook it turned out that calling dlsym() may call malloc() in turn, so we end up with the same deadlock described in the latest commit. we thus now put all the fds passed to close pre-init into a list and close them at init time. this may finally fix #119. --- src/libproxychains.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libproxychains.c b/src/libproxychains.c index d8fab4c..b9ffab9 100644 --- a/src/libproxychains.c +++ b/src/libproxychains.c @@ -110,6 +110,9 @@ static void setup_hooks(void) { SETUP_SYM(close); } +static int close_fds[16]; +static int close_fds_cnt = 0; + static void do_init(void) { srand(time(NULL)); core_initialize(); @@ -123,6 +126,8 @@ static void do_init(void) { setup_hooks(); + while(close_fds_cnt) true_close(close_fds[--close_fds_cnt]); + init_l = 1; } @@ -305,14 +310,17 @@ static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_typ int close(int fd) { if(!init_l) { - SETUP_SYM(close); - return true_close(fd); + if(close_fds_cnt>=(sizeof close_fds/sizeof close_fds[0])) goto err; + close_fds[close_fds_cnt++] = fd; + errno = 0; + return 0; } /* prevent rude programs (like ssh) from closing our pipes */ if(fd != req_pipefd[0] && fd != req_pipefd[1] && fd != resp_pipefd[0] && fd != resp_pipefd[1]) { return true_close(fd); } + err: errno = EBADF; return -1; }