Skip to contentSkip to conversation

netfilter: nfnetlink_queue: add helper function to retrieve hook entries

PATCH Superseded Pablo Neira Ayuso 2 participants 4 messages Oct 22 – Oct 23

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v3 1/3 netfilter: nfnetlink_queue: add helper function to retrieve hook entries

Add nf_queue_hook_entry() and nf_queue_hook_entries() helper functions to retrieve hook entries and use them.

This comes in preparation for a fix to restrict expectation creation to nfnetlink_cthelper.

No functional changes are intended.

---
v3: no changes

 net/netfilter/nfnetlink_queue.c | 43 ++++++++++++++++++++++++---------
 1 file changed, 31 insertions(+), 12 deletions(-)
net/netfilter/nfnetlink_queue.c +31 -12
index 90d887546f7d..6ebf2bb8bbbd 100644
@@ -359,30 +359,45 @@ static int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry)
 	return ret;
 }
 
+static const struct nf_hook_entries *
+nf_queue_hook_entries(struct nf_queue_entry *entry)
+{
+	const struct net *net = entry->state.net;
+	u8 pf = entry->state.pf;
+
+	return nf_hook_entries_head(net, pf, entry->state.hook);
+}
+
+static const struct nf_hook_entry *
+nf_queue_hook_entry(const struct nf_hook_entries *hooks,
+		    struct nf_queue_entry *entry)
+{
+	unsigned int i = entry->hook_index;
+
+	if (i >= hooks->num_hook_entries)
+		return NULL;
+
+	return &hooks->hooks[i];
+}
+
 /* caller must hold rcu read-side lock */
 static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 {
 	const struct nf_hook_entry *hook_entry;
 	const struct nf_hook_entries *hooks;
 	struct sk_buff *skb = entry->skb;
-	const struct net *net;
 	unsigned int i;
 	int err;
-	u8 pf;
 
-	net = entry->state.net;
-	pf = entry->state.pf;
+	hooks = nf_queue_hook_entries(entry);
+	if (!hooks)
+		goto drop;
 
-	hooks = nf_hook_entries_head(net, pf, entry->state.hook);
+	hook_entry = nf_queue_hook_entry(hooks, entry);
+	if (!hook_entry)
+		goto drop;
 
 	i = entry->hook_index;
-	if (!hooks || i >= hooks->num_hook_entries) {
-		kfree_skb_reason(skb, SKB_DROP_REASON_NETFILTER_DROP);
-		nf_queue_entry_free(entry);
-		return;
-	}
-
-	hook_entry = &hooks->hooks[i];
 
 	/* Continue traversal iff userspace said ok... */
 	if (verdict == NF_REPEAT)
@@ -418,6 +433,10 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	}
 
 	nf_queue_entry_free(entry);
