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=1795619916; b=UO0HegV0+Pif0p4V/XA5rELBuZY2Tvl8GFgWAEK9iIMn4QQGBdbnM3Q0c/xibQDCBJS0 q9znR47QDLUlT7c0/GEkmYMDax2RU1mD5FtXONvLpI9XFZp8Bf7l1cwRMZh7ICI6nJtTp p1bCbysy6ZyexH8HTD9XJYIzNZO4ikNSZ6hiJXLFFJ9qfQV35F3Z1cC04nrm1g+sQGBMe tDgEr+GnxfUxtBRj1Woo8UBBLhWTtEnhsetg+6fowc6No7UmDIU39rZTaf5un600U1Phx hx7pQVRUkA4bvFLUOw97qvyYR2L6yRBaWhz5jC4w9PkTZ2IOukm9/wjX7Zs3BbWyPyg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795619916; 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=6U/yQZUParuux68qx32Bt9nBDikDwUVyC/+8dnsNYQE=; b=OoowtbpYJ/RjuXlTi3q9aKgxy76vm42VuwhZeZpIAFKafdq6QNhhPZ0rynKz0z81HnU0 Q7OMbPBvPBerTb+Jd7UFDFRfj27J4hynPKmnZFJSROmxjOAQ2YTqIla3GLNewQ2cfYqfb tRfNNRJ3Q7HhG73rY5DlVaTj8NKucv07btQ2e8NOGFAmBy1Uk5evnVeyG9BPSmqXL1wjL O6KK9Rfs1/uQLQY0w6X+Tj7bKVk/w/PvceOdsAThWvs+UXX5I2me5WhUdcrte+P+3Fm7g 92EheX0DvKoldLRZ9CuHbr3B1GfjMZAGSgsIHw7E7F7F925I083PO2olfVvJulaqpuA== 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=0twcjCND; 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 77FFA606A31E; Wed, 25 Nov 2026 15:18:36 +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=0twcjCND Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A0571F000C4; Wed, 25 Nov 2026 15:18:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795619915; bh=6U/yQZUParuux68qx32Bt9nBDikDwUVyC/+8dnsNYQE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=0twcjCNDd1ABgjqCilBEg2GcLObEcvgZVo04eaCLQSJoXhkEl9iUibDK2U7i4X+P+ JeSt3cyFMbLlIinLUAIW3pZk8IKuyXOKMWDF3SxtNr1RT2BBMVN9EOpUdaHcRuNLMw MhAKqX/0VZvuNjrYOtF6p7nndAFw8ACeUNOYjQZI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Qihang , Juergen Gross Subject: [PATCH 7.3 0741/1005] xen/privcmd: fix double free when an irqfd is shut down twice Date: Wed, 25 Nov 2026 15:34:39 +0100 Message-ID: <20261125143839.112065812@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125143821.729802100@linuxfoundation.org> References: <20261125143821.729802100@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 7.3-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 @@ -901,10 +901,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); }