Skip to contentSkip to conversation

usb: dwc3: ep0: Fix mem corruption on OUT transfers of more than 512 bytes

RFC PATCH In review Kishon Vijay Abraham I 4 participants 10 messages Jun 9, 2015 – Jun 11, 2015 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 usb: dwc3: ep0: Fix mem corruption on OUT transfers of more than 512 bytes

DWC3 uses bounce buffer to handle non max packet aligned OUT transfers and the size of bounce buffer is 512 bytes. However if the host initiates OUT transfers of size more than 512 bytes (and non max packet aligned), the driver throws a WARN dump but still programs the TRB to receive more than 512 bytes. This will cause bounce buffer to overflow and corrupt the adjacent memory locations which can be fatal.

Fix it by programming the TRB to receive a maximum of DWC3_EP0_BOUNCE_SIZE
(512) bytes.

---
Steps to see the issue (before this patch)
1) Insert g_zero in DUT
2) run './testusb -t 14 -c 1 -s 520 -v 1' in host (size should be > 512)

The test should FAIL since bounce buffer can handle only 512 bytes, but the test PASS. There is a WARN dump in DUT but still there will be memory corruption since the bounce buffer overflows.

Tested this patch using USB3 Gen X CV (ch9 tests: usb2 and usb3, link layer testing and MSC tests) and using USB2 X CV (ch9 tests, MSC tests).

After the patch, the tests timeout!
./testusb -t 14 -c 1 -s 514 -v 1
unknown speed /dev/bus/usb/001/018 0
/dev/bus/usb/001/018 test 14 --> 110 (Connection timed out)

IMO a patch to fix this is required for stable releases too. So If this patch is alright, I can post the patch cc'ing stable. While the actual fix would be to have chained TRB, I'm not sure if it can go to stable releases.

 drivers/usb/dwc3/ep0.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/usb/dwc3/ep0.c +5 -1
index 2ef3c8d..8858c60 100644
@@ -816,6 +816,11 @@ static void dwc3_ep0_complete_data(struct dwc3 *dwc,
 		unsigned maxp = ep0->endpoint.maxpacket;
 
 		transfer_size += (maxp - (transfer_size % maxp));
+
+		/* Maximum of DWC3_EP0_BOUNCE_SIZE can only be received */
+		if (transfer_size > DWC3_EP0_BOUNCE_SIZE)
+			transfer_size = DWC3_EP0_BOUNCE_SIZE;
+
 		transferred = min_t(u32, ur->length,
 				transfer_size - length);
 		memcpy(ur->buf, dwc->ep0_bounce, transferred);
@@ -937,11 +942,14 @@ static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
 			return;
 		}
 
-		WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);

A message that is not in the archive (sent off-list or not yet received): CAOf5uwmVRaHbiHaRsiwKjCbxg5BxjyP8WPq9whrtK-UdSsL47w@mail.gmail.com

#2 lore raw

Hi,

Quoted 47 lines · Michael Trimarchi

Hi

Quoted 43 lines · On Jun 9, 2015 4:36 PM, "Kishon Vijay Abraham I" <[email protected]…

DWC3 uses bounce buffer to handle non max packet aligned OUT transfers and the size of bounce buffer is 512 bytes. However if the host initiates OUT transfers of size more than 512 bytes (and non max packet aligned), the driver throws a WARN dump but still programs the TRB to receive more than 512 bytes. This will cause bounce buffer to overflow and corrupt the adjacent memory locations which can be fatal.

Fix it by programming the TRB to receive a maximum of DWC3_EP0_BOUNCE_SIZE
(512) bytes.

Signed-off-by Kishon Vijay Abraham I <[email protected] <mailto:[email protected]>>

---
Steps to see the issue (before this patch)
1) Insert g_zero in DUT
2) run './testusb -t 14 -c 1 -s 520 -v 1' in host (size should be > 512)

The test should FAIL since bounce buffer can handle only 512 bytes, but the test PASS. There is a WARN dump in DUT but still there will be memory corruption since the bounce buffer overflows.

Tested this patch using USB3 Gen X CV (ch9 tests: usb2 and usb3, link layer testing and MSC tests) and using USB2 X CV (ch9 tests, MSC tests).

After the patch, the tests timeout!
./testusb -t 14 -c 1 -s 514 -v 1
unknown speed /dev/bus/usb/001/018 0
/dev/bus/usb/001/018 test 14 --> 110 (Connection timed out)

IMO a patch to fix this is required for stable releases too. So If this patch is alright, I can post the patch cc'ing stable. While the actual fix would be to have chained TRB, I'm not sure if it can go to stable releases.

 drivers/usb/dwc3/ep0.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/usb/dwc3/ep0.c +0 -0
index 2ef3c8d..8858c60 100644
@@ -816,6 +816,11 @@ static void dwc3_ep0_complete_data(struct dwc3 *dwc,
                unsigned maxp = ep0->endpoint.maxpacket;
 
Quoted 8 lines

drivers/usb/dwc3/ep0.c static void dwc3_ep0_complete_data(struct dwc3 *dwc, · line 823

Quoted 6 lines
drivers/usb/dwc3/ep0.c +5 -0
                transfer_size += (maxp - (transfer_size % maxp));
+
+               /* Maximum of DWC3_EP0_BOUNCE_SIZE can only be received */
+               if (transfer_size > DWC3_EP0_BOUNCE_SIZE)
+                       transfer_size = DWC3_EP0_BOUNCE_SIZE;
+

Can you just use maxp in the correct way?

what do you mean by correct way? Using roundup() to calculate transfer_size?

Thanks
Kishon

#3 lore raw
Quoted 57 lines · Kishon Vijay Abraham I

Hi,

Quoted 54 lines · Michael Trimarchi

Hi

Quoted 49 lines · On Jun 9, 2015 4:36 PM, "Kishon Vijay Abraham I" <[email protected]…

DWC3 uses bounce buffer to handle non max packet aligned OUT transfers and the size of bounce buffer is 512 bytes. However if the host initiates OUT transfers of size more than 512 bytes (and non max packet aligned), the driver throws a WARN dump but still programs the TRB to receive more than 512 bytes. This will cause bounce buffer to overflow and corrupt the adjacent memory locations which can be fatal.

Fix it by programming the TRB to receive a maximum of DWC3_EP0_BOUNCE_SIZE
(512) bytes.

Signed-off-by Kishon Vijay Abraham I <[email protected] <mailto:[email protected]>>

---
Steps to see the issue (before this patch)
1) Insert g_zero in DUT
2) run './testusb -t 14 -c 1 -s 520 -v 1' in host (size should be > 512)

The test should FAIL since bounce buffer can handle only 512 bytes, but the test PASS. There is a WARN dump in DUT but still there will be memory corruption since the bounce buffer overflows.

Tested this patch using USB3 Gen X CV (ch9 tests: usb2 and usb3, link layer testing and MSC tests) and using USB2 X CV (ch9 tests, MSC tests).

After the patch, the tests timeout!
./testusb -t 14 -c 1 -s 514 -v 1
unknown speed /dev/bus/usb/001/018 0
/dev/bus/usb/001/018 test 14 --> 110 (Connection timed out)

IMO a patch to fix this is required for stable releases too. So If this patch is alright, I can post the patch cc'ing stable. While the actual fix would be to have chained TRB, I'm not sure if it can go to stable releases.

 drivers/usb/dwc3/ep0.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/usb/dwc3/ep0.c +5 -0
index 2ef3c8d..8858c60 100644
@@ -816,6 +816,11 @@ static void dwc3_ep0_complete_data(struct dwc3 *dwc,
                unsigned maxp = ep0->endpoint.maxpacket;
 
                transfer_size += (maxp - (transfer_size % maxp));
+
+               /* Maximum of DWC3_EP0_BOUNCE_SIZE can only be received */
+               if (transfer_size > DWC3_EP0_BOUNCE_SIZE)
+                       transfer_size = DWC3_EP0_BOUNCE_SIZE;
+
Quoted 3 lines
Quoted 1 line

Can you just use maxp in the correct way?

what do you mean by correct way? Using roundup() to calculate transfer_size?

Why not just make the bounce buffer size the same as the maxpacket size? In other words, 1024 bytes instead of 512, for ep0 on a USB-3 device.

Alan stern

#4 lore raw

Hi,

Quoted 59 lines · Alan Stern
Quoted 57 lines · Kishon Vijay Abraham I

Hi,

Quoted 54 lines · Michael Trimarchi

Hi

Quoted 49 lines · On Jun 9, 2015 4:36 PM, "Kishon Vijay Abraham I"…

DWC3 uses bounce buffer to handle non max packet aligned OUT transfers and the size of bounce buffer is 512 bytes. However if the host initiates OUT transfers of size more than 512 bytes (and non max packet aligned), the driver throws a WARN dump but still programs the TRB to receive more than 512 bytes. This will cause bounce buffer to overflow and corrupt the adjacent memory locations which can be fatal.

Fix it by programming the TRB to receive a maximum of DWC3_EP0_BOUNCE_SIZE
(512) bytes.

Signed-off-by Kishon Vijay Abraham I <[email protected] <mailto:[email protected]>>

---
Steps to see the issue (before this patch)
1) Insert g_zero in DUT
2) run './testusb -t 14 -c 1 -s 520 -v 1' in host (size should be > 512)

