Skip to contentSkip to conversation

mdio_bus: NULL dereference on allocation error

PATCH Applied Dan Carpenter 3 participants 3 messages Jan 12, 2016

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

PATCH 1/1 mdio_bus: NULL dereference on allocation error

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')
drivers/net/phy/mdio_bus.c +6 -5
index 0be7b3d..0cba64f 100644
@@ -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++)
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Dan Carpenter

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

Thanks
Andrew

Quoted 23 lines
drivers/net/phy/mdio_bus.c +6 -5
index 0be7b3d..0cba64f 100644
@@ -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++)
maintainer Applied
#2 lore raw
In reply to Dan Carpenter

From: Dan Carpenter <[email protected]>
Date: Tue, 12 Jan 2016 12:34:36 +0300

Quoted 6 lines

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

Applied.