caif: add error handling for allocation
Subsystems (3)
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
@@ -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);
Hi Dan,
...
Quoted 7 lines
    switch (
phy_type) {
    caseCFPHYTYPE_FRAG:
        phy_driver >           cfserl_create(CFPHYTYPE_FRAG, phyid, stx);         if (!phy_driver)
- Â Â Â Â Â Â Â Â Â Â Â goto out;
+ Â Â Â Â Â Â Â Â Â Â Â gotoout_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
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.
--- net/caif/caif_dev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
net/caif/caif_dev.c +5 -1
@@ -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;
Re: [PATCH] caif: fix a potential NULL dereference
Quoted 6 lines · Eric Dumazet
Commit
bd30ce4bc0b7(caif: Use RCU instead of spin-lock incaif_dev.c) added a potential NULL dereference in casealloc_percpu()fails.
caif_device_alloc()can also useGFP_KERNELinstead ofGFP_ATOMIC.Signed-off-by Eric Dumazet <[email protected]>
Looks good thanks,
Quoted 6 lines · Sjur Brændeland
Thank you for your patch.
When reviewing this I found another potential memory leak as well. Ifcffrml_createfails, we might be leaking thephy_driver. So perhaps you could dokfree(phy_driver)inout_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
Re: [PATCH] caif: fix a potential NULL dereference
In reply to Eric DumazetFrom: 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 incaif_dev.c) added a potential NULL dereference in casealloc_percpu()fails.
caif_device_alloc()can also useGFP_KERNELinstead ofGFP_ATOMIC.Signed-off-by Eric Dumazet <[email protected]>CC Sjur Brændeland <[email protected]>
Applied.
PATCH v2 1/1 caif: add error handling for allocation
In reply to Sjur BrændelandThe 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
@@ -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
[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.Signed-off-by Dan Carpenter <[email protected]>
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
David Miller
maintainerFrom: 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.Signed-off-by Dan Carpenter <[email protected]>Looks good, thank you Dan.
Acked-by Sjur Brændeland <[email protected]>
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