netfilter: bpf: must hold reference on net namespace
authorFlorian Westphal <fw@strlen.de>
Thu, 10 Oct 2024 16:34:05 +0000 (18:34 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Thu, 17 Oct 2024 11:58:57 +0000 (13:58 +0200)
BUG: KASAN: slab-use-after-free in __nf_unregister_net_hook+0x640/0x6b0
Read of size 8 at addr ffff8880106fe400 by task repro/72=
bpf_nf_link_release+0xda/0x1e0
bpf_link_free+0x139/0x2d0
bpf_link_release+0x68/0x80
__fput+0x414/0xb60

Eric says:
 It seems that bpf was able to defer the __nf_unregister_net_hook()
 after exit()/close() time.
 Perhaps a netns reference is missing, because the netns has been
 dismantled/freed already.
 bpf_nf_link_attach() does :
 link->net = net;
 But I do not see a reference being taken on net.

Add such a reference and release it after hook unreg.
Note that I was unable to get syzbot reproducer to work, so I
do not know if this resolves this splat.

Fixes: 84601d6ee68a ("bpf: add bpf_link support for BPF_NETFILTER programs")
Diagnosed-by: Eric Dumazet <edumazet@google.com>
Reported-by: Lai, Yi <yi1.lai@linux.intel.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
net/netfilter/nf_bpf_link.c

index 5257d5e..e5e79a0 100644 (file)
@@ -23,6 +23,7 @@ static unsigned int nf_hook_run_bpf(void *bpf_prog, struct sk_buff *skb,
 struct bpf_nf_link {
        struct bpf_link link;
        struct nf_hook_ops hook_ops;
+       netns_tracker ns_tracker;
        struct net *net;
        u32 dead;
        const struct nf_defrag_hook *defrag_hook;
@@ -120,6 +121,7 @@ static void bpf_nf_link_release(struct bpf_link *link)
        if (!cmpxchg(&nf_link->dead, 0, 1)) {
                nf_unregister_net_hook(nf_link->net, &nf_link->hook_ops);
                bpf_nf_disable_defrag(nf_link);
+               put_net_track(nf_link->net, &nf_link->ns_tracker);
        }
 }
 
@@ -257,6 +259,8 @@ int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
                return err;
        }
 
+       get_net_track(net, &link->ns_tracker, GFP_KERNEL);
+
        return bpf_link_settle(&link_primer);
 }