diff mbox

[3.13.y-ckt,stable] Patch "gadgetfs: use-after-free in ->aio_read()" has been added to staging queue

Message ID 1428357510-16692-1-git-send-email-kamal@canonical.com
State New
Headers show

Commit Message

Kamal Mostafa April 6, 2015, 9:58 p.m. UTC
This is a note to let you know that I have just added a patch titled

    gadgetfs: use-after-free in ->aio_read()

to the linux-3.13.y-queue branch of the 3.13.y-ckt extended stable tree 
which can be found at:

 http://kernel.ubuntu.com/git?p=ubuntu/linux.git;a=shortlog;h=refs/heads/linux-3.13.y-queue

This patch is scheduled to be released in version 3.13.11-ckt19.

If you, or anyone else, feels it should not be added to this tree, please 
reply to this email.

For more information about the 3.13.y-ckt tree, see
https://wiki.ubuntu.com/Kernel/Dev/ExtendedStable

Thanks.
-Kamal

------

From 29e97f7298ed490b12f6c27e60b0607b4f06f081 Mon Sep 17 00:00:00 2001
From: Al Viro <viro@zeniv.linux.org.uk>
Date: Fri, 6 Feb 2015 02:07:45 -0500
Subject: gadgetfs: use-after-free in ->aio_read()

commit f01d35a15fa04162a58b95970fc01fa70ec9dacd upstream.

AIO_PREAD requests call ->aio_read() with iovec on caller's stack, so if
we are going to access it asynchronously, we'd better get ourselves
a copy - the one on kernel stack of aio_run_iocb() won't be there
anymore.  function/f_fs.c take care of doing that, legacy/inode.c
doesn't...

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
---
 drivers/usb/gadget/inode.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

--
1.9.1

Comments

Luis Henriques April 7, 2015, 12:47 p.m. UTC | #1
On Mon, Apr 06, 2015 at 02:58:30PM -0700, Kamal Mostafa wrote:
> This is a note to let you know that I have just added a patch titled
> 
>     gadgetfs: use-after-free in ->aio_read()
> 
> to the linux-3.13.y-queue branch of the 3.13.y-ckt extended stable tree 
> which can be found at:
> 
>  http://kernel.ubuntu.com/git?p=ubuntu/linux.git;a=shortlog;h=refs/heads/linux-3.13.y-queue
> 
> This patch is scheduled to be released in version 3.13.11-ckt19.
> 
> If you, or anyone else, feels it should not be added to this tree, please 
> reply to this email.
>

This backport will add a compilation warning because the 'value'
variable may be used uninitialized in function ep_aio_rwtail -- see
below.

