Skip to content

Commit c297245

Browse files
ayuseleznevazat
ayuseleznev
authored andcommitted
evdns: Add additional validation for values of dns options
(cherry picked from commit 8fe35c7)
1 parent 141e37c commit c297245

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

evdns.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,6 +3531,7 @@ evdns_base_set_option_impl(struct evdns_base *base,
35313531
base->global_max_retransmits = retries;
35323532
} else if (str_matches_option(option, "randomize-case:")) {
35333533
int randcase = strtoint(val);
3534+
if (randcase == -1) return -1;
35343535
if (!(flags & DNS_OPTION_MISC)) return 0;
35353536
base->global_randomize_case = randcase;
35363537
} else if (str_matches_option(option, "bind-to:")) {
@@ -3554,11 +3555,13 @@ evdns_base_set_option_impl(struct evdns_base *base,
35543555
sizeof(tv));
35553556
} else if (str_matches_option(option, "so-rcvbuf:")) {
35563557
int buf = strtoint(val);
3558+
if (buf == -1) return -1;
35573559
if (!(flags & DNS_OPTION_MISC)) return 0;
35583560
log(EVDNS_LOG_DEBUG, "Setting SO_RCVBUF to %s", val);
35593561
base->so_rcvbuf = buf;
35603562
} else if (str_matches_option(option, "so-sndbuf:")) {
35613563
int buf = strtoint(val);
3564+
if (buf == -1) return -1;
35623565
if (!(flags & DNS_OPTION_MISC)) return 0;
35633566
log(EVDNS_LOG_DEBUG, "Setting SO_SNDBUF to %s", val);
35643567
base->so_sndbuf = buf;

test/regress_dns.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,71 @@ test_set_so_rcvbuf_so_sndbuf(void *arg)
23622362
evdns_base_free(dns_base, 0);
23632363
}
23642364

2365+
static void
2366+
test_set_option(void *arg)
2367+
{
2368+
#define SUCCESS 0
2369+
#define FAIL -1
2370+
struct basic_test_data *data = arg;
2371+
struct evdns_base *dns_base;
2372+
size_t i;
2373+
/* Option names are allowed to have ':' at the end.
2374+
* So all test option names come in pairs.
2375+
*/
2376+
const char *int_options[] = {
2377+
"ndots", "ndots:",
2378+
"max-timeouts", "max-timeouts:",
2379+
"max-inflight", "max-inflight:",
2380+
"attempts", "attempts:",
2381+
"randomize-case", "randomize-case:",
2382+
"so-rcvbuf", "so-rcvbuf:",
2383+
"so-sndbuf", "so-sndbuf:",
2384+
};
2385+
const char *timeval_options[] = {
2386+
"timeout", "timeout:",
2387+
"getaddrinfo-allow-skew", "getaddrinfo-allow-skew:",
2388+
"initial-probe-timeout", "initial-probe-timeout:",
2389+
};
2390+
const char *addr_port_options[] = {
2391+
"bind-to", "bind-to:",
2392+
};
2393+
2394+
dns_base = evdns_base_new(data->base, 0);
2395+
tt_assert(dns_base);
2396+
2397+
for (i = 0; i < ARRAY_SIZE(int_options); ++i) {
2398+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, int_options[i], "0"));
2399+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, int_options[i], "1"));
2400+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, int_options[i], "10000"));
2401+
tt_assert(FAIL == evdns_base_set_option(dns_base, int_options[i], "foo"));
2402+
tt_assert(FAIL == evdns_base_set_option(dns_base, int_options[i], "3.14"));
2403+
}
2404+
2405+
for (i = 0; i < ARRAY_SIZE(timeval_options); ++i) {
2406+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, timeval_options[i], "1"));
2407+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, timeval_options[i], "0.001"));
2408+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, timeval_options[i], "3.14"));
2409+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, timeval_options[i], "10000"));
2410+
tt_assert(FAIL == evdns_base_set_option(dns_base, timeval_options[i], "0"));
2411+
tt_assert(FAIL == evdns_base_set_option(dns_base, timeval_options[i], "foo"));
2412+
}
2413+
2414+
for (i = 0; i < ARRAY_SIZE(addr_port_options); ++i) {
2415+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, addr_port_options[i], "8.8.8.8:80"));
2416+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, addr_port_options[i], "1.2.3.4"));
2417+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, addr_port_options[i], "::1:82"));
2418+
tt_assert(SUCCESS == evdns_base_set_option(dns_base, addr_port_options[i], "3::4"));
2419+
tt_assert(FAIL == evdns_base_set_option(dns_base, addr_port_options[i], "3.14"));
2420+
tt_assert(FAIL == evdns_base_set_option(dns_base, addr_port_options[i], "foo"));
2421+
}
2422+
2423+
#undef SUCCESS
2424+
#undef FAIL
2425+
end:
2426+
if (dns_base)
2427+
evdns_base_free(dns_base, 0);
2428+
}
2429+
23652430
#define DNS_LEGACY(name, flags) \
23662431
{ #name, run_legacy_test_fn, flags|TT_LEGACY, &legacy_setup, \
23672432
dns_##name }
@@ -2435,6 +2500,8 @@ struct testcase_t dns_testcases[] = {
24352500

24362501
{ "set_SO_RCVBUF_SO_SNDBUF", test_set_so_rcvbuf_so_sndbuf,
24372502
TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2503+
{ "set_options", test_set_option,
2504+
TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
24382505

24392506
END_OF_TESTCASES
24402507
};

0 commit comments

Comments
 (0)