Skip to content

Commit 6bcc819

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 6bcc819

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

Adafruit_MQTT.cpp

Lines changed: 25 additions & 3 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,9 +637,17 @@ 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;
645653
uint16_t len=0;
@@ -650,7 +658,21 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
650658
if(qos > 0) {
651659
len += 2; // qos packet id
652660
}
653-
len += bLen; // payload length
661+
// calculate additional bytes for length field (if any)
662+
uint16_t additionalLen = packetAdditionalLen(len + bLen);
663+
664+
// payload length
665+
if (len + bLen + 2 + additionalLen <= maxPacketLen) {
666+
len += bLen + additionalLen;
667+
} else {
668+
// If we make it here, we got a pickle: the payload is not going
669+
// to fit in the packet buffer. Instead of corrupting memory, let's
670+
// do something less damaging by reducing the bLen to what we are
671+
// able to accomodate. Alternatively, consider using a bigger
672+
// maxPacketLen.
673+
bLen = maxPacketLen - (len + 2 + packetAdditionalLen(maxPacketLen));
674+
len = maxPacketLen - 4;
675+
}
654676

655677
// Now you can start generating the packet!
656678
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;

Adafruit_MQTT.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ 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+
static uint16_t packetAdditionalLen(uint16_t currLen);
247+
uint16_t publishPacket(uint8_t *packet, uint16_t maxPacketLen,
248+
const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos);
247249
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
248250
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
249251
uint8_t pingPacket(uint8_t *packet);

0 commit comments

Comments
 (0)