Skip to contentSkip to conversation

fix resource leak in pnp card_probe()

PATCH RESEND In review Jesper Juhl 3 participants 5 messages May 13, 2006 – May 14, 2006 Pnp support

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 fix resource leak in pnp card_probe()

(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()

---

 drivers/pnp/card.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletion(-)
drivers/pnp/card.c +3 -1
@@ -81,8 +81,10 @@ static int card_probe(struct pnp_card * 
 				}
 				kfree(clink);
 			}
-		} else
+		} else {
+			kfree(clink);
 			return 1;
+		}
 	}
 	return 0;
 }
#1 lore raw
Quoted 18 lines · Jesper Juhl

(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()

---

 drivers/pnp/card.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletion(-)
drivers/pnp/card.c +0 -0
@@ -81,8 +81,10 @@ static int card_probe(struct pnp_card * 
 				}

drivers/pnp/card.c static int card_probe(struct pnp_card * · line 90

Quoted 10 lines
drivers/pnp/card.c +3 -1
 				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?

drivers/pnp/card.c renamed from drivers/pnp/card.c~pnp-card_probe-fix-memory-leak +26 -22
@@ -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;
 }
 

_

#2 lore raw
Quoted 41 lines · 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?

drivers/pnp/card.c renamed from drivers/pnp/card.c~pnp-card_probe-fix-memory-leak +7 -22
@@ -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;

drivers/pnp/card.c static void card_remove_first(struct pnp · line 80

Quoted 10 lines
drivers/pnp/card.c renamed from drivers/pnp/card.c~pnp-card_probe-fix-memory-leak +10 -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.

Quoted 12 lines
drivers/pnp/card.c renamed from drivers/pnp/card.c~pnp-card_probe-fix-memory-leak +9 -0
+
+	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;
 }
#3 lore raw
Quoted 10 lines · Keith Owens

drivers/pnp/card.c static void card_remove_first(struct pnp · line 80

Quoted 7 lines
drivers/pnp/card.c renamed from drivers/pnp/card.c~pnp-card_probe-fix-memory-leak +6 -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.

Quoted 12 lines
Quoted 10 lines
drivers/pnp/card.c renamed from drivers/pnp/card.c~pnp-card_probe-fix-memory-leak +8 -0
+
+	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);
	}
drivers/pnp/card.c renamed from drivers/pnp/card.c~pnp-card_probe-fix-memory-leak +1 -0
+	kfree(clink);
Quoted 2 lines
	return 0;
}

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

It's all rather twisty and quite undocumented.

#4 lore raw
In reply to Andrew Morton
Quoted 8 lines · Andrew Morton
Quoted 7 lines · Jesper Juhl

(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]

Quoted 4 lines

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.

Quoted 2 lines

Like this?

Looks good to me, thanks.

[snip neater version of fix]

Signature · 3 lines
Jesper Juhl <[email protected]>
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