Skip to content

Commit 2e67d65

Browse files
AasthaGuptaBethGriggs
authored andcommitted
src: fix freeing unintialized pointer bug in ParseSoaReply
ares_expand_name doesn't guarantee that pointer variable is initialized if return code is ARES_EBADNAME or ARES_ENOMEM. But current usage of the function in the codebase thinks otherwise. There seems to be an assumption that pointer is always initialized even though it is a local variable and we create a unique pointer soon after calling ares_expand_name. This could potentially crash the program with an invalid free pointer. I was able to crash it by poisoning the memory and some manual hooks. By moving the unique_ptr after checking the return code we can fix the problem. As the underlying function guarantees that pointer is initialized when the status is ARES_SUCCESS. PR-URL: #35502 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 93f947a commit 2e67d65

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/cares_wrap.cc

+10-8
Original file line numberDiff line numberDiff line change
@@ -1066,29 +1066,31 @@ int ParseSoaReply(Environment* env,
10661066
// Can't use ares_parse_soa_reply() here which can only parse single record
10671067
const unsigned int ancount = cares_get_16bit(buf + 6);
10681068
unsigned char* ptr = buf + NS_HFIXEDSZ;
1069-
char* name_temp;
1069+
char* name_temp = nullptr;
10701070
long temp_len; // NOLINT(runtime/int)
10711071
int status = ares_expand_name(ptr, buf, len, &name_temp, &temp_len);
1072-
const ares_unique_ptr name(name_temp);
10731072
if (status != ARES_SUCCESS) {
10741073
// returns EBADRESP in case of invalid input
10751074
return status == ARES_EBADNAME ? ARES_EBADRESP : status;
10761075
}
10771076

1077+
const ares_unique_ptr name(name_temp);
1078+
10781079
if (ptr + temp_len + NS_QFIXEDSZ > buf + len) {
10791080
return ARES_EBADRESP;
10801081
}
10811082
ptr += temp_len + NS_QFIXEDSZ;
10821083

10831084
for (unsigned int i = 0; i < ancount; i++) {
1084-
char* rr_name_temp;
1085+
char* rr_name_temp = nullptr;
10851086
long rr_temp_len; // NOLINT(runtime/int)
10861087
int status2 = ares_expand_name(ptr, buf, len, &rr_name_temp, &rr_temp_len);
1087-
const ares_unique_ptr rr_name(rr_name_temp);
10881088

10891089
if (status2 != ARES_SUCCESS)
10901090
return status2 == ARES_EBADNAME ? ARES_EBADRESP : status2;
10911091

1092+
const ares_unique_ptr rr_name(rr_name_temp);
1093+
10921094
ptr += rr_temp_len;
10931095
if (ptr + NS_RRFIXEDSZ > buf + len) {
10941096
return ARES_EBADRESP;
@@ -1100,27 +1102,27 @@ int ParseSoaReply(Environment* env,
11001102

11011103
// only need SOA
11021104
if (rr_type == ns_t_soa) {
1103-
char* nsname_temp;
1105+
char* nsname_temp = nullptr;
11041106
long nsname_temp_len; // NOLINT(runtime/int)
11051107

11061108
int status3 = ares_expand_name(ptr, buf, len,
11071109
&nsname_temp,
11081110
&nsname_temp_len);
1109-
const ares_unique_ptr nsname(nsname_temp);
11101111
if (status3 != ARES_SUCCESS) {
11111112
return status3 == ARES_EBADNAME ? ARES_EBADRESP : status3;
11121113
}
1114+
const ares_unique_ptr nsname(nsname_temp);
11131115
ptr += nsname_temp_len;
11141116

1115-
char* hostmaster_temp;
1117+
char* hostmaster_temp = nullptr;
11161118
long hostmaster_temp_len; // NOLINT(runtime/int)
11171119
int status4 = ares_expand_name(ptr, buf, len,
11181120
&hostmaster_temp,
11191121
&hostmaster_temp_len);
1120-
const ares_unique_ptr hostmaster(hostmaster_temp);
11211122
if (status4 != ARES_SUCCESS) {
11221123
return status4 == ARES_EBADNAME ? ARES_EBADRESP : status4;
11231124
}
1125+
const ares_unique_ptr hostmaster(hostmaster_temp);
11241126
ptr += hostmaster_temp_len;
11251127

11261128
if (ptr + 5 * 4 > buf + len) {

0 commit comments

Comments
 (0)