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=1793212800; b=FcX3owVu+CxnGw1Ix40jmchSrgT8O3t9efePAO4Oj1ggy3zMenxnHYISMRHLtv7sw5D6 JUFVMYJZjGlI/n3uL5Iho4YlfLmAnCknXWjijgbXC/WL9QQe6rw8VGriIKyS2d3uWXLq4 FiwZb0MvFNkakb+z6v734K4d4WDvNAxCm3o8aHKCDvRAeJMuYALYC0Prmn7TvRl2fMfDn PNZLLmpIzcffrIYf97i/n1/VKNnbSHlDo339YJ7BbqMAK9Ktvz0ztrpcYZ3J/XL0k81xs jUz9XThee4oGgwX7FIUJ+VoSZ1upltwc6X45oxX237+Hn3WogaOCsu8LShPj68MSDRg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793212800; 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-Type:Content-Transfer-Encoding; bh=L1aM3O5+IcZqGWzKMzTvBfIbk+ylWT4Lfh7YMBKYkyY=; b=k7Vj/SZDPn1ynG019NbfTlP7RNNCCFD97mVZFVcK1zVKxUawI9JpdsB1AUSQhG5ORNWB nRzb3NUREITYaOkVhelBvxJbA0q02ZidbhVUR1C2FejFWEYtgYplihz1iSSNu6qFbj03T RIDyVselmN+khqzmisCX82apl3UIsd3j7xT8PwYTkiSxTQqa2q2KBOM5IGdJX5R1GRdcL gc8wQPI8M4f/9BvTX9WiJLyXu6XLc1rTVYu5RIQSkdCzoYuR8Vc0ZyIWe2v7HJO+i9HTn JYSrQhha7xGbZi6/c6+dRaLzch8mAVfpne2c6/vJiKcs5URSIDnympmXJ0DONUP28SA== 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=ROolM+DX; 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 8A974606A31A; Wed, 28 Oct 2026 18:40:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=ROolM+DX Received: by smtp.kernel.org (Postfix) with ESMTPSA id D536E1F01825; Wed, 28 Oct 2026 18:39:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1793212800; bh=L1aM3O5+IcZqGWzKMzTvBfIbk+ylWT4Lfh7YMBKYkyY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ROolM+DXBlxgMtV78R/xwciZQTXJ+UPf4ZoCKIZVnyiJbItpeoqDt9jMVyx9iiWcF FPkwQk022SH/nmc7ryDrsJNd1MS7MFep0OrdzDBXNkOQMCQ+sYacjULurd6YDDE1Gb sRJOGwZNkhzpLQsDUsSq9yj23B2UQsHWgX+pi2v8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, stable@kernel.org, Yuchao Zhang , =?UTF-8?q?Micha=C5=82=20Nazarewicz?= Subject: [PATCH 6.1 0280/1887] usb: gadget: f_fs: Fix NULL pointer dereference in FUNCTIONFS_ENDPOINT_DESC Date: Wed, 28 Oct 2026 17:39:36 +0100 Message-ID: <20261028170505.600883385@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261028170459.200466217@linuxfoundation.org> References: <20261028170459.200466217@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Yuchao Zhang commit 9cd072788c76595529eb38f42bf94d23852f8534 upstream. When user space sets up FunctionFS with only full-speed descriptors (e.g., flags = FUNCTIONFS_HAS_FS_DESC) and the gadget operates at a higher speed (such as USB_SPEED_HIGH or USB_SPEED_SUPER), config_ep_by_speed() detects that the function lacks descriptors for the current speed, logs a warning, and falls back to full-speed descriptors. The endpoint is then successfully enabled via usb_ep_enable(), and epfile->ep becomes valid. However, when user space subsequently issues the FUNCTIONFS_ENDPOINT_DESC ioctl to query the endpoint descriptor, ffs_epfile_ioctl() selects the descriptor index solely based on gadget->speed: switch (epfile->ffs->gadget->speed) { case USB_SPEED_SUPER: case USB_SPEED_SUPER_PLUS: desc_idx = 2; break; case USB_SPEED_HIGH: desc_idx = 1; break; default: desc_idx = 0; } desc = epfile->ep->descs[desc_idx]; memcpy(&desc1, desc, desc->bLength); Because user space only supplied full-speed descriptors, epfile->ep->descs[1] is NULL. Dereferencing desc->bLength triggers an immediate kernel NULL pointer dereference panic. Fix this by falling back through lower speed descriptors if the current speed descriptor was not supplied, matching the fallback logic in config_ep_by_speed() and historical f_fs behavior. If still missing, fall back to epfile->ep->ep->desc (the active descriptor), and return -EINVAL safely if no valid descriptor is found. In addition, mark desc as const to match struct usb_ep::desc and avoid discarding qualifiers. Fixes: c559a3534109 ("usb: gadget: f_fs: add ioctl returning ep descriptor") Cc: stable@kernel.org Signed-off-by: Yuchao Zhang Acked-by: MichaƂ Nazarewicz Link: https://patch.msgid.link/20260918005438.81263-2-ndaugoing@gmail.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/function/f_fs.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -1351,8 +1351,9 @@ static long ffs_epfile_ioctl(struct file break; case FUNCTIONFS_ENDPOINT_DESC: { + const struct usb_endpoint_descriptor *desc; + struct usb_endpoint_descriptor desc1; int desc_idx; - struct usb_endpoint_descriptor desc1, *desc; switch (epfile->ffs->gadget->speed) { case USB_SPEED_SUPER: @@ -1366,7 +1367,17 @@ static long ffs_epfile_ioctl(struct file desc_idx = 0; } - desc = epfile->ep->descs[desc_idx]; + do { + desc = epfile->ep->descs[desc_idx]; + } while (!desc && --desc_idx >= 0); + + if (!desc) + desc = epfile->ep->ep->desc; + if (!desc) { + ret = -EINVAL; + break; + } + memcpy(&desc1, desc, desc->bLength); spin_unlock_irq(&epfile->ffs->eps_lock);