* [PATCH] irq_work: Fix use-after-free of hard irq_work on PREEMPT_RT
@ 2026-11-30 9:35 Andrea Parri
0 siblings, 0 replies; only message in thread
From: Andrea Parri @ 2026-11-30 9:35 UTC (permalink / raw)
To: Sebastian Andrzej Siewior, Peter Zijlstra, Thomas Gleixner
Cc: Steven Rostedt, Clark Williams, Jiayuan Chen, Vlastimil Babka,
linux-rt-devel, linux-kernel
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@gmail.com>
---
Found while preparing "irq_work: Fix use-after-free in
irq_work_run_cpu() on PREEMPT_RT", sent at the same time. The two
patches touch different functions and apply in either order.
The schedutil change "cpufreq: schedutil: Run irq_work in hardirq
context on PREEMPT_RT" [1], queued for 7.5, adds another hard item
that sugov_stop() syncs before sugov_exit() frees it.
Tested on a 4-vCPU PREEMPT_RT=y x86_64 guest (v7.4-rc5, KASAN,
PROVE_LOCKING, PROVE_RCU) with the module below. It queues an
IRQ_WORK_HARD_IRQ item on CPU 1, then calls irq_work_sync() and kfree()
on CPU 0. To widen the window, a kprobe holds CPU 1 for 20 ms at the
instruction after the atomic_cmpxchg() in irq_work_single(); the
offsets of those instructions come from objdump of the build.
Before, the first of 10 rounds reports:
BUG: KASAN: slab-use-after-free in irq_work_single+0xf4/0x2a0
Read of size 4 at addr ffff88800c498788 by task swapper/1/0
CPU: 1 UID: 0 PID: 0 Comm: swapper/1 Tainted: G O 7.4.0-rc5 #2 PREEMPT_{RT,(lazy)}
Call Trace:
<IRQ>
dump_stack_lvl+0x5d/0x80
print_report+0x153/0x4c6
kasan_report+0xe4/0x1a0
kasan_check_range+0x125/0x200
irq_work_single+0xf4/0x2a0
irq_work_run+0x7e/0x110
__sysvec_irq_work+0x4f/0x2f0
sysvec_irq_work+0x6e/0x80
</IRQ>
[ ... ]
Allocated by task 142:
[ ... ]
__kmalloc_cache_noprof+0x22b/0x530
irqw_hard_run+0x59/0x1a0 [irqw_hard]
work_for_cpu_fn+0x54/0xb0
[ ... ]
Freed by task 142:
[ ... ]
kfree+0x1bc/0x4c0
irqw_hard_run+0x12b/0x1a0 [irqw_hard]
work_for_cpu_fn+0x54/0xb0
[ ... ]
The buggy address is located 8 bytes inside of
freed 32-byte region [ffff88800c498780, ffff88800c4987a0)
After, 50 rounds with the delay in place report nothing, also with
"irq_work: Fix use-after-free in irq_work_run_cpu() on PREEMPT_RT"
applied. The use-after-free was only seen with the injected delay.
Also compile-tested with PREEMPT_RT=n (x86_64, W=1).
[1] https://lore.kernel.org/all/20261125103451.1385246-1-zhongqiu.han@oss.qualcomm.com/
// SPDX-License-Identifier: GPL-2.0
/*
* Free an IRQ_WORK_HARD_IRQ item right after irq_work_sync() while the
* CPU running it is held, by a kprobe, just after irq_work_single() has
* cleared IRQ_WORK_BUSY. With KASAN, any access to the item by
* irq_work_single() after that point is reported as a use-after-free.
*
* offsets= are the offsets in irq_work_single() of the instructions after
* each copy of the atomic_cmpxchg() that clears IRQ_WORK_BUSY; the
* compiler may emit more than one (build-specific).
*/
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/irq_work.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/smp.h>
static unsigned long offsets[4];
static int nr_offsets;
module_param_array(offsets, ulong, &nr_offsets, 0444);
static int src_cpu, dst_cpu = 1, loops = 10, delay_ms = 20;
module_param(src_cpu, int, 0444);
module_param(dst_cpu, int, 0444);
module_param(loops, int, 0444);
module_param(delay_ms, int, 0444);
struct irqw_hard_item {
struct irq_work work;
};
static DEFINE_PER_CPU(bool, irqw_hard_hold);
static atomic_t ran, held;
/* Runs in hardirq on dst_cpu, just before irq_work_single() clears BUSY. */
static void irqw_hard_func(struct irq_work *work)
{
atomic_inc(&ran);
this_cpu_write(irqw_hard_hold, true);
}
/* Next hit on this CPU is the instruction after our item's cmpxchg(). */
static int irqw_hard_pre(struct kprobe *p, struct pt_regs *regs)
{
if (!this_cpu_read(irqw_hard_hold))
return 0;
this_cpu_write(irqw_hard_hold, false);
atomic_inc(&held);
mdelay(delay_ms);
return 0;
}
static struct kprobe irqw_hard_kp[ARRAY_SIZE(offsets)];
static struct kprobe *irqw_hard_kps[ARRAY_SIZE(offsets)];
/* IPI on dst_cpu: queue the item there (irq_work_queue_on() is not exported). */
static void irqw_hard_queue(void *work)
{
irq_work_queue(work);
}
/* Runs on src_cpu: queue on dst_cpu, wait, free. */
static long irqw_hard_run(void *arg)
{
int i;
for (i = 0; i < loops; i++) {
struct irqw_hard_item *it = kzalloc(sizeof(*it), GFP_KERNEL);
if (!it)
return -ENOMEM;
it->work = IRQ_WORK_INIT_HARD(irqw_hard_func);
smp_call_function_single(dst_cpu, irqw_hard_queue, &it->work, 1);
irq_work_sync(&it->work);
kfree(it);
msleep(2 * delay_ms);
}
return 0;
}
static int __init irqw_hard_init(void)
{
long ret;
int i;
if (!nr_offsets || src_cpu == dst_cpu || !cpu_online(src_cpu) ||
!cpu_online(dst_cpu))
return -EINVAL;
for (i = 0; i < nr_offsets; i++) {
irqw_hard_kp[i].symbol_name = "irq_work_single";
irqw_hard_kp[i].offset = offsets[i];
irqw_hard_kp[i].pre_handler = irqw_hard_pre;
irqw_hard_kps[i] = &irqw_hard_kp[i];
}
ret = register_kprobes(irqw_hard_kps, nr_offsets);
if (ret)
return ret;
ret = work_on_cpu(src_cpu, irqw_hard_run, NULL);
unregister_kprobes(irqw_hard_kps, nr_offsets);
pr_info("irqw_hard: loops=%d ran=%d held=%d ret=%ld\n",
loops, atomic_read(&ran), atomic_read(&held), ret);
return ret ? ret : -EAGAIN; /* nothing to keep loaded */
}
module_init(irqw_hard_init);
MODULE_DESCRIPTION("Free a hard irq_work while irq_work_single() is past the BUSY clear");
MODULE_LICENSE("GPL");
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 73eabcbdcd50c..0969f62d94c71 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);
}
--
2.53.0
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-30 9:35 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 9:35 [PATCH] irq_work: Fix use-after-free of hard irq_work on PREEMPT_RT Andrea Parri
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®