From 317bd40fecb88c28b2ea532c8c7108b7947e8179 Mon Sep 17 00:00:00 2001 From: "Virus.V" Date: Thu, 10 Jun 2021 10:02:03 +0800 Subject: [PATCH] net/dns:fix unaligned access in dns query --- libs/libc/netdb/lib_dnsquery.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/libc/netdb/lib_dnsquery.c b/libs/libc/netdb/lib_dnsquery.c index b24d4a1f28..97d9b3f829 100644 --- a/libs/libc/netdb/lib_dnsquery.c +++ b/libs/libc/netdb/lib_dnsquery.c @@ -336,6 +336,7 @@ static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr, FAR struct dns_question_s *que; uint16_t nquestions; uint16_t nanswers; + uint16_t temp; int naddr_read; int ret; @@ -436,11 +437,12 @@ static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr, /* Validate query type and class */ que = (FAR struct dns_question_s *)nameptr; - ninfo("Question: type=%04x, class=%04x\n", - ntohs(que->type), ntohs(que->class)); - if (que->type != qinfo->rectype || - que->class != HTONS(DNS_CLASS_IN)) + /* N.B. Unaligned access may occur here */ + + temp = HTONS(DNS_CLASS_IN); + if (memcmp(&que->type, &qinfo->rectype, sizeof(uint16_t)) != 0 || + memcmp(&que->class, &temp, sizeof(uint16_t)) != 0) { nerr("ERROR: DNS response with wrong question\n"); return -EBADMSG;