Skip to content

Latent memory leaks in KEM_KEY setter functions#3041

Open
justsmth wants to merge 1 commit intoaws:mainfrom
justsmth:fix-mlkem-free
Open

Latent memory leaks in KEM_KEY setter functions#3041
justsmth wants to merge 1 commit intoaws:mainfrom
justsmth:fix-mlkem-free

Conversation

@justsmth
Copy link
Contributor

Description of changes:

KEM_KEY_set_raw_public_key, KEM_KEY_set_raw_secret_key, and KEM_KEY_set_raw_key overwrite key->public_key and/or key->secret_key without first freeing any existing allocation. If these functions were ever called on a KEM_KEY that already had key material set, the previous buffers would be leaked.

  • Current call sites are not affected because they always operate on freshly zero-allocated keys (via KEM_KEY_new), but the functions themselves are unsafe if reused in other contexts.

This change adds OPENSSL_free calls before each pointer is overwritten, consistent with how KEM_KEY_init already calls KEM_KEY_clear before re-initializing.

Call-outs:

This is a hardening fix. The added OPENSSL_free calls are no-ops in all current call sites (the pointers are always NULL), so there is no change in observable behavior today.

Testing:

Existing tests cover these code paths. No new tests required since the fix does not alter behavior for current callers.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.36%. Comparing base (0628190) to head (53f9b2f).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3041   +/-   ##
=======================================
  Coverage   78.35%   78.36%           
=======================================
  Files         689      689           
  Lines      121010   121014    +4     
  Branches    16992    16991    -1     
=======================================
+ Hits        94813    94827   +14     
+ Misses      25302    25292   -10     
  Partials      895      895           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@justsmth justsmth requested a review from nebeid February 25, 2026 14:50
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.

3 participants