Skip to content

Commit ad914cd

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 ad914cd

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

Adafruit_MQTT.cpp

Lines changed: 27 additions & 3 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 = publishPacket(buffer, topic, data, bLen, qos,
321+
(uint16_t) sizeof(buffer));
321322
if (!sendPacket(buffer, len))
322323
return false;
323324

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

669+
uint16_t Adafruit_MQTT::packetAdditionalLen(uint16_t currLen)
670+
{
671+
/* Increase length field based on current length */
672+
if (currLen < 128) return 0;
673+
if (currLen < 16384) return 1;
674+
if (currLen < 2097151) return 2;
675+
return 3;
676+
}
677+
668678
// as per
669679
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
670680
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
671681
uint8_t *data, uint16_t bLen,
672-
uint8_t qos) {
682+
uint8_t qos, uint16_t maxPacketLen) {
673683
uint8_t *p = packet;
674684
uint16_t len = 0;
675685

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

684708
// Now you can start generating the packet!
685709
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)