]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Fix memory corruption when using `getAddressInfo` 15519/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 30 Apr 2025 14:51:04 +0000 (16:51 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 5 May 2025 10:58:54 +0000 (12:58 +0200)
The object holding the callback function, which is translated into
a `LuaContext::LuaFunctionCaller`, needs to be destroyed while holding
the Lua mutex because it will unregister itself from the Lua context,
causing a corruption if a different thread is accessing the Lua context
at the same time.

(cherry picked from commit fdfda2cc394e49c83b36536e14c91f165fef5a5d)

pdns/dnsdist-lua-bindings.cc

index ffed8f0bc171560ea4c2a13f2fa309e4cd6266cd..48673e81dcd8e30757688a9c7340926d1c719226 100644 (file)
@@ -845,7 +845,7 @@ void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck)
     if (client || configCheck) {
       return;
     }
-    std::thread newThread(dnsdist::resolver::asynchronousResolver, std::move(hostname), [callback=std::move(callback)](const std::string& resolvedHostname, std::vector<ComboAddress>& ips) {
+    std::thread newThread(dnsdist::resolver::asynchronousResolver, std::move(hostname), [callback = std::move(callback)](const std::string& resolvedHostname, std::vector<ComboAddress>& ips) mutable {
       LuaArray<ComboAddress> result;
       result.reserve(ips.size());
       for (const auto& entry : ips) {
@@ -853,7 +853,15 @@ void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck)
       }
       {
         auto lua = g_lua.lock();
-        callback(resolvedHostname, result);
+        try {
+          callback(resolvedHostname, result);
+        }
+        catch (const std::exception& exp) {
+          vinfolog("Error during execution of getAddressInfo callback: %s", exp.what());
+        }
+        // this _needs_ to be done while we are holding the lock,
+        // otherwise the destructor will corrupt the stack
+        callback = nullptr;
         dnsdist::handleQueuedAsynchronousEvents();
       }
     });