Commit d4274b51 authored by Panagiotis Issaris's avatar Panagiotis Issaris Committed by David S. Miller

[PPP]: handle kmalloc failures and convert to using kzalloc

The PPP code contains two kmalloc()s followed by memset()s without
handling a possible memory allocation failure.  (Suggested by Joe
Perches).

And furthermore, conversions from kmalloc+memset to kzalloc.

[akpm@osdl.org: fix error-path leak]
[akpm@osdl.org: cleanups]
[paulus@samba.org: don't add useless printk and cardmap_destroy calls]
Signed-off-by: default avatarPanagiotis Issaris <takis@issaris.org>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarPaul Mackerras <paulus@samba.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent c0956bd2
...@@ -192,7 +192,7 @@ struct cardmap { ...@@ -192,7 +192,7 @@ struct cardmap {
void *ptr[CARDMAP_WIDTH]; void *ptr[CARDMAP_WIDTH];
}; };
static void *cardmap_get(struct cardmap *map, unsigned int nr); static void *cardmap_get(struct cardmap *map, unsigned int nr);
static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr); static int cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
static unsigned int cardmap_find_first_free(struct cardmap *map); static unsigned int cardmap_find_first_free(struct cardmap *map);
static void cardmap_destroy(struct cardmap **map); static void cardmap_destroy(struct cardmap **map);
...@@ -1995,10 +1995,9 @@ ppp_register_channel(struct ppp_channel *chan) ...@@ -1995,10 +1995,9 @@ ppp_register_channel(struct ppp_channel *chan)
{ {
struct channel *pch; struct channel *pch;
pch = kmalloc(sizeof(struct channel), GFP_KERNEL); pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
if (pch == 0) if (pch == 0)
return -ENOMEM; return -ENOMEM;
memset(pch, 0, sizeof(struct channel));
pch->ppp = NULL; pch->ppp = NULL;
pch->chan = chan; pch->chan = chan;
chan->ppp = pch; chan->ppp = pch;
...@@ -2408,13 +2407,12 @@ ppp_create_interface(int unit, int *retp) ...@@ -2408,13 +2407,12 @@ ppp_create_interface(int unit, int *retp)
int ret = -ENOMEM; int ret = -ENOMEM;
int i; int i;
ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL); ppp = kzalloc(sizeof(struct ppp), GFP_KERNEL);
if (!ppp) if (!ppp)
goto out; goto out;
dev = alloc_netdev(0, "", ppp_setup); dev = alloc_netdev(0, "", ppp_setup);
if (!dev) if (!dev)
goto out1; goto out1;
memset(ppp, 0, sizeof(struct ppp));
ppp->mru = PPP_MRU; ppp->mru = PPP_MRU;
init_ppp_file(&ppp->file, INTERFACE); init_ppp_file(&ppp->file, INTERFACE);
...@@ -2454,11 +2452,16 @@ ppp_create_interface(int unit, int *retp) ...@@ -2454,11 +2452,16 @@ ppp_create_interface(int unit, int *retp)
} }
atomic_inc(&ppp_unit_count); atomic_inc(&ppp_unit_count);
cardmap_set(&all_ppp_units, unit, ppp); ret = cardmap_set(&all_ppp_units, unit, ppp);
if (ret != 0)
goto out3;
mutex_unlock(&all_ppp_mutex); mutex_unlock(&all_ppp_mutex);
*retp = 0; *retp = 0;
return ppp; return ppp;
out3:
atomic_dec(&ppp_unit_count);
out2: out2:
mutex_unlock(&all_ppp_mutex); mutex_unlock(&all_ppp_mutex);
free_netdev(dev); free_netdev(dev);
...@@ -2695,7 +2698,7 @@ static void *cardmap_get(struct cardmap *map, unsigned int nr) ...@@ -2695,7 +2698,7 @@ static void *cardmap_get(struct cardmap *map, unsigned int nr)
return NULL; return NULL;
} }
static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr) static int cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
{ {
struct cardmap *p; struct cardmap *p;
int i; int i;
...@@ -2704,8 +2707,9 @@ static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr) ...@@ -2704,8 +2707,9 @@ static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) { if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
do { do {
/* need a new top level */ /* need a new top level */
struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL); struct cardmap *np = kzalloc(sizeof(*np), GFP_KERNEL);
memset(np, 0, sizeof(*np)); if (!np)
goto enomem;
np->ptr[0] = p; np->ptr[0] = p;
if (p != NULL) { if (p != NULL) {
np->shift = p->shift + CARDMAP_ORDER; np->shift = p->shift + CARDMAP_ORDER;
...@@ -2719,8 +2723,9 @@ static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr) ...@@ -2719,8 +2723,9 @@ static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
while (p->shift > 0) { while (p->shift > 0) {
i = (nr >> p->shift) & CARDMAP_MASK; i = (nr >> p->shift) & CARDMAP_MASK;
if (p->ptr[i] == NULL) { if (p->ptr[i] == NULL) {
struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL); struct cardmap *np = kzalloc(sizeof(*np), GFP_KERNEL);
memset(np, 0, sizeof(*np)); if (!np)
goto enomem;
np->shift = p->shift - CARDMAP_ORDER; np->shift = p->shift - CARDMAP_ORDER;
np->parent = p; np->parent = p;
p->ptr[i] = np; p->ptr[i] = np;
...@@ -2735,6 +2740,9 @@ static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr) ...@@ -2735,6 +2740,9 @@ static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
set_bit(i, &p->inuse); set_bit(i, &p->inuse);
else else
clear_bit(i, &p->inuse); clear_bit(i, &p->inuse);
return 0;
enomem:
return -ENOMEM;
} }
static unsigned int cardmap_find_first_free(struct cardmap *map) static unsigned int cardmap_find_first_free(struct cardmap *map)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment