net/dns:fix unaligned access in dns query

This commit is contained in:
Virus.V 2021-06-10 10:02:03 +08:00 committed by Xiang Xiao
parent 3b94669b7f
commit 317bd40fec
1 changed files with 6 additions and 4 deletions

View File

@ -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; FAR struct dns_question_s *que;
uint16_t nquestions; uint16_t nquestions;
uint16_t nanswers; uint16_t nanswers;
uint16_t temp;
int naddr_read; int naddr_read;
int ret; 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 */ /* Validate query type and class */
que = (FAR struct dns_question_s *)nameptr; 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 || /* N.B. Unaligned access may occur here */
que->class != HTONS(DNS_CLASS_IN))
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"); nerr("ERROR: DNS response with wrong question\n");
return -EBADMSG; return -EBADMSG;