Skip to contentSkip to conversation

ASoC: mediatek: mtk-btcvsd: guard against 0xdeadfeed in btcvsd_tx_clean_buffer()

PATCH New Ilia Snezhkov 2 participants 2 messages Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: mediatek: mtk-btcvsd: guard against 0xdeadfeed in btcvsd_tx_clean_buffer()

mtk_btcvsd_read_from_bt() and mtk_btcvsd_write_to_bt() both reject the 0xdeadfeed sentinel returned by the BT connsys SRAM pointer register when BT SRAM is unpowered or resetting, and check the SRAM address before dereferencing it. btcvsd_tx_clean_buffer(), however, casts each stored bt_sram_addr[] entry straight to a pointer and writes mute frames through mtk_btcvsd_snd_data_transfer() without checking for 0xdeadfeed or NULL.

If an invalid or reset SRAM address is recorded or cleared concurrently around hw_free / mute transitions, btcvsd_tx_clean_buffer() dereferences an invalid address in atomic context under tx_lock.

Skip any bt_sram_addr[] slot holding 0xdeadfeed or 0 before transferring mute data.

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

Fixes 4bd8597dc36c ("ASoC: mediatek: add btcvsd driver")
Fixes d067b3378a78 ("ASoC: mediatek: mtk-btcvsd: Add checks for write and read of mtk_btcvsd_snd")
---
 sound/soc/mediatek/common/mtk-btcvsd.c | 4 ++++
 1 file changed, 4 insertions(+)
sound/soc/mediatek/common/mtk-btcvsd.c +4 -0
index 19329dd..1f52567 100644
@@ -341,6 +341,10 @@ static int btcvsd_tx_clean_buffer(struct mtk_btcvsd_snd *bt)
 		dev_info(bt->dev, "%s(), clean addr 0x%lx\n", __func__,
 			 bt->tx->buffer_info.bt_sram_addr[i]);
 
+		if (bt->tx->buffer_info.bt_sram_addr[i] == 0xdeadfeed ||
+		    !bt->tx->buffer_info.bt_sram_addr[i])
+			continue;
+
 		dst = (void *)bt->tx->buffer_info.bt_sram_addr[i];
 
 		mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT,
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ASoC: mediatek: ineffective 0xdeadfeed check in btcvsd_tx_clean_buffer() - [Low] ASoC: mediatek…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] ASoC: mediatek: ineffective 0xdeadfeed check in btcvsd_tx_clean_buffer()
- [Low] ASoC: mediatek: unrelated Fixes trailer d067b3378a78 in commit message
--

commit ed1dbfaac1e6a9adfd651ed2d3dd40c2aaf8a20b
Author: Ilia Snezhkov <[email protected]>

Quoted 14 lines

ASoC: mediatek: mtk-btcvsd: guard against 0xdeadfeed in btcvsd_tx_clean_buffer()

mtk_btcvsd_read_from_bt() and mtk_btcvsd_write_to_bt() both reject the 0xdeadfeed sentinel returned by the BT connsys SRAM pointer register when BT SRAM is unpowered or resetting, and check the SRAM address before dereferencing it. btcvsd_tx_clean_buffer(), however, casts each stored bt_sram_addr[] entry straight to a pointer and writes mute frames through mtk_btcvsd_snd_data_transfer() without checking for 0xdeadfeed or NULL.

If an invalid or reset SRAM address is recorded or cleared concurrently around hw_free / mute transitions, btcvsd_tx_clean_buffer() dereferences an invalid address in atomic context under tx_lock.

Quoted 8 lines

Skip any bt_sram_addr[] slot holding 0xdeadfeed or 0 before transferring mute data.

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

Fixes 4bd8597dc36c ("ASoC: mediatek: add btcvsd driver")
Fixes d067b3378a78 ("ASoC: mediatek: mtk-btcvsd: Add checks for write and read of mtk_btcvsd_snd")

[Severity: Low]
Should the second Fixes: tag for d067b3378a78 ("ASoC: mediatek: mtk-btcvsd:
Add checks for write and read of mtk_btcvsd_snd") be dropped? That commit only changed mtk_pcm_btcvsd_copy() to propagate return values from mtk_btcvsd_snd_write() and mtk_btcvsd_snd_read(), and did not touch btcvsd_tx_clean_buffer() or 0xdeadfeed handling.

Quoted 5 lines
sound/soc/mediatek/common/mtk-btcvsd.c +0 -0
index 19329dd7c0d75..1f525673c463b 100644

sound/soc/mediatek/common/mtk-btcvsd.c static int btcvsd_tx_clean_buffer(struct mtk_btcvsd_snd *bt) · line 350

Quoted 10 lines
sound/soc/mediatek/common/mtk-btcvsd.c +4 -0
@@ -341,6 +341,10 @@ static int btcvsd_tx_clean_buffer(struct mtk_btcvsd_snd *bt)
 		dev_info(bt->dev, "%s(), clean addr 0x%lx\n", __func__,
 			 bt->tx->buffer_info.bt_sram_addr[i]);
 
+		if (bt->tx->buffer_info.bt_sram_addr[i] == 0xdeadfeed ||
+		    !bt->tx->buffer_info.bt_sram_addr[i])
+			continue;
+
 		dst = (void *)bt->tx->buffer_info.bt_sram_addr[i];
 
 		mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT,

[Severity: Medium]
Can bt->tx->buffer_info.bt_sram_addr[i] ever equal 0xdeadfeed here?

mtk_btcvsd_write_to_bt() is the only place that populates bt_sram_addr[], and it checks the raw 32-bit register value connsys_addr_tx against 0xdeadfeed before storing the translated virtual address ap_addr_tx:

sound/soc/mediatek/common/mtk-btcvsd.c:mtk_btcvsd_write_to_bt() {
    ...
	connsys_addr_tx = *bt->bt_reg_pkt_w;
	ap_addr_tx = (unsigned long)bt->bt_sram_bank2_base +
		     (connsys_addr_tx & 0xFFFF);

	if (connsys_addr_tx == 0xdeadfeed) {
		/* bt return 0xdeadfeed if read register during bt sleep */
		dev_warn(bt->dev, "%s(), connsys_addr_tx == 0xdeadfeed\n",
			 __func__);
		return -EIO;
	}
    ...
		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;
		}
    ...
}

Because bt_sram_addr[] caches previously computed virtual addresses (bt_sram_bank2_base + (connsys_addr_tx & 0xFFFF)), an entry never holds 0xdeadfeed, and the cached entries do not change to 0xdeadfeed if BT connsys enters sleep or resets before mtk_pcm_btcvsd_hw_free() -> btcvsd_tx_clean_buffer() runs. As a result, btcvsd_tx_clean_buffer() will still write mute frames to BT SRAM while BT connsys is asleep unless it checks the live hardware register *bt->bt_reg_pkt_w == 0xdeadfeed.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1