@@ -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 ,
0 commit comments