fix: guard wp_kdf_free refCnt with mutex#436
Open
MarkAtwood wants to merge 1 commit into
Open
Conversation
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.
Fixes a race in wp_kdf_free where refCnt could be decremented without holding the mutex in the multi-threaded build, potentially causing double-free.
Changes:
- Adds an early-return when
wc_LockMutex(&kdf->mutex)fails. - Moves the
refCntdecrement to occur only while the mutex is held and always unlocks afterward.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
128
to
+133
| rc = wc_LockMutex(&kdf->mutex); | ||
| cnt = --kdf->refCnt; | ||
| if (rc == 0) { | ||
| wc_UnLockMutex(&kdf->mutex); | ||
| if (rc != 0) { | ||
| return; | ||
| } | ||
| cnt = --kdf->refCnt; | ||
| wc_UnLockMutex(&kdf->mutex); |
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
In
wp_kdf_free(src/wp_kdf_kmgmt.c), the reference-count decrement runs outside the mutex guard in the multi-threaded (#ifndef WP_SINGLE_THREADED) path:When
wc_LockMutexfails,--kdf->refCntis a non-atomic read-modify-write on shared state with no lock held. Two threads can race and both observecnt == 0, leading to a doubleOPENSSL_free(kdf).Fix
Return early when the lock cannot be acquired, and hold the mutex across the decrement — mirroring the already-correct
wp_mac_freein src/wp_mac_kmgmt.c:The single-threaded
#elsebranch is unchanged.Verification
Compiled the changed translation unit against a wolfSSL install + system OpenSSL headers (
gcc -c src/wp_kdf_kmgmt.c ...) — EXIT=0,wp_kdf_freeemitted as a defined text symbol.WP_SINGLE_THREADEDis not defined by default, so the fixed branch is the live one.Reported by static analysis (Fenrir finding 840).