Skip to content

Commit 785b5a4

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 956242a commit 785b5a4

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

Adafruit_MQTT.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
317317
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
318318
uint8_t qos) {
319319
// Construct and send publish packet.
320-
uint16_t len = publishPacket(buffer, topic, data, bLen, qos);
320+
uint16_t len =
321+
publishPacket(buffer, topic, data, bLen, qos, (uint16_t)sizeof(buffer));
321322
if (!sendPacket(buffer, len))
322323
return false;
323324

@@ -665,11 +666,22 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
665666
return len;
666667
}
667668

669+
uint16_t Adafruit_MQTT::packetAdditionalLen(uint16_t currLen) {
670+
/* Increase length field based on current length */
671+
if (currLen < 128)
672+
return 0;
673+
if (currLen < 16384)
674+
return 1;
675+
if (currLen < 2097151)
676+
return 2;
677+
return 3;
678+
}
679+
668680
// as per
669681
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
670682
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
671-
uint8_t *data, uint16_t bLen,
672-
uint8_t qos) {
683+
uint8_t *data, uint16_t bLen, uint8_t qos,
684+
uint16_t maxPacketLen) {
673685
uint8_t *p = packet;
674686
uint16_t len = 0;
675687

@@ -679,7 +691,21 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
679691
if (qos > 0) {
680692
len += 2; // qos packet id
681693
}
682-
len += bLen; // payload length
694+
// calculate additional bytes for length field (if any)
695+
uint16_t additionalLen = packetAdditionalLen(len + bLen);
696+
697+
// payload length
698+
if (len + bLen + 2 + additionalLen <= maxPacketLen) {
699+
len += bLen + additionalLen;
700+
} else {
701+
// If we make it here, we got a pickle: the payload is not going
702+
// to fit in the packet buffer. Instead of corrupting memory, let's
703+
// do something less damaging by reducing the bLen to what we are
704+
// able to accomodate. Alternatively, consider using a bigger
705+
// maxPacketLen.
706+
bLen = maxPacketLen - (len + 2 + packetAdditionalLen(maxPacketLen));
707+
len = maxPacketLen - 4;
708+
}
683709

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

Adafruit_MQTT.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ class Adafruit_MQTT {
257257
// Functions to generate MQTT packets.
258258
uint8_t connectPacket(uint8_t *packet);
259259
uint8_t disconnectPacket(uint8_t *packet);
260+
static uint16_t packetAdditionalLen(uint16_t currLen);
260261
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
261-
uint16_t bLen, uint8_t qos);
262+
uint16_t bLen, uint8_t qos, uint16_t maxPacketLen);
262263
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
263264
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
264265
uint8_t pingPacket(uint8_t *packet);

0 commit comments

Comments
 (0)