Skip to contentSkip to conversation

staging: greybus: audio: fix loop cursor use after iteration

PATCH Superseded Jared Kangas 4 participants 6 messages Jun 5, 2022 – Jun 7, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: greybus: audio: fix loop cursor use after iteration

gbaudio_dapm_free_controls() iterates over widgets using list_for_each_entry_safe(), which leaves the loop cursor pointing to a meaningless structure if it completes a traversal of the list. The cursor was set to NULL at the end of the loop body, but would be overwritten by the final loop cursor update.

Because of this behavior, the widget could be non-null after the loop even if the widget wasn't found, and the cleanup logic would treat the pointer as a valid widget to free.

To fix this, introduce a temporary variable to act as the loop cursor and copy it to a variable that can be accessed after the loop finishes.

This was detected with the help of Coccinelle.

---
 drivers/staging/greybus/audio_helper.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/staging/greybus/audio_helper.c +7 -6
index 843760675876..07461a5d97c7 100644
@@ -115,7 +115,7 @@ int gbaudio_dapm_free_controls(struct snd_soc_dapm_context *dapm,
 			       int num)
 {
 	int i;
-	struct snd_soc_dapm_widget *w, *next_w;
+	struct snd_soc_dapm_widget *w, *next_w, *tmp_w;
 #ifdef CONFIG_DEBUG_FS
 	struct dentry *parent = dapm->debugfs_dapm;
 	struct dentry *debugfs_w = NULL;
@@ -124,13 +124,14 @@ int gbaudio_dapm_free_controls(struct snd_soc_dapm_context *dapm,
 	mutex_lock(&dapm->card->dapm_mutex);
 	for (i = 0; i < num; i++) {
 		/* below logic can be optimized to identify widget pointer */
-		list_for_each_entry_safe(w, next_w, &dapm->card->widgets,
+		w = NULL;
+		list_for_each_entry_safe(tmp_w, next_w, &dapm->card->widgets,
 					 list) {
-			if (w->dapm != dapm)
-				continue;
-			if (!strcmp(w->name, widget->name))
+			if (tmp_w->dapm == dapm &&
+			    !strcmp(tmp_w->name, widget->name)) {
+				w = tmp_w;
 				break;
-			w = NULL;
+			}
 		}
 		if (!w) {
 			dev_err(dapm->dev, "%s: widget not found\n",
Signature · 1 line
2.34.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Jared Kangas

gbaudio_dapm_free_controls() iterates over widgets using list_for_each_entry_safe(), which leaves the loop cursor pointing to a meaningless structure if it completes a traversal of the list. The cursor was set to NULL at the end of the loop body, but would be overwritten by the final loop cursor update.

Quoted 9 lines

Because of this behavior, the widget could be non-null after the loop even if the widget wasn't found, and the cleanup logic would treat the pointer as a valid widget to free.

To fix this, introduce a temporary variable to act as the loop cursor and copy it to a variable that can be accessed after the loop finishes.

This was detected with the help of Coccinelle.

This needs a Fixes tag.

Fixes 510e340efe0c ("staging: greybus: audio: Add helper APIs for dynamic audio modules")

Otherwise, looks good!

regards,
dan carpenter

#2 lore raw

Thanks for catching that! Is there anything I need to do to add the 'Fixes:' tag to the patch? From my understanding, adding tags is done by a maintainer later on, but I'm new to the patch submission process so I want to make sure I'm not missing anything.

Jared

#3 lore raw
Quoted 4 lines · Jared Kangas

Thanks for catching that! Is there anything I need to do to add the 'Fixes:' tag to the patch? From my understanding, adding tags is done by a maintainer later on, but I'm new to the patch submission process so I want to make sure I'm not missing anything.

No, it's up to the person sending the patch to add the fixes tag.

regards,
dan carpenter

#4 lore raw
Quoted 7 lines · Dan Carpenter
Quoted 4 lines · Jared Kangas

Thanks for catching that! Is there anything I need to do to add the 'Fixes:' tag to the patch? From my understanding, adding tags is done by a maintainer later on, but I'm new to the patch submission process so I want to make sure I'm not missing anything.

No, it's up to the person sending the patch to add the fixes tag.

And FYI, "git blame" can be your friend here.

git blame drivers/staging/greybus/audio_helper.c

It looks like commit 510e340efe0cbd is a possible candidate, but I'll leave it up to you to determine that.

-Alex

Quoted 3 lines

regards,
dan carpenter

maintainer Reviewed-by on v1 1/1
#5 lore raw
In reply to Jared Kangas
Quoted 6 lines · Jared Kangas

gbaudio_dapm_free_controls() iterates over widgets using list_for_each_entry_safe(), which leaves the loop cursor pointing to a meaningless structure if it completes a traversal of the list. The cursor was set to NULL at the end of the loop body, but would be overwritten by the final loop cursor update.

Quoted 8 lines

Because of this behavior, the widget could be non-null after the loop even if the widget wasn't found, and the cleanup logic would treat the pointer as a valid widget to free.

To fix this, introduce a temporary variable to act as the loop cursor and copy it to a variable that can be accessed after the loop finishes.

This was detected with the help of Coccinelle.

Please add the missing Fixes tag and a CC stable tag here as Dan mentioned.

Quoted 19 lines
---
 drivers/staging/greybus/audio_helper.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/staging/greybus/audio_helper.c +1 -1
index 843760675876..07461a5d97c7 100644
@@ -115,7 +115,7 @@ int gbaudio_dapm_free_controls(struct snd_soc_dapm_context *dapm,
 			       int num)
 {
 	int i;
-	struct snd_soc_dapm_widget *w, *next_w;
+	struct snd_soc_dapm_widget *w, *next_w, *tmp_w;
 #ifdef CONFIG_DEBUG_FS
 	struct dentry *parent = dapm->debugfs_dapm;
 	struct dentry *debugfs_w = NULL;

drivers/staging/greybus/audio_helper.c int gbaudio_dapm_free_controls(struct snd_soc_dapm_context *dapm, · line 128

Quoted 6 lines
drivers/staging/greybus/audio_helper.c +2 -1
@@ -124,13 +124,14 @@ int gbaudio_dapm_free_controls(struct snd_soc_dapm_context *dapm,
 	mutex_lock(&dapm->card->dapm_mutex);
 	for (i = 0; i < num; i++) {
 		/* below logic can be optimized to identify widget pointer */
-		list_for_each_entry_safe(w, next_w, &dapm->card->widgets,
+		w = NULL;
+		list_for_each_entry_safe(tmp_w, next_w, &dapm->card->widgets,

This should be list_for_each_entry() as w is not unlinked in this loop.

drivers/staging/greybus/audio_helper.c int gbaudio_dapm_free_controls(struct snd_soc_dapm_context *dapm, · line 137

Quoted 13 lines
drivers/staging/greybus/audio_helper.c +4 -4
 					 list) {
-			if (w->dapm != dapm)
-				continue;
-			if (!strcmp(w->name, widget->name))
+			if (tmp_w->dapm == dapm &&
+			    !strcmp(tmp_w->name, widget->name)) {
+				w = tmp_w;
 				break;
-			w = NULL;
+			}
 		}
 		if (!w) {
 			dev_err(dapm->dev, "%s: widget not found\n",

Looks good otherwise: