Skip to content

Commit

Permalink
dns: prevent memory leak when reading invalid record data
Browse files Browse the repository at this point in the history
Before this commmit, if record data was invalid and ..._read() failed,
we would `goto fail` which would call hsk_dns_rrs_uninit(). However,
uninit() only frees rr objects if the rrset has a size > 0 and only
frees that many rr objects.

By calling hsk_dns_rrs_push() BEFORE ..._read() we increment that
size value and that ensures that if there is a failure, the rr
will be freed.

The rr is already allocated and pushing its pointer into the rrset
before we write its data doesn't affect anything else.
  • Loading branch information
pinheadmz committed Mar 1, 2022
1 parent e61eb76 commit 750b316
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,10 @@ hsk_dns_msg_read(uint8_t **data, size_t *data_len, hsk_dns_msg_t *msg) {
if (!qs)
goto fail;

hsk_dns_rrs_push(&msg->qd, qs);

if (!hsk_dns_qs_read(data, data_len, &dmp, qs))
goto fail;

hsk_dns_rrs_push(&msg->qd, qs);
}

for (i = 0; i < ancount; i++) {
Expand All @@ -419,10 +419,10 @@ hsk_dns_msg_read(uint8_t **data, size_t *data_len, hsk_dns_msg_t *msg) {
if (!rr)
goto fail;

hsk_dns_rrs_push(&msg->an, rr);

if (!hsk_dns_rr_read(data, data_len, &dmp, rr))
goto fail;

hsk_dns_rrs_push(&msg->an, rr);
}

for (i = 0; i < nscount; i++) {
Expand All @@ -434,10 +434,10 @@ hsk_dns_msg_read(uint8_t **data, size_t *data_len, hsk_dns_msg_t *msg) {
if (!rr)
goto fail;

hsk_dns_rrs_push(&msg->ns, rr);

if (!hsk_dns_rr_read(data, data_len, &dmp, rr))
goto fail;

hsk_dns_rrs_push(&msg->ns, rr);
}

for (i = 0; i < arcount; i++) {
Expand All @@ -449,6 +449,8 @@ hsk_dns_msg_read(uint8_t **data, size_t *data_len, hsk_dns_msg_t *msg) {
if (!rr)
goto fail;

hsk_dns_rrs_push(&msg->ar, rr);

if (!hsk_dns_rr_read(data, data_len, &dmp, rr))
goto fail;

Expand All @@ -472,8 +474,6 @@ hsk_dns_msg_read(uint8_t **data, size_t *data_len, hsk_dns_msg_t *msg) {

continue;
}

hsk_dns_rrs_push(&msg->ar, rr);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion test/hnsd-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ test_hsk_dns_msg_write() {

printf(" TYPE:%02d %s\n",record_read_vector.type, record_read_vector.name1);

uint8_t actual[total_len];
uint8_t actual[1024]; // enough for invalid message
uint8_t *actual_ = (uint8_t *)&actual;
int written = hsk_dns_msg_write(msg, &actual_);

Expand Down

0 comments on commit 750b316

Please sign in to comment.