🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: muc.lists.freebsd.bugs
Article #642

[Bug 301029] usb(4)/xhci(4): UAF race between usbd_transfer_unsetup_sub() and xhci_interrupt_poll() on struct usb_page_cache

#642
From: bugzilla-noreply
Date: Mon, 23 Nov 2026 01:26
123 lines
7414 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301029

            Bug ID: 301029
           Summary: usb(4)/xhci(4): UAF race between
                    usbd_transfer_unsetup_sub() and xhci_interrupt_poll()
                    on struct usb_page_cache
           Product: Base System
           Version: 15.1-RELEASE
          Hardware: amd64
                OS: Any
            Status: New
          Severity: Affects Some People
          Priority: ---
         Component: kern
          Assignee: bugs@FreeBSD.org
          Reporter: viktor.dolgopiatov@icloud.com

Environment:
FreeBSD 15.1-RELEASE-p3, releng/15.1-n283611-88e7371d9dc2 GENERIC amd64.
Intel N100 (Alder Lake-N), 2x Intel xHCI controllers (one in active use,
one unused/empty on this board). Panic reproduced once in the field on
an embedded appliance; race window independently confirmed live via
DTrace on a test unit (see below). Stock kernel, no local patches.

Summary:
A single, field-observed kernel panic (page fault in usb_pc_cpu_invalidate(),
called from xhci_activate_transfer()) pointed to a stale/UAF
bus_dma_tag_t inside a struct usb_page_cache. Deep analysis (lldb on the
vmcore, source review, and later a live DTrace capture) identified a
structural lock-discipline gap in usbd_transfer_unsetup_sub()
(sys/dev/usb/usb_transfer.c) that races with xhci_interrupt_poll()
(sys/dev/usb/controller/xhci.c) under real-world traffic on a busy
xHCI controller.

Root cause:
usbd_transfer_unsetup_sub() releases USB_BUS_LOCK before tearing down
the xfer's own page caches:

    USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
    ...
    usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
    USB_BUS_UNLOCK(info->bus);                 /* <-- lock released here */
    #if USB_HAVE_BUSDMA
    pc = info->dma_page_cache_start;
    while (pc != info->dma_page_cache_end) { usb_pc_free_mem(pc); pc++; }
    pc = info->xfer_page_cache_start;
    while (pc != info->xfer_page_cache_end) { usb_pc_dmamap_destroy(pc); pc++;
}
    usb_dma_tag_unsetup(&info->dma_parent_tag);
    #endif

usb_pc_dmamap_destroy() is the only place that zeroes pc->tag; it runs
with no lock held. Meanwhile xhci_interrupt_poll() always runs under
USB_BUS_LOCK and, via xhci_activate_transfer()/xhci_generic_done_sub(),
calls usb_pc_cpu_invalidate(td->page_cache)/usb_pc_cpu_flush(td->page_cache)
on the exact same usb_page_cache array.

The only safety net usb_pc_cpu_invalidate()/usb_pc_cpu_flush() have is:

    if (pc->page_offset_end == pc->page_offset_buf) return; /* "not loaded" */

usb_pc_free_mem() does NOT reset page_offset_buf/page_offset_end when it
frees pc->buffer, so this guard never fires for a pc that has just been
(or is concurrently being) freed by usbd_transfer_unsetup_sub() on
another CPU, and the racing call proceeds to dereference pc->tag/pc->map,
which may by then be stale.

Why this is not just theoretical:
Live DTrace on a test unit (2 USB RNDIS network/video interfaces + 2
USB-serial consoles on the same xHCI controller) showed
usbd_transfer_unsetup_sub() invoked roughly every 15-20 seconds after
boot. A clean DTrace capture measured the controller's true steady-state
interrupt rate at ~7400-7900/s (confirmed independently via vmstat -i),
with usb_pc_cpu_invalidate()+usb_pc_cpu_flush() combined firing at
~59000/s.

