SN_Decode_Register: reject short total_len before NUL-terminator write#540
Open
dxbjavid wants to merge 1 commit into
Open
SN_Decode_Register: reject short total_len before NUL-terminator write#540dxbjavid wants to merge 1 commit into
dxbjavid wants to merge 1 commit into
Conversation
Add a bounds check ensuring total_len covers at least the bytes the fixed header consumes (length + type + topicId + packet_id) before computing the topic-name length and writing the NUL terminator. A malformed REGISTER with extended-length encoding can declare a total_len smaller than the 8 bytes the extended header itself consumes (e.g. [0x01][0x00][0x07][type][topicId][packet_id]). With the prior code the index 'total_len - (rx_payload - rx_buf)' computes to a negative value, so the NUL terminator is written before regist->topicName instead of after, and regist->topicName ends up pointing past the parsed packet at non-NUL-terminated memory. When the SN client passes this topic name into the application register callback as a C string (e.g. printed via %s or measured with strlen()), the read walks past rx_buf into adjacent memory. ASAN flags this as a stack-buffer-overflow on the strlen() scan.
|
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_Register(src/mqtt_sn_packet.c:825) writes a NUL terminatorinto the receive buffer using the offset
total_len - (rx_payload - rx_buf)and storesregist->topicName = rx_payloadwithout verifying thattotal_lenis at least the numberof bytes already consumed by the fixed portion of the header.
In the extended-length encoding the fixed header is 8 bytes:
A malformed packet such as
01 00 07 0A DE AD BE EFdeclarestotal_len = 7even though those 8 bytes have already been consumed,which the existing
total_len < 7check at line 851 does not catch(7 is not < 7). The subtraction
total_len - (rx_payload - rx_buf) = 7 - 8 = -1then runs in signedptrdiff_t, so the NUL terminator iswritten at
rx_payload[-1](one byte beforeregist->topicName)and
regist->topicNameis left pointing one byte past the parsedpacket at non-NUL-terminated memory.
SN_Decode_Registeris reached fromSN_Client_HandlePacket(src/mqtt_sn_client.c:91) when the broker / gateway pushes a REGISTER
to the client; the resulting
topicNameis then passed straight tothe application's register callback as a C string, where any
strlen/printf("%s", ...)walks past the receive buffer.SN_Decode_Publish(src/mqtt_sn_packet.c:1248) already performs theequivalent check before computing its payload length; this change
brings
SN_Decode_Registerin line.Reproducer
Build with
cmake .. -DWOLFMQTT_SN=yesand-fsanitize=address:ASAN before the fix:
After the fix the malformed input is rejected with
MQTT_CODE_ERROR_MALFORMED_DATA(-3) and ASAN reports no errors.Well-formed REGISTER packets (single-byte and extended length) still
decode unchanged.
Fix
Reject the packet with
MQTT_CODE_ERROR_MALFORMED_DATAwhentotal_lendoes not cover the bytes the fixed header already consumed,matching the existing pattern in
SN_Decode_Publish. The check livesinside the callee so callers don't need to validate the length field
themselves.
Build / test
cmake .. -DWOLFMQTT_TLS=no -DWOLFMQTT_BROKER=yes -DWOLFMQTT_SN=yesthen
make -j— clean build.stack-buffer-overflow; post-patch returns -3.
3-byte length encodings) both decode the topic name unchanged.