klist: avoid accesses after waking klist_remove()

klist_remove() waits until a node is unreferenced so that its caller can
free the containing object.  klist_release() currently publishes
waiter->woken and wakes the waiter before its final accesses to the waiter
and node.  klist_remove() can then return, allowing its stack waiter and
the containing object to be freed or reused while klist_release() is still
running.  In particular, bus_remove_driver() can free drv->p while
__device_attach() walks the same bus klist with bus_for_each_drv().

On an arm64 Cortex-A72 system, an unpatched 7.2.0-rc3 kernel with
CONFIG_PREEMPT_RT=y and CONFIG_KASAN=y reproduced the bug through the
in-tree I2C/at24 path.  KASAN reported a use-after-free in
klist_dec_and_del() reached from klist_next()/bus_for_each_drv() while
at24 was being unregistered.

Clear n_klist and take a task reference before publishing woken.  Use
release/acquire accesses for that publication and wake the referenced
task.  The task reference keeps the waiter task alive if it returns and
exits before wake_up_process().

Link: https://lore.kernel.org/[email protected]
Fixes: 8b0c250be4 ("[PATCH] add klist_node_attached() to determine if a node is on a list or not.")
Fixes: 210272a284 ("driver core: Remove completion from struct klist_node")
Signed-off-by: Karl Mehltretter <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Assisted-by: LLM
Cc: Danilo Krummrich <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Matthew Wilcox (Oracle) <[email protected]>
Cc: "Rafael J. Wysocki" <[email protected]>
Cc: <[email protected]>
This commit is contained in:
Karl Mehltretter
2026-10-06 19:02:53 -07:00
committed by Andrew Morton
parent 34dbdb7e23
commit 732e3e1023
+12 -4
View File
@@ -36,6 +36,7 @@
#include <linux/klist.h>
#include <linux/export.h>
#include <linux/sched.h>
#include <linux/sched/task.h>
/*
* Use the lowest bit of n_klist to mark deleted nodes and exclude
@@ -187,18 +188,24 @@ static void klist_release(struct kref *kref)
WARN_ON(!knode_dead(n));
list_del(&n->n_node);
knode_set_klist(n, NULL);
spin_lock(&klist_remove_lock);
list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
struct task_struct *p;
if (waiter->node != n)
continue;
p = waiter->process;
get_task_struct(p);
list_del(&waiter->list);
waiter->woken = 1;
/* Publish only after the final waiter and n accesses */
smp_store_release(&waiter->woken, 1);
mb();
wake_up_process(waiter->process);
wake_up_process(p);
put_task_struct(p);
}
spin_unlock(&klist_remove_lock);
knode_set_klist(n, NULL);
}
static int klist_dec_and_del(struct klist_node *n)
@@ -250,7 +257,8 @@ void klist_remove(struct klist_node *n)
for (;;) {
set_current_state(TASK_UNINTERRUPTIBLE);
if (waiter.woken)
/* Pairs with the release store in klist_release() */
if (smp_load_acquire(&waiter.woken))
break;
schedule();
}