soc: meson: fix a missing check on list iterator
Subsystems (5)
PATCH 1/1 soc: meson: fix a missing check on list iterator
The bug is here:
*dai_name = dai->driver->name;
For for_each_component_dais(), just like list_for_each_entry, the list iterator 'runtime' 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, just move the assignment into loop and return 0 when element is found, otherwise return -EINVAL;
--- sound/soc/meson/aiu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
sound/soc/meson/aiu.c +5 -5
@@ -61,14 +61,14 @@ int aiu_of_xlate_dai_name(struct snd_soc_component *component,return -EINVAL ;for_each_component_dais (component, dai) { -if (id ==0 ) -break ; +if (id ==0 ) { + *dai_name = dai->driver->name; +return 0 ; + } id--; } - *dai_name = dai->driver->name; - -return 0 ; +return -EINVAL ; } static int aiu_cpu_of_xlate_dai_name(struct snd_soc_component *component,
Signature · 1 line
2.17.1
Jerome Brunet
maintainerQuoted 11 lines · Xiaomeng Tong
The bug is here:
*dai_name = dai->driver->name;For
for_each_component_dais(), just likelist_for_each_entry, the list iterator 'runtime' 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, just move the assignment into loop and return 0 when element is found, otherwise return
-EINVAL;
Except we already checked that the id is valid and know an element will be be found once we enter the loop. No bug here and this patch does not seem necessary to me.
Quoted 32 lines
Fixes 6ae9ca9ce986b ("ASoC: meson: aiu: add i2s and spdif support")Signed-off-by Xiaomeng Tong <[email protected]>--- sound/soc/meson/aiu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)sound/soc/meson/aiu.c +5
-5@@ -61,14 +61,14 @@ int aiu_of_xlate_dai_name(struct snd_soc_component *component,return -EINVAL ;for_each_component_dais (component, dai) { -if (id ==0 ) -break ; +if (id ==0 ) { + *dai_name = dai->driver->name; +return 0 ; + } id--; } - *dai_name = dai->driver->name; - -return 0 ; +return -EINVAL ; } static int aiu_cpu_of_xlate_dai_name(struct snd_soc_component *component,
Quoted 11 lines · Jerome Brunet
Quoted 9 lines · Xiaomeng Tong
The bug is here:
*dai_name = dai->driver->name;For
for_each_component_dais(), just likelist_for_each_entry, the list iterator 'runtime' 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.
Quoted 6 lines
Quoted 2 lines
To fix the bug, just move the assignment into loop and return 0 when element is found, otherwise return
-EINVAL;Except we already checked that the id is valid and know an element will be be found once we enter the loop. No bug here and this patch does not seem necessary to me.
Yea, you should be right, it is not a bug here. id already be checked before enter the loop:
if (id < 0 || id >= component->num_dai)
return -EINVAL;
but if component->num_dai is not correct due to miscaculation or others reason and the door is reopened, this patch can avoid a invalid memory access. Anyway, it is a good choice to use the list iterator only inside the loop, as linus suggested[1]. and we are on the way to change all these use-after-iter cases.
[1]https://lore.kernel.org/lkml/[email protected]/
--
Xiaomeng Tong
Jerome Brunet
maintainerQuoted 22 lines · Xiaomeng Tong
Quoted 11 lines · Jerome Brunet
Quoted 9 lines · Xiaomeng Tong
The bug is here:
*dai_name = dai->driver->name;For
for_each_component_dais(), just likelist_for_each_entry, the list iterator 'runtime' 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.Quoted 6 lines
Quoted 2 lines
To fix the bug, just move the assignment into loop and return 0 when element is found, otherwise return
-EINVAL;Except we already checked that the id is valid and know an element will be be found once we enter the loop. No bug here and this patch does not seem necessary to me.
Yea, you should be right, it is not a bug here. id already be checked before enter the loop:
Quoted 6 lines
if (id < 0 || id >=
component->num_dai)return -EINVAL;but if
component->num_daiis not correct due to miscaculation or others reason and the door is reopened, this patch can avoid a invalid memory access.
This is a speculation which just does not hold ATM. What this patch does is adding dead code cause the last "return -EINVAL;" will never be reached.
This no fix nor improvement.
Quoted 5 lines
Anyway,
it is a good choice to use the list iterator only inside the loop, as linus suggested[1]. and we are on the way to change all these use-after-iter cases.
You can make improvements as long as the code is kept clean an maintainable. Dead code is not OK.