Fix potential memory leak when copying into existing SHA contexts and zero init tmpSha#9829
Merged
SparkiDev merged 11 commits intowolfSSL:masterfrom Mar 2, 2026
Merged
Conversation
dc0bb05 to
47037dd
Compare
Contributor
Author
|
Jenkins Retest This Please |
SparkiDev
requested changes
Feb 25, 2026
… zero-initialize temp GetHash contexts
…h contexts; zero HMAC dst hash before copy to prevent shared pointers
… (MD5, SHA, SHA2, SHA3, SHAKE) and add copy cleanup tests to prevent resource leaks when copying into previously-used contexts.
…xceeding stack frame limit.
… avoid exceeding stack frame limit." This reverts commit d99fe3b.
…oid stack frame overflow on small-stack builds.
8dbc097 to
70ccda7
Compare
…ore passing to Copy, which now calls Free(dst) and requires valid fields.
SparkiDev
approved these changes
Mar 2, 2026
douzzer
added a commit
to douzzer/wolfssl
that referenced
this pull request
Mar 4, 2026
douzzer
added a commit
to douzzer/wolfssl
that referenced
this pull request
Mar 4, 2026
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.
Problem:
When
WOLFSSL_HASH_KEEPis enabled, SHA contexts accumulate message data in a dynamically allocatedmsgbuffer. The Copy functions perform anXMEMCPY(dst, src, sizeof(...))which overwrites the entire destination struct, including thedst->msgpointer. If the destination context was previously used and had an allocatedmsgbuffer, that memory is leaked since the pointer is overwritten before being freed.Additionally, the GetHash functions (
wc_ShaGetHash,wc_Sha256GetHash, etc.) allocate a temporary SHA context usingWC_ALLOC_VAR_EXwhich does not zero-initialize memory. This temporary context is then passed directly to Copy as the destination. If Copy or any callback tries to freedst->msgas part of cleanup, it operates on an uninitialized garbage pointer.Example code path of potential leak:
Then after applying the free fix we would need to edit the GetHash function due to:
Fixes:
wc_Sha224Copy,wc_Sha256Copy,wc_Sha512Copy,wc_Sha384Copy) wheredst->msgbuffer was not freed beforeXMEMCPYoverwrites the destination struct with source data, losing the old pointer whenWOLFSSL_HASH_KEEPis enabledWC_ALLOC_VAR_EXtoWC_CALLOC_VAR_EXin SHA GetHash functions to zero-initialize temporary contexts before they are passed to Copy, preventing use of uninitializedmsgpointer fields