Skip to contentSkip to conversation

net: phy: sfp: Prevent NULL deference of socket_ops

PATCH In review Florian Fainelli 2 participants 4 messages Sep 14, 2018 – Sep 17, 2018

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

PATCH 1/1 net: phy: sfp: Prevent NULL deference of socket_ops

In case we have specified a SFP and an I2C phandle in the Device Tree, but we somehow failed to look up the I2C adapter (e.g: the driver is not enabled), we will leave dangling socket_ops, and the sfp_bus will still have been registered. We can then observe NULL pointer dereferences while doing ethtool -m:

[   20.218613] Unable to handle kernel NULL pointer dereference at virtual address 00000008
[   20.226977] pgd = (ptrval)
[   20.229785] [00000008] *pgd=7c400831, *pte=00000000, *ppte=00000000
[   20.236274] Internal error: Oops: 17 [#1] SMP ARM
[   20.241122] Modules linked in:
[   20.244278] CPU: 0 PID: 1480 Comm: ethtool Not tainted 4.19.0-rc3 #138
[   20.251013] Hardware name: Broadcom Northstar Plus SoC
[   20.256316] PC is at sfp_get_module_info+0x8/0x10
[   20.261172] LR is at dev_ethtool+0x218c/0x2afc

Specifically guard against that.

Fixes ce0aa27ff3f6 ("sfp: add sfp-bus to bridge between network devices and sfp cages")
---
 drivers/net/phy/sfp-bus.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/phy/sfp-bus.c +6 -0
index 740655261e5b..9465be501372 100644
@@ -378,6 +378,9 @@ static void sfp_unregister_bus(struct sfp_bus *bus)
  */
 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
 {
+	if (!bus->socket_ops)
+		return -EOPNOTSUPP;
+
 	return bus->socket_ops->module_info(bus->sfp, modinfo);
 }
 EXPORT_SYMBOL_GPL(sfp_get_module_info);
@@ -396,6 +399,9 @@ EXPORT_SYMBOL_GPL(sfp_get_module_info);
 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
 			  u8 *data)
 {
+	if (!bus->socket_ops)
+		return -EOPNOTSUPP;
+
 	return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
 }
 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 16 lines · Florian Fainelli

In case we have specified a SFP and an I2C phandle in the Device Tree, but we somehow failed to look up the I2C adapter (e.g: the driver is not enabled), we will leave dangling socket_ops, and the sfp_bus will still have been registered. We can then observe NULL pointer dereferences while doing ethtool -m:

[   20.218613] Unable to handle kernel NULL pointer dereference at virtual address 00000008
[   20.226977] pgd = (ptrval)
[   20.229785] [00000008] *pgd=7c400831, *pte=00000000, *ppte=00000000
[   20.236274] Internal error: Oops: 17 [#1] SMP ARM
[   20.241122] Modules linked in:
[   20.244278] CPU: 0 PID: 1480 Comm: ethtool Not tainted 4.19.0-rc3 #138
[   20.251013] Hardware name: Broadcom Northstar Plus SoC
[   20.256316] PC is at sfp_get_module_info+0x8/0x10
[   20.261172] LR is at dev_ethtool+0x218c/0x2afc
Quoted 1 line

Specifically guard against that.

The other alternative to this would be to only set ndev->sfp_bus when the bus moves to registered state, which would probably be a saner alternative than to add an additional layer of tests. IOW:

 drivers/net/phy/sfp-bus.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/phy/sfp-bus.c +2 -2
index 48ca980f5c96..c4c2b5269216 100644
@@ -362,6 +362,7 @@ static int sfp_register_bus(struct sfp_bus *bus)
 	}
 	if (bus->started)
 		bus->socket_ops->start(bus->sfp);
+	bus->netdev->sfp_bus = bus;
 	bus->registered = true;
 	return 0;
 }
