From: Remi Gacogne Date: Wed, 2 Mar 2016 15:50:02 +0000 (+0100) Subject: dnsdist: Fix a crash when adding an invalid packet to the cache X-Git-Tag: rec-4.0.0-alpha2~21^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cceddbeff82cf031f93af458361bfcfb23909586;p=pdns dnsdist: Fix a crash when adding an invalid packet to the cache Reported by @rygl. --- diff --git a/pdns/dnsdist-cache.cc b/pdns/dnsdist-cache.cc index 2d951bc56..ed5221091 100644 --- a/pdns/dnsdist-cache.cc +++ b/pdns/dnsdist-cache.cc @@ -197,10 +197,17 @@ uint32_t DNSDistPacketCache::getKey(const DNSName& qname, uint16_t consumed, con { uint32_t result = 0; /* skip the query ID */ + if (packetLen < sizeof(dnsheader)) + throw std::range_error("Computing packet cache key for an invalid packet size"); result = burtle(packet + 2, sizeof(dnsheader) - 2, result); string lc(qname.toDNSStringLC()); result = burtle((const unsigned char*) lc.c_str(), lc.length(), result); - result = burtle(packet + sizeof(dnsheader) + consumed, packetLen - (sizeof(dnsheader) + consumed), result); + if (packetLen < sizeof(dnsheader) + consumed) { + throw std::range_error("Computing packet cache key for an invalid packet"); + } + if (packetLen > ((sizeof(dnsheader) + consumed))) { + result = burtle(packet + sizeof(dnsheader) + consumed, packetLen - (sizeof(dnsheader) + consumed), result); + } result = burtle((const unsigned char*) &tcp, sizeof(tcp), result); return result; }