Skip to contentSkip to conversation

ASoC: mediatek: common: bound the memif lookup in mtk_afe_pcm_pointer()

PATCH New Ilia Snezhkov 1 participant 1 message Nov 26

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

PATCH 1/1 ASoC: mediatek: common: bound the memif lookup in mtk_afe_pcm_pointer()

mtk_afe_pcm_pointer() indexes afe->memif[] with the id of the front-end CPU DAI. The hostless front ends registered by mt6797, mt8183 and mt8186 (e.g. MT8186_DAI_HOSTLESS_LPBK, MT8183_DAI_HOSTLESS_SPEECH) have ids at or above the memif count, so their PCMs read past the end of the afe->memif array and then dereference whatever memif->data that yields.

The pointer callback is reached whenever the PCM core updates the hardware pointer of such a running stream, for instance through SNDRV_PCM_IOCTL_HWSYNC, a status query or a read. On an MT6789 phone with the same common code, capturing from the Hostless_Speech PCM after starting it oopsed in mtk_afe_pcm_pointer() on a NULL memif->data.

A hostless front end has no memory interface and no position to report:
return 0 for a CPU DAI id outside the memif table.

Found on hardware and drafted with an AI coding assistant.

Fixes 283b612429a2 ("ASoC: mediatek: implement mediatek common structure")

---

Notes:
Observed with the downstream (MediaTek BSP-derived) copy of this file on an MT6789 phone running android17-6.18 (6.18.32): tinycap on the hostless speech PCM after start -> mtk_afe_pcm_pointer+0x60, NULL + 0x10. Our downstream fix also returns 0 for a memif without data; upstream every memif in range has data, so only the range check is needed here. Not reproduced on mt8183/mt8186 hardware; not compile-tested in this form.

 sound/soc/mediatek/common/mtk-afe-platform-driver.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
sound/soc/mediatek/common/mtk-afe-platform-driver.c +7 -2
index 7dd93d4..7877d80 100644
@@ -82,8 +82,8 @@ snd_pcm_uframes_t mtk_afe_pcm_pointer(struct snd_soc_component *component,
 {
 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
-	struct mtk_base_afe_memif *memif = &afe->memif[snd_soc_rtd_to_cpu(rtd, 0)->id];
-	const struct mtk_base_memif_data *memif_data = memif->data;
+	int id = snd_soc_rtd_to_cpu(rtd, 0)->id;
+	const struct mtk_base_memif_data *memif_data;
 	struct regmap *regmap = afe->regmap;
 	struct device *dev = afe->dev;
 	unsigned int hw_ptr_lower32 = 0, hw_ptr_upper32 = 0;
@@ -92,6 +92,11 @@ snd_pcm_uframes_t mtk_afe_pcm_pointer(struct snd_soc_component *component,
 	int ret;
 	unsigned long long pcm_ptr_bytes = 0;
 
+	if (id < 0 || id >= afe->memif_size)
+		return 0;
+
+	memif_data = afe->memif[id].data;
+
 	ret = regmap_read(regmap, memif_data->reg_ofs_cur, &hw_ptr_lower32);
 	if (ret) {
 		dev_err(dev, "%s hw_ptr_lower32 err\n", __func__);
Signature · 1 line
2.54.0 (Apple Git-157)