Skip to content

Commit 3ace5eb

Browse files
15characterlimisraptis-scy
authored andcommitted
Fix DefaultWalletKeyManager thread unsafe initialization.
Because the field wasn't re-checked after acquiring the mutex lock, it's possible that a second thread had already initialized the field concurrently. Worse, because the field isn't volatile, "concurrently" could have happened an arbitrarily long wall clock time ago, since the current thread isn't guaranteed to see a write to the field until after it acquires mutex. This commit adds an extra check inside the mutex block in order to prevent this race condition.
1 parent a468ac0 commit 3ace5eb

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

wallet-core/src/main/java/eu/europa/ec/eudi/wallet/provider/DefaultWalletKeyManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class DefaultWalletKeyManager(
5858

5959
private suspend fun getSecureAreaWalletKeyManager(): SecureAreaWalletKeyManager {
6060
return secureAreaBased ?: mutex.withLock {
61+
// another thread may have initialized secureAreaBased while we were acquiring mutex.
62+
secureAreaBased ?.let { return it }
6163
val storage = AndroidStorage("${context.noBackupFilesDir.path}/wallet-attest.bin")
6264
val secureArea = AndroidKeystoreSecureArea.create(storage)
6365

0 commit comments

Comments
 (0)