Skip to content

Commit 2575ee5

Browse files
committed
DNS client: properly validate XID before accepting record
F/480
1 parent ec81ea5 commit 2575ee5

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/test/unit/unit.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7766,6 +7766,60 @@ START_TEST(test_dns_callback_short_header_ignored)
77667766
}
77677767
END_TEST
77687768

7769+
START_TEST(test_dns_callback_wrong_id_ignored)
7770+
{
7771+
struct wolfIP s;
7772+
uint8_t response[128];
7773+
int pos;
7774+
struct dns_header *hdr = (struct dns_header *)response;
7775+
struct dns_question *q;
7776+
struct dns_rr *rr;
7777+
const uint8_t ip_bytes[4] = {0x0A, 0x00, 0x00, 0x42};
7778+
7779+
wolfIP_init(&s);
7780+
mock_link_init(&s);
7781+
s.dns_server = 0x0A000001U;
7782+
s.dns_query_type = DNS_QUERY_TYPE_A;
7783+
s.dns_id = 0x1234;
7784+
s.dns_lookup_cb = test_dns_lookup_cb;
7785+
dns_lookup_calls = 0;
7786+
dns_lookup_ip = 0;
7787+
s.dns_udp_sd = wolfIP_sock_socket(&s, AF_INET, IPSTACK_SOCK_DGRAM, WI_IPPROTO_UDP);
7788+
ck_assert_int_gt(s.dns_udp_sd, 0);
7789+
7790+
memset(response, 0, sizeof(response));
7791+
hdr->id = ee16(0x4321);
7792+
hdr->flags = ee16(0x8100);
7793+
hdr->qdcount = ee16(1);
7794+
hdr->ancount = ee16(1);
7795+
pos = sizeof(struct dns_header);
7796+
response[pos++] = 7; memcpy(&response[pos], "example", 7); pos += 7;
7797+
response[pos++] = 3; memcpy(&response[pos], "com", 3); pos += 3;
7798+
response[pos++] = 0;
7799+
q = (struct dns_question *)(response + pos);
7800+
q->qtype = ee16(DNS_A);
7801+
q->qclass = ee16(1);
7802+
pos += sizeof(struct dns_question);
7803+
response[pos++] = 0xC0;
7804+
response[pos++] = (uint8_t)sizeof(struct dns_header);
7805+
rr = (struct dns_rr *)(response + pos);
7806+
rr->type = ee16(DNS_A);
7807+
rr->class = ee16(1);
7808+
rr->ttl = ee32(60);
7809+
rr->rdlength = ee16(4);
7810+
pos += sizeof(struct dns_rr);
7811+
memcpy(&response[pos], ip_bytes, sizeof(ip_bytes));
7812+
pos += sizeof(ip_bytes);
7813+
7814+
enqueue_udp_rx(&s.udpsockets[SOCKET_UNMARK(s.dns_udp_sd)], response, (uint16_t)pos, DNS_PORT);
7815+
dns_callback(s.dns_udp_sd, CB_EVENT_READABLE, &s);
7816+
ck_assert_int_eq(dns_lookup_calls, 0);
7817+
ck_assert_uint_eq(dns_lookup_ip, 0U);
7818+
ck_assert_uint_eq(s.dns_id, 0x1234);
7819+
ck_assert_int_eq(s.dns_query_type, DNS_QUERY_TYPE_A);
7820+
}
7821+
END_TEST
7822+
77697823
START_TEST(test_tcp_input_ttl_zero_sends_icmp)
77707824
{
77717825
struct wolfIP s;
@@ -18693,6 +18747,7 @@ Suite *wolf_suite(void)
1869318747
tcase_add_test(tc_utils, test_dns_callback_bad_flags);
1869418748
tcase_add_test(tc_utils, test_dns_callback_bad_name);
1869518749
tcase_add_test(tc_utils, test_dns_callback_short_header_ignored);
18750+
tcase_add_test(tc_utils, test_dns_callback_wrong_id_ignored);
1869618751
tcase_add_test(tc_utils, test_tcp_input_ttl_zero_sends_icmp);
1869718752
tcase_add_test(tc_utils, test_dns_callback_bad_rr_rdlen);
1869818753
tcase_add_test(tc_utils, test_dhcp_parse_offer_no_match);

src/wolfip.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5875,6 +5875,8 @@ void dns_callback(int dns_sd, uint16_t ev, void *arg)
58755875
}
58765876
if (dns_len < (int)sizeof(struct dns_header))
58775877
return;
5878+
if (ee16(hdr->id) != s->dns_id)
5879+
return;
58785880
/* Parse DNS response */
58795881
if ((ee16(hdr->flags) & 0x8100) == 0x8100) {
58805882
int pos = sizeof(struct dns_header);

0 commit comments

Comments
 (0)