Skip to content

Commit fbc7556

Browse files
gasbytesdanielinux
authored andcommitted
guard dns_id assignment against zero, fall back to 1 when getrandom
truncation results to zero
1 parent e4b53f9 commit fbc7556

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/test/unit/unit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ Suite *wolf_suite(void)
677677
tcase_add_test(tc_proto, test_regression_dns_rcode_error_aborts_query);
678678
tcase_add_test(tc_proto, test_regression_udp_checksum_zero_substituted_with_ffff);
679679
tcase_add_test(tc_proto, test_regression_last_ack_rejects_out_of_window_segment);
680+
tcase_add_test(tc_proto, test_regression_dns_id_never_zero);
680681

681682
tcase_add_test(tc_utils, test_transport_checksum);
682683
tcase_add_test(tc_utils, test_iphdr_set_checksum);

src/test/unit/unit_tests_proto.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4426,5 +4426,32 @@ START_TEST(test_regression_last_ack_rejects_out_of_window_segment)
44264426
}
44274427
END_TEST
44284428

4429+
/* dns_id is assigned from wolfIP_getrandom() which can truncate to 0.
4430+
* Zero is the sentinel for "no query active," so a zero dns_id breaks
4431+
* the re-entry guard, disables retransmission, and puts a predictable
4432+
* transaction ID on the wire. */
4433+
START_TEST(test_regression_dns_id_never_zero)
4434+
{
4435+
struct wolfIP s;
4436+
uint16_t id = 0;
4437+
4438+
wolfIP_init(&s);
4439+
mock_link_init(&s);
4440+
s.dns_server = 0x08080808U;
4441+
4442+
/* Force wolfIP_getrandom to return 0 */
4443+
test_rand_override_enabled = 1;
4444+
test_rand_override_value = 0;
4445+
4446+
ck_assert_int_eq(dns_send_query(&s, "example.com", &id, DNS_A), 0);
4447+
4448+
/* dns_id must never be zero even when the RNG returns zero */
4449+
ck_assert_uint_ne(s.dns_id, 0);
4450+
ck_assert_uint_ne(id, 0);
4451+
4452+
test_rand_override_enabled = 0;
4453+
}
4454+
END_TEST
4455+
44294456

44304457
/* ----------------------------------------------------------------------- */

src/wolfip.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6747,7 +6747,9 @@ static int dns_send_query(struct wolfIP *s, const char *dname, uint16_t *id,
67476747
return -1;
67486748
wolfIP_register_callback(s, s->dns_udp_sd, dns_callback, s);
67496749
}
6750-
s->dns_id = wolfIP_getrandom();
6750+
s->dns_id = (uint16_t)(wolfIP_getrandom() & 0xFFFF);
6751+
if (s->dns_id == 0)
6752+
s->dns_id = 1;
67516753
*id = s->dns_id;
67526754
memset(buf, 0, 512);
67536755
s->dns_query_type = (qtype == DNS_PTR) ? DNS_QUERY_TYPE_PTR : DNS_QUERY_TYPE_A;

0 commit comments

Comments
 (0)