Skip to content

Commit d8d3ab7

Browse files
committed
net: dns: Check recursive pointers for CNAME handling
Make sure that the CNAME handling checks recursive name pointers and fails the response if recursion is detected. See RFC 9267 ch. 2 for details. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 7dcd584 commit d8d3ab7

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

subsys/net/lib/dns/dns_pack.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <string.h>
88
#include <zephyr/net/buf.h>
9+
#include <zephyr/sys/bitarray.h>
10+
#include <zephyr/net/dns_resolve.h>
911

1012
#include "dns_pack.h"
1113

@@ -361,10 +363,11 @@ int dns_unpack_response_query(struct dns_msg_t *dns_msg)
361363
int dns_copy_qname(uint8_t *buf, uint16_t *len, uint16_t size,
362364
struct dns_msg_t *dns_msg, uint16_t pos)
363365
{
366+
SYS_BITARRAY_DEFINE(visited, DNS_RESOLVER_MAX_BUF_SIZE);
364367
uint16_t msg_size = dns_msg->msg_size;
365368
uint8_t *msg = dns_msg->msg;
366369
uint16_t lb_size;
367-
int rc = -EINVAL;
370+
int rc = -EINVAL, ret, prev;
368371

369372
*len = 0U;
370373

@@ -388,6 +391,17 @@ int dns_copy_qname(uint8_t *buf, uint16_t *len, uint16_t size,
388391
/* See: RFC 1035, 4.1.4. Message compression */
389392
pos = ((msg[pos] & mask) << 8) + msg[pos + 1];
390393

394+
ret = sys_bitarray_test_and_set_bit(&visited, pos, &prev);
395+
if (ret < 0) {
396+
rc = -EINVAL;
397+
break;
398+
}
399+
400+
if (prev) {
401+
rc = -ELOOP;
402+
break;
403+
}
404+
391405
continue;
392406
}
393407

0 commit comments

Comments
 (0)