Skip to contentSkip to conversation

usb: r8a66597-hcd: fix potential divide-by-zero in prepare_packet_read()

PATCH In review pip-izony 2 participants 3 messages Jan 10 – Jan 11 1 message not in the archive USB

lore
Share & tools
lore

pip-izony

author
#0 lore raw

PATCH 1/1 usb: r8a66597-hcd: fix potential divide-by-zero in prepare_packet_read()

From: Seungjin Bae <[email protected]>

The prepare_packet_read() function calculates the number of packets required for a transfer by dividing the transfer buffer length by the maximum packet size (td->maxpacket). The td->maxpacket is initialized in r8a66597_make_td() function based on the endpoint descriptor. However, it does not validate whether td->maxpacket is zero before using it in the DIV_ROUND_UP macro.

If a malicious USB device sends a descriptor with wMaxPacketSize set to 0, it triggers a divide-by-zero exception (kernel panic).

Fix this by ensuring td->maxpacket is greater than 0 before performing the division.

Fixes 5d3043586db4 ("USB: r8a66597-hcd: host controller driver for R8A66597")
---
 drivers/usb/host/r8a66597-hcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/r8a66597-hcd.c +1 -1
index d21a03cf5c17..445551b4b05f 100644
@@ -1097,7 +1097,7 @@ static void prepare_packet_read(struct r8a66597 *r8a66597,
 		pipe_start(r8a66597, td->pipe);
 		pipe_irq_enable(r8a66597, urb, td->pipenum);
 	} else {
-		if (urb->actual_length == 0) {
+		if (urb->actual_length == 0 && td->maxpacket > 0) {
 			pipe_irq_disable(r8a66597, td->pipenum);
 			pipe_setting(r8a66597, td);
 			pipe_stop(r8a66597, td->pipe);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · pip-izony

From: Seungjin Bae <[email protected]>

The prepare_packet_read() function calculates the number of packets required for a transfer by dividing the transfer buffer length by the maximum packet size (td->maxpacket). The td->maxpacket is initialized in r8a66597_make_td() function based on the endpoint descriptor. However, it does not validate whether td->maxpacket is zero before using it in the DIV_ROUND_UP macro.

If a malicious USB device sends a descriptor with wMaxPacketSize set to 0, it triggers a divide-by-zero exception (kernel panic).

Same here, when can this happen, before a device is bound to the device or afterward?

thanks,

greg k-h

A message that is not in the archive (sent off-list or not yet received): CAAsoPpUv_G1Se_z9WRQ_4rHv=tiUW__eHU=03mytkdU9+kW1ag@mail.gmail.com

#3 lore raw
Quoted 27 lines · Seungjin Bae

2026년 1월 10일 (토) PM 3:02, Greg Kroah-Hartman <[email protected]>님이 작성:

Quoted 13 lines · pip-izony
Quoted 11 lines

From: Seungjin Bae <[email protected]>

The prepare_packet_read() function calculates the number of packets required for a transfer by dividing the transfer buffer length by the maximum packet size (td->maxpacket). The td->maxpacket is initialized in r8a66597_make_td() function based on the endpoint descriptor. However, it does not validate whether td->maxpacket is zero before using it in the DIV_ROUND_UP macro.

If a malicious USB device sends a descriptor with wMaxPacketSize set to 0, it triggers a divide-by-zero exception (kernel panic).

Quoted 7 lines

Same here, when can this happen, before a device is bound to the device or afterward?

thanks,

greg k-h

This logic triggers after the driver is bound, specifically during the packet preparation phase.

Quoted 8 lines

I checked usb_parse_endpoint() and confirmed that if wMaxPacketSize is 0, it only prints a warning and continues the binding process. It does not reject the device at that stage.

However, I missed that usb_submit_urb() explicitly checks if wMaxPacketSize is 0 before calling the driver's enqueue function. Since the core rejects such requests with -EMSGSIZE, this code path is unreachable in practice.

That's great to verify, thanks for checking.

also, when writing a patch that says "this will fix a crash", please verify that the tool that found this issue is actually correct and not making things up. We have a lot of bad AI generated bug reports these days, so be careful as we are getting much more sensitive to this.

thanks,

greg k-h