Skip to contentSkip to conversation

ASoC: SOF: Prevent NULL dereference in sof_pcm_dai_link_fixup()

PATCH In review Dan Carpenter 5 participants 7 messages Mar 18, 2022 – Mar 20, 2022

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

PATCH 1/2 ASoC: SOF: Prevent NULL dereference in sof_pcm_dai_link_fixup()

The "dia" pointer can be NULL, so handle that condition first before storing "dia->private".

Fixes 839e484f9e17 ("ASoC: SOF: make struct snd_sof_dai IPC agnostic")
---
 sound/soc/sof/pcm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
sound/soc/sof/pcm.c +2 -1
index 1661b0bc6f12..71f5bce0c4c7 100644
@@ -702,7 +702,7 @@ int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_pa
 	struct snd_sof_dai *dai =
 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
-	struct sof_dai_private_data *private = dai->private;
+	struct sof_dai_private_data *private;
 	struct snd_soc_dpcm *dpcm;
 
 	/* no topology exists for this BE, try a common configuration */
@@ -727,6 +727,7 @@ int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_pa
 	/* read format from topology */
 	snd_mask_none(fmt);
 
+	private = dai->private;
 	switch (private->comp_dai->config.frame_fmt) {
 	case SOF_IPC_FRAME_S16_LE:
 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/2 ASoC: SOF: check for NULL before dereferencing

This code dereferences "dai" before checking whether it can be NULL.

Fixes 839e484f9e17 ("ASoC: SOF: make struct snd_sof_dai IPC agnostic")
---
 sound/soc/sof/sof-audio.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
sound/soc/sof/sof-audio.c +10 -4
index 15c36a51f89f..88ddd1e2476d 100644
@@ -626,10 +626,13 @@ int sof_set_up_pipelines(struct snd_sof_dev *sdev, bool verify)
 		/* update DAI config. The IPC will be sent in sof_widget_setup() */
 		if (WIDGET_IS_DAI(swidget->id)) {
 			struct snd_sof_dai *dai = swidget->private;
-			struct sof_dai_private_data *private = dai->private;
+			struct sof_dai_private_data *private;
 			struct sof_ipc_dai_config *config;
 
-			if (!dai || !private || !private->dai_config)
+			if (!dai)
+				continue;
+			private = dai->private;
+			if (!private || !private->dai_config)
 				continue;
 
 			config = private->dai_config;
@@ -918,10 +921,13 @@ static int sof_dai_get_clk(struct snd_soc_pcm_runtime *rtd, int clk_type)
 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
 	struct snd_sof_dai *dai =
 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
-	struct sof_dai_private_data *private = dai->private;
+	struct sof_dai_private_data *private;
 
 	/* use the tplg configured mclk if existed */
-	if (!dai || !private || !private->dai_config)
+	if (!dai)
+		return 0;
+	private = dai->private;
+	if (!private || !private->dai_config)
 		return 0;
 
 	switch (private->dai_config->type) {
Signature · 1 line
2.20.1
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] ASoC: SOF: check for NULL before dereferencing

Quoted 4 lines · Dan Carpenter

This code dereferences "dai" before checking whether it can be NULL.

Fixes 839e484f9e17 ("ASoC: SOF: make struct snd_sof_dai IPC agnostic")

Same thing, this code will be removed in follow-up patches and we missed the intermediate error. Thanks for the patch.

Quoted 40 lines
---
  sound/soc/sof/sof-audio.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
sound/soc/sof/sof-audio.c +10 -4
index 15c36a51f89f..88ddd1e2476d 100644
@@ -626,10 +626,13 @@ int sof_set_up_pipelines(struct snd_sof_dev *sdev, bool verify)
  		/* update DAI config. The IPC will be sent in sof_widget_setup() */
  		if (WIDGET_IS_DAI(swidget->id)) {
  			struct snd_sof_dai *dai = swidget->private;
-			struct sof_dai_private_data *private = dai->private;
+			struct sof_dai_private_data *private;
  			struct sof_ipc_dai_config *config;
 
-			if (!dai || !private || !private->dai_config)
+			if (!dai)
+				continue;
+			private = dai->private;
+			if (!private || !private->dai_config)
  				continue;
 
  			config = private->dai_config;
@@ -918,10 +921,13 @@ static int sof_dai_get_clk(struct snd_soc_pcm_runtime *rtd, int clk_type)
  		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
  	struct snd_sof_dai *dai =
  		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
-	struct sof_dai_private_data *private = dai->private;
+	struct sof_dai_private_data *private;
 
  	/* use the tplg configured mclk if existed */
-	if (!dai || !private || !private->dai_config)
+	if (!dai)
+		return 0;
+	private = dai->private;
+	if (!private || !private->dai_config)
  		return 0;
 
  	switch (private->dai_config->type) {
Acked-by on v1 1/2
#3 lore raw
In reply to Dan Carpenter
Quoted 5 lines · Dan Carpenter

The "dia" pointer can be NULL, so handle that condition first before storing "dia->private".

Fixes 839e484f9e17 ("ASoC: SOF: make struct snd_sof_dai IPC agnostic")

Thanks for the patch.

This part will be removed in follow-up patches, likely the reason why this problem was missed.

Quoted 25 lines
---
  sound/soc/sof/pcm.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
sound/soc/sof/pcm.c +2 -1
index 1661b0bc6f12..71f5bce0c4c7 100644
@@ -702,7 +702,7 @@ int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_pa
  	struct snd_sof_dai *dai =
  		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
  	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
-	struct sof_dai_private_data *private = dai->private;
+	struct sof_dai_private_data *private;
  	struct snd_soc_dpcm *dpcm;
 
  	/* no topology exists for this BE, try a common configuration */
@@ -727,6 +727,7 @@ int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_pa
  	/* read format from topology */
  	snd_mask_none(fmt);
 
+	private = dai->private;
  	switch (private->comp_dai->config.frame_fmt) {
  	case SOF_IPC_FRAME_S16_LE:
  		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
#4 lore raw
Quoted 10 lines · Pierre-Louis Bossart
Quoted 7 lines · Dan Carpenter

The "dia" pointer can be NULL, so handle that condition first before
storing "dia->private".

Fixes 839e484f9e17 ("ASoC: SOF: make struct snd_sof_dai IPC

agnostic")

Quoted 5 lines

Thanks for the patch.

This part will be removed in follow-up patches, likely the reason why
this problem was missed.

Hi Dan/Pierre,

Both these problems are address in the series I posted yesterday. Particularly patches 16 and 18.

Thanks,
Ranjani

Quoted 33 lines
Quoted 30 lines
---
  sound/soc/sof/pcm.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
sound/soc/sof/pcm.c +0 -0
index 1661b0bc6f12..71f5bce0c4c7 100644
@@ -702,7 +702,7 @@ int sof_pcm_dai_link_fixup(struct

snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_pa
struct snd_sof_dai *dai =
snd_sof_find_dai(component, (char *)rtd->dai_link-

Quoted 1 line

name);

struct snd_sof_dev *sdev =
snd_soc_component_get_drvdata(component);

sound/soc/sof/pcm.c +1 -1
-	struct sof_dai_private_data *private = dai->private;
+	struct sof_dai_private_data *private;
  	struct snd_soc_dpcm *dpcm;
 
  	/* no topology exists for this BE, try a common configuration

*/

sound/soc/sof/pcm.c +0 -0
@@ -727,6 +727,7 @@ int sof_pcm_dai_link_fixup(struct

snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_pa
/* read format from topology */
snd_mask_none(fmt);

sound/soc/sof/pcm.c +1 -0
+	private = dai->private;
  	switch (private->comp_dai->config.frame_fmt) {
  	case SOF_IPC_FRAME_S16_LE:
  		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
maintainer
#5 lore raw
Quoted 8 lines · Ranjani Sridharan
Quoted 3 lines · Pierre-Louis Bossart

This part will be removed in follow-up patches, likely the reason why
this problem was missed.

Both these problems are address in the series I posted yesterday. Particularly patches 16 and 18.

OK, I've got that patch series queued already so I'll skip these for now.

#6 lore raw
In reply to Dan Carpenter
Quoted 3 lines · Dan Carpenter

The "dia" pointer can be NULL, so handle that condition first before storing "dia->private".

Typo in commit message, it is DAI in code, not dia.

Quoted 38 lines
Fixes 839e484f9e17 ("ASoC: SOF: make struct snd_sof_dai IPC agnostic")
---
 sound/soc/sof/pcm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
sound/soc/sof/pcm.c +1 -1
index 1661b0bc6f12..71f5bce0c4c7 100644
@@ -702,7 +702,7 @@ int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime
 *rtd, struct snd_pcm_hw_pa
        struct snd_sof_dai *dai =
                snd_sof_find_dai(component, (char *)rtd->dai_link->name);
        struct snd_sof_dev *sdev =
 snd_soc_component_get_drvdata(component);
-       struct sof_dai_private_data *private = dai->private;
+       struct sof_dai_private_data *private;
        struct snd_soc_dpcm *dpcm;
        /* no topology exists for this BE, try a common configuration */
sound/soc/sof/pcm.c +1 -0
@@ -727,6 +727,7 @@ int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime
 *rtd, struct snd_pcm_hw_pa
        /* read format from topology */
        snd_mask_none(fmt);
 
+       private = dai->private;
        switch (private->comp_dai->config.frame_fmt) {
        case SOF_IPC_FRAME_S16_LE:
                snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);

--
2.20.1

_______________________________________________
Sound-open-firmware mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/sound-open-firmware