[PATCH] ASoC: rockchip: max98090: Fix reference leak in snd_rk_mc_probe

Kery Qi posted 1 patch 1 month ago
sound/soc/rockchip/rockchip_max98090.c | 27 +++++++++++++++++++++++---
1 file changed, 24 insertions(+), 3 deletions(-)
[PATCH] ASoC: rockchip: max98090: Fix reference leak in snd_rk_mc_probe
Posted by Kery Qi 1 month ago
The snd_rk_mc_probe() function calls of_parse_phandle() to acquire
device nodes for "rockchip,i2s-controller", "rockchip,audio-codec",
and "rockchip,hdmi-codec". These functions return device nodes with
incremented reference counts.

However, in several error paths (e.g., when rk_parse_headset_from_of(),
snd_soc_of_parse_card_name(), or devm_snd_soc_register_card() fails),
the function returns directly without releasing the acquired nodes,
leading to reference leaks.

This patch adds the missing of_node_put() calls in the error handling
paths. It also cleans up the of_node pointers in the dai_link structure
to prevent potential use-after-free issues.

Signed-off-by: Kery Qi <qikeyu2017@gmail.com>
---
 sound/soc/rockchip/rockchip_max98090.c | 27 +++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/sound/soc/rockchip/rockchip_max98090.c b/sound/soc/rockchip/rockchip_max98090.c
index 075d0990a126..cd9305a5af72 100644
--- a/sound/soc/rockchip/rockchip_max98090.c
+++ b/sound/soc/rockchip/rockchip_max98090.c
@@ -414,6 +414,8 @@ static int snd_rk_mc_probe(struct platform_device *pdev)
 		card->dai_link[0].platforms->of_node = np_cpu;
 	} else {
 		dev_err(dev, "At least one of codecs should be specified\n");
+		if (np_cpu)
+			of_node_put(np_cpu);
 		return -EINVAL;
 	}
 
@@ -423,7 +425,7 @@ static int snd_rk_mc_probe(struct platform_device *pdev)
 	if (np_audio) {
 		ret = rk_parse_headset_from_of(dev, np);
 		if (ret)
-			return ret;
+			goto err_put_nodes;
 	}
 
 	/* Parse card name. */
@@ -431,7 +433,7 @@ static int snd_rk_mc_probe(struct platform_device *pdev)
 	if (ret) {
 		dev_err(&pdev->dev,
 			"Soc parse card name failed %d\n", ret);
-		return ret;
+		goto err_put_nodes;
 	}
 
 	/* register the soc card */
@@ -439,10 +441,29 @@ static int snd_rk_mc_probe(struct platform_device *pdev)
 	if (ret) {
 		dev_err(&pdev->dev,
 			"Soc register card failed %d\n", ret);
-		return ret;
+		goto err_put_nodes;
 	}
 
 	return ret;
+
+err_put_nodes:
+	for (int i = 0; i < card->num_links; i++) {
+		if (card->dai_link[i].codecs->of_node)
+			card->dai_link[i].codecs->of_node = NULL;
+		if (card->dai_link[i].cpus->of_node)
+			card->dai_link[i].cpus->of_node = NULL;
+		if (card->dai_link[i].platforms->of_node)
+			card->dai_link[i].platforms->of_node = NULL;
+	}
+
+	if (np_audio)
+		of_node_put(np_audio);
+	if (np_hdmi)
+		of_node_put(np_hdmi);
+	if (np_cpu)
+		of_node_put(np_cpu);
+
+	return ret;
 }
 
 static const struct of_device_id rockchip_max98090_of_match[] = {
-- 
2.34.1