Skip to content

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

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

stenslae wants to merge 2 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().

Testing

Added whitebox vectors for re-initialization cycles, mutex lock refusals, and held-mutex cleanup after internal failures

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

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 on lines +496 to +498
if (ret != WC_NO_ERR_TRACE(BAD_MUTEX_E) || wb_unlocks != 1) {
WB_NOTE("Entropy_Init left entropy_mutex held");
wb_fail = 1;

@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 on lines +1020 to +1022
/* Short circuit return -- a competing thread initialized the
* state while we were waiting. Note: threadsafe when
* WOLFSSL_MUTEX_INITIALIZER or WOLFSSL_ATOMIC_OPS is available.

@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

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed stale reviews from themself October 9, 2026 20:05

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

@stenslae stenslae changed the title Fix entropy_mutex ownership tracking, re-init race, and whitebox coverage Fix entropy_mutex ownership tracking & re-init race Oct 9, 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.

3 participants