mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drivers/usb/gadget/inode.c: add missing kfree
@ 2011-06-02  8:26 Julia Lawall
  2011-06-02 14:53 ` Alan Stern
  0 siblings, 1 reply; 4+ messages in thread
From: Julia Lawall @ 2011-06-02  8:26 UTC (permalink / raw)
  To: David Brownell
  Cc: kernel-janitors, Greg Kroah-Hartman, linux-usb, linux-kernel

From: Julia Lawall <julia@diku.dk>

The label fail frees dev->buf, but kbuf hasn't yet been stored there at
this point.

A simplified version of the semantic match that finds this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@exists@
local idexpression x;
statement S;
expression E;
expression *ptr != NULL;
@@

x = memdup_user(...);
...
if (IS_ERR(x)) S
... when != x
    when != x = E
(
 return \(0\|<+...x...+>\|ptr\);
|
*return ...;
)
// </smpl>

Signed-off-by: Julia Lawall <julia@diku.dk>

---
I wonder if it is really correct to free dev->buf at fail in this case.
Because it is freeing the previously allocated value of dev->buf, not the
current one as done in subsequent cases.

 drivers/usb/gadget/inode.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index a01383f..316547a 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -1870,8 +1870,10 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
 
 	spin_lock_irq (&dev->lock);
 	value = -EINVAL;
-	if (dev->buf)
+	if (dev->buf) {
+		kfree(kbuf);
 		goto fail;
+	}
 	dev->buf = kbuf;
 
 	/* full or low speed config */


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drivers/usb/gadget/inode.c: add missing kfree
  2011-06-02  8:26 [PATCH] drivers/usb/gadget/inode.c: add missing kfree Julia Lawall
@ 2011-06-02 14:53 ` Alan Stern
  2011-06-02 14:56   ` Julia Lawall
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Stern @ 2011-06-02 14:53 UTC (permalink / raw)
  To: Julia Lawall
  Cc: David Brownell, kernel-janitors, Greg Kroah-Hartman, linux-usb,
	linux-kernel

On Thu, 2 Jun 2011, Julia Lawall wrote:

> From: Julia Lawall <julia@diku.dk>
> 
> The label fail frees dev->buf, but kbuf hasn't yet been stored there at
> this point.

...

> Signed-off-by: Julia Lawall <julia@diku.dk>
> 
> ---
> I wonder if it is really correct to free dev->buf at fail in this case.
> Because it is freeing the previously allocated value of dev->buf, not the
> current one as done in subsequent cases.

I don't think it matters.  This check is probably there only for 
safety; it should not be possible for dev->buf to be non-NULL at this 
point.  You ought to be able to remove the entire

	if (dev->buf)
		goto fail;

statement with no ill effects.

Alan Stern


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drivers/usb/gadget/inode.c: add missing kfree
  2011-06-02 14:53 ` Alan Stern
@ 2011-06-02 14:56   ` Julia Lawall
  2011-06-02 15:17     ` Alan Stern
  0 siblings, 1 reply; 4+ messages in thread
From: Julia Lawall @ 2011-06-02 14:56 UTC (permalink / raw)
  To: Alan Stern
  Cc: David Brownell, kernel-janitors, Greg Kroah-Hartman, linux-usb,
	linux-kernel

On Thu, 2 Jun 2011, Alan Stern wrote:

> On Thu, 2 Jun 2011, Julia Lawall wrote:
> 
> > From: Julia Lawall <julia@diku.dk>
> > 
> > The label fail frees dev->buf, but kbuf hasn't yet been stored there at
> > this point.
> 
> ...
> 
> > Signed-off-by: Julia Lawall <julia@diku.dk>
> > 
> > ---
> > I wonder if it is really correct to free dev->buf at fail in this case.
> > Because it is freeing the previously allocated value of dev->buf, not the
> > current one as done in subsequent cases.
> 
> I don't think it matters.  This check is probably there only for 
> safety; it should not be possible for dev->buf to be non-NULL at this 
> point.  You ought to be able to remove the entire
> 
> 	if (dev->buf)
> 		goto fail;
> 
> statement with no ill effects.

I can send such a patch if you think it is OK.  But I have no way to test 
it.

julia

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drivers/usb/gadget/inode.c: add missing kfree
  2011-06-02 14:56   ` Julia Lawall
@ 2011-06-02 15:17     ` Alan Stern
  0 siblings, 0 replies; 4+ messages in thread
From: Alan Stern @ 2011-06-02 15:17 UTC (permalink / raw)
  To: Julia Lawall
  Cc: David Brownell, kernel-janitors, Greg Kroah-Hartman, linux-usb,
	linux-kernel

On Thu, 2 Jun 2011, Julia Lawall wrote:

> > I don't think it matters.  This check is probably there only for 
> > safety; it should not be possible for dev->buf to be non-NULL at this 
> > point.  You ought to be able to remove the entire
> > 
> > 	if (dev->buf)
> > 		goto fail;
> > 
> > statement with no ill effects.
> 
> I can send such a patch if you think it is OK.  But I have no way to test 
> it.

I don't either, since as far as I know the "if" condition can never be 
true.  You may be able to verify this just by reading the code.  Note 
that dev_config is used only as a field in dev_init_operations, which 
is used only in the gadgetfs_fill_super routine and therefore only when 
a file in this filesystem is opened.

Note also that the only time dev->buf becomes non-NULL is when
dev_config() succeeds, at which point fd->f_op is switched from
dev_init_operations to ep0_io_operations.  Therefore dev_config()
cannot be called successfully more than once for the same open file.

Alan Stern


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-06-02 15:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-02  8:26 [PATCH] drivers/usb/gadget/inode.c: add missing kfree Julia Lawall
2011-06-02 14:53 ` Alan Stern
2011-06-02 14:56   ` Julia Lawall
2011-06-02 15:17     ` Alan Stern

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®