sock_dns: Fix incorrect buffer bounds check#15345
Merged
miri64 merged 1 commit intoRIOT-OS:masterfrom Oct 31, 2020
Merged
Conversation
miri64
reviewed
Oct 30, 2020
Member
miri64
left a comment
There was a problem hiding this comment.
Change looks sensible, will test. In the mean time please apply the following style nit.
Member
|
ACK. I can reproduce the steps the testing procedures in include ../Makefile.tests_common
USEMODULE += sock_dns
USEMODULE += sock_udp
USEMODULE += gnrc_nettype_ipv6
include $(RIOTBASE)/Makefile.include |
Member
|
Please squash |
03d64d5 to
85296ce
Compare
Member
Author
|
Thank you for testing! :)
Done. |
Apart from advancing the buffer by RR_TYPE_LENGTH, RR_CLASS_LENGTH, and RR_TTL_LENGTH the code also attempts to read a two byte unsigned integer using _get_short(bufpos): unsigned addrlen = ntohs(_get_short(bufpos)); The bounds check must therefore ensure that the given buffer is large enough to contain two more bytes after advancing the buffer.
Member
Author
|
Seems to have passed on CI, is there anything else I can do in order to get this merged? |
miri64
approved these changes
Oct 31, 2020
Member
miri64
left a comment
There was a problem hiding this comment.
My ACK was missing ;-). I tested it and the fix fixes, what it is supposed to do. ACK
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
The following bounds check performed in
sock_dnsis IMHO incorrect:RIOT/sys/net/application_layer/dns/dns.c
Lines 128 to 130 in 1de1493
It does not take into account that after the
bufposis advanced byRR_TYPE_LENGTH,RR_CLASS_LENGTH, andRR_TTL_LENGTHtwo bytes are read unconditionally using the_get_shortfunction in the following line:RIOT/sys/net/application_layer/dns/dns.c
Line 137 in 1de1493
This is currently not taken into account by the bounds check, thus resulting in a potential out-of-bounds buffer access by a maximum of two bytes.
Testing procedure
The easiest way to confirm this issue is using the following application:
Attention: This application passes data directly to
_parse_dns_reply, for this reason the static keyword must be removed from the_parse_dns_replyfunction insys/net/application_layer/dns/dns.c.Afterwards, compile the application with:
And execute it with:
This will result in the following error message:
With the proposed patch applied no error is detected by ASAN. I think the
_get_shortfunction should also be renamed to_get_u16and the issue could be avoided all together by passing buffer bounds to_get_u16and checking these bounds before performing the access in this function.Issues/PRs references
The incorrect check was introduced in #10740 for fixing #10739.