Repository navigation
Conversation
|
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
Entropy-source synchronization changes need platform-aware human review and stronger concurrency regression coverage.
Review effort: Balanced
Findings: 2
Open (2)
What changed in this PR
Hardens wolfCrypt’s memory-based entropy source against mutex ownership and re-initialization errors.
Changes:
- Guards mutex release and counter-thread shutdown with ownership tracking.
- Uses one-time mutex initialization and resets its flag during finalization.
- Adds whitebox lifecycle and failure-path tests.
| File | Description |
|---|---|
| wolfcrypt/src/wolfentropy.c | Updates mutex ownership, initialization, and cleanup handling. |
| tests/unit-mcdc/test_wolfentropy_whitebox.c | Adds re-initialization, lock-refusal, and held-mutex cleanup checks. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11645
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Coverage: 2 of 2 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Lite
3479616 to
9c11a59
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11645
Scan targets checked: wolfssl-bugs
Unchanged since last review (not re-run): wolfcrypt-src, wolfcrypt-bugs
Coverage: 1 of 1 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Lite
9c11a59 to
1b65c7a
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11645
Scan targets checked: wolfssl-bugs
Unchanged since last review (not re-run): wolfcrypt-src, wolfcrypt-bugs
Coverage: 1 of 1 in-scope changed file(s) opened by the reviewer
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Lite
There was a problem hiding this comment.
Note
Copilot was unable to run its full agentic suite in this review.
Copilot review overview
Review effort: Lite
Findings: 5
Open (5)
wb_regressis used as the “real failure” indicator, but the name is ambiguous (it reads like… · Newwb_entropy_get_mutex()reports a skip viaWB_NOTE(...)but doesn’t set any flag that affects… · New In production code,Entropy_StartThread()/Entropy_StopThread()are invoked while holding… · New Same issue as inwb_entropy_get_mutex(): this early-return “skip” path is not reflected in the… · Newwb_regressis used as the “real failure” indicator, but the name is ambiguous (it reads like… · New
Resolved since last review (1)
1b65c7a to
eb4b2dd
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Entropy-source synchronization needs platform-specific maintainer validation, and assertion-failure reporting remains unresolved.
1 open finding
5 resolved since last review
wb_regressis used as the “real failure” indicator, but the name is ambiguous (it reads like… Same issue as inwb_entropy_get_mutex(): this early-return “skip” path is not reflected in the… In production code,Entropy_StartThread()/Entropy_StopThread()are invoked while holding…wb_entropy_get_mutex()reports a skip viaWB_NOTE(...)but doesn’t set any flag that affects…wb_regressis used as the “real failure” indicator, but the name is ambiguous (it reads like…
🧠 Review effort: Balanced
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11645
Scan targets checked: wolfssl-bugs
Unchanged since last review (not re-run): wolfcrypt-src, wolfcrypt-bugs
Coverage: 1 of 1 in-scope changed file(s) opened by the reviewer
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Lite
eb4b2dd to
52173d9
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11645
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Coverage: 2 of 2 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Lite
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11645
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs
Unchanged since last review (not re-run): wolfssl-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/wolfentropy.h
Coverage: 1 of 1 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Lite
Fenrir's latest completed scan found no issues; clearing the prior automated change request.
d374b41 to
47ab7a6
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Entropy bootstrap changes require maintainer validation with a FIPS source bundle and dynamic-mutex/counter-thread configurations to verify pre-init KATs and concurrent first use beyond this source-only review.
No open issues 🎉
Review effort: Balanced
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11645
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/wolfentropy.h
Coverage: 3 of 3 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Lite
47ab7a6 to
e031465
Compare
|
Rebased onto master. #11707 already merged part of this PR:
I've dropped both, so the only commit left is Fix Entropy_Init() re-init race. It moves mutex setup, locking and SHA-3 init into Entropy_SetupAndLock(). wc_Entropy_Get(), wc_Entropy_OnDemandTest() and Entropy_Init() all call it, and Entropy_Init() reruns the startup health test until one passes. |
|
@claude review |
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11645
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/wolfentropy.h
Coverage: 2 of 2 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Lite
| and returns its error on failure rather than retrying. | ||
| \return 0 On success | ||
| \return BAD_FUNC_ARG If entropy is NULL |
There was a problem hiding this comment.
Can we add the bits range check to the BAD_FUNC_ARG line in the docs for completeness? This is a minor item and I wouldn't hold up the PR over it, just noticed the bits range check wasn't documented.
Also I think we should mention/call out that a NULL buffer is accepted when the length is zero and why we opted to allow that case instead of rejecting it.
There was a problem hiding this comment.
Sure, added comments/docs for it.
| /* Free the mutex if created, even if SHA-3 setup failed. */ | ||
| if (WOLFSSL_ATOMIC_LOAD(entropy_mutex_init) == WC_MUTEX_READY_ONCE) { | ||
| wc_FreeMutex(&entropy_mutex); | ||
| WOLFSSL_ATOMIC_STORE(entropy_mutex_init, 0); |
There was a problem hiding this comment.
Can we name this 0 something like the ready-state got a name? WC_MUTEX_UNINITIALIZED or something similar? Not a blocker for merging, just a nice-to-have.
There was a problem hiding this comment.
Added WC_MUTEX_UNINIT_ONCE (0) next to WC_MUTEX_READY_ONCE in wc_port.h. Entropy_Final() and the entropy_mutex_init initializer use it now.
| WC_ATOMIC_UINT_ARG expected; | ||
|
|
||
| if (WOLFSSL_ATOMIC_LOAD(*flag) == 2) { | ||
| if (WOLFSSL_ATOMIC_LOAD(*flag) == WC_MUTEX_READY_ONCE) { |
There was a problem hiding this comment.
I love that we gave this ready-state a name instead of using 2, a magic number that has no meaning! Can we do the same for the uninitialized-state in wolfentropy.c too around line 1136 of this PR? (I'll leave a comment there also). NOTE: Not a blocker to PR merge, just a nice-to-have.
There was a problem hiding this comment.
Thanks! Added the state above, and I also named the in-progress state (WC_MUTEX_BUSY_ONCE, 1), so wc_local_InitMutexOnce() has no bare numbers left. ecc.c and iotsafe.c still initialize their flags with a literal 0. It's the same value, but I left them out to keep this PR on entropy and can follow up separately.
| #if !defined(SINGLE_THREADED) && !defined(WOLFSSL_MUTEX_INITIALIZER) | ||
| /* Free the mutex if created, even if SHA-3 setup failed. */ | ||
| if (WOLFSSL_ATOMIC_LOAD(entropy_mutex_init) == WC_MUTEX_READY_ONCE) { | ||
| wc_FreeMutex(&entropy_mutex); |
There was a problem hiding this comment.
There are multiple implementations for wc_FreeMutex() and most of them are capable of returning errors when the mutex is actively held or busy. I'm not sure that's possible here but I should ask the question: Should we check the return code here to make sure the wc_FreeMutex call succeeded before assuming we are good to reset the flag to the uninitialized-state so we don't end up re-initializing a live mutex later?
Something like:
if (WOLFSSL_ATOMIC_LOAD(entropy_mutex_init) == WC_MUTEX_READY_ONCE) {
int ret; /* could put at the top of the Entropy_Final function but since it's only used here, local scope to the if condition is fine too */
ret = wc_FreeMutex(&entropy_mutex);
if (ret == 0) {
WOLFSSL_ATOMIC_STORE(entropy_mutex_init, 0);
} else {
WOLFSSL_MSG("Entropy_Final: Failed to free mutex, flag left initialized");
}
}
@stenslae can you see if this is a realistic scenario (could the mutex at this point ever be held by another thread to where the free COULD fail)? If it's not then don't bother checking the return code.
If a code path exists where we could set this flag to uninitialized and then later try to re-initialize it after a failed wc_FreeMutex() call then perhaps we should consider checking the return code here and logging a failure with WOLFSSL_MSG().
There was a problem hiding this comment.
I originally followed the existing teardown pattern for lazily created global mutexes, like the cache locks in ecc.c.
No thread that holds a wolfCrypt_Init() reference can be inside an entropy call when wolfCrypt_Cleanup() calls Entropy_Final(). But in cases of misuse, since the entropy source sets itself up lazily, a thread can be seeding an RNG through wc_Entropy_Get() while another thread's wolfCrypt_Cleanup() hits zero. Ports whose free can fail on a held mutex (ThreadX, VxWorks, etc.) can then refuse the free. Since the flag was reset anyway, wc_InitMutex() could get called on a live mutex.
It's not a likely path, but since it does exist, I think it makes more sense to change to the format you suggested. It matches gRandMethodMutex in ssl_crypto.c now: on a failed free the flag stays WC_MUTEX_READY_ONCE, so the existing mutex is reused, and the failure is logged.
e031465 to
1769250
Compare


Description
Fixes a re-initialization race in Entropy_Init().
The mutex-ownership tracking from F-7091, F-7419 and F-8169 was originally part of this PR and is now in master via #11707.
Checklist