Skip to content

Commit

Permalink
mysql: fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
QianKaiLin committed Oct 9, 2024
1 parent 2c28309 commit 149c42b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 45 deletions.
2 changes: 1 addition & 1 deletion etc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 4 additions & 6 deletions rust/src/mysql/detect.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/src/mysql/logger.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion rust/src/mysql/mod.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
83 changes: 48 additions & 35 deletions rust/src/mysql/mysql.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -326,6 +326,9 @@ impl MysqlState {
}
self.tx_index_completed = index;
}
if tx.tls {
tx.complete = true;
}
tx
}

Expand All @@ -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()
}

Expand All @@ -362,6 +366,7 @@ impl MysqlState {
AppLayerRequestProtocolTLSUpgrade(f);
}
self.tls = true;
self.create_tx("".to_string());
Some(MysqlStateProgress::Finished)
}
MysqlFEMessage::AuthRequest => None,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<Vec<String>>(),
);
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::<Vec<String>>(),
);
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,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/mysql/parser.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 149c42b

Please sign in to comment.