public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] usb: dwc3: gadget: check that event count does not exceed event buffer length
@ 2025-04-01 12:53 Frode Isaksen
  2025-04-01 13:49 ` Greg KH
  2025-04-01 23:43 ` Thinh Nguyen
  0 siblings, 2 replies; 11+ messages in thread
From: Frode Isaksen @ 2025-04-01 12:53 UTC (permalink / raw)
  To: linux-usb, Thinh.Nguyen; +Cc: gregkh, krishna.kurapati, Frode Isaksen, stable

From: Frode Isaksen <frode@meta.com>

The event count is read from register DWC3_GEVNTCOUNT.
There is a check for the count being zero, but not for exceeding the
event buffer length.
Check that event count does not exceed event buffer length,
avoiding an out-of-bounds access when memcpy'ing the event.
Crash log:
Unable to handle kernel paging request at virtual address ffffffc0129be000
pc : __memcpy+0x114/0x180
lr : dwc3_check_event_buf+0xec/0x348
x3 : 0000000000000030 x2 : 000000000000dfc4
x1 : ffffffc0129be000 x0 : ffffff87aad60080
Call trace:
__memcpy+0x114/0x180
dwc3_interrupt+0x24/0x34

Signed-off-by: Frode Isaksen <frode@meta.com>
Fixes: ebbb2d59398f ("usb: dwc3: gadget: use evt->cache for processing events")
Cc: stable@vger.kernel.org
---
v1->v2: added error log

 drivers/usb/dwc3/gadget.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 89a4dc8ebf94..923737776d82 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -4564,6 +4564,12 @@ static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
 	if (!count)
 		return IRQ_NONE;
 
+	if (count > evt->length) {
+		dev_err(dwc->dev, "invalid count(%u) > evt->length(%u)\n",
+			count, evt->length);
+		return IRQ_NONE;
+	}
+
 	evt->count = count;
 	evt->flags |= DWC3_EVENT_PENDING;
 
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread
* [PATCH v2] usb: dwc3: gadget: check that event count does not exceed event buffer length
@ 2025-03-28 10:44 Frode Isaksen
  2025-04-01  5:43 ` Krishna Kurapati
  0 siblings, 1 reply; 11+ messages in thread
From: Frode Isaksen @ 2025-03-28 10:44 UTC (permalink / raw)
  To: linux-usb, Thinh.Nguyen; +Cc: gregkh, fisaksen, Frode Isaksen, stable

From: Frode Isaksen <frode@meta.com>

The event count is read from register DWC3_GEVNTCOUNT.
There is a check for the count being zero, but not for exceeding the
event buffer length.
Check that event count does not exceed event buffer length,
avoiding an out-of-bounds access when memcpy'ing the event.
Crash log:
Unable to handle kernel paging request at virtual address ffffffc0129be000
pc : __memcpy+0x114/0x180
lr : dwc3_check_event_buf+0xec/0x348
x3 : 0000000000000030 x2 : 000000000000dfc4
x1 : ffffffc0129be000 x0 : ffffff87aad60080
Call trace:
__memcpy+0x114/0x180
dwc3_interrupt+0x24/0x34

Signed-off-by: Frode Isaksen <frode@meta.com>
Fixes: ebbb2d59398f ("usb: dwc3: gadget: use evt->cache for processing events")
Cc: stable@vger.kernel.org
---
v1 -> v2: Added Fixes and Cc tag.

This bug was discovered, tested and fixed (no more crashes seen) on Meta Quest 3 device.
Also tested on T.I. AM62x board.

 drivers/usb/dwc3/gadget.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 63fef4a1a498..548e112167f3 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -4564,7 +4564,7 @@ static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
 
 	count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
 	count &= DWC3_GEVNTCOUNT_MASK;
-	if (!count)
+	if (!count || count > evt->length)
 		return IRQ_NONE;
 
 	evt->count = count;
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-04-02 22:22 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-01 12:53 [PATCH v2] usb: dwc3: gadget: check that event count does not exceed event buffer length Frode Isaksen
2025-04-01 13:49 ` Greg KH
2025-04-01 23:43 ` Thinh Nguyen
  -- strict thread matches above, loose matches on Subject: below --
2025-03-28 10:44 Frode Isaksen
2025-04-01  5:43 ` Krishna Kurapati
2025-04-01 12:08   ` Frode Isaksen
2025-04-01 12:26     ` Krishna Kurapati
2025-04-01 23:36       ` Thinh Nguyen
2025-04-02  4:42         ` Krishna Kurapati
2025-04-02  7:50           ` Frode Isaksen
2025-04-02 22:22             ` Thinh Nguyen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox