Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length
@ 2026-11-25 12:12 Maoyi Xie
  2026-11-25 12:12 ` [PATCH 1/3] ALSA: bebob: bound the channel position walk " Maoyi Xie
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Maoyi Xie @ 2026-11-25 12:12 UTC (permalink / raw)
  To: Clemens Ladisch, Takashi Sakamoto
  Cc: Jaroslav Kysela, Takashi Iwai, linux-sound, stable

The BeBoB and OXFW drivers walk three AV/C responses by counts that the
device puts into the response. None of the walks is checked against the
length of the response. A count that is too large reads past the
response buffer, or later past a stored copy of the format.

Each patch rejects such a response with -EIO. A complete response is
handled as before. The formats in the Griffin FireWave dump at [1] pass
the new check. I found no such dump of a BeBoB device.

Found with a CodeQL-based static analysis tool that we are developing.
Each case was reproduced in a userspace harness under AddressSanitizer.
Not tested on hardware. Compile-tested with W=1 on next-20261116.

I used an LLM to help write the harnesses and these patches.

[1] https://lore.kernel.org/all/543FD6AA.5080004@sakamocchi.jp/

Maoyi Xie (3):
  ALSA: bebob: bound the channel position walk to the response length
  ALSA: bebob: bound the stream format entries to the response length
  ALSA: oxfw: bound the stream format entries to the response length

 sound/firewire/bebob/bebob.h         |  2 +-
 sound/firewire/bebob/bebob_command.c |  5 +++--
 sound/firewire/bebob/bebob_stream.c  | 15 +++++++++++++--
 sound/firewire/oxfw/oxfw-stream.c    | 22 ++++++++++++++++++++--
 4 files changed, 37 insertions(+), 7 deletions(-)

-- 
2.34.1


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

* [PATCH 1/3] ALSA: bebob: bound the channel position walk to the response length
  2026-11-25 12:12 [PATCH 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length Maoyi Xie
@ 2026-11-25 12:12 ` Maoyi Xie
  2026-11-25 12:12 ` [PATCH 2/3] ALSA: bebob: bound the stream format entries " Maoyi Xie
  2026-11-25 12:12 ` [PATCH 3/3] ALSA: oxfw: " Maoyi Xie
  2 siblings, 0 replies; 5+ messages in thread
From: Maoyi Xie @ 2026-11-25 12:12 UTC (permalink / raw)
  To: Clemens Ladisch, Takashi Sakamoto
  Cc: Jaroslav Kysela, Takashi Iwai, linux-sound, stable

map_data_channels() walks the channel position response by the section
and channel counts in that response. Nothing checks the counts against
the length of the response. A section of 128 channels can read past the
256-byte buffer.

Take the length of the data from avc_bridgeco_get_plug_ch_pos(). Return
-EIO for a section that does not fit.

Found with a CodeQL-based static analysis tool. Reproduced in a
userspace harness under AddressSanitizer. Not tested on hardware.

Fixes: eb7b3a056cd8 ("ALSA: bebob: Add commands and connections/streams management")
Cc: stable@vger.kernel.org
Signed-off-by: Maoyi Xie <maoyixie.tju@gmail.com>
---
I used an LLM to help write the harness and this patch.

 sound/firewire/bebob/bebob.h         |  2 +-
 sound/firewire/bebob/bebob_command.c |  5 +++--
 sound/firewire/bebob/bebob_stream.c  | 11 +++++++++--
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/sound/firewire/bebob/bebob.h b/sound/firewire/bebob/bebob.h
index 41e019b9e24e..7891a5802f1a 100644
--- a/sound/firewire/bebob/bebob.h
+++ b/sound/firewire/bebob/bebob.h
@@ -194,7 +194,7 @@ avc_bridgeco_fill_msu_addr(u8 buf[AVC_BRIDGECO_ADDR_BYTES],
 }
 int avc_bridgeco_get_plug_ch_pos(struct fw_unit *unit,
 				 u8 addr[AVC_BRIDGECO_ADDR_BYTES],
-				 u8 *buf, unsigned int len);
+				 u8 *buf, unsigned int *len);
 int avc_bridgeco_get_plug_type(struct fw_unit *unit,
 			       u8 addr[AVC_BRIDGECO_ADDR_BYTES],
 			       enum avc_bridgeco_plug_type *type);
