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.
This commit is contained in:
rofl0r 2016-05-26 19:01:14 +01:00
parent 8870140ff0
commit f1e5f2ba01

View File

@ -110,6 +110,9 @@ static void setup_hooks(void) {
SETUP_SYM(close); SETUP_SYM(close);
} }
static int close_fds[16];
static int close_fds_cnt = 0;
static void do_init(void) { static void do_init(void) {
srand(time(NULL)); srand(time(NULL));
core_initialize(); core_initialize();
@ -123,6 +126,8 @@ static void do_init(void) {
setup_hooks(); setup_hooks();
while(close_fds_cnt) true_close(close_fds[--close_fds_cnt]);
init_l = 1; 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) { int close(int fd) {
if(!init_l) { if(!init_l) {
SETUP_SYM(close); if(close_fds_cnt>=(sizeof close_fds/sizeof close_fds[0])) goto err;
return true_close(fd); close_fds[close_fds_cnt++] = fd;
errno = 0;
return 0;
} }
/* prevent rude programs (like ssh) from closing our pipes */ /* prevent rude programs (like ssh) from closing our pipes */
if(fd != req_pipefd[0] && fd != req_pipefd[1] && if(fd != req_pipefd[0] && fd != req_pipefd[1] &&
fd != resp_pipefd[0] && fd != resp_pipefd[1]) { fd != resp_pipefd[0] && fd != resp_pipefd[1]) {
return true_close(fd); return true_close(fd);
} }
err:
errno = EBADF; errno = EBADF;
return -1; return -1;
} }