[PATCH v2 2/2] ASoC: mediatek: mtk-btcvsd: bound bt_sram_addr[] writes in mtk_btcvsd_write_to_bt()

From: Ilia Snezhkov via B4 Relay

Date: Wed Nov 25 2026 - 19:53:25 EST


From: Ilia Snezhkov <iasnezhkov@xxxxxxxxx>

bt->tx->buffer_info.bt_sram_addr[] has a fixed capacity of 20 entries,
but mtk_btcvsd_write_to_bt() unconditionally increments num_valid_addr
and writes bt_sram_addr[next_idx] whenever a new SRAM offset is seen.

If more than 20 distinct SRAM offsets are ever observed (for instance
during BT subsystem resets or corrupted register reads), this writes
out of bounds past bt_sram_addr[19] into adjacent fields of
struct mtk_btcvsd_snd_hw_info and surrounding memory.

Check next_idx against ARRAY_SIZE(bt->tx->buffer_info.bt_sram_addr)
under tx_lock before storing the address and advancing num_valid_addr.

Found and drafted with an AI coding assistant while bringing up MT6789.

Fixes: 4bd8597dc36c ("ASoC: mediatek: add btcvsd driver")
Signed-off-by: Ilia Snezhkov <iasnezhkov@xxxxxxxxx>
---
sound/soc/mediatek/common/mtk-btcvsd.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/sound/soc/mediatek/common/mtk-btcvsd.c b/sound/soc/mediatek/common/mtk-btcvsd.c
index 752856a..362d608 100644
--- a/sound/soc/mediatek/common/mtk-btcvsd.c
+++ b/sound/soc/mediatek/common/mtk-btcvsd.c
@@ -466,15 +466,22 @@ static int mtk_btcvsd_write_to_bt(struct mtk_btcvsd_snd *bt,
}
if (new_ap_addr_tx) {
unsigned int next_idx;
+ bool stored = false;

scoped_guard(spinlock_irqsave, &bt->tx_lock) {
- bt->tx->buffer_info.num_valid_addr++;
- next_idx = bt->tx->buffer_info.num_valid_addr - 1;
- bt->tx->buffer_info.bt_sram_addr[next_idx] = ap_addr_tx;
+ next_idx = bt->tx->buffer_info.num_valid_addr;
+ if (next_idx < ARRAY_SIZE(bt->tx->buffer_info.bt_sram_addr)) {
+ bt->tx->buffer_info.bt_sram_addr[next_idx] = ap_addr_tx;
+ bt->tx->buffer_info.num_valid_addr = next_idx + 1;
+ stored = true;
+ }
}
- dev_info(bt->dev, "%s(), new ap_addr_tx = 0x%lx, num_valid_addr %d\n",
- __func__, ap_addr_tx,
- bt->tx->buffer_info.num_valid_addr);
+ if (stored)
+ dev_info(bt->dev, "%s(), new ap_addr_tx = 0x%lx, num_valid_addr %u\n",
+ __func__, ap_addr_tx, next_idx + 1);
+ else
+ dev_warn_once(bt->dev, "%s(), bt_sram_addr table full\n",
+ __func__);
}

if (bt->tx->mute)
--
2.54.0 (Apple Git-157)