]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: move mpm engines into detect engine ctx
authorVictor Julien <victor@inliniac.net>
Mon, 30 Oct 2017 21:37:42 +0000 (22:37 +0100)
committerVictor Julien <victor@inliniac.net>
Wed, 14 Feb 2018 13:25:46 +0000 (14:25 +0100)
This allows safe registration at runtime.

src/detect-engine-build.c
src/detect-engine-mpm.c
src/detect-engine-mpm.h
src/detect-engine.c
src/detect.h

index e68aba7c8d7427cd1ca8325a72354f2129e559d7..39f78f27f503045ce9edacc44673ca3e650acb99 100644 (file)
@@ -554,7 +554,7 @@ static int SignatureCreateMask(Signature *s)
 static void SigInitStandardMpmFactoryContexts(DetectEngineCtx *de_ctx)
 {
     DetectMpmInitializeBuiltinMpms(de_ctx);
-    DetectMpmInitializeAppMpms(de_ctx);
+    DetectMpmSetupAppMpms(de_ctx);
 
     return;
 }
index 47b0c1c10425f7fc0e7362cf4f1db42afa772379..ffad501ba2ac7fd7d91e11b80e33a3109b47cd4a 100644 (file)
@@ -183,12 +183,14 @@ void DetectAppLayerMpmRegister(const char *name,
     SupportFastPatternForSigMatchList(sm_list, priority);
 }
 
-void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
+/** \brief copy a mpm engine from parent_id, add in transforms */
+void DetectAppLayerMpmRegisterByParentId(DetectEngineCtx *de_ctx,
+        const int id, const int parent_id,
         DetectEngineTransforms *transforms)
 {
     SCLogDebug("registering %d/%d", id, parent_id);
 
-    DetectMpmAppLayerRegistery *t = g_app_mpms_list;
+    DetectMpmAppLayerRegistery *t = de_ctx->app_mpms_list;
     while (t) {
         if (t->sm_list == parent_id) {
             DetectMpmAppLayerRegistery *am = SCCalloc(1, sizeof(*am));
@@ -196,7 +198,7 @@ void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
             am->name = t->name;
             snprintf(am->pname, sizeof(am->pname), "%s#%d", am->name, id);
             am->direction = t->direction;
-            am->sm_list = id;           // use new id
+            am->sm_list = id; // use new id
             am->PrefilterRegister = t->PrefilterRegister;
             am->v2.PrefilterRegisterWithListId = t->v2.PrefilterRegisterWithListId;
             am->v2.GetData = t->v2.GetData;
@@ -207,11 +209,12 @@ void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
             if (transforms) {
                 memcpy(&am->v2.transforms, transforms, sizeof(*transforms));
             }
-            am->id = g_app_mpms_list_cnt++;
+            am->id = de_ctx->app_mpms_list_cnt++;
 
             SupportFastPatternForSigMatchList(am->sm_list, am->priority);
             t->next = am;
-            SCLogDebug("copied mpm registration for %s id %u with parent %u and GetData %p",
+            SCLogDebug("copied mpm registration for %s id %u "
+                    "with parent %u and GetData %p",
                     t->name, id, parent_id, am->v2.GetData);
             t = am;
         }
@@ -221,12 +224,40 @@ void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
 
 void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx)
 {
-    BUG_ON(g_app_mpms_list_cnt == 0);
+    const DetectMpmAppLayerRegistery *list = g_app_mpms_list;
+    while (list != NULL) {
+        DetectMpmAppLayerRegistery *n = SCCalloc(1, sizeof(*n));
+        BUG_ON(n == NULL);
+
+        *n = *list;
+        n->next = NULL;
+
+        if (de_ctx->app_mpms_list == NULL) {
+            de_ctx->app_mpms_list = n;
+        } else {
+            DetectMpmAppLayerRegistery *t = de_ctx->app_mpms_list;
+            while (t->next != NULL) {
+                t = t->next;
+            }
+
+            t->next = n;
+        }
+
+        list = list->next;
+    }
+    de_ctx->app_mpms_list_cnt = g_app_mpms_list_cnt;
+    SCLogDebug("mpm: de_ctx app_mpms_list %p %u",
+            de_ctx->app_mpms_list, de_ctx->app_mpms_list_cnt);
+}
+
+void DetectMpmSetupAppMpms(DetectEngineCtx *de_ctx)
+{
+    BUG_ON(de_ctx->app_mpms_list_cnt == 0);
 
-    de_ctx->app_mpms = SCCalloc(g_app_mpms_list_cnt + 1, sizeof(DetectMpmAppLayerKeyword));
+    de_ctx->app_mpms = SCCalloc(de_ctx->app_mpms_list_cnt + 1, sizeof(DetectMpmAppLayerKeyword));
     BUG_ON(de_ctx->app_mpms == NULL);
 
-    DetectMpmAppLayerRegistery *list = g_app_mpms_list;
+    DetectMpmAppLayerRegistery *list = de_ctx->app_mpms_list;
     while (list != NULL) {
         DetectMpmAppLayerKeyword *am = &de_ctx->app_mpms[list->id];
         am->reg = list;
index 3574db758dc74149ed54e63ea836f01b93327be9..316b8938e4ccd10c70bdf94b86e585e692e1ea71 100644 (file)
@@ -33,6 +33,7 @@
 #include "stream.h"
 
 void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx);
+void DetectMpmSetupAppMpms(DetectEngineCtx *de_ctx);
 int DetectMpmPrepareAppMpms(DetectEngineCtx *de_ctx);
 void DetectMpmInitializeBuiltinMpms(DetectEngineCtx *de_ctx);
 int DetectMpmPrepareBuiltinMpms(DetectEngineCtx *de_ctx);
@@ -95,7 +96,9 @@ void DetectAppLayerMpmRegister2(const char *name,
             const DetectMpmAppLayerRegistery *mpm_reg, int list_id),
         InspectionBufferGetDataPtr GetData,
         AppProto alproto, int tx_min_progress);
-void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
+void DetectAppLayerMpmRegisterByParentId(
+        DetectEngineCtx *de_ctx,
+        const int id, const int parent_id,
         DetectEngineTransforms *transforms);
 
 #endif /* __DETECT_ENGINE_MPM_H__ */
index 2a9a0d3fe697afbf380c0239d17c2ffccc4945d4..62978dfc356194bdf5a3853f083dae6badfc45b0 100644 (file)
@@ -940,6 +940,7 @@ static void DetectBufferTypeSetupDetectEngine(DetectEngineCtx *de_ctx)
     }
     de_ctx->buffer_type_id = g_buffer_type_id;
 
+    DetectMpmInitializeAppMpms(de_ctx);
     DetectAppLayerInspectEngineCopyListToDetectCtx(de_ctx);
 }
 