Update (2026-11-23): a 2-hour live DTrace capture (same probes, normal
production load) recorded 119 usbd_transfer_unsetup_sub() teardown
windows and 437,314,015 usb_pc_cpu_invalidate()+usb_pc_cpu_flush() calls
total; 15,823 of those reads from another CPU landed inside an open
teardown window (confirming the race window is routinely entered), but
none of those 15,823 happened to hit a pc pointer inside the 119
concurrently-freed ranges in this particular run. Two earlier shorter
captures (30s, 3min) on the same unit caught 0 teardowns at all, so hit
rate is highly workload-dependent.

Suggested fix:
Rather than extending USB_BUS_LOCK's hold time over the bus_dmamem_free()/
bus_dmamap_destroy()/bus_dma_tag_destroy() calls, set the existing
"not loaded" sentinel while STILL holding the lock, before USB_BUS_UNLOCK():

    for each pc about to be destroyed (both loops):
        pc->page_offset_buf = pc->page_offset_end;
    USB_BUS_UNLOCK(info->bus);
    /* existing unlocked free/destroy loops unchanged */

We checked (by grepping all of sys/dev/usb/) that no other code path
dereferences pc->tag/pc->map without going through this same guard, and
that usb_pc_free_mem()/usb_pc_dmamap_destroy()/usb_dma_tag_unsetup() do
not themselves depend on page_offset_buf/page_offset_end, so this
sentinel write should not have other side effects. We have not
submitted a formal patch/diff yet; happy to if useful.

How to reproduce:
No reliable minimal repro (the original panic happened once, in the
field, under real load). The lock-discipline gap is visible by code
inspection; the race WINDOW (not the exact same-pointer collision) is
directly observable with a DTrace script probing
usbd_transfer_unsetup_sub:entry/return, usb_pc_cpu_invalidate:entry,
usb_pc_cpu_flush:entry on any amd64 box with an actively-used xHCI
controller carrying more than one USB device generating steady traffic.

-- 
You are receiving this mail because:
You are the assignee for the bug.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Technical Details
Message-ID: <bug-301029-227@https.bugs.freebsd.org/bugzilla/>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail
From: bugzilla-noreply@freebsd.org
Newsgroups: muc.lists.freebsd.bugs
Subject: [Bug 301029] usb(4)/xhci(4): UAF race between
 usbd_transfer_unsetup_sub() and xhci_interrupt_poll() on struct
 usb_page_cache
Date: Mon, 23 Nov 2026 01:26:46 +0000
Organization: Newsgate at muc.de e.V.
Approved: news-admin@muc.de
Distribution: world
Message-ID: <bug-301029-227@https.bugs.freebsd.org/bugzilla/>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2";
	logging-data="60668"; mail-complaints-to="news-admin@muc.de"
X-Original-To: bugs@mlmmj.nyi.freebsd.org
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim;
	t=1795397206;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:mime-version:mime-version:content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding;
	bh=FhuCGw8Pnlqe+EHqllVnDSRcdyMIFtorpjBfWhbZShs=;
	b=H61reU4lFDFL9gs7wMmHKMRWWxCXaptdGjOAhmQaRez9xxO1eHcSprSystnIwvSi0aHhbQ
	hQeibL3DBZL8eCgPbqoyqqY5v8Bagd0RBhR3qzl2nRIJviXaD+y8cPPf20r98P2fwja4Vw
	yZtM6KsgLNkjcxEbGiiY/XvtmCchdg8iXoebkkl4k3Xo6l4CnANxCe0s6kvTRdoEntbI1v
	Yx8DyMrfuCPhyxkzKS+sIO0vjPLtDpebYA/Acw2Ah/mbzIpc/86iVbBEXwdLVB/QEdW444
	CdozuuJa2QmjzD2e3NplW25VxdoHuNZ8SdIKqhQhWFy2yvpzXm2AMb13qLmumA==
X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f
Auto-Submitted: auto-generated
Precedence: list
X-Newsgate-Id: f02c70b8849f+
X-No-Archive: Yes