mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hans de Goede <hdegoede@redhat.com>
To: Navid Emamdoost <navid.emamdoost@gmail.com>
Cc: emamd001@umn.edu, kjlu@umn.edu, smccaman@umn.edu,
	Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] virt: vbox: fix memory leak in hgcm_call_preprocess_linaddr
Date: Sat, 28 Sep 2019 11:54:20 +0200	[thread overview]
Message-ID: <3b5913e3-856a-db81-7708-929e62ee53d4@redhat.com> (raw)
In-Reply-To: <20190927230421.20837-1-navid.emamdoost@gmail.com>

Hi,

On 28-09-2019 01:04, Navid Emamdoost wrote:
> In hgcm_call_preprocess_linaddr memory is allocated for bounce_buf but
> is not released if copy_form_user fails. The release is added.
> 
> Fixes: 579db9d45cb4 ("virt: Add vboxguest VMMDEV communication code")
> 
> Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>

Thank you for catching this, I agree this is a bug, but I think we
can fix it in a cleaner way (see below).

> ---
>   drivers/virt/vboxguest/vboxguest_utils.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/virt/vboxguest/vboxguest_utils.c b/drivers/virt/vboxguest/vboxguest_utils.c
> index 75fd140b02ff..7965885a50fa 100644
> --- a/drivers/virt/vboxguest/vboxguest_utils.c
> +++ b/drivers/virt/vboxguest/vboxguest_utils.c
> @@ -222,8 +222,10 @@ static int hgcm_call_preprocess_linaddr(
>   
>   	if (copy_in) {
>   		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
> -		if (ret)
> +		if (ret) {
> +			kvfree(bounce_buf);
>   			return -EFAULT;
> +		}
>   	} else {
>   		memset(bounce_buf, 0, len);
>   	}
> 

First let me quote some more of the function, pre leak fix, for context:

	bounce_buf = kvmalloc(len, GFP_KERNEL);
	if (!bounce_buf)
		return -ENOMEM;

	if (copy_in) {
		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
		if (ret)
			return -EFAULT;
	} else {
		memset(bounce_buf, 0, len);
	}

	*bounce_buf_ret = bounce_buf;

This function gets called repeatedly by hgcm_call_preprocess(), and the
caller of hgcm_call_preprocess() already takes care of freeing the
bounce bufs both on a (later) error and on success:

	ret = hgcm_call_preprocess(parms, parm_count, &bounce_bufs, &size);
	if (ret) {
		/* Even on error bounce bufs may still have been allocated */
		goto free_bounce_bufs;
	}

	...

free_bounce_bufs:
	if (bounce_bufs) {
		for (i = 0; i < parm_count; i++)
			kvfree(bounce_bufs[i]);
		kfree(bounce_bufs);
	}

So we are already taking care of freeing bounce-bufs allocated for previous
parameters to the call (which me must do anyways), so a cleaner fix would
be to store the allocated bounce_buf in the bounce_bufs array before
doing the copy_from_user, then if copy_from_user fails it will be cleaned
up by the code at the free_bounce_bufs label.

IOW I believe it is better to fix this by changing the part of
hgcm_call_preprocess_linaddr I quoted to:

	bounce_buf = kvmalloc(len, GFP_KERNEL);
	if (!bounce_buf)
		return -ENOMEM;

	*bounce_buf_ret = bounce_buf;

	if (copy_in) {
		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
		if (ret)
			return -EFAULT;
	} else {
		memset(bounce_buf, 0, len);
	}

This should also fix the leak in IMHO is a clear way of doing so.

Regards,

Hans






  reply	other threads:[~2019-09-28  9:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-27 23:04 Navid Emamdoost
2019-09-28  9:54 ` Hans de Goede [this message]
2019-09-30  2:22   ` Navid Emamdoost
2019-09-30  8:22     ` Hans de Goede
2019-09-30 20:42       ` [PATCH v2] " Navid Emamdoost
2019-10-01  6:15         ` Hans de Goede
2019-09-30 20:43       ` [PATCH] " Navid Emamdoost

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=3b5913e3-856a-db81-7708-929e62ee53d4@redhat.com \
    --to=hdegoede@redhat.com \
    --cc=arnd@arndb.de \
    --cc=emamd001@umn.edu \
    --cc=gregkh@linuxfoundation.org \
    --cc=kjlu@umn.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=navid.emamdoost@gmail.com \
    --cc=smccaman@umn.edu \
    /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

all inboxes | Powered by JetHome®