mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-24 12:44:11 +08:00
lib/vsprintf: Initialize vsprintf's pointer hash once the random core is ready.
The printk code invokes vnsprintf in order to compute the complete string before adding it into its buffer. This happens in an IRQ-off region which leads to a warning on PREEMPT_RT in the random code if the format strings contains a %p for pointer printing. This happens because the random core acquires locks which become sleeping locks on PREEMPT_RT which must not be acquired with disabled interrupts and or preemption disabled. By default the pointers are hashed which requires a random value on the first invocation (either by printk or another user which comes first. One could argue that there is no need for printk to disable interrupts during the vsprintf() invocation which would fix the just mentioned problem. However printk itself can be invoked in a context with disabled interrupts which would lead to the very same problem. Move the initialization of ptr_key into a worker and schedule it from subsys_initcall(). This happens early but after the workqueue subsystem is ready. Use get_random_bytes() to retrieve the random value if the RNG core is ready, otherwise schedule a worker in two seconds and try again. Another advantage is that it removes a lock from the vsprintf() code path. It prevents a possible deadlock when printk("%p", ptr) is called under the lock taken in get_random_bytes(). Reported-by: Mike Galbraith <efault@gmx.de> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Acked-by: Jason A. Donenfeld <Jason@zx2c4.com> Reviewed-by: Petr Mladek <pmladek@suse.com> [pmladek@suse.com: Added a note about the it prevented a possible deadlock in printk().] Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20220927104912.622645-3-bigeasy@linutronix.de
This commit is contained in:
parent
e4279b5998
commit
6f0ac3b52a
@ -751,31 +751,39 @@ static int __init debug_boot_weak_hash_enable(char *str)
|
||||
early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
|
||||
|
||||
static bool filled_random_ptr_key __read_mostly;
|
||||
static siphash_key_t ptr_key __read_mostly;
|
||||
static void fill_ptr_key_workfn(struct work_struct *work);
|
||||
static DECLARE_DELAYED_WORK(fill_ptr_key_work, fill_ptr_key_workfn);
|
||||
|
||||
static void fill_ptr_key_workfn(struct work_struct *work)
|
||||
{
|
||||
if (!rng_is_initialized()) {
|
||||
queue_delayed_work(system_unbound_wq, &fill_ptr_key_work, HZ * 2);
|
||||
return;
|
||||
}
|
||||
|
||||
get_random_bytes(&ptr_key, sizeof(ptr_key));
|
||||
|
||||
/* Pairs with smp_rmb() before reading ptr_key. */
|
||||
smp_wmb();
|
||||
WRITE_ONCE(filled_random_ptr_key, true);
|
||||
}
|
||||
|
||||
static int __init vsprintf_init_hashval(void)
|
||||
{
|
||||
fill_ptr_key_workfn(NULL);
|
||||
return 0;
|
||||
}
|
||||
subsys_initcall(vsprintf_init_hashval)
|
||||
|
||||
/* Maps a pointer to a 32 bit unique identifier. */
|
||||
static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
|
||||
{
|
||||
static siphash_key_t ptr_key __read_mostly;
|
||||
unsigned long hashval;
|
||||
|
||||
if (!READ_ONCE(filled_random_ptr_key)) {
|
||||
static bool filled = false;
|
||||
static DEFINE_SPINLOCK(filling);
|
||||
unsigned long flags;
|
||||
if (!READ_ONCE(filled_random_ptr_key))
|
||||
return -EBUSY;
|
||||
|
||||
if (!rng_is_initialized() ||
|
||||
!spin_trylock_irqsave(&filling, flags))
|
||||
return -EAGAIN;
|
||||
|
||||
if (!filled) {
|
||||
get_random_bytes(&ptr_key, sizeof(ptr_key));
|
||||
/* Pairs with smp_rmb() before reading ptr_key. */
|
||||
smp_wmb();
|
||||
WRITE_ONCE(filled_random_ptr_key, true);
|
||||
filled = true;
|
||||
}
|
||||
spin_unlock_irqrestore(&filling, flags);
|
||||
}
|
||||
/* Pairs with smp_wmb() after writing ptr_key. */
|
||||
smp_rmb();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user