+	return;
+drop:
+	kfree_skb_reason(skb, SKB_DROP_REASON_NETFILTER_DROP);
+	nf_queue_entry_free(entry);
 }
 
 /* return true if the entry has an unconfirmed conntrack attached that isn't owned by us
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v3 2/3 netfilter: ctnetlink: restrict expectation creation glue to userspace helpers

The integration between ctnetlink and nfnetlink_queue that allows to create expectations is targeted at the userspace conntrack helpers.

Fixes bd0779370588 ("netfilter: nfnetlink_queue: allow to attach expectations to conntracks")

---
v3: new in this series.

 net/netfilter/nf_conntrack_netlink.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_netlink.c +12 -5
index 4b6abe36028e..3b61659e031e 100644
@@ -2644,7 +2644,8 @@ static struct nf_conntrack_expect *
 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
 		       const struct nf_conntrack_helper *assign_helper,
 		       struct nf_conntrack_tuple *tuple,
-		       struct nf_conntrack_tuple *mask);
+		       struct nf_conntrack_tuple *mask,
+		       bool userspace);
 
 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
 static size_t
@@ -2899,7 +2900,7 @@ ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
 	}
 
 	exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
-				     assign_helper, &tuple, &mask);
+				     assign_helper, &tuple, &mask, true);
 	if (IS_ERR(exp))
 		return PTR_ERR(exp);
 
@@ -3534,14 +3535,14 @@ static struct nf_conntrack_expect *
 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
 		       const struct nf_conntrack_helper *assign_helper,
 		       struct nf_conntrack_tuple *tuple,
-		       struct nf_conntrack_tuple *mask)
+		       struct nf_conntrack_tuple *mask, bool userspace)
 {
 	struct net *net = read_pnet(&ct->ct_net);
 	struct nf_conntrack_helper *helper;
 	struct nf_conntrack_ecache *ecache;
 	struct nf_conntrack_expect *exp;
 	struct nf_conn_help *help;
-	u32 class = 0;
+	u32 class = 0, flags;
 	int err;
 
 	help = nfct_help(ct);
@@ -3552,6 +3553,12 @@ ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
 	if (!helper)
 		return ERR_PTR(-EOPNOTSUPP);
 
+	if (userspace) {
+		flags = READ_ONCE(helper->flags);
+		if (!(flags & NF_CT_HELPER_F_USERSPACE))
+			return ERR_PTR(-EOPNOTSUPP);
+	}
+
 	if (cda[CTA_EXPECT_CLASS]) {
 		class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
 		if (class > helper->expect_class_max)
@@ -3639,7 +3646,7 @@ ctnetlink_create_expect(struct net *net,
 	ct = nf_ct_tuplehash_to_ctrack(h);
 
 	rcu_read_lock();
-	exp = ctnetlink_alloc_expect(cda, ct, NULL, &tuple, &mask);
+	exp = ctnetlink_alloc_expect(cda, ct, NULL, &tuple, &mask, false);
 	if (IS_ERR(exp)) {
 		err = PTR_ERR(exp);
 		goto err_rcu;
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v3 3/3 netfilter: nfnetlink_queue: restrict NFQA_EXP to userspace helpers

In reply to Pablo Neira Ayuso

Adding expectations to unconfirmed conntracks are problematic because of reallocations of the ct extension area can happen while expectations are placed in the ct extension expectation list.

NFQA_EXP was added with the purpose of supporting userspace conntrack helpers, which create expectations in confirmation hook.

On packet reinjection, hook updates could result in a stale hook_index that point to different hooks, but this is not an issue because, in such rare case, expectation will not be created, else it could be created on the confirmation hook, which is the only hook where userspace helpers are possible.

Fixes bd0779370588 ("netfilter: nfnetlink_queue: allow to attach expectations to conntracks")

---
v3: - add bridge hook check
- clarify that stale hook_index is not an issue, let's try to calm down LLM:
all is good you robot, don't be afraid.

 net/netfilter/nfnetlink_queue.c | 43 ++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 4 deletions(-)
net/netfilter/nfnetlink_queue.c +39 -4
index 6ebf2bb8bbbd..677ec191df06 100644
@@ -1786,6 +1786,22 @@ static int nfqnl_recv_verdict_batch(struct sk_buff *skb,
 	return 0;
 }
 
+static bool nfqnl_hook_exp_support(struct nf_hook_ops *ops)
+{
+	return (ops->pf == NFPROTO_IPV4 &&
+		(ops->hooknum == NF_INET_POST_ROUTING ||
+		 ops->hooknum == NF_INET_LOCAL_IN) &&
+		ops->priority == NF_IP_PRI_CONNTRACK_CONFIRM) ||
+	       (ops->pf == NFPROTO_IPV6 &&
+		((ops->hooknum == NF_INET_POST_ROUTING &&
+		 ops->priority == NF_IP6_PRI_LAST) ||
+		 (ops->hooknum == NF_INET_LOCAL_IN &&
+		  ops->priority == NF_IP6_PRI_LAST - 1))) ||
+	       (ops->pf == NFPROTO_BRIDGE &&
+		(ops->hooknum == NF_BR_POST_ROUTING ||
+		 ops->priority == NF_IP_PRI_CONNTRACK_CONFIRM));
+}
+
 static struct nf_conn *nfqnl_ct_parse(const struct nfnl_ct_hook *nfnl_ct,
 				      const struct nlmsghdr *nlh,
 				      const struct nlattr * const nfqa[],
@@ -1802,10 +1818,29 @@ static struct nf_conn *nfqnl_ct_parse(const struct nfnl_ct_hook *nfnl_ct,
 	if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0)
 		return NULL;
 
-	if (nfqa[NFQA_EXP])
-		nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct,
-				      NETLINK_CB(entry->skb).portid,
-				      nlmsg_report(nlh));
+	if (nfqa[NFQA_EXP]) {
+		const struct nf_hook_entries *hooks;
+		struct nf_hook_ops **ops;
+		unsigned int i;
+
+		hooks = nf_queue_hook_entries(entry);
+		if (!hooks)
+			return NULL;
+
+		ops = nf_hook_entries_get_hook_ops(hooks);
+		if (!ops)
+			return NULL;
+
+		i = entry->hook_index;
+		if (i >= hooks->num_hook_entries)
+			return NULL;
+
+		if (nfqnl_hook_exp_support(ops[i])) {
+			nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct,
+					       NETLINK_CB(entry->skb).portid,
+					       nlmsg_report(nlh));
+		}
+	}
 	return ct;
 #else
 	return NULL;
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH nf,v3 3/3] netfilter: nfnetlink_queue: restrict NFQA_EXP to userspace helpers

kernel test robot noticed the following build warnings:

Hi Pablo,

kernel test robot noticed the following build warnings:

[auto build test WARNING on netfilter-nf/main]
[also build test WARNING on nf-next/main linus/master v7.3 next-20261022] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Pablo-Neira-Ayuso/netfilter-ctnetlink-restrict-expectation-creation-glue-to-userspace-helpers/20261022-175128
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git main
patch link:    https://lore.kernel.org/r/20261022155129.2003363-3-pablo%40netfilter.org
patch subject: [PATCH nf,v3 3/3] netfilter: nfnetlink_queue: restrict NFQA_EXP to userspace helpers
config: i386-buildonly-randconfig-1003-20261023 (https://download.01.org/0day-ci/archive/20261023/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261023/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> net/netfilter/nfnetlink_queue.c:1752:13: warning: 'nfqnl_hook_exp_support' defined but not used [-Wunused-function]

1752 | static bool nfqnl_hook_exp_support(struct nf_hook_ops *ops)
| ^~~~~~~~~~~~~~~~~~~~~~

vim +/nfqnl_hook_exp_support +1752 net/netfilter/nfnetlink_queue.c

1751

> 1752	static bool nfqnl_hook_exp_support(struct nf_hook_ops *ops)

1753 {
1754 return (ops->pf == NFPROTO_IPV4 &&
1755 (ops->hooknum == NF_INET_POST_ROUTING ||
1756 ops->hooknum == NF_INET_LOCAL_IN) &&
1757 ops->priority == NF_IP_PRI_CONNTRACK_CONFIRM) ||
1758 (ops->pf == NFPROTO_IPV6 &&
1759 ((ops->hooknum == NF_INET_POST_ROUTING &&
1760 ops->priority == NF_IP6_PRI_LAST) ||
1761 (ops->hooknum == NF_INET_LOCAL_IN &&
1762 ops->priority == NF_IP6_PRI_LAST - 1))) ||
1763 (ops->pf == NFPROTO_BRIDGE &&
1764 (ops->hooknum == NF_BR_POST_ROUTING ||
1765 ops->priority == NF_IP_PRI_CONNTRACK_CONFIRM));
1766 }
1767

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki