Skip to contentSkip to conversation

usb: gadget: aspeed_udc: validate pointers before dereferencing in ast_udc_ep_enable()

PATCH In review TAKANO Mitsuhiro 3 participants 3 messages Nov 28 – Dec 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: aspeed_udc: validate pointers before dereferencing in ast_udc_ep_enable()

In ast_udc_ep_enable(), desc and ep are dereferenced in local variable initializers (usb_endpoint_maxp(desc), ep->udc, and usb_endpoint_num(desc)) before checking whether _ep or desc is NULL. In addition, the error path calls EP_DBG(ep, ...), which dereferences ep->udc and ep->ep.name when AST_EP_DEBUG is enabled.

Check _ep and desc for NULL first, then derive ep, udc, maxpacket, and epnum. Since _ep is already verified non-NULL, the redundant !ep check after container_of() is also removed.

Fixes 055276c13205 ("usb: gadget: add Aspeed ast2600 udc driver")

---
Verification: compile-tested on x86_64 with CONFIG_COMPILE_TEST=y and CONFIG_USB_ASPEED_UDC=m (make W=1 drivers/usb/gadget/udc/aspeed_udc.o; 0 warnings) and checked with scripts/checkpatch.pl --strict. Checked against linux-next (next-20261127) and lore.kernel.org for duplicates. Not runtime-tested on Aspeed hardware.

The change and the changelog text were produced with an AI coding assistant (Gemini) working from the tree; I reviewed the diff and the reasoning, and I am responsible for the submission.

---
 drivers/usb/gadget/udc/aspeed_udc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/usb/gadget/udc/aspeed_udc.c +13 -5
index 10214232c..38817d99c 100644
@@ -341,16 +341,24 @@ static void ast_udc_stop_activity(struct ast_udc_dev *udc)
 static int ast_udc_ep_enable(struct usb_ep *_ep,
 			     const struct usb_endpoint_descriptor *desc)
 {
-	u16 maxpacket = usb_endpoint_maxp(desc);
-	struct ast_udc_ep *ep = to_ast_ep(_ep);
-	struct ast_udc_dev *udc = ep->udc;
-	u8 epnum = usb_endpoint_num(desc);
+	struct ast_udc_ep *ep;
+	struct ast_udc_dev *udc;
 	unsigned long flags;
 	u32 ep_conf = 0;
+	u16 maxpacket;
+	u8 epnum;
 	u8 dir_in;
 	u8 type;
 
-	if (!_ep || !ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT ||
+	if (!_ep || !desc)
+		return -EINVAL;
+
+	ep = to_ast_ep(_ep);
+	udc = ep->udc;
+	maxpacket = usb_endpoint_maxp(desc);
+	epnum = usb_endpoint_num(desc);
+
+	if (desc->bDescriptorType != USB_DT_ENDPOINT ||
 	    maxpacket == 0 || maxpacket > ep->ep.maxpacket) {
 		EP_DBG(ep, "Failed, invalid EP enable param\n");
 		return -EINVAL;

---
base-commit: f93eb7cfc4bfa5d46340f360873726e3e77f2365
change-id: 20261128-b4-aspeed-udc-ep-enable-null-76fee6aa38e4

Best regards,

Signature · 1 line
TAKANO Mitsuhiro <[email protected]>
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

From: TAKANO Mitsuhiro <[email protected]>

In ast_udc_ep_enable(), desc and ep are dereferenced in local variable initializers (usb_endpoint_maxp(desc), ep->udc, and usb_endpoint_num(desc)) before checking whether _ep or desc is NULL. In addition, the error path calls EP_DBG(ep, ...), which dereferences ep->udc and ep->ep.name when AST_EP_DEBUG is enabled.

Quoted 6 lines

Check _ep and desc for NULL first, then derive ep, udc, maxpacket, and epnum. Since _ep is already verified non-NULL, the redundant !ep check after container_of() is also removed.

Fixes 055276c13205 ("usb: gadget: add Aspeed ast2600 udc driver")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-b4-aspeed-udc-ep-enable-null-v1-1-26e41e0cd96b@gmail.com?part=1
#2 lore raw
In reply to TAKANO Mitsuhiro

Hi Takano,

Quoted 5 lines

In ast_udc_ep_enable(), desc and ep are dereferenced in local variable initializers (usb_endpoint_maxp(desc), ep->udc, and usb_endpoint_num(desc)) before checking whether _ep or desc is NULL. In addition, the error path calls EP_DBG(ep, ...), which dereferences ep->udc and ep->ep.name when AST_EP_DEBUG is enabled.

Thanks for the patch, but I don't think this change is needed.

ast_udc_ep_enable() is only reached through ep->ops->enable(), and the only caller of that is usb_ep_enable() in udc/core.c. usb_ep_enable() already dereferences ep (ep->enabled) before the call, and it returns -EINVAL with a WARN if ep->desc is NULL or its maxpacket is 0, so _ep and desc can never be NULL by the time ast_udc_ep_enable() runs.

Because of that there is no NULL dereference to fix here, and the
Fixes: tag does not apply.

Thanks,
Joe Wang