You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I notice there's a thread-safety (non-?)issue in mbedtls_aesni_has_support(), where the done variable may be set before c even though they're set in the other order in the C source. The compiler is free to re-order. The effect would be that another thread could run non-AES-NI code (perhaps just once) even on an AES-NI capable CPU. As long as either code is indeed correct and they're compatible in terms of context struct content, this should be a non-issue. But are they compatible? Not sure.
A compiler memory barrier may need to be added before the done = 1; line. And another before reading from c, as the reads may also be re-ordered otherwise. Like this:
Good point. IIRC the AESNI assembly, AESNI with intrinsics and our pure-software implementation use the same context layout (it's pretty natural for a “naive” AES implementation), but we should check this.
Also of note, mbedtls_aesce_has_support_impl in aesce.c follows the same pattern and has the same bug.
Also of note, mbedtls_aesce_has_support_impl in aesce.c follows the same pattern and has the same bug.
I had actually looked at it and no, I think it does not have the same bug - it uses just one variable, not two. So it may perform the checks more than once (in different threads), but with the same results (each thread will choose the same AES code version).
... and this suggests a different way to fix mbedtls_aesni_has_support() - switch it to using just one variable as well, move the flag check inside the do-once portion.
I notice there's a thread-safety (non-?)issue in
mbedtls_aesni_has_support()
, where thedone
variable may be set beforec
even though they're set in the other order in the C source. The compiler is free to re-order. The effect would be that another thread could run non-AES-NI code (perhaps just once) even on an AES-NI capable CPU. As long as either code is indeed correct and they're compatible in terms of context struct content, this should be a non-issue. But are they compatible? Not sure.A compiler memory barrier may need to be added before the
done = 1;
line. And another before reading fromc
, as the reads may also be re-ordered otherwise. Like this:Curiously, this reduced code size a tiny bit for me.
Since this code is x86-only, I assume we can rely on x86's strict memory ordering, without introducing hardware memory barriers.
The text was updated successfully, but these errors were encountered: