]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
pcap: free per thread resources
authorVictor Julien <vjulien@oisf.net>
Thu, 18 May 2023 06:03:31 +0000 (08:03 +0200)
committerVictor Julien <vjulien@oisf.net>
Tue, 27 Jun 2023 14:21:59 +0000 (16:21 +0200)
Bug: #4750.
(cherry picked from commit 3049151bc2ec593b8d028a6aae675019b3bf4032)

src/source-pcap.c

index b13fea57e3b01e2e8b095b41f2091046f2206bd0..b68b49c232748556ea244559e9dcb9047c23b6c6 100644 (file)
@@ -33,6 +33,7 @@
 #include "tm-threads.h"
 #include "source-pcap.h"
 #include "conf.h"
+#include "util-bpf.h"
 #include "util-debug.h"
 #include "util-error.h"
 #include "util-privs.h"
@@ -108,6 +109,7 @@ typedef struct PcapThreadVars_
 } PcapThreadVars;
 
 static TmEcode ReceivePcapThreadInit(ThreadVars *, const void *, void **);
+static TmEcode ReceivePcapThreadDeinit(ThreadVars *tv, void *data);
 static void ReceivePcapThreadExitStats(ThreadVars *, void *);
 static TmEcode ReceivePcapLoop(ThreadVars *tv, void *data, void *slot);
 static TmEcode ReceivePcapBreakLoop(ThreadVars *tv, void *data);
@@ -131,6 +133,7 @@ void TmModuleReceivePcapRegister (void)
 {
     tmm_modules[TMM_RECEIVEPCAP].name = "ReceivePcap";
     tmm_modules[TMM_RECEIVEPCAP].ThreadInit = ReceivePcapThreadInit;
+    tmm_modules[TMM_RECEIVEPCAP].ThreadDeinit = ReceivePcapThreadDeinit;
     tmm_modules[TMM_RECEIVEPCAP].PktAcqLoop = ReceivePcapLoop;
     tmm_modules[TMM_RECEIVEPCAP].PktAcqBreakLoop = ReceivePcapBreakLoop;
     tmm_modules[TMM_RECEIVEPCAP].ThreadExitPrintStats = ReceivePcapThreadExitStats;
@@ -404,7 +407,7 @@ static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void
     ptv->livedev = LiveGetDevice(pcapconfig->iface);
     if (ptv->livedev == NULL) {
         SCLogError(SC_ERR_INVALID_VALUE, "unable to find Live device");
-        SCFree(ptv);
+        ReceivePcapThreadDeinit(tv, ptv);
         SCReturnInt(TM_ECODE_FAILED);
     }
     SCLogInfo("using interface %s", (char *)pcapconfig->iface);
@@ -433,7 +436,7 @@ static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void
                     "pcap handler for %s",
                     (char *)pcapconfig->iface);
         }
-        SCFree(ptv);
+        ReceivePcapThreadDeinit(tv, ptv);
         pcapconfig->DerefFunc(pcapconfig);
         SCReturnInt(TM_ECODE_FAILED);
     }
@@ -450,7 +453,7 @@ static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void
         if (pcap_set_snaplen_r != 0) {
             SCLogError(SC_ERR_PCAP_SET_SNAPLEN, "could not set snaplen, "
                     "error: %s", pcap_geterr(ptv->pcap_handle));
-            SCFree(ptv);
+            ReceivePcapThreadDeinit(tv, ptv);
             pcapconfig->DerefFunc(pcapconfig);
             SCReturnInt(TM_ECODE_FAILED);
         }
@@ -463,7 +466,7 @@ static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void
     if (pcap_set_promisc_r != 0) {
         SCLogError(SC_ERR_PCAP_SET_PROMISC, "could not set promisc mode, "
                 "error %s", pcap_geterr(ptv->pcap_handle));
-        SCFree(ptv);
+        ReceivePcapThreadDeinit(tv, ptv);
         pcapconfig->DerefFunc(pcapconfig);
         SCReturnInt(TM_ECODE_FAILED);
     }
@@ -472,7 +475,7 @@ static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void
     if (pcap_set_timeout_r != 0) {
         SCLogError(SC_ERR_PCAP_SET_TIMEOUT, "could not set timeout, "
                 "error %s", pcap_geterr(ptv->pcap_handle));
-        SCFree(ptv);
+        ReceivePcapThreadDeinit(tv, ptv);
         pcapconfig->DerefFunc(pcapconfig);
         SCReturnInt(TM_ECODE_FAILED);
     }
@@ -487,7 +490,7 @@ static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void
         if (pcap_set_buffer_size_r != 0) {
             SCLogError(SC_ERR_PCAP_SET_BUFF_SIZE, "could not set "
                     "pcap buffer size, error %s", pcap_geterr(ptv->pcap_handle));
-            SCFree(ptv);
+            ReceivePcapThreadDeinit(tv, ptv);
             pcapconfig->DerefFunc(pcapconfig);
             SCReturnInt(TM_ECODE_FAILED);
         }
@@ -499,7 +502,7 @@ static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void
     if (pcap_activate_r != 0) {
         SCLogError(SC_ERR_PCAP_ACTIVATE_HANDLE, "could not activate the "
                 "pcap handler, error %s", pcap_geterr(ptv->pcap_handle));
-        SCFree(ptv);
+        ReceivePcapThreadDeinit(tv, ptv);
         pcapconfig->DerefFunc(pcapconfig);
         SCReturnInt(TM_ECODE_FAILED);
     }
@@ -518,7 +521,7 @@ static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void
                     pcap_geterr(ptv->pcap_handle));
 
             SCMutexUnlock(&pcap_bpf_compile_lock);
-            SCFree(ptv);
+            ReceivePcapThreadDeinit(tv, ptv);
             pcapconfig->DerefFunc(pcapconfig);
             return TM_ECODE_FAILED;
         }
@@ -528,7 +531,7 @@ static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void
                     pcap_geterr(ptv->pcap_handle));
 
             SCMutexUnlock(&pcap_bpf_compile_lock);
-            SCFree(ptv);
+            ReceivePcapThreadDeinit(tv, ptv);
             pcapconfig->DerefFunc(pcapconfig);
             return TM_ECODE_FAILED;
         }
@@ -597,6 +600,22 @@ static void ReceivePcapThreadExitStats(ThreadVars *tv, void *data)
     }
 }
 
+static TmEcode ReceivePcapThreadDeinit(ThreadVars *tv, void *data)
+{
+    SCEnter();
+    PcapThreadVars *ptv = (PcapThreadVars *)data;
+    if (ptv != NULL) {
+        if (ptv->pcap_handle != NULL) {
+            pcap_close(ptv->pcap_handle);
+        }
+        if (ptv->filter.bf_insns) {
+            SCBPFFree(&ptv->filter);
+        }
+        SCFree(ptv);
+    }
+    SCReturnInt(TM_ECODE_OK);
+}
+
 /**
  * \brief This function passes off to link type decoders.
  *