Skip to contentSkip to conversation

netfilter: nfnetlink_queue: restrict NFQA_EXP to userspace helpers

PATCH In review Pablo Neira Ayuso 2 participants 3 messages Oct 22 – Oct 28

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

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

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 check for bridge
- expand commit description to try to calm down LLM, stale hook_index is not an issue in this case.

 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
author maintainer
#1 lore raw
Quoted 4 lines · 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.

Quoted 8 lines

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.

I forgot to send two dependency patches, scratch this, I'm resending.

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

---
v3: - add check for bridge
- expand commit description to try to calm down LLM, stale hook_index is not an issue in this case.

 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
#2 lore raw
In reply to Pablo Neira Ayuso
kernel test robot noticed the following build errors:

Hi Pablo,

kernel test robot noticed the following build errors:

[auto build test ERROR on netfilter-nf/main]
[also build test ERROR on nf-next/main linus/master v7.3 next-20261027] [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-nfnetlink_queue-restrict-NFQA_EXP-to-userspace-helpers/20261022-174644
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git main
patch link:    https://lore.kernel.org/r/20261022154644.2002766-1-pablo%40netfilter.org
patch subject: [PATCH nf,v3] netfilter: nfnetlink_queue: restrict NFQA_EXP to userspace helpers
config: x86_64-rhel-9.4-func (https://download.01.org/0day-ci/archive/20261029/[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/20261029/[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 errors (new ones prefixed by >>):

net/netfilter/nfnetlink_queue.c: In function 'nfqnl_ct_parse':

>> net/netfilter/nfnetlink_queue.c:1770:25: error: implicit declaration of function 'nf_queue_hook_entries' [-Wimplicit-function-declaration]
    1770 |                 hooks = nf_queue_hook_entries(entry);
         |                         ^~~~~~~~~~~~~~~~~~~~~
>> net/netfilter/nfnetlink_queue.c:1770:23: error: assignment to 'const struct nf_hook_entries *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
    1770 |                 hooks = nf_queue_hook_entries(entry);
         |                       ^

vim +/nf_queue_hook_entries +1770 net/netfilter/nfnetlink_queue.c

1748
1749 static struct nf_conn *nfqnl_ct_parse(const struct nfnl_ct_hook *nfnl_ct,

  1750					      const struct nlmsghdr *nlh,
  1751					      const struct nlattr * const nfqa[],
  1752					      struct nf_queue_entry *entry,
  1753					      enum ip_conntrack_info *ctinfo)
  1754	{
  1755	#if IS_ENABLED(CONFIG_NF_CONNTRACK)
  1756		struct nf_conn *ct;
  1757	
  1758		ct = nf_ct_get(entry->skb, ctinfo);
  1759		if (ct == NULL)
  1760			return NULL;
  1761	
  1762		if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0)
  1763			return NULL;
  1764	
  1765		if (nfqa[NFQA_EXP]) {
  1766			const struct nf_hook_entries *hooks;
  1767			struct nf_hook_ops **ops;
  1768			unsigned int i;
  1769	
> 1770			hooks = nf_queue_hook_entries(entry);

1771 if (!hooks)
1772 return NULL;
1773
1774 ops = nf_hook_entries_get_hook_ops(hooks);
1775 if (!ops)
1776 return NULL;
1777
1778 i = entry->hook_index;
1779 if (i >= hooks->num_hook_entries)
1780 return NULL;
1781
1782 if (nfqnl_hook_exp_support(ops[i])) {
1783 nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct,
1784 NETLINK_CB(entry->skb).portid,
1785 nlmsg_report(nlh));
1786 }
1787 }
1788 return ct;
1789 #else
1790 return NULL;
1791 #endif
1792 }
1793

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