Skip to content

Commit c8d45c9

Browse files
authored
Merge pull request #2601 from cesanta/dns
cap # responses instead of rejecting
2 parents 1a5ea93 + 0b667ca commit c8d45c9

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

mongoose.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,20 +1161,24 @@ size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,
11611161
bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
11621162
const struct mg_dns_header *h = (struct mg_dns_header *) buf;
11631163
struct mg_dns_rr rr;
1164-
size_t i, n, ofs = sizeof(*h);
1164+
size_t i, n, num_answers, ofs = sizeof(*h);
11651165
memset(dm, 0, sizeof(*dm));
11661166

11671167
if (len < sizeof(*h)) return 0; // Too small, headers dont fit
11681168
if (mg_ntohs(h->num_questions) > 1) return 0; // Sanity
1169-
if (mg_ntohs(h->num_answers) > 15) return 0; // Sanity
1169+
num_answers = mg_ntohs(h->num_answers);
1170+
if (num_answers > 10) {
1171+
MG_DEBUG(("Got %u answers, ignoring beyond 10th one", num_answers));
1172+
num_answers = 10; // Sanity cap
1173+
}
11701174
dm->txnid = mg_ntohs(h->txnid);
11711175

11721176
for (i = 0; i < mg_ntohs(h->num_questions); i++) {
11731177
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
11741178
// MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass));
11751179
ofs += n;
11761180
}
1177-
for (i = 0; i < mg_ntohs(h->num_answers); i++) {
1181+
for (i = 0; i < num_answers; i++) {
11781182
if ((n = mg_dns_parse_rr(buf, len, ofs, false, &rr)) == 0) return false;
11791183
// MG_INFO(("A -- %lu %lu %hu/%hu %s", ofs, n, rr.atype, rr.aclass,
11801184
// dm->name));

src/dns.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,24 @@ size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,
9999
bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
100100
const struct mg_dns_header *h = (struct mg_dns_header *) buf;
101101
struct mg_dns_rr rr;
102-
size_t i, n, ofs = sizeof(*h);
102+
size_t i, n, num_answers, ofs = sizeof(*h);
103103
memset(dm, 0, sizeof(*dm));
104104

105105
if (len < sizeof(*h)) return 0; // Too small, headers dont fit
106106
if (mg_ntohs(h->num_questions) > 1) return 0; // Sanity
107-
if (mg_ntohs(h->num_answers) > 15) return 0; // Sanity
107+
num_answers = mg_ntohs(h->num_answers);
108+
if (num_answers > 10) {
109+
MG_DEBUG(("Got %u answers, ignoring beyond 10th one", num_answers));
110+
num_answers = 10; // Sanity cap
111+
}
108112
dm->txnid = mg_ntohs(h->txnid);
109113

110114
for (i = 0; i < mg_ntohs(h->num_questions); i++) {
111115
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
112116
// MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass));
113117
ofs += n;
114118
}
115-
for (i = 0; i < mg_ntohs(h->num_answers); i++) {
119+
for (i = 0; i < num_answers; i++) {
116120
if ((n = mg_dns_parse_rr(buf, len, ofs, false, &rr)) == 0) return false;
117121
// MG_INFO(("A -- %lu %lu %hu/%hu %s", ofs, n, rr.atype, rr.aclass,
118122
// dm->name));

0 commit comments

Comments
 (0)