Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: caiaq: stop the iso packet parsers before a partial block
@ 2026-11-19 13:16 Alexandre Becholey
  2026-11-23  8:57 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Alexandre Becholey @ 2026-11-19 13:16 UTC (permalink / raw)
  To: Daniel Mack, Jaroslav Kysela, Takashi Iwai, Karsten Wiese
  Cc: Alexandre Becholey, stable, Daniel Mack, Takashi Iwai,
	Jaroslav Kysela, linux-sound

The iso packet parsers in audio.c walk a packet in blocks of one sample
per stream, but only compare the start of each block with the packet
length. The block size follows from the channel counts the device
reports, so it need not divide the packet length, and the last block of
a packet runs past its end. A device reporting e.g. 62 channels can make
the driver read and write up to 232 bytes past the capture and playback
URB buffers in URB completion context.

Make each parser round the packet length down to a multiple of its own
block size, which depends only on n_streams and not on
spec.data_alignment (the device can change that at any time), and zero
the unused tail of a playback packet.

The bug was found, and the reproducer and this fix were written, by AI
agents (Claude Mythos and Claude Opus models). The analysis and the
patch were reviewed by a human.

Fixes: 523f1dce3743 ("[ALSA] Add Native Instrument usb audio device support")
Cc: stable@vger.kernel.org
Signed-off-by: Alexandre Becholey <alex@becholey.ch>
---
An emulated caiaq device on QEMU xHCI reporting 62 channels and sending
512-byte iso packets triggers KASAN slab-out-of-bounds in
read_completed() on v7.3.1 and master 9d66f26fefac, as do 3- and
12-stream variants in modes 0, 2 and 3, also with the device switching
data_alignment mid-stream. With the fix: no report in 85 boots (20 on
master); a well-behaved 4-stream device streams as before in modes 0, 2
and 3. Not tested on real hardware. Reproducer available on request.

 sound/usb/caiaq/audio.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/sound/usb/caiaq/audio.c b/sound/usb/caiaq/audio.c
