Skip to content

Latent memory leaks in KEM_KEY setter functions#3041

Merged
justsmth merged 2 commits intoaws:mainfrom
justsmth:fix-mlkem-free
Mar 4, 2026
Merged

Latent memory leaks in KEM_KEY setter functions#3041
justsmth merged 2 commits intoaws:mainfrom
justsmth:fix-mlkem-free

Conversation

@justsmth
Copy link
Copy Markdown
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
Copy Markdown

codecov-commenter commented Feb 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.36%. Comparing base (37d8646) to head (cf5c4d3).
⚠️ Report is 22 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3041      +/-   ##
==========================================
- Coverage   78.37%   78.36%   -0.01%     
==========================================
  Files         689      689              
  Lines      121078   121082       +4     
  Branches    16966    16965       -1     
==========================================
- Hits        94889    94887       -2     
- Misses      25294    25299       +5     
- Partials      895      896       +1     

☔ 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
@justsmth justsmth enabled auto-merge (squash) March 4, 2026 15:17
@justsmth justsmth disabled auto-merge March 4, 2026 20:05
@justsmth justsmth merged commit e0cf5f8 into aws:main Mar 4, 2026
439 of 455 checks passed
@justsmth justsmth deleted the fix-mlkem-free branch March 4, 2026 20:06
WillChilds-Klein pushed a commit to WillChilds-Klein/aws-lc that referenced this pull request Mar 11, 2026
### 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.
nebeid pushed a commit to nebeid/aws-lc that referenced this pull request Mar 23, 2026
### 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.
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.

4 participants