All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net] net: mpls: fix stale pointer if allocation fails during device rename
@ 2023-02-14  6:53 Jakub Kicinski
  2023-02-14  9:33 ` Gong Ruiqi
  2023-02-15 10:30 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 5+ messages in thread
From: Jakub Kicinski @ 2023-02-14  6:53 UTC (permalink / raw)
  To: davem
  Cc: netdev, edumazet, pabeni, Jakub Kicinski, lianhui tang, kuniyu,
	gongruiqi1, rshearma

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.

Reported-by: lianhui tang <bluetlh@gmail.com>
Fixes: 0fae3bf018d9 ("mpls: handle device renames for per-device sysctls")
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: kuniyu@amazon.co.jp
CC: gongruiqi1@huawei.com
CC: rshearma@brocade.com
---
 net/mpls/af_mpls.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/net/mpls/af_mpls.c b/net/mpls/af_mpls.c
index 35b5f806fdda..dc5165d3eec4 100644
--- a/net/mpls/af_mpls.c
+++ b/net/mpls/af_mpls.c
@@ -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);
-- 
2.39.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: mpls: fix stale pointer if allocation fails during device rename
  2023-02-14  6:53 [PATCH net] net: mpls: fix stale pointer if allocation fails during device rename Jakub Kicinski
@ 2023-02-14  9:33 ` Gong Ruiqi
  2023-02-14 21:23   ` Jakub Kicinski
  2023-02-15 10:30 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 5+ messages in thread
From: Gong Ruiqi @ 2023-02-14  9:33 UTC (permalink / raw)
  To: Jakub Kicinski, davem
  Cc: netdev, edumazet, pabeni, lianhui tang, kuniyu, rshearma

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.


On 2023/02/14 14:53, Jakub Kicinski 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.
> 
> 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.
> 
> Reported-by: lianhui tang <bluetlh@gmail.com>
> Fixes: 0fae3bf018d9 ("mpls: handle device renames for per-device sysctls")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
> CC: kuniyu@amazon.co.jp
> CC: gongruiqi1@huawei.com
> CC: rshearma@brocade.com
> ---
>  net/mpls/af_mpls.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/net/mpls/af_mpls.c b/net/mpls/af_mpls.c
> index 35b5f806fdda..dc5165d3eec4 100644
> --- a/net/mpls/af_mpls.c
> +++ b/net/mpls/af_mpls.c
> @@ -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);

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: mpls: fix stale pointer if allocation fails during device rename
  2023-02-14  9:33 ` Gong Ruiqi
@ 2023-02-14 21:23   ` Jakub Kicinski
  2023-02-15  6:29     ` Gong Ruiqi
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2023-02-14 21:23 UTC (permalink / raw)
  To: Gong Ruiqi
  Cc: davem, netdev, edumazet, pabeni, lianhui tang, kuniyu, rshearma

On Tue, 14 Feb 2023 17:33:36 +0800 Gong Ruiqi wrote:
> 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.

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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: mpls: fix stale pointer if allocation fails during device rename
  2023-02-14 21:23   ` Jakub Kicinski
@ 2023-02-15  6:29     ` Gong Ruiqi
  0 siblings, 0 replies; 5+ messages in thread
From: Gong Ruiqi @ 2023-02-15  6:29 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, lianhui tang, kuniyu, rshearma



On 2023/02/15 5:23, Jakub Kicinski wrote:
> On Tue, 14 Feb 2023 17:33:36 +0800 Gong Ruiqi wrote:
>> 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.
> 
> 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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: mpls: fix stale pointer if allocation fails during device rename
  2023-02-14  6:53 [PATCH net] net: mpls: fix stale pointer if allocation fails during device rename Jakub Kicinski
  2023-02-14  9:33 ` Gong Ruiqi
@ 2023-02-15 10:30 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-02-15 10:30 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, bluetlh, kuniyu, gongruiqi1, rshearma

Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-02-15 10:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-14  6:53 [PATCH net] net: mpls: fix stale pointer if allocation fails during device rename Jakub Kicinski
2023-02-14  9:33 ` Gong Ruiqi
2023-02-14 21:23   ` Jakub Kicinski
2023-02-15  6:29     ` Gong Ruiqi
2023-02-15 10:30 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.