Re: [PATCH 2/3] imap-send: don't expect an ASN1_STRING to be NUL-terminated
From: Patrick Steinhardt <hidden>
Date: 2026-09-08 08:26:47
On Mon, Sep 07, 2026 at 11:12:09PM +0200, Beat Bolli wrote:
quoted hunk ↗ jump to hunk
diff --git a/imap-send.c b/imap-send.c index 977d78005c..9a807cdde8 100644 --- a/imap-send.c +++ b/imap-send.c@@ -226,20 +226,25 @@ static int ssl_socket_connect(struct imap_socket *sock UNUSED, static int host_matches(const char *host, const ASN1_STRING *asn1_str) { - const char *pattern = (const char *)ASN1_STRING_get0_data(asn1_str); + int ret = 0; + size_t len = ASN1_STRING_get_length(asn1_str); + char *pattern = xmemdupz(ASN1_STRING_get0_data(asn1_str), len); /* embedded NUL characters may open a security hole */ - if (memchr(pattern, '\0', ASN1_STRING_get_length(asn1_str))) - return 0; + if (memchr(pattern, '\0', len)) + goto out; if (pattern[0] == '*' && pattern[1] == '.') { pattern += 2; if (!(host = strchr(host, '.'))) - return 0; + goto out; host++; } - return *host && *pattern && !strcasecmp(host, pattern); + ret = *host && *pattern && !strcasecmp(host, pattern); +out: + free(pattern); + return ret; }
I don't quite see a reason why we even have to memdup the string. We
already use memchr, which is bounded by the length of the string. We do
have two other sites though:
- We use strchr, but that can be adapted to use memchr.
- Likewise, we use strcasecmp, but that can be adapted to use
strncasecmp.
So with that, all calls that inspect the string would be bounded by the
length of the encoded string, and that means we don't have to copy the
string first, do we?
Patrick