This is a note to let you know that I've just added the patch titled random: avoid initializing twice in credit race to the 5.18-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: random-avoid-initializing-twice-in-credit-race.patch and it can be found in the queue-5.18 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From foo@baz Thu May 26 04:17:01 PM CEST 2022 From: "Jason A. Donenfeld" <Jason@xxxxxxxxx> Date: Mon, 9 May 2022 13:40:55 +0200 Subject: random: avoid initializing twice in credit race From: "Jason A. Donenfeld" <Jason@xxxxxxxxx> commit fed7ef061686cc813b1f3d8d0edc6c35b4d3537b upstream. Since all changes of crng_init now go through credit_init_bits(), we can fix a long standing race in which two concurrent callers of credit_init_bits() have the new bit count >= some threshold, but are doing so with crng_init as a lower threshold, checked outside of a lock, resulting in crng_reseed() or similar being called twice. In order to fix this, we can use the original cmpxchg value of the bit count, and only change crng_init when the bit count transitions from below a threshold to meeting the threshold. Reviewed-by: Dominik Brodowski <linux@xxxxxxxxxxxxxxxxxxxx> Signed-off-by: Jason A. Donenfeld <Jason@xxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- drivers/char/random.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -821,7 +821,7 @@ static void extract_entropy(void *buf, s static void credit_init_bits(size_t nbits) { - unsigned int init_bits, orig, add; + unsigned int new, orig, add; unsigned long flags; if (crng_ready() || !nbits) @@ -831,12 +831,12 @@ static void credit_init_bits(size_t nbit do { orig = READ_ONCE(input_pool.init_bits); - init_bits = min_t(unsigned int, POOL_BITS, orig + add); - } while (cmpxchg(&input_pool.init_bits, orig, init_bits) != orig); + new = min_t(unsigned int, POOL_BITS, orig + add); + } while (cmpxchg(&input_pool.init_bits, orig, new) != orig); - if (!crng_ready() && init_bits >= POOL_READY_BITS) + if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) crng_reseed(); - else if (unlikely(crng_init == CRNG_EMPTY && init_bits >= POOL_EARLY_BITS)) { + else if (orig < POOL_EARLY_BITS && new >= POOL_EARLY_BITS) { spin_lock_irqsave(&base_crng.lock, flags); if (crng_init == CRNG_EMPTY) { extract_entropy(base_crng.key, sizeof(base_crng.key)); Patches currently in stable-queue which might be from Jason@xxxxxxxxx are queue-5.18/random-remove-ratelimiting-for-in-kernel-unseeded-randomness.patch queue-5.18/random-fix-sysctl-documentation-nits.patch queue-5.18/random-help-compiler-out-with-fast_mix-by-using-simpler-arguments.patch queue-5.18/siphash-use-one-source-of-truth-for-siphash-permutations.patch queue-5.18/um-use-fallback-for-random_get_entropy-instead-of-zero.patch queue-5.18/random-order-timer-entropy-functions-below-interrupt-functions.patch queue-5.18/random-unify-batched-entropy-implementations.patch queue-5.18/random-make-consistent-use-of-buf-and-len.patch queue-5.18/random-move-randomize_page-into-mm-where-it-belongs.patch queue-5.18/random-use-first-128-bits-of-input-as-fast-init.patch queue-5.18/random-use-proper-return-types-on-get_random_-int-long-_wait.patch queue-5.18/s390-define-get_cycles-macro-for-arch-override.patch queue-5.18/timekeeping-add-raw-clock-fallback-for-random_get_entropy.patch queue-5.18/random-use-static-branch-for-crng_ready.patch queue-5.18/arm-use-fallback-for-random_get_entropy-instead-of-zero.patch queue-5.18/mips-use-fallback-for-random_get_entropy-instead-of-just-c0-random.patch queue-5.18/random-avoid-initializing-twice-in-credit-race.patch queue-5.18/random-move-initialization-functions-out-of-hot-pages.patch queue-5.18/random-do-not-pretend-to-handle-premature-next-security-model.patch queue-5.18/random-do-not-use-batches-when-crng_ready.patch queue-5.18/m68k-use-fallback-for-random_get_entropy-instead-of-zero.patch queue-5.18/random-move-initialization-out-of-reseeding-hot-path.patch queue-5.18/x86-tsc-use-fallback-for-random_get_entropy-instead-of-zero.patch queue-5.18/random-credit-architectural-init-the-exact-amount.patch queue-5.18/random-check-for-signals-after-page-of-pool-writes.patch queue-5.18/random-remove-extern-from-functions-in-header.patch queue-5.18/random-do-not-use-input-pool-from-hard-irqs.patch queue-5.18/random-wire-up-fops-splice_-read-write-_iter.patch queue-5.18/random-insist-on-random_get_entropy-existing-in-order-to-simplify.patch queue-5.18/powerpc-define-get_cycles-macro-for-arch-override.patch queue-5.18/parisc-define-get_cycles-macro-for-arch-override.patch queue-5.18/sparc-use-fallback-for-random_get_entropy-instead-of-zero.patch queue-5.18/nios2-use-fallback-for-random_get_entropy-instead-of-zero.patch queue-5.18/init-call-time_init-before-rand_initialize.patch queue-5.18/riscv-use-fallback-for-random_get_entropy-instead-of-zero.patch queue-5.18/ia64-define-get_cycles-macro-for-arch-override.patch queue-5.18/random-handle-latent-entropy-and-command-line-from-random_init.patch queue-5.18/random-use-proper-jiffies-comparison-macro.patch queue-5.18/alpha-define-get_cycles-macro-for-arch-override.patch queue-5.18/random-convert-to-using-fops-read_iter.patch queue-5.18/xtensa-use-fallback-for-random_get_entropy-instead-of-zero.patch queue-5.18/random-use-symbolic-constants-for-crng_init-states.patch queue-5.18/random-convert-to-using-fops-write_iter.patch