]> 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)
committerVictor Julien <vjulien@oisf.net>
Wed, 20 Apr 2022 21:15:45 +0000 (23:15 +0200)
And avoids memory leaks on it

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

index 59e22d10cbc0d4c368253f73c451671554dde919..7479759e59014d7ff0e519de7d3b43eb1e0cfdea 100644 (file)
@@ -24,6 +24,7 @@ use std;
 use std::mem::transmute;
 
 use crate::applayer::LoggerFlags;
+use crate::core;
 
 #[derive(Debug)]
 pub struct TFTPTransaction {
@@ -32,6 +33,7 @@ pub struct TFTPTransaction {
     pub mode : String,
     pub logged : LoggerFlags,
     id: u64,
+    pub de_state: Option<*mut core::DetectEngineState>,
 }
 
 pub struct TFTPState {
@@ -62,6 +64,7 @@ impl TFTPTransaction {
             mode : mode.to_lowercase(),
             logged : LoggerFlags::new(),
             id : 0,
+            de_state: None,
         }
     }
     pub fn is_mode_ok(&self) -> bool {
@@ -70,6 +73,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]
@@ -137,6 +155,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 fc4be55e059e7654c2e14f188d3c00e02a9ca7f3..0d664f586368ee1cb2fd40b1c7fb2e66d96d1fa4 100644 (file)
@@ -177,13 +177,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)
@@ -262,7 +262,6 @@ void RegisterTFTPParsers(void)
         AppLayerParserRegisterGetTx(IPPROTO_UDP, ALPROTO_TFTP,
                                     TFTPGetTx);
 
-        /* What is this being registered for? */
         AppLayerParserRegisterDetectStateFuncs(IPPROTO_UDP, ALPROTO_TFTP,
                                                TFTPGetTxDetectState,
                                                TFTPSetTxDetectState);