Skip to contentSkip to conversation

usb: core: wBytesPerInterval checked with unsanitized bMaxBurst

BUG New Cheng Yongkang 3 participants 4 messages Nov 15 – Nov 17 USB

lore
Share & tools
lore
#0 lore raw

Hi Greg, Alan,

I hit a corner case in usb_parse_ss_endpoint_companion(). It needs a SuperSpeed device with an invalid bMaxBurst or Mult, so it may not be worth fixing.

usb_parse_ss_endpoint_companion() clamps bMaxBurst to 15 and the isoc Mult to 3 in ep->ss_ep_comp, but then computes max_tx for the wBytesPerInterval check from the original descriptor. The stored wBytesPerInterval can then be larger than the clamped endpoint can carry.

With wMaxPacketSize=1024, bMaxBurst=16, Mult=1 and wBytesPerInterval=65535, bMaxBurst becomes 15 but wBytesPerInterval is set to 17408 instead of 16384. usb_submit_urb() uses the clamped values and rejects isoc frames above 16384, while usb_endpoint_max_periodic_payload() returns 17408, which xHCI also uses as the Max ESIT Payload.

On v7.3 with a virtual SuperSpeed device over USB/IP, the kernel logged "setting to 17408", and 16385- and 17000-byte isoc frames failed with -EMSGSIZE. I only tested the bMaxBurst case; the Mult case follows from the same code. The code is unchanged in next-20261113.

Using ep->ss_ep_comp in the max_tx calculation fixes it:

  -		max_tx = (desc->bMaxBurst + 1) *
  -			(USB_SS_MULT(desc->bmAttributes)) *
  +		max_tx = (ep->ss_ep_comp.bMaxBurst + 1) *
  +			(USB_SS_MULT(ep->ss_ep_comp.bmAttributes)) *
   			usb_endpoint_maxp(&ep->desc);
   	else if (usb_endpoint_xfer_int(&ep->desc))
   		max_tx = usb_endpoint_maxp(&ep->desc) *
  -			(desc->bMaxBurst + 1);
  +			(ep->ss_ep_comp.bMaxBurst + 1);

I can send a patch if you want one.

Thanks,
Cheng Yongkang

#1 lore raw
Quoted 37 lines · Cheng Yongkang

Hi Greg, Alan,

I hit a corner case in usb_parse_ss_endpoint_companion(). It needs a SuperSpeed device with an invalid bMaxBurst or Mult, so it may not be worth fixing.

usb_parse_ss_endpoint_companion() clamps bMaxBurst to 15 and the isoc Mult to 3 in ep->ss_ep_comp, but then computes max_tx for the wBytesPerInterval check from the original descriptor. The stored wBytesPerInterval can then be larger than the clamped endpoint can carry.

With wMaxPacketSize=1024, bMaxBurst=16, Mult=1 and wBytesPerInterval=65535, bMaxBurst becomes 15 but wBytesPerInterval is set to 17408 instead of 16384. usb_submit_urb() uses the clamped values and rejects isoc frames above 16384, while usb_endpoint_max_periodic_payload() returns 17408, which xHCI also uses as the Max ESIT Payload.

On v7.3 with a virtual SuperSpeed device over USB/IP, the kernel logged "setting to 17408", and 16385- and 17000-byte isoc frames failed with -EMSGSIZE. I only tested the bMaxBurst case; the Mult case follows from the same code. The code is unchanged in next-20261113.

Using ep->ss_ep_comp in the max_tx calculation fixes it:

  -		max_tx = (desc->bMaxBurst + 1) *
  -			(USB_SS_MULT(desc->bmAttributes)) *
  +		max_tx = (ep->ss_ep_comp.bMaxBurst + 1) *
  +			(USB_SS_MULT(ep->ss_ep_comp.bmAttributes)) *
   			usb_endpoint_maxp(&ep->desc);
   	else if (usb_endpoint_xfer_int(&ep->desc))
   		max_tx = usb_endpoint_maxp(&ep->desc) *
  -			(desc->bMaxBurst + 1);
  +			(ep->ss_ep_comp.bMaxBurst + 1);
Quoted 1 line

I can send a patch if you want one.

Please send a patch to the xHCI maintainer.

Alan Stern

#2 lore raw

PATCH 1/1 usb: core: use the clamped bMaxBurst and Mult for the wBytesPerInterval check

