From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796385426; b=XFEN/JdgRginGAYBsMHwNXAAVK7szqyOzxCTeyfLFWHFCIVldGTtkOi/uhpx5VPYFjbx 7qz6PaSdc9RL0QRgF6mpLAl1x+ss3DwvqS1PnPkhshynP1/+QI2Gn/x0QCa9W3tYaK32T 8SvDKSPLI+SXxEI5qsmVHf7Dy/++WWz1pjlalW9Kx8pjNfrwQwKcbx9YptO/cy7VAX9Gl zQtLNRpQXjr3gQ95EgPxPbLoBpprad/sIDv5aMHeQsU6MeY+azsvkpntaEeGft9Irb4pd 4V/CJkhj5JT3KCsWR1HEhUk4LuwZMwmOsl/THHSyPFf2f4OotfWUbT8kxLIOTJuL1Gw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796385426; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=dQHiFDwUtQUTntrEiEa8o9x7U70lnHK2mgbFds16k+s=; b=JxYHpvmnhd9Vh6gj+zH88PHjZyXeGY0FvgKQnkGU4cNpzVdFW5bUHiqR73OkvGI1g+cb Gl7rVz3CYJEYdgrrJQngBIQcDTUKQjA9El1kfz5hqAug2x6bz3A+j2Jgjx8o+4XS9Q8MF sxYEPL1MmmVHLocrtbVtfiH30w2V9lpNWA2f+h9dXM8Tu2gFOh71L+a34pwnjOFa+VQdq QPOlxLcl8dgo6B6r22sKcgYK/FYZuS5C7tkbOv1yanqJm9TE+JWijM3PHRnPxz24IjsWg +ah6jri5VjoPfcuCCcWBU4T6t9VQchPP3Bk8SpmBue5d9LUDv9U/QFAFpXOWRhiOktw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=gdT3eurn; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BA55606A322; Fri, 4 Dec 2026 11:57:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=gdT3eurn Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74ED01F0092C; Fri, 4 Dec 2026 11:57:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796385425; bh=dQHiFDwUtQUTntrEiEa8o9x7U70lnHK2mgbFds16k+s=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=gdT3eurnORTbD/7Sh/VLGeIkfh5zK6fptSeriwQOu+8836Hg7RCPun8wgKulhkh80 q1zgzvSMTMsfeZ5G6o0haglW/Phb8Vuagkf7AbQtkNv66Kzs8aND7n54P7+UOIU6gA ObFxfoCYsNwPGbAUb4YivQJdLeGd0+JjUprcXotY= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Qihang , Juergen Gross Subject: [PATCH 6.6 0553/1167] xen/privcmd: fix double free when an irqfd is shut down twice Date: Fri, 4 Dec 2026 18:59:40 +0900 Message-ID: <20261204100716.458263522@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@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 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Qihang commit 72a900c666adb97df77b05cf8540c4117f9c83ba upstream. irqfd_deactivate() takes the irqfd off irqfds_list and queues irqfd_shutdown(). queue_work() is not a once-only gate: it refuses to enqueue a work item only while WORK_STRUCT_PENDING is set, and process_one_work() clears that bit before it calls the work function. So nothing records that the shutdown work has already been queued, and nothing protects the window between the start of irqfd_shutdown() and the kfree() at its end. Two paths call irqfd_deactivate() for the same irqfd: - privcmd_irqfd_deassign(), from the PRIVCMD_IRQFD_FLAG_DEASSIGN ioctl; - irqfd_wakeup(), with EPOLLHUP set, when the last reference to the eventfd file goes away, which runs wake_up_poll(&ctx->wqh, EPOLLHUP) from eventfd_release(). irqfds_lock serialises the two calls, but it does not make the second one a no-op, and eventfd_ctx_fdget() takes a reference to the eventfd_ctx, not to the struct file, so the last fd can still be closed while the ioctl is running: CPU0 CPU1 eventfd_ctx_fdget() spin_lock(irqfds_lock) list_del_init(&kirqfd->list) queue_work(&kirqfd->shutdown) close(fd) eventfd_release() wake_up_poll(EPOLLHUP) irqfd_wakeup() spin_lock(irqfds_lock) irqfd_deactivate() again If the work item has already started by then, its pending bit has been cleared and the second queue_work() succeeds. irqfd_shutdown() then runs twice, so eventfd_ctx_remove_wait_queue(), eventfd_ctx_put() and kfree() each hit the same object a second time; and when the second queue_work() lands after the irqfd has been freed, the workqueue list operations write to freed memory. virt/kvm/eventfd.c has the same two callers and has had this guard since the original irqfd support (721eecbf4fe99): irqfd_wakeup() calls irqfd_deactivate() only if irqfd_is_active(). The comment there explains why that is enough: the other side deactivates the irqfd before it takes it off the waitqueue, and irqfd_shutdown() needs the waitqueue lock, which this callback holds, so the irqfd cannot be freed under it. Do the same here. list_del_init() leaves the list head empty and the irqfd stays off the list until kfree(), so it serves as a one-way "active" flag. The test is in irqfd_deactivate() rather than in irqfd_wakeup() because privcmd_irqfd_deassign() walks irqfds_list and can therefore never reach an inactive irqfd; only the wakeup path can, and a single test covers both callers. Fixes: f8941e6c4c712 ("xen: privcmd: Add support for irqfd") Cc: stable@vger.kernel.org Signed-off-by: Qihang Reviewed-by: Juergen Gross Signed-off-by: Juergen Gross Message-ID: <20261101163251.55099-1-q.h.hack.winter@gmail.com> Signed-off-by: Greg Kroah-Hartman --- drivers/xen/privcmd.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) --- a/drivers/xen/privcmd.c +++ b/drivers/xen/privcmd.c @@ -872,10 +872,25 @@ struct privcmd_kernel_irqfd { poll_table pt; }; +static bool irqfd_is_active(struct privcmd_kernel_irqfd *kirqfd) +{ + lockdep_assert_held(&irqfds_lock); + + return !list_empty(&kirqfd->list); +} + static void irqfd_deactivate(struct privcmd_kernel_irqfd *kirqfd) { lockdep_assert_held(&irqfds_lock); + /* + * Both the deassign ioctl and the eventfd wakeup path can reach + * this function. list_del_init() marks the irqfd inactive, so + * only the first caller may queue the shutdown work. + */ + if (!irqfd_is_active(kirqfd)) + return; + list_del_init(&kirqfd->list); queue_work(irqfd_cleanup_wq, &kirqfd->shutdown); }