Skip to contentSkip to conversation

usb: gadget: u_audio: don't exceed maxpacket when adding a residue frame

PATCH In review Kyle Farrell 3 participants 4 messages Oct 26 – Nov 16 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: u_audio: don't exceed maxpacket when adding a residue frame

u_audio_iso_complete() clamps the base packet size to ep->maxpacket and accumulates fractional-frame residue in p_residue_mil. Once a whole frame has accumulated, it is appended to the packet without checking whether it fits within ep->maxpacket.

Only append the extra frame if it fits. Consume the accumulated residue whether or not the frame is sent, preventing it from growing when the additional frame cannot fit. When the frame fits, behavior is unchanged. Otherwise, the stream cannot sustain the requested sample rate.

For example, with 27 channels of 32-bit samples at 19200 Hz on a high-speed link with p_hs_bint=3, the required packet size of 1080 bytes is capped at 1024. The base packet is 972 bytes, but appending a residue frame increases it to 1080 bytes, exceeding the allocated request buffer by 56 bytes.

Fixes 9bb87f168931 ("usb: gadget: f_uac2: send reasonably sized packets")
---
 drivers/usb/gadget/function/u_audio.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_audio.c +6 -2
index f0ee83abcebc..f73d1085c1d1 100644
@@ -230,9 +230,13 @@ static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
 		do_div(div_result, uac->p_interval);
 		do_div(div_result, 1000000);
 		if ((unsigned int) div_result >= uac->p_framesize) {
-			req->length += uac->p_framesize;
+			/* req->length is bounded by ep->maxpacket above. */
+			if (uac->p_framesize <= ep->maxpacket - req->length) {
+				req->length += uac->p_framesize;
+				pr_debug("increased req length to %d\n", req->length);
+			}
+
 			uac->p_residue_mil -= uac->p_framesize * p_interval_mil;
-			pr_debug("increased req length to %d\n", req->length);
 		}
 		pr_debug("remains uac->p_residue_mil %llu\n", uac->p_residue_mil);
 

base-commit: bdf12cde30830d46a8ef1ba93986356f1164b4f1

Signature · 1 line
2.34.1
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 17 lines

u_audio_iso_complete() clamps the base packet size to ep->maxpacket and accumulates fractional-frame residue in p_residue_mil. Once a whole frame has accumulated, it is appended to the packet without checking whether it fits within ep->maxpacket.

Only append the extra frame if it fits. Consume the accumulated residue whether or not the frame is sent, preventing it from growing when the additional frame cannot fit. When the frame fits, behavior is unchanged. Otherwise, the stream cannot sustain the requested sample rate.

For example, with 27 channels of 32-bit samples at 19200 Hz on a high-speed link with p_hs_bint=3, the required packet size of 1080 bytes is capped at 1024. The base packet is 972 bytes, but appending a residue frame increases it to 1080 bytes, exceeding the allocated request buffer by 56 bytes.

Quoted 3 lines
Fixes 9bb87f168931 ("usb: gadget: f_uac2: send reasonably sized packets")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Kyle Farrell
Quoted 47 lines · Kyle Farrell

u_audio_iso_complete() clamps the base packet size to ep->maxpacket and accumulates fractional-frame residue in p_residue_mil. Once a whole frame has accumulated, it is appended to the packet without checking whether it fits within ep->maxpacket.

Only append the extra frame if it fits. Consume the accumulated residue whether or not the frame is sent, preventing it from growing when the additional frame cannot fit. When the frame fits, behavior is unchanged. Otherwise, the stream cannot sustain the requested sample rate.

For example, with 27 channels of 32-bit samples at 19200 Hz on a high-speed link with p_hs_bint=3, the required packet size of 1080 bytes is capped at 1024. The base packet is 972 bytes, but appending a residue frame increases it to 1080 bytes, exceeding the allocated request buffer by 56 bytes.

Fixes 9bb87f168931 ("usb: gadget: f_uac2: send reasonably sized packets")
---
 drivers/usb/gadget/function/u_audio.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_audio.c +6 -2
index f0ee83abcebc..f73d1085c1d1 100644
@@ -230,9 +230,13 @@ static void u_audio_iso_complete(struct usb_ep
 *ep, struct usb_request *req)
 		do_div(div_result, uac->p_interval);
 		do_div(div_result, 1000000);
 		if ((unsigned int) div_result >= uac->p_framesize) {
-			req->length += uac->p_framesize;
+			/* req->length is bounded by ep->maxpacket above. */
+			if (uac->p_framesize <= ep->maxpacket - req->length) {
+				req->length += uac->p_framesize;
+				pr_debug("increased req length to %d\n", req->length);
+			}
+
 			uac->p_residue_mil -= uac->p_framesize * p_interval_mil;
-			pr_debug("increased req length to %d\n", req->length);
 		}
 		pr_debug("remains uac->p_residue_mil %llu\n", uac->p_residue_mil);
Quoted 1 line

base-commit: bdf12cde30830d46a8ef1ba93986356f1164b4f1

This patch also is suitable for stable backporting, as it fixes a potential out-of-bounds write in existing configurations. Please add the following trailer when applying:

Thanks,
Kyle Farrell

Reviewed-by on v1 1/1
#3 lore raw
In reply to Kyle Farrell
Quoted 11 lines

u_audio_iso_complete() clamps the base packet size to ep->maxpacket and accumulates fractional-frame residue in p_residue_mil. Once a whole frame has accumulated, it is appended to the packet without checking whether it fits within ep->maxpacket.

Only append the extra frame if it fits. Consume the accumulated residue whether or not the frame is sent, preventing it from growing when the additional frame cannot fit. When the frame fits, behavior is unchanged. Otherwise, the stream cannot sustain the requested sample rate.

Quoted 7 lines

For example, with 27 channels of 32-bit samples at 19200 Hz on a high-speed link with p_hs_bint=3, the required packet size of 1080 bytes is capped at 1024. The base packet is 972 bytes, but appending a residue frame increases it to 1080 bytes, exceeding the allocated request buffer by 56 bytes.

Fixes 9bb87f168931 ("usb: gadget: f_uac2: send reasonably sized packets")

Thanks for the patch, though looks like you forgot to Cc:
[email protected].

Signature · 1 line
Neill Kapron <[email protected]>