@@ -370,6 +371,7 @@ static void sfp_unregister_bus(struct sfp_bus *bus)
 {
 	const struct sfp_upstream_ops *ops = bus->upstream_ops;
 
+	bus->netdev->sfp_bus = NULL;
 	if (bus->registered) {
 		if (bus->started)
 			bus->socket_ops->stop(bus->sfp);
@@ -451,7 +453,6 @@ static void sfp_upstream_clear(struct sfp_bus *bus)
 {
 	bus->upstream_ops = NULL;
 	bus->upstream = NULL;
-	bus->netdev->sfp_bus = NULL;
 	bus->netdev = NULL;
 }
 
@@ -480,7 +481,6 @@ struct sfp_bus *sfp_register_upstream(struct fwnode_handle *fwnode,
 		bus->upstream_ops = ops;
 		bus->upstream = upstream;
 		bus->netdev = ndev;
-		ndev->sfp_bus = bus;
 
 		if (bus->sfp) {
 			ret = sfp_register_bus(bus);
Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 13.8Mbps down 630kbps up
According to speedtest.net: 13Mbps down 490kbps up
author Tested-by
#2 lore raw
Quoted 17 lines · Russell King - ARM Linux
Quoted 16 lines · Florian Fainelli

In case we have specified a SFP and an I2C phandle in the Device Tree, but we somehow failed to look up the I2C adapter (e.g: the driver is not enabled), we will leave dangling socket_ops, and the sfp_bus will still have been registered. We can then observe NULL pointer dereferences while doing ethtool -m:

[   20.218613] Unable to handle kernel NULL pointer dereference at virtual address 00000008
[   20.226977] pgd = (ptrval)
[   20.229785] [00000008] *pgd=7c400831, *pte=00000000, *ppte=00000000
[   20.236274] Internal error: Oops: 17 [#1] SMP ARM
[   20.241122] Modules linked in:
[   20.244278] CPU: 0 PID: 1480 Comm: ethtool Not tainted 4.19.0-rc3 #138
[   20.251013] Hardware name: Broadcom Northstar Plus SoC
[   20.256316] PC is at sfp_get_module_info+0x8/0x10
[   20.261172] LR is at dev_ethtool+0x218c/0x2afc
Quoted 5 lines
Quoted 1 line

Specifically guard against that.

The other alternative to this would be to only set ndev->sfp_bus when the bus moves to registered state, which would probably be a saner alternative than to add an additional layer of tests. IOW:

Indeed, your patch works for me:

Quoted 41 lines
 drivers/net/phy/sfp-bus.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/phy/sfp-bus.c +2 -2
index 48ca980f5c96..c4c2b5269216 100644
@@ -362,6 +362,7 @@ static int sfp_register_bus(struct sfp_bus *bus)
 	}
 	if (bus->started)
 		bus->socket_ops->start(bus->sfp);
+	bus->netdev->sfp_bus = bus;
 	bus->registered = true;
 	return 0;
 }
@@ -370,6 +371,7 @@ static void sfp_unregister_bus(struct sfp_bus *bus)
 {
 	const struct sfp_upstream_ops *ops = bus->upstream_ops;
 
+	bus->netdev->sfp_bus = NULL;
 	if (bus->registered) {
 		if (bus->started)
 			bus->socket_ops->stop(bus->sfp);
@@ -451,7 +453,6 @@ static void sfp_upstream_clear(struct sfp_bus *bus)
 {
 	bus->upstream_ops = NULL;
 	bus->upstream = NULL;
-	bus->netdev->sfp_bus = NULL;
 	bus->netdev = NULL;
 }
 
@@ -480,7 +481,6 @@ struct sfp_bus *sfp_register_upstream(struct fwnode_handle *fwnode,
 		bus->upstream_ops = ops;
 		bus->upstream = upstream;
 		bus->netdev = ndev;
-		ndev->sfp_bus = bus;
 
 		if (bus->sfp) {
 			ret = sfp_register_bus(bus);
Signature · 1 line
Florian
#3 lore raw
Quoted 20 lines · Florian Fainelli
Quoted 19 lines · Russell King - ARM Linux
Quoted 17 lines · Florian Fainelli

In case we have specified a SFP and an I2C phandle in the Device Tree, but we somehow failed to look up the I2C adapter (e.g: the driver is not enabled), we will leave dangling socket_ops, and the sfp_bus will still have been registered. We can then observe NULL pointer dereferences while doing ethtool -m:

[   20.218613] Unable to handle kernel NULL pointer dereference at virtual address 00000008
[   20.226977] pgd = (ptrval)
[   20.229785] [00000008] *pgd=7c400831, *pte=00000000, *ppte=00000000
[   20.236274] Internal error: Oops: 17 [#1] SMP ARM
[   20.241122] Modules linked in:
[   20.244278] CPU: 0 PID: 1480 Comm: ethtool Not tainted 4.19.0-rc3 #138
[   20.251013] Hardware name: Broadcom Northstar Plus SoC
[   20.256316] PC is at sfp_get_module_info+0x8/0x10
[   20.261172] LR is at dev_ethtool+0x218c/0x2afc

Specifically guard against that.

Quoted 7 lines
Quoted 3 lines

The other alternative to this would be to only set ndev->sfp_bus when the bus moves to registered state, which would probably be a saner alternative than to add an additional layer of tests. IOW:

Indeed, your patch works for me:

Thanks, I'll re-send it properly for davem tomorrow.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 13.8Mbps down 630kbps up
According to speedtest.net: 13Mbps down 490kbps up