Re: [PATCH v2] net: tun: Fix use-after-free in tun_detach()

From: Eric Dumazet
Date: Sun Nov 20 2022 - 19:54:36 EST


On Sun, Nov 20, 2022 at 4:34 PM Hillf Danton <hdanton@xxxxxxxx> wrote:
>
> On 20 Nov 2022 08:04:13 -0800 Eric Dumazet <edumazet@xxxxxxxxxx>
> > On Sun, Nov 20, 2022 at 2:49 AM Hillf Danton <hdanton@xxxxxxxx> wrote:
> > > On 20 Nov 2022 18:02:13 +0900 Shigeru Yoshida <syoshida@xxxxxxxxxx>
> > > >
> > > > This patch fixes the issue by calling sock_put() from tun_detach()
> > > > after all necessary accesses for the struct net has done.
> > >
> > > Thanks for your fix.
> > >
> > > But tun is not special wrt netdev_run_todo() and call_netdevice_notifiers(),
> > > so the correct fix should be making netdev grab another hold on net and
> > > invoking put_net() in the path of netdev_run_todo().
> >
> > Well, this is not going to work. Unless I am missing something.
>
> Thanks for taking a look.
>
> I mean bump up refcount for net when updating netdev->nd_net in a bid to
> make dev_net() safe throught netdev's life span.

This would prevent netns deletion, as the following sequence would
then no longer work as intended.

ip netns add foo
ip netns add ip link set lo up
ip netns del foo

When a netns is deleted ("ip netns del" and no more refcounted sockets),
we have callbacks to unregister all devices tied to it.