Repository navigation
fix(mqtt): prevent stack buffer overrun when parsing command topics - #3256
Open
mathieucarbou wants to merge 1 commit into
Open
mathieucarbou wants to merge 1 commit into
mathieucarbou wants to merge 1 commit into
Conversation
Contributor
Author
|
Note: I have included this fix in my dev branch and runs with it today. |
Contributor
Author
|
@tbnobody Will this fix be included in next release ? |
Owner
|
Thank you. Will include it in the next release |
onMqttMessage() copied the received MQTT topic into a fixed 72-byte stack buffer (MQTT_MAX_TOPIC_STRLEN + 40) using strncpy() without guaranteeing NUL termination. The subscribed command topics follow the pattern <prefix>/<serial>/cmd/<command>, where the longest command subtopic is 'limit_nonpersistent_relative' (27 chars). With the maximum allowed prefix (32 chars) the fully qualified topic is up to 77 characters and therefore longer than the buffer. strncpy() then left the buffer unterminated, and the subsequent strlen(), strtok_r() and strtoull() read past its end, corrupting the stack of the mqttclient task. Fix this by sizing the copy from the actual topic length, guaranteeing NUL termination. Also make sure the configured prefix is part of the received topic before indexing past it.
mathieucarbou
force-pushed
the
fix/mqtt_stack_overrun
branch
from
November 11, 2026 16:51
61042a6 to
516070e
Compare
Contributor
Author
|
Note: commit added in latest OpenDTU Zero-Export release v26.11.11-ze1 rebased on latest OpenDTU version v26.11.11 |
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.
onMqttMessage() copied the received MQTT topic into a fixed 72-byte stack buffer (MQTT_MAX_TOPIC_STRLEN + 40) using strncpy() without guaranteeing NUL termination.
The subscribed command topics follow the pattern
//cmd/, where the longest command subtopic is 'limit_nonpersistent_relative' (27 chars). With the maximum allowed prefix (32 chars) the fully qualified topic is up to 77 characters and therefore longer than the buffer. strncpy() then left the buffer unterminated, and the subsequent strlen(), strtok_r() and strtoull() read past its end, corrupting the stack of the mqttclient task.
Fix this by sizing the copy from the actual topic length, guaranteeing NUL termination. Also make sure the configured prefix is part of the received topic before indexing past it.