usb_parse_ss_endpoint_companion() clamps bMaxBurst to 15 and the isoc Mult to 3 in ep->ss_ep_comp, but then computes max_tx for the wBytesPerInterval check from the original descriptor. A device with an invalid bMaxBurst or Mult can then keep a wBytesPerInterval that the clamped endpoint cannot carry.

With wMaxPacketSize 1024, bMaxBurst 16 and Mult 1, bMaxBurst becomes 15 but wBytesPerInterval is limited to 17408 instead of 16384. usb_submit_urb() uses the clamped values and rejects isoc frames above 16384, while usb_endpoint_max_periodic_payload() returns 17408, which xHCI uses as the Max ESIT Payload. The same endpoint context gets a Max Burst of 15, so the two fields disagree.

The rest of the code treats ep->ss_ep_comp as the sanitized copy of the descriptor. usb_parse_endpoint() checks and fixes ep->desc the same way, and everything after parsing, including usb_submit_urb(), usb_endpoint_max_periodic_payload() and xHCI, reads only these copies. max_tx itself already takes wMaxPacketSize from ep->desc. Before commit 842f16905dfc ("USB: remove the usb_host_ss_ep_comp structure"), the clamps modified the descriptor in place, so max_tx was computed from the clamped values. That commit moved the clamps to the copy but left max_tx reading the original descriptor.

Compute max_tx from ep->ss_ep_comp.

Fixes 842f16905dfc ("USB: remove the usb_host_ss_ep_comp structure")

---
Report: https://lore.kernel.org/linux-usb/[email protected]/

 drivers/usb/core/config.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/usb/core/config.c +3 -3
index 0e79b7f8a73d..2f031ea6a437 100644
@@ -170,12 +170,12 @@ static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
 	}
 
 	if (usb_endpoint_xfer_isoc(&ep->desc))
-		max_tx = (desc->bMaxBurst + 1) *
-			(USB_SS_MULT(desc->bmAttributes)) *
+		max_tx = (ep->ss_ep_comp.bMaxBurst + 1) *
+			(USB_SS_MULT(ep->ss_ep_comp.bmAttributes)) *
 			usb_endpoint_maxp(&ep->desc);
 	else if (usb_endpoint_xfer_int(&ep->desc))
 		max_tx = usb_endpoint_maxp(&ep->desc) *
-			(desc->bMaxBurst + 1);
+			(ep->ss_ep_comp.bMaxBurst + 1);
 	else
 		max_tx = 999999;
 	/*

base-commit: 3149e8b3237f58d4dd440fc64c45c93404c4b064

Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH] usb: core: use the clamped bMaxBurst and Mult for the wBytesPerInterval check

Sashiko has reviewed this patch and found no issues.
Quoted 23 lines

usb_parse_ss_endpoint_companion() clamps bMaxBurst to 15 and the isoc Mult to 3 in ep->ss_ep_comp, but then computes max_tx for the wBytesPerInterval check from the original descriptor. A device with an invalid bMaxBurst or Mult can then keep a wBytesPerInterval that the clamped endpoint cannot carry.

With wMaxPacketSize 1024, bMaxBurst 16 and Mult 1, bMaxBurst becomes 15 but wBytesPerInterval is limited to 17408 instead of 16384. usb_submit_urb() uses the clamped values and rejects isoc frames above 16384, while usb_endpoint_max_periodic_payload() returns 17408, which xHCI uses as the Max ESIT Payload. The same endpoint context gets a Max Burst of 15, so the two fields disagree.

The rest of the code treats ep->ss_ep_comp as the sanitized copy of the descriptor. usb_parse_endpoint() checks and fixes ep->desc the same way, and everything after parsing, including usb_submit_urb(), usb_endpoint_max_periodic_payload() and xHCI, reads only these copies. max_tx itself already takes wMaxPacketSize from ep->desc. Before commit 842f16905dfc ("USB: remove the usb_host_ss_ep_comp structure"), the clamps modified the descriptor in place, so max_tx was computed from the clamped values. That commit moved the clamps to the copy but left max_tx reading the original descriptor.

Quoted 4 lines

Compute max_tx from ep->ss_ep_comp.

Fixes 842f16905dfc ("USB: remove the usb_host_ss_ep_comp structure")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1