Skip to content

Commit fd09d7f

Browse files
sebastianasherbertx
authored andcommitted
crypto: ansi_prng - alloc cipher just in init
As reported by Eric Sesterhenn the re-allocation of the cipher in reset leads to: |BUG: sleeping function called from invalid context at kernel/rwsem.c:21 |in_atomic(): 1, irqs_disabled(): 0, pid: 4926, name: modprobe |INFO: lockdep is turned off. |Pid: 4926, comm: modprobe Tainted: G M 2.6.31-rc1-22297-g5298976 raspberrypi#24 |Call Trace: | [<c011dd93>] __might_sleep+0xf9/0x101 | [<c0777aa0>] down_read+0x16/0x68 | [<c048bf04>] crypto_alg_lookup+0x16/0x34 | [<c048bf52>] crypto_larval_lookup+0x30/0xf9 | [<c048c038>] crypto_alg_mod_lookup+0x1d/0x62 | [<c048c13e>] crypto_alloc_base+0x1e/0x64 | [<c04bf991>] reset_prng_context+0xab/0x13f | [<c04e5cfc>] ? __spin_lock_init+0x27/0x51 | [<c04bfce1>] cprng_init+0x2a/0x42 | [<c048bb4c>] __crypto_alloc_tfm+0xfa/0x128 | [<c048c153>] crypto_alloc_base+0x33/0x64 | [<c04933c9>] alg_test_cprng+0x30/0x1f4 | [<c0493329>] alg_test+0x12f/0x19f | [<c0177f1f>] ? __alloc_pages_nodemask+0x14d/0x481 | [<d09219e2>] do_test+0xf9d/0x163f [tcrypt] | [<d0920de6>] do_test+0x3a1/0x163f [tcrypt] | [<d0926035>] tcrypt_mod_init+0x35/0x7c [tcrypt] | [<c010113c>] _stext+0x54/0x12c | [<d0926000>] ? tcrypt_mod_init+0x0/0x7c [tcrypt] | [<c01398a3>] ? up_read+0x16/0x2b | [<c0139fc4>] ? __blocking_notifier_call_chain+0x40/0x4c | [<c014ee8d>] sys_init_module+0xa9/0x1bf | [<c010292b>] sysenter_do_call+0x12/0x32 because a spin lock is held and crypto_alloc_base() may sleep. There is no reason to re-allocate the cipher, the state is resetted in ->setkey(). This patches makes the cipher allocation a one time thing and moves it to init. Reported-by: Eric Sesterhenn <eric.sesterhenn@lsexperts.de> Signed-off-by: Sebastian Andrzej Siewior <sebastian@breakpoint.cc> Acked-by: Neil Horman <nhorman@tuxdriver.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent ed94070 commit fd09d7f

1 file changed

Lines changed: 8 additions & 17 deletions

File tree

‎crypto/ansi_cprng.c‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ static int reset_prng_context(struct prng_context *ctx,
283283
unsigned char *V, unsigned char *DT)
284284
{
285285
int ret;
286-
int rc = -EINVAL;
287286
unsigned char *prng_key;
288287

289288
spin_lock_bh(&ctx->prng_lock);
@@ -307,41 +306,33 @@ static int reset_prng_context(struct prng_context *ctx,
307306
memset(ctx->rand_data, 0, DEFAULT_BLK_SZ);
308307
memset(ctx->last_rand_data, 0, DEFAULT_BLK_SZ);
309308

310-
if (ctx->tfm)
311-
crypto_free_cipher(ctx->tfm);
312-
313-
ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
314-
if (IS_ERR(ctx->tfm)) {
315-
dbgprint(KERN_CRIT "Failed to alloc tfm for context %p\n",
316-
ctx);
317-
ctx->tfm = NULL;
318-
goto out;
319-
}
320-
321309
ctx->rand_data_valid = DEFAULT_BLK_SZ;
322310

323311
ret = crypto_cipher_setkey(ctx->tfm, prng_key, klen);
324312
if (ret) {
325313
dbgprint(KERN_CRIT "PRNG: setkey() failed flags=%x\n",
326314
crypto_cipher_get_flags(ctx->tfm));
327-
crypto_free_cipher(ctx->tfm);
328315
goto out;
329316
}
330317

331-
rc = 0;
318+
ret = 0;
332319
ctx->flags &= ~PRNG_NEED_RESET;
333320
out:
334321
spin_unlock_bh(&ctx->prng_lock);
335-
336-
return rc;
337-
322+
return ret;
338323
}
339324

340325
static int cprng_init(struct crypto_tfm *tfm)
341326
{
342327
struct prng_context *ctx = crypto_tfm_ctx(tfm);
343328

344329
spin_lock_init(&ctx->prng_lock);
330+
ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
331+
if (IS_ERR(ctx->tfm)) {
332+
dbgprint(KERN_CRIT "Failed to alloc tfm for context %p\n",
333+
ctx);
334+
return PTR_ERR(ctx->tfm);
335+
}
345336

346337
if (reset_prng_context(ctx, NULL, DEFAULT_PRNG_KSZ, NULL, NULL) < 0)
347338
return -EINVAL;

0 commit comments

Comments
 (0)