From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796031335; b=JdhIVW1xwxElRUKB0sL4RqFhnXALyyfLQQaIi9HsbksAaltsj6Tj9pgduprQby/g9q5/ VydYsCBc/SRhG4RHKNzlOTw9olySdH/9TMQ2pSqfHv95u1e7r1snq93b6pOnesWP0rH5x Dkf7mQRTeI61c2X77H+0eJ3NVbpWuxXMwhHUzerYg2uaxs/xinKyuQ6WPlSibnENqUs9B 8hZ5h5sOnF+ZHxX34Ym93Yi4vbzPthudLzq246kp5ygnGvE2b0GZ+dfr3mgvf1WiZOfBW PLOXru77q7G7v9w06IyKHdS+wzzs/eKi/LImBxKkguHBOKKY0+1g78+nPUOZ6O40+Sg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796031335; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=gJuxdwm0eYNmOhD+kJgShuV71WjJ0ZKtPDK8Ts+n6Ok=; b=ACWG87nyaIWtvBRz65QnLGVbYdI9hSW5lyFIFJ6zaytlo515BJe5ptXut7sWek6GNsJj j1shQFAEF11s+L4nifliy21vFFi0WsG7CXWgmcVIX17ktGOrhSIfzjhMjIa7U2eRA3B// 9f5wrTKqTjvBky6jMDq/2/SDGZWg1w/2sQeGZGHKPGII6fQdC+07lQ4M3p/ZmqKLGvGxC CqMNKslMHKaQIC9icM5zlq0WIuhmrv+okyOZJ2yPq/aO23hLlGHlKezlEQotQ7tBXEzMi 9hQa2OSXIkh5uyp2W+W6CmMU5BPnAZy1Qt0mdKmN9Y+sjvT3/llTA97sZK/a5W18KLg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bPGiu6dw; arc=none smtp.remote-ip=209.85.128.53 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 047A6606A339 for ; Mon, 30 Nov 2026 09:35:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 047A6606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bPGiu6dw Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4a35a60499eso78254945e9.0 for ; Mon, 30 Nov 2026 01:35:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796031330; x=1796636130; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=gJuxdwm0eYNmOhD+kJgShuV71WjJ0ZKtPDK8Ts+n6Ok=; b=bPGiu6dwighYtR4aJYlQxbS4L2UQmp+fCsWLsxZv00cnHp1FR89kzFe77ahepm+fd4 6TlFCxWOtEqin2Edz0zEBETqbYxgbkDGujkyo82UcsS6xYotFdQIm9/TWwpQuLfNub73 kb8HRVmnZHubqwhS4QAoZ3LV9qF+vgAYAZ0iMXiqbJELuY8UPRWOjlJknHRq7HyzArzi rWizrd+IUhT99MxKK0WHhlkOGT2mmq0BN5HCC0ujtyGTNZ8ZBbAEYF7Dzl061LuZWfCL SLGmfXwYDzHKKbVRYOHTMrmMvE8E0TF3BFaL7E9FkxW1cNrOlbP2JAvAGmSgAEXnhCYS GB8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796031330; x=1796636130; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=gJuxdwm0eYNmOhD+kJgShuV71WjJ0ZKtPDK8Ts+n6Ok=; b=sEw2F5jVNdXkkJ7U6npD0jRO+L93FfnXhn6QS+D6tSyBQZkUSPP/FojrEVdQBDEg9n AKZxwRlfOaY3eoFYsZIQNWwC8koHT+RlEi25+BrnTqDFUvg2eB2PciVa7g2HuCnfWekA oX1eZVoePQ11H0fOuVxcN+RLqs+uSeAcXj4MYWUa2QCgk6nFKDyqNNB91fonywJRdDBk HJElTHNRQTsfNkhQUETrHP/CUdEQ9EHCkn3xqDQ0fBf52mA7kryz5mOF+kwkxHpLlo6T zJHGU+UKogZlY6I3OU5MeAOUCEfR4yxrpDB7qLDsteUiuFfbcbDvWXs1lpbARbf+rECT RkQg== X-Forwarded-Encrypted: i=1; AL+hErEF4v0tkDBegv3KeqFUKlgdKPJlw7MaLUnf26gooWW/OpwOvuDUtb3DohqG2sjDRHwyCp6Nx1ZFoKjikT0=@vger.kernel.org X-Gm-Message-State: APuU4Z+jL5g46iccJ+1gN/CeQQ8Iw8ArBJBVi8SI7inNiCwflFDxnzjn WVFlLJdvPk8V3/bibUxUKSIGFqtqV7ro6h3o2t13YWX+lE/bGsS6x9L4 X-Gm-Gg: AZFdTZfYwExFVZuZxj1lTOD5jIwhZwbMIRzrGwc+ftLCHx7axP0Gq88LccAw0x/odWR /3beyAgFGXjs1IU3g+hQ8M3J3LvrbON3iPHnEDf+Uv0CcxtlDvEAoEBzFRJ/lOJJ06OWFCU/Gnp bRGpnKamw358V1eS8TospSfXsLTk89o+MZ6bjFa09TdxNN4HFnw7Z9ZzYaldlIuKUIbH0Oz34KC UCnSb7HfiNXKsMmzaQTnbo/ATLSmnISwLYb8EyfBboq4IjhFtCLem7QGCaJw8XOSbH7UvC0nCWG +oE54xKMA0+XvqdZLELIv+AmdH5bO+LmOn29jrkF0Dh8dtZdakD/bf4OWUjLTuJttNL6DZ8B8DD 5EM5IPUpBp9QnWdihkQ6KoUdJ/xtrNZLYU9jHfmZK6OrLaAKiO0de/jJE6zSZUwW6stACztOp5C 2rXWkOjW8wJBdYU3oQhNORYqcdwvhHrxgncWI42hYBHIoGs4FO2qjiped/yzRFo41zEw3YUCe+3 HJDIxDdpVRKYyrO3T5Kb1+5r7Mi5LUYpQok+sLYp0SBZjZqVgNsznFUk7b0meVdZ5pPhuqw2vjq 4Xhf6HhoEBp/n3hjAkERgXWQrohggsj8btKdEjLDdabGGWDKy9apB1JQkacIkyPNbMHHdXBR/Lb fpClfH7WRLRcEbqzdde3XgiUWe2oo1tSSh/GS8g== X-Received: by 2002:a05:600c:4f84:b0:4bb:5f3b:25a9 with SMTP id 5b1f17b1804b1-4bb88820f39mr610832195e9.30.1796031330278; Mon, 30 Nov 2026 01:35:30 -0800 (PST) Received: from andreayoga.localdomain (93-42-14-189.ip84.fastwebnet.it. [93.42.14.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6c61f558sm65429049f8f.58.2026.11.30.01.35.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 01:35:29 -0800 (PST) From: Andrea Parri To: Sebastian Andrzej Siewior , Peter Zijlstra , Thomas Gleixner Cc: Steven Rostedt , Clark Williams , Jiayuan Chen , Vlastimil Babka , linux-rt-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] irq_work: Fix use-after-free of hard irq_work on PREEMPT_RT Date: Mon, 30 Nov 2026 10:35:15 +0100 Message-ID: <20261130093516.5653-1-parri.andrea@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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: 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 [ ... ] 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 #include #include #include #include #include #include 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