8000 Blank DNS names would result in ARES_ENOMEM due to bug in query cache · c-ares/c-ares@9e574af · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 9e574af

Browse files
committed
Blank DNS names would result in ARES_ENOMEM due to bug in query cache
If a blank DNS name is used, the DNS query cache would fail due to an invalid sanity check. This can be legitimate such as: adig -t SOA . This fixes that situation as well as a few other spots that were uncovered and adds a test case to validate the behavior to ensure it won't regress in the future. Fixes #858 Reported-By: Nodar Chkuaselidze (@nodech) Authored-By: Brad House (@bradh352)
1 parent 6b3fd5a commit 9e574af

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

src/lib/ares_qcache.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ static char *ares__qcache_calc_key(const ares_dns_record_t *dnsrec)
121121
name_len--;
122122
}
123123

124-
status = ares__buf_append(buf, (const unsigned char *)name, name_len);
125-
if (status != ARES_SUCCESS) {
126-
goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
124+
if (name_len > 0) {
125+
status = ares__buf_append(buf, (const unsigned char *)name, name_len);
126+
if (status != ARES_SUCCESS) {
127+
goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
128+
}
127129
}
128130
}
129131

src/lib/ares_send.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ static ares_status_t ares_apply_dns0x20(ares_channel_t *channel,
6262
}
6363

6464
len = ares_strlen(name);
65-
if (len == 0 || len >= sizeof(dns0x20name)) {
65+
if (len == 0) {
66+
return ARES_SUCCESS;
67+
}
68+
69+
if (len >= sizeof(dns0x20name)) {
6670
status = ARES_EBADNAME;
6771
goto done;
6872
}

src/lib/str/ares__buf.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,14 @@ ares_status_t ares__buf_append(ares__buf_t *buf, const unsigned char *data,
213213
{
214214
ares_status_t status;
215215

216-
if (data == NULL || data_len == 0) {
216+
if (data == NULL && data_len != 0) {
217217
return ARES_EFORMERR;
218218
}
219219

220+
if (data_len == 0) {
221+
return ARES_SUCCESS;
222+
}
223+
220224
status = ares__buf_ensure_space(buf, data_len);
221225
if (status != ARES_SUCCESS) {
222226
return status;

test/ares-test-internal.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ TEST_F(LibraryTest, ArrayMisuse) {
10211021
TEST_F(LibraryTest, BufMisuse) {
10221022
EXPECT_EQ(NULL, ares__buf_create_const(NULL, 0));
10231023
ares__buf_reclaim(NULL);
1024-
EXPECT_NE(ARES_SUCCESS, ares__buf_append(NULL, NULL, 0));
1024+
EXPECT_NE(ARES_SUCCESS, ares__buf_append(NULL, NULL, 55));
10251025
size_t len = 10;
10261026
EXPECT_EQ(NULL, ares__buf_append_start(NULL, &len));
10271027
EXPECT_EQ(NULL, ares__buf_append_start(NULL, NULL));

test/ares-test-mock.cc

+22
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,28 @@ TEST_P(MockChannelTest, SearchDomains) {
817817
EXPECT_EQ("{'www.third.gov' aliases=[] addrs=[2.3.4.5]}", ss.str());
818818
}
819819

820+
// Issue #858
821+
TEST_P(CacheQueriesTest, BlankName) {
822+
DNSPacket rsp;
823+
rsp.set_response().set_aa()
824+
.add_question(new DNSQuestion(".", T_SOA))
825+
.add_answer(new DNSSoaRR(".", 600, "a.root-servers.net", "nstld.verisign-grs.com", 123456, 3600, 3600, 3600, 3600));
826+
EXPECT_CALL(server_, OnRequest("", T_SOA))
827+
.WillOnce(SetReply(&server_, &rsp));
828+
829+
QueryResult result;
830+
ares_query_dnsrec(channel_, ".", ARES_CLASS_IN, ARES_REC_TYPE_SOA, QueryCallback, &result, NULL);
831+
Process();
832+
EXPECT_TRUE(result.done_);
833+
EXPECT_EQ(0, result.timeouts_);
834+
835+
QueryResult cacheresult;
836+
ares_query_dnsrec(channel_, ".", ARES_CLASS_IN, ARES_REC_TYPE_SOA, QueryCallback, &cacheresult, NULL);
837+
Process();
838+
EXPECT_TRUE(cacheresult.done_);
839+
EXPECT_EQ(0, cacheresult.timeouts_);
840+
}
841+
820842
// Relies on retries so is UDP-only
821843
TEST_P(MockUDPChannelTest, SearchDomainsWithResentReply) {
822844
DNSPacket nofirst;

0 commit comments

Comments
 (0)
0