* [PATCH v2 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length
@ 2026-11-30 10:49 Maoyi Xie
2026-11-30 10:49 ` [PATCH v2 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-30 10:49 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.
Changes in v2:
- Patch 3 checks the length in snd_oxfw_stream_parse_format() instead of
a new function, as Takashi Sakamoto suggested. The stored formats keep
their length for the later calls.
- Patches 1 and 2 are unchanged.
v1: https://lore.kernel.org/all/20261125121238.1602172-1-maoyixie.tju@gmail.com/
[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-pcm.c | 44 +++++++++++++++++++------
sound/firewire/oxfw/oxfw-proc.c | 6 ++--
sound/firewire/oxfw/oxfw-stream.c | 48 +++++++++++++++++++++-------
sound/firewire/oxfw/oxfw.h | 3 ++
7 files changed, 95 insertions(+), 28 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v2 1/3] ALSA: bebob: bound the channel position walk to the response length 2026-11-30 10:49 [PATCH v2 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length Maoyi Xie @ 2026-11-30 10:49 ` Maoyi Xie 2026-11-30 10:49 ` [PATCH v2 2/3] ALSA: bebob: bound the stream format entries " Maoyi Xie 2026-11-30 10:49 ` [PATCH v2 3/3] ALSA: oxfw: " Maoyi Xie 2 siblings, 0 replies; 5+ messages in thread From: Maoyi Xie @ 2026-11-30 10:49 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. v2: No change. 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 v2 2/3] ALSA: bebob: bound the stream format entries to the response length 2026-11-30 10:49 [PATCH v2 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length Maoyi Xie 2026-11-30 10:49 ` [PATCH v2 1/3] ALSA: bebob: bound the channel position walk " Maoyi Xie @ 2026-11-30 10:49 ` Maoyi Xie 2026-11-30 10:49 ` [PATCH v2 3/3] ALSA: oxfw: " Maoyi Xie 2 siblings, 0 replies; 5+ messages in thread From: Maoyi Xie @ 2026-11-30 10:49 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. v2: No change. 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 v2 3/3] ALSA: oxfw: bound the stream format entries to the response length 2026-11-30 10:49 [PATCH v2 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length Maoyi Xie 2026-11-30 10:49 ` [PATCH v2 1/3] ALSA: bebob: bound the channel position walk " Maoyi Xie 2026-11-30 10:49 ` [PATCH v2 2/3] ALSA: bebob: bound the stream format entries " Maoyi Xie @ 2026-11-30 10:49 ` Maoyi Xie 2026-12-01 13:43 ` Takashi Sakamoto 2 siblings, 1 reply; 5+ messages in thread From: Maoyi Xie @ 2026-11-30 10:49 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 copied into a buffer of the response length and parsed again later. A count that needs more than that length reads past the copy. Pass the length to snd_oxfw_stream_parse_format() and return -EIO when the entries do not fit. Keep the length of each copy for the later calls. Formats other than AM824 compound still 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") Suggested-by: Takashi Sakamoto <o-takashi@sakamocchi.jp> 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. v2: Move the check into snd_oxfw_stream_parse_format() (Takashi Sakamoto). sound/firewire/oxfw/oxfw-pcm.c | 44 ++++++++++++++++++++++------ sound/firewire/oxfw/oxfw-proc.c | 6 ++-- sound/firewire/oxfw/oxfw-stream.c | 48 +++++++++++++++++++++++-------- sound/firewire/oxfw/oxfw.h | 3 ++ 4 files changed, 78 insertions(+), 23 deletions(-) diff --git a/sound/firewire/oxfw/oxfw-pcm.c b/sound/firewire/oxfw/oxfw-pcm.c index 774b8a763795..4b8417ba1227 100644 --- a/sound/firewire/oxfw/oxfw-pcm.c +++ b/sound/firewire/oxfw/oxfw-pcm.c @@ -10,7 +10,10 @@ static int hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) { - u8 **formats = rule->private; + struct snd_pcm_substream *substream = rule->private; + struct snd_oxfw *oxfw = substream->private_data; + u8 **formats; + const unsigned int *lens; struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); const struct snd_interval *c = @@ -21,11 +24,19 @@ static int hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_oxfw_stream_formation formation; int i, err; + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { + formats = oxfw->tx_stream_formats; + lens = oxfw->tx_stream_format_lens; + } else { + formats = oxfw->rx_stream_formats; + lens = oxfw->rx_stream_format_lens; + } + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { if (formats[i] == NULL) continue; - err = snd_oxfw_stream_parse_format(formats[i], &formation); + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &formation); if (err < 0) continue; if (!snd_interval_test(c, formation.pcm)) @@ -41,7 +52,10 @@ static int hw_rule_rate(struct snd_pcm_hw_params *params, static int hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) { - u8 **formats = rule->private; + struct snd_pcm_substream *substream = rule->private; + struct snd_oxfw *oxfw = substream->private_data; + u8 **formats; + const unsigned int *lens; struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); const struct snd_interval *r = @@ -50,12 +64,20 @@ static int hw_rule_channels(struct snd_pcm_hw_params *params, int i, j, err; unsigned int count, list[SND_OXFW_STREAM_FORMAT_ENTRIES] = {0}; + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { + formats = oxfw->tx_stream_formats; + lens = oxfw->tx_stream_format_lens; + } else { + formats = oxfw->rx_stream_formats; + lens = oxfw->rx_stream_format_lens; + } + count = 0; for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { if (formats[i] == NULL) break; - err = snd_oxfw_stream_parse_format(formats[i], &formation); + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &formation); if (err < 0) continue; if (!snd_interval_test(r, formation.rate)) @@ -77,7 +99,8 @@ static int hw_rule_channels(struct snd_pcm_hw_params *params, return snd_interval_list(c, count, list, 0); } -static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats) +static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats, + const unsigned int *lens) { struct snd_oxfw_stream_formation formation; int i, err; @@ -93,7 +116,7 @@ static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats) if (formats[i] == NULL) break; - err = snd_oxfw_stream_parse_format(formats[i], &formation); + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &formation); if (err < 0) continue; @@ -111,6 +134,7 @@ static int init_hw_params(struct snd_oxfw *oxfw, { struct snd_pcm_runtime *runtime = substream->runtime; u8 **formats; + const unsigned int *lens; struct amdtp_stream *stream; int err; @@ -118,22 +142,24 @@ static int init_hw_params(struct snd_oxfw *oxfw, runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS; stream = &oxfw->tx_stream; formats = oxfw->tx_stream_formats; + lens = oxfw->tx_stream_format_lens; } else { runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS; stream = &oxfw->rx_stream; formats = oxfw->rx_stream_formats; + lens = oxfw->rx_stream_format_lens; } - limit_channels_and_rates(&runtime->hw, formats); + limit_channels_and_rates(&runtime->hw, formats, lens); err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, - hw_rule_channels, formats, + hw_rule_channels, substream, SNDRV_PCM_HW_PARAM_RATE, -1); if (err < 0) goto end; err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, - hw_rule_rate, formats, + hw_rule_rate, substream, SNDRV_PCM_HW_PARAM_CHANNELS, -1); if (err < 0) goto end; diff --git a/sound/firewire/oxfw/oxfw-proc.c b/sound/firewire/oxfw/oxfw-proc.c index 260c60364f39..bcae6299ceca 100644 --- a/sound/firewire/oxfw/oxfw-proc.c +++ b/sound/firewire/oxfw/oxfw-proc.c @@ -30,7 +30,8 @@ static void proc_read_formation(struct snd_info_entry *entry, if (format == NULL) continue; - err = snd_oxfw_stream_parse_format(format, &formation); + err = snd_oxfw_stream_parse_format(format, oxfw->rx_stream_format_lens[i], + &formation); if (err < 0) continue; @@ -60,7 +61,8 @@ static void proc_read_formation(struct snd_info_entry *entry, if (format == NULL) continue; - err = snd_oxfw_stream_parse_format(format, &formation); + err = snd_oxfw_stream_parse_format(format, oxfw->tx_stream_format_lens[i], + &formation); if (err < 0) continue; diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c index 5e36d7153a7b..034bdaae09df 100644 --- a/sound/firewire/oxfw/oxfw-stream.c +++ b/sound/firewire/oxfw/oxfw-stream.c @@ -58,6 +58,7 @@ static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, unsigned int rate, unsigned int pcm_channels) { u8 **formats; + const unsigned int *lens; struct snd_oxfw_stream_formation formation; enum avc_general_plug_dir dir; unsigned int len; @@ -65,15 +66,17 @@ static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, if (s == &oxfw->tx_stream) { formats = oxfw->tx_stream_formats; + lens = oxfw->tx_stream_format_lens; dir = AVC_GENERAL_PLUG_DIR_OUT; } else { formats = oxfw->rx_stream_formats; + lens = oxfw->rx_stream_format_lens; dir = AVC_GENERAL_PLUG_DIR_IN; } /* Seek stream format for requirements. */ for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { - err = snd_oxfw_stream_parse_format(formats[i], &formation); + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &formation); if (err < 0) return err; @@ -202,6 +205,7 @@ static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream) { enum avc_general_plug_dir dir; u8 **formats; + const unsigned int *lens; struct snd_oxfw_stream_formation formation; struct cmp_connection *conn; int i; @@ -210,10 +214,12 @@ static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream) if (stream == &oxfw->rx_stream) { dir = AVC_GENERAL_PLUG_DIR_IN; formats = oxfw->rx_stream_formats; + lens = oxfw->rx_stream_format_lens; conn = &oxfw->in_conn; } else { dir = AVC_GENERAL_PLUG_DIR_OUT; formats = oxfw->tx_stream_formats; + lens = oxfw->tx_stream_format_lens; conn = &oxfw->out_conn; } @@ -227,7 +233,7 @@ static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream) if (formats[i] == NULL) break; - err = snd_oxfw_stream_parse_format(formats[i], &fmt); + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &fmt); if (err < 0) return err; @@ -504,7 +510,7 @@ int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, if (len < 3) err = -EIO; else - err = snd_oxfw_stream_parse_format(format, formation); + err = snd_oxfw_stream_parse_format(format, len, formation); } kfree(format); @@ -513,22 +519,26 @@ int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, // command. Use the duplicated hard-coded format, instead. unsigned int rate; u8 *const *formats; + const unsigned int *lens; int i; err = avc_general_get_sig_fmt(oxfw->unit, &rate, dir, 0); if (err < 0) return err; - if (dir == AVC_GENERAL_PLUG_DIR_IN) + if (dir == AVC_GENERAL_PLUG_DIR_IN) { formats = oxfw->rx_stream_formats; - else + lens = oxfw->rx_stream_format_lens; + } else { formats = oxfw->tx_stream_formats; + lens = oxfw->tx_stream_format_lens; + } for (i = 0; (i < SND_OXFW_STREAM_FORMAT_ENTRIES); ++i) { if (!formats[i]) continue; - err = snd_oxfw_stream_parse_format(formats[i], formation); + err = snd_oxfw_stream_parse_format(formats[i], lens[i], formation); if (err < 0) continue; @@ -549,6 +559,7 @@ int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005 */ int snd_oxfw_stream_parse_format(const u8 *format, + unsigned int len, struct snd_oxfw_stream_formation *formation) { unsigned int i, e, channels, type; @@ -563,6 +574,10 @@ int snd_oxfw_stream_parse_format(const u8 *format, if ((format[0] != 0x90) || (format[1] != 0x40)) return -ENXIO; + /* The entries should be within the response. */ + if (len < 5 || len < 5 + format[4] * 2) + return -EIO; + /* check the sampling rate */ for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) { if (format[2] == avc_stream_rate_table[i]) @@ -627,7 +642,7 @@ int snd_oxfw_stream_parse_format(const u8 *format, static int assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, unsigned int pid, u8 *buf, unsigned int *len, - u8 **formats) + u8 **formats, unsigned int *lens) { struct snd_oxfw_stream_formation formation; unsigned int i, eid; @@ -664,7 +679,7 @@ assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, /* parse and set stream format */ eid = 0; - err = snd_oxfw_stream_parse_format(buf, &formation); + err = snd_oxfw_stream_parse_format(buf, *len, &formation); if (err < 0) goto end; @@ -674,6 +689,7 @@ assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, err = -ENOMEM; goto end; } + lens[eid] = *len; /* apply the format for each available sampling rate */ for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) { @@ -693,6 +709,7 @@ assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, err = -ENOMEM; goto end; } + lens[eid] = *len; formats[eid][2] = avc_stream_rate_table[i]; } @@ -707,6 +724,7 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, unsigned short pid) { u8 *buf, **formats; + unsigned int *lens; unsigned int len, eid = 0; struct snd_oxfw_stream_formation dummy; int err; @@ -715,10 +733,13 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, if (buf == NULL) return -ENOMEM; - if (dir == AVC_GENERAL_PLUG_DIR_OUT) + if (dir == AVC_GENERAL_PLUG_DIR_OUT) { formats = oxfw->tx_stream_formats; - else + lens = oxfw->tx_stream_format_lens; + } else { formats = oxfw->rx_stream_formats; + lens = oxfw->rx_stream_format_lens; + } /* get first entry */ len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; @@ -727,7 +748,7 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, /* LIST subfunction is not implemented */ len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; err = assume_stream_formats(oxfw, dir, pid, buf, &len, - formats); + formats, lens); goto end; } else if (err < 0) { dev_err(&oxfw->unit->device, @@ -746,7 +767,7 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, } /* parse and set stream format */ - err = snd_oxfw_stream_parse_format(buf, &dummy); + err = snd_oxfw_stream_parse_format(buf, len, &dummy); if (err < 0) break; @@ -756,6 +777,7 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, err = -ENOMEM; break; } + lens[eid] = len; /* get next entry */ len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; @@ -814,6 +836,7 @@ int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) if (format == NULL) continue; err = snd_oxfw_stream_parse_format(format, + oxfw->tx_stream_format_lens[i], &formation); if (err < 0) continue; @@ -842,6 +865,7 @@ int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) if (format == NULL) continue; err = snd_oxfw_stream_parse_format(format, + oxfw->rx_stream_format_lens[i], &formation); if (err < 0) continue; diff --git a/sound/firewire/oxfw/oxfw.h b/sound/firewire/oxfw/oxfw.h index aeb83e1595b7..439182c50c8a 100644 --- a/sound/firewire/oxfw/oxfw.h +++ b/sound/firewire/oxfw/oxfw.h @@ -72,6 +72,8 @@ struct snd_oxfw { bool has_input; u8 *tx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES]; u8 *rx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES]; + unsigned int tx_stream_format_lens[SND_OXFW_STREAM_FORMAT_ENTRIES]; + unsigned int rx_stream_format_lens[SND_OXFW_STREAM_FORMAT_ENTRIES]; bool assumed; struct cmp_connection out_conn; struct cmp_connection in_conn; @@ -141,6 +143,7 @@ struct snd_oxfw_stream_formation { unsigned int midi; }; int snd_oxfw_stream_parse_format(const u8 *format, + unsigned int len, struct snd_oxfw_stream_formation *formation); int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, -- 2.34.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 3/3] ALSA: oxfw: bound the stream format entries to the response length 2026-11-30 10:49 ` [PATCH v2 3/3] ALSA: oxfw: " Maoyi Xie @ 2026-12-01 13:43 ` Takashi Sakamoto 0 siblings, 0 replies; 5+ messages in thread From: Takashi Sakamoto @ 2026-12-01 13:43 UTC (permalink / raw) To: Maoyi Xie Cc: Clemens Ladisch, Jaroslav Kysela, Takashi Iwai, linux-sound, stable Hi, Thank you for revising the patch. However, it is no need to validate the stream formats again once they are checked and cached. Validation is only required along the call graph from snd_oxfw_stream_discover() and serves no purpose elsewhere. There is also no need to store the length of the stream formats in the structure. Thanks Takashi Sakamoto On Mon, Nov 30, 2026 at 06:49:49PM +0800, Maoyi Xie wrote: > 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 copied into a buffer of the response > length and parsed again later. A count that needs more than that length > reads past the copy. > > Pass the length to snd_oxfw_stream_parse_format() and return -EIO when > the entries do not fit. Keep the length of each copy for the later > calls. Formats other than AM824 compound still 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") > Suggested-by: Takashi Sakamoto <o-takashi@sakamocchi.jp> > 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. > > v2: Move the check into snd_oxfw_stream_parse_format() (Takashi Sakamoto). > > sound/firewire/oxfw/oxfw-pcm.c | 44 ++++++++++++++++++++++------ > sound/firewire/oxfw/oxfw-proc.c | 6 ++-- > sound/firewire/oxfw/oxfw-stream.c | 48 +++++++++++++++++++++++-------- > sound/firewire/oxfw/oxfw.h | 3 ++ > 4 files changed, 78 insertions(+), 23 deletions(-) > > diff --git a/sound/firewire/oxfw/oxfw-pcm.c b/sound/firewire/oxfw/oxfw-pcm.c > index 774b8a763795..4b8417ba1227 100644 > --- a/sound/firewire/oxfw/oxfw-pcm.c > +++ b/sound/firewire/oxfw/oxfw-pcm.c > @@ -10,7 +10,10 @@ > static int hw_rule_rate(struct snd_pcm_hw_params *params, > struct snd_pcm_hw_rule *rule) > { > - u8 **formats = rule->private; > + struct snd_pcm_substream *substream = rule->private; > + struct snd_oxfw *oxfw = substream->private_data; > + u8 **formats; > + const unsigned int *lens; > struct snd_interval *r = > hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); > const struct snd_interval *c = > @@ -21,11 +24,19 @@ static int hw_rule_rate(struct snd_pcm_hw_params *params, > struct snd_oxfw_stream_formation formation; > int i, err; > > + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { > + formats = oxfw->tx_stream_formats; > + lens = oxfw->tx_stream_format_lens; > + } else { > + formats = oxfw->rx_stream_formats; > + lens = oxfw->rx_stream_format_lens; > + } > + > for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { > if (formats[i] == NULL) > continue; > > - err = snd_oxfw_stream_parse_format(formats[i], &formation); > + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &formation); > if (err < 0) > continue; > if (!snd_interval_test(c, formation.pcm)) > @@ -41,7 +52,10 @@ static int hw_rule_rate(struct snd_pcm_hw_params *params, > static int hw_rule_channels(struct snd_pcm_hw_params *params, > struct snd_pcm_hw_rule *rule) > { > - u8 **formats = rule->private; > + struct snd_pcm_substream *substream = rule->private; > + struct snd_oxfw *oxfw = substream->private_data; > + u8 **formats; > + const unsigned int *lens; > struct snd_interval *c = > hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); > const struct snd_interval *r = > @@ -50,12 +64,20 @@ static int hw_rule_channels(struct snd_pcm_hw_params *params, > int i, j, err; > unsigned int count, list[SND_OXFW_STREAM_FORMAT_ENTRIES] = {0}; > > + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { > + formats = oxfw->tx_stream_formats; > + lens = oxfw->tx_stream_format_lens; > + } else { > + formats = oxfw->rx_stream_formats; > + lens = oxfw->rx_stream_format_lens; > + } > + > count = 0; > for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { > if (formats[i] == NULL) > break; > > - err = snd_oxfw_stream_parse_format(formats[i], &formation); > + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &formation); > if (err < 0) > continue; > if (!snd_interval_test(r, formation.rate)) > @@ -77,7 +99,8 @@ static int hw_rule_channels(struct snd_pcm_hw_params *params, > return snd_interval_list(c, count, list, 0); > } > > -static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats) > +static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats, > + const unsigned int *lens) > { > struct snd_oxfw_stream_formation formation; > int i, err; > @@ -93,7 +116,7 @@ static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats) > if (formats[i] == NULL) > break; > > - err = snd_oxfw_stream_parse_format(formats[i], &formation); > + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &formation); > if (err < 0) > continue; > > @@ -111,6 +134,7 @@ static int init_hw_params(struct snd_oxfw *oxfw, > { > struct snd_pcm_runtime *runtime = substream->runtime; > u8 **formats; > + const unsigned int *lens; > struct amdtp_stream *stream; > int err; > > @@ -118,22 +142,24 @@ static int init_hw_params(struct snd_oxfw *oxfw, > runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS; > stream = &oxfw->tx_stream; > formats = oxfw->tx_stream_formats; > + lens = oxfw->tx_stream_format_lens; > } else { > runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS; > stream = &oxfw->rx_stream; > formats = oxfw->rx_stream_formats; > + lens = oxfw->rx_stream_format_lens; > } > > - limit_channels_and_rates(&runtime->hw, formats); > + limit_channels_and_rates(&runtime->hw, formats, lens); > > err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, > - hw_rule_channels, formats, > + hw_rule_channels, substream, > SNDRV_PCM_HW_PARAM_RATE, -1); > if (err < 0) > goto end; > > err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, > - hw_rule_rate, formats, > + hw_rule_rate, substream, > SNDRV_PCM_HW_PARAM_CHANNELS, -1); > if (err < 0) > goto end; > diff --git a/sound/firewire/oxfw/oxfw-proc.c b/sound/firewire/oxfw/oxfw-proc.c > index 260c60364f39..bcae6299ceca 100644 > --- a/sound/firewire/oxfw/oxfw-proc.c > +++ b/sound/firewire/oxfw/oxfw-proc.c > @@ -30,7 +30,8 @@ static void proc_read_formation(struct snd_info_entry *entry, > if (format == NULL) > continue; > > - err = snd_oxfw_stream_parse_format(format, &formation); > + err = snd_oxfw_stream_parse_format(format, oxfw->rx_stream_format_lens[i], > + &formation); > if (err < 0) > continue; > > @@ -60,7 +61,8 @@ static void proc_read_formation(struct snd_info_entry *entry, > if (format == NULL) > continue; > > - err = snd_oxfw_stream_parse_format(format, &formation); > + err = snd_oxfw_stream_parse_format(format, oxfw->tx_stream_format_lens[i], > + &formation); > if (err < 0) > continue; > > diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c > index 5e36d7153a7b..034bdaae09df 100644 > --- a/sound/firewire/oxfw/oxfw-stream.c > +++ b/sound/firewire/oxfw/oxfw-stream.c > @@ -58,6 +58,7 @@ static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, > unsigned int rate, unsigned int pcm_channels) > { > u8 **formats; > + const unsigned int *lens; > struct snd_oxfw_stream_formation formation; > enum avc_general_plug_dir dir; > unsigned int len; > @@ -65,15 +66,17 @@ static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, > > if (s == &oxfw->tx_stream) { > formats = oxfw->tx_stream_formats; > + lens = oxfw->tx_stream_format_lens; > dir = AVC_GENERAL_PLUG_DIR_OUT; > } else { > formats = oxfw->rx_stream_formats; > + lens = oxfw->rx_stream_format_lens; > dir = AVC_GENERAL_PLUG_DIR_IN; > } > > /* Seek stream format for requirements. */ > for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { > - err = snd_oxfw_stream_parse_format(formats[i], &formation); > + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &formation); > if (err < 0) > return err; > > @@ -202,6 +205,7 @@ static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream) > { > enum avc_general_plug_dir dir; > u8 **formats; > + const unsigned int *lens; > struct snd_oxfw_stream_formation formation; > struct cmp_connection *conn; > int i; > @@ -210,10 +214,12 @@ static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream) > if (stream == &oxfw->rx_stream) { > dir = AVC_GENERAL_PLUG_DIR_IN; > formats = oxfw->rx_stream_formats; > + lens = oxfw->rx_stream_format_lens; > conn = &oxfw->in_conn; > } else { > dir = AVC_GENERAL_PLUG_DIR_OUT; > formats = oxfw->tx_stream_formats; > + lens = oxfw->tx_stream_format_lens; > conn = &oxfw->out_conn; > } > > @@ -227,7 +233,7 @@ static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream) > if (formats[i] == NULL) > break; > > - err = snd_oxfw_stream_parse_format(formats[i], &fmt); > + err = snd_oxfw_stream_parse_format(formats[i], lens[i], &fmt); > if (err < 0) > return err; > > @@ -504,7 +510,7 @@ int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, > if (len < 3) > err = -EIO; > else > - err = snd_oxfw_stream_parse_format(format, formation); > + err = snd_oxfw_stream_parse_format(format, len, formation); > } > > kfree(format); > @@ -513,22 +519,26 @@ int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, > // command. Use the duplicated hard-coded format, instead. > unsigned int rate; > u8 *const *formats; > + const unsigned int *lens; > int i; > > err = avc_general_get_sig_fmt(oxfw->unit, &rate, dir, 0); > if (err < 0) > return err; > > - if (dir == AVC_GENERAL_PLUG_DIR_IN) > + if (dir == AVC_GENERAL_PLUG_DIR_IN) { > formats = oxfw->rx_stream_formats; > - else > + lens = oxfw->rx_stream_format_lens; > + } else { > formats = oxfw->tx_stream_formats; > + lens = oxfw->tx_stream_format_lens; > + } > > for (i = 0; (i < SND_OXFW_STREAM_FORMAT_ENTRIES); ++i) { > if (!formats[i]) > continue; > > - err = snd_oxfw_stream_parse_format(formats[i], formation); > + err = snd_oxfw_stream_parse_format(formats[i], lens[i], formation); > if (err < 0) > continue; > > @@ -549,6 +559,7 @@ int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, > * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005 > */ > int snd_oxfw_stream_parse_format(const u8 *format, > + unsigned int len, > struct snd_oxfw_stream_formation *formation) > { > unsigned int i, e, channels, type; > @@ -563,6 +574,10 @@ int snd_oxfw_stream_parse_format(const u8 *format, > if ((format[0] != 0x90) || (format[1] != 0x40)) > return -ENXIO; > > + /* The entries should be within the response. */ > + if (len < 5 || len < 5 + format[4] * 2) > + return -EIO; > + > /* check the sampling rate */ > for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) { > if (format[2] == avc_stream_rate_table[i]) > @@ -627,7 +642,7 @@ int snd_oxfw_stream_parse_format(const u8 *format, > static int > assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, > unsigned int pid, u8 *buf, unsigned int *len, > - u8 **formats) > + u8 **formats, unsigned int *lens) > { > struct snd_oxfw_stream_formation formation; > unsigned int i, eid; > @@ -664,7 +679,7 @@ assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, > > /* parse and set stream format */ > eid = 0; > - err = snd_oxfw_stream_parse_format(buf, &formation); > + err = snd_oxfw_stream_parse_format(buf, *len, &formation); > if (err < 0) > goto end; > > @@ -674,6 +689,7 @@ assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, > err = -ENOMEM; > goto end; > } > + lens[eid] = *len; > > /* apply the format for each available sampling rate */ > for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) { > @@ -693,6 +709,7 @@ assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, > err = -ENOMEM; > goto end; > } > + lens[eid] = *len; > formats[eid][2] = avc_stream_rate_table[i]; > } > > @@ -707,6 +724,7 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, > unsigned short pid) > { > u8 *buf, **formats; > + unsigned int *lens; > unsigned int len, eid = 0; > struct snd_oxfw_stream_formation dummy; > int err; > @@ -715,10 +733,13 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, > if (buf == NULL) > return -ENOMEM; > > - if (dir == AVC_GENERAL_PLUG_DIR_OUT) > + if (dir == AVC_GENERAL_PLUG_DIR_OUT) { > formats = oxfw->tx_stream_formats; > - else > + lens = oxfw->tx_stream_format_lens; > + } else { > formats = oxfw->rx_stream_formats; > + lens = oxfw->rx_stream_format_lens; > + } > > /* get first entry */ > len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; > @@ -727,7 +748,7 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, > /* LIST subfunction is not implemented */ > len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; > err = assume_stream_formats(oxfw, dir, pid, buf, &len, > - formats); > + formats, lens); > goto end; > } else if (err < 0) { > dev_err(&oxfw->unit->device, > @@ -746,7 +767,7 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, > } > > /* parse and set stream format */ > - err = snd_oxfw_stream_parse_format(buf, &dummy); > + err = snd_oxfw_stream_parse_format(buf, len, &dummy); > if (err < 0) > break; > > @@ -756,6 +777,7 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, > err = -ENOMEM; > break; > } > + lens[eid] = len; > > /* get next entry */ > len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; > @@ -814,6 +836,7 @@ int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) > if (format == NULL) > continue; > err = snd_oxfw_stream_parse_format(format, > + oxfw->tx_stream_format_lens[i], > &formation); > if (err < 0) > continue; > @@ -842,6 +865,7 @@ int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) > if (format == NULL) > continue; > err = snd_oxfw_stream_parse_format(format, > + oxfw->rx_stream_format_lens[i], > &formation); > if (err < 0) > continue; > diff --git a/sound/firewire/oxfw/oxfw.h b/sound/firewire/oxfw/oxfw.h > index aeb83e1595b7..439182c50c8a 100644 > --- a/sound/firewire/oxfw/oxfw.h > +++ b/sound/firewire/oxfw/oxfw.h > @@ -72,6 +72,8 @@ struct snd_oxfw { > bool has_input; > u8 *tx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES]; > u8 *rx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES]; > + unsigned int tx_stream_format_lens[SND_OXFW_STREAM_FORMAT_ENTRIES]; > + unsigned int rx_stream_format_lens[SND_OXFW_STREAM_FORMAT_ENTRIES]; > bool assumed; > struct cmp_connection out_conn; > struct cmp_connection in_conn; > @@ -141,6 +143,7 @@ struct snd_oxfw_stream_formation { > unsigned int midi; > }; > int snd_oxfw_stream_parse_format(const u8 *format, > + unsigned int len, > struct snd_oxfw_stream_formation *formation); > int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, > enum avc_general_plug_dir dir, > -- > 2.34.1 > ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-01 13:43 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-30 10:49 [PATCH v2 0/3] ALSA: bebob/oxfw: bound AV/C response walks to the response length Maoyi Xie 2026-11-30 10:49 ` [PATCH v2 1/3] ALSA: bebob: bound the channel position walk " Maoyi Xie 2026-11-30 10:49 ` [PATCH v2 2/3] ALSA: bebob: bound the stream format entries " Maoyi Xie 2026-11-30 10:49 ` [PATCH v2 3/3] ALSA: oxfw: " Maoyi Xie 2026-12-01 13:43 ` Takashi Sakamoto
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox