Skip to content

Commit c450130

Browse files
rlubosnashif
authored andcommitted
net: mqtt: Improve buffer bounds validation in mqtt_read_message_chunk
Verify more strictly that data read from the transport fits into RX buffer. Switch to unsigned integers, where possible, to prevent unnecessary signed/unsigned operations. Signed-off-by: Robert Lubos <[email protected]>
1 parent bb2f4a7 commit c450130

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

subsys/net/lib/mqtt/mqtt_rx.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,23 @@ static int mqtt_handle_packet(struct mqtt_client *client,
140140
static int mqtt_read_message_chunk(struct mqtt_client *client,
141141
struct buf_ctx *buf, u32_t length)
142142
{
143-
int remaining;
143+
u32_t remaining;
144144
int len;
145145

146+
/* In case all data requested has already been buffered, return. */
147+
if (length <= (buf->end - buf->cur)) {
148+
return 0;
149+
}
150+
146151
/* Calculate how much data we need to read from the transport,
147152
* given the already buffered data.
148153
*/
149154
remaining = length - (buf->end - buf->cur);
150-
if (remaining <= 0) {
151-
return 0;
152-
}
153155

154156
/* Check if read does not exceed the buffer. */
155-
if (buf->end + remaining > client->rx_buf + client->rx_buf_size) {
156-
MQTT_ERR("[CID %p]: Buffer too small to receive the message",
157+
if ((buf->end + remaining > client->rx_buf + client->rx_buf_size) ||
158+
(buf->end + remaining < client->rx_buf)) {
159+
MQTT_ERR("[CID %p]: Read would exceed RX buffer bounds.",
157160
client);
158161
return -ENOMEM;
159162
}

0 commit comments

Comments
 (0)