From nobody Mon Nov 23 04:43:57 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1792496482; cv=none; d=zohomail.com; s=zohoarc; b=gi/l3sEtGA0QY1L3PBV7OwggHQxRJYtL3jddWk8AfVVEPh1JTvZWR44M+nE9IbKpXsUhImWLk53lroPL1ZQPggLHAgV+U7nSNL/hqQvLSGzezmqnGXAVU1x2hYL8cWhE/fTj7qtCZck+OJFlhyVSACOeP5FR3vDpX40WneS9JZ4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792496482; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Sp9jq6kXtWZZsUwq0XdkMYbhEcu8HadaVkOvMEE9mqE=; b=kWiyRtEp0o9gf9m6UhNRjmtlawXk2kbFt8GYHi7gh3hYboGC8tI54jhl5Q3DBi12lMQzkIjc1XPfpLw8Tm/kpAjRZtUH93EFXWMUhRSu3BBeJgf1bMUhKJX+FME9tKVD67PN+EUG6iHhgkQwPOTLUl0/4gKpfrZXGRmL1ucFm1o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792496482527210.55941687529594; Tue, 20 Oct 2026 04:41:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xJ8D1-0005Mv-9g; Tue, 20 Oct 2026 07:40:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xJ8Cz-0005MX-MA for qemu-devel@nongnu.org; Tue, 20 Oct 2026 07:40:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xJ8Cx-0005ie-5J for qemu-devel@nongnu.org; Tue, 20 Oct 2026 07:40:45 -0400 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-179-iER7kwwmMn2aL75OgZxO1g-1; Tue, 20 Oct 2026 11:40:37 +0000 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 17EBB19541B4; Tue, 20 Oct 2026 11:40:36 +0000 (UTC) Received: from thuth-p1g4.redhat.com (unknown [10.44.48.18]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 744161956089; Tue, 20 Oct 2026 11:40:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1792496441; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Sp9jq6kXtWZZsUwq0XdkMYbhEcu8HadaVkOvMEE9mqE=; b=Y1jCIZu/kAbKhObCQDnJmQxfd5T11Rmy2pZaBsH5E7JFKrsMeRb9fKQUVE/D1DpJ7p58vI XRkDEIZA6t+G/GRy6g7pJjlB1PKpsO1/vf2h9i7d8yENQluIdZD2zvj8VeD1YUG23jzByv K/NPP6UT1LacY5cjLXl6Yr4A435yoos= X-MC-Unique: iER7kwwmMn2aL75OgZxO1g-1 X-Mimecast-MFC-AGG-ID: iER7kwwmMn2aL75OgZxO1g_1792496436 From: Thomas Huth To: qemu-devel@nongnu.org Cc: Tommaso Califano , Junjie Cao , Nikhil Agrawal , Alexander Bulekov , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Peter Maydell Subject: [PATCH] hw/usb/hcd-xhci: More reentrancy guard fixes for the XHCI code Date: Tue, 20 Oct 2026 13:40:31 +0200 Message-ID: <20261020114031.687929-1-thuth@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=thuth@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 9 X-Spam_score: 0.9 X-Spam_bar: / X-Spam_report: (0.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.329, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1792496486421158500 Content-Type: text/plain; charset="utf-8" The XHCI controller's USB port and bus callbacks -- xhci_complete(), xhci_wakeup_endpoint(), xhci_attach(), xhci_detach(), and xhci_wakeup() -- can be invoked from outside any MMIO handler context. The device's mem_reentrancy_guard is therefore not engaged during this processing. A malicious guest can exploit this gap via MSI-X self-targeting: it programs the MSI-X vector's message address to point at the XHCI doorbell register array (both reside in the same BAR0 MemoryRegion). When any of these callbacks posts an event via xhci_event() -> xhci_intr_raise(), the resulting msix_notify() -> pci_msi_trigger() issues an address_space_stl_le() that writes doorbell 0, triggering xhci_process_commands() reentrantly. A CR_DISABLE_SLOT command planted on the command ring then frees XHCITransfer/XHCIEPContext objects that the outer call stack still references, causing a heap use-after-free. Fix this by setting engaged_in_io on the device's mem_reentrancy_guard around the processing in all five callbacks. The guard is set conditionally (only when not already engaged) because these callbacks may also be reached from within a guarded MMIO context. xhci_child_detach() is intentionally left unguarded: it only calls xhci_detach_slot() which passes report=3D0 to xhci_ep_nuke_xfers(), suppressing event generation, so it never reaches xhci_event(). Note: This is similar to the fixes done for CVE-2026-17588, see f91b0544fe ("Set reentrancy guard in timer functions"). CVE: CVE-2026-111819 Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4625 Reported-by: Ken Hsu, Royce Lu, and Bo Qu of Palo Alto Networks Buglink: https://gitlab.com/qemu-project/qemu/-/work_items/4642 Reported-by: Nikhil Agrawal Signed-off-by: Thomas Huth --- hw/usb/hcd-xhci.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/hw/usb/hcd-xhci.c b/hw/usb/hcd-xhci.c index 376bc00264d..5d7a77b8bcf 100644 --- a/hw/usb/hcd-xhci.c +++ b/hw/usb/hcd-xhci.c @@ -3251,49 +3251,73 @@ static const MemoryRegionOps xhci_doorbell_ops =3D { .endianness =3D DEVICE_LITTLE_ENDIAN, }; =20 +static bool xhci_engage_reentrancy_guard(XHCIState *xhci) +{ + bool prev_engaged =3D xhci->parent.mem_reentrancy_guard.engaged_in_io; + + xhci->parent.mem_reentrancy_guard.engaged_in_io =3D true; + return prev_engaged; +} + +static void xhci_restore_reentrancy_guard(XHCIState *xhci, bool engaged) +{ + xhci->parent.mem_reentrancy_guard.engaged_in_io =3D engaged; +} + static void xhci_attach(USBPort *usbport) { XHCIState *xhci =3D usbport->opaque; XHCIPort *port =3D xhci_lookup_port(xhci, usbport); + bool prev_engaged =3D xhci_engage_reentrancy_guard(xhci); =20 xhci_port_update(port, 0); + xhci_restore_reentrancy_guard(xhci, prev_engaged); } =20 static void xhci_detach(USBPort *usbport) { XHCIState *xhci =3D usbport->opaque; XHCIPort *port =3D xhci_lookup_port(xhci, usbport); + bool prev_engaged =3D xhci_engage_reentrancy_guard(xhci); =20 xhci_detach_slot(xhci, usbport); xhci_port_update(port, 1); + xhci_restore_reentrancy_guard(xhci, prev_engaged); } =20 static void xhci_wakeup(USBPort *usbport) { XHCIState *xhci =3D usbport->opaque; XHCIPort *port =3D xhci_lookup_port(xhci, usbport); + bool prev_engaged; =20 assert(port); if (get_field(port->portsc, PORTSC_PLS) !=3D PLS_U3) { return; } + prev_engaged =3D xhci_engage_reentrancy_guard(xhci); set_field(&port->portsc, PLS_RESUME, PORTSC_PLS); xhci_port_notify(port, PORTSC_PLC); + xhci_restore_reentrancy_guard(xhci, prev_engaged); } =20 static void xhci_complete(USBPort *port, USBPacket *packet) { XHCITransfer *xfer =3D container_of(packet, XHCITransfer, packet); + XHCIState *xhci =3D xfer->epctx->xhci; + bool prev_engaged; =20 if (packet->status =3D=3D USB_RET_REMOVE_FROM_QUEUE) { xhci_ep_nuke_one_xfer(xfer, 0); return; } + prev_engaged =3D xhci_engage_reentrancy_guard(xhci); xhci_try_complete_packet(xfer); xhci_kick_epctx(xfer->epctx, xfer->streamid); if (xfer->complete) { xhci_ep_free_xfer(xfer); } + xhci_restore_reentrancy_guard(xhci, prev_engaged); } =20 static void xhci_child_detach(USBPort *uport, USBDevice *child) @@ -3345,6 +3369,7 @@ static void xhci_wakeup_endpoint(USBBus *bus, USBEndp= oint *ep, { XHCIState *xhci =3D container_of(bus, XHCIState, bus); int slotid; + bool prev_engaged; =20 DPRINTF("%s\n", __func__); slotid =3D ep->dev->addr; @@ -3353,7 +3378,9 @@ static void xhci_wakeup_endpoint(USBBus *bus, USBEndp= oint *ep, DPRINTF("%s: oops, no slot for dev %d\n", __func__, ep->dev->addr); return; } + prev_engaged =3D xhci_engage_reentrancy_guard(xhci); xhci_kick_ep(xhci, slotid, xhci_find_epid(ep), stream); + xhci_restore_reentrancy_guard(xhci, prev_engaged); } =20 static USBBusOps xhci_bus_ops =3D { --=20 2.56.0