mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Liu Chao <liuc63@xiaopeng.com>
To: Greg KH <gregkh@linuxfoundation.org>
Cc: Andrey Konovalov <andreyknvl@gmail.com>,
	Gopi Krishna Menon <krishnagopi487@gmail.com>,
	Kees Cook <kees@kernel.org>, Felipe Balbi <balbi@kernel.org>,
	Guanglei Zhu <zhugl3@xiaopeng.com>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] usb: gadget: raw_gadget: fix dangling ep->desc on enable failure
Date: Tue,  3 Nov 2026 10:59:58 +0800	[thread overview]
Message-ID: <20261103025959.4007679-1-liuc63@xiaopeng.com> (raw)
In-Reply-To: <2026102733-cameo-upturned-bf2a@gregkh>

On Tue, Oct 27, 2026 at 06:58:44AM +0100, Greg KH wrote:
> How was this found and tested?

By code audit of the raw-gadget endpoint ownership, not from a crash report
or a fuzzer.

Reaching the failing path from userspace is straightforward.
usb_gadget_ep_match_desc() does not look at bDescriptorType, but dummy_hcd's
dummy_enable() does and rejects anything that is not USB_DT_ENDPOINT with
-EINVAL. So a single USB_RAW_IOCTL_EP_ENABLE with an otherwise valid
descriptor (0x81, bulk, 512 bytes, i.e. ep1in-bulk at high speed) and
bDescriptorType = 0 makes usb_ep_enable() fail and drops us into out_free:

  raw-gadget.0 gadget.0: fail, usb_ep_enable returned -22

one line before kfree(desc), with ep->ep->desc still pointing at it.

Tested on mainline with CONFIG_USB_RAW_GADGET=y, CONFIG_USB_DUMMY_HCD=y and
KASAN, userspace side running in a QEMU guest: the descriptor gets freed and
ep->ep->desc keeps the stale value.

I could not find any in-tree reader of that pointer along this path, though.
usb_ep_enable() only reads a descriptor assigned right before it,
raw_process_ep_io() and the halt/wedge ioctls return early on the endpoint
state, and dummy_hcd keeps its own private descriptor pointer which
dummy_disable() clears, so the value left behind is only the core struct
usb_ep.desc. So a stock kernel stays silent under KASAN: as of today this is
a latent dangling reference, not a reachable crash. Reading the pointer from
a temporary probe patch (not part of the submission) right after the kfree()
gives:

  BUG: KASAN: slab-use-after-free in raw_ioctl
  Read of size 1 at 0xffff88800a7425a0 by task repro/71
  ...
  Allocated by task 71:
    memdup_user+0x26/0x80
    raw_ioctl+0xbfb/0x1ea0
  Freed by task 71:
    kfree+0x189/0x3b0
    raw_ioctl+0x1648/0x1ea0

and with the patch applied there is nothing left to read.

What makes it worth fixing anyway is that the stale pointer outlives the
file descriptor: a UDC's struct usb_ep is owned by the UDC, not by
raw_gadget, so the dangling value survives dev_free() and is still there for
whichever gadget driver binds that UDC next. The core keeps the opposite
invariant elsewhere -- usb_ep_autoconfig_ss() sets ep->desc = NULL,
composite.c assigns _ep->desc before enabling, and f_fs reads ep->ep->desc.

thanks,

      reply	other threads:[~2026-11-03  3:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27  3:23 Liu Chao
2026-10-27  5:58 ` Greg KH
2026-11-03  2:59   ` Liu Chao [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261103025959.4007679-1-liuc63@xiaopeng.com \
    --to=liuc63@xiaopeng.com \
    --cc=andreyknvl@gmail.com \
    --cc=balbi@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=kees@kernel.org \
    --cc=krishnagopi487@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=zhugl3@xiaopeng.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®