[tip: irq/core] irq_work: Fix use-after-free of hard irq_work on PREEMPT_RT
From: tip-bot2 for Andrea Parri
Date: Fri Dec 04 2026 - 16:02:47 EST
The following commit has been merged into the irq/core branch of tip:
Commit-ID: f173884be701588ad63bc0fadef13fe9b1da1574
Gitweb: https://git.kernel.org/tip/f173884be701588ad63bc0fadef13fe9b1da1574
Author: Andrea Parri <parri.andrea@xxxxxxxxx>
AuthorDate: Mon, 30 Nov 2026 10:35:15 +01:00
Committer: Thomas Gleixner <tglx@xxxxxxxxxx>
CommitterDate: Fri, 04 Dec 2026 21:56:06 +01:00
irq_work: Fix use-after-free of hard irq_work on PREEMPT_RT
On PREEMPT_RT, when the architecture has an irq_work interrupt,
irq_work_sync() on an IRQ_WORK_HARD_IRQ item spins until IRQ_WORK_BUSY
clears and returns without waiting for an RCU grace period, so the
caller can free the item right away. irq_domain_remove_sim(), for
example, frees the structure holding its hard item just after
irq_work_sync().
irq_work_single() reads work->node.a_flags again after clearing
IRQ_WORK_BUSY, in irq_work_is_hard(), to decide whether to call
rcuwait_wake_up(). For a hard item that read can hit freed memory, and
if the freed memory has IRQ_WORK_HARD_IRQ clear, rcuwait_wake_up() then
dereferences work->irqwait as well:
CPU 0 (hardirq) CPU 1
irq_work_single(work)
work->func(work)
atomic_cmpxchg() // clear BUSY
irq_work_sync(work)
// spins until !BUSY
free work
irq_work_is_hard(work) // use-after-free
Test IRQ_WORK_HARD_IRQ in the flags that irq_work_single() read on
entry instead. The bit is set when the item is initialized and never
changes afterwards, so the result is the same. With an irq_work
interrupt, irq_work_single() then no longer touches a hard item after
clearing IRQ_WORK_BUSY. The rcuwait_wake_up() path, which relies on
the synchronize_rcu() in irq_work_sync(), is unchanged.
The bug was found with the help of an LLM, which was also used to write
the fix, the changelog and the test.
Fixes: 09089db79859 ("irq_work: Also rcuwait for !IRQ_WORK_HARD_IRQ on PREEMPT_RT")
Signed-off-by: Andrea Parri <parri.andrea@xxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Link: https://patch.msgid.link/20261130093516.5653-1-parri.andrea@xxxxxxxxx
---
kernel/irq_work.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/irq_work.c b/kernel/irq_work.c
index 73eabcb..0969f62 100644
--- a/kernel/irq_work.c
+++ b/kernel/irq_work.c
@@ -227,7 +227,7 @@ void irq_work_single(void *arg)
*/
(void)atomic_cmpxchg(&work->node.a_flags, flags, flags & ~IRQ_WORK_BUSY);
- if ((IS_ENABLED(CONFIG_PREEMPT_RT) && !irq_work_is_hard(work)) ||
+ if ((IS_ENABLED(CONFIG_PREEMPT_RT) && !(flags & IRQ_WORK_HARD_IRQ)) ||
!arch_irq_work_has_interrupt())
rcuwait_wake_up(&work->irqwait);
}