]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/mpm: fix id confusion in mpm_ctx sharing
authorVictor Julien <victor@inliniac.net>
Tue, 27 Oct 2020 07:16:25 +0000 (08:16 +0100)
committerVictor Julien <victor@inliniac.net>
Wed, 4 Nov 2020 15:30:40 +0000 (16:30 +0100)
Mixing of dynamic id's and hardcoded config values could possibly
lead to the settings not getting applied properly.

src/detect.h
src/util-mpm.c
src/util-mpm.h

index 9e9547512c84ba8436a33a4f5f42703c5db8253c..c1fc715ca02e0d9c8a18f89199cd94bea5a53b81 100644 (file)
@@ -967,9 +967,10 @@ enum {
 
 /* Siggroup mpm context profile */
 enum {
-    ENGINE_SGH_MPM_FACTORY_CONTEXT_FULL,
+    ENGINE_SGH_MPM_FACTORY_CONTEXT_FULL = 0,
     ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE,
-    ENGINE_SGH_MPM_FACTORY_CONTEXT_AUTO
+    ENGINE_SGH_MPM_FACTORY_CONTEXT_AUTO,
+#define ENGINE_SGH_MPM_FACTORY_CONTEXT_START_ID_RANGE (ENGINE_SGH_MPM_FACTORY_CONTEXT_AUTO + 1)
 };
 
 typedef struct HttpReassembledBody_ {
index 61f3c1899f097d77a869e5df2cf06249722b4360..09aecee99fac2924118ef2d1433a91ea28fe1dbe 100644 (file)
@@ -67,6 +67,7 @@ int32_t MpmFactoryRegisterMpmCtxProfile(
             FatalError(SC_ERR_FATAL, "Error allocating memory");
         }
         memset(de_ctx->mpm_ctx_factory_container, 0, sizeof(MpmCtxFactoryContainer));
+        de_ctx->mpm_ctx_factory_container->max_id = ENGINE_SGH_MPM_FACTORY_CONTEXT_START_ID_RANGE;
 
         MpmCtxFactoryItem *item = SCMalloc(sizeof(MpmCtxFactoryItem));
         if (unlikely(item == NULL)) {
@@ -91,10 +92,7 @@ int32_t MpmFactoryRegisterMpmCtxProfile(
         }
         memset(item[0].mpm_ctx_tc, 0, sizeof(MpmCtx));
         item[0].mpm_ctx_tc->flags |= MPMCTX_FLAGS_GLOBAL;
-
-        /* our id starts from 0 always.  Helps us with the ctx retrieval from
-         * the array */
-        item[0].id = 0;
+        item[0].id = de_ctx->mpm_ctx_factory_container->max_id++;
 
         /* store the newly created item */
         de_ctx->mpm_ctx_factory_container->items = item;
@@ -161,7 +159,7 @@ int32_t MpmFactoryRegisterMpmCtxProfile(
         memset(new_item[0].mpm_ctx_tc, 0, sizeof(MpmCtx));
         new_item[0].mpm_ctx_tc->flags |= MPMCTX_FLAGS_GLOBAL;
 
-        new_item[0].id = de_ctx->mpm_ctx_factory_container->no_of_items;
+        new_item[0].id = de_ctx->mpm_ctx_factory_container->max_id++;
         de_ctx->mpm_ctx_factory_container->no_of_items++;
 
         /* the newly created id */
@@ -200,13 +198,17 @@ MpmCtx *MpmFactoryGetMpmCtxForProfile(const DetectEngineCtx *de_ctx, int32_t id,
     } else if (id < -1) {
         SCLogError(SC_ERR_INVALID_ARGUMENTS, "Invalid argument - %d\n", id);
         return NULL;
-    } else if (id >= de_ctx->mpm_ctx_factory_container->no_of_items) {
+    } else if (id >= de_ctx->mpm_ctx_factory_container->max_id) {
         /* this id does not exist */
         return NULL;
     } else {
-        return (direction == 0) ?
-            de_ctx->mpm_ctx_factory_container->items[id].mpm_ctx_ts :
-            de_ctx->mpm_ctx_factory_container->items[id].mpm_ctx_tc;
+        for (int i = 0; i < de_ctx->mpm_ctx_factory_container->no_of_items; i++) {
+            if (id == de_ctx->mpm_ctx_factory_container->items[i].id) {
+                return (direction == 0) ? de_ctx->mpm_ctx_factory_container->items[i].mpm_ctx_ts
+                                        : de_ctx->mpm_ctx_factory_container->items[i].mpm_ctx_tc;
+            }
+        }
+        return NULL;
     }
 }
 
index 0e5311293a7ff9fbe588a8eca2ab6c173287feb5..ac7d2f98ca15c456b1aacfcc8c38221fb56f962a 100644 (file)
@@ -123,6 +123,7 @@ typedef struct MpmCtxFactoryItem_ {
 typedef struct MpmCtxFactoryContainer_ {
     MpmCtxFactoryItem *items;
     int32_t no_of_items;
+    int32_t max_id;
 } MpmCtxFactoryContainer;
 
 /** pattern is case insensitive */