Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nanocoap: defuse footgun in coap_get_uri_query() #20195

Merged
merged 3 commits into from
Dec 19, 2023

Conversation

benpicco
Copy link
Contributor

@benpicco benpicco commented Dec 19, 2023

Contribution description

coap_get_uri_query() takes no size parameter and assumes the buffer supplied by the caller will be sufficiently large.

This is a huge foot-gun and we can actually supply the max length to the underlying coap_opt_get_string() function, so just do that.

Also that function is not very useful. It's nice for debugging, but when actually wanting to obtain the query parameters, we certainly don't want to first convert them into a string only to then parse the string. Rename the function to coap_get_uri_query_string() to better reflect what it does and to make the name available for a more useful query iterator function.

Testing procedure

The tests-nanocoap test in tests/unittests still works.

Issues/PRs references

includes #20195 because I don't like merge conflicts.

@benpicco benpicco requested a review from miri64 as a code owner December 19, 2023 13:06
@github-actions github-actions bot added Area: network Area: Networking Area: tests Area: tests and testing framework Area: sys Area: System labels Dec 19, 2023
@benpicco benpicco added Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Dec 19, 2023
@riot-ci
Copy link

riot-ci commented Dec 19, 2023

Murdock results

✔️ PASSED

d6ca03a tests/unittests: coap_get_uri_query() -> coap_get_uri_query_string()

Success Failures Total Runtime
8098 0 8098 10m:45s

Artifacts

* @returns nr of bytes written to @p target (including '\0')
*/
static inline ssize_t coap_get_uri_query(coap_pkt_t *pkt, uint8_t *target)
static inline ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this to char * is also a huge improvement. Since callers have to change the function call anyway, I agree that this quality of life improvement can also be added just as well.

@benpicco benpicco added this pull request to the merge queue Dec 19, 2023
Copy link
Member

@chrysn chrysn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really agree with this being a footgun (it's C; you pass a mutable pointer, you better know how large a buffer the callee expexte there, or you pass it as uint_t target[CONFIG_NANOCOAP_URIMAX], which would be compatible), but I have no objections to making that more explicit either.

@@ -847,18 +847,18 @@ static inline ssize_t coap_get_uri_path(coap_pkt_t *pkt, uint8_t *target)
* This function decodes the pkt's URI_QUERY option into a "&"-separated and
* '\0'-terminated string.
*
* Caller must ensure @p target can hold at least CONFIG_NANOCOAP_URI_MAX bytes!
*
* @param[in] pkt pkt to work on
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param[in] pkt pkt to work on
* @note This function is mainly intended to get easy debug output.
* It is needlessly complex and error prone for parsing parameters out of the query,
* which is better done by @ref coap_opt_get_opaque with increasing values of @p opt_num,
* until that returns `-ENOENT`.
* @param[in] pkt pkt to work on

Part of the footgun (IMO the bigger footgun even) is that this function drives people toward assembling and later parsing this string, which inevitably leads to trouble once the first & appears in a query parameter (and if that were fixed, the string would go through percent encoding and then back again).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that -- we'll still need such a note, but #20197 adds something that would be pointed to instead.

Merged via the queue into RIOT-OS:master with commit 30f8971 Dec 19, 2023
30 checks passed
@benpicco benpicco deleted the coap_get_uri_query-defuse branch December 19, 2023 16:06
@MrKevinWeiss MrKevinWeiss added this to the Release 2024.01 milestone Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: network Area: Networking Area: sys Area: System Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants