From: Oleksandr Stepanov -X (ostepano - SOFTSERVE INC at Cisco) Date: Thu, 13 Jul 2023 12:57:03 +0000 (+0000) Subject: Pull request #3902: appid: cache CHP glossary before detectors reload X-Git-Tag: 3.1.66.0~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=16687f4e0a7beb791f2516c5253244d33f099290;p=thirdparty%2Fsnort3.git Pull request #3902: appid: cache CHP glossary before detectors reload Merge in SNORT/snort3 from ~OSTEPANO/snort3:chp_heap_fix to master Squashed commit of the following: commit 4a19f21de3a022a1b88234f6def378a7a8e0941a Author: Oleksandr Stepanov Date: Thu Jul 6 05:59:00 2023 -0400 appid: cache CHP glossary before detectors reload --- diff --git a/src/network_inspectors/appid/appid_module.cc b/src/network_inspectors/appid/appid_module.cc index f95d01f94..08d898118 100644 --- a/src/network_inspectors/appid/appid_module.cc +++ b/src/network_inspectors/appid/appid_module.cc @@ -269,6 +269,7 @@ ACOdpContextSwap::~ACOdpContextSwap() odp_ctxt.get_app_info_mgr().cleanup_appid_info_table(); delete &odp_ctxt; AppIdContext& ctxt = inspector.get_ctxt(); + LuaDetectorManager::cleanup_after_swap(); if (ctxt.config.app_detector_dir) { std::string file_path = std::string(ctxt.config.app_detector_dir) + "/custom/userappid.conf"; @@ -405,6 +406,7 @@ static int reload_detectors(lua_State* L) LuaDetectorManager::clear_lua_detector_mgrs(); ctxt.create_odp_ctxt(); assert(odp_thread_local_ctxt); + odp_thread_local_ctxt->get_lua_detector_mgr().set_ignore_chp_cleanup(true); delete odp_thread_local_ctxt; odp_thread_local_ctxt = new OdpThreadContext; diff --git a/src/network_inspectors/appid/lua_detector_api.cc b/src/network_inspectors/appid/lua_detector_api.cc index 5f9ab3fc9..337336467 100644 --- a/src/network_inspectors/appid/lua_detector_api.cc +++ b/src/network_inspectors/appid/lua_detector_api.cc @@ -69,25 +69,38 @@ enum LuaLogLevels LUA_LOG_TRACE = 5, }; -static std::unordered_map* CHP_glossary = nullptr; // tracks http multipatterns +static CHPGlossary* CHP_glossary = nullptr; // tracks http multipatterns +static CHPGlossary* old_CHP_glossary = nullptr; void init_chp_glossary() { - CHP_glossary = new std::unordered_map; + if(CHP_glossary) + old_CHP_glossary = CHP_glossary; + CHP_glossary = new CHPGlossary; } -void free_chp_glossary() +static void free_chp_glossary(CHPGlossary*& glossary) { - if (!CHP_glossary) + + if (!glossary) return; - for (auto& entry : *CHP_glossary) + for (auto& entry : *glossary) { if (entry.second) snort_free(entry.second); } - delete CHP_glossary; - CHP_glossary = nullptr; + delete glossary; + glossary = nullptr; +} + +void free_current_chp_glossary(){ + free_chp_glossary(CHP_glossary); +} + +void free_old_chp_glossary() +{ + free_chp_glossary(old_CHP_glossary); } static inline int convert_string_to_address(const char* string, SfIp* address) diff --git a/src/network_inspectors/appid/lua_detector_api.h b/src/network_inspectors/appid/lua_detector_api.h index 5619e3f23..269e33be1 100644 --- a/src/network_inspectors/appid/lua_detector_api.h +++ b/src/network_inspectors/appid/lua_detector_api.h @@ -155,10 +155,13 @@ public: { return cd; } }; +typedef std::unordered_map CHPGlossary; + int register_detector(lua_State*); void init_chp_glossary(); int init(lua_State*, int result=0); -void free_chp_glossary(); +void free_current_chp_glossary(); +void free_old_chp_glossary(); void check_detector_callback(const snort::Packet& p, AppIdSession& asd, AppidSessionDirection dir, AppId app_id, AppidChangeBits& change_bits, AppInfoTableEntry* entry = nullptr); diff --git a/src/network_inspectors/appid/lua_detector_module.cc b/src/network_inspectors/appid/lua_detector_module.cc index 8bd6512f0..9ac9afc9e 100644 --- a/src/network_inspectors/appid/lua_detector_module.cc +++ b/src/network_inspectors/appid/lua_detector_module.cc @@ -170,8 +170,8 @@ LuaDetectorManager::~LuaDetectorManager() if (L) { - if (init(L)) - free_chp_glossary(); + if (init(L) and !ignore_chp_cleanup) + free_current_chp_glossary(); for ( auto& lua_object : allocated_objects ) { @@ -242,6 +242,11 @@ void LuaDetectorManager::init_thread_manager(const SnortConfig* sc, const AppIdC lua_detector_mgr->list_lua_detectors(); } +void LuaDetectorManager::cleanup_after_swap() +{ + free_old_chp_glossary(); +} + void LuaDetectorManager::clear_lua_detector_mgrs() { lua_detector_mgr_list.clear(); diff --git a/src/network_inspectors/appid/lua_detector_module.h b/src/network_inspectors/appid/lua_detector_module.h index 7d10e2166..b52f84d30 100644 --- a/src/network_inspectors/appid/lua_detector_module.h +++ b/src/network_inspectors/appid/lua_detector_module.h @@ -58,6 +58,7 @@ public: static void initialize(const snort::SnortConfig*, AppIdContext&, bool is_control=false, bool reload=false); static void init_thread_manager(const snort::SnortConfig*, const AppIdContext&); + static void cleanup_after_swap(); static void clear_lua_detector_mgrs(); void set_detector_flow(DetectorFlow* df) @@ -69,6 +70,12 @@ public: { return detector_flow; } + + void set_ignore_chp_cleanup(bool value) + { + ignore_chp_cleanup = value; + } + void free_detector_flow(); lua_State* L; bool insert_cb_detector(AppId app_id, LuaObject* ud); @@ -88,6 +95,7 @@ private: size_t num_odp_detectors = 0; std::map cb_detectors; DetectorFlow* detector_flow = nullptr; + bool ignore_chp_cleanup = false; }; #endif