Skip to content

x509asn1: free the field buffer when a public key value fails to convert - #23459

Closed
1rhino2 wants to merge 1 commit into
curl:masterfrom
1rhino2:x509asn1-free-field-on-error
Closed

1rhino2 wants to merge 1 commit into
curl:masterfrom
1rhino2:x509asn1-free-field-on-error

Conversation

@1rhino2

@1rhino2 1rhino2 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

do_pubkey_field() frees its dynbuf only when ASN1tostr() succeeds, but utf8asn1str() appends one character at a time and bails with CURLE_WEIRD_SERVER_REPLY on a 4 byte code point of 0x200000 or more, so a chain cert whose public key element is a UniversalString with a bad character after some good ones leaks everything stored so far, up to the 100000 byte field cap, on every certinfo extraction. i found it on a GnuTLS build with CURLOPT_CERTINFO (the tool sets that for any -w): ASAN reports a 100016 byte direct leak from dyn_nappend < ASN1tostr < do_pubkey_field < Curl_extract_certinfo < Curl_gtls_verifyserver, which runs before verification so the chain does not even have to validate. Regression from 623c3a8 (8.6.0), the malloc based code freed the buffer on that path. The cert added to test 1651 makes the test fail without the fix and pass with it, checked on 8.23.0 with GnuTLS.

do_pubkey_field() only freed its dynbuf when ASN1tostr() succeeded.
utf8asn1str() appends one character at a time and bails out with
CURLE_WEIRD_SERVER_REPLY on a 4 byte code point of 0x200000 or more, so
a public key element encoded as a UniversalString with a bad character
after some good ones leaked everything stored so far, up to the 100000
byte field limit, once per certinfo extraction. The server picks that
encoding, and with GnuTLS the extraction runs before verification.

Regression from 623c3a8 (8.6.0), the malloc based code freed the
buffer on that path.

Add a cert with such a key to test 1651.
@github-actions github-actions Bot added the tests label Oct 17, 2026
@testclutch

Copy link
Copy Markdown

Analysis of PR #23459 at 7af45af8:

Test ../../tests/http/test_60_h3_proxy.py::TestH3Proxy::test_60_12_quic_session_resumption failed, but it has been permanently failing lately, so it's probably NOT a fault of the PR. Note that this test has failed in 2 different CI jobs (the link just goes to one of them). Note that this CI job has had a number of other flaky tests recently (4, to be specific) so it may be that this failure is rather a systemic issue with this job and not with this specific PR.

Generated by Testclutch

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

The localized cleanup fixes the leak while preserving existing behavior, and the regression case targets the failure path.

No open issues 🎉

What changed in this PR

Fixes a memory leak during certificate-info extraction when public-key field conversion fails.

Changes:

  • Frees the field buffer on both successful and failed conversions.
  • Adds a malformed-certificate regression case targeting partial conversion failure.
File Description
tests/​unit/​unit1651.c Adds a certificate fixture that triggers conversion failure after buffering data.
lib/​vtls/​x509asn1.c Makes public-key field buffer cleanup unconditional.

Review effort: Balanced

@bagder bagder closed this in 7423b3e Oct 20, 2026
@bagder

bagder commented Oct 20, 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.

5 participants