fix: zeroize RSA private key before free#273
Open
MarkAtwood wants to merge 1 commit into
Open
Conversation
wolfCLU_genKey_RSA frees the RSA private-key DER (derBuf) and PEM (pemBuf) buffers without wiping them first, leaving private key material in freed heap. Every other key path in this file (Dilithium, XMSS, and the ECC/Ed25519 cleanups) zeroizes before free; RSA was the outlier. Add wolfCLU_ForceZero before XFREE at both the fall-through free (when also writing the public key) and the final cleanup, guarded on size > 0 because derBufSz/pemBufSz can be negative on an error break and wolfCLU_ForceZero takes an unsigned length. Build-verified: patched src/genkey/clu_genkey.c compiles clean against an --enable-all wolfSSL install; wolfCLU_ForceZero now referenced from the RSA TU. Reported by static analysis (Fenrir finding 666).
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.
This PR hardens RSA key generation cleanup by zeroizing RSA private-key buffers before freeing them, aligning RSA behavior with other key types in the same file.
Changes:
- Add
wolfCLU_ForceZero()calls beforeXFREE()forderBufandpemBufin theflagOutputPub == 1fall-through cleanup. - Add
wolfCLU_ForceZero()calls beforeXFREE()forderBufandpemBufin the final cleanup block. - Guard zeroization on
*_BufSz > 0to avoid unsigned-length issues on error paths.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+924
to
930
| if (derBufSz > 0) | ||
| wolfCLU_ForceZero(derBuf, derBufSz); | ||
| XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); | ||
| derBuf = NULL; | ||
| if (pemBufSz > 0) | ||
| wolfCLU_ForceZero(pemBuf, pemBufSz); | ||
| XFREE(pemBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); |
Comment on lines
+1021
to
+1022
| if (derBufSz > 0) | ||
| wolfCLU_ForceZero(derBuf, derBufSz); |
Comment on lines
+1026
to
+1027
| if (pemBufSz > 0) | ||
| wolfCLU_ForceZero(pemBuf, pemBufSz); |
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.
Bug
wolfCLU_genKey_RSA(src/genkey/clu_genkey.c) frees the RSA private-key DER buffer (derBuf) and PEM buffer (pemBuf) with bareXFREE, leaving private key material in freed heap. There are ZEROwolfCLU_ForceZerocalls anywhere in the RSA function, while every other key path in the same file (Dilithium, XMSS, ECC/Ed25519 cleanups — 30+ ForceZero sites) wipes before free. RSA was the outlier.Two sites are affected, both holding the just-written private key:
flagOutputPub == 1(also emitting the public key)Fix
Add
wolfCLU_ForceZerobefore eachXFREE, matching the in-file Dilithium precedent. Guarded onsize > 0becausederBufSz/pemBufSzcan be negative on an error break (e.g. a failedwc_RsaKeyToDer) andwolfCLU_ForceZerotakes anunsigned intlength — a bare call would overwrite past the allocation.wolfCLU_ForceZerois already declared in wolfclu/clu_header_main.h and defined in src/tools/clu_funcs.c; no new include. Both hunks stay inside the existing#ifndef NO_RSAregion.Verification
Patched
src/genkey/clu_genkey.ccompiles clean (gcc -c) against an--enable-allwolfSSL install;nmshowswolfCLU_ForceZeronow referenced from the RSA TU andwolfCLU_genKey_RSAdefined.Reported by static analysis (Fenrir finding 666).