]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Fix memory corruption when using `getAddressInfo` 15514/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 30 Apr 2025 14:51:04 +0000 (16:51 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 30 Apr 2025 14:51:04 +0000 (16:51 +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.

pdns/dnsdistdist/dnsdist-lua-bindings.cc

index a0e4ed0d6693b24fec729e9500d2d5bd50a75fa8..abc80f7d1b258371ea3798e7ec10c5c593065216 100644 (file)
@@ -869,7 +869,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) {
@@ -877,7 +877,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();
       }
     });