]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: fail properly on invalid transform pcrexform
authorPhilippe Antoine <contact@catenacyber.fr>
Fri, 19 Jun 2020 07:34:46 +0000 (09:34 +0200)
committerVictor Julien <victor@inliniac.net>
Mon, 6 Jul 2020 09:45:37 +0000 (11:45 +0200)
src/detect-parse.c
src/detect-parse.h
src/detect-pcre.c
src/detect-transform-pcrexform.c
src/tests/detect-transform-pcrexform.c [new file with mode: 0644]

index 1fe4eefb9a92298de05ac2d6130373b7056db320..673a8196ef30775d4047d6042480597b0223fa04 100644 (file)
@@ -2431,32 +2431,35 @@ void DetectParseRegexAddToFreeList(DetectParseRegex *detect_parse)
     g_detect_parse_regex_list = r;
 }
 
-void DetectSetupParseRegexesOpts(const char *parse_str, DetectParseRegex *detect_parse, int opts)
+bool DetectSetupParseRegexesOpts(const char *parse_str, DetectParseRegex *detect_parse, int opts)
 {
     const char *eb;
     int eo;
 
     detect_parse->regex = pcre_compile(parse_str, opts, &eb, &eo, NULL);
     if (detect_parse->regex == NULL) {
-        FatalError(SC_ERR_PCRE_COMPILE, "pcre compile of \"%s\" failed at "
+        SCLogError(SC_ERR_PCRE_COMPILE, "pcre compile of \"%s\" failed at "
                 "offset %" PRId32 ": %s", parse_str, eo, eb);
+        return false;
     }
 
     detect_parse->study = pcre_study(detect_parse->regex, 0 , &eb);
     if (eb != NULL) {
-        FatalError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
+        SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
+        return false;
     }
 
 
     DetectParseRegexAddToFreeList(detect_parse);
 
-    return;
+    return true;
 }
 
 void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
 {
-    DetectSetupParseRegexesOpts(parse_str, detect_parse, 0);
-    return;
+    if (!DetectSetupParseRegexesOpts(parse_str, detect_parse, 0)) {
+        FatalError(SC_ERR_PCRE_COMPILE, "pcre compile and study failed");
+    }
 }
 
 
index be6a6a16afce5f14f9882457f97ba5fd4b9951c4..5a2f902d607e146997d3ed400a536de6e4578b18 100644 (file)
@@ -88,7 +88,7 @@ int WARN_UNUSED DetectSignatureSetAppProto(Signature *s, AppProto alproto);
 
 /* parse regex setup and free util funcs */
 
-void DetectSetupParseRegexesOpts(const char *parse_str, DetectParseRegex *parse_regex, int opts);
+bool DetectSetupParseRegexesOpts(const char *parse_str, DetectParseRegex *parse_regex, int opts);
 void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *parse_regex);
 void DetectParseRegexAddToFreeList(DetectParseRegex *parse_regex);
 void DetectParseFreeRegexes(void);
index cc43645775df3c8b752f20549e89a94034de2525..0668814b809475acc40f396209527cfceacab725 100644 (file)
@@ -154,8 +154,10 @@ void DetectPcreRegister (void)
     DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
 
     /* setup the capture regex, as it needs PCRE_UNGREEDY we do it manually */
