-
Notifications
You must be signed in to change notification settings - Fork 2.1k
sock_dns: fix out-of-bound errors #10740
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright (C) 2019 Freie Universität Berlin | ||
| * | ||
| * This file is subject to the terms and conditions of the GNU Lesser | ||
| * General Public License v2.1. See the file LICENSE in the top level | ||
| * directory for more details. | ||
| */ | ||
|
|
||
| /** | ||
| * @defgroup net_dns DNS defines | ||
| * @ingroup net | ||
| * @brief Generic DNS values | ||
| * @{ | ||
| * | ||
| * @file | ||
| * @brief Generic DNS values | ||
| * | ||
| * @author Martine Lenders <[email protected]> | ||
| */ | ||
| #ifndef NET_DNS_H | ||
| #define NET_DNS_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * @name Field lengths | ||
| * @{ | ||
| */ | ||
| #define RR_TYPE_LENGTH (2U) | ||
| #define RR_CLASS_LENGTH (2U) | ||
| #define RR_TTL_LENGTH (4U) | ||
| #define RR_RDLENGTH_LENGTH (2U) | ||
| /** @} */ | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* NET_DNS_H */ | ||
| /** @} */ |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,11 @@ | |
| * @} | ||
| */ | ||
|
|
||
| #include <arpa/inet.h> | ||
| #include <string.h> | ||
| #include <stdio.h> | ||
|
|
||
| #include "net/dns.h" | ||
| #include "net/sock/udp.h" | ||
| #include "net/sock/dns.h" | ||
|
|
||
|
|
@@ -73,55 +75,91 @@ static unsigned _get_short(uint8_t *buf) | |
| return _tmp; | ||
| } | ||
|
|
||
| static size_t _skip_hostname(uint8_t *buf) | ||
| static ssize_t _skip_hostname(const uint8_t *buf, size_t len, uint8_t *bufpos) | ||
| { | ||
| uint8_t *bufpos = buf; | ||
| const uint8_t *buflim = buf + len; | ||
| unsigned res = 0; | ||
|
|
||
| if (bufpos >= buflim) { | ||
| /* out-of-bound */ | ||
| return -EBADMSG; | ||
| } | ||
| /* handle DNS Message Compression */ | ||
| if (*bufpos >= 192) { | ||
| if ((bufpos + 2) >= buflim) { | ||
| return -EBADMSG; | ||
| } | ||
| return 2; | ||
miri64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| while(*bufpos) { | ||
| bufpos += *bufpos + 1; | ||
| while (bufpos[res]) { | ||
| res += bufpos[res] + 1; | ||
| if ((&bufpos[res]) >= buflim) { | ||
| /* out-of-bound */ | ||
| return -EBADMSG; | ||
| } | ||
| } | ||
| return (bufpos - buf + 1); | ||
| return res + 1; | ||
| } | ||
|
|
||
| static int _parse_dns_reply(uint8_t *buf, size_t len, void* addr_out, int family) | ||
| { | ||
| const uint8_t *buflim = buf + len; | ||
| sock_dns_hdr_t *hdr = (sock_dns_hdr_t*) buf; | ||
| uint8_t *bufpos = buf + sizeof(*hdr); | ||
|
|
||
miri64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /* skip all queries that are part of the reply */ | ||
| for (unsigned n = 0; n < ntohs(hdr->qdcount); n++) { | ||
| bufpos += _skip_hostname(bufpos); | ||
| bufpos += 4; /* skip type and class of query */ | ||
| ssize_t tmp = _skip_hostname(buf, len, bufpos); | ||
| if (tmp < 0) { | ||
| return tmp; | ||
| } | ||
| bufpos += tmp; | ||
| /* skip type and class of query */ | ||
| bufpos += (RR_TYPE_LENGTH + RR_CLASS_LENGTH); | ||
| } | ||
|
|
||
| for (unsigned n = 0; n < ntohs(hdr->ancount); n++) { | ||
| bufpos += _skip_hostname(bufpos); | ||
| ssize_t tmp = _skip_hostname(buf, len, bufpos); | ||
| if (tmp < 0) { | ||
| return tmp; | ||
| } | ||
miri64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bufpos += tmp; | ||
| if ((bufpos + RR_TYPE_LENGTH + RR_CLASS_LENGTH + RR_TTL_LENGTH) >= buflim) { | ||
| return -EBADMSG; | ||
| } | ||
| uint16_t _type = ntohs(_get_short(bufpos)); | ||
miri64 marked this conversation as resolved.
Show resolved
Hide resolved
miri64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bufpos += 2; | ||
| bufpos += RR_TYPE_LENGTH; | ||
| uint16_t class = ntohs(_get_short(bufpos)); | ||
| bufpos += 2; | ||
| bufpos += 4; /* skip ttl */ | ||
| bufpos += RR_CLASS_LENGTH; | ||
| bufpos += RR_TTL_LENGTH; /* skip ttl */ | ||
|
|
||
| unsigned addrlen = ntohs(_get_short(bufpos)); | ||
| bufpos += 2; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this got dropped |
||
| if ((bufpos + addrlen) > (buf + len)) { | ||
| return -EBADMSG; | ||
| } | ||
|
|
||
| /* skip unwanted answers */ | ||
| if ((class != DNS_CLASS_IN) || | ||
| ((_type == DNS_TYPE_A) && (family == AF_INET6)) || | ||
| ((_type == DNS_TYPE_AAAA) && (family == AF_INET)) || | ||
| ! ((_type == DNS_TYPE_A) || ((_type == DNS_TYPE_AAAA)) | ||
| )) { | ||
| if (addrlen > len) { | ||
| /* buffer wraps around memory space */ | ||
| return -EBADMSG; | ||
| } | ||
miri64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bufpos += addrlen; | ||
miri64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /* other out-of-bound is checked in `_skip_hostname()` at start of | ||
| * loop */ | ||
| continue; | ||
| } | ||
| if (((addrlen != INADDRSZ) && (family == AF_INET)) || | ||
| ((addrlen != IN6ADDRSZ) && (family == AF_INET6)) || | ||
| ((addrlen != IN6ADDRSZ) && (addrlen != INADDRSZ) && | ||
| (family == AF_UNSPEC))) { | ||
| return -EBADMSG; | ||
| } | ||
| bufpos += RR_RDLENGTH_LENGTH; | ||
| if ((bufpos + addrlen) >= buflim) { | ||
| return -EBADMSG; | ||
| } | ||
|
|
||
| memcpy(addr_out, bufpos, addrlen); | ||
| return addrlen; | ||
|
|
@@ -174,7 +212,6 @@ int sock_dns_query(const char *domain_name, void *addr_out, int family) | |
|
|
||
| ssize_t res = sock_udp_create(&sock_dns, NULL, &sock_dns_server, 0); | ||
| if (res) { | ||
| puts("a"); | ||
| goto out; | ||
| } | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though this dependency is also pulled in by
sock_util, I think it is cleaner to add this dependency due to https://github.com/RIOT-OS/RIOT/pull/10740/files#diff-0f2b4bcad3760716a76eab7708ea0e28R18 (it just adds the posix include path).