random: replace custom notifier chain with standard one

commit 5acd35487dc911541672b3ffc322851769c32a56 upstream.

We previously rolled our own randomness readiness notifier, which only
has two users in the whole kernel. Replace this with a more standard
atomic notifier block that serves the same purpose with less code. Also
unexport the symbols, because no modules use it, only unconditional
builtins. The only drawback is that it's possible for a notification
handler returning the "stop" code to prevent further processing, but
given that there are only two users, and that we're unexporting this
anyway, that doesn't seem like a significant drawback for the
simplification we receive here.

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Dominik Brodowski <linux@dominikbrodowski.net>
[Jason: for stable, also backported to crypto/drbg.c, not unexporting.]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Jason A. Donenfeld 2022-03-01 20:03:49 +01:00 committed by Greg Kroah-Hartman
parent 1ae73fb2a6
commit 631503001c
6 changed files with 47 additions and 74 deletions

View File

@ -1478,12 +1478,13 @@ static int drbg_generate_long(struct drbg_state *drbg,
return 0; return 0;
} }
static void drbg_schedule_async_seed(struct random_ready_callback *rdy) static int drbg_schedule_async_seed(struct notifier_block *nb, unsigned long action, void *data)
{ {
struct drbg_state *drbg = container_of(rdy, struct drbg_state, struct drbg_state *drbg = container_of(nb, struct drbg_state,
random_ready); random_ready);
schedule_work(&drbg->seed_work); schedule_work(&drbg->seed_work);
return 0;
} }
static int drbg_prepare_hrng(struct drbg_state *drbg) static int drbg_prepare_hrng(struct drbg_state *drbg)
@ -1496,10 +1497,8 @@ static int drbg_prepare_hrng(struct drbg_state *drbg)
INIT_WORK(&drbg->seed_work, drbg_async_seed); INIT_WORK(&drbg->seed_work, drbg_async_seed);
drbg->random_ready.owner = THIS_MODULE; drbg->random_ready.notifier_call = drbg_schedule_async_seed;
drbg->random_ready.func = drbg_schedule_async_seed; err = register_random_ready_notifier(&drbg->random_ready);
err = add_random_ready_callback(&drbg->random_ready);
switch (err) { switch (err) {
case 0: case 0:
@ -1510,7 +1509,7 @@ static int drbg_prepare_hrng(struct drbg_state *drbg)
/* fall through */ /* fall through */
default: default:
drbg->random_ready.func = NULL; drbg->random_ready.notifier_call = NULL;
return err; return err;
} }
@ -1616,8 +1615,8 @@ free_everything:
*/ */
static int drbg_uninstantiate(struct drbg_state *drbg) static int drbg_uninstantiate(struct drbg_state *drbg)
{ {
if (drbg->random_ready.func) { if (drbg->random_ready.notifier_call) {
del_random_ready_callback(&drbg->random_ready); unregister_random_ready_notifier(&drbg->random_ready);
cancel_work_sync(&drbg->seed_work); cancel_work_sync(&drbg->seed_work);
crypto_free_rng(drbg->jent); crypto_free_rng(drbg->jent);
drbg->jent = NULL; drbg->jent = NULL;

View File

@ -83,8 +83,8 @@ static int crng_init = 0;
/* Various types of waiters for crng_init->2 transition. */ /* Various types of waiters for crng_init->2 transition. */
static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait);
static struct fasync_struct *fasync; static struct fasync_struct *fasync;
static DEFINE_SPINLOCK(random_ready_list_lock); static DEFINE_SPINLOCK(random_ready_chain_lock);
static LIST_HEAD(random_ready_list); static RAW_NOTIFIER_HEAD(random_ready_chain);
/* Control how we warn userspace. */ /* Control how we warn userspace. */
static struct ratelimit_state unseeded_warning = static struct ratelimit_state unseeded_warning =
@ -147,72 +147,45 @@ EXPORT_SYMBOL(wait_for_random_bytes);
* *
* returns: 0 if callback is successfully added * returns: 0 if callback is successfully added
* -EALREADY if pool is already initialised (callback not called) * -EALREADY if pool is already initialised (callback not called)
* -ENOENT if module for callback is not alive
*/ */
int add_random_ready_callback(struct random_ready_callback *rdy) int register_random_ready_notifier(struct notifier_block *nb)
{ {
struct module *owner;
unsigned long flags; unsigned long flags;
int err = -EALREADY; int ret = -EALREADY;
if (crng_ready()) if (crng_ready())
return err; return ret;
owner = rdy->owner; spin_lock_irqsave(&random_ready_chain_lock, flags);
if (!try_module_get(owner)) if (!crng_ready())
return -ENOENT; ret = raw_notifier_chain_register(&random_ready_chain, nb);
spin_unlock_irqrestore(&random_ready_chain_lock, flags);
spin_lock_irqsave(&random_ready_list_lock, flags); return ret;
if (crng_ready())
goto out;
owner = NULL;
list_add(&rdy->list, &random_ready_list);
err = 0;
out:
spin_unlock_irqrestore(&random_ready_list_lock, flags);
module_put(owner);
return err;
} }
EXPORT_SYMBOL(add_random_ready_callback); EXPORT_SYMBOL(register_random_ready_notifier);
/* /*
* Delete a previously registered readiness callback function. * Delete a previously registered readiness callback function.
*/ */
void del_random_ready_callback(struct random_ready_callback *rdy) int unregister_random_ready_notifier(struct notifier_block *nb)
{ {
unsigned long flags; unsigned long flags;
struct module *owner = NULL; int ret;
spin_lock_irqsave(&random_ready_list_lock, flags); spin_lock_irqsave(&random_ready_chain_lock, flags);
if (!list_empty(&rdy->list)) { ret = raw_notifier_chain_unregister(&random_ready_chain, nb);
list_del_init(&rdy->list); spin_unlock_irqrestore(&random_ready_chain_lock, flags);
owner = rdy->owner; return ret;
}
spin_unlock_irqrestore(&random_ready_list_lock, flags);
module_put(owner);
} }
EXPORT_SYMBOL(del_random_ready_callback); EXPORT_SYMBOL(unregister_random_ready_notifier);
static void process_random_ready_list(void) static void process_random_ready_list(void)
{ {
unsigned long flags; unsigned long flags;
struct random_ready_callback *rdy, *tmp;
spin_lock_irqsave(&random_ready_list_lock, flags); spin_lock_irqsave(&random_ready_chain_lock, flags);
list_for_each_entry_safe(rdy, tmp, &random_ready_list, list) { raw_notifier_call_chain(&random_ready_chain, 0, NULL);
struct module *owner = rdy->owner; spin_unlock_irqrestore(&random_ready_chain_lock, flags);
list_del_init(&rdy->list);
rdy->func(rdy);
module_put(owner);
}
spin_unlock_irqrestore(&random_ready_list_lock, flags);
} }
#define warn_unseeded_randomness(previous) \ #define warn_unseeded_randomness(previous) \

View File

@ -136,7 +136,7 @@ struct drbg_state {
const struct drbg_state_ops *d_ops; const struct drbg_state_ops *d_ops;
const struct drbg_core *core; const struct drbg_core *core;
struct drbg_string test_data; struct drbg_string test_data;
struct random_ready_callback random_ready; struct notifier_block random_ready;
}; };
static inline __u8 drbg_statelen(struct drbg_state *drbg) static inline __u8 drbg_statelen(struct drbg_state *drbg)

View File

@ -10,11 +10,7 @@
#include <uapi/linux/random.h> #include <uapi/linux/random.h>
struct random_ready_callback { struct notifier_block;
struct list_head list;
void (*func)(struct random_ready_callback *rdy);
struct module *owner;
};
extern void add_device_randomness(const void *, size_t); extern void add_device_randomness(const void *, size_t);
extern void add_bootloader_randomness(const void *, size_t); extern void add_bootloader_randomness(const void *, size_t);
@ -39,8 +35,8 @@ extern void get_random_bytes(void *buf, size_t nbytes);
extern int wait_for_random_bytes(void); extern int wait_for_random_bytes(void);
extern int __init rand_initialize(void); extern int __init rand_initialize(void);
extern bool rng_is_initialized(void); extern bool rng_is_initialized(void);
extern int add_random_ready_callback(struct random_ready_callback *rdy); extern int register_random_ready_notifier(struct notifier_block *nb);
extern void del_random_ready_callback(struct random_ready_callback *rdy); extern int unregister_random_ready_notifier(struct notifier_block *nb);
extern size_t __must_check get_random_bytes_arch(void *buf, size_t nbytes); extern size_t __must_check get_random_bytes_arch(void *buf, size_t nbytes);
#ifndef MODULE #ifndef MODULE

View File

@ -40,6 +40,7 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/bitops.h> #include <linux/bitops.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/notifier.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
/** /**
@ -546,9 +547,11 @@ static void prandom_reseed(struct timer_list *unused)
* To avoid worrying about whether it's safe to delay that interrupt * To avoid worrying about whether it's safe to delay that interrupt
* long enough to seed all CPUs, just schedule an immediate timer event. * long enough to seed all CPUs, just schedule an immediate timer event.
*/ */
static void prandom_timer_start(struct random_ready_callback *unused) static int prandom_timer_start(struct notifier_block *nb,
unsigned long action, void *data)
{ {
mod_timer(&seed_timer, jiffies); mod_timer(&seed_timer, jiffies);
return 0;
} }
/* /*
@ -557,13 +560,13 @@ static void prandom_timer_start(struct random_ready_callback *unused)
*/ */
static int __init prandom_init_late(void) static int __init prandom_init_late(void)
{ {
static struct random_ready_callback random_ready = { static struct notifier_block random_ready = {
.func = prandom_timer_start .notifier_call = prandom_timer_start
}; };
int ret = add_random_ready_callback(&random_ready); int ret = register_random_ready_notifier(&random_ready);
if (ret == -EALREADY) { if (ret == -EALREADY) {
prandom_timer_start(&random_ready); prandom_timer_start(&random_ready, 0, NULL);
ret = 0; ret = 0;
} }
return ret; return ret;

View File

@ -731,14 +731,16 @@ static void enable_ptr_key_workfn(struct work_struct *work)
static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
static void fill_random_ptr_key(struct random_ready_callback *unused) static int fill_random_ptr_key(struct notifier_block *nb,
unsigned long action, void *data)
{ {
/* This may be in an interrupt handler. */ /* This may be in an interrupt handler. */
queue_work(system_unbound_wq, &enable_ptr_key_work); queue_work(system_unbound_wq, &enable_ptr_key_work);
return 0;
} }
static struct random_ready_callback random_ready = { static struct notifier_block random_ready = {
.func = fill_random_ptr_key .notifier_call = fill_random_ptr_key
}; };
static int __init initialize_ptr_random(void) static int __init initialize_ptr_random(void)
@ -752,7 +754,7 @@ static int __init initialize_ptr_random(void)
return 0; return 0;
} }
ret = add_random_ready_callback(&random_ready); ret = register_random_ready_notifier(&random_ready);
if (!ret) { if (!ret) {
return 0; return 0;
} else if (ret == -EALREADY) { } else if (ret == -EALREADY) {