Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(crypto): mac_context_dtor should free MAC #9119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

qzhuyan
Copy link
Contributor

@qzhuyan qzhuyan commented Nov 27, 2024

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)."

This also ensures the same behaviour as in EVP_Q_mac which is used by NIF: crypto:mac/4.

note, without this fix there is no memory leak as openssl3 returns the same addr of 'mac' (for the same MAC type), the side effect is the refcnt keeps bumping but I think it still good to have this fix also consider the case where the libcrypto is used by other NIF.

update: 2nd thought, I think it may have the risk of refcnt overflow.
update: yes. it causes refcnt overflow and triggers double free.

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)."

This also ensures the same behaviour as in EVP_Q_mac which is used by
NIF: crypto:mac/4.

note, without this fix there is no memory leak as openssl3 returns
the same addr of 'mac' (for the same MAC type), the side effect is the refcnt
keeps bumping but I think it still good to have this fix also consider
the case where the libcrypto is used by other NIF.
Copy link
Contributor

github-actions bot commented Nov 27, 2024

CT Test Results

  2 files   14 suites   5m 55s ⏱️
186 tests 172 ✅  14 💤 0 ❌
459 runs  329 ✅ 130 💤 0 ❌

Results for commit 5754f78.

♻️ 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

qzhuyan added a commit to qzhuyan/emqx that referenced this pull request Nov 30, 2024
prefer to use crypto:pbkdf2_hmac to avoid refcnt overflow
in OTP26:

erlang/otp#9119
@jhogberg jhogberg added the team:VM Assigned to OTP team VM label Dec 2, 2024
@@ -498,6 +498,7 @@ struct mac_context
{
#if defined(HAS_3_0_API)
EVP_MAC_CTX *ctx;
EVP_MAC *mac;
Copy link
Contributor Author

@qzhuyan qzhuyan Dec 2, 2024

Choose a reason for hiding this comment

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

I think we should better have a global var instead for different alg.

@qzhuyan
Copy link
Contributor Author

qzhuyan commented Dec 2, 2024

also have a drity but handy tool to reproduce the issue.
https://github.com/qzhuyan/lli/blob/main/src/lli.erl#L14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team:VM Assigned to OTP team VM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants