]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
tftp: use destate
authorPhilippe Antoine <contact@catenacyber.fr>
Mon, 22 Nov 2021 09:37:33 +0000 (10:37 +0100)
committerShivani Bhardwaj <shivanib134@gmail.com>
Thu, 21 Apr 2022 07:31:48 +0000 (13:01 +0530)
And avoids memory leaks on it

Ticket #4848

rust/src/tftp/tftp.rs
src/app-layer-tftp.c

index 53c7fd67c02425baa161aff133da4ad46b91a267..c8bb0ef9d002397fae9963553d1b187f110e57d5 100644 (file)
@@ -23,6 +23,8 @@ use std::str;
 use std;
 use std::mem::transmute;
 
+use crate::core;
+
 use crate::applayer::AppLayerTxData;
 
 #[derive(Debug)]
@@ -32,6 +34,7 @@ pub struct TFTPTransaction {
     pub mode : String,
     id: u64,
     tx_data: AppLayerTxData,
+    pub de_state: Option<*mut core::DetectEngineState>,
 }
 
 pub struct TFTPState {
@@ -62,6 +65,7 @@ impl TFTPTransaction {
             mode : mode.to_lowercase(),
             id : 0,
             tx_data: AppLayerTxData::new(),
+            de_state: None,
         }
     }
     pub fn is_mode_ok(&self) -> bool {
@@ -70,6 +74,21 @@ impl TFTPTransaction {
             _ => false
         }
     }
+
+    pub fn free(&mut self) {
+        match self.de_state {
+            Some(state) => {
+                core::sc_detect_engine_state_free(state);
+            }
+            None => { },
+        }
+    }
+}
+
+impl Drop for TFTPTransaction {
+    fn drop(&mut self) {
+        self.free();
+    }
 }
 
 #[no_mangle]
@@ -123,6 +142,31 @@ named!(pub tftp_request<TFTPTransaction>,
     )
 );
 
+#[no_mangle]
+pub extern "C" fn rs_tftp_state_set_tx_detect_state(
+    tx: *mut std::os::raw::c_void,
+    de_state: &mut core::DetectEngineState) -> std::os::raw::c_int
+{
+    let tx = cast_pointer!(tx, TFTPTransaction);
+    tx.de_state = Some(de_state);
+    return 0;
+}
+
+#[no_mangle]
+pub extern "C" fn rs_tftp_state_get_tx_detect_state(
+    tx: *mut std::os::raw::c_void)
+    -> *mut core::DetectEngineState
+{
+    let tx = cast_pointer!(tx, TFTPTransaction);
+    match tx.de_state {
+        Some(ds) => {
+            return ds;
+        },
+        None => {
+            return std::ptr::null_mut();
+        }
+    }
+}
 
 #[no_mangle]
 pub extern "C" fn rs_tftp_request(state: &mut TFTPState,
index f41a059a36a02f1e9e02cb800718dffd922d8da2..1484fbad93a143c1bb3e94c2f0cf73d0e776cb80 100644 (file)
@@ -170,13 +170,13 @@ static int TFTPGetStateProgress(void *tx, uint8_t direction)
 
 static DetectEngineState *TFTPGetTxDetectState(void *vtx)
 {
-    return NULL;
+    return rs_tftp_state_get_tx_detect_state(vtx);
 }
 
 static int TFTPSetTxDetectState(void *vtx,
     DetectEngineState *s)
 {
-    return 0;
+    return rs_tftp_state_set_tx_detect_state(vtx, s);
 }
 
 void RegisterTFTPParsers(void)
@@ -252,7 +252,6 @@ void RegisterTFTPParsers(void)
         AppLayerParserRegisterGetTx(IPPROTO_UDP, ALPROTO_TFTP,
                                     TFTPGetTx);
 
-        /* What is this being registered for? */
         AppLayerParserRegisterDetectStateFuncs(IPPROTO_UDP, ALPROTO_TFTP,
                                                TFTPGetTxDetectState,
                                                TFTPSetTxDetectState);