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 --- 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