mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH][resend] fix resource leak in pnp card_probe()
@ 2006-05-13 20:35 Jesper Juhl
  2006-05-14  9:38 ` Andrew Morton
  0 siblings, 1 reply; 5+ messages in thread
From: Jesper Juhl @ 2006-05-13 20:35 UTC (permalink / raw)
  To: linux-kernel; +Cc: Adam Belay, Jesper Juhl, Andrew Morton

(resend of patch already send once on 23/03-2006 
  - still applies cleanly to latest -git)


We can leak `clink' in drivers/pnp/card.c::card_probe()


Signed-off-by: Jesper Juhl <jesper.juhl@gmail.com>
---

 drivers/pnp/card.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletion(-)

--- linux-2.6.16-mm1-orig/drivers/pnp/card.c	2006-03-26 13:43:38.000000000 +0200
+++ linux-2.6.16-mm1/drivers/pnp/card.c	2006-03-26 15:45:00.000000000 +0200
@@ -81,8 +81,10 @@ static int card_probe(struct pnp_card * 
 				}
 				kfree(clink);
 			}
-		} else
+		} else {
+			kfree(clink);
 			return 1;
+		}
 	}
 	return 0;
 }




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

* Re: [PATCH][resend] fix resource leak in pnp card_probe()
  2006-05-13 20:35 [PATCH][resend] fix resource leak in pnp card_probe() Jesper Juhl
@ 2006-05-14  9:38 ` Andrew Morton
  2006-05-14  9:48   ` Keith Owens
  2006-05-14 10:42   ` Jesper Juhl
  0 siblings, 2 replies; 5+ messages in thread
From: Andrew Morton @ 2006-05-14  9:38 UTC (permalink / raw)
  To: Jesper Juhl; +Cc: linux-kernel, ambx1, jesper.juhl

Jesper Juhl <jesper.juhl@gmail.com> wrote:
>
> (resend of patch already send once on 23/03-2006 
>   - still applies cleanly to latest -git)
> 
> 
> We can leak `clink' in drivers/pnp/card.c::card_probe()
> 
> 
> Signed-off-by: Jesper Juhl <jesper.juhl@gmail.com>
> ---
> 
>  drivers/pnp/card.c |    4 +++-
>  1 files changed, 3 insertions(+), 1 deletion(-)
> 
> --- linux-2.6.16-mm1-orig/drivers/pnp/card.c	2006-03-26 13:43:38.000000000 +0200
> +++ linux-2.6.16-mm1/drivers/pnp/card.c	2006-03-26 15:45:00.000000000 +0200
> @@ -81,8 +81,10 @@ static int card_probe(struct pnp_card * 
>  				}
>  				kfree(clink);
>  			}
> -		} else
> +		} else {
> +			kfree(clink);
>  			return 1;
> +		}
>  	}
>  	return 0;
>  }

If !drv->probe then there's not much point in doing the kmalloc and then
immediately freeing it again.

Like this?

--- devel/drivers/pnp/card.c~pnp-card_probe-fix-memory-leak	2006-05-14 02:30:25.000000000 -0700
+++ devel-akpm/drivers/pnp/card.c	2006-05-14 02:36:24.000000000 -0700
@@ -60,30 +60,34 @@ static void card_remove_first(struct pnp
 	card_remove(dev);
 }
 
