Skip to contentSkip to conversation

usb:host: fix divide-by-zero in function fhci_queue_urb

PATCH zhuyan (M) 2 participants 2 messages Apr 18, 2019 USB

lore
Share & tools
lore

zhuyan (M)

author
#0 lore raw
Quoted 108 lines · Alan Stern
Quoted 82 lines · zhuyan (M)
Quoted 30 lines · Alan Stern
Quoted 28 lines · zhuyan (M)
Quoted 25 lines · Alan Stern
Quoted 14 lines · zhuyan (M)
Quoted 13 lines · Greg KH
Quoted 4 lines · zhuyan

In function fhci_queue_urb, the divisor of expression (urb->transfer_buffer_length % usb_maxpacket(urb->dev, urb->pipe,
usb_pipeout(urb->pipe))) may be zero.

How can you hit that?

Quoted 4 lines

When it is zero, unexpected results may occur, so it is necessary to ensure that the divisor is not zero.

Quoted 7 lines
Quoted 5 lines

I need a "Full" name here, not just a single name. Whatever you use to sign documents is good.

thanks,

greg k-h

In function usb_maxpacket, when ep is NULL, its return value is 0.

fhci_queue_urb() shouldn't use urb->pipe to compute the maxpacket size anyway. It should use usb_endpoint_maxp(&urb->ep->desc).

Quoted 7 lines
Quoted 3 lines

Currently, fhci_queue_urb(), call usb_maxpacket() multiple times to calculate the maxpacket size. The usb_maxpacket() will call usb_endpoint_maxp() to compute the maxpacket size.

I know that. What fhci_queue_urb() is doing is wrong. You should change it:
Make it call usb_endpoint_maxp directly instead of calling usb_maxpacket.

From 1996456d0cc17b5ff7746a598ff355b25d13db3e Mon Sep 17 00:00:00 2001
From: zhuyan <[email protected]>
Date: Thu, 18 Apr 2019 00:53:03 +0800
Subject: [PATCH] usb: host: fix divide-by-zero in function fhci_queue_urb

fhci_queue_urb() shouldn't use urb->pipe to compute the maxpacket size anyway.It should use usb_endpoint_maxp(&urb->ep->desc).

In function fhci_queue_urb, the divisor of expression (urb->transfer_buffer_length % usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) may be zero. When it is zero, unexpected results may occur, so it is necessary to ensure that the divisor is not zero.

---
 drivers/usb/host/fhci-sched.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/drivers/usb/host/fhci-sched.c index 3d12cdd..7dcfe22 100644

drivers/usb/host/fhci-sched.c +2 -2
@@ -704,6 +704,7 @@ void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
 	struct td *td;
 	u8 *data;
 	u16 cnt = 0;
+	u16 max_pkt_size = 0;
 
 	if (ed == NULL) {
 		ed = fhci_get_empty_ed(fhci);
@@ -727,8 +728,7 @@ void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
 		}
 		ed->speed = (urb->dev->speed == USB_SPEED_LOW) ?
 			FHCI_LOW_SPEED : FHCI_FULL_SPEED;
-		ed->max_pkt_size = usb_maxpacket(urb->dev,
-			urb->pipe, usb_pipeout(urb->pipe));
+		ed->max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
 		urb->ep->hcpriv = ed;
 		fhci_dbg(fhci, "new ep speed=%d max_pkt_size=%d\n",
 			 ed->speed, ed->max_pkt_size);

drivers/usb/host/fhci-sched.c void fhci_queue_urb(struct fhci_hcd *fhci, · line 772

Quoted 8 lines
drivers/usb/host/fhci-sched.c +2 -0
@@ -765,11 +765,12 @@ void fhci_queue_urb(struct fhci_hcd *fhci, 
 struct urb *urb)
 
 	switch (ed->mode) {
 	case FHCI_TF_BULK:
+		max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
 		if (urb->transfer_flags & URB_ZERO_PACKET &&
 				urb->transfer_buffer_length > 0 &&
+				(max_pkt_size != 0) &&

Now you shouldn't need to add this extra test.

Alan Stern

Quoted 10 lines
drivers/usb/host/fhci-sched.c +1 -2
 				((urb->transfer_buffer_length %
-				usb_maxpacket(urb->dev, urb->pipe,
-				usb_pipeout(urb->pipe))) == 0))
+				max_pkt_size) == 0))
 			urb_state = US_BULK0;
 		while (data_len > 4096) {

td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt, @@ -807,8 +808,8
@@ void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)

drivers/usb/host/fhci-sched.c +0 -0
 		break;
 	case FHCI_TF_CTRL:
Quoted 8 lines

drivers/usb/host/fhci-sched.c

Quoted 8 lines
drivers/usb/host/fhci-sched.c +2 -2
 		ed->dev_addr = usb_pipedevice(urb->pipe);
-		ed->max_pkt_size = usb_maxpacket(urb->dev, urb->pipe,
-			usb_pipeout(urb->pipe));
+		ed->max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
+
 		/* setup stage */
 		td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, FHCI_TA_SETUP,
 			USB_TD_TOGGLE_DATA0, urb->setup_packet, 8, 0, 0, true);

But the return value of usb_endpoint_maxp() may be 0. Do we need to take protective measures?

Yan Zhu

#1 lore raw
Quoted 12 lines · zhuyan (M)
Quoted 12 lines

Review void fhci_queue_urb(struct fhci_hcd *fhci, · line 772

Quoted 9 lines
(quoted diff) +2 -0
@@ -765,11 +765,12 @@ void fhci_queue_urb(struct fhci_hcd *fhci, 
 struct urb *urb)
 
 	switch (ed->mode) {
 	case FHCI_TF_BULK:
+		max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
 		if (urb->transfer_flags & URB_ZERO_PACKET &&
 				urb->transfer_buffer_length > 0 &&
+				(max_pkt_size != 0) &&

Now you shouldn't need to add this extra test.

Quoted 4 lines
Quoted 1 line

Alan Stern

But the return value of usb_endpoint_maxp() may be 0. Do we need to take protective measures?

Yes, we do. But the protective measures don't belong in the fhci driver.

They should go in core/config.c:usb_parse_endpoint(). That routine already has code to check for maxpacket sizes that are too large; you can add code that checks for maxpacket sizes that are too small.

In theory an interrupt or isochronous endpoint might have maxpacket set to 0 -- we could warn about these and accept them -- but this is not allowed for bulk endpoints (see section 5.8.3 of the USB 2.0 specification). In fact, bulk endpoints are not allowed to have maxpacket size smaller than 8.

Maybe if you make this change then fhci-hcd won't need any modifications at all.

Alan Stern