Skip to content

Commit ede25c1

Browse files
pfalconAnas Nashif
authored andcommitted
net: sockets: getaddrinfo() buffer overflow, etc. fixes
The existing implementation assumed DNS resolv callback will be called just once, but that's not always the case (apparently, for multi-homes hosts or something). So, apply array bounds checking (and do pointer arithmetic only after it, as the C standard otherwise warns of "undefined behavior"). In such a case, the port number wasn't set in each entry too, so rework how it's done. The issues discovered while resolving archive.ubuntu.com. Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent ce6c8f3 commit ede25c1

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

subsys/net/lib/sockets/getaddrinfo.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ struct getaddrinfo_state {
2020
const struct zsock_addrinfo *hints;
2121
struct k_sem sem;
2222
int status;
23-
int idx;
23+
u16_t idx;
24+
u16_t port;
2425
};
2526

2627
static struct zsock_addrinfo ai_arr[2];
@@ -30,7 +31,7 @@ static void dns_resolve_cb(enum dns_resolve_status status,
3031
struct dns_addrinfo *info, void *user_data)
3132
{
3233
struct getaddrinfo_state *state = user_data;
33-
struct zsock_addrinfo *ai = ai_arr + state->idx;
34+
struct zsock_addrinfo *ai;
3435
int socktype = SOCK_STREAM;
3536
int proto;
3637

@@ -45,7 +46,14 @@ static void dns_resolve_cb(enum dns_resolve_status status,
4546
return;
4647
}
4748

49+
if (state->idx >= ARRAY_SIZE(ai_arr)) {
50+
NET_DBG("getaddrinfo entries overflow");
51+
return;
52+
}
53+
54+
ai = ai_arr + state->idx;
4855
memcpy(&ai->_ai_addr, &info->ai_addr, info->ai_addrlen);
56+
net_sin(&ai->_ai_addr)->sin_port = state->port;
4957
ai->ai_addr = &ai->_ai_addr;
5058
ai->ai_addrlen = info->ai_addrlen;
5159
memcpy(&ai->_ai_canonname, &info->ai_canonname,
@@ -90,6 +98,7 @@ int zsock_getaddrinfo(const char *host, const char *service,
9098

9199
ai_state.hints = hints;
92100
ai_state.idx = 0;
101+
ai_state.port = htons(port);
93102
k_sem_init(&ai_state.sem, 0, UINT_MAX);
94103

95104
/* Link entries in advance */

0 commit comments

Comments
 (0)