← ALL ADVISORIES

MEDIUM BYTERAY-2026-0357Disclosed

wolfSSL — Race Condition in Lazy Initialization of the Hardware Crypto Mutex

wolfSSL_CryptHwMutexInit() guards the one-time creation of the hardware crypto mutex with a plain int that is tested and set without a lock. Threads that reach hardware crypto before wolfCrypt_Init() has completed can both initialize the mutex, which breaks mutual exclusion on the device or fails the operation.

Advisory
BYTERAY-2026-0357
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, builds with WOLFSSL_CRYPT_HW_MUTEX on ports that have no static mutex initializer (for example --with-maxq10xx, or FreeRTOS with a hardware crypto port)
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

wolfCrypt serializes access to a hardware crypto engine with one global mutex. On ports whose mutex type has a static initializer the mutex is ready at load time. On the others it is created at run time by wolfSSL_CryptHwMutexInit(), which decides whether to do so by reading a plain static int. Nothing makes that read and the later write atomic.

wolfCrypt_Init() calls the function once while it holds the library's initialization state, so an application that finishes wolfCrypt_Init() before it starts worker threads never races. The race needs two threads to reach a hardware-backed operation before that, or after wolfCrypt_Init() failed. Both threads then see the flag at zero and both initialize the same mutex object. The second initialization replaces a mutex the first thread may already hold.

No attacker input is involved. The trigger is the application's own start-up order. A default ./configure build does not compile this code.

Affected Products and Versions

  • Component: wolfCrypt port layer, wolfSSL_CryptHwMutexInit() and wolfSSL_CryptHwMutexLock() in wolfcrypt/src/wc_port.c.
  • Required build: WOLFSSL_CRYPT_HW_MUTEX set to 1 and a mutex type without WOLFSSL_MUTEX_INITIALIZER. The pthread port defines the initializer (wolfssl/wolfcrypt/wc_port.h:452-457) and is not affected. The MAXQ10xx shared mutex (wolfssl/wolfcrypt/wc_port.h:446-451), FreeRTOS and the Windows critical-section port do not define it.
  • WOLFSSL_CRYPT_HW_MUTEX is turned on by --with-maxq10xx when pthreads are present (wolfssl/wolfcrypt/settings.h:2536-2539), by the STM32, PIC32MZ, MMCAU, MAX3266X and RTL8735B ports (wolfssl/wolfcrypt/wc_port.h:1022-1029), and by several other hardware ports.
  • 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 flag.
  • Partly addressed on master: the MAXQ10xx mutex creation was reworked, the flag was not. See Remediation.
  • A default ./configure build leaves WOLFSSL_CRYPT_HW_MUTEX at 0 and does not include the code. Earlier releases were not checked.

Technical Details

The flag and its only guard, at wolfcrypt/src/wc_port.c:2682-2698:

#ifndef WOLFSSL_MUTEX_INITIALIZER
static int wcCryptHwMutexInit = 0;
#endif

