Skip to contentSkip to conversation

ASoC: dapm: using freed pointer in dapm_kcontrol_add_widget()

PATCH Applied Dan Carpenter 4 participants 8 messages Jul 31, 2013 – Aug 1, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: dapm: using freed pointer in dapm_kcontrol_add_widget()

There is a typo here so we end up using the old freed pointer instead of the newly allocated one. (If the "n" is zero then the code works, obviously).

---
Only needed in linux-next

sound/soc/soc-dapm.c +4 -4
index 9abb3b2..d74c356 100644
@@ -225,13 +225,13 @@ static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
 
 	new_data = krealloc(data, sizeof(*data) + sizeof(widget) * n,
 		GFP_KERNEL);
-	if (!data)
+	if (!new_data)
 		return -ENOMEM;
 
-	data->wlist.widgets[n - 1] = widget;
-	data->wlist.num_widgets = n;
+	new_data->wlist.widgets[n - 1] = widget;
+	new_data->wlist.num_widgets = n;
 
-	kcontrol->private_data = data;
+	kcontrol->private_data = new_data;
 
 	return 0;
 }
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Dan Carpenter

There is a typo here so we end up using the old freed pointer instead of the newly allocated one. (If the "n" is zero then the code works, obviously).

Thanks.

Olof, can you check whether this fixes the crash you see?

Quoted 26 lines

---
Only needed in linux-next

sound/soc/soc-dapm.c +4 -4
index 9abb3b2..d74c356 100644
@@ -225,13 +225,13 @@ static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
 
  	new_data = krealloc(data, sizeof(*data) + sizeof(widget) * n,
  		GFP_KERNEL);
-	if (!data)
+	if (!new_data)
  		return -ENOMEM;
 
-	data->wlist.widgets[n - 1] = widget;
-	data->wlist.num_widgets = n;
+	new_data->wlist.widgets[n - 1] = widget;
+	new_data->wlist.num_widgets = n;
 
-	kcontrol->private_data = data;
+	kcontrol->private_data = new_data;
 
  	return 0;
  }
maintainer Applied
#7 lore raw
In reply to Dan Carpenter
Quoted 3 lines · Dan Carpenter

There is a typo here so we end up using the old freed pointer instead of the newly allocated one. (If the "n" is zero then the code works, obviously).

Applied, thanks.

#2 lore raw
In reply to Lars-Peter Clausen

Hi,

Quoted 6 lines · Lars-Peter Clausen
Quoted 5 lines · Dan Carpenter

There is a typo here so we end up using the old freed pointer instead of the newly allocated one. (If the "n" is zero then the code works, obviously).

Quoted 8 lines
Quoted 1 line

Thanks.

Olof, can you check whether this fixes the crash you see?

Nope.

There's also remaining issues with the code, that patch isn't enough. The structure that is krealloced() has a list_head in it, but the list isn't moved from the old head to the new one. There's no safe way to do that using krealloc, since the old list_head is gone by then, so it's probably easest to open-code with kzalloc/memcpy/kfree.

But even with that fixed, I still see the same issue. Boggle.

(Btw, I'm testing on top of commit 5106b92f80a2cd37c52cffed80b4f5acfb77ccfd).

-Olof

#3 lore raw
Quoted 18 lines · Olof Johansson

Hi,

Quoted 6 lines · Lars-Peter Clausen
Quoted 5 lines · Dan Carpenter

There is a typo here so we end up using the old freed pointer instead of the newly allocated one. (If the "n" is zero then the code works, obviously).

Quoted 8 lines
Quoted 1 line

Thanks.

Olof, can you check whether this fixes the crash you see?

Quoted 7 lines

Nope.

There's also remaining issues with the code, that patch isn't enough. The structure that is krealloced() has a list_head in it, but the list isn't moved from the old head to the new one. There's no safe way to do that using krealloc, since the old list_head is gone by then, so it's probably easest to open-code with kzalloc/memcpy/kfree.

Hm, right I didn't think of that. Maybe it's better to just keep a the widget list in a separate pointer, so none of the other fields of the kcontrol_data struct are affected by the krealloc.

- Lars

#4 lore raw
Quoted 27 lines · Lars-Peter Clausen
Quoted 18 lines · Olof Johansson

Hi,

Quoted 6 lines · Lars-Peter Clausen
Quoted 5 lines · Dan Carpenter

There is a typo here so we end up using the old freed pointer instead of the newly allocated one. (If the "n" is zero then the code works, obviously).

Quoted 8 lines
Quoted 1 line

Thanks.

Olof, can you check whether this fixes the crash you see?

Quoted 7 lines

Nope.

There's also remaining issues with the code, that patch isn't enough. The structure that is krealloced() has a list_head in it, but the list isn't moved from the old head to the new one. There's no safe way to do that using krealloc, since the old list_head is gone by then, so it's probably easest to open-code with kzalloc/memcpy/kfree.

Quoted 4 lines

Hm, right I didn't think of that. Maybe it's better to just keep a the widget list in a separate pointer, so none of the other fields of the kcontrol_data struct are affected by the krealloc.

Something along the lines of this:

sound/soc/soc-dapm.c +28 -18
index d74c356..ef1db0b 100644
@@ -177,7 +177,7 @@ static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
 struct dapm_kcontrol_data {
 	unsigned int value;
 	struct list_head paths;
-	struct snd_soc_dapm_widget_list wlist;
+	struct snd_soc_dapm_widget_list *wlist;
 };
 
 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
@@ -185,26 +185,36 @@ static int dapm_kcontrol_data_alloc(struct
 {
 	struct dapm_kcontrol_data *data;
 
-	data = kzalloc(sizeof(*data) + sizeof(widget), GFP_KERNEL);
-	if (!data) {
-		dev_err(widget->dapm->dev,
-				"ASoC: can't allocate kcontrol data for %s\n",
-				widget->name);
-		return -ENOMEM;
-	}
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		goto err;
 
-	data->wlist.widgets[0] = widget;
-	data->wlist.num_widgets = 1;
+	data->wlist = kzalloc(sizeof(*data->wlist) + sizeof(widget),
+		GFP_KERNEL);
+	if (!data->wlist)
+		goto err_free;
+
+	data->wlist->widgets[0] = widget;
+	data->wlist->num_widgets = 1;
 	INIT_LIST_HEAD(&data->paths);
 
 	kcontrol->private_data = data;
 
 	return 0;
+err_free:
+	kfree(data);
+err:
+	dev_err(widget->dapm->dev,
+			"ASoC: can't allocate kcontrol data for %s\n",
+			widget->name);
+
+	return -ENOMEM;
 }
 
 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
 {
 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
+	kfree(data->wlist);
 	kfree(data);
 }
 
@@ -213,25 +223,25 @@ static struct snd_soc_dapm_widget_list
 {
 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
 
-	return &data->wlist;
+	return data->wlist;
 }
 
 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
 	struct snd_soc_dapm_widget *widget)
 {
 	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
-	struct dapm_kcontrol_data *new_data;
-	unsigned int n = data->wlist.num_widgets + 1;
+	struct snd_soc_dapm_widget_list *new_wlist;
+	unsigned int n = data->wlist->num_widgets + 1;
 
-	new_data = krealloc(data, sizeof(*data) + sizeof(widget) * n,
+	new_wlist = krealloc(data, sizeof(*new_wlist) + sizeof(widget) * n,
 		GFP_KERNEL);
-	if (!new_data)
+	if (!new_wlist)
 		return -ENOMEM;
 
-	new_data->wlist.widgets[n - 1] = widget;
-	new_data->wlist.num_widgets = n;
+	new_wlist->widgets[n - 1] = widget;
+	new_wlist->num_widgets = n;
 
-	kcontrol->private_data = new_data;
+	data->wlist = new_wlist;
 
 	return 0;
 }
maintainer
#5 lore raw
Quoted 1 line · Lars-Peter Clausen

Something along the lines of this:

This seems to fix it for me, can you submit properly please?

Tested-by
#6 lore raw
Quoted 5 lines · Mark Brown
Quoted 1 line · Lars-Peter Clausen

Something along the lines of this:

This seems to fix it for me, can you submit properly please?

Here too. If you submit that as a patch, feel free to add:

-Olof