The test should FAIL since bounce buffer can handle only 512 bytes, but the test PASS. There is a WARN dump in DUT but still there will be memory corruption since the bounce buffer overflows.

Tested this patch using USB3 Gen X CV (ch9 tests: usb2 and usb3, link layer testing and MSC tests) and using USB2 X CV (ch9 tests, MSC tests).

After the patch, the tests timeout!
./testusb -t 14 -c 1 -s 514 -v 1
unknown speed /dev/bus/usb/001/018 0
/dev/bus/usb/001/018 test 14 --> 110 (Connection timed out)

IMO a patch to fix this is required for stable releases too. So If this patch is alright, I can post the patch cc'ing stable. While the actual fix would be to have chained TRB, I'm not sure if it can go to stable releases.

 drivers/usb/dwc3/ep0.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/usb/dwc3/ep0.c +5 -0
index 2ef3c8d..8858c60 100644
@@ -816,6 +816,11 @@ static void dwc3_ep0_complete_data(struct dwc3 *dwc,
                unsigned maxp = ep0->endpoint.maxpacket;
 
                transfer_size += (maxp - (transfer_size % maxp));
+
+               /* Maximum of DWC3_EP0_BOUNCE_SIZE can only be received */
+               if (transfer_size > DWC3_EP0_BOUNCE_SIZE)
+                       transfer_size = DWC3_EP0_BOUNCE_SIZE;
+
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Can you just use maxp in the correct way?

what do you mean by correct way? Using roundup() to calculate transfer_size?

Why not just make the bounce buffer size the same as the maxpacket size? In other words, 1024 bytes instead of 512, for ep0 on a USB-3 device.

It would still be possible for the host to send data more than 1024 bytes no? When working with DFU gadget, I've seen host sends data upto 4KB. Changing the bounce buffer size might not be able to fix all the cases IMO. The actual fix will be something like [1]

[1] -> http://comments.gmane.org/gmane.linux.kernel/1883688

Thanks
Kishon
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 5 lines · Kishon Vijay Abraham I
Quoted 3 lines

Why not just make the bounce buffer size the same as the maxpacket size? In other words, 1024 bytes instead of 512, for ep0 on a USB-3 device.

It would still be possible for the host to send data more than 1024 bytes no?

Yes.

Quoted 5 lines

When working with DFU gadget, I've seen host sends data upto 4KB. Changing the bounce buffer size might not be able to fix all the cases IMO. The actual fix will be something like [1]

[1] -> http://comments.gmane.org/gmane.linux.kernel/1883688

But with a bounce buffer that's only 512 bytes long, you can never send an entire packet's worth of data. If the bounce buffer is 1024 bytes then you can send the entire first packet. When that's done, you can send the second packet. And so on. It wouldn't be quite as fast, but for ep0 that shouldn't matter.

Alan Stern

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Hi,

Quoted 10 lines · Alan Stern
Quoted 5 lines · Kishon Vijay Abraham I
Quoted 3 lines

Why not just make the bounce buffer size the same as the maxpacket size? In other words, 1024 bytes instead of 512, for ep0 on a USB-3 device.

It would still be possible for the host to send data more than 1024 bytes no?

Yes.

Quoted 8 lines
Quoted 5 lines

When working with DFU gadget, I've seen host sends data upto 4KB. Changing the bounce buffer size might not be able to fix all the cases IMO. The actual fix will be something like [1]

[1] -> http://comments.gmane.org/gmane.linux.kernel/1883688

But with a bounce buffer that's only 512 bytes long, you can never send an entire packet's worth of data. If the bounce buffer is 1024 bytes

for control endpoint, 512 bytes should be sufficient to send entire packet right?

Quoted 3 lines

then you can send the entire first packet. When that's done, you can send the second packet. And so on. It wouldn't be quite as fast, but for ep0 that shouldn't matter.

right! this is a variant of what I tried to implement in chained TRB [1]. $subject tries just to avoid memory corruption instead of actually trying to receive all the data.

Thanks
Kishon
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw
Quoted 4 lines · Kishon Vijay Abraham I
Quoted 2 lines

But with a bounce buffer that's only 512 bytes long, you can never send an entire packet's worth of data. If the bounce buffer is 1024 bytes

for control endpoint, 512 bytes should be sufficient to send entire packet right?

Yes, you're right. I had confused control endpoints with bulk endpoints, where the maxpacket size is 1024. Sorry for the mistake.

Quoted 7 lines
Quoted 3 lines

then you can send the entire first packet. When that's done, you can send the second packet. And so on. It wouldn't be quite as fast, but for ep0 that shouldn't matter.

right! this is a variant of what I tried to implement in chained TRB [1]. $subject tries just to avoid memory corruption instead of actually trying to receive all the data.

Okay. If you take the $SUBJECT approach, I think it would be better for an URB submission to fail than for the host controller to send only part of the data.

Alan Stern

#8 lore raw

Hi,

Quoted 9 lines · Alan Stern
Quoted 4 lines · Kishon Vijay Abraham I
Quoted 2 lines

But with a bounce buffer that's only 512 bytes long, you can never send an entire packet's worth of data. If the bounce buffer is 1024 bytes

for control endpoint, 512 bytes should be sufficient to send entire packet right?

Yes, you're right. I had confused control endpoints with bulk endpoints, where the maxpacket size is 1024. Sorry for the mistake.

no problem.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

then you can send the entire first packet. When that's done, you can send the second packet. And so on. It wouldn't be quite as fast, but for ep0 that shouldn't matter.

Quoted 7 lines
Quoted 3 lines

right! this is a variant of what I tried to implement in chained TRB [1]. $subject tries just to avoid memory corruption instead of actually trying to receive all the data.

Okay. If you take the $SUBJECT approach, I think it would be better for an URB submission to fail than for the host controller to send only part of the data.

Could be but we also want to prevent mem corruption in the case of a faulty host to be more robust.

Thanks
Kishon
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw
In reply to Alan Stern
Quoted 59 lines · Alan Stern
Quoted 57 lines · Kishon Vijay Abraham I

Hi,

Quoted 54 lines · Michael Trimarchi

Hi

Quoted 49 lines · On Jun 9, 2015 4:36 PM, "Kishon Vijay Abraham I" <[email protected]…

DWC3 uses bounce buffer to handle non max packet aligned OUT transfers and the size of bounce buffer is 512 bytes. However if the host initiates OUT transfers of size more than 512 bytes (and non max packet aligned), the driver throws a WARN dump but still programs the TRB to receive more than 512 bytes. This will cause bounce buffer to overflow and corrupt the adjacent memory locations which can be fatal.

Fix it by programming the TRB to receive a maximum of DWC3_EP0_BOUNCE_SIZE
(512) bytes.

Signed-off-by Kishon Vijay Abraham I <[email protected] <mailto:[email protected]>>

---
Steps to see the issue (before this patch)
1) Insert g_zero in DUT
2) run './testusb -t 14 -c 1 -s 520 -v 1' in host (size should be > 512)

