]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: mpm store frees mpm_ctx' it owns
authorVictor Julien <victor@inliniac.net>
Tue, 6 Oct 2015 13:04:33 +0000 (15:04 +0200)
committerVictor Julien <victor@inliniac.net>
Tue, 5 Apr 2016 07:07:10 +0000 (09:07 +0200)
src/detect-engine-mpm.c
src/detect-engine-mpm.h
src/detect-engine-siggroup.c
src/detect.c

index 6bed2e556ee0df01df77b295000d5ebc7bdd56c6..6dbdedc0d06b6b4732ad8ca78abcc49b8a7a5942 100644 (file)
@@ -895,252 +895,6 @@ void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matche
     MpmInitThreadCtx(mpm_thread_ctx, mpm_matcher);
 }
 
-
-/* free the pattern matcher part of a SigGroupHead */
-void PatternMatchDestroyGroup(SigGroupHead *sh)
-{
-    /* content */
-    if (!(sh->flags & SIG_GROUP_HEAD_MPM_COPY)) {
-        if (sh->mpm_proto_tcp_ctx_ts != NULL &&
-            !sh->mpm_proto_tcp_ctx_ts->global)
-        {
-            SCLogDebug("destroying mpm_ctx %p (sh %p)",
-                    sh->mpm_proto_tcp_ctx_ts, sh);
-            mpm_table[sh->mpm_proto_tcp_ctx_ts->mpm_type].
-                DestroyCtx(sh->mpm_proto_tcp_ctx_ts);
-            SCFree(sh->mpm_proto_tcp_ctx_ts);
-        }
-        /* ready for reuse */
-        sh->mpm_proto_tcp_ctx_ts = NULL;
-
-        if (sh->mpm_proto_tcp_ctx_tc != NULL &&
-            !sh->mpm_proto_tcp_ctx_tc->global)
-        {
-            SCLogDebug("destroying mpm_ctx %p (sh %p)",
-                    sh->mpm_proto_tcp_ctx_tc, sh);
-            mpm_table[sh->mpm_proto_tcp_ctx_tc->mpm_type].
-                DestroyCtx(sh->mpm_proto_tcp_ctx_tc);
-            SCFree(sh->mpm_proto_tcp_ctx_tc);
-        }
-        /* ready for reuse */
-        sh->mpm_proto_tcp_ctx_tc = NULL;
-
-        if (sh->mpm_proto_udp_ctx_ts != NULL &&
-            !sh->mpm_proto_udp_ctx_ts->global)
-        {
-            SCLogDebug("destroying mpm_ctx %p (sh %p)",
-                    sh->mpm_proto_udp_ctx_ts, sh);
-            mpm_table[sh->mpm_proto_udp_ctx_ts->mpm_type].
-                DestroyCtx(sh->mpm_proto_udp_ctx_ts);
-            SCFree(sh->mpm_proto_udp_ctx_ts);
-        }
-        /* ready for reuse */
-        sh->mpm_proto_udp_ctx_ts = NULL;
-
-        if (sh->mpm_proto_udp_ctx_tc != NULL &&
-                !sh->mpm_proto_udp_ctx_tc->global)
-        {
-            SCLogDebug("destroying mpm_ctx %p (sh %p)",
-                    sh->mpm_proto_udp_ctx_tc, sh);
-            mpm_table[sh->mpm_proto_udp_ctx_tc->mpm_type].
-                DestroyCtx(sh->mpm_proto_udp_ctx_tc);
-            SCFree(sh->mpm_proto_udp_ctx_tc);
-        }
-        /* ready for reuse */
-        sh->mpm_proto_udp_ctx_tc = NULL;
-
-        if (sh->mpm_proto_other_ctx != NULL &&
-                !sh->mpm_proto_other_ctx->global)
-        {
-            SCLogDebug("destroying mpm_ctx %p (sh %p)",
-                    sh->mpm_proto_other_ctx, sh);
-            mpm_table[sh->mpm_proto_other_ctx->mpm_type].
-                DestroyCtx(sh->mpm_proto_other_ctx);
-            SCFree(sh->mpm_proto_other_ctx);
-        }
-        /* ready for reuse */
-        sh->mpm_proto_other_ctx = NULL;
-    }
-
-    /* uricontent */
-    if ((sh->mpm_uri_ctx_ts != NULL) && !(sh->flags & SIG_GROUP_HEAD_MPM_URI_COPY)) {
-        if (sh->mpm_uri_ctx_ts != NULL) {
-            if (!sh->mpm_uri_ctx_ts->global) {
-                SCLogDebug("destroying mpm_uri_ctx %p (sh %p)", sh->mpm_uri_ctx_ts, sh);
-                mpm_table[sh->mpm_uri_ctx_ts->mpm_type].DestroyCtx(sh->mpm_uri_ctx_ts);
-                SCFree(sh->mpm_uri_ctx_ts);
-            }
-            /* ready for reuse */
-            sh->mpm_uri_ctx_ts = NULL;
-        }
-    }
-
-    /* stream content */
-    if ((sh->mpm_stream_ctx_ts != NULL || sh->mpm_stream_ctx_tc != NULL) &&
-        !(sh->flags & SIG_GROUP_HEAD_MPM_STREAM_COPY)) {
-        if (sh->mpm_stream_ctx_ts != NULL) {
-            if (!sh->mpm_stream_ctx_ts->global) {
-                SCLogDebug("destroying mpm_stream_ctx %p (sh %p)", sh->mpm_stream_ctx_ts, sh);
-                mpm_table[sh->mpm_stream_ctx_ts->mpm_type].DestroyCtx(sh->mpm_stream_ctx_ts);
-                SCFree(sh->mpm_stream_ctx_ts);
-            }
-            /* ready for reuse */
-            sh->mpm_stream_ctx_ts = NULL;
-        }
-        if (sh->mpm_stream_ctx_tc != NULL) {
-            if (!sh->mpm_stream_ctx_tc->global) {
-                SCLogDebug("destroying mpm_stream_ctx %p (sh %p)", sh->mpm_stream_ctx_tc, sh);
-                mpm_table[sh->mpm_stream_ctx_tc->mpm_type].DestroyCtx(sh->mpm_stream_ctx_tc);
-                SCFree(sh->mpm_stream_ctx_tc);
-            }
-            /* ready for reuse */
-            sh->mpm_stream_ctx_tc = NULL;
-        }
-    }
-
-    if (sh->mpm_hcbd_ctx_ts != NULL) {
-        if (sh->mpm_hcbd_ctx_ts != NULL) {
-            if (!sh->mpm_hcbd_ctx_ts->global) {
-                mpm_table[sh->mpm_hcbd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hcbd_ctx_ts);
-                SCFree(sh->mpm_hcbd_ctx_ts);
-            }
-            sh->mpm_hcbd_ctx_ts = NULL;
-        }
-    }
-
-    if (sh->mpm_hsbd_ctx_tc != NULL) {
-        if (sh->mpm_hsbd_ctx_tc != NULL) {
-            if (!sh->mpm_hsbd_ctx_tc->global) {
-                mpm_table[sh->mpm_hsbd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hsbd_ctx_tc);
-                SCFree(sh->mpm_hsbd_ctx_tc);
-            }
-            sh->mpm_hsbd_ctx_tc = NULL;
-        }
-    }
-
-    if (sh->mpm_smtp_filedata_ctx_ts != NULL) {
-        if (sh->mpm_smtp_filedata_ctx_ts != NULL) {
-            if (!sh->mpm_smtp_filedata_ctx_ts->global) {
-                mpm_table[sh->mpm_smtp_filedata_ctx_ts->mpm_type].DestroyCtx(sh->mpm_smtp_filedata_ctx_ts);
-                SCFree(sh->mpm_smtp_filedata_ctx_ts);
-            }
-            sh->mpm_smtp_filedata_ctx_ts = NULL;
-        }
-    }
-
-    if (sh->mpm_hhd_ctx_ts != NULL || sh->mpm_hhd_ctx_tc != NULL) {
-        if (sh->mpm_hhd_ctx_ts != NULL) {
-            if (!sh->mpm_hhd_ctx_ts->global) {
-                mpm_table[sh->mpm_hhd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hhd_ctx_ts);
-                SCFree(sh->mpm_hhd_ctx_ts);
-            }
-            sh->mpm_hhd_ctx_ts = NULL;
-        }
-        if (sh->mpm_hhd_ctx_tc != NULL) {
-            if (!sh->mpm_hhd_ctx_tc->global) {
-                mpm_table[sh->mpm_hhd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hhd_ctx_tc);
-                SCFree(sh->mpm_hhd_ctx_tc);
-            }
-            sh->mpm_hhd_ctx_tc = NULL;
-        }
-    }
-
-    if (sh->mpm_hrhd_ctx_ts != NULL || sh->mpm_hrhd_ctx_tc != NULL) {
-        if (sh->mpm_hrhd_ctx_ts != NULL) {
-            if (!sh->mpm_hrhd_ctx_ts->global) {
-                mpm_table[sh->mpm_hrhd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hrhd_ctx_ts);
-                SCFree(sh->mpm_hrhd_ctx_ts);
-            }
-            sh->mpm_hrhd_ctx_ts = NULL;
-        }
-        if (sh->mpm_hrhd_ctx_tc != NULL) {
-            if (!sh->mpm_hrhd_ctx_tc->global) {
-                mpm_table[sh->mpm_hrhd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hrhd_ctx_tc);
-                SCFree(sh->mpm_hrhd_ctx_tc);
-            }
-            sh->mpm_hrhd_ctx_tc = NULL;
-        }
-    }
-
-    if (sh->mpm_hmd_ctx_ts != NULL) {
-        if (sh->mpm_hmd_ctx_ts != NULL) {
-            if (!sh->mpm_hmd_ctx_ts->global) {
-                mpm_table[sh->mpm_hmd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hmd_ctx_ts);
-                SCFree(sh->mpm_hmd_ctx_ts);
-            }
-            sh->mpm_hmd_ctx_ts = NULL;
-        }
-    }
-
-    if (sh->mpm_hcd_ctx_ts != NULL || sh->mpm_hcd_ctx_tc != NULL) {
-        if (sh->mpm_hcd_ctx_ts != NULL) {
-            if (!sh->mpm_hcd_ctx_ts->global) {
-                mpm_table[sh->mpm_hcd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hcd_ctx_ts);
-                SCFree(sh->mpm_hcd_ctx_ts);
-            }
-            sh->mpm_hcd_ctx_ts = NULL;
-        }
-        if (sh->mpm_hcd_ctx_tc != NULL) {
-            if (!sh->mpm_hcd_ctx_tc->global) {
-                mpm_table[sh->mpm_hcd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hcd_ctx_tc);
-                SCFree(sh->mpm_hcd_ctx_tc);
-            }
-            sh->mpm_hcd_ctx_tc = NULL;
-        }
-    }
-
-    if (sh->mpm_hrud_ctx_ts != NULL) {
-        if (sh->mpm_hrud_ctx_ts != NULL) {
-            if (!sh->mpm_hrud_ctx_ts->global) {
-                mpm_table[sh->mpm_hrud_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hrud_ctx_ts);
-                SCFree(sh->mpm_hrud_ctx_ts);
-            }
-            sh->mpm_hrud_ctx_ts = NULL;
-        }
-    }
-
-    if (sh->mpm_hsmd_ctx_tc != NULL) {
-        if (sh->mpm_hsmd_ctx_tc != NULL) {
-            if (!sh->mpm_hsmd_ctx_tc->global) {
-                mpm_table[sh->mpm_hsmd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hsmd_ctx_tc);
-                SCFree(sh->mpm_hsmd_ctx_tc);
-            }
-            sh->mpm_hsmd_ctx_tc = NULL;
-        }
-    }
-
-    if (sh->mpm_hscd_ctx_tc != NULL) {
-        if (sh->mpm_hscd_ctx_tc != NULL) {
-            if (!sh->mpm_hscd_ctx_tc->global) {
-                mpm_table[sh->mpm_hscd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hscd_ctx_tc);
-                SCFree(sh->mpm_hscd_ctx_tc);
-            }
-            sh->mpm_hscd_ctx_tc = NULL;
-        }
-    }
-
-    if (sh->mpm_huad_ctx_ts != NULL) {
-        if (sh->mpm_huad_ctx_ts != NULL) {
-            if (!sh->mpm_huad_ctx_ts->global) {
-                mpm_table[sh->mpm_huad_ctx_ts->mpm_type].DestroyCtx(sh->mpm_huad_ctx_ts);
-                SCFree(sh->mpm_huad_ctx_ts);
-            }
-            sh->mpm_huad_ctx_ts = NULL;
-        }
-    }
-
-    /* dns query */
-    if (sh->mpm_dnsquery_ctx_ts != NULL) {
-        if (!sh->mpm_dnsquery_ctx_ts->global) {
-            mpm_table[sh->mpm_dnsquery_ctx_ts->mpm_type].DestroyCtx(sh->mpm_dnsquery_ctx_ts);
-            SCFree(sh->mpm_dnsquery_ctx_ts);
-        }
-        sh->mpm_dnsquery_ctx_ts = NULL;
-    }
-
-    return;
-}
-
 /** \brief Predict a strength value for patterns
  *
  *  Patterns with high character diversity score higher.
@@ -1408,6 +1162,23 @@ static char MpmStoreCompareFunc(void *data1, uint16_t len1, void *data2,
     return 1;
 }
 
+static void MpmStoreFreeFunc(void *ptr)
+{
+    MpmStore *ms = ptr;
+    if (ms != NULL) {
+        if (ms->mpm_ctx != NULL && !ms->mpm_ctx->global)
+        {
+            SCLogDebug("destroying mpm_ctx %p", ms->mpm_ctx);
+            mpm_table[ms->mpm_ctx->mpm_type].DestroyCtx(ms->mpm_ctx);
+            SCFree(ms->mpm_ctx);
+        }
+        ms->mpm_ctx = NULL;
+
+        SCFree(ms->sid_array);
+        SCFree(ms);
+    }
+}
+
 /**
  * \brief Initializes the MpmStore mpm hash table to be used by the detection
  *        engine context.
@@ -1422,7 +1193,7 @@ int MpmStoreInit(DetectEngineCtx *de_ctx)
     de_ctx->mpm_hash_table = HashListTableInit(4096,
                                                MpmStoreHashFunc,
                                                MpmStoreCompareFunc,
-                                               NULL);
+                                               MpmStoreFreeFunc);
     if (de_ctx->mpm_hash_table == NULL)
         goto error;
 
index 4cbc0fbe80686a2524322d1e442ca7f6cde25a86..9984791c26797c890bd4f867921ca5e086614609 100644 (file)
@@ -69,7 +69,6 @@ void PatternMatchThreadPrint(MpmThreadCtx *, uint16_t);
 
 int PatternMatchPrepareGroup(DetectEngineCtx *, SigGroupHead *);
 void DetectEngineThreadCtxInfo(ThreadVars *, DetectEngineThreadCtx *);
-void PatternMatchDestroyGroup(SigGroupHead *);
 
 TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **);
 TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *);
index 9d04697cc93773a01640316a7fd893928abf0b50..1ec12111e7503a9d8a4daad759213bd2f4b935a7 100644 (file)
@@ -173,8 +173,6 @@ void SigGroupHeadFree(SigGroupHead *sgh)
 
     SCLogDebug("sgh %p", sgh);
 
-    PatternMatchDestroyGroup(sgh);
-
     if (sgh->match_array != NULL) {
         detect_siggroup_matcharray_free_cnt++;
         detect_siggroup_matcharray_memory -= (sgh->sig_cnt * sizeof(Signature *));
index 23df4b30eb2b83ba532f06ff0ae159abbd8774ca..4a63c4d39f05b82c3089d4837212c9f16c663532 100644 (file)
@@ -3526,7 +3526,6 @@ int SigAddressPrepareStage4(DetectEngineCtx *de_ctx)
      * after the initialization phase. */
     SigGroupHeadHashFree(de_ctx);
     SigGroupHeadDPortHashFree(de_ctx);
-    MpmStoreFree(de_ctx);
 
     SCFree(de_ctx->sgh_array);
     de_ctx->sgh_array_cnt = 0;