]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #3902: appid: cache CHP glossary before detectors reload
authorOleksandr Stepanov -X (ostepano - SOFTSERVE INC at Cisco) <ostepano@cisco.com>
Thu, 13 Jul 2023 12:57:03 +0000 (12:57 +0000)
committerChris Sherwin (chsherwi) <chsherwi@cisco.com>
Thu, 13 Jul 2023 12:57:03 +0000 (12:57 +0000)
Merge in SNORT/snort3 from ~OSTEPANO/snort3:chp_heap_fix to master

Squashed commit of the following:

commit 4a19f21de3a022a1b88234f6def378a7a8e0941a
Author: Oleksandr Stepanov <ostepano@cisco.com>
Date:   Thu Jul 6 05:59:00 2023 -0400

    appid: cache CHP glossary before detectors reload

src/network_inspectors/appid/appid_module.cc
src/network_inspectors/appid/lua_detector_api.cc
src/network_inspectors/appid/lua_detector_api.h
src/network_inspectors/appid/lua_detector_module.cc
src/network_inspectors/appid/lua_detector_module.h

index f95d01f94933598f4e74ca51da3f1f5fc05d4712..08d898118c6f9d88908c1ea7aafb3f1462d756cc 100644 (file)
@@ -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;
 
index 5f9ab3fc987274c37005850189bcfe9e100373b4..337336467e6f73e510aa6f894bade5142a440fd9 100644 (file)
@@ -69,25 +69,38 @@ enum LuaLogLevels
     LUA_LOG_TRACE = 5,
 };
 
-static std::unordered_map<AppId, CHPApp*>* 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<AppId, CHPApp*>;
+    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)
index 5619e3f237f4f1a61d18802b9cdabd49f0368d07..269e33be1d56f0813d10ceffe01750f26d48b3b0 100644 (file)
@@ -155,10 +155,13 @@ public:
     { return cd; }
 };
 
+typedef std::unordered_map<AppId, CHPApp*> 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);
index 8bd6512f0e536c3132e544aa1fa543ce1e61522c..9ac9afc9e132198c8bfd363a30b3207013ac7b4a 100644 (file)
@@ -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();
index 7d10e2166cfb2a6c1a484e23e89da27b0595b914..b52f84d30bf943baf6a442485e31c05878807eb9 100644 (file)
@@ -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<AppId, LuaObject*> cb_detectors;
     DetectorFlow* detector_flow = nullptr;
+    bool ignore_chp_cleanup = false;
 };
 
 #endif