wolfSSL — Uninitialized Shared Mutex Used After a Failed Initialization of the MAXQ10xx Hardware Lock
The MAXQ10xx wc_InitMutex() creates a shared memory object, publishes a pointer to it and only then initializes the mutex inside. If a step fails after creation, the object stays behind uninitialized. The next attempt finds it, skips initialization and reports success. Error paths also leak the descriptor and the mapping.
- Advisory
- BYTERAY-2026-0355
- 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 --with-maxq10xx and pthreads (MAXQ10XX_MUTEX)
- Fixed in
- Master commit dc268ec3000ac726d6aff309f2afeb8f22c17aa4 (PR 12067, merged 2026-11-16). Not in a wolfSSL stable release as of 2026-12-03.
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
In MAXQ1065 and MAXQ1080 builds with pthreads, the hardware crypto lock is a pthread_mutex_t inside a POSIX shared memory object named /maxq-mutex, so that every process using the secure element shares it. wc_InitMutex() opens the object, or creates it if it does not exist, maps it, and initializes the mutex only in the call that created it.
The steps are not undone when one of them fails. A call that creates the object and then fails before pthread_mutex_init() completes returns an error and leaves the object in place. The module's "initialized" flag stays clear, so the next lock attempt calls wc_InitMutex() again. This time the object exists, the call did not create it, the initialization block is skipped and the function returns success. From then on this process, and every other process on the host, locks and unlocks memory that was never initialized as a mutex. The object is never removed, so the state outlasts the process.
The same function stores the global mutex pointer before the mutex is initialized, and its error returns do not close the descriptor or unmap the mapping. The trigger is a failed system or library call, not attacker input. A default ./configure build does not compile this code.
Affected Products and Versions
- Component: wolfCrypt port layer, the
MAXQ10XX_MUTEXvariants ofwc_InitMutex()andmaxq_LockMutex()inwolfcrypt/src/wc_port.c. - Required build:
--with-maxq10xx=MAXQ1065or--with-maxq10xx=MAXQ108xwith pthreads, which definesWOLFSSL_CRYPT_HW_MUTEXandMAXQ10XX_MUTEX(wolfssl/wolfcrypt/settings.h:2536-2539). - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- The
v5.9.2-stableandv5.9.4-stabletags contain the same code. Fixed on master by commit dc268ec3000ac726d6aff309f2afeb8f22c17aa4 (PR 12067); no release contains the fix. - A default
./configurebuild does not include this code. Earlier releases were not checked.
Technical Details
The function is at wolfcrypt/src/wc_port.c:3282-3354. It decides whether it owns the initialization from which of two opens succeeded (wolfcrypt/src/wc_port.c:3295-3300):
int shm_fd = shm_open("/maxq-mutex", O_RDWR, 0666);
if (shm_fd < 0) {
/* create mutex memory */
shm_fd = shm_open("/maxq-mutex", O_RDWR | O_CREAT | O_EXCL, 0666);
created = 1;
}
It then sizes the object with ftruncate(), maps it, and stores the mapping in the global wcCryptHwSharedMutexPtr at wolfcrypt/src/wc_port.c:3320. Only after that does it close the descriptor (wolfcrypt/src/wc_port.c:3322-3325) and, in an if (created) block, initialize the mutex attributes and the mutex (wolfcrypt/src/wc_port.c:3327-3346). Every failure from line 3307 to line 3350 is a bare return BAD_MUTEX_E.
The path to an uninitialized mutex:
- A call creates the object (
createdis 1) and one offtruncate(),mmap(),close(),pthread_mutexattr_init(),pthread_mutexattr_setpshared()orpthread_mutex_init()fails. The function returns an error. The object remains; nothing in the tested tree callsshm_unlink(). wolfSSL_CryptHwMutexInit()sets its flag only on success (wolfcrypt/src/wc_port.c:2686-2698), so the nextwolfSSL_CryptHwMutexLock()callswc_InitMutex()again.- The first
shm_open()now succeeds.createdstays 0, the block at lines 3327-3346 is skipped, and the function returns 0. - The lock and unlock functions check only that the pointer is not NULL (
wolfcrypt/src/wc_port.c:3396-3398,wolfcrypt/src/wc_port.c:3461-3463) and callpthread_mutex_trylock(),pthread_mutex_lock()(wolfcrypt/src/wc_port.c:3421-3424) andpthread_mutex_unlock()on the zero-filled object.
A process that exits between the create and the mutex initialization leaves the same object behind. Any other process that starts later takes step 3 directly.
Separate defects in the same code:
- The descriptor is not closed when
ftruncate()ormmap()fails (wolfcrypt/src/wc_port.c:3307-3318). Each failed attempt leaks one descriptor. - After line 3320 the mapping is never unmapped on failure, and a later call overwrites the pointer.
wc_FreeMutex()unmaps only the current pointer (wolfcrypt/src/wc_port.c:3366-3374). maxq_LockMutex()allocates a per-thread counter and returns without freeing it whenpthread_setspecific()fails (wolfcrypt/src/wc_port.c:3402-3411).ftruncate()is applied to an object the call did not create (wolfcrypt/src/wc_port.c:3307). Trust in a pre-existing object is the subject of BYTERAY-2026-0356. The unsynchronized flag inwolfSSL_CryptHwMutexInit()is covered in BYTERAY-2026-0357.
Reachability and Preconditions
- The build is a MAXQ10xx build with pthreads, as listed above.
wolfCrypt_Init()callswolfSSL_CryptHwMutexInit()(wolfcrypt/src/wc_port.c:877-884), so the function runs at start-up of every process that uses the library. - The shared object does not yet exist, and one of the calls in step 1 fails in the process that creates it, or that process exits part way through.
- These failures are uncommon.
mmap()can fail under memory pressure; the attribute and mutex initialization calls rarely fail. The source shows no way for a peer or an unprivileged local user to force them in another process. - No network input, authentication state or user interaction is involved.
Impact
Confirmed from source: after one failed creation, wc_InitMutex() reports success without initializing the shared mutex, for every later caller on the host, until the object is removed or the system restarts. All MAXQ10xx operations then lock memory that was never set up as a process-shared mutex. POSIX leaves that undefined. Also confirmed: descriptor, mapping and per-thread allocation leaks on the error paths. One descriptor is lost per failed attempt, so the leak needs repeated failures to matter.
Plausible but not verified: the concrete result on a given C library, which can range from a lock that happens to work inside one process to callers that block, or two processes driving the secure element at once. ByteRay did not test this. The effect is on availability and correctness of hardware-backed operations.
ByteRay did not build a proof of concept for this issue. The analysis is from source review.
Remediation and Mitigation
Fixed on master by commit dc268ec3000ac726d6aff309f2afeb8f22c17aa4 (PR 12067, merged 2026-11-16). The commit closes the descriptor on every path, unmaps on failure, destroys the mutex attributes, removes an object it created but could not initialize, stores the global pointer only after initialization has succeeded, and frees the per-thread counter when pthread_setspecific() fails. A second commit in the same pull request, b2c719310556e6f491f99e652388bad4de4c457b, replaces the two-open scheme: the object's size records whether the mutex is ready, an object left part way through initialization is initialized again, and an existing object is no longer truncated. The fix is on master only. No release contains it as of 2026-12-03.
Until a release does, check the return value of wolfCrypt_Init() and wolfSSL_Init(). If either fails on a MAXQ10xx build, remove the shared memory object (/dev/shm/maxq-mutex on Linux) before retrying, while no process is using the device. Removing it at boot, before the first process that uses the secure element starts, has the same effect.
Timeline
- 2026-09-28 — Reported to wolfSSL.
- 2026-11-09 — Pull request 12067 opened upstream.
- 2026-11-16 — Pull request 12067 merged to master.
- 2026-12-03 — Public disclosure.
Credits
Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.
References
- MAXQ10xx
wc_InitMutexat the tested commit maxq_LockMutexat the tested commit- PR 12067
- Fix commit dc268ec, error paths
- Commit b2c7193, initialization between processes
- BYTERAY-2026-0356, trust in a pre-existing shared memory object
- BYTERAY-2026-0357, the initialization flag of the hardware crypto mutex
- CWE-412