int wolfSSL_CryptHwMutexInit(void)
{
    int ret = 0;
#ifndef WOLFSSL_MUTEX_INITIALIZER
    if (wcCryptHwMutexInit == 0) {
        ret = wc_InitMutex(&wcCryptHwMutex);
        if (ret == 0) {
            wcCryptHwMutexInit = 1;

wolfSSL_CryptHwMutexLock() calls this function on every lock (wolfcrypt/src/wc_port.c:2699-2707), and so does maxq_CryptHwMutexTryLock() (wolfcrypt/src/wc_port.c:3440-3448). The header states the problem itself: the comment at wolfssl/wolfcrypt/wc_port.h:1036-1039 recommends calling the init function directly at hardware init "to avoid possible race condition where two calls to wolfSSL_CryptHwMutexLock are made at the same time".

The protected path is wolfCrypt_Init(). It enters through wc_local_InitUp() (wolfcrypt/src/wc_port.c:783), an atomic state machine that lets one thread initialize, and calls wolfSSL_CryptHwMutexInit() inside it (wolfcrypt/src/wc_port.c:879). After that the flag is 1 and is never cleared. The unprotected path is any hardware-backed operation that runs first:

  1. Thread A reads the flag as 0 at wolfcrypt/src/wc_port.c:2690 and enters wc_InitMutex().
  2. Thread B reads the flag as 0 before A reaches wolfcrypt/src/wc_port.c:2693, and enters wc_InitMutex() on the same object.
  3. Whichever call returns first sets the flag, and that thread locks the mutex at wolfcrypt/src/wc_port.c:2704.
  4. The other call then completes its initialization of a mutex that is already in use.

What step 4 does depends on the port. On FreeRTOS, wc_InitMutex() creates a new semaphore and overwrites the handle (wolfcrypt/src/wc_port.c:3140-3151). The first semaphore is leaked, and a thread that took it then releases a different one. On the MAXQ10xx port, each call maps the shared object and stores the mapping (wolfcrypt/src/wc_port.c:3295-3320), but only the call that created the object runs pthread_mutex_init() (wolfcrypt/src/wc_port.c:3327-3346). The other call returns success without waiting for it, so its thread can lock the mutex before the creator initializes it, and the creator's initialization then resets a held lock. If both calls miss the first shm_open(), the loser of the exclusive create returns BAD_MUTEX_E (wolfcrypt/src/wc_port.c:3298-3305). A failed initialization leaves the flag at 0, so later callers repeat the attempt with the same lack of serialization.

Reachability and Preconditions

  • The build has WOLFSSL_CRYPT_HW_MUTEX enabled and a mutex type with no static initializer.
  • Two or more threads call a hardware-backed wolfCrypt operation before wolfCrypt_Init() (or wolfSSL_Init(), which calls it) has completed in that process, or the application continues after wolfCrypt_Init() returned an error.
  • The lock functions are internal (WOLFSSL_LOCAL). They are reached through the hardware ports, for example MAXQ10xx AES key setup (wolfcrypt/src/port/maxim/maxq10xx.c:586) and STM32 SHA-1 (wolfcrypt/src/sha.c:174).
  • No network input, authentication state or user interaction is involved. A remote or local attacker has no direct way to cause the start-up ordering that the race needs.

Impact

Confirmed from the source: on an affected build, two threads can initialize the hardware mutex concurrently. The result is one of a failed lock (BAD_MUTEX_E, which the calling port turns into a failed or software-fallback operation), a leaked semaphore or mapping, or two threads inside the section the mutex is meant to protect. Concurrent use of a single hardware engine then produces failed or incorrect hardware operations.

Plausible but not verified: a thread left blocked on a mutex that another thread reinitialized. POSIX does not define the result of locking a mutex whose initialization has not finished, so the outcome on the MAXQ10xx port depends on the C library. The effect is limited to availability and correctness of hardware-backed operations in the racing process, and the window is the first use of the mutex.

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

Remediation and Mitigation

The flag is not fixed on wolfSSL master as of commit e4b0841b32e7 (2026-12-03). wolfSSL_CryptHwMutexInit() there is unchanged.

Master did rework the MAXQ10xx side. Commit dc268ec3000ac726d6aff309f2afeb8f22c17aa4 publishes the shared mutex pointer only after initialization succeeds and cleans up on error. Commit b2c719310556e6f491f99e652388bad4de4c457b serializes creation of the shared object with a record lock between processes and a statically initialized lock between threads, and no longer reinitializes a mutex that is ready. With those two commits a double entry on the MAXQ10xx port maps the same initialized mutex twice instead of resetting it. Other ports are unchanged. Neither commit is in a release as of 2026-12-03.

The fix direction is to elect one initializer atomically. The tree already has a helper for this, wc_local_InitMutexOnce() (wolfcrypt/src/wc_port.c:2973-3007), used by the ECC cache locks; the hardware mutex does not use it.

Workaround: call wolfCrypt_Init() or wolfSSL_Init() once, check the result, and only then start threads that use wolfCrypt. This has no cost and removes the race.

Timeline

  • 2026-09-28 — Reported to wolfSSL.
  • 2026-11-16 — wolfSSL PR 12067 merged to master. It carries commits dc268ec3000a and b2c719310556, which rework MAXQ10xx shared mutex initialization.
  • 2026-12-03 — Public disclosure.

Credits

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

References