crypto: Fix double EVP_MAC_free bug#10859
Conversation
Cherry-picked f2acfa0 In the doc of EVP_MAC_fetch(), https://docs.openssl.org/3.0/man3/EVP_MAC "The returned value must eventually be freed with EVP_MAC_free(3)." Co-authored-by: Sverker Eriksson <sverker@erlang.org> Co-authored-by: William Yang <mscame@gmail.com>
CT Test Results 2 files 14 suites 5m 55s ⏱️ Results for commit 1ec3ed9. ♻️ This comment has been updated with latest results. To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts// Erlang/OTP Github Action Bot |
|
Note to self: Based on maint-27 to avoid merge conflicts. Do manual merge. |
There was a problem hiding this comment.
Pull request overview
Fixes an OpenSSL 3 EVP_MAC_fetch() lifetime issue in the streaming MAC NIF (crypto:mac_init/3) by ensuring fetched EVP_MAC objects are freed, preventing long-running refcount overflow that can lead to a double-free crash.
Changes:
- Cache
EVP_MAC*per MAC algorithm at NIF load time (OpenSSL 3) instead of fetching permac_initcall. - Add
fini_mac_types()to release cachedEVP_MAC*objects during NIF unload. - Minor destructor hardening by guarding the context free with braces.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/crypto/c_src/mac.h | Exposes fini_mac_types() for cleanup on unload. |
| lib/crypto/c_src/mac.c | Adds cached OpenSSL 3 EVP_MAC storage/fetch and unload-time freeing; updates mac_init_nif to reuse cached handles. |
| lib/crypto/c_src/crypto.c | Calls fini_mac_types() during unload (OpenSSL 3 path) before provider unload. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Fix #10794.
Caused by
crypto:mac_initdoingEVP_MAC_fetchwithout matchingEVP_MAC_free.An internal 32-bit signed reference counter could overflow into negative after ~2 billion calls tocrypto:mac_initand lead to a double-free crash.Already fixed in OTP 28.0 by #9119.
Cherry-picked f2acfa0.