linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: musb: fix crash with highmen PIO and usbmon
@ 2020-03-07 13:07 Mans Rullgard
  2020-03-09 14:01 ` Bin Liu
  0 siblings, 1 reply; 3+ messages in thread
From: Mans Rullgard @ 2020-03-07 13:07 UTC (permalink / raw)
  To: Bin Liu; +Cc: Greg Kroah-Hartman, linux-usb, linux-kernel

When handling a PIO bulk transfer with highmem buffer, a temporary
mapping is assigned to urb->transfer_buffer.  After the transfer is
complete, an invalid address is left behind in this pointer.  This is
not ordinarily a problem since nothing touches that buffer before the
urb is released.  However, when usbmon is active, usbmon_urb_complete()
calls (indirectly) mon_bin_get_data() which does access the transfer
buffer if it is set.  To prevent an invalid memory access here, reset
urb->tranfer_buffer to NULL when finished.

Fixes: 8e8a55165469 ("usb: musb: host: Handle highmem in PIO mode")
Signed-off-by: Mans Rullgard <mans@mansr.com>
---
 drivers/usb/musb/musb_host.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index 1c813c37462a..b67b40de1947 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -1459,8 +1459,10 @@ void musb_host_tx(struct musb *musb, u8 epnum)
 	qh->segsize = length;
 
 	if (qh->use_sg) {
-		if (offset + length >= urb->transfer_buffer_length)
+		if (offset + length >= urb->transfer_buffer_length) {
 			qh->use_sg = false;
+			urb->transfer_buffer = NULL;
+		}
 	}
 
 	musb_ep_select(mbase, epnum);
@@ -1977,8 +1979,10 @@ void musb_host_rx(struct musb *musb, u8 epnum)
 	urb->actual_length += xfer_len;
 	qh->offset += xfer_len;
 	if (done) {
-		if (qh->use_sg)
+		if (qh->use_sg) {
 			qh->use_sg = false;
+			urb->transfer_buffer = NULL;
+		}
 
 		if (urb->status == -EINPROGRESS)
 			urb->status = status;
-- 
2.25.1


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

* Re: [PATCH] usb: musb: fix crash with highmen PIO and usbmon
  2020-03-07 13:07 [PATCH] usb: musb: fix crash with highmen PIO and usbmon Mans Rullgard
@ 2020-03-09 14:01 ` Bin Liu
  2020-03-09 14:41   ` Måns Rullgård
  0 siblings, 1 reply; 3+ messages in thread
From: Bin Liu @ 2020-03-09 14:01 UTC (permalink / raw)
  To: Mans Rullgard; +Cc: Greg Kroah-Hartman, linux-usb, linux-kernel

Hi Mans,

On Sat, Mar 07, 2020 at 01:07:20PM +0000, Mans Rullgard wrote:
> When handling a PIO bulk transfer with highmem buffer, a temporary
> mapping is assigned to urb->transfer_buffer.  After the transfer is
> complete, an invalid address is left behind in this pointer.  This is
> not ordinarily a problem since nothing touches that buffer before the
> urb is released.  However, when usbmon is active, usbmon_urb_complete()
> calls (indirectly) mon_bin_get_data() which does access the transfer
> buffer if it is set.  To prevent an invalid memory access here, reset
> urb->tranfer_buffer to NULL when finished.
> 
> Fixes: 8e8a55165469 ("usb: musb: host: Handle highmem in PIO mode")
> Signed-off-by: Mans Rullgard <mans@mansr.com>

Thanks for fixing the bug.

> ---
>  drivers/usb/musb/musb_host.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
> index 1c813c37462a..b67b40de1947 100644
> --- a/drivers/usb/musb/musb_host.c
> +++ b/drivers/usb/musb/musb_host.c
> @@ -1459,8 +1459,10 @@ void musb_host_tx(struct musb *musb, u8 epnum)
>  	qh->segsize = length;
>  
>  	if (qh->use_sg) {
> -		if (offset + length >= urb->transfer_buffer_length)
> +		if (offset + length >= urb->transfer_buffer_length) {
>  			qh->use_sg = false;
> +			urb->transfer_buffer = NULL;
> +		}

In this tx case, can you directly pass qh->sg_miter.addr to
musb_write_fifo() so that urb->transfer_buffer is not touched at all?

-Bin.

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

* Re: [PATCH] usb: musb: fix crash with highmen PIO and usbmon
  2020-03-09 14:01 ` Bin Liu
@ 2020-03-09 14:41   ` Måns Rullgård
  0 siblings, 0 replies; 3+ messages in thread
From: Måns Rullgård @ 2020-03-09 14:41 UTC (permalink / raw)
  To: Bin Liu; +Cc: Greg Kroah-Hartman, linux-usb, linux-kernel

Bin Liu <b-liu@ti.com> writes:

> Hi Mans,
>
> On Sat, Mar 07, 2020 at 01:07:20PM +0000, Mans Rullgard wrote:
>> When handling a PIO bulk transfer with highmem buffer, a temporary
>> mapping is assigned to urb->transfer_buffer.  After the transfer is
>> complete, an invalid address is left behind in this pointer.  This is
>> not ordinarily a problem since nothing touches that buffer before the
>> urb is released.  However, when usbmon is active, usbmon_urb_complete()
>> calls (indirectly) mon_bin_get_data() which does access the transfer
>> buffer if it is set.  To prevent an invalid memory access here, reset
>> urb->tranfer_buffer to NULL when finished.
>> 
>> Fixes: 8e8a55165469 ("usb: musb: host: Handle highmem in PIO mode")
>> Signed-off-by: Mans Rullgard <mans@mansr.com>
>
> Thanks for fixing the bug.
>
>> ---
>>  drivers/usb/musb/musb_host.c | 8 ++++++--
>>  1 file changed, 6 insertions(+), 2 deletions(-)
>> 
>> diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
>> index 1c813c37462a..b67b40de1947 100644
>> --- a/drivers/usb/musb/musb_host.c
>> +++ b/drivers/usb/musb/musb_host.c
>> @@ -1459,8 +1459,10 @@ void musb_host_tx(struct musb *musb, u8 epnum)
>>  	qh->segsize = length;
>>  
>>  	if (qh->use_sg) {
>> -		if (offset + length >= urb->transfer_buffer_length)
>> +		if (offset + length >= urb->transfer_buffer_length) {
>>  			qh->use_sg = false;
>> +			urb->transfer_buffer = NULL;
>> +		}
>
> In this tx case, can you directly pass qh->sg_miter.addr to
> musb_write_fifo() so that urb->transfer_buffer is not touched at all?

Yes, that seems to work.  I'll prepare a new patch.

-- 
Måns Rullgård

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

end of thread, other threads:[~2020-03-09 14:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-07 13:07 [PATCH] usb: musb: fix crash with highmen PIO and usbmon Mans Rullgard
2020-03-09 14:01 ` Bin Liu
2020-03-09 14:41   ` Måns Rullgård

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).