Skip to contentSkip to conversation

netfilter: nfnetlink: Fix for interrupted hook dumps

PATCH Superseded Phil Sutter 2 participants 6 messages Sep 4 – Sep 9

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH v3 1/1 netfilter: nfnetlink: Fix for interrupted hook dumps

Handling of concurrent hook changes with a dump in progress was problematic in nfnl_hook_dump and entirely broken in nfnl_hook_dump_nat. Address all issues in a single patch to please the review LLM.

Introduce sequence numbers in struct netns_nf to replace pointer value-based modification detection which may fail due to memory buffer reuse. This also eliminates the need for most manual cb->seq updates and excessive array index value checks.

Since nat hook updates are protected by a different mutex than others, they have to bump their own sequence number. nfnl_hook_dump_nat therefore open-codes what nl_dump_check_consistent does but bumps cb->seq instead of setting NLM_F_DUMP_INTR flag.

Dumps of many nat hooks was entirely broken since the array index was not (re)stored. Use cb->args[1] for that and make sure it is reset upon completion as the same dump may loop over multiple arrays of nat hooks.

In nfnl_hook_dump, don't signal NLM_F_DUMP_INTR if nfnl_hook_entries_head returns error: This is a permanent condition which does not change during a dump. It is already caught by nfnl_hook_dump_start though, so should not happen anyway.

In general, access ops array pointer values using READ_ONCE since they are assigned to using WRITE_ONCE.

Avoid setting NLM_F_DUMP_INTR flag in garbage memory by calling nl_dump_check_consistent only for non-empty skbs. If not a single netlink message was created, nfnetlink code will take care of setting the flag.

Fixes e2cf17d3774c ("netfilter: add new hook nfnl subsystem")
Fixes b010e2a4a9ac ("netfilter: nfnetlink_hook: Dump nat type chains")

---
Changes since v2:
- Bump seq after RCU-pointer assign in __nf_register_net_hook
- Fix coding style (braces)
- Rebase onto https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

Changes since v1:
- Reset cursor value and perform EINTR check if NAT priv->entries becomes NULL during dump.

---
 include/net/netns/netfilter.h  |  2 +
 net/netfilter/core.c           | 13 ++++++
 net/netfilter/nf_nat_core.c    | 11 +++++
 net/netfilter/nfnetlink_hook.c | 74 ++++++++++++++++++++--------------
 4 files changed, 69 insertions(+), 31 deletions(-)
include/net/netns/netfilter.h +2 -0
index a6a0bf4a247e..7fd78394d1e7 100644
@@ -33,5 +33,7 @@ struct netns_nf {
 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
 	unsigned int defrag_ipv6_users;
 #endif
+	unsigned int hook_base_seq;
+	unsigned int nat_hook_base_seq;
 };
 #endif
net/netfilter/core.c +13 -0
index 675a1034b340..d041f916c98b 100644
@@ -386,6 +386,15 @@ static void nf_static_key_dec(const struct nf_hook_ops *reg, int pf)
 #endif
 }
 
+static void bump_hook_base_seq(struct net *net)
+{
+	unsigned int base_seq = READ_ONCE(net->nf.hook_base_seq);
+
+	while (++base_seq == 0)
+		;
+	smp_store_release(&net->nf.hook_base_seq, base_seq);
+}
+
 static int __nf_register_net_hook(struct net *net, int pf,
 				  const struct nf_hook_ops *reg)
 {
@@ -430,6 +439,7 @@ static int __nf_register_net_hook(struct net *net, int pf,
 	if (!IS_ERR(new_hooks)) {
 		hooks_validate(new_hooks);
 		rcu_assign_pointer(*pp, new_hooks);
+		bump_hook_base_seq(net);
 	}
 
 	mutex_unlock(&nf_hook_mutex);
@@ -506,6 +516,7 @@ static void __nf_unregister_net_hook(struct net *net, int pf,
 			net_dec_egress_queue();
 #endif
 		nf_static_key_dec(reg, pf);
+		bump_hook_base_seq(net);
 	} else {
 		WARN_ONCE(1, "hook not found, pf %d num %d", pf, reg->hooknum);
 	}
@@ -784,6 +795,8 @@ static int __net_init netfilter_net_init(struct net *net)
 		return -ENOMEM;
 	}
 #endif
+	net->nf.hook_base_seq = 1;
+	net->nf.nat_hook_base_seq = 1;
 
 	return 0;
 }
net/netfilter/nf_nat_core.c +11 -0
index a4858c2b2d65..27d67a677aa4 100644
@@ -1160,6 +1160,15 @@ nfnetlink_parse_nat_setup(struct nf_conn *ct,
 }
 #endif
 
+static void bump_nat_hook_base_seq(struct net *net)
+{
+	unsigned int base_seq = READ_ONCE(net->nf.nat_hook_base_seq);
+
+	while (++base_seq == 0)
+		;
+	smp_store_release(&net->nf.nat_hook_base_seq, base_seq);
+}
+
 static struct nf_ct_helper_expectfn follow_master_nat = {
 	.name		= "nat-follow-master",
 	.expectfn	= nf_nat_follow_master,
@@ -1244,6 +1253,7 @@ int nf_nat_register_fn(struct net *net, u8 pf, const struct nf_hook_ops *ops,
 		nat_proto_net->nat_hook_ops = nat_ops;
 
 	nat_proto_net->users++;
+	bump_nat_hook_base_seq(net);
 
 	mutex_unlock(&nf_nat_proto_mutex);
 
@@ -1298,6 +1308,7 @@ void nf_nat_unregister_fn(struct net *net, u8 pf, const struct nf_hook_ops *ops,
 		goto unlock;
 	priv = nat_ops[hooknum].priv;
 	nf_hook_entries_delete_raw(&priv->entries, ops);
+	bump_nat_hook_base_seq(net);
 
 	if (nat_proto_net->users == 0) {
 		nf_unregister_net_hooks(net, nat_ops, ops_count);
net/netfilter/nfnetlink_hook.c +43 -31

122 lines not on this page · raw message

Signature · 1 line
2.54.0
#1 lore raw

Hi Phil,

Quoted 12 lines · Phil Sutter
net/netfilter/nfnetlink_hook.c +2 -4
@@ -338,27 +337,47 @@ nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de
 }
 
 static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
-			      const struct nfnl_dump_hook_data *ctx,
-			      const struct nf_hook_ops *ops,
-			      int family, unsigned int seq)
+			      struct netlink_callback *cb,
+			      const struct nf_hook_ops *ops, int family)
 {
 	struct nf_nat_lookup_hook_priv *priv = ops->priv;
-	struct nf_hook_entries *e = rcu_dereference(priv->entries);

net/netfilter/nfnetlink_hook.c nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de · line 352

Quoted 10 lines
net/netfilter/nfnetlink_hook.c +7 -1
+	struct nfnl_dump_hook_data *ctx = cb->data;
+	struct net *net = sock_net(nlskb->sk);
 	struct nf_hook_ops **nat_ops;
-	int i, err;
+	unsigned int i = cb->args[1];
+	struct nf_hook_entries *e;
+	unsigned int base_seq;
+	int err = 0;
 
+	base_seq = smp_load_acquire(&net->nf.nat_hook_base_seq);

Maybe annnotate this base sequence in the .start via:

struct netlink_dump_control c = {
        .start = ...;

We should probably start doing this in other nfnetlink subsystems too.

This will help catch an interference between two netlink recv() calls which results in calling netlink_dump() which calls this function.

Let me know, thanks!

author reviewer
#2 lore raw

Hi Pablo,

Quoted 26 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
net/netfilter/nfnetlink_hook.c +2 -4
@@ -338,27 +337,47 @@ nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de
 }
 
 static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
-			      const struct nfnl_dump_hook_data *ctx,
-			      const struct nf_hook_ops *ops,
-			      int family, unsigned int seq)
+			      struct netlink_callback *cb,
+			      const struct nf_hook_ops *ops, int family)
 {
 	struct nf_nat_lookup_hook_priv *priv = ops->priv;
-	struct nf_hook_entries *e = rcu_dereference(priv->entries);

net/netfilter/nfnetlink_hook.c nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de · line 352

Quoted 10 lines
net/netfilter/nfnetlink_hook.c +7 -1
+	struct nfnl_dump_hook_data *ctx = cb->data;
+	struct net *net = sock_net(nlskb->sk);
 	struct nf_hook_ops **nat_ops;
-	int i, err;
+	unsigned int i = cb->args[1];
+	struct nf_hook_entries *e;
+	unsigned int base_seq;
+	int err = 0;
 
+	base_seq = smp_load_acquire(&net->nf.nat_hook_base_seq);

Maybe annnotate this base sequence in the .start via:

Quoted 7 lines
struct netlink_dump_control c = {
        .start = ...;

We should probably start doing this in other nfnetlink subsystems too.

This will help catch an interference between two netlink recv() calls which results in calling netlink_dump() which calls this function.

I do not comprehend, sorry. The concurrent hook dumps have distinct cb buffers and net->nf.{nat_,}hook_base_seq is shared but read-only. How does the problematic interference happen?

Cheers, Phil

#3 lore raw
Quoted 32 lines · Phil Sutter

Hi Pablo,

Quoted 29 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
net/netfilter/nfnetlink_hook.c +2 -4
@@ -338,27 +337,47 @@ nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de
 }
 
 static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
-			      const struct nfnl_dump_hook_data *ctx,
-			      const struct nf_hook_ops *ops,
-			      int family, unsigned int seq)
+			      struct netlink_callback *cb,
+			      const struct nf_hook_ops *ops, int family)
 {
 	struct nf_nat_lookup_hook_priv *priv = ops->priv;
-	struct nf_hook_entries *e = rcu_dereference(priv->entries);

net/netfilter/nfnetlink_hook.c nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de · line 352

Quoted 10 lines
net/netfilter/nfnetlink_hook.c +7 -1
+	struct nfnl_dump_hook_data *ctx = cb->data;
+	struct net *net = sock_net(nlskb->sk);
 	struct nf_hook_ops **nat_ops;
-	int i, err;
+	unsigned int i = cb->args[1];
+	struct nf_hook_entries *e;
+	unsigned int base_seq;
+	int err = 0;
 
+	base_seq = smp_load_acquire(&net->nf.nat_hook_base_seq);

Maybe annnotate this base sequence in the .start via:

struct netlink_dump_control c = {
        .start = ...;
Quoted 8 lines
Quoted 4 lines

We should probably start doing this in other nfnetlink subsystems too.

This will help catch an interference between two netlink recv() calls which results in calling netlink_dump() which calls this function.

I do not comprehend, sorry. The concurrent hook dumps have distinct cb buffers and net->nf.{nat_,}hook_base_seq is shared but read-only. How does the problematic interference happen?

See nf_tables_dump_rules_start() for instance. It allocates the struct nft_rule_dump_ctx, which is reachable through .start, .dump and .done callbacks. I think it should be possible to annotate the current base_seq at the beginning of the netlink dump from .start in a ctx object. Then, use it from .dump to check if dump is consistent (ie. turn on the NLM_F_DUMP_INTR flag).

So, instead of fetching the current sequence from .dump like this:

        cb->seq = nft_base_seq(net);

Use the sequence available in the new ctx object, ie. from .dump path you do this:

        cb->seq = ctx->seq;

Because netlink_dump() is called for each userspace recv() call (netlink delivers the chunked listing in several messages), this would allow userspace to know that the listing is inconsistent, then optionally retry.

Makes sense to you?

author reviewer
#4 lore raw
Quoted 49 lines · Pablo Neira Ayuso
Quoted 32 lines · Phil Sutter

Hi Pablo,

Quoted 29 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
net/netfilter/nfnetlink_hook.c +2 -4
@@ -338,27 +337,47 @@ nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de
 }
 
 static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
-			      const struct nfnl_dump_hook_data *ctx,
-			      const struct nf_hook_ops *ops,
-			      int family, unsigned int seq)
+			      struct netlink_callback *cb,
+			      const struct nf_hook_ops *ops, int family)
 {
 	struct nf_nat_lookup_hook_priv *priv = ops->priv;
-	struct nf_hook_entries *e = rcu_dereference(priv->entries);

net/netfilter/nfnetlink_hook.c nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de · line 352

Quoted 10 lines
net/netfilter/nfnetlink_hook.c +7 -1
+	struct nfnl_dump_hook_data *ctx = cb->data;
+	struct net *net = sock_net(nlskb->sk);
 	struct nf_hook_ops **nat_ops;
-	int i, err;
+	unsigned int i = cb->args[1];
+	struct nf_hook_entries *e;
+	unsigned int base_seq;
+	int err = 0;
 
+	base_seq = smp_load_acquire(&net->nf.nat_hook_base_seq);

Maybe annnotate this base sequence in the .start via:

struct netlink_dump_control c = {
        .start = ...;
Quoted 8 lines
Quoted 4 lines

We should probably start doing this in other nfnetlink subsystems too.

This will help catch an interference between two netlink recv() calls which results in calling netlink_dump() which calls this function.

I do not comprehend, sorry. The concurrent hook dumps have distinct cb buffers and net->nf.{nat_,}hook_base_seq is shared but read-only. How does the problematic interference happen?

See nf_tables_dump_rules_start() for instance. It allocates the struct nft_rule_dump_ctx, which is reachable through .start, .dump and .done callbacks. I think it should be possible to annotate the current base_seq at the beginning of the netlink dump from .start in a ctx object. Then, use it from .dump to check if dump is consistent (ie. turn on the NLM_F_DUMP_INTR flag).

Quoted 8 lines

So, instead of fetching the current sequence from .dump like this:

        cb->seq = nft_base_seq(net);

Use the sequence available in the new ctx object, ie. from .dump path you do this:

        cb->seq = ctx->seq;

But to detect a concurrent hook update between to .dump callback calls (if skb space was exceeded), the current value in per-net data has to be fetched, no? So this would have to look like this in .dump callback:

| ctx->seq = nft_base_seq(net);
| cb->seq = ctx->seq;

Then I don't get the detour via struct nfnl_dump_hook_data. Or is it possible we get multiple concurrent dump requests on the same netlink socket and thus cb->seq (and cb->prev_seq) gets shared between them?

Quoted 6 lines

Because netlink_dump() is called for each userspace recv() call (netlink delivers the chunked listing in several messages), this would allow userspace to know that the listing is inconsistent, then optionally retry.

Makes sense to you?

Not quite, sorry.

#5 lore raw
Quoted 70 lines · Phil Sutter
Quoted 49 lines · Pablo Neira Ayuso
Quoted 32 lines · Phil Sutter

Hi Pablo,

Quoted 29 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
net/netfilter/nfnetlink_hook.c +2 -4
@@ -338,27 +337,47 @@ nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de
 }
 
 static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
-			      const struct nfnl_dump_hook_data *ctx,
-			      const struct nf_hook_ops *ops,
-			      int family, unsigned int seq)
+			      struct netlink_callback *cb,
+			      const struct nf_hook_ops *ops, int family)
 {
 	struct nf_nat_lookup_hook_priv *priv = ops->priv;
-	struct nf_hook_entries *e = rcu_dereference(priv->entries);

net/netfilter/nfnetlink_hook.c nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *de · line 352

Quoted 10 lines
net/netfilter/nfnetlink_hook.c +7 -1
+	struct nfnl_dump_hook_data *ctx = cb->data;
+	struct net *net = sock_net(nlskb->sk);
 	struct nf_hook_ops **nat_ops;
-	int i, err;
+	unsigned int i = cb->args[1];
+	struct nf_hook_entries *e;
+	unsigned int base_seq;
+	int err = 0;
 
+	base_seq = smp_load_acquire(&net->nf.nat_hook_base_seq);

Maybe annnotate this base sequence in the .start via:

struct netlink_dump_control c = {
        .start = ...;
Quoted 8 lines
Quoted 4 lines

We should probably start doing this in other nfnetlink subsystems too.

This will help catch an interference between two netlink recv() calls which results in calling netlink_dump() which calls this function.

I do not comprehend, sorry. The concurrent hook dumps have distinct cb buffers and net->nf.{nat_,}hook_base_seq is shared but read-only. How does the problematic interference happen?

See nf_tables_dump_rules_start() for instance. It allocates the struct nft_rule_dump_ctx, which is reachable through .start, .dump and .done callbacks. I think it should be possible to annotate the current base_seq at the beginning of the netlink dump from .start in a ctx object. Then, use it from .dump to check if dump is consistent (ie. turn on the NLM_F_DUMP_INTR flag).

Quoted 8 lines

So, instead of fetching the current sequence from .dump like this:

        cb->seq = nft_base_seq(net);

Use the sequence available in the new ctx object, ie. from .dump path you do this:

        cb->seq = ctx->seq;

But to detect a concurrent hook update between to .dump callback calls (if skb space was exceeded), the current value in per-net data has to be fetched, no? So this would have to look like this in .dump callback:

| ctx->seq = nft_base_seq(net);
| cb->seq = ctx->seq;

Then I don't get the detour via struct nfnl_dump_hook_data. Or is it possible we get multiple concurrent dump requests on the same netlink socket and thus cb->seq (and cb->prev_seq) gets shared between them?

Quoted 8 lines
Quoted 6 lines

Because netlink_dump() is called for each userspace recv() call (netlink delivers the chunked listing in several messages), this would allow userspace to know that the listing is inconsistent, then optionally retry.

Makes sense to you?

Not quite, sorry.

Right, I got confused by the LLM report.

Should be postpone bumping the seq also after array has been shrunk as LLM suggest in the remove path? For consistency with the insert operations, just bump sequence _after_ the datastructure update.