The test should FAIL since bounce buffer can handle only 512 bytes, but the test PASS. There is a WARN dump in DUT but still there will be memory corruption since the bounce buffer overflows.

Tested this patch using USB3 Gen X CV (ch9 tests: usb2 and usb3, link layer testing and MSC tests) and using USB2 X CV (ch9 tests, MSC tests).

After the patch, the tests timeout!
./testusb -t 14 -c 1 -s 514 -v 1
unknown speed /dev/bus/usb/001/018 0
/dev/bus/usb/001/018 test 14 --> 110 (Connection timed out)

IMO a patch to fix this is required for stable releases too. So If this patch is alright, I can post the patch cc'ing stable. While the actual fix would be to have chained TRB, I'm not sure if it can go to stable releases.

 drivers/usb/dwc3/ep0.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/usb/dwc3/ep0.c +5 -0
index 2ef3c8d..8858c60 100644
@@ -816,6 +816,11 @@ static void dwc3_ep0_complete_data(struct dwc3 *dwc,
                unsigned maxp = ep0->endpoint.maxpacket;
 
                transfer_size += (maxp - (transfer_size % maxp));
+
+               /* Maximum of DWC3_EP0_BOUNCE_SIZE can only be received */
+               if (transfer_size > DWC3_EP0_BOUNCE_SIZE)
+                       transfer_size = DWC3_EP0_BOUNCE_SIZE;
+
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Can you just use maxp in the correct way?

what do you mean by correct way? Using roundup() to calculate transfer_size?

Why not just make the bounce buffer size the same as the maxpacket size? In other words, 1024 bytes instead of 512, for ep0 on a USB-3 device.

EP0 max packet size is 512 on USB3.

Signature · 1 line
balbi
#10 lore raw
In reply to Kishon Vijay Abraham I

Hi

Quoted 70 lines · Kishon Vijay Abraham I

Hi,

Quoted 67 lines · Michael Trimarchi

Hi

Quoted 2 lines · On Jun 9, 2015 4:36 PM, "Kishon Vijay Abraham I"…

DWC3 uses bounce buffer to handle non max packet aligned OUT transfers

and

Quoted 1 line

the size of bounce buffer is 512 bytes. However if the host initiates

OUT

Quoted 2 lines

transfers of size more than 512 bytes (and non max packet aligned), the driver throws a WARN dump but still programs the TRB to receive more

than

Quoted 4 lines

512 bytes. This will cause bounce buffer to overflow and corrupt the adjacent memory locations which can be fatal.

Fix it by programming the TRB to receive a maximum of

DWC3_EP0_BOUNCE_SIZE

Quoted 3 lines

(512) bytes.

<mailto:[email protected]>>

Quoted 4 lines

---
Steps to see the issue (before this patch)
1) Insert g_zero in DUT
2) run './testusb -t 14 -c 1 -s 520 -v 1' in host (size should be >

512)

