From 149c42bcdab91627ded2d75d2676c342858a4635 Mon Sep 17 00:00:00 2001 From: QianKai Lin Date: Wed, 9 Oct 2024 09:26:36 +0800 Subject: [PATCH] mysql: fix bug --- etc/schema.json | 2 +- rust/src/mysql/detect.rs | 10 ++--- rust/src/mysql/logger.rs | 2 +- rust/src/mysql/mod.rs | 2 +- rust/src/mysql/mysql.rs | 83 +++++++++++++++++++++++----------------- rust/src/mysql/parser.rs | 2 +- 6 files changed, 56 insertions(+), 45 deletions(-) diff --git a/etc/schema.json b/etc/schema.json index 0a382c07a47c..2f98332761fa 100644 --- a/etc/schema.json +++ b/etc/schema.json @@ -2396,7 +2396,7 @@ }, "command": { "type": "string", - "description": "sql query statement or some utility commands like ping, quit etc." + "description": "sql query statement or some utility commands like ping." }, "affected_rows": { "type": "integer" diff --git a/rust/src/mysql/detect.rs b/rust/src/mysql/detect.rs index 3cdb7418e4c9..7765d1949238 100644 --- a/rust/src/mysql/detect.rs +++ b/rust/src/mysql/detect.rs @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Open Information Security Foundation +/* Copyright (C) 2024 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 @@ -115,11 +115,9 @@ pub unsafe extern "C" fn SCMysqlGetRowsData( if !rows.is_empty() { let index = local_id as usize; if let Some(row) = rows.get(index) { - if !row.is_empty() { - *buf = row.as_ptr(); - *len = row.len() as u32; - return true; - } + *buf = row.as_ptr(); + *len = row.len() as u32; + return true; } } } diff --git a/rust/src/mysql/logger.rs b/rust/src/mysql/logger.rs index 88d7778c4671..38cd85ebb461 100644 --- a/rust/src/mysql/logger.rs +++ b/rust/src/mysql/logger.rs @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Open Information Security Foundation +/* Copyright (C) 2024 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 diff --git a/rust/src/mysql/mod.rs b/rust/src/mysql/mod.rs index 6f4fd0b5972c..4bc3005880c4 100644 --- a/rust/src/mysql/mod.rs +++ b/rust/src/mysql/mod.rs @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Open Information Security Foundation +/* Copyright (C) 2024 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 diff --git a/rust/src/mysql/mysql.rs b/rust/src/mysql/mysql.rs index 65cfb77d9afe..699dc9bbdd41 100644 --- a/rust/src/mysql/mysql.rs +++ b/rust/src/mysql/mysql.rs @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Open Information Security Foundation +/* Copyright (C) 2024 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 @@ -326,6 +326,9 @@ impl MysqlState { } self.tx_index_completed = index; } + if tx.tls { + tx.complete = true; + } tx } @@ -335,8 +338,9 @@ impl MysqlState { /// If we can't find a transaction and we should not create one, we return None /// The moment when this is called will may impact the logic of transaction tracking (e.g. when a tx is considered completed) fn create_tx(&mut self, command: String) -> Option<&mut MysqlTransaction> { - let _tx = self.new_tx(command); + let tx = self.new_tx(command); SCLogDebug!("create state is {:?}", &self.state_progress); + self.transactions.push_back(tx); self.transactions.back_mut() } @@ -362,6 +366,7 @@ impl MysqlState { AppLayerRequestProtocolTLSUpgrade(f); } self.tls = true; + self.create_tx("".to_string()); Some(MysqlStateProgress::Finished) } MysqlFEMessage::AuthRequest => None, @@ -738,23 +743,27 @@ impl MysqlState { rows, } => { let tx = self.get_tx_mut(self.tx_id - 1); - let mut rows = rows.into_iter().map(|row| row.texts.join(",")).collect(); - if let Some(tx) = tx { - if eof.status_flags != 0x0A { - tx.rows = Some(rows); - Some(MysqlStateProgress::CommandResponseReceived) - } else { - // MultiStatement - if let Some(state_rows) = tx.rows.as_mut() { - state_rows.append(&mut rows); - } else { + if !rows.is_empty() { + let mut rows = rows.into_iter().map(|row| row.texts.join(",")).collect(); + if let Some(tx) = tx { + if eof.status_flags != 0x0A { tx.rows = Some(rows); - } + Some(MysqlStateProgress::CommandResponseReceived) + } else { + // MultiStatement + if let Some(state_rows) = tx.rows.as_mut() { + state_rows.append(&mut rows); + } else { + tx.rows = Some(rows); + } - Some(MysqlStateProgress::TextResulsetContinue) + Some(MysqlStateProgress::TextResulsetContinue) + } + } else { + Some(MysqlStateProgress::Finished) } } else { - Some(MysqlStateProgress::Finished) + Some(MysqlStateProgress::CommandResponseReceived) } } MysqlResponsePacket::StmtPrepare { @@ -787,28 +796,32 @@ impl MysqlState { return Some(MysqlStateProgress::StmtFetchResponseContinue); } - if eof.status_flags != 0x0A { - let tx = self.get_tx_mut(self.tx_id - 1); - if let Some(tx) = tx { - tx.rows = Some( - rows.into_iter() - .map(|row| match row { - MysqlResultBinarySetRow::Err => String::new(), - MysqlResultBinarySetRow::Text(text) => text, - }) - .collect::>(), - ); - tx.complete = true; - } + if !rows.is_empty() { + if eof.status_flags != 0x0A { + let tx = self.get_tx_mut(self.tx_id - 1); + if let Some(tx) = tx { + tx.rows = Some( + rows.into_iter() + .map(|row| match row { + MysqlResultBinarySetRow::Err => String::new(), + MysqlResultBinarySetRow::Text(text) => text, + }) + .collect::>(), + ); + tx.complete = true; + } - Some(MysqlStateProgress::StmtExecResponseReceived) - } else { - // MultiResulset - if let Some(prepare_stmt) = &mut self.prepare_stmt { - prepare_stmt.add_rows(rows); - } + Some(MysqlStateProgress::StmtExecResponseReceived) + } else { + // MultiResulset + if let Some(prepare_stmt) = &mut self.prepare_stmt { + prepare_stmt.add_rows(rows); + } - Some(MysqlStateProgress::StmtExecResponseContinue) + Some(MysqlStateProgress::StmtExecResponseContinue) + } + } else { + Some(MysqlStateProgress::StmtExecResponseReceived) } } _ => None, diff --git a/rust/src/mysql/parser.rs b/rust/src/mysql/parser.rs index 6c794e786a30..913c80a03d14 100644 --- a/rust/src/mysql/parser.rs +++ b/rust/src/mysql/parser.rs @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Open Information Security Foundation +/* Copyright (C) 2024 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