Skip to contentSkip to conversation

usb: musb: fix crash with highmen PIO and usbmon

PATCH Superseded Mans Rullgard 2 participants 3 messages Mar 7, 2020 – Mar 9, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: musb: fix crash with highmen PIO and usbmon

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")
---
 drivers/usb/musb/musb_host.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/musb/musb_host.c +6 -2
index 1c813c37462a..b67b40de1947 100644
@@ -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;
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hi Mans,

Quoted 11 lines · Mans Rullgard

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")

Thanks for fixing the bug.

Quoted 9 lines
---
 drivers/usb/musb/musb_host.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/musb/musb_host.c +0 -0
index 1c813c37462a..b67b40de1947 100644

drivers/usb/musb/musb_host.c void musb_host_tx(struct musb *musb, u8 epnum) · line 1465

Quoted 8 lines
drivers/usb/musb/musb_host.c +3 -1
@@ -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.

#2 lore raw
Quoted 28 lines · Bin Liu

Hi Mans,

Quoted 11 lines · Mans Rullgard

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")

Thanks for fixing the bug.

Quoted 11 lines
---
 drivers/usb/musb/musb_host.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/musb/musb_host.c +0 -0
index 1c813c37462a..b67b40de1947 100644
@@ -1459,8 +1459,10 @@ void musb_host_tx(struct musb *musb, u8 epnum)
 	qh->segsize = length;
 
Quoted 9 lines

drivers/usb/musb/musb_host.c void musb_host_tx(struct musb *musb, u8 epnum) · line 1465

Quoted 6 lines
drivers/usb/musb/musb_host.c +3 -1
 	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.

Signature · 1 line
Måns Rullgård