SN_Decode_Header: reject extended-length frame with total_len <= header size#541
Open
dxbjavid wants to merge 1 commit into
Open
SN_Decode_Header: reject extended-length frame with total_len <= header size#541dxbjavid wants to merge 1 commit into
dxbjavid wants to merge 1 commit into
Conversation
… no room for the message type The extended-length header form ([0x01][len_hi][len_lo]) decodes total_len from the second and third bytes and then reads the message-type byte that follows. The only bound check is `total_len > rx_buf_len`, which is met by total_len == rx_buf_len even though the three header bytes alone have already consumed the entire buffer. The subsequent `*rx_buf++` read of the type byte then crosses one byte past the caller-supplied rx_buf/rx_buf_len bound. Reject the malformed frame before that read.
|
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
src/mqtt_sn_packet.c::SN_Decode_Headerreads one byte past its caller-suppliedrx_buf/rx_buf_lenbound when the peer uses the MQTT-SNextended-length header form (
0x01 [len_hi] [len_lo]) and the decodedtotal_lenis exactly equal torx_buf_len.Why it's a bug
src/mqtt_sn_packet.c:98-125(pre-patch):With a 3-byte input
01 00 03,total_lendecodes to 3 and thetotal_len > rx_buf_lentest (3 > 3) is false, so the function proceedsto read the message-type byte at
rx_buf[3]— one byte past thecaller's stated bound.
SN_Decode_Publishalready carries the matching guard(
total_len < (word16)(rx_payload - rx_buf)atsrc/mqtt_sn_packet.c:1248),so this patch follows the same pattern.
Reproducer
Drop the program below alongside the wolfMQTT source tree and build it
straight against the sources so the bound check is exact. To make the
out-of-bounds read crash deterministically (without depending on a
sanitizer), the buffer is placed at the end of a writable page and the
following page is set
PROT_NONE:Build and run:
Before this patch:
killed by signal 10(SIGBUS — the read at offset 3crosses into the guard page).
After this patch:
child rc=-3(MQTT_CODE_ERROR_MALFORMED_DATA), no signal.Fix
Save the original
rx_bufpointer and require that the declaredtotal_lencover the bytes already consumed plus the upcoming type byte.The check matches the style of the analogous guard in
SN_Decode_Publishand is confined to
SN_Decode_Header; no caller changes are needed.Build / test
makeclean, no new warnings.tests/unit_tests: 173/173 PASS, unchanged from master.MQTT_CODE_ERROR_MALFORMED_DATAcleanly.