random: fix data race on crng_node_pool
authorEric Biggers <ebiggers@google.com>
Mon, 20 Dec 2021 22:41:56 +0000 (16:41 -0600)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 16 Jan 2022 08:14:23 +0000 (09:14 +0100)
commit3de9478230c3e3ae0ae599847ca2ac6b7781e780
tree07c4f2cda267a932e2621374198b05900e6b427d
parent43c494294f30a3899c07cf27f6dc76e0b8f8b133
random: fix data race on crng_node_pool

commit 5d73d1e320c3fd94ea15ba5f79301da9a8bcc7de upstream.

extract_crng() and crng_backtrack_protect() load crng_node_pool with a
plain load, which causes undefined behavior if do_numa_crng_init()
modifies it concurrently.

Fix this by using READ_ONCE().  Note: as per the previous discussion
https://lore.kernel.org/lkml/20211219025139.31085-1-ebiggers@kernel.org/T/#u,
READ_ONCE() is believed to be sufficient here, and it was requested that
it be used here instead of smp_load_acquire().

Also change do_numa_crng_init() to set crng_node_pool using
cmpxchg_release() instead of mb() + cmpxchg(), as the former is
sufficient here but is more lightweight.

Fixes: 1e7f583af67b ("random: make /dev/urandom scalable for silly userspace programs")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Acked-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/char/random.c