All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] soc: soc-core: fix a missing check on list iterator
@ 2022-03-27  8:20 ` Xiaomeng Tong
  0 siblings, 0 replies; 4+ messages in thread
From: Xiaomeng Tong @ 2022-03-27  8:20 UTC (permalink / raw)
  To: lgirdwood, broonie, perex, tiwai
  Cc: kuninori.morimoto.gx, alsa-devel, linux-kernel, Xiaomeng Tong, stable

The bug is here:
	*dai_name = dai->driver->name;

For for_each_component_dais, just like list_for_each_entry,
the list iterator 'dai' will point to a bogus position
containing HEAD if the list is empty or no element is found.
This case must be checked before any use of the iterator,
otherwise it will lead to a invalid memory access.

To fix the bug, use a new variable 'iter' as the list iterator,
while use the original variable 'dai' as a dedicated pointer
to point to the found element.

Cc: stable@vger.kernel.org
Fixes: 58bf4179000a3 ("ASoC: soc-core: remove dai_drv from snd_soc_component")
Signed-off-by: Xiaomeng Tong <xiam0nd.tong@gmail.com>
---
 sound/soc/soc-core.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c
index 434e61b46983..064fc0347868 100644
--- a/sound/soc/soc-core.c
+++ b/sound/soc/soc-core.c
@@ -3238,7 +3238,7 @@ int snd_soc_get_dai_name(const struct of_phandle_args *args,
 
 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
 		if (ret == -ENOTSUPP) {
-			struct snd_soc_dai *dai;
+			struct snd_soc_dai *dai = NULL, *iter;
 			int id = -1;
 
 			switch (args->args_count) {
@@ -3261,12 +3261,19 @@ int snd_soc_get_dai_name(const struct of_phandle_args *args,
 			ret = 0;
 
 			/* find target DAI */
-			for_each_component_dais(pos, dai) {
-				if (id == 0)
+			for_each_component_dais(pos, iter) {
+				if (id == 0) {
+					dai = iter;
 					break;
+				}
 				id--;
 			}
 
+			if (!dai) {
+				ret = -EINVAL;
+				continue;
+			}
+
 			*dai_name = dai->driver->name;
 			if (!*dai_name)
 				*dai_name = pos->name;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] soc: soc-core: fix a missing check on list iterator
@ 2022-03-27  8:20 ` Xiaomeng Tong
  0 siblings, 0 replies; 4+ messages in thread
From: Xiaomeng Tong @ 2022-03-27  8:20 UTC (permalink / raw)
  To: lgirdwood, broonie, perex, tiwai
  Cc: stable, alsa-devel, Xiaomeng Tong, linux-kernel, kuninori.morimoto.gx

The bug is here:
	*dai_name = dai->driver->name;

For for_each_component_dais, just like list_for_each_entry,
the list iterator 'dai' will point to a bogus position
containing HEAD if the list is empty or no element is found.
This case must be checked before any use of the iterator,
otherwise it will lead to a invalid memory access.

To fix the bug, use a new variable 'iter' as the list iterator,
while use the original variable 'dai' as a dedicated pointer
to point to the found element.

Cc: stable@vger.kernel.org
Fixes: 58bf4179000a3 ("ASoC: soc-core: remove dai_drv from snd_soc_component")
Signed-off-by: Xiaomeng Tong <xiam0nd.tong@gmail.com>
---
 sound/soc/soc-core.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c
index 434e61b46983..064fc0347868 100644
--- a/sound/soc/soc-core.c
+++ b/sound/soc/soc-core.c
@@ -3238,7 +3238,7 @@ int snd_soc_get_dai_name(const struct of_phandle_args *args,
 
 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
 		if (ret == -ENOTSUPP) {
-			struct snd_soc_dai *dai;
+			struct snd_soc_dai *dai = NULL, *iter;
 			int id = -1;
 
 			switch (args->args_count) {
@@ -3261,12 +3261,19 @@ int snd_soc_get_dai_name(const struct of_phandle_args *args,
 			ret = 0;
 
 			/* find target DAI */
-			for_each_component_dais(pos, dai) {
-				if (id == 0)
+			for_each_component_dais(pos, iter) {
+				if (id == 0) {
+					dai = iter;
 					break;
+				}
 				id--;
 			}
 
+			if (!dai) {
+				ret = -EINVAL;
+				continue;
+			}
+
 			*dai_name = dai->driver->name;
 			if (!*dai_name)
 				*dai_name = pos->name;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] soc: soc-core: fix a missing check on list iterator
  2022-03-27  8:20 ` Xiaomeng Tong
@ 2022-03-27 23:31   ` Kuninori Morimoto
  -1 siblings, 0 replies; 4+ messages in thread
From: Kuninori Morimoto @ 2022-03-27 23:31 UTC (permalink / raw)
  To: Xiaomeng Tong
  Cc: lgirdwood, broonie, perex, tiwai, alsa-devel, linux-kernel, stable


Hi Xiaomeng

Thank you for your patch

> The bug is here:
> 	*dai_name = dai->driver->name;
> 
> For for_each_component_dais, just like list_for_each_entry,
> the list iterator 'dai' will point to a bogus position
> containing HEAD if the list is empty or no element is found.
> This case must be checked before any use of the iterator,
> otherwise it will lead to a invalid memory access.
> 
> To fix the bug, use a new variable 'iter' as the list iterator,
> while use the original variable 'dai' as a dedicated pointer
> to point to the found element.
> 
> Cc: stable@vger.kernel.org
> Fixes: 58bf4179000a3 ("ASoC: soc-core: remove dai_drv from snd_soc_component")
> Signed-off-by: Xiaomeng Tong <xiam0nd.tong@gmail.com>
> ---

From "code" point of view, indeed current code doesn't care non-find case,
but from "logic" point of view, it should find target DAI here.

If it couldn't find the DAI, it means
"This is the target component, but it doesn't have target DAI",
and it is very strange for me.

If you got such bug, it is CPU or Codec driver side bug, I think.
If this is for security reason, indicating such message somewhere
(for example by /* comment */) is nice idea for me.

Thank you for your help !!

Best regards
---
Kuninori Morimoto

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] soc: soc-core: fix a missing check on list iterator
@ 2022-03-27 23:31   ` Kuninori Morimoto
  0 siblings, 0 replies; 4+ messages in thread
From: Kuninori Morimoto @ 2022-03-27 23:31 UTC (permalink / raw)
  To: Xiaomeng Tong; +Cc: alsa-devel, lgirdwood, linux-kernel, stable, tiwai, broonie


Hi Xiaomeng

Thank you for your patch

> The bug is here:
> 	*dai_name = dai->driver->name;
> 
> For for_each_component_dais, just like list_for_each_entry,
> the list iterator 'dai' will point to a bogus position
> containing HEAD if the list is empty or no element is found.
> This case must be checked before any use of the iterator,
> otherwise it will lead to a invalid memory access.
> 
> To fix the bug, use a new variable 'iter' as the list iterator,
> while use the original variable 'dai' as a dedicated pointer
> to point to the found element.
> 
> Cc: stable@vger.kernel.org
> Fixes: 58bf4179000a3 ("ASoC: soc-core: remove dai_drv from snd_soc_component")
> Signed-off-by: Xiaomeng Tong <xiam0nd.tong@gmail.com>
> ---

From "code" point of view, indeed current code doesn't care non-find case,
but from "logic" point of view, it should find target DAI here.

If it couldn't find the DAI, it means
"This is the target component, but it doesn't have target DAI",
and it is very strange for me.

If you got such bug, it is CPU or Codec driver side bug, I think.
If this is for security reason, indicating such message somewhere
(for example by /* comment */) is nice idea for me.

Thank you for your help !!

Best regards
---
Kuninori Morimoto

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-03-27 23:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-27  8:20 [PATCH] soc: soc-core: fix a missing check on list iterator Xiaomeng Tong
2022-03-27  8:20 ` Xiaomeng Tong
2022-03-27 23:31 ` Kuninori Morimoto
2022-03-27 23:31   ` Kuninori Morimoto

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.