From: Remi Gacogne Date: Sat, 5 Mar 2022 16:06:29 +0000 (+0200) Subject: dnsdist: Prevent the allocation of a large buffer in NetworkListener::readCB() X-Git-Tag: dnsdist-1.8.0-rc1~285^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd23b326f4d8d1f8b43551b32c60e3f0ea227f7a;p=thirdparty%2Fpdns.git dnsdist: Prevent the allocation of a large buffer in NetworkListener::readCB() --- diff --git a/pdns/dnsdistdist/dnsdist-lua-network.cc b/pdns/dnsdistdist/dnsdist-lua-network.cc index bec50f8d1a..4165025c66 100644 --- a/pdns/dnsdistdist/dnsdist-lua-network.cc +++ b/pdns/dnsdistdist/dnsdist-lua-network.cc @@ -35,9 +35,18 @@ NetworkListener::NetworkListener() : void NetworkListener::readCB(int desc, FDMultiplexer::funcparam_t& param) { auto cbData = boost::any_cast>(param); - /* reuse ? */ std::string packet; - packet.resize(65535); + +#ifdef MSG_TRUNC + /* first we peek to avoid allocating a very large buffer. "MSG_TRUNC [...] return the real length of the datagram, even when it was longer than the passed buffer" */ + auto peeked = recvfrom(desc, nullptr, 0, MSG_PEEK | MSG_TRUNC, nullptr, 0); + if (peeked > 0) { + packet.resize(static_cast(peeked)); + } +#endif + if (packet.size() == 0) { + packet.resize(65535); + } struct sockaddr_un from; memset(&from, 0, sizeof(from));