Skip to content

openssl: avoid strlen() on the data from OpenSSL - #22767

Closed
bagder wants to merge 1 commit into
masterfrom
bagder/openssl-iastring
Closed

bagder wants to merge 1 commit into
masterfrom
bagder/openssl-iastring

Conversation

@bagder

@bagder bagder commented Aug 31, 2026

Copy link
Copy Markdown
Member

It is not guaranteed to be null terminated. Use memchr() instead.

Reported-by: Max Dymond

It is not guaranteed to be null terminated. Use memchr() instead.

Reported-by: Max Dymond

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Pull request overview

Updates the OpenSSL backend’s hostname verification to avoid calling strlen() on certificate name data returned by OpenSSL APIs, since it is not guaranteed to be NUL-terminated.

Changes:

  • Replaces altlen == strlen(altptr) with an in-bounds memchr(altptr, '\0', altlen) check when verifying subjectAltName DNS entries.
  • Keeps the embedded-NUL rejection behavior while avoiding out-of-bounds reads on non-terminated ASN.1 string data.
File summaries
File Description
lib/vtls/openssl.c Avoids strlen() on SAN data from OpenSSL by using memchr() within the provided length.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@bagder
bagder marked this pull request as ready for review August 31, 2026 20:05
@bagder bagder closed this in 543adc4 Aug 31, 2026
@bagder
bagder deleted the bagder/openssl-iastring branch August 31, 2026 20:29
bagder pushed a commit that referenced this pull request Sep 2, 2026
#22767 removed some of the strlen
calls, but it wasn't quite sufficient. Curl_cert_hostcheck has a number
of places where it implicitly assumes the buffer is zero-terminated:

- The check for empty strings assume that, even if the buffer is
  zero-length, it can be dereferenced. This only works for C strings
  where there's always a zero byte in there. Replace this with a length
  check.

- The strncmp("*.") call will read past the buffer if the pattern equals
  "*". This works for C strings because it will stop at the zero byte
  first. For general buffers, this needs a length check first.

The various checks for the first and last character are actually OK,
because the caller rejects empty strings first, but I similarly guarded
them for completeness. Likewise, I guarded the accesses of hostname in
hopes it can eventually be de-C-string-ified, but the call to
Curl_host_is_ipnum requires it to be a C string.

Update unit1397 to cover these cases. Run each test with a
separate-allocated buffer. The two fixed invalid accesses are then
caught by ASan and valgrind.

Closes #22795
bagder pushed a commit that referenced this pull request Sep 2, 2026
The code removed from #22767 was part
of a mess from when cURL once that strings in certificates were C
strings and did not have embedded zeros. See these historical commits:

* c0e8bed
* 0b66efa
* 781b82b

Since it seems there was never a test for any of this, fill in some of
these missing tests now.

(I was working on a fix for the same strlen issue but never got around
to uploading it. Since #22767 has since landed, I figure I may as well
contribute the tests.)

Closes #22794
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants