From: Remi Gacogne Date: Mon, 11 Jan 2021 14:37:52 +0000 (+0100) Subject: dnsdist: Fix a crash when a DoH responses map is updated at runtime X-Git-Tag: rec-4.5.0-alpha1~8^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F9934%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Fix a crash when a DoH responses map is updated at runtime --- diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 97a748ac44..e8049beb34 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -2192,11 +2192,11 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) luaCtx.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 b1a0c4c027..919db49bcf 100644 --- a/pdns/dnsdistdist/doh.cc +++ b/pdns/dnsdistdist/doh.cc @@ -843,11 +843,16 @@ static int doh_handler(h2o_handler_t *self, h2o_req_t *req) // would be nice to be able to use a pdns_string_view there, // but regex (called by matches() internally) requires a null-terminated string string path(req->path.base, req->path.len); - 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 5327457d7f..10ed9a1b6d 100644 --- a/pdns/doh.hh +++ b/pdns/doh.hh @@ -71,7 +71,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"};