sys/net/nanocoap: fix buffer overflow in separate response handling #21075
Merged
maribu merged 5 commits intoRIOT-OS:masterfrom Dec 13, 2024
Merged
Conversation
benpicco
reviewed
Dec 11, 2024
41d8af4 to
6221061
Compare
6221061 to
7e0c535
Compare
maribu
commented
Dec 11, 2024
7e0c535 to
4c46407
Compare
4c46407 to
c391765
Compare
benpicco
approved these changes
Dec 12, 2024
Contributor
benpicco
left a comment
There was a problem hiding this comment.
Even makes then code nicer :)
C++ does not know about `restrict`, but both g++ and clang++ support `__restrict`, as do `clang` and GCC [1]. Using `__restrict` instead of `restrict` is also what glibc does. [1]: https://en.wikipedia.org/wiki/Restrict#Support_by_C++_compilers
When RFC 8974 support (module `nanocoap_token_ext`) is in use, the request token may be longer than the buffer in the separate response context is large. This adds a check to not overflow the buffer. Sadly, this is an API change: Preparing the separate response context can actually fail, so we need to report this with a return value. The example application has been adapted to only proceed if the separate reply context could have been prepared, and rather directly emit a reset message if the token exceeds the static buffer. Co-authored-by: benpicco <[email protected]>
This adds a function to convert a hex string to a byte array.
This adds the client_token shell command that allows to specify the CoAP Token. This is particularly useful to test extended length Tokens, as enabled with module `nanocoap_token_ext`. Co-authored-by: benpicco <[email protected]>
An RST message has no token, so don't reply with a token when sending RST. This also adds unit tests to ensure this this exact bug does not sneak back in.
c391765 to
c7af4b2
Compare
nandojve
approved these changes
Dec 12, 2024
Member
Author
|
Thx a lot! |
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.
Contribution description
When RFC 8974 support (module
nanocoap_token_ext) is in use, the request token may be longer than the buffer in the separate response context is large. This adds a check to not overflow the buffer.Sadly, this is an API change: Preparing the separate response context can actually fail, so we need to report this with a return value.
The example application has been adapted to only proceed if the separate reply context could have been prepared, and rather directly emit a reset message if the token exceeds the static buffer.
Testing procedure
A second commit has been sneaked in to add the
client_tokenshell command totests/net/nanocoap_clithat allow specifying the Token as hex. The most recently set token is used inclient.Preparation
Running the Server
This should be done with
masterand this PRcopy the
<IPV6_ADDR>part of the outputRunning the Client
This should be done with this PR only (or cherry-picking the second commit onto
master).Output
On
master, this will result inWith this PR, a RST message is replied and no buffer overflow / crash should appear.
Issues/PRs references
None