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

sys/net/nanocoap: fix UB when building hdr #20917

Merged
merged 1 commit into from
Oct 17, 2024

Conversation

maribu
Copy link
Member

@maribu maribu commented Oct 17, 2024

Contribution description

Some calls to coap_build_hdr() were done with the target buffer for the header and the source buffer for the token overlapping: They reuse the buffer that held the request to assemble the response in. We cannot use memcpy() in this case to copy the token into the target buffer, as source and destination would (fully) overlap.

This commit makes reusing the request buffer for the response a special case: memcpy() is only used to copy the token if source and destination address of the token differ.

An alternative fix would have been to use memmove() unconditionally. But memmove() does not make any assumption about the layout of target and source buffer, while we know that the token either will already be at the right position (when reusing the request buffer for the response) or be in a non-overlapping buffer (when generating a fresh token). This approach is more efficient than memmove().

Testing procedure

This did not cause any issues yet, so just testing for regressions should be sufficient. The CI should provide decent test coverage.

Issues/PRs references

Issue uncovered while debugging #20900

@maribu maribu added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Oct 17, 2024
@github-actions github-actions bot added Area: network Area: Networking Area: CoAP Area: Constrained Application Protocol implementations Area: sys Area: System labels Oct 17, 2024
Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

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

Just a typo, otherwise looks good to me, and CI will take over the (regression) testing.

sys/net/application_layer/nanocoap/nanocoap.c Outdated Show resolved Hide resolved
Some calls to `coap_build_hdr()` were done with the target buffer for
the header and the source buffer for the token overlapping:
They reuse the buffer that held the request to assemble the response in.
We cannot use `memcpy()` in this case to copy the token into the target
buffer, as source and destination would (fully) overlap.

This commit makes reusing the request buffer for the response a special
case: `memcpy()` is only used to copy the token if source and
destination address of the token differ.

An alternative fix would have been to use `memmove()` unconditionally.
But `memmove()` does not make any assumption about the layout of target
and source buffer, while we know that the token either will already be
at the right position (when reusing the request buffer for the response)
or be in a non-overlapping buffer (when generating a fresh token). This
approach is more efficient than `memmove()`.
@maribu maribu force-pushed the sys/net/nanocoap/fix-ub branch from 4f4ef64 to 835571c Compare October 17, 2024 12:02
@maribu maribu enabled auto-merge October 17, 2024 12:02
@riot-ci
Copy link

riot-ci commented Oct 17, 2024

Murdock results

✔️ PASSED

835571c sys/net/nanocoap: fix UB when building hdr

Success Failures Total Runtime
10214 0 10215 14m:08s

Artifacts

@maribu maribu added this pull request to the merge queue Oct 17, 2024
Merged via the queue into RIOT-OS:master with commit 52dd2c7 Oct 17, 2024
25 checks passed
@maribu maribu deleted the sys/net/nanocoap/fix-ub branch October 18, 2024 20:10
@benpicco benpicco added this to the Release 2024.10 milestone Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: CoAP Area: Constrained Application Protocol implementations Area: network Area: Networking Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants