Skip to contentSkip to conversation

caif: add error handling for allocation

PATCH In review Dan Carpenter 6 participants 9 messages Sep 2, 2011 – Oct 3, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 caif: add error handling for allocation

The allocation could fail so we should check, or other errors could happen and we should free the "phyinfo" variable.

net/caif/cfcnfg.c +13 -9
index f07ab8c..b213b53 100644
@@ -467,7 +467,7 @@ cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
 {
 	struct cflayer *frml;
 	struct cflayer *phy_driver = NULL;
-	struct cfcnfg_phyinfo *phyinfo;
+	struct cfcnfg_phyinfo *phyinfo = NULL;
 	int i;
 	u8 phyid;
 
@@ -482,23 +482,25 @@ cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
 			goto got_phyid;
 	}
 	pr_warn("Too many CAIF Link Layers (max 6)\n");
-	goto out;
+	goto out_err;
 
 got_phyid:
 	phyinfo = kzalloc(sizeof(struct cfcnfg_phyinfo), GFP_ATOMIC);
+	if (!phyinfo)
+		goto out_err;
 
 	switch (phy_type) {
 	case CFPHYTYPE_FRAG:
 		phy_driver  		    cfserl_create(CFPHYTYPE_FRAG, phyid, stx);
 		if (!phy_driver)
-			goto out;
+			goto out_err;
 		break;
 	case CFPHYTYPE_CAIF:
 		phy_driver = NULL;
 		break;
 	default:
-		goto out;
+		goto out_err;
 	}
 	phy_layer->id = phyid;
 	phyinfo->pref = pref;
@@ -512,10 +514,8 @@ got_phyid:
 
 	frml = cffrml_create(phyid, fcs);
 
-	if (!frml) {
-		kfree(phyinfo);
-		goto out;
-	}
+	if (!frml)
+		goto out_err;
 	phyinfo->frm_layer = frml;
 	layer_set_up(frml, cnfg->mux);
 
@@ -531,7 +531,11 @@ got_phyid:
 	}
 
 	list_add_rcu(&phyinfo->node, &cnfg->phys);
-out:
+	mutex_unlock(&cnfg->lock);
+	return;
+
+out_err:
+	kfree(phyinfo);
 	mutex_unlock(&cnfg->lock);
 }
 EXPORT_SYMBOL(cfcnfg_add_phy_layer);
#1 lore raw

Hi Dan,
...

Quoted 7 lines

       switch (phy_type) {
       case CFPHYTYPE_FRAG:
               phy_driver >                    cfserl_create(CFPHYTYPE_FRAG, phyid, stx);                if (!phy_driver)
-                       goto out;
+                       goto out_err;
               break;

...

Quoted 7 lines
net/caif/cfcnfg.c +5 -1
-out:
+       mutex_unlock(&cnfg->lock);
+       return;
+
+out_err:
+       kfree(phyinfo);

       mutex_unlock(&cnfg->lock);

Thank you for your patch.
When reviewing this I found another potential memory leak as well. If cffrml_create fails, we might be leaking the phy_driver. So perhaps you could do kfree(phy_driver) in out_err: as well, while you are at it?

Regards,
Sjur ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ¥Šwÿº{.nÇ+‰·¤z¹Ş—øÚ�+h®Ïâ�Ø^n‡r¡ö¦zË�ëh™¨è­Ú&£ûàz¿äz¹Ş—ú+€Ê+zf£¢·hšˆ§~†­†Ûiÿÿï�êÿ‘êçz_è®æj:+v‰¨ş)ߣøm

#2 lore raw

PATCH 1/1 caif: fix a potential NULL dereference

Commit bd30ce4bc0b7 (caif: Use RCU instead of spin-lock in caif_dev.c) added a potential NULL dereference in case alloc_percpu() fails.

caif_device_alloc() can also use GFP_KERNEL instead of GFP_ATOMIC.

CC Sjur Brændeland <[email protected]>
---
 net/caif/caif_dev.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/caif/caif_dev.c +5 -1
index 7c2fa0a..7f9ac07 100644
@@ -93,10 +93,14 @@ static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
 	caifdevs = caif_device_list(dev_net(dev));
 	BUG_ON(!caifdevs);
 
-	caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
+	caifd = kzalloc(sizeof(*caifd), GFP_KERNEL);
 	if (!caifd)
 		return NULL;
 	caifd->pcpu_refcnt = alloc_percpu(int);
+	if (!caifd->pcpu_refcnt) {
+		kfree(caifd);
+		return NULL;
+	}
 	caifd->netdev = dev;
 	dev_hold(dev);
 	return caifd;
#5 lore raw
In reply to Sjur Brændeland
Quoted 6 lines · Sjur Brændeland

Thank you for your patch.
When reviewing this I found another potential memory leak as well. If cffrml_create fails, we might be leaking the phy_driver. So perhaps you could do kfree(phy_driver) in out_err: as well, while you are at it?

Good point. A kfree(phy_driver) would fix the leak. But why does cfserl_create() return &this->layer; instead of just "return this;" Their equivalent now, but if you change the cfserl struct it will break the kfree().

I'll be travelling for a while, so I may be out of reach until Wednessday.

regards,
dan carpenter

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer Applied
#4 lore raw

Re: [PATCH] caif: fix a potential NULL dereference

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>
Date: Fri, 02 Sep 2011 14:19:23 +0200

Quoted 7 lines

Commit bd30ce4bc0b7 (caif: Use RCU instead of spin-lock in caif_dev.c) added a potential NULL dereference in case alloc_percpu() fails.

caif_device_alloc() can also use GFP_KERNEL instead of GFP_ATOMIC.

CC Sjur Brændeland <[email protected]>

Applied.

#6 lore raw

PATCH v2 1/1 caif: add error handling for allocation

In reply to Sjur Brændeland

The allocation of "phyinfo" wasn't checked, and also the allocation wasn't freed on error paths. Sjur Brændeland pointed out as well that "phy_driver" should be freed on the error path too.

---
V2: Add a kfree(phy_driver).

net/caif/cfcnfg.c +14 -9
index f07ab8c..00523ec 100644
@@ -467,7 +467,7 @@ cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
 {
 	struct cflayer *frml;
 	struct cflayer *phy_driver = NULL;
-	struct cfcnfg_phyinfo *phyinfo;
+	struct cfcnfg_phyinfo *phyinfo = NULL;
 	int i;
 	u8 phyid;
 
@@ -482,23 +482,25 @@ cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
 			goto got_phyid;
 	}
 	pr_warn("Too many CAIF Link Layers (max 6)\n");
-	goto out;
+	goto out_err;
 
 got_phyid:
 	phyinfo = kzalloc(sizeof(struct cfcnfg_phyinfo), GFP_ATOMIC);
+	if (!phyinfo)
+		goto out_err;
 
 	switch (phy_type) {
 	case CFPHYTYPE_FRAG:
 		phy_driver  		    cfserl_create(CFPHYTYPE_FRAG, phyid, stx);
 		if (!phy_driver)
-			goto out;
+			goto out_err;
 		break;
 	case CFPHYTYPE_CAIF:
 		phy_driver = NULL;
 		break;
 	default:
-		goto out;
+		goto out_err;
 	}
 	phy_layer->id = phyid;
 	phyinfo->pref = pref;
@@ -512,10 +514,8 @@ got_phyid:
 
 	frml = cffrml_create(phyid, fcs);
 
-	if (!frml) {
-		kfree(phyinfo);
-		goto out;
-	}
+	if (!frml)
+		goto out_err;
 	phyinfo->frm_layer = frml;
 	layer_set_up(frml, cnfg->mux);
 
@@ -531,7 +531,12 @@ got_phyid:
 	}
 
 	list_add_rcu(&phyinfo->node, &cnfg->phys);
-out:
+	mutex_unlock(&cnfg->lock);
+	return;
+
+out_err:
+	kfree(phy_driver);
+	kfree(phyinfo);
 	mutex_unlock(&cnfg->lock);
 }
 EXPORT_SYMBOL(cfcnfg_add_phy_layer);

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Acked-by on v2 1/1
#7 lore raw

[Dan]:

Quoted 5 lines

The allocation of "phyinfo" wasn't checked, and also the allocation wasn't freed on error paths. Sjur Brændeland pointed out as well that "phy_driver" should be freed on the error path too.

Looks good, thank you Dan.

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#8 lore raw

From: Sjur BRENDELAND <[email protected]>
Date: Fri, 23 Sep 2011 12:38:45 +0200

Quoted 9 lines

[Dan]:

Quoted 5 lines

The allocation of "phyinfo" wasn't checked, and also the allocation wasn't freed on error paths. Sjur Brændeland pointed out as well that "phy_driver" should be freed on the error path too.

Looks good, thank you Dan.

Applied, thanks.
--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html