Skip to content

Commit

Permalink
crypto: rng - fix crypto_rng_reset() refcounting when !CRYPTO_STATS
Browse files Browse the repository at this point in the history
crypto_stats_get() is a no-op when the kernel is compiled without
CONFIG_CRYPTO_STATS, so pairing it with crypto_alg_put() unconditionally
(as crypto_rng_reset() does) is wrong.

Fix this by moving the call to crypto_stats_get() to just before the
actual algorithm operation which might need it.  This makes it always
paired with crypto_stats_rng_seed().

Fixes: eed74b3 ("crypto: rng - Fix a refcounting bug in crypto_rng_reset()")
Cc: [email protected]
Signed-off-by: Eric Biggers <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
ebiggers authored and herbertx committed Apr 2, 2021
1 parent 10cb823 commit 30d0f6a
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions crypto/rng.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,18 @@ int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
u8 *buf = NULL;
int err;

crypto_stats_get(alg);
if (!seed && slen) {
buf = kmalloc(slen, GFP_KERNEL);
if (!buf) {
crypto_alg_put(alg);
if (!buf)
return -ENOMEM;
}

err = get_random_bytes_wait(buf, slen);
if (err) {
crypto_alg_put(alg);
if (err)
goto out;
}
seed = buf;
}

crypto_stats_get(alg);
err = crypto_rng_alg(tfm)->seed(tfm, seed, slen);
crypto_stats_rng_seed(alg, err);
out:
Expand Down

0 comments on commit 30d0f6a

Please sign in to comment.