Skip to content

Commit 73166bb

Browse files
jukkarkartben
authored andcommitted
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]> (cherry picked from commit 5746f61)
1 parent 11bda5e commit 73166bb

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
@@ -5,6 +5,8 @@
55
*/
66

77
#include <string.h>
8+
#include <zephyr/sys/bitarray.h>
9+
#include <zephyr/net/dns_resolve.h>
810
#include <zephyr/net_buf.h>
911

1012
#include "dns_pack.h"
@@ -367,10 +369,11 @@ int dns_unpack_response_query(struct dns_msg_t *dns_msg)
367369
int dns_copy_qname(uint8_t *buf, uint16_t *len, uint16_t size,
368370
struct dns_msg_t *dns_msg, uint16_t pos)
369371
{
372+
SYS_BITARRAY_DEFINE(visited, DNS_RESOLVER_MAX_BUF_SIZE);
370373
uint16_t msg_size = dns_msg->msg_size;
371374
uint8_t *msg = dns_msg->msg;
372375
uint16_t lb_size;
373-
int rc = -EINVAL;
376+
int rc = -EINVAL, ret, prev;
374377

375378
*len = 0U;
376379

@@ -394,6 +397,17 @@ int dns_copy_qname(uint8_t *buf, uint16_t *len, uint16_t size,
394397
/* See: RFC 1035, 4.1.4. Message compression */
395398
pos = ((msg[pos] & mask) << 8) + msg[pos + 1];
396399

400+
ret = sys_bitarray_test_and_set_bit(&visited, pos, &prev);
401+
if (ret < 0) {
402+
rc = -EINVAL;
403+
break;
404+
}
405+
406+
if (prev) {
407+
rc = -ELOOP;
408+
break;
409+
}
410+
397411
continue;
398412
}
399413

0 commit comments

Comments
 (0)