From: Takashi Iwai <tiwai@suse.de>
To: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Oliver Neukum <oneukum@suse.com>,
Vasily Khoruzhick <anarsoul@gmail.com>,
alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] ALSA: line6: Improve poor error handling in line6_init_cap_control
Date: Tue, 25 May 2021 09:05:13 +0200 [thread overview]
Message-ID: <s5hh7ire1jq.wl-tiwai@suse.de> (raw)
In-Reply-To: <20210524173644.GA116662@hyeyoo>
On Mon, 24 May 2021 19:36:44 +0200,
Hyeonggon Yoo wrote:
>
> line6_init_cap_control doesn't free resources properly when allocations
> like kmalloc, usb_alloc_urb fails. It can cause memory leak when some
> allocations succeed, and then an error occurs.
>
> This patch makes line6_init_cap_control to properly free the resources,
> freeing previously allocated resources when there's an error.
>
> Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
> ---
> sound/usb/line6/driver.c | 36 +++++++++++++++++++++++++++---------
> 1 file changed, 27 insertions(+), 9 deletions(-)
>
> diff --git a/sound/usb/line6/driver.c b/sound/usb/line6/driver.c
> index 9602929b7de9..6991cb855023 100644
> --- a/sound/usb/line6/driver.c
> +++ b/sound/usb/line6/driver.c
> @@ -688,34 +688,52 @@ static int line6_init_cap_control(struct usb_line6 *line6)
>
> /* initialize USB buffers: */
> line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
> - if (!line6->buffer_listen)
> - return -ENOMEM;
> + if (!line6->buffer_listen) {
> + ret = -ENOMEM;
> + goto fail_ret;
> + }
>
> line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
> - if (!line6->urb_listen)
> - return -ENOMEM;
> + if (!line6->urb_listen) {
> + ret = -ENOMEM;
> + goto fail1;
> + }
>
> if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
> line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
> - if (!line6->buffer_message)
> - return -ENOMEM;
> + if (!line6->buffer_message) {
> + ret = -ENOMEM;
> + goto fail2;
> + }
>
> ret = line6_init_midi(line6);
> if (ret < 0)
> - return ret;
> + goto fail3;
> } else {
> ret = line6_hwdep_init(line6);
> if (ret < 0)
> - return ret;
> + goto fail2;
> }
>
> ret = line6_start_listen(line6);
> if (ret < 0) {
> dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
> - return ret;
> + if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI)
> + goto fail3;
> + else
> + goto fail2;
> }
>
> return 0;
> +
> +fail3:
> + kfree(line6->buffer_message);
> +fail2:
> + usb_free_urb(line6->urb_listen);
> +fail1:
> + kfree(line6->buffer_listen);
> +fail_ret:
> + return ret;
> }
Those resources are freed in the common destructor of the card
instance, line6_destruct(), at disconnect callback, so it's redundant
to release them here; even worse, since you haven't re-initialize with
NULL, this change would lead to double-free.
thanks,
Takashi
next prev parent reply other threads:[~2021-05-25 7:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-24 17:36 [PATCH] ALSA: line6: Improve poor error handling in line6_init_cap_control Hyeonggon Yoo
2021-05-24 17:39 ` Hyeonggon Yoo
2021-05-25 7:05 ` Takashi Iwai [this message]
2021-05-25 8:00 ` Hyeonggon Yoo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=s5hh7ire1jq.wl-tiwai@suse.de \
--to=tiwai@suse.de \
--cc=42.hyeyoo@gmail.com \
--cc=alsa-devel@alsa-project.org \
--cc=anarsoul@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=oneukum@suse.com \
--cc=perex@perex.cz \
--cc=tiwai@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox