Skip to content

Commit 4204e1d

Browse files
Adafruit_MQTT::publishPacket: Protect against memory corruption.
Avoid memory corruption from happening when data payload provided in Adafruit_MQTT::publishPacket is greater than MAXBUFFERSIZE. In order to do that, a helper function is being added to calculate how much space is available for the payload after subtracting what is used as the header. Pull request #166 Fixes #109 Fixes #122 Signed-off-by: Flavio Fernandes <flavio@flaviof.com>
1 parent 3693eb8 commit 4204e1d

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

Adafruit_MQTT.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
303303

304304
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos) {
305305
// Construct and send publish packet.
306-
uint16_t len = publishPacket(buffer, topic, data, bLen, qos);
306+
uint16_t len = publishPacket(buffer, (uint16_t) sizeof(buffer), topic, data, bLen, qos);
307307
if (!sendPacket(buffer, len))
308308
return false;
309309

@@ -637,20 +637,40 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
637637
return len;
638638
}
639639

640+
uint16_t Adafruit_MQTT::packetAdditionalLen(uint16_t currLen)
641+
{
642+
/* Increase length field based on current length */
643+
if (currLen < 128) return 0;
644+
if (currLen < 16384) return 1;
645+
if (currLen < 2097151) return 2;
646+
return 3;
647+
}
640648

641649
// as per http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
642-
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
650+
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, uint16_t maxPacketLen, const char *topic,
643651
uint8_t *data, uint16_t bLen, uint8_t qos) {
644652
uint8_t *p = packet;
645-
uint16_t len=0;
653+
uint16_t len=2; // control + length
646654

647-
// calc length of non-header data
648-
len += 2; // two bytes to set the topic size
649-
len += strlen(topic); // topic length
655+
len += strlen(topic);
650656
if(qos > 0) {
651657
len += 2; // qos packet id
652658
}
653-
len += bLen; // payload length
659+
// calculate additional bytes for length field (if any)
660+
uint16_t additionalLen = packetAdditionalLen(len + bLen);
661+
662+
// payload length
663+
if (len + bLen + 2 + additionalLen <= maxPacketLen) {
664+
len += bLen + additionalLen;
665+
} else {
666+
// If we make it here, we got a pickle: the payload is not going
667+
// to fit in the packet buffer. Instead of corrupting memory, let's
668+
// do something less damaging by reducing the bLen to what we are
669+
// able to accomodate. Alternatively, consider using a bigger
670+
// maxPacketLen.
671+
bLen = maxPacketLen - (len + 2 + packetAdditionalLen(maxPacketLen));
672+
len = maxPacketLen - 4;
673+
}
654674

655675
// Now you can start generating the packet!
656676
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
@@ -684,7 +704,8 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
684704
memmove(p, data, bLen);
685705
p+= bLen;
686706
len = p - packet;
687-
DEBUG_PRINTLN(F("MQTT publish packet:"));
707+
DEBUG_PRINT(F("MQTT publish packet (length: "));
708+
DEBUG_PRINT(len); DEBUG_PRINTLN(F("):"));
688709
DEBUG_PRINTBUFFER(buffer, len);
689710
return len;
690711
}

Adafruit_MQTT.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,13 @@ class Adafruit_MQTT {
243243
// Functions to generate MQTT packets.
244244
uint8_t connectPacket(uint8_t *packet);
245245
uint8_t disconnectPacket(uint8_t *packet);
246-
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos);
246+
uint16_t publishPacket(uint8_t *packet, uint16_t maxPacketLen,
247+
const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos);
247248
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
248249
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
249250
uint8_t pingPacket(uint8_t *packet);
250251
uint8_t pubackPacket(uint8_t *packet, uint16_t packetid);
252+
static uint16_t packetAdditionalLen(uint16_t currLen);
251253
};
252254

253255

0 commit comments

Comments
 (0)