Mosquitto Uses DISCONNECT 0x82 Instead of 0x81 For DISCONNECT Reserved-Bit Violations
Mosquitto Uses DISCONNECT 0x82 Instead of 0x81 For DISCONNECT Reserved-Bit Violations
Conclusion
MQTT 5 requires invalid fixed-header flags on DISCONNECT to be classified as
Malformed Packet, which corresponds to DISCONNECT 0x81.
Mosquitto does check those reserved bits and does disconnect the peer, but it
returns MOSQ_ERR_PROTOCOL for that condition. The MQTT 5 read-dispatch layer
then maps that to DISCONNECT 0x82 (Protocol Error).
This same reason-code mismatch covers both item 487 and item 491.
MQTT Standard Requirement
- Standard: MQTT Version 5.0 OASIS Standard
- Section
2.1.3Fixed Header Flags - Section
3.14.1DISCONNECT Fixed Header - Section
4.13Handling errors
The key normative text is:
The Fixed Header flags for this packet type MUST be set to the value
listed in Table 2-2.
If invalid flags are received it is a Malformed Packet.
For DISCONNECT, the low four bits are reserved and must be 0000.
That means:
- non-zero reserved bits on
DISCONNECTare not just a generic protocol mistake - the specification classifies them specifically as
Malformed Packet - the corresponding MQTT 5 wire reason code is
0x81, not0x82
Relevant Code
The Reserved-Bit Check Exists
src/handle_disconnect.c validates the low four bits:
if(context->protocol == mosq_p_mqtt311 || context->protocol == mosq_p_mqtt5){
if((context->in_packet.command&0x0F) != 0x00){
log__printf(NULL, MOSQ_LOG_INFO, "Protocol error from %s: DISCONNECT packet with incorrect flags %02X.",
context->id, context->in_packet.command);
return MOSQ_ERR_PROTOCOL;
}
}
So mosquitto is not missing the check itself.
The Internal Classification Is The Problem
The same branch returns:
MOSQ_ERR_PROTOCOL
Then src/read_handle.c performs the MQTT 5 reason-code mapping:
if(context->protocol == mosq_p_mqtt5){
if(rc == MOSQ_ERR_PROTOCOL || rc == MOSQ_ERR_DUPLICATE_PROPERTY){
send__disconnect(context, MQTT_RC_PROTOCOL_ERROR, NULL);
}else if(rc == MOSQ_ERR_MALFORMED_PACKET){
send__disconnect(context, MQTT_RC_MALFORMED_PACKET, NULL);
}
...
}
That means the end-to-end behavior is:
invalid DISCONNECT reserved bits
-> handle__disconnect() returns MOSQ_ERR_PROTOCOL
-> read_handle maps that to MQTT_RC_PROTOCOL_ERROR
-> broker sends DISCONNECT 0x82
But the MQTT 5 classification should be:
invalid DISCONNECT reserved bits
-> malformed packet
-> DISCONNECT 0x81
Runtime Evidence
This behavior was already reproduced on the same code path in the earlier audit batch and is explicitly reused here because the implementation branch is the same.
Observed runtime result:
bad_disconnect_flags -> e0 01 82
Packet interpretation:
e0 01 82 = DISCONNECT Protocol Error
Expected MQTT 5 result:
e0 01 81 = DISCONNECT Malformed Packet
That means:
- the peer is disconnected, so the validation is active
- but the wire-visible reason code is wrong
- the mismatch is therefore in classification, not in basic enforcement
Why This Is Partial
Implemented:
- invalid reserved bits are detected
- the connection is terminated
Missing:
- the condition is not classified as
Malformed Packet - the outgoing MQTT 5 reason code is therefore
0x82instead of0x81
So this is a narrower classification bug, not a total lack of validation.
Suggested Fix
For this specific branch, handle__disconnect() should return
MOSQ_ERR_MALFORMED_PACKET instead of MOSQ_ERR_PROTOCOL, so the existing
dispatch layer will emit:
DISCONNECT 0x81
I have analyzed the issue and the suggested fix. I would like to work on this and submit a PR. Can you assign it to me?
Thanks for looking into this! I don’t think I have permission to assign issues in this repository. Please feel free to open a PR referencing this issue, and the maintainers can assign it if needed.
Thanks for the guidance! I've just opened the PR https://github.com/eclipse-mosquitto/mosquitto/pull/3642 addressing this issue.