From 0b667ca869dd999ce1c3efc17ad13c57f47f3b28 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 5 Feb 2024 10:33:48 -0300 Subject: [PATCH] cap # responses instead of rejecting --- mongoose.c | 10 +++++++--- src/dns.c | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/mongoose.c b/mongoose.c index 6d25f19a60..b18e632fb0 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1161,12 +1161,16 @@ size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs, bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) { const struct mg_dns_header *h = (struct mg_dns_header *) buf; struct mg_dns_rr rr; - size_t i, n, ofs = sizeof(*h); + size_t i, n, num_answers, ofs = sizeof(*h); memset(dm, 0, sizeof(*dm)); if (len < sizeof(*h)) return 0; // Too small, headers dont fit if (mg_ntohs(h->num_questions) > 1) return 0; // Sanity - if (mg_ntohs(h->num_answers) > 15) return 0; // Sanity + num_answers = mg_ntohs(h->num_answers); + if (num_answers > 10) { + MG_DEBUG(("Got %u answers, ignoring beyond 10th one", num_answers)); + num_answers = 10; // Sanity cap + } dm->txnid = mg_ntohs(h->txnid); for (i = 0; i < mg_ntohs(h->num_questions); i++) { @@ -1174,7 +1178,7 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) { // MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass)); ofs += n; } - for (i = 0; i < mg_ntohs(h->num_answers); i++) { + for (i = 0; i < num_answers; i++) { if ((n = mg_dns_parse_rr(buf, len, ofs, false, &rr)) == 0) return false; // MG_INFO(("A -- %lu %lu %hu/%hu %s", ofs, n, rr.atype, rr.aclass, // dm->name)); diff --git a/src/dns.c b/src/dns.c index 0df9288180..b7c53bb10c 100644 --- a/src/dns.c +++ b/src/dns.c @@ -99,12 +99,16 @@ size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs, bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) { const struct mg_dns_header *h = (struct mg_dns_header *) buf; struct mg_dns_rr rr; - size_t i, n, ofs = sizeof(*h); + size_t i, n, num_answers, ofs = sizeof(*h); memset(dm, 0, sizeof(*dm)); if (len < sizeof(*h)) return 0; // Too small, headers dont fit if (mg_ntohs(h->num_questions) > 1) return 0; // Sanity - if (mg_ntohs(h->num_answers) > 15) return 0; // Sanity + num_answers = mg_ntohs(h->num_answers); + if (num_answers > 10) { + MG_DEBUG(("Got %u answers, ignoring beyond 10th one", num_answers)); + num_answers = 10; // Sanity cap + } dm->txnid = mg_ntohs(h->txnid); for (i = 0; i < mg_ntohs(h->num_questions); i++) { @@ -112,7 +116,7 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) { // MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass)); ofs += n; } - for (i = 0; i < mg_ntohs(h->num_answers); i++) { + for (i = 0; i < num_answers; i++) { if ((n = mg_dns_parse_rr(buf, len, ofs, false, &rr)) == 0) return false; // MG_INFO(("A -- %lu %lu %hu/%hu %s", ofs, n, rr.atype, rr.aclass, // dm->name));