Repository navigation
Free key->pubkey digits before re-init in ecc_make_pub_ex - #12150
kaleb-himes wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the change that frees existing ECC public-key coordinates before reinitialization.
No open issues 🎉
What changed in this PR
This PR addresses a memory leak in wolfCrypt’s ECC public-key generation by freeing existing coordinate digits before reinitializing key->pubkey in ecc_make_pub_ex.
Changes:
- Clears the public key’s
x,y, andzcoordinates beforemp_init_multi(). - Guards the cleanup with
#ifndef ALT_ECC_SIZE, preserving that configuration’s existing behavior.
| File | Description |
|---|---|
wolfcrypt/src/ecc.c |
Frees existing public-key coordinate digits before reinitialization. |
Review effort: Balanced
dgarske
left a comment
There was a problem hiding this comment.
Approving: the fix is correct, and I checked it with heap math under ASan.
In the Jenkins PRB-fsanitize configuration (plain --enable-heapmath), the second wc_ecc_make_key() you added to test_wc_ecc_make_key leaks without the ecc.c change and is clean with it. That test is what lets PR CI catch a regression here: the #12023 tests that first exposed the leak need FP_ECC or HAVE_ECC_KOBLITZ, so they compile out of that build. The full intmath-sanitizer configuration (--enable-all with heap math under ASan) also passes make check with this branch.
Non-blocking notes:
- Follow-up for your pattern scan:
wc_ecc_make_pub()with a caller-suppliedpubOutstill runsmp_init_multi()on that point, so deriving twice into the same caller point leaks the same way. Arguably the caller's contract, so fine to leave out of this PR. - Background for the description: the leak predates #12023 (v5.9.4 with
--enable-all --enable-heapmathleaks fromwc_ecc_make_key()followed bywc_ecc_make_pub_ex()on the same key); #12023's tests only exposed it. - Before leaving draft: the description still has the unchecked template checklist and "May be related to F-10917, confirming".
|
retest this please |
|
|
Moved to my fork: https://github.com/kaleb-himes/wolfssl/tree/fix/ecc-make-pub-heapmath-leak. A new PR will follow from there; closing this one. |
|
Moved to #12151 |
#Fenrir:
F-10917: Free ECC key memory before reusing it
#Description:
If an app generated a key, or rebuilt its public key, more than once on the same ecc_key, the memory from the earlier run was never freed. The old private key was also left in that memory without being erased. This only happened with heap math (--enable-heapmath).
Fix: free the old public key before reusing it, and erase and free the old private key. test_wc_ecc_make_key now generates a key twice on the same object.
Tested:
Built with heap math and a memory-leak checker. Before the fix, three ECC tests leaked memory, which matches the CI failure. After the fix, the full API test suite passes with no leaks.
Built with the default math on macOS; the three tests pass.
Skoll review found nothing that needs a change.
Checklist