From: Remi Gacogne Date: Tue, 29 Mar 2016 12:49:50 +0000 (+0200) Subject: dnsdist: Prevent dangling TCP fd in case setupTCPDownstream() fails X-Git-Tag: dnsdist-1.0.0-beta1~48^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F3634%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Prevent dangling TCP fd in case setupTCPDownstream() fails Remove the closed socket descriptor from the sockets map. Otherwise, if an exception occurs in setupTCPDownstream(), we might try to use it and close it again later, not knowing it has been reassigned to another socket or, worse, to a TCP acceptor pipe. --- diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index 81954a30fc..eaeb974317 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -358,6 +358,8 @@ void* tcpClientThread(int pipefd) if(!sendNonBlockingMsgLen(dsock, dq.len, ds->tcpSendTimeout, ds->remote, ds->sourceAddr, ds->sourceItf)) { vinfolog("Downstream connection to %s died on us, getting a new one!", ds->getName()); close(dsock); + dsock=-1; + sockets.erase(ds->remote); sockets[ds->remote]=dsock=setupTCPDownstream(ds); downstream_failures++; goto retry; @@ -374,6 +376,8 @@ void* tcpClientThread(int pipefd) catch(const runtime_error& e) { vinfolog("Downstream connection to %s died on us, getting a new one!", ds->getName()); close(dsock); + dsock=-1; + sockets.erase(ds->remote); sockets[ds->remote]=dsock=setupTCPDownstream(ds); downstream_failures++; goto retry; @@ -382,6 +386,8 @@ void* tcpClientThread(int pipefd) if(!getNonBlockingMsgLen(dsock, &rlen, ds->tcpRecvTimeout)) { vinfolog("Downstream connection to %s died on us phase 2, getting a new one!", ds->getName()); close(dsock); + dsock=-1; + sockets.erase(ds->remote); sockets[ds->remote]=dsock=setupTCPDownstream(ds); downstream_failures++; goto retry;