Skip to contentSkip to conversation

net: mpls: fix stale pointer if allocation fails during device rename

PATCH Applied Jakub Kicinski 3 participants 5 messages Feb 14, 2023 – Feb 15, 2023

Subsystems (2)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 net: mpls: fix stale pointer if allocation fails during device rename

lianhui reports that when MPLS fails to register the sysctl table under new location (during device rename) the old pointers won't get overwritten and may be freed again (double free).

Handle this gracefully. The best option would be unregistering the MPLS from the device completely on failure, but unfortunately mpls_ifdown() can fail. So failing fully is also unreliable.

Another option is to register the new table first then only remove old one if the new one succeeds. That requires more code, changes order of notifications and two tables may be visible at the same time.

sysctl point is not used in the rest of the code - set to NULL on failures and skip unregister if already NULL.

Fixes 0fae3bf018d9 ("mpls: handle device renames for per-device sysctls")

---
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 net/mpls/af_mpls.c | 4 ++++
 1 file changed, 4 insertions(+)
net/mpls/af_mpls.c +4 -0
index 35b5f806fdda..dc5165d3eec4 100644
@@ -1428,6 +1428,7 @@ static int mpls_dev_sysctl_register(struct net_device *dev,
 free:
 	kfree(table);
 out:
+	mdev->sysctl = NULL;
 	return -ENOBUFS;
 }
 
@@ -1437,6 +1438,9 @@ static void mpls_dev_sysctl_unregister(struct net_device *dev,
 	struct net *net = dev_net(dev);
 	struct ctl_table *table;
 
+	if (!mdev->sysctl)
+		return;
+
 	table = mdev->sysctl->ctl_table_arg;
 	unregister_net_sysctl_table(mdev->sysctl);
 	kfree(table);
Signature · 1 line
2.39.1
#1 lore raw

Just be curious: would this be a simpler solution?

@@ -1439,6 +1439,7 @@ static void mpls_dev_sysctl_unregister(struct net_device *dev,

        table = mdev->sysctl->ctl_table_arg;
        unregister_net_sysctl_table(mdev->sysctl);
+       mdev->sysctl = NULL;
        kfree(table);

        mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);

However I'm not sure if we need to preserve the old value of mdev->sysctl after we unregister it.

Quoted 49 lines · Jakub Kicinski

lianhui reports that when MPLS fails to register the sysctl table under new location (during device rename) the old pointers won't get overwritten and may be freed again (double free).

Handle this gracefully. The best option would be unregistering the MPLS from the device completely on failure, but unfortunately mpls_ifdown() can fail. So failing fully is also unreliable.

Another option is to register the new table first then only remove old one if the new one succeeds. That requires more code, changes order of notifications and two tables may be visible at the same time.

sysctl point is not used in the rest of the code - set to NULL on failures and skip unregister if already NULL.

Fixes 0fae3bf018d9 ("mpls: handle device renames for per-device sysctls")

---
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 net/mpls/af_mpls.c | 4 ++++
 1 file changed, 4 insertions(+)
net/mpls/af_mpls.c +4 -0
index 35b5f806fdda..dc5165d3eec4 100644
@@ -1428,6 +1428,7 @@ static int mpls_dev_sysctl_register(struct net_device *dev,
 free:
 	kfree(table);
 out:
+	mdev->sysctl = NULL;
 	return -ENOBUFS;
 }
 
@@ -1437,6 +1438,9 @@ static void mpls_dev_sysctl_unregister(struct net_device *dev,
 	struct net *net = dev_net(dev);
 	struct ctl_table *table;
 
+	if (!mdev->sysctl)
+		return;
+
 	table = mdev->sysctl->ctl_table_arg;
 	unregister_net_sysctl_table(mdev->sysctl);
 	kfree(table);
author maintainer
#2 lore raw
Quoted 12 lines · On Tue, 14 Feb 2023 17:33:36 +0800 Gong Ruiqi wrote:

Just be curious: would this be a simpler solution?

net/mpls/af_mpls.c +1 -0
@@ -1439,6 +1439,7 @@ static void mpls_dev_sysctl_unregister(struct
 net_device *dev,
 
        table = mdev->sysctl->ctl_table_arg;
        unregister_net_sysctl_table(mdev->sysctl);
+       mdev->sysctl = NULL;
        kfree(table);
 
        mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
Quoted 2 lines

However I'm not sure if we need to preserve the old value of mdev->sysctl after we unregister it.

It'd work too, I decided to limit the zeroing to the exception case because of recent discussions on the list. The argument there was that zeroing in cases were we don't expect it to be necessary may hide bugs. We generally try to avoid defensive programming in the kernel.

#3 lore raw
Quoted 13 lines · Jakub Kicinski
Quoted 12 lines · On Tue, 14 Feb 2023 17:33:36 +0800 Gong Ruiqi wrote:

Just be curious: would this be a simpler solution?

net/mpls/af_mpls.c +1 -0
@@ -1439,6 +1439,7 @@ static void mpls_dev_sysctl_unregister(struct
 net_device *dev,
 
        table = mdev->sysctl->ctl_table_arg;
        unregister_net_sysctl_table(mdev->sysctl);
+       mdev->sysctl = NULL;
        kfree(table);
 
        mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
Quoted 7 lines
Quoted 2 lines

However I'm not sure if we need to preserve the old value of mdev->sysctl after we unregister it.

It'd work too, I decided to limit the zeroing to the exception case because of recent discussions on the list. The argument there was that zeroing in cases were we don't expect it to be necessary may hide bugs. We generally try to avoid defensive programming in the kernel.

Actually my original thought was not to do defensive programming, but to clearly mark it as invalid after its de-registration. Nevertheless "to avoid defensive programming in the kernel" is a good point :)

And oops, for my proposal the complete solution should be:

@@ -1437,8 +1437,12 @@ static void mpls_dev_sysctl_unregister(struct net_device *dev,

        struct net *net = dev_net(dev);
        struct ctl_table *table;

+ if (!mdev->sysctl)
+ return;
+

        table = mdev->sysctl->ctl_table_arg;
        unregister_net_sysctl_table(mdev->sysctl);
+       mdev->sysctl = NULL;
        kfree(table);

        mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);

to avoid NULL dereference at table = mdev->sysctl->... if we try to unregister the device after a failed renaming. Then it looks really similar with your patch xD. So yeah I'm ok with both of them.

#4 lore raw
In reply to Jakub Kicinski
This patch was applied to netdev/net.git (master) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <[email protected]>:

Quoted 9 lines · On Mon, 13 Feb 2023 22:53:55 -0800 you wrote:

lianhui reports that when MPLS fails to register the sysctl table under new location (during device rename) the old pointers won't get overwritten and may be freed again (double free).

Handle this gracefully. The best option would be unregistering the MPLS from the device completely on failure, but unfortunately mpls_ifdown() can fail. So failing fully is also unreliable.

[...]

Here is the summary with links:
- [net] net: mpls: fix stale pointer if allocation fails during device rename

    https://git.kernel.org/netdev/net/c/fda6c89fe3d9

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html