Skip to content

Commit e825f58

Browse files
authored
Merge pull request managarm#1795 from Qwinci/dns-fixes
2 parents c5a96ad + 2dbae4a commit e825f58

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

options/posix/generic/lookup.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static frg::string<MemoryAllocator> read_dns_name(char *buf, char *&it) {
5454
char code = *it++;
5555
if ((code & 0xC0) == 0xC0) {
5656
// pointer
57-
uint8_t offset = ((code & 0x3F) << 8) | *it++;
57+
uint16_t offset = (static_cast<uint8_t>(code & 0x3F) << 8) | static_cast<uint8_t>(*it++);
5858
auto offset_it = buf + offset;
5959
return res + read_dns_name(buf, offset_it);
6060
} else if (!(code & 0xC0)) {
@@ -146,7 +146,7 @@ int lookup_name_dns(struct lookup_result &buf, const char *name,
146146
return -EAI_SYSTEM;
147147
}
148148

149-
char response[256];
149+
char response[512];
150150
int num_ans = 0;
151151
int fds_ready;
152152
struct timespec start_time;
@@ -159,7 +159,7 @@ int lookup_name_dns(struct lookup_result &buf, const char *name,
159159
mlibc::panicLogger() << "mlibc: sys_clock_get() failed with error code: " << e << frg::endlog;
160160

161161
while ((fds_ready = poll(&pollfd, 1, get_poll_timeout(&start_time))) > 0) {
162-
ssize_t rlen = recvfrom(fd, response, 256, 0, nullptr, nullptr);
162+
ssize_t rlen = recvfrom(fd, response, sizeof(response), 0, nullptr, nullptr);
163163
if (rlen < 0) {
164164
mlibc::infoLogger() << "lookup_name_dns(): recvfrom() failed" << frg::endlog;
165165
return -EAI_SYSTEM;
@@ -186,16 +186,18 @@ int lookup_name_dns(struct lookup_result &buf, const char *name,
186186
struct dns_addr_buf buffer;
187187
auto dns_name = read_dns_name(response, it);
188188

189-
uint16_t rr_type = (it[0] << 8) | it[1];
190-
uint16_t rr_class = (it[2] << 8) | it[3];
191-
uint16_t rr_length = (it[8] << 8) | it[9];
189+
uint16_t rr_type = (static_cast<uint8_t>(it[0]) << 8) | static_cast<uint8_t>(it[1]);
190+
uint16_t rr_class = (static_cast<uint8_t>(it[2]) << 8) | static_cast<uint8_t>(it[3]);
191+
uint16_t rr_length = (static_cast<uint8_t>(it[8]) << 8) | static_cast<uint8_t>(it[9]);
192192
it += 10;
193193
(void)rr_class;
194194

195195
switch (rr_type) {
196196
case RECORD_A:
197-
if (family != AF_UNSPEC && family != AF_INET)
197+
if (family != AF_UNSPEC && family != AF_INET) {
198+
it += rr_length;
198199
continue;
200+
}
199201

200202
memcpy(buffer.addr, it, rr_length);
201203
it += rr_length;
@@ -204,8 +206,10 @@ int lookup_name_dns(struct lookup_result &buf, const char *name,
204206
buf.buf.push(std::move(buffer));
205207
break;
206208
case RECORD_AAAA:
207-
if (family != AF_UNSPEC && family != AF_INET6)
209+
if (family != AF_UNSPEC && family != AF_INET6) {
210+
it += rr_length;
208211
continue;
212+
}
209213

210214
memcpy(buffer.addr, it, rr_length);
211215
it += rr_length;
@@ -220,6 +224,7 @@ int lookup_name_dns(struct lookup_result &buf, const char *name,
220224
default:
221225
mlibc::infoLogger() << "lookup_name_dns: unknown rr type "
222226
<< rr_type << frg::endlog;
227+
it += rr_length;
223228
break;
224229
}
225230
}
@@ -355,12 +360,11 @@ int lookup_addr_dns(frg::span<char> name, frg::array<uint8_t, 16> &addr, int fam
355360
struct dns_addr_buf buffer;
356361
auto dns_name = read_dns_name(response, it);
357362

358-
uint16_t rr_type = (it[0] << 8) | it[1];
359-
uint16_t rr_class = (it[2] << 8) | it[3];
360-
uint16_t rr_length = (it[8] << 8) | it[9];
363+
uint16_t rr_type = (static_cast<uint8_t>(it[0]) << 8) | static_cast<uint8_t>(it[1]);
364+
uint16_t rr_class = (static_cast<uint8_t>(it[2]) << 8) | static_cast<uint8_t>(it[3]);
365+
uint16_t rr_length = (static_cast<uint8_t>(it[8]) << 8) | static_cast<uint8_t>(it[9]);
361366
it += 10;
362367
(void)rr_class;
363-
(void)rr_length;
364368

365369
(void)dns_name;
366370

@@ -376,6 +380,7 @@ int lookup_addr_dns(frg::span<char> name, frg::array<uint8_t, 16> &addr, int fam
376380
default:
377381
mlibc::infoLogger() << "lookup_addr_dns: unknown rr type "
378382
<< rr_type << frg::endlog;
383+
it += rr_length;
379384
break;
380385
}
381386
num_ans += ntohs(response_header->no_ans);

options/posix/generic/netdb.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,14 @@ struct servent *getservbyname(const char *name, const char *proto) {
597597
free(ret.s_name);
598598
ret.s_name = nullptr;
599599

600-
for (char **alias = ret.s_aliases; *alias != nullptr; alias++) {
601-
free(*alias);
602-
*alias = nullptr;
600+
if (ret.s_aliases) {
601+
for (char **alias = ret.s_aliases; *alias != nullptr; alias++) {
602+
free(*alias);
603+
*alias = nullptr;
604+
}
605+
606+
free(ret.s_aliases);
607+
ret.s_aliases = nullptr;
603608
}
604609

605610
free(ret.s_proto);
@@ -658,9 +663,14 @@ struct servent *getservbyport(int port, const char *proto) {
658663
free(ret.s_name);
659664
ret.s_name = nullptr;
660665

661-
for (char **alias = ret.s_aliases; *alias != nullptr; alias++) {
662-
free(*alias);
663-
*alias = nullptr;
666+
if (ret.s_aliases) {
667+
for (char **alias = ret.s_aliases; *alias != nullptr; alias++) {
668+
free(*alias);
669+
*alias = nullptr;
670+
}
671+
672+
free(ret.s_aliases);
673+
ret.s_aliases = nullptr;
664674
}
665675

666676
free(ret.s_proto);

0 commit comments

Comments
 (0)