mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] ASoC: mediatek: mtk-btcvsd: fix 0xdeadfeed check and bt_sram_addr bounds
@ 2026-11-26  0:53 Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH v2 1/2] ASoC: mediatek: mtk-btcvsd: check bt_reg_pkt_w for 0xdeadfeed in btcvsd_tx_clean_buffer() Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH v2 2/2] ASoC: mediatek: mtk-btcvsd: bound bt_sram_addr[] writes in mtk_btcvsd_write_to_bt() Ilia Snezhkov via B4 Relay
  0 siblings, 2 replies; 3+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: broonie, lgirdwood
  Cc: perex, tiwai, matthias.bgg, angelogioacchino.delregno,
	linux-sound, linux-mediatek, linux-arm-kernel, linux-kernel

This two-patch v2 series addresses two safety issues in
sound/soc/mediatek/common/mtk-btcvsd.c:

1. Check the live *bt->bt_reg_pkt_w register for 0xdeadfeed under tx_lock
   in btcvsd_tx_clean_buffer() (after skipping the empty num_valid_addr
   case), matching mtk_btcvsd_read_from_bt() and mtk_btcvsd_write_to_bt().
2. Bound bt_sram_addr[] writes in mtk_btcvsd_write_to_bt() against
   ARRAY_SIZE(bt->tx->buffer_info.bt_sram_addr) to prevent an
   out-of-bounds write past the 20-entry array.

Changes in v2:
- Replaced the ineffective check on cached virtual addresses
  bt_sram_addr[i] with a check on the live *bt->bt_reg_pkt_w register
  (returning 0 early when num_valid_addr == 0).
- Dropped the unrelated Fixes: d067b3378a78 tag; kept only
  Fixes: 4bd8597dc36c.
- Added patch 2/2 fixing the unbounded bt_sram_addr[] write spotted
  while auditing mtk_btcvsd_write_to_bt().
- Link to v1: https://lore.kernel.org/r/20261125234830.94895-1-iasnezhkov@gmail.com

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

Ilia Snezhkov (2):
  ASoC: mediatek: mtk-btcvsd: check bt_reg_pkt_w for 0xdeadfeed in
    btcvsd_tx_clean_buffer()
  ASoC: mediatek: mtk-btcvsd: bound bt_sram_addr[] writes in
    mtk_btcvsd_write_to_bt()

 sound/soc/mediatek/common/mtk-btcvsd.c | 28 ++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

-- 
2.54.0 (Apple Git-157)




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

* [PATCH v2 1/2] ASoC: mediatek: mtk-btcvsd: check bt_reg_pkt_w for 0xdeadfeed in btcvsd_tx_clean_buffer()
  2026-11-26  0:53 [PATCH v2 0/2] ASoC: mediatek: mtk-btcvsd: fix 0xdeadfeed check and bt_sram_addr bounds Ilia Snezhkov via B4 Relay
@ 2026-11-26  0:53 ` Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH v2 2/2] ASoC: mediatek: mtk-btcvsd: bound bt_sram_addr[] writes in mtk_btcvsd_write_to_bt() Ilia Snezhkov via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: broonie, lgirdwood
  Cc: perex, tiwai, matthias.bgg, angelogioacchino.delregno,
	linux-sound, linux-mediatek, linux-arm-kernel, linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

mtk_btcvsd_read_from_bt(), mtk_btcvsd_write_to_bt(), and the loopback
path in mtk_btcvsd_snd_irq_handler() all read *bt->bt_reg_pkt_w and
bail out on the 0xdeadfeed sentinel returned when BT connsys SRAM is
unpowered or resetting.

btcvsd_tx_clean_buffer(), called from mtk_pcm_btcvsd_hw_free() and the
TX mute path, writes mute frames directly to previously cached
bt_sram_addr[] addresses without checking whether BT connsys SRAM is
still powered. If BT entered sleep or reset after those addresses were
recorded, writing to the cached SRAM addresses accesses unpowered BT
SRAM under tx_lock.

Return 0 early when num_valid_addr is zero so closing an unused stream
does not touch BT registers, and otherwise verify *bt->bt_reg_pkt_w !=
0xdeadfeed under tx_lock before writing mute frames.

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@gmail.com>
---
 sound/soc/mediatek/common/mtk-btcvsd.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/sound/soc/mediatek/common/mtk-btcvsd.c b/sound/soc/mediatek/common/mtk-btcvsd.c
index 19329dd..752856a 100644
--- a/sound/soc/mediatek/common/mtk-btcvsd.c
+++ b/sound/soc/mediatek/common/mtk-btcvsd.c
@@ -335,6 +335,15 @@ static int btcvsd_tx_clean_buffer(struct mtk_btcvsd_snd *bt)
 	dev_info(bt->dev, "%s(), band %d, num_valid_addr %u\n",
 		 __func__, band, num_valid_addr);
 
+	if (!num_valid_addr)
+		return 0;
+
+	if (*bt->bt_reg_pkt_w == 0xdeadfeed) {
+		dev_warn(bt->dev, "%s(), connsys_addr_tx == 0xdeadfeed\n",
+			 __func__);
+		return -EIO;
+	}
+
 	for (i = 0; i < num_valid_addr; i++) {
 		void *dst;
 
-- 
2.54.0 (Apple Git-157)




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

* [PATCH v2 2/2] ASoC: mediatek: mtk-btcvsd: bound bt_sram_addr[] writes in mtk_btcvsd_write_to_bt()
  2026-11-26  0:53 [PATCH v2 0/2] ASoC: mediatek: mtk-btcvsd: fix 0xdeadfeed check and bt_sram_addr bounds Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH v2 1/2] ASoC: mediatek: mtk-btcvsd: check bt_reg_pkt_w for 0xdeadfeed in btcvsd_tx_clean_buffer() Ilia Snezhkov via B4 Relay
@ 2026-11-26  0:53 ` Ilia Snezhkov via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: broonie, lgirdwood
  Cc: perex, tiwai, matthias.bgg, angelogioacchino.delregno,
	linux-sound, linux-mediatek, linux-arm-kernel, linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

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@gmail.com>
---
 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)




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

end of thread, other threads:[~2026-11-26  0:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  0:53 [PATCH v2 0/2] ASoC: mediatek: mtk-btcvsd: fix 0xdeadfeed check and bt_sram_addr bounds Ilia Snezhkov via B4 Relay
2026-11-26  0:53 ` [PATCH v2 1/2] ASoC: mediatek: mtk-btcvsd: check bt_reg_pkt_w for 0xdeadfeed in btcvsd_tx_clean_buffer() Ilia Snezhkov via B4 Relay
2026-11-26  0:53 ` [PATCH v2 2/2] ASoC: mediatek: mtk-btcvsd: bound bt_sram_addr[] writes in mtk_btcvsd_write_to_bt() Ilia Snezhkov via B4 Relay

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

all inboxes | Powered by JetHome®