Skip to content

Commit

Permalink
x509asn1: avoid freeing unallocated pointers
Browse files Browse the repository at this point in the history
When utf8asn1str fails there is no allocation returned, so freeing
the return pointer in **to is at best a no-op and at worst a double-
free bug waiting to happen. The current coding isn't hiding any such
bugs but to future proof, avoid freeing the return value pointer iff
the function failed.

Closes: curl#10087
Reviewed-by: Daniel Stenberg <daniel@haxx.se>
  • Loading branch information
danielgustafsson committed Dec 14, 2022
1 parent a20f99d commit 6b19247
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/vtls/x509asn1.c
Expand Up @@ -1349,14 +1349,15 @@ CURLcode Curl_verifyhost(struct Curl_cfilter *cf,
break;
switch(name.tag) {
case 2: /* DNS name. */
matched = 0;
len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
name.beg, name.end);
if(len > 0 && (size_t)len == strlen(dnsname))
matched = Curl_cert_hostcheck(dnsname, (size_t)len,
connssl->hostname, hostlen);
else
matched = 0;
free(dnsname);
if(len > 0) {
if(size_t)len == strlen(dnsname)
matched = Curl_cert_hostcheck(dnsname, (size_t)len,
connssl->hostname, hostlen);
free(dnsname);
}
break;

case 7: /* IP address. */
Expand Down Expand Up @@ -1406,10 +1407,8 @@ CURLcode Curl_verifyhost(struct Curl_cfilter *cf,
failf(data, "SSL: unable to obtain common name from peer certificate");
else {
len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
if(len < 0) {
free(dnsname);
if(len < 0)
return CURLE_OUT_OF_MEMORY;
}
if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
failf(data, "SSL: illegal cert name field");
else if(Curl_cert_hostcheck((const char *) dnsname,
Expand Down

0 comments on commit 6b19247

Please sign in to comment.