← ALL ADVISORIES

MEDIUM BYTERAY-2026-0354Disclosed

wolfSSL — Double Initialization Race on the Per-Algorithm Hardware Crypto Mutexes

With WOLFSSL_ALGO_HW_MUTEX, each per-algorithm hardware mutex is created on first lock, guarded by a plain int that is tested and set without synchronization. Two tasks that first use the same algorithm together both create the mutex, which leaks one and can let both into the protected section.

Advisory
BYTERAY-2026-0354
CWE
CWE-412
CVSS
5.1 (CVSS:3.1/AV:L/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:H)
Vendor
wolfSSL
Product
wolfSSL
Reported
2026-09-28
Disclosure
2026-12-03
Affected
wolfSSL 5.9.2 through 5.9.4, multi-threaded builds with WOLFSSL_ALGO_HW_MUTEX on a port that enables WOLFSSL_CRYPT_HW_MUTEX (in tree: the MAX3266X port)
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

wolfCrypt can protect a hardware crypto engine with one mutex per algorithm class (RNG, AES, hash, public key) instead of a single global one. The option is WOLFSSL_ALGO_HW_MUTEX. Each of these mutexes is created lazily: every lock call first runs hwAlgoMutexInit(), which tests a plain static int and, if it is zero, calls wc_InitMutex() and then sets it.

The test and the store are not atomic and nothing serializes them. Two tasks that take the same per-algorithm lock for the first time at the same moment both see zero and both initialize the same mutex object. Unlike the global hardware mutex, these mutexes are not created by wolfCrypt_Init(); no code in the tree calls their init functions, so the first lock is always the lazy path.

What a second initialization does depends on the RTOS. On FreeRTOS it creates a second semaphore and overwrites the handle. The first semaphore is leaked, and the two tasks can each hold a different one and use the hardware at the same time. The window exists once per mutex per boot. No attacker input is involved, and the option is off by default.

Affected Products and Versions

  • Component: wolfCrypt port layer, hwAlgoMutexInit() and hwAlgoMutexLock() in wolfcrypt/src/wc_port.c.
  • Required build: WOLFSSL_ALGO_HW_MUTEX defined by the integrator (documented as default off at wolfcrypt/src/wc_port.c:47), WOLFSSL_CRYPT_HW_MUTEX set to 1, and a multi-threaded mutex type without WOLFSSL_MUTEX_INITIALIZER.
  • In the tested tree only the MAX3266X port uses the per-algorithm lock functions. It enables the hardware mutex (wolfssl/wolfcrypt/wc_port.h:1020-1029) and, with the option, keeps a separate mutex for the RNG only (wolfssl/wolfcrypt/port/maxim/max3266x.h:73-81).
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • The v5.9.2-stable and v5.9.4-stable tags contain the same function with the same flags.
  • Not fixed on wolfSSL master as of commit e4b0841b32e7 (2026-12-03).
  • A default ./configure build does not include this code. Earlier releases were not checked.

Technical Details

The mutex objects and their flags are file-scope statics. The flags exist only when the mutex type has no static initializer (wolfcrypt/src/wc_port.c:2744-2757), for example:

static int wcCryptHwRngMutexInit = 0;

hwAlgoMutexInit() looks up the mutex and its flag and then does this, at wolfcrypt/src/wc_port.c:2809-2814:

    if (*wcHwAlgoInitPtr == 0) {
        ret = wc_InitMutex(wcHwAlgoMutexPtr);
        if (ret == 0) {
            *wcHwAlgoInitPtr = 1;
        }
    }

hwAlgoMutexLock() calls it immediately before locking (wolfcrypt/src/wc_port.c:2829-2831). The wrappers wolfSSL_HwRngMutexLock() and its AES, hash and public-key counterparts go straight to that function (wolfcrypt/src/wc_port.c:2854-2865). The matching wolfSSL_Hw*MutexInit() wrappers have no caller in the tested tree, and wolfCrypt_Init() initializes only the global hardware mutex (wolfcrypt/src/wc_port.c:877-884).