> For more information about the 3.13.y-ckt tree, see
> https://wiki.ubuntu.com/Kernel/Dev/ExtendedStable
> 
> Thanks.
> -Kamal
> 
> ------
> 
> From 29e97f7298ed490b12f6c27e60b0607b4f06f081 Mon Sep 17 00:00:00 2001
> From: Al Viro <viro@zeniv.linux.org.uk>
> Date: Fri, 6 Feb 2015 02:07:45 -0500
> Subject: gadgetfs: use-after-free in ->aio_read()
> 
> commit f01d35a15fa04162a58b95970fc01fa70ec9dacd upstream.
> 
> AIO_PREAD requests call ->aio_read() with iovec on caller's stack, so if
> we are going to access it asynchronously, we'd better get ourselves
> a copy - the one on kernel stack of aio_run_iocb() won't be there
> anymore.  function/f_fs.c take care of doing that, legacy/inode.c
> doesn't...
> 
> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
> Signed-off-by: Kamal Mostafa <kamal@canonical.com>
> ---
>  drivers/usb/gadget/inode.c | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
> index b94c049..ff3e6e6 100644
> --- a/drivers/usb/gadget/inode.c
> +++ b/drivers/usb/gadget/inode.c
> @@ -568,7 +568,6 @@ static ssize_t ep_copy_to_user(struct kiocb_priv *priv)
>  		if (total == 0)
>  			break;
>  	}
> -
>  	return len;
>  }
> 
> @@ -587,6 +586,7 @@ static void ep_user_copy_worker(struct work_struct *work)
>  	aio_complete(iocb, ret, ret);
> 
>  	kfree(priv->buf);
> +	kfree(priv->iv);
>  	kfree(priv);
>  }
> 
> @@ -607,6 +607,7 @@ static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
>  	 */
>  	if (priv->iv == NULL || unlikely(req->actual == 0)) {
>  		kfree(req->buf);
> +		kfree(priv->iv);
>  		kfree(priv);
>  		iocb->private = NULL;
>  		/* aio_complete() reports bytes-transferred _and_ faults */
> @@ -642,7 +643,7 @@ ep_aio_rwtail(
>  	struct usb_request	*req;
>  	ssize_t			value;
> 
> -	priv = kmalloc(sizeof *priv, GFP_KERNEL);
> +	priv = kzalloc(sizeof *priv, GFP_KERNEL);
>  	if (!priv) {
>  		value = -ENOMEM;
>  fail:
> @@ -651,7 +652,14 @@ fail:
>  	}
>  	iocb->private = priv;
>  	priv->iocb = iocb;
> -	priv->iv = iv;
> +	if (iv) {
> +		priv->iv = kmemdup(iv, nr_segs * sizeof(struct iovec),
> +				   GFP_KERNEL);
> +		if (!priv->iv) {
> +			kfree(priv);
> +			goto fail;
> +		}
> +	}


This is where 'value' may be used without being initialized, if
kmemdup() fails and the goto statement is executed: 'value' is
returned in the 'fail' label.

This issue seems to have been present upstream but this code was
completely refactored by commit 7fe3976e0f3a ("gadget: switch
ep_io_operations to ->read_iter/->write_iter") and the issue is gone.

Probably, the best thing to do is to add a 'value = -ENOMEM;'
immediately before the goto statment.

Cheers,
--
Luís

>  	priv->nr_segs = nr_segs;
>  	INIT_WORK(&priv->work, ep_user_copy_worker);
> 
> @@ -691,6 +699,7 @@ fail:
>  	mutex_unlock(&epdata->lock);
> 
>  	if (unlikely(value)) {
> +		kfree(priv->iv);
>  		kfree(priv);
>  		put_ep(epdata);
>  	} else
> --
> 1.9.1
> 
> 
> -- 
> kernel-team mailing list
> kernel-team@lists.ubuntu.com
> https://lists.ubuntu.com/mailman/listinfo/kernel-team
Kamal Mostafa April 7, 2015, 7:18 p.m. UTC | #2
On Tue, 2015-04-07 at 13:47 +0100, Luis Henriques wrote:
> On Mon, Apr 06, 2015 at 02:58:30PM -0700, Kamal Mostafa wrote:
> > This is a note to let you know that I have just added a patch titled
> > 
> >     gadgetfs: use-after-free in ->aio_read()
> > 

> This backport will add a compilation warning because the 'value'
> variable may be used uninitialized in function ep_aio_rwtail -- see
> below.

Yes, I see the possible uninit condition now.  Strangely I don't get
that warning on this file in my amd64  build (with any version of gcc).
[[ Noting that you mentioned in irc that you only get the warning when
cross-compiling with powerpc-linux-gnu-gcc (4.8.2). ]]

> Probably, the best thing to do is to add a 'value = -ENOMEM;'
> immediately before the goto statment.

Alas, even that's not sufficient.  For 3.13 at least, there's yet
another "goto fail" path which would end up leaking priv->iv too.

Given the mess, I'm just going to drop this patch from 3.13-stable
altogether, which appears to match other the stables < 3.18.

 -Kamal


> 
> > For more information about the 3.13.y-ckt tree, see
> > https://wiki.ubuntu.com/Kernel/Dev/ExtendedStable
> > 
> > Thanks.
> > -Kamal
> > 
> > ------
> > 
> > From 29e97f7298ed490b12f6c27e60b0607b4f06f081 Mon Sep 17 00:00:00 2001
> > From: Al Viro <viro@zeniv.linux.org.uk>
> > Date: Fri, 6 Feb 2015 02:07:45 -0500
> > Subject: gadgetfs: use-after-free in ->aio_read()
> > 
> > commit f01d35a15fa04162a58b95970fc01fa70ec9dacd upstream.
> > 
> > AIO_PREAD requests call ->aio_read() with iovec on caller's stack, so if
> > we are going to access it asynchronously, we'd better get ourselves
> > a copy - the one on kernel stack of aio_run_iocb() won't be there
> > anymore.  function/f_fs.c take care of doing that, legacy/inode.c
> > doesn't...
> > 
> > Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
> > Signed-off-by: Kamal Mostafa <kamal@canonical.com>
> > ---
> >  drivers/usb/gadget/inode.c | 15 ++++++++++++---
> >  1 file changed, 12 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
> > index b94c049..ff3e6e6 100644
> > --- a/drivers/usb/gadget/inode.c
> > +++ b/drivers/usb/gadget/inode.c
> > @@ -568,7 +568,6 @@ static ssize_t ep_copy_to_user(struct kiocb_priv *priv)
> >  		if (total == 0)
> >  			break;
> >  	}
> > -
> >  	return len;
> >  }
> > 
> > @@ -587,6 +586,7 @@ static void ep_user_copy_worker(struct work_struct *work)
> >  	aio_complete(iocb, ret, ret);
> > 
> >  	kfree(priv->buf);
> > +	kfree(priv->iv);
> >  	kfree(priv);
> >  }
> > 
> > @@ -607,6 +607,7 @@ static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
> >  	 */
> >  	if (priv->iv == NULL || unlikely(req->actual == 0)) {
> >  		kfree(req->buf);
> > +		kfree(priv->iv);
> >  		kfree(priv);
> >  		iocb->private = NULL;
> >  		/* aio_complete() reports bytes-transferred _and_ faults */
> > @@ -642,7 +643,7 @@ ep_aio_rwtail(
> >  	struct usb_request	*req;
> >  	ssize_t			value;
> > 
> > -	priv = kmalloc(sizeof *priv, GFP_KERNEL);
> > +	priv = kzalloc(sizeof *priv, GFP_KERNEL);
> >  	if (!priv) {
> >  		value = -ENOMEM;
> >  fail:
> > @@ -651,7 +652,14 @@ fail:
> >  	}
> >  	iocb->private = priv;
> >  	priv->iocb = iocb;
> > -	priv->iv = iv;
> > +	if (iv) {
> > +		priv->iv = kmemdup(iv, nr_segs * sizeof(struct iovec),
> > +				   GFP_KERNEL);
> > +		if (!priv->iv) {
> > +			kfree(priv);
> > +			goto fail;
> > +		}
> > +	}
> 
> 
> This is where 'value' may be used without being initialized, if
> kmemdup() fails and the goto statement is executed: 'value' is
> returned in the 'fail' label.
> 
> This issue seems to have been present upstream but this code was
> completely refactored by commit 7fe3976e0f3a ("gadget: switch
> ep_io_operations to ->read_iter/->write_iter") and the issue is gone.
> 
> Probably, the best thing to do is to add a 'value = -ENOMEM;'
> immediately before the goto statment.
> 
> Cheers,
> --
> Luís
> 
> >  	priv->nr_segs = nr_segs;
> >  	INIT_WORK(&priv->work, ep_user_copy_worker);
> > 
> > @@ -691,6 +699,7 @@ fail:
> >  	mutex_unlock(&epdata->lock);
> > 
> >  	if (unlikely(value)) {
> > +		kfree(priv->iv);
> >  		kfree(priv);
> >  		put_ep(epdata);
> >  	} else
> > --
> > 1.9.1
> > 
> > 
> > -- 
> > kernel-team mailing list
> > kernel-team@lists.ubuntu.com
> > https://lists.ubuntu.com/mailman/listinfo/kernel-team
>
diff mbox

Patch

diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index b94c049..ff3e6e6 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -568,7 +568,6 @@  static ssize_t ep_copy_to_user(struct kiocb_priv *priv)
 		if (total == 0)
 			break;
 	}
-
 	return len;
 }

@@ -587,6 +586,7 @@  static void ep_user_copy_worker(struct work_struct *work)
 	aio_complete(iocb, ret, ret);

 	kfree(priv->buf);
+	kfree(priv->iv);
 	kfree(priv);
 }

