Skip to content

Commit 2f20203

Browse files
nfrapradobroonie
authored andcommitted
ASoC: mediatek: Check num_codecs is not zero to avoid panic during probe
Following commit 13f5826 ("ASoC: soc.h: don't create dummy Component via COMP_DUMMY()"), COMP_DUMMY() became an array with zero length, and only gets populated with the dummy struct after the card is registered. Since the sound card driver's probe happens before the card registration, accessing any of the members of a dummy component during probe will result in undefined behavior. This can be observed in the mt8188 and mt8195 machine sound drivers. By omitting a dai link subnode in the sound card's node in the Devicetree, the default uninitialized dummy codec is used, and when its dai_name pointer gets passed to strcmp() it results in a null pointer dereference and a kernel panic. In addition to that, set_card_codec_info() in the generic helpers file, mtk-soundcard-driver.c, will populate a dai link with a dummy codec when a dai link node is present in DT but with no codec property. The result is that at probe time, a dummy codec can either be uninitialized with num_codecs = 0, or be an initialized dummy codec, with num_codecs = 1 and dai_name = "snd-soc-dummy-dai". In order to accommodate for both situations, check that num_codecs is not zero before accessing the codecs' fields but still check for the codec's dai name against "snd-soc-dummy-dai" as needed. While at it, also drop the check that dai_name is not null in the mt8192 driver, introduced in commit 4d4e1b6 ("ASoC: mediatek: mt8192: Check existence of dai_name before dereferencing"), as it is actually redundant given the preceding num_codecs != 0 check. Fixes: 13f5826 ("ASoC: soc.h: don't create dummy Component via COMP_DUMMY()") Signed-off-by: Nícolas F. R. A. Prado <nfraprado@collabora.com> Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> Acked-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> Reviewed-by: Fei Shao <fshao@chromium.org> Acked-by: Trevor Wu <trevor.wu@mediatek.com> Link: https://patch.msgid.link/20241126-asoc-mtk-dummy-panic-v1-1-42d53e168d2e@collabora.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 4095cf8 commit 2f20203

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

sound/soc/mediatek/mt8188/mt8188-mt6359.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,10 +1277,12 @@ static int mt8188_mt6359_soc_card_probe(struct mtk_soc_card_data *soc_card_data,
12771277

12781278
for_each_card_prelinks(card, i, dai_link) {
12791279
if (strcmp(dai_link->name, "DPTX_BE") == 0) {
1280-
if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1280+
if (dai_link->num_codecs &&
1281+
strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
12811282
dai_link->init = mt8188_dptx_codec_init;
12821283
} else if (strcmp(dai_link->name, "ETDM3_OUT_BE") == 0) {
1283-
if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1284+
if (dai_link->num_codecs &&
1285+
strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
12841286
dai_link->init = mt8188_hdmi_codec_init;
12851287
} else if (strcmp(dai_link->name, "DL_SRC_BE") == 0 ||
12861288
strcmp(dai_link->name, "UL_SRC_BE") == 0) {
@@ -1292,6 +1294,9 @@ static int mt8188_mt6359_soc_card_probe(struct mtk_soc_card_data *soc_card_data,
12921294
strcmp(dai_link->name, "ETDM2_OUT_BE") == 0 ||
12931295
strcmp(dai_link->name, "ETDM1_IN_BE") == 0 ||
12941296
strcmp(dai_link->name, "ETDM2_IN_BE") == 0) {
1297+
if (!dai_link->num_codecs)
1298+
continue;
1299+
12951300
if (!strcmp(dai_link->codecs->dai_name, MAX98390_CODEC_DAI)) {
12961301
/*
12971302
* The TDM protocol settings with fixed 4 slots are defined in

sound/soc/mediatek/mt8192/mt8192-mt6359-rt1015-rt5682.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ static int mt8192_mt6359_legacy_probe(struct mtk_soc_card_data *soc_card_data)
10911091
dai_link->ignore = 0;
10921092
}
10931093

1094-
if (dai_link->num_codecs && dai_link->codecs[0].dai_name &&
1094+
if (dai_link->num_codecs &&
10951095
strcmp(dai_link->codecs[0].dai_name, RT1015_CODEC_DAI) == 0)
10961096
dai_link->ops = &mt8192_rt1015_i2s_ops;
10971097
}
@@ -1119,7 +1119,7 @@ static int mt8192_mt6359_soc_card_probe(struct mtk_soc_card_data *soc_card_data,
11191119
int i;
11201120

11211121
for_each_card_prelinks(card, i, dai_link)
1122-
if (dai_link->num_codecs && dai_link->codecs[0].dai_name &&
1122+
if (dai_link->num_codecs &&
11231123
strcmp(dai_link->codecs[0].dai_name, RT1015_CODEC_DAI) == 0)
11241124
dai_link->ops = &mt8192_rt1015_i2s_ops;
11251125
}

sound/soc/mediatek/mt8195/mt8195-mt6359.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,12 @@ static int mt8195_mt6359_soc_card_probe(struct mtk_soc_card_data *soc_card_data,
13781378

13791379
for_each_card_prelinks(card, i, dai_link) {
13801380
if (strcmp(dai_link->name, "DPTX_BE") == 0) {
1381-
if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1381+
if (dai_link->num_codecs &&
1382+
strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
13821383
dai_link->init = mt8195_dptx_codec_init;
13831384
} else if (strcmp(dai_link->name, "ETDM3_OUT_BE") == 0) {
1384-
if (strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
1385+
if (dai_link->num_codecs &&
1386+
strcmp(dai_link->codecs->dai_name, "snd-soc-dummy-dai"))
13851387
dai_link->init = mt8195_hdmi_codec_init;
13861388
} else if (strcmp(dai_link->name, "DL_SRC_BE") == 0 ||
13871389
strcmp(dai_link->name, "UL_SRC1_BE") == 0 ||
@@ -1394,6 +1396,9 @@ static int mt8195_mt6359_soc_card_probe(struct mtk_soc_card_data *soc_card_data,
13941396
strcmp(dai_link->name, "ETDM2_OUT_BE") == 0 ||
13951397
strcmp(dai_link->name, "ETDM1_IN_BE") == 0 ||
13961398
strcmp(dai_link->name, "ETDM2_IN_BE") == 0) {
1399+
if (!dai_link->num_codecs)
1400+
continue;
1401+
13971402
if (!strcmp(dai_link->codecs->dai_name, MAX98390_CODEC_DAI)) {
13981403
if (!(codec_init & MAX98390_CODEC_INIT)) {
13991404
dai_link->init = mt8195_max98390_init;

0 commit comments

Comments
 (0)