]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Prevent the allocation of a large buffer in NetworkListener::readCB()
authorRemi Gacogne <remi.gacogne@powerdns.com>
Sat, 5 Mar 2022 16:06:29 +0000 (18:06 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 7 Oct 2022 15:48:06 +0000 (17:48 +0200)
pdns/dnsdistdist/dnsdist-lua-network.cc

index bec50f8d1ab0e345aec8877ab3022cbb75ff7202..4165025c665437a357139f643f0e2d0d32c5c4b0 100644 (file)
@@ -35,9 +35,18 @@ NetworkListener::NetworkListener() :
 void NetworkListener::readCB(int desc, FDMultiplexer::funcparam_t& param)
 {
   auto cbData = boost::any_cast<std::shared_ptr<NetworkListener::CBData>>(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<size_t>(peeked));
+  }
+#endif
+  if (packet.size() == 0) {
+    packet.resize(65535);
+  }
 
   struct sockaddr_un from;
   memset(&from, 0, sizeof(from));