Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1307160 > unrolled thread

[patch] mdio_bus: NULL dereference on allocation error

Started byDan Carpenter <dan.carpenter@oracle.com>
First post2016-01-12 10:40 +0100
Last post2016-01-12 20:40 +0100
Articles 3 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [patch] mdio_bus: NULL dereference on allocation error Dan Carpenter <dan.carpenter@oracle.com> - 2016-01-12 10:40 +0100
    Re: [patch] mdio_bus: NULL dereference on allocation error Andrew Lunn <andrew@lunn.ch> - 2016-01-12 15:10 +0100
    Re: [patch] mdio_bus: NULL dereference on allocation error David Miller <davem@davemloft.net> - 2016-01-12 20:40 +0100

#1307160 — [patch] mdio_bus: NULL dereference on allocation error

FromDan Carpenter <dan.carpenter@oracle.com>
Date2016-01-12 10:40 +0100
Subject[patch] mdio_bus: NULL dereference on allocation error
Message-ID<qQ3AL-7rn-31@gated-at.bofh.it>
If bus = kzalloc() fails then we end up dereferencing bus when we do
"bus->irq[i] = PHY_POLL;".  The code is a little simpler if we reverse
the NULL check and return directly on failure.

Fixes: e7f4dc3536a4 ('mdio: Move allocation of interrupts into core')
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
index 0be7b3d..0cba64f 100644
--- a/drivers/net/phy/mdio_bus.c
+++ b/drivers/net/phy/mdio_bus.c
@@ -102,11 +102,12 @@ struct mii_bus *mdiobus_alloc_size(size_t size)
 		alloc_size = sizeof(*bus);
 
 	bus = kzalloc(alloc_size, GFP_KERNEL);
-	if (bus) {
-		bus->state = MDIOBUS_ALLOCATED;
-		if (size)
-			bus->priv = (void *)bus + aligned_size;
-	}
+	if (!bus)
+		return NULL;
+
+	bus->state = MDIOBUS_ALLOCATED;
+	if (size)
+		bus->priv = (void *)bus + aligned_size;
 
 	/* Initialise the interrupts to polling */
 	for (i = 0; i < PHY_MAX_ADDR; i++)

[toc] | [next] | [standalone]


#1307433

FromAndrew Lunn <andrew@lunn.ch>
Date2016-01-12 15:10 +0100
Message-ID<qQ7O2-1Z5-17@gated-at.bofh.it>
In reply to#1307160
On Tue, Jan 12, 2016 at 12:34:36PM +0300, Dan Carpenter wrote:
> If bus = kzalloc() fails then we end up dereferencing bus when we do
> "bus->irq[i] = PHY_POLL;".  The code is a little simpler if we reverse
> the NULL check and return directly on failure.
> 
> Fixes: e7f4dc3536a4 ('mdio: Move allocation of interrupts into core')
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

Thanks
	Andrew

> 
> diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
> index 0be7b3d..0cba64f 100644
> --- a/drivers/net/phy/mdio_bus.c
> +++ b/drivers/net/phy/mdio_bus.c
> @@ -102,11 +102,12 @@ struct mii_bus *mdiobus_alloc_size(size_t size)
>  		alloc_size = sizeof(*bus);
>  
>  	bus = kzalloc(alloc_size, GFP_KERNEL);
> -	if (bus) {
> -		bus->state = MDIOBUS_ALLOCATED;
> -		if (size)
> -			bus->priv = (void *)bus + aligned_size;
> -	}
> +	if (!bus)
> +		return NULL;
> +
> +	bus->state = MDIOBUS_ALLOCATED;
> +	if (size)
> +		bus->priv = (void *)bus + aligned_size;
>  
>  	/* Initialise the interrupts to polling */
>  	for (i = 0; i < PHY_MAX_ADDR; i++)

[toc] | [prev] | [next] | [standalone]


#1307767

FromDavid Miller <davem@davemloft.net>
Date2016-01-12 20:40 +0100
Message-ID<qQcXo-5t4-21@gated-at.bofh.it>
In reply to#1307160
From: Dan Carpenter <dan.carpenter@oracle.com>
Date: Tue, 12 Jan 2016 12:34:36 +0300

> If bus = kzalloc() fails then we end up dereferencing bus when we do
> "bus->irq[i] = PHY_POLL;".  The code is a little simpler if we reverse
> the NULL check and return directly on failure.
> 
> Fixes: e7f4dc3536a4 ('mdio: Move allocation of interrupts into core')
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Applied.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web