Skip to content

Release i2s_ASN1_OCTET_STRING output with OPENSSL_free#37

Open
achamayou wants to merge 1 commit into
mainfrom
fix_keyid_openssl_free
Open

Release i2s_ASN1_OCTET_STRING output with OPENSSL_free#37
achamayou wants to merge 1 commit into
mainfrom
fix_keyid_openssl_free

Conversation

@achamayou

Copy link
Copy Markdown
Member

Summary

subject_key_id() and authority_key_id() released the string returned by i2s_ASN1_OCTET_STRING with the CRT free():

const std::unique_ptr<char, decltype(&free)> c(i2s_ASN1_OCTET_STRING(nullptr, key_id), free);
return {c.get()};

i2s_ASN1_OCTET_STRING allocates its result with OPENSSL_malloc. When OpenSSL is built against a different allocator than the CRT (notably on Windows, or with a custom CRYPTO_set_mem_functions), releasing it with free() corrupts the heap.

There is also a latent UB: if i2s_ASN1_OCTET_STRING returns nullptr, return {c.get()} constructs std::string(nullptr).

Fix

  • Release the buffer with OPENSSL_free via a unique_ptr deleter.
  • CHECKNULL the result so a nullptr becomes a thrown exception rather than UB.

Both methods are currently unused, so this is a portability/correctness hardening fix that prevents a heap-corruption bug the moment a caller is added.

Testing

  • cmake --build build — clean.
  • ctest — all tests pass.
  • clang-tidy ../didx509cpp.h — exit 0, no findings (mirrors CI).

Found during a security review of the codebase.

subject_key_id() and authority_key_id() freed the string returned by
i2s_ASN1_OCTET_STRING with the CRT free(), but OpenSSL allocates it with
OPENSSL_malloc. When OpenSSL is built against a different allocator than the
CRT (notably on Windows), mixing the two corrupts the heap. Use OPENSSL_free
via a unique_ptr deleter.

Also guard against i2s_ASN1_OCTET_STRING returning nullptr: the previous code
did 'return {c.get()}', which would construct std::string(nullptr) (UB).
CHECKNULL now turns that into a thrown exception.
@achamayou achamayou requested a review from a team as a code owner June 12, 2026 22:28
@achamayou achamayou requested a review from Copilot June 13, 2026 06:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens the didx509::X509 key identifier helpers against allocator-mismatch bugs by ensuring buffers returned from OpenSSL are freed with OpenSSL’s allocator and by converting potential nullptr returns into exceptions.

Changes:

  • Replace CRT free() with OPENSSL_free for buffers returned by i2s_ASN1_OCTET_STRING.
  • Add CHECKNULL after i2s_ASN1_OCTET_STRING to avoid std::string(nullptr) UB and provide a defined failure mode.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants