Skip to content

Commit 5cde93d

Browse files
committed
mqtt: bounds number of messages per tx
Ticket: 8525 (cherry picked from commit 03ba8a7)
1 parent 9d78857 commit 5cde93d

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

rules/mqtt-events.rules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ alert mqtt any any -> any any (msg:"SURICATA MQTT missing message ID"; app-layer
1515
alert mqtt any any -> any any (msg:"SURICATA MQTT unassigned message type (0 or >15)"; app-layer-event:mqtt.unassigned_msg_type; classtype:protocol-command-decode; sid:2229008; rev:1;)
1616
alert mqtt any any -> any any (msg:"SURICATA MQTT too many transactions"; app-layer-event:mqtt.too_many_transactions; classtype:protocol-command-decode; sid:2229009; rev:1;)
1717
alert mqtt any any -> any any (msg:"SURICATA MQTT malformed traffic"; app-layer-event:mqtt.malformed_traffic; classtype:protocol-command-decode; sid:2229010; rev:1;)
18+
alert mqtt any any -> any any (msg:"SURICATA MQTT too many messages"; app-layer-event:mqtt.too_many_messages; classtype:protocol-command-decode; sid:2229011; rev:1;)

rust/src/mqtt/mqtt.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ static mut MAX_MSG_LEN: u32 = 1048576;
4040

4141
static mut MQTT_MAX_TX: usize = 1024;
4242

43+
static mut MQTT_MAX_MSGS: usize = 1024;
44+
4345
static mut ALPROTO_MQTT: AppProto = ALPROTO_UNKNOWN;
4446

4547
#[derive(AppLayerFrameType)]
@@ -62,6 +64,7 @@ pub enum MQTTEvent {
6264
UnassignedMsgType,
6365
TooManyTransactions,
6466
MalformedTraffic,
67+
TooManyMessages,
6568
}
6669

6770
#[derive(Debug)]
@@ -313,7 +316,11 @@ impl MQTTState {
313316
}
314317
MQTTOperation::PUBREC(ref v) | MQTTOperation::PUBREL(ref v) => {
315318
if let Some(tx) = self.get_tx_by_pkt_id(v.message_id as u32) {
316-
tx.msg.push(msg);
319+
if tx.msg.len() >= unsafe { MQTT_MAX_MSGS } {
320+
tx.tx_data.set_event(MQTTEvent::TooManyMessages as u8);
321+
} else {
322+
tx.msg.push(msg);
323+
}
317324
} else {
318325
let mut tx = self.new_tx(msg, toclient);
319326
MQTTState::set_event(&mut tx, MQTTEvent::MissingPublish);
@@ -805,6 +812,13 @@ pub unsafe extern "C" fn rs_mqtt_register_parser(cfg_max_msg_len: u32) {
805812
SCLogError!("Invalid value for mqtt.max-tx");
806813
}
807814
}
815+
if let Some(val) = conf_get("app-layer.protocols.mqtt.max-messages") {
816+
if let Ok(v) = val.parse::<usize>() {
817+
MQTT_MAX_MSGS = v;
818+
} else {
819+
SCLogWarning!("Invalid value for mqtt.max-messages");
820+
}
821+
}
808822
} else {
809823
SCLogDebug!("Protocol detector and parser disabled for MQTT.");
810824
}

suricata.yaml.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,8 @@ app-layer:
909909
# unsubscribe-topic-match-limit: 100
910910
# Maximum number of live MQTT transactions per flow
911911
# max-tx: 4096
912+
# Maximum number of messages per transaction
913+
# max-messages: 1024
912914
krb5:
913915
enabled: yes
914916
bittorrent-dht:

0 commit comments

Comments
 (0)