Skip to content

Free key->pubkey digits before re-init in ecc_make_pub_ex - #12150

Closed
kaleb-himes wants to merge 2 commits into
masterfrom
fix/ecc-make-pub-heapmath-leak
Closed

kaleb-himes wants to merge 2 commits into
masterfrom
fix/ecc-make-pub-heapmath-leak

Conversation

@kaleb-himes

@kaleb-himes kaleb-himes commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

#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

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@kaleb-himes kaleb-himes self-assigned this Nov 16, 2026
Copilot AI balanced review requested due to automatic review settings November 16, 2026 22:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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, and z coordinates before mp_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 dgarske left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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-supplied pubOut still runs mp_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-heapmath leaks from wc_ecc_make_key() followed by wc_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".

@dgarske
dgarske marked this pull request as ready for review November 16, 2026 22:48
@github-actions

Copy link
Copy Markdown

retest this please

@dgarske dgarske assigned dgarske and wolfSSL-Bot and unassigned kaleb-himes Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

  • FLASH: .text +20 B (+0.0%, 71,311 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m3

  • FLASH: .text +24 B (+0.0%, 132,297 B / 262,144 B, total: 50% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +64 B (+0.0%, 197,344 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-min-ecc

  • FLASH: .text +64 B (+0.1%, 68,561 B / 262,144 B, total: 26% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +64 B (+0.0%, 805,444 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .text +64 B (+0.0%, 224,140 B / 262,144 B, total: 86% used)

gcc-arm-cortex-m4-sp-math

  • FLASH: .text +64 B (+0.1%, 68,561 B / 262,144 B, total: 26% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +64 B (+0.0%, 133,065 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +64 B (+0.0%, 251,970 B / 262,144 B, total: 96% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +64 B (+0.0%, 251,970 B / 262,144 B, total: 96% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +104 B, text +12,168 B (+4.3%, 294,744 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@kaleb-himes

Copy link
Copy Markdown
Contributor Author

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.

@kaleb-himes
kaleb-himes deleted the fix/ecc-make-pub-heapmath-leak branch November 16, 2026 23:17
@kaleb-himes

Copy link
Copy Markdown
Contributor Author

Moved to #12151

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