[PATCH v2 06/14] ASoC: qcom: common: validate cpu dai id during parsing

From: Srinivas Kandagatla

Date: Mon Feb 23 2026 - 13:10:16 EST


lpass ports numbers have been added but the afe/apm driver never got updated
with new max port value that it uses to store dai specific data. There
are more than one places these values are cached and always become out
of sync.

This will result in array out of bounds and weird driver behaviour.

To catch such issues, first add a single place where we can define max
port and second add a check in common parsing code which can error
out before corrupting the memory with out of bounds array access.

This should help both avoid and catch these type of mistakes in future.

Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@xxxxxxxxxxxxxxxx>
---
sound/soc/qcom/common.c | 6 ++++++
sound/soc/qcom/common.h | 3 +++
sound/soc/qcom/lpass.h | 3 ++-
sound/soc/qcom/qdsp6/q6afe.h | 3 ++-
sound/soc/qcom/qdsp6/q6apm.h | 3 ++-
5 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/sound/soc/qcom/common.c b/sound/soc/qcom/common.c
index 7ee60a58a336..2f5d3ab1a353 100644
--- a/sound/soc/qcom/common.c
+++ b/sound/soc/qcom/common.c
@@ -120,6 +120,12 @@ int qcom_snd_parse_of(struct snd_soc_card *card)

link->id = args.args[0];

+ if (link->id > LPASS_MAX_PORT) {
+ dev_err(dev, "%s: Invalid cpu dai id %d \n", link->name, link->id);
+ ret = -EINVAL;
+ goto err;
+ }
+
if (platform) {
link->platforms->of_node = of_parse_phandle(platform,
"sound-dai",
diff --git a/sound/soc/qcom/common.h b/sound/soc/qcom/common.h
index 1b8d3f90bffa..ab8061049375 100644
--- a/sound/soc/qcom/common.h
+++ b/sound/soc/qcom/common.h
@@ -4,8 +4,11 @@
#ifndef __QCOM_SND_COMMON_H__
#define __QCOM_SND_COMMON_H__

+#include <dt-bindings/sound/qcom,q6afe.h>
#include <sound/soc.h>

+#define LPASS_MAX_PORT (USB_RX + 1)
+
int qcom_snd_parse_of(struct snd_soc_card *card);
int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd,
struct snd_soc_jack *jack, bool *jack_setup);
diff --git a/sound/soc/qcom/lpass.h b/sound/soc/qcom/lpass.h
index de3ec6f594c1..68b71039b981 100644
--- a/sound/soc/qcom/lpass.h
+++ b/sound/soc/qcom/lpass.h
@@ -14,10 +14,11 @@
#include <linux/regmap.h>
#include <dt-bindings/sound/qcom,lpass.h>
#include <dt-bindings/sound/qcom,q6afe.h>
+#include "common.h"
#include "lpass-hdmi.h"

#define LPASS_AHBIX_CLOCK_FREQUENCY 131072000
-#define LPASS_MAX_PORTS (DISPLAY_PORT_RX_7 + 1)
+#define LPASS_MAX_PORTS (LPASS_MAX_PORT)
#define LPASS_MAX_MI2S_PORTS (8)
#define LPASS_MAX_DMA_CHANNELS (8)
#define LPASS_MAX_HDMI_DMA_CHANNELS (4)
diff --git a/sound/soc/qcom/qdsp6/q6afe.h b/sound/soc/qcom/qdsp6/q6afe.h
index a29abe4ce436..0b8c3ec1315c 100644
--- a/sound/soc/qcom/qdsp6/q6afe.h
+++ b/sound/soc/qcom/qdsp6/q6afe.h
@@ -2,8 +2,9 @@

#ifndef __Q6AFE_H__
#define __Q6AFE_H__
+#include "../common.h"

-#define AFE_PORT_MAX 137
+#define AFE_PORT_MAX (LPASS_MAX_PORT)

#define MSM_AFE_PORT_TYPE_RX 0
#define MSM_AFE_PORT_TYPE_TX 1
diff --git a/sound/soc/qcom/qdsp6/q6apm.h b/sound/soc/qcom/qdsp6/q6apm.h
index 7ce08b401e31..5cf538397841 100644
--- a/sound/soc/qcom/qdsp6/q6apm.h
+++ b/sound/soc/qcom/qdsp6/q6apm.h
@@ -14,9 +14,10 @@
#include <linux/of_platform.h>
#include <linux/jiffies.h>
#include <linux/soc/qcom/apr.h>
+#include "../common.h"
#include "audioreach.h"

-#define APM_PORT_MAX 127
+#define APM_PORT_MAX LPASS_MAX_PORT
#define APM_PORT_MAX_AUDIO_CHAN_CNT 8
#define PCM_CHANNEL_NULL 0
#define PCM_CHANNEL_FL 1 /* Front left channel. */
--
2.47.3