Skip to contentSkip to conversation

ASoC: xilinx: formatter_pcm: fix stream_data leak on open error

PATCH Superseded Rosen Penev 2 participants 2 messages Aug 7 – Aug 11

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: xilinx: formatter_pcm: fix stream_data leak on open error

In xlnx_formatter_pcm_open(), stream_data is allocated and adata->play_stream or adata->capture_stream is assigned early. If a later step, such as snd_pcm_hw_constraint_step() or snd_pcm_hw_constraint_integer(), fails, the function returns the error immediately. ALSA does not call the close callback when open fails, so stream_data is leaked and the stream pointer is left dangling, pointing to a substream that ALSA frees. A later interrupt would then call snd_pcm_period_elapsed() on the freed substream.

Free stream_data and clear the stream pointer on the error paths.

Fixes 6f6c3c36f091 ("ASoC: xlnx: add pcm formatter platform driver")
Assisted-by opencode:deepseek-v4-flash-free
---
 sound/soc/xilinx/xlnx_formatter_pcm.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
sound/soc/xilinx/xlnx_formatter_pcm.c +11 -3
index 7eba3a0205f1..4f4c1e650aaf 100644
@@ -385,7 +385,7 @@ static int xlnx_formatter_pcm_open(struct snd_soc_component *component,
 	if (err) {
 		dev_err(component->dev,
 			"Unable to set constraint on period bytes\n");
-		return err;
+		goto err;
 	}
 
 	/* Resize the buffer bytes as divisible by 64 */
@@ -395,7 +395,7 @@ static int xlnx_formatter_pcm_open(struct snd_soc_component *component,
 	if (err) {
 		dev_err(component->dev,
 			"Unable to set constraint on buffer bytes\n");
-		return err;
+		goto err;
 	}
 
 	/* Set periods as integer multiple */
@@ -404,7 +404,7 @@ static int xlnx_formatter_pcm_open(struct snd_soc_component *component,
 	if (err < 0) {
 		dev_err(component->dev,
 			"Unable to set constraint on periods to be integer\n");
-		return err;
+		goto err;
 	}
 
 	/* enable DMA IOC irq */
@@ -413,6 +413,14 @@ static int xlnx_formatter_pcm_open(struct snd_soc_component *component,
 	writel(val, stream_data->mmio + XLNX_AUD_CTRL);
 
 	return 0;
+
+err:
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		adata->play_stream = NULL;
+	else
+		adata->capture_stream = NULL;
+	kfree(stream_data);
+	return err;
 }
 
 static int xlnx_formatter_pcm_close(struct snd_soc_component *component,
Signature · 1 line
2.55.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 50 lines · Rosen Penev

In xlnx_formatter_pcm_open(), stream_data is allocated and adata->play_stream or adata->capture_stream is assigned early. If a later step, such as snd_pcm_hw_constraint_step() or snd_pcm_hw_constraint_integer(), fails, the function returns the error immediately. ALSA does not call the close callback when open fails, so stream_data is leaked and the stream pointer is left dangling, pointing to a substream that ALSA frees. A later interrupt would then call snd_pcm_period_elapsed() on the freed substream.

Free stream_data and clear the stream pointer on the error paths.

Fixes 6f6c3c36f091 ("ASoC: xlnx: add pcm formatter platform driver")
Assisted-by opencode:deepseek-v4-flash-free
---
  sound/soc/xilinx/xlnx_formatter_pcm.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
sound/soc/xilinx/xlnx_formatter_pcm.c +3 -3
index 7eba3a0205f1..4f4c1e650aaf 100644
@@ -385,7 +385,7 @@ static int xlnx_formatter_pcm_open(struct snd_soc_component *component,
  	if (err) {
  		dev_err(component->dev,
  			"Unable to set constraint on period bytes\n");
-		return err;
+		goto err;
  	}
 
  	/* Resize the buffer bytes as divisible by 64 */
@@ -395,7 +395,7 @@ static int xlnx_formatter_pcm_open(struct snd_soc_component *component,
  	if (err) {
  		dev_err(component->dev,
  			"Unable to set constraint on buffer bytes\n");
-		return err;
+		goto err;
  	}
 
  	/* Set periods as integer multiple */
@@ -404,7 +404,7 @@ static int xlnx_formatter_pcm_open(struct snd_soc_component *component,
  	if (err < 0) {
  		dev_err(component->dev,
  			"Unable to set constraint on periods to be integer\n");
-		return err;
+		goto err;
  	}
 
  	/* enable DMA IOC irq */

sound/soc/xilinx/xlnx_formatter_pcm.c static int xlnx_formatter_pcm_open(struct snd_soc_component *component, · line 417

Quoted 5 lines
sound/soc/xilinx/xlnx_formatter_pcm.c +2 -0
@@ -413,6 +413,14 @@ static int xlnx_formatter_pcm_open(struct snd_soc_component *component,
  	writel(val, stream_data->mmio + XLNX_AUD_CTRL);
 
  	return 0;
+
+err:

err is also variable in this code that's why I suggest you to rename this label.

sound/soc/xilinx/xlnx_formatter_pcm.c static int xlnx_formatter_pcm_open(struct snd_soc_component *component, · line 426

Quoted 9 lines
sound/soc/xilinx/xlnx_formatter_pcm.c +6 -0
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		adata->play_stream = NULL;
+	else
+		adata->capture_stream = NULL;
+	kfree(stream_data);
+	return err;
  }
 
  static int xlnx_formatter_pcm_close(struct snd_soc_component *component,

With that fixed fell free to add

Thanks,
Michal