Skip to content

Commit 2971607

Browse files
committed
Fix: Enforce MQTT v5 property-packet validation and improve decoding safety
Implements necessary protocol validation in MqttEncode_Props and MqttDecode_Props to ensure that properties are only used in their allowed packet types, addressing the 'TODO: validate packet type'. - Defines new error code: MQTT_CODE_ERROR_PROPERTY_MISMATCH. - Adds critical buffer boundary checks in MqttDecode_Props before VBI and string decoding to prevent potential buffer overruns. Signed-off-by: Badr Bacem KAABIA <[email protected]>
1 parent ab2088a commit 2971607

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/mqtt_packet.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,15 @@ int MqttEncode_Props(MqttPacketType packet, MqttProp* props, byte* buf)
377377

378378
/* TODO: Check against max size. Sometimes all properties are not
379379
expected to be added */
380-
/* TODO: Validate property type is allowed for packet type */
381380

382381
/* loop through the list properties */
383382
while ((cur_prop != NULL) && (rc >= 0))
384383
{
385-
/* TODO: validate packet type */
386-
(void)packet;
384+
/* Validate property type is allowed for this packet type */
385+
if (!(gPropMatrix[cur_prop->type].packet_type_mask & (1 << packet))) {
386+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_PROPERTY_MISMATCH);
387+
break;
388+
}
387389

388390
/* Encode the Identifier */
389391
tmp = MqttEncode_Vbi(buf, (word32)cur_prop->type);
@@ -505,11 +507,12 @@ int MqttDecode_Props(MqttPacketType packet, MqttProp** props, byte* pbuf,
505507
break;
506508
}
507509

508-
/* Decode the Identifier */
509-
if (buf_len < (word32)(buf - pbuf)) {
510+
/* Check boundary before VBI decoding */
511+
if ((buf - pbuf) > (int)buf_len) {
510512
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_OUT_OF_BUFFER);
511513
break;
512514
}
515+
/* Decode the Identifier */
513516
rc = MqttDecode_Vbi(buf, (word32*)&cur_prop->type,
514517
(word32)(buf_len - (buf - pbuf)));
515518
if (rc < 0) {
@@ -520,8 +523,11 @@ int MqttDecode_Props(MqttPacketType packet, MqttProp** props, byte* pbuf,
520523
total += tmp;
521524
prop_len -= tmp;
522525

523-
/* TODO: Validate property type is allowed for packet type */
524-
(void)packet;
526+
/* Validate property type is allowed for packet type */
527+
if (!(gPropMatrix[cur_prop->type].packet_type_mask & (1 << packet))) {
528+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_PROPERTY_MISMATCH);
529+
break;
530+
}
525531

526532
if (cur_prop->type >= sizeof(gPropMatrix) / sizeof(gPropMatrix[0])) {
527533
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_PROPERTY);
@@ -561,6 +567,11 @@ int MqttDecode_Props(MqttPacketType packet, MqttProp** props, byte* pbuf,
561567
}
562568
case MQTT_DATA_TYPE_STRING:
563569
{
570+
if ((buf - pbuf) > (int)buf_len) {
571+
/* Should already be caught earlier, but safe to recheck */
572+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_OUT_OF_BUFFER);
573+
break;
574+
}
564575
tmp = MqttDecode_String(buf,
565576
(const char**)&cur_prop->data_str.str,
566577
&cur_prop->data_str.len,

wolfmqtt/mqtt_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ enum MqttPacketResponseCodes {
202202
MQTT_CODE_ERROR_CURL = -16, /* An error in libcurl that is not clearly
203203
* a network, memory, TLS, or system error. */
204204
#endif
205+
MQTT_CODE_ERROR_PROPERTY_MISMATCH = -17,
205206

206207
MQTT_CODE_CONTINUE = -101,
207208
MQTT_CODE_STDIN_WAKE = -102,

0 commit comments

Comments
 (0)