SN_Decode_GWInfo: reject extended-length frame too short for gwId#542
Open
dxbjavid wants to merge 1 commit into
Open
SN_Decode_GWInfo: reject extended-length frame too short for gwId#542dxbjavid wants to merge 1 commit into
dxbjavid wants to merge 1 commit into
Conversation
An MQTT-SN GWINFO frame with the 0x01 SN_PACKET_LEN_IND prefix consumes three header bytes (indicator + two-byte length) before the message-type and gateway-ID bytes that SN_Decode_GWInfo reads via *rx_payload++. The existing minimum-size guard (total_len < 3) was sized for the short-form header (one byte) and accepts an extended-length frame whose decoded total_len equals the bytes the function has already consumed for the header (3) or sits one byte short of also covering the gateway-ID byte (4). In both cases the type / gwId reads run past the caller-supplied rx_buf bound; a page-guarded reproducer (mmap + adjacent PROT_NONE page) shows SIGBUS on the *rx_payload++ at offset 4. Replace the fixed "< 3" minimum with a check against (rx_payload - rx_buf) + 2, which is the number of bytes the function must still read after the length-indicator block (one message-type byte plus one gateway-ID byte). This covers both header layouts uniformly: the short form keeps the prior >= 3 minimum, and the extended-length form requires >= 5. After the patch the page-guarded frame returns MQTT_CODE_ERROR_MALFORMED_DATA cleanly and the existing valid short-form and extended-length-with-address inputs continue to decode.
|
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_GWInforeads the message-type and gateway-ID bytes from thecaller-supplied receive buffer via
*rx_payload++(src/mqtt_sn_packet.c:306,src/mqtt_sn_packet.c:313). When the GWINFO frame uses the extended-length
encoding (first byte =
SN_PACKET_LEN_IND = 0x01, length in the next twobytes), the function has already consumed three header bytes by the time
it reaches these reads — but the existing minimum-size guard
is sized for the short-form header (one byte). An extended-length frame
whose decoded
total_lenis<= (rx_payload - rx_buf)slips past it, andthe subsequent message-type / gateway-ID reads run past the caller's
rx_bufbound.SN_Packet_Readaccepts both header forms and forwardsthe buffer to
SN_Decode_GWInfoviaMqttClient_DecodePacket, so thisis reachable from the wire.
How to reproduce
Place a four-byte frame at the tail of a page-sized region whose next
page is
PROT_NONE:With
rx_buf_len = 4:total_len = 4, three bytes are consumed for the extended-lengthheader, leaving
rx_payloadat offset 3.4 > 4is false (passestotal_len > rx_buf_len).4 < 3is false (passes the existing minimum).type = *rx_payload++reads offset 3 (OK).gw_info->gwId = *rx_payload++reads offset 4 → SIGBUS on thePROT_NONE guard page.
Fix
Replace the fixed
< 3minimum with a check against(rx_payload - rx_buf) + 2, which is the bytes the function still needsto read after the length-indicator block (one message-type byte plus one
gateway-ID byte). The check covers both header layouts uniformly: the
short form (
rx_payload - rx_buf == 1) keeps the prior>= 3minimum,and the extended-length form (
rx_payload - rx_buf == 3) requirestotal_len >= 5. The fix is in the callee; callers do not need tochange.
Test / build evidence
A small reproducer with the page-guarded frame above plus positive
controls was built against
libwolfmqttfrom this tree:Before the patch the page-guarded reproducer exits with signal 138
(SIGBUS / SIGSEGV depending on the OS) on the offset-4 read; after the
patch it returns
MQTT_CODE_ERROR_MALFORMED_DATAand the existingshort-form and extended-length-with-address inputs continue to decode.
./configure --enable-sn && make -jbuilds clean.