]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
pcre: use thread-storage for matches
authorPhilippe Antoine <contact@catenacyber.fr>
Thu, 30 Sep 2021 13:13:13 +0000 (15:13 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 1 Oct 2021 06:00:51 +0000 (08:00 +0200)
src/detect-pcre.c
src/detect-pcre.h

index 740651afb7dd3d36561a8a40e8ad4c6930eeddfb..bb70a70cd5e19cbe3dbe79b223299daceab345f1 100644 (file)
@@ -200,7 +200,9 @@ int DetectPcrePayloadMatch(DetectEngineThreadCtx *det_ctx, const Signature *s,
     }
 
     /* run the actual pcre detection */
-    pcre2_match_data *match = pcre2_match_data_create_from_pattern(pe->parse_regex.regex, NULL);
+    pcre2_match_data *match =
+            (pcre2_match_data *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, pe->thread_ctx_id);
+
     ret = DetectPcreExec(det_ctx, pe, (char *)ptr, len, start_offset, 0, match);
     SCLogDebug("ret %d (negating %s)", ret, (pe->flags & DETECT_PCRE_NEGATE) ? "set" : "not set");
 
@@ -303,7 +305,6 @@ int DetectPcrePayloadMatch(DetectEngineThreadCtx *det_ctx, const Signature *s,
         SCLogDebug("pcre had matching error");
         ret = 0;
     }
-    pcre2_match_data_free(match);
     SCReturnInt(ret);
 }
 
@@ -830,6 +831,21 @@ error:
     return -1;
 }
 
+static void *DetectPcreThreadInit(void *data)
+{
+    DetectPcreData *pd = (DetectPcreData *)data;
+    pcre2_match_data *match = pcre2_match_data_create_from_pattern(pd->parse_regex.regex, NULL);
+    return match;
+}
+
+static void DetectPcreThreadFree(void *ctx)
+{
+    if (ctx != NULL) {
+        pcre2_match_data *match = (pcre2_match_data *)ctx;
+        pcre2_match_data_free(match);
+    }
+}
+
 static int DetectPcreSetup (DetectEngineCtx *de_ctx, Signature *s, const char *regexstr)
 {
     SCEnter();
@@ -847,6 +863,11 @@ static int DetectPcreSetup (DetectEngineCtx *de_ctx, Signature *s, const char *r
     if (DetectPcreParseCapture(regexstr, de_ctx, pd, capture_names) < 0)
         goto error;
 
+    pd->thread_ctx_id = DetectRegisterThreadCtxFuncs(
+            de_ctx, "pcre", DetectPcreThreadInit, (void *)pd, DetectPcreThreadFree, 0);
+    if (pd->thread_ctx_id == -1)
+        goto error;
+
     int sm_list = -1;
     if (s->init_data->list != DETECT_SM_LIST_NOTSET) {
         if (parsed_sm_list != DETECT_SM_LIST_NOTSET && parsed_sm_list != s->init_data->list) {
@@ -934,6 +955,8 @@ static void DetectPcreFree(DetectEngineCtx *de_ctx, void *ptr)
 
     DetectPcreData *pd = (DetectPcreData *)ptr;
     DetectParseFreeRegex(&pd->parse_regex);
+    DetectUnregisterThreadCtxFuncs(de_ctx, NULL, pd, "pcre");
+
     SCFree(pd);
 
     return;
index 1c284664054b4a7800e672b076bd9814e0c41f14..a7d33e7ffb223a92ff241a8eb31edce8cdcdf4e1 100644 (file)
@@ -45,6 +45,7 @@ typedef struct DetectPcreData_ {
     uint8_t idx;
     uint8_t captypes[DETECT_PCRE_CAPTURE_MAX];
     uint32_t capids[DETECT_PCRE_CAPTURE_MAX];
+    int thread_ctx_id;
 } DetectPcreData;
 
 /* prototypes */