-    int opts = PCRE_UNGREEDY; /* pkt_http_ua should be pkt, http_ua, for this reason the UNGREEDY */
-    DetectSetupParseRegexesOpts(PARSE_CAPTURE_REGEX, &parse_capture_regex, opts);
+    /* pkt_http_ua should be pkt, http_ua, for this reason the UNGREEDY */
+    if (!DetectSetupParseRegexesOpts(PARSE_CAPTURE_REGEX, &parse_capture_regex, PCRE_UNGREEDY)) {
+        FatalError(SC_ERR_PCRE_COMPILE, "pcre compile and study failed");
+    }
 
 #ifdef PCRE_HAVE_JIT
     if (PageSupportsRWX() == 0) {
index a489232d7b83516e83305b4cf9fcccd2ff49180e..54da1b5412b068830bf3b66fc49a23f7c37353fb 100644 (file)
@@ -35,6 +35,9 @@ typedef DetectParseRegex DetectTransformPcrexformData;
 static int DetectTransformPcrexformSetup (DetectEngineCtx *, Signature *, const char *);
 static void DetectTransformPcrexformFree(DetectEngineCtx *, void *);
 static void DetectTransformPcrexform(InspectionBuffer *buffer, void *options);
+#ifdef UNITTESTS
+void DetectTransformPcrexformRegisterTests (void);
+#endif
 
 void DetectTransformPcrexformRegister(void)
 {
@@ -48,6 +51,9 @@ void DetectTransformPcrexformRegister(void)
         DetectTransformPcrexformFree;
     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].Setup =
         DetectTransformPcrexformSetup;
+#ifdef UNITTESTS
+    sigmatch_table[DETECT_TRANSFORM_PCREXFORM].RegisterTests = DetectTransformPcrexformRegisterTests;
+#endif
     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].flags |= SIGMATCH_QUOTES_MANDATORY;
 }
 
@@ -78,7 +84,10 @@ static int DetectTransformPcrexformSetup (DetectEngineCtx *de_ctx, Signature *s,
         SCReturnInt(-1);
     }
 
-    DetectSetupParseRegexes(regexstr, pxd);
+    if (!DetectSetupParseRegexesOpts(regexstr, pxd, 0)) {
+        SCFree(pxd);
+        SCReturnInt(-1);
+    }
 
     int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_PCREXFORM, pxd);
     if (r != 0) {
@@ -108,3 +117,7 @@ static void DetectTransformPcrexform(InspectionBuffer *buffer, void *options)
         }
     }
 }
+
+#ifdef UNITTESTS
+#include "tests/detect-transform-pcrexform.c"
+#endif
diff --git a/src/tests/detect-transform-pcrexform.c b/src/tests/detect-transform-pcrexform.c
new file mode 100644 (file)
index 0000000..a9b3962
--- /dev/null
@@ -0,0 +1,67 @@
+/* Copyright (C) 2020 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "../suricata-common.h"
+
+#include "../detect-engine.h"
+
+#include "../detect-transform-pcrexform.h"
+
+#include "../util-unittest.h"
+
+/**
+ * \test signature with an invalid pcrexform value.
+ */
+
+static int DetectTransformPcrexformParseTest01 (void)
+{
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    FAIL_IF_NULL(de_ctx);
+
+    Signature *sig = DetectEngineAppendSig(de_ctx,
+            "alert tcp any any <> any 1 pcrexform:\"[\";");
+    FAIL_IF_NOT_NULL(sig);
+
+    DetectEngineCtxFree(de_ctx);
+    PASS;
+}
+
+/**
+ * \test signature with a valid pcrexform value.
+ */
+
+static int DetectTransformPcrexformParseTest02 (void)
+{
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    FAIL_IF_NULL(de_ctx);
+
+    Signature *sig = DetectEngineAppendSig(de_ctx,
+            "alert http any any -> any any (msg:\"HTTP with pcrexform\"; http.request_line; pcrexform:\"[a-zA-Z]+\\s+(.*)\\s+HTTP\"; content:\"/z4d4kWk.jpg\"; sid:1;)");
+    FAIL_IF_NULL(sig);
+
+    DetectEngineCtxFree(de_ctx);
+    PASS;
+}
+
+/**
+ * \brief this function registers unit tests for DetectTransformPcrexform
+ */
+void DetectTransformPcrexformRegisterTests(void)
+{
+    UtRegisterTest("DetectTransformPcrexformParseTest01", DetectTransformPcrexformParseTest01);
+    UtRegisterTest("DetectTransformPcrexformParseTest02", DetectTransformPcrexformParseTest02);
+}