Conversation
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The change is narrowly scoped, aligns with existing benchmark patterns (e.g., FrodoKEM), and directly prevents a confirmed invalid-free fault without introducing new control-flow or API behavior changes.
Review effort: Lite
Findings: None
What changed in this PR
This PR fixes a correctness issue in the ML-KEM benchmark where MlKemKey objects could be freed while still uninitialized (especially on the first iteration of bench_mlkem_keygen() and when encapsulate/decapsulate benchmarks are compiled out), which can trigger invalid frees on bare-metal/RTOS targets.
Changes:
- Zero-initialize
key1andkey2inbench_mlkem()before any benchmark helpers/free calls, ensuringwc_MlKemKey_Free()is safe even if a key was never initialized.
| File | Description |
|---|---|
| wolfcrypt/benchmark/benchmark.c | Zeroes ML-KEM key objects before first free to prevent freeing garbage pointers in constrained/embedded environments. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
bench_mlkem()passes its key objects tobench_mlkem_keygen(), which callswc_MlKemKey_Free(key)at the top of its loop to release the previous iteration's key. On the first iteration there is no previous key and the object has only been allocated - withXMALLOCunderWOLFSSL_SMALL_STACK, or left as an uninitialized stack array otherwise - sowc_MlKemKey_Free()disposes of whatever that memory happened to hold, callingXFREEonkey->priv,key->puband the PRF and hash objects. The same applies tokey2, which reaches the trailingwc_MlKemKey_Free(key2)uninitialized when the encapsulate and decapsulate benchmarks are both compiled out. On a host allocator this usually goes unnoticed; on a bare-metal target it frees a wild pointer and faults. Zero both objects before use, which is whatbench_frodokem()already does immediately above for the same reason.Validated on an NXP i.MX95 Cortex-M7 running the benchmark under Zephyr, where the fault was reproducible, and with a host build of the benchmark.