The pthread mutex type defines WOLFSSL_MUTEX_INITIALIZER (wolfssl/wolfcrypt/wc_port.h:452-457), and the flags are then not declared. The lookup helper refers to them unconditionally (wolfcrypt/src/wc_port.c:2769-2773), so the option builds only on mutex types that lack the initializer. Every build that has the option has the lazy path.

The sequence:

  1. Task A calls a per-algorithm lock for the first time, reads the flag as 0 at line 2809 and enters wc_InitMutex().
  2. Task B makes the same call and reads the flag as 0 before A reaches line 2812.
  3. Both calls initialize the same wolfSSL_Mutex object. Each then sets the flag and locks.

On FreeRTOS, wc_InitMutex() assigns a newly created semaphore to the handle (wolfcrypt/src/wc_port.c:3140-3151), and the lock and unlock functions use whatever handle is stored at the time and ignore the kernel's result (wolfcrypt/src/wc_port.c:3159-3170). If A has already taken its semaphore when B overwrites the handle, B takes the new one, and both tasks are inside the section. A then releases a semaphore it does not hold. The first semaphore is never freed. On a port whose create call rejects an object that already exists, the second call returns BAD_MUTEX_E and that one hardware operation fails.

On the MAX3266X port the affected mutex guards the TRNG: the first lock is in wc_GenerateSeed() (wolfcrypt/src/random.c:9560) or wc_MXC_TRNG_Random() (wolfcrypt/src/port/maxim/max3266x.c:624). BYTERAY-2026-0430 describes a separate flag in that seed function. BYTERAY-2026-0357 covers the same pattern on the global hardware mutex.

Reachability and Preconditions

  • The build has the option enabled on a multi-threaded RTOS target, as listed above.
  • Two tasks make the first lock of the same per-algorithm mutex at the same time. On MAX3266X that means two tasks seeding or reading the TRNG for the first time.
  • Calling wolfCrypt_Init() first does not close the window, because it does not create these mutexes.
  • No network input, authentication state or user interaction is involved. An attacker has no direct control over the timing.

Impact

Confirmed from source: two tasks can run wc_InitMutex() on the same per-algorithm mutex. On FreeRTOS that leaks one semaphore and can leave two tasks inside the section the mutex protects for that first operation. On ports that refuse a second create, one lock call fails and the hardware operation returns an error.

Plausible but not verified: what the hardware returns when two tasks drive it at once. On MAX3266X the section is a TRNG initialization, health test or read; the behaviour of the vendor SDK under concurrent use was not examined.

Not supported by the source: a crash or a permanently blocked hardware path. After the window, all tasks use the last handle stored and the flag stays set. ByteRay's review found a one-time loss of mutual exclusion and a small leak, not a lasting denial of service. Readers should weigh the published score against that.

ByteRay did not build a proof of concept for this issue. The analysis is from source review.

Remediation and Mitigation

Not fixed on wolfSSL master as of commit e4b0841b32e7 (2026-12-03). hwAlgoMutexInit() there is unchanged.

The fix direction is to create the per-algorithm mutexes once from wolfCrypt_Init(), next to the global one, or to elect a single initializer atomically. The tree has a helper for the second approach, wc_local_InitMutexOnce() (wolfcrypt/src/wc_port.c:2973-3007).

Workarounds for affected builds:

  • Make the first use of each affected algorithm from one task before starting others. On MAX3266X, one wc_InitRng() call during start-up creates the RNG mutex.
  • Or build without WOLFSSL_ALGO_HW_MUTEX. The port then uses the global hardware mutex, which wolfCrypt_Init() creates.

Timeline

  • 2026-09-28 — Reported to wolfSSL.
  • 2026-12-03 — Public disclosure.

Credits

Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.

References