Skip to content

Integer overflow on Arduino (ATmega) in "MqttDecode_Vbi" #235

Description

@cbjornram

After unsuccessfully trying to get wolfMQTT up and running on an Arduino Mega I found something I believe is an issue. I am relatively new to digging around in C++ libraries, so please correct me (and feel free to close the issue) if I have misunderstood.

In the following lines used when decoding incoming MQTT messages, "multiplier" is defined as an int. For regular Arduino ATmega-chipsets (Uno, Mega, etc.) integers are 16 bits, which means the part at lines 229-232 overflow since the do/while-loop is designed to run up to four times and "MQTT_PACKET_LEN_ENCODE_MASK" is defined as 0x80 in MQTT_packet.h.

wolfMQTT/src/mqtt_packet.c

Lines 217 to 240 in 456ac9e

int MqttDecode_Vbi(byte *buf, word32 *value, word32 buf_len)
{
word32 rc = 0;
int multiplier = 1;
byte encodedByte;
*value = 0;
do {
if (buf_len < rc + 1)
return MQTT_CODE_ERROR_OUT_OF_BUFFER;
encodedByte = *(buf++);
*value += (encodedByte & ~MQTT_PACKET_LEN_ENCODE_MASK) * multiplier;
multiplier *= MQTT_PACKET_LEN_ENCODE_MASK;
if (multiplier > (MQTT_PACKET_LEN_ENCODE_MASK *
MQTT_PACKET_LEN_ENCODE_MASK *
MQTT_PACKET_LEN_ENCODE_MASK))
{
return MQTT_CODE_ERROR_MALFORMED_DATA;
}
rc++;
} while ((encodedByte & MQTT_PACKET_LEN_ENCODE_MASK) != 0);
return (int)rc;
}

This causes an "Error (Malformed Remaining Length)" (MQTT_CODE_ERROR_MALFORMED_DATA) at runtime when trying to initialise a connection to a broker even if the received CONNACK header is correctly formatted. As a consequence, the Arduino can not make a connection to a broker at all.

Kind regards,
Christoffer

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions