Skip to content

Commit 01d798e

Browse files
hxjerryherbertx
authored andcommitted
crypto: jitterentropy - replace long-held spinlock with mutex
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>
1 parent 06c4214 commit 01d798e

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
/* Use SHA3-256 as conditioner */
234235
hash = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
@@ -257,7 +258,6 @@ static int jent_kcapi_init(struct crypto_tfm *tfm)
257258
goto err;
258259
}
259260

260-
spin_lock_init(&rng->jent_lock);
261261
return 0;
262262

263263
err:
@@ -272,7 +272,7 @@ static int jent_kcapi_random(struct crypto_rng *tfm,
272272
struct jitterentropy *rng = crypto_rng_ctx(tfm);
273273
int ret = 0;
274274

275-
spin_lock(&rng->jent_lock);
275+
mutex_lock(&rng->jent_lock);
276276

277277
ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
278278

@@ -298,7 +298,7 @@ static int jent_kcapi_random(struct crypto_rng *tfm,
298298
ret = -EINVAL;
299299
}
300300

301-
spin_unlock(&rng->jent_lock);
301+
mutex_unlock(&rng->jent_lock);
302302

303303
return ret;
304304
}

0 commit comments

Comments
 (0)