Skip to contentSkip to conversation

Address Sashiko review of NAT hook dump code

PATCH Superseded Phil Sutter 1 participant 6 messages Jul 9

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

v2 cover Address Sashiko review of NAT hook dump code

Changes since v1:
- New patch 1
- Fixed three aspects of patch 3
- Fixed two issues of patch 5

Patch 1 fixes a bug (actually, two) in old code. Latter patches benefit from the fix it implements.

Patch 2 is mere preparation to add the missing case handling to nfnl_hook_dump_nat().

Patch 3 fixes for missing READ_ONCE() calls when addressing hook ops array elements.

Patch 4 adds missing multipart dump support to nfnl_hook_dump_nat.

Patch 5 adds code to detect and mitigate concurrent hook updates while amidst a multipart dump to nfnl_hook_dump_nat.

Phil Sutter (5):
netfilter: nfnetlink_hook: Fix for EINTR due to index too large
netfilter: nfnetlink_hook: Pass cb object to nfnl_hook_dump_nat()
netfilter: nfnetlink_hook: Address hook ops using READ_ONCE()
netfilter: nfnetlink_hook: Handle multipart NAT hook dumps
netfilter: nfnetlink_hook: Fix for concurrent NAT hooks dump and
change

 net/netfilter/nfnetlink_hook.c | 75 +++++++++++++++++++++++-----------
 1 file changed, 52 insertions(+), 23 deletions(-)
Signature · 1 line
2.54.0
author reviewer
#1 lore raw

PATCH v2 1/5 netfilter: nfnetlink_hook: Fix for EINTR due to index too large

When resuming a multipart dump and e->num_hook_entries has become lower than the stored index, nfnl_hook_dump_one is not called and therefore nl_dump_check_consistent will be passed an invalid nlmsghdr pointer.

Detect the situation and put an empty message to carry NLM_F_DUMP_INTR flag.

This will help fix for concurrent NAT hook changes while dumping as well since nfnl_hook_dump_nat will also abort before putting a single message in that case.

The old code did assign NLM_F_DUMP_INTR to garbage in (or beyond) nlskb with uninterrupted dumps, too: The mandatory last call saw 'i == e->num_hook_entries' and therefore incremented cb->seq. This patch makes this a non-error (and the function will just return 0 without further action).

Fixes e2cf17d3774c ("netfilter: add new hook nfnl subsystem")
---
 net/netfilter/nfnetlink_hook.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
net/netfilter/nfnetlink_hook.c +18 -9
index 95005e9a6066..755d8f148db3 100644
@@ -188,13 +188,21 @@ static int nfnl_hook_put_nft_ft_info(struct sk_buff *nlskb,
 	return 0;
 }
 
+static struct nlmsghdr *nfnl_put_get_hook_msg(struct sk_buff *nlskb,
+					      unsigned int seq, int family)
+{
+	u16 event = nfnl_msg_type(NFNL_SUBSYS_HOOK, NFNL_MSG_HOOK_GET);
+	unsigned int portid = NETLINK_CB(nlskb).portid;
+
+	return nfnl_msg_put(nlskb, portid, seq, event,
+			   NLM_F_MULTI, family, NFNETLINK_V0, 0);
+}
+
 static int nfnl_hook_dump_one(struct sk_buff *nlskb,
 			      const struct nfnl_dump_hook_data *ctx,
 			      const struct nf_hook_ops *ops, int priority,
 			      int family, unsigned int seq)
 {
-	u16 event = nfnl_msg_type(NFNL_SUBSYS_HOOK, NFNL_MSG_HOOK_GET);
-	unsigned int portid = NETLINK_CB(nlskb).portid;
 	struct nlmsghdr *nlh;
 	int ret = -EMSGSIZE;
 	u32 hooknum;
@@ -202,8 +210,7 @@ static int nfnl_hook_dump_one(struct sk_buff *nlskb,
 	char sym[KSYM_SYMBOL_LEN];
 	char *module_name;
 #endif
-	nlh = nfnl_msg_put(nlskb, portid, seq, event,
-			   NLM_F_MULTI, family, NFNETLINK_V0, 0);
+	nlh = nfnl_put_get_hook_msg(nlskb, seq, family);
 	if (!nlh)
 		goto nla_put_failure;
 
@@ -366,7 +373,7 @@ static int nfnl_hook_dump(struct sk_buff *nlskb,
 {
 	struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
 	struct nfnl_dump_hook_data *ctx = cb->data;
-	int err, family = nfmsg->nfgen_family;
+	int err = 0, family = nfmsg->nfgen_family;
 	struct net *net = sock_net(nlskb->sk);
 	struct nf_hook_ops * const *ops;
 	const struct nf_hook_entries *e;
@@ -378,14 +385,14 @@ static int nfnl_hook_dump(struct sk_buff *nlskb,
 	if (!e)
 		goto done;
 
-	if (IS_ERR(e)) {
+	if (IS_ERR(e) ||
+	    i > e->num_hook_entries ||
+	    (unsigned long)e != ctx->headv) {
 		cb->seq++;
+		err = -EINTR;
 		goto done;
 	}
 
-	if ((unsigned long)e != ctx->headv || i >= e->num_hook_entries)
-		cb->seq++;
-
 	ops = nf_hook_entries_get_hook_ops(e);
 
 	for (; i < e->num_hook_entries; i++) {
@@ -401,6 +408,8 @@ static int nfnl_hook_dump(struct sk_buff *nlskb,
 	}
 
 done:
+	if (err && !nlskb->len)
+		nfnl_put_get_hook_msg(nlskb, cb->nlh->nlmsg_seq, family);
 	nl_dump_check_consistent(cb, nlmsg_hdr(nlskb));
 	rcu_read_unlock();
 	cb->args[0] = i;
Signature · 1 line
2.54.0
author reviewer
#2 lore raw

PATCH v2 2/5 netfilter: nfnetlink_hook: Pass cb object to nfnl_hook_dump_nat()

In reply to Phil Sutter

Preparing for proper multipart dump handling, pass the object reference directly instead of individual fields. Keep passing the 'family' field though so caller does not have to extract that value from netlink header again.

---
 net/netfilter/nfnetlink_hook.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
net/netfilter/nfnetlink_hook.c +6 -6
index 755d8f148db3..b0cf0ec41bc5 100644
@@ -345,12 +345,12 @@ 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);
+	struct nfnl_dump_hook_data *ctx = cb->data;
 	struct nf_hook_ops **nat_ops;
 	int i, err;
 
@@ -361,7 +361,8 @@ static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
 
 	for (i = 0; i < e->num_hook_entries; i++) {
 		err = nfnl_hook_dump_one(nlskb, ctx, nat_ops[i],
-					 ops->priority, family, seq);
+					 ops->priority, family,
+					 cb->nlh->nlmsg_seq);
 		if (err)
 			return err;
 	}
@@ -397,8 +398,7 @@ static int nfnl_hook_dump(struct sk_buff *nlskb,
 
 	for (; i < e->num_hook_entries; i++) {
 		if (ops[i]->hook_ops_type == NF_HOOK_OP_NAT)
-			err = nfnl_hook_dump_nat(nlskb, ctx, ops[i], family,
-						 cb->nlh->nlmsg_seq);
+			err = nfnl_hook_dump_nat(nlskb, cb, ops[i], family);
 		else
 			err = nfnl_hook_dump_one(nlskb, ctx, ops[i],
 						 ops[i]->priority, family,
Signature · 1 line
2.54.0
author reviewer
#3 lore raw

PATCH v2 3/5 netfilter: nfnetlink_hook: Address hook ops using READ_ONCE()

In reply to Phil Sutter

Writer (nf_remove_net_hook) assigns to the field value using WRITE_ONCE(), appropriately call READ_ONCE() to make sure reader (nfnl_hook_dump) sees either the old or new value, not both.

Fixes b010e2a4a9ac ("netfilter: nfnetlink_hook: Dump nat type chains")

---
Changes since v1:
- Cover for previously missed ops[i]->priority parameter
- Also cover for nat_ops field access, adjust patch subject
- Declare temporary variable as const

---
 net/netfilter/nfnetlink_hook.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
net/netfilter/nfnetlink_hook.c +8 -5
index b0cf0ec41bc5..95cc9a0cee20 100644
@@ -360,7 +360,8 @@ static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
 	nat_ops = nf_hook_entries_get_hook_ops(e);
 
 	for (i = 0; i < e->num_hook_entries; i++) {
-		err = nfnl_hook_dump_one(nlskb, ctx, nat_ops[i],
+		err = nfnl_hook_dump_one(nlskb, ctx,
+					 READ_ONCE(nat_ops[i]),
 					 ops->priority, family,
 					 cb->nlh->nlmsg_seq);
 		if (err)
@@ -397,11 +398,13 @@ static int nfnl_hook_dump(struct sk_buff *nlskb,
 	ops = nf_hook_entries_get_hook_ops(e);
 
 	for (; i < e->num_hook_entries; i++) {
-		if (ops[i]->hook_ops_type == NF_HOOK_OP_NAT)
-			err = nfnl_hook_dump_nat(nlskb, cb, ops[i], family);
+		const struct nf_hook_ops *cur = READ_ONCE(ops[i]);
+
+		if (cur->hook_ops_type == NF_HOOK_OP_NAT)
+			err = nfnl_hook_dump_nat(nlskb, cb, cur, family);
 		else
-			err = nfnl_hook_dump_one(nlskb, ctx, ops[i],
-						 ops[i]->priority, family,
+			err = nfnl_hook_dump_one(nlskb, ctx, cur,
+						 cur->priority, family,
 						 cb->nlh->nlmsg_seq);
 		if (err)
 			break;
Signature · 1 line
2.54.0
author reviewer
#4 lore raw

PATCH v2 4/5 netfilter: nfnetlink_hook: Handle multipart NAT hook dumps

In reply to Phil Sutter

If the number of hooks exceeds available sk_buff space, the function is called again for the remaining data. Make use of the second netlink_callback::args field to store the current index between invocations. Since this is an inner dump loop, it may run multiple times (for different hook types) with the same context buffer, so manually zero the stored value if complete array dump was possible.

Fixes b010e2a4a9ac ("netfilter: nfnetlink_hook: Dump nat type chains")
---
 net/netfilter/nfnetlink_hook.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
net/netfilter/nfnetlink_hook.c +9 -4
index 95cc9a0cee20..e3b5d21eeb2e 100644
@@ -352,22 +352,27 @@ static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
 	struct nf_hook_entries *e = rcu_dereference(priv->entries);
 	struct nfnl_dump_hook_data *ctx = cb->data;
 	struct nf_hook_ops **nat_ops;
-	int i, err;
+	unsigned int i = cb->args[1];
+	int err = 0;
 
 	if (!e)
 		return 0;
 
 	nat_ops = nf_hook_entries_get_hook_ops(e);
 
-	for (i = 0; i < e->num_hook_entries; i++) {
+	for (; i < e->num_hook_entries; i++) {
 		err = nfnl_hook_dump_one(nlskb, ctx,
 					 READ_ONCE(nat_ops[i]),
 					 ops->priority, family,
 					 cb->nlh->nlmsg_seq);
 		if (err)
-			return err;
+			break;
 	}
-	return 0;
+
+	if (!err)
+		i = 0;
+	cb->args[1] = i;
+	return err;
 }
 
 static int nfnl_hook_dump(struct sk_buff *nlskb,
Signature · 1 line
2.54.0
author reviewer
#5 lore raw

PATCH v2 5/5 netfilter: nfnetlink_hook: Fix for concurrent NAT hooks dump and change

In reply to Phil Sutter

If hooks are added or deleted, the nf_hook_entries buffer is reallocated and the RCU-protected pointer updated. If a dump requires multiple parts (exceeding sk_buff space) the second invocation may see a different nf_hook_entries pointer than the first one. Detect this just like the outer nfnl_hook_dump function does, by storing the pointer value in context. If it changes, signal user space it must restart.

As with the stored loop counter, that stored pointer value must be manually zeroed upon successful loop completion since this inner function may run multiple times for different nf_hook_ops but with same context.

Fixes b010e2a4a9ac ("netfilter: nfnetlink_hook: Dump nat type chains")

---
Changes since v1:
- Fix a stupid typo introduced last minute when resolving checkpatch.pl complaints
- If 'e' becomes zero, it also indicates an interrupted dump

---
 net/netfilter/nfnetlink_hook.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
net/netfilter/nfnetlink_hook.c +13 -1
index e3b5d21eeb2e..95873c7a524f 100644
@@ -55,6 +55,7 @@ static int nf_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
 struct nfnl_dump_hook_data {
 	char devname[IFNAMSIZ];
 	unsigned long headv;
+	unsigned long natv;
 	u8 hook;
 };
 
@@ -355,6 +356,15 @@ static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
 	unsigned int i = cb->args[1];
 	int err = 0;
 
+	if (!ctx->natv)
+		ctx->natv = (unsigned long)e;
+
+	if ((e && i >= e->num_hook_entries) ||
+	    ctx->natv != (unsigned long)e) {
+		cb->seq++;
+		return -EINTR;
+	}
+
 	if (!e)
 		return 0;
 
@@ -369,8 +379,10 @@ static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
 			break;
 	}
 
-	if (!err)
+	if (!err) {
+		ctx->natv = 0;
 		i = 0;
+	}
 	cb->args[1] = i;
 	return err;
 }
Signature · 1 line
2.54.0