Skip to content

Commit ff734db

Browse files
hxjerrygregkh
authored andcommitted
crypto: jitterentropy - replace long-held spinlock with mutex
[ Upstream commit 01d798e ] jent_kcapi_random() serializes the shared jitterentropy state, but it currently holds a spinlock across the jent_read_entropy() call. That path performs expensive jitter collection and SHA3 conditioning, so parallel readers can trigger stalls as contending waiters spin for the same lock. To prevent non-preemptible lock hold, replace rng->jent_lock with a mutex so contended readers sleep instead of spinning on a shared lock held across expensive entropy generation. Fixes: bb5530e ("crypto: jitterentropy - add jitterentropy RNG") Reported-by: Yifan Wu <yifanwucs@gmail.com> Reported-by: Juefei Pu <tomapufckgml@gmail.com> Reported-by: Yuan Tan <yuantan098@gmail.com> Suggested-by: Xin Liu <bird@lzu.edu.cn> Signed-off-by: Haixin Xu <jerryxucs@gmail.com> Reviewed-by: Stephan Mueller <smueller@chronox.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 3ab9ab2 commit ff734db

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

crypto/jitterentropy-kcapi.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <linux/fips.h>
4343
#include <linux/kernel.h>
4444
#include <linux/module.h>
45+
#include <linux/mutex.h>
4546
#include <linux/slab.h>
4647
#include <linux/time.h>
4748
#include <crypto/internal/rng.h>
@@ -193,7 +194,7 @@ int jent_read_random_block(void *hash_state, char *dst, unsigned int dst_len)
193194
***************************************************************************/
194195

195196
struct jitterentropy {
196-
spinlock_t jent_lock;
197+
struct mutex jent_lock;
197198
struct rand_data *entropy_collector;
198199
struct crypto_shash *tfm;
199200
struct shash_desc *sdesc;
@@ -203,7 +204,7 @@ static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
203204
{
204205
struct jitterentropy *rng = crypto_tfm_ctx(tfm);
205206

206-
spin_lock(&rng->jent_lock);
207+
mutex_lock(&rng->jent_lock);
207208

208209
if (rng->sdesc) {
209210
shash_desc_zero(rng->sdesc);
@@ -218,7 +219,7 @@ static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
218219
if (rng->entropy_collector)
219220
jent_entropy_collector_free(rng->entropy_collector);
220221
rng->entropy_collector = NULL;
221-
spin_unlock(&rng->jent_lock);
222+
mutex_unlock(&rng->jent_lock);
222223
}
223224

224225
static int jent_kcapi_init(struct crypto_tfm *tfm)
@@ -228,7 +229,7 @@ static int jent_kcapi_init(struct crypto_tfm *tfm)
228229
struct shash_desc *sdesc;
229230
int size, ret = 0;
230231

231-
spin_lock_init(&rng->jent_lock);
232+
mutex_init(&rng->jent_lock);
232233

233234
/*
234235
* Use SHA3-256 as conditioner. We allocate only the generic
@@ -265,7 +266,6 @@ static int jent_kcapi_init(struct crypto_tfm *tfm)
265266
goto err;
266267
}
267268

268-
spin_lock_init(&rng->jent_lock);
269269
return 0;
270270

271271
err:
@@ -280,7 +280,7 @@ static int jent_kcapi_random(struct crypto_rng *tfm,
280280
struct jitterentropy *rng = crypto_rng_ctx(tfm);
281281
int ret = 0;
282282

283-
spin_lock(&rng->jent_lock);
283+
mutex_lock(&rng->jent_lock);
284284

285285
ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
286286

@@ -306,7 +306,7 @@ static int jent_kcapi_random(struct crypto_rng *tfm,
306306
ret = -EINVAL;
307307
}
308308

309-
spin_unlock(&rng->jent_lock);
309+
mutex_unlock(&rng->jent_lock);
310310

311311
return ret;
312312
}

0 commit comments

Comments
 (0)