From: Otto Moerbeek Date: Wed, 20 Nov 2019 12:39:09 +0000 (+0100) Subject: Check return value of dup() and avoid fd leak if if fdopen() fails. X-Git-Tag: auth-4.3.0-alpha1~39^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a3284c1a9e8ad9fc14fd075d2296e66ac46d1aca;p=thirdparty%2Fpdns.git Check return value of dup() and avoid fd leak if if fdopen() fails. --- diff --git a/pdns/syncres.cc b/pdns/syncres.cc index 501d453f33..4dc7740151 100644 --- a/pdns/syncres.cc +++ b/pdns/syncres.cc @@ -413,8 +413,13 @@ bool SyncRes::isForwardOrAuth(const DNSName &qname) const { uint64_t SyncRes::doEDNSDump(int fd) { - auto fp = std::unique_ptr(fdopen(dup(fd), "w"), fclose); + int newfd = dup(fd); + if (newfd == -1) { + return 0; + } + auto fp = std::unique_ptr(fdopen(newfd, "w"), fclose); if (!fp) { + close(newfd); return 0; } uint64_t count = 0; @@ -430,9 +435,15 @@ uint64_t SyncRes::doEDNSDump(int fd) uint64_t SyncRes::doDumpNSSpeeds(int fd) { - auto fp = std::unique_ptr(fdopen(dup(fd), "w"), fclose); - if(!fp) + int newfd = dup(fd); + if (newfd == -1) { return 0; + } + auto fp = std::unique_ptr(fdopen(newfd, "w"), fclose); + if (!fp) { + close(newfd); + return 0; + } fprintf(fp.get(), "; nsspeed dump from thread follows\n;\n"); uint64_t count=0; @@ -454,9 +465,15 @@ uint64_t SyncRes::doDumpNSSpeeds(int fd) uint64_t SyncRes::doDumpThrottleMap(int fd) { - auto fp = std::unique_ptr(fdopen(dup(fd), "w"), fclose); - if(!fp) + int newfd = dup(fd); + if (newfd == -1) { + return 0; + } + auto fp = std::unique_ptr(fdopen(newfd, "w"), fclose); + if (!fp) { + close(newfd); return 0; + } fprintf(fp.get(), "; throttle map dump follows\n"); fprintf(fp.get(), "; remote IP\tqname\tqtype\tcount\tttd\n"); uint64_t count=0; @@ -475,9 +492,15 @@ uint64_t SyncRes::doDumpThrottleMap(int fd) uint64_t SyncRes::doDumpFailedServers(int fd) { - auto fp = std::unique_ptr(fdopen(dup(fd), "w"), fclose); - if(!fp) + int newfd = dup(fd); + if (newfd == -1) { return 0; + } + auto fp = std::unique_ptr(fdopen(newfd, "w"), fclose); + if (!fp) { + close(newfd); + return 0; + } fprintf(fp.get(), "; failed servers dump follows\n"); fprintf(fp.get(), "; remote IP\tcount\ttimestamp\n"); uint64_t count=0;