Skip to content

Fix entropy_mutex ownership tracking & re-init race - #11645

Open
stenslae wants to merge 3 commits into
wolfSSL:masterfrom
stenslae:audit-entropy-mutex
Open

stenslae wants to merge 3 commits into
wolfSSL:masterfrom
stenslae:audit-entropy-mutex

Conversation

@stenslae

@stenslae stenslae commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Description

Fixed fenrir findings for F-7091, F-7419, and F-8169.

Added boolean tracking on the mutex lock, and made it so mutex locking and stop thread are only called when the current thread successfully acquired the lock. Also changed wc_InitMutex() in Entropy_Init() with wc_local_InitMutexOnce() to prevent re-initalization of an active mutex, and added resetting the atomic entropy flag in Entropy_Final().

Checklist

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

@stenslae stenslae self-assigned this Oct 5, 2026
Copilot AI balanced review requested due to automatic review settings October 5, 2026 18:35
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4

  • FLASH: .text +64 B (-1.3%, 207,216 B / 262,144 B, total: 79% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: -2,796 B (-1.5%, 179,795 B / 262,144 B, total: 69% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .rodata -2,768 B (-0.3%, 793,436 B / 1,048,576 B, total: 76% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: -2,796 B (-1.3%, 220,820 B / 262,144 B, total: 84% used)

gcc-arm-cortex-m4-pq

  • FLASH: .rodata -2,772 B, .text +64 B (-0.9%, 309,008 B / 1,048,576 B, total: 29% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .rodata -2,768 B (-0.8%, 340,360 B / 1,048,576 B, total: 32% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +64 B (-1.1%, 246,498 B / 262,144 B, total: 94% used)

gcc-arm-cortex-m7

  • FLASH: .text +64 B (-1.3%, 207,216 B / 262,144 B, total: 79% used)

gcc-arm-cortex-m7-pq

  • FLASH: .rodata -2,772 B (-0.9%, 309,904 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m7-tls13

  • FLASH: -2,796 B (-1.1%, 246,498 B / 262,144 B, total: 94% used)

stm32-sim-stm32h753

  • FLASH: .text -2,732 B (-1.3%, 202,988 B / 2,097,152 B, total: 10% used)

No memory changes detected for:


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

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.

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 Medium severity

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.

Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated
Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

@stenslae
stenslae force-pushed the audit-entropy-mutex branch from 3479616 to 9c11a59 Compare October 5, 2026 19:39
@stenslae
stenslae requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot October 5, 2026 19:39

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

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.

Copilot review overview

🟡 Changes recommended

The new on-demand test baseline can falsely report a regression when the threaded entropy counter is stopped.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
Resolved since last review (2)

Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated
@stenslae
stenslae force-pushed the audit-entropy-mutex branch from 9c11a59 to 1b65c7a Compare October 5, 2026 20:02
@stenslae
stenslae requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot October 5, 2026 20:02

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated

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.

Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated
Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated
Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated
Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated
Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated
@stenslae
stenslae force-pushed the audit-entropy-mutex branch from 1b65c7a to eb4b2dd Compare October 7, 2026 21:32
@stenslae
stenslae requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot October 7, 2026 21:35

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.

Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c Outdated
@stenslae
stenslae force-pushed the audit-entropy-mutex branch from eb4b2dd to 52173d9 Compare October 9, 2026 19:59
@stenslae
stenslae requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot October 9, 2026 20:00

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.

🟡 Changes recommended

Initialization readiness can be published before startup completes, allowing a concurrent caller to return success prematurely.

2 open findings

🧠 Review effort: Balanced

Comment thread wolfcrypt/src/wolfentropy.c Outdated

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

@lealem47 lealem47 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.

Looks great @stenslae ! Just a couple of edge case tweaks requested

Comment thread wolfcrypt/src/wolfentropy.c Outdated
/* Dispose of the SHA3-356 hash object. */
wc_Sha3_256_Free(&entropyHash);
#if !defined(SINGLE_THREADED) && !defined(WOLFSSL_MUTEX_INITIALIZER)
wc_FreeMutex(&entropy_mutex);

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.

Can we free this mutex based on entropy_mutex_init instead of basing it on entropy_memuse_initialized to avoid leaking in the case where the mutex is successfully created but Entropy_Init() ultimately fails?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, changed it so Entropy_Final() now frees the mutex whenever entropy_mutex_init == 2, whether or not init succeeded, and resets the flag to 0 so a later Entropy_Init() re-creates it.

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.

Okay that sounds good. Can you move define ENTROPY_MUTEX_READY 2U to wc_port.h? And use that in wc_local_InitMutexOnce instead of the hard coded 2? Then we can use the same macro in Entropy_Final()

#define WC_MUTEX_READY_ONCE 2U might be a better name if we're making it global

Comment thread wolfcrypt/src/wolfentropy.c Outdated
Comment thread wolfcrypt/src/wolfentropy.c Outdated
Comment thread wolfcrypt/src/wolfentropy.c Outdated
/* MemUse entropy global state initialized. Published with a release store
* after all startup work succeeds; checked with an acquire load on the
* fast-path so a reader seeing 1 is guaranteed to see a complete init. */
static wolfSSL_Atomic_Int entropy_memuse_initialized = WOLFSSL_ATOMIC_INITIALIZER(0);

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.

MEDIUM-2: Switching entropy_memuse_initialized from `volatile int` to wolfSSL_Atomic_Int drops volatile in threaded WOLFSSL_NO_ATOMICS builds [SUGGEST] (bug)
  File:       wolfcrypt/src/wolfentropy.c:69-72
  Function:   (file scope)
  Confidence: Medium

  The flag changes from `static volatile int` to `static wolfSSL_Atomic_Int`. In every config where `WOLFSSL_ATOMIC_OPS` gets defined this is a strict improvement (seq_cst load/store, which subsumes the acquire/release the new comments describe). But when no atomic backend matches, `wolfssl/wolfcrypt/wc_port.h:645-653` falls back to `typedef int wolfSSL_Atomic_Int;` with `WOLFSSL_ATOMIC_LOAD(x) (x)` / `WOLFSSL_ATOMIC_STORE(x, val) (x) = (val)` — and that header comment is explicit that it is *not* volatile: "_NO_ATOMICS configs promise no concurrent mutation (single-threaded, or externally serialized)".

That promise does not hold here: `HAVE_ENTROPY_MEMUSE` with `!SINGLE_THREADED` is genuinely multi-threaded and the flag is the subject of the double-checked lock in `Entropy_Init()`. Such a build is narrow but real — a threaded target whose compiler matches none of the atomic arms (no `WOLFSSL_USER_DEFINED_ATOMICS`, not BSDKM, no C11 `<stdatomic.h>`, not GNUC `__ATOMIC_SEQ_CST`, not MSVC/Windows — e.g. older IAR/Keil/CC-RX with an RTOS), or any user who sets `WOLFSSL_NO_ATOMICS` in `user_settings.h`. There, the compiler is free to collapse the two `WOLFSSL_ATOMIC_LOAD(entropy_memuse_initialized)` reads in `Entropy_Init()` into one, defeating the re-check that the diff adds — something the old `volatile` prevented.

Related: the new comments say "Published with a release store ... checked with an acquire load" and "Release store: publish only after all startup work has succeeded." Those hold under `WOLFSSL_ATOMIC_OPS` but not in the fallback, where the macros carry no ordering at all.

  Code:
    -/* MemUse entropy global state initialized. */
    -static volatile int entropy_memuse_initialized = 0;
    +/* MemUse entropy global state initialized.  Published with a release store
    + * after all startup work succeeds; checked with an acquire load on the
    + * fast-path so a reader seeing 1 is guaranteed to see a complete init. */
    +static wolfSSL_Atomic_Int entropy_memuse_initialized = WOLFSSL_ATOMIC_INITIALIZER(0);

  Recommendation: Either keep `volatile` for the `WOLFSSL_NO_ATOMICS && !SINGLE_THREADED` case, or qualify the new comments to say the acquire/release guarantee applies only when `WOLFSSL_ATOMIC_OPS` is defined (matching how `wc_local_InitMutexOnce()` in wc_port.c:3052 explicitly documents and branches on that distinction). Also note the line is 84 characters, over the project's 80-column convention — wrap the initializer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Returned that back to using volatile.

@stenslae
stenslae force-pushed the audit-entropy-mutex branch from 5eb688f to 00e748a Compare October 28, 2026 22:10
@stenslae
stenslae requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot October 28, 2026 22:10
@stenslae

Copy link
Copy Markdown
Member Author

@claude review

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.

🔵 Needs a closer look

The concurrency- and FIPS-sensitive entropy and mutex lifecycle changes need maintainer runtime validation, since comment-only test updates do not verify concurrent first use, cleanup/reinitialization without static mutex initializers, or counter-thread builds.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread wolfcrypt/src/wolfentropy.c

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #11645

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, 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

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

Comment thread tests/unit-mcdc/test_wolfentropy_whitebox.c
@stenslae
stenslae requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot October 28, 2026 22:47
@stenslae

Copy link
Copy Markdown
Member Author

@claude review

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 are reported for the wolfEntropy mutex-ownership and initialization-race changes.

No open issues 🎉

Review effort: Balanced

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review October 28, 2026 22:51

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code review found no issues

No high-confidence issues detected in this change.

@stenslae stenslae assigned lealem47 and unassigned stenslae Oct 28, 2026
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.

7 participants