fix(massdns): guard ptrToIp against out-of-bounds read for short names - #3414
Closed
toor11 wants to merge 1 commit into
Closed
fix(massdns): guard ptrToIp against out-of-bounds read for short names#3414toor11 wants to merge 1 commit into
toor11 wants to merge 1 commit into
Conversation
DNS::Factory::ptrToIp() locates the ".in-addr.arpa" / ".ip6.arpa" suffix
with:
strcasestr(cptr + ptr.length() + 1 - sizeof(C_IPV4_PTR_DOMAIN), ...)
When the name is shorter than the suffix, that start pointer is computed
before the beginning of the name and strcasestr reads from it. The name
is the answer/CNAME owner name taken straight off the wire, and a root
label decodes to "." (length 1) since commit "Correctly handle root
domain label", so a DNS server can trigger this on a reverse-DNS lookup.
On libc++ (the default on macOS and the BSDs) the short-string buffer
sits near the start of the std::string object, so the read lands outside
it. Building the function against libc++ with -fsanitize=address shows a
stack-buffer-overflow READ for a "." name; with the length guards below,
the same input is clean. On libstdc++ the inline buffer sits high enough
in the object that the read stays inside it, so the access is inert there
but is still undefined pointer arithmetic.
Guard each strcasestr call on the name being at least as long as the
suffix it is compared against; a name shorter than the suffix cannot end
with it, so the result is unchanged. Add short-name and uppercase-suffix
cases to tests/nmap_dns_test.cc.
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.
What
DNS::Factory::ptrToIp()(inlibnetutil/massdns.cc) finds the reverse-DNS suffix by starting astrcasestrat a fixed offset from the end of the name:sizeof(".in-addr.arpa")is 14, so the search starts atcptr + len - 13. Whenlen < 13this pointer is before the start of the name, andstrcasestrreads from it. The IPv6 branch has the same issue withsizeof(".ip6.arpa")= 10 (cptr + len - 9).Reachability
The argument is the answer/CNAME owner name decoded straight off the wire —
process_result(a.name, ...)and the CNAME branch — with no length check on either path. A root label decodes to"."(length 1) since8769ab35f"Correctly handle root domain label", so a DNS server can return a short owner name and trigger this during a normal reverse-DNS lookup.Impact
Low, but real on
libc++targets. On libc++ (the default C++ library on macOS and the BSDs) the short-string buffer sits near the start of thestd::stringobject, so the computed pointer lands outside it. Building the function against libc++ with AddressSanitizer, exercised through the realstd::stringAPI:With the guards in this PR, the same inputs are clean. On libstdc++ (the default on Linux) the inline buffer sits high enough in the object that the read stays inside it, so the access is inert there — but it is still undefined pointer arithmetic.
It is a bounded (≤ 12 bytes), read-only access whose decoded value is only compared against the address that was requested and rejected on mismatch, so there is no write and no code execution.
The fix
Guard each
strcasestron the name being at least as long as the suffix it is compared against. A name shorter than the suffix cannot end with it, so results for all valid inputs are unchanged:Tests
Added short-name (including the root label
".") and uppercase-suffix cases totests/nmap_dns_test.cc.make check-nmapgoes from 43 to 53 assertions, 0 failures. (The in-tree suite builds with libstdc++, where the pre-fix read is inert, so these serve as regression guards; the libc++ ASan run above is the reproduction of the fault itself.)