Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: rng - fix crypto_rng_reset() refcounting when !CRYPTO_STATS

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: eed74b3eba9e ("crypto: rng - Fix a refcounting bug in crypto_rng_reset()")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Eric Biggers and committed by
Herbert Xu
30d0f6a9 10cb823b

+3 -7
+3 -7
crypto/rng.c
··· 34 34 u8 *buf = NULL; 35 35 int err; 36 36 37 - crypto_stats_get(alg); 38 37 if (!seed && slen) { 39 38 buf = kmalloc(slen, GFP_KERNEL); 40 - if (!buf) { 41 - crypto_alg_put(alg); 39 + if (!buf) 42 40 return -ENOMEM; 43 - } 44 41 45 42 err = get_random_bytes_wait(buf, slen); 46 - if (err) { 47 - crypto_alg_put(alg); 43 + if (err) 48 44 goto out; 49 - } 50 45 seed = buf; 51 46 } 52 47 48 + crypto_stats_get(alg); 53 49 err = crypto_rng_alg(tfm)->seed(tfm, seed, slen); 54 50 crypto_stats_rng_seed(alg, err); 55 51 out: