SN_Decode_PublishResp: reject total_len not matching fixed layout#543
Open
jmestwa-coder wants to merge 1 commit into
Open
SN_Decode_PublishResp: reject total_len not matching fixed layout#543jmestwa-coder wants to merge 1 commit into
jmestwa-coder wants to merge 1 commit into
Conversation
The MQTT-SN PUBACK frame is fixed at 7 octets (Length + MsgType + TopicID + MsgID + ReturnCode) and PUBREC/PUBREL/PUBCOMP are fixed at 4 octets (Length + MsgType + MsgID). SN_Decode_PublishResp only checked total_len <= rx_buf_len before reading the message-type byte at offset 1, so a frame whose declared total_len is smaller than the relevant fixed layout passed validation and the subsequent *rx_payload++ for rec_type walked past the caller-supplied bound. A page-guard reproducer (mmap of one RW page followed by an adjacent PROT_NONE page, with rx_buf placed at the last byte of the readable page and rx_buf[0] = 0x00 so total_len=0) crashes with SIGBUS at the rec_type read on master and returns MQTT_CODE_ERROR_MALFORMED_DATA cleanly after the patch. Existing well-formed PUBACK (total_len=7) and PUBREC/PUBREL/PUBCOMP (total_len=4) frames continue to decode unchanged.
|
Can one of the admins verify this patch? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
SN_Decode_PublishRespinsrc/mqtt_sn_packet.cvalidatestotal_lenagainst
rx_buf_lenbut never against the actual fixed-size layoutdefined by the MQTT-SN spec for the four packet types it decodes:
MsgID(2) + ReturnCode.
MsgID(2).
The relevant code, at
src/mqtt_sn_packet.c:1308-1317on master:With
rx_buf_len = 1andrx_buf[0] = 0x00(or0x01), the function:total_lenfrom byte 0.total_len > rx_buf_lencheck (0 > 1and1 > 1areboth false).
rec_typefrom byte 1 — one byte past the caller-suppliedbound.
How to reproduce
A page-guard reproducer (mmap of one RW page followed by an adjacent
PROT_NONEpage, withrx_bufplaced at the last byte of the readablepage) crashes with
SIGBUSat therec_type = *rx_payload++read onmaster:
Master: process terminates with
SIGBUS(exit 138 on macOS).Patched: returns
MQTT_CODE_ERROR_MALFORMED_DATA(-3), no crash.Fix
Add an in-callee
total_lencheck that mirrors the strict-equalitypattern already used by the surrounding decoders for fixed-size MQTT-SN
packets (e.g.
SN_Decode_RegAckusestotal_len != 7,SN_Decode_SubscribeAckusestotal_len != 8,SN_Decode_UnsubscribeAckusestotal_len != 4,SN_Decode_DisconnectandSN_Decode_Pingusetotal_len != 2):This rejects the malformed frame before any further field read and is
local to the callee. Well-formed PUBACK (total_len=7) and
PUBREC/PUBREL/PUBCOMP (total_len=4) frames continue to decode
unchanged.
Build / test evidence
./autogen.sh && ./configure --enable-sn --disable-tls && make -j4succeeds.
make checkunit_tests pass.(
scripts/firmware.testis pre-existing and requires a livetest.mosquitto.orgconnection.)(
{7,PUBACK,0x01,0x02,0x03,0x04,0x05}) still returns 7 withtopicId=0x0102,packet_id=0x0304,return_code=0x05.(
{4,PUBREL,0x10,0x20}) still returns 4 withpacket_id=0x1020.SIGBUSto a cleanMQTT_CODE_ERROR_MALFORMED_DATAreturn.