Quoted 2 lines

The test should FAIL since bounce buffer can handle only 512 bytes, but

the

Quoted 4 lines

test PASS. There is a WARN dump in DUT but still there will be memory corruption since the bounce buffer overflows.

Tested this patch using USB3 Gen X CV (ch9 tests: usb2 and usb3, link

layer

Quoted 9 lines

testing and MSC tests) and using USB2 X CV (ch9 tests, MSC tests).

After the patch, the tests timeout!
./testusb -t 14 -c 1 -s 514 -v 1
unknown speed /dev/bus/usb/001/018 0
/dev/bus/usb/001/018 test 14 --> 110 (Connection timed out)

IMO a patch to fix this is required for stable releases too. So If this patch is alright, I can post the patch cc'ing stable. While the actual

fix

drivers/usb/dwc3/ep0.c static void dwc3_ep0_complete_data(struct dwc3

Quoted 10 lines

would be to have chained TRB, I'm not sure if it can go to stable releases.

 drivers/usb/dwc3/ep0.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/usb/dwc3/ep0.c +0 -0
index 2ef3c8d..8858c60 100644
@@ -816,6 +816,11 @@ static void dwc3_ep0_complete_data(struct dwc3

*dwc,

drivers/usb/dwc3/ep0.c static void dwc3_ep0_complete_data(struct dwc3 *dwc,

Quoted 5 lines
drivers/usb/dwc3/ep0.c +2 -0
                unsigned maxp = ep0->endpoint.maxpacket;
 
                transfer_size += (maxp - (transfer_size % maxp));
+
+               /* Maximum of DWC3_EP0_BOUNCE_SIZE can only be received

*/

Quoted 3 lines
drivers/usb/dwc3/ep0.c +3 -0
+               if (transfer_size > DWC3_EP0_BOUNCE_SIZE)
+                       transfer_size = DWC3_EP0_BOUNCE_SIZE;
+
Quoted 5 lines
Quoted 1 line

Can you just use maxp in the correct way?

what do you mean by correct way? Using roundup() to calculate transfer_size?

just a max

Michael

Quoted 2 lines

Thanks
Kishon

Signature · 8 lines
| Michael Nazzareno Trimarchi                     Amarula Solutions BV |
| COO  -  Founder                                      Cruquiuskade 47 |
| +31(0)851119172                                 Amsterdam 1018 AM NL |
|                  [`as] http://www.amarulasolutions.com               |
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html