-static int card_probe(struct pnp_card * card, struct pnp_card_driver * drv)
+static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
 {
-	const struct pnp_card_device_id *id = match_card(drv,card);
-	if (id) {
-		struct pnp_card_link * clink = pnp_alloc(sizeof(struct pnp_card_link));
-		if (!clink)
-			return 0;
-		clink->card = card;
-		clink->driver = drv;
-		clink->pm_state = PMSG_ON;
-		if (drv->probe) {
-			if (drv->probe(clink, id)>=0)
-				return 1;
-			else {
-				struct pnp_dev * dev;
-				card_for_each_dev(card, dev) {
-					if (dev->card_link == clink)
-						pnp_release_card_device(dev);
-				}
-				kfree(clink);
-			}
-		} else
-			return 1;
+	const struct pnp_card_device_id *id;
+	struct pnp_card_link *clink;
+	struct pnp_dev *dev;
+
+	if (!drv->probe)
+		return 0;
+	id = match_card(drv,card);
+	if (!id)
+		return 0;
+
+	clink = pnp_alloc(sizeof(*clink));
+	if (!clink)
+		return 0;
+	clink->card = card;
+	clink->driver = drv;
+	clink->pm_state = PMSG_ON;
+
+	if (drv->probe(clink, id) >= 0)
+		return 1;
+
+	/* Recovery */
+	card_for_each_dev(card, dev) {
+		if (dev->card_link == clink)
+			pnp_release_card_device(dev);
 	}
+	kfree(clink);
 	return 0;
 }
 
_


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

* Re: [PATCH][resend] fix resource leak in pnp card_probe()
  2006-05-14  9:38 ` Andrew Morton
@ 2006-05-14  9:48   ` Keith Owens
  2006-05-14  9:58     ` Andrew Morton
  2006-05-14 10:42   ` Jesper Juhl
  1 sibling, 1 reply; 5+ messages in thread
From: Keith Owens @ 2006-05-14  9:48 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Jesper Juhl, linux-kernel, ambx1

Andrew Morton (on Sun, 14 May 2006 02:38:33 -0700) wrote:
>If !drv->probe then there's not much point in doing the kmalloc and then
>immediately freeing it again.
>
>Like this?
>
>--- devel/drivers/pnp/card.c~pnp-card_probe-fix-memory-leak	2006-05-14 02:30:25.000000000 -0700
>+++ devel-akpm/drivers/pnp/card.c	2006-05-14 02:36:24.000000000 -0700
>@@ -60,30 +60,34 @@ static void card_remove_first(struct pnp
> 	card_remove(dev);
> }
> 
>-static int card_probe(struct pnp_card * card, struct pnp_card_driver * drv)
>+static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
> {
>-	const struct pnp_card_device_id *id = match_card(drv,card);
>-	if (id) {
>-		struct pnp_card_link * clink = pnp_alloc(sizeof(struct pnp_card_link));
>-		if (!clink)
>-			return 0;
>-		clink->card = card;
>-		clink->driver = drv;
>-		clink->pm_state = PMSG_ON;
>-		if (drv->probe) {
>-			if (drv->probe(clink, id)>=0)
>-				return 1;
>-			else {
>-				struct pnp_dev * dev;
>-				card_for_each_dev(card, dev) {
>-					if (dev->card_link == clink)
>-						pnp_release_card_device(dev);
>-				}
>-				kfree(clink);
>-			}
>-		} else
>-			return 1;
>+	const struct pnp_card_device_id *id;
>+	struct pnp_card_link *clink;
>+	struct pnp_dev *dev;
>+
>+	if (!drv->probe)
>+		return 0;
>+	id = match_card(drv,card);
>+	if (!id)
>+		return 0;
>+
>+	clink = pnp_alloc(sizeof(*clink));
>+	if (!clink)
>+		return 0;
>+	clink->card = card;
>+	clink->driver = drv;
>+	clink->pm_state = PMSG_ON;

Memory leak of clink on next test.

>+
>+	if (drv->probe(clink, id) >= 0)
>+		return 1;
>+
>+	/* Recovery */
>+	card_for_each_dev(card, dev) {
>+		if (dev->card_link == clink)
>+			pnp_release_card_device(dev);
> 	}
>+	kfree(clink);
> 	return 0;
> }


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

* Re: [PATCH][resend] fix resource leak in pnp card_probe()
  2006-05-14  9:48   ` Keith Owens
@ 2006-05-14  9:58     ` Andrew Morton
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2006-05-14  9:58 UTC (permalink / raw)
  To: Keith Owens; +Cc: jesper.juhl, linux-kernel, ambx1

Keith Owens <kaos@ocs.com.au> wrote:
>
> >+	clink = pnp_alloc(sizeof(*clink));
>  >+	if (!clink)
>  >+		return 0;
>  >+	clink->card = card;
>  >+	clink->driver = drv;
>  >+	clink->pm_state = PMSG_ON;
> 
>  Memory leak of clink on next test.
> 
>  >+
>  >+	if (drv->probe(clink, id) >= 0)
>  >+		return 1;
>  >+
>  >+	/* Recovery */
>  >+	card_for_each_dev(card, dev) {
>  >+		if (dev->card_link == clink)
>  >+			pnp_release_card_device(dev);
>  > 	}
>  >+	kfree(clink);
>  > 	return 0;
>  > }

No, if ->probe succeeded, it took over control of the memory at *clink.

It's all rather twisty and quite undocumented.

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

* Re: [PATCH][resend] fix resource leak in pnp card_probe()
  2006-05-14  9:38 ` Andrew Morton
  2006-05-14  9:48   ` Keith Owens
@ 2006-05-14 10:42   ` Jesper Juhl
  1 sibling, 0 replies; 5+ messages in thread
From: Jesper Juhl @ 2006-05-14 10:42 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, ambx1

On 14/05/06, Andrew Morton <akpm@osdl.org> wrote:
> Jesper Juhl <jesper.juhl@gmail.com> wrote:
> >
> > (resend of patch already send once on 23/03-2006
> >   - still applies cleanly to latest -git)
> >
> >
> > We can leak `clink' in drivers/pnp/card.c::card_probe()
> >
[snip]
>
> If !drv->probe then there's not much point in doing the kmalloc and then
> immediately freeing it again.
>
True. It was simply the simplest and least intrusive fix I could make.

> Like this?
>
Looks good to me, thanks.

[snip neater version of fix]


-- 
Jesper Juhl <jesper.juhl@gmail.com>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html

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

end of thread, other threads:[~2006-05-14 10:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-13 20:35 [PATCH][resend] fix resource leak in pnp card_probe() Jesper Juhl
2006-05-14  9:38 ` Andrew Morton
2006-05-14  9:48   ` Keith Owens
2006-05-14  9:58     ` Andrew Morton
2006-05-14 10:42   ` Jesper Juhl

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®