1
0
mirror of https://github.com/ARMmbed/mbedtls.git synced 2025-07-14 05:29:25 +08:00

Merge pull request #1367 from gilles-peskine-arm/aesni_has_support-volatile-3.6

Backport 3.6: Fix race condition in mbedtls_aesni_has_support
This commit is contained in:
Gilles Peskine 2025-06-13 23:20:28 +02:00 committed by GitHub
commit 3c9ad42719
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 2 deletions

View File

@ -0,0 +1,17 @@
Bugfix
* Fix a race condition on x86/amd64 platforms in AESNI support detection
that could lead to using software AES in some threads at the very
beginning of a multithreaded program. Reported by Solar Designer.
Fixes #9840.
Security
* On x86/amd64 platforms, with some compilers, when the library is
compiled with support for both AESNI and software AES and AESNI is
available in hardware, an adversary with fine control over which
threads make progress in a multithreaded program could force software
AES to be used for some time when the program starts. This could allow
the adversary to conduct timing attacks and potentially recover the
key. In particular, this attacker model may be possible against an SGX
enclave.
The same vulnerability affects GCM acceleration, which could allow
a similarly powerful adversary to craft GCM forgeries.

View File

@ -48,8 +48,19 @@
*/ */
int mbedtls_aesni_has_support(unsigned int what) int mbedtls_aesni_has_support(unsigned int what)
{ {
static int done = 0; /* To avoid a race condition, tell the compiler that the assignment
static unsigned int c = 0; * `done = 1` and the assignment to `c` may not be reordered.
* https://github.com/Mbed-TLS/mbedtls/issues/9840
*
* Note that we may also be worried about memory access reordering,
* but fortunately the x86 memory model is not too wild: stores
* from the same thread are observed consistently by other threads.
* (See example 8-1 in Sewell et al., "x86-TSO: A Rigorous and Usable
* Programmers Model for x86 Multiprocessors", CACM, 2010,
* https://www.cl.cam.ac.uk/~pes20/weakmemory/cacm.pdf)
*/
static volatile int done = 0;
static volatile unsigned int c = 0;
if (!done) { if (!done) {
#if MBEDTLS_AESNI_HAVE_CODE == 2 #if MBEDTLS_AESNI_HAVE_CODE == 2