Skip to content

Commit ef759aa

Browse files
committed
F-2358 - https://fenrir.wolfssl.com/finding/2358 - Reject overlong Variable Byte Integer encodings [MQTT-1.5.5-1]
1 parent 6044657 commit ef759aa

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/mqtt_packet.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ int MqttDecode_Vbi(byte *buf, word32 *value, word32 buf_len)
244244
rc++;
245245
} while ((encodedByte & MQTT_PACKET_LEN_ENCODE_MASK) != 0);
246246

247+
/* [MQTT-1.5.5-1] Reject non-canonical overlong encodings */
248+
if (rc > 1 && encodedByte == 0) {
249+
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_MALFORMED_DATA);
250+
}
251+
247252
return (int)rc;
248253
}
249254

tests/unit_test.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,31 @@ static void test_vbi(void)
132132
rc = MqttDecode_Vbi(buf, &value, sizeof(buf));
133133
CHECK(rc == 4, "Decode VBI 2097152 roundtrip: rc == 4");
134134
CHECK(value == 2097152, "Decode VBI 2097152 roundtrip: value correct");
135+
136+
/* [MQTT-1.5.5-1] Overlong encodings must be rejected */
137+
/* Overlong 2-byte encoding of 0: [0x80, 0x00] */
138+
buf[0] = 0x80; buf[1] = 0x00;
139+
rc = MqttDecode_Vbi(buf, &value, sizeof(buf));
140+
CHECK(rc == MQTT_CODE_ERROR_MALFORMED_DATA,
141+
"Overlong 2-byte VBI (0): returns MALFORMED_DATA");
142+
143+
/* Overlong 3-byte encoding of 0: [0x80, 0x80, 0x00] */
144+
buf[0] = 0x80; buf[1] = 0x80; buf[2] = 0x00;
145+
rc = MqttDecode_Vbi(buf, &value, sizeof(buf));
146+
CHECK(rc == MQTT_CODE_ERROR_MALFORMED_DATA,
147+
"Overlong 3-byte VBI (0): returns MALFORMED_DATA");
148+
149+
/* Overlong 4-byte encoding of 0: [0x80, 0x80, 0x80, 0x00] */
150+
buf[0] = 0x80; buf[1] = 0x80; buf[2] = 0x80; buf[3] = 0x00;
151+
rc = MqttDecode_Vbi(buf, &value, sizeof(buf));
152+
CHECK(rc == MQTT_CODE_ERROR_MALFORMED_DATA,
153+
"Overlong 4-byte VBI (0): returns MALFORMED_DATA");
154+
155+
/* Overlong 2-byte encoding of 127: [0xFF, 0x00] */
156+
buf[0] = 0xFF; buf[1] = 0x00;
157+
rc = MqttDecode_Vbi(buf, &value, sizeof(buf));
158+
CHECK(rc == MQTT_CODE_ERROR_MALFORMED_DATA,
159+
"Overlong 2-byte VBI (127): returns MALFORMED_DATA");
135160
}
136161

137162
/* -------------------------------------------------------------------------- */

0 commit comments

Comments
 (0)