Skip to content

Commit b430f2c

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 <badrbacemkaabia@gmail.com>
1 parent ab2088a commit b430f2c

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/mqtt_packet.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,20 @@ 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+
if (cur_prop->type >= sizeof(gPropMatrix) / sizeof(gPropMatrix[0])) {
385+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_PROPERTY);
386+
break;
387+
}
388+
389+
/* Validate property type is allowed for this packet type */
390+
if (!(gPropMatrix[cur_prop->type].packet_type_mask & (1 << packet))) {
391+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_PROPERTY_MISMATCH);
392+
break;
393+
}
387394

388395
/* Encode the Identifier */
389396
tmp = MqttEncode_Vbi(buf, (word32)cur_prop->type);
@@ -505,11 +512,12 @@ int MqttDecode_Props(MqttPacketType packet, MqttProp** props, byte* pbuf,
505512
break;
506513
}
507514

508-
/* Decode the Identifier */
509-
if (buf_len < (word32)(buf - pbuf)) {
515+
/* Check boundary before VBI decoding */
516+
if ((buf - pbuf) > (int)buf_len) {
510517
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_OUT_OF_BUFFER);
511518
break;
512519
}
520+
/* Decode the Identifier */
513521
rc = MqttDecode_Vbi(buf, (word32*)&cur_prop->type,
514522
(word32)(buf_len - (buf - pbuf)));
515523
if (rc < 0) {
@@ -520,14 +528,17 @@ int MqttDecode_Props(MqttPacketType packet, MqttProp** props, byte* pbuf,
520528
total += tmp;
521529
prop_len -= tmp;
522530

523-
/* TODO: Validate property type is allowed for packet type */
524-
(void)packet;
525-
526531
if (cur_prop->type >= sizeof(gPropMatrix) / sizeof(gPropMatrix[0])) {
527532
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_PROPERTY);
528533
break;
529534
}
530535

536+
/* Validate property type is allowed for packet type */
537+
if (!(gPropMatrix[cur_prop->type].packet_type_mask & (1 << packet))) {
538+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_PROPERTY_MISMATCH);
539+
break;
540+
}
541+
531542
switch (gPropMatrix[cur_prop->type].data)
532543
{
533544
case MQTT_DATA_TYPE_BYTE:
@@ -561,6 +572,11 @@ int MqttDecode_Props(MqttPacketType packet, MqttProp** props, byte* pbuf,
561572
}
562573
case MQTT_DATA_TYPE_STRING:
563574
{
575+
if ((buf - pbuf) > (int)buf_len) {
576+
/* Should already be caught earlier, but safe to recheck */
577+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_OUT_OF_BUFFER);
578+
break;
579+
}
564580
tmp = MqttDecode_String(buf,
565581
(const char**)&cur_prop->data_str.str,
566582
&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)