diff --git a/sound/firewire/bebob/bebob_command.c b/sound/firewire/bebob/bebob_command.c
index 022df09c68ff..2e5a43e24615 100644
--- a/sound/firewire/bebob/bebob_command.c
+++ b/sound/firewire/bebob/bebob_command.c
@@ -181,14 +181,14 @@ end:
 
 int avc_bridgeco_get_plug_ch_pos(struct fw_unit *unit,
 				 u8 addr[AVC_BRIDGECO_ADDR_BYTES],
-				 u8 *buf, unsigned int len)
+				 u8 *buf, unsigned int *len)
 {
 	int err;
 
 	/* Info type is 'channel position'. */
 	avc_bridgeco_fill_plug_info_extension_command(buf, addr, 0x03);
 
-	err = fcp_avc_transaction(unit, buf, 12, buf, 256,
+	err = fcp_avc_transaction(unit, buf, 12, buf, *len,
 				  BIT(1) | BIT(2) | BIT(3) | BIT(4) |
 				  BIT(5) | BIT(6) | BIT(7) | BIT(9));
 	if (err < 0)
@@ -206,6 +206,7 @@ int avc_bridgeco_get_plug_ch_pos(struct fw_unit *unit,
 
 	/* Pick up specific data. */
 	memmove(buf, buf + 10, err - 10);
+	*len = err - 10;
 	err = 0;
 end:
 	return err;
diff --git a/sound/firewire/bebob/bebob_stream.c b/sound/firewire/bebob/bebob_stream.c
index 449cb17717f0..5850357cb3da 100644
--- a/sound/firewire/bebob/bebob_stream.c
+++ b/sound/firewire/bebob/bebob_stream.c
@@ -255,7 +255,7 @@ static int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
 {
 	unsigned int sec, sections, ch, channels;
 	unsigned int pcm, midi, location;
-	unsigned int stm_pos, sec_loc, pos;
+	unsigned int stm_pos, sec_loc, pos, len;
 	u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
 	enum avc_bridgeco_plug_dir dir;
 	int err;
@@ -274,7 +274,8 @@ static int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
 		dir = AVC_BRIDGECO_PLUG_DIR_IN;
 
 	avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
-	err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
+	len = 256;
+	err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, &len);
 	if (err < 0) {
 		dev_err(&bebob->unit->device,
 			"fail to get channel position for isoc %s plug 0: %d\n",
@@ -311,6 +312,12 @@ static int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
 			goto end;
 		}
 
+		/* this section should be within the response */
+		if (pos >= len || pos + 1 + buf[pos] * 2 > len) {
+			err = -EIO;
+			goto end;
+		}
+
 		/* the number of channels in this section */
 		channels = buf[pos++];
 
-- 
2.34.1


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

* [PATCH 2/3] ALSA: bebob: bound the stream format entries to the response length
  2026-11-25 12:12 [PATCH 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length Maoyi Xie
  2026-11-25 12:12 ` [PATCH 1/3] ALSA: bebob: bound the channel position walk " Maoyi Xie
@ 2026-11-25 12:12 ` Maoyi Xie
  2026-11-25 12:20   ` sashiko-bot
  2026-11-25 12:12 ` [PATCH 3/3] ALSA: oxfw: " Maoyi Xie
  2 siblings, 1 reply; 5+ messages in thread
From: Maoyi Xie @ 2026-11-25 12:12 UTC (permalink / raw)
  To: Clemens Ladisch, Takashi Sakamoto
  Cc: Jaroslav Kysela, Takashi Iwai, linux-sound, stable

parse_stream_formation() takes the number of two-byte entries of a
stream format from buf[4]. It ignores its len argument. A count of 62
or more can read past the 128-byte buffer. A smaller count can still
read bytes that are not part of the response.

Return -EIO when the entries do not fit into the response.

Found with a CodeQL-based static analysis tool. Reproduced in a
userspace harness under AddressSanitizer. Not tested on hardware.

Fixes: eb7b3a056cd8 ("ALSA: bebob: Add commands and connections/streams management")
Cc: stable@vger.kernel.org
Signed-off-by: Maoyi Xie <maoyixie.tju@gmail.com>
---
I used an LLM to help write the harness and this patch.

 sound/firewire/bebob/bebob_stream.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/sound/firewire/bebob/bebob_stream.c b/sound/firewire/bebob/bebob_stream.c
index 5850357cb3da..8eceafc70a44 100644
--- a/sound/firewire/bebob/bebob_stream.c
+++ b/sound/firewire/bebob/bebob_stream.c
@@ -730,6 +730,10 @@ parse_stream_formation(u8 *buf, unsigned int len,
 	if (i == ARRAY_SIZE(bridgeco_freq_table))
 		return -ENOSYS;
 
+	/* The entries should be within the response. */
+	if (len < 5 || len < 5 + buf[4] * 2)
+		return -EIO;
+
 	/* Avoid double count by different entries for the same rate. */
 	memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
 
-- 
2.34.1


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

* [PATCH 3/3] ALSA: oxfw: bound the stream format entries to the response length
  2026-11-25 12:12 [PATCH 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length Maoyi Xie
  2026-11-25 12:12 ` [PATCH 1/3] ALSA: bebob: bound the channel position walk " Maoyi Xie
  2026-11-25 12:12 ` [PATCH 2/3] ALSA: bebob: bound the stream format entries " Maoyi Xie
@ 2026-11-25 12:12 ` Maoyi Xie
  2 siblings, 0 replies; 5+ messages in thread
From: Maoyi Xie @ 2026-11-25 12:12 UTC (permalink / raw)
  To: Clemens Ladisch, Takashi Sakamoto
  Cc: Jaroslav Kysela, Takashi Iwai, linux-sound, stable

snd_oxfw_stream_parse_format() takes the number of two-byte entries of
a stream format from format[4] and never sees the length of the
response. A count of 254 or 255 can read past the 512-byte response
buffer. An accepted format is stored with the length of its response
and parsed again later. A count that needs more than that length reads
past the copy.

Check the length of a response against its entry count before the
format is parsed or stored. Formats other than AM824 compound still
reach the parser and get -ENXIO.

Found with a CodeQL-based static analysis tool. Reproduced in a
userspace harness under AddressSanitizer. Not tested on hardware.

Fixes: 5cd1d3f47a63 ("ALSA: oxfw: Change the way to make PCM rules/constraints")
Cc: stable@vger.kernel.org
Signed-off-by: Maoyi Xie <maoyixie.tju@gmail.com>
---
I used an LLM to help write the harness and this patch.

 sound/firewire/oxfw/oxfw-stream.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c
index 5e36d7153a7b..d8fe136369d8 100644
--- a/sound/firewire/oxfw/oxfw-stream.c
+++ b/sound/firewire/oxfw/oxfw-stream.c
@@ -484,6 +484,19 @@ void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
 	}
 }
 
+/*
+ * A format of AM824 compound has five bytes, followed by two bytes for each of its entries.
+ * A response can be shorter than that. The parser rejects every other format before it reads
+ * an entry.
+ */
+static bool format_has_all_entries(const u8 *format, unsigned int len)
+{
+	if (len < 2 || format[0] != 0x90 || format[1] != 0x40)
+		return true;
+
+	return len >= 5 && len >= 5 + format[4] * 2;
+}
+
 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
 				enum avc_general_plug_dir dir,
 				struct snd_oxfw_stream_formation *formation)
@@ -501,7 +514,7 @@ int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
 
 		err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
 		if (err >= 0) {
-			if (len < 3)
+			if (len < 3 || !format_has_all_entries(format, len))
 				err = -EIO;
 			else
 				err = snd_oxfw_stream_parse_format(format, formation);
@@ -662,6 +675,11 @@ assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
 		*len = 7;
 	}
 
+	if (!format_has_all_entries(buf, *len)) {
+		err = -EIO;
+		goto end;
+	}
+
 	/* parse and set stream format */
 	eid = 0;
 	err = snd_oxfw_stream_parse_format(buf, &formation);
@@ -740,7 +758,7 @@ static int fill_stream_formats(struct snd_oxfw *oxfw,
 	/* LIST subfunction is implemented */
 	while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
 		/* The format is too short. */
-		if (len < 3) {
+		if (len < 3 || !format_has_all_entries(buf, len)) {
 			err = -EIO;
 			break;
 		}
-- 
2.34.1


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

* Re: [PATCH 2/3] ALSA: bebob: bound the stream format entries to the response length
  2026-11-25 12:12 ` [PATCH 2/3] ALSA: bebob: bound the stream format entries " Maoyi Xie
@ 2026-11-25 12:20   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-25 12:20 UTC (permalink / raw)
  To: Maoyi Xie; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ALSA: bebob: parse_stream_formation() reads buf[0..2] before
  checking len
--

commit e6e7f1e1f29ad5a2012df4b6e2f6ca27670bc0c9
Author: Maoyi Xie <maoyixie.tju@gmail.com>

ALSA: bebob: bound the stream format entries to the response length

This commit checks in parse_stream_formation() that the stream format
entries described by buf[4] fit within the response length len, returning
-EIO otherwise.

> diff --git a/sound/firewire/bebob/bebob_stream.c b/sound/firewire/bebob/bebob_stream.c
> index 5850357cb3dad..8eceafc70a447 100644
> --- a/sound/firewire/bebob/bebob_stream.c
> +++ b/sound/firewire/bebob/bebob_stream.c
> @@ -730,6 +730,10 @@ parse_stream_formation(u8 *buf, unsigned int len,
>  	if (i == ARRAY_SIZE(bridgeco_freq_table))
>  		return -ENOSYS;
>  
> +	/* The entries should be within the response. */
> +	if (len < 5 || len < 5 + buf[4] * 2)
> +		return -EIO;

[Severity: Medium]
Should the length check come at the start of parse_stream_formation(),
before reading buf[0], buf[1] and buf[2]?

avc_bridgeco_get_plug_strm_fmt() accepts FCP responses of 12 bytes or
more, shifts err - 11 bytes to the front of buf, and sets *len = err - 11.
When len is 1..4, parse_stream_formation() reads buf[0..2] past the
len-byte response (hitting leftover request bytes in buf when len <= 2)
and can return -ENOSYS before reaching the len < 5 check.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125121238.1602172-1-maoyixie.tju@gmail.com?part=2

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

end of thread, other threads:[~2026-11-25 12:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 12:12 [PATCH 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length Maoyi Xie
2026-11-25 12:12 ` [PATCH 1/3] ALSA: bebob: bound the channel position walk " Maoyi Xie
2026-11-25 12:12 ` [PATCH 2/3] ALSA: bebob: bound the stream format entries " Maoyi Xie
2026-11-25 12:20   ` sashiko-bot
2026-11-25 12:12 ` [PATCH 3/3] ALSA: oxfw: " Maoyi Xie

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