[PATCH bpf-next v4 3/6] netfilter: bpf: Prevent defrag module unload while link active

From: Daniel Xu
Date: Wed Jul 12 2023 - 19:44:49 EST


While in practice we could handle the module being unloaded while a
netfilter link (that requested defrag) was active, it's a better user
experience to prevent the defrag module from going away. It would
violate user expectations if fragmented packets started showing up if
some other part of the system tried to unload defrag module.

Reviewed-by: Florian Westphal <fw@xxxxxxxxx>
Signed-off-by: Daniel Xu <dxu@xxxxxxxxx>
---
include/linux/netfilter.h | 3 +++
net/ipv4/netfilter/nf_defrag_ipv4.c | 1 +
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 1 +
net/netfilter/nf_bpf_link.c | 25 +++++++++++++++++++++--
4 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/include/linux/netfilter.h b/include/linux/netfilter.h
index 77a637b681f2..a160dc1e23bf 100644
--- a/include/linux/netfilter.h
+++ b/include/linux/netfilter.h
@@ -11,6 +11,7 @@
#include <linux/wait.h>
#include <linux/list.h>
#include <linux/static_key.h>
+#include <linux/module.h>
#include <linux/netfilter_defs.h>
#include <linux/netdevice.h>
#include <linux/sockptr.h>
@@ -482,12 +483,14 @@ struct nfnl_ct_hook {
extern const struct nfnl_ct_hook __rcu *nfnl_ct_hook;

struct nf_defrag_v4_hook {
+ struct module *owner;
int (*enable)(struct net *net);
void (*disable)(struct net *net);
};
extern const struct nf_defrag_v4_hook __rcu *nf_defrag_v4_hook;

struct nf_defrag_v6_hook {
+ struct module *owner;
int (*enable)(struct net *net);
void (*disable)(struct net *net);
};
diff --git a/net/ipv4/netfilter/nf_defrag_ipv4.c b/net/ipv4/netfilter/nf_defrag_ipv4.c
index 1f3e0e893b7a..fb133bf3131d 100644
--- a/net/ipv4/netfilter/nf_defrag_ipv4.c
+++ b/net/ipv4/netfilter/nf_defrag_ipv4.c
@@ -115,6 +115,7 @@ static void __net_exit defrag4_net_exit(struct net *net)
}

static const struct nf_defrag_v4_hook defrag_hook = {
+ .owner = THIS_MODULE,
.enable = nf_defrag_ipv4_enable,
.disable = nf_defrag_ipv4_disable,
};
diff --git a/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c b/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
index f7c7ee31c472..29d31721c9c0 100644
--- a/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
+++ b/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
@@ -98,6 +98,7 @@ static void __net_exit defrag6_net_exit(struct net *net)
}

static const struct nf_defrag_v6_hook defrag_hook = {
+ .owner = THIS_MODULE,
.enable = nf_defrag_ipv6_enable,
.disable = nf_defrag_ipv6_disable,
};
diff --git a/net/netfilter/nf_bpf_link.c b/net/netfilter/nf_bpf_link.c
index 5b72aa246577..77ffbf26ba3d 100644
--- a/net/netfilter/nf_bpf_link.c
+++ b/net/netfilter/nf_bpf_link.c
@@ -2,6 +2,7 @@
#include <linux/bpf.h>
#include <linux/filter.h>
#include <linux/kmod.h>
+#include <linux/module.h>
#include <linux/netfilter.h>

#include <net/netfilter/nf_bpf_link.h>
@@ -53,7 +54,15 @@ static int bpf_nf_enable_defrag(struct bpf_nf_link *link)
}
}

+ /* Prevent defrag module from going away while in use */
+ if (!try_module_get(v4_hook->owner)) {
+ err = -ENOENT;
+ goto out_v4;
+ }
+
err = v4_hook->enable(link->net);
+ if (err)
+ module_put(v4_hook->owner);
out_v4:
rcu_read_unlock();
return err;
@@ -77,7 +86,15 @@ static int bpf_nf_enable_defrag(struct bpf_nf_link *link)
}
}

+ /* Prevent defrag module from going away while in use */
+ if (!try_module_get(v6_hook->owner)) {
+ err = -ENOENT;
+ goto out_v6;
+ }
+
err = v6_hook->enable(link->net);
+ if (err)
+ module_put(v6_hook->owner);
out_v6:
rcu_read_unlock();
return err;
@@ -97,8 +114,10 @@ static void bpf_nf_disable_defrag(struct bpf_nf_link *link)
case NFPROTO_IPV4:
rcu_read_lock();
v4_hook = rcu_dereference(nf_defrag_v4_hook);
- if (v4_hook)
+ if (v4_hook) {
v4_hook->disable(link->net);
+ module_put(v4_hook->owner);
+ }
rcu_read_unlock();

break;
@@ -107,8 +126,10 @@ static void bpf_nf_disable_defrag(struct bpf_nf_link *link)
case NFPROTO_IPV6:
rcu_read_lock();
v6_hook = rcu_dereference(nf_defrag_v6_hook);
- if (v6_hook)
+ if (v6_hook) {
v6_hook->disable(link->net);
+ module_put(v6_hook->owner);
+ }
rcu_read_unlock();

break;
--
2.41.0