2019-04-09 14:41:25

by Yan Zhu

[permalink] [raw]
Subject: [PATCH] usb:host: fix divide-by-zero in function fhci_queue_urb

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.

Signed-off-by: zhuyan <[email protected]>
---
drivers/usb/host/fhci-sched.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/host/fhci-sched.c b/drivers/usb/host/fhci-sched.c
index 3d12cdd..27ed63c 100644
--- a/drivers/usb/host/fhci-sched.c
+++ b/drivers/usb/host/fhci-sched.c
@@ -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);
@@ -765,11 +766,13 @@ void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)

switch (ed->mode) {
case FHCI_TF_BULK:
+ max_pkt_size = usb_maxpacket(urb->dev, urb->pipe,
+ usb_pipeout(urb->pipe));
if (urb->transfer_flags & URB_ZERO_PACKET &&
urb->transfer_buffer_length > 0 &&
+ (max_pkt_size != 0) &&
((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,
--
1.8.5.6


2019-04-16 09:46:43

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH] usb:host: fix divide-by-zero in function fhci_queue_urb

On Tue, Apr 09, 2019 at 10:37:12PM +0800, zhuyan wrote:
> 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?

> When it is zero, unexpected results may occur, so it is necessary to
> ensure that the divisor is not zero.
>
> Signed-off-by: zhuyan <[email protected]>

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

thanks,

greg k-h

2019-04-16 14:49:53

by Yan Zhu

[permalink] [raw]
Subject: Re: [PATCH] usb:host: fix divide-by-zero in function fhci_queue_urb

On Tue, 16 Apr 2019 at 11:45:45 +0200, Greg KH wrote:
> On Tue, Apr 09, 2019 at 10:37:12PM +0800, zhuyan wrote:
> > 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?
>
> > When it is zero, unexpected results may occur, so it is necessary to
> > ensure that the divisor is not zero.
> >
> > Signed-off-by: zhuyan <[email protected]>
>
> 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.
Then, in function fhci_queue_urb, the divisor of expression
(urb->transfer_buffer_length % usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))
is zero. It will lead to unpredictable results.

Thanks!

Yan Zhu

2019-04-16 15:09:16

by Alan Stern

[permalink] [raw]
Subject: Re: [PATCH] usb:host: fix divide-by-zero in function fhci_queue_urb

On Tue, 16 Apr 2019, zhuyan (M) wrote:

> On Tue, 16 Apr 2019 at 11:45:45 +0200, Greg KH wrote:
> > On Tue, Apr 09, 2019 at 10:37:12PM +0800, zhuyan wrote:
> > > 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?
> >
> > > When it is zero, unexpected results may occur, so it is necessary to
> > > ensure that the divisor is not zero.
> > >
> > > Signed-off-by: zhuyan <[email protected]>
> >
> > 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).

Alan Stern

> Then, in function fhci_queue_urb, the divisor of expression
> (urb->transfer_buffer_length % usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))
> is zero. It will lead to unpredictable results.

2019-04-17 07:11:29

by Yan Zhu

[permalink] [raw]
Subject: Re: [PATCH] usb:host: fix divide-by-zero in function fhci_queue_urb

On Tue, 16 Apr 2019 11:07:56 -0400, Alan Stern wrote:

> On Tue, 16 Apr 2019, zhuyan (M) wrote:
> > On Tue, 16 Apr 2019 at 11:45:45 +0200, Greg KH wrote:
> > > On Tue, Apr 09, 2019 at 10:37:12PM +0800, zhuyan wrote:
> > > > 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?
> > >
> > > > When it is zero, unexpected results may occur, so it is necessary
> > > > to ensure that the divisor is not zero.
> > > >
> > > > Signed-off-by: zhuyan <[email protected]>
> > >
> > > 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).

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.

zhuyan

> > Then, in function fhci_queue_urb, the divisor of expression
> > (urb->transfer_buffer_length % usb_maxpacket(urb->dev, urb->pipe,
> > usb_pipeout(urb->pipe))) is zero. It will lead to unpredictable results.

2019-04-17 13:46:38

by Alan Stern

[permalink] [raw]
Subject: Re: [PATCH] usb:host: fix divide-by-zero in function fhci_queue_urb

On Wed, 17 Apr 2019, zhuyan (M) wrote:

> On Tue, 16 Apr 2019 11:07:56 -0400, Alan Stern wrote:
>
> > On Tue, 16 Apr 2019, zhuyan (M) wrote:
> > > On Tue, 16 Apr 2019 at 11:45:45 +0200, Greg KH wrote:
> > > > On Tue, Apr 09, 2019 at 10:37:12PM +0800, zhuyan wrote:
> > > > > 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?
> > > >
> > > > > When it is zero, unexpected results may occur, so it is necessary
> > > > > to ensure that the divisor is not zero.
> > > > >
> > > > > Signed-off-by: zhuyan <[email protected]>
> > > >
> > > > 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).
>
> 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.

Alan Stern

2019-04-17 15:34:34

by Yan Zhu

[permalink] [raw]
Subject: Re: [PATCH] usb:host: fix divide-by-zero in function fhci_queue_urb

On Wed, 17 Apr 2019, 9:45 -400, Alan Stern wrote:
> On Wed, 17 Apr 2019, zhuyan (M) wrote:
> > On Tue, 16 Apr 2019 11:07:56 -0400, Alan Stern wrote:
> > > On Tue, 16 Apr 2019, zhuyan (M) wrote:
> > > > On Tue, 16 Apr 2019 at 11:45:45 +0200, Greg KH wrote:
> > > > > On Tue, Apr 09, 2019 at 10:37:12PM +0800, zhuyan wrote:
> > > > > > 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?
> > > > >
> > > > > > When it is zero, unexpected results may occur, so it is
> > > > > > necessary to ensure that the divisor is not zero.
> > > > > >
> > > > > > Signed-off-by: zhuyan <[email protected]>
> > > > >
> > > > > 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).
> >
> > 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.

ok, I will resubmit a patch to repair it.

Best wish to you!
zhuyan