@@ -607,6 +607,7 @@  static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
 	 */
 	if (priv->iv == NULL || unlikely(req->actual == 0)) {
 		kfree(req->buf);
+		kfree(priv->iv);
 		kfree(priv);
 		iocb->private = NULL;
 		/* aio_complete() reports bytes-transferred _and_ faults */
@@ -642,7 +643,7 @@  ep_aio_rwtail(
 	struct usb_request	*req;
 	ssize_t			value;

-	priv = kmalloc(sizeof *priv, GFP_KERNEL);
+	priv = kzalloc(sizeof *priv, GFP_KERNEL);
 	if (!priv) {
 		value = -ENOMEM;
 fail:
@@ -651,7 +652,14 @@  fail:
 	}
 	iocb->private = priv;
 	priv->iocb = iocb;
-	priv->iv = iv;
+	if (iv) {
+		priv->iv = kmemdup(iv, nr_segs * sizeof(struct iovec),
+				   GFP_KERNEL);
+		if (!priv->iv) {
+			kfree(priv);
+			goto fail;
+		}
+	}
 	priv->nr_segs = nr_segs;
 	INIT_WORK(&priv->work, ep_user_copy_worker);

@@ -691,6 +699,7 @@  fail:
 	mutex_unlock(&epdata->lock);

 	if (unlikely(value)) {
+		kfree(priv->iv);
 		kfree(priv);
 		put_ep(epdata);
 	} else