Skip to contentSkip to conversation

fix for potential NULL pointer dereference with bare lan743x

PATCH Superseded Sergej Bauer 3 participants 6 messages Oct 29, 2020 – Nov 1, 2020 1 message not in the archive

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix for potential NULL pointer dereference with bare lan743x

This is just a minor fix which prevents a kernel NULL pointer
dereference when using phy-less lan743x.

---
 drivers/net/ethernet/microchip/lan743x_ethtool.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/ethernet/microchip/lan743x_ethtool.c +6 -1
index dcde496da7fb..354d72d550f2 100644
@@ -780,7 +780,9 @@ static void lan743x_ethtool_get_wol(struct net_device *netdev,
 
 	wol->supported = 0;
 	wol->wolopts = 0;
-	phy_ethtool_get_wol(netdev->phydev, wol);
+
+	if (netdev->phydev)
+		phy_ethtool_get_wol(netdev->phydev, wol);
 
 	wol->supported |= WAKE_BCAST | WAKE_UCAST | WAKE_MCAST |
 		WAKE_MAGIC | WAKE_PHY | WAKE_ARP;
@@ -793,6 +795,9 @@ static int lan743x_ethtool_set_wol(struct net_device *netdev,
 {
 	struct lan743x_adapter *adapter = netdev_priv(netdev);
 
+	if (!netdev->phydev)
+		return -EIO;
+
 	adapter->wolopts = 0;
 	if (wol->wolopts & WAKE_UCAST)
 		adapter->wolopts |= WAKE_UCAST;
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 4 lines · On Thu, 29 Oct 2020 03:28:45 +0300 Sergej Bauer wrote:

This is just a minor fix which prevents a kernel NULL pointer
dereference when using phy-less lan743x.

I take you mean when the device is down netdev->phydev will be NULL?

drivers/net/ethernet/microchip/lan743x_ethtool.c static int lan743x_ethtool_set_wol(struct net_device *netdev, · line 799

Quoted 10 lines
drivers/net/ethernet/microchip/lan743x_ethtool.c +2 -0
index dcde496da7fb..354d72d550f2 100644
@@ -793,6 +795,9 @@ static int lan743x_ethtool_set_wol(struct net_device *netdev,
 {
 	struct lan743x_adapter *adapter = netdev_priv(netdev);
 
+	if (!netdev->phydev)
+		return -EIO;

Does it make sense to just skip the phy_ethtool_set_wol() call instead?

Also doesn't the wol configuration of the PHY get lost across an netdev up/down cycle in this driver? Should it be re-applied after phy is connected back?

Quoted 3 lines
drivers/net/ethernet/microchip/lan743x_ethtool.c +0 -0
 	adapter->wolopts = 0;
 	if (wol->wolopts & WAKE_UCAST)
 		adapter->wolopts |= WAKE_UCAST;
#2 lore raw

PATCH v2 1/1 fix for potential NULL pointer dereference with bare lan743x

---
 drivers/net/ethernet/microchip/lan743x_ethtool.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/net/ethernet/microchip/lan743x_ethtool.c +9 -3
index dcde496da7fb..ad38fc9e1468 100644
@@ -780,7 +780,9 @@ static void lan743x_ethtool_get_wol(struct net_device *netdev,
 
 	wol->supported = 0;
 	wol->wolopts = 0;
-	phy_ethtool_get_wol(netdev->phydev, wol);
+
+	if (netdev->phydev)
+		phy_ethtool_get_wol(netdev->phydev, wol);
 
 	wol->supported |= WAKE_BCAST | WAKE_UCAST | WAKE_MCAST |
 		WAKE_MAGIC | WAKE_PHY | WAKE_ARP;
@@ -792,6 +794,7 @@ static int lan743x_ethtool_set_wol(struct net_device *netdev,
 				   struct ethtool_wolinfo *wol)
 {
 	struct lan743x_adapter *adapter = netdev_priv(netdev);
+	int ret;
 
 	adapter->wolopts = 0;
 	if (wol->wolopts & WAKE_UCAST)
@@ -809,9 +812,12 @@ static int lan743x_ethtool_set_wol(struct net_device *netdev,
 
 	device_set_wakeup_enable(&adapter->pdev->dev, (bool)wol->wolopts);
 
-	phy_ethtool_set_wol(netdev->phydev, wol);
+	if (netdev->phydev)
+		ret = phy_ethtool_set_wol(netdev->phydev, wol);
+	else
+		ret = -EIO;
 
-	return 0;
+	return ret;
 }
 #endif /* CONFIG_PM */
 
Signature · 1 line
2.20.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Re: [PATCH v2] lan743x: Fix for potential null pointer dereference

Quoted 3 lines
Quoted 1 line

* I miss a change description here.

The reason for the fix is when the device is down netdev->phydev will be NULL and there is no checking for this situation. So 'ethtool ethN' leads to kernel panic.

$ sudo ethtool eth7
[  103.510336] BUG: kernel NULL pointer dereference, address: 0000000000000340
[  103.510454] #PF: supervisor read access in kernel mode
[  103.510530] #PF: error_code(0x0000) - not-present page
[  103.510600] PGD 0 P4D 0 
[  103.510635] Oops: 0000 [#1] SMP PTI
[  103.510675] CPU: 1 PID: 7182 Comm: ethtool Not tainted 5.9.0upstream+ #5
[  103.510737] Hardware name: Gigabyte Technology Co., Ltd. H110-D3/H110-D3-
CF, BIOS F24 04/11/2018
[  103.510836] RIP: 0010:phy_ethtool_get_wol+0x5/0x30 [libphy]
[  103.510892] Code: 00 48 85 c0 74 11 48 8b 80 40 01 00 00 48 85 c0 74 05 e9 
8e 7a 6f dd b8 a1 ff ff ff c3 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 <48> 8b 87 
40 03 00 00 48 85 c0 74 11 48 8b 80 48 01 00 00 48 85 c0
[  103.511054] RSP: 0018:ffffb6cd85123cf0 EFLAGS: 00010286
[  103.511106] RAX: ffffffffc03f0d00 RBX: ffffb6cd85123d90 RCX: ffffffff9e6fdd20
[  103.511171] RDX: 0000000000000001 RSI: ffffb6cd85123d90 RDI: 0000000000000000
[  103.511237] RBP: ffff946f811b4000 R08: 0000000000001000 R09: 0000000000000000
[  103.511302] R10: 0000000000000000 R11: 0000000000000089 R12: 
00007ffde92be040
[  103.511367] R13: 0000000000000005 R14: ffff946f811b4000 R15: 0000000000000000
[  103.511434] FS:  00007f54a9bc7740(0000) GS:ffff9470b6c80000(0000) knlGS:
0000000000000000
[  103.511508] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  103.511564] CR2: 0000000000000340 CR3: 000000011d366001 CR4: 
00000000003706e0
[  103.511629] Call Trace:
[  103.511666]  lan743x_ethtool_get_wol+0x21/0x40 [lan743x]
[  103.511724]  dev_ethtool+0x1507/0x29d0
[  103.511769]  ? avc_has_extended_perms+0x17f/0x440
[  103.511820]  ? tomoyo_init_request_info+0x84/0x90
[  103.511870]  ? tomoyo_path_number_perm+0x68/0x1e0
[  103.511919]  ? tty_insert_flip_string_fixed_flag+0x82/0xe0
[  103.511973]  ? inet_ioctl+0x187/0x1d0
[  103.512016]  dev_ioctl+0xb5/0x560
[  103.512055]  sock_do_ioctl+0xa0/0x140
[  103.512098]  sock_ioctl+0x2cb/0x3c0
[  103.512139]  __x64_sys_ioctl+0x84/0xc0
[  103.512183]  do_syscall_64+0x33/0x80
[  103.512224]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[  103.512274] RIP: 0033:0x7f54a9cba427
[  103.512313] Code: 00 00 90 48 8b 05 69 aa 0c 00 64 c7 00 26 00 00 00 48 c7 

c0 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 b8 10 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 39 aa 0c 00 f7 d8 64 89 01 48
...
---
So changes - is just to check a pointer for NULL;

Quoted 2 lines

* Should a prefix be specified in the patch subject?

as far as I understand subject should be "[PATCH v2] lan743x: fix for potential NULL pointer dereference with bare lan743x"?

ok, I've got it.

Quoted 26 lines

…

Quoted 1 line

+++ b/drivers/net/ethernet/microchip/lan743x_ethtool.c

…

Quoted 5 lines
drivers/net/ethernet/microchip/lan743x_ethtool.c +0 -1
@@ -809,9 +812,12 @@ static int lan743x_ethtool_set_wol(struct net_device
 *netdev,> 
 	device_set_wakeup_enable(&adapter->pdev->dev, (bool)wol->wolopts);
 
-	phy_ethtool_set_wol(netdev->phydev, wol);

drivers/net/ethernet/microchip/lan743x_ethtool.c static int lan743x_ethtool_set_wol(struct net_device · line 823

Quoted 10 lines
drivers/net/ethernet/microchip/lan743x_ethtool.c +5 -1
+	if (netdev->phydev)
+		ret = phy_ethtool_set_wol(netdev->phydev, wol);
+	else
+		ret = -EIO;
 
-	return 0;
+	return ret;
 
 }
 #endif /* CONFIG_PM */

How do you think about to use the following code variant?

Review

Quoted 1 line
(quoted diff) +1 -0
+	return netdev->phydev ? phy_ethtool_set_wol(netdev->phydev, wol) : -EIO;

It will be quite shorter, thanks.

Quoted 2 lines

Regards,
Markus

Regards.
Sergej.

#5 lore raw

Re: [PATCH v2] lan743x: Fix for potential null pointer dereference

Quoted 11 lines · Sergej Bauer
Quoted 3 lines
Quoted 1 line

* I miss a change description here.

The reason for the fix is when the device is down netdev->phydev will be NULL and there is no checking for this situation. So 'ethtool ethN' leads to kernel panic.

Quoted 4 lines
Quoted 4 lines
drivers/net/ethernet/microchip/lan743x_ethtool.c +0 -0
@@ -809,9 +812,12 @@ static int lan743x_ethtool_set_wol(struct net_device
 *netdev,> 
 	device_set_wakeup_enable(&adapter->pdev->dev, (bool)wol->wolopts);
 
Quoted 5 lines
Quoted 5 lines

drivers/net/ethernet/microchip/lan743x_ethtool.c static int lan743x_ethtool_set_wol(struct net_device *netdev, · line 818

Quoted 5 lines
drivers/net/ethernet/microchip/lan743x_ethtool.c +4 -1
-	phy_ethtool_set_wol(netdev->phydev, wol);
+	if (netdev->phydev)
+		ret = phy_ethtool_set_wol(netdev->phydev, wol);
+	else
+		ret = -EIO;

-ENETDOWN would be better, it gives a hit that WoL can be configured when the interface is configured up.

Andrew

#6 lore raw

Re: [PATCH v2] lan743x: Fix for potential null pointer dereference

Quoted 24 lines · MSK Andrew Lunn
Quoted 12 lines · Sergej Bauer
Quoted 3 lines
Quoted 1 line

* I miss a change description here.

The reason for the fix is when the device is down netdev->phydev will be NULL and there is no checking for this situation. So 'ethtool ethN' leads to kernel panic.

Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -809,9 +812,12 @@ static int lan743x_ethtool_set_wol(struct

net_device
*netdev,>

Quoted 7 lines
Quoted 7 lines

drivers/net/ethernet/microchip/lan743x_ethtool.c static int lan743x_ethtool_set_wol(struct net_device *netdev, · line 818

Quoted 7 lines
drivers/net/ethernet/microchip/lan743x_ethtool.c +4 -1
 	device_set_wakeup_enable(&adapter->pdev->dev, (bool)wol->wolopts);
 
-	phy_ethtool_set_wol(netdev->phydev, wol);
+	if (netdev->phydev)
+		ret = phy_ethtool_set_wol(netdev->phydev, wol);
+	else
+		ret = -EIO;

-ENETDOWN would be better, it gives a hit that WoL can be configured when the interface is configured up.

Quoted 1 line

Andrew

Ok, thank you, Andrew! I was doubted in the correctness of returning -EIO.

Regards,
Sergej.