From: Philippe Antoine Date: Mon, 22 Nov 2021 09:37:33 +0000 (+0100) Subject: tftp: use destate X-Git-Tag: suricata-5.0.9~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f418dbfecb346f0612638b703fb116fd4b08f390;p=thirdparty%2Fsuricata.git tftp: use destate And avoids memory leaks on it --- diff --git a/rust/src/tftp/tftp.rs b/rust/src/tftp/tftp.rs index 59e22d10cb..7479759e59 100644 --- a/rust/src/tftp/tftp.rs +++ b/rust/src/tftp/tftp.rs @@ -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, ) ); +#[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, diff --git a/src/app-layer-tftp.c b/src/app-layer-tftp.c index fc4be55e05..0d664f5863 100644 --- a/src/app-layer-tftp.c +++ b/src/app-layer-tftp.c @@ -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);