From a7b68ae7e414ec9f3184df70ac8008f8a310ae60 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Mon, 5 Dec 2016 16:42:55 +0100 Subject: [PATCH] Handle exceptions raised by `closesocket()` This was not very well handled, and could cause the PowerDNS process to terminate. This is especially nasty when `closesocket()` is called from a destructor, as we could already be dealing with an exception. --- pdns/dnsproxy.cc | 9 +++++++- pdns/pdns_recursor.cc | 46 +++++++++++++++++++++++++++++++++------ pdns/resolver.cc | 19 +++++++++++++--- pdns/rfc2136handler.cc | 49 ++++++++++++++++++++++++++++++++++++------ pdns/sstuff.hh | 7 +++++- pdns/tcpreceiver.cc | 15 +++++++++++-- pdns/utility.hh | 3 --- 7 files changed, 124 insertions(+), 24 deletions(-) diff --git a/pdns/dnsproxy.cc b/pdns/dnsproxy.cc index 280fc127c4..0c300b086f 100644 --- a/pdns/dnsproxy.cc +++ b/pdns/dnsproxy.cc @@ -303,6 +303,13 @@ void DNSProxy::mainloop(void) } DNSProxy::~DNSProxy() { - if (d_sock>-1) closesocket(d_sock); + if (d_sock>-1) { + try { + closesocket(d_sock); + } + catch(const PDNSException& e) { + } + } + d_sock=-1; } diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index e89b1855b0..51457f8068 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -384,7 +384,13 @@ public: if(connect(*fd, (struct sockaddr*)(&toaddr), toaddr.getSocklen()) < 0) { int err = errno; // returnSocket(*fd); - closesocket(*fd); + try { + closesocket(*fd); + } + catch(const PDNSException& e) { + L<count(d_remote) && !(*t_tcpClientCounts)[d_remote]--) t_tcpClientCounts->erase(d_remote); --s_currentConnections; @@ -1405,7 +1422,12 @@ void handleNewTCPQuestion(int fd, FDMultiplexer::funcparam_t& ) if(newsock>=0) { if(MT->numProcesses() > g_maxMThreads) { g_stats.overCapacityDrops++; - closesocket(newsock); + try { + closesocket(newsock); + } + catch(const PDNSException& e) { + L<getTid()<<"] dropping TCP query from "<count(addr) && (*t_tcpClientCounts)[addr] >= g_maxTCPPerClient) { g_stats.tcpClientOverflow++; - closesocket(newsock); // don't call TCPConnection::closeAndCleanup here - did not enter it in the counts yet! + try { + closesocket(newsock); // don't call TCPConnection::closeAndCleanup here - did not enter it in the counts yet! + } + catch(const PDNSException& e) { + L<post(); - closesocket(fd); + try { + closesocket(fd); + } + catch(const PDNSException& e) { + L<post(); - closesocket(fd); + + try { + closesocket(fd); + } + catch(const PDNSException& e) { + L< 0 on success -- 2.47.2