From 15e1bdfb796d1e673f8c107523461cdea6526c6c Mon Sep 17 00:00:00 2001 From: Matthew Cather Date: Fri, 26 Sep 2025 12:37:56 -0500 Subject: [PATCH] dns: check there is room in answer packet `dns_packet_record_add` will return `NULL` if there is not enough room in the packet. This needs to be check to avoid crashes. Signed-off-by: Matthew Cather --- dns.c | 8 +++++++- dns.h | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/dns.c b/dns.c index e2db851..9869c24 100644 --- a/dns.c +++ b/dns.c @@ -150,13 +150,18 @@ bool dns_packet_question(const char *name, int type) return true; } -void dns_packet_answer(const char *name, int type, const uint8_t *rdata, uint16_t rdlength, int ttl) +bool dns_packet_answer(const char *name, int type, const uint8_t *rdata, uint16_t rdlength, int ttl) { struct dns_answer *a; pkt.h.flags |= cpu_to_be16(0x8400); a = dns_packet_record_add(sizeof(*a) + rdlength, name); + if (!a) { + DBG(0, "Not enough room for answer %d\n", be16_to_cpu(pkt.h.answers)+1); + return false; + } + memset(a, 0, sizeof(*a)); a->type = cpu_to_be16(type); a->class = cpu_to_be16(1); @@ -166,6 +171,7 @@ void dns_packet_answer(const char *name, int type, const uint8_t *rdata, uint16_ DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), name); pkt.h.answers += cpu_to_be16(1); + return true; } static void dns_question_set_multicast(struct dns_question *q, bool val) diff --git a/dns.h b/dns.h index fde049f..f7e6e0c 100644 --- a/dns.h +++ b/dns.h @@ -75,7 +75,7 @@ extern int cfg_no_subnet; void dns_packet_init(void); bool dns_packet_question(const char *name, int type); -void dns_packet_answer(const char *name, int type, const uint8_t *rdata, uint16_t rdlength, int ttl); +bool dns_packet_answer(const char *name, int type, const uint8_t *rdata, uint16_t rdlength, int ttl); void dns_packet_send(struct interface *iface, struct sockaddr *to, bool query, int multicast); void dns_query(const char *name, uint16_t type);