Skip to content

vtls: fix Curl_cert_hostcheck when the pattern is not a C string - #22795

Closed
davidben wants to merge 3 commits into
curl:masterfrom
davidben:hostcheck-out-of-bounds
Closed

davidben wants to merge 3 commits into
curl:masterfrom
davidben:hostcheck-out-of-bounds

Conversation

@davidben

@davidben davidben commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

#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.

curl#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.
@github-actions github-actions Bot added the tests label Sep 2, 2026
We're not quite sure if it's 1999 yet.

@bagder bagder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lovely!

@bagder bagder closed this in 717ebe4 Sep 2, 2026
@bagder

bagder commented Sep 2, 2026

Copy link
Copy Markdown
Member

Thanks!

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