@@ -950,6 +951,19 @@ static void DetectBufferTypeFreeDetectEngine(DetectEngineCtx *de_ctx)
             SCFree(de_ctx->buffer_type_map);
         if (de_ctx->buffer_type_hash)
             HashListTableFree(de_ctx->buffer_type_hash);
+
+        DetectEngineAppInspectionEngine *ilist = de_ctx->app_inspect_engines;
+        while (ilist) {
+            DetectEngineAppInspectionEngine *next = ilist->next;
+            SCFree(ilist);
+            ilist = next;
+        }
+        DetectMpmAppLayerRegistery *mlist = de_ctx->app_mpms_list;
+        while (mlist) {
+            DetectMpmAppLayerRegistery *next = mlist->next;
+            SCFree(mlist);
+            mlist = next;
+        }
     }
 }
 
@@ -999,7 +1013,8 @@ int DetectBufferTypeGetByIdTransforms(DetectEngineCtx *de_ctx, const int id,
     map->mpm = base_map->mpm;
     map->SetupCallback = base_map->SetupCallback;
     map->ValidateCallback = base_map->ValidateCallback;
-    DetectAppLayerMpmRegisterByParentId(map->id, map->parent_id, &map->transforms);
+    DetectAppLayerMpmRegisterByParentId(de_ctx,
+            map->id, map->parent_id, &map->transforms);
 
     BUG_ON(HashListTableAdd(de_ctx->buffer_type_hash, (void *)map, 0) != 0);
     SCLogDebug("buffer %s registered with id %d, parent %d", map->string, map->id, map->parent_id);
index 457d7109853d787a242ec40406f7bb7241c33b3b..4bd680123b0c54dbb832c09d75dc622f314b9134 100644 (file)
@@ -861,6 +861,8 @@ typedef struct DetectEngineCtx_ {
     /* list with app inspect engines. Both the start-time registered ones and
      * the rule-time registered ones. */
     DetectEngineAppInspectionEngine *app_inspect_engines;
+    DetectMpmAppLayerRegistery *app_mpms_list;
+    uint32_t app_mpms_list_cnt;
 
     /** table with mpms and their registration function
      *  \todo we only need this at init, so perhaps this