Skip to content

Commit

Permalink
net: dns_sd: Prevent dead code in query parsing
Browse files Browse the repository at this point in the history
The number of buffer provided was verified in the final else block of a
long validation sequence. It would never be executed though, as one of
the conditions before would always evaluate to true.

As the number of buffers provided verification appears to be significant
in this case, as the buffers are referenced during other validations,
move this check at the beginning of the sequence instead. This also
eliminates the dead-code problem.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
  • Loading branch information
rlubos authored and nashif committed Jun 3, 2023
1 parent 40312a8 commit 0ae7812
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions subsys/net/lib/dns/dns_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,12 @@ int dns_sd_query_extract(const uint8_t *query, size_t query_size, struct dns_sd_
size[i] = 0;
}

if (qlabels > N) {
NET_DBG("too few buffers to extract query: qlabels: %zu, N: %zu",
qlabels, N);
return -ENOBUFS;
}

if (qlabels < DNS_SD_MIN_LABELS) {
NET_DBG("too few labels in query %zu, DNS_SD_MIN_LABELS: %d", qlabels,
DNS_SD_MIN_LABELS);
Expand Down Expand Up @@ -1120,10 +1126,6 @@ int dns_sd_query_extract(const uint8_t *query, size_t query_size, struct dns_sd_
NET_DBG("domain '%s' is invalid", record->domain);
return -EINVAL;
}
} else if (qlabels > N) {
NET_DBG("too few buffers to extract query: qlabels: %zu, N: %zu",
qlabels, N);
return -ENOBUFS;
}

return offset;
Expand Down

0 comments on commit 0ae7812

Please sign in to comment.