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 Cc: stable@vger.kernel.org Signed-off-by: Maoyi Xie --- 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