ASoC: topology: Avoid card NULL deref in snd_soc_tplg_component_remove()
Subsystems (3)
PATCH 1/1 ASoC: topology: Avoid card NULL deref in snd_soc_tplg_component_remove()
Don't deference card in comp->card->snd_card before checking for NULL card.
During the unloading of ASoC kernel modules, there is a kernel oops in snd_soc_tplg_component_remove() that happens because comp->card is set to NULL in soc_cleanup_component().
--- sound/soc/soc-topology.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
sound/soc/soc-topology.c +7 -3
@@ -2613,15 +2613,18 @@ EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);/* remove dynamic controls from the component driver */ int snd_soc_tplg_component_remove (struct snd_soc_component *comp) { -struct snd_card *card = comp->card->snd_card; +struct snd_card *card;struct snd_soc_dobj *dobj, *next_dobj;int pass;/* process the header types from end to start */ for (pass =SOC_TPLG_PASS_END ; pass >=SOC_TPLG_PASS_START ; pass--) { + card = (comp->card) ? comp->card->snd_card :NULL ; +/* remove mixer controls */ -down_write (&card->controls_rwsem); +if (card) +down_write (&card->controls_rwsem);list_for_each_entry_safe (dobj, next_dobj, &comp->dobj_list, list) {
@@ -2660,7 +2663,8 @@ int snd_soc_tplg_component_remove(struct snd_soc_component *comp)break ; } } -up_write (&card->controls_rwsem); +if (card) +up_write (&card->controls_rwsem); }/* let caller know if FW can be freed when no objects are left */
Signature · 1 line
2.17.1
Quoted 33 lines · Dean Gehnert
Don't deference card in
comp->card->snd_cardbefore checking for NULL card.During the unloading of ASoC kernel modules, there is a kernel oops in
snd_soc_tplg_component_remove()that happens becausecomp->cardis set to NULL insoc_cleanup_component().Cc Liam Girdwood <[email protected]>Cc Mark Brown <[email protected]>Cc Jaroslav Kysela <[email protected]>Cc Takashi Iwai <[email protected]>Fixes 7e567b5ae063 ("ASoC: topology: Add missing rwsem around snd_ctl_remove() calls")Signed-off-by Dean Gehnert <[email protected]>--- sound/soc/soc-topology.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)sound/soc/soc-topology.c +1
-1@@ -2613,15 +2613,18 @@ EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);/* remove dynamic controls from the component driver */ int snd_soc_tplg_component_remove (struct snd_soc_component *comp) { -struct snd_card *card = comp->card->snd_card; +struct snd_card *card;struct snd_soc_dobj *dobj, *next_dobj;int pass;/* process the header types from end to start */
sound/soc/soc-topology.c EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); · line 2629
Quoted 10 lines
sound/soc/soc-topology.c +4
-1for (pass =SOC_TPLG_PASS_END ; pass >=SOC_TPLG_PASS_START ; pass--) { + card = (comp->card) ? comp->card->snd_card :NULL ; +/* remove mixer controls */ -down_write (&card->controls_rwsem); +if (card) +down_write (&card->controls_rwsem);list_for_each_entry_safe (dobj, next_dobj, &comp->dobj_list, list) {
I'm pretty sure that quite a lot of operations in this list_for_each_entry_safe() loop require existing card...
And trying to investigate more closely, there is no soc_cleanup_component() mentioned in commit message, for quite a few kernel versions - seems to have been removed during v5.5-rc1.
I would say to not merge this, unless problem can be reproduced with latest kernel and even then would consider if it is a correct fix.
Quoted 11 lines
sound/soc/soc-topology.c +2
-1@@ -2660,7 +2663,8 @@ int snd_soc_tplg_component_remove(struct snd_soc_component *comp)break ; } } -up_write (&card->controls_rwsem); +if (card) +up_write (&card->controls_rwsem); }/* let caller know if FW can be freed when no objects are left */
Quoted 36 lines · Amadeusz Sławiński
Quoted 35 lines · Dean Gehnert
Don't deference card in
comp->card->snd_cardbefore checking for NULL card.During the unloading of ASoC kernel modules, there is a kernel oops in
snd_soc_tplg_component_remove()that happens becausecomp->cardis set to NULL insoc_cleanup_component().Cc Liam Girdwood <[email protected]>Cc Mark Brown <[email protected]>Cc Jaroslav Kysela <[email protected]>Cc Takashi Iwai <[email protected]>Fixes 7e567b5ae063 ("ASoC: topology: Add missing rwsem around snd_ctl_remove() calls")Signed-off-by Dean Gehnert <[email protected]>---
sound/soc/soc-topology.c| 10 +++++++---1 file changed, 7 insertions(+), 3 deletions(-)sound/soc/soc-topology.c +2
-1@@ -2613,15 +2613,18 @@ EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); /* remove dynamic controls from the component driver */ int snd_soc_tplg_component_remove(struct snd_soc_component *comp) { - struct snd_card *card = comp->card->snd_card; + struct snd_card *card; struct snd_soc_dobj *dobj, *next_dobj; int pass; /* process the header types from end to start */ for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) { + card = (comp->card) ? comp->card->snd_card : NULL; +
Quoted 8 lines
sound/soc/soc-topology.c EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); · line 2629
Quoted 6 lines
sound/soc/soc-topology.c +2
-1/* remove mixer controls */ - down_write(&card->controls_rwsem); + if (card) + down_write(&card->controls_rwsem); list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, list) {I'm pretty sure that quite a lot of operations in this
list_for_each_entry_safe()loop require existing card...
I get your point... Many of the cases in the loop, the functions are also dereferencing 'comp->card->', so this patch may not be the actual root cause fix... It worked for us since the kernel oops no longer happens, but looks like there are many more dereferences that could still cause problems.
Quoted 2 lines
And trying to investigate more closely, there is no
soc_cleanup_component()mentioned in commit message, for quite a few kernel versions - seems to have been removed during v5.5-rc1.
Ah yes! You are correct. soc_cleanup_component() functionality has been moved to soc_remove_component() in later kernels.
Quoted 2 lines
I would say to not merge this, unless problem can be reproduced with latest kernel and even then would consider if it is a correct fix.
Agreed... This patch made our our kernel oops go away, however, we are going to dig deeper into the kernel oops call stack to see if I can find the root cause problem. There is something going on with the sound cleanup since this happens during rmmod... We just need to dig deeper and see if we can find what is really causing the problems.
Quoted 11 lines
Quoted 10 lines
sound/soc/soc-topology.c +0
-0@@ -2660,7 +2663,8 @@ int snd_soc_tplg_component_remove(struct snd_soc_component *comp) break;}
}
-up_write(&card->controls_rwsem);
+ if (card)
+up_write(&card->controls_rwsem);
}
/* let caller know if FW can be freed when no objects are left */