Commit 5d73d1e3 authored by Eric Biggers's avatar Eric Biggers Committed by Jason A. Donenfeld

random: fix data race on crng_node_pool

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: 1e7f583a ("random: make /dev/urandom scalable for silly userspace programs")
Cc: stable@vger.kernel.org
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Acked-by: default avatarPaul E. McKenney <paulmck@kernel.org>
Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
parent 5320eb42
...@@ -854,8 +854,8 @@ static void do_numa_crng_init(struct work_struct *work) ...@@ -854,8 +854,8 @@ static void do_numa_crng_init(struct work_struct *work)
crng_initialize_secondary(crng); crng_initialize_secondary(crng);
pool[i] = crng; pool[i] = crng;
} }
mb(); /* pairs with READ_ONCE() in select_crng() */
if (cmpxchg(&crng_node_pool, NULL, pool)) { if (cmpxchg_release(&crng_node_pool, NULL, pool) != NULL) {
for_each_node(i) for_each_node(i)
kfree(pool[i]); kfree(pool[i]);
kfree(pool); kfree(pool);
...@@ -868,8 +868,26 @@ static void numa_crng_init(void) ...@@ -868,8 +868,26 @@ static void numa_crng_init(void)
{ {
schedule_work(&numa_crng_init_work); schedule_work(&numa_crng_init_work);
} }
static struct crng_state *select_crng(void)
{
struct crng_state **pool;
int nid = numa_node_id();
/* pairs with cmpxchg_release() in do_numa_crng_init() */
pool = READ_ONCE(crng_node_pool);
if (pool && pool[nid])
return pool[nid];
return &primary_crng;
}
#else #else
static void numa_crng_init(void) {} static void numa_crng_init(void) {}
static struct crng_state *select_crng(void)
{
return &primary_crng;
}
#endif #endif
/* /*
...@@ -1016,15 +1034,7 @@ static void _extract_crng(struct crng_state *crng, ...@@ -1016,15 +1034,7 @@ static void _extract_crng(struct crng_state *crng,
static void extract_crng(__u8 out[CHACHA_BLOCK_SIZE]) static void extract_crng(__u8 out[CHACHA_BLOCK_SIZE])
{ {
struct crng_state *crng = NULL; _extract_crng(select_crng(), out);
#ifdef CONFIG_NUMA
if (crng_node_pool)
crng = crng_node_pool[numa_node_id()];
if (crng == NULL)
#endif
crng = &primary_crng;
_extract_crng(crng, out);
} }
/* /*
...@@ -1053,15 +1063,7 @@ static void _crng_backtrack_protect(struct crng_state *crng, ...@@ -1053,15 +1063,7 @@ static void _crng_backtrack_protect(struct crng_state *crng,
static void crng_backtrack_protect(__u8 tmp[CHACHA_BLOCK_SIZE], int used) static void crng_backtrack_protect(__u8 tmp[CHACHA_BLOCK_SIZE], int used)
{ {
struct crng_state *crng = NULL; _crng_backtrack_protect(select_crng(), tmp, used);
#ifdef CONFIG_NUMA
if (crng_node_pool)
crng = crng_node_pool[numa_node_id()];
if (crng == NULL)
#endif
crng = &primary_crng;
_crng_backtrack_protect(crng, tmp, used);
} }
static ssize_t extract_crng_user(void __user *buf, size_t nbytes) static ssize_t extract_crng_user(void __user *buf, size_t nbytes)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment