]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/ssl_state: Fix memory leaks from pcre_get_substring
authorJeff Lucovsky <jeff@lucovsky.org>
Sat, 22 Feb 2020 18:24:13 +0000 (13:24 -0500)
committerVictor Julien <victor@inliniac.net>
Mon, 24 Feb 2020 10:05:46 +0000 (11:05 +0100)
This commit replaces usages of pcre_get_substring with
pcre_copy_substring to avoid leaking memory on error conditions.

src/detect-ssl-state.c

index 01a1cac6e2e0d699bc1ce49b38587d468baf6fcb..134d9150c0bc83a05fdd4c11bd7827a424acd369 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2016 Open Information Security Foundation
+/* Copyright (C) 2007-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
@@ -165,8 +165,8 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
     int ret = 0, res = 0;
     int ov1[MAX_SUBSTRINGS];
     int ov2[MAX_SUBSTRINGS];
-    const char *str1;
-    const char *str2;
+    char str1[64];
+    char str2[64];
     int negate = 0;
     uint32_t flags = 0, mask = 0;
     DetectSslStateData *ssd = NULL;
@@ -179,17 +179,16 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
         goto error;
     }
 
-    res = pcre_get_substring((char *)arg, ov1, MAX_SUBSTRINGS, 1, &str1);
+    res = pcre_copy_substring((char *)arg, ov1, MAX_SUBSTRINGS, 1, str1, sizeof(str1));
     if (res < 0) {
-        SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
+        SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre_copy_substring failed");
         goto error;
     }
     negate = !strcmp("!", str1);
-    pcre_free_substring(str1);
 
-    res = pcre_get_substring((char *)arg, ov1, MAX_SUBSTRINGS, 2, &str1);
+    res = pcre_copy_substring((char *)arg, ov1, MAX_SUBSTRINGS, 2, str1, sizeof(str1));
     if (res < 0) {
-        SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
+        SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre_copy_substring failed");
         goto error;
     }
 
@@ -219,11 +218,9 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
         goto error;
     }
 
-    pcre_free_substring(str1);
-
-    res = pcre_get_substring((char *)arg, ov1, MAX_SUBSTRINGS, 3, &str1);
+    res = pcre_copy_substring((char *)arg, ov1, MAX_SUBSTRINGS, 3, str1, sizeof(str1));
     if (res < 0) {
-        SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
+        SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre_copy_substring failed");
         goto error;
     }
     while (res > 0) {
@@ -235,17 +232,16 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
             goto error;
         }
 
-        res = pcre_get_substring((char *)str1, ov2, MAX_SUBSTRINGS, 1, &str2);
+        res = pcre_copy_substring((char *)str1, ov2, MAX_SUBSTRINGS, 1, str2, sizeof(str2));
         if (res < 0) {
-            SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
+            SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre_copy_substring failed");
             goto error;
         }
         negate = !strcmp("!", str2);
-        pcre_free_substring(str2);
 
-        res = pcre_get_substring((char *)str1, ov2, MAX_SUBSTRINGS, 2, &str2);
+        res = pcre_copy_substring((char *)str1, ov2, MAX_SUBSTRINGS, 2, str2, sizeof(str2));
         if (res <= 0) {
-            SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
+            SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre_copy_substring failed");
             goto error;
         }
         if (strcmp("client_hello", str2) == 0) {
@@ -274,16 +270,14 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
             goto error;
         }
 
-        res = pcre_get_substring((char *)str1, ov2, MAX_SUBSTRINGS, 3, &str2);
+        res = pcre_copy_substring((char *)str1, ov2, MAX_SUBSTRINGS, 3, str2, sizeof(str2));
         if (res < 0) {
-            SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
+            SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre_copy_substring failed");
             goto error;
         }
 
-        pcre_free_substring(str1);
-        str1 = str2;
+        memcpy(str1, str2, sizeof(str1));
     }
-    pcre_free_substring(str1);
 
     if ( (ssd = SCMalloc(sizeof(DetectSslStateData))) == NULL) {
         goto error;