MQTT state machine confusion: PINGRESP/DISCONNECT with non-zero remaining_length dispatches to stale nextstate
curl's MQTT client did not check that PINGRESP and DISCONNECT packets carry an empty body, so a malicious broker could send one with extra trailing bytes. Those bytes were then parsed as whatever message type the client expected next, letting the broker inject PUBLISH data or fake SUBACK/CONNACK replies.
Originally disclosed on HackerOne
- Disclosed after 13 hours
Summary:
In lib/mqtt.c, the state machine in mqtt_doing (lines 894-911 in curl 8.20.0) does not validate that PINGRESP (0xD0) and DISCONNECT (0xE0) packets have remaining_length == 0 as required by MQTT 3.1.1 spec sections 3.13.1 and 3.14.1. A malicious broker can send a PINGRESP fixed header with non-zero remaining_length followed by trailing bytes, and the state machine will dispatch to whatever mqtt->nextstate was set to from a previous cycle (typically MQTT_PUBWAIT after a SUBSCRIBE) and parse the trailing bytes as that state's payload. Depending on nextstate, this allows a malicious broker to inject PUBLISH payload data, fake a SUBACK/CONNACK, or otherwise corrupt the client's protocol state. A patch is available at https://github.com/GovClawAPI/curl/commit/7ad90263bc which validates remaining_length == 0 for both packet types before state dispatch and returns CURLE_WEIRD_SERVER_REPLY on violation.
Affected version
curl 8.20.0 (the current stable release at time of audit). The affected code in lib/mqtt.c:894-911 appears unchanged in current main. Built and tested on Linux x86_64.
Steps To Reproduce:
- Run a malicious MQTT broker that completes the standard CONNECT/CONNACK and SUBSCRIBE/SUBACK handshake with a curl client.
- After the SUBACK, instead of a normal PUBLISH or PINGRESP, send the bytes
0xD0 0x02 0x00 0x00(PINGRESP fixed header claiming remaining_length=2, plus two payload bytes). - Observe that curl does not reject the malformed PINGRESP. The state machine consumes the two trailing bytes as the start of the next expected message (typically the variable-header of a PUBLISH), corrupting subsequent message parsing.
- The same applies to DISCONNECT (0xE0) packets with non-zero remaining_length.
Again: there is no bug-bounty for curl. We do not offer any rewards, only proper credits.
Impact
Summary:
A malicious or compromised MQTT broker can cause protocol-level state confusion in curl's MQTT client. The trailing bytes from a malformed PINGRESP or DISCONNECT are interpreted as the payload of whatever state was queued in mqtt->nextstate. Concrete consequences depending on the queued state: trailing bytes parsed as PUBLISH payload allow the broker to inject arbitrary content into the client's receive callback; trailing bytes parsed as SUBACK can cause the application to believe a subscription was confirmed when it was not; trailing bytes parsed as CONNACK can cause incorrect connection-state assumptions. The exposure is to MQTT clients connecting to untrusted or compromised brokers, which is a realistic threat model for MQTT given its common deployment across IoT and edge environments.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The MQTT state machine dispatched PINGRESP and DISCONNECT packets without first checking that their remaining_length field was zero, so leftover bytes were fed into the previously queued next state.
Impact
A malicious or compromised broker could inject content into the client's receive callback, make the application believe a subscription was confirmed, or cause wrong connection-state assumptions; only clients connecting to untrusted brokers are exposed.
What the attacker needed
Client connects to a malicious broker; broker completes the MQTT handshake
The fix
The patch validates that remaining_length is zero for both packet types before dispatch and returns a server-reply error on violation.
How it was found
- Run a malicious MQTT broker that completes the normal CONNECT/CONNACK and SUBSCRIBE/SUBACK handshake with a curl client.
- After the SUBACK, send a PINGRESP fixed header that claims a non-zero remaining_length, followed by extra bytes.
- Observe that curl accepts the malformed PINGRESP and consumes the trailing bytes as the start of the next expected message.
- The same behaviour applies to DISCONNECT packets with a non-zero remaining_length.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
MQTT is a messaging protocol used by small devices to talk to a broker. Each message starts with a header that says what kind of message it is and how many bytes follow. Two message types, PINGRESP and DISCONNECT, are supposed to carry no data at all, but curl did not enforce that, so a hostile broker could sneak extra bytes in and curl would treat them as the start of the next message.
What is Misconfiguration?
Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.