index bb6280aa3533..c7d47f0833da 100644
--- a/sound/usb/caiaq/audio.c
+++ b/sound/usb/caiaq/audio.c
@@ -332,13 +332,14 @@ static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
 			      const struct usb_iso_packet_descriptor *iso)
 {
 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
+	unsigned int len = rounddown(iso->actual_length, cdev->n_streams);
 	struct snd_pcm_substream *sub;
 	int stream, i;
 
 	if (all_substreams_zero(cdev->sub_capture))
 		return;
 
-	for (i = 0; i < iso->actual_length;) {
+	for (i = 0; i < len;) {
 		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
 			sub = cdev->sub_capture[stream];
 			if (sub) {
@@ -360,11 +361,13 @@ static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
 			      const struct usb_iso_packet_descriptor *iso)
 {
 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
+	unsigned int len = rounddown(iso->actual_length,
+				     cdev->n_streams * BYTES_PER_SAMPLE_USB);
 	unsigned char check_byte;
 	struct snd_pcm_substream *sub;
 	int stream, i;
 
-	for (i = 0; i < iso->actual_length;) {
+	for (i = 0; i < len;) {
 		if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
 			for (stream = 0;
 			     stream < cdev->n_streams;
@@ -407,6 +410,8 @@ static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
 			      const struct usb_iso_packet_descriptor *iso)
 {
 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
+	unsigned int len = rounddown(iso->actual_length, cdev->n_streams *
+				     BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM);
 	struct device *dev = caiaqdev_to_dev(cdev);
 	int stream, i;
 
@@ -414,7 +419,7 @@ static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
 	if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
 		return;
 
-	for (i = 0; i < iso->actual_length;) {
+	for (i = 0; i < len;) {
 		for (stream = 0; stream < cdev->n_streams; stream++) {
 			struct snd_pcm_substream *sub = cdev->sub_capture[stream];
 			char *audio_buf = NULL;
@@ -495,10 +500,11 @@ static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
 				const struct usb_iso_packet_descriptor *iso)
 {
 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
+	unsigned int len = rounddown(iso->length, cdev->n_streams);
 	struct snd_pcm_substream *sub;
 	int stream, i;
 
-	for (i = 0; i < iso->length;) {
+	for (i = 0; i < len;) {
 		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
 			sub = cdev->sub_playback[stream];
 			if (sub) {
@@ -516,12 +522,14 @@ static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
 		}
 
 		/* fill in the check bytes */
-		if (cdev->spec.data_alignment == 2 &&
+		if (cdev->spec.data_alignment == 2 && i < len &&
 		    i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
 		        (cdev->n_streams * CHANNELS_PER_STREAM))
 			for (stream = 0; stream < cdev->n_streams; stream++, i++)
 				usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
 	}
+
+	memset(usb_buf + len, 0, iso->length - len);
 }
 
 static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
@@ -529,9 +537,11 @@ static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
 				const struct usb_iso_packet_descriptor *iso)
 {
 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
+	unsigned int len = rounddown(iso->length, cdev->n_streams *
+				     BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM);
 	int stream, i;
 
-	for (i = 0; i < iso->length;) {
+	for (i = 0; i < len;) {
 		for (stream = 0; stream < cdev->n_streams; stream++) {
 			struct snd_pcm_substream *sub = cdev->sub_playback[stream];
 			char *audio_buf = NULL;
@@ -565,6 +575,8 @@ static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
 			}
 		}
 	}
+
+	memset(usb_buf + len, 0, iso->length - len);
 }
 
 static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
-- 
2.53.0



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

* Re: [PATCH] ALSA: caiaq: stop the iso packet parsers before a partial block
  2026-11-19 13:16 [PATCH] ALSA: caiaq: stop the iso packet parsers before a partial block Alexandre Becholey
@ 2026-11-23  8:57 ` Takashi Iwai
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2026-11-23  8:57 UTC (permalink / raw)
  To: Alexandre Becholey
  Cc: Daniel Mack, Jaroslav Kysela, Takashi Iwai, Karsten Wiese, stable,
	Daniel Mack, Takashi Iwai, Jaroslav Kysela, linux-sound

On Thu, 19 Nov 2026 14:16:55 +0100,
Alexandre Becholey wrote:
> 
> The iso packet parsers in audio.c walk a packet in blocks of one sample
> per stream, but only compare the start of each block with the packet
> length. The block size follows from the channel counts the device
> reports, so it need not divide the packet length, and the last block of
> a packet runs past its end. A device reporting e.g. 62 channels can make
> the driver read and write up to 232 bytes past the capture and playback
> URB buffers in URB completion context.
> 
> Make each parser round the packet length down to a multiple of its own
> block size, which depends only on n_streams and not on
> spec.data_alignment (the device can change that at any time), and zero
> the unused tail of a playback packet.
> 
> The bug was found, and the reproducer and this fix were written, by AI
> agents (Claude Mythos and Claude Opus models). The analysis and the
> patch were reviewed by a human.
> 
> Fixes: 523f1dce3743 ("[ALSA] Add Native Instrument usb audio device support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Alexandre Becholey <alex@becholey.ch>
> ---
> An emulated caiaq device on QEMU xHCI reporting 62 channels and sending
> 512-byte iso packets triggers KASAN slab-out-of-bounds in
> read_completed() on v7.3.1 and master 9d66f26fefac, as do 3- and
> 12-stream variants in modes 0, 2 and 3, also with the device switching
> data_alignment mid-stream. With the fix: no report in 85 boots (20 on
> master); a well-behaved 4-stream device streams as before in modes 0, 2
> and 3. Not tested on real hardware. Reproducer available on request.

OK, I applied now to for-next branch, and let's hope this works on the
actual device, too.


thanks,

Takashi

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

end of thread, other threads:[~2026-11-23  8:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 13:16 [PATCH] ALSA: caiaq: stop the iso packet parsers before a partial block Alexandre Becholey
2026-11-23  8:57 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox