Skip to contentSkip to conversation

most: fix a memleak in audio_probe_channel

PATCH In review Zhipeng Lu 2 participants 3 messages Jan 22, 2024 – Jan 29, 2024 Most(r) technology driver

lore
Share & tools
lore

Zhipeng Lu

author
#0 lore raw

PATCH 1/1 most: fix a memleak in audio_probe_channel

When get_channel fails, audio_probe_channel should free adpt like all its following error-handling paths after get_channel. Otherwise there could be a memleak.

Fixes 15600aea2754 ("staging: most: sound: create one sound card w/ multiple PCM devices per MOST device")
---
 drivers/most/most_snd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/most/most_snd.c +2 -1
index 45d762804c5e..6cccc9c26796 100644
@@ -564,7 +564,8 @@ static int audio_probe_channel(struct most_interface *iface, int channel_id,
 	if (get_channel(iface, channel_id)) {
 		pr_err("channel (%s:%d) is already linked\n",
 		       iface->description, channel_id);
-		return -EEXIST;
+		ret = -EEXIST;
+		goto err_free_adpt;
 	}
 
 	if (cfg->direction == MOST_CH_TX) {
Signature · 1 line
2.34.1
#1 lore raw
Quoted 15 lines · Zhipeng Lu

When get_channel fails, audio_probe_channel should free adpt like all its following error-handling paths after get_channel. Otherwise there could be a memleak.

Fixes 15600aea2754 ("staging: most: sound: create one sound card w/ multiple PCM devices per MOST device")
---
 drivers/most/most_snd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/most/most_snd.c +0 -0
index 45d762804c5e..6cccc9c26796 100644

drivers/most/most_snd.c static int audio_probe_channel(struct most_interface *iface, int channel_id, · line 568

Quoted 6 lines
drivers/most/most_snd.c +2 -1
@@ -564,7 +564,8 @@ static int audio_probe_channel(struct most_interface *iface, int channel_id,
 	if (get_channel(iface, channel_id)) {
 		pr_err("channel (%s:%d) is already linked\n",
 		       iface->description, channel_id);
-		return -EEXIST;
+		ret = -EEXIST;
+		goto err_free_adpt;

No, this doesn't work. Someone should add a comment here explaining why.

This function is a bit complicated because we sometimes allocate "adpt" and sometimes we don't. Why can we not make it consistently one way or the other? This is not my code and I don't know. But presumably there is a good reason. I looked up the previous discussion of this and found this thread.

https://lore.kernel.org/all/[email protected]/

Anyway, in the end, we're trying to clean up and on the other error paths we're allowed to free "adpt" even though we didn't allocate it.

However once it's got a channel linked then we cannot. At that stage "adpt" is already added to the &iface->p->channel_list list. The release_adapter() adapter function will free it without removing it from the list so it leads to a use after free.

However, memory on this path is not leaked as your commit message says. "adpt" is still on the lists. The stuff is in a messed up state so the user will need to tear it all down manually and recreate the configuration. Quoting the email I linked to, "This involves the call to mdev_link_destroy_link_store() that cleans up everything."

So we can't apply your patch because it leads to a use after free. But we could apply a patch which adds a comment like:

/*
* This error path doesn't leak. If the channel is already set
* up then something has gone badly wrong. The user will have
* to tear everything down and reconfigure from scratch. The
* memory will be released via mdev_link_destroy_link_store(). *
*/

regards,
dan carpenter

#2 lore raw
Quoted 52 lines · Zhipeng Lu
Quoted 15 lines

When get_channel fails, audio_probe_channel should free adpt like all its following error-handling paths after get_channel. Otherwise there could be a memleak.

Fixes 15600aea2754 ("staging: most: sound: create one sound card w/ multiple PCM devices per MOST device")
---
 drivers/most/most_snd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/most/most_snd.c +0 -0
index 45d762804c5e..6cccc9c26796 100644

drivers/most/most_snd.c static int audio_probe_channel(struct most_interface *iface, int channel_id, · line 568

Quoted 6 lines
drivers/most/most_snd.c +2 -1
@@ -564,7 +564,8 @@ static int audio_probe_channel(struct most_interface *iface, int channel_id,
 	if (get_channel(iface, channel_id)) {
 		pr_err("channel (%s:%d) is already linked\n",
 		       iface->description, channel_id);
-		return -EEXIST;
+		ret = -EEXIST;
+		goto err_free_adpt;

No, this doesn't work. Someone should add a comment here explaining why.

This function is a bit complicated because we sometimes allocate "adpt" and sometimes we don't. Why can we not make it consistently one way or the other? This is not my code and I don't know. But presumably there is a good reason. I looked up the previous discussion of this and found this thread.

https://lore.kernel.org/all/[email protected]/

Anyway, in the end, we're trying to clean up and on the other error paths we're allowed to free "adpt" even though we didn't allocate it.

However once it's got a channel linked then we cannot. At that stage "adpt" is already added to the &iface->p->channel_list list. The release_adapter() adapter function will free it without removing it from the list so it leads to a use after free.

However, memory on this path is not leaked as your commit message says. "adpt" is still on the lists. The stuff is in a messed up state so the user will need to tear it all down manually and recreate the configuration. Quoting the email I linked to, "This involves the call to mdev_link_destroy_link_store() that cleans up everything."

So we can't apply your patch because it leads to a use after free. But we could apply a patch which adds a comment like:

Quoted 8 lines

/*
* This error path doesn't leak. If the channel is already set
* up then something has gone badly wrong. The user will have
* to tear everything down and reconfigure from scratch. The
* memory will be released via mdev_link_destroy_link_store(). *
*/

Thank you for your correction and advise.
We would like to submit a new patch to add such comment.\

Regards,
Zhipeng.

Quoted 2 lines

regards,
dan carpenter