From 38a446f599ba88743d30946c151907bba1ccd0b8 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Mon, 11 Jan 2021 15:37:52 +0100 Subject: [PATCH] dnsdist: Fix a crash when a DoH responses map is updated at runtime (cherry picked from commit 312baf27f5357fae66582a8bbe937abff3c8d4fc) --- pdns/dnsdist-lua.cc | 6 +++--- pdns/dnsdistdist/doh.cc | 15 ++++++++++----- pdns/doh.hh | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 5de7eb5991..730ebe5e26 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -2079,11 +2079,11 @@ static void setupLuaConfig(bool client, bool configCheck) g_lua.registerFunction::*)(const std::map>&)>("setResponsesMap", [](std::shared_ptr frontend, const std::map>& map) { if (frontend != nullptr) { - std::vector> newMap; - newMap.reserve(map.size()); + auto newMap = std::make_shared>>(); + newMap->reserve(map.size()); for (const auto& entry : map) { - newMap.push_back(entry.second); + newMap->push_back(entry.second); } frontend->d_responsesMap = std::move(newMap); diff --git a/pdns/dnsdistdist/doh.cc b/pdns/dnsdistdist/doh.cc index 24e17b000c..86efdf1eed 100644 --- a/pdns/dnsdistdist/doh.cc +++ b/pdns/dnsdistdist/doh.cc @@ -824,11 +824,16 @@ try return 0; } - for (const auto& entry : dsc->df->d_responsesMap) { - if (entry->matches(path)) { - const auto& customHeaders = entry->getHeaders(); - handleResponse(*dsc->df, req, entry->getStatusCode(), entry->getContent(), customHeaders ? *customHeaders : dsc->df->d_customResponseHeaders, std::string(), false); - return 0; + /* the responses map can be updated at runtime, so we need to take a copy of + the shared pointer, increasing the reference counter */ + auto responsesMap = dsc->df->d_responsesMap; + if (responsesMap) { + for (const auto& entry : *responsesMap) { + if (entry->matches(path)) { + const auto& customHeaders = entry->getHeaders(); + handleResponse(*dsc->df, req, entry->getStatusCode(), entry->getContent(), customHeaders ? *customHeaders : dsc->df->d_customResponseHeaders, std::string(), false); + return 0; + } } } diff --git a/pdns/doh.hh b/pdns/doh.hh index 36d720cd42..87b1e1ad02 100644 --- a/pdns/doh.hh +++ b/pdns/doh.hh @@ -66,7 +66,7 @@ struct DOHFrontend } std::shared_ptr d_dsc{nullptr}; - std::vector> d_responsesMap; + std::shared_ptr>> d_responsesMap; TLSConfig d_tlsConfig; TLSErrorCounters d_tlsCounters; std::string d_serverTokens{"h2o/dnsdist"}; -- 2.47.2